chiark / gitweb /
ee2daf3f9ddab23b933e8270f160615e9f43b011
[sod] / class-output.lisp
1 ;;; -*-lisp-*-
2 ;;;
3 ;;; Output functions for classes
4 ;;;
5 ;;; (c) 2009 Straylight/Edgeware
6 ;;;
7
8 ;;;----- Licensing notice ---------------------------------------------------
9 ;;;
10 ;;; This file is part of the Simple Object Definition system.
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 ;;; Utility macro.
30
31 (defmacro sequence-output
32     ((streamvar sequencer) &body clauses)
33   (let ((seqvar (gensym "SEQ")))
34     (labels ((convert-item-name (name)
35                (if (listp name)
36                    (cons 'list name)
37                    name))
38              (convert-constraint (constraint)
39                (cons 'list (mapcar #'convert-item-name constraint)))
40              (process-body (clauses)
41                (if (eq (car clauses) :constraint)
42                    (cons `(add-sequencer-constraint
43                            ,seqvar
44                            ,(convert-constraint (cadr clauses)))
45                          (process-body (cddr clauses)))
46                    (mapcar (lambda (clause)
47                              (let ((name (car clause))
48                                    (body (cdr clause)))
49                                `(add-sequencer-item-function
50                                  ,seqvar
51                                  ,(convert-item-name name)
52                                  (lambda (,streamvar)
53                                    ,@body))))
54                            clauses))))
55       `(let ((,seqvar ,sequencer))
56          ,@(process-body clauses)))))
57
58 ;;;--------------------------------------------------------------------------
59 ;;; Classes.
60
61 (defmethod add-output-hooks progn
62     ((class sod-class) (reason (eql :h)) sequencer)
63
64   ;; Main output sequencing.
65   (sequence-output (stream sequencer)
66
67     :constraint
68     ((:classes :start)
69      (class :banner)
70      (class :islots :start) (class :islots :slots) (class :islots :end)
71      (class :vtmsgs :start) (class :vtmsgs :end)
72      (class :vtables :start) (class :vtables :end)
73      (class :vtable-externs) (class :vtable-externs-after)
74      (class :methods :start) (class :methods) (class :methods :end)
75      (class :ichains :start) (class :ichains :end)
76      (class :ilayout :start) (class :ilayout :slots) (class :ilayout :end)
77      (class :conversions)
78      (class :object)
79      (:classes :end))
80
81     (:typedefs
82      (format stream "typedef struct ~A ~A;~%"
83              (ichain-struct-tag class (sod-class-chain-head class)) class))
84
85     ((class :banner)
86      (banner (format nil "Class ~A" class) stream))
87     ((class :vtable-externs-after)
88      (terpri stream))
89
90     ((class :vtable-externs)
91      (format stream "/* Vtable structures. */~%"))
92
93     ((class :object)
94      (let ((metaclass (sod-class-metaclass class))
95            (metaroot (find-root-metaclass class)))
96        (format stream "/* The class object. */~%~
97                        extern const struct ~A ~A__classobj;~%~
98                        #define ~:*~A__class (&~:*~A__classobj.~A.~A)~2%"
99                (ilayout-struct-tag metaclass) class
100                (sod-class-nickname (sod-class-chain-head metaroot))
101                (sod-class-nickname metaroot)))))
102
103   ;; Maybe generate an islots structure.
104   (when (sod-class-slots class)
105     (dolist (slot (sod-class-slots class))
106       (add-output-hooks slot 'populate-islots sequencer))
107     (sequence-output (stream sequencer)
108       ((class :islots :start)
109        (format stream "/* Instance slots. */~%~
110                        struct ~A {~%"
111                (islots-struct-tag class)))
112       ((class :islots :end)
113        (format stream "};~2%"))))
114
115   ;; Declare the direct methods.
116   (when (sod-class-methods class)
117     (sequence-output (stream sequencer)
118       ((class :methods :start)
119        (format stream "/* Direct methods. */~%"))
120       ((class :methods :end)
121        (terpri stream))))
122
123   ;; Provide upcast macros which do the right thing.
124   (when (sod-class-direct-superclasses class)
125     (sequence-output (stream sequencer)
126       ((class :conversions)
127        (let ((chain-head (sod-class-chain-head class)))
128          (format stream "/* Conversion macros. */~%")
129          (dolist (super (cdr (sod-class-precedence-list class)))
130            (let ((super-head (sod-class-chain-head super)))
131              (format stream "#define ~:@(~A__CONV_~A~)(p) ((~A *)~
132                                      ~:[SOD_XCHAIN(~A, (p))~;(p)~])~%"
133                      class (sod-class-nickname super) super
134                      (eq chain-head super-head)
135                      (sod-class-nickname super-head))))
136          (terpri stream)))))
137
138   ;; Generate vtmsgs structure for all superclasses.
139   (add-output-hooks (car (sod-class-vtables class))
140                     'populate-vtmsgs
141                     sequencer))
142
143 (defmethod add-output-hooks progn ((class sod-class) reason sequencer)
144   (with-slots (ilayout vtables methods) class
145     (add-output-hooks ilayout reason sequencer)
146     (dolist (method methods) (add-output-hooks method reason sequencer))
147     (dolist (vtable vtables) (add-output-hooks vtable reason sequencer))))
148
149 ;;;--------------------------------------------------------------------------
150 ;;; Instance structure.
151
152 (defmethod add-output-hooks progn
153     ((slot sod-slot) (reason (eql 'populate-islots)) sequencer)
154   (sequence-output (stream sequencer)
155     (((sod-slot-class slot) :islots :slots)
156      (pprint-logical-block (stream nil :prefix "  " :suffix ";")
157        (pprint-c-type (sod-slot-type slot) stream (sod-slot-name slot)))
158      (terpri stream))))
159
160 (defmethod add-output-hooks progn ((ilayout ilayout) reason sequencer)
161   (with-slots (ichains) ilayout
162     (dolist (ichain ichains) (add-output-hooks ichain reason sequencer))))
163
164 (defmethod add-output-hooks progn
165     ((ilayout ilayout) (reason (eql :h)) sequencer)
166   (with-slots (class ichains) ilayout
167     (sequence-output (stream sequencer)
168       ((class :ilayout :start)
169        (format stream "/* Instance layout. */~%~
170                        struct ~A {~%"
171                (ilayout-struct-tag class)))
172       ((class :ilayout :end)
173        (format stream "};~2%")))
174     (dolist (ichain ichains)
175       (add-output-hooks ichain 'populate-ilayout sequencer))))
176
177 (defmethod add-output-hooks progn
178     ((ichain ichain) (reason (eql :h)) sequencer)
179   (with-slots (class chain-head chain-tail) ichain
180     (when (eq class chain-tail)
181       (sequence-output (stream sequencer)
182         :constraint ((class :ichains :start)
183                      (class :ichain chain-head :start)
184                      (class :ichain chain-head :slots)
185                      (class :ichain chain-head :end)
186                      (class :ichains :end))
187         ((class :ichain chain-head :start)
188          (format stream "/* Instance chain structure. */~%~
189                          struct ~A {~%"
190                  (ichain-struct-tag chain-tail chain-head)))
191         ((class :ichain chain-head :end)
192          (format stream "};~2%")
193          (format stream "/* Union of equivalent superclass chains. */~%~
194                          union ~A {~%~
195                          ~:{  struct ~A ~A;~%~}~
196                          };~2%"
197                  (ichain-union-tag chain-tail chain-head)
198                  (mapcar (lambda (super)
199                            (list (ichain-struct-tag super chain-head)
200                                  (sod-class-nickname super)))
201                          (sod-class-chain chain-tail))))))))
202
203 (defmethod add-output-hooks progn
204     ((ichain ichain) (reason (eql 'populate-ilayout)) sequencer)
205   (with-slots (class chain-head chain-tail) ichain
206     (sequence-output (stream sequencer)
207       ((class :ilayout :slots)
208        (format stream "  union ~A ~A;~%"
209                (ichain-union-tag chain-tail chain-head)
210                (sod-class-nickname chain-head))))))
211
212 (defmethod add-output-hooks progn
213     ((vtptr vtable-pointer) (reason (eql :h)) sequencer)
214   (with-slots (class chain-head chain-tail) vtptr
215     (sequence-output (stream sequencer)
216       ((class :ichain chain-head :slots)
217        (format stream "  const struct ~A *_vt;~%"
218                (vtable-struct-tag chain-tail chain-head))))))
219
220 (defmethod add-output-hooks progn ((islots islots) reason sequencer)
221   (dolist (slot (islots-slots islots))
222     (add-output-hooks slot reason sequencer)))
223
224 (defmethod add-output-hooks progn
225     ((islots islots) (reason (eql :h)) sequencer)
226   (with-slots (class subclass slots) islots
227     (sequence-output (stream sequencer)
228       ((subclass :ichain (sod-class-chain-head class) :slots)
229        (format stream "  struct ~A ~A;~%"
230                (islots-struct-tag class)
231                (sod-class-nickname class))))))
232
233 ;;;--------------------------------------------------------------------------
234 ;;; Vtable structure.
235
236 (defmethod add-output-hooks progn ((vtable vtable) reason sequencer)
237   (with-slots (body) vtable
238     (dolist (item body) (add-output-hooks item reason sequencer))))
239
240 (defmethod add-output-hooks progn
241     ((method sod-method) (reason (eql :h)) sequencer)
242   (with-slots (class) method
243     (sequence-output (stream sequencer)
244       ((class :methods)
245        (let ((type (sod-method-function-type method)))
246          (princ "extern " stream)
247          (pprint-c-type (commentify-function-type type) stream
248                         (sod-method-function-name method))
249          (format stream ";~%"))))))
250
251 (defmethod add-output-hooks progn
252     ((vtable vtable) (reason (eql :h)) sequencer)
253   (with-slots (class chain-head chain-tail) vtable
254     (when (eq class chain-tail)
255       (sequence-output (stream sequencer)
256         :constraint ((class :vtables :start)
257                      (class :vtable chain-head :start)
258                      (class :vtable chain-head :slots)
259                      (class :vtable chain-head :end)
260                      (class :vtables :end))
261         ((class :vtable chain-head :start)
262          (format stream "/* Vtable structure. */~%~
263                          struct ~A {~%"
264                  (vtable-struct-tag chain-tail chain-head)))
265         ((class :vtable chain-head :end)
266          (format stream "};~2%"))))
267     (sequence-output (stream sequencer)
268       ((class :vtable-externs)
269        (format stream "~@<extern struct ~A ~2I~_~A__vtable_~A;~:>~%"
270                (vtable-struct-tag chain-tail chain-head)
271                class (sod-class-nickname chain-head))))))
272
273 (defmethod add-output-hooks progn
274     ((vtmsgs vtmsgs) (reason (eql :h)) sequencer)
275   (with-slots (class subclass chain-head chain-tail) vtmsgs
276     (sequence-output (stream sequencer)
277       ((subclass :vtable chain-head :slots)
278        (format stream "  struct ~A ~A;~%"
279                (vtmsgs-struct-tag subclass class)
280                (sod-class-nickname class))))))
281
282 (defmethod add-output-hooks progn
283     ((vtmsgs vtmsgs) (reason (eql 'populate-vtmsgs)) sequencer)
284   (when (vtmsgs-entries vtmsgs)
285     (with-slots (class subclass) vtmsgs
286       (sequence-output (stream sequencer)
287         :constraint ((subclass :vtmsgs :start)
288                      (subclass :vtmsgs class :start)
289                      (subclass :vtmsgs class :slots)
290                      (subclass :vtmsgs class :end)
291                      (subclass :vtmsgs :end))
292         ((subclass :vtmsgs class :start)
293          (format stream "/* Messages protocol from class ~A */~%~
294                          struct ~A {~%"
295                  class
296                  (vtmsgs-struct-tag subclass class)))
297         ((subclass :vtmsgs class :end)
298          (format stream "};~2%"))))))
299
300 (defmethod add-output-hooks progn ((vtmsgs vtmsgs) reason sequencer)
301   (with-slots (entries) vtmsgs
302     (dolist (entry entries) (add-output-hooks entry reason sequencer))))
303
304 (defmethod add-output-hooks progn ((entry method-entry) reason sequencer)
305   (with-slots (method) entry
306     (add-output-hooks method reason sequencer)))
307
308 (defmethod add-output-hooks progn
309     ((entry method-entry) (reason (eql 'populate-vtmsgs)) sequencer)
310   (let* ((method (method-entry-effective-method entry))
311          (message (effective-method-message method))
312          (class (effective-method-class method))
313          (type (method-entry-function-type entry))
314          (commented-type (commentify-function-type type)))
315     (sequence-output (stream sequencer)
316       ((class :vtmsgs (sod-message-class message) :slots)
317        (pprint-logical-block (stream nil :prefix "  " :suffix ";")
318          (pprint-c-type commented-type stream (sod-message-name message)))
319        (terpri stream)))))
320
321 (defmethod add-output-hooks progn
322     ((cptr class-pointer) (reason (eql :h)) sequencer)
323   (with-slots (class chain-head metaclass meta-chain-head) cptr
324     (sequence-output (stream sequencer)
325       ((class :vtable chain-head :slots)
326        (format stream "  const ~A *~:[_class~;~:*_cls_~A~];~%"
327                metaclass
328                (if (sod-class-direct-superclasses meta-chain-head)
329                    (sod-class-nickname meta-chain-head)
330                    nil))))))
331
332 (defmethod add-output-hooks progn
333     ((boff base-offset) (reason (eql :h)) sequencer)
334   (with-slots (class chain-head) boff
335     (sequence-output (stream sequencer)
336       ((class :vtable chain-head :slots)
337        (write-line "  size_t _base;" stream)))))
338
339 (defmethod add-output-hooks progn
340     ((choff chain-offset) (reason (eql :h)) sequencer)
341   (with-slots (class chain-head target-head) choff
342     (sequence-output (stream sequencer)
343       ((class :vtable chain-head :slots)
344        (format stream "  ptrdiff_t _off_~A;~%"
345                (sod-class-nickname target-head))))))
346
347 ;;;--------------------------------------------------------------------------
348 ;;; Implementation output.
349
350 (defvar *instance-class*)
351
352 (defmethod add-output-hooks progn
353     ((class sod-class) (reason (eql :c)) sequencer)
354   (sequence-output (stream sequencer)
355
356     :constraint
357     ((:classes :start)
358      (class :banner)
359      (class :direct-methods :start) (class :direct-methods :end)
360      (class :effective-methods :start) (class :effective-methods :end)
361      (class :vtables :start) (class :vtables :end)
362      (class :object :prepare) (class :object :start) (class :object :end)
363      (:classes :end))
364
365     ((class :banner)
366      (banner (format nil "Class ~A" class) stream))
367
368     ((class :object :start)
369      (format stream "~
370 /* The class object. */
371 const struct ~A ~A__classobj = {~%"
372              (ilayout-struct-tag (sod-class-metaclass class))
373              class))
374     ((class :object :end)
375      (format stream "};~2%")))
376
377   (let ((*instance-class* class))
378     (add-output-hooks (sod-class-ilayout (sod-class-metaclass class))
379                       'populate-class
380                       sequencer)))
381
382 ;;;--------------------------------------------------------------------------
383 ;;; Direct methods.
384
385 ;; This could well want splitting out into some more elaborate protocol.  We
386 ;; need a bunch of refactoring anyway.
387
388 (defmethod add-output-hooks progn
389     ((method delegating-direct-method) (reason (eql :c)) sequencer)
390   (with-slots (class body) method
391     (unless body
392       (return-from add-output-hooks))
393     (sequence-output (stream sequencer)
394       ((class :direct-method method :start)
395        (format stream "#define CALL_NEXT_METHOD (next_method(~{~A~^, ~}))~%"
396                (mapcar #'argument-name
397                        (c-function-arguments (sod-method-next-method-type
398                                               method)))))
399       ((class :direct-method method :end)
400        (format stream "#undef CALL_NEXT_METHOD~%")))))
401
402 (defmethod add-output-hooks progn
403     ((method sod-method) (reason (eql :c)) sequencer)
404   (with-slots (class body) method
405     (unless body
406       (return-from add-output-hooks))
407     (sequence-output (stream sequencer)
408       :constraint ((class :direct-methods :start)
409                    (class :direct-method method :start)
410                    (class :direct-method method :body)
411                    (class :direct-method method :end)
412                    (class :direct-methods :end))
413       ((class :direct-method method :body)
414        (pprint-c-type (sod-method-function-type method)
415                       stream
416                       (sod-method-function-name method))
417        (format stream "~&{~%")
418        (write body :stream stream :pretty nil :escape nil)
419        (format stream "~&}~%"))
420       ((class :direct-method method :end)
421        (terpri stream)))))
422
423 ;;;--------------------------------------------------------------------------
424 ;;; Filling in the class object.
425
426 (defmethod add-output-hooks progn
427     ((ichain ichain) (reason (eql 'populate-class)) sequencer)
428   (with-slots (class chain-head) ichain
429     (sequence-output (stream sequencer)
430       :constraint ((*instance-class* :object :start)
431                    (*instance-class* :object chain-head :ichain :start)
432                    (*instance-class* :object chain-head :ichain :end)
433                    (*instance-class* :object :end))
434       ((*instance-class* :object chain-head :ichain :start)
435        (format stream "  { { /* ~A ichain */~%"
436                (sod-class-nickname chain-head)))
437       ((*instance-class* :object chain-head :ichain :end)
438        (format stream "  } },~%")))))
439
440 (defmethod add-output-hooks progn
441     ((islots islots) (reason (eql 'populate-class)) sequencer)
442   (with-slots (class) islots
443     (let ((chain-head (sod-class-chain-head class)))
444       (sequence-output (stream sequencer)
445         :constraint ((*instance-class* :object chain-head :ichain :start)
446                      (*instance-class* :object class :slots :start)
447                      (*instance-class* :object class :slots)
448                      (*instance-class* :object class :slots :end)
449                      (*instance-class* :object chain-head :ichain :end))
450         ((*instance-class* :object class :slots :start)
451          (format stream "      { /* Class ~A */~%" class))
452         ((*instance-class* :object class :slots :end)
453          (format stream "      },~%"))))))
454
455 (defmethod add-output-hooks progn
456     ((vtptr vtable-pointer) (reason (eql 'populate-class)) sequencer)
457   (with-slots (class chain-head chain-tail) vtptr
458     (sequence-output (stream sequencer)
459       :constraint ((*instance-class* :object chain-head :ichain :start)
460                    (*instance-class* :object chain-head :vtable)
461                    (*instance-class* :object chain-head :ichain :end))
462       ((*instance-class* :object chain-head :vtable)
463        (format stream "      &~A__vtable_~A,~%"
464                class (sod-class-nickname chain-head))))))
465
466 (defgeneric find-class-initializer (slot class)
467   (:method ((slot effective-slot) (class sod-class))
468     (let ((dslot (effective-slot-direct-slot slot)))
469       (or (some (lambda (super)
470                   (find dslot (sod-class-class-initializers super)
471                         :test #'sod-initializer-slot))
472                 (sod-class-precedence-list class))
473           (effective-slot-initializer slot)))))
474
475 (defgeneric output-class-initializer (slot instance stream)
476   (:method ((slot sod-class-effective-slot) (instance sod-class) stream)
477     (let ((func (effective-slot-initializer-function slot)))
478       (if func
479           (format stream "        ~A,~%" (funcall func instance))
480           (call-next-method))))
481   (:method ((slot effective-slot) (instance sod-class) stream)
482     (let ((init (find-class-initializer slot instance)))
483       (ecase (sod-initializer-value-kind init)
484         (:simple (format stream "        ~A,~%"
485                          (sod-initializer-value-form init)))
486         (:compound (format stream "        ~@<{ ~;~A~; },~:>~%"
487                          (sod-initializer-value-form init)))))))
488
489 (defmethod add-output-hooks progn ((slot sod-class-effective-slot)
490                                    (reason (eql 'populate-class))
491                                    sequencer)
492   (let ((instance *instance-class*)
493         (func (effective-slot-prepare-function slot)))
494     (when func
495       (sequence-output (stream sequencer)
496         ((instance :object :prepare)
497          (funcall func instance stream))))))
498
499 (defmethod add-output-hooks progn
500     ((slot effective-slot) (reason (eql 'populate-class)) sequencer)
501   (with-slots (class (dslot slot)) slot
502     (let ((instance *instance-class*)
503           (super (sod-slot-class dslot)))
504       (sequence-output (stream sequencer)
505         ((instance :object super :slots)
506          (output-class-initializer slot instance stream))))))
507
508 ;;;--------------------------------------------------------------------------
509 ;;; Testing.
510
511 #+test
512 (defun test (name)
513   (let ((sequencer (make-instance 'sequencer))
514         (class (find-sod-class name)))
515     (add-output-hooks class :h sequencer)
516     (invoke-sequencer-items sequencer *standard-output*)
517     sequencer))
518
519 ;;;----- That's all, folks --------------------------------------------------