1 | #include <cassert> |
---|
2 | #include <algorithm> |
---|
3 | #include <iterator> |
---|
4 | #include "utility.h" |
---|
5 | #include "TypeData.h" |
---|
6 | #include "SynTree/Type.h" |
---|
7 | #include "SynTree/Declaration.h" |
---|
8 | #include "SynTree/Expression.h" |
---|
9 | #include "SynTree/Statement.h" |
---|
10 | |
---|
11 | |
---|
12 | TypeData::TypeData( Kind k ) |
---|
13 | : kind( k ), base( 0 ), forall( 0 ) |
---|
14 | { |
---|
15 | switch( kind ) { |
---|
16 | case Unknown: |
---|
17 | case Pointer: |
---|
18 | case EnumConstant: |
---|
19 | // nothing else to initialize |
---|
20 | break; |
---|
21 | |
---|
22 | case Basic: |
---|
23 | basic = new Basic_t; |
---|
24 | break; |
---|
25 | |
---|
26 | case Array: |
---|
27 | array = new Array_t; |
---|
28 | array->dimension = 0; |
---|
29 | array->isVarLen = false; |
---|
30 | array->isStatic = false; |
---|
31 | break; |
---|
32 | |
---|
33 | case Function: |
---|
34 | function = new Function_t; |
---|
35 | function->params = 0; |
---|
36 | function->idList = 0; |
---|
37 | function->oldDeclList = 0; |
---|
38 | function->body = 0; |
---|
39 | function->hasBody = false; |
---|
40 | break; |
---|
41 | |
---|
42 | case Aggregate: |
---|
43 | aggregate = new Aggregate_t; |
---|
44 | aggregate->params = 0; |
---|
45 | aggregate->actuals = 0; |
---|
46 | aggregate->members = 0; |
---|
47 | break; |
---|
48 | |
---|
49 | case AggregateInst: |
---|
50 | aggInst = new AggInst_t; |
---|
51 | aggInst->aggregate = 0; |
---|
52 | aggInst->params = 0; |
---|
53 | break; |
---|
54 | |
---|
55 | case Enum: |
---|
56 | enumeration = new Enumeration_t; |
---|
57 | enumeration->constants = 0; |
---|
58 | break; |
---|
59 | |
---|
60 | case Symbolic: |
---|
61 | case SymbolicInst: |
---|
62 | symbolic = new Symbolic_t; |
---|
63 | symbolic->params = 0; |
---|
64 | symbolic->actuals = 0; |
---|
65 | symbolic->assertions = 0; |
---|
66 | break; |
---|
67 | |
---|
68 | case Variable: |
---|
69 | variable = new Variable_t; |
---|
70 | variable->tyClass = DeclarationNode::Type; |
---|
71 | variable->assertions = 0; |
---|
72 | break; |
---|
73 | |
---|
74 | case Tuple: |
---|
75 | tuple = new Tuple_t; |
---|
76 | tuple->members = 0; |
---|
77 | break; |
---|
78 | |
---|
79 | case Typeof: |
---|
80 | typeexpr = new Typeof_t; |
---|
81 | typeexpr->expr = 0; |
---|
82 | break; |
---|
83 | |
---|
84 | case Attr: |
---|
85 | attr = new Attr_t; |
---|
86 | attr->expr = 0; |
---|
87 | attr->type = 0; |
---|
88 | break; |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | TypeData::~TypeData() |
---|
93 | { |
---|
94 | delete base; |
---|
95 | delete forall; |
---|
96 | |
---|
97 | switch( kind ) { |
---|
98 | case Unknown: |
---|
99 | case Pointer: |
---|
100 | case EnumConstant: |
---|
101 | // nothing to destroy |
---|
102 | break; |
---|
103 | |
---|
104 | case Basic: |
---|
105 | delete basic; |
---|
106 | break; |
---|
107 | |
---|
108 | case Array: |
---|
109 | delete array->dimension; |
---|
110 | delete array; |
---|
111 | break; |
---|
112 | |
---|
113 | case Function: |
---|
114 | delete function->params; |
---|
115 | delete function->idList; |
---|
116 | delete function->oldDeclList; |
---|
117 | delete function->body; |
---|
118 | delete function; |
---|
119 | break; |
---|
120 | |
---|
121 | case Aggregate: |
---|
122 | delete aggregate->params; |
---|
123 | delete aggregate->actuals; |
---|
124 | delete aggregate->members; |
---|
125 | delete aggregate; |
---|
126 | break; |
---|
127 | |
---|
128 | case AggregateInst: |
---|
129 | delete aggInst->aggregate; |
---|
130 | delete aggInst->params; |
---|
131 | delete aggInst; |
---|
132 | break; |
---|
133 | |
---|
134 | case Enum: |
---|
135 | delete enumeration->constants; |
---|
136 | delete enumeration; |
---|
137 | break; |
---|
138 | |
---|
139 | case Symbolic: |
---|
140 | case SymbolicInst: |
---|
141 | delete symbolic->params; |
---|
142 | delete symbolic->actuals; |
---|
143 | delete symbolic->assertions; |
---|
144 | delete symbolic; |
---|
145 | break; |
---|
146 | |
---|
147 | case Variable: |
---|
148 | delete variable->assertions; |
---|
149 | delete variable; |
---|
150 | break; |
---|
151 | |
---|
152 | case Tuple: |
---|
153 | delete tuple->members; |
---|
154 | delete tuple; |
---|
155 | break; |
---|
156 | |
---|
157 | case Typeof: |
---|
158 | delete typeexpr->expr; |
---|
159 | delete typeexpr; |
---|
160 | break; |
---|
161 | |
---|
162 | case Attr: |
---|
163 | delete attr->expr; |
---|
164 | delete attr->type; |
---|
165 | delete attr; |
---|
166 | break; |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | TypeData * |
---|
171 | TypeData::clone() const |
---|
172 | { |
---|
173 | TypeData *newtype = new TypeData( kind ); |
---|
174 | newtype->qualifiers = qualifiers; |
---|
175 | newtype->base = maybeClone( base ); |
---|
176 | newtype->forall = maybeClone( forall ); |
---|
177 | |
---|
178 | switch( kind ) { |
---|
179 | case Unknown: |
---|
180 | case EnumConstant: |
---|
181 | case Pointer: |
---|
182 | // nothing else to copy |
---|
183 | break; |
---|
184 | |
---|
185 | case Basic: |
---|
186 | newtype->basic->typeSpec = basic->typeSpec; |
---|
187 | newtype->basic->modifiers = basic->modifiers; |
---|
188 | break; |
---|
189 | |
---|
190 | case Array: |
---|
191 | newtype->array->dimension = maybeClone( array->dimension ); |
---|
192 | newtype->array->isVarLen = array->isVarLen; |
---|
193 | newtype->array->isStatic = array->isStatic; |
---|
194 | break; |
---|
195 | |
---|
196 | case Function: |
---|
197 | newtype->function->params = maybeClone( function->params ); |
---|
198 | newtype->function->idList = maybeClone( function->idList ); |
---|
199 | newtype->function->oldDeclList = maybeClone( function->oldDeclList ); |
---|
200 | newtype->function->body = maybeClone( function->body ); |
---|
201 | newtype->function->hasBody = function->hasBody; |
---|
202 | newtype->function->newStyle = function->newStyle; |
---|
203 | break; |
---|
204 | |
---|
205 | case Aggregate: |
---|
206 | newtype->aggregate->params = maybeClone( aggregate->params ); |
---|
207 | newtype->aggregate->actuals = maybeClone( aggregate->actuals ); |
---|
208 | newtype->aggregate->members = maybeClone( aggregate->members ); |
---|
209 | newtype->aggregate->name = aggregate->name; |
---|
210 | newtype->aggregate->kind = aggregate->kind; |
---|
211 | break; |
---|
212 | |
---|
213 | case AggregateInst: |
---|
214 | newtype->aggInst->aggregate = maybeClone( aggInst->aggregate ); |
---|
215 | newtype->aggInst->params = maybeClone( aggInst->params ); |
---|
216 | break; |
---|
217 | |
---|
218 | case Enum: |
---|
219 | newtype->enumeration->name = enumeration->name; |
---|
220 | newtype->enumeration->constants = maybeClone( enumeration->constants ); |
---|
221 | break; |
---|
222 | |
---|
223 | case Symbolic: |
---|
224 | case SymbolicInst: |
---|
225 | newtype->symbolic->params = maybeClone( symbolic->params ); |
---|
226 | newtype->symbolic->actuals = maybeClone( symbolic->actuals ); |
---|
227 | newtype->symbolic->assertions = maybeClone( symbolic->assertions ); |
---|
228 | newtype->symbolic->isTypedef = symbolic->isTypedef; |
---|
229 | newtype->symbolic->name = symbolic->name; |
---|
230 | break; |
---|
231 | |
---|
232 | case Variable: |
---|
233 | newtype->variable->assertions = maybeClone( variable->assertions ); |
---|
234 | newtype->variable->name = variable->name; |
---|
235 | newtype->variable->tyClass = variable->tyClass; |
---|
236 | break; |
---|
237 | |
---|
238 | case Tuple: |
---|
239 | newtype->tuple->members = maybeClone( tuple->members ); |
---|
240 | break; |
---|
241 | |
---|
242 | case Typeof: |
---|
243 | newtype->typeexpr->expr = maybeClone( typeexpr->expr ); |
---|
244 | break; |
---|
245 | |
---|
246 | case Attr: |
---|
247 | newtype->attr->expr = maybeClone( attr->expr ); |
---|
248 | newtype->attr->type = maybeClone( attr->type ); |
---|
249 | break; |
---|
250 | } |
---|
251 | return newtype; |
---|
252 | } |
---|
253 | |
---|
254 | void |
---|
255 | TypeData::print( std::ostream &os, int indent ) const |
---|
256 | { |
---|
257 | using std::endl; |
---|
258 | using std::string; |
---|
259 | |
---|
260 | printEnums( qualifiers.begin(), qualifiers.end(), DeclarationNode::qualifierName, os ); |
---|
261 | |
---|
262 | if( forall ) { |
---|
263 | os << "forall " << endl; |
---|
264 | forall->printList( os, indent+4 ); |
---|
265 | } |
---|
266 | |
---|
267 | switch( kind ) { |
---|
268 | case Unknown: |
---|
269 | os << "entity of unknown type "; |
---|
270 | break; |
---|
271 | |
---|
272 | case Pointer: |
---|
273 | os << "pointer "; |
---|
274 | if( base ) { |
---|
275 | os << "to "; |
---|
276 | base->print( os, indent ); |
---|
277 | } |
---|
278 | break; |
---|
279 | |
---|
280 | case EnumConstant: |
---|
281 | os << "enumeration constant "; |
---|
282 | break; |
---|
283 | |
---|
284 | case Basic: |
---|
285 | printEnums( basic->modifiers.begin(), basic->modifiers.end(), DeclarationNode::modifierName, os ); |
---|
286 | printEnums( basic->typeSpec.begin(), basic->typeSpec.end(), DeclarationNode::basicTypeName, os ); |
---|
287 | break; |
---|
288 | |
---|
289 | case Array: |
---|
290 | if( array->isStatic ) { |
---|
291 | os << "static "; |
---|
292 | } |
---|
293 | if( array->dimension ) { |
---|
294 | os << "array of "; |
---|
295 | array->dimension->printOneLine( os, indent ); |
---|
296 | } else if ( array->isVarLen ) { |
---|
297 | os << "variable-length array of "; |
---|
298 | } else { |
---|
299 | os << "open array of "; |
---|
300 | } |
---|
301 | if( base ) { |
---|
302 | base->print( os, indent ); |
---|
303 | } |
---|
304 | break; |
---|
305 | |
---|
306 | case Function: |
---|
307 | os << "function" << endl; |
---|
308 | if ( function->params ) { |
---|
309 | os << string( indent+2, ' ' ) << "with parameters " << endl; |
---|
310 | function->params->printList( os, indent+4 ); |
---|
311 | } else { |
---|
312 | os << string( indent+2, ' ' ) << "with no parameters " << endl; |
---|
313 | } |
---|
314 | if ( function->idList ) { |
---|
315 | os << string( indent+2, ' ' ) << "with old-style identifier list " << endl; |
---|
316 | function->idList->printList( os, indent+4 ); |
---|
317 | } |
---|
318 | if ( function->oldDeclList ) { |
---|
319 | os << string( indent+2, ' ' ) << "with old-style declaration list " << endl; |
---|
320 | function->oldDeclList->printList( os, indent+4 ); |
---|
321 | } |
---|
322 | os << string( indent+2, ' ' ) << "returning "; |
---|
323 | if ( base ) { |
---|
324 | base->print( os, indent+4 ); |
---|
325 | } else { |
---|
326 | os << "nothing "; |
---|
327 | } |
---|
328 | os << endl; |
---|
329 | if ( function->hasBody ) { |
---|
330 | os << string( indent+2, ' ' ) << "with body " << endl; |
---|
331 | } |
---|
332 | if ( function->body ) { |
---|
333 | function->body->printList( os, indent+2 ); |
---|
334 | } |
---|
335 | break; |
---|
336 | |
---|
337 | case Aggregate: |
---|
338 | os << DeclarationNode::tyConName[ aggregate->kind ] << ' ' << aggregate->name << endl; |
---|
339 | if( aggregate->params ) { |
---|
340 | os << string( indent+2, ' ' ) << "with type parameters " << endl; |
---|
341 | aggregate->params->printList( os, indent+4 ); |
---|
342 | } |
---|
343 | if( aggregate->actuals ) { |
---|
344 | os << string( indent+2, ' ' ) << "instantiated with actual parameters " << endl; |
---|
345 | aggregate->actuals->printList( os, indent+4 ); |
---|
346 | } |
---|
347 | if( aggregate->members ) { |
---|
348 | os << string( indent+2, ' ' ) << "with members " << endl; |
---|
349 | aggregate->members->printList( os, indent+4 ); |
---|
350 | /// } else { |
---|
351 | /// os << string( indent+2, ' ' ) << "with no members " << endl; |
---|
352 | } |
---|
353 | break; |
---|
354 | |
---|
355 | case AggregateInst: |
---|
356 | if( aggInst->aggregate ) { |
---|
357 | os << "instance of " ; |
---|
358 | aggInst->aggregate->print( os, indent ); |
---|
359 | } else { |
---|
360 | os << "instance of an unspecified aggregate "; |
---|
361 | } |
---|
362 | if( aggInst->params ) { |
---|
363 | os << string( indent+2, ' ' ) << "with parameters " << endl; |
---|
364 | aggInst->params->printList( os, indent+2 ); |
---|
365 | } |
---|
366 | break; |
---|
367 | |
---|
368 | case Enum: |
---|
369 | os << "enumeration "; |
---|
370 | if( enumeration->constants ) { |
---|
371 | os << "with constants" << endl; |
---|
372 | enumeration->constants->printList( os, indent+2 ); |
---|
373 | } |
---|
374 | break; |
---|
375 | |
---|
376 | case SymbolicInst: |
---|
377 | os << "instance of type " << symbolic->name; |
---|
378 | if( symbolic->actuals ) { |
---|
379 | os << " with parameters" << endl; |
---|
380 | symbolic->actuals->printList( os, indent + 2 ); |
---|
381 | } |
---|
382 | break; |
---|
383 | |
---|
384 | case Symbolic: |
---|
385 | if( symbolic->isTypedef ) { |
---|
386 | os << "typedef definition "; |
---|
387 | } else { |
---|
388 | os << "type definition "; |
---|
389 | } |
---|
390 | if( symbolic->params ) { |
---|
391 | os << endl << string( indent+2, ' ' ) << "with parameters" << endl; |
---|
392 | symbolic->params->printList( os, indent + 2 ); |
---|
393 | } |
---|
394 | if( symbolic->assertions ) { |
---|
395 | os << endl << string( indent+2, ' ' ) << "with assertions" << endl; |
---|
396 | symbolic->assertions->printList( os, indent + 4 ); |
---|
397 | os << string( indent+2, ' ' ); |
---|
398 | } |
---|
399 | if( base ) { |
---|
400 | os << "for "; |
---|
401 | base->print( os, indent + 2 ); |
---|
402 | } |
---|
403 | break; |
---|
404 | |
---|
405 | case Variable: |
---|
406 | os << DeclarationNode::typeClassName[ variable->tyClass ] << " variable "; |
---|
407 | if( variable->assertions ) { |
---|
408 | os << endl << string( indent+2, ' ' ) << "with assertions" << endl; |
---|
409 | variable->assertions->printList( os, indent + 4 ); |
---|
410 | os << string( indent+2, ' ' ); |
---|
411 | } |
---|
412 | break; |
---|
413 | |
---|
414 | case Tuple: |
---|
415 | os << "tuple "; |
---|
416 | if( tuple->members ) { |
---|
417 | os << "with members " << endl; |
---|
418 | tuple->members->printList( os, indent + 2 ); |
---|
419 | } |
---|
420 | break; |
---|
421 | |
---|
422 | case Typeof: |
---|
423 | os << "type-of expression "; |
---|
424 | if( typeexpr->expr ) { |
---|
425 | typeexpr->expr->print( os, indent + 2 ); |
---|
426 | } |
---|
427 | break; |
---|
428 | |
---|
429 | case Attr: |
---|
430 | os << "attribute type decl " << attr->name << " applied to "; |
---|
431 | if( attr->expr ) { |
---|
432 | attr->expr->print( os, indent + 2 ); |
---|
433 | } |
---|
434 | if( attr->type ) { |
---|
435 | attr->type->print( os, indent + 2 ); |
---|
436 | } |
---|
437 | break; |
---|
438 | } |
---|
439 | } |
---|
440 | |
---|
441 | TypeData * |
---|
442 | TypeData::extractAggregate( bool toplevel ) const |
---|
443 | { |
---|
444 | TypeData *ret = 0; |
---|
445 | |
---|
446 | switch( kind ) { |
---|
447 | case Aggregate: |
---|
448 | if( !toplevel && aggregate->members ) { |
---|
449 | ret = clone(); |
---|
450 | ret->qualifiers.clear(); |
---|
451 | } |
---|
452 | break; |
---|
453 | |
---|
454 | case Enum: |
---|
455 | if( !toplevel && enumeration->constants ) { |
---|
456 | ret = clone(); |
---|
457 | ret->qualifiers.clear(); |
---|
458 | } |
---|
459 | break; |
---|
460 | |
---|
461 | case AggregateInst: |
---|
462 | if( aggInst->aggregate ) { |
---|
463 | ret = aggInst->aggregate->extractAggregate( false ); |
---|
464 | } |
---|
465 | break; |
---|
466 | |
---|
467 | default: |
---|
468 | if( base ) { |
---|
469 | ret = base->extractAggregate( false ); |
---|
470 | } |
---|
471 | } |
---|
472 | return ret; |
---|
473 | } |
---|
474 | |
---|
475 | void |
---|
476 | buildForall( const DeclarationNode *firstNode, std::list< TypeDecl* > &outputList ) |
---|
477 | { |
---|
478 | |
---|
479 | buildList( firstNode, outputList ); |
---|
480 | for( std::list< TypeDecl* >::iterator i = outputList.begin(); i != outputList.end(); ++i ) { |
---|
481 | if( (*i)->get_kind() == TypeDecl::Any ) { |
---|
482 | FunctionType *assignType = new FunctionType( Type::Qualifiers(), false ); |
---|
483 | assignType->get_parameters().push_back( new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ) ), 0 ) ); |
---|
484 | assignType->get_parameters().push_back( new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) ); |
---|
485 | assignType->get_returnVals().push_back( new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, new TypeInstType( Type::Qualifiers(), (*i)->get_name(), *i ), 0 ) ); |
---|
486 | (*i)->get_assertions().push_front( new FunctionDecl( "?=?", Declaration::NoStorageClass, LinkageSpec::Cforall, assignType, 0, false ) ); |
---|
487 | } |
---|
488 | } |
---|
489 | } |
---|
490 | |
---|
491 | Declaration * |
---|
492 | TypeData::buildDecl( std::string name, Declaration::StorageClass sc, Expression *bitfieldWidth, bool isInline, LinkageSpec::Type linkage, Initializer *init ) const |
---|
493 | { |
---|
494 | |
---|
495 | if ( kind == TypeData::Function ) { |
---|
496 | FunctionDecl *decl; |
---|
497 | if( function->hasBody ) { |
---|
498 | if( function->body ) { |
---|
499 | Statement *stmt = function->body->build(); |
---|
500 | CompoundStmt *body = dynamic_cast< CompoundStmt* >( stmt ); |
---|
501 | assert( body ); |
---|
502 | decl = new FunctionDecl( name, sc, linkage, buildFunction(), body, isInline ); |
---|
503 | } else { |
---|
504 | // std::list<Label> ls; |
---|
505 | decl = new FunctionDecl( name, sc, linkage, buildFunction(), new CompoundStmt( std::list<Label>() ), isInline ); |
---|
506 | } |
---|
507 | } else { |
---|
508 | decl = new FunctionDecl( name, sc, linkage, buildFunction(), 0, isInline ); |
---|
509 | } |
---|
510 | for( DeclarationNode *cur = function->idList; cur != 0; cur = dynamic_cast< DeclarationNode* >( cur->get_link() ) ) { |
---|
511 | if( cur->get_name() != "" ) { |
---|
512 | decl->get_oldIdents().insert( decl->get_oldIdents().end(), cur->get_name() ); |
---|
513 | } |
---|
514 | } |
---|
515 | buildList( function->oldDeclList, decl->get_oldDecls() ); |
---|
516 | return decl; |
---|
517 | } else if ( kind == TypeData::Aggregate ) { |
---|
518 | return buildAggregate(); |
---|
519 | } else if ( kind == TypeData::Enum ) { |
---|
520 | return buildEnum(); |
---|
521 | } else if ( kind == TypeData::Symbolic ) { |
---|
522 | return buildSymbolic( name, sc ); |
---|
523 | } else if ( kind == TypeData::Variable ) { |
---|
524 | return buildVariable(); |
---|
525 | } else { |
---|
526 | if( isInline ) { |
---|
527 | throw SemanticError( "invalid inline specification in declaration of ", this ); |
---|
528 | } else { |
---|
529 | return new ObjectDecl( name, sc, linkage, bitfieldWidth, build(), init ); |
---|
530 | } |
---|
531 | } |
---|
532 | return 0; |
---|
533 | } |
---|
534 | |
---|
535 | Type * |
---|
536 | TypeData::build() const |
---|
537 | { |
---|
538 | |
---|
539 | switch( kind ) { |
---|
540 | case Unknown: |
---|
541 | // fill in implicit int |
---|
542 | return new BasicType( buildQualifiers(), BasicType::SignedInt ); |
---|
543 | |
---|
544 | case Basic: |
---|
545 | return buildBasicType(); |
---|
546 | |
---|
547 | case Pointer: |
---|
548 | return buildPointer(); |
---|
549 | |
---|
550 | case Array: |
---|
551 | return buildArray(); |
---|
552 | |
---|
553 | case Function: |
---|
554 | return buildFunction(); |
---|
555 | |
---|
556 | case AggregateInst: |
---|
557 | return buildAggInst(); |
---|
558 | |
---|
559 | case EnumConstant: |
---|
560 | // the name gets filled in later -- by SymTab::Validate |
---|
561 | return new EnumInstType( buildQualifiers(), "" ); |
---|
562 | |
---|
563 | case SymbolicInst: |
---|
564 | return buildSymbolicInst();; |
---|
565 | |
---|
566 | case Tuple: |
---|
567 | return buildTuple(); |
---|
568 | |
---|
569 | case Typeof: |
---|
570 | return buildTypeof(); |
---|
571 | |
---|
572 | case Attr: |
---|
573 | return buildAttr(); |
---|
574 | |
---|
575 | case Symbolic: |
---|
576 | case Enum: |
---|
577 | case Aggregate: |
---|
578 | case Variable: |
---|
579 | assert( false ); |
---|
580 | } |
---|
581 | |
---|
582 | return 0; |
---|
583 | } |
---|
584 | |
---|
585 | Type::Qualifiers |
---|
586 | TypeData::buildQualifiers() const |
---|
587 | { |
---|
588 | Type::Qualifiers q; |
---|
589 | for( std::list< DeclarationNode::Qualifier >::const_iterator i = qualifiers.begin(); i != qualifiers.end(); ++i ) { |
---|
590 | switch( *i ) { |
---|
591 | case DeclarationNode::Const: |
---|
592 | q.isConst = true; |
---|
593 | break; |
---|
594 | |
---|
595 | case DeclarationNode::Volatile: |
---|
596 | q.isVolatile = true; |
---|
597 | break; |
---|
598 | |
---|
599 | case DeclarationNode::Restrict: |
---|
600 | q.isRestrict = true; |
---|
601 | break; |
---|
602 | |
---|
603 | case DeclarationNode::Lvalue: |
---|
604 | q.isLvalue = true; |
---|
605 | break; |
---|
606 | } |
---|
607 | } |
---|
608 | return q; |
---|
609 | } |
---|
610 | |
---|
611 | Type* |
---|
612 | TypeData::buildBasicType() const |
---|
613 | { |
---|
614 | |
---|
615 | static const BasicType::Kind kindMap[] = { BasicType::Char, BasicType::SignedInt, BasicType::Float, BasicType::Double, |
---|
616 | BasicType::Char /* void */, BasicType::Bool, BasicType::DoubleComplex, |
---|
617 | BasicType::DoubleImaginary }; |
---|
618 | |
---|
619 | bool init = false; |
---|
620 | bool sawDouble = false; |
---|
621 | bool sawSigned = false; |
---|
622 | BasicType::Kind ret; |
---|
623 | |
---|
624 | for( std::list< DeclarationNode::BasicType >::const_iterator i = basic->typeSpec.begin(); i != basic->typeSpec.end(); ++i ) { |
---|
625 | if( !init ) { |
---|
626 | init = true; |
---|
627 | if( *i == DeclarationNode::Void ) { |
---|
628 | if( basic->typeSpec.size() != 1 || !basic->modifiers.empty() ) { |
---|
629 | throw SemanticError( "invalid type specifier \"void\" in type ", this ); |
---|
630 | } else { |
---|
631 | return new VoidType( buildQualifiers() ); |
---|
632 | } |
---|
633 | } else { |
---|
634 | ret = kindMap[ *i ]; |
---|
635 | } |
---|
636 | } else { |
---|
637 | switch( *i ) { |
---|
638 | case DeclarationNode::Float: |
---|
639 | if( sawDouble ) { |
---|
640 | throw SemanticError( "invalid type specifier \"float\" in type ", this ); |
---|
641 | } else { |
---|
642 | switch( ret ) { |
---|
643 | case BasicType::DoubleComplex: |
---|
644 | ret = BasicType::FloatComplex; |
---|
645 | break; |
---|
646 | |
---|
647 | case BasicType::DoubleImaginary: |
---|
648 | ret = BasicType::FloatImaginary; |
---|
649 | break; |
---|
650 | |
---|
651 | default: |
---|
652 | throw SemanticError( "invalid type specifier \"float\" in type ", this ); |
---|
653 | } |
---|
654 | } |
---|
655 | break; |
---|
656 | |
---|
657 | case DeclarationNode::Double: |
---|
658 | if( sawDouble ) { |
---|
659 | throw SemanticError( "duplicate type specifier \"double\" in type ", this ); |
---|
660 | } else { |
---|
661 | switch( ret ) { |
---|
662 | case BasicType::DoubleComplex: |
---|
663 | case BasicType::DoubleImaginary: |
---|
664 | break; |
---|
665 | |
---|
666 | default: |
---|
667 | throw SemanticError( "invalid type specifier \"double\" in type ", this ); |
---|
668 | } |
---|
669 | } |
---|
670 | break; |
---|
671 | |
---|
672 | case DeclarationNode::Complex: |
---|
673 | switch( ret ) { |
---|
674 | case BasicType::Float: |
---|
675 | ret = BasicType::FloatComplex; |
---|
676 | break; |
---|
677 | |
---|
678 | case BasicType::Double: |
---|
679 | ret = BasicType::DoubleComplex; |
---|
680 | break; |
---|
681 | |
---|
682 | default: |
---|
683 | throw SemanticError( "invalid type specifier \"complex\" in type ", this ); |
---|
684 | } |
---|
685 | break; |
---|
686 | |
---|
687 | case DeclarationNode::Imaginary: |
---|
688 | switch( ret ) { |
---|
689 | case BasicType::Float: |
---|
690 | ret = BasicType::FloatImaginary; |
---|
691 | break; |
---|
692 | |
---|
693 | case BasicType::Double: |
---|
694 | ret = BasicType::DoubleImaginary; |
---|
695 | break; |
---|
696 | |
---|
697 | default: |
---|
698 | throw SemanticError( "invalid type specifier \"imaginary\" in type ", this ); |
---|
699 | } |
---|
700 | break; |
---|
701 | |
---|
702 | default: |
---|
703 | throw SemanticError( std::string( "invalid type specifier \"" ) + DeclarationNode::basicTypeName[ *i ] + "\" in type ", this ); |
---|
704 | } |
---|
705 | } |
---|
706 | if( *i == DeclarationNode::Double ) { |
---|
707 | sawDouble = true; |
---|
708 | } |
---|
709 | } |
---|
710 | |
---|
711 | for( std::list< DeclarationNode::Modifier >::const_iterator i = basic->modifiers.begin(); i != basic->modifiers.end(); ++i ) { |
---|
712 | switch( *i ) { |
---|
713 | case DeclarationNode::Long: |
---|
714 | if( !init ) { |
---|
715 | init = true; |
---|
716 | ret = BasicType::LongSignedInt; |
---|
717 | } else { |
---|
718 | switch( ret ) { |
---|
719 | case BasicType::SignedInt: |
---|
720 | ret = BasicType::LongSignedInt; |
---|
721 | break; |
---|
722 | |
---|
723 | case BasicType::UnsignedInt: |
---|
724 | ret = BasicType::LongUnsignedInt; |
---|
725 | break; |
---|
726 | |
---|
727 | case BasicType::LongSignedInt: |
---|
728 | ret = BasicType::LongLongSignedInt; |
---|
729 | break; |
---|
730 | |
---|
731 | case BasicType::LongUnsignedInt: |
---|
732 | ret = BasicType::LongLongUnsignedInt; |
---|
733 | break; |
---|
734 | |
---|
735 | case BasicType::Double: |
---|
736 | ret = BasicType::LongDouble; |
---|
737 | break; |
---|
738 | |
---|
739 | case BasicType::DoubleComplex: |
---|
740 | ret = BasicType::LongDoubleComplex; |
---|
741 | break; |
---|
742 | |
---|
743 | case BasicType::DoubleImaginary: |
---|
744 | ret = BasicType::LongDoubleImaginary; |
---|
745 | break; |
---|
746 | |
---|
747 | default: |
---|
748 | throw SemanticError( "invalid type modifier \"long\" in type ", this ); |
---|
749 | } |
---|
750 | } |
---|
751 | break; |
---|
752 | |
---|
753 | case DeclarationNode::Short: |
---|
754 | if( !init ) { |
---|
755 | init = true; |
---|
756 | ret = BasicType::ShortSignedInt; |
---|
757 | } else { |
---|
758 | switch( ret ) { |
---|
759 | case BasicType::SignedInt: |
---|
760 | ret = BasicType::ShortSignedInt; |
---|
761 | break; |
---|
762 | |
---|
763 | case BasicType::UnsignedInt: |
---|
764 | ret = BasicType::ShortUnsignedInt; |
---|
765 | break; |
---|
766 | |
---|
767 | default: |
---|
768 | throw SemanticError( "invalid type modifier \"short\" in type ", this ); |
---|
769 | } |
---|
770 | } |
---|
771 | break; |
---|
772 | |
---|
773 | case DeclarationNode::Signed: |
---|
774 | if( !init ) { |
---|
775 | init = true; |
---|
776 | ret = BasicType::SignedInt; |
---|
777 | } else if( sawSigned ) { |
---|
778 | throw SemanticError( "duplicate type modifer \"signed\" in type ", this ); |
---|
779 | } else { |
---|
780 | switch( ret ) { |
---|
781 | case BasicType::SignedInt: |
---|
782 | case BasicType::ShortSignedInt: |
---|
783 | break; |
---|
784 | |
---|
785 | case BasicType::Char: |
---|
786 | ret = BasicType::SignedChar; |
---|
787 | break; |
---|
788 | |
---|
789 | default: |
---|
790 | throw SemanticError( "invalid type modifer \"signed\" in type ", this ); |
---|
791 | } |
---|
792 | } |
---|
793 | break; |
---|
794 | |
---|
795 | case DeclarationNode::Unsigned: |
---|
796 | if( !init ) { |
---|
797 | init = true; |
---|
798 | ret = BasicType::UnsignedInt; |
---|
799 | } else if( sawSigned ) { |
---|
800 | throw SemanticError( "invalid type modifer \"unsigned\" in type ", this ); |
---|
801 | } else { |
---|
802 | switch( ret ) { |
---|
803 | case BasicType::LongSignedInt: |
---|
804 | ret = BasicType::LongUnsignedInt; |
---|
805 | break; |
---|
806 | |
---|
807 | case BasicType::SignedInt: |
---|
808 | ret = BasicType::UnsignedInt; |
---|
809 | break; |
---|
810 | |
---|
811 | case BasicType::ShortSignedInt: |
---|
812 | ret = BasicType::ShortUnsignedInt; |
---|
813 | break; |
---|
814 | |
---|
815 | case BasicType::Char: |
---|
816 | ret = BasicType::UnsignedChar; |
---|
817 | break; |
---|
818 | |
---|
819 | default: |
---|
820 | throw SemanticError( "invalid type modifer \"unsigned\" in type ", this ); |
---|
821 | } |
---|
822 | } |
---|
823 | break; |
---|
824 | } |
---|
825 | |
---|
826 | if( *i == DeclarationNode::Signed ) { |
---|
827 | sawSigned = true; |
---|
828 | } |
---|
829 | } |
---|
830 | |
---|
831 | BasicType *bt; |
---|
832 | if( !init ) { |
---|
833 | bt = new BasicType( buildQualifiers(), BasicType::SignedInt ); |
---|
834 | } else { |
---|
835 | bt = new BasicType( buildQualifiers(), ret ); |
---|
836 | } |
---|
837 | buildForall( forall, bt->get_forall() ); |
---|
838 | return bt; |
---|
839 | } |
---|
840 | |
---|
841 | |
---|
842 | PointerType * |
---|
843 | TypeData::buildPointer() const |
---|
844 | { |
---|
845 | |
---|
846 | PointerType *pt; |
---|
847 | if( base ) { |
---|
848 | pt = new PointerType( buildQualifiers(), base->build() ); |
---|
849 | } else { |
---|
850 | pt = new PointerType( buildQualifiers(), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
851 | } |
---|
852 | buildForall( forall, pt->get_forall() ); |
---|
853 | return pt; |
---|
854 | } |
---|
855 | |
---|
856 | ArrayType * |
---|
857 | TypeData::buildArray() const |
---|
858 | { |
---|
859 | |
---|
860 | ArrayType *at; |
---|
861 | if( base ) { |
---|
862 | at = new ArrayType( buildQualifiers(), base->build(), maybeBuild< Expression >( array->dimension ), |
---|
863 | array->isVarLen, array->isStatic ); |
---|
864 | } else { |
---|
865 | at = new ArrayType( buildQualifiers(), new BasicType( Type::Qualifiers(), BasicType::SignedInt ), |
---|
866 | maybeBuild< Expression >( array->dimension ), array->isVarLen, array->isStatic ); |
---|
867 | } |
---|
868 | buildForall( forall, at->get_forall() ); |
---|
869 | return at; |
---|
870 | } |
---|
871 | |
---|
872 | FunctionType * |
---|
873 | TypeData::buildFunction() const |
---|
874 | { |
---|
875 | assert( kind == Function ); |
---|
876 | |
---|
877 | |
---|
878 | bool hasEllipsis = function->params ? function->params->get_hasEllipsis() : true; |
---|
879 | if( !function->params ) hasEllipsis = !function->newStyle; |
---|
880 | FunctionType *ft = new FunctionType( buildQualifiers(), hasEllipsis ); |
---|
881 | buildList( function->params, ft->get_parameters() ); |
---|
882 | buildForall( forall, ft->get_forall() ); |
---|
883 | if( base ) { |
---|
884 | switch( base->kind ) { |
---|
885 | case Tuple: |
---|
886 | buildList( base->tuple->members, ft->get_returnVals() ); |
---|
887 | break; |
---|
888 | |
---|
889 | default: |
---|
890 | ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType* >( base->buildDecl( "", Declaration::NoStorageClass, 0, false, LinkageSpec::Cforall ) ) ); |
---|
891 | } |
---|
892 | } else { |
---|
893 | ft->get_returnVals().push_back( new ObjectDecl( "", Declaration::NoStorageClass, LinkageSpec::Cforall, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 0 ) ); |
---|
894 | } |
---|
895 | return ft; |
---|
896 | } |
---|
897 | |
---|
898 | AggregateDecl * |
---|
899 | TypeData::buildAggregate() const |
---|
900 | { |
---|
901 | assert( kind == Aggregate ); |
---|
902 | |
---|
903 | |
---|
904 | AggregateDecl *at; |
---|
905 | switch( aggregate->kind ) { |
---|
906 | case DeclarationNode::Struct: |
---|
907 | at = new StructDecl( aggregate->name ); |
---|
908 | break; |
---|
909 | |
---|
910 | case DeclarationNode::Union: |
---|
911 | at = new UnionDecl( aggregate->name ); |
---|
912 | break; |
---|
913 | |
---|
914 | case DeclarationNode::Context: |
---|
915 | at = new ContextDecl( aggregate->name ); |
---|
916 | break; |
---|
917 | |
---|
918 | default: |
---|
919 | assert( false ); |
---|
920 | } |
---|
921 | |
---|
922 | buildList( aggregate->params, at->get_parameters() ); |
---|
923 | buildList( aggregate->members, at->get_members() ); |
---|
924 | |
---|
925 | return at; |
---|
926 | } |
---|
927 | |
---|
928 | /// namespace { |
---|
929 | /// Type* |
---|
930 | /// makeType( Declaration* decl ) |
---|
931 | /// { |
---|
932 | /// if( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType* >( decl ) ) { |
---|
933 | /// return dwt->get_type()->clone(); |
---|
934 | /// } else { |
---|
935 | /// return 0; |
---|
936 | /// } |
---|
937 | /// } |
---|
938 | /// } |
---|
939 | |
---|
940 | ReferenceToType * |
---|
941 | TypeData::buildAggInst() const |
---|
942 | { |
---|
943 | assert( kind == AggregateInst ); |
---|
944 | |
---|
945 | |
---|
946 | std::string name; |
---|
947 | |
---|
948 | ReferenceToType *ret; |
---|
949 | if( aggInst->aggregate->kind == Enum ) { |
---|
950 | ret = new EnumInstType( buildQualifiers(), aggInst->aggregate->enumeration->name ); |
---|
951 | } else { |
---|
952 | assert( aggInst->aggregate->kind == Aggregate ); |
---|
953 | switch( aggInst->aggregate->aggregate->kind ) { |
---|
954 | case DeclarationNode::Struct: |
---|
955 | ret = new StructInstType( buildQualifiers(), aggInst->aggregate->aggregate->name ); |
---|
956 | break; |
---|
957 | |
---|
958 | case DeclarationNode::Union: |
---|
959 | ret = new UnionInstType( buildQualifiers(), aggInst->aggregate->aggregate->name ); |
---|
960 | break; |
---|
961 | |
---|
962 | case DeclarationNode::Context: |
---|
963 | ret = new ContextInstType( buildQualifiers(), aggInst->aggregate->aggregate->name ); |
---|
964 | break; |
---|
965 | |
---|
966 | default: |
---|
967 | assert( false ); |
---|
968 | } |
---|
969 | } |
---|
970 | buildList( aggInst->params, ret->get_parameters() ); |
---|
971 | buildForall( forall, ret->get_forall() ); |
---|
972 | return ret; |
---|
973 | } |
---|
974 | |
---|
975 | NamedTypeDecl* |
---|
976 | TypeData::buildSymbolic( const std::string &name, Declaration::StorageClass sc ) const |
---|
977 | { |
---|
978 | assert( kind == Symbolic ); |
---|
979 | |
---|
980 | |
---|
981 | NamedTypeDecl *ret; |
---|
982 | if( symbolic->isTypedef ) { |
---|
983 | ret = new TypedefDecl( name, sc, maybeBuild< Type >( base ) ); |
---|
984 | } else { |
---|
985 | ret = new TypeDecl( name, sc, maybeBuild< Type >( base ), TypeDecl::Any ); |
---|
986 | } |
---|
987 | buildList( symbolic->params, ret->get_parameters() ); |
---|
988 | buildList( symbolic->assertions, ret->get_assertions() ); |
---|
989 | return ret; |
---|
990 | } |
---|
991 | |
---|
992 | TypeDecl* |
---|
993 | TypeData::buildVariable() const |
---|
994 | { |
---|
995 | assert( kind == Variable ); |
---|
996 | |
---|
997 | |
---|
998 | static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype }; |
---|
999 | |
---|
1000 | TypeDecl *ret = new TypeDecl( variable->name, Declaration::NoStorageClass, 0, kindMap[ variable->tyClass ] ); |
---|
1001 | buildList( variable->assertions, ret->get_assertions() ); |
---|
1002 | |
---|
1003 | return ret; |
---|
1004 | } |
---|
1005 | |
---|
1006 | EnumDecl* |
---|
1007 | TypeData::buildEnum() const |
---|
1008 | { |
---|
1009 | assert( kind == Enum ); |
---|
1010 | |
---|
1011 | |
---|
1012 | EnumDecl *ret = new EnumDecl( enumeration->name ); |
---|
1013 | buildList( enumeration->constants, ret->get_members() ); |
---|
1014 | |
---|
1015 | return ret; |
---|
1016 | } |
---|
1017 | |
---|
1018 | TypeInstType * |
---|
1019 | TypeData::buildSymbolicInst() const |
---|
1020 | { |
---|
1021 | assert( kind == SymbolicInst ); |
---|
1022 | |
---|
1023 | |
---|
1024 | TypeInstType *ret = new TypeInstType( buildQualifiers(), symbolic->name, false ); |
---|
1025 | buildList( symbolic->actuals, ret->get_parameters() ); |
---|
1026 | buildForall( forall, ret->get_forall() ); |
---|
1027 | |
---|
1028 | return ret; |
---|
1029 | } |
---|
1030 | |
---|
1031 | TupleType * |
---|
1032 | TypeData::buildTuple() const |
---|
1033 | { |
---|
1034 | assert( kind == Tuple ); |
---|
1035 | |
---|
1036 | |
---|
1037 | TupleType *ret = new TupleType( buildQualifiers() ); |
---|
1038 | buildTypeList( tuple->members, ret->get_types() ); |
---|
1039 | buildForall( forall, ret->get_forall() ); |
---|
1040 | |
---|
1041 | return ret; |
---|
1042 | } |
---|
1043 | |
---|
1044 | TypeofType * |
---|
1045 | TypeData::buildTypeof() const |
---|
1046 | { |
---|
1047 | assert( kind == Typeof ); |
---|
1048 | |
---|
1049 | |
---|
1050 | assert( typeexpr ); |
---|
1051 | assert( typeexpr->expr ); |
---|
1052 | TypeofType *ret = new TypeofType( buildQualifiers(), typeexpr->expr->build() ); |
---|
1053 | |
---|
1054 | return ret; |
---|
1055 | } |
---|
1056 | |
---|
1057 | AttrType * |
---|
1058 | TypeData::buildAttr() const |
---|
1059 | { |
---|
1060 | assert( kind == Attr ); |
---|
1061 | |
---|
1062 | |
---|
1063 | assert( attr ); |
---|
1064 | AttrType *ret; |
---|
1065 | if( attr->expr ) { |
---|
1066 | ret = new AttrType( buildQualifiers(), attr->name, attr->expr->build() ); |
---|
1067 | } else { |
---|
1068 | assert( attr->type ); |
---|
1069 | ret = new AttrType( buildQualifiers(), attr->name, attr->type->buildType() ); |
---|
1070 | } |
---|
1071 | |
---|
1072 | return ret; |
---|
1073 | } |
---|
1074 | |
---|