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