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