proc arguments are in scope when the result type definition is being processed
This is intended for type deduction depending on the arguments proc foo(x: type): type(expr(x))
This commit is contained in:
parent
e4e74034c2
commit
de4b894541
5 changed files with 16 additions and 5 deletions
|
|
@ -135,7 +135,8 @@ proc generateInstance(c: PContext, fn: PSym, pt: TIdTable,
|
||||||
if n.sons[paramsPos].kind != nkEmpty:
|
if n.sons[paramsPos].kind != nkEmpty:
|
||||||
removeDefaultParamValues(n.sons[ParamsPos])
|
removeDefaultParamValues(n.sons[ParamsPos])
|
||||||
semParamList(c, n.sons[ParamsPos], nil, result)
|
semParamList(c, n.sons[ParamsPos], nil, result)
|
||||||
addParams(c, result.typ.n)
|
# XXX: obsoleted - happens in semParamList #
|
||||||
|
# addParams(c, result.typ.n)
|
||||||
else:
|
else:
|
||||||
result.typ = newTypeS(tyProc, c)
|
result.typ = newTypeS(tyProc, c)
|
||||||
addSon(result.typ, nil)
|
addSon(result.typ, nil)
|
||||||
|
|
|
||||||
|
|
@ -616,7 +616,8 @@ proc semLambda(c: PContext, n: PNode): PNode =
|
||||||
illFormedAst(n) # process parameters:
|
illFormedAst(n) # process parameters:
|
||||||
if n.sons[paramsPos].kind != nkEmpty:
|
if n.sons[paramsPos].kind != nkEmpty:
|
||||||
semParamList(c, n.sons[ParamsPos], nil, s)
|
semParamList(c, n.sons[ParamsPos], nil, s)
|
||||||
addParams(c, s.typ.n)
|
# XXX: obsoleted - happens in semParamList
|
||||||
|
# addParams(c, s.typ.n)
|
||||||
ParamsTypeCheck(c, s.typ)
|
ParamsTypeCheck(c, s.typ)
|
||||||
else:
|
else:
|
||||||
s.typ = newTypeS(tyProc, c)
|
s.typ = newTypeS(tyProc, c)
|
||||||
|
|
@ -665,7 +666,8 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
|
||||||
n.sons[genericParamsPos] = gp
|
n.sons[genericParamsPos] = gp
|
||||||
# check for semantics again:
|
# check for semantics again:
|
||||||
semParamList(c, n.sons[ParamsPos], nil, s)
|
semParamList(c, n.sons[ParamsPos], nil, s)
|
||||||
addParams(c, s.typ.n)
|
# XXX: obsoleted - happens in semParamList
|
||||||
|
# addParams(c, s.typ.n)
|
||||||
else:
|
else:
|
||||||
s.typ = newTypeS(tyProc, c)
|
s.typ = newTypeS(tyProc, c)
|
||||||
addSon(s.typ, nil)
|
addSon(s.typ, nil)
|
||||||
|
|
|
||||||
|
|
@ -185,7 +185,8 @@ proc semTemplateDef(c: PContext, n: PNode): PNode =
|
||||||
# use ``stmt`` as implicit result type
|
# use ``stmt`` as implicit result type
|
||||||
s.typ.sons[0] = newTypeS(tyStmt, c)
|
s.typ.sons[0] = newTypeS(tyStmt, c)
|
||||||
s.typ.n.sons[0] = newNodeIT(nkType, n.info, s.typ.sons[0])
|
s.typ.n.sons[0] = newNodeIT(nkType, n.info, s.typ.sons[0])
|
||||||
addParams(c, s.typ.n) # resolve parameters:
|
# XXX: obsoleted - happens in semParamList #
|
||||||
|
# addParams(c, s.typ.n) # resolve parameters:
|
||||||
var toBind = initIntSet()
|
var toBind = initIntSet()
|
||||||
n.sons[bodyPos] = resolveTemplateParams(c, n.sons[bodyPos], false, toBind)
|
n.sons[bodyPos] = resolveTemplateParams(c, n.sons[bodyPos], false, toBind)
|
||||||
if s.typ.sons[0].kind notin {tyStmt, tyTypeDesc}:
|
if s.typ.sons[0].kind notin {tyStmt, tyTypeDesc}:
|
||||||
|
|
|
||||||
|
|
@ -569,6 +569,8 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
|
||||||
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))
|
||||||
addSon(result, typ)
|
addSon(result, typ)
|
||||||
|
addDecl(c, arg)
|
||||||
|
|
||||||
if n.sons[0].kind != nkEmpty:
|
if n.sons[0].kind != nkEmpty:
|
||||||
var r = paramType(c, n.sons[0], genericParams, cl)
|
var r = paramType(c, n.sons[0], genericParams, cl)
|
||||||
# turn explicit 'void' return type into 'nil' because the rest of the
|
# turn explicit 'void' return type into 'nil' because the rest of the
|
||||||
|
|
@ -711,11 +713,13 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
|
||||||
of nkDistinctTy: result = semDistinct(c, n, prev)
|
of nkDistinctTy: result = semDistinct(c, n, prev)
|
||||||
of nkProcTy:
|
of nkProcTy:
|
||||||
checkSonsLen(n, 2)
|
checkSonsLen(n, 2)
|
||||||
|
openScope(c.tab)
|
||||||
result = semProcTypeNode(c, n.sons[0], nil, prev)
|
result = semProcTypeNode(c, n.sons[0], nil, prev)
|
||||||
# dummy symbol for `pragma`:
|
# dummy symbol for `pragma`:
|
||||||
var s = newSymS(skProc, newIdentNode(getIdent("dummy"), n.info), c)
|
var s = newSymS(skProc, newIdentNode(getIdent("dummy"), n.info), c)
|
||||||
s.typ = result
|
s.typ = result
|
||||||
pragma(c, s, n.sons[1], procTypePragmas)
|
pragma(c, s, n.sons[1], procTypePragmas)
|
||||||
|
closeScope(c.tab)
|
||||||
of nkEnumTy: result = semEnum(c, n, prev)
|
of nkEnumTy: result = semEnum(c, n, prev)
|
||||||
of nkType: result = n.typ
|
of nkType: result = n.typ
|
||||||
of nkStmtListType: result = semStmtListType(c, n, prev)
|
of nkStmtListType: result = semStmtListType(c, n, prev)
|
||||||
|
|
|
||||||
|
|
@ -35,3 +35,6 @@ var y*: type(t2(100)) = "test"
|
||||||
proc t6*(x: type(t3(0))): type(t1(0)) =
|
proc t6*(x: type(t3(0))): type(t1(0)) =
|
||||||
result = $x
|
result = $x
|
||||||
|
|
||||||
|
proc t7*(x: int): type($x) =
|
||||||
|
result = "test"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue