'bind' as a declarative statement

This commit is contained in:
Araq 2011-10-10 02:04:15 +02:00
commit 51e01879ba
18 changed files with 204 additions and 93 deletions

View file

@ -20,11 +20,13 @@ type
withinBind, withinTypeDesc
TSemGenericFlags = set[TSemGenericFlag]
proc semGenericStmt(c: PContext, n: PNode, flags: TSemGenericFlags): PNode
proc semGenericStmt(c: PContext, n: PNode, flags: TSemGenericFlags,
toBind: var TIntSet): PNode
proc semGenericStmtScope(c: PContext, n: PNode,
flags: TSemGenericFlags): PNode =
flags: TSemGenericFlags,
toBind: var TIntSet): PNode =
openScope(c.tab)
result = semGenericStmt(c, n, flags)
result = semGenericStmt(c, n, flags, toBind)
closeScope(c.tab)
proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym): PNode =
@ -60,7 +62,7 @@ proc getIdentNode(n: PNode): PNode =
result = n
proc semGenericStmt(c: PContext, n: PNode,
flags: TSemGenericFlags): PNode =
flags: TSemGenericFlags, toBind: var TIntSet): PNode =
result = n
if gCmd == cmdIdeTools: suggestStmt(c, n)
case n.kind
@ -68,10 +70,10 @@ proc semGenericStmt(c: PContext, n: PNode,
var s = SymtabGet(c.Tab, n.ident)
if s == nil:
# no error if symbol cannot be bound, unless in ``bind`` context:
if withinBind in flags:
if withinBind in flags:
localError(n.info, errUndeclaredIdentifier, n.ident.s)
else:
if withinBind in flags: result = symChoice(c, n, s)
if withinBind in flags or s.name.id in toBind: result = symChoice(c, n, s)
else: result = semGenericStmtSymbol(c, n, s)
of nkDotExpr:
var s = QualifiedLookUp(c, n, {})
@ -80,7 +82,9 @@ proc semGenericStmt(c: PContext, n: PNode,
of nkEmpty, nkSym..nkNilLit:
nil
of nkBind:
result = semGenericStmt(c, n.sons[0], flags+{withinBind})
result = semGenericStmt(c, n.sons[0], flags+{withinBind}, toBind)
of nkBindStmt:
result = semBindStmt(c, n, toBind)
of nkCall, nkHiddenCallConv, nkInfix, nkPrefix, nkCommand, nkCallStrLit:
# check if it is an expression macro:
checkMinSonsLen(n, 1)
@ -112,52 +116,53 @@ proc semGenericStmt(c: PContext, n: PNode,
result.sons[0] = newSymNode(s, n.sons[0].info)
first = 1
for i in countup(first, sonsLen(result) - 1):
result.sons[i] = semGenericStmt(c, result.sons[i], flags)
result.sons[i] = semGenericStmt(c, result.sons[i], flags, toBind)
of nkMacroStmt:
result = semMacroStmt(c, n, false)
for i in countup(0, sonsLen(result)-1):
result.sons[i] = semGenericStmt(c, result.sons[i], flags)
result.sons[i] = semGenericStmt(c, result.sons[i], flags, toBind)
of nkIfStmt:
for i in countup(0, sonsLen(n)-1):
n.sons[i] = semGenericStmtScope(c, n.sons[i], flags)
n.sons[i] = semGenericStmtScope(c, n.sons[i], flags, toBind)
of nkWhileStmt:
openScope(c.tab)
for i in countup(0, sonsLen(n)-1):
n.sons[i] = semGenericStmt(c, n.sons[i], flags)
n.sons[i] = semGenericStmt(c, n.sons[i], flags, toBind)
closeScope(c.tab)
of nkCaseStmt:
openScope(c.tab)
n.sons[0] = semGenericStmt(c, n.sons[0], flags)
for i in countup(1, sonsLen(n)-1):
var a = n.sons[i]
checkMinSonsLen(a, 1)
var L = sonsLen(a)
for j in countup(0, L-2): a.sons[j] = semGenericStmt(c, a.sons[j], flags)
a.sons[L - 1] = semGenericStmtScope(c, a.sons[L-1], flags)
closeScope(c.tab)
of nkForStmt:
var L = sonsLen(n)
openScope(c.tab)
n.sons[L - 2] = semGenericStmt(c, n.sons[L-2], flags)
for i in countup(0, L - 3): addDecl(c, newSymS(skUnknown, n.sons[i], c))
n.sons[L - 1] = semGenericStmt(c, n.sons[L-1], flags)
closeScope(c.tab)
of nkBlockStmt, nkBlockExpr, nkBlockType:
checkSonsLen(n, 2)
openScope(c.tab)
if n.sons[0].kind != nkEmpty: addDecl(c, newSymS(skUnknown, n.sons[0], c))
n.sons[1] = semGenericStmt(c, n.sons[1], flags)
closeScope(c.tab)
of nkTryStmt:
checkMinSonsLen(n, 2)
n.sons[0] = semGenericStmtScope(c, n.sons[0], flags)
n.sons[0] = semGenericStmt(c, n.sons[0], flags, toBind)
for i in countup(1, sonsLen(n)-1):
var a = n.sons[i]
checkMinSonsLen(a, 1)
var L = sonsLen(a)
for j in countup(0, L-2):
a.sons[j] = semGenericStmt(c, a.sons[j], flags+{withinTypeDesc})
a.sons[L-1] = semGenericStmtScope(c, a.sons[L-1], flags)
a.sons[j] = semGenericStmt(c, a.sons[j], flags, toBind)
a.sons[L - 1] = semGenericStmtScope(c, a.sons[L-1], flags, toBind)
closeScope(c.tab)
of nkForStmt:
var L = sonsLen(n)
openScope(c.tab)
n.sons[L - 2] = semGenericStmt(c, n.sons[L-2], flags, toBind)
for i in countup(0, L - 3): addDecl(c, newSymS(skUnknown, n.sons[i], c))
n.sons[L - 1] = semGenericStmt(c, n.sons[L-1], flags, toBind)
closeScope(c.tab)
of nkBlockStmt, nkBlockExpr, nkBlockType:
checkSonsLen(n, 2)
openScope(c.tab)
if n.sons[0].kind != nkEmpty: addDecl(c, newSymS(skUnknown, n.sons[0], c))
n.sons[1] = semGenericStmt(c, n.sons[1], flags, toBind)
closeScope(c.tab)
of nkTryStmt:
checkMinSonsLen(n, 2)
n.sons[0] = semGenericStmtScope(c, n.sons[0], flags, toBind)
for i in countup(1, sonsLen(n)-1):
var a = n.sons[i]
checkMinSonsLen(a, 1)
var L = sonsLen(a)
for j in countup(0, L-2):
a.sons[j] = semGenericStmt(c, a.sons[j], flags+{withinTypeDesc}, toBind)
a.sons[L-1] = semGenericStmtScope(c, a.sons[L-1], flags, toBind)
of nkVarSection:
for i in countup(0, sonsLen(n) - 1):
var a = n.sons[i]
@ -165,8 +170,9 @@ proc semGenericStmt(c: PContext, n: PNode,
if (a.kind != nkIdentDefs) and (a.kind != nkVarTuple): IllFormedAst(a)
checkMinSonsLen(a, 3)
var L = sonsLen(a)
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc})
a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags)
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc},
toBind)
a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, toBind)
for j in countup(0, L-3):
addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c))
of nkGenericParams:
@ -175,7 +181,8 @@ proc semGenericStmt(c: PContext, n: PNode,
if (a.kind != nkIdentDefs): IllFormedAst(a)
checkMinSonsLen(a, 3)
var L = sonsLen(a)
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc})
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc},
toBind)
# do not perform symbol lookup for default expressions
for j in countup(0, L-3):
addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c))
@ -186,8 +193,8 @@ proc semGenericStmt(c: PContext, n: PNode,
if (a.kind != nkConstDef): IllFormedAst(a)
checkSonsLen(a, 3)
addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[0]), c))
a.sons[1] = semGenericStmt(c, a.sons[1], flags+{withinTypeDesc})
a.sons[2] = semGenericStmt(c, a.sons[2], flags)
a.sons[1] = semGenericStmt(c, a.sons[1], flags+{withinTypeDesc}, toBind)
a.sons[2] = semGenericStmt(c, a.sons[2], flags, toBind)
of nkTypeSection:
for i in countup(0, sonsLen(n) - 1):
var a = n.sons[i]
@ -202,15 +209,15 @@ proc semGenericStmt(c: PContext, n: PNode,
checkSonsLen(a, 3)
if a.sons[1].kind != nkEmpty:
openScope(c.tab)
a.sons[1] = semGenericStmt(c, a.sons[1], flags)
a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc})
a.sons[1] = semGenericStmt(c, a.sons[1], flags, toBind)
a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc}, toBind)
closeScope(c.tab)
else:
a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc})
a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc}, toBind)
of nkEnumTy:
checkMinSonsLen(n, 1)
if n.sons[0].kind != nkEmpty:
n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc})
n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc}, toBind)
for i in countup(1, sonsLen(n) - 1):
var a: PNode
case n.sons[i].kind
@ -223,14 +230,15 @@ proc semGenericStmt(c: PContext, n: PNode,
of nkFormalParams:
checkMinSonsLen(n, 1)
if n.sons[0].kind != nkEmpty:
n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc})
n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc}, toBind)
for i in countup(1, sonsLen(n) - 1):
var a = n.sons[i]
if (a.kind != nkIdentDefs): IllFormedAst(a)
checkMinSonsLen(a, 3)
var L = sonsLen(a)
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc})
a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags)
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc},
toBind)
a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, toBind)
for j in countup(0, L-3):
addDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c))
of nkProcDef, nkMethodDef, nkConverterDef, nkMacroDef, nkTemplateDef,
@ -238,15 +246,16 @@ proc semGenericStmt(c: PContext, n: PNode,
checkSonsLen(n, codePos + 1)
addDecl(c, newSymS(skUnknown, getIdentNode(n.sons[0]), c))
openScope(c.tab)
n.sons[genericParamsPos] = semGenericStmt(c, n.sons[genericParamsPos], flags)
n.sons[genericParamsPos] = semGenericStmt(c, n.sons[genericParamsPos],
flags, toBind)
if n.sons[paramsPos].kind != nkEmpty:
if n.sons[paramsPos].sons[0].kind != nkEmpty:
addDecl(c, newSym(skUnknown, getIdent("result"), nil))
n.sons[paramsPos] = semGenericStmt(c, n.sons[paramsPos], flags)
n.sons[pragmasPos] = semGenericStmt(c, n.sons[pragmasPos], flags)
n.sons[codePos] = semGenericStmtScope(c, n.sons[codePos], flags)
n.sons[paramsPos] = semGenericStmt(c, n.sons[paramsPos], flags, toBind)
n.sons[pragmasPos] = semGenericStmt(c, n.sons[pragmasPos], flags, toBind)
n.sons[codePos] = semGenericStmtScope(c, n.sons[codePos], flags, toBind)
closeScope(c.tab)
else:
for i in countup(0, sonsLen(n) - 1):
result.sons[i] = semGenericStmt(c, n.sons[i], flags)
result.sons[i] = semGenericStmt(c, n.sons[i], flags, toBind)