From d462cca21fe9fa9aa05209e5f5c9cab93b1f746f Mon Sep 17 00:00:00 2001 From: James Osborn Date: Sat, 16 Apr 2016 12:16:58 -0500 Subject: added getTypeInst which includes generic parameters --- lib/core/macros.nim | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib/core/macros.nim') diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 678982a52..76fdea9b0 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -197,6 +197,12 @@ proc typeKind*(n: NimNode): NimTypeKind {.magic: "NGetType", noSideEffect.} ## Returns the type kind of the node 'n' that should represent a type, that ## means the node should have been obtained via `getType`. +proc getTypeInst*(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.} + ## Like getType except it includes generic parameters for a specific instance + +proc getTypeInst*(n: typedesc): NimNode {.magic: "NGetType", noSideEffect.} + ## Like getType except it includes generic parameters for a specific instance + proc strVal*(n: NimNode): string {.magic: "NStrVal", noSideEffect.} proc `intVal=`*(n: NimNode, val: BiggestInt) {.magic: "NSetIntVal", noSideEffect.} -- cgit 1.4.1-2-gfad0 From 0356f53b5619782c650f56a6842cbaf91a82137d Mon Sep 17 00:00:00 2001 From: James Osborn Date: Wed, 4 May 2016 00:21:22 -0500 Subject: getTypeInst and getTypeImpl mostly working now and added test --- compiler/vm.nim | 9 ++- compiler/vmdeps.nim | 143 +++++++++++++++++++++++++++++++++--------- compiler/vmgen.nim | 3 +- lib/core/macros.nim | 6 ++ tests/macros/tgettypeinst.nim | 122 +++++++++++++++++++++++++++++++++++ 5 files changed, 251 insertions(+), 32 deletions(-) create mode 100644 tests/macros/tgettypeinst.nim (limited to 'lib/core/macros.nim') diff --git a/compiler/vm.nim b/compiler/vm.nim index ee58b6d48..cddb57f17 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -1200,13 +1200,20 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = regs[ra].intVal = ord(regs[rb].node.typ.kind) #else: # stackTrace(c, tos, pc, errGenerated, "node has no type") - else: + of 2: # getTypeInst opcode: ensureKind(rkNode) if regs[rb].kind == rkNode and regs[rb].node.typ != nil: regs[ra].node = opMapTypeInstToAst(regs[rb].node.typ, c.debug[pc]) else: stackTrace(c, tos, pc, errGenerated, "node has no type") + else: + # getTypeImpl opcode: + ensureKind(rkNode) + if regs[rb].kind == rkNode and regs[rb].node.typ != nil: + regs[ra].node = opMapTypeImplToAst(regs[rb].node.typ, c.debug[pc]) + else: + stackTrace(c, tos, pc, errGenerated, "node has no type") of opcNStrVal: decodeB(rkNode) createStr regs[ra] diff --git a/compiler/vmdeps.nim b/compiler/vmdeps.nim index fb7449001..5fef00257 100644 --- a/compiler/vmdeps.nim +++ b/compiler/vmdeps.nim @@ -67,11 +67,11 @@ proc atomicTypeX(name: string; t: PType; info: TLineInfo): PNode = result = newSymNode(sym) result.typ = t -proc mapTypeToAstImpl(t: PType; info: TLineInfo; - inst=false; allowRecursion=false): PNode +proc mapTypeToAstX(t: PType; info: TLineInfo; + inst=false; allowRecursionX=false): PNode -proc mapTypeToBracketImpl(name: string; t: PType; info: TLineInfo; - inst=false): PNode = +proc mapTypeToBracketX(name: string; t: PType; info: TLineInfo; + inst=false): PNode = result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t) result.add atomicTypeX(name, t, info) for i in 0 .. < t.len: @@ -80,16 +80,39 @@ proc mapTypeToBracketImpl(name: string; t: PType; info: TLineInfo; void.typ = newType(tyEmpty, t.owner) result.add void else: - result.add mapTypeToAstImpl(t.sons[i], info, inst) + result.add mapTypeToAstX(t.sons[i], info, inst) -proc mapTypeToAstImpl(t: PType; info: TLineInfo; - inst=false; allowRecursion=false): PNode = +proc mapTypeToAstX(t: PType; info: TLineInfo; + inst=false; allowRecursionX=false): PNode = + var allowRecursion = allowRecursionX template atomicType(name): expr = atomicTypeX(name, t, info) - template mapTypeToAst(t,info): expr = mapTypeToAstImpl(t, info, inst) + template mapTypeToAst(t,info): expr = mapTypeToAstX(t, info, inst) + template mapTypeToAstR(t,info): expr = mapTypeToAstX(t, info, inst, true) + template mapTypeToAst(t,i,info): expr = + if i0: + var rl = copyNode(t.n) # handle nkRecList + for s in t.n.sons: + rl.add newIdentDefs(s) + result.add rl + else: + result.add ast.emptyNode else: - result = atomicType(t.sym.name.s) + if allowRecursion or t.sym == nil: + result = newNodeIT(nkObjectTy, if t.n.isNil: info else: t.n.info, t) + result.add ast.emptyNode + if t.sons[0] == nil: + result.add ast.emptyNode + else: + result.add mapTypeToAst(t.sons[0], info) + result.add copyTree(t.n) + else: + result = atomicType(t.sym.name.s) of tyEnum: result = newNodeIT(nkEnumTy, if t.n.isNil: info else: t.n.info, t) result.add copyTree(t.n) - of tyTuple: result = mapTypeToBracket("tuple", t, info) + of tyTuple: + if inst: + result = newNodeX(nkTupleTy) + for s in t.n.sons: + result.add newIdentDefs(s) + else: + result = mapTypeToBracket("tuple", t, info) of tySet: result = mapTypeToBracket("set", t, info) - of tyPtr: result = mapTypeToBracket("ptr", t, info) - of tyRef: result = mapTypeToBracket("ref", t, info) + of tyPtr: + if inst: + result = newNodeX(nkPtrTy) + result.add mapTypeToAst(t.sons[0], info) + else: + result = mapTypeToBracket("ptr", t, info) + of tyRef: + if inst: + result = newNodeX(nkRefTy) + result.add mapTypeToAst(t.sons[0], info) + else: + result = mapTypeToBracket("ref", t, info) of tyVar: result = mapTypeToBracket("var", t, info) of tySequence: result = mapTypeToBracket("seq", t, info) - of tyProc: result = mapTypeToBracket("proc", t, info) + of tyProc: + if inst: + result = newNodeX(nkProcTy) + var fp = newNodeX(nkFormalParams) + if t.sons[0] == nil: + fp.add ast.emptyNode + else: + fp.add mapTypeToAst(t.sons[0], t.n[0].info) + for i in 1..