Skip to content

Commit b6c77ed

Browse files
authored
refactoring: tag generation (#815)
* tag generator: fewer magic numbers, better scalability * refactoring complete * fewer magic numbers
1 parent 4162834 commit b6c77ed

11 files changed

+913
-920
lines changed

src/lib/nifindexes.nim

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import bitabs, lineinfos, nifreader, nifstreams, nifcursors, nifchecksums
1111

1212
#import std / [sha1]
1313
import "$nim"/dist/checksums/src/checksums/sha1
14-
import ".." / models / nifindex_tags
14+
import ".." / models / [tags, nifindex_tags]
1515

1616
proc entryKind(tag: TagId): NifIndexKind =
17-
if rawTagIsNifIndexKind(tag.uint32):
17+
if rawTagIsNifIndexKind(cast[TagEnum](tag)):
1818
result = cast[NifIndexKind](tag)
1919
else:
2020
result = NoIndexTag

src/lib/nifstreams.nim

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ const
101101

102102
proc createLiterals(data: openArray[(string, int)]): Literals =
103103
result = default(Literals)
104-
for d in data:
105-
let t = result.tags.getOrIncl(d[0])
106-
assert t.int == d[1]
104+
for i in 1 ..< data.len:
105+
let t = result.tags.getOrIncl(data[i][0])
106+
assert t.int == data[i][1]
107107

108108
var pool* = createLiterals(TagData)
109109

src/models/callconv_tags.nim

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
# Generated by tools/gen_tags.nim from doc/tags.md. DO NOT EDIT!
22

3+
import tags
34

45
type
56
CallConv* = enum
67
NoCallConv
7-
Cdecl = (114, "cdecl") ## `cdecl` calling convention
8-
Stdcall = (115, "stdcall") ## `stdcall` calling convention
9-
Safecall = (116, "safecall") ## `safecall` calling convention
10-
Syscall = (117, "syscall") ## `syscall` calling convention
11-
Fastcall = (118, "fastcall") ## `fastcall` calling convention
12-
Thiscall = (119, "thiscall") ## `thiscall` calling convention
13-
Noconv = (120, "noconv") ## no explicit calling convention
14-
Member = (121, "member") ## `member` calling convention
15-
Nimcall = (122, "nimcall") ## `nimcall` calling convention
8+
Cdecl = (ord(CdeclTagId), "cdecl") ## `cdecl` calling convention
9+
Stdcall = (ord(StdcallTagId), "stdcall") ## `stdcall` calling convention
10+
Safecall = (ord(SafecallTagId), "safecall") ## `safecall` calling convention
11+
Syscall = (ord(SyscallTagId), "syscall") ## `syscall` calling convention
12+
Fastcall = (ord(FastcallTagId), "fastcall") ## `fastcall` calling convention
13+
Thiscall = (ord(ThiscallTagId), "thiscall") ## `thiscall` calling convention
14+
Noconv = (ord(NoconvTagId), "noconv") ## no explicit calling convention
15+
Member = (ord(MemberTagId), "member") ## `member` calling convention
16+
Nimcall = (ord(NimcallTagId), "nimcall") ## `nimcall` calling convention
1617

17-
proc rawTagIsCallConv*(raw: uint32): bool {.inline.} =
18-
raw >= 114'u32 and raw <= 122'u32
18+
proc rawTagIsCallConv*(raw: TagEnum): bool {.inline.} =
19+
raw >= CdeclTagId and raw <= NimcallTagId
1920

src/models/nifc_tags.nim

+138-143
Large diffs are not rendered by default.

src/models/nifindex_tags.nim

+35-35
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
# Generated by tools/gen_tags.nim from doc/tags.md. DO NOT EDIT!
22

3+
import tags
34

45
type
56
NifIndexKind* = enum
67
NoIndexTag
7-
KvIdx = (28, "kv") ## key-value pair
8-
VvIdx = (29, "vv") ## value-value pair (used for explicitly named arguments in function calls)
9-
GvarIdx = (51, "gvar") ## global variable declaration
10-
TvarIdx = (52, "tvar") ## thread local variable declaration
11-
VarIdx = (53, "var") ## variable declaration
12-
ConstIdx = (55, "const") ## const variable declaration
13-
GletIdx = (57, "glet") ## global let variable declaration
14-
TletIdx = (58, "tlet") ## thread local let variable declaration
15-
LetIdx = (59, "let") ## let variable declaration
16-
CursorIdx = (60, "cursor") ## cursor variable declaration
17-
ProcIdx = (64, "proc") ## proc declaration
18-
FuncIdx = (65, "func") ## function declaration
19-
IteratorIdx = (66, "iterator") ## iterator declaration
20-
ConverterIdx = (67, "converter") ## converter declaration
21-
MethodIdx = (68, "method") ## method declaration
22-
MacroIdx = (69, "macro") ## macro declaration
23-
TemplateIdx = (70, "template") ## template declaration
24-
TypeIdx = (71, "type") ## type declaration
25-
InlineIdx = (123, "inline") ## `inline` proc annotation
26-
ExportIdx = (143, "export") ## `export` statement
27-
FromexportIdx = (144, "fromexport") ## specific exported symbols from a module
28-
ExportexceptIdx = (145, "exportexcept") ## `exportexcept` statement
29-
BuildIdx = (204, "build") ## `build` pragma
30-
DestroyIdx = (250, "destroy")
31-
DupIdx = (251, "dup")
32-
CopyIdx = (252, "copy")
33-
WasmovedIdx = (253, "wasmoved")
34-
SinkhIdx = (254, "sinkh")
35-
TraceIdx = (255, "trace")
36-
IndexIdx = (263, "index") ## index section
37-
PublicIdx = (264, "public") ## public section
38-
PrivateIdx = (265, "private") ## private section
8+
KvIdx = (ord(KvTagId), "kv") ## key-value pair
9+
VvIdx = (ord(VvTagId), "vv") ## value-value pair (used for explicitly named arguments in function calls)
10+
GvarIdx = (ord(GvarTagId), "gvar") ## global variable declaration
11+
TvarIdx = (ord(TvarTagId), "tvar") ## thread local variable declaration
12+
VarIdx = (ord(VarTagId), "var") ## variable declaration
13+
ConstIdx = (ord(ConstTagId), "const") ## const variable declaration
14+
GletIdx = (ord(GletTagId), "glet") ## global let variable declaration
15+
TletIdx = (ord(TletTagId), "tlet") ## thread local let variable declaration
16+
LetIdx = (ord(LetTagId), "let") ## let variable declaration
17+
CursorIdx = (ord(CursorTagId), "cursor") ## cursor variable declaration
18+
ProcIdx = (ord(ProcTagId), "proc") ## proc declaration
19+
FuncIdx = (ord(FuncTagId), "func") ## function declaration
20+
IteratorIdx = (ord(IteratorTagId), "iterator") ## iterator declaration
21+
ConverterIdx = (ord(ConverterTagId), "converter") ## converter declaration
22+
MethodIdx = (ord(MethodTagId), "method") ## method declaration
23+
MacroIdx = (ord(MacroTagId), "macro") ## macro declaration
24+
TemplateIdx = (ord(TemplateTagId), "template") ## template declaration
25+
TypeIdx = (ord(TypeTagId), "type") ## type declaration
26+
InlineIdx = (ord(InlineTagId), "inline") ## `inline` proc annotation
27+
ExportIdx = (ord(ExportTagId), "export") ## `export` statement
28+
FromexportIdx = (ord(FromexportTagId), "fromexport") ## specific exported symbols from a module
29+
ExportexceptIdx = (ord(ExportexceptTagId), "exportexcept") ## `exportexcept` statement
30+
BuildIdx = (ord(BuildTagId), "build") ## `build` pragma
31+
DestroyIdx = (ord(DestroyTagId), "destroy")
32+
DupIdx = (ord(DupTagId), "dup")
33+
CopyIdx = (ord(CopyTagId), "copy")
34+
WasmovedIdx = (ord(WasmovedTagId), "wasmoved")
35+
SinkhIdx = (ord(SinkhTagId), "sinkh")
36+
TraceIdx = (ord(TraceTagId), "trace")
37+
IndexIdx = (ord(IndexTagId), "index") ## index section
38+
PublicIdx = (ord(PublicTagId), "public") ## public section
39+
PrivateIdx = (ord(PrivateTagId), "private") ## private section
3940

40-
proc rawTagIsNifIndexKind*(raw: uint32): bool {.inline.} =
41-
let r = raw - 28'u32
42-
r <= 255'u32 and r.uint8 in {0'u8, 1'u8, 23'u8, 24'u8, 25'u8, 27'u8, 29'u8, 30'u8, 31'u8, 32'u8, 36'u8, 37'u8, 38'u8, 39'u8, 40'u8, 41'u8, 42'u8, 43'u8, 95'u8, 115'u8, 116'u8, 117'u8, 176'u8, 222'u8, 223'u8, 224'u8, 225'u8, 226'u8, 227'u8, 235'u8, 236'u8, 237'u8}
41+
proc rawTagIsNifIndexKind*(raw: TagEnum): bool {.inline.} =
42+
raw in {KvTagId, VvTagId, GvarTagId, TvarTagId, VarTagId, ConstTagId, GletTagId, TletTagId, LetTagId, CursorTagId, ProcTagId, FuncTagId, IteratorTagId, ConverterTagId, MethodTagId, MacroTagId, TemplateTagId, TypeTagId, InlineTagId, ExportTagId, FromexportTagId, ExportexceptTagId, BuildTagId, DestroyTagId, DupTagId, CopyTagId, WasmovedTagId, SinkhTagId, TraceTagId, IndexTagId, PublicTagId, PrivateTagId}
4343

src/models/nifler_tags.nim

+96-95
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,102 @@
11
# Generated by tools/gen_tags.nim from doc/tags.md. DO NOT EDIT!
22

3+
import tags
34

45
type
56
NiflerKind* = enum
67
None
7-
ErrL = (1, "err") ## indicates an error
8-
SufL = (2, "suf") ## literal with suffix annotation
9-
AtL = (3, "at") ## array indexing operation
10-
DerefL = (4, "deref") ## pointer deref operation
11-
DotL = (5, "dot") ## object field selection
12-
ParL = (7, "par") ## syntactic parenthesis
13-
AddrL = (8, "addr") ## address of operation
14-
NilL = (9, "nil") ## nil pointer value
15-
OconstrL = (23, "oconstr") ## object constructor
16-
BracketL = (25, "bracket") ## untyped array constructor
17-
CurlyL = (26, "curly") ## untyped set constructor
18-
CurlyatL = (27, "curlyat") ## curly expression `a{i}`
19-
KvL = (28, "kv") ## key-value pair
20-
VvL = (29, "vv") ## value-value pair (used for explicitly named arguments in function calls)
21-
CastL = (45, "cast") ## `cast` operation
22-
CallL = (47, "call") ## call operation
23-
CmdL = (48, "cmd") ## command operation
24-
RangesL = (50, "ranges")
25-
VarL = (53, "var") ## variable declaration
26-
ParamL = (54, "param") ## parameter declaration
27-
ConstL = (55, "const") ## const variable declaration
28-
LetL = (59, "let") ## let variable declaration
29-
TypevarL = (61, "typevar") ## type variable declaration
30-
EfldL = (62, "efld") ## enum field declaration
31-
FldL = (63, "fld") ## field declaration
32-
ProcL = (64, "proc") ## proc declaration
33-
FuncL = (65, "func") ## function declaration
34-
IteratorL = (66, "iterator") ## iterator declaration
35-
ConverterL = (67, "converter") ## converter declaration
36-
MethodL = (68, "method") ## method declaration
37-
MacroL = (69, "macro") ## macro declaration
38-
TemplateL = (70, "template") ## template declaration
39-
TypeL = (71, "type") ## type declaration
40-
BlockL = (72, "block") ## block declaration
41-
AsgnL = (77, "asgn") ## assignment statement
42-
IfL = (79, "if") ## if statement header
43-
WhenL = (80, "when") ## when statement header
44-
ElifL = (81, "elif") ## pair of (condition, action)
45-
ElseL = (82, "else") ## `else` action
46-
TypevarsL = (83, "typevars") ## type variable/generic parameters
47-
BreakL = (84, "break") ## `break` statement
48-
ContinueL = (85, "continue") ## `continue` statement
49-
ForL = (86, "for") ## for statement
50-
WhileL = (87, "while") ## `while` statement
51-
CaseL = (88, "case") ## `case` statement
52-
OfL = (89, "of") ## `of` branch within a `case` statement
53-
RetL = (92, "ret") ## `return` instruction
54-
YldL = (93, "yld") ## yield statement
55-
StmtsL = (94, "stmts") ## list of statements
56-
ParamsL = (95, "params") ## list of proc parameters, also used as a "proc type"
57-
ObjectL = (97, "object") ## object type declaration
58-
EnumL = (98, "enum") ## enum type declaration
59-
ProctypeL = (99, "proctype") ## proc type declaration (soon obsolete, use params instead)
60-
PtrL = (110, "ptr") ## `ptr` type contructor
61-
PragmasL = (129, "pragmas") ## begin of pragma section
62-
PragmaxL = (130, "pragmax") ## pragma expressions
63-
IncludeL = (138, "include") ## `include` statement
64-
ImportL = (139, "import") ## `import` statement
65-
ImportasL = (140, "importas") ## `import as` statement
66-
FromimportL = (141, "fromimport") ## `from import` statement
67-
ImportexceptL = (142, "importexcept") ## `importexcept` statement
68-
ExportL = (143, "export") ## `export` statement
69-
ExportexceptL = (145, "exportexcept") ## `exportexcept` statement
70-
CommentL = (146, "comment") ## `comment` statement
71-
DiscardL = (147, "discard") ## `discard` statement
72-
TryL = (148, "try") ## `try` statement
73-
RaiseL = (149, "raise") ## `raise` statement
74-
StaticL = (153, "static") ## `static` type or annotation
75-
UnpackflatL = (158, "unpackflat") ## unpack into flat variable list
76-
UnpacktupL = (159, "unpacktup") ## unpack tuple
77-
UnpackdeclL = (160, "unpackdecl") ## unpack var/let/const declaration
78-
ExceptL = (161, "except") ## except subsection
79-
FinL = (162, "fin") ## finally subsection
80-
TupleL = (163, "tuple") ## `tuple` type
81-
RefL = (165, "ref") ## `ref` type
82-
MutL = (166, "mut") ## `mut` type
83-
OutL = (167, "out") ## `out` type
84-
ConceptL = (171, "concept") ## `concept` type
85-
DistinctL = (172, "distinct") ## `distinct` type
86-
ItertypeL = (173, "itertype") ## `itertype` type
87-
QuotedL = (207, "quoted") ## name in backticks
88-
TupL = (213, "tup") ## untyped tuple constructor
89-
TabconstrL = (216, "tabconstr") ## table constructor
90-
CallstrlitL = (221, "callstrlit")
91-
InfixL = (222, "infix")
92-
PrefixL = (223, "prefix")
93-
TypeofL = (230, "typeof")
94-
ExprL = (236, "expr")
95-
DoL = (237, "do") ## `do` expression
96-
StaticstmtL = (257, "staticstmt") ## `static` statement
97-
BindL = (258, "bind") ## `bind` statement
98-
MixinL = (259, "mixin") ## `mixin` statement
99-
UsingL = (260, "using") ## `using` statement
100-
AsmL = (261, "asm") ## `asm` statement
101-
DeferL = (262, "defer") ## `defer` statement
8+
ErrL = (ord(ErrTagId), "err") ## indicates an error
9+
SufL = (ord(SufTagId), "suf") ## literal with suffix annotation
10+
AtL = (ord(AtTagId), "at") ## array indexing operation
11+
DerefL = (ord(DerefTagId), "deref") ## pointer deref operation
12+
DotL = (ord(DotTagId), "dot") ## object field selection
13+
ParL = (ord(ParTagId), "par") ## syntactic parenthesis
14+
AddrL = (ord(AddrTagId), "addr") ## address of operation
15+
NilL = (ord(NilTagId), "nil") ## nil pointer value
16+
OconstrL = (ord(OconstrTagId), "oconstr") ## object constructor
17+
BracketL = (ord(BracketTagId), "bracket") ## untyped array constructor
18+
CurlyL = (ord(CurlyTagId), "curly") ## untyped set constructor
19+
CurlyatL = (ord(CurlyatTagId), "curlyat") ## curly expression `a{i}`
20+
KvL = (ord(KvTagId), "kv") ## key-value pair
21+
VvL = (ord(VvTagId), "vv") ## value-value pair (used for explicitly named arguments in function calls)
22+
CastL = (ord(CastTagId), "cast") ## `cast` operation
23+
CallL = (ord(CallTagId), "call") ## call operation
24+
CmdL = (ord(CmdTagId), "cmd") ## command operation
25+
RangesL = (ord(RangesTagId), "ranges")
26+
VarL = (ord(VarTagId), "var") ## variable declaration
27+
ParamL = (ord(ParamTagId), "param") ## parameter declaration
28+
ConstL = (ord(ConstTagId), "const") ## const variable declaration
29+
LetL = (ord(LetTagId), "let") ## let variable declaration
30+
TypevarL = (ord(TypevarTagId), "typevar") ## type variable declaration
31+
EfldL = (ord(EfldTagId), "efld") ## enum field declaration
32+
FldL = (ord(FldTagId), "fld") ## field declaration
33+
ProcL = (ord(ProcTagId), "proc") ## proc declaration
34+
FuncL = (ord(FuncTagId), "func") ## function declaration
35+
IteratorL = (ord(IteratorTagId), "iterator") ## iterator declaration
36+
ConverterL = (ord(ConverterTagId), "converter") ## converter declaration
37+
MethodL = (ord(MethodTagId), "method") ## method declaration
38+
MacroL = (ord(MacroTagId), "macro") ## macro declaration
39+
TemplateL = (ord(TemplateTagId), "template") ## template declaration
40+
TypeL = (ord(TypeTagId), "type") ## type declaration
41+
BlockL = (ord(BlockTagId), "block") ## block declaration
42+
AsgnL = (ord(AsgnTagId), "asgn") ## assignment statement
43+
IfL = (ord(IfTagId), "if") ## if statement header
44+
WhenL = (ord(WhenTagId), "when") ## when statement header
45+
ElifL = (ord(ElifTagId), "elif") ## pair of (condition, action)
46+
ElseL = (ord(ElseTagId), "else") ## `else` action
47+
TypevarsL = (ord(TypevarsTagId), "typevars") ## type variable/generic parameters
48+
BreakL = (ord(BreakTagId), "break") ## `break` statement
49+
ContinueL = (ord(ContinueTagId), "continue") ## `continue` statement
50+
ForL = (ord(ForTagId), "for") ## for statement
51+
WhileL = (ord(WhileTagId), "while") ## `while` statement
52+
CaseL = (ord(CaseTagId), "case") ## `case` statement
53+
OfL = (ord(OfTagId), "of") ## `of` branch within a `case` statement
54+
RetL = (ord(RetTagId), "ret") ## `return` instruction
55+
YldL = (ord(YldTagId), "yld") ## yield statement
56+
StmtsL = (ord(StmtsTagId), "stmts") ## list of statements
57+
ParamsL = (ord(ParamsTagId), "params") ## list of proc parameters, also used as a "proc type"
58+
ObjectL = (ord(ObjectTagId), "object") ## object type declaration
59+
EnumL = (ord(EnumTagId), "enum") ## enum type declaration
60+
ProctypeL = (ord(ProctypeTagId), "proctype") ## proc type declaration (soon obsolete, use params instead)
61+
PtrL = (ord(PtrTagId), "ptr") ## `ptr` type contructor
62+
PragmasL = (ord(PragmasTagId), "pragmas") ## begin of pragma section
63+
PragmaxL = (ord(PragmaxTagId), "pragmax") ## pragma expressions
64+
IncludeL = (ord(IncludeTagId), "include") ## `include` statement
65+
ImportL = (ord(ImportTagId), "import") ## `import` statement
66+
ImportasL = (ord(ImportasTagId), "importas") ## `import as` statement
67+
FromimportL = (ord(FromimportTagId), "fromimport") ## `from import` statement
68+
ImportexceptL = (ord(ImportexceptTagId), "importexcept") ## `importexcept` statement
69+
ExportL = (ord(ExportTagId), "export") ## `export` statement
70+
ExportexceptL = (ord(ExportexceptTagId), "exportexcept") ## `exportexcept` statement
71+
CommentL = (ord(CommentTagId), "comment") ## `comment` statement
72+
DiscardL = (ord(DiscardTagId), "discard") ## `discard` statement
73+
TryL = (ord(TryTagId), "try") ## `try` statement
74+
RaiseL = (ord(RaiseTagId), "raise") ## `raise` statement
75+
StaticL = (ord(StaticTagId), "static") ## `static` type or annotation
76+
UnpackflatL = (ord(UnpackflatTagId), "unpackflat") ## unpack into flat variable list
77+
UnpacktupL = (ord(UnpacktupTagId), "unpacktup") ## unpack tuple
78+
UnpackdeclL = (ord(UnpackdeclTagId), "unpackdecl") ## unpack var/let/const declaration
79+
ExceptL = (ord(ExceptTagId), "except") ## except subsection
80+
FinL = (ord(FinTagId), "fin") ## finally subsection
81+
TupleL = (ord(TupleTagId), "tuple") ## `tuple` type
82+
RefL = (ord(RefTagId), "ref") ## `ref` type
83+
MutL = (ord(MutTagId), "mut") ## `mut` type
84+
OutL = (ord(OutTagId), "out") ## `out` type
85+
ConceptL = (ord(ConceptTagId), "concept") ## `concept` type
86+
DistinctL = (ord(DistinctTagId), "distinct") ## `distinct` type
87+
ItertypeL = (ord(ItertypeTagId), "itertype") ## `itertype` type
88+
QuotedL = (ord(QuotedTagId), "quoted") ## name in backticks
89+
TupL = (ord(TupTagId), "tup") ## untyped tuple constructor
90+
TabconstrL = (ord(TabconstrTagId), "tabconstr") ## table constructor
91+
CallstrlitL = (ord(CallstrlitTagId), "callstrlit")
92+
InfixL = (ord(InfixTagId), "infix")
93+
PrefixL = (ord(PrefixTagId), "prefix")
94+
TypeofL = (ord(TypeofTagId), "typeof")
95+
ExprL = (ord(ExprTagId), "expr")
96+
DoL = (ord(DoTagId), "do") ## `do` expression
97+
StaticstmtL = (ord(StaticstmtTagId), "staticstmt") ## `static` statement
98+
BindL = (ord(BindTagId), "bind") ## `bind` statement
99+
MixinL = (ord(MixinTagId), "mixin") ## `mixin` statement
100+
UsingL = (ord(UsingTagId), "using") ## `using` statement
101+
AsmL = (ord(AsmTagId), "asm") ## `asm` statement
102+
DeferL = (ord(DeferTagId), "defer") ## `defer` statement

0 commit comments

Comments
 (0)