'is' operator for generic code

This commit is contained in:
Araq 2011-07-31 00:55:30 +02:00
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 = proc semOf(c: PContext, n: PNode): PNode =
if sonsLen(n) == 3: if sonsLen(n) == 3:
#LocalError(n.info, errXcanNeverBeOfThisSubtype, " CAME HERE")
n.sons[1] = semExprWithType(c, n.sons[1], {efAllowType}) n.sons[1] = semExprWithType(c, n.sons[1], {efAllowType})
n.sons[2] = semExprWithType(c, n.sons[2], {efAllowType}) n.sons[2] = semExprWithType(c, n.sons[2], {efAllowType})
var a = skipTypes(n.sons[1].typ, abstractPtrs) var a = skipTypes(n.sons[1].typ, abstractPtrs)
@ -220,22 +219,14 @@ proc semOf(c: PContext, n: PNode): PNode =
result = n result = n
proc semIs(c: PContext, n: PNode): PNode = proc semIs(c: PContext, n: PNode): PNode =
GlobalError(n.info, errXExpectsTwoArguments, "is") if sonsLen(n) == 3:
if sonsLen(n) == 3: var a = semExprWithType(c, n.sons[1], {efAllowType})
#LocalError(n.info, errXcanNeverBeOfThisSubtype, " CAME HERE") var b = semExprWithType(c, n.sons[2], {efAllowType})
n.sons[1] = semExprWithType(c, n.sons[1], {efAllowType}) result = newIntNode(nkIntLit, ord(sameType(a.typ, b.typ)))
n.sons[2] = semExprWithType(c, n.sons[2], {efAllowType}) result.typ = getSysType(tyBool)
var a = skipTypes(n.sons[1].typ, abstractPtrs) result.info = n.info
var b = skipTypes(n.sons[2].typ, abstractPtrs) else:
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:
GlobalError(n.info, errXExpectsTwoArguments, "is") GlobalError(n.info, errXExpectsTwoArguments, "is")
result = n
proc semOpAux(c: PContext, n: PNode) = proc semOpAux(c: PContext, n: PNode) =
for i in countup(1, sonsLen(n) - 1): for i in countup(1, sonsLen(n) - 1):

View file

@ -348,14 +348,14 @@ proc getConstExpr(m: PSym, n: PNode): PNode =
of nkIfExpr: of nkIfExpr:
result = getConstIfExpr(m, n) result = getConstIfExpr(m, n)
of nkCall, nkCommand, nkCallStrLit: of nkCall, nkCommand, nkCallStrLit:
if (n.sons[0].kind != nkSym): return if n.sons[0].kind != nkSym: return
var s = n.sons[0].sym var s = n.sons[0].sym
if (s.kind != skProc): return if s.kind != skProc: return
try: try:
case s.magic case s.magic
of mNone: of mNone:
return # XXX: if it has no sideEffect, it should be evaluated return # XXX: if it has no sideEffect, it should be evaluated
of mSizeOf: of mSizeOf:
var a = n.sons[1] var a = n.sons[1]
if computeSize(a.typ) < 0: if computeSize(a.typ) < 0:
LocalError(a.info, errCannotEvalXBecauseIncompletelyDefined, 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 Version 0.8.14
============== ==============
- ``when T is int`` for generic code
- ``var T`` as a return type: - ``var T`` as a return type:
* for iterators * for iterators
* add ``modGet`` for generics * add ``modGet`` for generics