Skip to content

Commit fd5d0a8

Browse files
committed
[TableGen] Allow references to class template arguments in defvar
We can't refer to template arguments for defvar statements in class definitions, or it will report some errors like: ``` error: Variable not defined: 'xxx'. ``` The key point here is we used to pass nullptr to `ParseValue` in `ParseDefvar`. As a result, we can't refer to template arguments since `CurRec` is nullptr in `ParseIDValue`. So we add an argument `CurRec` to `ParseDefvar` and provide it when parsing defvar statements in class definitions. Reviewed By: tra, simon_tatham Differential Revision: https://reviews.llvm.org/D148197
1 parent 99e52b6 commit fd5d0a8

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

llvm/lib/TableGen/TGParser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,7 +3023,7 @@ bool TGParser::ParseBodyItem(Record *CurRec) {
30233023
return ParseAssert(nullptr, CurRec);
30243024

30253025
if (Lex.getCode() == tgtok::Defvar)
3026-
return ParseDefvar();
3026+
return ParseDefvar(CurRec);
30273027

30283028
if (Lex.getCode() != tgtok::Let) {
30293029
if (!ParseDeclaration(CurRec, false))
@@ -3254,7 +3254,7 @@ bool TGParser::ParseDefset() {
32543254
///
32553255
/// Defvar ::= DEFVAR Id '=' Value ';'
32563256
///
3257-
bool TGParser::ParseDefvar() {
3257+
bool TGParser::ParseDefvar(Record *CurRec) {
32583258
assert(Lex.getCode() == tgtok::Defvar);
32593259
Lex.Lex(); // Eat the 'defvar' token
32603260

@@ -3273,7 +3273,7 @@ bool TGParser::ParseDefvar() {
32733273
if (!consume(tgtok::equal))
32743274
return TokError("expected '='");
32753275

3276-
Init *Value = ParseValue(nullptr);
3276+
Init *Value = ParseValue(CurRec);
32773277
if (!Value)
32783278
return true;
32793279

llvm/lib/TableGen/TGParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class TGParser {
231231
bool ParseDefm(MultiClass *CurMultiClass);
232232
bool ParseDef(MultiClass *CurMultiClass);
233233
bool ParseDefset();
234-
bool ParseDefvar();
234+
bool ParseDefvar(Record *CurRec = nullptr);
235235
bool ParseForeach(MultiClass *CurMultiClass);
236236
bool ParseIf(MultiClass *CurMultiClass);
237237
bool ParseIfBody(MultiClass *CurMultiClass, StringRef Kind);

llvm/test/TableGen/defvar.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ defvar myvar = "foo";
1818
defvar myvar = "another value";
1919
#endif
2020

21+
// These variables should be overrided by template arguments.
22+
defvar a = 2333;
23+
defvar b = 2333;
24+
class VarScopeTest<int a, int b> {
25+
defvar c = !add(a, b);
26+
int value = !add(c, c);
27+
}
28+
29+
// CHECK: def aaa_scope_test {
30+
// CHECK-NEXT: int value = 10;
31+
def aaa_scope_test: VarScopeTest<2, 3>;
32+
2133
multiclass Test<int x> {
2234
// Refer to a global variable, while inside a local scope like a multiclass.
2335
def _with_global_string { string s = myvar; }

0 commit comments

Comments
 (0)