source: src/Parser/DeclarationNode.cpp@ 4d5c5b6a

Last change on this file since 4d5c5b6a was 4175659, checked in by Peter A. Buhr <pabuhr@…>, 10 months ago

remove BadQualifiersZeroOne warning message

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