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 | // DeclarationNode.cc -- |
---|
8 | // |
---|
9 | // Author : Rodolfo G. Esteves |
---|
10 | // Created On : Sat May 16 12:34:05 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Feb 16 14:12:03 2023 |
---|
13 | // Update Count : 1388 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cassert> // for assert, assertf, strict_dynamic_cast |
---|
17 | #include <iterator> // for back_insert_iterator |
---|
18 | #include <list> // for list |
---|
19 | #include <memory> // for unique_ptr |
---|
20 | #include <ostream> // for operator<<, ostream, basic_ostream |
---|
21 | #include <string> // for string, operator+, allocator, char... |
---|
22 | |
---|
23 | #include "Common/SemanticError.h" // for SemanticError |
---|
24 | #include "Common/UniqueName.h" // for UniqueName |
---|
25 | #include "Common/utility.h" // for maybeClone, maybeBuild, CodeLocation |
---|
26 | #include "Parser/ParseNode.h" // for DeclarationNode, ExpressionNode |
---|
27 | #include "SynTree/LinkageSpec.h" // for Spec, linkageName, Cforall |
---|
28 | #include "SynTree/Attribute.h" // for Attribute |
---|
29 | #include "SynTree/Declaration.h" // for TypeDecl, ObjectDecl, InlineMemberDecl, Declaration |
---|
30 | #include "SynTree/Expression.h" // for Expression, ConstantExpr |
---|
31 | #include "SynTree/Statement.h" // for AsmStmt |
---|
32 | #include "SynTree/Type.h" // for Type, Type::StorageClasses, Type::... |
---|
33 | #include "TypeData.h" // for TypeData, TypeData::Aggregate_t |
---|
34 | #include "TypedefTable.h" // for TypedefTable |
---|
35 | |
---|
36 | class Initializer; |
---|
37 | |
---|
38 | extern TypedefTable typedefTable; |
---|
39 | |
---|
40 | using namespace std; |
---|
41 | |
---|
42 | // These must harmonize with the corresponding DeclarationNode enumerations. |
---|
43 | const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "int128", |
---|
44 | "float", "double", "long double", "float80", "float128", |
---|
45 | "_float16", "_float32", "_float32x", "_float64", "_float64x", "_float128", "_float128x", "NoBasicTypeNames" }; |
---|
46 | const char * DeclarationNode::complexTypeNames[] = { "_Complex", "NoComplexTypeNames", "_Imaginary" }; // Imaginary unsupported => parse, but make invisible and print error message |
---|
47 | const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" }; |
---|
48 | const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" }; |
---|
49 | const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "__auto_type", "zero_t", "one_t", "NoBuiltinTypeNames" }; |
---|
50 | |
---|
51 | UniqueName DeclarationNode::anonymous( "__anonymous" ); |
---|
52 | |
---|
53 | extern LinkageSpec::Spec linkage; // defined in parser.yy |
---|
54 | |
---|
55 | DeclarationNode::DeclarationNode() : |
---|
56 | linkage( ::linkage ) { |
---|
57 | |
---|
58 | // variable.name = nullptr; |
---|
59 | variable.tyClass = TypeDecl::NUMBER_OF_KINDS; |
---|
60 | variable.assertions = nullptr; |
---|
61 | variable.initializer = nullptr; |
---|
62 | |
---|
63 | assert.condition = nullptr; |
---|
64 | assert.message = nullptr; |
---|
65 | } |
---|
66 | |
---|
67 | DeclarationNode::~DeclarationNode() { |
---|
68 | // delete variable.name; |
---|
69 | delete variable.assertions; |
---|
70 | delete variable.initializer; |
---|
71 | |
---|
72 | // delete type; |
---|
73 | delete bitfieldWidth; |
---|
74 | |
---|
75 | delete asmStmt; |
---|
76 | // asmName, no delete, passed to next stage |
---|
77 | delete initializer; |
---|
78 | |
---|
79 | delete assert.condition; |
---|
80 | delete assert.message; |
---|
81 | } |
---|
82 | |
---|
83 | DeclarationNode * DeclarationNode::clone() const { |
---|
84 | DeclarationNode * newnode = new DeclarationNode; |
---|
85 | newnode->set_next( maybeClone( get_next() ) ); |
---|
86 | newnode->name = name ? new string( *name ) : nullptr; |
---|
87 | |
---|
88 | newnode->builtin = NoBuiltinType; |
---|
89 | newnode->type = maybeClone( type ); |
---|
90 | newnode->inLine = inLine; |
---|
91 | newnode->storageClasses = storageClasses; |
---|
92 | newnode->funcSpecs = funcSpecs; |
---|
93 | newnode->bitfieldWidth = maybeClone( bitfieldWidth ); |
---|
94 | newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) ); |
---|
95 | newnode->hasEllipsis = hasEllipsis; |
---|
96 | newnode->linkage = linkage; |
---|
97 | newnode->asmName = maybeClone( asmName ); |
---|
98 | cloneAll( attributes, newnode->attributes ); |
---|
99 | newnode->initializer = maybeClone( initializer ); |
---|
100 | newnode->extension = extension; |
---|
101 | newnode->asmStmt = maybeClone( asmStmt ); |
---|
102 | newnode->error = error; |
---|
103 | |
---|
104 | // newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr; |
---|
105 | newnode->variable.tyClass = variable.tyClass; |
---|
106 | newnode->variable.assertions = maybeClone( variable.assertions ); |
---|
107 | newnode->variable.initializer = maybeClone( variable.initializer ); |
---|
108 | |
---|
109 | newnode->assert.condition = maybeClone( assert.condition ); |
---|
110 | newnode->assert.message = maybeClone( assert.message ); |
---|
111 | return newnode; |
---|
112 | } // DeclarationNode::clone |
---|
113 | |
---|
114 | void DeclarationNode::print( std::ostream & os, int indent ) const { |
---|
115 | os << string( indent, ' ' ); |
---|
116 | if ( name ) { |
---|
117 | os << *name << ": "; |
---|
118 | } // if |
---|
119 | |
---|
120 | if ( linkage != LinkageSpec::Cforall ) { |
---|
121 | os << LinkageSpec::name( linkage ) << " "; |
---|
122 | } // if |
---|
123 | |
---|
124 | storageClasses.print( os ); |
---|
125 | funcSpecs.print( os ); |
---|
126 | |
---|
127 | if ( type ) { |
---|
128 | type->print( os, indent ); |
---|
129 | } else { |
---|
130 | os << "untyped entity "; |
---|
131 | } // if |
---|
132 | |
---|
133 | if ( bitfieldWidth ) { |
---|
134 | os << endl << string( indent + 2, ' ' ) << "with bitfield width "; |
---|
135 | bitfieldWidth->printOneLine( os ); |
---|
136 | } // if |
---|
137 | |
---|
138 | if ( initializer ) { |
---|
139 | os << endl << string( indent + 2, ' ' ) << "with initializer "; |
---|
140 | initializer->printOneLine( os ); |
---|
141 | os << " maybe constructed? " << initializer->get_maybeConstructed(); |
---|
142 | } // if |
---|
143 | |
---|
144 | if ( ! attributes.empty() ) { |
---|
145 | os << string( indent + 2, ' ' ) << "with attributes " << endl; |
---|
146 | for ( Attribute * attr: reverseIterate( attributes ) ) { |
---|
147 | os << string( indent + 4, ' ' ) << attr->name.c_str() << endl; |
---|
148 | } // for |
---|
149 | } // if |
---|
150 | |
---|
151 | os << endl; |
---|
152 | } |
---|
153 | |
---|
154 | void DeclarationNode::printList( std::ostream & os, int indent ) const { |
---|
155 | ParseNode::printList( os, indent ); |
---|
156 | if ( hasEllipsis ) { |
---|
157 | os << string( indent, ' ' ) << "and a variable number of other arguments" << endl; |
---|
158 | } // if |
---|
159 | } |
---|
160 | |
---|
161 | DeclarationNode * DeclarationNode::newStorageClass( Type::StorageClasses sc ) { |
---|
162 | DeclarationNode * newnode = new DeclarationNode; |
---|
163 | newnode->storageClasses = sc; |
---|
164 | return newnode; |
---|
165 | } // DeclarationNode::newStorageClass |
---|
166 | |
---|
167 | DeclarationNode * DeclarationNode::newFuncSpecifier( Type::FuncSpecifiers fs ) { |
---|
168 | DeclarationNode * newnode = new DeclarationNode; |
---|
169 | newnode->funcSpecs = fs; |
---|
170 | return newnode; |
---|
171 | } // DeclarationNode::newFuncSpecifier |
---|
172 | |
---|
173 | DeclarationNode * DeclarationNode::newTypeQualifier( Type::Qualifiers tq ) { |
---|
174 | DeclarationNode * newnode = new DeclarationNode; |
---|
175 | newnode->type = new TypeData(); |
---|
176 | newnode->type->qualifiers = tq; |
---|
177 | return newnode; |
---|
178 | } // DeclarationNode::newQualifier |
---|
179 | |
---|
180 | DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) { |
---|
181 | DeclarationNode * newnode = new DeclarationNode; |
---|
182 | newnode->type = new TypeData( TypeData::Basic ); |
---|
183 | newnode->type->basictype = bt; |
---|
184 | return newnode; |
---|
185 | } // DeclarationNode::newBasicType |
---|
186 | |
---|
187 | DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) { |
---|
188 | DeclarationNode * newnode = new DeclarationNode; |
---|
189 | newnode->type = new TypeData( TypeData::Basic ); |
---|
190 | newnode->type->complextype = ct; |
---|
191 | return newnode; |
---|
192 | } // DeclarationNode::newComplexType |
---|
193 | |
---|
194 | DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) { |
---|
195 | DeclarationNode * newnode = new DeclarationNode; |
---|
196 | newnode->type = new TypeData( TypeData::Basic ); |
---|
197 | newnode->type->signedness = sn; |
---|
198 | return newnode; |
---|
199 | } // DeclarationNode::newSignedNess |
---|
200 | |
---|
201 | DeclarationNode * DeclarationNode::newLength( Length lnth ) { |
---|
202 | DeclarationNode * newnode = new DeclarationNode; |
---|
203 | newnode->type = new TypeData( TypeData::Basic ); |
---|
204 | newnode->type->length = lnth; |
---|
205 | return newnode; |
---|
206 | } // DeclarationNode::newLength |
---|
207 | |
---|
208 | DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) { |
---|
209 | DeclarationNode * newnode = new DeclarationNode; |
---|
210 | newnode->type = new TypeData( TypeData::Unknown ); |
---|
211 | newnode->type->forall = forall; |
---|
212 | return newnode; |
---|
213 | } // DeclarationNode::newForall |
---|
214 | |
---|
215 | DeclarationNode * DeclarationNode::newFromGlobalScope() { |
---|
216 | DeclarationNode * newnode = new DeclarationNode; |
---|
217 | newnode->type = new TypeData( TypeData::GlobalScope ); |
---|
218 | return newnode; |
---|
219 | } |
---|
220 | |
---|
221 | DeclarationNode * DeclarationNode::newQualifiedType( DeclarationNode * parent, DeclarationNode * child) { |
---|
222 | DeclarationNode * newnode = new DeclarationNode; |
---|
223 | newnode->type = new TypeData( TypeData::Qualified ); |
---|
224 | newnode->type->qualified.parent = parent->type; |
---|
225 | newnode->type->qualified.child = child->type; |
---|
226 | parent->type = nullptr; |
---|
227 | child->type = nullptr; |
---|
228 | delete parent; |
---|
229 | delete child; |
---|
230 | return newnode; |
---|
231 | } |
---|
232 | |
---|
233 | DeclarationNode * DeclarationNode::newAggregate( AggregateDecl::Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) { |
---|
234 | DeclarationNode * newnode = new DeclarationNode; |
---|
235 | newnode->type = new TypeData( TypeData::Aggregate ); |
---|
236 | newnode->type->aggregate.kind = kind; |
---|
237 | newnode->type->aggregate.anon = name == nullptr; |
---|
238 | newnode->type->aggregate.name = newnode->type->aggregate.anon ? new string( DeclarationNode::anonymous.newName() ) : name; |
---|
239 | newnode->type->aggregate.actuals = actuals; |
---|
240 | newnode->type->aggregate.fields = fields; |
---|
241 | newnode->type->aggregate.body = body; |
---|
242 | newnode->type->aggregate.tagged = false; |
---|
243 | newnode->type->aggregate.parent = nullptr; |
---|
244 | return newnode; |
---|
245 | } // DeclarationNode::newAggregate |
---|
246 | |
---|
247 | DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body, bool typed, DeclarationNode * base, EnumHiding hiding ) { |
---|
248 | DeclarationNode * newnode = new DeclarationNode; |
---|
249 | newnode->type = new TypeData( TypeData::Enum ); |
---|
250 | newnode->type->enumeration.anon = name == nullptr; |
---|
251 | newnode->type->enumeration.name = newnode->type->enumeration.anon ? new string( DeclarationNode::anonymous.newName() ) : name; |
---|
252 | newnode->type->enumeration.constants = constants; |
---|
253 | newnode->type->enumeration.body = body; |
---|
254 | newnode->type->enumeration.typed = typed; |
---|
255 | newnode->type->enumeration.hiding = hiding; |
---|
256 | if ( base && base->type) { |
---|
257 | newnode->type->base = base->type; |
---|
258 | } // if |
---|
259 | |
---|
260 | return newnode; |
---|
261 | } // DeclarationNode::newEnum |
---|
262 | |
---|
263 | |
---|
264 | |
---|
265 | DeclarationNode * DeclarationNode::newName( const string * name ) { |
---|
266 | DeclarationNode * newnode = new DeclarationNode; |
---|
267 | assert( ! newnode->name ); |
---|
268 | newnode->name = name; |
---|
269 | return newnode; |
---|
270 | } // DeclarationNode::newName |
---|
271 | |
---|
272 | DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) { |
---|
273 | DeclarationNode * newnode = newName( name ); |
---|
274 | newnode->enumeratorValue.reset( constant ); |
---|
275 | return newnode; |
---|
276 | } // DeclarationNode::newEnumConstant |
---|
277 | |
---|
278 | DeclarationNode * DeclarationNode::newEnumValueGeneric( const string * name, InitializerNode * init ) { |
---|
279 | if ( init ) { |
---|
280 | if ( init->get_expression() ) { |
---|
281 | return newEnumConstant( name, init->get_expression() ); |
---|
282 | } else { |
---|
283 | DeclarationNode * newnode = newName( name ); |
---|
284 | newnode->initializer = init; |
---|
285 | return newnode; |
---|
286 | } // if |
---|
287 | } else { |
---|
288 | return newName( name ); |
---|
289 | } // if |
---|
290 | } // DeclarationNode::newEnumValueGeneric |
---|
291 | |
---|
292 | DeclarationNode * DeclarationNode::newEnumInLine( const string name ) { |
---|
293 | DeclarationNode * newnode = newName( new std::string(name) ); |
---|
294 | newnode->enumInLine = true; |
---|
295 | return newnode; |
---|
296 | } |
---|
297 | |
---|
298 | DeclarationNode * DeclarationNode::newFromTypedef( const string * name ) { |
---|
299 | DeclarationNode * newnode = new DeclarationNode; |
---|
300 | newnode->type = new TypeData( TypeData::SymbolicInst ); |
---|
301 | newnode->type->symbolic.name = name; |
---|
302 | newnode->type->symbolic.isTypedef = true; |
---|
303 | newnode->type->symbolic.params = nullptr; |
---|
304 | return newnode; |
---|
305 | } // DeclarationNode::newFromTypedef |
---|
306 | |
---|
307 | DeclarationNode * DeclarationNode::newFromTypeGen( const string * name, ExpressionNode * params ) { |
---|
308 | DeclarationNode * newnode = new DeclarationNode; |
---|
309 | newnode->type = new TypeData( TypeData::SymbolicInst ); |
---|
310 | newnode->type->symbolic.name = name; |
---|
311 | newnode->type->symbolic.isTypedef = false; |
---|
312 | newnode->type->symbolic.actuals = params; |
---|
313 | return newnode; |
---|
314 | } // DeclarationNode::newFromTypeGen |
---|
315 | |
---|
316 | DeclarationNode * DeclarationNode::newTypeParam( TypeDecl::Kind tc, const string * name ) { |
---|
317 | DeclarationNode * newnode = newName( name ); |
---|
318 | newnode->type = nullptr; |
---|
319 | newnode->variable.tyClass = tc; |
---|
320 | newnode->variable.assertions = nullptr; |
---|
321 | return newnode; |
---|
322 | } // DeclarationNode::newTypeParam |
---|
323 | |
---|
324 | DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) { |
---|
325 | DeclarationNode * newnode = new DeclarationNode; |
---|
326 | newnode->type = new TypeData( TypeData::Aggregate ); |
---|
327 | newnode->type->aggregate.name = name; |
---|
328 | newnode->type->aggregate.kind = AggregateDecl::Trait; |
---|
329 | newnode->type->aggregate.params = params; |
---|
330 | newnode->type->aggregate.fields = asserts; |
---|
331 | return newnode; |
---|
332 | } // DeclarationNode::newTrait |
---|
333 | |
---|
334 | DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) { |
---|
335 | DeclarationNode * newnode = new DeclarationNode; |
---|
336 | newnode->type = new TypeData( TypeData::AggregateInst ); |
---|
337 | newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate ); |
---|
338 | newnode->type->aggInst.aggregate->aggregate.kind = AggregateDecl::Trait; |
---|
339 | newnode->type->aggInst.aggregate->aggregate.name = name; |
---|
340 | newnode->type->aggInst.params = params; |
---|
341 | return newnode; |
---|
342 | } // DeclarationNode::newTraitUse |
---|
343 | |
---|
344 | DeclarationNode * DeclarationNode::newTypeDecl( const string * name, DeclarationNode * typeParams ) { |
---|
345 | DeclarationNode * newnode = newName( name ); |
---|
346 | newnode->type = new TypeData( TypeData::Symbolic ); |
---|
347 | newnode->type->symbolic.isTypedef = false; |
---|
348 | newnode->type->symbolic.params = typeParams; |
---|
349 | return newnode; |
---|
350 | } // DeclarationNode::newTypeDecl |
---|
351 | |
---|
352 | DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers, OperKinds kind ) { |
---|
353 | DeclarationNode * newnode = new DeclarationNode; |
---|
354 | newnode->type = new TypeData( kind == OperKinds::PointTo ? TypeData::Pointer : TypeData::Reference ); |
---|
355 | if ( kind == OperKinds::And ) { |
---|
356 | // T && is parsed as 'And' operator rather than two references => add a second reference type |
---|
357 | TypeData * td = new TypeData( TypeData::Reference ); |
---|
358 | td->base = newnode->type; |
---|
359 | newnode->type = td; |
---|
360 | } |
---|
361 | if ( qualifiers ) { |
---|
362 | return newnode->addQualifiers( qualifiers ); |
---|
363 | } else { |
---|
364 | return newnode; |
---|
365 | } // if |
---|
366 | } // DeclarationNode::newPointer |
---|
367 | |
---|
368 | DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) { |
---|
369 | DeclarationNode * newnode = new DeclarationNode; |
---|
370 | newnode->type = new TypeData( TypeData::Array ); |
---|
371 | newnode->type->array.dimension = size; |
---|
372 | newnode->type->array.isStatic = isStatic; |
---|
373 | if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) { |
---|
374 | newnode->type->array.isVarLen = false; |
---|
375 | } else { |
---|
376 | newnode->type->array.isVarLen = true; |
---|
377 | } // if |
---|
378 | return newnode->addQualifiers( qualifiers ); |
---|
379 | } // DeclarationNode::newArray |
---|
380 | |
---|
381 | DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) { |
---|
382 | DeclarationNode * newnode = new DeclarationNode; |
---|
383 | newnode->type = new TypeData( TypeData::Array ); |
---|
384 | newnode->type->array.dimension = nullptr; |
---|
385 | newnode->type->array.isStatic = false; |
---|
386 | newnode->type->array.isVarLen = true; |
---|
387 | return newnode->addQualifiers( qualifiers ); |
---|
388 | } |
---|
389 | |
---|
390 | DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) { |
---|
391 | DeclarationNode * newnode = new DeclarationNode; |
---|
392 | newnode->bitfieldWidth = size; |
---|
393 | return newnode; |
---|
394 | } |
---|
395 | |
---|
396 | DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) { |
---|
397 | DeclarationNode * newnode = new DeclarationNode; |
---|
398 | newnode->type = new TypeData( TypeData::Tuple ); |
---|
399 | newnode->type->tuple = members; |
---|
400 | return newnode; |
---|
401 | } |
---|
402 | |
---|
403 | DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr, bool basetypeof ) { |
---|
404 | DeclarationNode * newnode = new DeclarationNode; |
---|
405 | newnode->type = new TypeData( basetypeof ? TypeData::Basetypeof : TypeData::Typeof ); |
---|
406 | newnode->type->typeexpr = expr; |
---|
407 | return newnode; |
---|
408 | } |
---|
409 | |
---|
410 | DeclarationNode * DeclarationNode::newVtableType( DeclarationNode * decl ) { |
---|
411 | DeclarationNode * newnode = new DeclarationNode; |
---|
412 | newnode->type = new TypeData( TypeData::Vtable ); |
---|
413 | newnode->setBase( decl->type ); |
---|
414 | return newnode; |
---|
415 | } |
---|
416 | |
---|
417 | DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) { |
---|
418 | DeclarationNode * newnode = new DeclarationNode; |
---|
419 | newnode->type = new TypeData( TypeData::Builtin ); |
---|
420 | newnode->builtin = bt; |
---|
421 | newnode->type->builtintype = newnode->builtin; |
---|
422 | return newnode; |
---|
423 | } // DeclarationNode::newBuiltinType |
---|
424 | |
---|
425 | DeclarationNode * DeclarationNode::newFunction( const string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ) { |
---|
426 | DeclarationNode * newnode = newName( name ); |
---|
427 | newnode->type = new TypeData( TypeData::Function ); |
---|
428 | newnode->type->function.params = param; |
---|
429 | newnode->type->function.body = body; |
---|
430 | |
---|
431 | if ( ret ) { |
---|
432 | newnode->type->base = ret->type; |
---|
433 | ret->type = nullptr; |
---|
434 | delete ret; |
---|
435 | } // if |
---|
436 | |
---|
437 | return newnode; |
---|
438 | } // DeclarationNode::newFunction |
---|
439 | |
---|
440 | DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) { |
---|
441 | DeclarationNode * newnode = new DeclarationNode; |
---|
442 | newnode->type = nullptr; |
---|
443 | std::list< Expression * > exprs; |
---|
444 | buildList( expr, exprs ); |
---|
445 | newnode->attributes.push_back( new Attribute( *name, exprs ) ); |
---|
446 | delete name; |
---|
447 | return newnode; |
---|
448 | } |
---|
449 | |
---|
450 | DeclarationNode * DeclarationNode::newDirectiveStmt( StatementNode * stmt ) { |
---|
451 | DeclarationNode * newnode = new DeclarationNode; |
---|
452 | newnode->directiveStmt = stmt; |
---|
453 | return newnode; |
---|
454 | } |
---|
455 | |
---|
456 | DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) { |
---|
457 | DeclarationNode * newnode = new DeclarationNode; |
---|
458 | newnode->asmStmt = stmt; |
---|
459 | return newnode; |
---|
460 | } |
---|
461 | |
---|
462 | DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, Expression * message ) { |
---|
463 | DeclarationNode * newnode = new DeclarationNode; |
---|
464 | newnode->assert.condition = condition; |
---|
465 | newnode->assert.message = message; |
---|
466 | return newnode; |
---|
467 | } |
---|
468 | |
---|
469 | |
---|
470 | void appendError( string & dst, const string & src ) { |
---|
471 | if ( src.empty() ) return; |
---|
472 | if ( dst.empty() ) { dst = src; return; } |
---|
473 | dst += ", " + src; |
---|
474 | } // appendError |
---|
475 | |
---|
476 | void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) { |
---|
477 | const Type::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization |
---|
478 | |
---|
479 | if ( (qsrc & qdst).any() ) { // duplicates ? |
---|
480 | for ( unsigned int i = 0; i < Type::NumTypeQualifier; i += 1 ) { // find duplicates |
---|
481 | if ( qsrc[i] && qdst[i] ) { |
---|
482 | appendError( error, string( "duplicate " ) + Type::QualifiersNames[i] ); |
---|
483 | } // if |
---|
484 | } // for |
---|
485 | } // for |
---|
486 | } // DeclarationNode::checkQualifiers |
---|
487 | |
---|
488 | void DeclarationNode::checkSpecifiers( DeclarationNode * src ) { |
---|
489 | if ( (funcSpecs & src->funcSpecs).any() ) { // duplicates ? |
---|
490 | for ( unsigned int i = 0; i < Type::NumFuncSpecifier; i += 1 ) { // find duplicates |
---|
491 | if ( funcSpecs[i] && src->funcSpecs[i] ) { |
---|
492 | appendError( error, string( "duplicate " ) + Type::FuncSpecifiersNames[i] ); |
---|
493 | } // if |
---|
494 | } // for |
---|
495 | } // if |
---|
496 | |
---|
497 | if ( storageClasses.any() && src->storageClasses.any() ) { // any reason to check ? |
---|
498 | if ( (storageClasses & src->storageClasses ).any() ) { // duplicates ? |
---|
499 | for ( unsigned int i = 0; i < Type::NumStorageClass; i += 1 ) { // find duplicates |
---|
500 | if ( storageClasses[i] && src->storageClasses[i] ) { |
---|
501 | appendError( error, string( "duplicate " ) + Type::StorageClassesNames[i] ); |
---|
502 | } // if |
---|
503 | } // for |
---|
504 | // src is the new item being added and has a single bit |
---|
505 | } else if ( ! src->storageClasses.is_threadlocal_any() ) { // conflict ? |
---|
506 | appendError( error, string( "conflicting " ) + Type::StorageClassesNames[storageClasses.ffs()] + |
---|
507 | " & " + Type::StorageClassesNames[src->storageClasses.ffs()] ); |
---|
508 | src->storageClasses.reset(); // FIX to preserve invariant of one basic storage specifier |
---|
509 | } // if |
---|
510 | } // if |
---|
511 | |
---|
512 | appendError( error, src->error ); |
---|
513 | } // DeclarationNode::checkSpecifiers |
---|
514 | |
---|
515 | DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) { |
---|
516 | funcSpecs |= q->funcSpecs; |
---|
517 | storageClasses |= q->storageClasses; |
---|
518 | |
---|
519 | for ( Attribute * attr: reverseIterate( q->attributes ) ) { |
---|
520 | attributes.push_front( attr->clone() ); |
---|
521 | } // for |
---|
522 | return this; |
---|
523 | } // DeclarationNode::copySpecifiers |
---|
524 | |
---|
525 | static void addQualifiersToType( TypeData *& src, TypeData * dst ) { |
---|
526 | if ( dst->base ) { |
---|
527 | addQualifiersToType( src, dst->base ); |
---|
528 | } else if ( dst->kind == TypeData::Function ) { |
---|
529 | dst->base = src; |
---|
530 | src = nullptr; |
---|
531 | } else { |
---|
532 | dst->qualifiers |= src->qualifiers; |
---|
533 | } // if |
---|
534 | } // addQualifiersToType |
---|
535 | |
---|
536 | DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) { |
---|
537 | if ( ! q ) { return this; } // empty qualifier |
---|
538 | |
---|
539 | checkSpecifiers( q ); |
---|
540 | copySpecifiers( q ); |
---|
541 | |
---|
542 | if ( ! q->type ) { delete q; return this; } |
---|
543 | |
---|
544 | if ( ! type ) { |
---|
545 | type = q->type; // reuse structure |
---|
546 | q->type = nullptr; |
---|
547 | delete q; |
---|
548 | return this; |
---|
549 | } // if |
---|
550 | |
---|
551 | if ( q->type->forall ) { // forall qualifier ? |
---|
552 | if ( type->forall ) { // polymorphic routine ? |
---|
553 | type->forall->appendList( q->type->forall ); // augment forall qualifier |
---|
554 | } else { |
---|
555 | if ( type->kind == TypeData::Aggregate ) { // struct/union ? |
---|
556 | if ( type->aggregate.params ) { // polymorphic ? |
---|
557 | type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier |
---|
558 | } else { // not polymorphic |
---|
559 | type->aggregate.params = q->type->forall; // set forall qualifier |
---|
560 | } // if |
---|
561 | } else { // not polymorphic |
---|
562 | type->forall = q->type->forall; // make polymorphic routine |
---|
563 | } // if |
---|
564 | } // if |
---|
565 | q->type->forall = nullptr; // forall qualifier moved |
---|
566 | } // if |
---|
567 | |
---|
568 | checkQualifiers( type, q->type ); |
---|
569 | if ( (builtin == Zero || builtin == One) && q->type->qualifiers.val != 0 && error.length() == 0 ) { |
---|
570 | SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, Type::QualifiersNames[ilog2( q->type->qualifiers.val )], builtinTypeNames[builtin] ); |
---|
571 | } // if |
---|
572 | addQualifiersToType( q->type, type ); |
---|
573 | |
---|
574 | delete q; |
---|
575 | return this; |
---|
576 | } // addQualifiers |
---|
577 | |
---|
578 | static void addTypeToType( TypeData *& src, TypeData *& dst ) { |
---|
579 | if ( src->forall && dst->kind == TypeData::Function ) { |
---|
580 | if ( dst->forall ) { |
---|
581 | dst->forall->appendList( src->forall ); |
---|
582 | } else { |
---|
583 | dst->forall = src->forall; |
---|
584 | } // if |
---|
585 | src->forall = nullptr; |
---|
586 | } // if |
---|
587 | if ( dst->base ) { |
---|
588 | addTypeToType( src, dst->base ); |
---|
589 | } else { |
---|
590 | switch ( dst->kind ) { |
---|
591 | case TypeData::Unknown: |
---|
592 | src->qualifiers |= dst->qualifiers; |
---|
593 | dst = src; |
---|
594 | src = nullptr; |
---|
595 | break; |
---|
596 | case TypeData::Basic: |
---|
597 | dst->qualifiers |= src->qualifiers; |
---|
598 | if ( src->kind != TypeData::Unknown ) { |
---|
599 | assert( src->kind == TypeData::Basic ); |
---|
600 | |
---|
601 | if ( dst->basictype == DeclarationNode::NoBasicType ) { |
---|
602 | dst->basictype = src->basictype; |
---|
603 | } else if ( src->basictype != DeclarationNode::NoBasicType ) |
---|
604 | SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: " ); |
---|
605 | |
---|
606 | if ( dst->complextype == DeclarationNode::NoComplexType ) { |
---|
607 | dst->complextype = src->complextype; |
---|
608 | } else if ( src->complextype != DeclarationNode::NoComplexType ) |
---|
609 | SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: " ); |
---|
610 | |
---|
611 | if ( dst->signedness == DeclarationNode::NoSignedness ) { |
---|
612 | dst->signedness = src->signedness; |
---|
613 | } else if ( src->signedness != DeclarationNode::NoSignedness ) |
---|
614 | SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: " ); |
---|
615 | |
---|
616 | if ( dst->length == DeclarationNode::NoLength ) { |
---|
617 | dst->length = src->length; |
---|
618 | } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) { |
---|
619 | dst->length = DeclarationNode::LongLong; |
---|
620 | } else if ( src->length != DeclarationNode::NoLength ) |
---|
621 | SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: " ); |
---|
622 | } // if |
---|
623 | break; |
---|
624 | default: |
---|
625 | switch ( src->kind ) { |
---|
626 | case TypeData::Aggregate: |
---|
627 | case TypeData::Enum: |
---|
628 | dst->base = new TypeData( TypeData::AggregateInst ); |
---|
629 | dst->base->aggInst.aggregate = src; |
---|
630 | if ( src->kind == TypeData::Aggregate ) { |
---|
631 | dst->base->aggInst.params = maybeClone( src->aggregate.actuals ); |
---|
632 | } // if |
---|
633 | dst->base->qualifiers |= src->qualifiers; |
---|
634 | src = nullptr; |
---|
635 | break; |
---|
636 | default: |
---|
637 | if ( dst->forall ) { |
---|
638 | dst->forall->appendList( src->forall ); |
---|
639 | } else { |
---|
640 | dst->forall = src->forall; |
---|
641 | } // if |
---|
642 | src->forall = nullptr; |
---|
643 | dst->base = src; |
---|
644 | src = nullptr; |
---|
645 | } // switch |
---|
646 | } // switch |
---|
647 | } // if |
---|
648 | } |
---|
649 | |
---|
650 | DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) { |
---|
651 | if ( o ) { |
---|
652 | checkSpecifiers( o ); |
---|
653 | copySpecifiers( o ); |
---|
654 | if ( o->type ) { |
---|
655 | if ( ! type ) { |
---|
656 | if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) { |
---|
657 | type = new TypeData( TypeData::AggregateInst ); |
---|
658 | type->aggInst.aggregate = o->type; |
---|
659 | if ( o->type->kind == TypeData::Aggregate ) { |
---|
660 | type->aggInst.hoistType = o->type->aggregate.body; |
---|
661 | type->aggInst.params = maybeClone( o->type->aggregate.actuals ); |
---|
662 | } else { |
---|
663 | type->aggInst.hoistType = o->type->enumeration.body; |
---|
664 | } // if |
---|
665 | type->qualifiers |= o->type->qualifiers; |
---|
666 | } else { |
---|
667 | type = o->type; |
---|
668 | } // if |
---|
669 | o->type = nullptr; |
---|
670 | } else { |
---|
671 | addTypeToType( o->type, type ); |
---|
672 | } // if |
---|
673 | } // if |
---|
674 | if ( o->bitfieldWidth ) { |
---|
675 | bitfieldWidth = o->bitfieldWidth; |
---|
676 | } // if |
---|
677 | |
---|
678 | // there may be typedefs chained onto the type |
---|
679 | if ( o->get_next() ) { |
---|
680 | set_last( o->get_next()->clone() ); |
---|
681 | } // if |
---|
682 | } // if |
---|
683 | delete o; |
---|
684 | |
---|
685 | return this; |
---|
686 | } |
---|
687 | |
---|
688 | DeclarationNode * DeclarationNode::addEnumBase( DeclarationNode * o ) { |
---|
689 | if ( o && o -> type) { |
---|
690 | type->base= o->type; |
---|
691 | } |
---|
692 | delete o; |
---|
693 | return this; |
---|
694 | } |
---|
695 | |
---|
696 | DeclarationNode * DeclarationNode::addTypedef() { |
---|
697 | TypeData * newtype = new TypeData( TypeData::Symbolic ); |
---|
698 | newtype->symbolic.params = nullptr; |
---|
699 | newtype->symbolic.isTypedef = true; |
---|
700 | newtype->symbolic.name = name ? new string( *name ) : nullptr; |
---|
701 | newtype->base = type; |
---|
702 | type = newtype; |
---|
703 | return this; |
---|
704 | } |
---|
705 | |
---|
706 | DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) { |
---|
707 | if ( variable.tyClass != TypeDecl::NUMBER_OF_KINDS ) { |
---|
708 | if ( variable.assertions ) { |
---|
709 | variable.assertions->appendList( assertions ); |
---|
710 | } else { |
---|
711 | variable.assertions = assertions; |
---|
712 | } // if |
---|
713 | return this; |
---|
714 | } // if |
---|
715 | |
---|
716 | assert( type ); |
---|
717 | switch ( type->kind ) { |
---|
718 | case TypeData::Symbolic: |
---|
719 | if ( type->symbolic.assertions ) { |
---|
720 | type->symbolic.assertions->appendList( assertions ); |
---|
721 | } else { |
---|
722 | type->symbolic.assertions = assertions; |
---|
723 | } // if |
---|
724 | break; |
---|
725 | default: |
---|
726 | assert( false ); |
---|
727 | } // switch |
---|
728 | |
---|
729 | return this; |
---|
730 | } |
---|
731 | |
---|
732 | DeclarationNode * DeclarationNode::addName( string * newname ) { |
---|
733 | assert( ! name ); |
---|
734 | name = newname; |
---|
735 | return this; |
---|
736 | } |
---|
737 | |
---|
738 | DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) { |
---|
739 | assert( ! asmName ); |
---|
740 | asmName = newname ? newname->asmName : nullptr; |
---|
741 | return this->addQualifiers( newname ); |
---|
742 | } |
---|
743 | |
---|
744 | DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) { |
---|
745 | bitfieldWidth = size; |
---|
746 | return this; |
---|
747 | } |
---|
748 | |
---|
749 | DeclarationNode * DeclarationNode::addVarArgs() { |
---|
750 | assert( type ); |
---|
751 | hasEllipsis = true; |
---|
752 | return this; |
---|
753 | } |
---|
754 | |
---|
755 | DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) { |
---|
756 | assert( type ); |
---|
757 | assert( type->kind == TypeData::Function ); |
---|
758 | assert( ! type->function.body ); |
---|
759 | type->function.body = body; |
---|
760 | type->function.withExprs = withExprs; |
---|
761 | return this; |
---|
762 | } |
---|
763 | |
---|
764 | DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) { |
---|
765 | assert( type ); |
---|
766 | assert( type->kind == TypeData::Function ); |
---|
767 | assert( ! type->function.oldDeclList ); |
---|
768 | type->function.oldDeclList = list; |
---|
769 | return this; |
---|
770 | } |
---|
771 | |
---|
772 | DeclarationNode * DeclarationNode::setBase( TypeData * newType ) { |
---|
773 | if ( type ) { |
---|
774 | TypeData * prevBase = type; |
---|
775 | TypeData * curBase = type->base; |
---|
776 | while ( curBase != nullptr ) { |
---|
777 | prevBase = curBase; |
---|
778 | curBase = curBase->base; |
---|
779 | } // while |
---|
780 | prevBase->base = newType; |
---|
781 | } else { |
---|
782 | type = newType; |
---|
783 | } // if |
---|
784 | return this; |
---|
785 | } |
---|
786 | |
---|
787 | DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) { |
---|
788 | if ( a ) { |
---|
789 | for ( Attribute *attr: reverseIterate( a->attributes ) ) { |
---|
790 | attributes.push_front( attr ); |
---|
791 | } // for |
---|
792 | a->attributes.clear(); |
---|
793 | } // if |
---|
794 | return this; |
---|
795 | } // copyAttribute |
---|
796 | |
---|
797 | DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) { |
---|
798 | if ( p ) { |
---|
799 | assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference ); |
---|
800 | setBase( p->type ); |
---|
801 | p->type = nullptr; |
---|
802 | copyAttribute( p ); |
---|
803 | delete p; |
---|
804 | } // if |
---|
805 | return this; |
---|
806 | } |
---|
807 | |
---|
808 | DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) { |
---|
809 | if ( a ) { |
---|
810 | assert( a->type->kind == TypeData::Array ); |
---|
811 | setBase( a->type ); |
---|
812 | a->type = nullptr; |
---|
813 | copyAttribute( a ); |
---|
814 | delete a; |
---|
815 | } // if |
---|
816 | return this; |
---|
817 | } |
---|
818 | |
---|
819 | DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) { |
---|
820 | if ( p ) { |
---|
821 | assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference ); |
---|
822 | if ( type ) { |
---|
823 | switch ( type->kind ) { |
---|
824 | case TypeData::Aggregate: |
---|
825 | case TypeData::Enum: |
---|
826 | p->type->base = new TypeData( TypeData::AggregateInst ); |
---|
827 | p->type->base->aggInst.aggregate = type; |
---|
828 | if ( type->kind == TypeData::Aggregate ) { |
---|
829 | p->type->base->aggInst.params = maybeClone( type->aggregate.actuals ); |
---|
830 | } // if |
---|
831 | p->type->base->qualifiers |= type->qualifiers; |
---|
832 | break; |
---|
833 | |
---|
834 | default: |
---|
835 | p->type->base = type; |
---|
836 | } // switch |
---|
837 | type = nullptr; |
---|
838 | } // if |
---|
839 | delete this; |
---|
840 | return p; |
---|
841 | } else { |
---|
842 | return this; |
---|
843 | } // if |
---|
844 | } |
---|
845 | |
---|
846 | static TypeData * findLast( TypeData * a ) { |
---|
847 | assert( a ); |
---|
848 | TypeData * cur = a; |
---|
849 | while ( cur->base ) { |
---|
850 | cur = cur->base; |
---|
851 | } // while |
---|
852 | return cur; |
---|
853 | } |
---|
854 | |
---|
855 | DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) { |
---|
856 | if ( ! a ) return this; |
---|
857 | assert( a->type->kind == TypeData::Array ); |
---|
858 | TypeData * lastArray = findLast( a->type ); |
---|
859 | if ( type ) { |
---|
860 | switch ( type->kind ) { |
---|
861 | case TypeData::Aggregate: |
---|
862 | case TypeData::Enum: |
---|
863 | lastArray->base = new TypeData( TypeData::AggregateInst ); |
---|
864 | lastArray->base->aggInst.aggregate = type; |
---|
865 | if ( type->kind == TypeData::Aggregate ) { |
---|
866 | lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals ); |
---|
867 | } // if |
---|
868 | lastArray->base->qualifiers |= type->qualifiers; |
---|
869 | break; |
---|
870 | default: |
---|
871 | lastArray->base = type; |
---|
872 | } // switch |
---|
873 | type = nullptr; |
---|
874 | } // if |
---|
875 | delete this; |
---|
876 | return a; |
---|
877 | } |
---|
878 | |
---|
879 | DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) { |
---|
880 | TypeData * ftype = new TypeData( TypeData::Function ); |
---|
881 | ftype->function.params = params; |
---|
882 | setBase( ftype ); |
---|
883 | return this; |
---|
884 | } |
---|
885 | |
---|
886 | static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) { |
---|
887 | if ( type ) { |
---|
888 | if ( type->kind != TypeData::Function ) { |
---|
889 | type->base = addIdListToType( type->base, ids ); |
---|
890 | } else { |
---|
891 | type->function.idList = ids; |
---|
892 | } // if |
---|
893 | return type; |
---|
894 | } else { |
---|
895 | TypeData * newtype = new TypeData( TypeData::Function ); |
---|
896 | newtype->function.idList = ids; |
---|
897 | return newtype; |
---|
898 | } // if |
---|
899 | } // addIdListToType |
---|
900 | |
---|
901 | DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) { |
---|
902 | type = addIdListToType( type, ids ); |
---|
903 | return this; |
---|
904 | } |
---|
905 | |
---|
906 | DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) { |
---|
907 | initializer = init; |
---|
908 | return this; |
---|
909 | } |
---|
910 | |
---|
911 | DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) { |
---|
912 | assertf( variable.tyClass != TypeDecl::NUMBER_OF_KINDS, "Called addTypeInitializer on something that isn't a type variable." ); |
---|
913 | variable.initializer = init; |
---|
914 | return this; |
---|
915 | } |
---|
916 | |
---|
917 | DeclarationNode * DeclarationNode::cloneType( string * name ) { |
---|
918 | DeclarationNode * newnode = newName( name ); |
---|
919 | newnode->type = maybeClone( type ); |
---|
920 | newnode->copySpecifiers( this ); |
---|
921 | return newnode; |
---|
922 | } |
---|
923 | |
---|
924 | DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) { |
---|
925 | if ( ! o ) return nullptr; |
---|
926 | |
---|
927 | o->copySpecifiers( this ); |
---|
928 | if ( type ) { |
---|
929 | TypeData * srcType = type; |
---|
930 | |
---|
931 | // search for the base type by scanning off pointers and array designators |
---|
932 | while ( srcType->base ) { |
---|
933 | srcType = srcType->base; |
---|
934 | } // while |
---|
935 | |
---|
936 | TypeData * newType = srcType->clone(); |
---|
937 | if ( newType->kind == TypeData::AggregateInst ) { |
---|
938 | // don't duplicate members |
---|
939 | if ( newType->aggInst.aggregate->kind == TypeData::Enum ) { |
---|
940 | delete newType->aggInst.aggregate->enumeration.constants; |
---|
941 | newType->aggInst.aggregate->enumeration.constants = nullptr; |
---|
942 | newType->aggInst.aggregate->enumeration.body = false; |
---|
943 | } else { |
---|
944 | assert( newType->aggInst.aggregate->kind == TypeData::Aggregate ); |
---|
945 | delete newType->aggInst.aggregate->aggregate.fields; |
---|
946 | newType->aggInst.aggregate->aggregate.fields = nullptr; |
---|
947 | newType->aggInst.aggregate->aggregate.body = false; |
---|
948 | } // if |
---|
949 | // don't hoist twice |
---|
950 | newType->aggInst.hoistType = false; |
---|
951 | } // if |
---|
952 | |
---|
953 | newType->forall = maybeClone( type->forall ); |
---|
954 | if ( ! o->type ) { |
---|
955 | o->type = newType; |
---|
956 | } else { |
---|
957 | addTypeToType( newType, o->type ); |
---|
958 | delete newType; |
---|
959 | } // if |
---|
960 | } // if |
---|
961 | return o; |
---|
962 | } |
---|
963 | |
---|
964 | DeclarationNode * DeclarationNode::extractAggregate() const { |
---|
965 | if ( type ) { |
---|
966 | TypeData * ret = typeextractAggregate( type ); |
---|
967 | if ( ret ) { |
---|
968 | DeclarationNode * newnode = new DeclarationNode; |
---|
969 | newnode->type = ret; |
---|
970 | return newnode; |
---|
971 | } // if |
---|
972 | } // if |
---|
973 | return nullptr; |
---|
974 | } |
---|
975 | |
---|
976 | void buildList( const DeclarationNode * firstNode, std::list< Declaration * > & outputList ) { |
---|
977 | SemanticErrorException errors; |
---|
978 | std::back_insert_iterator< std::list< Declaration * > > out( outputList ); |
---|
979 | |
---|
980 | for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) { |
---|
981 | try { |
---|
982 | bool extracted = false, anon = false; |
---|
983 | AggregateDecl * unionDecl = nullptr; |
---|
984 | |
---|
985 | if ( DeclarationNode * extr = cur->extractAggregate() ) { |
---|
986 | // handle the case where a structure declaration is contained within an object or type declaration |
---|
987 | |
---|
988 | Declaration * decl = extr->build(); |
---|
989 | if ( decl ) { |
---|
990 | // Remember the declaration if it is a union aggregate ? |
---|
991 | unionDecl = dynamic_cast<UnionDecl *>( decl ); |
---|
992 | |
---|
993 | decl->location = cur->location; |
---|
994 | *out++ = decl; |
---|
995 | |
---|
996 | // need to remember the cases where a declaration contains an anonymous aggregate definition |
---|
997 | extracted = true; |
---|
998 | assert( extr->type ); |
---|
999 | if ( extr->type->kind == TypeData::Aggregate ) { |
---|
1000 | // typedef struct { int A } B is the only case? |
---|
1001 | anon = extr->type->aggregate.anon; |
---|
1002 | } else if ( extr->type->kind == TypeData::Enum ) { |
---|
1003 | // typedef enum { A } B is the only case? |
---|
1004 | anon = extr->type->enumeration.anon; |
---|
1005 | } |
---|
1006 | } // if |
---|
1007 | delete extr; |
---|
1008 | } // if |
---|
1009 | |
---|
1010 | Declaration * decl = cur->build(); |
---|
1011 | if ( decl ) { |
---|
1012 | if ( TypedefDecl * typedefDecl = dynamic_cast<TypedefDecl *>( decl ) ) { |
---|
1013 | if ( unionDecl ) { // is the typedef alias a union aggregate ? |
---|
1014 | // This code handles a special issue with the attribute transparent_union. |
---|
1015 | // |
---|
1016 | // typedef union U { int i; } typedef_name __attribute__(( aligned(16) )) __attribute__(( transparent_union )) |
---|
1017 | // |
---|
1018 | // Here the attribute aligned goes with the typedef_name, so variables declared of this type are |
---|
1019 | // aligned. However, the attribute transparent_union must be moved from the typedef_name to |
---|
1020 | // alias union U. Currently, this is the only know attribute that must be moved from typedef to |
---|
1021 | // alias. |
---|
1022 | |
---|
1023 | // If typedef is an alias for a union, then its alias type was hoisted above and remembered. |
---|
1024 | if ( UnionInstType * unionInstType = dynamic_cast<UnionInstType *>( typedefDecl->base ) ) { |
---|
1025 | // Remove all transparent_union attributes from typedef and move to alias union. |
---|
1026 | list<Attribute *>::iterator attr; |
---|
1027 | for ( attr = unionInstType->attributes.begin(); attr != unionInstType->attributes.end(); ) { // forward order |
---|
1028 | if ( (*attr)->name == "transparent_union" || (*attr)->name == "__transparent_union__" ) { |
---|
1029 | list<Attribute *>::iterator cur = attr; // remember current node |
---|
1030 | attr++; // advance iterator |
---|
1031 | unionDecl->attributes.emplace_back( *cur ); // move current |
---|
1032 | unionInstType->attributes.erase( cur ); // remove current |
---|
1033 | } else { |
---|
1034 | attr++; // advance iterator |
---|
1035 | } // if |
---|
1036 | } // for |
---|
1037 | } // if |
---|
1038 | } // if |
---|
1039 | } // if |
---|
1040 | |
---|
1041 | // don't include anonymous declaration for named aggregates, but do include them for anonymous aggregates, e.g.: |
---|
1042 | // struct S { |
---|
1043 | // struct T { int x; }; // no anonymous member |
---|
1044 | // struct { int y; }; // anonymous member |
---|
1045 | // struct T; // anonymous member |
---|
1046 | // }; |
---|
1047 | if ( ! (extracted && decl->name == "" && ! anon && ! cur->get_inLine()) ) { |
---|
1048 | if ( decl->name == "" ) { |
---|
1049 | if ( DeclarationWithType * dwt = dynamic_cast<DeclarationWithType *>( decl ) ) { |
---|
1050 | if ( ReferenceToType * aggr = dynamic_cast<ReferenceToType *>( dwt->get_type() ) ) { |
---|
1051 | if ( aggr->name.find("anonymous") == std::string::npos ) { |
---|
1052 | if ( ! cur->get_inLine() ) { |
---|
1053 | // temporary: warn about anonymous member declarations of named types, since |
---|
1054 | // this conflicts with the syntax for the forward declaration of an anonymous type |
---|
1055 | SemanticWarning( cur->location, Warning::AggrForwardDecl, aggr->name.c_str() ); |
---|
1056 | } // if |
---|
1057 | } // if |
---|
1058 | } // if |
---|
1059 | } // if |
---|
1060 | } // if |
---|
1061 | decl->location = cur->location; |
---|
1062 | *out++ = decl; |
---|
1063 | } // if |
---|
1064 | } // if |
---|
1065 | } catch( SemanticErrorException & e ) { |
---|
1066 | errors.append( e ); |
---|
1067 | } // try |
---|
1068 | } // for |
---|
1069 | |
---|
1070 | if ( ! errors.isEmpty() ) { |
---|
1071 | throw errors; |
---|
1072 | } // if |
---|
1073 | } // buildList |
---|
1074 | |
---|
1075 | // currently only builds assertions, function parameters, and return values |
---|
1076 | void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > & outputList ) { |
---|
1077 | SemanticErrorException errors; |
---|
1078 | std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList ); |
---|
1079 | |
---|
1080 | for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) { |
---|
1081 | try { |
---|
1082 | Declaration * decl = cur->build(); |
---|
1083 | assert( decl ); |
---|
1084 | if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { |
---|
1085 | dwt->location = cur->location; |
---|
1086 | *out++ = dwt; |
---|
1087 | } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) { |
---|
1088 | // e.g., int foo(struct S) {} |
---|
1089 | StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->name ); |
---|
1090 | auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); |
---|
1091 | obj->location = cur->location; |
---|
1092 | *out++ = obj; |
---|
1093 | delete agg; |
---|
1094 | } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) { |
---|
1095 | // e.g., int foo(union U) {} |
---|
1096 | UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->name ); |
---|
1097 | auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); |
---|
1098 | obj->location = cur->location; |
---|
1099 | *out++ = obj; |
---|
1100 | } else if ( EnumDecl * agg = dynamic_cast< EnumDecl * >( decl ) ) { |
---|
1101 | // e.g., int foo(enum E) {} |
---|
1102 | EnumInstType * inst = new EnumInstType( Type::Qualifiers(), agg->name ); |
---|
1103 | auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); |
---|
1104 | obj->location = cur->location; |
---|
1105 | *out++ = obj; |
---|
1106 | } // if |
---|
1107 | } catch( SemanticErrorException & e ) { |
---|
1108 | errors.append( e ); |
---|
1109 | } // try |
---|
1110 | } // for |
---|
1111 | |
---|
1112 | if ( ! errors.isEmpty() ) { |
---|
1113 | throw errors; |
---|
1114 | } // if |
---|
1115 | } // buildList |
---|
1116 | |
---|
1117 | void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > & outputList ) { |
---|
1118 | SemanticErrorException errors; |
---|
1119 | std::back_insert_iterator< std::list< Type * > > out( outputList ); |
---|
1120 | const DeclarationNode * cur = firstNode; |
---|
1121 | |
---|
1122 | while ( cur ) { |
---|
1123 | try { |
---|
1124 | * out++ = cur->buildType(); |
---|
1125 | } catch( SemanticErrorException & e ) { |
---|
1126 | errors.append( e ); |
---|
1127 | } // try |
---|
1128 | cur = dynamic_cast< DeclarationNode * >( cur->get_next() ); |
---|
1129 | } // while |
---|
1130 | |
---|
1131 | if ( ! errors.isEmpty() ) { |
---|
1132 | throw errors; |
---|
1133 | } // if |
---|
1134 | } // buildTypeList |
---|
1135 | |
---|
1136 | Declaration * DeclarationNode::build() const { |
---|
1137 | if ( ! error.empty() ) SemanticError( this, error + " in declaration of " ); |
---|
1138 | |
---|
1139 | if ( asmStmt ) { |
---|
1140 | return new AsmDecl( strict_dynamic_cast<AsmStmt *>( asmStmt->build() ) ); |
---|
1141 | } // if |
---|
1142 | if ( directiveStmt ) { |
---|
1143 | return new DirectiveDecl( strict_dynamic_cast<DirectiveStmt *>( directiveStmt->build() ) ); |
---|
1144 | } // if |
---|
1145 | |
---|
1146 | if ( variable.tyClass != TypeDecl::NUMBER_OF_KINDS ) { |
---|
1147 | // otype is internally converted to dtype + otype parameters |
---|
1148 | static const TypeDecl::Kind kindMap[] = { TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype, TypeDecl::Dimension }; |
---|
1149 | static_assert( sizeof(kindMap) / sizeof(kindMap[0]) == TypeDecl::NUMBER_OF_KINDS, "DeclarationNode::build: kindMap is out of sync." ); |
---|
1150 | assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." ); |
---|
1151 | TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ], variable.tyClass == TypeDecl::Otype || variable.tyClass == TypeDecl::DStype, variable.initializer ? variable.initializer->buildType() : nullptr ); |
---|
1152 | buildList( variable.assertions, ret->get_assertions() ); |
---|
1153 | return ret; |
---|
1154 | } // if |
---|
1155 | |
---|
1156 | if ( type ) { |
---|
1157 | // Function specifiers can only appear on a function definition/declaration. |
---|
1158 | // |
---|
1159 | // inline _Noreturn int f(); // allowed |
---|
1160 | // inline _Noreturn int g( int i ); // allowed |
---|
1161 | // inline _Noreturn int i; // disallowed |
---|
1162 | if ( type->kind != TypeData::Function && funcSpecs.any() ) { |
---|
1163 | SemanticError( this, "invalid function specifier for " ); |
---|
1164 | } // if |
---|
1165 | // Forall qualifier can only appear on a function/aggregate definition/declaration. |
---|
1166 | // |
---|
1167 | // forall int f(); // allowed |
---|
1168 | // forall int g( int i ); // allowed |
---|
1169 | // forall int i; // disallowed |
---|
1170 | if ( type->kind != TypeData::Function && type->forall ) { |
---|
1171 | SemanticError( this, "invalid type qualifier for " ); |
---|
1172 | } // if |
---|
1173 | bool isDelete = initializer && initializer->get_isDelete(); |
---|
1174 | Declaration * decl = buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, isDelete ? nullptr : maybeBuild< Initializer >(initializer), attributes )->set_extension( extension ); |
---|
1175 | if ( isDelete ) { |
---|
1176 | DeclarationWithType * dwt = strict_dynamic_cast<DeclarationWithType *>( decl ); |
---|
1177 | dwt->isDeleted = true; |
---|
1178 | } |
---|
1179 | return decl; |
---|
1180 | } // if |
---|
1181 | |
---|
1182 | if ( assert.condition ) { |
---|
1183 | return new StaticAssertDecl( maybeBuild< Expression >( assert.condition ), strict_dynamic_cast< ConstantExpr * >( maybeClone( assert.message ) ) ); |
---|
1184 | } |
---|
1185 | |
---|
1186 | // SUE's cannot have function specifiers, either |
---|
1187 | // |
---|
1188 | // inline _Noreturn struct S { ... }; // disallowed |
---|
1189 | // inline _Noreturn enum E { ... }; // disallowed |
---|
1190 | if ( funcSpecs.any() ) { |
---|
1191 | SemanticError( this, "invalid function specifier for " ); |
---|
1192 | } // if |
---|
1193 | if ( enumInLine ) { |
---|
1194 | return new InlineMemberDecl( *name, storageClasses, linkage, nullptr ); |
---|
1195 | } // if |
---|
1196 | assertf( name, "ObjectDecl must a have name\n" ); |
---|
1197 | return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension ); |
---|
1198 | } |
---|
1199 | |
---|
1200 | Type * DeclarationNode::buildType() const { |
---|
1201 | assert( type ); |
---|
1202 | |
---|
1203 | switch ( type->kind ) { |
---|
1204 | case TypeData::Enum: |
---|
1205 | case TypeData::Aggregate: { |
---|
1206 | ReferenceToType * ret = buildComAggInst( type, attributes, linkage ); |
---|
1207 | buildList( type->aggregate.actuals, ret->get_parameters() ); |
---|
1208 | return ret; |
---|
1209 | } |
---|
1210 | case TypeData::Symbolic: { |
---|
1211 | TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes ); |
---|
1212 | buildList( type->symbolic.actuals, ret->get_parameters() ); |
---|
1213 | return ret; |
---|
1214 | } |
---|
1215 | default: |
---|
1216 | Type * simpletypes = typebuild( type ); |
---|
1217 | simpletypes->get_attributes() = attributes; // copy because member is const |
---|
1218 | return simpletypes; |
---|
1219 | } // switch |
---|
1220 | } |
---|
1221 | |
---|
1222 | // Local Variables: // |
---|
1223 | // tab-width: 4 // |
---|
1224 | // mode: c++ // |
---|
1225 | // compile-command: "make install" // |
---|
1226 | // End: // |
---|