summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-08-19 22:35:16 +0200
committerAraq <rumpf_a@web.de>2014-08-19 22:35:16 +0200
commitebe313e627d0184596cc7af6d49fa0814ec407d0 (patch)
tree287b0f3e3f6ccc42444438103495b0eb261d8172 /compiler
parent7ef076a04afa7201ab945eecfe2386d14c5084a7 (diff)
downloadNim-ebe313e627d0184596cc7af6d49fa0814ec407d0.tar.gz
fixes #1334
Diffstat (limited to 'compiler')
-rw-r--r--compiler/astalgo.nim12
-rw-r--r--compiler/types.nim32
2 files changed, 32 insertions, 12 deletions
diff --git a/compiler/astalgo.nim b/compiler/astalgo.nim
index 16efbb172..eb7ffc63e 100644
--- a/compiler/astalgo.nim
+++ b/compiler/astalgo.nim
@@ -116,14 +116,6 @@ proc iiTablePut*(t: var TIITable, key, val: int)
 
 # implementation
 
-proc skipConv*(n: PNode): PNode = 
-  case n.kind
-  of nkObjUpConv, nkObjDownConv, nkChckRange, nkChckRangeF, nkChckRange64:
-    result = n.sons[0]
-  of nkHiddenStdConv, nkHiddenSubConv, nkConv:
-    result = n.sons[1]
-  else: result = n
-
 proc skipConvAndClosure*(n: PNode): PNode =
   result = n
   while true:
@@ -135,10 +127,6 @@ proc skipConvAndClosure*(n: PNode): PNode =
       result = result.sons[1]
     else: break
 
-proc skipConvTakeType*(n: PNode): PNode =
-  result = n.skipConv
-  result.typ = n.typ
-
 proc sameValue*(a, b: PNode): bool = 
   result = false
   case a.kind
diff --git a/compiler/types.nim b/compiler/types.nim
index b25e3f697..824ca47d0 100644
--- a/compiler/types.nim
+++ b/compiler/types.nim
@@ -1369,3 +1369,35 @@ proc containsCompileTimeOnly*(t: PType): bool =
       if t.sons[i] != nil and isCompileTimeOnly(t.sons[i]):
         return true
   return false
+
+type
+  OrdinalType* = enum
+    NoneLike, IntLike, FloatLike
+
+proc classify*(t: PType): OrdinalType =
+  ## for convenient type checking:
+  if t == nil:
+    result = NoneLike
+  else:
+    case skipTypes(t, abstractVarRange).kind
+    of tyFloat..tyFloat128: result = FloatLike
+    of tyInt..tyInt64, tyUInt..tyUInt64, tyBool, tyChar, tyEnum:
+      result = IntLike
+    else: result = NoneLike
+
+proc skipConv*(n: PNode): PNode =
+  result = n
+  case n.kind
+  of nkObjUpConv, nkObjDownConv, nkChckRange, nkChckRangeF, nkChckRange64:
+    # only skip the conversion if it doesn't lose too important information
+    # (see bug #
+    if n.sons[0].typ.classify == n.typ.classify:
+      result = n.sons[0]
+  of nkHiddenStdConv, nkHiddenSubConv, nkConv:
+    if n.sons[1].typ.classify == n.typ.classify:
+      result = n.sons[1]
+  else: discard
+
+proc skipConvTakeType*(n: PNode): PNode =
+  result = n.skipConv
+  result.typ = n.typ