[llvm] fd5d0a8 - [TableGen] Allow references to class template arguments in defvar
via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 13 20:17:13 PDT 2023
Author: wangpc
Date: 2023-04-14T11:12:35+08:00
New Revision: fd5d0a88dde007728004e43cb28f3c8c0be7b97c
URL: https://github.com/llvm/llvm-project/commit/fd5d0a88dde007728004e43cb28f3c8c0be7b97c
DIFF: https://github.com/llvm/llvm-project/commit/fd5d0a88dde007728004e43cb28f3c8c0be7b97c.diff
LOG: [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
Added:
Modified:
llvm/lib/TableGen/TGParser.cpp
llvm/lib/TableGen/TGParser.h
llvm/test/TableGen/defvar.td
Removed:
################################################################################
diff --git a/llvm/lib/TableGen/TGParser.cpp b/llvm/lib/TableGen/TGParser.cpp
index 6c52edc5dd413..14c529caa5947 100644
--- a/llvm/lib/TableGen/TGParser.cpp
+++ b/llvm/lib/TableGen/TGParser.cpp
@@ -3023,7 +3023,7 @@ bool TGParser::ParseBodyItem(Record *CurRec) {
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 @@ bool TGParser::ParseDefset() {
///
/// 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 @@ bool TGParser::ParseDefvar() {
if (!consume(tgtok::equal))
return TokError("expected '='");
- Init *Value = ParseValue(nullptr);
+ Init *Value = ParseValue(CurRec);
if (!Value)
return true;
diff --git a/llvm/lib/TableGen/TGParser.h b/llvm/lib/TableGen/TGParser.h
index f7271a5e79c32..63a8db0f4f6bc 100644
--- a/llvm/lib/TableGen/TGParser.h
+++ b/llvm/lib/TableGen/TGParser.h
@@ -231,7 +231,7 @@ class TGParser {
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/test/TableGen/defvar.td b/llvm/test/TableGen/defvar.td
index 4c39c37c8a3b8..2bf9fabf9eca5 100644
--- a/llvm/test/TableGen/defvar.td
+++ b/llvm/test/TableGen/defvar.td
@@ -18,6 +18,18 @@ defvar myvar = "foo";
defvar myvar = "another value";
#endif
+// These variables should be overrided by template arguments.
+defvar a = 2333;
+defvar b = 2333;
+class VarScopeTest<int a, int b> {
+ 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<int x> {
// Refer to a global variable, while inside a local scope like a multiclass.
def _with_global_string { string s = myvar; }
More information about the llvm-commits
mailing list