summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2012-03-22 16:24:12 +0200
committerZahary Karadjov <zahary@gmail.com>2012-03-22 16:24:12 +0200
commit296ef07955b9b9813d3dbd0d8de3e483ff79ee08 (patch)
tree1d3cd2184bbd125aff2759b7a6c9ce0240ac6277 /compiler
parent3a5cf3d63a5f31809acf5c5f07d6f6a33c19af3d (diff)
downloadNim-296ef07955b9b9813d3dbd0d8de3e483ff79ee08.tar.gz
tyOrdinal now means "integral types". tyTypeClass created to take care of type constraints
Diffstat (limited to 'compiler')
-rwxr-xr-xcompiler/ast.nim3
-rwxr-xr-xcompiler/ccgutils.nim4
-rwxr-xr-xcompiler/ecmasgen.nim2
-rwxr-xr-xcompiler/semtypes.nim27
-rwxr-xr-xcompiler/sigmatch.nim9
-rwxr-xr-xcompiler/transf.nim2
-rwxr-xr-xcompiler/types.nim26
7 files changed, 43 insertions, 30 deletions
diff --git a/compiler/ast.nim b/compiler/ast.nim
index a1b69bed8..7104c020b 100755
--- a/compiler/ast.nim
+++ b/compiler/ast.nim
@@ -259,7 +259,7 @@ type
     tyGenericParam,      # ``a`` in the above patterns
     tyDistinct,
     tyEnum,
-    tyOrdinal,           # misnamed: should become 'tyConstraint'
+    tyOrdinal,           # integer types (including enums and boolean)
     tyArray,
     tyObject,
     tyTuple,
@@ -278,6 +278,7 @@ type
     tyConst, tyMutable, tyVarargs, 
     tyIter, # unused
     tyProxy # currently unused
+    tyTypeClass,
 
 const
   tyPureObject* = tyTuple
diff --git a/compiler/ccgutils.nim b/compiler/ccgutils.nim
index 610fcb39b..12795358a 100755
--- a/compiler/ccgutils.nim
+++ b/compiler/ccgutils.nim
@@ -82,9 +82,11 @@ proc GetUniqueType*(key: PType): PType =
     if result == nil:
       gCanonicalTypes[k] = key
       result = key
+  of tyGenericParam, tyTypeClass:
+    InternalError("GetUniqueType")
   of tyGenericInst, tyDistinct, tyOrdinal, tyMutable, tyConst, tyIter:
     result = GetUniqueType(lastSon(key))
-  of tyArrayConstr, tyGenericInvokation, tyGenericBody, tyGenericParam,
+  of tyArrayConstr, tyGenericInvokation, tyGenericBody,
      tyOpenArray, tyArray, tyTuple, tySet, tyRange, 
      tyPtr, tyRef, tySequence, tyForward, tyVarargs, tyProxy, tyVar:
     # we have to do a slow linear search because types may need
diff --git a/compiler/ecmasgen.nim b/compiler/ecmasgen.nim
index 5307ea7d1..6499f7a6f 100755
--- a/compiler/ecmasgen.nim
+++ b/compiler/ecmasgen.nim
@@ -111,7 +111,7 @@ proc mapType(typ: PType): TEcmasTypeKind =
     result = etyObject
   of tyNil: result = etyNull
   of tyGenericInst, tyGenericParam, tyGenericBody, tyGenericInvokation, tyNone, 
-     tyForward, tyEmpty, tyExpr, tyStmt, tyTypeDesc: 
+     tyForward, tyEmpty, tyExpr, tyStmt, tyTypeDesc, tyTypeClass: 
     result = etyNone
   of tyProc: result = etyProc
   of tyCString: result = etyString
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index c0ed5b237..0ae4ae5fd 100755
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -520,9 +520,13 @@ proc addParamOrResult(c: PContext, param: PSym, kind: TSymKind) =
   else:
     addDecl(c, param)
 
-proc isTypeClass(c: PContext, t: PType): bool =
-  return t.kind in {tyExpr}
-
+proc paramTypeClass(c: PContext, paramType: PType, procKind: TSymKind): PType =
+  case paramType.kind:
+  of tyExpr:
+    if procKind notin {skTemplate, skMacro}:
+      result = newTypeS(tyGenericParam, c)
+  else: nil
+  
 proc semProcTypeNode(c: PContext, n, genericParams: PNode, 
                      prev: PType, kind: TSymKind): PType = 
   var
@@ -552,6 +556,11 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
 
     if hasType:
       typ = paramType(c, a.sons[length-2], genericParams, cl)
