summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2013-12-25 22:29:44 +0200
committerZahary Karadjov <zahary@gmail.com>2013-12-25 22:29:44 +0200
commitedab4aaad02bf414f7f0c6e3148ade8a7b485c40 (patch)
tree5239c5f1259440420f0adb71e855bbd578b8dcc8 /compiler
parent1d02f2ea531ad14f686a75c30af9228ba84fa194 (diff)
downloadNim-edab4aaad02bf414f7f0c6e3148ade8a7b485c40.tar.gz
better integration of tyStatic into typeRel
Diffstat (limited to 'compiler')
-rw-r--r--compiler/ast.nim1
-rw-r--r--compiler/evals.nim2
-rw-r--r--compiler/msgs.nim14
-rw-r--r--compiler/sem.nim10
-rw-r--r--compiler/semdata.nim5
-rw-r--r--compiler/semtypes.nim16
-rw-r--r--compiler/semtypinst.nim8
-rw-r--r--compiler/sigmatch.nim60
-rw-r--r--compiler/types.nim8
9 files changed, 66 insertions, 58 deletions
diff --git a/compiler/ast.nim b/compiler/ast.nim
index 64f959fe2..130f5a5ad 100644
--- a/compiler/ast.nim
+++ b/compiler/ast.nim
@@ -399,6 +399,7 @@ type
     tfHasMeta,        # type has "typedesc" or "expr" somewhere; or uses '|'
     tfHasGCedMem,     # type contains GC'ed memory
     tfGenericTypeParam
+    tfHasStatic
 
   TTypeFlags* = set[TTypeFlag]
 
diff --git a/compiler/evals.nim b/compiler/evals.nim
index 4a2586d5f..f7d94e4c0 100644
--- a/compiler/evals.nim
+++ b/compiler/evals.nim
@@ -91,7 +91,7 @@ proc evalMacroCall*(c: PEvalContext, n, nOrig: PNode, sym: PSym): PNode
 proc evalAux(c: PEvalContext, n: PNode, flags: TEvalFlags): PNode
 
 proc raiseCannotEval(c: PEvalContext, info: TLineInfo): PNode =
-  if defined(debug): writeStackTrace()
+  if defined(debug) and gVerbosity >= 3: writeStackTrace()
   result = newNodeI(nkExceptBranch, info)
   # creating a nkExceptBranch without sons 
   # means that it could not be evaluated
diff --git a/compiler/msgs.nim b/compiler/msgs.nim
index 9c24295a6..81e738ee8 100644
--- a/compiler/msgs.nim
+++ b/compiler/msgs.nim
@@ -701,23 +701,21 @@ type
   TErrorHandling = enum doNothing, doAbort, doRaise
 
 proc handleError(msg: TMsgKind, eh: TErrorHandling, s: string) =
-  template maybeTrace =
-    if defined(debug) or gVerbosity >= 3:
-      writeStackTrace()
+  template quit =
+    if defined(debug) or gVerbosity >= 3: writeStackTrace()
+    quit 1
 
   if msg == errInternal:
     writeStackTrace() # we always want a stack trace here
   if msg >= fatalMin and msg <= fatalMax: 
-    maybeTrace()
-    quit(1)
+    quit()
   if msg >= errMin and msg <= errMax: 
-    maybeTrace()
     inc(gErrorCounter)
     options.gExitcode = 1'i8
     if gErrorCounter >= gErrorMax: 
-      quit(1)
+      quit()
     elif eh == doAbort and gCmd != cmdIdeTools:
-      quit(1)
+      quit()
     elif eh == doRaise:
       raiseRecoverableError(s)
 
diff --git a/compiler/sem.nim b/compiler/sem.nim
index 3ace623bc..67d400ac5 100644
--- a/compiler/sem.nim
+++ b/compiler/sem.nim
@@ -189,6 +189,15 @@ proc evalConstExpr(c: PContext, module: PSym, e: PNode): PNode =
 proc evalStaticExpr(c: PContext, module: PSym, e: PNode, prc: PSym): PNode = 
   result = evalConstExprAux(c.createEvalContext(emStatic), module, prc, e)
 
+proc tryConstExpr(c: PContext, n: PNode): PNode =
+  var e = semExprWithType(c, n)
+  if e == nil: return
+  result = getConstExpr(c.module, e)
+  if result == nil:
+    result = evalConstExpr(c, c.module, e)
+    if result == nil or result.kind == nkEmpty:
+      return nil
+  
 proc semConstExpr(c: PContext, n: PNode): PNode =
   var e = semExprWithType(c, n)
   if e == nil:
@@ -282,6 +291,7 @@ proc myOpen(module: PSym): PPassContext =
   c.semConstExpr = semConstExpr
   c.semExpr = semExpr
   c.semTryExpr = tryExpr
+  c.semTryConstExpr = tryConstExpr
   c.semOperand = semOperand
   c.semConstBoolExpr = semConstBoolExpr
   c.semOverloadedCall = semOverloadedCall
diff --git a/compiler/semdata.nim b/compiler/semdata.nim
index 1984bfccb..874e5dab4 100644
--- a/compiler/semdata.nim
+++ b/compiler/semdata.nim
@@ -75,6 +75,7 @@ type
     semExpr*: proc (c: PContext, n: PNode, flags: TExprFlags = {}): PNode {.nimcall.}
     semTryExpr*: proc (c: PContext, n: PNode,flags: TExprFlags = {},
                        bufferErrors = false): PNode {.nimcall.}
+    semTryConstExpr*: proc (c: PContext, n: PNode): PNode {.nimcall.}
     semOperand*: proc (c: PContext, n: PNode, flags: TExprFlags = {}): PNode {.nimcall.}
     semConstBoolExpr*: proc (c: PContext, n: PNode): PNode {.nimcall.} # XXX bite the bullet
     semOverloadedCall*: proc (c: PContext, n, nOrig: PNode,
@@ -217,11 +218,15 @@ proc makeAndType*(c: PContext, t1, t2: PType): PType =
   result = newTypeS(tyAnd, c)
   result.sons = @[t1, t2]
   result.flags.incl tfHasMeta
+  if tfHasStatic in t1.flags or tfHasStatic in t2.flags:
+    result.flags.incl tfHasStatic
 
 proc makeOrType*(c: PContext, t1, t2: PType): PType =
   result = newTypeS(tyOr, c)
   result.sons = @[t1, t2]
   result.flags.incl tfHasMeta
+  if tfHasStatic in t1.flags or tfHasStatic in t2.flags:
+    result.flags.incl tfHasStatic
 
 proc makeNotType*(c: PContext, t1: PType): PType =
   result = newTypeS(tyNot, c)
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index 17b7687b4..201622016 100644
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -678,12 +678,9 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
         paramType.sons[i] = lifted
         result = paramType
 
-    if paramType.lastSon.kind == tyTypeClass and false:
-      result = paramType
-      result.kind = tyParametricTypeClass
-      result = addImplicitGeneric(copyType(result,
-                                           getCurrOwner(), false))
-    elif result != nil:
+    if result == nil:
+      result = liftingWalk(paramType.lastSon)
+    else:
       result.kind = tyGenericInvokation
       result.sons.setLen(result.sons.len - 1)
   of tyTypeClass, tyBuiltInTypeClass, tyAnd, tyOr, tyNot:
@@ -1014,9 +1011,10 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
   of nkVarTy: result = semVarType(c, n, prev)
   of nkDistinctTy: result = semDistinct(c, n, prev)
   of nkStaticTy:
-      result = newOrPrevType(tyStatic, prev, c)
-      var base = semTypeNode(c, n.sons[0], nil)
-      result.rawAddSon(base)
+    result = newOrPrevType(tyStatic, prev, c)
+    var base = semTypeNode(c, n.sons[0], nil)
+    result.rawAddSon(base)
+    result.flags.incl tfHasStatic
   of nkProcTy, nkIteratorTy:
     if n.sonsLen == 0:
       result = newConstraint(c, tyProc)
diff --git a/compiler/semtypinst.nim b/compiler/semtypinst.nim
index f23d87763..69d91766b 100644
--- a/compiler/semtypinst.nim
+++ b/compiler/semtypinst.nim
@@ -194,16 +194,16 @@ proc handleGenericInvokation(cl: var TReplTypeVars, t: PType): PType =
   
 proc ReplaceTypeVarsT*(cl: var TReplTypeVars, t: PType): PType = 
   result = t
-  if t == nil: return 
+  if t == nil: return
+  if t.kind == tyStatic and t.sym != nil and t.sym.kind == skGenericParam:
+    return lookupTypeVar(cl, t)
+
   case t.kind
   of tyTypeClass: nil
   of tyGenericParam:
     result = lookupTypeVar(cl, t)
     if result.kind == tyGenericInvokation:
       result = handleGenericInvokation(cl, result)
-  of tyStatic:
-    if t.sym != nil and t.sym.kind == skGenericParam:
-      result = lookupTypeVar(cl, t)
   of tyGenericInvokation: 
     result = handleGenericInvokation(cl, t)
   of tyGenericBody:
diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim
index 03c37438c..c3d197c3b 100644
--- a/compiler/sigmatch.nim
+++ b/compiler/sigmatch.nim
@@ -753,6 +753,14 @@ proc typeRel(c: var TCandidate, f, a: PType, doBind = true): TTypeRelation =
       result = isGeneric
     else:
       result = typeRel(c, x, a) # check if it fits
+  
+  of tyStatic:
+    if a.kind == tyStatic:
+      result = typeRel(c, f.lastSon, a.lastSon)
+      if result != isNone: put(c.bindings, f, a)
+    else:
+      result = isNone
+
   of tyTypeDesc:
     var prev = PType(idTableGet(c.bindings, f))
     if prev == nil:
@@ -904,40 +912,28 @@ proc matchUserTypeClass*(c: PContext, m: var TCandidate,
 proc ParamTypesMatchAux(m: var TCandidate, f, argType: PType,
                         argSemantized, argOrig: PNode): PNode =
   var
-    r: TTypeRelation
+    fMaybeStatic = f.skipTypes({tyDistinct})
     arg = argSemantized
-
-  let
     c = m.c
-    a0 = if c.InTypeClass > 0: argType.skipTypes({tyTypeDesc})
-         else: argType
-    a = if a0 != nil: a0.skipTypes({tyStatic}) else: a0
-    fMaybeStatic = f.skipTypes({tyDistinct})
-
+    argType = argType
+
+  if tfHasStatic in fMaybeStatic.flags:
+    # XXX: When implicit statics are the default
+    # this will be done earlier - we just have to
+    # make sure that static types enter here
+    var evaluated = c.semTryConstExpr(c, arg)
+    if evaluated != nil:
+      arg.typ = newTypeS(tyStatic, c)
+      arg.typ.sons = @[evaluated.typ]
+      arg.typ.n = evaluated
+      argType = arg.typ
+  
+  var
+    r: TTypeRelation
+    a = if c.InTypeClass > 0: argType.skipTypes({tyTypeDesc})
+        else: argType
+ 
   case fMaybeStatic.kind
-  of tyStatic:
-    if a.kind == tyStatic:
-      InternalAssert a.len > 0
-      r = typeRel(m, f.lastSon, a.lastSon)
-    else:
-      r = typeRel(m, fMaybeStatic, a)
-      if r != isNone:
-        # XXX: Ideally, this should happen much earlier somewhere near 
-        # semOpAux, but to do that, we need to be able to query the 
-        # overload set to determine whether compile-time value is expected
-        # for the param before entering the full-blown sigmatch algorithm.
-        # This is related to the immediate pragma since querying the
-        # overload set could help there too.
-        var evaluated = c.semConstExpr(c, arg)
-        if evaluated != nil:
-          r = isGeneric
-          arg.typ = newTypeS(tyStatic, c)
-          arg.typ.sons = @[evaluated.typ]
-          arg.typ.n = evaluated
-        
-    if r == isGeneric:
-      put(m.bindings, f, arg.typ)
-
   of tyTypeClass, tyParametricTypeClass:
     if fMaybeStatic.n != nil:
       let match = matchUserTypeClass(c, m, arg, fMaybeStatic, a)
@@ -955,7 +951,7 @@ proc ParamTypesMatchAux(m: var TCandidate, f, argType: PType,
     r = typeRel(m, f, a)
 
   case r
-  of isConvertible: 
+  of isConvertible:
     inc(m.convMatches)
     result = implicitConv(nkHiddenStdConv, f, copyTree(arg), m, c)
   of isIntConv:
diff --git a/compiler/types.nim b/compiler/types.nim
index 872834810..f6e426f39 100644
--- a/compiler/types.nim
+++ b/compiler/types.nim
@@ -434,10 +434,10 @@ proc TypeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
     add(result, ']')
   of tyTypeDesc:
     if t.len == 0: result = "typedesc"
-    else: result = "typedesc[" & typeToString(t) & "]"
+    else: result = "typedesc[" & typeToString(t.sons[0]) & "]"
   of tyStatic:
     InternalAssert t.len > 0
-    result = "static[" & typeToString(t) & "]"
+    result = "static[" & typeToString(t.sons[0]) & "]"
   of tyTypeClass:
     InternalAssert t.sym != nil and t.sym.owner != nil
     return t.sym.owner.name.s
@@ -450,8 +450,8 @@ proc TypeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
   of tyNot:
     result = "not " & typeToString(t.sons[0])
   of tyExpr:
-    if t.len == 0: result = "expr"
-    else: result = "expr[" & typeToString(t) & "]"
+    InternalAssert t.len == 0
+    result = "expr"
   of tyArray: 
     if t.sons[0].kind == tyRange: 
       result = "array[" & rangeToStr(t.sons[0].n) & ", " &