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