1 //===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implement the Parser for TableGen.
12 //===----------------------------------------------------------------------===//
18 #include "llvm/ADT/StringExtras.h"
21 //===----------------------------------------------------------------------===//
22 // Support Code for the Semantic Actions.
23 //===----------------------------------------------------------------------===//
27 Record Rec; // Placeholder for template args and Name.
28 typedef std::vector<Record*> RecordVector;
29 RecordVector DefPrototypes;
31 MultiClass(const std::string &Name, TGLoc Loc) : Rec(Name, Loc) {}
34 struct SubClassReference {
37 std::vector<Init*> TemplateArgs;
38 SubClassReference() : Rec(0) {}
40 bool isInvalid() const { return Rec == 0; }
43 struct SubMultiClassReference {
46 std::vector<Init*> TemplateArgs;
47 SubMultiClassReference() : MC(0) {}
49 bool isInvalid() const { return MC == 0; }
52 } // end namespace llvm
54 bool TGParser::AddValue(Record *CurRec, TGLoc Loc, const RecordVal &RV) {
56 CurRec = &CurMultiClass->Rec;
58 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
59 // The value already exists in the class, treat this as a set.
60 if (ERV->setValue(RV.getValue()))
61 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
62 RV.getType()->getAsString() + "' is incompatible with " +
63 "previous definition of type '" +
64 ERV->getType()->getAsString() + "'");
72 /// Return true on error, false on success.
73 bool TGParser::SetValue(Record *CurRec, TGLoc Loc, const std::string &ValName,
74 const std::vector<unsigned> &BitList, Init *V) {
77 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
79 RecordVal *RV = CurRec->getValue(ValName);
81 return Error(Loc, "Value '" + ValName + "' unknown!");
83 // Do not allow assignments like 'X = X'. This will just cause infinite loops
84 // in the resolution machinery.
86 if (VarInit *VI = dynamic_cast<VarInit*>(V))
87 if (VI->getName() == ValName)
90 // If we are assigning to a subset of the bits in the value... then we must be
91 // assigning to a field of BitsRecTy, which must have a BitsInit
94 if (!BitList.empty()) {
95 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
97 return Error(Loc, "Value '" + ValName + "' is not a bits type");
99 // Convert the incoming value to a bits type of the appropriate size...
100 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList.size()));
102 V->convertInitializerTo(new BitsRecTy(BitList.size()));
103 return Error(Loc, "Initializer is not compatible with bit range");
106 // We should have a BitsInit type now.
107 BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
110 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
112 // Loop over bits, assigning values as appropriate.
113 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
114 unsigned Bit = BitList[i];
115 if (NewVal->getBit(Bit))
116 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
117 ValName + "' more than once");
118 NewVal->setBit(Bit, BInit->getBit(i));
121 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
122 if (NewVal->getBit(i) == 0)
123 NewVal->setBit(i, CurVal->getBit(i));
129 return Error(Loc, "Value '" + ValName + "' of type '" +
130 RV->getType()->getAsString() +
131 "' is incompatible with initializer '" + V->getAsString() +"'");
135 /// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
136 /// args as SubClass's template arguments.
137 bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
138 Record *SC = SubClass.Rec;
139 // Add all of the values in the subclass into the current class.
140 const std::vector<RecordVal> &Vals = SC->getValues();
141 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
142 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
145 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
147 // Ensure that an appropriate number of template arguments are specified.
148 if (TArgs.size() < SubClass.TemplateArgs.size())
149 return Error(SubClass.RefLoc, "More template args specified than expected");
151 // Loop over all of the template arguments, setting them to the specified
152 // value or leaving them as the default if necessary.
153 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
154 if (i < SubClass.TemplateArgs.size()) {
155 // If a value is specified for this template arg, set it now.
156 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
157 SubClass.TemplateArgs[i]))
161 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
164 CurRec->removeValue(TArgs[i]);
166 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
167 return Error(SubClass.RefLoc,"Value not specified for template argument #"
168 + utostr(i) + " (" + TArgs[i] + ") of subclass '" +
169 SC->getName() + "'!");
173 // Since everything went well, we can now set the "superclass" list for the
175 const std::vector<Record*> &SCs = SC->getSuperClasses();
176 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
177 if (CurRec->isSubClassOf(SCs[i]))
178 return Error(SubClass.RefLoc,
179 "Already subclass of '" + SCs[i]->getName() + "'!\n");
180 CurRec->addSuperClass(SCs[i]);
183 if (CurRec->isSubClassOf(SC))
184 return Error(SubClass.RefLoc,
185 "Already subclass of '" + SC->getName() + "'!\n");
186 CurRec->addSuperClass(SC);
190 /// AddSubMultiClass - Add SubMultiClass as a subclass to
191 /// CurMultiClass, resolving its template args as SubMultiClass's
192 /// template arguments.
193 bool TGParser::AddSubMultiClass(MultiClass *CurMultiClass, class SubMultiClassReference &SubMultiClass) {
194 MultiClass *SMC = SubMultiClass.MC;
195 Record *CurRec = &CurMultiClass->Rec;
197 const std::vector<RecordVal> &MCVals = CurMultiClass->Rec.getValues();
199 // Add all of the values in the subclass into the current class.
200 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
201 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
202 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
205 // Add all of the defs in the subclass into the current multiclass.
206 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
207 iend = SMC->DefPrototypes.end();
210 // Clone the def and add it to the current multiclass
211 Record *NewDef = new Record(**i);
213 // Add all of the values in the superclass into the current def.
214 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
215 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
218 CurMultiClass->DefPrototypes.push_back(NewDef);
221 const std::vector<std::string> &SMCTArgs = SMC->Rec.getTemplateArgs();
223 // Ensure that an appropriate number of template arguments are specified.
224 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
225 return Error(SubMultiClass.RefLoc, "More template args specified than expected");
227 // Loop over all of the template arguments, setting them to the specified
228 // value or leaving them as the default if necessary.
229 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
230 if (i < SubMultiClass.TemplateArgs.size()) {
231 // If a value is specified for this template arg, set it in the superclass now.
232 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i], std::vector<unsigned>(),
233 SubMultiClass.TemplateArgs[i]))
237 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
240 CurRec->removeValue(SMCTArgs[i]);
242 // If a value is specified for this template arg, set it in the defs now.
243 for (MultiClass::RecordVector::iterator j = CurMultiClass->DefPrototypes.begin(),
244 jend = CurMultiClass->DefPrototypes.end();
249 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i], std::vector<unsigned>(),
250 SubMultiClass.TemplateArgs[i]))
254 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
257 Def->removeValue(SMCTArgs[i]);
259 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
260 return Error(SubMultiClass.RefLoc,"Value not specified for template argument #"
261 + utostr(i) + " (" + SMCTArgs[i] + ") of subclass '" +
262 SMC->Rec.getName() + "'!");
269 //===----------------------------------------------------------------------===//
271 //===----------------------------------------------------------------------===//
273 /// isObjectStart - Return true if this is a valid first token for an Object.
274 static bool isObjectStart(tgtok::TokKind K) {
275 return K == tgtok::Class || K == tgtok::Def ||
276 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
279 /// ParseObjectName - If an object name is specified, return it. Otherwise,
280 /// return an anonymous name.
281 /// ObjectName ::= ID
282 /// ObjectName ::= /*empty*/
284 std::string TGParser::ParseObjectName() {
285 if (Lex.getCode() == tgtok::Id) {
286 std::string Ret = Lex.getCurStrVal();
291 static unsigned AnonCounter = 0;
292 return "anonymous."+utostr(AnonCounter++);
296 /// ParseClassID - Parse and resolve a reference to a class name. This returns
301 Record *TGParser::ParseClassID() {
302 if (Lex.getCode() != tgtok::Id) {
303 TokError("expected name for ClassID");
307 Record *Result = Records.getClass(Lex.getCurStrVal());
309 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
315 /// ParseMultiClassID - Parse and resolve a reference to a multiclass name. This returns
318 /// MultiClassID ::= ID
320 MultiClass *TGParser::ParseMultiClassID() {
321 if (Lex.getCode() != tgtok::Id) {
322 TokError("expected name for ClassID");
326 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
328 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
334 Record *TGParser::ParseDefmID() {
335 if (Lex.getCode() != tgtok::Id) {
336 TokError("expected multiclass name");
340 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
342 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
352 /// ParseSubClassReference - Parse a reference to a subclass or to a templated
353 /// subclass. This returns a SubClassRefTy with a null Record* on error.
355 /// SubClassRef ::= ClassID
356 /// SubClassRef ::= ClassID '<' ValueList '>'
358 SubClassReference TGParser::
359 ParseSubClassReference(Record *CurRec, bool isDefm) {
360 SubClassReference Result;
361 Result.RefLoc = Lex.getLoc();
364 Result.Rec = ParseDefmID();
366 Result.Rec = ParseClassID();
367 if (Result.Rec == 0) return Result;
369 // If there is no template arg list, we're done.
370 if (Lex.getCode() != tgtok::less)
372 Lex.Lex(); // Eat the '<'
374 if (Lex.getCode() == tgtok::greater) {
375 TokError("subclass reference requires a non-empty list of template values");
380 Result.TemplateArgs = ParseValueList(CurRec);
381 if (Result.TemplateArgs.empty()) {
382 Result.Rec = 0; // Error parsing value list.
386 if (Lex.getCode() != tgtok::greater) {
387 TokError("expected '>' in template value list");
396 /// ParseSubMultiClassReference - Parse a reference to a subclass or to a templated
397 /// submulticlass. This returns a SubMultiClassRefTy with a null Record* on error.
399 /// SubMultiClassRef ::= MultiClassID
400 /// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
402 SubMultiClassReference TGParser::
403 ParseSubMultiClassReference(MultiClass *CurMC) {
404 SubMultiClassReference Result;
405 Result.RefLoc = Lex.getLoc();
407 Result.MC = ParseMultiClassID();
408 if (Result.MC == 0) return Result;
410 // If there is no template arg list, we're done.
411 if (Lex.getCode() != tgtok::less)
413 Lex.Lex(); // Eat the '<'
415 if (Lex.getCode() == tgtok::greater) {
416 TokError("subclass reference requires a non-empty list of template values");
421 Result.TemplateArgs = ParseValueList(&CurMC->Rec);
422 if (Result.TemplateArgs.empty()) {
423 Result.MC = 0; // Error parsing value list.
427 if (Lex.getCode() != tgtok::greater) {
428 TokError("expected '>' in template value list");
437 /// ParseRangePiece - Parse a bit/value range.
438 /// RangePiece ::= INTVAL
439 /// RangePiece ::= INTVAL '-' INTVAL
440 /// RangePiece ::= INTVAL INTVAL
441 bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
442 if (Lex.getCode() != tgtok::IntVal) {
443 TokError("expected integer or bitrange");
446 int64_t Start = Lex.getCurIntVal();
450 return TokError("invalid range, cannot be negative");
452 switch (Lex.Lex()) { // eat first character.
454 Ranges.push_back(Start);
457 if (Lex.Lex() != tgtok::IntVal) {
458 TokError("expected integer value as end of range");
461 End = Lex.getCurIntVal();
464 End = -Lex.getCurIntVal();
468 return TokError("invalid range, cannot be negative");
473 for (; Start <= End; ++Start)
474 Ranges.push_back(Start);
476 for (; Start >= End; --Start)
477 Ranges.push_back(Start);
482 /// ParseRangeList - Parse a list of scalars and ranges into scalar values.
484 /// RangeList ::= RangePiece (',' RangePiece)*
486 std::vector<unsigned> TGParser::ParseRangeList() {
487 std::vector<unsigned> Result;
489 // Parse the first piece.
490 if (ParseRangePiece(Result))
491 return std::vector<unsigned>();
492 while (Lex.getCode() == tgtok::comma) {
493 Lex.Lex(); // Eat the comma.
495 // Parse the next range piece.
496 if (ParseRangePiece(Result))
497 return std::vector<unsigned>();
502 /// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
503 /// OptionalRangeList ::= '<' RangeList '>'
504 /// OptionalRangeList ::= /*empty*/
505 bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
506 if (Lex.getCode() != tgtok::less)
509 TGLoc StartLoc = Lex.getLoc();
510 Lex.Lex(); // eat the '<'
512 // Parse the range list.
513 Ranges = ParseRangeList();
514 if (Ranges.empty()) return true;
516 if (Lex.getCode() != tgtok::greater) {
517 TokError("expected '>' at end of range list");
518 return Error(StartLoc, "to match this '<'");
520 Lex.Lex(); // eat the '>'.
524 /// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
525 /// OptionalBitList ::= '{' RangeList '}'
526 /// OptionalBitList ::= /*empty*/
527 bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
528 if (Lex.getCode() != tgtok::l_brace)
531 TGLoc StartLoc = Lex.getLoc();
532 Lex.Lex(); // eat the '{'
534 // Parse the range list.
535 Ranges = ParseRangeList();
536 if (Ranges.empty()) return true;
538 if (Lex.getCode() != tgtok::r_brace) {
539 TokError("expected '}' at end of bit list");
540 return Error(StartLoc, "to match this '{'");
542 Lex.Lex(); // eat the '}'.
547 /// ParseType - Parse and return a tblgen type. This returns null on error.
549 /// Type ::= STRING // string type
550 /// Type ::= BIT // bit type
551 /// Type ::= BITS '<' INTVAL '>' // bits<x> type
552 /// Type ::= INT // int type
553 /// Type ::= LIST '<' Type '>' // list<x> type
554 /// Type ::= CODE // code type
555 /// Type ::= DAG // dag type
556 /// Type ::= ClassID // Record Type
558 RecTy *TGParser::ParseType() {
559 switch (Lex.getCode()) {
560 default: TokError("Unknown token when expecting a type"); return 0;
561 case tgtok::String: Lex.Lex(); return new StringRecTy();
562 case tgtok::Bit: Lex.Lex(); return new BitRecTy();
563 case tgtok::Int: Lex.Lex(); return new IntRecTy();
564 case tgtok::Code: Lex.Lex(); return new CodeRecTy();
565 case tgtok::Dag: Lex.Lex(); return new DagRecTy();
567 if (Record *R = ParseClassID()) return new RecordRecTy(R);
570 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
571 TokError("expected '<' after bits type");
574 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
575 TokError("expected integer in bits<n> type");
578 uint64_t Val = Lex.getCurIntVal();
579 if (Lex.Lex() != tgtok::greater) { // Eat count.
580 TokError("expected '>' at end of bits<n> type");
583 Lex.Lex(); // Eat '>'
584 return new BitsRecTy(Val);
587 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
588 TokError("expected '<' after list type");
591 Lex.Lex(); // Eat '<'
592 RecTy *SubType = ParseType();
593 if (SubType == 0) return 0;
595 if (Lex.getCode() != tgtok::greater) {
596 TokError("expected '>' at end of list<ty> type");
599 Lex.Lex(); // Eat '>'
600 return new ListRecTy(SubType);
605 /// ParseIDValue - Parse an ID as a value and decode what it means.
607 /// IDValue ::= ID [def local value]
608 /// IDValue ::= ID [def template arg]
609 /// IDValue ::= ID [multiclass local value]
610 /// IDValue ::= ID [multiclass template argument]
611 /// IDValue ::= ID [def name]
613 Init *TGParser::ParseIDValue(Record *CurRec) {
614 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
615 std::string Name = Lex.getCurStrVal();
616 TGLoc Loc = Lex.getLoc();
618 return ParseIDValue(CurRec, Name, Loc);
621 /// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
622 /// has already been read.
623 Init *TGParser::ParseIDValue(Record *CurRec,
624 const std::string &Name, TGLoc NameLoc) {
626 if (const RecordVal *RV = CurRec->getValue(Name))
627 return new VarInit(Name, RV->getType());
629 std::string TemplateArgName = CurRec->getName()+":"+Name;
630 if (CurRec->isTemplateArg(TemplateArgName)) {
631 const RecordVal *RV = CurRec->getValue(TemplateArgName);
632 assert(RV && "Template arg doesn't exist??");
633 return new VarInit(TemplateArgName, RV->getType());
638 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
639 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
640 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
641 assert(RV && "Template arg doesn't exist??");
642 return new VarInit(MCName, RV->getType());
646 if (Record *D = Records.getDef(Name))
647 return new DefInit(D);
649 Error(NameLoc, "Variable not defined: '" + Name + "'");
653 /// ParseSimpleValue - Parse a tblgen value. This returns null on error.
655 /// SimpleValue ::= IDValue
656 /// SimpleValue ::= INTVAL
657 /// SimpleValue ::= STRVAL+
658 /// SimpleValue ::= CODEFRAGMENT
659 /// SimpleValue ::= '?'
660 /// SimpleValue ::= '{' ValueList '}'
661 /// SimpleValue ::= ID '<' ValueListNE '>'
662 /// SimpleValue ::= '[' ValueList ']'
663 /// SimpleValue ::= '(' IDValue DagArgList ')'
664 /// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
665 /// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
666 /// SimpleValue ::= SRATOK '(' Value ',' Value ')'
667 /// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
668 /// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
670 Init *TGParser::ParseSimpleValue(Record *CurRec) {
672 switch (Lex.getCode()) {
673 default: TokError("Unknown token when parsing a value"); break;
674 case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break;
675 case tgtok::StrVal: {
676 std::string Val = Lex.getCurStrVal();
679 // Handle multiple consecutive concatenated strings.
680 while (Lex.getCode() == tgtok::StrVal) {
681 Val += Lex.getCurStrVal();
685 R = new StringInit(Val);
688 case tgtok::CodeFragment:
689 R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break;
690 case tgtok::question: R = new UnsetInit(); Lex.Lex(); break;
692 TGLoc NameLoc = Lex.getLoc();
693 std::string Name = Lex.getCurStrVal();
694 if (Lex.Lex() != tgtok::less) // consume the Id.
695 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
697 // Value ::= ID '<' ValueListNE '>'
698 if (Lex.Lex() == tgtok::greater) {
699 TokError("expected non-empty value list");
702 std::vector<Init*> ValueList = ParseValueList(CurRec);
703 if (ValueList.empty()) return 0;
705 if (Lex.getCode() != tgtok::greater) {
706 TokError("expected '>' at end of value list");
709 Lex.Lex(); // eat the '>'
711 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
712 // a new anonymous definition, deriving from CLASS<initvalslist> with no
714 Record *Class = Records.getClass(Name);
716 Error(NameLoc, "Expected a class name, got '" + Name + "'");
720 // Create the new record, set it as CurRec temporarily.
721 static unsigned AnonCounter = 0;
722 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),NameLoc);
723 SubClassReference SCRef;
724 SCRef.RefLoc = NameLoc;
726 SCRef.TemplateArgs = ValueList;
727 // Add info about the subclass to NewRec.
728 if (AddSubClass(NewRec, SCRef))
730 NewRec->resolveReferences();
731 Records.addDef(NewRec);
733 // The result of the expression is a reference to the new record.
734 return new DefInit(NewRec);
736 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
737 TGLoc BraceLoc = Lex.getLoc();
738 Lex.Lex(); // eat the '{'
739 std::vector<Init*> Vals;
741 if (Lex.getCode() != tgtok::r_brace) {
742 Vals = ParseValueList(CurRec);
743 if (Vals.empty()) return 0;
745 if (Lex.getCode() != tgtok::r_brace) {
746 TokError("expected '}' at end of bit list value");
749 Lex.Lex(); // eat the '}'
751 BitsInit *Result = new BitsInit(Vals.size());
752 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
753 Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy());
755 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
756 ") is not convertable to a bit");
759 Result->setBit(Vals.size()-i-1, Bit);
763 case tgtok::l_square: { // Value ::= '[' ValueList ']'
764 Lex.Lex(); // eat the '['
765 std::vector<Init*> Vals;
767 if (Lex.getCode() != tgtok::r_square) {
768 Vals = ParseValueList(CurRec);
769 if (Vals.empty()) return 0;
771 if (Lex.getCode() != tgtok::r_square) {
772 TokError("expected ']' at end of list value");
775 Lex.Lex(); // eat the ']'
776 return new ListInit(Vals);
778 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
779 Lex.Lex(); // eat the '('
780 if (Lex.getCode() != tgtok::Id) {
781 TokError("expected identifier in dag init");
785 Init *Operator = ParseIDValue(CurRec);
786 if (Operator == 0) return 0;
788 // If the operator name is present, parse it.
789 std::string OperatorName;
790 if (Lex.getCode() == tgtok::colon) {
791 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
792 TokError("expected variable name in dag operator");
795 OperatorName = Lex.getCurStrVal();
796 Lex.Lex(); // eat the VarName.
800 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
801 if (Lex.getCode() != tgtok::r_paren) {
802 DagArgs = ParseDagArgList(CurRec);
803 if (DagArgs.empty()) return 0;
806 if (Lex.getCode() != tgtok::r_paren) {
807 TokError("expected ')' in dag init");
810 Lex.Lex(); // eat the ')'
812 return new DagInit(Operator, OperatorName, DagArgs);
818 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
819 BinOpInit::BinaryOp Code;
820 switch (Lex.getCode()) {
821 default: assert(0 && "Unhandled code!");
822 case tgtok::XConcat: Code = BinOpInit::CONCAT; break;
823 case tgtok::XSRA: Code = BinOpInit::SRA; break;
824 case tgtok::XSRL: Code = BinOpInit::SRL; break;
825 case tgtok::XSHL: Code = BinOpInit::SHL; break;
826 case tgtok::XStrConcat: Code = BinOpInit::STRCONCAT; break;
828 Lex.Lex(); // eat the operation
829 if (Lex.getCode() != tgtok::l_paren) {
830 TokError("expected '(' after binary operator");
833 Lex.Lex(); // eat the '('
835 Init *LHS = ParseValue(CurRec);
836 if (LHS == 0) return 0;
838 if (Lex.getCode() != tgtok::comma) {
839 TokError("expected ',' in binary operator");
842 Lex.Lex(); // eat the ','
844 Init *RHS = ParseValue(CurRec);
845 if (RHS == 0) return 0;
847 if (Lex.getCode() != tgtok::r_paren) {
848 TokError("expected ')' in binary operator");
851 Lex.Lex(); // eat the ')'
852 return (new BinOpInit(Code, LHS, RHS))->Fold();
859 /// ParseValue - Parse a tblgen value. This returns null on error.
861 /// Value ::= SimpleValue ValueSuffix*
862 /// ValueSuffix ::= '{' BitList '}'
863 /// ValueSuffix ::= '[' BitList ']'
864 /// ValueSuffix ::= '.' ID
866 Init *TGParser::ParseValue(Record *CurRec) {
867 Init *Result = ParseSimpleValue(CurRec);
868 if (Result == 0) return 0;
870 // Parse the suffixes now if present.
872 switch (Lex.getCode()) {
873 default: return Result;
874 case tgtok::l_brace: {
875 TGLoc CurlyLoc = Lex.getLoc();
876 Lex.Lex(); // eat the '{'
877 std::vector<unsigned> Ranges = ParseRangeList();
878 if (Ranges.empty()) return 0;
880 // Reverse the bitlist.
881 std::reverse(Ranges.begin(), Ranges.end());
882 Result = Result->convertInitializerBitRange(Ranges);
884 Error(CurlyLoc, "Invalid bit range for value");
889 if (Lex.getCode() != tgtok::r_brace) {
890 TokError("expected '}' at end of bit range list");
896 case tgtok::l_square: {
897 TGLoc SquareLoc = Lex.getLoc();
898 Lex.Lex(); // eat the '['
899 std::vector<unsigned> Ranges = ParseRangeList();
900 if (Ranges.empty()) return 0;
902 Result = Result->convertInitListSlice(Ranges);
904 Error(SquareLoc, "Invalid range for list slice");
909 if (Lex.getCode() != tgtok::r_square) {
910 TokError("expected ']' at end of list slice");
917 if (Lex.Lex() != tgtok::Id) { // eat the .
918 TokError("expected field identifier after '.'");
921 if (!Result->getFieldType(Lex.getCurStrVal())) {
922 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
923 Result->getAsString() + "'");
926 Result = new FieldInit(Result, Lex.getCurStrVal());
927 Lex.Lex(); // eat field name
933 /// ParseDagArgList - Parse the argument list for a dag literal expression.
935 /// ParseDagArgList ::= Value (':' VARNAME)?
936 /// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
937 std::vector<std::pair<llvm::Init*, std::string> >
938 TGParser::ParseDagArgList(Record *CurRec) {
939 std::vector<std::pair<llvm::Init*, std::string> > Result;
942 Init *Val = ParseValue(CurRec);
943 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
945 // If the variable name is present, add it.
947 if (Lex.getCode() == tgtok::colon) {
948 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
949 TokError("expected variable name in dag literal");
950 return std::vector<std::pair<llvm::Init*, std::string> >();
952 VarName = Lex.getCurStrVal();
953 Lex.Lex(); // eat the VarName.
956 Result.push_back(std::make_pair(Val, VarName));
958 if (Lex.getCode() != tgtok::comma) break;
959 Lex.Lex(); // eat the ','
966 /// ParseValueList - Parse a comma separated list of values, returning them as a
967 /// vector. Note that this always expects to be able to parse at least one
968 /// value. It returns an empty list if this is not possible.
970 /// ValueList ::= Value (',' Value)
972 std::vector<Init*> TGParser::ParseValueList(Record *CurRec) {
973 std::vector<Init*> Result;
974 Result.push_back(ParseValue(CurRec));
975 if (Result.back() == 0) return std::vector<Init*>();
977 while (Lex.getCode() == tgtok::comma) {
978 Lex.Lex(); // Eat the comma
980 Result.push_back(ParseValue(CurRec));
981 if (Result.back() == 0) return std::vector<Init*>();
989 /// ParseDeclaration - Read a declaration, returning the name of field ID, or an
990 /// empty string on error. This can happen in a number of different context's,
991 /// including within a def or in the template args for a def (which which case
992 /// CurRec will be non-null) and within the template args for a multiclass (in
993 /// which case CurRec will be null, but CurMultiClass will be set). This can
994 /// also happen within a def that is within a multiclass, which will set both
995 /// CurRec and CurMultiClass.
997 /// Declaration ::= FIELD? Type ID ('=' Value)?
999 std::string TGParser::ParseDeclaration(Record *CurRec,
1000 bool ParsingTemplateArgs) {
1001 // Read the field prefix if present.
1002 bool HasField = Lex.getCode() == tgtok::Field;
1003 if (HasField) Lex.Lex();
1005 RecTy *Type = ParseType();
1006 if (Type == 0) return "";
1008 if (Lex.getCode() != tgtok::Id) {
1009 TokError("Expected identifier in declaration");
1013 TGLoc IdLoc = Lex.getLoc();
1014 std::string DeclName = Lex.getCurStrVal();
1017 if (ParsingTemplateArgs) {
1019 DeclName = CurRec->getName() + ":" + DeclName;
1021 assert(CurMultiClass);
1024 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
1028 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
1031 // If a value is present, parse it.
1032 if (Lex.getCode() == tgtok::equal) {
1034 TGLoc ValLoc = Lex.getLoc();
1035 Init *Val = ParseValue(CurRec);
1037 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
1044 /// ParseTemplateArgList - Read a template argument list, which is a non-empty
1045 /// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1046 /// template args for a def, which may or may not be in a multiclass. If null,
1047 /// these are the template args for a multiclass.
1049 /// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
1051 bool TGParser::ParseTemplateArgList(Record *CurRec) {
1052 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1053 Lex.Lex(); // eat the '<'
1055 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
1057 // Read the first declaration.
1058 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1059 if (TemplArg.empty())
1062 TheRecToAddTo->addTemplateArg(TemplArg);
1064 while (Lex.getCode() == tgtok::comma) {
1065 Lex.Lex(); // eat the ','
1067 // Read the following declarations.
1068 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1069 if (TemplArg.empty())
1071 TheRecToAddTo->addTemplateArg(TemplArg);
1074 if (Lex.getCode() != tgtok::greater)
1075 return TokError("expected '>' at end of template argument list");
1076 Lex.Lex(); // eat the '>'.
1081 /// ParseBodyItem - Parse a single item at within the body of a def or class.
1083 /// BodyItem ::= Declaration ';'
1084 /// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1085 bool TGParser::ParseBodyItem(Record *CurRec) {
1086 if (Lex.getCode() != tgtok::Let) {
1087 if (ParseDeclaration(CurRec, false).empty())
1090 if (Lex.getCode() != tgtok::semi)
1091 return TokError("expected ';' after declaration");
1096 // LET ID OptionalRangeList '=' Value ';'
1097 if (Lex.Lex() != tgtok::Id)
1098 return TokError("expected field identifier after let");
1100 TGLoc IdLoc = Lex.getLoc();
1101 std::string FieldName = Lex.getCurStrVal();
1102 Lex.Lex(); // eat the field name.
1104 std::vector<unsigned> BitList;
1105 if (ParseOptionalBitList(BitList))
1107 std::reverse(BitList.begin(), BitList.end());
1109 if (Lex.getCode() != tgtok::equal)
1110 return TokError("expected '=' in let expression");
1111 Lex.Lex(); // eat the '='.
1113 Init *Val = ParseValue(CurRec);
1114 if (Val == 0) return true;
1116 if (Lex.getCode() != tgtok::semi)
1117 return TokError("expected ';' after let expression");
1120 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1123 /// ParseBody - Read the body of a class or def. Return true on error, false on
1127 /// Body ::= '{' BodyList '}'
1128 /// BodyList BodyItem*
1130 bool TGParser::ParseBody(Record *CurRec) {
1131 // If this is a null definition, just eat the semi and return.
1132 if (Lex.getCode() == tgtok::semi) {
1137 if (Lex.getCode() != tgtok::l_brace)
1138 return TokError("Expected ';' or '{' to start body");
1142 while (Lex.getCode() != tgtok::r_brace)
1143 if (ParseBodyItem(CurRec))
1151 /// ParseObjectBody - Parse the body of a def or class. This consists of an
1152 /// optional ClassList followed by a Body. CurRec is the current def or class
1153 /// that is being parsed.
1155 /// ObjectBody ::= BaseClassList Body
1156 /// BaseClassList ::= /*empty*/
1157 /// BaseClassList ::= ':' BaseClassListNE
1158 /// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1160 bool TGParser::ParseObjectBody(Record *CurRec) {
1161 // If there is a baseclass list, read it.
1162 if (Lex.getCode() == tgtok::colon) {
1165 // Read all of the subclasses.
1166 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1169 if (SubClass.Rec == 0) return true;
1172 if (AddSubClass(CurRec, SubClass))
1175 if (Lex.getCode() != tgtok::comma) break;
1176 Lex.Lex(); // eat ','.
1177 SubClass = ParseSubClassReference(CurRec, false);
1181 // Process any variables on the let stack.
1182 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1183 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1184 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1185 LetStack[i][j].Bits, LetStack[i][j].Value))
1188 return ParseBody(CurRec);
1192 /// ParseDef - Parse and return a top level or multiclass def, return the record
1193 /// corresponding to it. This returns null on error.
1195 /// DefInst ::= DEF ObjectName ObjectBody
1197 llvm::Record *TGParser::ParseDef(MultiClass *CurMultiClass) {
1198 TGLoc DefLoc = Lex.getLoc();
1199 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
1200 Lex.Lex(); // Eat the 'def' token.
1202 // Parse ObjectName and make a record for it.
1203 Record *CurRec = new Record(ParseObjectName(), DefLoc);
1205 if (!CurMultiClass) {
1206 // Top-level def definition.
1208 // Ensure redefinition doesn't happen.
1209 if (Records.getDef(CurRec->getName())) {
1210 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
1213 Records.addDef(CurRec);
1215 // Otherwise, a def inside a multiclass, add it to the multiclass.
1216 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1217 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1218 Error(DefLoc, "def '" + CurRec->getName() +
1219 "' already defined in this multiclass!");
1222 CurMultiClass->DefPrototypes.push_back(CurRec);
1225 if (ParseObjectBody(CurRec))
1228 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
1229 CurRec->resolveReferences();
1231 // If ObjectBody has template arguments, it's an error.
1232 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
1237 /// ParseClass - Parse a tblgen class definition.
1239 /// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1241 bool TGParser::ParseClass() {
1242 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1245 if (Lex.getCode() != tgtok::Id)
1246 return TokError("expected class name after 'class' keyword");
1248 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1250 // If the body was previously defined, this is an error.
1251 if (!CurRec->getValues().empty() ||
1252 !CurRec->getSuperClasses().empty() ||
1253 !CurRec->getTemplateArgs().empty())
1254 return TokError("Class '" + CurRec->getName() + "' already defined");
1256 // If this is the first reference to this class, create and add it.
1257 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc());
1258 Records.addClass(CurRec);
1260 Lex.Lex(); // eat the name.
1262 // If there are template args, parse them.
1263 if (Lex.getCode() == tgtok::less)
1264 if (ParseTemplateArgList(CurRec))
1267 // Finally, parse the object body.
1268 return ParseObjectBody(CurRec);
1271 /// ParseLetList - Parse a non-empty list of assignment expressions into a list
1274 /// LetList ::= LetItem (',' LetItem)*
1275 /// LetItem ::= ID OptionalRangeList '=' Value
1277 std::vector<LetRecord> TGParser::ParseLetList() {
1278 std::vector<LetRecord> Result;
1281 if (Lex.getCode() != tgtok::Id) {
1282 TokError("expected identifier in let definition");
1283 return std::vector<LetRecord>();
1285 std::string Name = Lex.getCurStrVal();
1286 TGLoc NameLoc = Lex.getLoc();
1287 Lex.Lex(); // Eat the identifier.
1289 // Check for an optional RangeList.
1290 std::vector<unsigned> Bits;
1291 if (ParseOptionalRangeList(Bits))
1292 return std::vector<LetRecord>();
1293 std::reverse(Bits.begin(), Bits.end());
1295 if (Lex.getCode() != tgtok::equal) {
1296 TokError("expected '=' in let expression");
1297 return std::vector<LetRecord>();
1299 Lex.Lex(); // eat the '='.
1301 Init *Val = ParseValue(0);
1302 if (Val == 0) return std::vector<LetRecord>();
1304 // Now that we have everything, add the record.
1305 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
1307 if (Lex.getCode() != tgtok::comma)
1309 Lex.Lex(); // eat the comma.
1313 /// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
1314 /// different related productions.
1316 /// Object ::= LET LetList IN '{' ObjectList '}'
1317 /// Object ::= LET LetList IN Object
1319 bool TGParser::ParseTopLevelLet() {
1320 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1323 // Add this entry to the let stack.
1324 std::vector<LetRecord> LetInfo = ParseLetList();
1325 if (LetInfo.empty()) return true;
1326 LetStack.push_back(LetInfo);
1328 if (Lex.getCode() != tgtok::In)
1329 return TokError("expected 'in' at end of top-level 'let'");
1332 // If this is a scalar let, just handle it now
1333 if (Lex.getCode() != tgtok::l_brace) {
1334 // LET LetList IN Object
1337 } else { // Object ::= LETCommand '{' ObjectList '}'
1338 TGLoc BraceLoc = Lex.getLoc();
1339 // Otherwise, this is a group let.
1340 Lex.Lex(); // eat the '{'.
1342 // Parse the object list.
1343 if (ParseObjectList())
1346 if (Lex.getCode() != tgtok::r_brace) {
1347 TokError("expected '}' at end of top level let command");
1348 return Error(BraceLoc, "to match this '{'");
1353 // Outside this let scope, this let block is not active.
1354 LetStack.pop_back();
1358 /// ParseMultiClassDef - Parse a def in a multiclass context.
1360 /// MultiClassDef ::= DefInst
1362 bool TGParser::ParseMultiClassDef(MultiClass *CurMC) {
1363 if (Lex.getCode() != tgtok::Def)
1364 return TokError("expected 'def' in multiclass body");
1366 Record *D = ParseDef(CurMC);
1367 if (D == 0) return true;
1369 // Copy the template arguments for the multiclass into the def.
1370 const std::vector<std::string> &TArgs = CurMC->Rec.getTemplateArgs();
1372 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1373 const RecordVal *RV = CurMC->Rec.getValue(TArgs[i]);
1374 assert(RV && "Template arg doesn't exist?");
1381 /// ParseMultiClass - Parse a multiclass definition.
1383 /// MultiClassInst ::= MULTICLASS ID TemplateArgList? ':' BaseMultiClassList '{' MultiClassDef+ '}'
1385 bool TGParser::ParseMultiClass() {
1386 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1387 Lex.Lex(); // Eat the multiclass token.
1389 if (Lex.getCode() != tgtok::Id)
1390 return TokError("expected identifier after multiclass for name");
1391 std::string Name = Lex.getCurStrVal();
1393 if (MultiClasses.count(Name))
1394 return TokError("multiclass '" + Name + "' already defined");
1396 CurMultiClass = MultiClasses[Name] = new MultiClass(Name, Lex.getLoc());
1397 Lex.Lex(); // Eat the identifier.
1399 // If there are template args, parse them.
1400 if (Lex.getCode() == tgtok::less)
1401 if (ParseTemplateArgList(0))
1404 // If there are submulticlasses, parse them.
1405 if (Lex.getCode() == tgtok::colon) {
1408 // Read all of the submulticlasses.
1409 SubMultiClassReference SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1412 if (SubMultiClass.MC == 0) return true;
1415 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1418 if (Lex.getCode() != tgtok::comma) break;
1419 Lex.Lex(); // eat ','.
1420 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1424 if (Lex.getCode() != tgtok::l_brace)
1425 return TokError("expected '{' in multiclass definition");
1427 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1428 return TokError("multiclass must contain at least one def");
1430 while (Lex.getCode() != tgtok::r_brace)
1431 if (ParseMultiClassDef(CurMultiClass))
1434 Lex.Lex(); // eat the '}'.
1440 /// ParseDefm - Parse the instantiation of a multiclass.
1442 /// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
1444 bool TGParser::ParseDefm() {
1445 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
1446 if (Lex.Lex() != tgtok::Id) // eat the defm.
1447 return TokError("expected identifier after defm");
1449 TGLoc DefmPrefixLoc = Lex.getLoc();
1450 std::string DefmPrefix = Lex.getCurStrVal();
1451 if (Lex.Lex() != tgtok::colon)
1452 return TokError("expected ':' after defm identifier");
1457 TGLoc SubClassLoc = Lex.getLoc();
1458 SubClassReference Ref = ParseSubClassReference(0, true);
1459 if (Ref.Rec == 0) return true;
1461 if (Lex.getCode() != tgtok::semi)
1462 return TokError("expected ';' at end of defm");
1465 // To instantiate a multiclass, we need to first get the multiclass, then
1466 // instantiate each def contained in the multiclass with the SubClassRef
1467 // template parameters.
1468 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
1469 assert(MC && "Didn't lookup multiclass correctly?");
1470 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
1472 // Verify that the correct number of template arguments were specified.
1473 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
1474 if (TArgs.size() < TemplateVals.size())
1475 return Error(SubClassLoc,
1476 "more template args specified than multiclass expects");
1478 // Loop over all the def's in the multiclass, instantiating each one.
1479 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
1480 Record *DefProto = MC->DefPrototypes[i];
1482 // Add the suffix to the defm name to get the new name.
1483 Record *CurRec = new Record(DefmPrefix + DefProto->getName(),DefmPrefixLoc);
1485 SubClassReference Ref;
1486 Ref.RefLoc = DefmPrefixLoc;
1488 AddSubClass(CurRec, Ref);
1490 // Loop over all of the template arguments, setting them to the specified
1491 // value or leaving them as the default if necessary.
1492 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1493 if (i < TemplateVals.size()) { // A value is specified for this temp-arg?
1495 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1500 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1503 CurRec->removeValue(TArgs[i]);
1505 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
1506 return Error(SubClassLoc, "value not specified for template argument #"+
1507 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1508 MC->Rec.getName() + "'");
1512 // If the mdef is inside a 'let' expression, add to each def.
1513 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1514 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1515 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1516 LetStack[i][j].Bits, LetStack[i][j].Value)) {
1517 Error(DefmPrefixLoc, "when instantiating this defm");
1522 // Ensure redefinition doesn't happen.
1523 if (Records.getDef(CurRec->getName()))
1524 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
1525 "' already defined, instantiating defm with subdef '" +
1526 DefProto->getName() + "'");
1527 Records.addDef(CurRec);
1528 CurRec->resolveReferences();
1535 /// Object ::= ClassInst
1536 /// Object ::= DefInst
1537 /// Object ::= MultiClassInst
1538 /// Object ::= DefMInst
1539 /// Object ::= LETCommand '{' ObjectList '}'
1540 /// Object ::= LETCommand Object
1541 bool TGParser::ParseObject() {
1542 switch (Lex.getCode()) {
1543 default: assert(0 && "This is not an object");
1544 case tgtok::Let: return ParseTopLevelLet();
1545 case tgtok::Def: return ParseDef(0) == 0;
1546 case tgtok::Defm: return ParseDefm();
1547 case tgtok::Class: return ParseClass();
1548 case tgtok::MultiClass: return ParseMultiClass();
1553 /// ObjectList :== Object*
1554 bool TGParser::ParseObjectList() {
1555 while (isObjectStart(Lex.getCode())) {
1563 bool TGParser::ParseFile() {
1564 Lex.Lex(); // Prime the lexer.
1565 if (ParseObjectList()) return true;
1567 // If we have unread input at the end of the file, report it.
1568 if (Lex.getCode() == tgtok::Eof)
1571 return TokError("Unexpected input at top level");