get rid of TOverloadIter.stackPtr

This commit is contained in:
Zahary Karadjov 2013-05-12 02:01:16 +03:00
commit af081f995e
3 changed files with 39 additions and 28 deletions

View file

@ -45,11 +45,15 @@ proc addUniqueSym*(scope: PScope, s: PSym): TResult =
result = Success result = Success
proc openScope*(c: PContext): PScope {.discardable.} = proc openScope*(c: PContext): PScope {.discardable.} =
c.currentScope = PScope(parent: c.currentScope, symbols: newStrTable()) inc c.scopeDepth
result = c.currentScope result = PScope(parent: c.currentScope,
symbols: newStrTable(),
depthLevel: c.scopeDepth)
c.currentScope = result
proc rawCloseScope*(c: PContext) = proc rawCloseScope*(c: PContext) =
c.currentScope = c.currentScope.parent c.currentScope = c.currentScope.parent
dec c.scopeDepth
proc closeScope*(c: PContext) = proc closeScope*(c: PContext) =
ensureNoMissingOrUnusedSymbols(c.currentScope) ensureNoMissingOrUnusedSymbols(c.currentScope)
@ -96,10 +100,11 @@ type
oimDone, oimNoQualifier, oimSelfModule, oimOtherModule, oimSymChoice, oimDone, oimNoQualifier, oimSelfModule, oimOtherModule, oimSymChoice,
oimSymChoiceLocalLookup oimSymChoiceLocalLookup
TOverloadIter*{.final.} = object TOverloadIter*{.final.} = object
stackPtr*: int
it*: TIdentIter it*: TIdentIter
m*: PSym m*: PSym
mode*: TOverloadIterMode mode*: TOverloadIterMode
symChoiceIndex*: int
scope*: PScope
inSymChoice: TIntSet inSymChoice: TIntSet
proc getSymRepr*(s: PSym): string = proc getSymRepr*(s: PSym): string =
@ -241,12 +246,15 @@ proc InitOverloadIter*(o: var TOverloadIter, c: PContext, n: PNode): PSym =
case n.kind case n.kind
of nkIdent, nkAccQuoted: of nkIdent, nkAccQuoted:
var ident = considerAcc(n) var ident = considerAcc(n)
o.stackPtr = c.tab.tos o.scope = c.currentScope
o.mode = oimNoQualifier o.mode = oimNoQualifier
while result == nil: while true:
dec(o.stackPtr) result = InitIdentIter(o.it, o.scope.symbols, ident)
if o.stackPtr < 0: break if result != nil:
result = InitIdentIter(o.it, c.tab.stack[o.stackPtr], ident) break
else:
o.scope = o.scope.parent
if o.scope == nil: break
of nkSym: of nkSym:
result = n.sym result = n.sym
o.mode = oimDone o.mode = oimDone
@ -273,7 +281,7 @@ proc InitOverloadIter*(o: var TOverloadIter, c: PContext, n: PNode): PSym =
of nkClosedSymChoice, nkOpenSymChoice: of nkClosedSymChoice, nkOpenSymChoice:
o.mode = oimSymChoice o.mode = oimSymChoice
result = n.sons[0].sym result = n.sons[0].sym
o.stackPtr = 1 o.symChoiceIndex = 1
o.inSymChoice = initIntSet() o.inSymChoice = initIntSet()
Incl(o.inSymChoice, result.id) Incl(o.inSymChoice, result.id)
else: nil else: nil
@ -281,7 +289,7 @@ proc InitOverloadIter*(o: var TOverloadIter, c: PContext, n: PNode): PSym =
proc lastOverloadScope*(o: TOverloadIter): int = proc lastOverloadScope*(o: TOverloadIter): int =
case o.mode case o.mode
of oimNoQualifier: result = o.stackPtr of oimNoQualifier: result = o.scope.depthLevel
of oimSelfModule: result = 1 of oimSelfModule: result = 1
of oimOtherModule: result = 0 of oimOtherModule: result = 0
else: result = -1 else: result = -1
@ -291,12 +299,12 @@ proc nextOverloadIter*(o: var TOverloadIter, c: PContext, n: PNode): PSym =
of oimDone: of oimDone:
result = nil result = nil
of oimNoQualifier: of oimNoQualifier:
if o.stackPtr >= 0: if o.scope != nil:
result = nextIdentIter(o.it, c.tab.stack[o.stackPtr]) result = nextIdentIter(o.it, o.scope.symbols)
while result == nil: while result == nil:
dec(o.stackPtr) o.scope = o.scope.parent
if o.stackPtr < 0: break if o.scope == nil: break
result = InitIdentIter(o.it, c.tab.stack[o.stackPtr], o.it.name) result = InitIdentIter(o.it, o.scope.symbols, o.it.name)
# BUGFIX: o.it.name <-> n.ident # BUGFIX: o.it.name <-> n.ident
else: else:
result = nil result = nil
@ -305,27 +313,27 @@ proc nextOverloadIter*(o: var TOverloadIter, c: PContext, n: PNode): PSym =
of oimOtherModule: of oimOtherModule:
result = nextIdentIter(o.it, o.m.tab) result = nextIdentIter(o.it, o.m.tab)
of oimSymChoice: of oimSymChoice:
if o.stackPtr < sonsLen(n): if o.symChoiceIndex < sonsLen(n):
result = n.sons[o.stackPtr].sym result = n.sons[o.symChoiceIndex].sym
Incl(o.inSymChoice, result.id) Incl(o.inSymChoice, result.id)
inc(o.stackPtr) inc o.symChoiceIndex
elif n.kind == nkOpenSymChoice: elif n.kind == nkOpenSymChoice:
# try 'local' symbols too for Koenig's lookup: # try 'local' symbols too for Koenig's lookup:
o.mode = oimSymChoiceLocalLookup o.mode = oimSymChoiceLocalLookup
o.stackPtr = c.tab.tos-1 o.scope = c.currentScope
result = FirstIdentExcluding(o.it, c.tab.stack[o.stackPtr], result = FirstIdentExcluding(o.it, o.scope.symbols,
n.sons[0].sym.name, o.inSymChoice) n.sons[0].sym.name, o.inSymChoice)
while result == nil: while result == nil:
dec(o.stackPtr) o.scope = o.scope.parent
if o.stackPtr < 0: break if o.scope == nil: break
result = FirstIdentExcluding(o.it, c.tab.stack[o.stackPtr], result = FirstIdentExcluding(o.it, o.scope.symbols,
n.sons[0].sym.name, o.inSymChoice) n.sons[0].sym.name, o.inSymChoice)
of oimSymChoiceLocalLookup: of oimSymChoiceLocalLookup:
result = nextIdentExcluding(o.it, c.tab.stack[o.stackPtr], o.inSymChoice) result = nextIdentExcluding(o.it, o.scope.symbols, o.inSymChoice)
while result == nil: while result == nil:
dec(o.stackPtr) o.scope = o.scope.parent
if o.stackPtr < 0: break if o.scope == nil: break
result = FirstIdentExcluding(o.it, c.tab.stack[o.stackPtr], result = FirstIdentExcluding(o.it, o.scope.symbols,
n.sons[0].sym.name, o.inSymChoice) n.sons[0].sym.name, o.inSymChoice)
if result != nil and result.kind == skStub: loadStub(result) if result != nil and result.kind == skStub: loadStub(result)

