-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclang.clj
581 lines (500 loc) · 19.7 KB
/
clang.clj
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
(ns com.phronemophobic.clong.clang
(:require [com.phronemophobic.clong.clang.jna.raw :as c]
[clojure.string :as str]
[loom.graph :as g]
[clojure.java.io :as io]
[clojure.pprint :refer [pprint]]
loom.alg)
(:import java.io.PushbackReader
com.sun.jna.ptr.FloatByReference
com.sun.jna.ptr.IntByReference
com.sun.jna.ptr.PointerByReference))
(defn write-edn [w obj]
(binding [*print-length* nil
*print-level* nil
*print-dup* false
*print-meta* false
*print-readably* true
;; namespaced maps not part of edn spec
*print-namespace-maps* false
*out* w]
(pr obj)))
;; can find by calling clang -### empty-file.h
(def default-arguments
[ "-resource-dir"
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0"
"-isysroot"
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"
"-I/usr/local/include"
"-internal-isystem"
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include"
"-internal-isystem"
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0/include"
"-internal-externc-isystem"
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
"-internal-externc-isystem"
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include"
])
(defn get-spelling-location [location]
(let [filep (PointerByReference.)
line (IntByReference.)
col (IntByReference.)
offset (IntByReference.)]
(c/clang_getSpellingLocation location filep line col offset)
(let [filename (-> (c/clang_getFileName (.getValue filep))
(c/clang_getCString))]
{:line (.getValue line)
:file filename
:col (.getValue col)
:offset (.getValue offset)})))
(defn get-spelling-extent [cur]
(let [extent (c/clang_getCursorExtent cur)
start-loc (-> extent
c/clang_getRangeStart
get-spelling-location)
end-loc (-> extent
c/clang_getRangeStart
get-spelling-location)]
{:file (:file start-loc)
:start start-loc
:end end-loc}))
(declare field->map get-fields)
(defn struct-type->id [type]
(let [cur (c/clang_getTypeDeclaration type)
anonymous? (not (zero? (c/clang_Cursor_isAnonymous cur)))]
(if anonymous?
;; should use clang_getCursorUSR?
;; negative hash numbers add hypens
(keyword "clong" (str "Struct_" (format "%X" (hash (mapv field->map (get-fields type)) ))))
(let [struct-name (-> type
(c/clang_getCanonicalType)
(c/clang_getTypeSpelling)
(c/clang_getCString))
orig struct-name
struct-name (if (str/starts-with? struct-name "const ")
(subs struct-name (count "const "))
struct-name)
struct-name (if (str/starts-with? struct-name "struct ")
(subs struct-name (count "struct "))
struct-name)
;; ;; enforce starting with a letter?
;; struct-name (if (not (re-find #"^[a-zA-Z]" struct-name))
;; (str ))
]
(keyword "clong" struct-name)))))
(defn union-type? [t]
(let [t (c/clang_getCanonicalType t)
cur (c/clang_getTypeDeclaration t)]
(= c/CXCursor_UnionDecl
(:kind cur))))
(defn coffi-integer-type [size]
(case size
1 :coffi.mem/char
2 :coffi.mem/short
4 :coffi.mem/int
8 :coffi.mem/long
;; not sure what to call this
16 :coffi.mem/longlong
))
(defn coffi-float-type [size]
(case size
4 :coffi.mem/float
8 :coffi.mem/double
16 [:coffi.mem/array :coffi.mem/double 2]))
(defn get-argument-types [type]
(into []
(map (fn [i]
(c/clang_getArgType type i)))
(range (c/clang_getNumArgTypes type))))
(defn clang-type->coffi [type]
(let [type (c/clang_getCanonicalType type)]
#_(prn (c/clang_getCString
(c/clang_getTypeSpelling type)))
(condp = (.kind type)
c/CXType_Void :coffi.mem/void
c/CXType_ConstantArray [:coffi.mem/array
(clang-type->coffi
(c/clang_getArrayElementType type))
(c/clang_getArraySize type)]
c/CXType_Bool (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_Char_U (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_UChar (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_Char16 (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_Char32 (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_UShort (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_UInt (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_ULong (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_ULongLong (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_UInt128 (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_Char_S (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_SChar (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_WChar (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_Short (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_Int (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_Long (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_LongLong (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_Int128 (coffi-integer-type (c/clang_Type_getSizeOf type))
c/CXType_Float (coffi-float-type (c/clang_Type_getSizeOf type))
c/CXType_Double (coffi-float-type (c/clang_Type_getSizeOf type))
c/CXType_LongDouble (coffi-float-type (c/clang_Type_getSizeOf type))
c/CXType_Float128 (coffi-float-type (c/clang_Type_getSizeOf type))
;; c/CXType_NullPtr
;; c/CXType_Complex
;; A type whose specific kind is not exposed via this interface
c/CXType_Half :coffi.mem/type-half
c/CXType_Pointer (let [pointee-type (-> (c/clang_getPointeeType type)
(c/clang_getCanonicalType))
pointee-type-kind (-> pointee-type
(.kind)) ]
(cond
(= c/CXType_Void
pointee-type-kind)
:coffi.mem/pointer
(= c/CXType_FunctionProto
pointee-type-kind)
(clang-type->coffi pointee-type)
:else
[:coffi.mem/pointer
(clang-type->coffi pointee-type)]))
c/CXType_FunctionProto
[:coffi.ffi/fn
(mapv clang-type->coffi
(get-argument-types type))
(clang-type->coffi (c/clang_getResultType type))]
c/CXType_IncompleteArray
:coffi.mem/pointer
;; objc block function pointer?
c/CXType_BlockPointer [:coffi.mem/pointer]
c/CXType_Record
(if (union-type? type)
;; pretend for now
[:coffi.mem/array :coffi.mem/char (c/clang_Type_getSizeOf type)]
(struct-type->id type))
c/CXType_Enum :coffi.mem/int
)))
(defonce handles (atom #{}))
(defn ref! [o]
(swap! handles conj o)
o)
(defn parse
"Returns a CXCursor.
Further processing can be done via the raw api in com.phronemophobic.clong.clang.jna.raw. For basic usage, just use:
(->> (parse \"myheader.h\" default-arguments)
get-children
(map cursor-info))
"
[fname args]
(let [
index (ref! (c/clang_createIndex 0 0))
options c/CXTranslationUnit_DetailedPreprocessingRecord
translation-unit* (PointerByReference.)
err
(c/clang_parseTranslationUnit2 index
(ref! fname)
(ref!
(into-array String
args))
(count args)
nil
0
options
translation-unit*)
_ (when (not (zero? err))
(throw (ex-info "Parse Error"
{:error-code err})))
translation-unit (.getValue translation-unit*)
_ (assert translation-unit)
cursor (ref!
(c/clang_getTranslationUnitCursor translation-unit))]
cursor))
(defn get-children [cursor]
(let [childs (volatile! [])
visitor (ref!
(fn [child parent _]
(vswap! childs conj child)
c/CXChildVisit_Recurse))]
(c/clang_visitChildren cursor
visitor
nil)
(ref! @childs)
@childs))
(defn get-immediate-children [cursor]
(let [childs (volatile! [])
visitor (ref!
(fn [child parent _]
(vswap! childs conj child)
c/CXChildVisit_Continue))]
(c/clang_visitChildren cursor
visitor
nil)
(ref! @childs)
@childs))
(def ^:private cursor-kinds*
(->> (:enums c/clang-api)
(filter #(= "CXCursorKind"
(:enum %)))
(into {}
(map (fn [e]
[(:value e)
(:name e)])))))
(defn cursor-kind->str [n]
(cursor-kinds* n))
(def ^:private cursor-type-kinds*
(->> (:enums c/clang-api)
(filter #(= "CXTypeKind"
(:enum %)))
(into {}
(map (fn [e]
[(:value e)
(:name e)])))))
(defn cursor-type-kind->str [n]
(cursor-type-kinds* n))
(def ^:private linkage-type-kinds*
(->> (:enums c/clang-api)
(filter #(= "CXLinkageKind"
(:enum %)))
(into {}
(map (fn [e]
[(:value e)
(:name e)])))))
(defn linkage-kind->str [n]
(linkage-type-kinds* n))
(defmulti extra-cursor-info (fn [cur]
(cursor-kind->str (.kind cur))))
(defmethod extra-cursor-info :default
[cur]
{})
(defn cursor-info [cur]
;; (prn (:file (get-spelling-extent cur)))
(merge
{:kind (cursor-kind->str (.kind cur))
:spelling (c/clang_getCString
(c/clang_getCursorSpelling cur))
:cur cur
:location (get-spelling-extent cur)
:type (cursor-type-kind->str (.kind (ref!
(c/clang_getCursorType cur))))}
(extra-cursor-info cur)))
(defn enum-decl? [cur]
(= "CXCursor_EnumConstantDecl"
(:kind cur)))
(defmethod extra-cursor-info "CXCursor_EnumConstantDecl"
[cur]
{:name (c/clang_getCString
(c/clang_getCursorSpelling cur))
:value (-> cur
c/clang_getEnumConstantDeclValue)
:enum (-> cur
c/clang_getCursorSemanticParent
c/clang_getCursorSpelling
c/clang_getCString)
:raw-comment (c/clang_getCString
(c/clang_Cursor_getRawCommentText cur))})
(defn function-decl? [cur]
(= "CXCursor_FunctionDecl"
(:kind cur)))
(defonce get-source (memoize slurp))
(defn get-snippet [cur]
(let [range (c/clang_getCursorExtent cur)
start-location (get-spelling-location
(c/clang_getRangeStart range))
end-location (get-spelling-location(c/clang_getRangeEnd range))
snippet (when-let [filename (:file start-location)]
(let [source (get-source filename)]
(subs source
(:offset start-location)
(:offset end-location))))]
snippet))
(comment
(defmethod extra-cursor-info "CXCursor_MacroDefinition"
[cur]
(let [snippet (get-snippet cur)]
{:spelling (c/clang_getCString
(c/clang_getCursorSpelling cur))
:snippet snippet
:definition (-> cur
(c/clang_getCursorDefinition)
(c/clang_getCursorSpelling )
(c/clang_getCString))
:children (->> (get-immediate-children cur)
(map cursor-info))
:type (-> cur
c/clang_getCursorType
c/clang_getCanonicalType
c/clang_getTypeSpelling
c/clang_getCString)}
)))
(defmethod extra-cursor-info "CXCursor_FunctionDecl"
[cur]
(let [fname (c/clang_getCString
(c/clang_getCursorSpelling cur))
num-arguments (c/clang_Cursor_getNumArguments cur)]
#_(prn fname)
{:id (keyword fname)
:symbol fname
:args (into []
(comp
(map (fn [i]
(c/clang_Cursor_getArgument cur i)))
(map (fn [arg]
{:spelling (-> arg
c/clang_getCursorSpelling
c/clang_getCString)
:type (-> arg
c/clang_getCursorType
c/clang_getCanonicalType
c/clang_getTypeSpelling
c/clang_getCString)})))
(range num-arguments))
:ret {:spelling (-> (c/clang_getCursorResultType cur)
c/clang_getCanonicalType
c/clang_getTypeSpelling
c/clang_getCString)}
:function/args
(into []
(comp
(map (fn [i]
(c/clang_Cursor_getArgument cur i)))
(map c/clang_getCursorType)
(map clang-type->coffi))
(range num-arguments))
:linkage (linkage-kind->str (c/clang_getCursorLinkage cur))
:function/ret (-> cur
(c/clang_getCursorResultType)
clang-type->coffi)
:raw-comment (c/clang_getCString
(c/clang_Cursor_getRawCommentText cur))}))
(defn struct-decl? [cur]
(= "CXCursor_StructDecl"
(:kind cur)))
(defn get-fields [field-type]
(let [childs (volatile! [])
visitor (ref!
(fn [child _]
(vswap! childs conj child)
c/CXChildVisit_Continue))]
(c/clang_Type_visitFields field-type
visitor
nil)
(ref! @childs)
@childs))
(defn field->map [cur]
(let [field-type (-> cur
(c/clang_getCursorType)
(c/clang_getCanonicalType))
spelling (c/clang_getCString
(c/clang_getCursorSpelling cur))
type-spelling (c/clang_getCString
(c/clang_getTypeSpelling field-type))
bitfield? (not (zero? (c/clang_Cursor_isBitField cur)))
m {:type type-spelling
:datatype (clang-type->coffi field-type)
:name spelling
:bitfield? bitfield?
:calculated-offset (c/clang_Cursor_getOffsetOfField cur)}]
(if bitfield?
(assoc m :bitfield-width (c/clang_getFieldDeclBitWidth cur))
m)))
(defmethod extra-cursor-info "CXCursor_StructDecl"
[cur]
(let [
num-arguments (c/clang_Cursor_getNumArguments cur)
stype (-> (c/clang_getCursorType cur)
c/clang_getCanonicalType)
spelling (c/clang_getCString
(c/clang_getTypeSpelling stype))
anonymous (not (zero? (c/clang_Cursor_isAnonymous cur)))]
#_(prn spelling (not (zero? (c/clang_equalCursors cur
(c/clang_getCanonicalCursor cur))))
loc
(mapv field->map (get-fields stype)))
{:anonymous anonymous
:id (struct-type->id stype)
:spelling spelling
:size-in-bytes (c/clang_Type_getSizeOf stype)
:fields (mapv field->map (get-fields stype))}))
(def default-api-xforms
(comp (map cursor-info)
(remove (fn [cur]
(let [file (-> cur :location :file)]
(or (nil? file)
(str/starts-with? file
"/Applications/Xcode.app")))))
;; remove forward declarations
(remove (fn [cur]
(and (struct-decl? cur)
(empty? (:fields cur)))))))
(def enum-api-keys [:kind :spelling :type :name :value :enum :raw-comment])
(def struct-api-keys [:kind :spelling :type :id :size-in-bytes :fields])
(def function-api-keys [:args :ret :function/args :symbol :function/ret :type :linkage :id :raw-comment :kind :spelling])
(defn gen-api [cursors]
(let [enums (->> cursors
(filter enum-decl?)
(map #(select-keys % enum-api-keys))
doall)
structs-by-id (->> cursors
(filter struct-decl?)
(map #(select-keys % struct-api-keys))
(into {}
(map (fn [m]
[(:id m)
m]))))
;; sort topologically
struct-g
(apply g/digraph
(concat
;; nodes
(keys structs-by-id)
;; edges
(eduction
(mapcat (fn [m]
(keep (fn [field]
(when (contains? structs-by-id
(:datatype field))
[(:datatype field) (:id m)]))
(:fields m))))
(vals structs-by-id))))
structs (->> (loom.alg/topsort struct-g)
(keep structs-by-id))
functions (->> cursors
(filter function-decl?)
(map #(select-keys % function-api-keys))
(filter #(= "CXLinkage_External"
(:linkage %)))
doall)
]
{:functions functions
:structs structs
:enums enums}))
(defn easy-api
([header]
(easy-api header default-arguments))
([header args]
(->> (parse header args)
get-children
(eduction default-api-xforms)
gen-api)))
(defn dump-clang-api []
(with-open [w (io/writer (io/file
"resources"
"com"
"phronemophobic"
"clong"
"clang"
"api.edn"))]
(write-edn w (->> (parse "/Users/adrian/workspace/llvm-project/out/include/clang-c/Index.h"
(conj
default-arguments
"-I/Users/adrian/workspace/llvm-project/out/include/"))
get-children
(map cursor-info)
(remove (fn [cur]
(let [file (-> cur :location :file)]
(or (nil? file)
(str/starts-with? file
"/Applications/Xcode.app")))))
;; remove forward declarations
(remove (fn [cur]
(and (struct-decl? cur)
(empty? (:fields cur)))))
(gen-api)))))