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