[TableGen] Use getValueInit to reduce code duplication (NFC) (#153167)

This commit is contained in:
Sergei Barannikov 2025-08-12 17:18:00 +03:00 committed by GitHub
parent 89ea9df6a2
commit c69355e7d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3064,11 +3064,11 @@ const Init *Record::getValueInit(StringRef FieldName) const {
} }
StringRef Record::getValueAsString(StringRef FieldName) const { StringRef Record::getValueAsString(StringRef FieldName) const {
std::optional<StringRef> S = getValueAsOptionalString(FieldName); const Init *I = getValueInit(FieldName);
if (!S) if (const auto *SI = dyn_cast<StringInit>(I))
PrintFatalError(getLoc(), "Record `" + getName() + return SI->getValue();
"' does not have a field named `" + FieldName + "'!\n"); PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
return *S; "' exists but does not have a string value");
} }
std::optional<StringRef> std::optional<StringRef>
@ -3088,24 +3088,16 @@ Record::getValueAsOptionalString(StringRef FieldName) const {
} }
const BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const { const BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
const RecordVal *R = getValue(FieldName); const Init *I = getValueInit(FieldName);
if (!R || !R->getValue()) if (const auto *BI = dyn_cast<BitsInit>(I))
PrintFatalError(getLoc(), "Record `" + getName() +
"' does not have a field named `" + FieldName + "'!\n");
if (const auto *BI = dyn_cast<BitsInit>(R->getValue()))
return BI; return BI;
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName + PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
"' exists but does not have a bits value"); "' exists but does not have a bits value");
} }
const ListInit *Record::getValueAsListInit(StringRef FieldName) const { const ListInit *Record::getValueAsListInit(StringRef FieldName) const {
const RecordVal *R = getValue(FieldName); const Init *I = getValueInit(FieldName);
if (!R || !R->getValue()) if (const auto *LI = dyn_cast<ListInit>(I))
PrintFatalError(getLoc(), "Record `" + getName() +
"' does not have a field named `" + FieldName + "'!\n");
if (const auto *LI = dyn_cast<ListInit>(R->getValue()))
return LI; return LI;
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName + PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
"' exists but does not have a list value"); "' exists but does not have a list value");
@ -3127,17 +3119,13 @@ Record::getValueAsListOfDefs(StringRef FieldName) const {
} }
int64_t Record::getValueAsInt(StringRef FieldName) const { int64_t Record::getValueAsInt(StringRef FieldName) const {
const RecordVal *R = getValue(FieldName); const Init *I = getValueInit(FieldName);
if (!R || !R->getValue()) if (const auto *II = dyn_cast<IntInit>(I))
PrintFatalError(getLoc(), "Record `" + getName() +
"' does not have a field named `" + FieldName + "'!\n");
if (const auto *II = dyn_cast<IntInit>(R->getValue()))
return II->getValue(); return II->getValue();
PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" + PrintFatalError(
FieldName + getLoc(),
"' exists but does not have an int value: " + Twine("Record `") + getName() + "', field `" + FieldName +
R->getValue()->getAsString()); "' exists but does not have an int value: " + I->getAsString());
} }
std::vector<int64_t> std::vector<int64_t>
@ -3173,67 +3161,47 @@ Record::getValueAsListOfStrings(StringRef FieldName) const {
} }
const Record *Record::getValueAsDef(StringRef FieldName) const { const Record *Record::getValueAsDef(StringRef FieldName) const {
const RecordVal *R = getValue(FieldName); const Init *I = getValueInit(FieldName);
if (!R || !R->getValue()) if (const auto *DI = dyn_cast<DefInit>(I))
PrintFatalError(getLoc(), "Record `" + getName() +
"' does not have a field named `" + FieldName + "'!\n");
if (const auto *DI = dyn_cast<DefInit>(R->getValue()))
return DI->getDef(); return DI->getDef();
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
FieldName + "' does not have a def initializer!"); FieldName + "' does not have a def initializer!");
} }
const Record *Record::getValueAsOptionalDef(StringRef FieldName) const { const Record *Record::getValueAsOptionalDef(StringRef FieldName) const {
const RecordVal *R = getValue(FieldName); const Init *I = getValueInit(FieldName);
if (!R || !R->getValue()) if (const auto *DI = dyn_cast<DefInit>(I))
PrintFatalError(getLoc(), "Record `" + getName() +
"' does not have a field named `" + FieldName + "'!\n");
if (const auto *DI = dyn_cast<DefInit>(R->getValue()))
return DI->getDef(); return DI->getDef();
if (isa<UnsetInit>(R->getValue())) if (isa<UnsetInit>(I))
return nullptr; return nullptr;
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
FieldName + "' does not have either a def initializer or '?'!"); FieldName + "' does not have either a def initializer or '?'!");
} }
bool Record::getValueAsBit(StringRef FieldName) const { bool Record::getValueAsBit(StringRef FieldName) const {
const RecordVal *R = getValue(FieldName); const Init *I = getValueInit(FieldName);
if (!R || !R->getValue()) if (const auto *BI = dyn_cast<BitInit>(I))
PrintFatalError(getLoc(), "Record `" + getName() +
"' does not have a field named `" + FieldName + "'!\n");
if (const auto *BI = dyn_cast<BitInit>(R->getValue()))
return BI->getValue(); return BI->getValue();
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
FieldName + "' does not have a bit initializer!"); FieldName + "' does not have a bit initializer!");
} }
bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const { bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
const RecordVal *R = getValue(FieldName); const Init *I = getValueInit(FieldName);
if (!R || !R->getValue()) if (isa<UnsetInit>(I)) {
PrintFatalError(getLoc(), "Record `" + getName() +
"' does not have a field named `" + FieldName.str() + "'!\n");
if (isa<UnsetInit>(R->getValue())) {
Unset = true; Unset = true;
return false; return false;
} }
Unset = false; Unset = false;
if (const auto *BI = dyn_cast<BitInit>(R->getValue())) if (const auto *BI = dyn_cast<BitInit>(I))
return BI->getValue(); return BI->getValue();
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
FieldName + "' does not have a bit initializer!"); FieldName + "' does not have a bit initializer!");
} }
const DagInit *Record::getValueAsDag(StringRef FieldName) const { const DagInit *Record::getValueAsDag(StringRef FieldName) const {
const RecordVal *R = getValue(FieldName); const Init *I = getValueInit(FieldName);
if (!R || !R->getValue()) if (const auto *DI = dyn_cast<DagInit>(I))
PrintFatalError(getLoc(), "Record `" + getName() +
"' does not have a field named `" + FieldName + "'!\n");
if (const auto *DI = dyn_cast<DagInit>(R->getValue()))
return DI; return DI;
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
FieldName + "' does not have a dag initializer!"); FieldName + "' does not have a dag initializer!");