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