[PATCH] D148197: [TableGen] Allow references to class template arguments in defvar

Wang Pengcheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 13 00:10:11 PDT 2023


pcwang-thead created this revision.
pcwang-thead added reviewers: nhaehnle, craig.topper, tra, simon_tatham.
Herald added a subscriber: hiraditya.
Herald added a project: All.
pcwang-thead requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148197

Files:
  llvm/lib/TableGen/TGParser.cpp
  llvm/lib/TableGen/TGParser.h
  llvm/test/TableGen/defvar.td


Index: llvm/test/TableGen/defvar.td
===================================================================
--- llvm/test/TableGen/defvar.td
+++ 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<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; }
Index: llvm/lib/TableGen/TGParser.h
===================================================================
--- llvm/lib/TableGen/TGParser.h
+++ 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);
Index: llvm/lib/TableGen/TGParser.cpp
===================================================================
--- llvm/lib/TableGen/TGParser.cpp
+++ 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;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148197.513080.patch
Type: text/x-patch
Size: 2082 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230413/1720a11c/attachment.bin>


More information about the llvm-commits mailing list