Skip to content

Commit

Permalink
Add isInstanceOf for generic procs to the macros module (#9730)
Browse files Browse the repository at this point in the history
  • Loading branch information
cooldome authored and Araq committed Nov 21, 2018
1 parent 1cc8b78 commit 0866767
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ proc enumToString*(enums: openArray[enum]): string =
- Added `system.typeof` for more control over how `type` expressions
can be deduced.

- Added `macros.isInstantiationOf` for checking if the proc symbol
is instantiation of generic proc symbol.


### Library changes

- The string output of `macros.lispRepr` proc has been tweaked
Expand Down
3 changes: 2 additions & 1 deletion compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,8 @@ type
mNHint, mNWarning, mNError,
mInstantiationInfo, mGetTypeInfo,
mNimvm, mIntDefine, mStrDefine, mRunnableExamples,
mException, mBuiltinType, mSymOwner, mUncheckedArray, mGetImplTransf
mException, mBuiltinType, mSymOwner, mUncheckedArray, mGetImplTransf,
mSymIsInstantiationOf

# things that we can evaluate safely at compile time, even if not asked for it:
const
Expand Down
2 changes: 1 addition & 1 deletion compiler/condsyms.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ proc initDefines*(symbols: StringTableRef) =
defineSymbol("nimUncheckedArrayTyp")
defineSymbol("nimHasTypeof")
defineSymbol("nimErrorProcCanHaveBody")

defineSymbol("nimHasInstantiationOfInMacro")
defineSymbol("nimHasNilSeqs")
for f in low(Feature)..high(Feature):
defineSymbol("nimHas" & $f)
Expand Down
11 changes: 11 additions & 0 deletions compiler/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,17 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
regs[ra].node.flags.incl nfIsRef
else:
stackTrace(c, tos, pc, "node is not a symbol")
of opcSymIsInstantiationOf:
decodeBC(rkInt)
let a = regs[rb].node
let b = regs[rc].node
if a.kind == nkSym and a.sym.kind in skProcKinds and
b.kind == nkSym and b.sym.kind in skProcKinds:
regs[ra].intVal =
if sfFromGeneric in a.sym.flags and a.sym.owner == b.sym: 1
else: 0
else:
stackTrace(c, tos, pc, "node is not a proc symbol")
of opcEcho:
let rb = instr.regB
if rb == 1:
Expand Down
3 changes: 2 additions & 1 deletion compiler/vmdef.nim
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ type
opcTypeTrait,
opcMarshalLoad, opcMarshalStore,
opcToNarrowInt,
opcSymOwner
opcSymOwner,
opcSymIsInstantiationOf

TBlock* = object
label*: PSym
Expand Down
1 change: 1 addition & 0 deletions compiler/vmgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,7 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) =
of mGetImpl: genUnaryABC(c, n, dest, opcGetImpl)
of mGetImplTransf: genUnaryABC(c, n, dest, opcGetImplTransf)
of mSymOwner: genUnaryABC(c, n, dest, opcSymOwner)
of mSymIsInstantiationOf: genBinaryABC(c, n, dest, opcSymIsInstantiationOf)
of mNChild: genBinaryABC(c, n, dest, opcNChild)
of mNSetChild: genVoidABC(c, n, dest, opcNSetChild)
of mNDel: genVoidABC(c, n, dest, opcNDel)
Expand Down
6 changes: 6 additions & 0 deletions lib/core/macros.nim
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ when defined(nimHasSymOwnerInMacro):
## result is also mnde of kind nnkSym if owner exists otherwise
## nnkNilLit is returned

when defined(nimHasInstantiationOfInMacro):
proc isInstantiationOf*(instanceProcSym, genProcSym: NimNode): bool {.magic: "SymIsInstantiationOf", noSideEffect.}
## check if proc symbol is instance of the generic proc symbol
## useful to check proc symbols against generic symbols
## returned by `bindSym`

proc getType*(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.}
## with 'getType' you can access the node's `type`:idx:. A Nim type is
## mapped to a Nim AST too, so it's slightly confusing but it means the same
Expand Down
19 changes: 19 additions & 0 deletions tests/macros/tgetimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,22 @@ static:
doAssert isSameOwner(foo, poo)
doAssert isSameOwner(foo, echo) == false
doAssert isSameOwner(poo, len) == false

#---------------------------------------------------------------

macro check_gen_proc(ex: typed): (bool, bool) =
let lenChoice = bindsym"len"
var is_equal = false
var is_instance_of = false
for child in lenChoice:
if not is_equal:
is_equal = ex[0] == child
if not is_instance_of:
is_instance_of = isInstantiationOf(ex[0], child)

result = nnkTupleConstr.newTree(newLit(is_equal), newLit(is_instance_of))

# check that len(seq[int]) is not equal to bindSym"len", but is instance of it
let a = @[1,2,3]
assert: check_gen_proc(len(a)) == (false, true)

0 comments on commit 0866767

Please sign in to comment.