summaryrefslogtreecommitdiffstats
path: root/llvm/lib/AsmParser/LLParser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/AsmParser/LLParser.cpp')
-rw-r--r--llvm/lib/AsmParser/LLParser.cpp89
1 files changed, 87 insertions, 2 deletions
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index f94b616e596..9bc34f927e8 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -3492,6 +3492,39 @@ template <class FieldTy> struct MDFieldImpl {
: Val(std::move(Default)), Seen(false) {}
};
+/// Structure to represent an optional metadata field that
+/// can be of either type (A or B) and encapsulates the
+/// MD<typeofA>Field and MD<typeofB>Field structs, so not
+/// to reimplement the specifics for representing each Field.
+template <class FieldTypeA, class FieldTypeB> struct MDEitherFieldImpl {
+ typedef MDEitherFieldImpl<FieldTypeA, FieldTypeB> ImplTy;
+ FieldTypeA A;
+ FieldTypeB B;
+ bool Seen;
+
+ enum {
+ IsInvalid = 0,
+ IsTypeA = 1,
+ IsTypeB = 2
+ } WhatIs;
+
+ void assign(FieldTypeA A) {
+ Seen = true;
+ this->A = std::move(A);
+ WhatIs = IsTypeA;
+ }
+
+ void assign(FieldTypeB B) {
+ Seen = true;
+ this->B = std::move(B);
+ WhatIs = IsTypeB;
+ }
+
+ explicit MDEitherFieldImpl(FieldTypeA DefaultA, FieldTypeB DefaultB)
+ : A(std::move(DefaultA)), B(std::move(DefaultB)), Seen(false),
+ WhatIs(IsInvalid) {}
+};
+
struct MDUnsignedField : public MDFieldImpl<uint64_t> {
uint64_t Max;
@@ -3582,6 +3615,26 @@ struct ChecksumKindField : public MDFieldImpl<DIFile::ChecksumKind> {
ChecksumKindField(DIFile::ChecksumKind CSKind) : ImplTy(CSKind) {}
};
+struct MDSignedOrMDField : MDEitherFieldImpl<MDSignedField, MDField> {
+ MDSignedOrMDField(int64_t Default = 0, bool AllowNull = true)
+ : ImplTy(MDSignedField(Default), MDField(AllowNull)) {}
+
+ MDSignedOrMDField(int64_t Default, int64_t Min, int64_t Max,
+ bool AllowNull = true)
+ : ImplTy(MDSignedField(Default, Min, Max), MDField(AllowNull)) {}
+
+ bool isMDSignedField() const { return WhatIs == IsTypeA; }
+ bool isMDField() const { return WhatIs == IsTypeB; }
+ int64_t getMDSignedValue() const {
+ assert(isMDSignedField() && "Wrong field type");
+ return A.Val;
+ }
+ Metadata *getMDFieldValue() const {
+ assert(isMDField() && "Wrong field type");
+ return B.Val;
+ }
+};
+
} // end anonymous namespace
namespace llvm {
@@ -3836,6 +3889,29 @@ bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDField &Result) {
}
template <>
+bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
+ MDSignedOrMDField &Result) {
+ // Try to parse a signed int.
+ if (Lex.getKind() == lltok::APSInt) {
+ MDSignedField Res = Result.A;
+ if (!ParseMDField(Loc, Name, Res)) {
+ Result.assign(Res);
+ return false;
+ }
+ return true;
+ }
+
+ // Otherwise, try to parse as an MDField.
+ MDField Res = Result.B;
+ if (!ParseMDField(Loc, Name, Res)) {
+ Result.assign(Res);
+ return false;
+ }
+
+ return true;
+}
+
+template <>
bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDStringField &Result) {
LocTy ValueLoc = Lex.getLoc();
std::string S;
@@ -3979,14 +4055,23 @@ bool LLParser::ParseGenericDINode(MDNode *&Result, bool IsDistinct) {
/// ParseDISubrange:
/// ::= !DISubrange(count: 30, lowerBound: 2)
+/// ::= !DISubrange(count: !node, lowerBound: 2)
bool LLParser::ParseDISubrange(MDNode *&Result, bool IsDistinct) {
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
- REQUIRED(count, MDSignedField, (-1, -1, INT64_MAX)); \
+ REQUIRED(count, MDSignedOrMDField, (-1, -1, INT64_MAX, false)); \
OPTIONAL(lowerBound, MDSignedField, );
PARSE_MD_FIELDS();
#undef VISIT_MD_FIELDS
- Result = GET_OR_DISTINCT(DISubrange, (Context, count.Val, lowerBound.Val));
+ if (count.isMDSignedField())
+ Result = GET_OR_DISTINCT(
+ DISubrange, (Context, count.getMDSignedValue(), lowerBound.Val));
+ else if (count.isMDField())
+ Result = GET_OR_DISTINCT(
+ DISubrange, (Context, count.getMDFieldValue(), lowerBound.Val));
+ else
+ return true;
+
return false;
}
OpenPOWER on IntegriCloud