Add and use a new method
[oota-llvm.git] / support / tools / TableGen / Record.h
index 467f12a6ab368ba3a86735da11294427831ef21e..5c50a07651dd29faaeae643da184b468c6592959 100644 (file)
 #include <iostream>
 #include <cassert>
 
+// RecTy subclasses...
+class BitRecTy;
+class BitsRecTy;
+class IntRecTy;
+class StringRecTy;
+class ListRecTy;
+class CodeRecTy;
+class DagRecTy;
+class RecordRecTy;
+
+// Init subclasses...
 class Init;
 class UnsetInit;
 class BitInit;
@@ -23,10 +34,13 @@ class StringInit;
 class CodeInit;
 class ListInit;
 class DefInit;
+class DagInit;
 class TypedInit;
 class VarInit;
 class FieldInit;
 class VarBitInit;
+
+// Other classes...
 class Record;
 
 //===----------------------------------------------------------------------===//
@@ -36,6 +50,14 @@ class Record;
 struct RecTy {
   virtual ~RecTy() {}
 
+  virtual void print(std::ostream &OS) const = 0;
+  void dump() const;
+
+  /// typeIsConvertibleTo - Return true if all values of 'this' type can be
+  /// converted to the specified type.
+  virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
+
+public:   // These methods should only be called from subclasses of Init
   virtual Init *convertValue( UnsetInit *UI) { return 0; }
   virtual Init *convertValue(   BitInit *BI) { return 0; }
   virtual Init *convertValue(  BitsInit *BI) { return 0; }
@@ -45,6 +67,7 @@ struct RecTy {
   virtual Init *convertValue(  CodeInit *CI) { return 0; }
   virtual Init *convertValue(VarBitInit *VB) { return 0; }
   virtual Init *convertValue(   DefInit *DI) { return 0; }
+  virtual Init *convertValue(   DagInit *DI) { return 0; }
   virtual Init *convertValue( TypedInit *TI) { return 0; }
   virtual Init *convertValue(   VarInit *VI) {
     return convertValue((TypedInit*)VI);
@@ -53,8 +76,17 @@ struct RecTy {
     return convertValue((TypedInit*)FI);
   }
 
-  virtual void print(std::ostream &OS) const = 0;
-  void dump() const;
+public:   // These methods should only be called by subclasses of RecTy.
+  // baseClassOf - These virtual methods should be overloaded to return true iff
+  // all values of type 'RHS' can be converted to the 'this' type.
+  virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
+  virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
+  virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
+  virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
+  virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
+  virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
+  virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
+  virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
 };
 
 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
@@ -74,6 +106,13 @@ struct BitRecTy : public RecTy {
   Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
 
   void print(std::ostream &OS) const { OS << "bit"; }
+
+  bool typeIsConvertibleTo(const RecTy *RHS) const {
+    return RHS->baseClassOf(this);
+  }
+  virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
+  virtual bool baseClassOf(const BitsRecTy *RHS) const;
+  virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
 };
 
 
@@ -93,6 +132,15 @@ public:
   Init *convertValue(TypedInit *VI);
 
   void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; }
+
+  bool typeIsConvertibleTo(const RecTy *RHS) const {
+    return RHS->baseClassOf(this);
+  }
+  virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
+  virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
+  virtual bool baseClassOf(const BitsRecTy *RHS) const {
+    return RHS->Size == Size;
+  }
 };
 
 
@@ -101,10 +149,19 @@ public:
 struct IntRecTy : public RecTy {
   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
   Init *convertValue(IntInit *II) { return (Init*)II; }
+  Init *convertValue(BitInit *BI);
   Init *convertValue(BitsInit *BI);
   Init *convertValue(TypedInit *TI);
 
   void print(std::ostream &OS) const { OS << "int"; }
+
+  bool typeIsConvertibleTo(const RecTy *RHS) const {
+    return RHS->baseClassOf(this);
+  }
+
+  virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
+  virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
+  virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
 };
 
 /// StringRecTy - 'string' - Represent an string value
@@ -114,25 +171,37 @@ struct StringRecTy : public RecTy {
   Init *convertValue(StringInit *SI) { return (Init*)SI; }
   Init *convertValue(TypedInit *TI);
   void print(std::ostream &OS) const { OS << "string"; }
+
+  bool typeIsConvertibleTo(const RecTy *RHS) const {
+    return RHS->baseClassOf(this);
+  }
+
+  virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
 };
 
-/// ListRecTy - 'list<class>' - Represent a list defs, all of which must be
-/// derived from the specified class.
+/// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
+/// the specified type.
 ///
 class ListRecTy : public RecTy {
-  Record *Class;
+  RecTy *Ty;
 public:
-  ListRecTy(Record *C) : Class(C) {}
+  ListRecTy(RecTy *T) : Ty(T) {}
 
-  /// getElementClass - Return the class that the list contains.
-  ///
-  Record *getElementClass() const { return Class; }
+  RecTy *getElementType() const { return Ty; }
 
   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
   Init *convertValue(ListInit *LI);
   Init *convertValue(TypedInit *TI);
   
   void print(std::ostream &OS) const;
+
+  bool typeIsConvertibleTo(const RecTy *RHS) const {
+    return RHS->baseClassOf(this);
+  }
+
+  virtual bool baseClassOf(const ListRecTy *RHS) const {
+    return RHS->getElementType()->typeIsConvertibleTo(Ty); 
+  }
 };
 
 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
