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 : Sat Sep 24 11:14:26 2016 |
---|
13 | // Update Count : 415 |
---|
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 | using namespace std; |
---|
27 | |
---|
28 | TypeData::TypeData( Kind k ) : kind( k ), base( nullptr ), forall( nullptr ) { |
---|
29 | switch ( kind ) { |
---|
30 | case Unknown: |
---|
31 | case Pointer: |
---|
32 | case EnumConstant: |
---|
33 | // nothing else to initialize |
---|
34 | break; |
---|
35 | case Basic: |
---|
36 | // basic = new Basic_t; |
---|
37 | break; |
---|
38 | case Array: |
---|
39 | // array = new Array_t; |
---|
40 | array.dimension = nullptr; |
---|
41 | array.isVarLen = false; |
---|
42 | array.isStatic = false; |
---|
43 | break; |
---|
44 | case Function: |
---|
45 | // function = new Function_t; |
---|
46 | function.params = nullptr; |
---|
47 | function.idList = nullptr; |
---|
48 | function.oldDeclList = nullptr; |
---|
49 | function.body = nullptr; |
---|
50 | function.hasBody = false; |
---|
51 | function.newStyle = false; |
---|
52 | break; |
---|
53 | case Aggregate: |
---|
54 | // aggregate = new Aggregate_t; |
---|
55 | aggregate.name = nullptr; |
---|
56 | aggregate.params = nullptr; |
---|
57 | aggregate.actuals = nullptr; |
---|
58 | aggregate.fields = nullptr; |
---|
59 | break; |
---|
60 | case AggregateInst: |
---|
61 | // aggInst = new AggInst_t; |
---|
62 | aggInst.aggregate = nullptr; |
---|
63 | aggInst.params = nullptr; |
---|
64 | break; |
---|
65 | case Enum: |
---|
66 | // enumeration = new Enumeration_t; |
---|
67 | enumeration.name = nullptr; |
---|
68 | enumeration.constants = nullptr; |
---|
69 | break; |
---|
70 | case Symbolic: |
---|
71 | case SymbolicInst: |
---|
72 | // symbolic = new Symbolic_t; |
---|
73 | symbolic.name = nullptr; |
---|
74 | symbolic.params = nullptr; |
---|
75 | symbolic.actuals = nullptr; |
---|
76 | symbolic.assertions = nullptr; |
---|
77 | break; |
---|
78 | case Tuple: |
---|
79 | // tuple = new Tuple_t; |
---|
80 | tuple = nullptr; |
---|
81 | break; |
---|
82 | case Typeof: |
---|
83 | // typeexpr = new Typeof_t; |
---|
84 | typeexpr = nullptr; |
---|
85 | break; |
---|
86 | case Builtin: |
---|
87 | // builtin = new Builtin_t; |
---|
88 | break; |
---|
89 | } // switch |
---|
90 | } // TypeData::TypeData |
---|
91 | |
---|
92 | TypeData::~TypeData() { |
---|
93 | delete base; |
---|
94 | delete forall; |
---|
95 | |
---|
96 | switch ( kind ) { |
---|
97 | case Unknown: |
---|
98 | case Pointer: |
---|
99 | case EnumConstant: |
---|
100 | // nothing to destroy |
---|
101 | break; |
---|
102 | case Basic: |
---|
103 | // delete basic; |
---|
104 | break; |
---|
105 | case Array: |
---|
106 | delete array.dimension; |
---|
107 | // delete array; |
---|
108 | break; |
---|
109 | case Function: |
---|
110 | delete function.params; |
---|
111 | delete function.idList; |
---|
112 | delete function.oldDeclList; |
---|
113 | delete function.body; |
---|
114 | // delete function; |
---|
115 | break; |
---|
116 | case Aggregate: |
---|
117 | delete aggregate.name; |
---|
118 | delete aggregate.params; |
---|
119 | delete aggregate.actuals; |
---|
120 | delete aggregate.fields; |
---|
121 | // delete aggregate; |
---|
122 | break; |
---|
123 | case AggregateInst: |
---|
124 | delete aggInst.aggregate; |
---|
125 | delete aggInst.params; |
---|
126 | // delete aggInst; |
---|
127 | break; |
---|
128 | case Enum: |
---|
129 | delete enumeration.name; |
---|
130 | delete enumeration.constants; |
---|
131 | // delete enumeration; |
---|
132 | break; |
---|
133 | case Symbolic: |
---|
134 | case SymbolicInst: |
---|
135 | delete symbolic.name; |
---|
136 | delete symbolic.params; |
---|
137 | delete symbolic.actuals; |
---|
138 | delete symbolic.assertions; |
---|
139 | // delete symbolic; |
---|
140 | break; |
---|
141 | case Tuple: |
---|
142 | // delete tuple->members; |
---|
143 | delete tuple; |
---|
144 | break; |
---|
145 | case Typeof: |
---|
146 | // delete typeexpr->expr; |
---|
147 | delete typeexpr; |
---|
148 | break; |
---|
149 | case Builtin: |
---|
150 | // delete builtin; |
---|
151 | break; |
---|
152 | } // switch |
---|
153 | } // TypeData::~TypeData |
---|
154 | |
---|
155 | TypeData * TypeData::clone() const { |
---|
156 | TypeData * newtype = new TypeData( kind ); |
---|
157 | newtype->qualifiers = qualifiers; |
---|
158 | newtype->base = maybeClone( base ); |
---|
159 | newtype->forall = maybeClone( forall ); |
---|
160 | |
---|
161 | switch ( kind ) { |
---|
162 | case Unknown: |
---|
163 | case EnumConstant: |
---|
164 | case Pointer: |
---|
165 | // nothing else to copy |
---|
166 | break; |
---|
167 | case Basic: |
---|
168 | newtype->basictype = basictype; |
---|
169 | newtype->complextype = complextype; |
---|
170 | newtype->signedness = signedness; |
---|
171 | newtype->length = length; |
---|
172 | break; |
---|
173 | case Array: |
---|
174 | newtype->array.dimension = maybeClone( array.dimension ); |
---|
175 | newtype->array.isVarLen = array.isVarLen; |
---|
176 | newtype->array.isStatic = array.isStatic; |
---|
177 | break; |
---|
178 | case Function: |
---|
179 | newtype->function.params = maybeClone( function.params ); |
---|
180 | newtype->function.idList = maybeClone( function.idList ); |
---|
181 | newtype->function.oldDeclList = maybeClone( function.oldDeclList ); |
---|
182 | newtype->function.body = maybeClone( function.body ); |
---|
183 | newtype->function.hasBody = function.hasBody; |
---|
184 | newtype->function.newStyle = function.newStyle; |
---|
185 | break; |
---|
186 | case Aggregate: |
---|
187 | newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr; |
---|
188 | newtype->aggregate.params = maybeClone( aggregate.params ); |
---|
189 | newtype->aggregate.actuals = maybeClone( aggregate.actuals ); |
---|
190 | newtype->aggregate.fields = maybeClone( aggregate.fields ); |
---|
191 | newtype->aggregate.kind = aggregate.kind; |
---|
192 | newtype->aggregate.body = aggregate.body; |
---|
193 | break; |
---|
194 | case AggregateInst: |
---|
195 | newtype->aggInst.aggregate = maybeClone( aggInst.aggregate ); |
---|
196 | newtype->aggInst.params = maybeClone( aggInst.params ); |
---|
197 | break; |
---|
198 | case Enum: |
---|
199 | newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr; |
---|
200 | newtype->enumeration.constants = maybeClone( enumeration.constants ); |
---|
201 | break; |
---|
202 | case Symbolic: |
---|
203 | case SymbolicInst: |
---|
204 | newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr; |
---|
205 | newtype->symbolic.params = maybeClone( symbolic.params ); |
---|
206 | newtype->symbolic.actuals = maybeClone( symbolic.actuals ); |
---|
207 | newtype->symbolic.assertions = maybeClone( symbolic.assertions ); |
---|
208 | newtype->symbolic.isTypedef = symbolic.isTypedef; |
---|
209 | break; |
---|
210 | case Tuple: |
---|
211 | newtype->tuple = maybeClone( tuple ); |
---|
212 | break; |
---|
213 | case Typeof: |
---|
214 | newtype->typeexpr = maybeClone( typeexpr ); |
---|
215 | break; |
---|
216 | case Builtin: |
---|
217 | assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One ); |
---|
218 | newtype->builtintype = builtintype; |
---|
219 | break; |
---|
220 | } // switch |
---|
221 | return newtype; |
---|
222 | } // TypeData::clone |
---|
223 | |
---|
224 | void TypeData::print( ostream &os, int indent ) const { |
---|
225 | for ( int i = 0; i < DeclarationNode::NoQualifier; i += 1 ) { |
---|
226 | if ( qualifiers[i] ) os << DeclarationNode::qualifierName[ i ] << ' '; |
---|
227 | } // for |
---|
228 | |
---|
229 | if ( forall ) { |
---|
230 | os << "forall " << endl; |
---|
231 | forall->printList( os, indent + 4 ); |
---|
232 | } // if |
---|
233 | |
---|
234 | switch ( kind ) { |
---|
235 | case Unknown: |
---|
236 | os << "entity of unknown type "; |
---|
237 | break; |
---|
238 | case Pointer: |
---|
239 | os << "pointer "; |
---|
240 | if ( base ) { |
---|
241 | os << "to "; |
---|
242 | base->print( os, indent ); |
---|
243 | } // if |
---|
244 | break; |
---|
245 | case EnumConstant: |
---|
246 | os << "enumeration constant "; |
---|
247 | break; |
---|
248 | case Basic: |
---|
249 | if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessName[ signedness ] << " "; |
---|
250 | if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthName[ length ] << " "; |
---|
251 | assert( basictype != DeclarationNode::NoBasicType ); |
---|
252 | os << DeclarationNode::basicTypeName[ basictype ] << " "; |
---|
253 | if ( complextype != DeclarationNode::NoComplexType ) os << DeclarationNode::complexTypeName[ complextype ] << " "; |
---|
254 | break; |
---|
255 | case Array: |
---|
256 | if ( array.isStatic ) { |
---|
257 | os << "static "; |
---|
258 | } // if |
---|
259 | if ( array.dimension ) { |
---|
260 | os << "array of "; |
---|
261 | array.dimension->printOneLine( os, indent ); |
---|
262 | } else if ( array.isVarLen ) { |
---|
263 | os << "variable-length array of "; |
---|
264 | } else { |
---|
265 | os << "open array of "; |
---|
266 | } // if |
---|
267 | if ( base ) { |
---|
268 | base->print( os, indent ); |
---|
269 | } // if |
---|
270 | break; |
---|
271 | case Function: |
---|
272 | os << "function" << endl; |
---|
273 | if ( function.params ) { |
---|
274 | os << string( indent + 2, ' ' ) << "with parameters " << endl; |
---|
275 | function.params->printList( os, indent + 4 ); |
---|
276 | } else { |
---|
277 | os << string( indent + 2, ' ' ) << "with no parameters " << endl; |
---|
278 | } // if |
---|
279 | if ( function.idList ) { |
---|
280 | os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl; |
---|
281 | function.idList->printList( os, indent + 4 ); |
---|
282 | } // if |
---|
283 | if ( function.oldDeclList ) { |
---|
284 | os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl; |
---|
285 | function.oldDeclList->printList( os, indent + 4 ); |
---|
286 | } // if |
---|
287 | os << string( indent + 2, ' ' ) << "returning "; |
---|
288 | if ( base ) { |
---|
289 | base->print( os, indent + 4 ); |
---|
290 | } else { |
---|
291 | os << "nothing "; |
---|
292 | } // if |
---|
293 | os << endl; |
---|
294 | if ( function.hasBody ) { |
---|
295 | os << string( indent + 2, ' ' ) << "with body " << endl; |
---|
296 | } // if |
---|
297 | if ( function.body ) { |
---|
298 | function.body->printList( os, indent + 2 ); |
---|
299 | } // if |
---|
300 | break; |
---|
301 | case Aggregate: |
---|
302 | os << DeclarationNode::aggregateName[ aggregate.kind ] << ' ' << *aggregate.name << endl; |
---|
303 | if ( aggregate.params ) { |
---|
304 | os << string( indent + 2, ' ' ) << "with type parameters " << endl; |
---|
305 | aggregate.params->printList( os, indent + 4 ); |
---|
306 | } // if |
---|
307 | if ( aggregate.actuals ) { |
---|
308 | os << string( indent + 2, ' ' ) << "instantiated with actual parameters " << endl; |
---|
309 | aggregate.actuals->printList( os, indent + 4 ); |
---|
310 | } // if |
---|
311 | if ( aggregate.fields ) { |
---|
312 | os << string( indent + 2, ' ' ) << "with members " << endl; |
---|
313 | aggregate.fields->printList( os, indent + 4 ); |
---|
314 | } // if |
---|
315 | if ( aggregate.body ) { |
---|
316 | os << string( indent + 2, ' ' ) << " with body " << endl; |
---|
317 | } // if |
---|
318 | break; |
---|
319 | case AggregateInst: |
---|
320 | if ( aggInst.aggregate ) { |
---|
321 | os << "instance of " ; |
---|
322 | aggInst.aggregate->print( os, indent ); |
---|
323 | } else { |
---|
324 | os << "instance of an unspecified aggregate "; |
---|
325 | } // if |
---|
326 | if ( aggInst.params ) { |
---|
327 | os << string( indent + 2, ' ' ) << "with parameters " << endl; |
---|
328 | aggInst.params->printList( os, indent + 2 ); |
---|
329 | } // if |
---|
330 | break; |
---|
331 | case Enum: |
---|
332 | os << "enumeration "; |
---|
333 | if ( enumeration.constants ) { |
---|
334 | os << "with constants" << endl; |
---|
335 | enumeration.constants->printList( os, indent + 2 ); |
---|
336 | } // if |
---|
337 | break; |
---|
338 | case SymbolicInst: |
---|
339 | os << "instance of type " << *symbolic.name; |
---|
340 | if ( symbolic.actuals ) { |
---|
341 | os << " with parameters" << endl; |
---|
342 | symbolic.actuals->printList( os, indent + 2 ); |
---|
343 | } // if |
---|
344 | break; |
---|
345 | case Symbolic: |
---|
346 | if ( symbolic.isTypedef ) { |
---|
347 | os << "typedef definition "; |
---|
348 | } else { |
---|
349 | os << "type definition "; |
---|
350 | } // if |
---|
351 | if ( symbolic.params ) { |
---|
352 | os << endl << string( indent + 2, ' ' ) << "with parameters" << endl; |
---|
353 | symbolic.params->printList( os, indent + 2 ); |
---|
354 | } // if |
---|
355 | if ( symbolic.assertions ) { |
---|
356 | os << endl << string( indent + 2, ' ' ) << "with assertions" << endl; |
---|
357 | symbolic.assertions->printList( os, indent + 4 ); |
---|
358 | os << string( indent + 2, ' ' ); |
---|
359 | } // if |
---|
360 | if ( base ) { |
---|
361 | os << "for "; |
---|
362 | base->print( os, indent + 2 ); |
---|
363 | } // if |
---|
364 | break; |
---|
365 | case Tuple: |
---|
366 | os << "tuple "; |
---|
367 | if ( tuple ) { |
---|
368 | os << "with members " << endl; |
---|
369 | tuple->printList( os, indent + 2 ); |
---|
370 | } // if |
---|
371 | break; |
---|
372 | case Typeof: |
---|
373 | os << "type-of expression "; |
---|
374 | if ( typeexpr ) { |
---|
375 | typeexpr->print( os, indent + 2 ); |
---|
376 | } // if |
---|
377 | break; |
---|
378 | case Builtin: |
---|
379 | os << "gcc builtin type"; |
---|
380 | break; |
---|
381 | default: |
---|
382 | os << "internal error: TypeData::print " << kind << endl; |
---|
383 | assert( false ); |
---|
384 | } // switch |
---|
385 | } // TypeData::print |
---|
386 | |
---|
387 | template< typename ForallList > |
---|
388 | void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) { |
---|
389 | buildList( firstNode, outputList ); |
---|
390 | for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i ) { |
---|
391 | TypeDecl * td = static_cast<TypeDecl*>(*i); |
---|
392 | if ( td->get_kind() == TypeDecl::Any ) { |
---|
393 | // add assertion parameters to `type' tyvars in reverse order |
---|
394 | // add dtor: void ^?{}(T *) |
---|
395 | FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false ); |
---|
396 | dtorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) ); |
---|
397 | td->get_assertions().push_front( new FunctionDecl( "^?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, dtorType, nullptr, false, false ) ); |
---|
398 | |
---|
399 | // add copy ctor: void ?{}(T *, T) |
---|
400 | FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false ); |
---|
401 | copyCtorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) ); |
---|
402 | copyCtorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) ); |
---|
403 | td->get_assertions().push_front( new FunctionDecl( "?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, copyCtorType, nullptr, false, false ) ); |
---|
404 | |
---|
405 | // add default ctor: void ?{}(T *) |
---|
406 | FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false ); |
---|
407 | ctorType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) ); |
---|
408 | td->get_assertions().push_front( new FunctionDecl( "?{}", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, ctorType, nullptr, false, false ) ); |
---|
409 | |
---|
410 | // add assignment operator: T * ?=?(T *, T) |
---|
411 | FunctionType * assignType = new FunctionType( Type::Qualifiers(), false ); |
---|
412 | assignType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) ); |
---|
413 | assignType->get_parameters().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) ); |
---|
414 | assignType->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) ); |
---|
415 | td->get_assertions().push_front( new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, assignType, nullptr, false, false ) ); |
---|
416 | } // if |
---|
417 | } // for |
---|
418 | } |
---|
419 | |
---|
420 | Type * typebuild( const TypeData * td ) { |
---|
421 | assert( td ); |
---|
422 | switch ( td->kind ) { |
---|
423 | case TypeData::Unknown: |
---|
424 | // fill in implicit int |
---|
425 | return new BasicType( buildQualifiers( td ), BasicType::SignedInt ); |
---|
426 | case TypeData::Basic: |
---|
427 | return buildBasicType( td ); |
---|
428 | case TypeData::Pointer: |
---|
429 | return buildPointer( td ); |
---|
430 | case TypeData::Array: |
---|
431 | return buildArray( td ); |
---|
432 | case TypeData::Function: |
---|
433 | return buildFunction( td ); |
---|
434 | case TypeData::AggregateInst: |
---|
435 | return buildAggInst( td ); |
---|
436 | case TypeData::EnumConstant: |
---|
437 | // the name gets filled in later -- by SymTab::Validate |
---|
438 | return new EnumInstType( buildQualifiers( td ), "" ); |
---|
439 | case TypeData::SymbolicInst: |
---|
440 | return buildSymbolicInst( td );; |
---|
441 | case TypeData::Tuple: |
---|
442 | return buildTuple( td ); |
---|
443 | case TypeData::Typeof: |
---|
444 | return buildTypeof( td ); |
---|
445 | case TypeData::Builtin: |
---|
446 | if(td->builtintype == DeclarationNode::Zero) { |
---|
447 | return new ZeroType( emptyQualifiers ); |
---|
448 | } |
---|
449 | else if(td->builtintype == DeclarationNode::One) { |
---|
450 | return new OneType( emptyQualifiers ); |
---|
451 | } |
---|
452 | else { |
---|
453 | return new VarArgsType( buildQualifiers( td ) ); |
---|
454 | } |
---|
455 | case TypeData::Symbolic: |
---|
456 | case TypeData::Enum: |
---|
457 | case TypeData::Aggregate: |
---|
458 | assert( false ); |
---|
459 | } // switch |
---|
460 | return nullptr; |
---|
461 | } // typebuild |
---|
462 | |
---|
463 | TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) { |
---|
464 | TypeData * ret = nullptr; |
---|
465 | |
---|
466 | switch ( td->kind ) { |
---|
467 | case TypeData::Aggregate: |
---|
468 | if ( ! toplevel && td->aggregate.fields ) { |
---|
469 | ret = td->clone(); |
---|
470 | } // if |
---|
471 | break; |
---|
472 | case TypeData::Enum: |
---|
473 | if ( ! toplevel && td->enumeration.constants ) { |
---|
474 | ret = td->clone(); |
---|
475 | } // if |
---|
476 | break; |
---|
477 | case TypeData::AggregateInst: |
---|
478 | if ( td->aggInst.aggregate ) { |
---|
479 | ret = typeextractAggregate( td->aggInst.aggregate, false ); |
---|
480 | } // if |
---|
481 | break; |
---|
482 | default: |
---|
483 | if ( td->base ) { |
---|
484 | ret = typeextractAggregate( td->base, false ); |
---|
485 | } // if |
---|
486 | } // switch |
---|
487 | return ret; |
---|
488 | } // typeextractAggregate |
---|
489 | |
---|
490 | Type::Qualifiers buildQualifiers( const TypeData * td ) { |
---|
491 | Type::Qualifiers q; |
---|
492 | q.isConst = td->qualifiers[ DeclarationNode::Const ]; |
---|
493 | q.isVolatile = td->qualifiers[ DeclarationNode::Volatile ]; |
---|
494 | q.isRestrict = td->qualifiers[ DeclarationNode::Restrict ]; |
---|
495 | q.isLvalue = td->qualifiers[ DeclarationNode::Lvalue ]; |
---|
496 | q.isAtomic = td->qualifiers[ DeclarationNode::Atomic ];; |
---|
497 | return q; |
---|
498 | } // buildQualifiers |
---|
499 | |
---|
500 | Type * buildBasicType( const TypeData * td ) { |
---|
501 | BasicType::Kind ret; |
---|
502 | |
---|
503 | switch ( td->basictype ) { |
---|
504 | case DeclarationNode::Void: |
---|
505 | if ( td->signedness != DeclarationNode::NoSignedness && td->length != DeclarationNode::NoLength ) { |
---|
506 | throw SemanticError( "invalid type specifier \"void\" in type: ", td ); |
---|
507 | } // if |
---|
508 | |
---|
509 | return new VoidType( buildQualifiers( td ) ); |
---|
510 | break; |
---|
511 | |
---|
512 | case DeclarationNode::Bool: |
---|
513 | if ( td->signedness != DeclarationNode::NoSignedness ) { |
---|
514 | throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::signednessName[ td->signedness ] + " in type: ", td ); |
---|
515 | } // if |
---|
516 | if ( td->length != DeclarationNode::NoLength ) { |
---|
517 | throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthName[ td->length ] + " in type: ", td ); |
---|
518 | } // if |
---|
519 | |
---|
520 | ret = BasicType::Bool; |
---|
521 | break; |
---|
522 | |
---|
523 | case DeclarationNode::Char: |
---|
524 | // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the |
---|
525 | // character types. The implementation shall define char to have the same range, representation, and behavior as |
---|
526 | // either signed char or unsigned char. |
---|
527 | static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char }; |
---|
528 | |
---|
529 | if ( td->length != DeclarationNode::NoLength ) { |
---|
530 | throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthName[ td->length ] + " in type: ", td ); |
---|
531 | } // if |
---|
532 | |
---|
533 | ret = chartype[ td->signedness ]; |
---|
534 | break; |
---|
535 | |
---|
536 | case DeclarationNode::Int: |
---|
537 | static BasicType::Kind inttype[2][4] = { |
---|
538 | { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt }, |
---|
539 | { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt }, |
---|
540 | }; |
---|
541 | |
---|
542 | Integral: ; |
---|
543 | if ( td->signedness == DeclarationNode::NoSignedness ) { |
---|
544 | const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed; |
---|
545 | } // if |
---|
546 | ret = inttype[ td->signedness ][ td->length ]; |
---|
547 | break; |
---|
548 | |
---|
549 | case DeclarationNode::Float: |
---|
550 | case DeclarationNode::Double: |
---|
551 | case DeclarationNode::LongDouble: // not set until below |
---|
552 | static BasicType::Kind floattype[3][3] = { |
---|
553 | { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
554 | { BasicType::FloatImaginary, BasicType::DoubleImaginary, BasicType::LongDoubleImaginary }, |
---|
555 | { BasicType::Float, BasicType::Double, BasicType::LongDouble }, |
---|
556 | }; |
---|
557 | |
---|
558 | FloatingPoint: ; |
---|
559 | if ( td->signedness != DeclarationNode::NoSignedness ) { |
---|
560 | throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::signednessName[ td->signedness ] + " in type: ", td ); |
---|
561 | } // if |
---|
562 | if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) { |
---|
563 | throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthName[ td->length ] + " in type: ", td ); |
---|
564 | } // if |
---|
565 | if ( td->basictype == DeclarationNode::Float && td->length == DeclarationNode::Long ) { |
---|
566 | throw SemanticError( "invalid type specifier \"long\" in type: ", td ); |
---|
567 | } // if |
---|
568 | if ( td->length == DeclarationNode::Long ) { |
---|
569 | const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble; |
---|
570 | } // if |
---|
571 | |
---|
572 | ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ]; |
---|
573 | break; |
---|
574 | |
---|
575 | case DeclarationNode::NoBasicType: |
---|
576 | // No basic type in declaration => default double for Complex/Imaginary and int type for integral types |
---|
577 | if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) { |
---|
578 | const_cast<TypeData *>(td)->basictype = DeclarationNode::Double; |
---|
579 | goto FloatingPoint; |
---|
580 | } // if |
---|
581 | |
---|
582 | const_cast<TypeData *>(td)->basictype = DeclarationNode::Int; |
---|
583 | goto Integral; |
---|
584 | } // switch |
---|
585 | |
---|
586 | BasicType * bt = new BasicType( buildQualifiers( td ), ret ); |
---|
587 | buildForall( td->forall, bt->get_forall() ); |
---|
588 | return bt; |
---|
589 | } // buildBasicType |
---|
590 | |
---|
591 | PointerType * buildPointer( const TypeData * td ) { |
---|
592 | PointerType * pt; |
---|
593 | if ( td->base ) { |
---|
594 | pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) ); |
---|
595 | } else { |
---|
596 | pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
597 | } // if |
---|
598 | buildForall( td->forall, pt->get_forall() ); |
---|
599 | return pt; |
---|
600 | } // buildPointer |
---|
601 | |
---|
602 | ArrayType * buildArray( const TypeData * td ) { |
---|
603 | ArrayType * at; |
---|
604 | if ( td->base ) { |
---|
605 | at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ), |
---|
606 | td->array.isVarLen, td->array.isStatic ); |
---|
607 | } else { |
---|
608 | at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ), |
---|
609 | maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic ); |
---|
610 | } // if |
---|
611 | buildForall( td->forall, at->get_forall() ); |
---|
612 | return at; |
---|
613 | } // buildPointer |
---|
614 | |
---|
615 | AggregateDecl * buildAggregate( const TypeData * td ) { |
---|
616 | assert( td->kind == TypeData::Aggregate ); |
---|
617 | AggregateDecl * at; |
---|
618 | switch ( td->aggregate.kind ) { |
---|
619 | case DeclarationNode::Struct: |
---|
620 | at = new StructDecl( *td->aggregate.name ); |
---|
621 | buildForall( td->aggregate.params, at->get_parameters() ); |
---|
622 | break; |
---|
623 | case DeclarationNode::Union: |
---|
624 | at = new UnionDecl( *td->aggregate.name ); |
---|
625 | buildForall( td->aggregate.params, at->get_parameters() ); |
---|
626 | break; |
---|
627 | case DeclarationNode::Trait: |
---|
628 | at = new TraitDecl( *td->aggregate.name ); |
---|
629 | buildList( td->aggregate.params, at->get_parameters() ); |
---|
630 | break; |
---|
631 | default: |
---|
632 | assert( false ); |
---|
633 | } // switch |
---|
634 | |
---|
635 | buildList( td->aggregate.fields, at->get_members() ); |
---|
636 | at->set_body( td->aggregate.body ); |
---|
637 | |
---|
638 | return at; |
---|
639 | } // buildAggregate |
---|
640 | |
---|
641 | ReferenceToType * buildAggInst( const TypeData * td ) { |
---|
642 | assert( td->kind == TypeData::AggregateInst ); |
---|
643 | |
---|
644 | ReferenceToType * ret; |
---|
645 | if ( td->aggInst.aggregate->kind == TypeData::Enum ) { |
---|
646 | ret = new EnumInstType( buildQualifiers( td ), *td->aggInst.aggregate->enumeration.name ); |
---|
647 | } else { |
---|
648 | assert( td->aggInst.aggregate->kind == TypeData::Aggregate ); |
---|
649 | switch ( td->aggInst.aggregate->aggregate.kind ) { |
---|
650 | case DeclarationNode::Struct: |
---|
651 | assert( td->aggInst.aggregate->aggregate.name ); |
---|
652 | ret = new StructInstType( buildQualifiers( td ), *td->aggInst.aggregate->aggregate.name ); |
---|
653 | break; |
---|
654 | case DeclarationNode::Union: |
---|
655 | ret = new UnionInstType( buildQualifiers( td ), *td->aggInst.aggregate->aggregate.name ); |
---|
656 | break; |
---|
657 | case DeclarationNode::Trait: |
---|
658 | ret = new TraitInstType( buildQualifiers( td ), *td->aggInst.aggregate->aggregate.name ); |
---|
659 | break; |
---|
660 | default: |
---|
661 | assert( false ); |
---|
662 | } // switch |
---|
663 | } // if |
---|
664 | buildList( td->aggInst.params, ret->get_parameters() ); |
---|
665 | buildForall( td->forall, ret->get_forall() ); |
---|
666 | return ret; |
---|
667 | } // buildAggInst |
---|
668 | |
---|
669 | NamedTypeDecl * buildSymbolic( const TypeData * td, const string & name, DeclarationNode::StorageClass sc ) { |
---|
670 | assert( td->kind == TypeData::Symbolic ); |
---|
671 | NamedTypeDecl * ret; |
---|
672 | assert( td->base ); |
---|
673 | if ( td->symbolic.isTypedef ) { |
---|
674 | ret = new TypedefDecl( name, sc, typebuild( td->base ) ); |
---|
675 | } else { |
---|
676 | ret = new TypeDecl( name, sc, typebuild( td->base ), TypeDecl::Any ); |
---|
677 | } // if |
---|
678 | buildList( td->symbolic.params, ret->get_parameters() ); |
---|
679 | buildList( td->symbolic.assertions, ret->get_assertions() ); |
---|
680 | return ret; |
---|
681 | } // buildSymbolic |
---|
682 | |
---|
683 | EnumDecl * buildEnum( const TypeData * td ) { |
---|
684 | assert( td->kind == TypeData::Enum ); |
---|
685 | EnumDecl * ret = new EnumDecl( *td->enumeration.name ); |
---|
686 | buildList( td->enumeration.constants, ret->get_members() ); |
---|
687 | list< Declaration * >::iterator members = ret->get_members().begin(); |
---|
688 | for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) { |
---|
689 | if ( cur->has_enumeratorValue() ) { |
---|
690 | ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members); |
---|
691 | member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ), list< Expression * >() ) ); |
---|
692 | } // if |
---|
693 | } // for |
---|
694 | return ret; |
---|
695 | } // buildEnum |
---|
696 | |
---|
697 | TypeInstType * buildSymbolicInst( const TypeData * td ) { |
---|
698 | assert( td->kind == TypeData::SymbolicInst ); |
---|
699 | TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false ); |
---|
700 | buildList( td->symbolic.actuals, ret->get_parameters() ); |
---|
701 | buildForall( td->forall, ret->get_forall() ); |
---|
702 | return ret; |
---|
703 | } // buildSymbolicInst |
---|
704 | |
---|
705 | TupleType * buildTuple( const TypeData * td ) { |
---|
706 | assert( td->kind == TypeData::Tuple ); |
---|
707 | TupleType * ret = new TupleType( buildQualifiers( td ) ); |
---|
708 | buildTypeList( td->tuple, ret->get_types() ); |
---|
709 | buildForall( td->forall, ret->get_forall() ); |
---|
710 | return ret; |
---|
711 | } // buildTuple |
---|
712 | |
---|
713 | TypeofType * buildTypeof( const TypeData * td ) { |
---|
714 | assert( td->kind == TypeData::Typeof ); |
---|
715 | assert( td->typeexpr ); |
---|
716 | // assert( td->typeexpr->expr ); |
---|
717 | return new TypeofType( buildQualifiers( td ), td->typeexpr->build() ); |
---|
718 | } // buildTypeof |
---|
719 | |
---|
720 | Declaration * buildDecl( const TypeData * td, const string &name, DeclarationNode::StorageClass sc, Expression * bitfieldWidth, bool isInline, bool isNoreturn, LinkageSpec::Spec linkage, Initializer * init ) { |
---|
721 | if ( td->kind == TypeData::Function ) { |
---|
722 | FunctionDecl * decl; |
---|
723 | if ( td->function.hasBody ) { |
---|
724 | if ( td->function.body ) { |
---|
725 | Statement * stmt = td->function.body->build(); |
---|
726 | CompoundStmt * body = dynamic_cast< CompoundStmt* >( stmt ); |
---|
727 | assert( body ); |
---|
728 | decl = new FunctionDecl( name, sc, linkage, buildFunction( td ), body, isInline, isNoreturn ); |
---|
729 | } else { |
---|
730 | // list< Label > ls; |
---|
731 | decl = new FunctionDecl( name, sc, linkage, buildFunction( td ), new CompoundStmt( list< Label >() ), isInline, isNoreturn ); |
---|
732 | } // if |
---|
733 | } else { |
---|
734 | decl = new FunctionDecl( name, sc, linkage, buildFunction( td ), nullptr, isInline, isNoreturn ); |
---|
735 | } // if |
---|
736 | for ( DeclarationNode * cur = td->function.idList; cur != nullptr; cur = dynamic_cast< DeclarationNode* >( cur->get_next() ) ) { |
---|
737 | if ( cur->name ) { |
---|
738 | decl->get_oldIdents().insert( decl->get_oldIdents().end(), *cur->name ); |
---|
739 | } // if |
---|
740 | } // for |
---|
741 | buildList( td->function.oldDeclList, decl->get_oldDecls() ); |
---|
742 | return decl; |
---|
743 | } else if ( td->kind == TypeData::Aggregate ) { |
---|
744 | return buildAggregate( td ); |
---|
745 | } else if ( td->kind == TypeData::Enum ) { |
---|
746 | return buildEnum( td ); |
---|
747 | } else if ( td->kind == TypeData::Symbolic ) { |
---|
748 | return buildSymbolic( td, name, sc ); |
---|
749 | } else { |
---|
750 | return new ObjectDecl( name, sc, linkage, bitfieldWidth, typebuild( td ), init, list< Attribute * >(), isInline, isNoreturn ); |
---|
751 | } // if |
---|
752 | return nullptr; |
---|
753 | } // buildDecl |
---|
754 | |
---|
755 | FunctionType * buildFunction( const TypeData * td ) { |
---|
756 | assert( td->kind == TypeData::Function ); |
---|
757 | bool hasEllipsis = td->function.params ? td->function.params->get_hasEllipsis() : true; |
---|
758 | if ( ! td->function.params ) hasEllipsis = ! td->function.newStyle; |
---|
759 | FunctionType * ft = new FunctionType( buildQualifiers( td ), hasEllipsis ); |
---|
760 | buildList( td->function.params, ft->get_parameters() ); |
---|
761 | buildForall( td->forall, ft->get_forall() ); |
---|
762 | if ( td->base ) { |
---|
763 | switch ( td->base->kind ) { |
---|
764 | case TypeData::Tuple: |
---|
765 | buildList( td->base->tuple, ft->get_returnVals() ); |
---|
766 | break; |
---|
767 | default: |
---|
768 | ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType* >( buildDecl( td->base, "", DeclarationNode::NoStorageClass, nullptr, false, false, LinkageSpec::Cforall ) ) ); |
---|
769 | } // switch |
---|
770 | } else { |
---|
771 | ft->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) ); |
---|
772 | } // if |
---|
773 | return ft; |
---|
774 | } // buildFunction |
---|
775 | |
---|
776 | // Local Variables: // |
---|
777 | // tab-width: 4 // |
---|
778 | // mode: c++ // |
---|
779 | // compile-command: "make install" // |
---|
780 | // End: // |
---|