2018-12-18 15:39:39 +00:00
|
|
|
import
|
2020-05-23 17:18:20 +00:00
|
|
|
std/[macros, tables, hashes]
|
2018-12-18 15:39:39 +00:00
|
|
|
|
|
|
|
export
|
|
|
|
macros
|
|
|
|
|
|
|
|
type
|
|
|
|
FieldDescription* = object
|
|
|
|
name*: NimNode
|
|
|
|
isPublic*: bool
|
2019-10-22 16:06:53 +00:00
|
|
|
isDiscriminator*: bool
|
2018-12-18 15:39:39 +00:00
|
|
|
typ*: NimNode
|
|
|
|
pragmas*: NimNode
|
2018-12-19 10:27:48 +00:00
|
|
|
caseField*: NimNode
|
|
|
|
caseBranch*: NimNode
|
|
|
|
|
|
|
|
const
|
|
|
|
nnkPragmaCallKinds = {nnkExprColonExpr, nnkCall, nnkCallStrLit}
|
|
|
|
|
2020-05-23 17:18:20 +00:00
|
|
|
proc hash*(x: LineInfo): Hash =
|
|
|
|
!$(hash(x.filename) !& hash(x.line) !& hash(x.column))
|
|
|
|
|
|
|
|
var
|
|
|
|
# Please note that we are storing NimNode here in order to
|
|
|
|
# incur the code rendering cost only on a successful compilation.
|
|
|
|
macroLocations {.compileTime.} = newSeq[LineInfo]()
|
|
|
|
macroOutputs {.compileTime.} = newSeq[NimNode]()
|
|
|
|
|
2020-05-23 21:49:12 +00:00
|
|
|
proc writeMacroResultsNow* {.compileTime.} =
|
2020-05-23 17:18:20 +00:00
|
|
|
var files = initTable[string, NimNode]()
|
|
|
|
|
|
|
|
proc addToFile(file: var NimNode, location: LineInfo, macroOutput: NimNode) =
|
|
|
|
if file == nil:
|
|
|
|
file = newNimNode(nnkStmtList, macroOutput)
|
|
|
|
|
2020-05-23 21:49:12 +00:00
|
|
|
file.add newCommentStmtNode("Generated at line " & $location.line)
|
2020-05-23 17:18:20 +00:00
|
|
|
file.add macroOutput
|
|
|
|
|
|
|
|
for i in 0..< macroLocations.len:
|
|
|
|
addToFile files.mgetOrPut(macroLocations[i].filename, nil),
|
|
|
|
macroLocations[i], macroOutputs[i]
|
|
|
|
|
|
|
|
for name, contents in files:
|
|
|
|
let targetFile = name & ".generated.nim"
|
|
|
|
writeFile(targetFile, repr(contents))
|
|
|
|
hint "Wrote macro output to " & targetFile, contents
|
|
|
|
|
2020-05-23 21:49:12 +00:00
|
|
|
proc storeMacroResult*(callSite: LineInfo,
|
|
|
|
macroResult: NimNode,
|
|
|
|
writeOutputImmediately = false) =
|
|
|
|
macroLocations.add callSite
|
|
|
|
macroOutputs.add macroResult
|
|
|
|
if writeOutputImmediately:
|
|
|
|
# echo macroResult.repr
|
|
|
|
writeMacroResultsNow()
|
|
|
|
|
|
|
|
proc storeMacroResult*(macroResult: NimNode, writeOutputImmediately = false) =
|
|
|
|
let usageSite = callsite().lineInfoObj
|
|
|
|
storeMacroResult(usageSite, macroResult, writeOutputImmediately)
|
|
|
|
|
|
|
|
macro dumpMacroResults*: untyped =
|
|
|
|
writeMacroResultsNow()
|
|
|
|
|
2018-12-19 10:27:48 +00:00
|
|
|
proc findPragma*(pragmas: NimNode, pragmaSym: NimNode): NimNode =
|
|
|
|
for p in pragmas:
|
2019-06-05 00:14:54 +00:00
|
|
|
if p.kind in {nnkSym, nnkIdent} and eqIdent(p, pragmaSym):
|
2018-12-19 10:27:48 +00:00
|
|
|
return p
|
2019-06-05 00:14:54 +00:00
|
|
|
if p.kind in nnkPragmaCallKinds and p.len > 0 and eqIdent(p[0], pragmaSym):
|
2018-12-19 10:27:48 +00:00
|
|
|
return p
|
2018-12-18 15:39:39 +00:00
|
|
|
|
2019-08-02 12:34:54 +00:00
|
|
|
func isTuple*(t: NimNode): bool =
|
2019-08-14 15:29:14 +00:00
|
|
|
t.kind == nnkBracketExpr and t[0].kind == nnkSym and eqIdent(t[0], "tuple")
|
|
|
|
|
|
|
|
macro isTuple*(T: type): untyped =
|
|
|
|
newLit(isTuple(getType(T)[1]))
|
2019-08-02 12:34:54 +00:00
|
|
|
|
2020-05-26 15:42:33 +00:00
|
|
|
proc skipRef*(T: NimNode): NimNode =
|
|
|
|
result = T
|
|
|
|
if T.kind == nnkBracketExpr:
|
|
|
|
if eqIdent(T[0], bindSym"ref"):
|
|
|
|
result = T[1]
|
|
|
|
|
2019-07-02 23:30:30 +00:00
|
|
|
template readPragma*(field: FieldDescription, pragmaName: static string): NimNode =
|
2019-07-03 07:15:40 +00:00
|
|
|
let p = findPragma(field.pragmas, bindSym(pragmaName))
|
2019-07-02 23:30:30 +00:00
|
|
|
if p != nil and p.len == 2: p[1] else: p
|
|
|
|
|
2020-05-26 15:42:33 +00:00
|
|
|
proc collectFieldsFromRecList(result: var seq[FieldDescription],
|
|
|
|
n: NimNode,
|
|
|
|
parentCaseField: NimNode = nil,
|
|
|
|
parentCaseBranch: NimNode = nil,
|
|
|
|
isDiscriminator = false) =
|
2020-04-09 17:39:02 +00:00
|
|
|
case n.kind
|
|
|
|
of nnkRecList:
|
|
|
|
for entry in n:
|
2020-05-26 15:42:33 +00:00
|
|
|
collectFieldsFromRecList result, entry,
|
|
|
|
parentCaseField, parentCaseBranch
|
2020-04-09 17:39:02 +00:00
|
|
|
of nnkRecWhen:
|
|
|
|
for branch in n:
|
|
|
|
case branch.kind:
|
|
|
|
of nnkElifBranch:
|
2020-05-26 15:42:33 +00:00
|
|
|
collectFieldsFromRecList result, branch[1],
|
|
|
|
parentCaseField, parentCaseBranch
|
2020-04-09 17:39:02 +00:00
|
|
|
of nnkElse:
|
2020-05-26 15:42:33 +00:00
|
|
|
collectFieldsFromRecList result, branch[0],
|
|
|
|
parentCaseField, parentCaseBranch
|
2020-04-09 17:39:02 +00:00
|
|
|
else:
|
|
|
|
doAssert false
|
|
|
|
|
|
|
|
of nnkRecCase:
|
2020-05-26 15:42:33 +00:00
|
|
|
collectFieldsFromRecList result, n[0],
|
|
|
|
parentCaseField,
|
|
|
|
parentCaseBranch,
|
|
|
|
isDiscriminator = true
|
2020-04-09 17:39:02 +00:00
|
|
|
|
|
|
|
for i in 1 ..< n.len:
|
|
|
|
let branch = n[i]
|
|
|
|
case branch.kind
|
|
|
|
of nnkOfBranch:
|
2020-05-26 15:42:33 +00:00
|
|
|
collectFieldsFromRecList result, branch[^1], n[0], branch
|
2020-04-09 17:39:02 +00:00
|
|
|
of nnkElse:
|
2020-05-26 15:42:33 +00:00
|
|
|
collectFieldsFromRecList result, branch[0], n[0], branch
|
2020-04-09 17:39:02 +00:00
|
|
|
else:
|
|
|
|
doAssert false
|
|
|
|
|
|
|
|
of nnkIdentDefs:
|
|
|
|
let fieldType = n[^2]
|
|
|
|
for i in 0 ..< n.len - 2:
|
|
|
|
var field: FieldDescription
|
|
|
|
field.name = n[i]
|
|
|
|
field.typ = fieldType
|
|
|
|
field.caseField = parentCaseField
|
|
|
|
field.caseBranch = parentCaseBranch
|
|
|
|
field.isDiscriminator = isDiscriminator
|
|
|
|
|
|
|
|
if field.name.kind == nnkPragmaExpr:
|
|
|
|
field.pragmas = field.name[1]
|
|
|
|
field.name = field.name[0]
|
|
|
|
|
|
|
|
if field.name.kind == nnkPostfix:
|
|
|
|
field.isPublic = true
|
|
|
|
field.name = field.name[1]
|
|
|
|
|
|
|
|
result.add field
|
|
|
|
|
2020-05-25 17:32:29 +00:00
|
|
|
of nnkSym:
|
|
|
|
result.add FieldDescription(
|
|
|
|
name: n,
|
|
|
|
typ: getType(n),
|
|
|
|
caseField: parentCaseField,
|
|
|
|
caseBranch: parentCaseBranch,
|
|
|
|
isDiscriminator: isDiscriminator)
|
|
|
|
|
2020-04-09 17:39:02 +00:00
|
|
|
of nnkNilLit, nnkDiscardStmt, nnkCommentStmt, nnkEmpty:
|
|
|
|
discard
|
|
|
|
|
|
|
|
else:
|
|
|
|
doAssert false, "Unexpected nodes in recordFields:\n" & n.treeRepr
|
|
|
|
|
2020-05-26 15:42:33 +00:00
|
|
|
proc collectFieldsInHierarchy(result: var seq[FieldDescription],
|
|
|
|
objectType: NimNode) =
|
|
|
|
var objectType = objectType
|
|
|
|
|
2020-05-27 08:30:56 +00:00
|
|
|
objectType.expectKind {nnkObjectTy, nnkRefTy}
|
|
|
|
|
2020-05-26 15:42:33 +00:00
|
|
|
if objectType.kind == nnkRefTy:
|
|
|
|
objectType = objectType[0]
|
|
|
|
|
|
|
|
objectType.expectKind nnkObjectTy
|
|
|
|
|
|
|
|
var baseType = objectType[1]
|
|
|
|
if baseType.kind != nnkEmpty:
|
|
|
|
baseType.expectKind nnkOfInherit
|
|
|
|
baseType = baseType[0]
|
|
|
|
baseType.expectKind nnkSym
|
|
|
|
baseType = getImpl(baseType)
|
|
|
|
baseType.expectKind nnkTypeDef
|
|
|
|
baseType = baseType[2]
|
2020-05-27 08:30:56 +00:00
|
|
|
baseType.expectKind {nnkObjectTy, nnkRefTy}
|
2020-05-26 15:42:33 +00:00
|
|
|
collectFieldsInHierarchy result, baseType
|
|
|
|
|
|
|
|
let recList = objectType[2]
|
|
|
|
collectFieldsFromRecList result, recList
|
|
|
|
|
2019-08-12 14:49:39 +00:00
|
|
|
proc recordFields*(typeImpl: NimNode): seq[FieldDescription] =
|
2019-08-02 12:34:54 +00:00
|
|
|
if typeImpl.isTuple:
|
2019-08-12 14:49:39 +00:00
|
|
|
for i in 1 ..< typeImpl.len:
|
|
|
|
result.add FieldDescription(typ: typeImpl[i], name: ident("Field" & $(i - 1)))
|
|
|
|
return
|
|
|
|
|
2020-05-26 15:42:33 +00:00
|
|
|
typeImpl.expectKind nnkTypeDef
|
|
|
|
collectFieldsInHierarchy(result, typeImpl[2])
|
2018-12-18 15:39:39 +00:00
|
|
|
|
2019-07-02 23:30:30 +00:00
|
|
|
macro field*(obj: typed, fieldName: static string): untyped =
|
|
|
|
newDotExpr(obj, ident fieldName)
|
|
|
|
|
2019-01-23 11:47:13 +00:00
|
|
|
proc skipPragma*(n: NimNode): NimNode =
|
|
|
|
if n.kind == nnkPragmaExpr: n[0]
|
|
|
|
else: n
|
|
|
|
|
2020-05-20 10:42:29 +00:00
|
|
|
proc getPragma(T: NimNode, lookedUpField: string, pragma: NimNode): NimNode =
|
2019-08-12 14:49:39 +00:00
|
|
|
let Tresolved = getType(T)[1]
|
|
|
|
if isTuple(Tresolved):
|
2020-05-20 10:42:29 +00:00
|
|
|
return nil
|
2019-07-10 10:36:56 +00:00
|
|
|
|
2019-08-12 14:49:39 +00:00
|
|
|
for f in recordFields(Tresolved.getImpl):
|
2019-07-30 23:43:35 +00:00
|
|
|
var fieldName = f.name
|
|
|
|
# TODO: Fix this in eqIdent
|
|
|
|
if fieldName.kind == nnkAccQuoted: fieldName = fieldName[0]
|
2020-05-20 10:42:29 +00:00
|
|
|
if eqIdent(fieldName, lookedUpField):
|
|
|
|
return f.pragmas.findPragma(pragma)
|
|
|
|
|
|
|
|
error "The type " & $Tresolved & " doesn't have a field named " & lookedUpField
|
2019-07-10 10:36:56 +00:00
|
|
|
|
2020-05-20 10:42:29 +00:00
|
|
|
macro getCustomPragmaFixed*(T: type, field: static string, pragma: typed{nkSym}): untyped =
|
2021-02-10 13:10:08 +00:00
|
|
|
result = nil
|
2020-05-20 10:42:29 +00:00
|
|
|
let p = getPragma(T, field, pragma)
|
2021-02-10 13:10:08 +00:00
|
|
|
|
|
|
|
if p != nil and p.len > 0:
|
|
|
|
if p.len == 2:
|
|
|
|
result = p[1]
|
|
|
|
else:
|
|
|
|
let def = p[0].getImpl[3]
|
|
|
|
result = newTree(nnkPar)
|
|
|
|
for i in 1 ..< def.len:
|
|
|
|
let key = def[i][0]
|
|
|
|
let val = p[i]
|
|
|
|
result.add newTree(nnkExprColonExpr, key, val)
|
2020-05-20 10:42:29 +00:00
|
|
|
|
|
|
|
macro hasCustomPragmaFixed*(T: type, field: static string, pragma: typed{nkSym}): untyped =
|
|
|
|
newLit(getPragma(T, field, pragma) != nil)
|
2019-07-10 10:36:56 +00:00
|
|
|
|
2019-07-24 12:34:48 +00:00
|
|
|
proc humaneTypeName*(typedescNode: NimNode): string =
|
|
|
|
var t = getType(typedescNode)[1]
|
|
|
|
if t.kind != nnkBracketExpr:
|
|
|
|
let tImpl = t.getImpl
|
|
|
|
if tImpl != nil and tImpl.kind notin {nnkEmpty, nnkNilLit}:
|
|
|
|
t = tImpl
|
|
|
|
|
|
|
|
repr(t)
|
|
|
|
|
|
|
|
macro inspectType*(T: typed): untyped =
|
|
|
|
echo "Inspect type: ", humaneTypeName(T)
|
|
|
|
|
2018-12-19 10:27:48 +00:00
|
|
|
# FIXED NewLit
|
|
|
|
|
|
|
|
proc newLitFixed*(c: char): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
## Produces a new character literal node.
|
2018-12-19 10:27:48 +00:00
|
|
|
result = newNimNode(nnkCharLit)
|
|
|
|
result.intVal = ord(c)
|
|
|
|
|
|
|
|
proc newLitFixed*(i: int): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
## Produces a new integer literal node.
|
2018-12-19 10:27:48 +00:00
|
|
|
result = newNimNode(nnkIntLit)
|
|
|
|
result.intVal = i
|
|
|
|
|
|
|
|
proc newLitFixed*(i: int8): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
## Produces a new integer literal node.
|
2018-12-19 10:27:48 +00:00
|
|
|
result = newNimNode(nnkInt8Lit)
|
|
|
|
result.intVal = i
|
|
|
|
|
|
|
|
proc newLitFixed*(i: int16): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
## Produces a new integer literal node.
|
2018-12-19 10:27:48 +00:00
|
|
|
result = newNimNode(nnkInt16Lit)
|
|
|
|
result.intVal = i
|
|
|
|
|
|
|
|
proc newLitFixed*(i: int32): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
## Produces a new integer literal node.
|
2018-12-19 10:27:48 +00:00
|
|
|
result = newNimNode(nnkInt32Lit)
|
|
|
|
result.intVal = i
|
|
|
|
|
|
|
|
proc newLitFixed*(i: int64): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
## Produces a new integer literal node.
|
2018-12-19 10:27:48 +00:00
|
|
|
result = newNimNode(nnkInt64Lit)
|
|
|
|
result.intVal = i
|
|
|
|
|
|
|
|
proc newLitFixed*(i: uint): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
## Produces a new unsigned integer literal node.
|
2018-12-19 10:27:48 +00:00
|
|
|
result = newNimNode(nnkUIntLit)
|
|
|
|
result.intVal = BiggestInt(i)
|
|
|
|
|
|
|
|
proc newLitFixed*(i: uint8): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
## Produces a new unsigned integer literal node.
|
2018-12-19 10:27:48 +00:00
|
|
|
result = newNimNode(nnkUInt8Lit)
|
|
|
|
result.intVal = BiggestInt(i)
|
|
|
|
|
|
|
|
proc newLitFixed*(i: uint16): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
## Produces a new unsigned integer literal node.
|
2018-12-19 10:27:48 +00:00
|
|
|
result = newNimNode(nnkUInt16Lit)
|
|
|
|
result.intVal = BiggestInt(i)
|
|
|
|
|
|
|
|
proc newLitFixed*(i: uint32): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
## Produces a new unsigned integer literal node.
|
2018-12-19 10:27:48 +00:00
|
|
|
result = newNimNode(nnkUInt32Lit)
|
|
|
|
result.intVal = BiggestInt(i)
|
|
|
|
|
|
|
|
proc newLitFixed*(i: uint64): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
## Produces a new unsigned integer literal node.
|
2018-12-19 10:27:48 +00:00
|
|
|
result = newNimNode(nnkUInt64Lit)
|
|
|
|
result.intVal = BiggestInt(i)
|
|
|
|
|
|
|
|
proc newLitFixed*(b: bool): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
## Produces a new boolean literal node.
|
2018-12-19 10:27:48 +00:00
|
|
|
result = if b: bindSym"true" else: bindSym"false"
|
|
|
|
|
2019-10-28 12:30:49 +00:00
|
|
|
proc newLitFixed*(s: string): NimNode {.compileTime.} =
|
|
|
|
## Produces a new string literal node.
|
|
|
|
result = newNimNode(nnkStrLit)
|
|
|
|
result.strVal = s
|
|
|
|
|
|
|
|
when false:
|
|
|
|
# the float type is not really a distinct type as described in https://github.com/nim-lang/Nim/issues/5875
|
|
|
|
proc newLitFixed*(f: float): NimNode {.compileTime.} =
|
|
|
|
## Produces a new float literal node.
|
|
|
|
result = newNimNode(nnkFloatLit)
|
|
|
|
result.floatVal = f
|
|
|
|
|
2018-12-19 10:27:48 +00:00
|
|
|
proc newLitFixed*(f: float32): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
## Produces a new float literal node.
|
2018-12-19 10:27:48 +00:00
|
|
|
result = newNimNode(nnkFloat32Lit)
|
|
|
|
result.floatVal = f
|
|
|
|
|
|
|
|
proc newLitFixed*(f: float64): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
## Produces a new float literal node.
|
2018-12-19 10:27:48 +00:00
|
|
|
result = newNimNode(nnkFloat64Lit)
|
|
|
|
result.floatVal = f
|
|
|
|
|
2019-10-28 12:30:49 +00:00
|
|
|
when declared(float128):
|
|
|
|
proc newLitFixed*(f: float128): NimNode {.compileTime.} =
|
|
|
|
## Produces a new float literal node.
|
|
|
|
result = newNimNode(nnkFloat128Lit)
|
|
|
|
result.floatVal = f
|
|
|
|
|
|
|
|
proc newLitFixed*(arg: enum): NimNode {.compileTime.} =
|
|
|
|
result = newCall(
|
|
|
|
arg.type.getTypeInst[1],
|
|
|
|
newLitFixed(int(arg))
|
|
|
|
)
|
2018-12-19 10:27:48 +00:00
|
|
|
|
|
|
|
proc newLitFixed*[N,T](arg: array[N,T]): NimNode {.compileTime.}
|
|
|
|
proc newLitFixed*[T](arg: seq[T]): NimNode {.compileTime.}
|
2019-10-28 12:30:49 +00:00
|
|
|
proc newLitFixed*[T](s: set[T]): NimNode {.compileTime.}
|
2018-12-19 10:27:48 +00:00
|
|
|
proc newLitFixed*(arg: tuple): NimNode {.compileTime.}
|
|
|
|
|
|
|
|
proc newLitFixed*(arg: object): NimNode {.compileTime.} =
|
|
|
|
result = nnkObjConstr.newTree(arg.type.getTypeInst[1])
|
|
|
|
for a, b in arg.fieldPairs:
|
|
|
|
result.add nnkExprColonExpr.newTree( newIdentNode(a), newLitFixed(b) )
|
|
|
|
|
2019-10-28 12:30:49 +00:00
|
|
|
proc newLitFixed*(arg: ref object): NimNode {.compileTime.} =
|
|
|
|
## produces a new ref type literal node.
|
|
|
|
result = nnkObjConstr.newTree(arg.type.getTypeInst[1])
|
|
|
|
for a, b in fieldPairs(arg[]):
|
|
|
|
result.add nnkExprColonExpr.newTree(newIdentNode(a), newLitFixed(b))
|
|
|
|
|
2018-12-19 10:27:48 +00:00
|
|
|
proc newLitFixed*[N,T](arg: array[N,T]): NimNode {.compileTime.} =
|
|
|
|
result = nnkBracket.newTree
|
|
|
|
for x in arg:
|
|
|
|
result.add newLitFixed(x)
|
|
|
|
|
|
|
|
proc newLitFixed*[T](arg: seq[T]): NimNode {.compileTime.} =
|
2019-10-28 12:30:49 +00:00
|
|
|
let bracket = nnkBracket.newTree
|
2018-12-19 10:27:48 +00:00
|
|
|
for x in arg:
|
|
|
|
bracket.add newLitFixed(x)
|
2019-10-28 12:30:49 +00:00
|
|
|
result = nnkPrefix.newTree(
|
|
|
|
bindSym"@",
|
2018-12-19 10:27:48 +00:00
|
|
|
bracket
|
|
|
|
)
|
2019-10-28 12:30:49 +00:00
|
|
|
if arg.len == 0:
|
|
|
|
# add type cast for empty seq
|
|
|
|
var typ = getTypeInst(typeof(arg))[1]
|
|
|
|
result = newCall(typ,result)
|
|
|
|
|
|
|
|
proc newLitFixed*[T](s: set[T]): NimNode {.compileTime.} =
|
|
|
|
result = nnkCurly.newTree
|
|
|
|
for x in s:
|
|
|
|
result.add newLitFixed(x)
|
2018-12-19 10:27:48 +00:00
|
|
|
|
|
|
|
proc newLitFixed*(arg: tuple): NimNode {.compileTime.} =
|
|
|
|
result = nnkPar.newTree
|
|
|
|
for a,b in arg.fieldPairs:
|
|
|
|
result.add nnkExprColonExpr.newTree(newIdentNode(a), newLitFixed(b))
|
|
|
|
|
2019-03-11 09:37:51 +00:00
|
|
|
iterator typedParams*(n: NimNode, skip = 0): (NimNode, NimNode) =
|
2019-05-22 06:50:49 +00:00
|
|
|
let params = n[3]
|
|
|
|
for i in (1 + skip) ..< params.len:
|
|
|
|
let paramNodes = params[i]
|
2019-03-11 09:37:51 +00:00
|
|
|
let paramType = paramNodes[^2]
|
|
|
|
|
|
|
|
for j in 0 ..< paramNodes.len - 2:
|
2020-05-23 16:55:55 +00:00
|
|
|
yield (skipPragma paramNodes[j], paramType)
|
2019-03-11 09:37:51 +00:00
|
|
|
|
2020-05-23 17:18:20 +00:00
|
|
|
iterator baseTypes*(exceptionType: NimNode): NimNode =
|
|
|
|
var typ = exceptionType
|
|
|
|
while typ != nil:
|
|
|
|
let impl = getImpl(typ)
|
|
|
|
if impl.len != 3 or impl[2].kind != nnkObjectTy:
|
|
|
|
break
|
|
|
|
|
|
|
|
let objType = impl[2]
|
|
|
|
if objType[1].kind != nnkOfInherit:
|
|
|
|
break
|
|
|
|
|
|
|
|
typ = objType[1][0]
|
|
|
|
yield typ
|
|
|
|
|
2021-11-09 15:17:50 +00:00
|
|
|
macro unpackArgs*(callee: untyped, args: untyped): untyped =
|
2019-07-18 18:30:03 +00:00
|
|
|
result = newCall(callee)
|
|
|
|
for arg in args:
|
|
|
|
let arg = if arg.kind == nnkHiddenStdConv: arg[1]
|
|
|
|
else: arg
|
|
|
|
if arg.kind == nnkArgList:
|
|
|
|
for subarg in arg:
|
|
|
|
result.add subarg
|
|
|
|
else:
|
|
|
|
result.add arg
|
|
|
|
|
2020-07-07 21:32:28 +00:00
|
|
|
template genExpr*(treeType: NimNodeKind, body: untyped): untyped =
|
|
|
|
iterator generator: NimNode = body
|
|
|
|
|
|
|
|
macro payload: untyped =
|
|
|
|
result = newTree(treeType)
|
|
|
|
for node in generator():
|
|
|
|
result.add node
|
|
|
|
|
|
|
|
payload()
|
|
|
|
|
|
|
|
template genStmtList*(body: untyped) =
|
2019-07-19 00:04:21 +00:00
|
|
|
iterator generator: NimNode = body
|
|
|
|
|
|
|
|
macro payload: untyped =
|
|
|
|
result = newStmtList()
|
|
|
|
for node in generator():
|
|
|
|
result.add node
|
|
|
|
|
|
|
|
payload()
|
|
|
|
|
2020-07-07 21:32:28 +00:00
|
|
|
template genSimpleExpr*(body: untyped): untyped =
|
2019-08-02 12:26:08 +00:00
|
|
|
macro payload: untyped = body
|
|
|
|
payload()
|