blob: bf10abc8be7a0733fe98d89c327074ef893d087d [file] [log] [blame]
Chris Lattnere62c1182002-12-02 01:23:04 +00001//===-- FileParser.y - Parser for TableGen files ----------------*- C++ -*-===//
John Criswellaefb6662003-10-21 15:29:18 +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 Lattnere62c1182002-12-02 01:23:04 +00009//
10// This file implements the bison parser for Table Generator files...
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattnere62c1182002-12-02 01:23:04 +000013
14%{
15#include "Record.h"
Chris Lattnerfc06bf02003-07-30 04:26:44 +000016#include "Support/StringExtras.h"
Chris Lattnere62c1182002-12-02 01:23:04 +000017#include <algorithm>
Chris Lattnerfc06bf02003-07-30 04:26:44 +000018#include <cstdio>
Chris Lattnere62c1182002-12-02 01:23:04 +000019#define YYERROR_VERBOSE 1
20
21int yyerror(const char *ErrorMsg);
22int yylex();
Brian Gaeked0fde302003-11-11 22:41:34 +000023
24namespace llvm {
25
Chris Lattnere62c1182002-12-02 01:23:04 +000026extern int Filelineno;
Chris Lattnere62c1182002-12-02 01:23:04 +000027static Record *CurRec = 0;
28
29typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
30
Chris Lattner42aa89e2003-08-04 04:56:53 +000031struct LetRecord {
Chris Lattner60094252003-08-03 13:58:01 +000032 std::string Name;
33 std::vector<unsigned> Bits;
34 Init *Value;
35 bool HasBits;
Chris Lattner42aa89e2003-08-04 04:56:53 +000036 LetRecord(const std::string &N, std::vector<unsigned> *B, Init *V)
Chris Lattner60094252003-08-03 13:58:01 +000037 : Name(N), Value(V), HasBits(B != 0) {
38 if (HasBits) Bits = *B;
39 }
40};
41
Chris Lattner42aa89e2003-08-04 04:56:53 +000042static std::vector<std::vector<LetRecord> > LetStack;
Chris Lattner60094252003-08-03 13:58:01 +000043
Chris Lattnere62c1182002-12-02 01:23:04 +000044
Chris Lattner7dff0532003-07-30 20:56:47 +000045extern std::ostream &err();
Chris Lattnere62c1182002-12-02 01:23:04 +000046
47static void addValue(const RecordVal &RV) {
Chris Lattner60094252003-08-03 13:58:01 +000048 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
49 // The value already exists in the class, treat this as a set...
50 if (ERV->setValue(RV.getValue())) {
51 err() << "New definition of '" << RV.getName() << "' of type '"
52 << *RV.getType() << "' is incompatible with previous "
53 << "definition of type '" << *ERV->getType() << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000054 exit(1);
Chris Lattner60094252003-08-03 13:58:01 +000055 }
56 } else {
57 CurRec->addValue(RV);
Chris Lattnere62c1182002-12-02 01:23:04 +000058 }
Chris Lattnere62c1182002-12-02 01:23:04 +000059}
60
61static void addSuperClass(Record *SC) {
62 if (CurRec->isSubClassOf(SC)) {
63 err() << "Already subclass of '" << SC->getName() << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000064 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +000065 }
66 CurRec->addSuperClass(SC);
67}
68
69static void setValue(const std::string &ValName,
70 std::vector<unsigned> *BitList, Init *V) {
Chris Lattner81d50ad2004-02-28 17:31:28 +000071 if (!V) return;
Chris Lattnere62c1182002-12-02 01:23:04 +000072
73 RecordVal *RV = CurRec->getValue(ValName);
74 if (RV == 0) {
75 err() << "Value '" << ValName << "' unknown!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000076 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +000077 }
Chris Lattner81d50ad2004-02-28 17:31:28 +000078
79 // Do not allow assignments like 'X = X'. This will just cause infinite loops
80 // in the resolution machinery.
81 if (!BitList)
82 if (VarInit *VI = dynamic_cast<VarInit*>(V))
83 if (VI->getName() == ValName)
84 return;
Chris Lattnere62c1182002-12-02 01:23:04 +000085
86 // If we are assigning to a subset of the bits in the value... then we must be
87 // assigning to a field of BitsRecTy, which must have a BitsInit
88 // initializer...
89 //
90 if (BitList) {
91 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
92 if (CurVal == 0) {
93 err() << "Value '" << ValName << "' is not a bits type!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000094 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +000095 }
96
97 // Convert the incoming value to a bits type of the appropriate size...
98 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
99 if (BI == 0) {
100 V->convertInitializerTo(new BitsRecTy(BitList->size()));
101 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000102 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000103 }
104
105 // We should have a BitsInit type now...
106 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
107 BitsInit *BInit = (BitsInit*)BI;
108
109 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
110
Chris Lattnerade0de92002-12-06 03:55:39 +0000111 // Loop over bits, assigning values as appropriate...
Chris Lattnere62c1182002-12-02 01:23:04 +0000112 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
113 unsigned Bit = (*BitList)[i];
Chris Lattner28c2d402002-12-06 04:42:16 +0000114 if (NewVal->getBit(Bit)) {
115 err() << "Cannot set bit #" << Bit << " of value '" << ValName
Chris Lattnerade0de92002-12-06 03:55:39 +0000116 << "' more than once!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000117 exit(1);
Chris Lattnerade0de92002-12-06 03:55:39 +0000118 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000119 NewVal->setBit(Bit, BInit->getBit(i));
120 }
Chris Lattnerade0de92002-12-06 03:55:39 +0000121
122 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
123 if (NewVal->getBit(i) == 0)
124 NewVal->setBit(i, CurVal->getBit(i));
125
Chris Lattnere62c1182002-12-02 01:23:04 +0000126 V = NewVal;
127 }
128
129 if (RV->setValue(V)) {
130 err() << "Value '" << ValName << "' of type '" << *RV->getType()
131 << "' is incompatible with initializer '" << *V << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000132 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000133 }
134}
135
136static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
137 // Add all of the values in the subclass into the current class...
138 const std::vector<RecordVal> &Vals = SC->getValues();
139 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
140 addValue(Vals[i]);
141
142 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
143
144 // Ensure that an appropriate number of template arguments are specified...
145 if (TArgs.size() < TemplateArgs.size()) {
Misha Brukmane3d333e2003-05-20 23:45:36 +0000146 err() << "ERROR: More template args specified than expected!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000147 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000148 } else { // This class expects template arguments...
149 // Loop over all of the template arguments, setting them to the specified
Misha Brukman5560c9d2003-08-18 14:43:39 +0000150 // value or leaving them as the default as necessary.
Chris Lattnere62c1182002-12-02 01:23:04 +0000151 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
152 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
153 // Set it now.
154 setValue(TArgs[i], 0, TemplateArgs[i]);
155 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
156 err() << "ERROR: Value not specified for template argument #"
157 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
158 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000159 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000160 }
161 }
162 }
163
Chris Lattnere62c1182002-12-02 01:23:04 +0000164 // Since everything went well, we can now set the "superclass" list for the
165 // current record.
Chris Lattner81d50ad2004-02-28 17:31:28 +0000166 const std::vector<Record*> &SCs = SC->getSuperClasses();
Chris Lattnere62c1182002-12-02 01:23:04 +0000167 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
168 addSuperClass(SCs[i]);
169 addSuperClass(SC);
170}
171
Brian Gaeked0fde302003-11-11 22:41:34 +0000172} // End llvm namespace
173
174using namespace llvm;
Chris Lattnere62c1182002-12-02 01:23:04 +0000175
176%}
177
178%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000179 std::string* StrVal;
180 int IntVal;
181 llvm::RecTy* Ty;
182 llvm::Init* Initializer;
183 std::vector<llvm::Init*>* FieldList;
184 std::vector<unsigned>* BitList;
185 llvm::Record* Rec;
186 SubClassRefTy* SubClassRef;
187 std::vector<SubClassRefTy>* SubClassList;
188 std::vector<std::pair<llvm::Init*, std::string> >* DagValueList;
Chris Lattnere62c1182002-12-02 01:23:04 +0000189};
190
Chris Lattner42aa89e2003-08-04 04:56:53 +0000191%token INT BIT STRING BITS LIST CODE DAG CLASS DEF FIELD LET IN
Chris Lattnere62c1182002-12-02 01:23:04 +0000192%token <IntVal> INTVAL
Chris Lattnerfb9ea582003-08-10 22:04:25 +0000193%token <StrVal> ID VARNAME STRVAL CODEFRAGMENT
Chris Lattnere62c1182002-12-02 01:23:04 +0000194
195%type <Ty> Type
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000196%type <Rec> ClassInst DefInst Object ObjectBody ClassID
Chris Lattnere62c1182002-12-02 01:23:04 +0000197
198%type <SubClassRef> SubClassRef
199%type <SubClassList> ClassList ClassListNE
200%type <IntVal> OptPrefix
201%type <Initializer> Value OptValue
Chris Lattnerbc21c342003-08-04 20:44:43 +0000202%type <DagValueList> DagArgList DagArgListNE
Chris Lattnere62c1182002-12-02 01:23:04 +0000203%type <FieldList> ValueList ValueListNE
204%type <BitList> BitList OptBitList RBitList
Chris Lattner91290d72003-08-10 22:14:13 +0000205%type <StrVal> Declaration OptID OptVarName
Chris Lattnere62c1182002-12-02 01:23:04 +0000206
207%start File
Brian Gaeked0fde302003-11-11 22:41:34 +0000208
Chris Lattnere62c1182002-12-02 01:23:04 +0000209%%
210
211ClassID : ID {
212 $$ = Records.getClass(*$1);
213 if ($$ == 0) {
214 err() << "Couldn't find class '" << *$1 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000215 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000216 }
217 delete $1;
218 };
219
Chris Lattnere62c1182002-12-02 01:23:04 +0000220
221// TableGen types...
222Type : STRING { // string type
223 $$ = new StringRecTy();
224 } | BIT { // bit type
225 $$ = new BitRecTy();
226 } | BITS '<' INTVAL '>' { // bits<x> type
227 $$ = new BitsRecTy($3);
228 } | INT { // int type
229 $$ = new IntRecTy();
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000230 } | LIST '<' Type '>' { // list<x> type
Chris Lattnere62c1182002-12-02 01:23:04 +0000231 $$ = new ListRecTy($3);
Chris Lattnerf05760d2003-07-30 21:47:42 +0000232 } | CODE { // code type
233 $$ = new CodeRecTy();
Chris Lattner40f71132003-08-04 04:50:57 +0000234 } | DAG { // dag type
235 $$ = new DagRecTy();
Chris Lattnere62c1182002-12-02 01:23:04 +0000236 } | ClassID { // Record Type
237 $$ = new RecordRecTy($1);
238 };
239
240OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
241
242OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
243
244Value : INTVAL {
245 $$ = new IntInit($1);
246 } | STRVAL {
247 $$ = new StringInit(*$1);
248 delete $1;
Chris Lattnere3a1d052003-07-30 22:15:58 +0000249 } | CODEFRAGMENT {
250 $$ = new CodeInit(*$1);
251 delete $1;
Chris Lattnere62c1182002-12-02 01:23:04 +0000252 } | '?' {
253 $$ = new UnsetInit();
254 } | '{' ValueList '}' {
255 BitsInit *Init = new BitsInit($2->size());
256 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
257 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
258 if (Bit == 0) {
259 err() << "Element #" << i << " (" << *(*$2)[i]
260 << ") is not convertable to a bit!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000261 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000262 }
263 Init->setBit($2->size()-i-1, Bit);
264 }
265 $$ = Init;
266 delete $2;
267 } | ID {
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000268 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
Chris Lattnere62c1182002-12-02 01:23:04 +0000269 $$ = new VarInit(*$1, RV->getType());
270 } else if (Record *D = Records.getDef(*$1)) {
271 $$ = new DefInit(D);
272 } else {
273 err() << "Variable not defined: '" << *$1 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000274 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000275 }
276
277 delete $1;
278 } | Value '{' BitList '}' {
279 $$ = $1->convertInitializerBitRange(*$3);
280 if ($$ == 0) {
281 err() << "Invalid bit range for value '" << *$1 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000282 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000283 }
284 delete $3;
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000285 } | '[' ValueList ']' {
Chris Lattnere62c1182002-12-02 01:23:04 +0000286 $$ = new ListInit(*$2);
287 delete $2;
Chris Lattner34a77692002-12-02 16:43:43 +0000288 } | Value '.' ID {
289 if (!$1->getFieldType(*$3)) {
290 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000291 exit(1);
Chris Lattner34a77692002-12-02 16:43:43 +0000292 }
293 $$ = new FieldInit($1, *$3);
294 delete $3;
Chris Lattnerbc21c342003-08-04 20:44:43 +0000295 } | '(' ID DagArgList ')' {
296 Record *D = Records.getDef(*$2);
297 if (D == 0) {
298 err() << "Invalid def '" << *$2 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000299 exit(1);
Chris Lattnerbc21c342003-08-04 20:44:43 +0000300 }
301 $$ = new DagInit(D, *$3);
302 delete $2; delete $3;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000303 } | Value '[' BitList ']' {
304 std::reverse($3->begin(), $3->end());
305 $$ = $1->convertInitListSlice(*$3);
306 if ($$ == 0) {
307 err() << "Invalid list slice for value '" << *$1 << "'!\n";
308 exit(1);
309 }
310 delete $3;
Chris Lattnere62c1182002-12-02 01:23:04 +0000311 };
312
Chris Lattner91290d72003-08-10 22:14:13 +0000313OptVarName : /* empty */ {
314 $$ = new std::string();
Chris Lattnerbc21c342003-08-04 20:44:43 +0000315 }
Chris Lattner91290d72003-08-10 22:14:13 +0000316 | ':' VARNAME {
317 $$ = $2;
318 };
319
320DagArgListNE : Value OptVarName {
321 $$ = new std::vector<std::pair<Init*, std::string> >();
322 $$->push_back(std::make_pair($1, *$2));
323 delete $2;
324 }
325 | DagArgListNE ',' Value OptVarName {
326 $1->push_back(std::make_pair($3, *$4));
327 delete $4;
328 $$ = $1;
Chris Lattnerbc21c342003-08-04 20:44:43 +0000329 };
330
331DagArgList : /*empty*/ {
Chris Lattner91290d72003-08-10 22:14:13 +0000332 $$ = new std::vector<std::pair<Init*, std::string> >();
Chris Lattnerbc21c342003-08-04 20:44:43 +0000333 }
334 | DagArgListNE { $$ = $1; };
335
336
Chris Lattnere62c1182002-12-02 01:23:04 +0000337RBitList : INTVAL {
338 $$ = new std::vector<unsigned>();
339 $$->push_back($1);
340 } | INTVAL '-' INTVAL {
Chris Lattnerb0fef642004-07-26 23:21:34 +0000341 if ($1 < 0 || $3 < 0) {
342 err() << "Invalid range: " << $1 << "-" << $3 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000343 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000344 }
345 $$ = new std::vector<unsigned>();
Chris Lattnerb0fef642004-07-26 23:21:34 +0000346 if ($1 < $3) {
347 for (int i = $1; i <= $3; ++i)
348 $$->push_back(i);
349 } else {
350 for (int i = $1; i >= $3; --i)
351 $$->push_back(i);
352 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000353 } | INTVAL INTVAL {
354 $2 = -$2;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000355 if ($1 < 0 || $2 < 0) {
356 err() << "Invalid range: " << $1 << "-" << $2 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000357 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000358 }
359 $$ = new std::vector<unsigned>();
Chris Lattnerb0fef642004-07-26 23:21:34 +0000360 if ($1 < $2) {
361 for (int i = $1; i <= $2; ++i)
362 $$->push_back(i);
363 } else {
364 for (int i = $1; i >= $2; --i)
365 $$->push_back(i);
366 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000367 } | RBitList ',' INTVAL {
368 ($$=$1)->push_back($3);
369 } | RBitList ',' INTVAL '-' INTVAL {
Chris Lattnerb0fef642004-07-26 23:21:34 +0000370 if ($3 < 0 || $5 < 0) {
371 err() << "Invalid range: " << $3 << "-" << $5 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000372 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000373 }
374 $$ = $1;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000375 if ($3 < $5) {
376 for (int i = $3; i <= $5; ++i)
377 $$->push_back(i);
378 } else {
379 for (int i = $3; i >= $5; --i)
380 $$->push_back(i);
381 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000382 } | RBitList ',' INTVAL INTVAL {
383 $4 = -$4;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000384 if ($3 < 0 || $4 < 0) {
385 err() << "Invalid range: " << $3 << "-" << $4 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000386 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000387 }
388 $$ = $1;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000389 if ($3 < $4) {
390 for (int i = $3; i <= $4; ++i)
391 $$->push_back(i);
392 } else {
393 for (int i = $3; i >= $4; --i)
394 $$->push_back(i);
395 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000396 };
397
398BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
399
400OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
401
402
403
404ValueList : /*empty*/ {
405 $$ = new std::vector<Init*>();
406 } | ValueListNE {
407 $$ = $1;
408 };
409
410ValueListNE : Value {
411 $$ = new std::vector<Init*>();
412 $$->push_back($1);
413 } | ValueListNE ',' Value {
414 ($$ = $1)->push_back($3);
415 };
416
417Declaration : OptPrefix Type ID OptValue {
418 addValue(RecordVal(*$3, $2, $1));
419 setValue(*$3, 0, $4);
420 $$ = $3;
421};
422
423BodyItem : Declaration ';' {
424 delete $1;
Chris Lattner42aa89e2003-08-04 04:56:53 +0000425} | LET ID OptBitList '=' Value ';' {
Chris Lattnere62c1182002-12-02 01:23:04 +0000426 setValue(*$2, $3, $5);
427 delete $2;
428 delete $3;
429};
430
431BodyList : /*empty*/ | BodyList BodyItem;
432Body : ';' | '{' BodyList '}';
433
434SubClassRef : ClassID {
435 $$ = new SubClassRefTy($1, new std::vector<Init*>());
436 } | ClassID '<' ValueListNE '>' {
437 $$ = new SubClassRefTy($1, $3);
438 };
439
440ClassListNE : SubClassRef {
441 $$ = new std::vector<SubClassRefTy>();
442 $$->push_back(*$1);
443 delete $1;
444 }
445 | ClassListNE ',' SubClassRef {
446 ($$=$1)->push_back(*$3);
447 delete $3;
448 };
449
450ClassList : /*empty */ {
451 $$ = new std::vector<SubClassRefTy>();
452 }
453 | ':' ClassListNE {
454 $$ = $2;
455 };
456
457DeclListNE : Declaration {
458 CurRec->addTemplateArg(*$1);
459 delete $1;
460} | DeclListNE ',' Declaration {
461 CurRec->addTemplateArg(*$3);
462 delete $3;
463};
464
465TemplateArgList : '<' DeclListNE '>' {};
466OptTemplateArgList : /*empty*/ | TemplateArgList;
467
Chris Lattnerfc06bf02003-07-30 04:26:44 +0000468OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
469
470ObjectBody : OptID {
471 static unsigned AnonCounter = 0;
472 if ($1->empty())
473 *$1 = "anonymous."+utostr(AnonCounter++);
Chris Lattnere62c1182002-12-02 01:23:04 +0000474 CurRec = new Record(*$1);
475 delete $1;
476 } OptTemplateArgList ClassList {
477 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
478 addSubClass((*$4)[i].first, *(*$4)[i].second);
Chris Lattnerbfce0562003-07-30 04:56:05 +0000479 // Delete the template arg values for the class
480 delete (*$4)[i].second;
481 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000482
483 // Process any variables on the set stack...
Chris Lattner42aa89e2003-08-04 04:56:53 +0000484 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
485 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
486 setValue(LetStack[i][j].Name,
487 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
488 LetStack[i][j].Value);
Chris Lattnere62c1182002-12-02 01:23:04 +0000489 } Body {
490 CurRec->resolveReferences();
Chris Lattnerbfce0562003-07-30 04:56:05 +0000491
492 // Now that all of the references have been resolved, we can delete template
493 // arguments for superclasses, so they don't pollute our record, and so that
494 // their names won't conflict with later uses of the name...
495 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
496 Record *SuperClass = (*$4)[i].first;
497 for (unsigned i = 0, e = SuperClass->getTemplateArgs().size(); i != e; ++i)
Chris Lattner8a4045e2004-02-28 17:41:48 +0000498 if (!CurRec->isTemplateArg(SuperClass->getTemplateArgs()[i]))
499 CurRec->removeValue(SuperClass->getTemplateArgs()[i]);
Chris Lattnerbfce0562003-07-30 04:56:05 +0000500 }
501 delete $4; // Delete the class list...
502
Chris Lattnere62c1182002-12-02 01:23:04 +0000503 $$ = CurRec;
504 CurRec = 0;
505};
506
507ClassInst : CLASS ObjectBody {
508 if (Records.getClass($2->getName())) {
509 err() << "Class '" << $2->getName() << "' already defined!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000510 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000511 }
512 Records.addClass($$ = $2);
513};
514
515DefInst : DEF ObjectBody {
Chris Lattner554af5c2003-07-30 04:31:17 +0000516 if (!$2->getTemplateArgs().empty()) {
517 err() << "Def '" << $2->getName()
518 << "' is not permitted to have template arguments!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000519 exit(1);
Chris Lattner554af5c2003-07-30 04:31:17 +0000520 }
521 // If ObjectBody has template arguments, it's an error.
Chris Lattnere62c1182002-12-02 01:23:04 +0000522 if (Records.getDef($2->getName())) {
523 err() << "Def '" << $2->getName() << "' already defined!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000524 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000525 }
526 Records.addDef($$ = $2);
527};
528
529
530Object : ClassInst | DefInst;
531
Chris Lattner42aa89e2003-08-04 04:56:53 +0000532LETItem : ID OptBitList '=' Value {
533 LetStack.back().push_back(LetRecord(*$1, $2, $4));
Chris Lattner60094252003-08-03 13:58:01 +0000534 delete $1; delete $2;
Chris Lattner2e724542003-07-28 03:49:40 +0000535};
536
Chris Lattner42aa89e2003-08-04 04:56:53 +0000537LETList : LETItem | LETList ',' LETItem;
Chris Lattner60094252003-08-03 13:58:01 +0000538
Chris Lattner42aa89e2003-08-04 04:56:53 +0000539// LETCommand - A 'LET' statement start...
540LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
Chris Lattner60094252003-08-03 13:58:01 +0000541
Chris Lattner2e724542003-07-28 03:49:40 +0000542// Support Set commands wrapping objects... both with and without braces.
Chris Lattner42aa89e2003-08-04 04:56:53 +0000543Object : LETCommand '{' ObjectList '}' {
544 LetStack.pop_back();
Chris Lattner2e724542003-07-28 03:49:40 +0000545 }
Chris Lattner42aa89e2003-08-04 04:56:53 +0000546 | LETCommand Object {
547 LetStack.pop_back();
Chris Lattnere62c1182002-12-02 01:23:04 +0000548 };
549
550ObjectList : Object {} | ObjectList Object {};
551
552File : ObjectList {};
553
554%%
555
556int yyerror(const char *ErrorMsg) {
557 err() << "Error parsing: " << ErrorMsg << "\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000558 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000559}