allow the use of built-in type constraints in type sections

This commit is contained in:
Zahary Karadjov 2012-04-20 22:28:53 +03:00
commit 064f296213

View file

@ -17,7 +17,12 @@ proc newOrPrevType(kind: TTypeKind, prev: PType, c: PContext): PType =
result = prev result = prev
if result.kind == tyForward: result.kind = kind if result.kind == tyForward: result.kind = kind
proc newConstraint(c: PContext, k: TTypeKind): PType =
result = newTypeS(tyTypeClass, c)
result.addSon(newTypeS(k, c))
proc semEnum(c: PContext, n: PNode, prev: PType): PType = proc semEnum(c: PContext, n: PNode, prev: PType): PType =
if n.sonsLen == 0: return newConstraint(c, tyEnum)
var var
counter, x: BiggestInt counter, x: BiggestInt
e: PSym e: PSym
@ -93,28 +98,29 @@ proc semContainer(c: PContext, n: PNode, kind: TTypeKind, kindStr: string,
else: else:
GlobalError(n.info, errXExpectsOneTypeParam, kindStr) GlobalError(n.info, errXExpectsOneTypeParam, kindStr)
proc semAnyRef(c: PContext, n: PNode, kind: TTypeKind, kindStr: string, proc semAnyRef(c: PContext, n: PNode, kind: TTypeKind, prev: PType): PType =
prev: PType): PType =
result = newOrPrevType(kind, prev, c)
if sonsLen(n) == 1: if sonsLen(n) == 1:
result = newOrPrevType(kind, prev, c)
var base = semTypeNode(c, n.sons[0], nil) var base = semTypeNode(c, n.sons[0], nil)
addSon(result, base) addSon(result, base)
else: else:
GlobalError(n.info, errXExpectsOneTypeParam, kindStr) result = newConstraint(c, kind)
proc semVarType(c: PContext, n: PNode, prev: PType): PType = proc semVarType(c: PContext, n: PNode, prev: PType): PType =
result = newOrPrevType(tyVar, prev, c)
if sonsLen(n) == 1: if sonsLen(n) == 1:
result = newOrPrevType(tyVar, prev, c)
var base = semTypeNode(c, n.sons[0], nil) var base = semTypeNode(c, n.sons[0], nil)
if base.kind == tyVar: GlobalError(n.info, errVarVarTypeNotAllowed) if base.kind == tyVar: GlobalError(n.info, errVarVarTypeNotAllowed)
addSon(result, base) addSon(result, base)
else: else:
GlobalError(n.info, errXExpectsOneTypeParam, "var") result = newConstraint(c, tyVar)
proc semDistinct(c: PContext, n: PNode, prev: PType): PType = proc semDistinct(c: PContext, n: PNode, prev: PType): PType =
result = newOrPrevType(tyDistinct, prev, c) if sonsLen(n) == 1:
if sonsLen(n) == 1: addSon(result, semTypeNode(c, n.sons[0], nil)) result = newOrPrevType(tyDistinct, prev, c)
else: GlobalError(n.info, errXExpectsOneTypeParam, "distinct") addSon(result, semTypeNode(c, n.sons[0], nil))
else:
result = newConstraint(c, tyDistinct)
proc semRangeAux(c: PContext, n: PNode, prev: PType): PType = proc semRangeAux(c: PContext, n: PNode, prev: PType): PType =
assert IsRange(n) assert IsRange(n)
@ -197,8 +203,8 @@ proc semTypeIdent(c: PContext, n: PNode): PSym =
GlobalError(n.info, errIdentifierExpected) GlobalError(n.info, errIdentifierExpected)
proc semTuple(c: PContext, n: PNode, prev: PType): PType = proc semTuple(c: PContext, n: PNode, prev: PType): PType =
var if n.sonsLen == 0: return newConstraint(c, tyTuple)
typ: PType var typ: PType
result = newOrPrevType(tyTuple, prev, c) result = newOrPrevType(tyTuple, prev, c)
result.n = newNodeI(nkRecList, n.info) result.n = newNodeI(nkRecList, n.info)
var check = initIntSet() var check = initIntSet()
@ -459,6 +465,7 @@ proc skipGenericInvokation(t: PType): PType {.inline.} =
result = lastSon(result) result = lastSon(result)
proc semObjectNode(c: PContext, n: PNode, prev: PType): PType = proc semObjectNode(c: PContext, n: PNode, prev: PType): PType =
if n.sonsLen == 0: return newConstraint(c, tyObject)
var check = initIntSet() var check = initIntSet()
var pos = 0 var pos = 0
var base: PType = nil var base: PType = nil
@ -766,8 +773,8 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
GlobalError(n.info, errTypeExpected) GlobalError(n.info, errTypeExpected)
of nkObjectTy: result = semObjectNode(c, n, prev) of nkObjectTy: result = semObjectNode(c, n, prev)
of nkTupleTy: result = semTuple(c, n, prev) of nkTupleTy: result = semTuple(c, n, prev)
of nkRefTy: result = semAnyRef(c, n, tyRef, "ref", prev) of nkRefTy: result = semAnyRef(c, n, tyRef, prev)
of nkPtrTy: result = semAnyRef(c, n, tyPtr, "ptr", prev) of nkPtrTy: result = semAnyRef(c, n, tyPtr, prev)
of nkVarTy: result = semVarType(c, n, prev) of nkVarTy: result = semVarType(c, n, prev)
of nkDistinctTy: result = semDistinct(c, n, prev) of nkDistinctTy: result = semDistinct(c, n, prev)
of nkProcTy: of nkProcTy:
@ -828,23 +835,9 @@ proc processMagicType(c: PContext, m: PSym) =
of mPNimrodNode: nil of mPNimrodNode: nil
else: GlobalError(m.info, errTypeExpected) else: GlobalError(m.info, errTypeExpected)
proc newConstraint(c: PContext, k: TTypeKind): PType =
result = newTypeS(tyTypeClass, c)
result.addSon(newTypeS(k, c))
proc semGenericConstraints(c: PContext, n: PNode, result: PType) = proc semGenericConstraints(c: PContext, n: PNode, result: PType) =
case n.kind case n.kind
of nkProcTy: result.addSon(newConstraint(c, tyProc)) of nkProcTy: result.addSon(newConstraint(c, tyProc))
of nkEnumTy: result.addSon(newConstraint(c, tyEnum))
of nkObjectTy: result.addSon(newConstraint(c, tyObject))
of nkTupleTy: result.addSon(newConstraint(c, tyTuple))
of nkDistinctTy: result.addSon(newConstraint(c, tyDistinct))
of nkVarTy: result.addSon(newConstraint(c, tyVar))
of nkPtrTy: result.addSon(newConstraint(c, tyPtr))
of nkRefTy: result.addSon(newConstraint(c, tyRef))
of nkInfix:
semGenericConstraints(c, n.sons[1], result)
semGenericConstraints(c, n.sons[2], result)
else: else:
var x = semTypeNode(c, n, nil) var x = semTypeNode(c, n, nil)
if x.kind in StructuralEquivTypes and ( if x.kind in StructuralEquivTypes and (