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 : Sun Feb 19 11:00:46 2023 |
---|
13 | // Update Count : 679 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cassert> // for assert |
---|
17 | #include <ostream> // for operator<<, ostream, basic_ostream |
---|
18 | |
---|
19 | #include "Common/SemanticError.h" // for SemanticError |
---|
20 | #include "Common/utility.h" // for maybeClone, maybeBuild, maybeMoveB... |
---|
21 | #include "Parser/ParseNode.h" // for DeclarationNode, ExpressionNode |
---|
22 | #include "SynTree/Declaration.h" // for TypeDecl, ObjectDecl, FunctionDecl |
---|
23 | #include "SynTree/Expression.h" // for Expression, ConstantExpr (ptr only) |
---|
24 | #include "SynTree/Initializer.h" // for SingleInit, Initializer (ptr only) |
---|
25 | #include "SynTree/Statement.h" // for CompoundStmt, Statement |
---|
26 | #include "SynTree/Type.h" // for BasicType, Type, Type::ForallList |
---|
27 | #include "TypeData.h" |
---|
28 | |
---|
29 | class Attribute; |
---|
30 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | TypeData::TypeData( Kind k ) : location( yylloc ), kind( k ), base( nullptr ), forall( nullptr ) /*, PTR1( (void*)(0xdeadbeefdeadbeef)), PTR2( (void*)(0xdeadbeefdeadbeef) ) */ { |
---|
34 | switch ( kind ) { |
---|
35 | case Unknown: |
---|
36 | case Pointer: |
---|
37 | case Reference: |
---|
38 | case EnumConstant: |
---|
39 | case GlobalScope: |
---|
40 | case Basic: |
---|
41 | // No unique data to initialize. |
---|
42 | break; |
---|
43 | case Array: |
---|
44 | array.dimension = nullptr; |
---|
45 | array.isVarLen = false; |
---|
46 | array.isStatic = false; |
---|
47 | break; |
---|
48 | case Function: |
---|
49 | function.params = nullptr; |
---|
50 | function.idList = nullptr; |
---|
51 | function.oldDeclList = nullptr; |
---|
52 | function.body = nullptr; |
---|
53 | function.withExprs = nullptr; |
---|
54 | break; |
---|
55 | case Enum: |
---|
56 | enumeration.name = nullptr; |
---|
57 | enumeration.constants = nullptr; |
---|
58 | enumeration.body = false; |
---|
59 | enumeration.anon = false; |
---|
60 | break; |
---|
61 | case Aggregate: |
---|
62 | aggregate.kind = AggregateDecl::NoAggregate; |
---|
63 | aggregate.name = nullptr; |
---|
64 | aggregate.params = nullptr; |
---|
65 | aggregate.actuals = nullptr; |
---|
66 | aggregate.fields = nullptr; |
---|
67 | aggregate.body = false; |
---|
68 | aggregate.tagged = false; |
---|
69 | aggregate.parent = nullptr; |
---|
70 | aggregate.anon = false; |
---|
71 | break; |
---|
72 | case AggregateInst: |
---|
73 | aggInst.aggregate = nullptr; |
---|
74 | aggInst.params = nullptr; |
---|
75 | aggInst.hoistType = false; |
---|
76 | break; |
---|
77 | case Symbolic: |
---|
78 | case SymbolicInst: |
---|
79 | symbolic.name = nullptr; |
---|
80 | symbolic.params = nullptr; |
---|
81 | symbolic.actuals = nullptr; |
---|
82 | symbolic.assertions = nullptr; |
---|
83 | break; |
---|
84 | case Tuple: |
---|
85 | tuple = nullptr; |
---|
86 | break; |
---|
87 | case Typeof: |
---|
88 | case Basetypeof: |
---|
89 | typeexpr = nullptr; |
---|
90 | break; |
---|
91 | case Builtin: |
---|
92 | case Vtable: |
---|
93 | // No unique data to initialize. |
---|
94 | break; |
---|
95 | case Qualified: |
---|
96 | qualified.parent = nullptr; |
---|
97 | qualified.child = nullptr; |
---|
98 | break; |
---|
99 | } // switch |
---|
100 | } // TypeData::TypeData |
---|
101 | |
---|
102 | |
---|
103 | TypeData::~TypeData() { |
---|
104 | delete base; |
---|
105 | delete forall; |
---|
106 | |
---|
107 | switch ( kind ) { |
---|
108 | case Unknown: |
---|
109 | case Pointer: |
---|
110 | case Reference: |
---|
111 | case EnumConstant: |
---|
112 | case GlobalScope: |
---|
113 | // No unique data to deconstruct. |
---|
114 | break; |
---|
115 | case Basic: |
---|
116 | break; |
---|
117 | case Array: |
---|
118 | delete array.dimension; |
---|
119 | break; |
---|
120 | case Function: |
---|
121 | delete function.params; |
---|
122 | delete function.idList; |
---|
123 | delete function.oldDeclList; |
---|
124 | delete function.body; |
---|
125 | delete function.withExprs; |
---|
126 | break; |
---|
127 | case Aggregate: |
---|
128 | delete aggregate.name; |
---|
129 | delete aggregate.params; |
---|
130 | delete aggregate.actuals; |
---|
131 | delete aggregate.fields; |
---|
132 | break; |
---|
133 | case AggregateInst: |
---|
134 | delete aggInst.aggregate; |
---|
135 | delete aggInst.params; |
---|
136 | break; |
---|
137 | case Enum: |
---|
138 | delete enumeration.name; |
---|
139 | delete enumeration.constants; |
---|
140 | break; |
---|
141 | case Symbolic: |
---|
142 | case SymbolicInst: |
---|
143 | delete symbolic.name; |
---|
144 | delete symbolic.params; |
---|
145 | delete symbolic.actuals; |
---|
146 | delete symbolic.assertions; |
---|
147 | break; |
---|
148 | case Tuple: |
---|
149 | delete tuple; |
---|
150 | break; |
---|
151 | case Typeof: |
---|
152 | case Basetypeof: |
---|
153 | delete typeexpr; |
---|
154 | break; |
---|
155 | case Vtable: |
---|
156 | case Builtin: |
---|
157 | // No unique data to deconstruct. |
---|
158 | break; |
---|
159 | case Qualified: |
---|
160 | delete qualified.parent; |
---|
161 | delete qualified.child; |
---|
162 | break; |
---|
163 | } // switch |
---|
164 | } // TypeData::~TypeData |
---|
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 | case Reference: |
---|
178 | case GlobalScope: |
---|
179 | // nothing else to copy |
---|
180 | break; |
---|
181 | case Basic: |
---|
182 | newtype->basictype = basictype; |
---|
183 | newtype->complextype = complextype; |
---|
184 | newtype->signedness = signedness; |
---|
185 | newtype->length = length; |
---|
186 | break; |
---|
187 | case Array: |
---|
188 | newtype->array.dimension = maybeClone( array.dimension ); |
---|
189 | newtype->array.isVarLen = array.isVarLen; |
---|
190 | newtype->array.isStatic = array.isStatic; |
---|
191 | break; |
---|
192 | case Function: |
---|
193 | newtype->function.params = maybeClone( function.params ); |
---|
194 | newtype->function.idList = maybeClone( function.idList ); |
---|
195 | newtype->function.oldDeclList = maybeClone( function.oldDeclList ); |
---|
196 | newtype->function.body = maybeClone( function.body ); |
---|
197 | newtype->function.withExprs = maybeClone( function.withExprs ); |
---|
198 | break; |
---|
199 | case Aggregate: |
---|
200 | newtype->aggregate.kind = aggregate.kind; |
---|
201 | newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr; |
---|
202 | newtype->aggregate.params = maybeClone( aggregate.params ); |
---|
203 | newtype->aggregate.actuals = maybeClone( aggregate.actuals ); |
---|
204 | newtype->aggregate.fields = maybeClone( aggregate.fields ); |
---|
205 | newtype->aggregate.body = aggregate.body; |
---|
206 | newtype->aggregate.anon = aggregate.anon; |
---|
207 | newtype->aggregate.tagged = aggregate.tagged; |
---|
208 | newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr; |
---|
209 | break; |
---|
210 | case AggregateInst: |
---|
211 | newtype->aggInst.aggregate = maybeClone( aggInst.aggregate ); |
---|
212 | newtype->aggInst.params = maybeClone( aggInst.params ); |
---|
213 | newtype->aggInst.hoistType = aggInst.hoistType; |
---|
214 | break; |
---|
215 | case Enum: |
---|
216 | newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr; |
---|
217 | newtype->enumeration.constants = maybeClone( enumeration.constants ); |
---|
218 | newtype->enumeration.body = enumeration.body; |
---|
219 | newtype->enumeration.anon = enumeration.anon; |
---|
220 | break; |
---|
221 | case Symbolic: |
---|
222 | case SymbolicInst: |
---|
223 | newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr; |
---|
224 | newtype->symbolic.params = maybeClone( symbolic.params ); |
---|
225 | newtype->symbolic.actuals = maybeClone( symbolic.actuals ); |
---|
226 | newtype->symbolic.assertions = maybeClone( symbolic.assertions ); |
---|
227 | newtype->symbolic.isTypedef = symbolic.isTypedef; |
---|
228 | break; |
---|
229 | case Tuple: |
---|
230 | newtype->tuple = maybeClone( tuple ); |
---|
231 | break; |
---|
232 | case Typeof: |
---|
233 | case Basetypeof: |
---|
234 | newtype->typeexpr = maybeClone( typeexpr ); |
---|
235 | break; |
---|
236 | case Vtable: |
---|
237 | break; |
---|
238 | case Builtin: |
---|
239 | assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One ); |
---|
240 | newtype->builtintype = builtintype; |
---|
241 | break; |
---|
242 | case Qualified: |
---|
243 | newtype->qualified.parent = maybeClone( qualified.parent ); |
---|
244 | newtype->qualified.child = maybeClone( qualified.child ); |
---|
245 | break; |
---|
246 | } // switch |
---|
247 | return newtype; |
---|
248 | } // TypeData::clone |
---|
249 | |
---|
250 | |
---|
251 | void TypeData::print( ostream &os, int indent ) const { |
---|
252 | for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) { |
---|
253 | if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' '; |
---|
254 | } // for |
---|
255 | |
---|
256 | if ( forall ) { |
---|
257 | os << "forall " << endl; |
---|
258 | forall->printList( os, indent + 4 ); |
---|
259 | } // if |
---|
260 | |
---|
261 | switch ( kind ) { |
---|
262 | case Basic: |
---|
263 | if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " "; |
---|
264 | if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " "; |
---|
265 | if ( complextype != DeclarationNode::NoComplexType ) os << DeclarationNode::complexTypeNames[ complextype ] << " "; |
---|
266 | if ( basictype != DeclarationNode::NoBasicType ) os << DeclarationNode::basicTypeNames[ basictype ] << " "; |
---|
267 | break; |
---|
268 | case Pointer: |
---|
269 | os << "pointer "; |
---|
270 | if ( base ) { |
---|
271 | os << "to "; |
---|
272 | base->print( os, indent ); |
---|
273 | } // if |
---|
274 | break; |
---|
275 | case Reference: |
---|
276 | os << "reference "; |
---|
277 | if ( base ) { |
---|
278 | os << "to "; |
---|
279 | base->print( os, indent ); |
---|
280 | } // if |
---|
281 | break; |
---|
282 | case Array: |
---|
283 | if ( array.isStatic ) { |
---|
284 | os << "static "; |
---|
285 | } // if |
---|
286 | if ( array.dimension ) { |
---|
287 | os << "array of "; |
---|
288 | array.dimension->printOneLine( os, indent ); |
---|
289 | } else if ( array.isVarLen ) { |
---|
290 | os << "variable-length array of "; |
---|
291 | } else { |
---|
292 | os << "open array of "; |
---|
293 | } // if |
---|
294 | if ( base ) { |
---|
295 | base->print( os, indent ); |
---|
296 | } // if |
---|
297 | break; |
---|
298 | case Function: |
---|
299 | os << "function" << endl; |
---|
300 | if ( function.params ) { |
---|
301 | os << string( indent + 2, ' ' ) << "with parameters " << endl; |
---|
302 | function.params->printList( os, indent + 4 ); |
---|
303 | } else { |
---|
304 | os << string( indent + 2, ' ' ) << "with no parameters" << endl; |
---|
305 | } // if |
---|
306 | if ( function.idList ) { |
---|
307 | os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl; |
---|
308 | function.idList->printList( os, indent + 4 ); |
---|
309 | } // if |
---|
310 | if ( function.oldDeclList ) { |
---|
311 | os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl; |
---|
312 | function.oldDeclList->printList( os, indent + 4 ); |
---|
313 | } // if |
---|
314 | os << string( indent + 2, ' ' ) << "returning "; |
---|
315 | if ( base ) { |
---|
316 | base->print( os, indent + 4 ); |
---|
317 | } else { |
---|
318 | os << "nothing "; |
---|
319 | } // if |
---|
320 | os << endl; |
---|
321 | if ( function.body ) { |
---|
322 | os << string( indent + 2, ' ' ) << "with body " << endl; |
---|
323 | function.body->printList( os, indent + 2 ); |
---|
324 | } // if |
---|
325 | break; |
---|
326 | case Aggregate: |
---|
327 | os << AggregateDecl::aggrString( aggregate.kind ) << ' ' << *aggregate.name << endl; |
---|
328 | if ( aggregate.params ) { |
---|
329 | os << string( indent + 2, ' ' ) << "with type parameters" << endl; |
---|
330 | aggregate.params->printList( os, indent + 4 ); |
---|
331 | } // if |
---|
332 | if ( aggregate.actuals ) { |
---|
333 | os << string( indent + 2, ' ' ) << "instantiated with actual parameters" << endl; |
---|
334 | aggregate.actuals->printList( os, indent + 4 ); |
---|
335 | } // if |
---|
336 | if ( aggregate.fields ) { |
---|
337 | os << string( indent + 2, ' ' ) << "with members" << endl; |
---|
338 | aggregate.fields->printList( os, indent + 4 ); |
---|
339 | } // if |
---|
340 | if ( aggregate.body ) { |
---|
341 | os << string( indent + 2, ' ' ) << " with body" << endl; |
---|
342 | } // if |
---|
343 | break; |
---|
344 | case AggregateInst: |
---|
345 | if ( aggInst.aggregate ) { |
---|
346 | os << "instance of " ; |
---|
347 | aggInst.aggregate->print( os, indent ); |
---|
348 | } else { |
---|
349 | os << "instance of an unspecified aggregate "; |
---|
350 | } // if |
---|
351 | if ( aggInst.params ) { |
---|
352 | os << string( indent + 2, ' ' ) << "with parameters" << endl; |
---|
353 | aggInst.params->printList( os, indent + 2 ); |
---|
354 | } // if |
---|
355 | break; |
---|
356 | case Enum: |
---|
357 | os << "enumeration " << *enumeration.name << endl;; |
---|
358 | if ( enumeration.constants ) { |
---|
359 | os << "with constants" << endl; |
---|
360 | enumeration.constants->printList( os, indent + 2 ); |
---|
361 | } // if |
---|
362 | if ( enumeration.body ) { |
---|
363 | os << string( indent + 2, ' ' ) << " with body" << endl; |
---|
364 | } // if |
---|
365 | if ( base ) { |
---|
366 | os << "for "; |
---|
367 | base->print( os, indent + 2 ); |
---|
368 | } // if |
---|
369 | break; |
---|
370 | case EnumConstant: |
---|
371 | os << "enumeration constant "; |
---|
372 | break; |
---|
373 | case Symbolic: |
---|
374 | if ( symbolic.isTypedef ) { |
---|
375 | os << "typedef definition "; |
---|
376 | } else { |
---|
377 | os << "type definition "; |
---|
378 | } // if |
---|
379 | if ( symbolic.params ) { |
---|
380 | os << endl << string( indent + 2, ' ' ) << "with parameters" << endl; |
---|
381 | symbolic.params->printList( os, indent + 2 ); |
---|
382 | } // if |
---|
383 | if ( symbolic.assertions ) { |
---|
384 | os << endl << string( indent + 2, ' ' ) << "with assertions" << endl; |
---|
385 | symbolic.assertions->printList( os, indent + 4 ); |
---|
386 | os << string( indent + 2, ' ' ); |
---|
387 | } // if |
---|
388 | if ( base ) { |
---|
389 | os << "for "; |
---|
390 | base->print( os, indent + 2 ); |
---|
391 | } // if |
---|
392 | break; |
---|
393 | case SymbolicInst: |
---|
394 | os << *symbolic.name; |
---|
395 | if ( symbolic.actuals ) { |
---|
396 | os << "("; |
---|
397 | symbolic.actuals->printList( os, indent + 2 ); |
---|
398 | os << ")"; |
---|
399 | } // if |
---|
400 | break; |
---|
401 | case Tuple: |
---|
402 | os << "tuple "; |
---|
403 | if ( tuple ) { |
---|
404 | os << "with members" << endl; |
---|
405 | tuple->printList( os, indent + 2 ); |
---|
406 | } // if |
---|
407 | break; |
---|
408 | case Basetypeof: |
---|
409 | os << "base-"; |
---|
410 | #if defined(__GNUC__) && __GNUC__ >= 7 |
---|
411 | __attribute__((fallthrough)); |
---|
412 | #endif |
---|
413 | // FALL THROUGH |
---|
414 | case Typeof: |
---|
415 | os << "type-of expression "; |
---|
416 | if ( typeexpr ) { |
---|
417 | typeexpr->print( os, indent + 2 ); |
---|
418 | } // if |
---|
419 | break; |
---|
420 | case Vtable: |
---|
421 | os << "vtable"; |
---|
422 | break; |
---|
423 | case Builtin: |
---|
424 | os << DeclarationNode::builtinTypeNames[builtintype]; |
---|
425 | break; |
---|
426 | case GlobalScope: |
---|
427 | break; |
---|
428 | case Qualified: |
---|
429 | qualified.parent->print( os ); |
---|
430 | os << "."; |
---|
431 | qualified.child->print( os ); |
---|
432 | break; |
---|
433 | case Unknown: |
---|
434 | os << "entity of unknown type "; |
---|
435 | break; |
---|
436 | default: |
---|
437 | os << "internal error: TypeData::print " << kind << endl; |
---|
438 | assert( false ); |
---|
439 | } // switch |
---|
440 | } // TypeData::print |
---|
441 | |
---|
442 | const std::string * TypeData::leafName() const { |
---|
443 | switch ( kind ) { |
---|
444 | case Unknown: |
---|
445 | case Pointer: |
---|
446 | case Reference: |
---|
447 | case EnumConstant: |
---|
448 | case GlobalScope: |
---|
449 | case Array: |
---|
450 | case Basic: |
---|
451 | case Function: |
---|
452 | case AggregateInst: |
---|
453 | case Tuple: |
---|
454 | case Typeof: |
---|
455 | case Basetypeof: |
---|
456 | case Builtin: |
---|
457 | case Vtable: |
---|
458 | assertf(false, "Tried to get leaf name from kind without a name: %d", kind); |
---|
459 | break; |
---|
460 | case Aggregate: |
---|
461 | return aggregate.name; |
---|
462 | case Enum: |
---|
463 | return enumeration.name; |
---|
464 | case Symbolic: |
---|
465 | case SymbolicInst: |
---|
466 | return symbolic.name; |
---|
467 | case Qualified: |
---|
468 | return qualified.child->leafName(); |
---|
469 | } // switch |
---|
470 | assert(false); |
---|
471 | } |
---|
472 | |
---|
473 | |
---|
474 | template< typename ForallList > |
---|
475 | void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) { |
---|
476 | buildList( firstNode, outputList ); |
---|
477 | auto n = firstNode; |
---|
478 | for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i, n = (DeclarationNode*)n->get_next() ) { |
---|
479 | TypeDecl * td = static_cast<TypeDecl *>(*i); |
---|
480 | if ( n->variable.tyClass == TypeDecl::Otype ) { |
---|
481 | // add assertion parameters to `type' tyvars in reverse order |
---|
482 | // add dtor: void ^?{}(T *) |
---|
483 | FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false ); |
---|
484 | dtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) ); |
---|
485 | td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) ); |
---|
486 | |
---|
487 | // add copy ctor: void ?{}(T *, T) |
---|
488 | FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false ); |
---|
489 | copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) ); |
---|
490 | copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) ); |
---|
491 | td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) ); |
---|
492 | |
---|
493 | // add default ctor: void ?{}(T *) |
---|
494 | FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false ); |
---|
495 | ctorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) ); |
---|
496 | td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) ); |
---|
497 | |
---|
498 | // add assignment operator: T * ?=?(T *, T) |
---|
499 | FunctionType * assignType = new FunctionType( Type::Qualifiers(), false ); |
---|
500 | assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) ); |
---|
501 | assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) ); |
---|
502 | assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) ); |
---|
503 | td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) ); |
---|
504 | } // if |
---|
505 | } // for |
---|
506 | } // buildForall |
---|
507 | |
---|
508 | |
---|
509 | Type * typebuild( const TypeData * td ) { |
---|
510 | assert( td ); |
---|
511 | switch ( td->kind ) { |
---|
512 | case TypeData::Unknown: |
---|
513 | // fill in implicit int |
---|
514 | return new BasicType( buildQualifiers( td ), BasicType::SignedInt ); |
---|
515 | case TypeData::Basic: |
---|
516 | return buildBasicType( td ); |
---|
517 | case TypeData::Pointer: |
---|
518 | return buildPointer( td ); |
---|
519 | case TypeData::Array: |
---|
520 | return buildArray( td ); |
---|
521 | case TypeData::Reference: |
---|
522 | return buildReference( td ); |
---|
523 | case TypeData::Function: |
---|
524 | return buildFunction( td ); |
---|
525 | case TypeData::AggregateInst: |
---|
526 | return buildAggInst( td ); |
---|
527 | case TypeData::EnumConstant: |
---|
528 | return new EnumInstType( buildQualifiers( td ), "" ); |
---|
529 | case TypeData::SymbolicInst: |
---|
530 | return buildSymbolicInst( td ); |
---|
531 | case TypeData::Tuple: |
---|
532 | return buildTuple( td ); |
---|
533 | case TypeData::Typeof: |
---|
534 | case TypeData::Basetypeof: |
---|
535 | return buildTypeof( td ); |
---|
536 | case TypeData::Vtable: |
---|
537 | return buildVtable( td ); |
---|
538 | case TypeData::Builtin: |
---|
539 | switch ( td->builtintype ) { |
---|
540 | case DeclarationNode::Zero: |
---|
541 | return new ZeroType( noQualifiers ); |
---|
542 | case DeclarationNode::One: |
---|
543 | return new OneType( noQualifiers ); |
---|
544 | default: |
---|
545 | return new VarArgsType( buildQualifiers( td ) ); |
---|
546 | } // switch |
---|
547 | case TypeData::GlobalScope: |
---|
548 | return new GlobalScopeType(); |
---|
549 | case TypeData::Qualified: |
---|
550 | return new QualifiedType( buildQualifiers( td ), typebuild( td->qualified.parent ), typebuild( td->qualified.child ) ); |
---|
551 | case TypeData::Symbolic: |
---|
552 | case TypeData::Enum: |
---|
553 | case TypeData::Aggregate: |
---|
554 | assert( false ); |
---|
555 | } // switch |
---|
556 | |
---|
557 | return nullptr; |
---|
558 | } // typebuild |
---|
559 | |
---|
560 | |
---|
561 | TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) { |
---|
562 | TypeData * ret = nullptr; |
---|
563 | |
---|
564 | switch ( td->kind ) { |
---|
565 | case TypeData::Aggregate: |
---|
566 | if ( ! toplevel && td->aggregate.body ) { |
---|
567 | ret = td->clone(); |
---|
568 | } // if |
---|
569 | break; |
---|
570 | case TypeData::Enum: |
---|
571 | if ( ! toplevel && td->enumeration.body ) { |
---|
572 | ret = td->clone(); |
---|
573 | } // if |
---|
574 | break; |
---|
575 | case TypeData::AggregateInst: |
---|
576 | if ( td->aggInst.aggregate ) { |
---|
577 | ret = typeextractAggregate( td->aggInst.aggregate, false ); |
---|
578 | } // if |
---|
579 | break; |
---|
580 | default: |
---|
581 | if ( td->base ) { |
---|
582 | ret = typeextractAggregate( td->base, false ); |
---|
583 | } // if |
---|
584 | } // switch |
---|
585 | return ret; |
---|
586 | } // typeextractAggregate |
---|
587 | |
---|
588 | |
---|
589 | Type::Qualifiers buildQualifiers( const TypeData * td ) { |
---|
590 | return td->qualifiers; |
---|
591 | } // buildQualifiers |
---|
592 | |
---|
593 | |
---|
594 | static string genTSError( string msg, DeclarationNode::BasicType basictype ) { |
---|
595 | SemanticError( yylloc, string( "invalid type specifier \"" ) + msg + "\" for type \"" + DeclarationNode::basicTypeNames[basictype] + "\"." ); |
---|
596 | } // genTSError |
---|
597 | |
---|
598 | Type * buildBasicType( const TypeData * td ) { |
---|
599 | BasicType::Kind ret; |
---|
600 | |
---|
601 | switch ( td->basictype ) { |
---|
602 | case DeclarationNode::Void: |
---|
603 | if ( td->signedness != DeclarationNode::NoSignedness ) { |
---|
604 | genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype ); |
---|
605 | } // if |
---|
606 | if ( td->length != DeclarationNode::NoLength ) { |
---|
607 | genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype ); |
---|
608 | } // if |
---|
609 | return new VoidType( buildQualifiers( td ) ); |
---|
610 | break; |
---|
611 | |
---|
612 | case DeclarationNode::Bool: |
---|
613 | if ( td->signedness != DeclarationNode::NoSignedness ) { |
---|
614 | genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype ); |
---|
615 | } // if |
---|
616 | if ( td->length != DeclarationNode::NoLength ) { |
---|
617 | genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype ); |
---|
618 | } // if |
---|
619 | |
---|
620 | ret = BasicType::Bool; |
---|
621 | break; |
---|
622 | |
---|
623 | case DeclarationNode::Char: |
---|
624 | // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the |
---|
625 | // character types. The implementation shall define char to have the same range, representation, and behavior as |
---|
626 | // either signed char or unsigned char. |
---|
627 | static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char }; |
---|
628 | |
---|
629 | if ( td->length != DeclarationNode::NoLength ) { |
---|
630 | genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype ); |
---|
631 | } // if |
---|
632 | |
---|
633 | ret = chartype[ td->signedness ]; |
---|
634 | break; |
---|
635 | |
---|
636 | case DeclarationNode::Int: |
---|
637 | static BasicType::Kind inttype[2][4] = { |
---|
638 | { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt }, |
---|
639 | { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt }, |
---|
640 | }; |
---|
641 | |
---|
642 | Integral: ; |
---|
643 | if ( td->signedness == DeclarationNode::NoSignedness ) { |
---|
644 | const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed; |
---|
645 | } // if |
---|
646 | ret = inttype[ td->signedness ][ td->length ]; |
---|
647 | break; |
---|
648 | |
---|
649 | case DeclarationNode::Int128: |
---|
650 | ret = td->signedness == DeclarationNode::Unsigned ? BasicType::UnsignedInt128 : BasicType::SignedInt128; |
---|
651 | if ( td->length != DeclarationNode::NoLength ) { |
---|
652 | genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype ); |
---|
653 | } // if |
---|
654 | break; |
---|
655 | |
---|
656 | case DeclarationNode::Float: |
---|
657 | case DeclarationNode::Double: |
---|
658 | case DeclarationNode::LongDouble: // not set until below |
---|
659 | case DeclarationNode::uuFloat80: |
---|
660 | case DeclarationNode::uuFloat128: |
---|
661 | case DeclarationNode::uFloat16: |
---|
662 | case DeclarationNode::uFloat32: |
---|
663 | case DeclarationNode::uFloat32x: |
---|
664 | case DeclarationNode::uFloat64: |
---|
665 | case DeclarationNode::uFloat64x: |
---|
666 | case DeclarationNode::uFloat128: |
---|
667 | case DeclarationNode::uFloat128x: |
---|
668 | static BasicType::Kind floattype[2][12] = { |
---|
669 | { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, (BasicType::Kind)-1, (BasicType::Kind)-1, BasicType::uFloat16Complex, BasicType::uFloat32Complex, BasicType::uFloat32xComplex, BasicType::uFloat64Complex, BasicType::uFloat64xComplex, BasicType::uFloat128Complex, BasicType::uFloat128xComplex, }, |
---|
670 | { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x, }, |
---|
671 | }; |
---|
672 | |
---|
673 | FloatingPoint: ; |
---|
674 | if ( td->signedness != DeclarationNode::NoSignedness ) { |
---|
675 | genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype ); |
---|
676 | } // if |
---|
677 | if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) { |
---|
678 | genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype ); |
---|
679 | } // if |
---|
680 | if ( td->basictype != DeclarationNode::Double && td->length == DeclarationNode::Long ) { |
---|
681 | genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype ); |
---|
682 | } // if |
---|
683 | if ( td->complextype == DeclarationNode::Imaginary ) { |
---|
684 | genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype ); |
---|
685 | } // if |
---|
686 | if ( (td->basictype == DeclarationNode::uuFloat80 || td->basictype == DeclarationNode::uuFloat128) && td->complextype == DeclarationNode::Complex ) { // gcc unsupported |
---|
687 | genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype ); |
---|
688 | } // if |
---|
689 | if ( td->length == DeclarationNode::Long ) { |
---|
690 | const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble; |
---|
691 | } // if |
---|
692 | |
---|
693 | ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ]; |
---|
694 | //printf( "XXXX %d %d %d %d\n", td->complextype, td->basictype, DeclarationNode::Float, ret ); |
---|
695 | break; |
---|
696 | |
---|
697 | case DeclarationNode::NoBasicType: |
---|
698 | // No basic type in declaration => default double for Complex/Imaginary and int type for integral types |
---|
699 | if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) { |
---|
700 | const_cast<TypeData *>(td)->basictype = DeclarationNode::Double; |
---|
701 | goto FloatingPoint; |
---|
702 | } // if |
---|
703 | |
---|
704 | const_cast<TypeData *>(td)->basictype = DeclarationNode::Int; |
---|
705 | goto Integral; |
---|
706 | default: |
---|
707 | assertf( false, "unknown basic type" ); |
---|
708 | return nullptr; |
---|
709 | } // switch |
---|
710 | |
---|
711 | BasicType * bt = new BasicType( buildQualifiers( td ), ret ); |
---|
712 | buildForall( td->forall, bt->get_forall() ); |
---|
713 | return bt; |
---|
714 | } // buildBasicType |
---|
715 | |
---|
716 | |
---|
717 | PointerType * buildPointer( const TypeData * td ) { |
---|
718 | PointerType * pt; |
---|
719 | if ( td->base ) { |
---|
720 | pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) ); |
---|
721 | } else { |
---|
722 | pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
723 | } // if |
---|
724 | buildForall( td->forall, pt->get_forall() ); |
---|
725 | return pt; |
---|
726 | } // buildPointer |
---|
727 | |
---|
728 | |
---|
729 | ArrayType * buildArray( const TypeData * td ) { |
---|
730 | ArrayType * at; |
---|
731 | if ( td->base ) { |
---|
732 | at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild( td->array.dimension ), |
---|
733 | td->array.isVarLen, td->array.isStatic ); |
---|
734 | } else { |
---|
735 | at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ), |
---|
736 | maybeBuild( td->array.dimension ), td->array.isVarLen, td->array.isStatic ); |
---|
737 | } // if |
---|
738 | buildForall( td->forall, at->get_forall() ); |
---|
739 | return at; |
---|
740 | } // buildArray |
---|
741 | |
---|
742 | |
---|
743 | ReferenceType * buildReference( const TypeData * td ) { |
---|
744 | ReferenceType * rt; |
---|
745 | if ( td->base ) { |
---|
746 | rt = new ReferenceType( buildQualifiers( td ), typebuild( td->base ) ); |
---|
747 | } else { |
---|
748 | rt = new ReferenceType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
749 | } // if |
---|
750 | buildForall( td->forall, rt->get_forall() ); |
---|
751 | return rt; |
---|
752 | } // buildReference |
---|
753 | |
---|
754 | |
---|
755 | AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) { |
---|
756 | assert( td->kind == TypeData::Aggregate ); |
---|
757 | AggregateDecl * at; |
---|
758 | switch ( td->aggregate.kind ) { |
---|
759 | case AggregateDecl::Struct: |
---|
760 | case AggregateDecl::Coroutine: |
---|
761 | case AggregateDecl::Exception: |
---|
762 | case AggregateDecl::Generator: |
---|
763 | case AggregateDecl::Monitor: |
---|
764 | case AggregateDecl::Thread: |
---|
765 | at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage ); |
---|
766 | buildForall( td->aggregate.params, at->get_parameters() ); |
---|
767 | break; |
---|
768 | case AggregateDecl::Union: |
---|
769 | at = new UnionDecl( *td->aggregate.name, attributes, linkage ); |
---|
770 | buildForall( td->aggregate.params, at->get_parameters() ); |
---|
771 | break; |
---|
772 | case AggregateDecl::Trait: |
---|
773 | at = new TraitDecl( *td->aggregate.name, attributes, linkage ); |
---|
774 | buildList( td->aggregate.params, at->get_parameters() ); |
---|
775 | break; |
---|
776 | default: |
---|
777 | assert( false ); |
---|
778 | } // switch |
---|
779 | |
---|
780 | buildList( td->aggregate.fields, at->get_members() ); |
---|
781 | at->set_body( td->aggregate.body ); |
---|
782 | |
---|
783 | return at; |
---|
784 | } // buildAggregate |
---|
785 | |
---|
786 | |
---|
787 | ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) { |
---|
788 | switch ( type->kind ) { |
---|
789 | case TypeData::Enum: |
---|
790 | if ( type->enumeration.body ) { |
---|
791 | EnumDecl * typedecl = buildEnum( type, attributes, linkage ); |
---|
792 | return new EnumInstType( buildQualifiers( type ), typedecl ); |
---|
793 | } else { |
---|
794 | return new EnumInstType( buildQualifiers( type ), *type->enumeration.name ); |
---|
795 | } // if |
---|
796 | case TypeData::Aggregate: |
---|
797 | if ( type->aggregate.body ) { |
---|
798 | AggregateDecl * typedecl = buildAggregate( type, attributes, linkage ); |
---|
799 | switch ( type->aggregate.kind ) { |
---|
800 | case AggregateDecl::Struct: |
---|
801 | case AggregateDecl::Coroutine: |
---|
802 | case AggregateDecl::Monitor: |
---|
803 | case AggregateDecl::Thread: |
---|
804 | return new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl ); |
---|
805 | case AggregateDecl::Union: |
---|
806 | return new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl ); |
---|
807 | case AggregateDecl::Trait: |
---|
808 | assert( false ); |
---|
809 | //return new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl ); |
---|
810 | break; |
---|
811 | default: |
---|
812 | assert( false ); |
---|
813 | } // switch |
---|
814 | } else { |
---|
815 | switch ( type->aggregate.kind ) { |
---|
816 | case AggregateDecl::Struct: |
---|
817 | case AggregateDecl::Coroutine: |
---|
818 | case AggregateDecl::Monitor: |
---|
819 | case AggregateDecl::Thread: |
---|
820 | return new StructInstType( buildQualifiers( type ), *type->aggregate.name ); |
---|
821 | case AggregateDecl::Union: |
---|
822 | return new UnionInstType( buildQualifiers( type ), *type->aggregate.name ); |
---|
823 | case AggregateDecl::Trait: |
---|
824 | return new TraitInstType( buildQualifiers( type ), *type->aggregate.name ); |
---|
825 | default: |
---|
826 | assert( false ); |
---|
827 | } // switch |
---|
828 | } // if |
---|
829 | return nullptr; |
---|
830 | default: |
---|
831 | assert( false ); |
---|
832 | } // switch |
---|
833 | } // buildAggInst |
---|
834 | |
---|
835 | |
---|
836 | ReferenceToType * buildAggInst( const TypeData * td ) { |
---|
837 | assert( td->kind == TypeData::AggregateInst ); |
---|
838 | |
---|
839 | // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() ); |
---|
840 | ReferenceToType * ret = nullptr; |
---|
841 | TypeData * type = td->aggInst.aggregate; |
---|
842 | switch ( type->kind ) { |
---|
843 | case TypeData::Enum: |
---|
844 | return new EnumInstType( buildQualifiers( type ), *type->enumeration.name ); |
---|
845 | case TypeData::Aggregate: |
---|
846 | switch ( type->aggregate.kind ) { |
---|
847 | case AggregateDecl::Struct: |
---|
848 | case AggregateDecl::Coroutine: |
---|
849 | case AggregateDecl::Monitor: |
---|
850 | case AggregateDecl::Thread: |
---|
851 | ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name ); |
---|
852 | break; |
---|
853 | case AggregateDecl::Union: |
---|
854 | ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name ); |
---|
855 | break; |
---|
856 | case AggregateDecl::Trait: |
---|
857 | ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name ); |
---|
858 | break; |
---|
859 | default: |
---|
860 | assert( false ); |
---|
861 | } // switch |
---|
862 | break; |
---|
863 | default: |
---|
864 | assert( false ); |
---|
865 | } // switch |
---|
866 | |
---|
867 | ret->set_hoistType( td->aggInst.hoistType ); |
---|
868 | buildList( td->aggInst.params, ret->get_parameters() ); |
---|
869 | buildForall( td->forall, ret->get_forall() ); |
---|
870 | return ret; |
---|
871 | } // buildAggInst |
---|
872 | |
---|
873 | |
---|
874 | NamedTypeDecl * buildSymbolic( const TypeData * td, std::list< Attribute * > attributes, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) { |
---|
875 | assert( td->kind == TypeData::Symbolic ); |
---|
876 | NamedTypeDecl * ret; |
---|
877 | assert( td->base ); |
---|
878 | if ( td->symbolic.isTypedef ) { |
---|
879 | ret = new TypedefDecl( name, td->location, scs, typebuild( td->base ), linkage ); |
---|
880 | } else { |
---|
881 | ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Dtype, true ); |
---|
882 | } // if |
---|
883 | buildList( td->symbolic.assertions, ret->get_assertions() ); |
---|
884 | ret->base->attributes.splice( ret->base->attributes.end(), attributes ); |
---|
885 | return ret; |
---|
886 | } // buildSymbolic |
---|
887 | |
---|
888 | |
---|
889 | EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) { |
---|
890 | assert( td->kind == TypeData::Enum ); |
---|
891 | Type * baseType = td->base ? typebuild(td->base) : nullptr; |
---|
892 | EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, td->enumeration.typed, linkage, baseType ); |
---|
893 | buildList( td->enumeration.constants, ret->get_members() ); |
---|
894 | list< Declaration * >::iterator members = ret->get_members().begin(); |
---|
895 | ret->hide = td->enumeration.hiding == EnumHiding::Hide ? EnumDecl::EnumHiding::Hide : EnumDecl::EnumHiding::Visible; |
---|
896 | for ( const DeclarationNode * cur = td->enumeration.constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) { |
---|
897 | if ( cur->enumInLine ) { |
---|
898 | // Do Nothing |
---|
899 | } else if ( ret->isTyped && !ret->base && cur->has_enumeratorValue() ) { |
---|
900 | SemanticError( td->location, "Enumerator of enum(void) cannot have an explicit initializer value." ); |
---|
901 | } else if ( cur->has_enumeratorValue() ) { |
---|
902 | ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members); |
---|
903 | member->set_init( new SingleInit( maybeMoveBuild( cur->consume_enumeratorValue() ) ) ); |
---|
904 | } else if ( !cur->initializer ) { |
---|
905 | if ( baseType && (!dynamic_cast<BasicType *>(baseType) || !dynamic_cast<BasicType *>(baseType)->isInteger())) { |
---|
906 | SemanticError( td->location, "Enumerators of an non-integer typed enum must be explicitly initialized." ); |
---|
907 | } |
---|
908 | } |
---|
909 | // else cur is a List Initializer and has been set as init in buildList() |
---|
910 | // if |
---|
911 | } // for |
---|
912 | ret->set_body( td->enumeration.body ); |
---|
913 | return ret; |
---|
914 | } // buildEnum |
---|
915 | |
---|
916 | |
---|
917 | TypeInstType * buildSymbolicInst( const TypeData * td ) { |
---|
918 | assert( td->kind == TypeData::SymbolicInst ); |
---|
919 | TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false ); |
---|
920 | buildList( td->symbolic.actuals, ret->get_parameters() ); |
---|
921 | buildForall( td->forall, ret->get_forall() ); |
---|
922 | return ret; |
---|
923 | } // buildSymbolicInst |
---|
924 | |
---|
925 | |
---|
926 | TupleType * buildTuple( const TypeData * td ) { |
---|
927 | assert( td->kind == TypeData::Tuple ); |
---|
928 | std::list< Type * > types; |
---|
929 | buildTypeList( td->tuple, types ); |
---|
930 | TupleType * ret = new TupleType( buildQualifiers( td ), types ); |
---|
931 | buildForall( td->forall, ret->get_forall() ); |
---|
932 | return ret; |
---|
933 | } // buildTuple |
---|
934 | |
---|
935 | |
---|
936 | TypeofType * buildTypeof( const TypeData * td ) { |
---|
937 | assert( td->kind == TypeData::Typeof || td->kind == TypeData::Basetypeof ); |
---|
938 | assert( td->typeexpr ); |
---|
939 | // assert( td->typeexpr->expr ); |
---|
940 | return new TypeofType{ buildQualifiers( td ), td->typeexpr->build(), td->kind == TypeData::Basetypeof }; |
---|
941 | } // buildTypeof |
---|
942 | |
---|
943 | |
---|
944 | VTableType * buildVtable( const TypeData * td ) { |
---|
945 | assert( td->base ); |
---|
946 | return new VTableType{ buildQualifiers( td ), typebuild( td->base ) }; |
---|
947 | } // buildVtable |
---|
948 | |
---|
949 | |
---|
950 | Declaration * buildDecl( const TypeData * td, const string &name, Type::StorageClasses scs, Expression * bitfieldWidth, Type::FuncSpecifiers funcSpec, LinkageSpec::Spec linkage, Expression *asmName, Initializer * init, std::list< Attribute * > attributes ) { |
---|
951 | if ( td->kind == TypeData::Function ) { |
---|
952 | if ( td->function.idList ) { // KR function ? |
---|
953 | buildKRFunction( td->function ); // transform into C11 function |
---|
954 | } // if |
---|
955 | |
---|
956 | FunctionDecl * decl; |
---|
957 | Statement * stmt = maybeBuild( td->function.body ); |
---|
958 | CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt ); |
---|
959 | decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec ); |
---|
960 | buildList( td->function.withExprs, decl->withExprs ); |
---|
961 | return decl->set_asmName( asmName ); |
---|
962 | } else if ( td->kind == TypeData::Aggregate ) { |
---|
963 | return buildAggregate( td, attributes, linkage ); |
---|
964 | } else if ( td->kind == TypeData::Enum ) { |
---|
965 | return buildEnum( td, attributes, linkage ); |
---|
966 | } else if ( td->kind == TypeData::Symbolic ) { |
---|
967 | return buildSymbolic( td, attributes, name, scs, linkage ); |
---|
968 | } else { |
---|
969 | return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName ); |
---|
970 | } // if |
---|
971 | return nullptr; |
---|
972 | } // buildDecl |
---|
973 | |
---|
974 | |
---|
975 | FunctionType * buildFunction( const TypeData * td ) { |
---|
976 | assert( td->kind == TypeData::Function ); |
---|
977 | FunctionType * ft = new FunctionType( buildQualifiers( td ), ! td->function.params || td->function.params->hasEllipsis ); |
---|
978 | buildList( td->function.params, ft->parameters ); |
---|
979 | buildForall( td->forall, ft->forall ); |
---|
980 | if ( td->base ) { |
---|
981 | switch ( td->base->kind ) { |
---|
982 | case TypeData::Tuple: |
---|
983 | buildList( td->base->tuple, ft->returnVals ); |
---|
984 | break; |
---|
985 | default: |
---|
986 | ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) ); |
---|
987 | } // switch |
---|
988 | } else { |
---|
989 | ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) ); |
---|
990 | } // if |
---|
991 | return ft; |
---|
992 | } // buildFunction |
---|
993 | |
---|
994 | |
---|
995 | // Transform KR routine declarations into C99 routine declarations: |
---|
996 | // |
---|
997 | // rtn( a, b, c ) int a, c; double b {} => int rtn( int a, double c, int b ) {} |
---|
998 | // |
---|
999 | // The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration |
---|
1000 | // is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and |
---|
1001 | // extra names are disallowed. |
---|
1002 | // |
---|
1003 | // Note, there is no KR routine-prototype syntax: |
---|
1004 | // |
---|
1005 | // rtn( a, b, c ) int a, c; double b; // invalid KR prototype |
---|
1006 | // rtn(); // valid KR prototype |
---|
1007 | |
---|
1008 | void buildKRFunction( const TypeData::Function_t & function ) { |
---|
1009 | assert( ! function.params ); |
---|
1010 | // loop over declaration first as it is easier to spot errors |
---|
1011 | for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) { |
---|
1012 | // scan ALL parameter names for each declaration name to check for duplicates |
---|
1013 | for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) { |
---|
1014 | if ( *decl->name == *param->name ) { |
---|
1015 | // type set => parameter name already transformed by a declaration names so there is a duplicate |
---|
1016 | // declaration name attempting a second transformation |
---|
1017 | if ( param->type ) SemanticError( param->location, string( "duplicate declaration name " ) + *param->name ); |
---|
1018 | // declaration type reset => declaration already transformed by a parameter name so there is a duplicate |
---|
1019 | // parameter name attempting a second transformation |
---|
1020 | if ( ! decl->type ) SemanticError( param->location, string( "duplicate parameter name " ) + *param->name ); |
---|
1021 | param->type = decl->type; // set copy declaration type to parameter type |
---|
1022 | decl->type = nullptr; // reset declaration type |
---|
1023 | param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter |
---|
1024 | } // if |
---|
1025 | } // for |
---|
1026 | // declaration type still set => type not moved to a matching parameter so there is a missing parameter name |
---|
1027 | if ( decl->type ) SemanticError( decl->location, string( "missing name in parameter list " ) + *decl->name ); |
---|
1028 | } // for |
---|
1029 | |
---|
1030 | // Parameter names without a declaration default to type int: |
---|
1031 | // |
---|
1032 | // rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {} |
---|
1033 | |
---|
1034 | for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) { |
---|
1035 | if ( ! param->type ) { // generate type int for empty parameter type |
---|
1036 | param->type = new TypeData( TypeData::Basic ); |
---|
1037 | param->type->basictype = DeclarationNode::Int; |
---|
1038 | } // if |
---|
1039 | } // for |
---|
1040 | |
---|
1041 | function.params = function.idList; // newly modified idList becomes parameters |
---|
1042 | function.idList = nullptr; // idList now empty |
---|
1043 | delete function.oldDeclList; // deletes entire list |
---|
1044 | function.oldDeclList = nullptr; // reset |
---|
1045 | } // buildKRFunction |
---|
1046 | |
---|
1047 | // Local Variables: // |
---|
1048 | // tab-width: 4 // |
---|
1049 | // mode: c++ // |
---|
1050 | // compile-command: "make install" // |
---|
1051 | // End: // |
---|