summaryrefslogtreecommitdiff
path: root/Lisp/type-handling.lisp
blob: 4cce90cf6063cac373c81781b49e409300a2b2ed (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
;;;; 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)


(defstruct typespec
  primary-type
  qualifiers
  name
  children
  bit-field-start
  bit-field-length
  bit-field-type
  array-length
  unrecognised-tokens)


(defun parse-typespec (typestring &optional return-type-p (start 0))
  (list->typespec (parse-typespec-to-list typestring return-type-p start)))


(defun list->typespec (list)
  (destructuring-bind (primary-type qualifiers . name-and-children) list
    (let ((name (case primary-type
                  ((struct union :struct :union)
                   (first name-and-children))
                  (otherwise nil)))
          (children (case primary-type
                      ((struct union array :struct :union :array)
                       (rest name-and-children))
                      (otherwise name-and-children))))
      (case primary-type
        ((:bit-field bit-field)
         (ecase +runtime-type+
           (:gnu (destructuring-bind (start length type) name-and-children
                   (make-typespec :primary-type :bit-field
                                  :qualifiers qualifiers
                                  :bit-field-start start
                                  :bit-field-length length
                                  :bit-field-type type)))
           (:next (destructuring-bind (start length) name-and-children
                    (make-typespec :primary-type :bit-field
                                   :qualifiers qualifiers
                                   :bit-field-start start
                                   :bit-field-length length)))))
        ((:array array)
         (make-typespec :primary-type primary-type
                        :qualifiers qualifiers
                        :array-length (first name-and-children)
                        :children (mapcar #'list->typespec children)))
        ((:unrecognised unrecognised)
         (make-typespec :primary-type primary-type
                        :qualifiers qualifiers
                        :unrecognised-tokens name-and-children))
        (otherwise
         (make-typespec :primary-type primary-type
                        :qualifiers qualifiers
                        :name name
                        :children (mapcar #'list->typespec children)))))))


(defun typespec->list (typespec)
  (with-slots (primary-type qualifiers name children bit-field-start
               bit-field-length bit-field-type array-length
               unrecognised-tokens)
      typespec
    `(,primary-type
      ,qualifiers
      ,@(when name (list name))
      ,@(when bit-field-length ;BIT-FIELD-START is NIL on Mac OS X, but
                               ;we need it regardless.
          (list bit-field-start))
      ,@(when bit-field-length (list bit-field-length))
      ,@(when bit-field-type (list bit-field-type))
      ,@(when array-length (list array-length))
      ,@unrecognised-tokens
      ,@(mapcar #'typespec->list children))))


(defun parse-typespec-to-list (typestring &optional return-type-p (start 0))
  "Parse a typestring like \"@0:4{_NSRange=II}8\" into something like (ID ()).

\"rn{_NSRange=II}8\" is parsed into (STRUCT (CONST IN)
\"_NSRange\" :INTEGER :INTEGER).

\"(sversion=\"version\"i\"next_free\"^v)\" is parsed into (UNION ()
\"sversion\" (:INT ((NAME \"version\"))) (POINTER ((NAME
\"next_free\")) (:VOID ())))

Returns: (VALUES typespec byte-position string-position)"

  (let ((init-char (char typestring start))
        (string-position start)
        (qualifiers (list)))
    (loop do (setq init-char (char typestring string-position))
          while (let ((qualifier (case init-char
                                   (#\r 'const)
                                   (#\n 'in)
                                   (#\N 'inout)
                                   (#\o 'out)
                                   (#\O 'bycopy)
                                   (#\V 'oneway)
                                   (#\R 'byref)
                                   (#\" :name))))
                  (and qualifier
                       (incf string-position)
                       (if (eq qualifier :name)
                           (let ((name-end (position #\"
                                                     typestring
                                                     :start string-position)))
                             (push (list qualifier
                                         (subseq typestring
                                                 string-position
                                                 name-end))
                                   qualifiers)
                             (setf string-position (1+ name-end))
                             qualifier)
                           (push qualifier qualifiers)))))
    (let ((typespec
            (case init-char
              ((#\{ #\()
               (let* ((=-token (position #\= typestring :start start))
                      (closing-delim (position (ecase init-char
                                                 (#\{ #\})
                                                 (#\( #\)))
                                               typestring
                                               :start start))
                      (name-end (if (and =-token closing-delim)
                                    (min =-token closing-delim)
                                    ;; An opaque struct whose contents
                                    ;; we don't know.
                                    (or closing-delim
                                        (error "Premature end of file in~
                                                typespec: ~A."
                                               typestring))))
                      (named-p (and =-token (= name-end =-token)))
                      (struct-name (subseq typestring
                                           (1+ string-position)
                                           name-end)))
                 (list* (ecase init-char
                          (#\{ 'struct)
                          (#\( 'union))
                        (if named-p
                            qualifiers
                            (cons 'opaque qualifiers))
                        struct-name
                        (progn
                          (setq string-position
                                (if named-p
                                    (1+ name-end) ; skip #\=
                                    name-end))
                          (loop until (char= (char typestring string-position)
                                             (ecase init-char
                                               (#\{ #\})
                                               (#\( #\))))
                                collect (multiple-value-bind (typespec
                                                              byte-position
                                                              new-string-pos)
                                            (parse-typespec-to-list
                                             typestring
                                             nil
                                             string-position)
                                          (declare (ignore byte-position))
                                          (setq string-position new-string-pos)
                                          typespec)
                                ;; Skip end marker (right brace/paren).
                                finally (incf string-position))))))
              (#\^ (list 'pointer
                         qualifiers
                         (multiple-value-bind (typespec byte-pos new-str-pos)
                             (parse-typespec-to-list typestring
                                                     nil
                                                     (1+ string-position))
                           (declare (ignore byte-pos))
                           (prog1 typespec
                                  (setq string-position new-str-pos)))))
              (#\[ (list 'array
                         qualifiers
                         (multiple-value-bind (count new-str-pos)
                             (parse-integer typestring
                                            :start (1+ string-position)
                                            :junk-allowed t)
                           (prog1 count
                                  (setq string-position new-str-pos)))
                         (multiple-value-bind (typespec byte-pos new-str-pos)
                             (parse-typespec-to-list typestring nil string-position)
                           (declare (ignore byte-pos))
                           ;; Skip end marker (right bracket).
                           (prog1 typespec
                                  (setq string-position (1+ new-str-pos))))))
              (#\j
               (list 'complex
                     qualifiers
                     (multiple-value-bind (typespec byte-pos new-str-pos)
                         (parse-typespec-to-list typestring nil (1+ string-position))
                       (declare (ignore byte-pos))
                       (prog1 typespec
                              (setq string-position new-str-pos)))))
              (#\b
               (if (eq +runtime-type+ :gnu)
                   (let (bit-field-starting-pos
                         bit-field-typespec
                         bit-field-length
                         byte-position)
                     (multiple-value-setq (bit-field-starting-pos string-position)
                         (parse-integer typestring
                                        :start (1+ string-position)
                                        :junk-allowed t))
                     (multiple-value-setq (bit-field-typespec
                                           byte-position
                                           string-position)
                         (parse-typespec-to-list typestring nil string-position))
                     (multiple-value-setq (bit-field-length string-position)
                         (parse-integer typestring
                                        :start string-position
                                        :junk-allowed t))
                     (list 'bit-field
                           qualifiers
                           bit-field-starting-pos
                           bit-field-length
                           bit-field-typespec))
                   (list 'bit-field
                         qualifiers
                         nil
                         (multiple-value-bind (bit-field-length new-string-pos)
                             (parse-integer typestring
                                            :start (1+ string-position)
                                            :junk-allowed t)
                           (setq string-position new-string-pos)
                           bit-field-length))))
              (otherwise
               (let ((children (list)))
                 (prog1 (list* (case init-char
                                 (#\B :boolean) ;XXX :int?
                                 (#\c (if (and (eq +runtime-type+ :next)
                                               (or return-type-p
                                                   (featurep 'cffi-features:ppc32)))
                                          (prog1 :int
                                            (push '(nominally :char)
                                                  qualifiers))
                                          :char))
                                 (#\C (if (and (eq +runtime-type+ :next)
                                               (or return-type-p
                                                   (featurep 'cffi-features:ppc32)))
                                          (prog1 :unsigned-int
                                            (push '(nominally :unsigned-char)
                                                  qualifiers))
                                          :unsigned-char))
                                 (#\s (if (and (eq +runtime-type+ :next)
                                               (or return-type-p
                                                   (featurep 'cffi-features:ppc32)))
                                          (prog1 :int
                                            (push '(nominally :short)
                                                  qualifiers))
                                          :short))
                                 (#\S (if (and (eq +runtime-type+ :next)
                                               (or return-type-p
                                                   (featurep 'cffi-features:ppc32)))
                                          (prog1 :unsigned-int
                                            (push '(nominally :unsigned-short)
                                                  qualifiers))
                                          :unsigned-short))
                                 (#\i :int)
                                 (#\I :unsigned-int)
                                 (#\l :long)
                                 (#\L :unsigned-long)
                                 (#\q :long-long)
                                 (#\Q :unsigned-long-long)
                                 (#\f :float)
                                 (#\d :double)
                                 (#\v :void)
                                 (#\@ 'id)
                                 (#\# 'objective-c-class)
                                 (#\: 'selector)
                                 (#\* :string)
                                 (#\? :unknown)
                                 (otherwise
                                  (prog1 :unrecognised
                                    (push init-char children))))
                               qualifiers
                               children)
                        (incf string-position)))))))
      (when (and (> (length typestring) string-position)
                 (char= (char typestring string-position) #\"))
        (let ((type-end (position #\" typestring :start (1+ string-position))))
          (push (list :type (subseq typestring (1+ string-position) type-end))
                (cadr typespec))
          (setf string-position (1+ type-end))))
      (values typespec nil string-position))))


(defun typespec (typespec-designator)
  (etypecase typespec-designator
    (symbol (make-typespec :primary-type typespec-designator))
    (list (list->typespec typespec-designator))
    (typespec typespec-designator)))


(defun print-typespec-to-string (typespec)
  (with-output-to-string (out)
    (print-typespec typespec out)))


(defun print-typespec (typespec &optional (stream *standard-output*))
  "Convert a TYPESPEC into a typestring and write the result to a STREAM."
  (destructuring-bind (type-name modifiers &rest rest)
      (typespec->list (typespec typespec))
    (dolist (modifier modifiers)
      (format stream "~A" (case modifier
                            (const #\r)
                            (in #\n)
                            (inout #\N)
                            (out #\o)
                            (bycopy #\O)
                            (oneway #\V)
                            (byref #\R)
                            (opaque "")
                            (otherwise
                             (assert (listp modifier))
                             (ecase (first modifier)
                               ((nominally) (setq type-name
                                                  (second modifier))))
                             ""))))
    (case type-name
      ((struct union) (destructuring-bind (name . children) rest
                        (format stream "~C~A"
                                (ecase type-name
                                  (struct #\{)
                                  (union #\())
                                name)
                        (unless (member 'opaque modifiers)
                          (format stream "=")
                          (dolist (child children)
                            (print-typespec child stream)))
                        (format stream "~C" (ecase type-name
                                              (struct #\})
                                              (union #\))))))
      ((bit-field :bit-field)
       (if (eq +runtime-type+ :gnu)
           (destructuring-bind (alignment length . children) rest
             (format stream "b~D" alignment)
             (dolist (child children)
               (print-typespec child stream))
             (format stream "~D" length))
           (destructuring-bind (alignment length . children) rest
             (declare (ignore alignment children))
             (format stream "b~D" length))))
      ((array :array)
       (destructuring-bind (length . children) rest
         (format stream "[~D" length)
         (dolist (child children)
           (print-typespec child stream))
         (format stream "]")))
      ((:unrecognised) (format stream "~{~A~}" rest))
      (t (format stream "~A" (typespec-name->type-id type-name))
         (dolist (child rest)
           (print-typespec child stream))))))


(defun typespec-nominal-type (typespec)
  ;; Do the modifiers include something like (NOMINALLY :UNSIGNED-CHAR)?
  ;; Return NIL if that is not the case, otherwise return the nominal
  ;; type found.
  (cadr (find-if #'(lambda (x) (and (consp x)
                                    (eq (car x) 'nominally)))
                 (typespec-qualifiers (typespec typespec)))))