Skip to content

Commit

Permalink
revert to ref, test shallow copying instead
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Oct 2, 2024
1 parent b833c46 commit 257fa42
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 25 deletions.
29 changes: 10 additions & 19 deletions compiler/layeredtable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,36 @@ import std/tables
import ast

type
LayeredIdTable* {.acyclic.} = object
LayeredIdTable* {.acyclic.} = ref object
topLayer*: TypeMapping
nextLayer*: ref LayeredIdTable
nextLayer*: LayeredIdTable
previousLen*: int # used to track if bindings were added

proc initLayeredTypeMap*(pt: sink TypeMapping = initTypeMapping()): LayeredIdTable =
result = LayeredIdTable(topLayer: pt, nextLayer: nil)

proc shallowCopy*(pt: LayeredIdTable): LayeredIdTable {.inline.} =
result = LayeredIdTable(topLayer: pt.topLayer, nextLayer: pt.nextLayer, previousLen: pt.previousLen)

proc currentLen*(pt: LayeredIdTable): int =
pt.previousLen + pt.topLayer.len

proc newTypeMapLayer*(pt: LayeredIdTable): LayeredIdTable =
result = LayeredIdTable(topLayer: initTable[ItemId, PType](), previousLen: pt.currentLen)
new(result.nextLayer)
result.nextLayer[] = pt
result = LayeredIdTable(topLayer: initTable[ItemId, PType](), nextLayer: pt, previousLen: pt.currentLen)

proc setToPreviousLayer*(pt: var LayeredIdTable) =
# not splitting the expression breaks refc
let y = pt.nextLayer[]
pt = y
proc setToPreviousLayer*(pt: var LayeredIdTable) {.inline.} =
pt = pt.nextLayer

proc lookup(typeMap: ref LayeredIdTable, key: ItemId): PType =
proc lookup(typeMap: LayeredIdTable, key: ItemId): PType =
result = nil
var tm = typeMap
while tm != nil:
result = getOrDefault(tm.topLayer, key)
if result != nil: return
tm = tm.nextLayer

proc lookup(typeMap: LayeredIdTable, key: ItemId): PType {.inline.} =
result = getOrDefault(typeMap.topLayer, key)
if result == nil and typeMap.nextLayer != nil:
result = lookup(typeMap.nextLayer, key)

template lookup*(typeMap: ref LayeredIdTable, key: PType): PType =
lookup(typeMap, key.itemId)

template lookup*(typeMap: LayeredIdTable, key: PType): PType =
lookup(typeMap, key.itemId)

proc put*(typeMap: var LayeredIdTable, key, value: PType) {.inline.} =
proc put*(typeMap: LayeredIdTable, key, value: PType) {.inline.} =
typeMap.topLayer[key.itemId] = value
2 changes: 1 addition & 1 deletion compiler/seminst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ proc instantiateProcType(c: PContext, pt: LayeredIdTable,
# will need to use openScope, addDecl, etc.
#addDecl(c, prc)
pushInfoContext(c.config, info)
var typeMap = newTypeMapLayer(pt)
var typeMap = shallowCopy(pt)
var cl = initTypeVars(c, typeMap, info, nil)
var result = instCopyType(cl, prc.typ)
let originalParams = result.n
Expand Down
8 changes: 4 additions & 4 deletions compiler/semtypinst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ proc initTypeVars*(p: PContext, typeMap: LayeredIdTable, info: TLineInfo;
proc replaceTypesInBody*(p: PContext, pt: LayeredIdTable, n: PNode;
owner: PSym, allowMetaTypes = false,
fromStaticExpr = false, expectedType: PType = nil): PNode =
var typeMap = newTypeMapLayer(pt)
var typeMap = shallowCopy(pt)
var cl = initTypeVars(p, typeMap, n.info, owner)
cl.allowMetaTypes = allowMetaTypes
pushInfoContext(p.config, n.info)
Expand All @@ -784,7 +784,7 @@ proc replaceTypesInBody*(p: PContext, pt: LayeredIdTable, n: PNode;

proc prepareTypesInBody*(p: PContext, pt: LayeredIdTable, n: PNode;
owner: PSym = nil): PNode =
var typeMap = newTypeMapLayer(pt)
var typeMap = shallowCopy(pt)
var cl = initTypeVars(p, typeMap, n.info, owner)
pushInfoContext(p.config, n.info)
result = prepareNode(cl, n)
Expand Down Expand Up @@ -823,7 +823,7 @@ proc generateTypeInstance*(p: PContext, pt: LayeredIdTable, info: TLineInfo,
# pt: Table with type mappings: T -> int
# Desired result: Foo[int]
# proc (x: T = 0); T -> int ----> proc (x: int = 0)
var typeMap = newTypeMapLayer(pt)
var typeMap = shallowCopy(pt)
var cl = initTypeVars(p, typeMap, info, nil)
pushInfoContext(p.config, info)
result = replaceTypeVarsT(cl, t)
Expand All @@ -835,7 +835,7 @@ proc generateTypeInstance*(p: PContext, pt: LayeredIdTable, info: TLineInfo,

proc prepareMetatypeForSigmatch*(p: PContext, pt: LayeredIdTable, info: TLineInfo,
t: PType): PType =
var typeMap = newTypeMapLayer(pt)
var typeMap = shallowCopy(pt)
var cl = initTypeVars(p, typeMap, info, nil)
cl.allowMetaTypes = true
pushInfoContext(p.config, info)
Expand Down
2 changes: 1 addition & 1 deletion compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ proc shallowCopyCandidate(dest: var TCandidate, src: TCandidate) =
dest.calleeSym = src.calleeSym
dest.call = copyTree(src.call)
dest.baseTypeMatch = src.baseTypeMatch
dest.bindings = src.bindings
dest.bindings = shallowCopy(src.bindings)

proc checkGeneric(a, b: TCandidate): int =
let c = a.c
Expand Down

0 comments on commit 257fa42

Please sign in to comment.