;;; -*-lisp-*- ;;; ;;; Builtin module provides the root of the class graph ;;; ;;; (c) 2009 Straylight/Edgeware ;;; ;;;----- Licensing notice --------------------------------------------------- ;;; ;;; This file is part of the Sensble Object Design, an object system for C. ;;; ;;; SOD is free software; you can redistribute it and/or modify ;;; it under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 2 of the License, or ;;; (at your option) any later version. ;;; ;;; SOD is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License ;;; along with SOD; if not, write to the Free Software Foundation, ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. (cl:in-package #:sod) ;;;-------------------------------------------------------------------------- ;;; Infrastructure. (defvar *class-slot-alist* nil) (defun add-class-slot-function (name function) "Attach a slot function to the `*class-slot-alist*'. The FUNCTION is invoked with one argument, which is a `sod-class' object to which it should add a slot. If a function with the same NAME is already defined then that function is replaced; otherwise a new name/ function pair is defined. Functions are are invoked in the order in which their names were first added." (aif (assoc name *class-slot-alist* :test #'string=) (setf (cdr it) function) (asetf *class-slot-alist* (append it (list (cons name function)))))) (defmacro define-class-slot (name (class &optional stream) type init &body prepare) "Define a new class slot. The slot will be called NAME (a string) and will be of TYPE (which should be a type S-expression). The slot's (static) initializer will be constructed by printing the value of INIT, which is evaluated with CLASS bound to the class object being constructed. If any PREPARE forms are provided, then they are evaluated as a progn, with CLASS bound to the class object, and STREAM bound to the output stream it should write on." (with-gensyms (classvar) `(add-class-slot-function ',name (lambda (,classvar) (make-sod-slot ,classvar ,name (c-type ,type) (make-property-set :lisp-class 'sod-class-slot :initializer-function (lambda (,class) ,init) ,@(and prepare `(:prepare-function (lambda (,class ,stream) ,@prepare))))))))) ;;;-------------------------------------------------------------------------- ;;; Basic information. (define-class-slot "name" (class) const-string (prin1-to-string (sod-class-name class))) (define-class-slot "nick" (class) const-string (prin1-to-string (sod-class-nickname class))) ;;;-------------------------------------------------------------------------- ;;; Instance allocation and initialization. (define-class-slot "initsz" (class) size-t (format nil "sizeof(struct ~A)" (ilayout-struct-tag class))) (define-class-slot "imprint" (class stream) (* (fun (* void) ("/*p*/" (* void)))) (format nil "~A__imprint" class) (let ((ilayout (sod-class-ilayout class))) (format stream "~&~: /* Imprint raw memory with instance structure. */ static void *~A__imprint(void *p) { struct ~A *sod__obj = p; ~:{sod__obj->~A.~A._vt = &~A;~:^~% ~} return (p); }~2%" class (ilayout-struct-tag class) (mapcar (lambda (ichain) (let* ((head (ichain-head ichain)) (tail (ichain-tail ichain))) (list (sod-class-nickname head) (sod-class-nickname tail) (vtable-name class head)))) (ilayout-ichains ilayout))))) (define-class-slot "init" (class stream) (* (fun (* void) ("/*p*/" (* void)))) (format nil "~A__init" class) ;; FIXME this needs a metaobject protocol (let ((ilayout (sod-class-ilayout class)) (used nil)) (format stream "~&~: /* Provide initial values for an instance's slots. */ static void *~A__init(void *p)~%{~%" class) (dolist (ichain (ilayout-ichains ilayout)) (let ((ich (format nil "sod__obj->~A.~A" (sod-class-nickname (ichain-head ichain)) (sod-class-nickname (ichain-tail ichain))))) (dolist (item (ichain-body ichain)) (etypecase item (vtable-pointer nil) (islots (let ((isl (format nil "~A.~A" ich (sod-class-nickname (islots-class item))))) (dolist (slot (islots-slots item)) (let ((dslot (effective-slot-direct-slot slot)) (init (effective-slot-initializer slot))) (when init (unless used (format stream " struct ~A *sod__obj = ~A__imprint(p);~2%" (ilayout-struct-tag class) class) (setf used t)) (format stream " ~A.~A =" isl (sod-slot-name dslot)) (ecase (sod-initializer-value-kind init) (:simple (write (sod-initializer-value-form init) :stream stream :pretty nil :escape nil) (format stream ";~%")) (:compound (format stream " (~A) {" (sod-slot-type dslot)) (write (sod-initializer-value-form init) :stream stream :pretty nil :escape nil) (format stream "};~%")))))))))))) (unless used (format stream " ~A__imprint(p);~%" class)) (format stream "~&~: return (p); }~2%"))) ;;;-------------------------------------------------------------------------- ;;; Superclass structure. (define-class-slot "n_supers" (class) size-t (length (sod-class-direct-superclasses class))) (define-class-slot "supers" (class stream) (* (* (class "SodClass" :const) :const)) (if (null (sod-class-direct-superclasses class)) 0 (format nil "~A__supers" class)) (let ((supers (sod-class-direct-superclasses class))) (when supers (format stream "~&~: /* Direct superclasses. */ static const SodClass *const ~A__supers[] = { ~{~A__class~^,~% ~} };~2%" class supers)))) (define-class-slot "n_cpl" (class) size-t (length (sod-class-precedence-list class))) (define-class-slot "cpl" (class stream) (* (* (class "SodClass" :const) :const)) (format nil "~A__cpl" class) (format stream "~&~: /* Class precedence list. */ static const SodClass *const ~A__cpl[] = { ~{~A__class~^,~% ~} };~2%" class (sod-class-precedence-list class))) ;;;-------------------------------------------------------------------------- ;;; Chain structure. (define-class-slot "link" (class) (* (class "SodClass" :const)) (aif (sod-class-chain-link class) (format nil "~A__class" it) 0)) (define-class-slot "head" (class) (* (class "SodClass" :const)) (format nil "~A__class" (sod-class-chain-head class))) (define-class-slot "level" (class) size-t (position class (reverse (sod-class-chain class)))) (define-class-slot "n_chains" (class) size-t (length (sod-class-chains class))) (define-class-slot "chains" (class stream) (* (struct "sod_chain" :const)) (format nil "~A__chains" class) (let ((chains (sod-class-chains class))) (format stream "~&~: /* Chain structure. */ ~1@*~:{static const SodClass *const ~A__chain_~A[] = { ~{~A__class~^,~% ~} };~:^~2%~} ~0@*static const struct sod_chain ~A__chains[] = { ~:{ { ~ /* n_classes = */ ~3@*~A, /* classes = */ ~0@*~A__chain_~A, /* off_ichain = */ ~4@*offsetof(struct ~A, ~A), /* vt = */ (const struct sod_vtable *)&~A, /* ichainsz = */ sizeof(struct ~A) }~:^,~%~} };~2%" class ;0 (mapcar (lambda (chain) ;1 (let* ((head (sod-class-chain-head (car chain))) (tail (sod-class-chain-head (car chain))) (chain-nick (sod-class-nickname head))) (list class chain-nick ;0 1 (reverse chain) ;2 (length chain) ;3 (ilayout-struct-tag class) chain-nick ;4 5 (vtable-name class head) ;6 (ichain-struct-tag (car chain) head)))) ;7 chains)))) ;;;-------------------------------------------------------------------------- ;;; Class-specific layout. (define-class-slot "off_islots" (class) size-t (if (sod-class-slots class) (format nil "offsetof(struct ~A, ~A)" (ichain-struct-tag class (sod-class-chain-head class)) (sod-class-nickname class)) "0")) (define-class-slot "islotsz" (class) size-t (if (sod-class-slots class) (format nil "sizeof(struct ~A)" (islots-struct-tag class)) "0")) ;;;-------------------------------------------------------------------------- ;;; Bootstrapping the class graph. (defun bootstrap-classes (module) "Bootstrap the braid in MODULE. This builds the fundamental recursive braid, where `SodObject' is an instance of `SodClass', and `SodClass' is a subclass of `SodObject' (and an instance of itself)." (let* ((sod-object (make-sod-class "SodObject" nil (make-property-set :nick 'obj))) (sod-class (make-sod-class "SodClass" (list sod-object) (make-property-set :nick 'cls))) (classes (list sod-object sod-class))) ;; Sort out the recursion. (setf (slot-value sod-class 'chain-link) sod-object) (dolist (class classes) (setf (slot-value class 'metaclass) sod-class)) ;; Predeclare the class types. (dolist (class classes) (make-class-type (sod-class-name class))) ;; Attach the class slots. (dolist (slot *class-slot-alist*) (funcall (cdr slot) sod-class)) ;; These classes are too closely intertwined. We must partially finalize ;; them together by hand. This is cloned from `finalize-sod-class'. (dolist (class classes) (with-slots (class-precedence-list chain-head chain chains) class (setf class-precedence-list (compute-cpl class)) (setf (values chain-head chain chains) (compute-chains class)))) ;; Done. (dolist (class classes) (finalize-sod-class class) (add-to-module module class)))) (defvar *builtin-module* nil "The builtin module.") (defun make-builtin-module () "Construct the builtin module. This involves constructing the braid (which is done in `bootstrap-classes') and defining a few obvious type names which users will find handy. Returns the newly constructed module, and stores it in the variable `*builtin-module*'." (let ((module (make-instance 'module :name (make-pathname :name "SOD-BASE" :type "SOD" :case :common) :state nil))) (with-module-environment (module) (dolist (name '("va_list" "size_t" "ptrdiff_t")) (add-to-module module (make-instance 'type-item :name name))) (flet ((header-name (name) (concatenate 'string "\"" (string-downcase name) ".h\"")) (add-includes (reason &rest names) (let ((text (with-output-to-string (out) (dolist (name names) (format out "#include ~A~%" name))))) (add-to-module module (make-instance 'code-fragment-item :reason reason :constraints nil :name :includes :fragment text))))) (add-includes :c (header-name "sod")) (add-includes :h "")) (bootstrap-classes module)) (setf *builtin-module* module))) ;;;----- That's all, folks --------------------------------------------------