Skip to content

Commit

Permalink
explicit global variables (#561)
Browse files Browse the repository at this point in the history
* explicit global variables

* completed
  • Loading branch information
Araq authored Feb 15, 2025
1 parent c7dc817 commit e096630
Show file tree
Hide file tree
Showing 32 changed files with 95 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/hexer/constparams.nim
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ proc tr(c: var Context; dest: var TokenBuf; n: var Cursor) =
case n.stmtKind
of ProcS, FuncS, MacroS, MethodS, ConverterS:
trProcDecl c, dest, n
of CursorS, LetS, VarS, ConstS, ResultS:
of LocalDecls:
trLocal c, dest, n
of ScopeS:
trScope c, dest, n
Expand Down
2 changes: 1 addition & 1 deletion src/hexer/destroyer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ proc tr(c: var Context; n: var Cursor) =
trCase c, n
of BlockS:
trBlock c, n
of VarS, LetS, ConstS, ResultS, CursorS:
of LocalDecls:
trLocal c, n
of WhileS:
trWhile c, n
Expand Down
2 changes: 1 addition & 1 deletion src/hexer/desugar.nim
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ proc tr(c: var Context; dest: var TokenBuf; n: var Cursor) =
tr(c, dest, n)
else:
tr(c, dest, n)
of ResultS, LetS, VarS, CursorS, ConstS:
of LocalDecls:
trLocal c, dest, n
of ProcS, FuncS, MacroS, MethodS, ConverterS:
trProc c, dest, n
Expand Down
2 changes: 1 addition & 1 deletion src/hexer/duplifier.nim
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ proc tr(c: var Context; n: var Cursor; e: Expects) =
trReturn c, n
of AsgnS:
trAsgn c, n
of VarS, LetS, ConstS, ResultS, CursorS:
of LocalDecls:
trLocal c, n
of ProcS, FuncS, ConverterS, MethodS, MacroS:
trProcDecl c, n
Expand Down
2 changes: 1 addition & 1 deletion src/hexer/inliner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ proc tr(c: var Context; dest: var TokenBuf; n: var Cursor) =
case n.stmtKind
of AsgnS:
trAsgn c, dest, n
of LetS, CursorS, VarS, ResultS:
of LocalDecls:
trLocalDecl c, dest, n
of ProcS, FuncS, MacroS, MethodS, ConverterS:
trProcDecl c, dest, n
Expand Down
2 changes: 1 addition & 1 deletion src/hexer/mover.nim
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ proc singlePath(pc, x: Cursor; pcs: var seq[Cursor]; otherUsage: var Cursor): bo
of StmtsS, ScopeS, BlockS, ContinueS, BreakS:
inc pc
inc nested
of VarS, LetS, CursorS, ResultS, ConstS, GvarS, TvarS, GletS, TletS:
of LocalDecls:
inc pc
if root != NoSymId and pc.kind == SymbolDef and pc.symId == root:
# found the definition, so it gets a new value:
Expand Down
14 changes: 6 additions & 8 deletions src/hexer/nifcgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1062,12 +1062,6 @@ proc traverseLocal(e: var EContext; c: var Cursor; tag: string; mode: TraverseMo
if prag.header != StrId(0):
e.headers.incl prag.header

if mode != TraverseTopLevel and tag in ["var", "const", "param"] and
prag.flags * {ThreadvarP, GlobalP} == {}: # register local variables
var declBuf = createTokenBuf()
takeTree(declBuf, localDecl)
publish s, declBuf

proc traverseWhile(e: var EContext; c: var Cursor) =
let info = c.info
e.nestedIn.add (WhileS, SymId(0))
Expand Down Expand Up @@ -1210,8 +1204,12 @@ proc traverseStmt(e: var EContext; c: var Cursor; mode = TraverseAll) =
e.loop c:
traverseStmt e, c, mode
e.closeMangleScope()
of VarS, LetS, CursorS, ResultS, GvarS, TvarS, GletS, TletS:
traverseLocal e, c, (if e.nestedIn[^1][0] == StmtsS and mode in {TraverseTopLevel, TraverseSig}: "gvar" else: "var"), mode
of VarS, LetS, CursorS, ResultS:
traverseLocal e, c, "var", mode
of GvarS, GletS:
traverseLocal e, c, "gvar", mode
of TvarS, TletS:
traverseLocal e, c, "tvar", mode
of ConstS:
traverseLocal e, c, "const", mode
of CmdS, CallS:
Expand Down
4 changes: 2 additions & 2 deletions src/hexer/xelim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ proc isComplex(n: Cursor): bool =
of IntLit, UIntLit, FloatLit, StringLit, CharLit, UnknownToken, EofToken, Ident, Symbol, SymbolDef, DotToken:
inc n
of ParLe:
if n.stmtKind in {IfS, CaseS, WhileS, AsgnS, LetS, VarS, CursorS, StmtsS, ResultS}:
if n.stmtKind in {IfS, CaseS, WhileS, AsgnS, LetS, VarS, CursorS, StmtsS, ResultS, GletS, TletS, GvarS, TvarS}:
return true
elif n.exprKind == ExprX:
inc n
Expand Down Expand Up @@ -373,7 +373,7 @@ proc trStmt(c: var Context; dest: var TokenBuf; n: var Cursor) =
while n.kind != ParRi:
trExpr c, dest, n, tar
dest.add tar
of ResultS, LetS, VarS, CursorS, ConstS, GvarS, TvarS, GletS, TletS:
of LocalDecls:
trLocal c, dest, n
of ProcS, FuncS, MacroS, MethodS, ConverterS:
trProc c, dest, n
Expand Down
2 changes: 1 addition & 1 deletion src/nimony/decls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ proc isRoutine*(t: SymKind): bool {.inline.} =
t in {ProcY, FuncY, IteratorY, MacroY, TemplateY, ConverterY, MethodY}

proc isLocal*(t: SymKind): bool {.inline.} =
t in {LetY, VarY, ResultY, ConstY, ParamY, TypevarY, CursorY, FldY, EfldY}
t in {LetY, VarY, ResultY, ConstY, ParamY, TypevarY, CursorY, FldY, EfldY, GletY, TletY, GvarY, TvarY}

proc isNominal*(t: TypeKind): bool {.inline.} =
## type kinds that should stay as symbols, see sigmatch.matchSymbol
Expand Down
6 changes: 3 additions & 3 deletions src/nimony/derefs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ proc isAddressable*(n: Cursor): bool =
let res = tryLoadSym(s)
assert res.status == LacksNothing
let local = asLocal(res.decl)
result = local.kind in {ParamY, LetY, ResultY, VarY, CursorY, LetY, ConstY}
result = local.kind in {ParamY, LetY, ResultY, VarY, CursorY, ConstY, GletY, TletY, GvarY, TvarY}
# Assignments to `ConstY` are prevented later.
else:
result = false
Expand Down Expand Up @@ -186,7 +186,7 @@ proc borrowsFromReadonly(c: var Context; n: Cursor): bool =
case local.kind
of ConstY:
result = true
of LetY:
of LetY, GletY, TletY:
let tk = local.typ.typeKind
result = tk notin {MutT, OutT}
if result and tk == OpenArrayT:
Expand Down Expand Up @@ -542,7 +542,7 @@ proc tr(c: var Context; n: var Cursor; e: Expects) =
trReturn(c, n)
of AsgnS:
trAsgn c, n
of VarS, LetS, ConstS, CursorS, ResultS:
of LocalDecls:
trLocal c, n
of ProcS, FuncS, MacroS, MethodS, ConverterS:
trProcDecl c, n
Expand Down
3 changes: 3 additions & 0 deletions src/nimony/nimony_model.nim
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,6 @@ proc skipModifier*(a: Cursor): Cursor =
result = a
if result.kind == ParLe and result.typeKind in TypeModifiers:
inc result

const
LocalDecls* = {VarS, LetS, ConstS, ResultS, CursorS, GvarS, TvarS, GletS, TletS}
26 changes: 24 additions & 2 deletions src/nimony/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2872,7 +2872,7 @@ proc semLocal(c: var SemContext; n: var Cursor; kind: SymKind) =
of TypevarY:
discard semLocalType(c, n, InGenericConstraint)
wantDot c, n
of ParamY, LetY, VarY, ConstY, CursorY, ResultY, FldY:
of ParamY, LetY, VarY, ConstY, CursorY, ResultY, FldY, GletY, TletY, GvarY, TvarY:
let beforeType = c.dest.len
if n.kind == DotToken:
# no explicit type given:
Expand Down Expand Up @@ -2903,6 +2903,16 @@ proc semLocal(c: var SemContext; n: var Cursor; kind: SymKind) =
assert false, "bug"
c.addSym delayed
takeParRi c, n
if kind == LetY:
if ThreadvarP in crucial.flags:
copyKeepLineInfo c.dest[declStart], parLeToken(TletS, NoLineInfo)
elif GlobalP in crucial.flags or c.currentScope.kind == TopLevelScope:
copyKeepLineInfo c.dest[declStart], parLeToken(GletS, NoLineInfo)
elif kind == VarY:
if ThreadvarP in crucial.flags:
copyKeepLineInfo c.dest[declStart], parLeToken(TvarS, NoLineInfo)
elif GlobalP in crucial.flags or c.currentScope.kind == TopLevelScope:
copyKeepLineInfo c.dest[declStart], parLeToken(GvarS, NoLineInfo)
publish c, delayed.s.name, declStart

proc semLocal(c: var SemContext; it: var Item; kind: SymKind) =
Expand Down Expand Up @@ -5234,9 +5244,21 @@ proc semExpr(c: var SemContext; it: var Item; flags: set[SemFlag] = {}) =
of VarS:
toplevelGuard c:
semLocal c, it, VarY
of GvarS:
toplevelGuard c:
semLocal c, it, GvarY
of TvarS:
toplevelGuard c:
semLocal c, it, TvarY
of LetS:
toplevelGuard c:
semLocal c, it, LetY
of GletS:
toplevelGuard c:
semLocal c, it, GletY
of TletS:
toplevelGuard c:
semLocal c, it, TletY
of CursorS:
toplevelGuard c:
semLocal c, it, CursorY
Expand Down Expand Up @@ -5301,7 +5323,7 @@ proc semExpr(c: var SemContext; it: var Item; flags: set[SemFlag] = {}) =
of ExportS, CommentS:
# XXX ignored for now
skip it.n
of EmitS, GvarS, TvarS, GletS, TletS:
of EmitS:
raiseAssert "unreachable"
of PragmasS:
toplevelGuard c:
Expand Down
2 changes: 1 addition & 1 deletion src/nimony/sembasics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ proc buildSymChoiceForDot(c: var SemContext; identifier: StrId; info: PackedLine
c.dest.add identToken(identifier, info)

proc isNonOverloadable(t: SymKind): bool {.inline.} =
t in {LetY, VarY, ParamY, TypevarY, ConstY, TypeY, ResultY, FldY, CursorY, BlockY}
t in {LetY, VarY, ParamY, TypevarY, ConstY, TypeY, ResultY, FldY, CursorY, BlockY, GletY, TletY, GvarY, TvarY}

proc buildSymChoiceForSelfModule(c: var SemContext;
identifier: StrId; info: PackedLineInfo) =
Expand Down
4 changes: 2 additions & 2 deletions tests/nimony/nosystem/tbinarytree.nif
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(ref 4 NodeObj.0.tbimvo1d9) .))) 7,6
(type ~5 :Node.0.tbimvo1d9 . . . 2
(ref 4 NodeObj.0.tbimvo1d9)) 4,8
(var :x.0.tbimvo1d9 . . 5,~2
(gvar :x.0.tbimvo1d9 . . 5,~2
(ref 4 NodeObj.0.tbimvo1d9) 8
(newobj ~3,~2
(ref 4 NodeObj.0.tbimvo1d9) 5
Expand All @@ -41,7 +41,7 @@
(ddot ~5
(ddot ~1 x.0.tbimvo1d9 left.0.tbimvo1d9 +0) right.0.tbimvo1d9 +0) 2
(nil)) 4,13
(var :y.0.tbimvo1d9 . . 5,~7
(gvar :y.0.tbimvo1d9 . . 5,~7
(ref 4 NodeObj.0.tbimvo1d9) 8
(newobj ~3,~7
(ref 4 NodeObj.0.tbimvo1d9) 5
Expand Down
2 changes: 1 addition & 1 deletion tests/nimony/nosystem/tbool.nif
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
(stmts
(ret "true"))))
(ret result.0))) 4,3
(let :x.0.tbokvwxm9 . . 1,~3
(glet :x.0.tbokvwxm9 . . 1,~3
(bool) 10
(true)))
6 changes: 3 additions & 3 deletions tests/nimony/nosystem/tcstring.nif
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@
(bool) 28
(pragmas 2
(magic 7 "LeI")) . .) 4,13
(var :x.0.tcsj45p9m . . 12,~5
(gvar :x.0.tcsj45p9m . . 12,~5
(i -1) 4
(cast 5
(i -1) 10 +55)) 4,15
(var :y.0.tcsj45p9m . . 3
(gvar :y.0.tcsj45p9m . . 3
(pointer) 13
(nil)) 2,18
(const :myconst.0.tcsj45p9m . . 10
(cstring) 17
(conv ~7
(cstring)
(suf "abc" "R"))) 4,20
(var :zz.0.tcsj45p9m . . 4
(gvar :zz.0.tcsj45p9m . . 4
(cstring)
(hconv 8,22,tests/nimony/nosystem/tcstring.nim
(cstring) 18,22,tests/nimony/nosystem/tcstring.nim "xzy")))
6 changes: 3 additions & 3 deletions tests/nimony/nosystem/tdefinedarg.nif
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
(untyped) .)) . 25
(pragmas 2
(magic 7 Defined)) . .) 4,4
(let :x.0.tdej0g1hh1 . .
(glet :x.0.tdej0g1hh1 . .
(bool) 11
(false)) 4,5
(let :y.0.tdej0g1hh1 . .
(glet :y.0.tdej0g1hh1 . .
(bool) 11
(true)) 4,6
(let :z.0.tdej0g1hh1 . .
(glet :z.0.tdej0g1hh1 . .
(bool) 15
(false)))
4 changes: 2 additions & 2 deletions tests/nimony/nosystem/tdistinct.nif
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@
(i -1) x.4)
(dconv 11,~2
(i -1) y.3)))))) 4,29
(var :x.0.tdiuhv8dm . . 3 VarId.0.tdiuhv8dm .) 2,31
(gvar :x.0.tdiuhv8dm . . 3 VarId.0.tdiuhv8dm .) 2,31
(asgn ~2 x.0.tdiuhv8dm 4
(infix \2B.1.tdiuhv8dm ~2 x.0.tdiuhv8dm 2 x.0.tdiuhv8dm)) 4,33
(let :y.0.tdiuhv8dm . . 4
(glet :y.0.tdiuhv8dm . . 4
(i +8) 8
(conv ~4
(i +8) 1 x.0.tdiuhv8dm)))
16 changes: 8 additions & 8 deletions tests/nimony/nosystem/tgeneric_obj.nif
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@
(pragmas 2
(magic 7 TypeOf)) . .) 10,14
(type ~8 :string.0.tge7jvqqk1 x . . string.0.sys9azlf) 4,16
(var :myset.0.tge7jvqqk1 . . 10
(gvar :myset.0.tge7jvqqk1 . . 10
(set 1
(u +8)) .) 4,17
(var :myarr.0.tge7jvqqk1 . . 12
(gvar :myarr.0.tge7jvqqk1 . . 12
(array 4
(i -1)
(rangetype
(i -1) +0 +2)) .) 4,18
(let :u8.0.tge7jvqqk1 . .
(glet :u8.0.tge7jvqqk1 . .
(u +8) 5
(suf +3u "u8")) 6,19
(asgn ~6 myset.0.tge7jvqqk1 2
Expand All @@ -76,8 +76,8 @@
(typevar :T.4.tge7jvqqk1 . . . .)) . 2
(object . ~13,1
(fld :x.0.tge7jvqqk1 . . 3 T.4.tge7jvqqk1 .))) 2,27
(var :myglob.0.tge7jvqqk1 . . 17 MyGeneric.1.tge7jvqqk1 .) 2,28
(var :other.0.tge7jvqqk1 . . 16 MyGeneric.2.tge7jvqqk1 .) 9,30
(gvar :myglob.0.tge7jvqqk1 . . 17 MyGeneric.1.tge7jvqqk1 .) 2,28
(gvar :other.0.tge7jvqqk1 . . 16 MyGeneric.2.tge7jvqqk1 .) 9,30
(asgn ~3
(dot ~6 myglob.0.tge7jvqqk1 x.1.tge7jvqqk1 +0) 2 +56) 8,31
(asgn ~3
Expand Down Expand Up @@ -144,7 +144,7 @@
(at ~6 HSlice.0.tge7jvqqk1 1 T.7.tge7jvqqk1 4 U.1.tge7jvqqk1) .)
(discard .) ~45
(ret result.1))) 4,56
(var :myarr2.0.tge7jvqqk1 . . 12,~39
(gvar :myarr2.0.tge7jvqqk1 . . 12,~39
(array 4
(i -1)
(rangetype
Expand All @@ -155,7 +155,7 @@
(i -1)
(rangetype
(i -1) +0 +2)) 1 +4 4 +5 7 +6)) 4,59
(var :m3.0.tge7jvqqk1 . . 9
(gvar :m3.0.tge7jvqqk1 . . 9
(array 4
(i -1)
(rangetype
Expand All @@ -165,7 +165,7 @@
(i -1)
(rangetype
(i -1) +0 +3)) 1 +1 4 +2 7 +3 10 +45)) 4,60
(var :x3.0.tge7jvqqk1 . . 9
(gvar :x3.0.tge7jvqqk1 . . 9
(array 7
(i -1)
(rangetype
Expand Down
6 changes: 3 additions & 3 deletions tests/nimony/nosystem/tgenericbinarytree.nif
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
(fld :right.0.tge70unlm . . 2,~3
(ref 11
(at ~7 NodeObj.0.tge70unlm ~1,3 T.1.tge70unlm)) .))) 4,8
(var :x.0.tge70unlm . . 8,~5
(gvar :x.0.tge70unlm . . 8,~5
(ref 11 NodeObj.1.tge70unlm) 13
(newobj ~5,~5
(ref 11 NodeObj.1.tge70unlm) 5
Expand All @@ -47,7 +47,7 @@
(ddot ~5
(ddot ~1 x.0.tge70unlm left.1.tge70unlm +0) right.1.tge70unlm +0) 2
(nil)) 4,13
(var :y.0.tge70unlm . . 8,~10
(gvar :y.0.tge70unlm . . 8,~10
(ref 11 NodeObj.1.tge70unlm) 13
(newobj ~5,~10
(ref 11 NodeObj.1.tge70unlm) 5
Expand Down Expand Up @@ -87,7 +87,7 @@
(asgn ~6
(ddot ~6 result.0 data.0.tge70unlm +0) 2 data.0) ~2,~1
(ret result.0))) 4,19
(let :a.0.tge70unlm . . 8,~16
(glet :a.0.tge70unlm . . 8,~16
(ref 11 NodeObj.1.tge70unlm) 7
(call ~3 foo.1.tge70unlm 1 +123)) 2,20
(asgn ~2 x.0.tge70unlm 2 a.0.tge70unlm) 11,19
Expand Down
Loading

0 comments on commit e096630

Please sign in to comment.