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