implemented opcTypeTrait

This commit is contained in:
Araq 2013-08-22 19:28:34 +02:00
commit cf38d635bf
3 changed files with 26 additions and 17 deletions

View file

@ -833,6 +833,12 @@ proc execute(c: PCtx, start: int) =
regs[ra] = newSymNode(newSym(k.TSymKind, name.getIdent, c.module, regs[ra] = newSymNode(newSym(k.TSymKind, name.getIdent, c.module,
c.debug[pc])) c.debug[pc]))
incl(regs[ra].sym.flags, sfGenSym) incl(regs[ra].sym.flags, sfGenSym)
of opcTypeTrait:
# XXX only supports 'name' for now; we can use regC to encode the
# type trait operation
decodeB(nkStrLit)
let typ = regs[rb].sym.typ.skipTypes({tyTypeDesc})
regs[ra].strVal = typ.typeToString(preferExported)
inc pc inc pc
proc evalStmt*(c: PCtx, n: PNode) = proc evalStmt*(c: PCtx, n: PNode) =

View file

@ -35,6 +35,21 @@ proc opSlurp*(file: string, info: TLineInfo, module: PSym): string =
result = "" result = ""
LocalError(info, errCannotOpenFile, file) LocalError(info, errCannotOpenFile, file)
proc opTypeTrait*(n: PNode, context: PSym): PNode =
## XXX: This should be pretty much guaranteed to be true
# by the type traits procs' signatures, but until the
# code is more mature it doesn't hurt to be extra safe
internalAssert n.len >= 2 and n.sons[1].kind == nkSym
let typ = n.sons[1].sym.typ.skipTypes({tyTypeDesc})
case n.sons[0].sym.name.s.normalize
of "name":
result = newStrNode(nkStrLit, typ.typeToString(preferExported))
result.typ = newType(tyString, context)
result.info = n.info
else:
internalAssert false
when false: when false:
proc opExpandToAst*(c: PEvalContext, original: PNode): PNode = proc opExpandToAst*(c: PEvalContext, original: PNode): PNode =
var var
@ -64,21 +79,6 @@ when false:
"ExpandToAst: expanded symbol is no macro or template") "ExpandToAst: expanded symbol is no macro or template")
result = emptyNode result = emptyNode
proc opTypeTrait*(n: PNode, context: PSym): PNode =
## XXX: This should be pretty much guaranteed to be true
# by the type traits procs' signatures, but until the
# code is more mature it doesn't hurt to be extra safe
internalAssert n.len >= 2 and n.sons[1].kind == nkSym
let typ = n.sons[1].sym.typ.skipTypes({tyTypeDesc})
case n.sons[0].sym.name.s.normalize
of "name":
result = newStrNode(nkStrLit, typ.typeToString(preferExported))
result.typ = newType(tyString, context)
result.info = n.info
else:
internalAssert false
proc opIs*(n: PNode): PNode = proc opIs*(n: PNode): PNode =
InternalAssert n.sonsLen == 3 and InternalAssert n.sonsLen == 3 and
n[1].kind == nkSym and n[1].sym.kind == skType and n[1].kind == nkSym and n[1].sym.kind == skType and

View file

@ -699,8 +699,11 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest) =
of mExpandToAst: of mExpandToAst:
InternalError(n.info, "cannot generate code for: " & $m) InternalError(n.info, "cannot generate code for: " & $m)
of mTypeTrait: of mTypeTrait:
let tmp = c.genx(n.sons[1])
InternalError(n.info, "cannot generate code for: " & $m) if dest < 0: dest = c.getTemp(n.typ)
c.gABx(n, opcSetType, tmp, c.genType(n.sons[1]))
c.gABC(n, opcTypeTrait, dest, tmp)
c.freeTemp(tmp)
of mIs: of mIs:
InternalError(n.info, "cannot generate code for: " & $m) InternalError(n.info, "cannot generate code for: " & $m)
of mSlurp: genUnaryABC(c, n, dest, opcSlurp) of mSlurp: genUnaryABC(c, n, dest, opcSlurp)