binding.scm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ;;; Ported from Scheme 48 1.9. See file COPYING for notices and license.
  2. ;;;
  3. ;;; Port Author: Andrew Whatson
  4. ;;;
  5. ;;; Original Authors: Richard Kelsey, Jonathan Rees, Mike Sperber
  6. ;;;
  7. ;;; scheme48-1.9.2/scheme/bcomp/binding.scm
  8. (define-module (prescheme bcomp binding)
  9. #:use-module (srfi srfi-9)
  10. #:use-module (prescheme scheme48)
  11. #:use-module (prescheme bcomp mtype)
  12. #:use-module (prescheme locations)
  13. #:use-module (prescheme record-discloser)
  14. #:export (binding?
  15. make-binding
  16. binding-place
  17. set-binding-place! ;;for package mutation, used in package.scm
  18. binding-static
  19. set-binding-static! ;; for letrec-syntax
  20. binding-type
  21. clobber-binding!
  22. maybe-fix-place!
  23. forget-integration
  24. impose-type
  25. same-denotation?))
  26. ;; Bindings: used to store bindings in packages.
  27. ;;
  28. ;; Representation is type place operator-or-transform-or-#f.
  29. ;; PLACE is a unique (to EQ?) value, usually a location.
  30. (define-record-type :binding
  31. (really-make-binding type place static)
  32. binding?
  33. (type binding-type set-binding-type!)
  34. (place binding-place set-binding-place!)
  35. (static binding-static set-binding-static!))
  36. (define-record-discloser :binding
  37. (lambda (b)
  38. (list 'binding
  39. (binding-type b)
  40. (binding-place b)
  41. (binding-static b))))
  42. (define (make-binding type place static)
  43. (really-make-binding type place static))
  44. ;; Used when updating a package binding.
  45. (define (clobber-binding! binding type place static)
  46. (set-binding-type! binding type)
  47. (if place
  48. (set-binding-place! binding place))
  49. (set-binding-static! binding static))
  50. ;; Return a binding that's similar to the given one, but has its type
  51. ;; replaced with the given type.
  52. (define (impose-type type binding integrate?)
  53. (if (or (eq? type syntax-type)
  54. (not (binding? binding)))
  55. binding
  56. (make-binding (if (eq? type undeclared-type)
  57. (let ((type (binding-type binding)))
  58. (if (variable-type? type)
  59. (variable-value-type type)
  60. type))
  61. type)
  62. (binding-place binding)
  63. (if integrate?
  64. (binding-static binding)
  65. #f))))
  66. ;; Return a binding that's similar to the given one, but has any
  67. ;; procedure integration or other unnecesary static information
  68. ;; removed. But don't remove static information for macros (or
  69. ;; structures, interfaces, etc.)
  70. (define (forget-integration binding)
  71. (if (and (binding-static binding)
  72. (subtype? (binding-type binding) any-values-type))
  73. (make-binding (binding-type binding)
  74. (binding-place binding)
  75. #f)
  76. binding))
  77. ;; Do X and Y denote the same thing?
  78. (define (same-denotation? x y)
  79. (or (eq? x y) ;; was EQUAL? because of names, now just for nodes
  80. (and (binding? x)
  81. (binding? y)
  82. (eq? (binding-place x)
  83. (binding-place y)))))
  84. ;; Special kludge for shadowing and package mutation.
  85. ;; Ignore this on first reading. See env/shadow.scm.
  86. (define (maybe-fix-place! binding)
  87. (let ((place (binding-place binding)))
  88. (if (and (location? place)
  89. (vector? (location-id place)))
  90. (set-binding-place! binding (follow-forwarding-pointers place))))
  91. binding)
  92. (define (follow-forwarding-pointers place)
  93. (let ((id (location-id place)))
  94. (if (vector? id)
  95. (follow-forwarding-pointers (vector-ref id 0))
  96. place)))