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 : Sat Jun 17 14:41:48 2023 |
---|
13 | // Update Count : 1405 |
---|
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( new ast::Attribute( *name, std::move( exprs ) ) ); |
---|
462 | delete name; |
---|
463 | return newnode; |
---|
464 | } |
---|
465 | |
---|
466 | DeclarationNode * DeclarationNode::newDirectiveStmt( StatementNode * stmt ) { |
---|
467 | DeclarationNode * newnode = new DeclarationNode; |
---|
468 | newnode->directiveStmt = stmt; |
---|
469 | return newnode; |
---|
470 | } |
---|
471 | |
---|
472 | DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) { |
---|
473 | DeclarationNode * newnode = new DeclarationNode; |
---|
474 | newnode->asmStmt = stmt; |
---|
475 | return newnode; |
---|
476 | } |
---|
477 | |
---|
478 | DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, ast::Expr * message ) { |
---|
479 | DeclarationNode * newnode = new DeclarationNode; |
---|
480 | newnode->assert.condition = condition; |
---|
481 | newnode->assert.message = message; |
---|
482 | return newnode; |
---|
483 | } |
---|
484 | |
---|
485 | static void appendError( string & dst, const string & src ) { |
---|
486 | if ( src.empty() ) return; |
---|
487 | if ( dst.empty() ) { dst = src; return; } |
---|
488 | dst += ", " + src; |
---|
489 | } // appendError |
---|
490 | |
---|
491 | void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) { |
---|
492 | const ast::CV::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization |
---|
493 | const ast::CV::Qualifiers duplicates = qsrc & qdst; |
---|
494 | |
---|
495 | if ( duplicates.any() ) { |
---|
496 | std::stringstream str; |
---|
497 | str << "duplicate "; |
---|
498 | ast::print( str, duplicates ); |
---|
499 | str << "qualifier(s)"; |
---|
500 | appendError( error, str.str() ); |
---|
501 | } // for |
---|
502 | } // DeclarationNode::checkQualifiers |
---|
503 | |
---|
504 | void DeclarationNode::checkSpecifiers( DeclarationNode * src ) { |
---|
505 | ast::Function::Specs fsDups = funcSpecs & src->funcSpecs; |
---|
506 | if ( fsDups.any() ) { |
---|
507 | std::stringstream str; |
---|
508 | str << "duplicate "; |
---|
509 | ast::print( str, fsDups ); |
---|
510 | str << "function specifier(s)"; |
---|
511 | appendError( error, str.str() ); |
---|
512 | } // if |
---|
513 | |
---|
514 | // Skip if everything is unset. |
---|
515 | if ( storageClasses.any() && src->storageClasses.any() ) { |
---|
516 | ast::Storage::Classes dups = storageClasses & src->storageClasses; |
---|
517 | // Check for duplicates. |
---|
518 | if ( dups.any() ) { |
---|
519 | std::stringstream str; |
---|
520 | str << "duplicate "; |
---|
521 | ast::print( str, dups ); |
---|
522 | str << "storage class(es)"; |
---|
523 | appendError( error, str.str() ); |
---|
524 | // Check for conflicts. |
---|
525 | } else if ( !src->storageClasses.is_threadlocal_any() ) { |
---|
526 | std::stringstream str; |
---|
527 | str << "conflicting "; |
---|
528 | ast::print( str, ast::Storage::Classes( 1 << storageClasses.ffs() ) ); |
---|
529 | str << "& "; |
---|
530 | ast::print( str, ast::Storage::Classes( 1 << src->storageClasses.ffs() ) ); |
---|
531 | str << "storage classes"; |
---|
532 | appendError( error, str.str() ); |
---|
533 | // FIX to preserve invariant of one basic storage specifier |
---|
534 | src->storageClasses.reset(); |
---|
535 | } |
---|
536 | } // if |
---|
537 | |
---|
538 | appendError( error, src->error ); |
---|
539 | } // DeclarationNode::checkSpecifiers |
---|
540 | |
---|
541 | DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) { |
---|
542 | funcSpecs |= q->funcSpecs; |
---|
543 | storageClasses |= q->storageClasses; |
---|
544 | |
---|
545 | std::vector<ast::ptr<ast::Attribute>> tmp; |
---|
546 | tmp.reserve( q->attributes.size() ); |
---|
547 | for ( auto const & attr : q->attributes ) { |
---|
548 | tmp.emplace_back( ast::shallowCopy( attr.get() ) ); |
---|
549 | } |
---|
550 | spliceBegin( attributes, tmp ); |
---|
551 | |
---|
552 | return this; |
---|
553 | } // DeclarationNode::copySpecifiers |
---|
554 | |
---|
555 | static void addQualifiersToType( TypeData *& src, TypeData * dst ) { |
---|
556 | if ( dst->base ) { |
---|
557 | addQualifiersToType( src, dst->base ); |
---|
558 | } else if ( dst->kind == TypeData::Function ) { |
---|
559 | dst->base = src; |
---|
560 | src = nullptr; |
---|
561 | } else { |
---|
562 | dst->qualifiers |= src->qualifiers; |
---|
563 | } // if |
---|
564 | } // addQualifiersToType |
---|
565 | |
---|
566 | DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) { |
---|
567 | if ( ! q ) { return this; } // empty qualifier |
---|
568 | |
---|
569 | checkSpecifiers( q ); |
---|
570 | copySpecifiers( q ); |
---|
571 | |
---|
572 | if ( ! q->type ) { delete q; return this; } |
---|
573 | |
---|
574 | if ( ! type ) { |
---|
575 | type = q->type; // reuse structure |
---|
576 | q->type = nullptr; |
---|
577 | delete q; |
---|
578 | return this; |
---|
579 | } // if |
---|
580 | |
---|
581 | if ( q->type->forall ) { // forall qualifier ? |
---|
582 | if ( type->forall ) { // polymorphic routine ? |
---|
583 | type->forall->appendList( q->type->forall ); // augment forall qualifier |
---|
584 | } else { |
---|
585 | if ( type->kind == TypeData::Aggregate ) { // struct/union ? |
---|
586 | if ( type->aggregate.params ) { // polymorphic ? |
---|
587 | type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier |
---|
588 | } else { // not polymorphic |
---|
589 | type->aggregate.params = q->type->forall; // set forall qualifier |
---|
590 | } // if |
---|
591 | } else { // not polymorphic |
---|
592 | type->forall = q->type->forall; // make polymorphic routine |
---|
593 | } // if |
---|
594 | } // if |
---|
595 | q->type->forall = nullptr; // forall qualifier moved |
---|
596 | } // if |
---|
597 | |
---|
598 | checkQualifiers( type, q->type ); |
---|
599 | if ( (builtin == Zero || builtin == One) && q->type->qualifiers.any() && error.length() == 0 ) { |
---|
600 | SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, builtinTypeNames[builtin] ); |
---|
601 | } // if |
---|
602 | addQualifiersToType( q->type, type ); |
---|
603 | |
---|
604 | delete q; |
---|
605 | return this; |
---|
606 | } // addQualifiers |
---|
607 | |
---|
608 | static void addTypeToType( TypeData *& src, TypeData *& dst ) { |
---|
609 | if ( src->forall && dst->kind == TypeData::Function ) { |
---|
610 | if ( dst->forall ) { |
---|
611 | dst->forall->appendList( src->forall ); |
---|
612 | } else { |
---|
613 | dst->forall = src->forall; |
---|
614 | } // if |
---|
615 | src->forall = nullptr; |
---|
616 | } // if |
---|
617 | if ( dst->base ) { |
---|
618 | addTypeToType( src, dst->base ); |
---|
619 | } else { |
---|
620 | switch ( dst->kind ) { |
---|
621 | case TypeData::Unknown: |
---|
622 | src->qualifiers |= dst->qualifiers; |
---|
623 | dst = src; |
---|
624 | src = nullptr; |
---|
625 | break; |
---|
626 | case TypeData::Basic: |
---|
627 | dst->qualifiers |= src->qualifiers; |
---|
628 | if ( src->kind != TypeData::Unknown ) { |
---|
629 | assert( src->kind == TypeData::Basic ); |
---|
630 | |
---|
631 | if ( dst->basictype == DeclarationNode::NoBasicType ) { |
---|
632 | dst->basictype = src->basictype; |
---|
633 | } else if ( src->basictype != DeclarationNode::NoBasicType ) |
---|
634 | SemanticError( yylloc, string( "multiple declaration types \"" ) + DeclarationNode::basicTypeNames[ dst->basictype ] + |
---|
635 | "\" and \"" + DeclarationNode::basicTypeNames[ src->basictype ] + "\"." ); |
---|
636 | |
---|
637 | if ( dst->complextype == DeclarationNode::NoComplexType ) { |
---|
638 | dst->complextype = src->complextype; |
---|
639 | } else if ( src->complextype != DeclarationNode::NoComplexType ) |
---|
640 | SemanticError( yylloc, string( "multiple declaration types \"" ) + DeclarationNode::complexTypeNames[ src->complextype ] + |
---|
641 | "\" and \"" + DeclarationNode::complexTypeNames[ src->complextype ] + "\"." ); |
---|
642 | |
---|
643 | if ( dst->signedness == DeclarationNode::NoSignedness ) { |
---|
644 | dst->signedness = src->signedness; |
---|
645 | } else if ( src->signedness != DeclarationNode::NoSignedness ) |
---|
646 | SemanticError( yylloc, string( "conflicting type specifier \"" ) + DeclarationNode::signednessNames[ dst->signedness ] + |
---|
647 | "\" and \"" + DeclarationNode::signednessNames[ src->signedness ] + "\"." ); |
---|
648 | |
---|
649 | if ( dst->length == DeclarationNode::NoLength ) { |
---|
650 | dst->length = src->length; |
---|
651 | } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) { |
---|
652 | dst->length = DeclarationNode::LongLong; |
---|
653 | } else if ( src->length != DeclarationNode::NoLength ) |
---|
654 | SemanticError( yylloc, string( "conflicting type specifier \"" ) + DeclarationNode::lengthNames[ dst->length ] + |
---|
655 | "\" and \"" + DeclarationNode::lengthNames[ src->length ] + "\"." ); |
---|
656 | } // if |
---|
657 | break; |
---|
658 | default: |
---|
659 | switch ( src->kind ) { |
---|
660 | case TypeData::Aggregate: |
---|
661 | case TypeData::Enum: |
---|
662 | dst->base = new TypeData( TypeData::AggregateInst ); |
---|
663 | dst->base->aggInst.aggregate = src; |
---|
664 | if ( src->kind == TypeData::Aggregate ) { |
---|
665 | dst->base->aggInst.params = maybeClone( src->aggregate.actuals ); |
---|
666 | } // if |
---|
667 | dst->base->qualifiers |= src->qualifiers; |
---|
668 | src = nullptr; |
---|
669 | break; |
---|
670 | default: |
---|
671 | if ( dst->forall ) { |
---|
672 | dst->forall->appendList( src->forall ); |
---|
673 | } else { |
---|
674 | dst->forall = src->forall; |
---|
675 | } // if |
---|
676 | src->forall = nullptr; |
---|
677 | dst->base = src; |
---|
678 | src = nullptr; |
---|
679 | } // switch |
---|
680 | } // switch |
---|
681 | } // if |
---|
682 | } |
---|
683 | |
---|
684 | DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) { |
---|
685 | if ( o ) { |
---|
686 | checkSpecifiers( o ); |
---|
687 | copySpecifiers( o ); |
---|
688 | if ( o->type ) { |
---|
689 | if ( ! type ) { |
---|
690 | if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) { |
---|
691 | type = new TypeData( TypeData::AggregateInst ); |
---|
692 | type->aggInst.aggregate = o->type; |
---|
693 | if ( o->type->kind == TypeData::Aggregate ) { |
---|
694 | type->aggInst.hoistType = o->type->aggregate.body; |
---|
695 | type->aggInst.params = maybeClone( o->type->aggregate.actuals ); |
---|
696 | } else { |
---|
697 | type->aggInst.hoistType = o->type->enumeration.body; |
---|
698 | } // if |
---|
699 | type->qualifiers |= o->type->qualifiers; |
---|
700 | } else { |
---|
701 | type = o->type; |
---|
702 | } // if |
---|
703 | o->type = nullptr; |
---|
704 | } else { |
---|
705 | addTypeToType( o->type, type ); |
---|
706 | } // if |
---|
707 | } // if |
---|
708 | if ( o->bitfieldWidth ) { |
---|
709 | bitfieldWidth = o->bitfieldWidth; |
---|
710 | } // if |
---|
711 | |
---|
712 | // there may be typedefs chained onto the type |
---|
713 | if ( o->get_next() ) { |
---|
714 | set_last( o->get_next()->clone() ); |
---|
715 | } // if |
---|
716 | } // if |
---|
717 | delete o; |
---|
718 | |
---|
719 | return this; |
---|
720 | } |
---|
721 | |
---|
722 | DeclarationNode * DeclarationNode::addEnumBase( DeclarationNode * o ) { |
---|
723 | if ( o && o->type) { |
---|
724 | type->base= o->type; |
---|
725 | } // if |
---|
726 | delete o; |
---|
727 | return this; |
---|
728 | } |
---|
729 | |
---|
730 | DeclarationNode * DeclarationNode::addTypedef() { |
---|
731 | TypeData * newtype = new TypeData( TypeData::Symbolic ); |
---|
732 | newtype->symbolic.params = nullptr; |
---|
733 | newtype->symbolic.isTypedef = true; |
---|
734 | newtype->symbolic.name = name ? new string( *name ) : nullptr; |
---|
735 | newtype->base = type; |
---|
736 | type = newtype; |
---|
737 | return this; |
---|
738 | } |
---|
739 | |
---|
740 | DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) { |
---|
741 | if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) { |
---|
742 | if ( variable.assertions ) { |
---|
743 | variable.assertions->appendList( assertions ); |
---|
744 | } else { |
---|
745 | variable.assertions = assertions; |
---|
746 | } // if |
---|
747 | return this; |
---|
748 | } // if |
---|
749 | |
---|
750 | assert( type ); |
---|
751 | switch ( type->kind ) { |
---|
752 | case TypeData::Symbolic: |
---|
753 | if ( type->symbolic.assertions ) { |
---|
754 | type->symbolic.assertions->appendList( assertions ); |
---|
755 | } else { |
---|
756 | type->symbolic.assertions = assertions; |
---|
757 | } // if |
---|
758 | break; |
---|
759 | default: |
---|
760 | assert( false ); |
---|
761 | } // switch |
---|
762 | |
---|
763 | return this; |
---|
764 | } |
---|
765 | |
---|
766 | DeclarationNode * DeclarationNode::addName( string * newname ) { |
---|
767 | assert( ! name ); |
---|
768 | name = newname; |
---|
769 | return this; |
---|
770 | } |
---|
771 | |
---|
772 | DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) { |
---|
773 | assert( ! asmName ); |
---|
774 | asmName = newname ? newname->asmName : nullptr; |
---|
775 | return this->addQualifiers( newname ); |
---|
776 | } |
---|
777 | |
---|
778 | DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) { |
---|
779 | bitfieldWidth = size; |
---|
780 | return this; |
---|
781 | } |
---|
782 | |
---|
783 | DeclarationNode * DeclarationNode::addVarArgs() { |
---|
784 | assert( type ); |
---|
785 | hasEllipsis = true; |
---|
786 | return this; |
---|
787 | } |
---|
788 | |
---|
789 | DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) { |
---|
790 | assert( type ); |
---|
791 | assert( type->kind == TypeData::Function ); |
---|
792 | assert( ! type->function.body ); |
---|
793 | type->function.body = body; |
---|
794 | type->function.withExprs = withExprs; |
---|
795 | return this; |
---|
796 | } |
---|
797 | |
---|
798 | DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) { |
---|
799 | assert( type ); |
---|
800 | assert( type->kind == TypeData::Function ); |
---|
801 | assert( ! type->function.oldDeclList ); |
---|
802 | type->function.oldDeclList = list; |
---|
803 | return this; |
---|
804 | } |
---|
805 | |
---|
806 | DeclarationNode * DeclarationNode::setBase( TypeData * newType ) { |
---|
807 | if ( type ) { |
---|
808 | TypeData * prevBase = type; |
---|
809 | TypeData * curBase = type->base; |
---|
810 | while ( curBase != nullptr ) { |
---|
811 | prevBase = curBase; |
---|
812 | curBase = curBase->base; |
---|
813 | } // while |
---|
814 | prevBase->base = newType; |
---|
815 | } else { |
---|
816 | type = newType; |
---|
817 | } // if |
---|
818 | return this; |
---|
819 | } |
---|
820 | |
---|
821 | DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) { |
---|
822 | if ( a ) { |
---|
823 | spliceBegin( attributes, a->attributes ); |
---|
824 | a->attributes.clear(); |
---|
825 | } // if |
---|
826 | return this; |
---|
827 | } // copyAttribute |
---|
828 | |
---|
829 | DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) { |
---|
830 | if ( p ) { |
---|
831 | assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference ); |
---|
832 | setBase( p->type ); |
---|
833 | p->type = nullptr; |
---|
834 | copyAttribute( p ); |
---|
835 | delete p; |
---|
836 | } // if |
---|
837 | return this; |
---|
838 | } |
---|
839 | |
---|
840 | DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) { |
---|
841 | if ( a ) { |
---|
842 | assert( a->type->kind == TypeData::Array ); |
---|
843 | setBase( a->type ); |
---|
844 | a->type = nullptr; |
---|
845 | copyAttribute( a ); |
---|
846 | delete a; |
---|
847 | } // if |
---|
848 | return this; |
---|
849 | } |
---|
850 | |
---|
851 | DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) { |
---|
852 | if ( p ) { |
---|
853 | assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference ); |
---|
854 | if ( type ) { |
---|
855 | switch ( type->kind ) { |
---|
856 | case TypeData::Aggregate: |
---|
857 | case TypeData::Enum: |
---|
858 | p->type->base = new TypeData( TypeData::AggregateInst ); |
---|
859 | p->type->base->aggInst.aggregate = type; |
---|
860 | if ( type->kind == TypeData::Aggregate ) { |
---|
861 | p->type->base->aggInst.params = maybeClone( type->aggregate.actuals ); |
---|
862 | } // if |
---|
863 | p->type->base->qualifiers |= type->qualifiers; |
---|
864 | break; |
---|
865 | |
---|
866 | default: |
---|
867 | p->type->base = type; |
---|
868 | } // switch |
---|
869 | type = nullptr; |
---|
870 | } // if |
---|
871 | delete this; |
---|
872 | return p; |
---|
873 | } else { |
---|
874 | return this; |
---|
875 | } // if |
---|
876 | } |
---|
877 | |
---|
878 | static TypeData * findLast( TypeData * a ) { |
---|
879 | assert( a ); |
---|
880 | TypeData * cur = a; |
---|
881 | while ( cur->base ) { |
---|
882 | cur = cur->base; |
---|
883 | } // while |
---|
884 | return cur; |
---|
885 | } |
---|
886 | |
---|
887 | DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) { |
---|
888 | if ( ! a ) return this; |
---|
889 | assert( a->type->kind == TypeData::Array ); |
---|
890 | TypeData * lastArray = findLast( a->type ); |
---|
891 | if ( type ) { |
---|
892 | switch ( type->kind ) { |
---|
893 | case TypeData::Aggregate: |
---|
894 | case TypeData::Enum: |
---|
895 | lastArray->base = new TypeData( TypeData::AggregateInst ); |
---|
896 | lastArray->base->aggInst.aggregate = type; |
---|
897 | if ( type->kind == TypeData::Aggregate ) { |
---|
898 | lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals ); |
---|
899 | } // if |
---|
900 | lastArray->base->qualifiers |= type->qualifiers; |
---|
901 | break; |
---|
902 | default: |
---|
903 | lastArray->base = type; |
---|
904 | } // switch |
---|
905 | type = nullptr; |
---|
906 | } // if |
---|
907 | delete this; |
---|
908 | return a; |
---|
909 | } |
---|
910 | |
---|
911 | DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) { |
---|
912 | TypeData * ftype = new TypeData( TypeData::Function ); |
---|
913 | ftype->function.params = params; |
---|
914 | setBase( ftype ); |
---|
915 | return this; |
---|
916 | } |
---|
917 | |
---|
918 | static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) { |
---|
919 | if ( type ) { |
---|
920 | if ( type->kind != TypeData::Function ) { |
---|
921 | type->base = addIdListToType( type->base, ids ); |
---|
922 | } else { |
---|
923 | type->function.idList = ids; |
---|
924 | } // if |
---|
925 | return type; |
---|
926 | } else { |
---|
927 | TypeData * newtype = new TypeData( TypeData::Function ); |
---|
928 | newtype->function.idList = ids; |
---|
929 | return newtype; |
---|
930 | } // if |
---|
931 | } // addIdListToType |
---|
932 | |
---|
933 | DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) { |
---|
934 | type = addIdListToType( type, ids ); |
---|
935 | return this; |
---|
936 | } |
---|
937 | |
---|
938 | DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) { |
---|
939 | initializer = init; |
---|
940 | return this; |
---|
941 | } |
---|
942 | |
---|
943 | DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) { |
---|
944 | assertf( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS, "Called addTypeInitializer on something that isn't a type variable." ); |
---|
945 | variable.initializer = init; |
---|
946 | return this; |
---|
947 | } |
---|
948 | |
---|
949 | DeclarationNode * DeclarationNode::cloneType( string * name ) { |
---|
950 | DeclarationNode * newnode = newName( name ); |
---|
951 | newnode->type = maybeClone( type ); |
---|
952 | newnode->copySpecifiers( this ); |
---|
953 | return newnode; |
---|
954 | } |
---|
955 | |
---|
956 | DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) { |
---|
957 | if ( ! o ) return nullptr; |
---|
958 | |
---|
959 | o->copySpecifiers( this ); |
---|
960 | if ( type ) { |
---|
961 | TypeData * srcType = type; |
---|
962 | |
---|
963 | // search for the base type by scanning off pointers and array designators |
---|
964 | while ( srcType->base ) { |
---|
965 | srcType = srcType->base; |
---|
966 | } // while |
---|
967 | |
---|
968 | TypeData * newType = srcType->clone(); |
---|
969 | if ( newType->kind == TypeData::AggregateInst ) { |
---|
970 | // don't duplicate members |
---|
971 | if ( newType->aggInst.aggregate->kind == TypeData::Enum ) { |
---|
972 | delete newType->aggInst.aggregate->enumeration.constants; |
---|
973 | newType->aggInst.aggregate->enumeration.constants = nullptr; |
---|
974 | newType->aggInst.aggregate->enumeration.body = false; |
---|
975 | } else { |
---|
976 | assert( newType->aggInst.aggregate->kind == TypeData::Aggregate ); |
---|
977 | delete newType->aggInst.aggregate->aggregate.fields; |
---|
978 | newType->aggInst.aggregate->aggregate.fields = nullptr; |
---|
979 | newType->aggInst.aggregate->aggregate.body = false; |
---|
980 | } // if |
---|
981 | // don't hoist twice |
---|
982 | newType->aggInst.hoistType = false; |
---|
983 | } // if |
---|
984 | |
---|
985 | newType->forall = maybeClone( type->forall ); |
---|
986 | if ( ! o->type ) { |
---|
987 | o->type = newType; |
---|
988 | } else { |
---|
989 | addTypeToType( newType, o->type ); |
---|
990 | delete newType; |
---|
991 | } // if |
---|
992 | } // if |
---|
993 | return o; |
---|
994 | } |
---|
995 | |
---|
996 | DeclarationNode * DeclarationNode::extractAggregate() const { |
---|
997 | if ( type ) { |
---|
998 | TypeData * ret = typeextractAggregate( type ); |
---|
999 | if ( ret ) { |
---|
1000 | DeclarationNode * newnode = new DeclarationNode; |
---|
1001 | newnode->type = ret; |
---|
1002 | return newnode; |
---|
1003 | } // if |
---|
1004 | } // if |
---|
1005 | return nullptr; |
---|
1006 | } |
---|
1007 | |
---|
1008 | // If a typedef wraps an anonymous declaration, name the inner declaration so it has a consistent name across |
---|
1009 | // translation units. |
---|
1010 | static void nameTypedefedDecl( |
---|
1011 | DeclarationNode * innerDecl, |
---|
1012 | const DeclarationNode * outerDecl ) { |
---|
1013 | TypeData * outer = outerDecl->type; |
---|
1014 | assert( outer ); |
---|
1015 | // First make sure this is a typedef: |
---|
1016 | if ( outer->kind != TypeData::Symbolic || !outer->symbolic.isTypedef ) { |
---|
1017 | return; |
---|
1018 | } |
---|
1019 | TypeData * inner = innerDecl->type; |
---|
1020 | assert( inner ); |
---|
1021 | // Always clear any CVs associated with the aggregate: |
---|
1022 | inner->qualifiers.reset(); |
---|
1023 | // Handle anonymous aggregates: typedef struct { int i; } foo |
---|
1024 | if ( inner->kind == TypeData::Aggregate && inner->aggregate.anon ) { |
---|
1025 | delete inner->aggregate.name; |
---|
1026 | inner->aggregate.name = new string( "__anonymous_" + *outerDecl->name ); |
---|
1027 | inner->aggregate.anon = false; |
---|
1028 | assert( outer->base ); |
---|
1029 | delete outer->base->aggInst.aggregate->aggregate.name; |
---|
1030 | outer->base->aggInst.aggregate->aggregate.name = new string( "__anonymous_" + *outerDecl->name ); |
---|
1031 | outer->base->aggInst.aggregate->aggregate.anon = false; |
---|
1032 | outer->base->aggInst.aggregate->qualifiers.reset(); |
---|
1033 | // Handle anonymous enumeration: typedef enum { A, B, C } foo |
---|
1034 | } else if ( inner->kind == TypeData::Enum && inner->enumeration.anon ) { |
---|
1035 | delete inner->enumeration.name; |
---|
1036 | inner->enumeration.name = new string( "__anonymous_" + *outerDecl->name ); |
---|
1037 | inner->enumeration.anon = false; |
---|
1038 | assert( outer->base ); |
---|
1039 | delete outer->base->aggInst.aggregate->enumeration.name; |
---|
1040 | outer->base->aggInst.aggregate->enumeration.name = new string( "__anonymous_" + *outerDecl->name ); |
---|
1041 | outer->base->aggInst.aggregate->enumeration.anon = false; |
---|
1042 | // No qualifiers.reset() here. |
---|
1043 | } |
---|
1044 | } |
---|
1045 | |
---|
1046 | // This code handles a special issue with the attribute transparent_union. |
---|
1047 | // |
---|
1048 | // typedef union U { int i; } typedef_name __attribute__(( aligned(16) )) __attribute__(( transparent_union )) |
---|
1049 | // |
---|
1050 | // Here the attribute aligned goes with the typedef_name, so variables declared of this type are |
---|
1051 | // aligned. However, the attribute transparent_union must be moved from the typedef_name to |
---|
1052 | // alias union U. Currently, this is the only know attribute that must be moved from typedef to |
---|
1053 | // alias. |
---|
1054 | static void moveUnionAttribute( ast::Decl * decl, ast::UnionDecl * unionDecl ) { |
---|
1055 | if ( auto typedefDecl = dynamic_cast<ast::TypedefDecl *>( decl ) ) { |
---|
1056 | // Is the typedef alias a union aggregate? |
---|
1057 | if ( nullptr == unionDecl ) return; |
---|
1058 | |
---|
1059 | // If typedef is an alias for a union, then its alias type was hoisted above and remembered. |
---|
1060 | if ( auto unionInstType = typedefDecl->base.as<ast::UnionInstType>() ) { |
---|
1061 | auto instType = ast::mutate( unionInstType ); |
---|
1062 | // Remove all transparent_union attributes from typedef and move to alias union. |
---|
1063 | for ( auto attr = instType->attributes.begin() ; attr != instType->attributes.end() ; ) { |
---|
1064 | assert( *attr ); |
---|
1065 | if ( (*attr)->name == "transparent_union" || (*attr)->name == "__transparent_union__" ) { |
---|
1066 | unionDecl->attributes.emplace_back( attr->release() ); |
---|
1067 | attr = instType->attributes.erase( attr ); |
---|
1068 | } else { |
---|
1069 | attr++; |
---|
1070 | } |
---|
1071 | } |
---|
1072 | typedefDecl->base = instType; |
---|
1073 | } |
---|
1074 | } |
---|
1075 | } |
---|
1076 | |
---|
1077 | // Get the non-anonymous name of the instance type of the declaration, |
---|
1078 | // if one exists. |
---|
1079 | static const std::string * getInstTypeOfName( ast::Decl * decl ) { |
---|
1080 | if ( auto dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) { |
---|
1081 | if ( auto aggr = dynamic_cast<ast::BaseInstType const *>( dwt->get_type() ) ) { |
---|
1082 | if ( aggr->name.find("anonymous") == std::string::npos ) { |
---|
1083 | return &aggr->name; |
---|
1084 | } |
---|
1085 | } |
---|
1086 | } |
---|
1087 | return nullptr; |
---|
1088 | } |
---|
1089 | |
---|
1090 | void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::Decl>> & outputList ) { |
---|
1091 | SemanticErrorException errors; |
---|
1092 | std::back_insert_iterator<std::vector<ast::ptr<ast::Decl>>> out( outputList ); |
---|
1093 | |
---|
1094 | for ( const DeclarationNode * cur = firstNode ; cur ; cur = strict_next( cur ) ) { |
---|
1095 | try { |
---|
1096 | bool extracted_named = false; |
---|
1097 | ast::UnionDecl * unionDecl = nullptr; |
---|
1098 | |
---|
1099 | if ( DeclarationNode * extr = cur->extractAggregate() ) { |
---|
1100 | assert( cur->type ); |
---|
1101 | nameTypedefedDecl( extr, cur ); |
---|
1102 | |
---|
1103 | if ( ast::Decl * decl = extr->build() ) { |
---|
1104 | // Remember the declaration if it is a union aggregate ? |
---|
1105 | unionDecl = dynamic_cast<ast::UnionDecl *>( decl ); |
---|
1106 | |
---|
1107 | *out++ = decl; |
---|
1108 | |
---|
1109 | // need to remember the cases where a declaration contains an anonymous aggregate definition |
---|
1110 | assert( extr->type ); |
---|
1111 | if ( extr->type->kind == TypeData::Aggregate ) { |
---|
1112 | // typedef struct { int A } B is the only case? |
---|
1113 | extracted_named = !extr->type->aggregate.anon; |
---|
1114 | } else if ( extr->type->kind == TypeData::Enum ) { |
---|
1115 | // typedef enum { A } B is the only case? |
---|
1116 | extracted_named = !extr->type->enumeration.anon; |
---|
1117 | } else { |
---|
1118 | extracted_named = true; |
---|
1119 | } |
---|
1120 | } // if |
---|
1121 | delete extr; |
---|
1122 | } // if |
---|
1123 | |
---|
1124 | if ( ast::Decl * decl = cur->build() ) { |
---|
1125 | moveUnionAttribute( decl, unionDecl ); |
---|
1126 | |
---|
1127 | if ( "" == decl->name && !cur->get_inLine() ) { |
---|
1128 | // Don't include anonymous declaration for named aggregates, |
---|
1129 | // but do include them for anonymous aggregates, e.g.: |
---|
1130 | // struct S { |
---|
1131 | // struct T { int x; }; // no anonymous member |
---|
1132 | // struct { int y; }; // anonymous member |
---|
1133 | // struct T; // anonymous member |
---|
1134 | // }; |
---|
1135 | if ( extracted_named ) { |
---|
1136 | continue; |
---|
1137 | } |
---|
1138 | |
---|
1139 | if ( auto name = getInstTypeOfName( decl ) ) { |
---|
1140 | // Temporary: warn about anonymous member declarations of named types, since |
---|
1141 | // this conflicts with the syntax for the forward declaration of an anonymous type. |
---|
1142 | SemanticWarning( cur->location, Warning::AggrForwardDecl, name->c_str() ); |
---|
1143 | } |
---|
1144 | } // if |
---|
1145 | *out++ = decl; |
---|
1146 | } // if |
---|
1147 | } catch ( SemanticErrorException & e ) { |
---|
1148 | errors.append( e ); |
---|
1149 | } // try |
---|
1150 | } // for |
---|
1151 | |
---|
1152 | if ( ! errors.isEmpty() ) { |
---|
1153 | throw errors; |
---|
1154 | } // if |
---|
1155 | } // buildList |
---|
1156 | |
---|
1157 | // currently only builds assertions, function parameters, and return values |
---|
1158 | void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList ) { |
---|
1159 | SemanticErrorException errors; |
---|
1160 | std::back_insert_iterator<std::vector<ast::ptr<ast::DeclWithType>>> out( outputList ); |
---|
1161 | |
---|
1162 | for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur ) ) { |
---|
1163 | try { |
---|
1164 | ast::Decl * decl = cur->build(); |
---|
1165 | assertf( decl, "buildList: build for ast::DeclWithType." ); |
---|
1166 | if ( ast::DeclWithType * dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) { |
---|
1167 | dwt->location = cur->location; |
---|
1168 | *out++ = dwt; |
---|
1169 | } else if ( ast::StructDecl * agg = dynamic_cast<ast::StructDecl *>( decl ) ) { |
---|
1170 | // e.g., int foo(struct S) {} |
---|
1171 | auto inst = new ast::StructInstType( agg->name ); |
---|
1172 | auto obj = new ast::ObjectDecl( cur->location, "", inst ); |
---|
1173 | obj->linkage = linkage; |
---|
1174 | *out++ = obj; |
---|
1175 | delete agg; |
---|
1176 | } else if ( ast::UnionDecl * agg = dynamic_cast<ast::UnionDecl *>( decl ) ) { |
---|
1177 | // e.g., int foo(union U) {} |
---|
1178 | auto inst = new ast::UnionInstType( agg->name ); |
---|
1179 | auto obj = new ast::ObjectDecl( cur->location, |
---|
1180 | "", inst, nullptr, ast::Storage::Classes(), |
---|
1181 | linkage ); |
---|
1182 | *out++ = obj; |
---|
1183 | } else if ( ast::EnumDecl * agg = dynamic_cast<ast::EnumDecl *>( decl ) ) { |
---|
1184 | // e.g., int foo(enum E) {} |
---|
1185 | auto inst = new ast::EnumInstType( agg->name ); |
---|
1186 | auto obj = new ast::ObjectDecl( cur->location, |
---|
1187 | "", |
---|
1188 | inst, |
---|
1189 | nullptr, |
---|
1190 | ast::Storage::Classes(), |
---|
1191 | linkage |
---|
1192 | ); |
---|
1193 | *out++ = obj; |
---|
1194 | } else { |
---|
1195 | assertf( false, "buildList: Could not convert to ast::DeclWithType." ); |
---|
1196 | } // if |
---|
1197 | } catch ( SemanticErrorException & e ) { |
---|
1198 | errors.append( e ); |
---|
1199 | } // try |
---|
1200 | } // for |
---|
1201 | |
---|
1202 | if ( ! errors.isEmpty() ) { |
---|
1203 | throw errors; |
---|
1204 | } // if |
---|
1205 | } // buildList |
---|
1206 | |
---|
1207 | void buildTypeList( const DeclarationNode * firstNode, |
---|
1208 | std::vector<ast::ptr<ast::Type>> & outputList ) { |
---|
1209 | SemanticErrorException errors; |
---|
1210 | std::back_insert_iterator<std::vector<ast::ptr<ast::Type>>> out( outputList ); |
---|
1211 | |
---|
1212 | for ( const DeclarationNode * cur = firstNode ; cur ; cur = strict_next( cur ) ) { |
---|
1213 | try { |
---|
1214 | * out++ = cur->buildType(); |
---|
1215 | } catch ( SemanticErrorException & e ) { |
---|
1216 | errors.append( e ); |
---|
1217 | } // try |
---|
1218 | } // for |
---|
1219 | |
---|
1220 | if ( ! errors.isEmpty() ) { |
---|
1221 | throw errors; |
---|
1222 | } // if |
---|
1223 | } // buildTypeList |
---|
1224 | |
---|
1225 | ast::Decl * DeclarationNode::build() const { |
---|
1226 | if ( ! error.empty() ) SemanticError( this, error + " in declaration of " ); |
---|
1227 | |
---|
1228 | if ( asmStmt ) { |
---|
1229 | auto stmt = strict_dynamic_cast<ast::AsmStmt *>( asmStmt->build() ); |
---|
1230 | return new ast::AsmDecl( stmt->location, stmt ); |
---|
1231 | } // if |
---|
1232 | if ( directiveStmt ) { |
---|
1233 | auto stmt = strict_dynamic_cast<ast::DirectiveStmt *>( directiveStmt->build() ); |
---|
1234 | return new ast::DirectiveDecl( stmt->location, stmt ); |
---|
1235 | } // if |
---|
1236 | |
---|
1237 | if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) { |
---|
1238 | // otype is internally converted to dtype + otype parameters |
---|
1239 | static const ast::TypeDecl::Kind kindMap[] = { ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Ftype, ast::TypeDecl::Ttype, ast::TypeDecl::Dimension }; |
---|
1240 | static_assert( sizeof(kindMap) / sizeof(kindMap[0]) == ast::TypeDecl::NUMBER_OF_KINDS, "DeclarationNode::build: kindMap is out of sync." ); |
---|
1241 | assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." ); |
---|
1242 | ast::TypeDecl * ret = new ast::TypeDecl( location, |
---|
1243 | *name, |
---|
1244 | ast::Storage::Classes(), |
---|
1245 | (ast::Type *)nullptr, |
---|
1246 | kindMap[ variable.tyClass ], |
---|
1247 | variable.tyClass == ast::TypeDecl::Otype || variable.tyClass == ast::TypeDecl::DStype, |
---|
1248 | variable.initializer ? variable.initializer->buildType() : nullptr |
---|
1249 | ); |
---|
1250 | buildList( variable.assertions, ret->assertions ); |
---|
1251 | return ret; |
---|
1252 | } // if |
---|
1253 | |
---|
1254 | if ( type ) { |
---|
1255 | // Function specifiers can only appear on a function definition/declaration. |
---|
1256 | // |
---|
1257 | // inline _Noreturn int f(); // allowed |
---|
1258 | // inline _Noreturn int g( int i ); // allowed |
---|
1259 | // inline _Noreturn int i; // disallowed |
---|
1260 | if ( type->kind != TypeData::Function && funcSpecs.any() ) { |
---|
1261 | SemanticError( this, "invalid function specifier for " ); |
---|
1262 | } // if |
---|
1263 | // Forall qualifier can only appear on a function/aggregate definition/declaration. |
---|
1264 | // |
---|
1265 | // forall int f(); // allowed |
---|
1266 | // forall int g( int i ); // allowed |
---|
1267 | // forall int i; // disallowed |
---|
1268 | if ( type->kind != TypeData::Function && type->forall ) { |
---|
1269 | SemanticError( this, "invalid type qualifier for " ); |
---|
1270 | } // if |
---|
1271 | bool isDelete = initializer && initializer->get_isDelete(); |
---|
1272 | ast::Decl * decl = buildDecl( |
---|
1273 | type, |
---|
1274 | name ? *name : string( "" ), |
---|
1275 | storageClasses, |
---|
1276 | maybeBuild( bitfieldWidth ), |
---|
1277 | funcSpecs, |
---|
1278 | linkage, |
---|
1279 | asmName, |
---|
1280 | isDelete ? nullptr : maybeBuild( initializer ), |
---|
1281 | copy( attributes ) |
---|
1282 | )->set_extension( extension ); |
---|
1283 | if ( isDelete ) { |
---|
1284 | auto dwt = strict_dynamic_cast<ast::DeclWithType *>( decl ); |
---|
1285 | dwt->isDeleted = true; |
---|
1286 | } |
---|
1287 | return decl; |
---|
1288 | } // if |
---|
1289 | |
---|
1290 | if ( assert.condition ) { |
---|
1291 | auto cond = maybeBuild( assert.condition ); |
---|
1292 | auto msg = strict_dynamic_cast<ast::ConstantExpr *>( maybeCopy( assert.message ) ); |
---|
1293 | return new ast::StaticAssertDecl( location, cond, msg ); |
---|
1294 | } |
---|
1295 | |
---|
1296 | // SUE's cannot have function specifiers, either |
---|
1297 | // |
---|
1298 | // inline _Noreturn struct S { ... }; // disallowed |
---|
1299 | // inline _Noreturn enum E { ... }; // disallowed |
---|
1300 | if ( funcSpecs.any() ) { |
---|
1301 | SemanticError( this, "invalid function specifier for " ); |
---|
1302 | } // if |
---|
1303 | if ( enumInLine ) { |
---|
1304 | return new ast::InlineMemberDecl( location, |
---|
1305 | *name, (ast::Type*)nullptr, storageClasses, linkage ); |
---|
1306 | } // if |
---|
1307 | assertf( name, "ObjectDecl must a have name\n" ); |
---|
1308 | auto ret = new ast::ObjectDecl( location, |
---|
1309 | *name, |
---|
1310 | (ast::Type*)nullptr, |
---|
1311 | maybeBuild( initializer ), |
---|
1312 | storageClasses, |
---|
1313 | linkage, |
---|
1314 | maybeBuild( bitfieldWidth ) |
---|
1315 | ); |
---|
1316 | ret->asmName = asmName; |
---|
1317 | ret->extension = extension; |
---|
1318 | return ret; |
---|
1319 | } |
---|
1320 | |
---|
1321 | ast::Type * DeclarationNode::buildType() const { |
---|
1322 | assert( type ); |
---|
1323 | |
---|
1324 | switch ( type->kind ) { |
---|
1325 | case TypeData::Enum: |
---|
1326 | case TypeData::Aggregate: { |
---|
1327 | ast::BaseInstType * ret = |
---|
1328 | buildComAggInst( type, copy( attributes ), linkage ); |
---|
1329 | buildList( type->aggregate.actuals, ret->params ); |
---|
1330 | return ret; |
---|
1331 | } |
---|
1332 | case TypeData::Symbolic: { |
---|
1333 | ast::TypeInstType * ret = new ast::TypeInstType( |
---|
1334 | *type->symbolic.name, |
---|
1335 | // This is just a default, the true value is not known yet. |
---|
1336 | ast::TypeDecl::Dtype, |
---|
1337 | buildQualifiers( type ), |
---|
1338 | copy( attributes ) ); |
---|
1339 | buildList( type->symbolic.actuals, ret->params ); |
---|
1340 | return ret; |
---|
1341 | } |
---|
1342 | default: |
---|
1343 | ast::Type * simpletypes = typebuild( type ); |
---|
1344 | // copy because member is const |
---|
1345 | simpletypes->attributes = attributes; |
---|
1346 | return simpletypes; |
---|
1347 | } // switch |
---|
1348 | } |
---|
1349 | |
---|
1350 | // Local Variables: // |
---|
1351 | // tab-width: 4 // |
---|
1352 | // mode: c++ // |
---|
1353 | // compile-command: "make install" // |
---|
1354 | // End: // |
---|