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