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