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