diff --git a/llvm/include/llvm/TableGen/Record.h b/llvm/include/llvm/TableGen/Record.h index 73ed342a6101af..f490c7c80c60c0 100644 --- a/llvm/include/llvm/TableGen/Record.h +++ b/llvm/include/llvm/TableGen/Record.h @@ -1663,6 +1663,12 @@ class Record { /// the value is not the right type. Record *getValueAsDef(StringRef FieldName) const; + /// This method looks up the specified field and returns its value as a + /// Record, returning null if the field exists but is "uninitialized" + /// (i.e. set to `?`), and throwing an exception if the field does not + /// exist or if its value is not the right type. + Record *getValueAsOptionalDef(StringRef FieldName) const; + /// This method looks up the specified field and returns its /// value as a bit, throwing an exception if the field does not exist or if /// the value is not the right type. diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp index 835ef8c7141b9b..2ab7b98ca0305a 100644 --- a/llvm/lib/TableGen/Record.cpp +++ b/llvm/lib/TableGen/Record.cpp @@ -2277,6 +2277,21 @@ Record *Record::getValueAsDef(StringRef FieldName) const { FieldName + "' does not have a def initializer!"); } +Record *Record::getValueAsOptionalDef(StringRef FieldName) const { + const RecordVal *R = getValue(FieldName); + if (!R || !R->getValue()) + PrintFatalError(getLoc(), "Record `" + getName() + + "' does not have a field named `" + FieldName + "'!\n"); + + if (DefInit *DI = dyn_cast(R->getValue())) + return DI->getDef(); + if (isa(R->getValue())) + return nullptr; + PrintFatalError(getLoc(), "Record `" + getName() + "', field `" + + FieldName + "' does not have either a def initializer or '?'!"); +} + + bool Record::getValueAsBit(StringRef FieldName) const { const RecordVal *R = getValue(FieldName); if (!R || !R->getValue())