'is' operator for generic code

This commit is contained in:
Araq
2011-07-31 00:55:30 +02:00
parent 6a8a409f1b
commit 05cffb9370
4 changed files with 25 additions and 22 deletions

View File

@@ -204,7 +204,6 @@ proc semSizeof(c: PContext, n: PNode): PNode =
proc semOf(c: PContext, n: PNode): PNode =
if sonsLen(n) == 3:
#LocalError(n.info, errXcanNeverBeOfThisSubtype, " CAME HERE")
n.sons[1] = semExprWithType(c, n.sons[1], {efAllowType})
n.sons[2] = semExprWithType(c, n.sons[2], {efAllowType})
var a = skipTypes(n.sons[1].typ, abstractPtrs)
@@ -220,22 +219,14 @@ proc semOf(c: PContext, n: PNode): PNode =
result = n
proc semIs(c: PContext, n: PNode): PNode =
GlobalError(n.info, errXExpectsTwoArguments, "is")
if sonsLen(n) == 3:
#LocalError(n.info, errXcanNeverBeOfThisSubtype, " CAME HERE")
n.sons[1] = semExprWithType(c, n.sons[1], {efAllowType})
n.sons[2] = semExprWithType(c, n.sons[2], {efAllowType})
var a = skipTypes(n.sons[1].typ, abstractPtrs)
var b = skipTypes(n.sons[2].typ, abstractPtrs)
if b.kind != tyObject or a.kind != tyObject:
GlobalError(n.info, errXExpectsObjectTypes, "is")
while b != nil and b.id != a.id: b = b.sons[0]
if b == nil:
GlobalError(n.info, errXcanNeverBeOfThisSubtype, typeToString(a))
n.typ = getSysType(tyBool)
else:
if sonsLen(n) == 3:
var a = semExprWithType(c, n.sons[1], {efAllowType})
var b = semExprWithType(c, n.sons[2], {efAllowType})
result = newIntNode(nkIntLit, ord(sameType(a.typ, b.typ)))
result.typ = getSysType(tyBool)
result.info = n.info
else:
GlobalError(n.info, errXExpectsTwoArguments, "is")
result = n
proc semOpAux(c: PContext, n: PNode) =
for i in countup(1, sonsLen(n) - 1):

View File

@@ -348,14 +348,14 @@ proc getConstExpr(m: PSym, n: PNode): PNode =
of nkIfExpr:
result = getConstIfExpr(m, n)
of nkCall, nkCommand, nkCallStrLit:
if (n.sons[0].kind != nkSym): return
if n.sons[0].kind != nkSym: return
var s = n.sons[0].sym
if (s.kind != skProc): return
try:
if s.kind != skProc: return
try:
case s.magic
of mNone:
of mNone:
return # XXX: if it has no sideEffect, it should be evaluated
of mSizeOf:
of mSizeOf:
var a = n.sons[1]
if computeSize(a.typ) < 0:
LocalError(a.info, errCannotEvalXBecauseIncompletelyDefined,

View File

@@ -0,0 +1,13 @@
discard """
output: "true true false yes"
"""
proc IsVoid[T](): string =
when T is void:
result = "yes"
else:
result = "no"
const x = int is int
echo x, " ", float is float, " ", float is string, " ", IsVoid[void]()

View File

@@ -1,7 +1,6 @@
Version 0.8.14
==============
- ``when T is int`` for generic code
- ``var T`` as a return type:
* for iterators
* add ``modGet`` for generics