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