interface.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees, Mike Sperber
  3. ; Interfaces
  4. ;
  5. ; An interface has four fields:
  6. ; - A procedure for looking up names in the interface. The procedure
  7. ; returns a base name and a type. If the name is not exported the
  8. ; base name is #F.
  9. ; - A procedure for walking over the declarations in the interfaces.
  10. ; The name, base-name, and type of each exported name are passed
  11. ; to the action procedure.
  12. ; - A population containing the structures that export this interface and
  13. ; any compound or modified interfaces that build on it. This population
  14. ; is used for propogating changes when an interface or structure is
  15. ; redefined.
  16. ; - A name for debugging.
  17. (define-record-type interface :interface
  18. (really-make-interface ref walk clients name)
  19. interface?
  20. (ref ref-method)
  21. (walk walk-method)
  22. (clients interface-clients)
  23. (name interface-name set-interface-name!))
  24. (define-record-discloser :interface
  25. (lambda (int)
  26. (list 'interface (interface-name int))))
  27. (define (make-interface ref walk name)
  28. (really-make-interface ref
  29. walk
  30. (make-population)
  31. name))
  32. ; The generic lookup function, and a simplified version for use when the
  33. ; base name and type are not needed.
  34. (define (interface-ref int name)
  35. ((ref-method int) name))
  36. (define (interface-member? int name)
  37. (receive (base-name type)
  38. (interface-ref int name)
  39. base-name))
  40. ; The generic walk function.
  41. (define (for-each-declaration proc int)
  42. ((walk-method int) proc))
  43. ; Adding to the client population.
  44. (define (note-reference-to-interface! int thing)
  45. (let ((pop (interface-clients int)))
  46. (if pop
  47. (add-to-population! thing pop))))
  48. ; Adding a late name.
  49. (define (note-interface-name! int name)
  50. (if (and name (not (interface-name int)))
  51. (set-interface-name! int name)))
  52. ;----------------
  53. ; Simple interfaces. ITEMS is a list of items of the form:
  54. ; - <name> ; use the default type
  55. ; - (<name> <type>) ; use <type>
  56. ; - ((<name> ...) <type>) ; use <type> for each <name>
  57. ;
  58. ; We make a table of the names and use it appropriately.
  59. (define (make-simple-interface name items)
  60. (let ((table (make-simple-interface-table items)))
  61. (make-interface (lambda (name)
  62. (let ((type (table-ref table name)))
  63. (if type
  64. (values name type)
  65. (values #f #f))))
  66. (lambda (proc)
  67. (table-walk (lambda (name type)
  68. (proc name name type))
  69. table))
  70. name)))
  71. (define (make-simple-interface-table items)
  72. (let ((table (make-symbol-table)))
  73. (for-each (lambda (item)
  74. (if (pair? item)
  75. (let ((name (car item))
  76. (type (cadr item)))
  77. (if (or (null? name)
  78. (pair? name))
  79. (for-each (lambda (name)
  80. (table-set! table name type))
  81. name)
  82. (table-set! table name type)))
  83. (table-set! table item undeclared-type)))
  84. items)
  85. (make-table-immutable! table)
  86. table))
  87. ;----------------
  88. ; Compound interfaces
  89. ;
  90. ; A compound interface is the union of a set of existing interfaces.
  91. ; To do lookups or walks we walk down the list of included interfaces.
  92. (define (make-compound-interface name . ints)
  93. (let ((int (make-interface (lambda (name)
  94. (let loop ((ints ints))
  95. (if (null? ints)
  96. (values #f #f)
  97. (receive (new-name type)
  98. (interface-ref (car ints) name)
  99. (if new-name
  100. (values new-name type)
  101. (loop (cdr ints)))))))
  102. (lambda (proc)
  103. (for-each (lambda (int)
  104. (for-each-declaration proc int))
  105. ints))
  106. name)))
  107. (for-each (lambda (i)
  108. (note-reference-to-interface! i int))
  109. ints)
  110. int))
  111. ;----------------
  112. ; Modified interfaces.
  113. ;
  114. ; We return a procedure that uses COMMANDS to modify any interface it is
  115. ; passed. We parse the commands first so that errors are detected before
  116. ; the structure that exports the modified interace is installed anywhere.
  117. ;
  118. ; Commands are:
  119. ; (prefix <symbol>)
  120. ; Add <symbol> to the beginning of every name in INTERFACE.
  121. ; (expose <symbol> ...)
  122. ; Export only those names in INTERFACE that are listed.
  123. ; (hide <symbol> ...)
  124. ; Do not export any of the names listed.
  125. ; (alias (<old> <new>) ...)
  126. ; Make name <old> also visible as <new>.
  127. ; (rename (<old> <new>) ...)
  128. ; Make name <old> visible as <new> but not as <old>.
  129. ; The commands are interpreted last-to-first. Thus
  130. ; ((expose foo:bar) (prefix foo:))
  131. ; and
  132. ; ((prefix foo:) (expose bar))
  133. ; both make BAR visible as FOO:BAR but
  134. ; ((expose bar) (prefix foo:))
  135. ; does not allow any names to be seen.
  136. (define (make-modified-interface-maker commands)
  137. (if (and (list? commands)
  138. (every okay-command? commands))
  139. (receive (alist hidden default)
  140. (process-commands commands)
  141. (lambda (interface)
  142. (let ((lookup (make-lookup alist hidden default interface))
  143. (walker (make-interface-walker alist hidden default interface)))
  144. (let ((int (make-interface lookup walker #f)))
  145. (note-reference-to-interface! interface int)
  146. int))))
  147. (assertion-violation 'make-modified-interface-maker
  148. "badly-formed structure modifiers" commands)))
  149. ; We process COMMANDS and compute three values:
  150. ; - an alist mapping visible names to their real names in the package
  151. ; - a list of names that are hidden (these may also appear in the alist;
  152. ; the hiding overrides the alist).
  153. ; - a default, which applies to all other names:
  154. ; = #f, there are no other visible names
  155. ; = #t, all other names are visible
  156. ; = <symbol>, names beginning with this prefix are visible
  157. ;
  158. ; We just loop over the commands, dispatching on the type of command.
  159. (define (process-commands commands)
  160. (let loop ((alist '())
  161. (hidden '())
  162. (default #t)
  163. (commands (reverse commands)))
  164. (if (null? commands)
  165. (values (filter (lambda (pair)
  166. (not (memq (car pair) hidden)))
  167. alist)
  168. hidden
  169. default)
  170. (receive (alist hidden default)
  171. (let ((proc (case (caar commands)
  172. ((prefix) process-prefix)
  173. ((expose) process-expose)
  174. ((hide) process-hide)
  175. ((alias) process-alias)
  176. ((rename) process-rename))))
  177. (proc (cdar commands) alist hidden default))
  178. (loop alist hidden default (cdr commands))))))
  179. ; Checks that COMMAND is properly formed.
  180. (define (okay-command? command)
  181. (and (list? command)
  182. (pair? command)
  183. (symbol? (car command))
  184. (pair? (cdr command))
  185. (let ((args (cdr command)))
  186. (case (car command)
  187. ((prefix)
  188. (and (symbol? (car args))
  189. (null? (cdr args))))
  190. ((expose hide)
  191. (every symbol? args))
  192. ((alias rename)
  193. (every (lambda (spec)
  194. (and (list? spec)
  195. (= 2 (length spec))
  196. (symbol? (car spec))
  197. (symbol? (cadr spec))))
  198. args))
  199. (else
  200. #f)))))
  201. ; We add the prefix to the names in ALIST and HIDDEN. If DEFAULT is already
  202. ; a prefix we add this one to it, otherwise the prefix is the new default.
  203. (define (process-prefix args alist hidden default)
  204. (let ((prefix (car args)))
  205. (values (map (lambda (pair)
  206. (cons (symbol-append prefix (car pair))
  207. (cdr pair)))
  208. alist)
  209. (map (lambda (name)
  210. (symbol-append prefix name))
  211. hidden)
  212. (cond ((symbol? default)
  213. (symbol-append default prefix))
  214. ((not default)
  215. #f)
  216. (else
  217. prefix)))))
  218. ; We make a new ALIST with the exposed names and with package names are looked
  219. ; up in the current state. Then we start again with no hidden names and no
  220. ; default.
  221. (define (process-expose args alist hidden default)
  222. (values (let loop ((args args) (new-alist '()))
  223. (if (null? args)
  224. (reverse new-alist)
  225. (let* ((name (car args))
  226. (pname (interface-lookup name alist hidden default)))
  227. (loop (cdr args)
  228. (if pname
  229. (cons (cons name pname)
  230. new-alist)
  231. new-alist)))))
  232. '()
  233. #f))
  234. ; Just add the names to the hidden list.
  235. (define (process-hide args alist hidden default)
  236. (values alist
  237. (append args hidden)
  238. default))
  239. ; Add the new aliases to ALIST.
  240. (define (process-alias args alist hidden default)
  241. (values (append (map (lambda (spec)
  242. (cons (cadr spec)
  243. (interface-lookup (car spec) alist hidden default)))
  244. args)
  245. alist)
  246. hidden
  247. default))
  248. ; Add the new aliases to ALIST and add the old names to HIDDEN.
  249. (define (process-rename args alist hidden default)
  250. (values (append (map (lambda (spec)
  251. (cons (cadr spec)
  252. (interface-lookup (car spec) alist hidden default)))
  253. args)
  254. alist)
  255. (append (map car args) hidden)
  256. default))
  257. ;----------------
  258. ; Look up a name, returning the name by which it is known in the base structure.
  259. ; - If it is in HIDDEN then it is not exported.
  260. ; - If there is an alias, then return the alias.
  261. ; - If there is no default the name is not exported.
  262. ; - A default of #T means every name is passed through.
  263. ; - Otherwise, check that NAME begins with the default and return the
  264. ; suffix after the default.
  265. (define (interface-lookup name alist hidden default)
  266. (cond ((memq name hidden)
  267. #f)
  268. ((assq name alist)
  269. => cdr)
  270. ((not default)
  271. #f)
  272. ((eq? default #t)
  273. name)
  274. ((prefix-match? (symbol->string name)
  275. (symbol->string default))
  276. (remove-prefix (symbol->string name)
  277. (symbol->string default)))
  278. (else
  279. #f)))
  280. ; Curried version of MAKE-LOOKUP for making the INTERFACE-REF method for
  281. ; modified structures.
  282. (define (make-lookup alist hidden default interface)
  283. (lambda (name)
  284. (let ((alias (interface-lookup name alist hidden default)))
  285. (if alias
  286. (interface-ref interface alias)
  287. (values #f #f)))))
  288. ; True if NAME begins with PREFIX (and is not just PREFIX).
  289. (define (prefix-match? name prefix)
  290. (and (< (string-length prefix)
  291. (string-length name))
  292. (let loop ((i 0))
  293. (cond ((= i (string-length prefix))
  294. #t)
  295. ((char=? (string-ref name i)
  296. (string-ref prefix i))
  297. (loop (+ i 1)))
  298. (else
  299. #f)))))
  300. ; Return the portion of NAME that follows PREFIX.
  301. (define (remove-prefix name prefix)
  302. (string->symbol (substring name
  303. (string-length prefix)
  304. (string-length name))))
  305. ;----------------
  306. ; Return a procedure for walking over the declarations in a modified interface.
  307. ; There are two helpers, depending on whether names are passed on by default.
  308. (define (make-interface-walker alist hidden default interface)
  309. (lambda (proc)
  310. (if default
  311. (walk-default proc alist hidden default interface))
  312. (walk-alist proc alist hidden interface)))
  313. ; If there is a default we need to walk over the declarations in the base
  314. ; interface and pass on the ones that are not hidden.
  315. (define (walk-default proc alist hidden default interface)
  316. (for-each-declaration
  317. (lambda (name base-name type)
  318. (let ((new-name
  319. (cond ((symbol? default)
  320. (symbol-append default name))
  321. (else name))))
  322. (if (not (memq new-name hidden))
  323. (proc new-name
  324. base-name
  325. type))))
  326. interface))
  327. ; With no default, all of the names are in the ALIST and we do not need to
  328. ; walk over the declarations in the base interface.
  329. (define (walk-alist proc alist hidden interface)
  330. (for-each (lambda (pair)
  331. (receive (base-name type)
  332. (interface-ref interface (cdr pair))
  333. (let ((new-name (car pair)))
  334. (if (and base-name (not (memq new-name hidden)))
  335. (proc new-name
  336. base-name
  337. type)))))
  338. alist))