[llvm] r325799 - TableGen: Allow implicit casting between string and code
Nicolai Haehnle via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 22 07:27:03 PST 2018
Author: nha
Date: Thu Feb 22 07:27:03 2018
New Revision: 325799
URL: http://llvm.org/viewvc/llvm-project?rev=325799&view=rev
Log:
TableGen: Allow implicit casting between string and code
Summary:
Perhaps the distinction between the two should be removed entirely
in the long term, and the [{ ... }] syntax should just be a convenient
way of writing multi-line strings.
In the meantime, a lot of existing .td files are quite relaxed about
string vs. code, and this change allows switching on more consistent
type checks without breaking those.
Change-Id: If85e3e04469e41b58e2703b62ac0032d2711713c
Reviewers: arsenm, craig.topper, tra, MartinO
Subscribers: wdng, llvm-commits
Differential Revision: https://reviews.llvm.org/D43557
Added:
llvm/trunk/test/TableGen/code.td
Modified:
llvm/trunk/include/llvm/TableGen/Record.h
llvm/trunk/lib/TableGen/Record.cpp
Modified: llvm/trunk/include/llvm/TableGen/Record.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/TableGen/Record.h?rev=325799&r1=325798&r2=325799&view=diff
==============================================================================
--- llvm/trunk/include/llvm/TableGen/Record.h (original)
+++ llvm/trunk/include/llvm/TableGen/Record.h Thu Feb 22 07:27:03 2018
@@ -141,6 +141,8 @@ public:
static CodeRecTy *get() { return &Shared; }
std::string getAsString() const override { return "code"; }
+
+ bool typeIsConvertibleTo(const RecTy *RHS) const override;
};
/// 'int' - Represent an integer value of no particular size
@@ -176,6 +178,8 @@ public:
static StringRecTy *get() { return &Shared; }
std::string getAsString() const override;
+
+ bool typeIsConvertibleTo(const RecTy *RHS) const override;
};
/// 'list<Ty>' - Represent a list of values, all of which must be of
Modified: llvm/trunk/lib/TableGen/Record.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/Record.cpp?rev=325799&r1=325798&r2=325799&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/Record.cpp (original)
+++ llvm/trunk/lib/TableGen/Record.cpp Thu Feb 22 07:27:03 2018
@@ -97,10 +97,20 @@ bool IntRecTy::typeIsConvertibleTo(const
return kind==BitRecTyKind || kind==BitsRecTyKind || kind==IntRecTyKind;
}
+bool CodeRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
+ RecTyKind Kind = RHS->getRecTyKind();
+ return Kind == CodeRecTyKind || Kind == StringRecTyKind;
+}
+
std::string StringRecTy::getAsString() const {
return "string";
}
+bool StringRecTy::typeIsConvertibleTo(const RecTy *RHS) const {
+ RecTyKind Kind = RHS->getRecTyKind();
+ return Kind == StringRecTyKind || Kind == CodeRecTyKind;
+}
+
std::string ListRecTy::getAsString() const {
return "list<" + Ty->getAsString() + ">";
}
@@ -433,6 +443,8 @@ StringInit *StringInit::get(StringRef V)
Init *StringInit::convertInitializerTo(RecTy *Ty) const {
if (isa<StringRecTy>(Ty))
return const_cast<StringInit *>(this);
+ if (isa<CodeRecTy>(Ty))
+ return CodeInit::get(getValue());
return nullptr;
}
@@ -440,6 +452,8 @@ Init *StringInit::convertInitializerTo(R
Init *CodeInit::convertInitializerTo(RecTy *Ty) const {
if (isa<CodeRecTy>(Ty))
return const_cast<CodeInit *>(this);
+ if (isa<StringRecTy>(Ty))
+ return StringInit::get(getValue());
return nullptr;
}
Added: llvm/trunk/test/TableGen/code.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/TableGen/code.td?rev=325799&view=auto
==============================================================================
--- llvm/trunk/test/TableGen/code.td (added)
+++ llvm/trunk/test/TableGen/code.td Thu Feb 22 07:27:03 2018
@@ -0,0 +1,23 @@
+// RUN: llvm-tblgen %s | FileCheck %s
+// XFAIL: vg_leak
+
+// CHECK: --- Defs ---
+
+// TODO: Both of these should result in CodeInits, i.e. print [{...}].
+// CHECK: def A0 {
+// CHECK: code Code = "Simple";
+// CHECK: }
+
+// CHECK: def B0 {
+// CHECK: code Code = "With paste 7";
+// CHECK: }
+
+class A<code c> {
+ code Code = c;
+}
+
+def A0 : A<"Simple">;
+
+class B<int i> : A<"With paste " # i>;
+
+def B0 : B<7>;
More information about the llvm-commits
mailing list