summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2018-10-25 13:04:51 +0200
committerAraq <rumpf_a@web.de>2018-10-25 13:04:51 +0200
commitc844a9169ccb445cc16584db4f434074614b357e (patch)
tree1ca86f131f35503c1f07d7f63783b2ae4e900615 /compiler
parent8fbdac544ba651dec1d0b1f10a5ccab1750e80a2 (diff)
downloadNim-c844a9169ccb445cc16584db4f434074614b357e.tar.gz
fixes #9498, typeof is for everybody
Diffstat (limited to 'compiler')
-rw-r--r--compiler/semexprs.nim5
-rw-r--r--compiler/semmagic.nim12
2 files changed, 11 insertions, 6 deletions
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim
index 883a8c0ae..a13281cd6 100644
--- a/compiler/semexprs.nim
+++ b/compiler/semexprs.nim
@@ -201,7 +201,7 @@ proc semConv(c: PContext, n: PNode): PNode =
   if targetType.kind == tyTypeDesc:
     internalAssert c.config, targetType.len > 0
     if targetType.base.kind == tyNone:
-      return semTypeOf(c, n[1])
+      return semTypeOf(c, n)
     else:
       targetType = targetType.base
   elif targetType.kind == tyStatic:
@@ -1963,8 +1963,7 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
     checkSonsLen(n, 2, c.config)
     result = semAddr(c, n.sons[1], s.name.s == "unsafeAddr")
   of mTypeOf:
-    checkSonsLen(n, 2, c.config)
-    result = semTypeOf(c, n.sons[1])
+    result = semTypeOf(c, n)
   #of mArrGet: result = semArrGet(c, n, flags)
   #of mArrPut: result = semArrPut(c, n, flags)
   #of mAsgn: result = semAsgnOpr(c, n)
diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim
index 5ab4d25e0..09d971236 100644
--- a/compiler/semmagic.nim
+++ b/compiler/semmagic.nim
@@ -21,8 +21,15 @@ proc semAddr(c: PContext; n: PNode; isUnsafeAddr=false): PNode =
   result.typ = makePtrType(c, x.typ)
 
 proc semTypeOf(c: PContext; n: PNode): PNode =
+  var m = BiggestInt 1 # typeOfIter
+  if n.len == 3:
+    let mode = semConstExpr(c, n[2])
+    if mode.kind != nkIntLit:
+      localError(c.config, n.info, "typeof: cannot evaluate 'mode' parameter at compile-time")
+    else:
+      m = mode.intVal
   result = newNodeI(nkTypeOfExpr, n.info)
-  let typExpr = semExprWithType(c, n, {efInTypeof})
+  let typExpr = semExprWithType(c, n[1], if m == 1: {efInTypeof} else: {})
   result.add typExpr
   result.typ = makeTypeDesc(c, typExpr.typ)
 
@@ -320,8 +327,7 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
     checkSonsLen(n, 2, c.config)
     result = semAddr(c, n.sons[1], n[0].sym.name.s == "unsafeAddr")
   of mTypeOf:
-    checkSonsLen(n, 2, c.config)
-    result = semTypeOf(c, n.sons[1])
+    result = semTypeOf(c, n)
   of mSizeOf:
       # TODO there is no proper way to find out if a type cannot be queried for the size.
       let size = getSize(c.config, n[1].typ)