first version of commonType
This commit is contained in:
parent
f7c7d95d89
commit
adc75d0201
3 changed files with 57 additions and 0 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue