compile.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ;;;; Copyright (C) 1999, 2001, 2006 Free Software Foundation, Inc.
  2. ;;;;
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 2.1 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;;;
  17. (define-module (oop goops compile)
  18. :use-module (oop goops)
  19. :use-module (oop goops util)
  20. :export (compute-cmethod compute-entry-with-cmethod
  21. compile-method cmethod-code cmethod-environment)
  22. :no-backtrace
  23. )
  24. (define source-formals cadr)
  25. (define source-body cddr)
  26. (define cmethod-code cdr)
  27. (define cmethod-environment car)
  28. ;;;
  29. ;;; Method entries
  30. ;;;
  31. (define code-table-lookup
  32. (letrec ((check-entry (lambda (entry types)
  33. (if (null? types)
  34. (and (not (struct? (car entry)))
  35. entry)
  36. (and (eq? (car entry) (car types))
  37. (check-entry (cdr entry) (cdr types)))))))
  38. (lambda (code-table types)
  39. (cond ((null? code-table) #f)
  40. ((check-entry (car code-table) types)
  41. => (lambda (cmethod)
  42. (cons (car code-table) cmethod)))
  43. (else (code-table-lookup (cdr code-table) types))))))
  44. (define (compute-entry-with-cmethod methods types)
  45. (or (code-table-lookup (slot-ref (car methods) 'code-table) types)
  46. (let* ((method (car methods))
  47. (place-holder (list #f))
  48. (entry (append types place-holder)))
  49. ;; In order to handle recursion nicely, put the entry
  50. ;; into the code-table before compiling the method
  51. (slot-set! (car methods) 'code-table
  52. (cons entry (slot-ref (car methods) 'code-table)))
  53. (let ((cmethod (compile-method methods types)))
  54. (set-car! place-holder (car cmethod))
  55. (set-cdr! place-holder (cdr cmethod)))
  56. (cons entry place-holder))))
  57. (define (compute-cmethod methods types)
  58. (cdr (compute-entry-with-cmethod methods types)))
  59. ;;;
  60. ;;; Next methods
  61. ;;;
  62. ;;; Temporary solution---return #f if x doesn't refer to `next-method'.
  63. (define (next-method? x)
  64. (and (pair? x)
  65. (or (eq? (car x) 'next-method)
  66. (next-method? (car x))
  67. (next-method? (cdr x)))))
  68. (define (make-final-make-next-method method)
  69. (lambda default-args
  70. (lambda args
  71. (@apply method (if (null? args) default-args args)))))
  72. (define (make-final-make-no-next-method gf)
  73. (lambda default-args
  74. (lambda args
  75. (no-next-method gf (if (null? args) default-args args)))))
  76. (define (make-make-next-method vcell gf methods types)
  77. (lambda default-args
  78. (lambda args
  79. (if (null? methods)
  80. (begin
  81. (set-cdr! vcell (make-final-make-no-next-method gf))
  82. (no-next-method gf (if (null? args) default-args args)))
  83. (let* ((cmethod (compute-cmethod methods types))
  84. (method (local-eval (cons 'lambda (cmethod-code cmethod))
  85. (cmethod-environment cmethod))))
  86. (set-cdr! vcell (make-final-make-next-method method))
  87. (@apply method (if (null? args) default-args args)))))))
  88. ;;;
  89. ;;; Method compilation
  90. ;;;
  91. ;;; NOTE: This section is far from finished. It will finally be
  92. ;;; implemented on C level.
  93. (define %tag-body
  94. (nested-ref the-root-module '(app modules oop goops %tag-body)))
  95. (define (compile-method methods types)
  96. (let* ((proc (method-procedure (car methods)))
  97. ;; XXX - procedure-source can not be guaranteed to be
  98. ;; reliable or efficient
  99. (src (procedure-source proc))
  100. (formals (source-formals src))
  101. (body (source-body src)))
  102. (if (next-method? body)
  103. (let ((vcell (cons 'goops:make-next-method #f)))
  104. (set-cdr! vcell
  105. (make-make-next-method
  106. vcell
  107. (method-generic-function (car methods))
  108. (cdr methods) types))
  109. ;;*fixme*
  110. `(,(cons vcell (procedure-environment proc))
  111. ,formals
  112. ;;*fixme* Only do this on source where next-method can't be inlined
  113. (let ((next-method ,(if (list? formals)
  114. `(goops:make-next-method ,@formals)
  115. `(apply goops:make-next-method
  116. ,@(improper->proper formals)))))
  117. ,@body)))
  118. (cons (procedure-environment proc)
  119. (cons formals
  120. (%tag-body body)))
  121. )))