-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathopir.nim
386 lines (360 loc) · 16.9 KB
/
opir.nim
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
import strutils, os, json, posix, options, tables
import clang, termstyle
proc `$`(cxstr: CXString): string =
result = $getCString(cxstr)
disposeString(cxstr)
proc getPointerInfo(ct: CXType): tuple[depth: int, baseType: CXType] =
result.baseType = ct
result.depth = 0
while result.baseType.kind == CXType_Pointer:
result.baseType = result.baseType.getPointeeType
inc result.depth
proc getLocation(c: CXCursor): tuple[filename: string, line, column: cuint] =
var filename: CXString
c.getCursorLocation.getPresumedLocation(filename.addr, result.line.addr, result.column.addr)
result.filename = $filename
proc toNimCallingConv(cc: CXCallingConv): string =
case cc:
of CXCallingConv_Default: "noconv"
of CXCallingConv_C: "cdecl"
of CXCallingConv_X86StdCall: "stdcall"
of CXCallingConv_X86FastCall: "fastcall"
of CXCallingConv_X86ThisCall: "thiscall"
of CXCallingConv_X86Pascal, CXCallingConv_AAPCS, CXCallingConv_AAPCS_VFP,
CXCallingConv_X86RegCall, CXCallingConv_IntelOclBicc, CXCallingConv_Win64,
CXCallingConv_X86_64SysV, CXCallingConv_X86VectorCall,
CXCallingConv_Swift, CXCallingConv_PreserveMost,
CXCallingConv_PreserveAll, CXCallingConv_AArch64VectorCall: "unknownCallingConv"
of CXCallingConv_Invalid: "invalidCallingConv"
of CXCallingConv_Unexposed: "unexposedCallingConv"
proc genStructDecl(struct: CXCursor): JsonNode
proc toNimType(ct: CXType): JsonNode =
case ct.kind:
of CXType_Invalid: %*{"kind": "invalid", "value": "invalid?"}
of CXType_Unexposed: %*{"kind": "invalid", "value": "unexposed?"}
of CXType_Void: %*{"kind": "base", "value": "void"}
of CXType_Bool: %*{"kind": "base", "value": "bool"}
of CXType_Char_U, CXType_UChar: %*{"kind": "base", "value": "cuchar"} # TODO: Difference between Char_U and UChar?
of CXType_Char16: %*{"kind": "base", "value": "int16"}
of CXType_Char32: %*{"kind": "base", "value": "int32"}
of CXType_UShort: %*{"kind": "base", "value": "cushort"}
of CXType_UInt: %*{"kind": "base", "value": "cuint"}
of CXType_ULong: %*{"kind": "base", "value": "culong"}
of CXType_ULongLong: %*{"kind": "base", "value": "culonglong"}
of CXType_UInt128: %*{"kind": "invalid", "value": "uint128"}
of CXType_Char_S, CXType_SChar: %*{"kind": "base", "value": "cschar"}
of CXType_WChar: %*{"kind": "base", "value": "Utf16Char"}
of CXType_Short: %*{"kind": "base", "value": "cshort"}
of CXType_Int: %*{"kind": "base", "value": "cint"}
of CXType_Long: %*{"kind": "base", "value": "clong"}
of CXType_LongLong: %*{"kind": "base", "value": "clonglong"}
of CXType_Int128: %*{"kind": "invalid", "value": "int128"}
of CXType_Float: %*{"kind": "base", "value": "cfloat"}
of CXType_Double: %*{"kind": "base", "value": "cdouble"}
of CXType_LongDouble: %*{"kind": "base", "value": "clongdouble"}
of CXType_NullPtr: %*{"kind": "base", "value": "pointer"}
of CXType_Overload, CXType_Dependent, CXType_ObjCId, CXType_ObjCClass, CXType_ObjCSel, CXType_Float128, CXType_Half, CXType_Float16, CXType_ShortAccum, CXType_Accum,
CXType_LongAccum, CXType_UShortAccum, CXType_UAccum, CXType_ULongAccum, CXType_Complex: %*{"kind": "invalid", "value": "???"}
of CXType_Pointer:
let info = ct.getPointerInfo
if info.depth == 1 and info.baseType.kind == CXType_CharS:
%*{"kind": "base", "value": "cstring"}
else:
let baseType = info.baseType.toNimType
%*{"kind": "pointer", "depth": info.depth, "base": if baseType.kind != JNull: baseType else: %*{"kind": "base", "value": "void"}}
of CXType_BlockPointer: %*{"kind": "pointer", "depth": 0}
of CXType_Typedef:
let name = $ct.getTypedefName
let base = case name:
of "int8_t": "int8"
of "int16_t": "int16"
of "int32_t": "int32"
of "int64_t": "int64"
of "uint8_t": "uint8"
of "uint16_t": "uint16"
of "uint32_t": "uint32"
of "uint64_t": "uint64"
of "size_t": "csize_t"
else: ""
if base.len == 0:
if name.len != 0:
%*{"kind": "alias", "value": name}
else:
newJNull()
else:
%*{"kind": "base", "value": base}
of CXType_Record:
#echo $ct.getTypedefName
#echo $ct.getTypeDeclaration.getCursorSpelling
#%*{"kind": "invalid", "value": "record?"}
let value = $ct.getTypeDeclaration.getCursorSpelling
if value.len != 0:
%*{"kind": "alias", "value": value}
else:
newJNull()
of CXType_LValueReference, CXType_RValueReference, CXType_ObjCInterface, CXType_ObjCObjectPointer: %*{"kind": "invalid", "value": "???"}
of CXType_Enum: %*{"kind": "invalid", "value": "inline enum?"}
#of CXType_FunctionNoProto: %*{"kind": "invalid", "value": "func_noproto?"}
of CXType_FunctionProto, CXType_FunctionNoProto:
# TODO: Move to use genProcDecl? Or at least share code
let retType = ct.getResultType
var args: seq[JsonNode]
for i in 0.cuint..<ct.getNumArgTypes.cuint:
args.add ct.getArgType(i).toNimType
%*{"kind": "proc", "return": retType.toNimType, "arguments": args, "callingConvention": ct.getFunctionTypeCallingConv.toNimCallingConv, "variadic": (ct.isFunctionTypeVariadic != 0) }
of CXType_ConstantArray:
%*{"kind": "array", "size": ct.getNumElements, "value": ct.getElementType.toNimType}
of CXType_IncompleteArray:
%*{"kind": "array", "value": ct.getElementType.toNimType}
of CXType_Vector, CXType_VariableArray, CXType_DependentSizedArray: %*{"kind": "invalid", "value": "array?"}
of CXType_Elaborated:
let value = $ct.getTypeDeclaration.getCursorSpelling
if value.len != 0:
%*{"kind": "alias", "value": value}
else:
#if ct.getTypeDeclaration.kind == CXCursor_StructDecl:
# ct.getTypeDeclaration.genStructDecl
#else:
newJNull()
else: %*{"kind": "invalid", "value": "???"}
proc toNimType(ct: CXCursor): JsonNode =
case ct.getCursorType.kind:
else: ct.getCursorType.toNimType
if paramCount() == 0:
echo "Usage: opir <C compiler arguments> <header files>"
quit -1
var
index = createIndex(0, 0)
commandLine = allocCStringArray(commandLineParams())
args = 0
fname = getTempDir() / "imports-" & $getPid() & ".h"
while commandLineParams()[args].startsWith "-": inc args
try:
var file = open(fname, fmWrite)
try:
for i in (args + 1)..paramCount():
file.writeLine "#include \"" & paramStr(i) & "\""
finally:
file.close()
except Exception as e:
stderr.writeLine red "Error:", " unable to open temporary file for translation: ", fname
stderr.writeLine e.msg
quit 1
var unit = parseTranslationUnit(index, fname.cstring,
commandLine, args.cint, nil, 0, CXTranslationUnit_DetailedPreprocessingRecord.cuint or CXTranslationUnit_SkipFunctionBodies.cuint)
deallocCStringArray(commandLine)
var fatal = false
for i in 0..<unit.getNumDiagnostics:
let diagnostic = unit.getDiagnostic(i)
case diagnostic.getDiagnosticSeverity():
of CXDiagnostic_Ignored: discard
of CXDiagnostic_Note: stderr.writeLine "\t", diagnostic.getDiagnosticSpelling()
of CXDiagnostic_Warning: stderr.writeLine yellow "Warning: ", diagnostic.getDiagnosticSpelling()
of CXDiagnostic_Error: stderr.writeLine red "Error: ", diagnostic.getDiagnosticSpelling()
of CXDiagnostic_Fatal:
stderr.writeLine red "Fatal: ", diagnostic.getDiagnosticSpelling()
fatal = true
if unit.isNil or fatal:
stderr.writeLine yellow "Tried to parse: ", commandLineParams()[args..^1].join " "
stderr.writeLine yellow "With arguments: ", commandLineParams()[0..<args].join " "
stderr.writeLine "Unable to parse translation unit succesfully. Quitting"
quit(-1)
proc genTypedefDecl(typedef: CXCursor): JsonNode =
let
name = $typedef.getCursorSpelling
theType = typedef.getTypeDefDeclUnderlyingType.toNimType
location = typedef.getLocation
if theType.kind != JNull and not (theType["kind"].str == "alias" and theType["value"].str == name):
return %*{"kind": "typedef", "file": location.filename, "position": {"column": location.column, "line": location.line}, "name": name, "type": theType}
proc genEnumDecl(enumdecl: CXCursor): JsonNode
proc genStructDecl(struct: CXCursor): JsonNode =
var name = $struct.getCursorType.getTypeSpelling
let
location = getLocation(struct)
kind =
if struct.getCursorKind == CXCursor_UnionDecl:
"union"
else:
"struct"
if name.startsWith(kind & " "):
name = name[len(kind & " ")..^1]
result =
if struct.Cursor_isAnonymous != 0:
%*{"kind": kind, "file": location.filename, "position": {"column": location.column, "line": location.line}, "fields": []}
else:
%*{"kind": kind, "file": location.filename, "position": {"column": location.column, "line": location.line}, "name": name, "fields": []}
discard visitChildren(struct, proc (field, parent: CXCursor, clientData: CXClientData): CXChildVisitResult {.cdecl.} =
var mainObj = cast[ptr JsonNode](clientData)
case field.getCursorKind:
of CXCursor_FieldDecl, CXCursor_StructDecl, CXCursor_UnionDecl, CXCursor_EnumDecl:
case field.getCursorType.kind:
of CXType_Record:
mainObj[]["fields"].add %*{"type": genStructDecl(field)}
of CXType_Enum:
mainObj[]["fields"].add %*{"type": genEnumDecl(field)}
of CXType_Elaborated:
if mainObj[]["fields"].elems.len != 0 and not mainObj[]["fields"].elems[^1].hasKey("name"):
mainObj[]["fields"].elems[^1]["name"] = %($field.getCursorSpelling)
else:
mainObj[]["fields"].add %*{"name": $field.getCursorSpelling, "type": field.toNimType}
else:
mainObj[]["fields"].add %*{"name": $field.getCursorSpelling, "type": field.toNimType}
of CXCursor_PackedAttr:
mainObj[]["packed"] = %true
of CXCursor_FirstAttr: discard # This should really be CXCursor_UnexposedAttr, but that's not exported from the module
else:
stderr.writeLine "Unknown element in structure or union: ", field.getCursorKind, " ", field.getLocation()
quit(-1)
return CXChildVisitContinue
, result.addr)
proc genEnumDecl(enumdecl: CXCursor): JsonNode =
var name = $enumdecl.getCursorType.getTypeSpelling
let location = getLocation(enumDecl)
if name.startsWith("enum "):
name = name[len("enum ")..^1]
result = %*{"kind": "enum", "file": location.filename, "position": {"column": location.column, "line": location.line}, "base": enumdecl.getEnumDeclIntegerType.toNimType, "fields": []}
if enumdecl.Cursor_isAnonymous == 0:
result["name"] = %name
discard visitChildren(enumDecl, proc (field, parent: CXCursor, clientData: CXClientData): CXChildVisitResult {.cdecl.} =
var mainObj = cast[ptr JsonNode](clientData)
mainObj[]["fields"].add %*{"name": $field.getCursorSpelling, "value": $field.getEnumConstantDeclValue}
CXChildVisitContinue
, result.addr)
proc genVarDecl(vardecl: CXCursor): JsonNode =
var pragmas: seq[string]
let location = getLocation(vardecl)
if varDecl.getCursorLinkage == CXLinkage_External:
# TODO: This is C `extern`, convert to something in Nim
discard
%*{"kind": "var", "file": location.filename, "position": {"column": location.column, "line": location.line}, "name": $varDecl.getCursorSpelling, "pragmas": pragmas, "type": varDecl.getCursorType.toNimType}
proc genProcDecl(funcDecl: CXCursor): JsonNode =
let funcDeclType = funcDecl.getCursorType
let name = $funcDecl.getCursorSpelling
let retType = funcDeclType.getResultType
let location = getLocation(funcdecl)
var args: seq[JsonNode]
var variadic = (funcDeclType.isFunctionTypeVariadic != 0)
for i in 0.cuint..<funcDeclType.getNumArgTypes.cuint:
var kind = funcDeclType.getArgType(i).toNimType
if kind["kind"].str == "alias" and kind["value"].str == "va_list" and i == funcDeclType.getNumArgTypes.cuint - 1:
variadic = true
continue
if kind["kind"].str == "invalid":
if funcDeclType.getArgType(i).kind == CXType_Elaborated:
if funcDeclType.getArgType(i).getTypeDeclaration.kind == CXCursor_EnumDecl:
kind = %*{"kind": "alias", "value": $funcDeclType.getArgType(i).getTypeDeclaration.getCursorSpelling}
var aname = $funcDecl.Cursor_getArgument(i).getCursorSpelling
if aname == "":
aname = "a" & $i
args.add %*{"name": aname, "type": kind}
%*{"kind": "proc", "file": location.filename, "position": {"column": location.column, "line": location.line}, "name": name, "return": retType.toNimType, "arguments": args, "callingConvention": funcDeclType.getFunctionTypeCallingConv.toNimCallingConv, "variadic": variadic}
var fileCache: Table[string, string] # TODO: Is there some way to get the macro body so we don't have to do this?
proc genMacroDecl(macroDef: CXCursor): JsonNode =
let name = $macroDef.getCursorSpelling
# Hard to get any usable data from this
# TODO: Figure out handling of macros..
# Options:
# importc, but what should be given as type? Maybe all have to be retyped?
# Try to guess type, but has to be able to read macro body (maybe get the range and read C source file directly?)
let extent = macroDef.getCursorExtent()
var
file: CXFile
line: cuint
column: cuint
offset: cuint
# Might be more information to be gathered
#echo macroDef.getCursorType()
#echo macroDef.CursorHasAttrs, ": ", name
#echo macroDef.CursorIsMacroFunctionLike, ": ", name
extent.getRangeStart.getExpansionLocation(file.addr, line.addr, column.addr, offset.addr)
let startOffset = offset
extent.getRangeEnd.getExpansionLocation(file.addr, nil, nil, offset.addr)
if offset - startOffset != name.len.uint32:
if macroDef.CursorIsMacroFunctionLike == 0:
let fname = $file.getFileName
if fname.len != 0:
let def = fileCache.mgetOrPut(fname, readFile(fname))[startOffset+name.len.cuint..<offset].strip
template parseReturn(x, defIn: untyped): untyped =
let def = defIn
try:
var
unsigned = false
long = false
longlong = false
size = false
pos = def.high
while def[pos] in {'u', 'U', 'l', 'L', 'z', 'Z'}:
case def[pos]:
of 'u', 'U': unsigned = true
of 'l', 'L': (if long: longlong = true else: long = true)
of 'z', 'Z': size = true
else: discard
dec pos
let
value = `parse x`(def[0..pos])
kind =
if unsigned or long or size:
%*{"kind":"base", "value": ("c" & (if unsigned: "u" else: "") & (if long or size: (if long: (if longlong: "longlong" else: "long") else: "size") else: "int"))}
else:
%*{"kind":"unknown"}
return %*{"kind": "const", "file": fname, "position": {"column": column, "line": line}, "name": name, "value": value, "type": kind}
except: discard
case def[0]:
of '0':
if def.len == 1: parseReturn(Int, def)
case def[1]:
of 'x', 'X': parseReturn(HexInt, def.replace("'", ""))
of 'b', 'B': parseReturn(BinInt, def.replace("'", ""))
of '1'..'9': parseReturn(OctInt, def[1..^1].replace("'", ""))
of 'u', 'U', 'l', 'L', 'z', 'Z': parseReturn(Int, def)
else: discard
of '-', '1'..'9': parseReturn(Int, def.replace("'", ""))
else: discard
if def.allCharsInSet({'a'..'z', 'A'..'Z', '0'..'9', '_'}) and def[0] notin '0'..'9':
return %*{"kind": "const", "file": fname, "position": {"column": column, "line": line}, "name": name, "type": {"kind": "alias", "value": def}}
# Might be useful to read a function signature for function like macros
#for i in 0..<macroDef.getCursorCompletionString.getNumCompletionChunks:
# echo "\t", macroDef.getCursorCompletionString.getCompletionChunkText(i)
# echo "\t", macroDef.getCursorCompletionString.getCompletionChunkKind(i)
return nil
var cursor = getTranslationUnitCursor(unit)
var output = newJArray()
discard visitChildren(cursor, proc (c, parent: CXCursor, clientData: CXClientData): CXChildVisitResult {.cdecl.} =
var decl =
case c.getCursorKind:
of CXCursor_TypedefDecl:
c.genTypedefDecl()
of CXCursor_StructDecl, CXCursor_UnionDecl:
let struct = c.genStructDecl()
if struct["fields"].len != 0:
struct
else: nil
of CXCursor_FunctionDecl:
c.genProcDecl()
of CXCursor_EnumDecl:
c.genEnumDecl()
of CXCursor_VarDecl:
c.genVarDecl()
of CXCursor_MacroDefinition:
c.genMacroDecl()
of CXCursor_MacroExpansion:
nil
of CXCursor_InclusionDirective:
# This could potentially be used to give more context for definitions
nil
else:
# Unknown cursor kind
stderr.writeLine yellow "Unknown cursor", " '", getCursorSpelling(c), "' of kind '", getCursorKindSpelling(getCursorKind(c)), "' and type '", getTypeSpelling(c.getCursorType), "'"
nil
if decl != nil:
output.add decl
return CXChildVisitContinue
, nil
)
for elem in output:
if not (elem.hasKey("kind") and elem.hasKey("file")):
stderr.writeLine red "Invalid element: ", elem
echo output
disposeTranslationUnit(unit)
disposeIndex(index)
removeFile(fname)