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