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