diff --git a/llvm/lib/TableGen/TGParser.h b/llvm/lib/TableGen/TGParser.h --- a/llvm/lib/TableGen/TGParser.h +++ b/llvm/lib/TableGen/TGParser.h @@ -231,7 +231,7 @@ bool ParseDefm(MultiClass *CurMultiClass); bool ParseDef(MultiClass *CurMultiClass); bool ParseDefset(); - bool ParseDefvar(); + bool ParseDefvar(Record *CurRec = nullptr); bool ParseForeach(MultiClass *CurMultiClass); bool ParseIf(MultiClass *CurMultiClass); bool ParseIfBody(MultiClass *CurMultiClass, StringRef Kind); diff --git a/llvm/lib/TableGen/TGParser.cpp b/llvm/lib/TableGen/TGParser.cpp --- a/llvm/lib/TableGen/TGParser.cpp +++ b/llvm/lib/TableGen/TGParser.cpp @@ -3023,7 +3023,7 @@ return ParseAssert(nullptr, CurRec); if (Lex.getCode() == tgtok::Defvar) - return ParseDefvar(); + return ParseDefvar(CurRec); if (Lex.getCode() != tgtok::Let) { if (!ParseDeclaration(CurRec, false)) @@ -3254,7 +3254,7 @@ /// /// Defvar ::= DEFVAR Id '=' Value ';' /// -bool TGParser::ParseDefvar() { +bool TGParser::ParseDefvar(Record *CurRec) { assert(Lex.getCode() == tgtok::Defvar); Lex.Lex(); // Eat the 'defvar' token @@ -3273,7 +3273,7 @@ if (!consume(tgtok::equal)) return TokError("expected '='"); - Init *Value = ParseValue(nullptr); + Init *Value = ParseValue(CurRec); if (!Value) return true; diff --git a/llvm/test/TableGen/defvar.td b/llvm/test/TableGen/defvar.td --- a/llvm/test/TableGen/defvar.td +++ b/llvm/test/TableGen/defvar.td @@ -18,6 +18,18 @@ defvar myvar = "another value"; #endif +// These variables should be overrided by template arguments. +defvar a = 2333; +defvar b = 2333; +class VarScopeTest { + defvar c = !add(a, b); + int value = !add(c, c); +} + +// CHECK: def aaa_scope_test { +// CHECK-NEXT: int value = 10; +def aaa_scope_test: VarScopeTest<2, 3>; + multiclass Test { // Refer to a global variable, while inside a local scope like a multiclass. def _with_global_string { string s = myvar; }