[PATCH] D43557: TableGen: Allow implicit casting between string and code

Phabricator via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 22 07:29:13 PST 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL325799: TableGen: Allow implicit casting between string and code (authored by nha, committed by ).

Repository:
  rL LLVM

https://reviews.llvm.org/D43557

Files:
  llvm/trunk/include/llvm/TableGen/Record.h
  llvm/trunk/lib/TableGen/Record.cpp
  llvm/trunk/test/TableGen/code.td


Index: llvm/trunk/lib/TableGen/Record.cpp
===================================================================
--- llvm/trunk/lib/TableGen/Record.cpp
+++ llvm/trunk/lib/TableGen/Record.cpp
@@ -97,10 +97,20 @@
   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,13 +443,17 @@
 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;
 }
 
 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;
 }
Index: llvm/trunk/include/llvm/TableGen/Record.h
===================================================================
--- llvm/trunk/include/llvm/TableGen/Record.h
+++ llvm/trunk/include/llvm/TableGen/Record.h
@@ -141,6 +141,8 @@
   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 @@
   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
Index: llvm/trunk/test/TableGen/code.td
===================================================================
--- llvm/trunk/test/TableGen/code.td
+++ llvm/trunk/test/TableGen/code.td
@@ -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>;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43557.135423.patch
Type: text/x-patch
Size: 2618 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180222/07f2636e/attachment.bin>


More information about the llvm-commits mailing list