Skip to content

Commit c69355e

Browse files
authored
[TableGen] Use getValueInit to reduce code duplication (NFC) (llvm#153167)
1 parent 89ea9df commit c69355e

File tree

1 file changed

+27
-59
lines changed

1 file changed

+27
-59
lines changed

llvm/lib/TableGen/Record.cpp

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3064,11 +3064,11 @@ const Init *Record::getValueInit(StringRef FieldName) const {
30643064
}
30653065

30663066
StringRef Record::getValueAsString(StringRef FieldName) const {
3067-
std::optional<StringRef> S = getValueAsOptionalString(FieldName);
3068-
if (!S)
3069-
PrintFatalError(getLoc(), "Record `" + getName() +
3070-
"' does not have a field named `" + FieldName + "'!\n");
3071-
return *S;
3067+
const Init *I = getValueInit(FieldName);
3068+
if (const auto *SI = dyn_cast<StringInit>(I))
3069+
return SI->getValue();
3070+
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
3071+
"' exists but does not have a string value");
30723072
}
30733073

30743074
std::optional<StringRef>
@@ -3088,24 +3088,16 @@ Record::getValueAsOptionalString(StringRef FieldName) const {
30883088
}
30893089

30903090
const BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const {
3091-
const RecordVal *R = getValue(FieldName);
3092-
if (!R || !R->getValue())
3093-
PrintFatalError(getLoc(), "Record `" + getName() +
3094-
"' does not have a field named `" + FieldName + "'!\n");
3095-
3096-
if (const auto *BI = dyn_cast<BitsInit>(R->getValue()))
3091+
const Init *I = getValueInit(FieldName);
3092+
if (const auto *BI = dyn_cast<BitsInit>(I))
30973093
return BI;
30983094
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
30993095
"' exists but does not have a bits value");
31003096
}
31013097

31023098
const ListInit *Record::getValueAsListInit(StringRef FieldName) const {
3103-
const RecordVal *R = getValue(FieldName);
3104-
if (!R || !R->getValue())
3105-
PrintFatalError(getLoc(), "Record `" + getName() +
3106-
"' does not have a field named `" + FieldName + "'!\n");
3107-
3108-
if (const auto *LI = dyn_cast<ListInit>(R->getValue()))
3099+
const Init *I = getValueInit(FieldName);
3100+
if (const auto *LI = dyn_cast<ListInit>(I))
31093101
return LI;
31103102
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + FieldName +
31113103
"' exists but does not have a list value");
@@ -3127,17 +3119,13 @@ Record::getValueAsListOfDefs(StringRef FieldName) const {
31273119
}
31283120

31293121
int64_t Record::getValueAsInt(StringRef FieldName) const {
3130-
const RecordVal *R = getValue(FieldName);
3131-
if (!R || !R->getValue())
3132-
PrintFatalError(getLoc(), "Record `" + getName() +
3133-
"' does not have a field named `" + FieldName + "'!\n");
3134-
3135-
if (const auto *II = dyn_cast<IntInit>(R->getValue()))
3122+
const Init *I = getValueInit(FieldName);
3123+
if (const auto *II = dyn_cast<IntInit>(I))
31363124
return II->getValue();
3137-
PrintFatalError(getLoc(), Twine("Record `") + getName() + "', field `" +
3138-
FieldName +
3139-
"' exists but does not have an int value: " +
3140-
R->getValue()->getAsString());
3125+
PrintFatalError(
3126+
getLoc(),
3127+
Twine("Record `") + getName() + "', field `" + FieldName +
3128+
"' exists but does not have an int value: " + I->getAsString());
31413129
}
31423130

31433131
std::vector<int64_t>
@@ -3173,67 +3161,47 @@ Record::getValueAsListOfStrings(StringRef FieldName) const {
31733161
}
31743162

31753163
const Record *Record::getValueAsDef(StringRef FieldName) const {
3176-
const RecordVal *R = getValue(FieldName);
3177-
if (!R || !R->getValue())
3178-
PrintFatalError(getLoc(), "Record `" + getName() +
3179-
"' does not have a field named `" + FieldName + "'!\n");
3180-
3181-
if (const auto *DI = dyn_cast<DefInit>(R->getValue()))
3164+
const Init *I = getValueInit(FieldName);
3165+
if (const auto *DI = dyn_cast<DefInit>(I))
31823166
return DI->getDef();
31833167
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
31843168
FieldName + "' does not have a def initializer!");
31853169
}
31863170

31873171
const Record *Record::getValueAsOptionalDef(StringRef FieldName) const {
3188-
const RecordVal *R = getValue(FieldName);
3189-
if (!R || !R->getValue())
3190-
PrintFatalError(getLoc(), "Record `" + getName() +
3191-
"' does not have a field named `" + FieldName + "'!\n");
3192-
3193-
if (const auto *DI = dyn_cast<DefInit>(R->getValue()))
3172+
const Init *I = getValueInit(FieldName);
3173+
if (const auto *DI = dyn_cast<DefInit>(I))
31943174
return DI->getDef();
3195-
if (isa<UnsetInit>(R->getValue()))
3175+
if (isa<UnsetInit>(I))
31963176
return nullptr;
31973177
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
31983178
FieldName + "' does not have either a def initializer or '?'!");
31993179
}
32003180

32013181
bool Record::getValueAsBit(StringRef FieldName) const {
3202-
const RecordVal *R = getValue(FieldName);
3203-
if (!R || !R->getValue())
3204-
PrintFatalError(getLoc(), "Record `" + getName() +
3205-
"' does not have a field named `" + FieldName + "'!\n");
3206-
3207-
if (const auto *BI = dyn_cast<BitInit>(R->getValue()))
3182+
const Init *I = getValueInit(FieldName);
3183+
if (const auto *BI = dyn_cast<BitInit>(I))
32083184
return BI->getValue();
32093185
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
32103186
FieldName + "' does not have a bit initializer!");
32113187
}
32123188

32133189
bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const {
3214-
const RecordVal *R = getValue(FieldName);
3215-
if (!R || !R->getValue())
3216-
PrintFatalError(getLoc(), "Record `" + getName() +
3217-
"' does not have a field named `" + FieldName.str() + "'!\n");
3218-
3219-
if (isa<UnsetInit>(R->getValue())) {
3190+
const Init *I = getValueInit(FieldName);
3191+
if (isa<UnsetInit>(I)) {
32203192
Unset = true;
32213193
return false;
32223194
}
32233195
Unset = false;
3224-
if (const auto *BI = dyn_cast<BitInit>(R->getValue()))
3196+
if (const auto *BI = dyn_cast<BitInit>(I))
32253197
return BI->getValue();
32263198
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
32273199
FieldName + "' does not have a bit initializer!");
32283200
}
32293201

32303202
const DagInit *Record::getValueAsDag(StringRef FieldName) const {
3231-
const RecordVal *R = getValue(FieldName);
3232-
if (!R || !R->getValue())
3233-
PrintFatalError(getLoc(), "Record `" + getName() +
3234-
"' does not have a field named `" + FieldName + "'!\n");
3235-
3236-
if (const auto *DI = dyn_cast<DagInit>(R->getValue()))
3203+
const Init *I = getValueInit(FieldName);
3204+
if (const auto *DI = dyn_cast<DagInit>(I))
32373205
return DI;
32383206
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
32393207
FieldName + "' does not have a dag initializer!");

0 commit comments

Comments
 (0)