Commit | Line | Data |
---|---|---|
048d0b2d MW |
1 | ;;; -*-lisp-*- |
2 | ;;; | |
3 | ;;; Output for modules | |
4 | ;;; | |
5 | ;;; (c) 2013 Straylight/Edgeware | |
6 | ;;; | |
7 | ||
8 | ;;;----- Licensing notice --------------------------------------------------- | |
9 | ;;; | |
e0808c47 | 10 | ;;; This file is part of the Sensible Object Design, an object system for C. |
048d0b2d 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 | ;;; Utilities. | |
30 | ||
31 | (export 'banner) | |
32 | (defun banner (title output &key (blank-line-p t)) | |
33 | "Write a banner to the OUTPUT stream, starting a new section called TITLE. | |
34 | ||
35 | If BLANK-LINE-P is false, then leave a blank line after the banner. (This | |
36 | is useful for a final banner at the end of a file.)" | |
37 | (format output "~&/*----- ~A ~A*/~%" | |
38 | title | |
39 | (make-string (- 77 2 5 1 (length title) 1 2) | |
40 | :initial-element #\-)) | |
41 | (when blank-line-p | |
42 | (terpri output))) | |
43 | ||
44 | (export 'guard-name) | |
45 | (defun guard-name (filename) | |
46 | "Return a sensible inclusion guard name for FILENAME." | |
47 | (with-output-to-string (guard) | |
48 | (let* ((pathname (make-pathname :name (pathname-name filename) | |
49 | :type (pathname-type filename))) | |
50 | (name (namestring pathname)) | |
51 | (uscore t)) | |
52 | (dotimes (i (length name)) | |
53 | (let ((ch (char name i))) | |
54 | (cond ((alphanumericp ch) | |
55 | (write-char (char-upcase ch) guard) | |
56 | (setf uscore nil)) | |
57 | ((not uscore) | |
58 | (write-char #\_ guard) | |
59 | (setf uscore t)))))))) | |
60 | ||
61 | (defun guess-output-file (module type) | |
62 | "Guess the filename to use for a file TYPE, generated from MODULE. | |
63 | ||
64 | Here, TYPE is a filetype string. The result is returned as a pathname." | |
65 | (merge-pathnames (make-pathname :type type :case :common) | |
66 | (module-name module))) | |
67 | ||
e674612e MW |
68 | (defvar *done-one-off-output* nil |
69 | "A list of tokens for things which should appear at most once in output.") | |
70 | ||
71 | (export 'one-off-output) | |
72 | (defun one-off-output (token sequencer item-name function) | |
73 | "Arrange to output a thing at most once. | |
74 | ||
75 | If there has been no previous call to `one-off-output' with the given | |
76 | TOKEN during this output run, then arrange to call FUNCTION when the item | |
77 | called ITEM-NAME is traversed. Otherwise do nothing." | |
78 | (unless (member token *done-one-off-output*) | |
79 | (push token *done-one-off-output*) | |
80 | (add-sequencer-item-function sequencer item-name function))) | |
81 | ||
048d0b2d MW |
82 | ;;;-------------------------------------------------------------------------- |
83 | ;;; Main output interface. | |
84 | ||
85 | (export 'output-module) | |
86 | (defun output-module (module reason stream) | |
87 | "Write the MODULE to STREAM, giving the output machinery the REASON. | |
88 | ||
89 | This is the top-level interface for producing output." | |
e674612e MW |
90 | (let ((*done-one-off-output* nil) |
91 | (sequencer (make-instance 'sequencer)) | |
048d0b2d MW |
92 | (stream (if (typep stream 'position-aware-output-stream) |
93 | stream | |
94 | (make-instance 'position-aware-output-stream | |
95 | :stream stream | |
ea578bb4 | 96 | :file (stream-pathname stream))))) |
9ec578d9 MW |
97 | (with-module-environment (module) |
98 | (hook-output module reason sequencer) | |
99 | (invoke-sequencer-items sequencer stream)))) | |
048d0b2d MW |
100 | |
101 | ;;;-------------------------------------------------------------------------- | |
102 | ;;; Output implementation. | |
103 | ||
104 | (defmethod hook-output progn ((module module) reason sequencer) | |
105 | ||
106 | ;; Ask the module's items to sequence themselves. | |
107 | (dolist (item (module-items module)) | |
108 | (hook-output item reason sequencer))) | |
109 | ||
110 | (defmethod hook-output progn ((frag code-fragment-item) reason sequencer) | |
111 | ||
112 | ;; Output fragments when their reasons are called up. | |
113 | (when (eq reason (code-fragment-reason frag)) | |
114 | (dolist (constraint (code-fragment-constraints frag)) | |
115 | (add-sequencer-constraint sequencer constraint)) | |
116 | (add-sequencer-item-function sequencer (code-fragment-name frag) | |
117 | (lambda (stream) | |
118 | (write (code-fragment frag) | |
119 | :stream stream | |
120 | :pretty nil | |
121 | :escape nil))))) | |
122 | ||
123 | (defmethod hook-output progn ((module module) (reason (eql :h)) sequencer) | |
124 | (sequence-output (stream sequencer) | |
125 | ||
126 | :constraint | |
127 | (:prologue | |
128 | (:guard :start) | |
129 | (:typedefs :start) :typedefs (:typedefs :end) | |
e674612e | 130 | (:includes :start) :includes :early-decls (:includes :end) |
d437292a | 131 | (:early-user :start) :early-user (:early-user :end) |
721c1ca4 | 132 | (:classes :start) (:classes :end) |
d437292a | 133 | (:user :start) :user (:user :end) |
048d0b2d MW |
134 | (:guard :end) |
135 | :epilogue) | |
136 | ||
137 | (:prologue | |
138 | (format stream "~ | |
9ec578d9 | 139 | /* -*- mode: c; indent-tabs-mode: nil -*- |
048d0b2d MW |
140 | * |
141 | * Header file generated by SOD for ~A | |
142 | */~2%" | |
143 | (namestring (module-name module)))) | |
144 | ||
145 | ((:guard :start) | |
146 | (format stream "~ | |
147 | #ifndef ~A | |
148 | #define ~:*~A | |
149 | ||
150 | #ifdef __cplusplus | |
151 | extern \"C\" { | |
152 | #endif~2%" | |
153 | (or (get-property (module-pset module) :guard :id) | |
154 | (guard-name (or (stream-pathname stream) | |
155 | (guess-output-file module "H")))))) | |
156 | ((:guard :end) | |
157 | (banner "That's all, folks" stream) | |
158 | (format stream "~ | |
159 | #ifdef __cplusplus | |
160 | } | |
161 | #endif | |
162 | ||
163 | #endif~%")) | |
164 | ||
165 | ((:typedefs :start) | |
166 | (banner "Forward type declarations" stream)) | |
167 | ((:typedefs :end) | |
168 | (terpri stream)) | |
169 | ||
170 | ((:includes :start) | |
171 | (banner "External header files" stream)) | |
172 | ((:includes :end) | |
173 | (terpri stream)))) | |
174 | ||
175 | (defmethod hook-output progn ((module module) (reason (eql :c)) sequencer) | |
176 | (sequence-output (stream sequencer) | |
177 | ||
178 | :constraint | |
179 | (:prologue | |
180 | (:includes :start) :includes (:includes :end) | |
d437292a | 181 | (:early-user :start) :early-user (:early-user :end) |
048d0b2d | 182 | (:classes :start) (:classes :end) |
d437292a | 183 | (:user :start) :user (:user :end) |
048d0b2d MW |
184 | :epilogue) |
185 | ||
186 | (:prologue | |
187 | (format stream "~ | |
9ec578d9 | 188 | /* -*- mode: c; indent-tabs-mode: nil -*- |
048d0b2d MW |
189 | * |
190 | * Implementation file generated by SOD for ~A | |
191 | */~2%" | |
192 | (namestring (module-name module)))) | |
193 | ||
194 | (:epilogue | |
195 | (banner "That's all, folks" stream :blank-line-p nil)) | |
196 | ||
197 | ((:includes :start) | |
198 | (banner "External header files" stream)) | |
199 | ((:includes :end) | |
200 | (terpri stream)))) | |
201 | ||
9ec578d9 MW |
202 | ;;;-------------------------------------------------------------------------- |
203 | ;;; Output types. | |
204 | ||
205 | (defvar *output-types* nil | |
206 | "List of known output types.") | |
207 | ||
208 | (export 'declare-output-type) | |
209 | (defun declare-output-type (reason pathname) | |
210 | "Record that REASON is a valid user-level output type. | |
211 | ||
212 | The output file name will be constructed by merging the module's pathname | |
213 | with PATHNAME." | |
214 | (setf (get reason 'output-type) pathname)) | |
215 | ||
216 | (export 'output-type-pathname) | |
217 | (defun output-type-pathname (reason) | |
218 | "Return the PATHNAME template for the output type REASON. | |
219 | ||
220 | Report an error if there is no such output type." | |
221 | (or (get reason 'output-type) | |
222 | (error "Unknown output type `~(~A~)'" reason))) | |
223 | ||
224 | (define-clear-the-decks reset-output-types | |
225 | "Clear out the registered output types." | |
226 | (dolist (reason *output-types*) (remprop reason 'output-type)) | |
227 | (setf *output-types* nil) | |
228 | (declare-output-type :c (make-pathname :type "C" :case :common)) | |
229 | (declare-output-type :h (make-pathname :type "H" :case :common))) | |
230 | ||
048d0b2d | 231 | ;;;----- That's all, folks -------------------------------------------------- |