TableGen: promote "code" type from syntactic sugar.
It's being immediately converted to a "string", but being able to tell what
type the field was originally can be useful in backends.
llvm-svn: 274575
diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index c98c5d3..9ee7781 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -82,6 +82,7 @@
//===----------------------------------------------------------------------===//
BitRecTy BitRecTy::Shared;
+CodeRecTy CodeRecTy::Shared;
IntRecTy IntRecTy::Shared;
StringRecTy StringRecTy::Shared;
DagRecTy DagRecTy::Shared;
@@ -453,6 +454,14 @@
return BitsInit::get(NewBits);
}
+CodeInit *CodeInit::get(StringRef V) {
+ static StringMap<std::unique_ptr<CodeInit>> ThePool;
+
+ std::unique_ptr<CodeInit> &I = ThePool[V];
+ if (!I) I.reset(new CodeInit(V));
+ return I.get();
+}
+
StringInit *StringInit::get(StringRef V) {
static StringMap<std::unique_ptr<StringInit>> ThePool;
@@ -468,6 +477,13 @@
return nullptr;
}
+Init *CodeInit::convertInitializerTo(RecTy *Ty) const {
+ if (isa<CodeRecTy>(Ty))
+ return const_cast<CodeInit *>(this);
+
+ return nullptr;
+}
+
static void ProfileListInit(FoldingSetNodeID &ID,
ArrayRef<Init *> Range,
RecTy *EltTy) {
@@ -1158,6 +1174,12 @@
return nullptr;
}
+ if (isa<CodeRecTy>(Ty)) {
+ if (isa<CodeRecTy>(getType()))
+ return const_cast<TypedInit *>(this);
+ return nullptr;
+ }
+
if (isa<BitRecTy>(Ty)) {
// Accept variable if it is already of bit type!
if (isa<BitRecTy>(getType()))
@@ -1744,6 +1766,9 @@
if (StringInit *SI = dyn_cast<StringInit>(R->getValue()))
return SI->getValue();
+ if (CodeInit *CI = dyn_cast<CodeInit>(R->getValue()))
+ return CI->getValue();
+
PrintFatalError(getLoc(), "Record `" + getName() + "', field `" +
FieldName + "' does not have a string initializer!");
}
diff --git a/llvm/lib/TableGen/TGParser.cpp b/llvm/lib/TableGen/TGParser.cpp
index 4239628..34e9092 100644
--- a/llvm/lib/TableGen/TGParser.cpp
+++ b/llvm/lib/TableGen/TGParser.cpp
@@ -660,7 +660,7 @@
switch (Lex.getCode()) {
default: TokError("Unknown token when expecting a type"); return nullptr;
case tgtok::String: Lex.Lex(); return StringRecTy::get();
- case tgtok::Code: Lex.Lex(); return StringRecTy::get();
+ case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
case tgtok::Int: Lex.Lex(); return IntRecTy::get();
case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
@@ -1164,7 +1164,7 @@
break;
}
case tgtok::CodeFragment:
- R = StringInit::get(Lex.getCurStrVal());
+ R = CodeInit::get(Lex.getCurStrVal());
Lex.Lex();
break;
case tgtok::question: