forked from pragmagic/godot-nim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgodotmacros.nim
677 lines (612 loc) · 24.6 KB
/
godotmacros.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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# Copyright 2018 Xored Software, Inc.
import macros, tables, typetraits
import godotinternal, internal.godotvariants
import godotnim, core.variants
import strutils, sets, sequtils
type
VarDecl = ref object
name: NimNode
typ: NimNode
defaultValue: NimNode
isNoGodot: bool
hint: string
hintStr: string
usage: NimNode
isExported: bool
MethodDecl = ref object
name: string
args: seq[VarDecl]
isVirtual: bool
returnType: NimNode
nimNode: NimNode
isNoGodot: bool
ObjectDecl = ref object
name: string
parentName: string
fields: seq[VarDecl]
methods: seq[MethodDecl]
isTool: bool
ParseError = object of Exception
proc godotToNim[T](val: Variant): (T, ConversionResult) =
mixin fromVariant
result[1] = fromVariant(result[0], val)
proc nimToGodot[T](val: T): Variant =
mixin toVariant
when compiles(toVariant(val)):
result = toVariant(val)
else:
const err = "Cannot convert Nim value of type " & T.name &
" into Variant"
{.error: err.}
template parseError(node: NimNode, msg: string) =
raise newException(ParseError, lineinfo(node) & ": " & msg)
proc extractNames(definition: NimNode):
tuple[name, parentName: string] =
if definition.kind == nnkIdent:
result.name = $(definition.ident)
else:
if not (definition.kind == nnkInfix and
definition[0].ident == toNimIdent("of")):
parseError(definition, "invalid type definition")
result.name = $(definition[1].ident)
case definition[2].kind:
of nnkIdent:
result.parentName = $definition[2].ident
else:
parseError(definition[2], "parent type expected")
when not declared(newRStrLit):
proc newRStrLit(s: string): NimNode {.compileTime.} =
result = newNimNode(nnkRStrLit)
result.strVal = s
proc newCStringLit(s: string): NimNode {.compileTime.} =
newNimNode(nnkCallStrLit).add(ident("cstring"), newRStrLit(s))
iterator pragmas(node: NimNode):
tuple[key: NimIdent, value: NimNode, index: int] =
assert node.kind in {nnkPragma, nnkEmpty}
for index in countdown(node.len - 1, 0):
if node[index].kind == nnkExprColonExpr:
yield (node[index][0].ident, node[index][1], index)
elif node[index].kind == nnkIdent:
yield (node[index].ident, nil, index)
proc removePragmaNode(statement: NimNode,
pname: string): NimNode {.compileTime.} =
## Removes the pragma from the node and returns value of the pragma
## Works for routine nodes or nnkPragmaExpr
if not (RoutineNodes.contains(statement.kind) or
statement.kind == nnkPragmaExpr):
return nil
result = nil
var pragmas = if RoutineNodes.contains(statement.kind): statement.pragma()
else: statement[1]
let pnameIdent = toNimIdent(pname)
for ident, val, i in pragmas(pragmas):
if ident == pnameIdent:
pragmas.del(i)
return val
proc removePragma(statement: NimNode, pname: string): bool =
## Removes the pragma from the node and returns whether pragma was removed
if not (RoutineNodes.contains(statement.kind) or
statement.kind == nnkPragmaExpr):
return false
var pragmas = if RoutineNodes.contains(statement.kind): statement.pragma()
else: statement[1]
let pnameIdent = toNimIdent(pname)
for ident, val, i in pragmas(pragmas):
if ident == pnameIdent:
pragmas.del(i)
return true
proc removeStrPragma(statement: NimNode,
pname: string): string {.compileTime.} =
## Removes the pragma from the node and returns value of the pragma
## Works for routine nodes or nnkPragmaExpr
let node = removePragmaNode(statement, pname)
result = if node.isNil: ""
else: $node
proc isExported(node: NimNode): bool {.compileTime.} =
if node.kind == nnkPragmaExpr:
result = isExported(node[0])
elif node.kind == nnkPostfix:
result = ($node[0] == "*")
proc identDefsToVarDecls(identDefs: NimNode): seq[VarDecl] =
assert(identDefs.kind == nnkIdentDefs)
result = newSeqOfCap[VarDecl](identDefs.len - 2)
var typ = identDefs[identDefs.len - 2]
if typ.kind == nnkEmpty:
let defaultValue = identDefs[identDefs.len - 1]
if defaultValue.kind != nnkEmpty:
typ = newCall("type", defaultValue)
for i in 0..<(identDefs.len - 2):
let nameNode = identDefs[i].copyNimTree()
let hint = removeStrPragma(nameNode, "hint")
let hintStr = removeStrPragma(nameNode, "hintStr")
let usage = removePragmaNode(nameNode, "usage")
let isGdExport = removePragma(nameNode, "gdExport")
result.add(VarDecl(
name: if nameNode.kind == nnkPragmaExpr: nameNode[0].basename()
else: nameNode.basename(),
typ: typ,
defaultValue: identDefs[identDefs.len - 1],
hint: hint,
hintStr: hintStr,
isNoGodot: not isGdExport,
usage: usage,
isExported: nameNode.isExported()
))
proc parseMethod(meth: NimNode): MethodDecl =
assert(meth.kind in {nnkProcDef, nnkMethodDef})
let isGdExport = removePragma(meth, "gdExport")
let isNoGodot = (meth.kind != nnkMethodDef and not isGdExport) or
removePragma(meth, "noGdExport")
result = MethodDecl(
name: $meth[0].basename,
args: newSeq[VarDecl](),
returnType: meth[3][0],
isVirtual: meth.kind == nnkMethodDef,
isNoGodot: isNoGodot,
nimNode: meth
)
for i in 1..<meth[3].len:
var identDefs = meth[3][i]
result.args.add(identDefsToVarDecls(identDefs))
proc parseVarSection(decl: NimNode): seq[VarDecl] =
assert(decl.kind == nnkVarSection)
for i in 0..<decl.len:
if i == 0:
result = identDefsToVarDecls(decl[i])
else:
result.add(identDefsToVarDecls(decl[i]))
proc parseType(definition, callSite: NimNode): ObjectDecl =
let body = callSite[^1]
result = ObjectDecl(
fields: newSeq[VarDecl](),
methods: newSeq[MethodDecl]()
)
(result.name, result.parentName) = extractNames(definition)
var isTool = false
for i in 2..(callSite.len - 2):
let option = callSite[i]
if option.kind != nnkIdent:
parseError(option, "type specifier expected")
if option.ident == toNimIdent("tool"):
isTool = true
else:
parseError(option, "valid type specifier expected")
result.isTool = isTool
if result.parentName == "": result.parentName = "Object"
for statement in body:
case statement.kind:
of nnkVarSection:
let varSection = parseVarSection(statement)
result.fields.add(varSection)
of nnkProcDef, nnkMethodDef:
let meth = parseMethod(statement)
result.methods.add(meth)
of nnkCommentStmt:
discard
else:
parseError(statement, "field or method declaration expected")
macro invokeVarArgs(procIdent, objIdent;
minArgs, maxArgs: static[int], numArgsIdent,
argSeqIdent; argTypes: seq[NimNode],
hasReturnValue, isStaticCall: static[bool]): typed =
## Produces statement in form:
##
## .. code-block:: nim
## case numArgs:
## of 0:
## meth(self)
## of 1:
## let (arg0, err) = godotToNim[ArgT](argSeq[0])
## if err != ConversionResult.OK:
## error(...)
## return
## meth(self, arg0)
## else:
## error(...)
template conv(procLit, argT, argSeq, idx, argIdent, errIdent) =
let v = newVariant(argSeq[idx][])
v.markNoDeinit() # args will be destroyed externally
let (argIdent, errIdent) = godotToNim[argT](v)
if errIdent != ConversionResult.OK:
let errorKind = if errIdent == ConversionResult.TypeError: "a type error"
else: "a range error"
printError(
"Failed to invoke Nim procedure " & $procLit &
": " & errorKind & " has occurred when converting argument " & $idx &
" of Godot type " & $argSeq[idx][].getType())
return
# these help to avoid exporting internal modules
# thanks to closed symbol binding
template initGodotVariantCall(result) =
initGodotVariant(result)
template initGodotVariantCall(result, src) =
initGodotVariant(result, src)
result = newNimNode(nnkCaseStmt)
result.add(numArgsIdent)
for i in minArgs..maxArgs:
let branch = newNimNode(nnkOfBranch).add(newIntLitNode(i))
let branchBody = newStmtList()
var invocation = newNimNode(nnkCall)
invocation.add(procIdent)
invocation.add(objIdent) # self
for idx in 0..<i:
let argIdent = genSym(nskLet, "arg")
let errIdent = genSym(nskLet, "err")
let argT = argTypes[idx]
branchBody.add(getAst(conv($procIdent, argT,
argSeqIdent, idx, argIdent, errIdent)))
invocation.add(argIdent)
if isStaticCall:
invocation = newNimNode(nnkCall).add(ident("procCall"), invocation)
if hasReturnValue:
let theCall = newNimNode(nnkBracketExpr).add(newNimNode(nnkDotExpr).add(
newCall("toVariant", invocation), ident("godotVariant")))
branchBody.add(getAst(initGodotVariantCall(ident("result"), theCall)))
else:
branchBody.add(invocation)
branchBody.add(getAst(initGodotVariantCall(ident("result"))))
branch.add(branchBody)
result.add(branch)
template printInvokeErr(procName, minArgs, maxArgs, numArgs) =
printError(
"Failed to invoke Nim procedure " & procName &
": expected " & $minArgs & "-" & $maxArgs &
" arguments, but got " & $numArgs)
result.add(newNimNode(nnkElse).add(getAst(
printInvokeErr($procIdent.ident, minArgs, maxArgs, numArgsIdent))))
proc typeError(nimType: string, value: string, godotType: VariantType,
className: cstring, propertyName: cstring): string =
result = "Tried to assign incompatible value " & value & " (" & $godotType &
") to field \"" & $propertyName & ": " & $nimType & "\" of " &
$className
proc rangeError(nimType: string, value: string, className: cstring,
propertyName: cstring): string =
result = "Tried to assign the out-of-range value " & value &
" to field \"" & $propertyName & ": " & $nimType & "\" of " &
$className
proc nimDestroyFunc(obj: ptr GodotObject, methData: pointer,
userData: pointer) {.noconv.} =
let nimObj = cast[NimGodotObject](userData)
nimObj.removeGodotObject()
GC_unref(nimObj)
proc nimDestroyRefFunc(obj: ptr GodotObject, methData: pointer,
userData: pointer) {.noconv.} =
# references are destroyed by Godot when they are already destroyed by Nim,
# so nothing to do here.
discard
proc refcountIncremented(obj: ptr GodotObject, methodData: pointer,
userData: pointer, numArgs: cint,
args: var array[MAX_ARG_COUNT, ptr GodotVariant]):
GodotVariant {.noconv.} =
let nimObj = cast[NimGodotObject](userData)
if not nimObj.isFinalized:
GC_ref(nimObj)
proc refcountDecremented(obj: ptr GodotObject, methodData: pointer,
userData: pointer, numArgs: cint,
args: var array[MAX_ARG_COUNT, ptr GodotVariant]):
GodotVariant {.noconv.} =
let nimObj = cast[NimGodotObject](userData)
if not nimObj.isFinalized:
GC_unref(nimObj)
initGodotVariant(result, nimObj.isFinalized)
template registerGodotClass(classNameIdent, classNameLit; isRefClass: bool;
baseNameLit, createFuncIdent; isTool: bool) =
proc createFuncIdent(obj: ptr GodotObject,
methData: pointer): pointer {.noconv.} =
var nimObj: classNameIdent
new(nimObj, nimGodotObjectFinalizer[classNameIdent])
nimObj.setGodotObject(obj)
nimObj.isRef = when isRefClass: true else: false
nimObj.setNativeObject(asNimGodotObject[NimGodotObject](
obj, forceNativeObject = true))
GC_ref(nimObj)
result = cast[pointer](nimObj)
when compiles(nimObj.init()):
nimObj.init()
let createFuncObj = GodotInstanceCreateFunc(
createFunc: createFuncIdent
)
let destroyFuncObj = GodotInstanceDestroyFunc(
destroyFunc: when isRefClass: nimDestroyRefFunc else: nimDestroyFunc
)
registerClass(classNameIdent, classNameLit, false)
when isTool:
nativeScriptRegisterToolClass(getNativeLibHandle(), classNameLit,
baseNameLit, createFuncObj, destroyFuncObj)
else:
nativeScriptRegisterClass(getNativeLibHandle(), classNameLit,
baseNameLit, createFuncObj, destroyFuncObj)
template registerGodotField(classNameLit, classNameIdent, propNameLit,
propNameIdent, propTypeLit, propTypeIdent,
setFuncIdent, getFuncIdent, hintStrLit,
hintIdent, usageExpr, hasDefaultValue,
defaultValueNode) =
proc setFuncIdent(obj: ptr GodotObject, methData: pointer,
nimPtr: pointer, val: GodotVariant) {.noconv.} =
let variant = newVariant(val)
variant.markNoDeinit()
let (nimVal, err) = godotToNim[propTypeIdent](variant)
case err:
of ConversionResult.OK:
cast[classNameIdent](nimPtr).propNameIdent = nimVal
of ConversionResult.TypeError:
let errStr = typeError(propTypeLit, $val, val.getType(),
classNameLit, astToStr(propNameIdent))
printError(errStr)
of ConversionResult.RangeError:
let errStr = rangeError(propTypeLit, $val,
classNameLit, astToStr(propNameIdent))
printError(errStr)
proc getFuncIdent(obj: ptr GodotObject, methData: pointer,
nimPtr: pointer): GodotVariant {.noconv.} =
let variant = nimToGodot(cast[classNameIdent](nimPtr).propNameIdent)
variant.markNoDeinit()
result = variant.godotVariant[]
let setFunc = GodotPropertySetFunc(
setFunc: setFuncIdent
)
let getFunc = GodotPropertyGetFunc(
getFunc: getFuncIdent
)
mixin godotTypeInfo
static:
var typeInfo: GodotTypeInfo
when compiles(godotTypeInfo(propTypeIdent)):
typeInfo = godotTypeInfo(propTypeIdent)
const hintStr = when hintStrLit != "NIM":
hintStrLit
else:
typeInfo.hintStr
const hint = when astToStr(hintIdent) != "NIM":
GodotPropertyHint.hintIdent
else:
typeInfo.hint
const variantType = typeInfo.variantType
var hintStrGodot = hintStr.toGodotString()
{.push warning[ProveInit]: off.} # false warning, Nim bug
var attr = GodotPropertyAttributes(
typ: ord(variantType),
hint: hint,
hintString: hintStrGodot,
usage: usageExpr
)
{.push warning[ProveInit]: on.}
when hasDefaultValue:
attr.defaultValue = (defaultValueNode).toVariant().godotVariant[]
nativeScriptRegisterProperty(getNativeLibHandle(), classNameLit, propNameLit,
unsafeAddr attr, setFunc, getFunc)
hintStrGodot.deinit()
proc toGodotStyle(s: string): string {.compileTime.} =
result = newStringOfCap(s.len + 10)
for c in s:
if c.isUpperAscii():
result.add('_')
result.add(c.toLowerAscii())
else:
result.add(c)
proc genType(obj: ObjectDecl): NimNode {.compileTime.} =
result = newNimNode(nnkStmtList)
# Nim type definition
let typeDef = newNimNode(nnkTypeDef)
result.add(newNimNode(nnkTypeSection).add(typeDef))
typeDef.add(postfix(ident(obj.name), "*"))
typeDef.add(newEmptyNode())
let objTy = newNimNode(nnkObjectTy)
typeDef.add(newNimNode(nnkRefTy).add(objTy))
objTy.add(newEmptyNode())
if obj.parentName == "":
objTy.add(newEmptyNode())
else:
objTy.add(newNimNode(nnkOfInherit).add(ident(obj.parentName)))
let recList = newNimNode(nnkRecList)
objTy.add(recList)
let initBody = newStmtList()
for decl in obj.fields:
if not decl.defaultValue.isNil and decl.defaultValue.kind != nnkEmpty:
initBody.add(newNimNode(nnkAsgn).add(decl.name, decl.defaultValue))
let name = if not decl.isExported: decl.name
else: postfix(decl.name, "*")
recList.add(newIdentDefs(name, decl.typ))
# Add default values and/or super call to init method
initBody.insert(0, newNimNode(nnkCommand).add(ident("procCall"),
newCall("init", newCall(obj.parentName, ident("self")))))
var initMethod: NimNode
for meth in obj.methods:
if meth.name == "init" and meth.nimNode[3].len == 1:
initMethod = meth.nimNode
break
if initMethod.isNil:
obj.methods.add(MethodDecl(
name: "init",
args: newSeq[VarDecl](),
returnType: newEmptyNode(),
isVirtual: true,
isNoGodot: false,
nimNode: newProc(postfix(ident("init"), "*"), body = initBody,
procType = nnkMethodDef)
))
else:
initMethod.body.insert(0, initBody)
# {.this: self.} for convenience
result.add(newNimNode(nnkPragma).add(newNimNode(nnkExprColonExpr).add(
ident("this"), ident("self")
)))
# Nim proc defintions
var decls = newSeqOfCap[NimNode](obj.methods.len)
for meth in obj.methods:
let selfArg = newIdentDefs(ident("self"), ident(obj.name))
meth.nimNode.params.insert(1, selfArg)
let decl = meth.nimNode.copyNimTree()
decl.body = newEmptyNode()
decl.addPragma(ident("gcsafe"))
decl.addPragma(newNimNode(nnkExprColonExpr).add(ident("locks"),
newIntLitNode(0)))
decls.add(decl)
# add forward declarations first
for decl in decls:
result.add(decl)
for meth in obj.methods:
result.add(meth.nimNode)
# Register Godot object
let parentName = if obj.parentName == "": newStrLitNode("Object")
else: newStrLitNode(obj.parentName)
let classNameLit = newStrLitNode(obj.name)
let classNameIdent = ident(obj.name)
let isRef: bool = if obj.parentName == "": false
else: obj.parentName in refClasses
result.add(getAst(
registerGodotClass(classNameIdent, classNameLit, newLit(isRef), parentName,
genSym(nskProc, "createFunc"), newLit(obj.isTool))))
# Register fields (properties)
for field in obj.fields:
if field.isNoGodot: continue
let hintStr = if field.hintStr == "": "NIM"
else: field.hintStr
let hint = if field.hint == "": "NIM"
else: field.hint
let usage =
if field.usage.isNil:
newLit(ord(GodotPropertyUsage.Default) or
ord(GodotPropertyUsage.ScriptVariable))
else: field.usage
let hasDefaultValue = not field.defaultValue.isNil and
field.defaultValue.kind != nnkEmpty
let hintIdent = ident(hint)
let nimType = repr field.typ
result.add(getAst(
registerGodotField(classNameLit, classNameIdent,
newCStringLit(toGodotStyle($field.name.basename())),
ident(field.name), nimType,
field.typ, genSym(nskProc, "setFunc"),
genSym(nskProc, "getFunc"),
newStrLitNode(hintStr), hintIdent, usage,
ident($hasDefaultValue), field.defaultValue)))
# Register methods
template registerGodotMethod(classNameLit, classNameIdent, methodNameIdent,
methodNameLit, minArgs, maxArgs,
argTypes, methFuncIdent, hasReturnValue) =
{.emit: """/*TYPESECTION*/
N_NOINLINE(void, nimGC_setStackBottom)(void* thestackbottom);
""".}
proc methFuncIdent(obj: ptr GodotObject, methodData: pointer,
userData: pointer, numArgs: cint,
args: var array[MAX_ARG_COUNT, ptr GodotVariant]):
GodotVariant {.noconv.} =
var stackBottom {.volatile.}: pointer
{.emit: """
nimGC_setStackBottom((void*)(&`stackBottom`));
""".}
let self = cast[classNameIdent](userData)
const isStaticCall = methodNameLit == cstring"_ready" or
methodNameLit == cstring"_process" or
methodNameLit == cstring"_fixed_process" or
methodNameLit == cstring"_enter_tree" or
methodNameLit == cstring"_exit_tree" or
methodNameLit == cstring"_enter_world" or
methodNameLit == cstring"_exit_world" or
methodNameLit == cstring"_draw"
when defined(release):
invokeVarArgs(methodNameIdent, self, minArgs, maxArgs, numArgs,
args, argTypes, hasReturnValue, isStaticCall)
else:
try:
invokeVarArgs(methodNameIdent, self, minArgs, maxArgs, numArgs,
args, argTypes, hasReturnValue, isStaticCall)
except:
let ex = getCurrentException()
printError("Unhandled Nim exception (" & $ex.name & "): " &
ex.msg & "\n" & ex.getStackTrace())
let meth = GodotInstanceMethod(
meth: methFuncIdent
)
nativeScriptRegisterMethod(getNativeLibHandle(), classNameLit,
methodNameLit, GodotMethodAttributes(), meth)
for meth in obj.methods:
if meth.isNoGodot: continue
let maxArgs = meth.args.len
var minArgs = maxArgs
var argTypes = newSeq[NimNode]()
for idx, arg in meth.args:
if minArgs == maxArgs and not arg.defaultValue.isNil and
arg.defaultValue.kind != nnkEmpty:
minArgs = idx
argTypes.add(arg.typ)
let godotMethodName = if meth.isVirtual: "_" & toGodotStyle(meth.name)
else: toGodotStyle(meth.name)
let hasReturnValueBool = not (meth.returnType.isNil or
meth.returnType.kind == nnkEmpty or
(meth.returnType.kind == nnkIdent and
meth.returnType.ident == toNimIdent("void")))
let hasReturnValue = if hasReturnValueBool: ident("true")
else: ident("false")
result.add(getAst(
registerGodotMethod(classNameLit, classNameIdent, ident(meth.name),
newCStringLit(godotMethodName), minArgs, maxArgs,
argTypes, genSym(nskProc, "methFunc"),
hasReturnValue)))
if isRef:
# add ref/unref for types inherited from Reference
template registerRefIncDec(classNameLit) =
let refInc = GodotInstanceMethod(
meth: refcountIncremented
)
let refDec = GodotInstanceMethod(
meth: refcountDecremented
)
nativeScriptRegisterMethod(getNativeLibHandle(), classNameLit,
cstring"_refcount_incremented",
GodotMethodAttributes(), refInc)
nativeScriptRegisterMethod(getNativeLibHandle(), classNameLit,
cstring"_refcount_decremented",
GodotMethodAttributes(), refDec)
result.add(getAst(registerRefIncDec(classNameLit)))
{.push warning[Deprecated]: off.}
# immediate macros are deprecated, but `untyped` doesn't make it immediate,
# as the warning and the documentation claim.
macro gdobj*(definition: untyped, body: typed): typed {.immediate.} =
## Generates Godot type. Self-documenting example:
##
## .. code-block:: nim
## import godot, node
##
## gdobj MyObj of Node:
## var myField: int
## ## Not exported to Godot (i.e. editor will not see this field).
##
## var myString* {.gdExport, hint: Length, hintStr: "20".}: string
## ## Exported to Godot as ``my_string``.
## ## Editor will limit this string to length 20.
## ## ``hint` is a value of ``GodotPropertyHint`` enum.
## ## ``hintStr`` depends on the value of ``hint``, its format is
## ## described in ``GodotPropertyHint`` documentation.
##
## method ready*() =
## ## Exported methods are exported to Godot by default,
## ## and their Godot names are prefixed with ``_``
## ## (in this case ``_ready``)
## print("I am ready! myString is: " & myString)
##
## proc myProc*() {.gdExport.} =
## ## Exported to Godot as ``my_proc``
## print("myProc is called! Incrementing myField.")
## inc myField
##
## If parent type is omitted, the type is inherited from ``Object``.
##
## ``tool`` specifier can be added to mark the type as an
## `editor plugin <https://godot.readthedocs.io/en/stable/development/plugins/making_plugins.html>`_:
##
## .. code-block:: nim
## import godot, editor_plugin
##
## gdobj(MyTool of EditorPlugin, tool):
## method enterTree*() =
## print("MyTool initialized!")
##
## Objects can be instantiated by invoking
## `gdnew <godotnim.html#gdnew>`_ or by using
## `load <godotapi/resource_loader.html#load,string,string,bool>`_ or any other way
## that you can find in `Godot API <index.html#modules-godot-api>`_.
let typeDef = parseType(definition, callsite())
result = genType(typeDef)
{.push warning[Deprecated]: on.}