constraint now part of the parameter symbol and not of the type
This commit is contained in:
parent
0834224070
commit
7171ae62cb
7 changed files with 25 additions and 21 deletions
|
|
@ -624,6 +624,7 @@ type
|
||||||
loc*: TLoc
|
loc*: TLoc
|
||||||
annex*: PLib # additional fields (seldom used, so we use a
|
annex*: PLib # additional fields (seldom used, so we use a
|
||||||
# reference to another object to safe space)
|
# reference to another object to safe space)
|
||||||
|
constraint*: PNode # additional constraints like 'lit|result'
|
||||||
|
|
||||||
TTypeSeq* = seq[PType]
|
TTypeSeq* = seq[PType]
|
||||||
TType* = object of TIdObj # types are identical iff they have the
|
TType* = object of TIdObj # types are identical iff they have the
|
||||||
|
|
@ -650,7 +651,6 @@ type
|
||||||
align*: int # the type's alignment requirements
|
align*: int # the type's alignment requirements
|
||||||
containerID*: int # used for type checking of generics
|
containerID*: int # used for type checking of generics
|
||||||
loc*: TLoc
|
loc*: TLoc
|
||||||
constraint*: PNode # additional constraints like 'lit|result'
|
|
||||||
|
|
||||||
TPair*{.final.} = object
|
TPair*{.final.} = object
|
||||||
key*, val*: PObject
|
key*, val*: PObject
|
||||||
|
|
|
||||||
|
|
@ -48,8 +48,8 @@ proc add(code: var TPatternCode, op: TOpcode) {.inline.} =
|
||||||
add(code, chr(ord(op)))
|
add(code, chr(ord(op)))
|
||||||
|
|
||||||
proc whichAlias*(p: PSym): TAliasRequest =
|
proc whichAlias*(p: PSym): TAliasRequest =
|
||||||
if p.typ.constraint != nil:
|
if p.constraint != nil:
|
||||||
result = TAliasRequest(p.typ.constraint.strVal[0].ord)
|
result = TAliasRequest(p.constraint.strVal[0].ord)
|
||||||
|
|
||||||
proc compileConstraints(p: PNode, result: var TPatternCode) =
|
proc compileConstraints(p: PNode, result: var TPatternCode) =
|
||||||
case p.kind
|
case p.kind
|
||||||
|
|
|
||||||
|
|
@ -71,8 +71,8 @@ proc inSymChoice(sc, x: PNode): bool =
|
||||||
|
|
||||||
proc checkTypes(c: PPatternContext, p: PSym, n: PNode): bool =
|
proc checkTypes(c: PPatternContext, p: PSym, n: PNode): bool =
|
||||||
# check param constraints first here as this is quite optimized:
|
# check param constraints first here as this is quite optimized:
|
||||||
if p.typ.constraint != nil:
|
if p.constraint != nil:
|
||||||
result = matchNodeKinds(p.typ.constraint, n)
|
result = matchNodeKinds(p.constraint, n)
|
||||||
if not result: return
|
if not result: return
|
||||||
if isNil(n.typ):
|
if isNil(n.typ):
|
||||||
result = p.typ.kind in {tyEmpty, tyStmt}
|
result = p.typ.kind in {tyEmpty, tyStmt}
|
||||||
|
|
|
||||||
|
|
@ -330,9 +330,6 @@ proc decodeType(r: PRodReader, info: TLineInfo): PType =
|
||||||
if r.s[r.pos] == '@':
|
if r.s[r.pos] == '@':
|
||||||
inc(r.pos)
|
inc(r.pos)
|
||||||
result.containerID = decodeVInt(r.s, r.pos)
|
result.containerID = decodeVInt(r.s, r.pos)
|
||||||
if r.s[r.pos] == '`':
|
|
||||||
inc(r.pos)
|
|
||||||
result.constraint = decodeNode(r, UnknownLineInfo())
|
|
||||||
decodeLoc(r, result.loc, info)
|
decodeLoc(r, result.loc, info)
|
||||||
while r.s[r.pos] == '^':
|
while r.s[r.pos] == '^':
|
||||||
inc(r.pos)
|
inc(r.pos)
|
||||||
|
|
@ -423,6 +420,9 @@ proc decodeSym(r: PRodReader, info: TLineInfo): PSym =
|
||||||
result.offset = - 1
|
result.offset = - 1
|
||||||
decodeLoc(r, result.loc, result.info)
|
decodeLoc(r, result.loc, result.info)
|
||||||
result.annex = decodeLib(r, info)
|
result.annex = decodeLib(r, info)
|
||||||
|
if r.s[r.pos] == '#':
|
||||||
|
inc(r.pos)
|
||||||
|
result.constraint = decodeNode(r, UnknownLineInfo())
|
||||||
if r.s[r.pos] == '(':
|
if r.s[r.pos] == '(':
|
||||||
if result.kind in routineKinds:
|
if result.kind in routineKinds:
|
||||||
result.ast = decodeNodeLazyBody(r, result.info, result)
|
result.ast = decodeNodeLazyBody(r, result.info, result)
|
||||||
|
|
|
||||||
|
|
@ -233,9 +233,6 @@ proc encodeType(w: PRodWriter, t: PType, result: var string) =
|
||||||
if t.containerID != 0:
|
if t.containerID != 0:
|
||||||
add(result, '@')
|
add(result, '@')
|
||||||
encodeVInt(t.containerID, result)
|
encodeVInt(t.containerID, result)
|
||||||
if t.constraint != nil:
|
|
||||||
add(result, '`')
|
|
||||||
encodeNode(w, UnknownLineInfo(), t.constraint, result)
|
|
||||||
encodeLoc(w, t.loc, result)
|
encodeLoc(w, t.loc, result)
|
||||||
for i in countup(0, sonsLen(t) - 1):
|
for i in countup(0, sonsLen(t) - 1):
|
||||||
if t.sons[i] == nil:
|
if t.sons[i] == nil:
|
||||||
|
|
@ -295,6 +292,9 @@ proc encodeSym(w: PRodWriter, s: PSym, result: var string) =
|
||||||
encodeVInt(s.offset, result)
|
encodeVInt(s.offset, result)
|
||||||
encodeLoc(w, s.loc, result)
|
encodeLoc(w, s.loc, result)
|
||||||
if s.annex != nil: encodeLib(w, s.annex, s.info, result)
|
if s.annex != nil: encodeLib(w, s.annex, s.info, result)
|
||||||
|
if s.constraint != nil:
|
||||||
|
add(result, '#')
|
||||||
|
encodeNode(w, UnknownLineInfo(), s.constraint, result)
|
||||||
# lazy loading will soon reload the ast lazily, so the ast needs to be
|
# lazy loading will soon reload the ast lazily, so the ast needs to be
|
||||||
# the last entry of a symbol:
|
# the last entry of a symbol:
|
||||||
if s.ast != nil:
|
if s.ast != nil:
|
||||||
|
|
|
||||||
|
|
@ -635,6 +635,13 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
|
||||||
genericParams.addSon(newSymNode(s))
|
genericParams.addSon(newSymNode(s))
|
||||||
result = typeClass
|
result = typeClass
|
||||||
|
|
||||||
|
proc semParamType(c: PContext, n: PNode, constraint: var PNode): PType =
|
||||||
|
if n.kind == nkCurlyExpr:
|
||||||
|
result = semTypeNode(c, n.sons[0], nil)
|
||||||
|
constraint = semNodeKindConstraints(n)
|
||||||
|
else:
|
||||||
|
result = semTypeNode(c, n, nil)
|
||||||
|
|
||||||
proc semProcTypeNode(c: PContext, n, genericParams: PNode,
|
proc semProcTypeNode(c: PContext, n, genericParams: PNode,
|
||||||
prev: PType, kind: TSymKind): PType =
|
prev: PType, kind: TSymKind): PType =
|
||||||
var
|
var
|
||||||
|
|
@ -660,13 +667,14 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
|
||||||
checkMinSonsLen(a, 3)
|
checkMinSonsLen(a, 3)
|
||||||
var
|
var
|
||||||
typ: PType = nil
|
typ: PType = nil
|
||||||
def: PNode = nil
|
def: PNode = nil
|
||||||
|
constraint: PNode = nil
|
||||||
length = sonsLen(a)
|
length = sonsLen(a)
|
||||||
hasType = a.sons[length-2].kind != nkEmpty
|
hasType = a.sons[length-2].kind != nkEmpty
|
||||||
hasDefault = a.sons[length-1].kind != nkEmpty
|
hasDefault = a.sons[length-1].kind != nkEmpty
|
||||||
|
|
||||||
if hasType:
|
if hasType:
|
||||||
typ = semTypeNode(c, a.sons[length-2], nil)
|
typ = semParamType(c, a.sons[length-2], constraint)
|
||||||
|
|
||||||
if hasDefault:
|
if hasDefault:
|
||||||
def = semExprWithType(c, a.sons[length-1])
|
def = semExprWithType(c, a.sons[length-1])
|
||||||
|
|
@ -689,6 +697,7 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
|
||||||
arg.name.s, arg.info).skipIntLit
|
arg.name.s, arg.info).skipIntLit
|
||||||
arg.typ = finalType
|
arg.typ = finalType
|
||||||
arg.position = counter
|
arg.position = counter
|
||||||
|
arg.constraint = constraint
|
||||||
inc(counter)
|
inc(counter)
|
||||||
if def != nil and def.kind != nkEmpty: arg.ast = copyTree(def)
|
if def != nil and def.kind != nkEmpty: arg.ast = copyTree(def)
|
||||||
if ContainsOrIncl(check, arg.name.id):
|
if ContainsOrIncl(check, arg.name.id):
|
||||||
|
|
@ -839,11 +848,6 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
|
||||||
result = semTypeExpr(c, n)
|
result = semTypeExpr(c, n)
|
||||||
else:
|
else:
|
||||||
result = semTypeExpr(c, n)
|
result = semTypeExpr(c, n)
|
||||||
of nkCurlyExpr:
|
|
||||||
result = semTypeNode(c, n.sons[0], nil)
|
|
||||||
if result != nil:
|
|
||||||
result = copyType(result, getCurrOwner(), true)
|
|
||||||
result.constraint = semNodeKindConstraints(n)
|
|
||||||
of nkWhenStmt:
|
of nkWhenStmt:
|
||||||
var whenResult = semWhen(c, n, false)
|
var whenResult = semWhen(c, n, false)
|
||||||
if whenResult.kind == nkStmtList: whenResult.kind = nkStmtListType
|
if whenResult.kind == nkStmtList: whenResult.kind = nkStmtListType
|
||||||
|
|
|
||||||
6
todo.txt
6
todo.txt
|
|
@ -1,8 +1,8 @@
|
||||||
version 0.9.2
|
version 0.9.2
|
||||||
=============
|
=============
|
||||||
|
|
||||||
- overloading based on ASTs: 'constraint' should not be in PType but for the
|
- overloading based on ASTs
|
||||||
parameter *symbol*
|
- implement generic converters
|
||||||
|
|
||||||
- implement ``partial`` pragma for partial evaluation: easily done with AST
|
- implement ``partial`` pragma for partial evaluation: easily done with AST
|
||||||
overloading
|
overloading
|
||||||
|
|
@ -20,7 +20,7 @@ version 0.9.X
|
||||||
=============
|
=============
|
||||||
|
|
||||||
- implement the missing features wrt inheritance
|
- implement the missing features wrt inheritance
|
||||||
- implement generic methods and generic converters
|
- implement generic methods
|
||||||
- improve the compiler as a service
|
- improve the compiler as a service
|
||||||
- ``=`` should be overloadable; requires specialization for ``=``
|
- ``=`` should be overloadable; requires specialization for ``=``
|
||||||
- implement constructors + full 'not nil' checking
|
- implement constructors + full 'not nil' checking
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue