[llvm] [TableGen] Add `!instances` operator to get defined records (PR #129680)
Rahul Joshi via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 24 08:41:21 PDT 2025
================
@@ -1455,6 +1455,49 @@ const Init *TGParser::ParseOperation(Record *CurRec, const RecTy *ItemType) {
return (ExistsOpInit::get(Type, Expr))->Fold(CurRec);
}
+ case tgtok::XInstances: {
+ // Value ::= !instances '<' Type '>' '(' Regex? ')'
+ Lex.Lex(); // eat the operation.
+
+ const RecTy *Type = ParseOperatorType();
+ if (!Type)
+ return nullptr;
+
+ if (!consume(tgtok::l_paren)) {
+ TokError("expected '(' after type of !instances");
+ return nullptr;
+ }
+
+ // The Regex can be optional.
+ const Init *Regex;
+ if (Lex.getCode() != tgtok::r_paren) {
+ SMLoc RegexLoc = Lex.getLoc();
+ Regex = ParseValue(CurRec);
+
+ const auto *RegexType = dyn_cast<TypedInit>(Regex);
+ if (!RegexType) {
+ Error(RegexLoc, "expected string type argument in !instances operator");
+ return nullptr;
+ }
+
+ const auto *SType = dyn_cast<StringRecTy>(RegexType->getType());
+ if (!SType) {
+ Error(RegexLoc, "expected string type argument in !instances operator");
+ return nullptr;
+ }
+ } else {
+ // Use wildcard when Regex is not specified.
+ Regex = StringInit::get(Records, ".*");
----------------
jurahul wrote:
wondering if this case can he handled inside the Fold method instead and we use a null StringInit in the constructor. The only reason is that get `InstancesOpInit::getAsString` in this case will print something different than what was in the input (which maybe ok?).
https://github.com/llvm/llvm-project/pull/129680
More information about the llvm-commits
mailing list