untyped proc/template/macro params default to expr
This commit is contained in:
parent
84202d2a4e
commit
70f5236d48
2 changed files with 15 additions and 17 deletions
|
|
@ -619,7 +619,7 @@ proc parseParamList(p: var TParser, retColon = true): PNode =
|
||||||
while true:
|
while true:
|
||||||
case p.tok.tokType #optInd(p, a);
|
case p.tok.tokType #optInd(p, a);
|
||||||
of tkSymbol, tkAccent:
|
of tkSymbol, tkAccent:
|
||||||
a = parseIdentColonEquals(p, {})
|
a = parseIdentColonEquals(p, {withBothOptional})
|
||||||
of tkParRi:
|
of tkParRi:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -523,8 +523,7 @@ proc addParamOrResult(c: PContext, param: PSym, kind: TSymKind) =
|
||||||
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
|
||||||
def, res: PNode
|
res: PNode
|
||||||
typ: PType
|
|
||||||
cl: TIntSet
|
cl: TIntSet
|
||||||
checkMinSonsLen(n, 1)
|
checkMinSonsLen(n, 1)
|
||||||
result = newOrPrevType(tyProc, prev, c)
|
result = newOrPrevType(tyProc, prev, c)
|
||||||
|
|
@ -541,20 +540,18 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
|
||||||
var a = n.sons[i]
|
var a = n.sons[i]
|
||||||
if a.kind != nkIdentDefs: IllFormedAst(a)
|
if a.kind != nkIdentDefs: IllFormedAst(a)
|
||||||
checkMinSonsLen(a, 3)
|
checkMinSonsLen(a, 3)
|
||||||
var length = sonsLen(a)
|
var
|
||||||
if a.sons[length-2].kind != nkEmpty:
|
typ: PType = nil
|
||||||
|
def: PNode = nil
|
||||||
|
length = sonsLen(a)
|
||||||
|
hasType = a.sons[length-2].kind != nkEmpty
|
||||||
|
hasDefault = a.sons[length-1].kind != nkEmpty
|
||||||
|
|
||||||
|
if hasType:
|
||||||
typ = paramType(c, a.sons[length-2], genericParams, cl)
|
typ = paramType(c, a.sons[length-2], genericParams, cl)
|
||||||
#if matchType(typ, [(tyVar, 0)], tyGenericInvokation):
|
#if matchType(typ, [(tyVar, 0)], tyGenericInvokation):
|
||||||
# debug a.sons[length-2][0][1]
|
# debug a.sons[length-2][0][1]
|
||||||
else:
|
if hasDefault:
|
||||||
typ = nil
|
|
||||||
# consider:
|
|
||||||
# template T(x)
|
|
||||||
# it's wrong, but the parser might not generate a fatal error (when in
|
|
||||||
# 'check' mode for example), so we need to check again here:
|
|
||||||
if unlikely(a.sons[length-1].kind == nkEmpty): continue
|
|
||||||
|
|
||||||
if a.sons[length-1].kind != nkEmpty:
|
|
||||||
def = semExprWithType(c, a.sons[length-1])
|
def = semExprWithType(c, a.sons[length-1])
|
||||||
# check type compability between def.typ and typ:
|
# check type compability between def.typ and typ:
|
||||||
if typ == nil:
|
if typ == nil:
|
||||||
|
|
@ -565,15 +562,16 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
|
||||||
# proc sort[T](cmp: proc(a, b: T): int = cmp)
|
# proc sort[T](cmp: proc(a, b: T): int = cmp)
|
||||||
if not containsGenericType(typ):
|
if not containsGenericType(typ):
|
||||||
def = fitNode(c, typ, def)
|
def = fitNode(c, typ, def)
|
||||||
else:
|
if not (hasType or hasDefault):
|
||||||
def = ast.emptyNode
|
typ = newTypeS(tyExpr, c)
|
||||||
|
|
||||||
if skipTypes(typ, {tyGenericInst}).kind == tyEmpty: continue
|
if skipTypes(typ, {tyGenericInst}).kind == tyEmpty: continue
|
||||||
for j in countup(0, length-3):
|
for j in countup(0, length-3):
|
||||||
var arg = newSymS(skParam, a.sons[j], c)
|
var arg = newSymS(skParam, a.sons[j], c)
|
||||||
arg.typ = typ
|
arg.typ = typ
|
||||||
arg.position = counter
|
arg.position = counter
|
||||||
inc(counter)
|
inc(counter)
|
||||||
if 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):
|
||||||
LocalError(a.sons[j].info, errAttemptToRedefine, arg.name.s)
|
LocalError(a.sons[j].info, errAttemptToRedefine, arg.name.s)
|
||||||
addSon(result.n, newSymNode(arg))
|
addSon(result.n, newSymNode(arg))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue