Get symbol kind (#7491)

This commit is contained in:
Arne Döring 2018-04-11 16:48:01 +02:00 • committed by Andreas Rumpf
commit 6baca58693
13 changed files with 319 additions and 272 deletions

View file

@ -118,7 +118,7 @@ type
## use ``ident"abc"``.
NimSymObj = object # hidden
NimSym* = ref NimSymObj
NimSym* {.deprecated.} = ref NimSymObj
## represents a Nim *symbol* in the compiler; a *symbol* is a looked-up
## *ident*.
@ -134,25 +134,23 @@ const
proc `!`*(s: string): NimIdent {.magic: "StrToIdent", noSideEffect, deprecated.}
## constructs an identifier from the string `s`
## **Deprecated since version 0.18.0**: Use ``toNimIdent`` instead.
## **Deprecated since version 0.18.0**: Use ``ident`` or ``newIdentNode`` instead.
proc toNimIdent*(s: string): NimIdent {.magic: "StrToIdent", noSideEffect.}
proc toNimIdent*(s: string): NimIdent {.magic: "StrToIdent", noSideEffect, deprecated.}
## constructs an identifier from the string `s`
## **Deprecated since version 0.18.1**; Use ``ident`` or ``newIdentNode`` instead.
proc `$`*(i: NimIdent): string {.magic: "IdentToStr", noSideEffect.}
## converts a Nim identifier to a string
proc `$`*(s: NimSym): string {.magic: "IdentToStr", noSideEffect.}
## converts a Nim symbol to a string
proc `==`*(a, b: NimIdent): bool {.magic: "EqIdent", noSideEffect.}
proc `==`*(a, b: NimIdent): bool {.magic: "EqIdent", noSideEffect, deprecated.}
## compares two Nim identifiers
## **Deprecated since version 0.18.1**; Use ``==`` on ``NimNode`` instead.
proc `==`*(a, b: NimNode): bool {.magic: "EqNimrodNode", noSideEffect.}
## compares two Nim nodes
proc `==`*(a, b: NimSym): bool {.magic: "EqNimrodNode", noSideEffect.}
proc `==`*(a, b: NimSym): bool {.magic: "EqNimrodNode", noSideEffect, deprecated.}
## compares two Nim symbols
## **Deprecated since version 0.18.1**; Use ```==`(NimNode,NimNode)`` instead.
proc sameType*(a, b: NimNode): bool {.magic: "SameNodeType", noSideEffect.} =
## compares two Nim nodes' types. Return true if the types are the same,
@ -195,8 +193,47 @@ proc kind*(n: NimNode): NimNodeKind {.magic: "NKind", noSideEffect.}
proc intVal*(n: NimNode): BiggestInt {.magic: "NIntVal", noSideEffect.}
proc floatVal*(n: NimNode): BiggestFloat {.magic: "NFloatVal", noSideEffect.}
proc symbol*(n: NimNode): NimSym {.magic: "NSymbol", noSideEffect.}
proc ident*(n: NimNode): NimIdent {.magic: "NIdent", noSideEffect.}
proc ident*(n: NimNode): NimIdent {.magic: "NIdent", noSideEffect, deprecated.} =
## **Deprecated since version 0.18.1**; All functionality is defined on ``NimNode``.
proc symbol*(n: NimNode): NimSym {.magic: "NSymbol", noSideEffect, deprecated.}
## **Deprecated since version 0.18.1**; All functionality is defined on ``NimNode``.
proc getImpl*(s: NimSym): NimNode {.magic: "GetImpl", noSideEffect, deprecated: "use `getImpl: NimNode -> NimNode` instead".}
when defined(nimSymKind):
proc symKind*(symbol: NimNode): NimSymKind {.magic: "NSymKind", noSideEffect.}
proc getImpl*(symbol: NimNode): NimNode {.magic: "GetImpl", noSideEffect.}
proc strVal*(n: NimNode): string {.magic: "NStrVal", noSideEffect.}
## retrieve the implementation of `symbol`. `symbol` can be a
## routine or a const.
proc `$`*(i: NimIdent): string {.magic: "NStrVal", noSideEffect, deprecated.}
## converts a Nim identifier to a string
## **Deprecated since version 0.18.1**; Use ``strVal`` instead.
proc `$`*(s: NimSym): string {.magic: "NStrVal", noSideEffect, deprecated.}
## converts a Nim symbol to a string
## **Deprecated since version 0.18.1**; Use ``strVal`` instead.
else: # bootstrapping substitute
proc getImpl*(symbol: NimNode): NimNode =
symbol.symbol.getImpl
proc strValOld(n: NimNode): string {.magic: "NStrVal", noSideEffect.}
proc `$`*(s: NimSym): string {.magic: "IdentToStr", noSideEffect.}
proc `$`*(i: NimIdent): string {.magic: "IdentToStr", noSideEffect.}
proc strVal*(n: NimNode): string =
if n.kind == nnkIdent:
$n.ident
elif n.kind == nnkSym:
$n.symbol
else:
n.strValOld
proc getType*(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.}
## with 'getType' you can access the node's `type`:idx:. A Nim type is
@ -228,12 +265,15 @@ proc getTypeImpl*(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.}
proc getTypeImpl*(n: typedesc): NimNode {.magic: "NGetType", noSideEffect.}
## Like getType except it includes generic parameters for the implementation
proc strVal*(n: NimNode): string {.magic: "NStrVal", noSideEffect.}
proc `intVal=`*(n: NimNode, val: BiggestInt) {.magic: "NSetIntVal", noSideEffect.}
proc `floatVal=`*(n: NimNode, val: BiggestFloat) {.magic: "NSetFloatVal", noSideEffect.}
proc `symbol=`*(n: NimNode, val: NimSym) {.magic: "NSetSymbol", noSideEffect.}
proc `ident=`*(n: NimNode, val: NimIdent) {.magic: "NSetIdent", noSideEffect.}
proc `symbol=`*(n: NimNode, val: NimSym) {.magic: "NSetSymbol", noSideEffect, deprecated.}
## **Deprecated since version 0.18.1**; Generate a new ``NimNode`` with ``genSym`` instead.
proc `ident=`*(n: NimNode, val: NimIdent) {.magic: "NSetIdent", noSideEffect, deprecated.}
## **Deprecated since version 0.18.1**; Generate a new ``NimNode`` with ``ident(string)`` instead.
#proc `typ=`*(n: NimNode, typ: typedesc) {.magic: "NSetType".}
# this is not sound! Unfortunately forbidding 'typ=' is not enough, as you
# can easily do:
@ -255,11 +295,6 @@ proc newNimNode*(kind: NimNodeKind,
proc copyNimNode*(n: NimNode): NimNode {.magic: "NCopyNimNode", noSideEffect.}
proc copyNimTree*(n: NimNode): NimNode {.magic: "NCopyNimTree", noSideEffect.}
proc getImpl*(s: NimSym): NimNode {.magic: "GetImpl", noSideEffect.} =
## retrieve the implementation of a symbol `s`. `s` can be a routine or a
## const.
discard
proc error*(msg: string, n: NimNode = nil) {.magic: "NError", benign.}
## writes an error message at compile time
@ -294,11 +329,9 @@ proc newIdentNode*(i: NimIdent): NimNode {.compileTime.} =
result = newNimNode(nnkIdent)
result.ident = i
proc newIdentNode*(i: string): NimNode {.compileTime.} =
## creates an identifier node from `i`
result = newNimNode(nnkIdent)
result.ident = toNimIdent i
proc newIdentNode*(i: string): NimNode {.magic: "StrToIdent", noSideEffect.}
## creates an identifier node from `i`. It is simply an alias for
## ``ident(string)``. Use that, it's shorter.
type
BindSymRule* = enum ## specifies how ``bindSym`` behaves
@ -464,9 +497,11 @@ proc newCall*(theProc: NimNode,
result.add(args)
proc newCall*(theProc: NimIdent,
args: varargs[NimNode]): NimNode {.compileTime.} =
args: varargs[NimNode]): NimNode {.compileTime, deprecated.} =
## produces a new call node. `theProc` is the proc that is called with
## the arguments ``args[0..]``.
## **Deprecated since version 0.18.1**; Use ``newCall(string, ...)``,
## or ``newCall(NimNode, ...)`` instead.
result = newNimNode(nnkCall)
result.add(newIdentNode(theProc))
result.add(args)
@ -594,17 +629,30 @@ proc newLit*(s: string): NimNode {.compileTime.} =
result = newNimNode(nnkStrLit)
result.strVal = s
proc nestList*(theProc: NimIdent,
x: NimNode): NimNode {.compileTime.} =
## nests the list `x` into a tree of call expressions:
## ``[a, b, c]`` is transformed into ``theProc(a, theProc(c, d))``.
proc nestList*(op: NimNode; pack: NimNode): NimNode {.compileTime.} =
## nests the list `pack` into a tree of call expressions:
## ``[a, b, c]`` is transformed into ``op(a, op(c, d))``.
## This is also known as fold expression.
if pack.len < 1:
error("`nestList` expects a node with at least 1 child")
result = pack[^1]
for i in countdown(pack.len - 2, 0):
result = newCall(op, pack[i], result)
proc nestList*(op: NimNode; pack: NimNode; init: NimNode): NimNode {.compileTime.} =
## nests the list `pack` into a tree of call expressions:
## ``[a, b, c]`` is transformed into ``op(a, op(c, d))``.
## This is also known as fold expression.
result = init
for i in countdown(pack.len - 1, 0):
result = newCall(op, pack[i], result)
proc nestList*(theProc: NimIdent, x: NimNode): NimNode {.compileTime, deprecated.} =
## **Deprecated since version 0.18.1**; Use one of ``nestList(NimNode, ...)`` instead.
var L = x.len
result = newCall(theProc, x[L-2], x[L-1])
for i in countdown(L-3, 0):
# XXX the 'copyNimTree' here is necessary due to a bug in the evaluation
# engine that would otherwise create an endless loop here. :-(
# This could easily user code and so should be fixed in evals.nim somehow.
result = newCall(theProc, x[i], copyNimTree(result))
result = newCall(theProc, x[i], result)
proc treeRepr*(n: NimNode): string {.compileTime, benign.} =
## Convert the AST `n` to a human-readable tree-like string.
@ -620,9 +668,8 @@ proc treeRepr*(n: NimNode): string {.compileTime, benign.} =
of nnkNilLit: res.add(" nil")
of nnkCharLit..nnkInt64Lit: res.add(" " & $n.intVal)
of nnkFloatLit..nnkFloat64Lit: res.add(" " & $n.floatVal)
of nnkStrLit..nnkTripleStrLit: res.add(" " & $n.strVal.newLit.repr)
of nnkIdent: res.add(" ident\"" & $n.ident & '"')
of nnkSym: res.add(" \"" & $n.symbol & '"')
of nnkStrLit..nnkTripleStrLit, nnkIdent, nnkSym:
res.add(" " & $n.strVal.newLit.repr)
of nnkNone: assert false
else:
for j in 0..n.len-1:
@ -645,9 +692,8 @@ proc lispRepr*(n: NimNode): string {.compileTime, benign.} =
of nnkNilLit: add(result, "nil")
of nnkCharLit..nnkInt64Lit: add(result, $n.intVal)
of nnkFloatLit..nnkFloat64Lit: add(result, $n.floatVal)
of nnkStrLit..nnkTripleStrLit, nnkCommentStmt: add(result, n.strVal.newLit.repr)
of nnkIdent: add(result, "ident\"" & $n.ident & '"')
of nnkSym: add(result, $n.symbol)
of nnkStrLit..nnkTripleStrLit, nnkCommentStmt, nnkident, nnkSym:
add(result, n.strVal.newLit.repr)
of nnkNone: assert false
else:
if n.len > 0:
@ -696,9 +742,8 @@ proc astGenRepr*(n: NimNode): string {.compileTime, benign.} =
of nnkCharLit: res.add("'" & $chr(n.intVal) & "'")
of nnkIntLit..nnkInt64Lit: res.add($n.intVal)
of nnkFloatLit..nnkFloat64Lit: res.add($n.floatVal)
of nnkStrLit..nnkTripleStrLit, nnkCommentStmt: res.add($n.strVal.newLit.repr)
of nnkIdent: res.add(($n.ident).newLit.repr())
of nnkSym: res.add(($n.symbol).newLit.repr())
of nnkStrLit..nnkTripleStrLit, nnkCommentStmt, nnkIdent, nnkSym:
res.add(n.strVal.newLit.repr)
of nnkNone: assert false
else:
res.add(".newTree(")
@ -742,10 +787,10 @@ macro dumpAstGen*(s: untyped): untyped = echo s.astGenRepr
## See `dumpTree`.
macro dumpTreeImm*(s: untyped): untyped {.deprecated.} = echo s.treeRepr
## Deprecated.
## Deprecated. Use `dumpTree` instead.
macro dumpLispImm*(s: untyped): untyped {.deprecated.} = echo s.lispRepr
## Deprecated.
## Deprecated. Use `dumpLisp` instead.
proc newEmptyNode*(): NimNode {.compileTime, noSideEffect.} =
## Create a new empty node
@ -991,28 +1036,21 @@ proc `body=`*(someProc: NimNode, val: NimNode) {.compileTime.} =
proc basename*(a: NimNode): NimNode {.compiletime, benign.}
proc `$`*(node: NimNode): string {.compileTime.} =
## Get the string of an identifier node
case node.kind
of nnkIdent:
result = $node.ident
of nnkPostfix:
result = $node.basename.ident & "*"
of nnkStrLit..nnkTripleStrLit:
result = node.basename.strVal & "*"
of nnkStrLit..nnkTripleStrLit, nnkCommentStmt, nnkSym, nnkIdent:
result = node.strVal
of nnkSym:
result = $node.symbol
of nnkOpenSymChoice, nnkClosedSymChoice:
result = $node[0]
of nnkAccQuoted:
result = $node[0]
of nnkCommentStmt:
result = node.strVal
else:
badNodeKind node.kind, "$"
proc ident*(name: string): NimNode {.compileTime,inline.} = newIdentNode(name)
proc ident*(name: string): NimNode {.magic: "StrToIdent", noSideEffect.}
## Create a new ident node from a string
iterator items*(n: NimNode): NimNode {.inline.} =
@ -1129,10 +1167,8 @@ proc eqIdent*(node: NimNode; s: string): bool {.compileTime.} =
## is the same as ``s``. Note that this is the preferred way to check! Most
## other ways like ``node.ident`` are much more error-prone, unfortunately.
case node.kind
of nnkIdent:
result = node.ident == toNimIdent s
of nnkSym:
result = eqIdent($node.symbol, s)
of nnkSym, nnkIdent:
result = eqIdent(node.strVal, s)
of nnkOpenSymChoice, nnkClosedSymChoice:
result = eqIdent($node[0], s)
else:
@ -1189,11 +1225,11 @@ macro expandMacros*(body: typed): untyped =
proc customPragmaNode(n: NimNode): NimNode =
expectKind(n, {nnkSym, nnkDotExpr})
if n.kind == nnkSym:
let sym = n.symbol.getImpl()
let sym = n.getImpl()
sym.expectRoutine()
result = sym.pragma
elif n.kind == nnkDotExpr:
let typDef = getImpl(getTypeInst(n[0]).symbol)
let typDef = getImpl(getTypeInst(n[0]))
typDef.expectKind(nnkTypeDef)
typDef[2].expectKind(nnkObjectTy)
let recList = typDef[2][2]