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