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