allow 'mixin' in generics

This commit is contained in:
Araq 2012-10-12 17:13:58 +02:00
commit 9bbf56bdbb
6 changed files with 84 additions and 80 deletions

View file

@ -58,6 +58,7 @@ proc InitDefines*() =
# for bootstrapping purposes and old code: # for bootstrapping purposes and old code:
DefineSymbol("nimhygiene") DefineSymbol("nimhygiene")
DefineSymbol("niminheritable") DefineSymbol("niminheritable")
DefineSymbol("nimmixin")
# add platform specific symbols: # add platform specific symbols:
case targetCPU case targetCPU

View file

@ -32,12 +32,12 @@ proc getIdentNode(n: PNode): PNode =
result = n result = n
proc semGenericStmt(c: PContext, n: PNode, flags: TSemGenericFlags, proc semGenericStmt(c: PContext, n: PNode, flags: TSemGenericFlags,
toBind: var TIntSet): PNode ctx: var TIntSet): PNode
proc semGenericStmtScope(c: PContext, n: PNode, proc semGenericStmtScope(c: PContext, n: PNode,
flags: TSemGenericFlags, flags: TSemGenericFlags,
toBind: var TIntSet): PNode = ctx: var TIntSet): PNode =
openScope(c.tab) openScope(c.tab)
result = semGenericStmt(c, n, flags, toBind) result = semGenericStmt(c, n, flags, ctx)
closeScope(c.tab) closeScope(c.tab)
template macroToExpand(s: expr): expr = template macroToExpand(s: expr): expr =
@ -73,20 +73,35 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym): PNode =
result = n result = n
else: result = newSymNode(s, n.info) else: result = newSymNode(s, n.info)
proc semMixinStmt(c: PContext, n: PNode, toMixin: var TIntSet): PNode =
for i in 0 .. < n.len:
toMixin.incl(considerAcc(n.sons[i]).id)
result = newNodeI(nkNilLit, n.info)
proc Lookup(c: PContext, n: PNode, flags: TSemGenericFlags,
ctx: var TIntSet): PNode =
result = n
let ident = considerAcc(n)
var s = SymtabGet(c.Tab, ident)
if s == nil:
if ident.id notin ctx and withinMixin notin flags:
localError(n.info, errUndeclaredIdentifier, ident.s)
else:
if withinMixin in flags:
result = symChoice(c, n, s, scClosed)
elif s.name.id in ctx:
result = symChoice(c, n, s, scForceOpen)
else:
result = semGenericStmtSymbol(c, n, s)
# else: leave as nkIdent
proc semGenericStmt(c: PContext, n: PNode, proc semGenericStmt(c: PContext, n: PNode,
flags: TSemGenericFlags, toBind: var TIntSet): PNode = flags: TSemGenericFlags, ctx: var TIntSet): PNode =
result = n result = n
if gCmd == cmdIdeTools: suggestStmt(c, n) if gCmd == cmdIdeTools: suggestStmt(c, n)
case n.kind case n.kind
of nkIdent, nkAccQuoted: of nkIdent, nkAccQuoted:
var s = SymtabGet(c.Tab, n.ident) result = Lookup(c, n, flags, ctx)
if s == nil:
if withinMixin notin flags:
localError(n.info, errUndeclaredIdentifier, n.ident.s)
else:
if withinBind in flags or s.id in toBind:
result = symChoice(c, n, s, scClosed)
else: result = semGenericStmtSymbol(c, n, s)
of nkDotExpr: of nkDotExpr:
let luf = if withinMixin notin flags: {checkUndeclared} else: {} let luf = if withinMixin notin flags: {checkUndeclared} else: {}
var s = QualifiedLookUp(c, n, luf) var s = QualifiedLookUp(c, n, luf)
@ -102,14 +117,18 @@ proc semGenericStmt(c: PContext, n: PNode,
# in the generic instantiation process... # in the generic instantiation process...
nil nil
of nkBind: of nkBind:
result = semGenericStmt(c, n.sons[0], flags+{withinBind}, toBind) result = semGenericStmt(c, n.sons[0], flags+{withinBind}, ctx)
of nkBindStmt: of nkMixinStmt:
result = semBindStmt(c, n, toBind) result = semMixinStmt(c, n, ctx)
of nkCall, nkHiddenCallConv, nkInfix, nkPrefix, nkCommand, nkCallStrLit: of nkCall, nkHiddenCallConv, nkInfix, nkPrefix, nkCommand, nkCallStrLit:
# check if it is an expression macro: # check if it is an expression macro:
checkMinSonsLen(n, 1) checkMinSonsLen(n, 1)
let luf = if withinMixin notin flags: {checkUndeclared} else: {} let fn = n.sons[0]
var s = qualifiedLookup(c, n.sons[0], luf) var s = qualifiedLookup(c, fn, {})
if s == nil and withinMixin notin flags and
fn.kind in {nkIdent, nkAccQuoted} and considerAcc(fn).id notin ctx:
localError(n.info, errUndeclaredIdentifier, fn.renderTree)
var first = 0 var first = 0
var isDefinedMagic = false var isDefinedMagic = false
if s != nil: if s != nil:
@ -152,54 +171,54 @@ proc semGenericStmt(c: PContext, n: PNode,
# is not exported and yet the generic 'threadProcWrapper' works correctly. # is not exported and yet the generic 'threadProcWrapper' works correctly.
let flags = if isDefinedMagic: flags+{withinMixin} else: flags let flags = if isDefinedMagic: flags+{withinMixin} else: flags
for i in countup(first, sonsLen(result) - 1): for i in countup(first, sonsLen(result) - 1):
result.sons[i] = semGenericStmt(c, result.sons[i], flags, toBind) result.sons[i] = semGenericStmt(c, result.sons[i], flags, ctx)
of nkIfStmt: of nkIfStmt:
for i in countup(0, sonsLen(n)-1): for i in countup(0, sonsLen(n)-1):
n.sons[i] = semGenericStmtScope(c, n.sons[i], flags, toBind) n.sons[i] = semGenericStmtScope(c, n.sons[i], flags, ctx)
of nkWhenStmt: of nkWhenStmt:
for i in countup(0, sonsLen(n)-1): for i in countup(0, sonsLen(n)-1):
n.sons[i] = semGenericStmt(c, n.sons[i], flags+{withinMixin}, toBind) n.sons[i] = semGenericStmt(c, n.sons[i], flags+{withinMixin}, ctx)
of nkWhileStmt: of nkWhileStmt:
openScope(c.tab) openScope(c.tab)
for i in countup(0, sonsLen(n)-1): for i in countup(0, sonsLen(n)-1):
n.sons[i] = semGenericStmt(c, n.sons[i], flags, toBind) n.sons[i] = semGenericStmt(c, n.sons[i], flags, ctx)
closeScope(c.tab) closeScope(c.tab)
of nkCaseStmt: of nkCaseStmt:
openScope(c.tab) openScope(c.tab)
n.sons[0] = semGenericStmt(c, n.sons[0], flags, toBind) n.sons[0] = semGenericStmt(c, n.sons[0], flags, ctx)
for i in countup(1, sonsLen(n)-1): for i in countup(1, sonsLen(n)-1):
var a = n.sons[i] var a = n.sons[i]
checkMinSonsLen(a, 1) checkMinSonsLen(a, 1)
var L = sonsLen(a) var L = sonsLen(a)
for j in countup(0, L-2): for j in countup(0, L-2):
a.sons[j] = semGenericStmt(c, a.sons[j], flags, toBind) a.sons[j] = semGenericStmt(c, a.sons[j], flags, ctx)
a.sons[L - 1] = semGenericStmtScope(c, a.sons[L-1], flags, toBind) a.sons[L - 1] = semGenericStmtScope(c, a.sons[L-1], flags, ctx)
closeScope(c.tab) closeScope(c.tab)
of nkForStmt, nkParForStmt: of nkForStmt, nkParForStmt:
var L = sonsLen(n) var L = sonsLen(n)
openScope(c.tab) openScope(c.tab)
n.sons[L - 2] = semGenericStmt(c, n.sons[L-2], flags, toBind) n.sons[L - 2] = semGenericStmt(c, n.sons[L-2], flags, ctx)
for i in countup(0, L - 3): for i in countup(0, L - 3):
addPrelimDecl(c, newSymS(skUnknown, n.sons[i], c)) addPrelimDecl(c, newSymS(skUnknown, n.sons[i], c))
n.sons[L - 1] = semGenericStmt(c, n.sons[L-1], flags, toBind) n.sons[L - 1] = semGenericStmt(c, n.sons[L-1], flags, ctx)
closeScope(c.tab) closeScope(c.tab)
of nkBlockStmt, nkBlockExpr, nkBlockType: of nkBlockStmt, nkBlockExpr, nkBlockType:
checkSonsLen(n, 2) checkSonsLen(n, 2)
openScope(c.tab) openScope(c.tab)
if n.sons[0].kind != nkEmpty: if n.sons[0].kind != nkEmpty:
addPrelimDecl(c, newSymS(skUnknown, n.sons[0], c)) addPrelimDecl(c, newSymS(skUnknown, n.sons[0], c))
n.sons[1] = semGenericStmt(c, n.sons[1], flags, toBind) n.sons[1] = semGenericStmt(c, n.sons[1], flags, ctx)
closeScope(c.tab) closeScope(c.tab)
of nkTryStmt: of nkTryStmt:
checkMinSonsLen(n, 2) checkMinSonsLen(n, 2)
n.sons[0] = semGenericStmtScope(c, n.sons[0], flags, toBind) n.sons[0] = semGenericStmtScope(c, n.sons[0], flags, ctx)
for i in countup(1, sonsLen(n)-1): for i in countup(1, sonsLen(n)-1):
var a = n.sons[i] var a = n.sons[i]
checkMinSonsLen(a, 1) checkMinSonsLen(a, 1)
var L = sonsLen(a) var L = sonsLen(a)
for j in countup(0, L-2): for j in countup(0, L-2):
a.sons[j] = semGenericStmt(c, a.sons[j], flags+{withinTypeDesc}, toBind) a.sons[j] = semGenericStmt(c, a.sons[j], flags+{withinTypeDesc}, ctx)
a.sons[L-1] = semGenericStmtScope(c, a.sons[L-1], flags, toBind) a.sons[L-1] = semGenericStmtScope(c, a.sons[L-1], flags, ctx)
of nkVarSection, nkLetSection: of nkVarSection, nkLetSection:
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):
var a = n.sons[i] var a = n.sons[i]
@ -208,8 +227,8 @@ proc semGenericStmt(c: PContext, n: PNode,
checkMinSonsLen(a, 3) checkMinSonsLen(a, 3)
var L = sonsLen(a) 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) ctx)
a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, toBind) a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, ctx)
for j in countup(0, L-3): for j in countup(0, L-3):
addPrelimDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c)) addPrelimDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c))
of nkGenericParams: of nkGenericParams:
@ -219,7 +238,7 @@ proc semGenericStmt(c: PContext, n: PNode,
checkMinSonsLen(a, 3) checkMinSonsLen(a, 3)
var L = sonsLen(a) 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) ctx)
# do not perform symbol lookup for default expressions # do not perform symbol lookup for default expressions
for j in countup(0, L-3): for j in countup(0, L-3):
addPrelimDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c)) addPrelimDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c))
@ -230,8 +249,8 @@ proc semGenericStmt(c: PContext, n: PNode,
if (a.kind != nkConstDef): IllFormedAst(a) if (a.kind != nkConstDef): IllFormedAst(a)
checkSonsLen(a, 3) checkSonsLen(a, 3)
addPrelimDecl(c, newSymS(skUnknown, getIdentNode(a.sons[0]), c)) addPrelimDecl(c, newSymS(skUnknown, getIdentNode(a.sons[0]), c))
a.sons[1] = semGenericStmt(c, a.sons[1], flags+{withinTypeDesc}, toBind) a.sons[1] = semGenericStmt(c, a.sons[1], flags+{withinTypeDesc}, ctx)
a.sons[2] = semGenericStmt(c, a.sons[2], flags, toBind) a.sons[2] = semGenericStmt(c, a.sons[2], flags, ctx)
of nkTypeSection: of nkTypeSection:
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):
var a = n.sons[i] var a = n.sons[i]
@ -246,15 +265,15 @@ proc semGenericStmt(c: PContext, n: PNode,
checkSonsLen(a, 3) checkSonsLen(a, 3)
if a.sons[1].kind != nkEmpty: if a.sons[1].kind != nkEmpty:
openScope(c.tab) openScope(c.tab)
a.sons[1] = semGenericStmt(c, a.sons[1], flags, toBind) a.sons[1] = semGenericStmt(c, a.sons[1], flags, ctx)
a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc}, toBind) a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc}, ctx)
closeScope(c.tab) closeScope(c.tab)
else: else:
a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc}, toBind) a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc}, ctx)
of nkEnumTy: of nkEnumTy:
if n.sonsLen > 0: if n.sonsLen > 0:
if n.sons[0].kind != nkEmpty: if n.sons[0].kind != nkEmpty:
n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc}, toBind) n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc}, ctx)
for i in countup(1, sonsLen(n) - 1): for i in countup(1, sonsLen(n) - 1):
var a: PNode var a: PNode
case n.sons[i].kind case n.sons[i].kind
@ -267,15 +286,15 @@ proc semGenericStmt(c: PContext, n: PNode,
of nkFormalParams: of nkFormalParams:
checkMinSonsLen(n, 1) checkMinSonsLen(n, 1)
if n.sons[0].kind != nkEmpty: if n.sons[0].kind != nkEmpty:
n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc}, toBind) n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc}, ctx)
for i in countup(1, sonsLen(n) - 1): for i in countup(1, sonsLen(n) - 1):
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 L = sonsLen(a) 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) ctx)
a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, toBind) a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, ctx)
for j in countup(0, L-3): for j in countup(0, L-3):
addPrelimDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c)) addPrelimDecl(c, newSymS(skUnknown, getIdentNode(a.sons[j]), c))
of nkProcDef, nkMethodDef, nkConverterDef, nkMacroDef, nkTemplateDef, of nkProcDef, nkMethodDef, nkConverterDef, nkMacroDef, nkTemplateDef,
@ -285,22 +304,22 @@ proc semGenericStmt(c: PContext, n: PNode,
addPrelimDecl(c, newSymS(skUnknown, getIdentNode(n.sons[0]), c)) addPrelimDecl(c, newSymS(skUnknown, getIdentNode(n.sons[0]), c))
openScope(c.tab) openScope(c.tab)
n.sons[genericParamsPos] = semGenericStmt(c, n.sons[genericParamsPos], n.sons[genericParamsPos] = semGenericStmt(c, n.sons[genericParamsPos],
flags, toBind) flags, ctx)
if n.sons[paramsPos].kind != nkEmpty: if n.sons[paramsPos].kind != nkEmpty:
if n.sons[paramsPos].sons[0].kind != nkEmpty: if n.sons[paramsPos].sons[0].kind != nkEmpty:
addPrelimDecl(c, newSym(skUnknown, getIdent("result"), nil, n.info)) addPrelimDecl(c, newSym(skUnknown, getIdent("result"), nil, n.info))
n.sons[paramsPos] = semGenericStmt(c, n.sons[paramsPos], flags, toBind) n.sons[paramsPos] = semGenericStmt(c, n.sons[paramsPos], flags, ctx)
n.sons[pragmasPos] = semGenericStmt(c, n.sons[pragmasPos], flags, toBind) n.sons[pragmasPos] = semGenericStmt(c, n.sons[pragmasPos], flags, ctx)
var body: PNode var body: PNode
if n.sons[namePos].kind == nkSym: body = n.sons[namePos].sym.getBody if n.sons[namePos].kind == nkSym: body = n.sons[namePos].sym.getBody
else: body = n.sons[bodyPos] else: body = n.sons[bodyPos]
n.sons[bodyPos] = semGenericStmtScope(c, body, flags, toBind) n.sons[bodyPos] = semGenericStmtScope(c, body, flags, ctx)
closeScope(c.tab) closeScope(c.tab)
of nkPragma, nkPragmaExpr: nil of nkPragma, nkPragmaExpr: nil
of nkExprColonExpr: of nkExprColonExpr:
checkMinSonsLen(n, 2) checkMinSonsLen(n, 2)
result.sons[1] = semGenericStmt(c, n.sons[1], flags, toBind) result.sons[1] = semGenericStmt(c, n.sons[1], flags, ctx)
else: else:
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):
result.sons[i] = semGenericStmt(c, n.sons[i], flags, toBind) result.sons[i] = semGenericStmt(c, n.sons[i], flags, ctx)

