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