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