blob: fee36b023a38372adf46cc392e5425e3392e0778 [file] [log] [blame]
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001//===- Record.cpp - Record implementation ---------------------------------===//
John Criswelld3032032003-10-20 20:20:30 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00009//
10//
11//===----------------------------------------------------------------------===//
12
13#include "Record.h"
14
15//===----------------------------------------------------------------------===//
16// Type implementations
17//===----------------------------------------------------------------------===//
18
Brian Gaeke960707c2003-11-11 22:41:34 +000019namespace llvm {
20
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000021void RecTy::dump() const { print(std::cerr); }
22
23Init *BitRecTy::convertValue(BitsInit *BI) {
24 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
25 return BI->getBit(0);
26}
27
28bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
29 return RHS->getNumBits() == 1;
30}
31
32Init *BitRecTy::convertValue(IntInit *II) {
33 int Val = II->getValue();
34 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
35
36 return new BitInit(Val != 0);
37}
38
39Init *BitRecTy::convertValue(TypedInit *VI) {
40 if (dynamic_cast<BitRecTy*>(VI->getType()))
41 return VI; // Accept variable if it is already of bit type!
42 return 0;
43}
44
45Init *BitsRecTy::convertValue(UnsetInit *UI) {
46 BitsInit *Ret = new BitsInit(Size);
47
48 for (unsigned i = 0; i != Size; ++i)
49 Ret->setBit(i, new UnsetInit());
50 return Ret;
51}
52
53Init *BitsRecTy::convertValue(BitInit *UI) {
54 if (Size != 1) return 0; // Can only convert single bit...
55 BitsInit *Ret = new BitsInit(1);
56 Ret->setBit(0, UI);
57 return Ret;
58}
59
60// convertValue from Int initializer to bits type: Split the integer up into the
61// appropriate bits...
62//
63Init *BitsRecTy::convertValue(IntInit *II) {
Misha Brukman6752fb52004-06-21 18:01:47 +000064 int64_t Value = II->getValue();
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000065 // Make sure this bitfield is large enough to hold the integer value...
66 if (Value >= 0) {
Misha Brukman6752fb52004-06-21 18:01:47 +000067 if (Value & ~((1LL << Size)-1))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000068 return 0;
69 } else {
70 if ((Value >> Size) != -1 || ((Value & (1 << Size-1)) == 0))
71 return 0;
72 }
73
74 BitsInit *Ret = new BitsInit(Size);
75 for (unsigned i = 0; i != Size; ++i)
76 Ret->setBit(i, new BitInit(Value & (1 << i)));
77
78 return Ret;
79}
80
81Init *BitsRecTy::convertValue(BitsInit *BI) {
82 // If the number of bits is right, return it. Otherwise we need to expand or
83 // truncate...
84 if (BI->getNumBits() == Size) return BI;
85 return 0;
86}
87
88Init *BitsRecTy::convertValue(TypedInit *VI) {
89 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
90 if (BRT->Size == Size) {
91 BitsInit *Ret = new BitsInit(Size);
92 for (unsigned i = 0; i != Size; ++i)
93 Ret->setBit(i, new VarBitInit(VI, i));
94 return Ret;
95 }
96 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
97 BitsInit *Ret = new BitsInit(1);
98 Ret->setBit(0, VI);
99 return Ret;
100 }
101
102 return 0;
103}
104
105Init *IntRecTy::convertValue(BitInit *BI) {
106 return new IntInit(BI->getValue());
107}
108
109Init *IntRecTy::convertValue(BitsInit *BI) {
110 int Result = 0;
111 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
112 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
113 Result |= Bit->getValue() << i;
114 } else {
115 return 0;
116 }
117 return new IntInit(Result);
118}
119
120Init *IntRecTy::convertValue(TypedInit *TI) {
121 if (TI->getType()->typeIsConvertibleTo(this))
122 return TI; // Accept variable if already of the right type!
123 return 0;
124}
125
126Init *StringRecTy::convertValue(TypedInit *TI) {
127 if (dynamic_cast<StringRecTy*>(TI->getType()))
128 return TI; // Accept variable if already of the right type!
129 return 0;
130}
131
132void ListRecTy::print(std::ostream &OS) const {
133 OS << "list<" << *Ty << ">";
134}
135
136Init *ListRecTy::convertValue(ListInit *LI) {
137 std::vector<Init*> Elements;
138
139 // Verify that all of the elements of the list are subclasses of the
140 // appropriate class!
141 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
142 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
143 Elements.push_back(CI);
144 else
145 return 0;
146
147 return new ListInit(Elements);
148}
149
150Init *ListRecTy::convertValue(TypedInit *TI) {
151 // Ensure that TI is compatible with our class.
152 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
153 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
154 return TI;
155 return 0;
156}
157
158Init *CodeRecTy::convertValue(TypedInit *TI) {
159 if (TI->getType()->typeIsConvertibleTo(this))
160 return TI;
161 return 0;
162}
163
164Init *DagRecTy::convertValue(TypedInit *TI) {
165 if (TI->getType()->typeIsConvertibleTo(this))
166 return TI;
167 return 0;
168}
169
170
171void RecordRecTy::print(std::ostream &OS) const {
172 OS << Rec->getName();
173}
174
175Init *RecordRecTy::convertValue(DefInit *DI) {
176 // Ensure that DI is a subclass of Rec.
177 if (!DI->getDef()->isSubClassOf(Rec))
178 return 0;
179 return DI;
180}
181
182Init *RecordRecTy::convertValue(TypedInit *TI) {
183 // Ensure that TI is compatible with Rec.
184 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
185 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
186 RRT->getRecord() == getRecord())
187 return TI;
188 return 0;
189}
190
191bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
192 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
193}
194
195
196//===----------------------------------------------------------------------===//
197// Initializer implementations
198//===----------------------------------------------------------------------===//
199
200void Init::dump() const { return print(std::cerr); }
201
202Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
203 BitsInit *BI = new BitsInit(Bits.size());
204 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
205 if (Bits[i] >= getNumBits()) {
206 delete BI;
207 return 0;
208 }
209 BI->setBit(i, getBit(Bits[i]));
210 }
211 return BI;
212}
213
214void BitsInit::print(std::ostream &OS) const {
215 //if (!printInHex(OS)) return;
216 //if (!printAsVariable(OS)) return;
217 //if (!printAsUnset(OS)) return;
218
219 OS << "{ ";
220 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
221 if (i) OS << ", ";
222 if (Init *Bit = getBit(e-i-1))
223 Bit->print(OS);
224 else
225 OS << "*";
226 }
227 OS << " }";
228}
229
230bool BitsInit::printInHex(std::ostream &OS) const {
231 // First, attempt to convert the value into an integer value...
232 int Result = 0;
233 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
234 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
235 Result |= Bit->getValue() << i;
236 } else {
237 return true;
238 }
239
240 OS << "0x" << std::hex << Result << std::dec;
241 return false;
242}
243
244bool BitsInit::printAsVariable(std::ostream &OS) const {
245 // Get the variable that we may be set equal to...
246 assert(getNumBits() != 0);
247 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
248 if (FirstBit == 0) return true;
249 TypedInit *Var = FirstBit->getVariable();
250
251 // Check to make sure the types are compatible.
252 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
253 if (Ty == 0) return true;
254 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
255
256 // Check to make sure all bits are referring to the right bits in the variable
257 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
258 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
259 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
260 return true;
261 }
262
263 Var->print(OS);
264 return false;
265}
266
267bool BitsInit::printAsUnset(std::ostream &OS) const {
268 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
269 if (!dynamic_cast<UnsetInit*>(getBit(i)))
270 return true;
271 OS << "?";
272 return false;
273}
274
275// resolveReferences - If there are any field references that refer to fields
276// that have been filled in, we can propagate the values now.
277//
278Init *BitsInit::resolveReferences(Record &R) {
279 bool Changed = false;
280 BitsInit *New = new BitsInit(getNumBits());
281
282 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
283 Init *B;
284 Init *CurBit = getBit(i);
285
286 do {
287 B = CurBit;
288 CurBit = CurBit->resolveReferences(R);
289 Changed |= B != CurBit;
290 } while (B != CurBit);
291 New->setBit(i, CurBit);
292 }
293
294 if (Changed)
295 return New;
296 delete New;
297 return this;
298}
299
300Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
301 BitsInit *BI = new BitsInit(Bits.size());
302
303 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
304 if (Bits[i] >= 32) {
305 delete BI;
306 return 0;
307 }
308 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
309 }
310 return BI;
311}
312
Chris Lattner8bf9e062004-07-26 23:21:34 +0000313Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) {
314 std::vector<Init*> Vals;
315 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
316 if (Elements[i] >= getSize())
317 return 0;
318 Vals.push_back(getElement(Elements[i]));
319 }
320 return new ListInit(Vals);
321}
322
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000323void ListInit::print(std::ostream &OS) const {
324 OS << "[";
325 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
326 if (i) OS << ", ";
327 OS << *Values[i];
328 }
329 OS << "]";
330}
331
332Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
333 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
334 if (T == 0) return 0; // Cannot subscript a non-bits variable...
335 unsigned NumBits = T->getNumBits();
336
337 BitsInit *BI = new BitsInit(Bits.size());
338 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
339 if (Bits[i] >= NumBits) {
340 delete BI;
341 return 0;
342 }
343 BI->setBit(i, new VarBitInit(this, Bits[i]));
344 }
345 return BI;
346}
347
348Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
349 if (R.isTemplateArg(getName()))
350 return this;
351
352 RecordVal *RV = R.getValue(getName());
353 assert(RV && "Reference to a non-existant variable?");
354 assert(dynamic_cast<BitsInit*>(RV->getValue()));
355 BitsInit *BI = (BitsInit*)RV->getValue();
356
357 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
358 Init *B = BI->getBit(Bit);
359
360 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
361 return B; // Replace the VarBitInit with it.
362 return this;
363}
364
365RecTy *VarInit::getFieldType(const std::string &FieldName) const {
366 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
367 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
368 return RV->getType();
369 return 0;
370}
371
372Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
373 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
Chris Lattnerd959ab92004-02-28 16:31:53 +0000374 if (const RecordVal *RV = R.getValue(VarName)) {
375 Init *TheInit = RV->getValue();
376 assert(TheInit != this && "Infinite loop detected!");
377 if (Init *I = TheInit->getFieldInit(R, FieldName))
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000378 return I;
379 else
380 return 0;
Chris Lattnerd959ab92004-02-28 16:31:53 +0000381 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000382 return 0;
383}
384
385/// resolveReferences - This method is used by classes that refer to other
386/// variables which may not be defined at the time they expression is formed.
387/// If a value is set for the variable later, this method will be called on
388/// users of the value to allow the value to propagate out.
389///
390Init *VarInit::resolveReferences(Record &R) {
391 if (RecordVal *Val = R.getValue(VarName))
392 if (!dynamic_cast<UnsetInit*>(Val->getValue()))
393 return Val->getValue();
394 return this;
395}
396
397
398Init *VarBitInit::resolveReferences(Record &R) {
399 Init *I = getVariable()->resolveBitReference(R, getBitNum());
400 if (I != getVariable())
401 return I;
402 return this;
403}
404
405RecTy *DefInit::getFieldType(const std::string &FieldName) const {
406 if (const RecordVal *RV = Def->getValue(FieldName))
407 return RV->getType();
408 return 0;
409}
410
411Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
412 return Def->getValue(FieldName)->getValue();
413}
414
415
416void DefInit::print(std::ostream &OS) const {
417 OS << Def->getName();
418}
419
420Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
421 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
422 if (T == 0) return 0; // Cannot subscript a non-bits field...
423 unsigned NumBits = T->getNumBits();
424
425 BitsInit *BI = new BitsInit(Bits.size());
426 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
427 if (Bits[i] >= NumBits) {
428 delete BI;
429 return 0;
430 }
431 BI->setBit(i, new VarBitInit(this, Bits[i]));
432 }
433 return BI;
434}
435
436Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
437 Init *BitsVal = Rec->getFieldInit(R, FieldName);
438 if (BitsVal)
439 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
440 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
441 Init *B = BI->getBit(Bit);
442
443 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
444 return B; // Replace the VarBitInit with it.
445 }
446 return this;
447}
448
449Init *FieldInit::resolveReferences(Record &R) {
450 Init *BitsVal = Rec->getFieldInit(R, FieldName);
451 if (BitsVal) {
452 Init *BVR = BitsVal->resolveReferences(R);
453 return BVR->isComplete() ? BVR : this;
454 }
455 return this;
456}
457
458
459void DagInit::print(std::ostream &OS) const {
460 OS << "(" << NodeTypeDef->getName();
461 if (Args.size()) {
462 OS << " " << *Args[0];
463 if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
464 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
465 OS << ", " << *Args[i];
466 if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
467 }
468 }
469 OS << ")";
470}
471
472
473//===----------------------------------------------------------------------===//
474// Other implementations
475//===----------------------------------------------------------------------===//
476
477RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
478 : Name(N), Ty(T), Prefix(P) {
479 Value = Ty->convertValue(new UnsetInit());
480 assert(Value && "Cannot create unset value for current type!");
481}
482
483void RecordVal::dump() const { std::cerr << *this; }
484
485void RecordVal::print(std::ostream &OS, bool PrintSem) const {
486 if (getPrefix()) OS << "field ";
487 OS << *getType() << " " << getName();
488 if (getValue()) {
489 OS << " = " << *getValue();
490 }
491 if (PrintSem) OS << ";\n";
492}
493
494// resolveReferences - If there are any field references that refer to fields
495// that have been filled in, we can propagate the values now.
496//
497void Record::resolveReferences() {
498 for (unsigned i = 0, e = Values.size(); i != e; ++i)
499 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
500}
501
502void Record::dump() const { std::cerr << *this; }
503
504std::ostream &operator<<(std::ostream &OS, const Record &R) {
505 OS << R.getName();
506
507 const std::vector<std::string> &TArgs = R.getTemplateArgs();
508 if (!TArgs.empty()) {
509 OS << "<";
510 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
511 if (i) OS << ", ";
512 const RecordVal *RV = R.getValue(TArgs[i]);
513 assert(RV && "Template argument record not found??");
514 RV->print(OS, false);
515 }
516 OS << ">";
517 }
518
519 OS << " {";
520 const std::vector<Record*> &SC = R.getSuperClasses();
521 if (!SC.empty()) {
522 OS << "\t//";
523 for (unsigned i = 0, e = SC.size(); i != e; ++i)
524 OS << " " << SC[i]->getName();
525 }
526 OS << "\n";
527
528 const std::vector<RecordVal> &Vals = R.getValues();
529 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
530 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
531 OS << Vals[i];
532 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
533 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
534 OS << Vals[i];
535
536 return OS << "}\n";
537}
538
539/// getValueInit - Return the initializer for a value with the specified name,
540/// or throw an exception if the field does not exist.
541///
542Init *Record::getValueInit(const std::string &FieldName) const {
543 const RecordVal *R = getValue(FieldName);
544 if (R == 0 || R->getValue() == 0)
545 throw "Record '" + getName() + "' does not have a field named '" +
546 FieldName + "!\n";
547 return R->getValue();
548}
549
550
551/// getValueAsString - This method looks up the specified field and returns its
552/// value as a string, throwing an exception if the field does not exist or if
553/// the value is not a string.
554///
555std::string Record::getValueAsString(const std::string &FieldName) const {
556 const RecordVal *R = getValue(FieldName);
557 if (R == 0 || R->getValue() == 0)
558 throw "Record '" + getName() + "' does not have a field named '" +
559 FieldName + "!\n";
560
561 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
562 return SI->getValue();
563 throw "Record '" + getName() + "', field '" + FieldName +
564 "' does not have a string initializer!";
565}
566
567/// getValueAsBitsInit - This method looks up the specified field and returns
568/// its value as a BitsInit, throwing an exception if the field does not exist
569/// or if the value is not the right type.
570///
571BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
572 const RecordVal *R = getValue(FieldName);
573 if (R == 0 || R->getValue() == 0)
574 throw "Record '" + getName() + "' does not have a field named '" +
575 FieldName + "!\n";
576
577 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
578 return BI;
579 throw "Record '" + getName() + "', field '" + FieldName +
580 "' does not have a BitsInit initializer!";
581}
582
583/// getValueAsListInit - This method looks up the specified field and returns
584/// its value as a ListInit, throwing an exception if the field does not exist
585/// or if the value is not the right type.
586///
587ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
588 const RecordVal *R = getValue(FieldName);
589 if (R == 0 || R->getValue() == 0)
590 throw "Record '" + getName() + "' does not have a field named '" +
591 FieldName + "!\n";
592
593 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
594 return LI;
595 throw "Record '" + getName() + "', field '" + FieldName +
596 "' does not have a list initializer!";
597}
598
599/// getValueAsInt - This method looks up the specified field and returns its
600/// value as an int, throwing an exception if the field does not exist or if
601/// the value is not the right type.
602///
603int Record::getValueAsInt(const std::string &FieldName) const {
604 const RecordVal *R = getValue(FieldName);
605 if (R == 0 || R->getValue() == 0)
606 throw "Record '" + getName() + "' does not have a field named '" +
607 FieldName + "!\n";
608
609 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
610 return II->getValue();
611 throw "Record '" + getName() + "', field '" + FieldName +
612 "' does not have a list initializer!";
613}
614
615/// getValueAsDef - This method looks up the specified field and returns its
616/// value as a Record, throwing an exception if the field does not exist or if
617/// the value is not the right type.
618///
619Record *Record::getValueAsDef(const std::string &FieldName) const {
620 const RecordVal *R = getValue(FieldName);
621 if (R == 0 || R->getValue() == 0)
622 throw "Record '" + getName() + "' does not have a field named '" +
623 FieldName + "!\n";
624
625 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
626 return DI->getDef();
627 throw "Record '" + getName() + "', field '" + FieldName +
628 "' does not have a list initializer!";
629}
630
631/// getValueAsBit - This method looks up the specified field and returns its
632/// value as a bit, throwing an exception if the field does not exist or if
633/// the value is not the right type.
634///
635bool Record::getValueAsBit(const std::string &FieldName) const {
636 const RecordVal *R = getValue(FieldName);
637 if (R == 0 || R->getValue() == 0)
638 throw "Record '" + getName() + "' does not have a field named '" +
639 FieldName + "!\n";
640
641 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
642 return BI->getValue();
643 throw "Record '" + getName() + "', field '" + FieldName +
644 "' does not have a bit initializer!";
645}
646
647/// getValueAsDag - This method looks up the specified field and returns its
648/// value as an Dag, throwing an exception if the field does not exist or if
649/// the value is not the right type.
650///
651DagInit *Record::getValueAsDag(const std::string &FieldName) const {
652 const RecordVal *R = getValue(FieldName);
653 if (R == 0 || R->getValue() == 0)
654 throw "Record '" + getName() + "' does not have a field named '" +
655 FieldName + "!\n";
656
657 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
658 return DI;
659 throw "Record '" + getName() + "', field '" + FieldName +
660 "' does not have a dag initializer!";
661}
662
663
664void RecordKeeper::dump() const { std::cerr << *this; }
665
666std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
667 OS << "------------- Classes -----------------\n";
668 const std::map<std::string, Record*> &Classes = RK.getClasses();
669 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
670 E = Classes.end(); I != E; ++I)
671 OS << "class " << *I->second;
672
673 OS << "------------- Defs -----------------\n";
674 const std::map<std::string, Record*> &Defs = RK.getDefs();
675 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
676 E = Defs.end(); I != E; ++I)
677 OS << "def " << *I->second;
678 return OS;
679}
680
681
682/// getAllDerivedDefinitions - This method returns all concrete definitions
683/// that derive from the specified class name. If a class with the specified
684/// name does not exist, an error is printed and true is returned.
685std::vector<Record*>
686RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
687 Record *Class = Records.getClass(ClassName);
688 if (!Class)
689 throw "ERROR: Couldn't find the '" + ClassName + "' class!\n";
690
691 std::vector<Record*> Defs;
692 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
693 E = getDefs().end(); I != E; ++I)
694 if (I->second->isSubClassOf(Class))
695 Defs.push_back(I->second);
696
697 return Defs;
698}
Brian Gaeke960707c2003-11-11 22:41:34 +0000699
700} // End llvm namespace