[llvm-commits] CVS: llvm/utils/TableGen/Record.h Record.cpp
Chris Lattner
lattner at cs.uiuc.edu
Mon Aug 4 00:00:49 PDT 2003
Changes in directory llvm/utils/TableGen:
Record.h updated: 1.24 -> 1.25
Record.cpp updated: 1.18 -> 1.19
---
Log message:
Changes to allow lists of any type
---
Diffs of the changes:
Index: llvm/utils/TableGen/Record.h
diff -u llvm/utils/TableGen/Record.h:1.24 llvm/utils/TableGen/Record.h:1.25
--- llvm/utils/TableGen/Record.h:1.24 Fri Aug 1 20:27:37 2003
+++ llvm/utils/TableGen/Record.h Sun Aug 3 13:16:55 2003
@@ -14,6 +14,16 @@
#include <iostream>
#include <cassert>
+// RecTy subclasses...
+class BitRecTy;
+class BitsRecTy;
+class IntRecTy;
+class StringRecTy;
+class ListRecTy;
+class CodeRecTy;
+class RecordRecTy;
+
+// Init subclasses...
class Init;
class UnsetInit;
class BitInit;
@@ -27,6 +37,8 @@
class VarInit;
class FieldInit;
class VarBitInit;
+
+// Other classes...
class Record;
//===----------------------------------------------------------------------===//
@@ -36,6 +48,14 @@
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; }
@@ -53,8 +73,16 @@
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 RecordRecTy *RHS) const { return false; }
};
inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
@@ -74,6 +102,13 @@
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 +128,15 @@
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;
+ }
};
@@ -105,6 +149,14 @@
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 +166,37 @@
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.
@@ -142,6 +206,11 @@
Init *convertValue( CodeInit *CI) { return (Init*)CI; }
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; }
};
@@ -160,6 +229,11 @@
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;
};
@@ -347,16 +421,16 @@
/// 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) {
Index: llvm/utils/TableGen/Record.cpp
diff -u llvm/utils/TableGen/Record.cpp:1.18 llvm/utils/TableGen/Record.cpp:1.19
--- llvm/utils/TableGen/Record.cpp:1.18 Fri Aug 1 20:27:37 2003
+++ llvm/utils/TableGen/Record.cpp Sun Aug 3 13:16:55 2003
@@ -16,6 +16,10 @@
return BI->getBit(0);
}
+bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
+ return RHS->getNumBits() == 1;
+}
+
Init *BitRecTy::convertValue(IntInit *II) {
int Val = II->getValue();
if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
@@ -104,22 +108,27 @@
}
void ListRecTy::print(std::ostream &OS) const {
- OS << "list<" << Class->getName() << ">";
+ OS << "list<" << *Ty << ">";
}
Init *ListRecTy::convertValue(ListInit *LI) {
+ std::vector<Init*> Elements;
+
// Verify that all of the elements of the list are subclasses of the
- // appopriate class!
+ // appropriate class!
for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
- if (!LI->getElement(i)->isSubClassOf(Class))
+ if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
+ Elements.push_back(CI);
+ else
return 0;
- return LI;
+
+ return new ListInit(Elements);
}
Init *ListRecTy::convertValue(TypedInit *TI) {
// Ensure that TI is compatible with our class.
if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
- if (LRT->getElementClass() == getElementClass())
+ if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
return TI;
return 0;
}
@@ -144,6 +153,11 @@
return 0;
}
+bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
+ return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
+}
+
+
//===----------------------------------------------------------------------===//
// Initializer implementations
//===----------------------------------------------------------------------===//
@@ -263,9 +277,9 @@
void ListInit::print(std::ostream &OS) const {
OS << "[";
- for (unsigned i = 0, e = Records.size(); i != e; ++i) {
+ for (unsigned i = 0, e = Values.size(); i != e; ++i) {
if (i) OS << ", ";
- OS << Records[i]->getName();
+ OS << *Values[i];
}
OS << "]";
}
More information about the llvm-commits
mailing list