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