summaryrefslogtreecommitdiff
path: root/Lisp/class-definition.lisp
blob: b8e23e471c495db0c6e38dc3f867d7f4d9cce8aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
;;;; Objective-CL, an Objective-C bridge for Common Lisp.
;;;; Copyright (C) 2007  Matthias Andreas Benkard.
;;;;
;;;; This program is free software: you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public License
;;;; as published by the Free Software Foundation, either version 3 of
;;;; the License, or (at your option) any later version.
;;;;
;;;; This program 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
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this program.  If not, see
;;;; <http://www.gnu.org/licenses/>.

(in-package #:mulk.objective-cl)


(defvar *objcl-foreign-default-initform* (gensym))


(defclass foreign-slot-definition-mixin ()
  ((foreign-name :initarg :foreign-name
                 :initform nil
                 :accessor slot-definition-foreign-name
                 :type (or null string))
   (foreign-type :initarg :foreign-type
                 :initform nil
                 :accessor slot-definition-foreign-type)
   (foreign-slot :initarg :foreign-slot
                 :initform nil
                 :accessor slot-definition-foreign-slot)
   #+#:unused
   (property :initarg :property
             :accessor slot-definition-property-p
             :type boolean)))

(defclass foreign-direct-slot-definition
     (foreign-slot-definition-mixin c2mop:standard-direct-slot-definition)
  ())

(defclass foreign-effective-slot-definition
     (foreign-slot-definition-mixin c2mop:standard-effective-slot-definition)
  ())


(defmethod c2mop:validate-superclass ((class objective-c-meta-class)
                                      (superclass standard-class))
  t)

(defmethod c2mop:validate-superclass ((class objective-c-class)
                                      (superclass standard-class))
  t)

(defmethod c2mop:validate-superclass ((class objective-c-class)
                                      (superclass objective-c-class))
  t)

(defmethod c2mop:direct-slot-definition-class ((class objective-c-class)
                                               &rest initargs)
  (if (some #'(lambda (symbol) (let ((nada '#:nada))
                                 (not (eq nada (getf initargs symbol nada)))))
            '(:foreign-type :foreign-name :foreign-slot))
      (find-class 'foreign-direct-slot-definition)
      (find-class 'c2mop:standard-direct-slot-definition)))


(defmethod c2mop:effective-slot-definition-class ((class objective-c-class)
                                                  &rest initargs)
  (if (some #'(lambda (symbol) (let ((nada '#:nada))
                                 (not (eq nada (getf initargs symbol nada)))))
            '(:foreign-type :foreign-name :foreign-slot))
      (find-class 'foreign-effective-slot-definition)
      (find-class 'c2mop:standard-effective-slot-definition)))


(defmethod c2mop:compute-effective-slot-definition ((class objective-c-class)
                                                    name
                                                    direct-slot-definitions)
  (etypecase (first direct-slot-definitions)
    (foreign-direct-slot-definition
     (let ((direct-slot (first direct-slot-definitions)))
       (with-accessors ((type c2mop:slot-definition-type)
                        (readers c2mop:slot-definition-readers)
                        (writers c2mop:slot-definition-writers)
                        (initargs c2mop:slot-definition-initargs)
                        (initform c2mop:slot-definition-initform)
                        (allocation c2mop:slot-definition-allocation)
                        (initfunction c2mop:slot-definition-initfunction)
                        (foreign-type slot-definition-foreign-type)
                        (foreign-name slot-definition-foreign-name)
                        (foreign-slot slot-definition-foreign-slot))
           direct-slot
         (make-instance 'foreign-effective-slot-definition
            :type (or type t)
            :name name
            :readers (or readers nil)
            :writers (or writers nil)
            :initargs (or initargs nil)
            :initform (or initform *objcl-foreign-default-initform*)
            :location nil
            :allocation (or allocation :instance)
            :initfunction (or initfunction
                              #'(lambda ()
                                  (or initform *objcl-foreign-default-initform*)))
            :foreign-type foreign-type
            :foreign-name foreign-name
            :foreign-slot foreign-slot
            :class class))))
    (c2mop:standard-direct-slot-definition (call-next-method))))


(defmethod initialize-instance :after
    ((slot-definition foreign-effective-slot-definition)
     &key foreign-name
          foreign-slot
          foreign-type
          name
          class
     &allow-other-keys)
  (when (and (not foreign-name) (not foreign-slot))
    (setf foreign-name (slot-name->foreign-slot-name name)
          (slot-value slot-definition 'foreign-name) foreign-name))
  (cond ((and foreign-name foreign-slot))
        (foreign-name
         (setf foreign-slot
               (or (find foreign-name
                         (mapcan #'objcl-class-direct-slots
                                 (c2mop:compute-class-precedence-list
                                  class))
                         :key #'objcl-slot-name
                         :test #'string=)
                   (error "There is no Objective-C slot named ~A in class ~A"
                          foreign-name
                          class))
               (slot-value slot-definition 'foreign-slot) foreign-slot))
        (foreign-slot
         (setf foreign-name (objcl-slot-name foreign-slot)
               (slot-value slot-definition 'foreign-name) foreign-name)))
  (unless foreign-type
    (setf (slot-value slot-definition 'foreign-type)
          foreign-slot)))


(defmethod c2mop:slot-value-using-class ((class objective-c-class)
                                         instance
                                         (effective-slot-definition
                                          foreign-effective-slot-definition))
  (cerror "Continue" "FIXME"))


(defmethod (setf c2mop:slot-value-using-class) (value
                                                (class objective-c-class)
                                                instance
                                                (effective-slot-definition
                                                 foreign-effective-slot-definition))
  (unless (eq value *objcl-foreign-default-initform*)
    (cerror "Continue" "FIXME")))


(defmethod c2mop:slot-boundp-using-class ((class objective-c-class)
                                          instance
                                          (effective-slot-definition
                                           foreign-effective-slot-definition))
  (declare (ignore instance)))


(defmethod c2mop:slot-makunbound-using-class ((class objective-c-class)
                                              instance
                                              effective-slot-definition)
  (declare (ignore instance))
  (etypecase effective-slot-definition
    (c2mop:standard-effective-slot-definition (call-next-method))
    (foreign-effective-slot-definition
     (cerror "Continue without doing anything"
             "Tried to SLOT-MAKUNBOUND a foreign slot"))))


(defmethod c2mop:compute-slots ((class objective-c-class))
  ;; FIXME: Maybe add lots of foreign slots here whose presence the
  ;; Objective-C runtime tells us.
  (call-next-method))


(defmethod initialize-instance :around ((class objective-c-class)
                                        &rest args
                                        &key direct-slots
                                             pointer
                                        &allow-other-keys)
  (let ((key-args (copy-list args)))
    (dolist (objc-slot (objcl-class-direct-slots/pointer pointer))
      (let ((slot-name
             (intern (string-upcase (objcl-slot-name objc-slot))
                     (find-package '#:objective-c-classes))))
        (when (notany #'(lambda (slot-definition)
                          (eql slot-name (car slot-definition)))
                      direct-slots)
          (push (list
                 :name slot-name
                 :foreign-name (objcl-slot-name objc-slot)
                 :foreign-type (typespec->c-type
                                (parse-typespec
                                 (objcl-slot-type objc-slot))))
                (getf key-args :direct-slots)))))
    (apply #'call-next-method class key-args)))


#+(or)
(defmethod make-instance ((class-name (eql 'objective-c-class)) &rest initargs)
  (let ((class (call-next-method)))
    class))

#+(or)
(defmethod c2mop:class-direct-superclasses ((class objective-c-class))
  (list (objcl-class-superclass class)))


#+(or)
(defmethod shared-initialize :after ((class objective-c-class)
                                     slot-names
                                     &rest initargs)
  )

(defmethod initialize-instance ((class objective-c-class)
                                &key documentation
                                     name
                                     plist
                                     direct-superclasses
                                     direct-slots
                                     direct-default-initargs
                                     pointer
                                     wrapped-foreign-class)
  (call-next-method))

(defmethod reinitialize-instance ((class objective-c-class)
                                  &key documentation
                                       name
                                       plist
                                       direct-superclasses
                                       direct-slots
                                       direct-default-initargs
                                       pointer
                                       wrapped-foreign-class)
  (call-next-method))

(defmethod initialize-instance ((class objective-c-meta-class)
                                &key documentation
                                     name
                                     plist
                                     direct-superclasses
                                     direct-slots
                                     direct-default-initargs
                                     pointer)
  (call-next-method))

(defmethod reinitialize-instance ((class objective-c-meta-class)
                                  &key documentation
                                       name
                                       plist
                                       direct-superclasses
                                       direct-slots
                                       direct-default-initargs
                                       pointer)
  (call-next-method))

#+(or)
(defmethod c2mop:compute-effective-slot-definition ((class objective-c-class)
                                                    slot-name
                                                    direct-slots)
  (call-next-method))


#+(or)
(make-instance 'objective-c-class :wrapped-foreign-class "NSString")
#+(or)
(c2mop:ensure-class 'ns-string
                    :metaclass 'objective-c-class
                    :wrapped-foreign-class "NSString")
#+(or)
(defclass ns-string ()
  ((bla :foreign-type 'string
        :foreign-name "_bla"
        :accessor ns-string-bla)
   (nothing :accessor ns-string-nothing
            :initarg :ns-string
            :initform 100))
  (:metaclass objective-c-classes::%ns-string)
  (:wrapped-foreign-class "NSString"))