Debian Codegraph @

src/clos/slot.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: CLOS -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1992, Giuseppe Attardi.
6 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
7 ;;;;
8 ;;;; See file 'LICENSE' for the copyright details.
9
10 (in-package "CLOS")
11
12 (defconstant +initform-unsupplied+ '+initform-unsupplied+)
13
14 ;;; ----------------------------------------------------------------------
15 ;;; SLOT descriptors
16 ;;;
17
18 (defun make-simple-slotd (class
19 &key name (initform +initform-unsupplied+) initfunction
20 (type 'T) (allocation :instance)
21 initargs readers writers documentation location)
22 (when (and (eq allocation :class)
23 (functionp initfunction))
24 (setf initfunction (constantly (funcall initfunction))))
25 (with-early-make-instance +slot-definition-slots+
26 (slotd class
27 :name name
28 :initform initform
29 :initfunction (if (listp initfunction) (eval initfunction) initfunction)
30 :type type
31 :allocation allocation
32 :initargs initargs
33 :readers readers
34 :writers writers
35 :documentation documentation
36 :location location)
37 slotd))
38
39 (defun freeze-class-slot-initfunction (slotd)
40 (when (eq (getf slotd :allocation) :class)
41 (let ((initfunc (getf slotd :initfunction)))
42 (when initfunc
43 (setf (getf slotd :initfunction)
44 (constantly (funcall initfunc))))))
45 slotd)
46
47 (defun canonical-slot-to-direct-slot (class slotd)
48 ;; Class slot init functions must be called right away
49 (setf slotd (freeze-class-slot-initfunction slotd))
50 (if (find-class 'slot-definition nil)
51 (apply #'make-instance
52 (apply #'direct-slot-definition-class class
53 (freeze-class-slot-initfunction slotd))
54 slotd)
55 (apply #'make-simple-slotd class slotd)))
56
57 (defun direct-slot-to-canonical-slot (slotd)
58 (list . #.(loop for (name . rest) in +slot-definition-slots+
59 collect (getf rest :initarg)
60 collect `(,(getf rest :accessor) slotd))))
61
62 ;;; ----------------------------------------------------------------------
63 ;;;
64 ;;; (PARSE-SLOTS slot-definition-form) => slot-definition-object
65 ;;;
66 ;;; This routine is the one responsible for parsing the definition of
67 ;;; a slot in DEFCLASS.
68 ;;;
69
70 (defun make-function-initform (form)
71 ;; INITFORM is a form that is to be evaluated at runtime. If it is a
72 ;; constant value, we output simply a quoted form. If it is not,
73 ;; we output a function that can be invoked at runtime to retrieve
74 ;; the value.
75 ;;
76 ;; Output => (FUNCTION (LAMBDA () form))
77 ;; => (QUOTE ...)
78 ;;
79 (if (constantp form)
80 `(constantly ,form)
81 `#'(lambda () ,form)))
82
83 (defun parse-slot (slot &optional (full nil))
84 (declare (si::c-local))
85 (if (symbolp slot)
86 (list* :name slot
87 (when full (list :initform '+INITFORM-UNSUPPLIED+ :initfunction nil
88 :initargs nil :readers nil :writers nil
89 :allocation :instance :documentation nil
90 :type 'T)))
91 (do* ((output (parse-slot (first slot) full))
92 (options (rest slot))
93 (value nil)
94 (extra nil))
95 ((null options)
96 (nconc output extra))
97 (let ((option (pop options)))
98 (when (endp options)
99 (si::simple-program-error
100 "In the slot description ~S,~%the option ~S is missing an argument"
101 slot option))
102 (let ((value (pop options)))
103 (when (and (member option '(:allocation :initform :type :documentation))
104 (getf options option))
105 (si::simple-program-error
106 "In the slot description ~S,~%the option ~S is duplicated"
107 slot option))
108 (case option
109 (:initarg (push value (getf output :initargs)))
110 (:initform (setf (getf output :initform) value
111 (getf output :initfunction)
112 (make-function-initform value)))
113 (:accessor (push value (getf output :readers))
114 (push `(setf ,value) (getf output :writers)))
115 (:reader (push value (getf output :readers)))
116 (:writer (push value (getf output :writers)))
117 (:allocation (setf (getf output :allocation) value))
118 (:type (setf (getf output :type) value))
119 (:documentation (setf (getf output :documentation) value))
120 (otherwise (if (or (getf extra option)
121 (getf options option))
122 (push value (getf extra option))
123 (setf (getf extra option) value)))))))))
124
125 (defun parse-slots (slots)
126 (do ((scan slots (cdr scan))
127 (collect))
128 ((null scan) (nreverse collect))
129 (let* ((slotd (parse-slot (first scan)))
130 (name (getf slotd :name)))
131 (dolist (other-slotd collect)
132 (when (eq name (getf other-slotd :name))
133 (si::simple-program-error
134 "A definition for the slot ~S appeared twice in a DEFCLASS form"
135 name)))
136 (push slotd collect))))
137
138 ;;; ----------------------------------------------------------------------