first version of commonType

This commit is contained in:
Araq 2013-04-22 16:03:28 +02:00
commit adc75d0201
3 changed files with 57 additions and 0 deletions

View file

@ -58,6 +58,37 @@ proc fitNode(c: PContext, formal: PType, arg: PNode): PNode =
result = copyNode(arg)
result.typ = formal
proc commonType*(x, y: PType): PType =
# new type relation that is used for array constructors,
# if expressions, etc.:
if x == nil: return y
var a = skipTypes(x, {tyGenericInst})
var b = skipTypes(y, {tyGenericInst})
result = x
if a.kind in {tyExpr, tyNil}: return y
elif b.kind in {tyExpr, tyNil}: return x
elif b.kind in {tyArray, tyArrayConstr, tySet, tySequence} and
a.kind == b.kind:
# check for seq[empty] vs. seq[int]
let idx = ord(b.kind in {tyArray, tyArrayConstr})
if a.sons[idx].kind == tyEmpty: return y
#elif b.sons[idx].kind == tyEmpty: return x
else:
var k = tyNone
if a.kind in {tyRef, tyPtr}:
k = a.kind
if b.kind != a.kind: return x
a = a.sons[0]
b = b.sons[0]
if a.kind == tyObject and b.kind == tyObject:
result = commonSuperclass(a, b)
# this will trigger an error later:
if result.isNil: return x
if k != tyNone:
let r = result
result = NewType(k, r.owner)
result.addSonSkipIntLit(r)
proc isTopLevel(c: PContext): bool {.inline.} =
result = c.tab.tos <= 2