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