clean separation of PNimrodNode and tyExpr

This commit is contained in:
Araq 2012-03-16 19:33:24 +01:00
commit be1154106a
7 changed files with 32 additions and 22 deletions

View file

@ -31,8 +31,8 @@ proc semLambda(c: PContext, n: PNode): PNode
proc semTypeNode(c: PContext, n: PNode, prev: PType): PType proc semTypeNode(c: PContext, n: PNode, prev: PType): PType
proc semStmt(c: PContext, n: PNode): PNode proc semStmt(c: PContext, n: PNode): PNode
proc semParamList(c: PContext, n, genericParams: PNode, s: PSym) proc semParamList(c: PContext, n, genericParams: PNode, s: PSym)
proc addParams(c: PContext, n: PNode) proc addParams(c: PContext, n: PNode, kind: TSymKind)
proc addResult(c: PContext, t: PType, info: TLineInfo) proc addResult(c: PContext, t: PType, info: TLineInfo, owner: TSymKind)
proc addResultNode(c: PContext, n: PNode) proc addResultNode(c: PContext, n: PNode)
proc instGenericContainer(c: PContext, n: PNode, header: PType): PType proc instGenericContainer(c: PContext, n: PNode, header: PType): PType

View file

@ -75,7 +75,7 @@ proc instantiateBody(c: PContext, n: PNode, result: PSym) =
addDecl(c, result) addDecl(c, result)
pushProcCon(c, result) pushProcCon(c, result)
if result.kind in {skProc, skMethod, skConverter}: if result.kind in {skProc, skMethod, skConverter}:
addResult(c, result.typ.sons[0], n.info) addResult(c, result.typ.sons[0], n.info, result.kind)
addResultNode(c, n) addResultNode(c, n)
var b = semStmtScope(c, n.sons[bodyPos]) var b = semStmtScope(c, n.sons[bodyPos])
# XXX Bad hack for tests/titer2 and tests/tactiontable # XXX Bad hack for tests/titer2 and tests/tactiontable
@ -92,7 +92,8 @@ proc fixupInstantiatedSymbols(c: PContext, s: PSym) =
openScope(c.tab) openScope(c.tab)
var n = oldPrc.ast var n = oldPrc.ast
n.sons[bodyPos] = copyTree(s.getBody) n.sons[bodyPos] = copyTree(s.getBody)
if n.sons[paramsPos].kind != nkEmpty: addParams(c, oldPrc.typ.n) if n.sons[paramsPos].kind != nkEmpty:
addParams(c, oldPrc.typ.n, oldPrc.kind)
instantiateBody(c, n, oldPrc) instantiateBody(c, n, oldPrc)
closeScope(c.tab) closeScope(c.tab)
popInfoContext() popInfoContext()

View file

@ -599,12 +599,12 @@ proc SemTypeSection(c: PContext, n: PNode): PNode =
result = n result = n
proc semParamList(c: PContext, n, genericParams: PNode, s: PSym) = proc semParamList(c: PContext, n, genericParams: PNode, s: PSym) =
s.typ = semProcTypeNode(c, n, genericParams, nil) s.typ = semProcTypeNode(c, n, genericParams, nil, s.kind)
proc addParams(c: PContext, n: PNode) = proc addParams(c: PContext, n: PNode, kind: TSymKind) =
for i in countup(1, sonsLen(n)-1): for i in countup(1, sonsLen(n)-1):
if (n.sons[i].kind != nkSym): InternalError(n.info, "addParams") if (n.sons[i].kind != nkSym): InternalError(n.info, "addParams")
addDecl(c, n.sons[i].sym) addParamOrResult(c, n.sons[i].sym, kind)
proc semBorrow(c: PContext, n: PNode, s: PSym) = proc semBorrow(c: PContext, n: PNode, s: PSym) =
# search for the correct alias: # search for the correct alias:
@ -615,13 +615,13 @@ proc semBorrow(c: PContext, n: PNode, s: PSym) =
else: else:
LocalError(n.info, errNoSymbolToBorrowFromFound) LocalError(n.info, errNoSymbolToBorrowFromFound)
proc addResult(c: PContext, t: PType, info: TLineInfo) = proc addResult(c: PContext, t: PType, info: TLineInfo, owner: TSymKind) =
if t != nil: if t != nil:
var s = newSym(skResult, getIdent"result", getCurrOwner()) var s = newSym(skResult, getIdent"result", getCurrOwner())
s.info = info s.info = info
s.typ = t s.typ = t
incl(s.flags, sfUsed) incl(s.flags, sfUsed)
addDecl(c, s) addParamOrResult(c, s, owner)
c.p.resultSym = s c.p.resultSym = s
proc addResultNode(c: PContext, n: PNode) = proc addResultNode(c: PContext, n: PNode) =
@ -653,7 +653,7 @@ proc semLambda(c: PContext, n: PNode): PNode =
if sfImportc in s.flags: if sfImportc in s.flags:
LocalError(n.sons[bodyPos].info, errImplOfXNotAllowed, s.name.s) LocalError(n.sons[bodyPos].info, errImplOfXNotAllowed, s.name.s)
pushProcCon(c, s) pushProcCon(c, s)
addResult(c, s.typ.sons[0], n.info) addResult(c, s.typ.sons[0], n.info, skProc)
n.sons[bodyPos] = semStmtScope(c, n.sons[bodyPos]) n.sons[bodyPos] = semStmtScope(c, n.sons[bodyPos])
addResultNode(c, n) addResultNode(c, n)
popProcCon(c) popProcCon(c)
@ -717,7 +717,7 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
openScope(c.tab) # open scope for old (correct) parameter symbols openScope(c.tab) # open scope for old (correct) parameter symbols
if proto.ast.sons[genericParamsPos].kind != nkEmpty: if proto.ast.sons[genericParamsPos].kind != nkEmpty:
addGenericParamListToScope(c, proto.ast.sons[genericParamsPos]) addGenericParamListToScope(c, proto.ast.sons[genericParamsPos])
addParams(c, proto.typ.n) addParams(c, proto.typ.n, proto.kind)
proto.info = s.info # more accurate line information proto.info = s.info # more accurate line information
s.typ = proto.typ s.typ = proto.typ
s = proto s = proto
@ -737,7 +737,7 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
ParamsTypeCheck(c, s.typ) ParamsTypeCheck(c, s.typ)
pushProcCon(c, s) pushProcCon(c, s)
if s.typ.sons[0] != nil and kind != skIterator: if s.typ.sons[0] != nil and kind != skIterator:
addResult(c, s.typ.sons[0], n.info) addResult(c, s.typ.sons[0], n.info, kind)
if sfImportc notin s.flags: if sfImportc notin s.flags:
# no semantic checking for importc: # no semantic checking for importc:
n.sons[bodyPos] = semStmtScope(c, n.sons[bodyPos]) n.sons[bodyPos] = semStmtScope(c, n.sons[bodyPos])

View file

@ -510,8 +510,17 @@ proc paramType(c: PContext, n, genericParams: PNode, cl: var TIntSet): PType =
if genericParams != nil and sonsLen(genericParams) == 0: if genericParams != nil and sonsLen(genericParams) == 0:
result = addTypeVarsOfGenericBody(c, result, genericParams, cl) result = addTypeVarsOfGenericBody(c, result, genericParams, cl)
proc addParamOrResult(c: PContext, param: PSym, kind: TSymKind) =
if kind == skMacro and param.typ.kind in {tyTypeDesc, tyExpr, tyStmt}:
let nn = getSysSym"PNimrodNode"
var a = copySym(param)
a.typ = nn.typ
addDecl(c, a)
else:
addDecl(c, param)
proc semProcTypeNode(c: PContext, n, genericParams: PNode, proc semProcTypeNode(c: PContext, n, genericParams: PNode,
prev: PType): PType = prev: PType, kind: TSymKind): PType =
var var
def, res: PNode def, res: PNode
typ: PType typ: PType
@ -568,7 +577,7 @@ 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) addParamOrResult(c, arg, kind)
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)
@ -717,7 +726,7 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
of nkProcTy: of nkProcTy:
checkSonsLen(n, 2) checkSonsLen(n, 2)
openScope(c.tab) openScope(c.tab)
result = semProcTypeNode(c, n.sons[0], nil, prev) result = semProcTypeNode(c, n.sons[0], nil, prev, skProc)
# 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
@ -769,6 +778,7 @@ proc processMagicType(c: PContext, m: PSym) =
of mSet: setMagicType(m, tySet, 0) of mSet: setMagicType(m, tySet, 0)
of mSeq: setMagicType(m, tySequence, 0) of mSeq: setMagicType(m, tySequence, 0)
of mOrdinal: setMagicType(m, tyOrdinal, 0) of mOrdinal: setMagicType(m, tyOrdinal, 0)
of mPNimrodNode: nil
else: GlobalError(m.info, errTypeExpected) else: GlobalError(m.info, errTypeExpected)
proc newConstraint(c: PContext, k: TTypeKind): PType = proc newConstraint(c: PContext, k: TTypeKind): PType =

View file

@ -87,10 +87,6 @@ type
## represents a Nimrod *symbol* in the compiler; a *symbol* is a looked-up ## represents a Nimrod *symbol* in the compiler; a *symbol* is a looked-up
## *ident*. ## *ident*.
TNimrodNode {.final.} = object
PNimrodNode* {.magic: "PNimrodNode".} = ref TNimrodNode
## represents a Nimrod AST node. Macros operate on this type.
const const
nnkLiterals* = {nnkCharLit..nnkNilLit} nnkLiterals* = {nnkCharLit..nnkNilLit}
nnkCallKinds* = {nnkCall, nnkInfix, nnkPrefix, nnkPostfix, nnkCommand, nnkCallKinds* = {nnkCall, nnkInfix, nnkPrefix, nnkPostfix, nnkCommand,

View file

@ -2220,6 +2220,11 @@ proc shallow*(s: var string) {.noSideEffect, inline.} =
# const res = e # const res = e
# res # res
type
TNimrodNode {.final.} = object
PNimrodNode* {.magic: "PNimrodNode".} = ref TNimrodNode
## represents a Nimrod AST node. Macros operate on this type.
template eval*(blk: stmt): stmt = template eval*(blk: stmt): stmt =
## executes a block of code at compile time just as if it was a macro ## executes a block of code at compile time just as if it was a macro
## optonally, the block can return an AST tree that will replace the ## optonally, the block can return an AST tree that will replace the

View file

@ -1,8 +1,6 @@
version 0.9.0 version 0.9.0
============= =============
- clean separation between tyExpr and PNimrodNode
- ``=`` should be overloadable; requires specialization for ``=`` - ``=`` should be overloadable; requires specialization for ``=``
- fix remaining generics bugs - fix remaining generics bugs
- fix remaining closure bugs: - fix remaining closure bugs: