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