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 : Andrew Beach |
---|
12 | // Last Modified On : Fri Aug 11 10:50:00 2017 |
---|
13 | // Update Count : 567 |
---|
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 ) /*, PTR1( (void*)(0xdeadbeefdeadbeef)), PTR2( (void*)(0xdeadbeefdeadbeef) ) */ { |
---|
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.newStyle = false; |
---|
51 | break; |
---|
52 | // Enum is an Aggregate, so both structures are initialized together. |
---|
53 | case Enum: |
---|
54 | // enumeration = new Enumeration_t; |
---|
55 | enumeration.name = nullptr; |
---|
56 | enumeration.constants = nullptr; |
---|
57 | enumeration.body = false; |
---|
58 | case Aggregate: |
---|
59 | // aggregate = new Aggregate_t; |
---|
60 | aggregate.name = nullptr; |
---|
61 | aggregate.params = nullptr; |
---|
62 | aggregate.actuals = nullptr; |
---|
63 | aggregate.fields = nullptr; |
---|
64 | aggregate.body = false; |
---|
65 | break; |
---|
66 | case AggregateInst: |
---|
67 | // aggInst = new AggInst_t; |
---|
68 | aggInst.aggregate = nullptr; |
---|
69 | aggInst.params = nullptr; |
---|
70 | aggInst.hoistType = false;; |
---|
71 | break; |
---|
72 | case Symbolic: |
---|
73 | case SymbolicInst: |
---|
74 | // symbolic = new Symbolic_t; |
---|
75 | symbolic.name = nullptr; |
---|
76 | symbolic.params = nullptr; |
---|
77 | symbolic.actuals = nullptr; |
---|
78 | symbolic.assertions = nullptr; |
---|
79 | break; |
---|
80 | case Tuple: |
---|
81 | // tuple = new Tuple_t; |
---|
82 | tuple = nullptr; |
---|
83 | break; |
---|
84 | case Typeof: |
---|
85 | // typeexpr = new Typeof_t; |
---|
86 | typeexpr = nullptr; |
---|
87 | break; |
---|
88 | case Builtin: |
---|
89 | // builtin = new Builtin_t; |
---|
90 | break; |
---|
91 | } // switch |
---|
92 | } // TypeData::TypeData |
---|
93 | |
---|
94 | TypeData::~TypeData() { |
---|
95 | delete base; |
---|
96 | delete forall; |
---|
97 | |
---|
98 | switch ( kind ) { |
---|
99 | case Unknown: |
---|
100 | case Pointer: |
---|
101 | case EnumConstant: |
---|
102 | // nothing to destroy |
---|
103 | break; |
---|
104 | case Basic: |
---|
105 | // delete basic; |
---|
106 | break; |
---|
107 | case Array: |
---|
108 | delete array.dimension; |
---|
109 | // delete array; |
---|
110 | break; |
---|
111 | case Function: |
---|
112 | delete function.params; |
---|
113 | delete function.idList; |
---|
114 | delete function.oldDeclList; |
---|
115 | delete function.body; |
---|
116 | // delete function; |
---|
117 | break; |
---|
118 | case Aggregate: |
---|
119 | delete aggregate.name; |
---|
120 | delete aggregate.params; |
---|
121 | delete aggregate.actuals; |
---|
122 | delete aggregate.fields; |
---|
123 | // delete aggregate; |
---|
124 | break; |
---|
125 | case AggregateInst: |
---|
126 | delete aggInst.aggregate; |
---|
127 | delete aggInst.params; |
---|
128 | // delete aggInst; |
---|
129 | break; |
---|
130 | case Enum: |
---|
131 | delete enumeration.name; |
---|
132 | delete enumeration.constants; |
---|
133 | // delete enumeration; |
---|
134 | break; |
---|
135 | case Symbolic: |
---|
136 | case SymbolicInst: |
---|
137 | delete symbolic.name; |
---|
138 | delete symbolic.params; |
---|
139 | delete symbolic.actuals; |
---|
140 | delete symbolic.assertions; |
---|
141 | // delete symbolic; |
---|
142 | break; |
---|
143 | case Tuple: |
---|
144 | // delete tuple->members; |
---|
145 | delete tuple; |
---|
146 | break; |
---|
147 | case Typeof: |
---|
148 | // delete typeexpr->expr; |
---|
149 | delete typeexpr; |
---|
150 | break; |
---|
151 | case Builtin: |
---|
152 | // delete builtin; |
---|
153 | break; |
---|
154 | } // switch |
---|
155 | } // TypeData::~TypeData |
---|
156 | |
---|
157 | TypeData * TypeData::clone() const { |
---|
158 | TypeData * newtype = new TypeData( kind ); |
---|
159 | newtype->qualifiers = qualifiers; |
---|
160 | newtype->base = maybeClone( base ); |
---|
161 | newtype->forall = maybeClone( forall ); |
---|
162 | |
---|
163 | switch ( kind ) { |
---|
164 | case Unknown: |
---|
165 | case EnumConstant: |
---|
166 | case Pointer: |
---|
167 | // nothing else to copy |
---|
168 | break; |
---|
169 | case Basic: |
---|
170 | newtype->basictype = basictype; |
---|
171 | newtype->complextype = complextype; |
---|
172 | newtype->signedness = signedness; |
---|
173 | newtype->length = length; |
---|
174 | break; |
---|
175 | case Array: |
---|
176 | newtype->array.dimension = maybeClone( array.dimension ); |
---|
177 | newtype->array.isVarLen = array.isVarLen; |
---|
178 | newtype->array.isStatic = array.isStatic; |
---|
179 | break; |
---|
180 | case Function: |
---|
181 | newtype->function.params = maybeClone( function.params ); |
---|
182 | newtype->function.idList = maybeClone( function.idList ); |
---|
183 | newtype->function.oldDeclList = maybeClone( function.oldDeclList ); |
---|
184 | newtype->function.body = maybeClone( function.body ); |
---|
185 | newtype->function.newStyle = function.newStyle; |
---|
186 | break; |
---|
187 | case Aggregate: |
---|
188 | newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr; |
---|
189 | newtype->aggregate.params = maybeClone( aggregate.params ); |
---|
190 | newtype->aggregate.actuals = maybeClone( aggregate.actuals ); |
---|
191 | newtype->aggregate.fields = maybeClone( aggregate.fields ); |
---|
192 | newtype->aggregate.kind = aggregate.kind; |
---|
193 | newtype->aggregate.body = aggregate.body; |
---|
194 | newtype->aggregate.tagged = aggregate.tagged; |
---|
195 | newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr; |
---|
196 | break; |
---|
197 | case AggregateInst: |
---|
198 | newtype->aggInst.aggregate = maybeClone( aggInst.aggregate ); |
---|
199 | newtype->aggInst.params = maybeClone( aggInst.params ); |
---|
200 | newtype->aggInst.hoistType = aggInst.hoistType; |
---|
201 | break; |
---|
202 | case Enum: |
---|
203 | newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr; |
---|
204 | newtype->enumeration.constants = maybeClone( enumeration.constants ); |
---|
205 | newtype->enumeration.body = enumeration.body; |
---|
206 | break; |
---|
207 | case Symbolic: |
---|
208 | case SymbolicInst: |
---|
209 | newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr; |
---|
210 | newtype->symbolic.params = maybeClone( symbolic.params ); |
---|
211 | newtype->symbolic.actuals = maybeClone( symbolic.actuals ); |
---|
212 | newtype->symbolic.assertions = maybeClone( symbolic.assertions ); |
---|
213 | newtype->symbolic.isTypedef = symbolic.isTypedef; |
---|
214 | break; |
---|
215 | case Tuple: |
---|
216 | newtype->tuple = maybeClone( tuple ); |
---|
217 | break; |
---|
218 | case Typeof: |
---|
219 | newtype->typeexpr = maybeClone( typeexpr ); |
---|
220 | break; |
---|
221 | case Builtin: |
---|
222 | assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One ); |
---|
223 | newtype->builtintype = builtintype; |
---|
224 | break; |
---|
225 | } // switch |
---|
226 | return newtype; |
---|
227 | } // TypeData::clone |
---|
228 | |
---|
229 | void TypeData::print( ostream &os, int indent ) const { |
---|
230 | for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) { |
---|
231 | if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' '; |
---|
232 | } // for |
---|
233 | |
---|
234 | if ( forall ) { |
---|
235 | os << "forall " << endl; |
---|
236 | forall->printList( os, indent + 4 ); |
---|
237 | } // if |
---|
238 | |
---|
239 | switch ( kind ) { |
---|
240 | case Unknown: |
---|
241 | os << "entity of unknown type "; |
---|
242 | break; |
---|
243 | case Pointer: |
---|
244 | os << "pointer "; |
---|
245 | if ( base ) { |
---|
246 | os << "to "; |
---|
247 | base->print( os, indent ); |
---|
248 | } // if |
---|
249 | break; |
---|
250 | case EnumConstant: |
---|
251 | os << "enumeration constant "; |
---|
252 | break; |
---|
253 | case Basic: |
---|
254 | if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " "; |
---|
255 | if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " "; |
---|
256 | assert( basictype != DeclarationNode::NoBasicType ); |
---|
257 | os << DeclarationNode::basicTypeNames[ basictype ] << " "; |
---|
258 | if ( complextype != DeclarationNode::NoComplexType ) os << DeclarationNode::complexTypeNames[ complextype ] << " "; |
---|
259 | break; |
---|
260 | case Array: |
---|
261 | if ( array.isStatic ) { |
---|
262 | os << "static "; |
---|
263 | } // if |
---|
264 | if ( array.dimension ) { |
---|
265 | os << "array of "; |
---|
266 | array.dimension->printOneLine( os, indent ); |
---|
267 | } else if ( array.isVarLen ) { |
---|
268 | os << "variable-length array of "; |
---|
269 | } else { |
---|
270 | os << "open array of "; |
---|
271 | } // if |
---|
272 | if ( base ) { |
---|
273 | base->print( os, indent ); |
---|
274 | } // if |
---|
275 | break; |
---|
276 | case Function: |
---|
277 | os << "function" << endl; |
---|
278 | if ( function.params ) { |
---|
279 | os << string( indent + 2, ' ' ) << "with parameters " << endl; |
---|
280 | function.params->printList( os, indent + 4 ); |
---|
281 | } else { |
---|
282 | os << string( indent + 2, ' ' ) << "with no parameters " << endl; |
---|
283 | } // if |
---|
284 | if ( function.idList ) { |
---|
285 | os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl; |
---|
286 | function.idList->printList( os, indent + 4 ); |
---|
287 | } // if |
---|
288 | if ( function.oldDeclList ) { |
---|
289 | os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl; |
---|
290 | function.oldDeclList->printList( os, indent + 4 ); |
---|
291 | } // if |
---|
292 | os << string( indent + 2, ' ' ) << "returning "; |
---|
293 | if ( base ) { |
---|
294 | base->print( os, indent + 4 ); |
---|
295 | } else { |
---|
296 | os << "nothing "; |
---|
297 | } // if |
---|
298 | os << endl; |
---|
299 | if ( function.body ) { |
---|
300 | os << string( indent + 2, ' ' ) << "with body " << endl; |
---|
301 | function.body->printList( os, indent + 2 ); |
---|
302 | } // if |
---|
303 | break; |
---|
304 | case Aggregate: |
---|
305 | os << DeclarationNode::aggregateNames[ aggregate.kind ] << ' ' << *aggregate.name << endl; |
---|
306 | if ( aggregate.params ) { |
---|
307 | os << string( indent + 2, ' ' ) << "with type parameters " << endl; |
---|
308 | aggregate.params->printList( os, indent + 4 ); |
---|
309 | } // if |
---|
310 | if ( aggregate.actuals ) { |
---|
311 | os << string( indent + 2, ' ' ) << "instantiated with actual parameters " << endl; |
---|
312 | aggregate.actuals->printList( os, indent + 4 ); |
---|
313 | } // if |
---|
314 | if ( aggregate.fields ) { |
---|
315 | os << string( indent + 2, ' ' ) << "with members " << endl; |
---|
316 | aggregate.fields->printList( os, indent + 4 ); |
---|
317 | } // if |
---|
318 | if ( aggregate.body ) { |
---|
319 | os << string( indent + 2, ' ' ) << " with body " << endl; |
---|
320 | } // if |
---|
321 | break; |
---|
322 | case AggregateInst: |
---|
323 | if ( aggInst.aggregate ) { |
---|
324 | os << "instance of " ; |
---|
325 | aggInst.aggregate->print( os, indent ); |
---|
326 | } else { |
---|
327 | os << "instance of an unspecified aggregate "; |
---|
328 | } // if |
---|
329 | if ( aggInst.params ) { |
---|
330 | os << string( indent + 2, ' ' ) << "with parameters " << endl; |
---|
331 | aggInst.params->printList( os, indent + 2 ); |
---|
332 | } // if |
---|
333 | break; |
---|
334 | case Enum: |
---|
335 | os << "enumeration "; |
---|
336 | if ( enumeration.constants ) { |
---|
337 | os << "with constants" << endl; |
---|
338 | enumeration.constants->printList( os, indent + 2 ); |
---|
339 | } // if |
---|
340 | if ( enumeration.body ) { |
---|
341 | os << string( indent + 2, ' ' ) << " with body " << endl; |
---|
342 | } // if |
---|
343 | break; |
---|
344 | case SymbolicInst: |
---|
345 | os << "instance of type " << *symbolic.name; |
---|
346 | if ( symbolic.actuals ) { |
---|
347 | os << " with parameters" << endl; |
---|
348 | symbolic.actuals->printList( os, indent + 2 ); |
---|
349 | } // if |
---|
350 | break; |
---|
351 | case Symbolic: |
---|
352 | if ( symbolic.isTypedef ) { |
---|
353 | os << "typedef definition "; |
---|
354 | } else { |
---|
355 | os << "type definition "; |
---|
356 | } // if |
---|
357 | if ( symbolic.params ) { |
---|
358 | os << endl << string( indent + 2, ' ' ) << "with parameters" << endl; |
---|
359 | symbolic.params->printList( os, indent + 2 ); |
---|
360 | } // if |
---|
361 | if ( symbolic.assertions ) { |
---|
362 | os << endl << string( indent + 2, ' ' ) << "with assertions" << endl; |
---|
363 | symbolic.assertions->printList( os, indent + 4 ); |
---|
364 | os << string( indent + 2, ' ' ); |
---|
365 | } // if |
---|
366 | if ( base ) { |
---|
367 | os << "for "; |
---|
368 | base->print( os, indent + 2 ); |
---|
369 | } // if |
---|
370 | break; |
---|
371 | case Tuple: |
---|
372 | os << "tuple "; |
---|
373 | if ( tuple ) { |
---|
374 | os << "with members " << endl; |
---|
375 | tuple->printList( os, indent + 2 ); |
---|
376 | } // if |
---|
377 | break; |
---|
378 | case Typeof: |
---|
379 | os << "type-of expression "; |
---|
380 | if ( typeexpr ) { |
---|
381 | typeexpr->print( os, indent + 2 ); |
---|
382 | } // if |
---|
383 | break; |
---|
384 | case Builtin: |
---|
385 | os << "gcc builtin type"; |
---|
386 | break; |
---|
387 | default: |
---|
388 | os << "internal error: TypeData::print " << kind << endl; |
---|
389 | assert( false ); |
---|
390 | } // switch |
---|
391 | } // TypeData::print |
---|
392 | |
---|
393 | template< typename ForallList > |
---|
394 | void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) { |
---|
395 | buildList( firstNode, outputList ); |
---|
396 | for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i ) { |
---|
397 | TypeDecl * td = static_cast<TypeDecl *>(*i); |
---|
398 | if ( td->get_kind() == TypeDecl::Any ) { |
---|
399 | // add assertion parameters to `type' tyvars in reverse order |
---|
400 | // add dtor: void ^?{}(T *) |
---|
401 | FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false ); |
---|
402 | dtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) ); |
---|
403 | td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) ); |
---|
404 | |
---|
405 | // add copy ctor: void ?{}(T *, T) |
---|
406 | FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false ); |
---|
407 | copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) ); |
---|
408 | copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) ); |
---|
409 | td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) ); |
---|
410 | |
---|
411 | // add default ctor: void ?{}(T *) |
---|
412 | FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false ); |
---|
413 | ctorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) ); |
---|
414 | td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) ); |
---|
415 | |
---|
416 | // add assignment operator: T * ?=?(T *, T) |
---|
417 | FunctionType * assignType = new FunctionType( Type::Qualifiers(), false ); |
---|
418 | assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) ); |
---|
419 | assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) ); |
---|
420 | assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) ); |
---|
421 | td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) ); |
---|
422 | } // if |
---|
423 | } // for |
---|
424 | } |
---|
425 | |
---|
426 | Type * typebuild( const TypeData * td ) { |
---|
427 | assert( td ); |
---|
428 | switch ( td->kind ) { |
---|
429 | case TypeData::Unknown: |
---|
430 | // fill in implicit int |
---|
431 | return new BasicType( buildQualifiers( td ), BasicType::SignedInt ); |
---|
432 | case TypeData::Basic: |
---|
433 | return buildBasicType( td ); |
---|
434 | case TypeData::Pointer: |
---|
435 | return buildPointer( td ); |
---|
436 | case TypeData::Array: |
---|
437 | return buildArray( td ); |
---|
438 | case TypeData::Function: |
---|
439 | return buildFunction( td ); |
---|
440 | case TypeData::AggregateInst: |
---|
441 | return buildAggInst( td ); |
---|
442 | case TypeData::EnumConstant: |
---|
443 | // the name gets filled in later -- by SymTab::Validate |
---|
444 | return new EnumInstType( buildQualifiers( td ), "" ); |
---|
445 | case TypeData::SymbolicInst: |
---|
446 | return buildSymbolicInst( td );; |
---|
447 | case TypeData::Tuple: |
---|
448 | return buildTuple( td ); |
---|
449 | case TypeData::Typeof: |
---|
450 | return buildTypeof( td ); |
---|
451 | case TypeData::Builtin: |
---|
452 | if(td->builtintype == DeclarationNode::Zero) { |
---|
453 | return new ZeroType( noQualifiers ); |
---|
454 | } |
---|
455 | else if(td->builtintype == DeclarationNode::One) { |
---|
456 | return new OneType( noQualifiers ); |
---|
457 | } |
---|
458 | else { |
---|
459 | return new VarArgsType( buildQualifiers( td ) ); |
---|
460 | } |
---|
461 | case TypeData::Symbolic: |
---|
462 | case TypeData::Enum: |
---|
463 | case TypeData::Aggregate: |
---|
464 | assert( false ); |
---|
465 | } // switch |
---|
466 | return nullptr; |
---|
467 | } // typebuild |
---|
468 | |
---|
469 | TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) { |
---|
470 | TypeData * ret = nullptr; |
---|
471 | |
---|
472 | switch ( td->kind ) { |
---|
473 | case TypeData::Aggregate: |
---|
474 | if ( ! toplevel && td->aggregate.fields ) { |
---|
475 | ret = td->clone(); |
---|
476 | } // if |
---|
477 | break; |
---|
478 | case TypeData::Enum: |
---|
479 | if ( ! toplevel && td->enumeration.constants ) { |
---|
480 | ret = td->clone(); |
---|
481 | } // if |
---|
482 | break; |
---|
483 | case TypeData::AggregateInst: |
---|
484 | if ( td->aggInst.aggregate ) { |
---|
485 | ret = typeextractAggregate( td->aggInst.aggregate, false ); |
---|
486 | } // if |
---|
487 | break; |
---|
488 | default: |
---|
489 | if ( td->base ) { |
---|
490 | ret = typeextractAggregate( td->base, false ); |
---|
491 | } // if |
---|
492 | } // switch |
---|
493 | return ret; |
---|
494 | } // typeextractAggregate |
---|
495 | |
---|
496 | Type::Qualifiers buildQualifiers( const TypeData * td ) { |
---|
497 | return td->qualifiers; |
---|
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::signednessNames[ td->signedness ] + " in type: ", td ); |
---|
515 | } // if |
---|
516 | if ( td->length != DeclarationNode::NoLength ) { |
---|
517 | throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ 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::lengthNames[ 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::signednessNames[ 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::lengthNames[ 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 | default: |
---|
585 | assert(false); |
---|
586 | return nullptr; |
---|
587 | } // switch |
---|
588 | |
---|
589 | BasicType * bt = new BasicType( buildQualifiers( td ), ret ); |
---|
590 | buildForall( td->forall, bt->get_forall() ); |
---|
591 | return bt; |
---|
592 | } // buildBasicType |
---|
593 | |
---|
594 | PointerType * buildPointer( const TypeData * td ) { |
---|
595 | PointerType * pt; |
---|
596 | if ( td->base ) { |
---|
597 | pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) ); |
---|
598 | } else { |
---|
599 | pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
600 | } // if |
---|
601 | buildForall( td->forall, pt->get_forall() ); |
---|
602 | return pt; |
---|
603 | } // buildPointer |
---|
604 | |
---|
605 | ArrayType * buildArray( const TypeData * td ) { |
---|
606 | ArrayType * at; |
---|
607 | if ( td->base ) { |
---|
608 | at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ), |
---|
609 | td->array.isVarLen, td->array.isStatic ); |
---|
610 | } else { |
---|
611 | at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ), |
---|
612 | maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic ); |
---|
613 | } // if |
---|
614 | buildForall( td->forall, at->get_forall() ); |
---|
615 | return at; |
---|
616 | } // buildPointer |
---|
617 | |
---|
618 | AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) { |
---|
619 | assert( td->kind == TypeData::Aggregate ); |
---|
620 | AggregateDecl * at; |
---|
621 | switch ( td->aggregate.kind ) { |
---|
622 | case DeclarationNode::Struct: |
---|
623 | case DeclarationNode::Coroutine: |
---|
624 | case DeclarationNode::Monitor: |
---|
625 | case DeclarationNode::Thread: |
---|
626 | at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage ); |
---|
627 | buildForall( td->aggregate.params, at->get_parameters() ); |
---|
628 | break; |
---|
629 | case DeclarationNode::Union: |
---|
630 | at = new UnionDecl( *td->aggregate.name, attributes ); |
---|
631 | buildForall( td->aggregate.params, at->get_parameters() ); |
---|
632 | break; |
---|
633 | case DeclarationNode::Trait: |
---|
634 | at = new TraitDecl( *td->aggregate.name, attributes ); |
---|
635 | buildList( td->aggregate.params, at->get_parameters() ); |
---|
636 | break; |
---|
637 | default: |
---|
638 | assert( false ); |
---|
639 | } // switch |
---|
640 | |
---|
641 | buildList( td->aggregate.fields, at->get_members() ); |
---|
642 | at->set_body( td->aggregate.body ); |
---|
643 | |
---|
644 | return at; |
---|
645 | } // buildAggregate |
---|
646 | |
---|
647 | ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) { |
---|
648 | switch ( type->kind ) { |
---|
649 | case TypeData::Enum: { |
---|
650 | if ( type->enumeration.body ) { |
---|
651 | EnumDecl * typedecl = buildEnum( type, attributes ); |
---|
652 | return new EnumInstType( buildQualifiers( type ), typedecl ); |
---|
653 | } else { |
---|
654 | return new EnumInstType( buildQualifiers( type ), *type->enumeration.name ); |
---|
655 | } // if |
---|
656 | } |
---|
657 | case TypeData::Aggregate: { |
---|
658 | ReferenceToType * ret; |
---|
659 | if ( type->aggregate.body ) { |
---|
660 | AggregateDecl * typedecl = buildAggregate( type, attributes, linkage ); |
---|
661 | switch ( type->aggregate.kind ) { |
---|
662 | case DeclarationNode::Struct: |
---|
663 | case DeclarationNode::Coroutine: |
---|
664 | case DeclarationNode::Monitor: |
---|
665 | case DeclarationNode::Thread: |
---|
666 | ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl ); |
---|
667 | break; |
---|
668 | case DeclarationNode::Union: |
---|
669 | ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl ); |
---|
670 | break; |
---|
671 | case DeclarationNode::Trait: |
---|
672 | assert( false ); |
---|
673 | //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl ); |
---|
674 | break; |
---|
675 | default: |
---|
676 | assert( false ); |
---|
677 | } // switch |
---|
678 | } else { |
---|
679 | switch ( type->aggregate.kind ) { |
---|
680 | case DeclarationNode::Struct: |
---|
681 | case DeclarationNode::Coroutine: |
---|
682 | case DeclarationNode::Monitor: |
---|
683 | case DeclarationNode::Thread: |
---|
684 | ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name ); |
---|
685 | break; |
---|
686 | case DeclarationNode::Union: |
---|
687 | ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name ); |
---|
688 | break; |
---|
689 | case DeclarationNode::Trait: |
---|
690 | ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name ); |
---|
691 | break; |
---|
692 | default: |
---|
693 | assert( false ); |
---|
694 | } // switch |
---|
695 | } // if |
---|
696 | return ret; |
---|
697 | } |
---|
698 | default: |
---|
699 | assert( false ); |
---|
700 | } // switch |
---|
701 | } // buildAggInst |
---|
702 | |
---|
703 | ReferenceToType * buildAggInst( const TypeData * td ) { |
---|
704 | assert( td->kind == TypeData::AggregateInst ); |
---|
705 | |
---|
706 | // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() ); |
---|
707 | ReferenceToType * ret = nullptr; |
---|
708 | TypeData * type = td->aggInst.aggregate; |
---|
709 | switch ( type->kind ) { |
---|
710 | case TypeData::Enum: { |
---|
711 | return new EnumInstType( buildQualifiers( type ), *type->enumeration.name ); |
---|
712 | } |
---|
713 | case TypeData::Aggregate: { |
---|
714 | switch ( type->aggregate.kind ) { |
---|
715 | case DeclarationNode::Struct: |
---|
716 | case DeclarationNode::Coroutine: |
---|
717 | case DeclarationNode::Monitor: |
---|
718 | case DeclarationNode::Thread: |
---|
719 | ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name ); |
---|
720 | break; |
---|
721 | case DeclarationNode::Union: |
---|
722 | ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name ); |
---|
723 | break; |
---|
724 | case DeclarationNode::Trait: |
---|
725 | ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name ); |
---|
726 | break; |
---|
727 | default: |
---|
728 | assert( false ); |
---|
729 | } // switch |
---|
730 | } |
---|
731 | break; |
---|
732 | default: |
---|
733 | assert( false ); |
---|
734 | } // switch |
---|
735 | |
---|
736 | ret->set_hoistType( td->aggInst.hoistType ); |
---|
737 | buildList( td->aggInst.params, ret->get_parameters() ); |
---|
738 | buildForall( td->forall, ret->get_forall() ); |
---|
739 | return ret; |
---|
740 | } // buildAggInst |
---|
741 | |
---|
742 | NamedTypeDecl * buildSymbolic( const TypeData * td, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) { |
---|
743 | assert( td->kind == TypeData::Symbolic ); |
---|
744 | NamedTypeDecl * ret; |
---|
745 | assert( td->base ); |
---|
746 | if ( td->symbolic.isTypedef ) { |
---|
747 | ret = new TypedefDecl( name, scs, typebuild( td->base ), linkage ); |
---|
748 | } else { |
---|
749 | ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Any ); |
---|
750 | } // if |
---|
751 | buildList( td->symbolic.params, ret->get_parameters() ); |
---|
752 | buildList( td->symbolic.assertions, ret->get_assertions() ); |
---|
753 | return ret; |
---|
754 | } // buildSymbolic |
---|
755 | |
---|
756 | EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes ) { |
---|
757 | assert( td->kind == TypeData::Enum ); |
---|
758 | EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes ); |
---|
759 | buildList( td->enumeration.constants, ret->get_members() ); |
---|
760 | list< Declaration * >::iterator members = ret->get_members().begin(); |
---|
761 | for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) { |
---|
762 | if ( cur->has_enumeratorValue() ) { |
---|
763 | ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members); |
---|
764 | member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) ); |
---|
765 | } // if |
---|
766 | } // for |
---|
767 | ret->set_body( td->enumeration.body ); |
---|
768 | return ret; |
---|
769 | } // buildEnum |
---|
770 | |
---|
771 | TypeInstType * buildSymbolicInst( const TypeData * td ) { |
---|
772 | assert( td->kind == TypeData::SymbolicInst ); |
---|
773 | TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false ); |
---|
774 | buildList( td->symbolic.actuals, ret->get_parameters() ); |
---|
775 | buildForall( td->forall, ret->get_forall() ); |
---|
776 | return ret; |
---|
777 | } // buildSymbolicInst |
---|
778 | |
---|
779 | TupleType * buildTuple( const TypeData * td ) { |
---|
780 | assert( td->kind == TypeData::Tuple ); |
---|
781 | std::list< Type * > types; |
---|
782 | buildTypeList( td->tuple, types ); |
---|
783 | TupleType * ret = new TupleType( buildQualifiers( td ), types ); |
---|
784 | buildForall( td->forall, ret->get_forall() ); |
---|
785 | return ret; |
---|
786 | } // buildTuple |
---|
787 | |
---|
788 | TypeofType * buildTypeof( const TypeData * td ) { |
---|
789 | assert( td->kind == TypeData::Typeof ); |
---|
790 | assert( td->typeexpr ); |
---|
791 | // assert( td->typeexpr->expr ); |
---|
792 | return new TypeofType( buildQualifiers( td ), td->typeexpr->build() ); |
---|
793 | } // buildTypeof |
---|
794 | |
---|
795 | Declaration * buildDecl( const TypeData * td, const string &name, Type::StorageClasses scs, Expression * bitfieldWidth, Type::FuncSpecifiers funcSpec, LinkageSpec::Spec linkage, ConstantExpr *asmName, Initializer * init, std::list< Attribute * > attributes ) { |
---|
796 | if ( td->kind == TypeData::Function ) { |
---|
797 | if ( td->function.idList ) { // KR function ? |
---|
798 | buildKRFunction( td->function ); // transform into C11 function |
---|
799 | } // if |
---|
800 | |
---|
801 | FunctionDecl * decl; |
---|
802 | Statement * stmt = maybeBuild<Statement>( td->function.body ); |
---|
803 | CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt ); |
---|
804 | decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec ); |
---|
805 | return decl->set_asmName( asmName ); |
---|
806 | } else if ( td->kind == TypeData::Aggregate ) { |
---|
807 | return buildAggregate( td, attributes, linkage ); |
---|
808 | } else if ( td->kind == TypeData::Enum ) { |
---|
809 | return buildEnum( td, attributes ); |
---|
810 | } else if ( td->kind == TypeData::Symbolic ) { |
---|
811 | return buildSymbolic( td, name, scs, linkage ); |
---|
812 | } else { |
---|
813 | return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName ); |
---|
814 | } // if |
---|
815 | return nullptr; |
---|
816 | } // buildDecl |
---|
817 | |
---|
818 | FunctionType * buildFunction( const TypeData * td ) { |
---|
819 | assert( td->kind == TypeData::Function ); |
---|
820 | bool hasEllipsis = td->function.params ? td->function.params->get_hasEllipsis() : true; |
---|
821 | if ( ! td->function.params ) hasEllipsis = ! td->function.newStyle; |
---|
822 | FunctionType * ft = new FunctionType( buildQualifiers( td ), hasEllipsis ); |
---|
823 | buildList( td->function.params, ft->get_parameters() ); |
---|
824 | buildForall( td->forall, ft->get_forall() ); |
---|
825 | if ( td->base ) { |
---|
826 | switch ( td->base->kind ) { |
---|
827 | case TypeData::Tuple: |
---|
828 | buildList( td->base->tuple, ft->get_returnVals() ); |
---|
829 | break; |
---|
830 | default: |
---|
831 | ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) ); |
---|
832 | } // switch |
---|
833 | } else { |
---|
834 | ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) ); |
---|
835 | } // if |
---|
836 | return ft; |
---|
837 | } // buildFunction |
---|
838 | |
---|
839 | // Transform KR routine declarations into C99 routine declarations: |
---|
840 | // |
---|
841 | // rtn( a, b, c ) int a, c; double b {} => int rtn( int a, double c, int b ) {} |
---|
842 | // |
---|
843 | // The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration |
---|
844 | // is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and |
---|
845 | // extra names are disallowed. |
---|
846 | // |
---|
847 | // Note, there is no KR routine-prototype syntax: |
---|
848 | // |
---|
849 | // rtn( a, b, c ) int a, c; double b; // invalid KR prototype |
---|
850 | // rtn(); // valid KR prototype |
---|
851 | |
---|
852 | void buildKRFunction( const TypeData::Function_t & function ) { |
---|
853 | assert( ! function.params ); |
---|
854 | // loop over declaration first as it is easier to spot errors |
---|
855 | for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) { |
---|
856 | // scan ALL parameter names for each declaration name to check for duplicates |
---|
857 | for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) { |
---|
858 | if ( *decl->name == *param->name ) { |
---|
859 | // type set => parameter name already transformed by a declaration names so there is a duplicate |
---|
860 | // declaration name attempting a second transformation |
---|
861 | if ( param->type ) throw SemanticError( string( "duplicate declaration name " ) + *param->name ); |
---|
862 | // declaration type reset => declaration already transformed by a parameter name so there is a duplicate |
---|
863 | // parameter name attempting a second transformation |
---|
864 | if ( ! decl->type ) throw SemanticError( string( "duplicate parameter name " ) + *param->name ); |
---|
865 | param->type = decl->type; // set copy declaration type to parameter type |
---|
866 | decl->type = nullptr; // reset declaration type |
---|
867 | param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter |
---|
868 | } // if |
---|
869 | } // for |
---|
870 | // declaration type still set => type not moved to a matching parameter so there is a missing parameter name |
---|
871 | if ( decl->type ) throw SemanticError( string( "missing name in parameter list " ) + *decl->name ); |
---|
872 | } // for |
---|
873 | |
---|
874 | // Parameter names without a declaration default to type int: |
---|
875 | // |
---|
876 | // rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {} |
---|
877 | |
---|
878 | for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) { |
---|
879 | if ( ! param->type ) { // generate type int for empty parameter type |
---|
880 | param->type = new TypeData( TypeData::Basic ); |
---|
881 | param->type->basictype = DeclarationNode::Int; |
---|
882 | } // if |
---|
883 | } // for |
---|
884 | |
---|
885 | function.params = function.idList; // newly modified idList becomes parameters |
---|
886 | function.idList = nullptr; // idList now empty |
---|
887 | delete function.oldDeclList; // deletes entire list |
---|
888 | function.oldDeclList = nullptr; // reset |
---|
889 | } // buildKRFunction |
---|
890 | |
---|
891 | // Local Variables: // |
---|
892 | // tab-width: 4 // |
---|
893 | // mode: c++ // |
---|
894 | // compile-command: "make install" // |
---|
895 | // End: // |
---|