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