summary refs log tree commit diff stats
path: root/compiler/semexprs.nim
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/semexprs.nim')
-rwxr-xr-xcompiler/semexprs.nim33
1 files changed, 22 insertions, 11 deletions
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim
index 356f1c196..54f0af9df 100755
--- a/compiler/semexprs.nim
+++ b/compiler/semexprs.nim
@@ -904,26 +904,37 @@ proc expectStringArg(c: PContext, n: PNode, i: int): PNode =
   if result.kind notin {nkStrLit, nkRStrLit, nkTripleStrLit}:
     GlobalError(result.info, errStringLiteralExpected)
 
-proc semExpandMacroToAst(c: PContext, n: PNode, flags: TExprFlags): PNode =
+proc isAstValue(n: PNode): bool =
+  result = n.typ.sym.name.s in [ "expr", "stmt", "PNimrodNode" ]
+
+proc semExpandToAst(c: PContext, n: PNode, magicSym: PSym, flags: TExprFlags): PNode =
   if sonsLen(n) == 2:
     if not isCallExpr(n.sons[1]):
       GlobalError(n.info, errXisNoMacroOrTemplate, n.renderTree)
 
     var macroCall = n.sons[1]
 
-    var s = qualifiedLookup(c, macroCall.sons[0], {checkUndeclared})
-    if s == nil:
+    var expandedSym = qualifiedLookup(c, macroCall.sons[0], {checkUndeclared})
+    if expandedSym == nil:
       GlobalError(n.info, errUndeclaredIdentifier, macroCall.sons[0].renderTree)
 
-    var expanded : Pnode
+    if not (expandedSym.kind in { skMacro, skTemplate }):
+      GlobalError(n.info, errXisNoMacroOrTemplate, expandedSym.name.s)
 
-    case s.kind
-    of skMacro: expanded = semMacroExpr(c, macroCall, s, false)
-    of skTemplate: expanded = semTemplateExpr(c, macroCall, s, false)
-    else: GlobalError(n.info, errXisNoMacroOrTemplate, s.name.s)
+    macroCall.sons[0] = newNodeI(nkSym, macroCall.info)
+    macroCall.sons[0].sym = expandedSym
+    markUsed(n, expandedSym)
 
-    var macroRetType = newTypeS(s.typ.sons[0].kind, c)
-    result = newMetaNodeIT(expanded, n.info, macroRetType)
+    for i in countup(1, macroCall.sonsLen - 1):
+      macroCall.sons[i] = semExprWithType(c, macroCall.sons[i], {efAllowType})
+
+    # Preserve the magic symbol in order to handled in evals.nim
+    n.sons[0] = newNodeI(nkSym, n.info)
+    n.sons[0].sym = magicSym
+   
+    n.typ = expandedSym.getReturnType
+
+    result = n
   else:
     result = semDirectOp(c, n, flags)
 
@@ -963,7 +974,7 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
     else:
       result = semDirectOp(c, n, flags)
   of mSlurp: result = semSlurp(c, n, flags)
-  of mExpandMacroToAst: result = semExpandMacroToAst(c, n, flags)
+  of mExpandToAst: result = semExpandToAst(c, n, s, flags)
   else: result = semDirectOp(c, n, flags)
 
 proc semIfExpr(c: PContext, n: PNode): PNode =