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