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