Commit | Line | Data |
---|---|---|
dea4d055 MW |
1 | ;;; -*-lisp-*- |
2 | ;;; | |
3 | ;;; Module protocol implementation | |
4 | ;;; | |
5 | ;;; (c) 2009 Straylight/Edgeware | |
6 | ;;; | |
7 | ||
8 | ;;;----- Licensing notice --------------------------------------------------- | |
9 | ;;; | |
e0808c47 | 10 | ;;; This file is part of the Sensible Object Design, an object system for C. |
dea4d055 MW |
11 | ;;; |
12 | ;;; SOD is free software; you can redistribute it and/or modify | |
13 | ;;; it under the terms of the GNU General Public License as published by | |
14 | ;;; the Free Software Foundation; either version 2 of the License, or | |
15 | ;;; (at your option) any later version. | |
16 | ;;; | |
17 | ;;; SOD is distributed in the hope that it will be useful, | |
18 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;;; GNU General Public License for more details. | |
21 | ;;; | |
22 | ;;; You should have received a copy of the GNU General Public License | |
23 | ;;; along with SOD; if not, write to the Free Software Foundation, | |
24 | ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | ||
26 | (cl:in-package #:sod) | |
27 | ||
28 | ;;;-------------------------------------------------------------------------- | |
29 | ;;; Module basics. | |
30 | ||
31 | (defmethod module-import ((module module)) | |
32 | (dolist (item (module-items module)) | |
33 | (module-import item))) | |
34 | ||
35 | (defmethod add-to-module ((module module) item) | |
36 | (setf (module-items module) | |
37 | (nconc (module-items module) (list item))) | |
38 | (module-import item)) | |
39 | ||
40 | (defmethod shared-initialize :after ((module module) slot-names &key pset) | |
41 | "Tick off known properties on the property set." | |
42 | (declare (ignore slot-names)) | |
43 | (dolist (prop '(:guard)) | |
44 | (get-property pset prop nil))) | |
45 | ||
46 | (defmethod finalize-module ((module module)) | |
47 | (let* ((pset (module-pset module)) | |
52a79ab8 | 48 | (class (get-property pset :module-class :symbol 'module))) |
dea4d055 MW |
49 | |
50 | ;; Always call `change-class', even if it's the same one; this will | |
51 | ;; exercise the property-set fiddling in `shared-initialize' and we can | |
52 | ;; catch unknown-property errors. | |
53 | (change-class module class :state t :pset pset) | |
8ce92a8f | 54 | (check-unused-properties pset))) |
dea4d055 MW |
55 | |
56 | ;;;-------------------------------------------------------------------------- | |
57 | ;;; Module objects. | |
58 | ||
e7d43325 | 59 | (defvar-unbound *module-map* |
dea4d055 | 60 | "Hash table mapping true names to module objects.") |
e7d43325 MW |
61 | (define-clear-the-decks reset-module-map |
62 | (setf *module-map* (make-hash-table :test #'equal))) | |
dea4d055 MW |
63 | |
64 | (defun build-module | |
65 | (name thunk &key (truename (probe-file name)) location) | |
66 | "Construct a new module. | |
67 | ||
bf090e02 MW |
68 | This is the functionality underlying `define-module': see that macro for |
69 | full information." | |
70 | ||
71 | ;; Check for an import cycle. | |
72 | (when truename | |
73 | (let ((existing (gethash truename *module-map*))) | |
74 | (cond ((null existing)) | |
75 | ((eq (module-state existing) t) | |
287e744e MW |
76 | (when (plusp (module-errors existing)) |
77 | (error "Module `~A' contains errors" name)) | |
bf090e02 MW |
78 | (return-from build-module existing)) |
79 | (t | |
287e744e | 80 | (error "Module `~A' already being imported at ~A" |
bf090e02 MW |
81 | name (module-state existing)))))) |
82 | ||
83 | ;; Construct the new module. | |
dea4d055 MW |
84 | (let ((*module* (make-instance 'module |
85 | :name (pathname name) | |
86 | :state (file-location location)))) | |
87 | (when truename | |
88 | (setf (gethash truename *module-map*) *module*)) | |
89 | (unwind-protect | |
9ec578d9 MW |
90 | (with-module-environment () |
91 | (module-import *builtin-module*) | |
92 | (funcall thunk) | |
8ce92a8f MW |
93 | (finalize-module *module*) |
94 | *module*) | |
dea4d055 MW |
95 | (when (and truename (not (eq (module-state *module*) t))) |
96 | (remhash truename *module-map*))))) | |
97 | ||
9ec578d9 MW |
98 | (defun call-with-module-environment (thunk &optional (module *module*)) |
99 | "Invoke THUNK with bindings for the module variables in scope. | |
100 | ||
101 | This is the guts of `with-module-environment', which you should probably | |
102 | use instead." | |
103 | (progv | |
104 | (mapcar #'car *module-bindings-alist*) | |
105 | (module-variables module) | |
287e744e MW |
106 | (handler-bind ((error (lambda (cond) |
107 | (declare (ignore cond)) | |
108 | (incf (slot-value module 'errors)) | |
109 | :decline))) | |
110 | (unwind-protect (funcall thunk) | |
111 | (setf (module-variables module) | |
112 | (mapcar (compose #'car #'symbol-value) | |
113 | *module-bindings-alist*)))))) | |
9ec578d9 | 114 | |
239fa5bd MW |
115 | (defun call-with-temporary-module (thunk) |
116 | "Invoke THUNK in the context of a temporary module, returning its values. | |
117 | ||
118 | This is mainly useful for testing things which depend on module variables. | |
119 | This is the functionality underlying `with-temporary-module'." | |
120 | (let ((*module* (make-instance 'module | |
121 | :name "<temp>" | |
122 | :state nil))) | |
9ec578d9 MW |
123 | (with-module-environment () |
124 | (module-import *builtin-module*) | |
125 | (funcall thunk)))) | |
239fa5bd | 126 | |
dea4d055 MW |
127 | ;;;-------------------------------------------------------------------------- |
128 | ;;; Type definitions. | |
129 | ||
130 | (export 'type-item) | |
131 | (defclass type-item () | |
132 | ((name :initarg :name :type string :reader type-name)) | |
133 | (:documentation | |
134 | "A note that a module exports a type. | |
135 | ||
136 | We can only export simple types, so we only need to remember the name. | |
137 | The magic simple-type cache will ensure that we get the same type object | |
138 | when we do the import.")) | |
139 | ||
140 | (defmethod module-import ((item type-item)) | |
141 | (let* ((name (type-name item)) | |
142 | (def (gethash name *module-type-map*)) | |
143 | (type (make-simple-type name))) | |
144 | (cond ((not def) | |
145 | (setf (gethash name *module-type-map*) type)) | |
146 | ((not (eq def type)) | |
147 | (error "Conflicting types `~A'" name))))) | |
148 | ||
149 | (defmethod module-import ((class sod-class)) | |
150 | (record-sod-class class)) | |
151 | ||
152 | ;;;-------------------------------------------------------------------------- | |
153 | ;;; Code fragments. | |
154 | ||
08b6e064 MW |
155 | (defun output-c-excursion (stream location func) |
156 | "Invoke FUNC surrounding it by writing #line markers to STREAM. | |
dea4d055 MW |
157 | |
158 | The first marker describes LOCATION; the second refers to the actual | |
159 | output position in STREAM. If LOCATION doesn't provide a line number then | |
160 | no markers are output after all. If the output stream isn't | |
08b6e064 MW |
161 | position-aware then no final marker is output. |
162 | ||
163 | FUNC is passed the output stream as an argument. Complicated games may be | |
164 | played with interposed streams. Try not to worry about it." | |
165 | ||
166 | (flet ((doit (stream) | |
167 | (let* ((location (file-location location)) | |
168 | (line (file-location-line location)) | |
169 | (filename (file-location-filename location))) | |
170 | (cond (line | |
171 | (when (typep stream 'position-aware-stream) | |
172 | (format stream "~&#line ~D~@[ ~S~]~%" line filename)) | |
173 | (funcall func stream) | |
174 | (when (typep stream 'position-aware-stream) | |
175 | (fresh-line stream) | |
176 | (format stream "#line ~D ~S~%" | |
177 | (1+ (position-aware-stream-line stream)) | |
178 | (let ((path (stream-pathname stream))) | |
179 | (if path (namestring path) | |
180 | "<sod-output>"))))) | |
181 | (t | |
182 | (funcall func stream)))))) | |
183 | (print-ugly-stuff stream #'doit))) | |
dea4d055 MW |
184 | |
185 | (defmethod print-object ((fragment c-fragment) stream) | |
186 | (let ((text (c-fragment-text fragment)) | |
88b38efd | 187 | (location (file-location fragment))) |
dea4d055 MW |
188 | (if *print-escape* |
189 | (print-unreadable-object (fragment stream :type t) | |
190 | (when location | |
191 | (format stream "~A " location)) | |
192 | (cond ((< (length text) 40) | |
193 | (prin1 text stream) stream) | |
194 | (t | |
195 | (prin1 (subseq text 0 37) stream) | |
196 | (write-string "..." stream)))) | |
197 | (output-c-excursion stream location | |
08b6e064 | 198 | (lambda (stream) (write-string text stream)))))) |
dea4d055 MW |
199 | |
200 | (defmethod make-load-form ((fragment c-fragment) &optional environment) | |
201 | (make-load-form-saving-slots fragment :environment environment)) | |
202 | ||
7f2917d2 MW |
203 | (export '(code-fragment-item code-fragment code-fragment-reason |
204 | code-fragment-name code-fragment-constraints)) | |
dea4d055 | 205 | (defclass code-fragment-item () |
1645e433 MW |
206 | ((fragment :initarg :fragment :type (or string c-fragment) |
207 | :reader code-fragment) | |
dea4d055 MW |
208 | (reason :initarg :reason :type keyword :reader code-fragment-reason) |
209 | (name :initarg :name :type t :reader code-fragment-name) | |
210 | (constraints :initarg :constraints :type list | |
211 | :reader code-fragment-constraints)) | |
212 | (:documentation | |
213 | "A plain fragment of C to be dropped in at top-level.")) | |
214 | ||
bf090e02 MW |
215 | ;;;-------------------------------------------------------------------------- |
216 | ;;; File searching. | |
217 | ||
218 | (export '*module-dirs*) | |
219 | (defparameter *module-dirs* nil | |
220 | "A list of directories (as pathname designators) to search for files. | |
221 | ||
222 | Both SOD module files and Lisp extension files are searched for in this | |
223 | list. The search works by merging the requested pathname with each | |
224 | element of this list in turn. The list is prefixed by the pathname of the | |
225 | requesting file, so that it can refer to other files relative to wherever | |
226 | it was found. | |
227 | ||
228 | See `find-file' for the grubby details.") | |
229 | ||
230 | (export 'find-file) | |
231 | (defun find-file (scanner name what thunk) | |
232 | "Find a file called NAME on the module search path, and call THUNK on it. | |
233 | ||
234 | The file is searched for relative to the SCANNER's current file, and also | |
235 | in the directories mentioned in the `*module-dirs*' list. If the file is | |
236 | found, then THUNK is invoked with two arguments: the name we used to find | |
237 | it (which might be relative to the starting directory) and the truename | |
238 | found by `probe-file'. | |
239 | ||
240 | If the file wasn't found, or there was some kind of error, then an error | |
241 | is signalled; WHAT should be a noun phrase describing the kind of thing we | |
242 | were looking for, suitable for inclusion in the error message. | |
243 | ||
244 | While `find-file' establishes condition handlers for its own purposes, | |
245 | THUNK is not invoked with any additional handlers defined." | |
246 | ||
247 | (handler-case | |
248 | (dolist (dir (cons (pathname (scanner-filename scanner)) *module-dirs*) | |
249 | (values nil nil)) | |
250 | (let* ((path (merge-pathnames name dir)) | |
251 | (probe (probe-file path))) | |
252 | (when probe | |
253 | (return (values path probe))))) | |
254 | (file-error (error) | |
255 | (error "Error searching for ~A ~S: ~A" what (namestring name) error)) | |
256 | (:no-error (path probe) | |
257 | (cond ((null path) | |
258 | (error "Failed to find ~A ~S" what (namestring name))) | |
259 | (t | |
260 | (funcall thunk path probe)))))) | |
261 | ||
dea4d055 | 262 | ;;;----- That's all, folks -------------------------------------------------- |