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