View file

@ -91,29 +91,6 @@ proc semBindStmt(c: PContext, n: PNode, toBind: var TIntSet): PNode =
illFormedAst(a) illFormedAst(a)
result = newNodeI(nkNilLit, n.info) result = newNodeI(nkNilLit, n.info)
when false:
# not active before 0.9.0 is out
proc semMixinStmt(c: PContext, n: PNode, toMixin: var TIntSet): PNode =
for i in 0 .. < n.len:
var a = n.sons[i]
# If 'a' is an overloaded symbol, we used to use the first symbol
# as a 'witness' and use the fact that subsequent lookups will yield
# the same symbol!
# This is however not true anymore for hygienic templates as semantic
# processing for them changes the symbol table...
let s = QualifiedLookUp(c, a)
if s != nil:
# we need to mark all symbols:
let sc = symChoice(c, n, s, scForceOpen)
if sc.kind == nkSym:
toMixin.incl(sc.sym.id)
else:
for x in items(sc): toMixin.incl(x.sym.id)
else:
# do nothing: identifiers are already not bound:
nil
result = newNodeI(nkNilLit, n.info)
proc replaceIdentBySym(n: var PNode, s: PNode) = proc replaceIdentBySym(n: var PNode, s: PNode) =
case n.kind case n.kind
of nkPostfix: replaceIdentBySym(n.sons[1], s) of nkPostfix: replaceIdentBySym(n.sons[1], s)