View file

@ -282,6 +282,7 @@ proc RecoverContext(c: PContext) =
# faster than wrapping every stack operation in a 'try finally' block and # faster than wrapping every stack operation in a 'try finally' block and
# requires far less code. # requires far less code.
c.currentScope = c.topLevelScope c.currentScope = c.topLevelScope
c.scopeDepth = 2 # importTable and top-level scope
while getCurrOwner().kind != skModule: popOwner() while getCurrOwner().kind != skModule: popOwner()
while c.p != nil and c.p.owner.kind != skModule: c.p = c.p.next while c.p != nil and c.p.owner.kind != skModule: c.p = c.p.next

View file

@ -46,6 +46,7 @@ type
TExprFlags* = set[TExprFlag] TExprFlags* = set[TExprFlag]
TScope* = object TScope* = object
depthLevel*: int
symbols*: TStrTable symbols*: TStrTable
parent*: PScope parent*: PScope
@ -57,6 +58,7 @@ type
currentScope*: PScope # current scope currentScope*: PScope # current scope
importTable*: PScope # scope for all imported symbols importTable*: PScope # scope for all imported symbols
topLevelScope*: PScope # scope for all top-level symbols topLevelScope*: PScope # scope for all top-level symbols
scopeDepth*: int # number of open scopes
p*: PProcCon # procedure context p*: PProcCon # procedure context
friendModule*: PSym # current friend module; may access private data; friendModule*: PSym # current friend module; may access private data;
# this is used so that generic instantiations # this is used so that generic instantiations