@@ -140,8 +209,29 @@ public:
 struct CodeRecTy : public RecTy {
   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
   Init *convertValue( CodeInit *CI) { return (Init*)CI; }
+  Init *convertValue(TypedInit *TI);
 
   void print(std::ostream &OS) const { OS << "code"; }
+
+  bool typeIsConvertibleTo(const RecTy *RHS) const {
+    return RHS->baseClassOf(this);
+  }
+  virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
+};
+
+/// DagRecTy - 'dag' - Represent a dag fragment
+///
+struct DagRecTy : public RecTy {
+  Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
+  Init *convertValue( DagInit *CI) { return (Init*)CI; }
+  Init *convertValue(TypedInit *TI);
+
+  void print(std::ostream &OS) const { OS << "dag"; }
+
+  bool typeIsConvertibleTo(const RecTy *RHS) const {
+    return RHS->baseClassOf(this);
+  }
+  virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
 };
 
 
@@ -160,6 +250,11 @@ public:
   Init *convertValue(TypedInit *VI); 
 
   void print(std::ostream &OS) const;
+
+  bool typeIsConvertibleTo(const RecTy *RHS) const {
+    return RHS->baseClassOf(this);
+  }
+  virtual bool baseClassOf(const RecordRecTy *RHS) const;
 };
 
 
@@ -335,6 +430,8 @@ class CodeInit : public Init {
 public:
   CodeInit(const std::string &V) : Value(V) {}
 
+  const std::string getValue() const { return Value; }
+
   virtual Init *convertInitializerTo(RecTy *Ty) {
     return Ty->convertValue(this);
   }
@@ -345,16 +442,16 @@ public:
 /// ListInit - [AL, AH, CL] - Represent a list of defs
 ///
 class ListInit : public Init {
-  std::vector<Record*> Records;
+  std::vector<Init*> Values;
 public:
-  ListInit(std::vector<Record*> &Rs) {
-    Records.swap(Rs);
+  ListInit(std::vector<Init*> &Vs) {
+    Values.swap(Vs);
   }
 
-  unsigned getSize() const { return Records.size(); }
-  Record  *getElement(unsigned i) const {
-    assert(i < Records.size() && "List element index out of range!");
-    return Records[i];
+  unsigned getSize() const { return Values.size(); }
+  Init *getElement(unsigned i) const {
+    assert(i < Values.size() && "List element index out of range!");
+    return Values[i];
   }
 
   virtual Init *convertInitializerTo(RecTy *Ty) {
@@ -480,11 +577,34 @@ public:
 
   virtual Init *resolveBitReference(Record &R, unsigned Bit);
 
+  virtual Init *resolveReferences(Record &R);
+
   virtual void print(std::ostream &OS) const {
     Rec->print(OS); OS << "." << FieldName;
   }
 };
 
+/// DagInit - (def a, b) - Represent a DAG tree value.  DAG inits are required
+/// to have Records for their first value, after that, any legal Init is
+/// possible.
+///
+class DagInit : public Init {
+  Record *NodeTypeDef;
+  std::vector<Init*> Args;
+public:
+  DagInit(Record *D, std::vector<Init*> &a) : NodeTypeDef(D) {
+    Args.swap(a);  // DESTRUCTIVELY take the arguments
+  }
+  
+  virtual Init *convertInitializerTo(RecTy *Ty) {
+    return Ty->convertValue(this);
+  }
+
+  Record *getNodeType() const { return NodeTypeDef; }
+  const std::vector<Init*> &getArgs() const { return Args; }
+
+  virtual void print(std::ostream &OS) const;
+};
 
 //===----------------------------------------------------------------------===//
 //  High-Level Classes
@@ -583,6 +703,13 @@ public:
     return false;
   }
 
+  bool isSubClassOf(const std::string &Name) const {
+    for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
+      if (SuperClasses[i]->getName() == Name)
+        return true;
+    return false;
+  }
+
   void addSuperClass(Record *R) {
     assert(!isSubClassOf(R) && "Already subclassing record!");
     SuperClasses.push_back(R);
@@ -599,12 +726,52 @@ public:
   // High-level methods useful to tablegen back-ends
   //
 
+  /// getValueInit - Return the initializer for a value with the specified name,
+  /// or throw an exception if the field does not exist.
+  ///
+  Init *getValueInit(const std::string &FieldName) const;
+
   /// getValueAsString - This method looks up the specified field and returns
   /// its value as a string, throwing an exception if the field does not exist
   /// or if the value is not a string.
   ///
   std::string getValueAsString(const std::string &FieldName) const;
 
+  /// getValueAsBitsInit - This method looks up the specified field and returns
+  /// its value as a BitsInit, throwing an exception if the field does not exist
+  /// or if the value is not the right type.
+  ///
+  BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
+
+  /// getValueAsListInit - This method looks up the specified field and returns
+  /// its value as a ListInit, throwing an exception if the field does not exist
+  /// or if the value is not the right type.
+  ///
+  ListInit *getValueAsListInit(const std::string &FieldName) const;
+
+  /// getValueAsDef - This method looks up the specified field and returns its
+  /// value as a Record, throwing an exception if the field does not exist or if
+  /// the value is not the right type.
+  ///
+  Record *getValueAsDef(const std::string &FieldName) const;
+
+  /// getValueAsBit - 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.
+  ///
+  bool getValueAsBit(const std::string &FieldName) const;
+
+  /// getValueAsInt - This method looks up the specified field and returns its
+  /// value as an int, throwing an exception if the field does not exist or if
+  /// the value is not the right type.
+  ///
+  int getValueAsInt(const std::string &FieldName) const;
+
+  /// getValueAsDag - This method looks up the specified field and returns its
+  /// value as an Dag, throwing an exception if the field does not exist or if
+  /// the value is not the right type.
+  ///
+  DagInit *getValueAsDag(const std::string &FieldName) const;
 };
 
 std::ostream &operator<<(std::ostream &OS, const Record &R);