View file

@ -240,7 +240,7 @@ elif false: # asmVersion and (defined(gcc) or defined(llvm_gcc)):
:"%edx" :"%edx"
""" """
# Platform independant versions of the above (slower!) # Platform independent versions of the above (slower!)
when not defined(addInt): when not defined(addInt):
proc addInt(a, b: int): int {.compilerProc, inline.} = proc addInt(a, b: int): int {.compilerProc, inline.} =
result = a +% b result = a +% b

View file

@ -144,11 +144,17 @@ proc objectInit(dest: Pointer, typ: PNimType) =
# ---------------------- assign zero ----------------------------------------- # ---------------------- assign zero -----------------------------------------
# dummy declaration; XXX we need 'mixin' here when not defined(nimmixin):
proc destroy(x: int) = nil proc destroy(x: int) = nil
proc nimDestroyRange*[T](r: T) = proc nimDestroyRange*[T](r: T) =
# internal proc used for destroying sequences and arrays # internal proc used for destroying sequences and arrays
for i in countup(0, r.len - 1): destroy(r[i]) for i in countup(0, r.len - 1): destroy(r[i])
else:
# XXX Why is this exported and no compilerproc?
proc nimDestroyRange*[T](r: T) =
# internal proc used for destroying sequences and arrays
mixin destroy
for i in countup(0, r.len - 1): destroy(r[i])
proc genericReset(dest: Pointer, mt: PNimType) {.compilerProc.} proc genericReset(dest: Pointer, mt: PNimType) {.compilerProc.}
proc genericResetAux(dest: Pointer, n: ptr TNimNode) = proc genericResetAux(dest: Pointer, n: ptr TNimNode) =

View file

@ -1,6 +1,8 @@
version 0.9.2 version 0.9.2
============= =============
- document 'mixin' for generics and symbol lookup rules; special rule
for ``[]=``
- implement the compiler as a service - implement the compiler as a service
- implement for loop transformation for first class iterators - implement for loop transformation for first class iterators
- ``=`` should be overloadable; requires specialization for ``=`` - ``=`` should be overloadable; requires specialization for ``=``
@ -65,7 +67,6 @@ version 0.9.XX
- proc specialization in the code gen for write barrier specialization - proc specialization in the code gen for write barrier specialization
- tlastmod returns wrong results on BSD (Linux, MacOS X: works) - tlastmod returns wrong results on BSD (Linux, MacOS X: works)
- nested tuple unpacking; tuple unpacking in non-var-context - nested tuple unpacking; tuple unpacking in non-var-context
- test branch coverage
- make pegs support a compile-time option and make c2nim use regexes instead - make pegs support a compile-time option and make c2nim use regexes instead
per default? per default?
- 'const' objects including case objects - 'const' objects including case objects