+      if c.filename.endsWith"nimdbg.nim" and typ != nil:
+        echo("PARAM TYPE ", typ.kind, " ")
+        if genericParams != nil:
+          echo genericParams.info.toFileLineCol
+        debug typ
       #if matchType(typ, [(tyVar, 0)], tyGenericInvokation):
       #  debug a.sons[length-2][0][1]
     if hasDefault:
@@ -571,9 +580,9 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
     if skipTypes(typ, {tyGenericInst}).kind == tyEmpty: continue
     for j in countup(0, length-3): 
       var arg = newSymS(skParam, a.sons[j], c)
-      arg.typ = typ
-      if kind notin {skTemplate, skMacro} and isTypeClass(c, typ):
-        let typeClassParamId = getIdent(":tcls_" & $i & "_" & $j)
+      let typeClass = paramTypeClass(c, typ, kind)
+      if typeClass != nil:
+        let typeClassParamId = getIdent(arg.name.s & ":type")
         if genericParams == nil:
           # genericParams is nil when the proc is being instantiated
           # the resolved type will be in scope then
@@ -581,11 +590,13 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
           arg.typ = s.typ
         else:
           var s = newSym(skType, typeClassParamId, getCurrOwner())
-          s.typ = newTypeS(tyGenericParam, c)
+          s.typ = typeClass
           s.typ.sym = s
           s.position = genericParams.len
           genericParams.addSon(newSymNode(s))
           arg.typ = s.typ
+      else:
+        arg.typ = typ
 
       arg.position = counter
       inc(counter)
@@ -799,7 +810,7 @@ proc processMagicType(c: PContext, m: PSym) =
   else: GlobalError(m.info, errTypeExpected)
   
 proc newConstraint(c: PContext, k: TTypeKind): PType = 
-  result = newTypeS(tyOrdinal, c)
+  result = newTypeS(tyTypeClass, c)
   result.addSon(newTypeS(k, c))
   
 proc semGenericConstraints(c: PContext, n: PNode, result: PType) = 
diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim
index 3db88569f..74131c21d 100755
--- a/compiler/sigmatch.nim
+++ b/compiler/sigmatch.nim
@@ -340,11 +340,10 @@ proc typeRel(mapping: var TIdTable, f, a: PType): TTypeRelation =
         result = typeRel(mapping, f.sons[0], a.sons[0])
         if result < isGeneric: result = isNone
     else: nil
-  of tyOrdinal: 
-    if f.sons[0].kind != tyGenericParam: 
-      # some constraint:
-      result = constraintRel(mapping, f.sons[0], a)
-    elif isOrdinalType(a): 
+  of tyTypeClass:
+    result = constraintRel(mapping, f.sons[0], a)
+  of tyOrdinal:
+    if isOrdinalType(a):
       var x = if a.kind == tyOrdinal: a.sons[0] else: a
       result = typeRel(mapping, f.sons[0], x)
       if result < isGeneric: result = isNone
diff --git a/compiler/transf.nim b/compiler/transf.nim
index ba74323be..3c685eeb4 100755
--- a/compiler/transf.nim
+++ b/compiler/transf.nim
@@ -437,7 +437,7 @@ proc transformConv(c: PTransf, n: PNode): PTransNode =
       result = generateThunk(c, x, dest).ptransnode
     else:
       result = transformSons(c, n)    
-  of tyGenericParam, tyOrdinal: 
+  of tyGenericParam, tyOrdinal, tyTypeClass:
     result = transform(c, n.sons[1])
     # happens sometimes for generated assignments, etc.
   else: 
diff --git a/compiler/types.nim b/compiler/types.nim
index 5f7496fc0..8865735ab 100755
--- a/compiler/types.nim
+++ b/compiler/types.nim
@@ -48,15 +48,15 @@ proc equalParams*(a, b: PNode): TParamsEquality
 proc isOrdinalType*(t: PType): bool
 proc enumHasHoles*(t: PType): bool
 const 
-  abstractPtrs* = {tyVar, tyPtr, tyRef, tyGenericInst, tyDistinct, tyOrdinal,
+  abstractPtrs* = {tyVar, tyPtr, tyRef, tyGenericInst, tyDistinct,
                    tyConst, tyMutable}
-  abstractVar* = {tyVar, tyGenericInst, tyDistinct, tyOrdinal, 
+  abstractVar* = {tyVar, tyGenericInst, tyDistinct,
                   tyConst, tyMutable}
-  abstractRange* = {tyGenericInst, tyRange, tyDistinct, tyOrdinal,
+  abstractRange* = {tyGenericInst, tyRange, tyDistinct,
                     tyConst, tyMutable}
-  abstractVarRange* = {tyGenericInst, tyRange, tyVar, tyDistinct, tyOrdinal,
+  abstractVarRange* = {tyGenericInst, tyRange, tyVar, tyDistinct,
                        tyConst, tyMutable}
-  abstractInst* = {tyGenericInst, tyDistinct, tyOrdinal, tyConst, tyMutable}
+  abstractInst* = {tyGenericInst, tyDistinct, tyConst, tyMutable}
 
   skipPtrs* = {tyVar, tyPtr, tyRef, tyGenericInst, tyConst, tyMutable}
 
@@ -142,7 +142,7 @@ proc skipTypes(t: PType, kinds: TTypeKinds): PType =
 proc isOrdinalType(t: PType): bool = 
   assert(t != nil)
   result = (t.Kind in {tyChar, tyInt..tyInt64, tyBool, tyEnum}) or
-      (t.Kind in {tyRange, tyOrdinal, tyConst, tyMutable, tyGenericInst}) and
+      (t.Kind in {tyRange, tyConst, tyMutable, tyGenericInst}) and
        isOrdinalType(t.sons[0])
 
 proc enumHasHoles(t: PType): bool = 
@@ -383,7 +383,7 @@ proc TypeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
       "int16", "int32", "int64", "float", "float32", "float64", "float128",
       
       "uint", "uint8", "uint16", "uint32", "uint64", "bignum", "const ",
-      "!", "varargs[$1]", "iter[$1]", "proxy[$1]"]
+      "!", "varargs[$1]", "iter[$1]", "proxy[$1]", "TypeClass" ]
   var t = typ
   result = ""
   if t == nil: return 
@@ -409,7 +409,7 @@ proc TypeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
   of tySequence: 
     result = "seq[" & typeToString(t.sons[0]) & ']'
   of tyOrdinal: 
-    result = "ordinal[" & typeToString(t.sons[0]) & ']'
+    result = "ordinal"
   of tySet: 
     result = "set[" & typeToString(t.sons[0]) & ']'
   of tyOpenArray: 
@@ -729,8 +729,8 @@ proc SameTypeAux(x, y: PType, c: var TSameTypeClosure): bool =
       while a.kind == tyDistinct: a = a.sons[0]
       if a.kind != b.kind: return false
   case a.Kind
-  of tyEmpty, tyChar, tyBool, tyNil, tyPointer, tyString, tyCString, 
-     tyInt..tyBigNum, tyExpr, tyStmt, tyTypeDesc: 
+  of tyEmpty, tyChar, tyBool, tyNil, tyPointer, tyString, tyCString,
+     tyInt..tyBigNum, tyExpr, tyStmt, tyTypeDesc, tyOrdinal:
     result = true
   of tyObject:
     IfFastObjectTypeCheckFailed(a, b):
@@ -740,7 +740,7 @@ proc SameTypeAux(x, y: PType, c: var TSameTypeClosure): bool =
     CycleCheck()
     if c.cmp == dcEq: result = sameDistinctTypes(a, b)
     else: result = sameTypeAux(a.sons[0], b.sons[0], c)
-  of tyEnum, tyForward, tyProxy:
+  of tyEnum, tyForward, tyProxy, tyTypeClass:
     # XXX generic enums do not make much sense, but require structural checking
     result = a.id == b.id
   of tyTuple:
@@ -748,7 +748,7 @@ proc SameTypeAux(x, y: PType, c: var TSameTypeClosure): bool =
     result = sameTuple(a, b, c)
   of tyGenericInst: result = sameTypeAux(lastSon(a), lastSon(b), c)
   of tyGenericParam, tyGenericInvokation, tyGenericBody, tySequence,
-     tyOrdinal, tyOpenArray, tySet, tyRef, tyPtr, tyVar, tyArrayConstr,
+     tyOpenArray, tySet, tyRef, tyPtr, tyVar, tyArrayConstr,
      tyArray, tyProc, tyConst, tyMutable, tyVarargs, tyIter: 
     if sonsLen(a) == sonsLen(b):
       CycleCheck()
@@ -845,7 +845,7 @@ proc typeAllowedAux(marker: var TIntSet, typ: PType, kind: TSymKind): bool =
       result = typeAllowedAux(marker, t.sons[0], skResult)
   of tyExpr, tyStmt, tyTypeDesc: 
     result = true
-  of tyGenericBody, tyGenericParam, tyForward, tyNone, tyGenericInvokation: 
+  of tyGenericBody, tyGenericParam, tyForward, tyNone, tyGenericInvokation, tyTypeClass:
     result = false            #InternalError('shit found');
   of tyEmpty, tyNil:
     result = kind == skConst