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