ab485553c0c1c38e3a3dd343fa6d0cd6e0edb8be
[oota-llvm.git] / support / tools / TableGen / Record.h
1 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
2 //
3 // This file defines the main TableGen data structures, including the TableGen
4 // types, values, and high-level data structures.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef RECORD_H
9 #define RECORD_H
10
11 #include <string>
12 #include <vector>
13 #include <map>
14 #include <iostream>
15 #include <cassert>
16
17 // RecTy subclasses...
18 class BitRecTy;
19 class BitsRecTy;
20 class IntRecTy;
21 class StringRecTy;
22 class ListRecTy;
23 class CodeRecTy;
24 class DagRecTy;
25 class RecordRecTy;
26
27 // Init subclasses...
28 class Init;
29 class UnsetInit;
30 class BitInit;
31 class BitsInit;
32 class IntInit;
33 class StringInit;
34 class CodeInit;
35 class ListInit;
36 class DefInit;
37 class DagInit;
38 class TypedInit;
39 class VarInit;
40 class FieldInit;
41 class VarBitInit;
42
43 // Other classes...
44 class Record;
45
46 //===----------------------------------------------------------------------===//
47 //  Type Classes
48 //===----------------------------------------------------------------------===//
49
50 struct RecTy {
51   virtual ~RecTy() {}
52
53   virtual void print(std::ostream &OS) const = 0;
54   void dump() const;
55
56   /// typeIsConvertibleTo - Return true if all values of 'this' type can be
57   /// converted to the specified type.
58   virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
59
60 public:   // These methods should only be called from subclasses of Init
61   virtual Init *convertValue( UnsetInit *UI) { return 0; }
62   virtual Init *convertValue(   BitInit *BI) { return 0; }
63   virtual Init *convertValue(  BitsInit *BI) { return 0; }
64   virtual Init *convertValue(   IntInit *II) { return 0; }
65   virtual Init *convertValue(StringInit *SI) { return 0; }
66   virtual Init *convertValue(  ListInit *LI) { return 0; }
67   virtual Init *convertValue(  CodeInit *CI) { return 0; }
68   virtual Init *convertValue(VarBitInit *VB) { return 0; }
69   virtual Init *convertValue(   DefInit *DI) { return 0; }
70   virtual Init *convertValue(   DagInit *DI) { return 0; }
71   virtual Init *convertValue( TypedInit *TI) { return 0; }
72   virtual Init *convertValue(   VarInit *VI) {
73     return convertValue((TypedInit*)VI);
74   }
75   virtual Init *convertValue( FieldInit *FI) {
76     return convertValue((TypedInit*)FI);
77   }
78
79 public:   // These methods should only be called by subclasses of RecTy.
80   // baseClassOf - These virtual methods should be overloaded to return true iff
81   // all values of type 'RHS' can be converted to the 'this' type.
82   virtual bool baseClassOf(const BitRecTy    *RHS) const { return false; }
83   virtual bool baseClassOf(const BitsRecTy   *RHS) const { return false; }
84   virtual bool baseClassOf(const IntRecTy    *RHS) const { return false; }
85   virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
86   virtual bool baseClassOf(const ListRecTy   *RHS) const { return false; }
87   virtual bool baseClassOf(const CodeRecTy   *RHS) const { return false; }
88   virtual bool baseClassOf(const DagRecTy    *RHS) const { return false; }
89   virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
90 };
91
92 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
93   Ty.print(OS);
94   return OS;
95 }
96
97
98 /// BitRecTy - 'bit' - Represent a single bit
99 ///
100 struct BitRecTy : public RecTy {
101   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
102   Init *convertValue(BitInit *BI) { return (Init*)BI; }
103   Init *convertValue(BitsInit *BI);
104   Init *convertValue(IntInit *II);
105   Init *convertValue(TypedInit *VI);
106   Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
107
108   void print(std::ostream &OS) const { OS << "bit"; }
109
110   bool typeIsConvertibleTo(const RecTy *RHS) const {
111     return RHS->baseClassOf(this);
112   }
113   virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
114   virtual bool baseClassOf(const BitsRecTy *RHS) const;
115   virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
116 };
117
118
119 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
120 ///
121 class BitsRecTy : public RecTy {
122   unsigned Size;
123 public:
124   BitsRecTy(unsigned Sz) : Size(Sz) {}
125
126   unsigned getNumBits() const { return Size; }
127
128   Init *convertValue(UnsetInit *UI);
129   Init *convertValue(BitInit *UI);
130   Init *convertValue(BitsInit *BI);
131   Init *convertValue(IntInit *II);
132   Init *convertValue(TypedInit *VI);
133
134   void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; }
135
136   bool typeIsConvertibleTo(const RecTy *RHS) const {
137     return RHS->baseClassOf(this);
138   }
139   virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
140   virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
141   virtual bool baseClassOf(const BitsRecTy *RHS) const {
142     return RHS->Size == Size;
143   }
144 };
145
146
147 /// IntRecTy - 'int' - Represent an integer value of no particular size
148 ///
149 struct IntRecTy : public RecTy {
150   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
151   Init *convertValue(IntInit *II) { return (Init*)II; }
152   Init *convertValue(BitInit *BI);
153   Init *convertValue(BitsInit *BI);
154   Init *convertValue(TypedInit *TI);
155
156   void print(std::ostream &OS) const { OS << "int"; }
157
158   bool typeIsConvertibleTo(const RecTy *RHS) const {
159     return RHS->baseClassOf(this);
160   }
161
162   virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
163   virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
164   virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
165 };
166
167 /// StringRecTy - 'string' - Represent an string value
168 ///
169 struct StringRecTy : public RecTy {
170   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
171   Init *convertValue(StringInit *SI) { return (Init*)SI; }
172   Init *convertValue(TypedInit *TI);
173   void print(std::ostream &OS) const { OS << "string"; }
174
175   bool typeIsConvertibleTo(const RecTy *RHS) const {
176     return RHS->baseClassOf(this);
177   }
178
179   virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
180 };
181
182 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
183 /// the specified type.
184 ///
185 class ListRecTy : public RecTy {
186   RecTy *Ty;
187 public:
188   ListRecTy(RecTy *T) : Ty(T) {}
189
190   RecTy *getElementType() const { return Ty; }
191
192   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
193   Init *convertValue(ListInit *LI);
194   Init *convertValue(TypedInit *TI);
195   
196   void print(std::ostream &OS) const;
197
198   bool typeIsConvertibleTo(const RecTy *RHS) const {
199     return RHS->baseClassOf(this);
200   }
201
202   virtual bool baseClassOf(const ListRecTy *RHS) const {
203     return RHS->getElementType()->typeIsConvertibleTo(Ty); 
204   }
205 };
206
207 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
208 ///
209 struct CodeRecTy : public RecTy {
210   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
211   Init *convertValue( CodeInit *CI) { return (Init*)CI; }
212   Init *convertValue(TypedInit *TI);
213
214   void print(std::ostream &OS) const { OS << "code"; }
215
216   bool typeIsConvertibleTo(const RecTy *RHS) const {
217     return RHS->baseClassOf(this);
218   }
219   virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
220 };
221
222 /// DagRecTy - 'dag' - Represent a dag fragment
223 ///
224 struct DagRecTy : public RecTy {
225   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
226   Init *convertValue( DagInit *CI) { return (Init*)CI; }
227   Init *convertValue(TypedInit *TI);
228
229   void print(std::ostream &OS) const { OS << "dag"; }
230
231   bool typeIsConvertibleTo(const RecTy *RHS) const {
232     return RHS->baseClassOf(this);
233   }
234   virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
235 };
236
237
238 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
239 /// (R32 X = EAX).
240 ///
241 class RecordRecTy : public RecTy {
242   Record *Rec;
243 public:
244   RecordRecTy(Record *R) : Rec(R) {}
245
246   Record *getRecord() const { return Rec; }
247
248   Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
249   Init *convertValue(  DefInit *DI);
250   Init *convertValue(TypedInit *VI); 
251
252   void print(std::ostream &OS) const;
253
254   bool typeIsConvertibleTo(const RecTy *RHS) const {
255     return RHS->baseClassOf(this);
256   }
257   virtual bool baseClassOf(const RecordRecTy *RHS) const;
258 };
259
260
261
262 //===----------------------------------------------------------------------===//
263 //  Initializer Classes
264 //===----------------------------------------------------------------------===//
265
266 struct Init {
267   virtual ~Init() {}
268
269   /// isComplete - This virtual method should be overridden by values that may
270   /// not be completely specified yet.
271   virtual bool isComplete() const { return true; }
272
273   /// print - Print out this value.
274   virtual void print(std::ostream &OS) const = 0;
275
276   /// dump - Debugging method that may be called through a debugger, just
277   /// invokes print on cerr.
278   void dump() const;
279
280   /// convertInitializerTo - This virtual function is a simple call-back
281   /// function that should be overridden to call the appropriate
282   /// RecTy::convertValue method.
283   ///
284   virtual Init *convertInitializerTo(RecTy *Ty) = 0;
285
286   /// convertInitializerBitRange - This method is used to implement the bitrange
287   /// selection operator.  Given an initializer, it selects the specified bits
288   /// out, returning them as a new init of bits type.  If it is not legal to use
289   /// the bit subscript operator on this initializer, return null.
290   ///
291   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
292     return 0;
293   }
294
295   /// getFieldType - This method is used to implement the FieldInit class.
296   /// Implementors of this method should return the type of the named field if
297   /// they are of record type.
298   ///
299   virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
300
301   /// getFieldInit - This method complements getFieldType to return the
302   /// initializer for the specified field.  If getFieldType returns non-null
303   /// this method should return non-null, otherwise it returns null.
304   ///
305   virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
306     return 0;
307   }
308
309   /// resolveReferences - This method is used by classes that refer to other
310   /// variables which may not be defined at the time they expression is formed.
311   /// If a value is set for the variable later, this method will be called on
312   /// users of the value to allow the value to propagate out.
313   ///
314   virtual Init *resolveReferences(Record &R) { return this; }
315 };
316
317 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
318   I.print(OS); return OS;
319 }
320
321
322 /// UnsetInit - ? - Represents an uninitialized value
323 ///
324 struct UnsetInit : public Init {
325   virtual Init *convertInitializerTo(RecTy *Ty) {
326     return Ty->convertValue(this);
327   }
328
329   virtual bool isComplete() const { return false; }
330   virtual void print(std::ostream &OS) const { OS << "?"; }
331 };
332
333
334 /// BitInit - true/false - Represent a concrete initializer for a bit.
335 ///
336 class BitInit : public Init {
337   bool Value;
338 public:
339   BitInit(bool V) : Value(V) {}
340
341   bool getValue() const { return Value; }
342
343   virtual Init *convertInitializerTo(RecTy *Ty) {
344     return Ty->convertValue(this);
345   }
346
347   virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
348 };
349
350 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
351 /// It contains a vector of bits, whose size is determined by the type.
352 ///
353 class BitsInit : public Init {
354   std::vector<Init*> Bits;
355 public:
356   BitsInit(unsigned Size) : Bits(Size) {}
357
358   unsigned getNumBits() const { return Bits.size(); }
359
360   Init *getBit(unsigned Bit) const {
361     assert(Bit < Bits.size() && "Bit index out of range!");
362     return Bits[Bit];
363   }
364   void setBit(unsigned Bit, Init *V) {
365     assert(Bit < Bits.size() && "Bit index out of range!");
366     assert(Bits[Bit] == 0 && "Bit already set!");
367     Bits[Bit] = V;
368   }
369
370   virtual Init *convertInitializerTo(RecTy *Ty) {
371     return Ty->convertValue(this);
372   }
373   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
374
375   virtual bool isComplete() const {
376     for (unsigned i = 0; i != getNumBits(); ++i)
377       if (!getBit(i)->isComplete()) return false;
378     return true;
379   }
380   virtual void print(std::ostream &OS) const;
381
382   virtual Init *resolveReferences(Record &R);
383
384   // printXX - Print this bitstream with the specified format, returning true if
385   // it is not possible.
386   bool printInHex(std::ostream &OS) const;
387   bool printAsVariable(std::ostream &OS) const;
388   bool printAsUnset(std::ostream &OS) const;
389 };
390
391
392 /// IntInit - 7 - Represent an initalization by a literal integer value.
393 ///
394 class IntInit : public Init {
395   int Value;
396 public:
397   IntInit(int V) : Value(V) {}
398
399   int getValue() const { return Value; }
400
401   virtual Init *convertInitializerTo(RecTy *Ty) {
402     return Ty->convertValue(this);
403   }
404   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
405
406   virtual void print(std::ostream &OS) const { OS << Value; }
407 };
408
409
410 /// StringInit - "foo" - Represent an initialization by a string value.
411 ///
412 class StringInit : public Init {
413   std::string Value;
414 public:
415   StringInit(const std::string &V) : Value(V) {}
416
417   const std::string &getValue() const { return Value; }
418
419   virtual Init *convertInitializerTo(RecTy *Ty) {
420     return Ty->convertValue(this);
421   }
422
423   virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
424 };
425
426 /// CodeInit - "[{...}]" - Represent a code fragment.
427 ///
428 class CodeInit : public Init {
429   std::string Value;
430 public:
431   CodeInit(const std::string &V) : Value(V) {}
432
433   const std::string getValue() const { return Value; }
434
435   virtual Init *convertInitializerTo(RecTy *Ty) {
436     return Ty->convertValue(this);
437   }
438
439   virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; }
440 };
441
442 /// ListInit - [AL, AH, CL] - Represent a list of defs
443 ///
444 class ListInit : public Init {
445   std::vector<Init*> Values;
446 public:
447   ListInit(std::vector<Init*> &Vs) {
448     Values.swap(Vs);
449   }
450
451   unsigned getSize() const { return Values.size(); }
452   Init *getElement(unsigned i) const {
453     assert(i < Values.size() && "List element index out of range!");
454     return Values[i];
455   }
456
457   virtual Init *convertInitializerTo(RecTy *Ty) {
458     return Ty->convertValue(this);
459   }
460
461   virtual void print(std::ostream &OS) const;
462 };
463
464
465 /// TypedInit - This is the common super-class of types that have a specific,
466 /// explicit, type.
467 ///
468 class TypedInit : public Init {
469   RecTy *Ty;
470 public:  
471   TypedInit(RecTy *T) : Ty(T) {}
472
473   RecTy *getType() const { return Ty; }
474
475   /// resolveBitReference - This method is used to implement
476   /// VarBitInit::resolveReferences.  If the bit is able to be resolved, we
477   /// simply return the resolved value, otherwise we return this.
478   ///
479   virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
480 };
481
482 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
483 ///
484 class VarInit : public TypedInit {
485   std::string VarName;
486 public:
487   VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
488   
489   virtual Init *convertInitializerTo(RecTy *Ty) {
490     return Ty->convertValue(this);
491   }
492
493   const std::string &getName() const { return VarName; }
494
495   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
496
497   virtual Init *resolveBitReference(Record &R, unsigned Bit);
498
499   virtual RecTy *getFieldType(const std::string &FieldName) const;
500   virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
501
502   /// resolveReferences - This method is used by classes that refer to other
503   /// variables which may not be defined at the time they expression is formed.
504   /// If a value is set for the variable later, this method will be called on
505   /// users of the value to allow the value to propagate out.
506   ///
507   virtual Init *resolveReferences(Record &R);
508   
509   virtual void print(std::ostream &OS) const { OS << VarName; }
510 };
511
512
513 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
514 ///
515 class VarBitInit : public Init {
516   TypedInit *TI;
517   unsigned Bit;
518 public:
519   VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
520     assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
521            ((BitsRecTy*)T->getType())->getNumBits() > B &&
522            "Illegal VarBitInit expression!");
523   }
524
525   virtual Init *convertInitializerTo(RecTy *Ty) {
526     return Ty->convertValue(this);
527   }
528
529   TypedInit *getVariable() const { return TI; }
530   unsigned getBitNum() const { return Bit; }
531   
532   virtual void print(std::ostream &OS) const {
533     TI->print(OS); OS << "{" << Bit << "}";
534   }
535   virtual Init *resolveReferences(Record &R);
536 };
537
538
539 /// DefInit - AL - Represent a reference to a 'def' in the description
540 ///
541 class DefInit : public Init {
542   Record *Def;
543 public:
544   DefInit(Record *D) : Def(D) {}
545   
546   virtual Init *convertInitializerTo(RecTy *Ty) {
547     return Ty->convertValue(this);
548   }
549
550   Record *getDef() const { return Def; }
551
552   //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
553
554   virtual RecTy *getFieldType(const std::string &FieldName) const;
555   virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
556   
557   virtual void print(std::ostream &OS) const;
558 };
559
560
561 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
562 ///
563 class FieldInit : public TypedInit {
564   Init *Rec;                // Record we are referring to
565   std::string FieldName;    // Field we are accessing
566 public:
567   FieldInit(Init *R, const std::string &FN)
568     : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
569     assert(getType() && "FieldInit with non-record type!");
570   }
571
572   virtual Init *convertInitializerTo(RecTy *Ty) {
573     return Ty->convertValue(this);
574   }
575
576   virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
577
578   virtual Init *resolveBitReference(Record &R, unsigned Bit);
579
580   virtual Init *resolveReferences(Record &R);
581
582   virtual void print(std::ostream &OS) const {
583     Rec->print(OS); OS << "." << FieldName;
584   }
585 };
586
587 /// DagInit - (def a, b) - Represent a DAG tree value.  DAG inits are required
588 /// to have Records for their first value, after that, any legal Init is
589 /// possible.
590 ///
591 class DagInit : public Init {
592   Record *NodeTypeDef;
593   std::vector<Init*> Args;
594 public:
595   DagInit(Record *D, std::vector<Init*> &a) : NodeTypeDef(D) {
596     Args.swap(a);  // DESTRUCTIVELY take the arguments
597   }
598   
599   virtual Init *convertInitializerTo(RecTy *Ty) {
600     return Ty->convertValue(this);
601   }
602
603   Record *getNodeType() const { return NodeTypeDef; }
604   const std::vector<Init*> getArgs() const { return Args; }
605
606   virtual void print(std::ostream &OS) const;
607 };
608
609 //===----------------------------------------------------------------------===//
610 //  High-Level Classes
611 //===----------------------------------------------------------------------===//
612
613 class RecordVal {
614   std::string Name;
615   RecTy *Ty;
616   unsigned Prefix;
617   Init *Value;
618 public:
619   RecordVal(const std::string &N, RecTy *T, unsigned P);
620
621   const std::string &getName() const { return Name; }
622
623   unsigned getPrefix() const { return Prefix; }
624   RecTy *getType() const { return Ty; }
625   Init *getValue() const { return Value; }
626
627   bool setValue(Init *V) {
628     if (V) {
629       Value = V->convertInitializerTo(Ty);
630       return Value == 0;
631     }
632     Value = 0;
633     return false;
634   }
635
636   void dump() const;
637   void print(std::ostream &OS, bool PrintSem = true) const;
638 };
639
640 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
641   RV.print(OS << "  ");
642   return OS;
643 }
644
645 struct Record {
646   const std::string Name;
647   std::vector<std::string> TemplateArgs;
648   std::vector<RecordVal> Values;
649   std::vector<Record*> SuperClasses;
650 public:
651
652   Record(const std::string &N) : Name(N) {}
653   ~Record() {}
654
655   const std::string &getName() const { return Name; }
656   const std::vector<std::string> &getTemplateArgs() const {
657     return TemplateArgs;
658   }
659   const std::vector<RecordVal> &getValues() const { return Values; }
660   const std::vector<Record*>   &getSuperClasses() const { return SuperClasses; }
661
662   bool isTemplateArg(const std::string &Name) const {
663     for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
664       if (TemplateArgs[i] == Name) return true;
665     return false;
666   }
667
668   const RecordVal *getValue(const std::string &Name) const {
669     for (unsigned i = 0, e = Values.size(); i != e; ++i)
670       if (Values[i].getName() == Name) return &Values[i];
671     return 0;
672   }
673   RecordVal *getValue(const std::string &Name) {
674     for (unsigned i = 0, e = Values.size(); i != e; ++i)
675       if (Values[i].getName() == Name) return &Values[i];
676     return 0;
677   }
678
679   void addTemplateArg(const std::string &Name) {
680     assert(!isTemplateArg(Name) && "Template arg already defined!");
681     TemplateArgs.push_back(Name);
682   }
683
684   void addValue(const RecordVal &RV) {
685     assert(getValue(RV.getName()) == 0 && "Value already added!");
686     Values.push_back(RV);
687   }
688
689   void removeValue(const std::string &Name) {
690     assert(getValue(Name) && "Cannot remove an entry that does not exist!");
691     for (unsigned i = 0, e = Values.size(); i != e; ++i)
692       if (Values[i].getName() == Name) {
693         Values.erase(Values.begin()+i);
694         return;
695       }
696     assert(0 && "Name does not exist in record!");
697   }
698
699   bool isSubClassOf(Record *R) const {
700     for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
701       if (SuperClasses[i] == R)
702         return true;
703     return false;
704   }
705
706   void addSuperClass(Record *R) {
707     assert(!isSubClassOf(R) && "Already subclassing record!");
708     SuperClasses.push_back(R);
709   }
710
711   // resolveReferences - If there are any field references that refer to fields
712   // that have been filled in, we can propagate the values now.
713   //
714   void resolveReferences();
715
716   void dump() const;
717
718   //===--------------------------------------------------------------------===//
719   // High-level methods useful to tablegen back-ends
720   //
721
722   /// getValueInit - Return the initializer for a value with the specified name,
723   /// or throw an exception if the field does not exist.
724   ///
725   Init *getValueInit(const std::string &FieldName) const;
726
727   /// getValueAsString - This method looks up the specified field and returns
728   /// its value as a string, throwing an exception if the field does not exist
729   /// or if the value is not a string.
730   ///
731   std::string getValueAsString(const std::string &FieldName) const;
732
733   /// getValueAsBitsInit - This method looks up the specified field and returns
734   /// its value as a BitsInit, throwing an exception if the field does not exist
735   /// or if the value is not the right type.
736   ///
737   BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
738
739   /// getValueAsListInit - This method looks up the specified field and returns
740   /// its value as a ListInit, throwing an exception if the field does not exist
741   /// or if the value is not the right type.
742   ///
743   ListInit *getValueAsListInit(const std::string &FieldName) const;
744
745   /// getValueAsDef - This method looks up the specified field and returns its
746   /// value as a Record, throwing an exception if the field does not exist or if
747   /// the value is not the right type.
748   ///
749   Record *getValueAsDef(const std::string &FieldName) const;
750
751   /// getValueAsBit - This method looks up the specified field and returns its
752   /// value as a bit, throwing an exception if the field does not exist or if
753   /// the value is not the right type.
754   ///
755   bool getValueAsBit(const std::string &FieldName) const;
756
757   /// getValueAsInt - This method looks up the specified field and returns its
758   /// value as an int, throwing an exception if the field does not exist or if
759   /// the value is not the right type.
760   ///
761   int getValueAsInt(const std::string &FieldName) const;
762 };
763
764 std::ostream &operator<<(std::ostream &OS, const Record &R);
765
766 class RecordKeeper {
767   std::map<std::string, Record*> Classes, Defs;
768 public:
769   ~RecordKeeper() {
770     for (std::map<std::string, Record*>::iterator I = Classes.begin(),
771            E = Classes.end(); I != E; ++I)
772       delete I->second;
773     for (std::map<std::string, Record*>::iterator I = Defs.begin(),
774            E = Defs.end(); I != E; ++I)
775       delete I->second;
776   }
777   
778   const std::map<std::string, Record*> &getClasses() const { return Classes; }
779   const std::map<std::string, Record*> &getDefs() const { return Defs; }
780
781   Record *getClass(const std::string &Name) const {
782     std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
783     return I == Classes.end() ? 0 : I->second;
784   }
785   Record *getDef(const std::string &Name) const {
786     std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
787     return I == Defs.end() ? 0 : I->second;
788   }
789   void addClass(Record *R) {
790     assert(getClass(R->getName()) == 0 && "Class already exists!");
791     Classes.insert(std::make_pair(R->getName(), R));
792   }
793   void addDef(Record *R) {
794     assert(getDef(R->getName()) == 0 && "Def already exists!");
795     Defs.insert(std::make_pair(R->getName(), R));
796   }
797
798   //===--------------------------------------------------------------------===//
799   // High-level helper methods, useful for tablegen backends...
800
801   /// getAllDerivedDefinitions - This method returns all concrete definitions
802   /// that derive from the specified class name.  If a class with the specified
803   /// name does not exist, an exception is thrown.
804   std::vector<Record*>
805   getAllDerivedDefinitions(const std::string &ClassName) const;
806
807
808   void dump() const;
809 };
810
811 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
812
813 extern RecordKeeper Records;
814
815 #endif