source: src/Parser/DeclarationNode.cc@ 00eaeb8

Last change on this file since 00eaeb8 was 4eb3a7c5, checked in by Peter A. Buhr <pabuhr@…>, 21 months ago

first attempt at correct distribution of attributes for aggregates

  • Property mode set to 100644
File size: 44.8 KB
RevLine 
[b87a5ed]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//
[974906e2]7// DeclarationNode.cc --
[b87a5ed]8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 12:34:05 2015
[b38f6da]11// Last Modified By : Peter A. Buhr
[4eb3a7c5]12// Last Modified On : Fri Feb 23 18:25:57 2024
13// Update Count : 1533
[b87a5ed]14//
15
[c468150]16#include "DeclarationNode.h"
17
[e3e16bc]18#include <cassert> // for assert, assertf, strict_dynamic_cast
[d180746]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...
[51b73452]24
[bb7422a]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
[d180746]35#include "Common/SemanticError.h" // for SemanticError
36#include "Common/UniqueName.h" // for UniqueName
[5bf685f]37#include "Common/utility.h" // for copy, spliceBegin
[c468150]38#include "Parser/ExpressionNode.h" // for ExpressionNode
39#include "Parser/InitializerNode.h"// for InitializerNode
40#include "Parser/StatementNode.h" // for StatementNode
[d180746]41#include "TypeData.h" // for TypeData, TypeData::Aggregate_t
[2f0a0678]42#include "TypedefTable.h" // for TypedefTable
[bdd516a]43
[de62360d]44extern TypedefTable typedefTable;
45
[51b73452]46using namespace std;
47
[201aeb9]48// These must harmonize with the corresponding DeclarationNode enumerations.
[702e826]49const 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};
54const char * DeclarationNode::complexTypeNames[] = {
55 "_Complex", "NoComplexTypeNames", "_Imaginary"
56}; // Imaginary unsupported => parse, but make invisible and print error message
57const char * DeclarationNode::signednessNames[] = {
58 "signed", "unsigned", "NoSignednessNames"
59};
60const char * DeclarationNode::lengthNames[] = {
61 "short", "long", "long long", "NoLengthNames"
62};
63const char * DeclarationNode::builtinTypeNames[] = {
64 "__builtin_va_list", "__auto_type", "zero_t", "one_t", "NoBuiltinTypeNames"
65};
[51b73452]66
67UniqueName DeclarationNode::anonymous( "__anonymous" );
68
[bb7422a]69extern ast::Linkage::Spec linkage; // defined in parser.yy
[51b73452]70
[7d05e7e]71DeclarationNode::DeclarationNode() :
[e07caa2]72 linkage( ::linkage ) {
[2298f728]73
[faddbd8]74// variable.name = nullptr;
[bb7422a]75 variable.tyClass = ast::TypeDecl::NUMBER_OF_KINDS;
[28307be]76 variable.assertions = nullptr;
[67cf18c]77 variable.initializer = nullptr;
[7d05e7e]78
[f6e3e34]79 assert.condition = nullptr;
80 assert.message = nullptr;
[28307be]81}
82
83DeclarationNode::~DeclarationNode() {
[faddbd8]84// delete variable.name;
[2298f728]85 delete variable.assertions;
[67cf18c]86 delete variable.initializer;
[2298f728]87
[702e826]88// delete type;
[28307be]89 delete bitfieldWidth;
[e994912]90
91 delete asmStmt;
[58dd019]92 // asmName, no delete, passed to next stage
[28307be]93 delete initializer;
[f6e3e34]94
95 delete assert.condition;
96 delete assert.message;
[28307be]97}
98
[ba7aa2d]99DeclarationNode * DeclarationNode::clone() const {
100 DeclarationNode * newnode = new DeclarationNode;
[5bf685f]101 newnode->set_next( maybeCopy( get_next() ) );
[2298f728]102 newnode->name = name ? new string( *name ) : nullptr;
[c0aa336]103
[2e5fa345]104 newnode->builtin = NoBuiltinType;
[5bf685f]105 newnode->type = maybeCopy( type );
[679e644]106 newnode->inLine = inLine;
[a7c90d4]107 newnode->storageClasses = storageClasses;
108 newnode->funcSpecs = funcSpecs;
[5bf685f]109 newnode->bitfieldWidth = maybeCopy( bitfieldWidth );
110 newnode->enumeratorValue.reset( maybeCopy( enumeratorValue.get() ) );
[b87a5ed]111 newnode->hasEllipsis = hasEllipsis;
112 newnode->linkage = linkage;
[bb7422a]113 newnode->asmName = maybeCopy( asmName );
114 newnode->attributes = attributes;
[5bf685f]115 newnode->initializer = maybeCopy( initializer );
[c0aa336]116 newnode->extension = extension;
[5bf685f]117 newnode->asmStmt = maybeCopy( asmStmt );
[c0aa336]118 newnode->error = error;
[3848e0e]119
[faddbd8]120// newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
[28307be]121 newnode->variable.tyClass = variable.tyClass;
[5bf685f]122 newnode->variable.assertions = maybeCopy( variable.assertions );
123 newnode->variable.initializer = maybeCopy( variable.initializer );
[3848e0e]124
[5bf685f]125 newnode->assert.condition = maybeCopy( assert.condition );
[bb7422a]126 newnode->assert.message = maybeCopy( assert.message );
[28307be]127 return newnode;
128} // DeclarationNode::clone
[3848e0e]129
[f2f512ba]130void DeclarationNode::print( std::ostream & os, int indent ) const {
[59db689]131 os << string( indent, ' ' );
[2298f728]132 if ( name ) {
133 os << *name << ": ";
[68cd1ce]134 } // if
[51b73452]135
[bb7422a]136 if ( linkage != ast::Linkage::Cforall ) {
137 os << ast::Linkage::name( linkage ) << " ";
[68cd1ce]138 } // if
[3848e0e]139
[bb7422a]140 ast::print( os, storageClasses );
141 ast::print( os, funcSpecs );
[dd020c0]142
[b87a5ed]143 if ( type ) {
144 type->print( os, indent );
145 } else {
146 os << "untyped entity ";
[68cd1ce]147 } // if
[3848e0e]148
[b87a5ed]149 if ( bitfieldWidth ) {
[59db689]150 os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
[b87a5ed]151 bitfieldWidth->printOneLine( os );
[68cd1ce]152 } // if
[3848e0e]153
[2298f728]154 if ( initializer ) {
[59db689]155 os << endl << string( indent + 2, ' ' ) << "with initializer ";
[b87a5ed]156 initializer->printOneLine( os );
[974906e2]157 os << " maybe constructed? " << initializer->get_maybeConstructed();
[68cd1ce]158 } // if
[3848e0e]159
[692c1cc]160 if ( ! attributes.empty() ) {
[4eb3a7c5]161 os << string( indent + 2, ' ' ) << "with attributes" << endl;
[bb7422a]162 for ( ast::ptr<ast::Attribute> const & attr : reverseIterate( attributes ) ) {
[4eb3a7c5]163 os << string( indent + 4, ' ' );
164 ast::print( os, attr, indent + 2 );
[692c1cc]165 } // for
166 } // if
[66406f3]167
[b87a5ed]168 os << endl;
[51b73452]169}
170
[f2f512ba]171void DeclarationNode::printList( std::ostream & os, int indent ) const {
[b87a5ed]172 ParseNode::printList( os, indent );
173 if ( hasEllipsis ) {
174 os << string( indent, ' ' ) << "and a variable number of other arguments" << endl;
[68cd1ce]175 } // if
[51b73452]176}
177
[bb7422a]178DeclarationNode * DeclarationNode::newStorageClass( ast::Storage::Classes sc ) {
[ba7aa2d]179 DeclarationNode * newnode = new DeclarationNode;
[08d5507b]180 newnode->storageClasses = sc;
[b87a5ed]181 return newnode;
[dd020c0]182} // DeclarationNode::newStorageClass
[3848e0e]183
[bb7422a]184DeclarationNode * DeclarationNode::newFuncSpecifier( ast::Function::Specs fs ) {
[ba7aa2d]185 DeclarationNode * newnode = new DeclarationNode;
[08d5507b]186 newnode->funcSpecs = fs;
[c1c1112]187 return newnode;
[dd020c0]188} // DeclarationNode::newFuncSpecifier
[c1c1112]189
[bb7422a]190DeclarationNode * DeclarationNode::newTypeQualifier( ast::CV::Qualifiers tq ) {
[ba7aa2d]191 DeclarationNode * newnode = new DeclarationNode;
[dd020c0]192 newnode->type = new TypeData();
[738e304]193 newnode->type->qualifiers = tq;
[b87a5ed]194 return newnode;
[dd020c0]195} // DeclarationNode::newQualifier
[3848e0e]196
[c1c1112]197DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
[ba7aa2d]198 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]199 newnode->type = new TypeData( TypeData::Basic );
[5b639ee]200 newnode->type->basictype = bt;
[b87a5ed]201 return newnode;
[984dce6]202} // DeclarationNode::newBasicType
[3848e0e]203
[5b639ee]204DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
[ba7aa2d]205 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]206 newnode->type = new TypeData( TypeData::Basic );
[5b639ee]207 newnode->type->complextype = ct;
[b87a5ed]208 return newnode;
[5b639ee]209} // DeclarationNode::newComplexType
210
211DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
[ba7aa2d]212 DeclarationNode * newnode = new DeclarationNode;
[5b639ee]213 newnode->type = new TypeData( TypeData::Basic );
214 newnode->type->signedness = sn;
215 return newnode;
216} // DeclarationNode::newSignedNess
217
218DeclarationNode * DeclarationNode::newLength( Length lnth ) {
[ba7aa2d]219 DeclarationNode * newnode = new DeclarationNode;
[5b639ee]220 newnode->type = new TypeData( TypeData::Basic );
221 newnode->type->length = lnth;
222 return newnode;
223} // DeclarationNode::newLength
[3848e0e]224
[dd020c0]225DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
226 DeclarationNode * newnode = new DeclarationNode;
227 newnode->type = new TypeData( TypeData::Unknown );
228 newnode->type->forall = forall;
229 return newnode;
230} // DeclarationNode::newForall
231
[47498bd]232DeclarationNode * DeclarationNode::newFromGlobalScope() {
233 DeclarationNode * newnode = new DeclarationNode;
234 newnode->type = new TypeData( TypeData::GlobalScope );
235 return newnode;
236}
237
[c5d7701]238DeclarationNode * DeclarationNode::newQualifiedType( DeclarationNode * parent, DeclarationNode * child) {
[c194661]239 DeclarationNode * newnode = new DeclarationNode;
240 newnode->type = new TypeData( TypeData::Qualified );
241 newnode->type->qualified.parent = parent->type;
242 newnode->type->qualified.child = child->type;
243 parent->type = nullptr;
244 child->type = nullptr;
245 delete parent;
246 delete child;
247 return newnode;
[c5d7701]248}
249
[bb7422a]250DeclarationNode * DeclarationNode::newAggregate( ast::AggregateDecl::Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
[ba7aa2d]251 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]252 newnode->type = new TypeData( TypeData::Aggregate );
[8f6f47d7]253 newnode->type->aggregate.kind = kind;
[692c1cc]254 newnode->type->aggregate.anon = name == nullptr;
255 newnode->type->aggregate.name = newnode->type->aggregate.anon ? new string( DeclarationNode::anonymous.newName() ) : name;
[8f6f47d7]256 newnode->type->aggregate.actuals = actuals;
257 newnode->type->aggregate.fields = fields;
258 newnode->type->aggregate.body = body;
[6ea87486]259 newnode->type->aggregate.tagged = false;
260 newnode->type->aggregate.parent = nullptr;
[b87a5ed]261 return newnode;
[984dce6]262} // DeclarationNode::newAggregate
[3848e0e]263
[e4d7c1c]264DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body, bool typed, DeclarationNode * base, EnumHiding hiding ) {
[ba7aa2d]265 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]266 newnode->type = new TypeData( TypeData::Enum );
[692c1cc]267 newnode->type->enumeration.anon = name == nullptr;
268 newnode->type->enumeration.name = newnode->type->enumeration.anon ? new string( DeclarationNode::anonymous.newName() ) : name;
[8f6f47d7]269 newnode->type->enumeration.constants = constants;
[ca1a547]270 newnode->type->enumeration.body = body;
[b0d9ff7]271 newnode->type->enumeration.typed = typed;
[e4d7c1c]272 newnode->type->enumeration.hiding = hiding;
[78e2fca]273 if ( base && base->type ) {
[ed9a1ae]274 newnode->type->base = base->type;
[9e7236f4]275 } // if
276
[b87a5ed]277 return newnode;
[984dce6]278} // DeclarationNode::newEnum
[3848e0e]279
[a46b69c]280DeclarationNode * DeclarationNode::newName( const string * name ) {
[ba7aa2d]281 DeclarationNode * newnode = new DeclarationNode;
[a46b69c]282 assert( ! newnode->name );
[2298f728]283 newnode->name = name;
[a46b69c]284 return newnode;
285} // DeclarationNode::newName
286
[374cb117]287DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) {
[a46b69c]288 DeclarationNode * newnode = newName( name );
[4f147cc]289 newnode->enumeratorValue.reset( constant );
[b87a5ed]290 return newnode;
[984dce6]291} // DeclarationNode::newEnumConstant
[3848e0e]292
[374cb117]293DeclarationNode * DeclarationNode::newEnumValueGeneric( const string * name, InitializerNode * init ) {
[b0d9ff7]294 if ( init ) {
295 if ( init->get_expression() ) {
[374cb117]296 return newEnumConstant( name, init->get_expression() );
[b0d9ff7]297 } else {
[374cb117]298 DeclarationNode * newnode = newName( name );
299 newnode->initializer = init;
300 return newnode;
301 } // if
302 } else {
[b0d9ff7]303 return newName( name );
[374cb117]304 } // if
[9e7236f4]305} // DeclarationNode::newEnumValueGeneric
[374cb117]306
[1e30df7]307DeclarationNode * DeclarationNode::newEnumInLine( const string name ) {
308 DeclarationNode * newnode = newName( new std::string(name) );
309 newnode->enumInLine = true;
310 return newnode;
311}
312
[a46b69c]313DeclarationNode * DeclarationNode::newFromTypedef( const string * name ) {
[ba7aa2d]314 DeclarationNode * newnode = new DeclarationNode;
[a46b69c]315 newnode->type = new TypeData( TypeData::SymbolicInst );
316 newnode->type->symbolic.name = name;
317 newnode->type->symbolic.isTypedef = true;
318 newnode->type->symbolic.params = nullptr;
[b87a5ed]319 return newnode;
[a46b69c]320} // DeclarationNode::newFromTypedef
[3848e0e]321
[25bca42]322DeclarationNode * DeclarationNode::newFromTypeGen( const string * name, ExpressionNode * params ) {
[ba7aa2d]323 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]324 newnode->type = new TypeData( TypeData::SymbolicInst );
[fb114fa1]325 newnode->type->symbolic.name = name;
[8f6f47d7]326 newnode->type->symbolic.isTypedef = false;
327 newnode->type->symbolic.actuals = params;
[b87a5ed]328 return newnode;
[984dce6]329} // DeclarationNode::newFromTypeGen
[3848e0e]330
[bb7422a]331DeclarationNode * DeclarationNode::newTypeParam( ast::TypeDecl::Kind tc, const string * name ) {
[a46b69c]332 DeclarationNode * newnode = newName( name );
[2298f728]333 newnode->type = nullptr;
[28307be]334 newnode->variable.tyClass = tc;
[faddbd8]335 newnode->variable.assertions = nullptr;
[b87a5ed]336 return newnode;
[984dce6]337} // DeclarationNode::newTypeParam
[3848e0e]338
[fb114fa1]339DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
[ba7aa2d]340 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]341 newnode->type = new TypeData( TypeData::Aggregate );
[2298f728]342 newnode->type->aggregate.name = name;
[bb7422a]343 newnode->type->aggregate.kind = ast::AggregateDecl::Trait;
[8f6f47d7]344 newnode->type->aggregate.params = params;
345 newnode->type->aggregate.fields = asserts;
[b87a5ed]346 return newnode;
[984dce6]347} // DeclarationNode::newTrait
[3848e0e]348
[fb114fa1]349DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
[ba7aa2d]350 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]351 newnode->type = new TypeData( TypeData::AggregateInst );
[8f6f47d7]352 newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
[bb7422a]353 newnode->type->aggInst.aggregate->aggregate.kind = ast::AggregateDecl::Trait;
[2298f728]354 newnode->type->aggInst.aggregate->aggregate.name = name;
[8f6f47d7]355 newnode->type->aggInst.params = params;
[b87a5ed]356 return newnode;
[984dce6]357} // DeclarationNode::newTraitUse
[3848e0e]358
[25bca42]359DeclarationNode * DeclarationNode::newTypeDecl( const string * name, DeclarationNode * typeParams ) {
[a46b69c]360 DeclarationNode * newnode = newName( name );
[b87a5ed]361 newnode->type = new TypeData( TypeData::Symbolic );
[8f6f47d7]362 newnode->type->symbolic.isTypedef = false;
363 newnode->type->symbolic.params = typeParams;
[b87a5ed]364 return newnode;
[984dce6]365} // DeclarationNode::newTypeDecl
[3848e0e]366
[ce8c12f]367DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers, OperKinds kind ) {
[ba7aa2d]368 DeclarationNode * newnode = new DeclarationNode;
[ce8c12f]369 newnode->type = new TypeData( kind == OperKinds::PointTo ? TypeData::Pointer : TypeData::Reference );
[71d0eab]370 if ( kind == OperKinds::And ) {
371 // T && is parsed as 'And' operator rather than two references => add a second reference type
372 TypeData * td = new TypeData( TypeData::Reference );
373 td->base = newnode->type;
374 newnode->type = td;
375 }
[c3396e0]376 if ( qualifiers ) {
377 return newnode->addQualifiers( qualifiers );
378 } else {
379 return newnode;
380 } // if
[984dce6]381} // DeclarationNode::newPointer
[3848e0e]382
[ba7aa2d]383DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
384 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]385 newnode->type = new TypeData( TypeData::Array );
[8f6f47d7]386 newnode->type->array.dimension = size;
387 newnode->type->array.isStatic = isStatic;
[bb7422a]388 if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ast::ConstantExpr *>() ) {
[8f6f47d7]389 newnode->type->array.isVarLen = false;
[71bd8c6]390 } else {
[8f6f47d7]391 newnode->type->array.isVarLen = true;
[71bd8c6]392 } // if
[b87a5ed]393 return newnode->addQualifiers( qualifiers );
[984dce6]394} // DeclarationNode::newArray
[3848e0e]395
[ba7aa2d]396DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
397 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]398 newnode->type = new TypeData( TypeData::Array );
[2298f728]399 newnode->type->array.dimension = nullptr;
[8f6f47d7]400 newnode->type->array.isStatic = false;
401 newnode->type->array.isVarLen = true;
[b87a5ed]402 return newnode->addQualifiers( qualifiers );
[3848e0e]403}
404
[ba7aa2d]405DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
406 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]407 newnode->bitfieldWidth = size;
408 return newnode;
[3848e0e]409}
410
[ba7aa2d]411DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
412 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]413 newnode->type = new TypeData( TypeData::Tuple );
[8f6f47d7]414 newnode->type->tuple = members;
[b87a5ed]415 return newnode;
[3848e0e]416}
417
[f855545]418DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr, bool basetypeof ) {
[ba7aa2d]419 DeclarationNode * newnode = new DeclarationNode;
[f855545]420 newnode->type = new TypeData( basetypeof ? TypeData::Basetypeof : TypeData::Typeof );
[8f6f47d7]421 newnode->type->typeexpr = expr;
[b87a5ed]422 return newnode;
[3848e0e]423}
424
[93bbbc4]425DeclarationNode * DeclarationNode::newVtableType( DeclarationNode * decl ) {
426 DeclarationNode * newnode = new DeclarationNode;
427 newnode->type = new TypeData( TypeData::Vtable );
428 newnode->setBase( decl->type );
429 return newnode;
430}
431
[8f6f47d7]432DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
[ba7aa2d]433 DeclarationNode * newnode = new DeclarationNode;
[8f6f47d7]434 newnode->type = new TypeData( TypeData::Builtin );
435 newnode->builtin = bt;
[148f7290]436 newnode->type->builtintype = newnode->builtin;
[8f6f47d7]437 return newnode;
438} // DeclarationNode::newBuiltinType
439
[a46b69c]440DeclarationNode * DeclarationNode::newFunction( const string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ) {
441 DeclarationNode * newnode = newName( name );
442 newnode->type = new TypeData( TypeData::Function );
443 newnode->type->function.params = param;
444 newnode->type->function.body = body;
445
446 if ( ret ) {
447 newnode->type->base = ret->type;
448 ret->type = nullptr;
449 delete ret;
450 } // if
451
452 return newnode;
453} // DeclarationNode::newFunction
454
[25bca42]455DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) {
[44a81853]456 DeclarationNode * newnode = new DeclarationNode;
457 newnode->type = nullptr;
[bb7422a]458 std::vector<ast::ptr<ast::Expr>> exprs;
[44a81853]459 buildList( expr, exprs );
[b38f6da]460 newnode->attributes.push_back( new ast::Attribute( *name, std::move( exprs ) ) );
[44a81853]461 delete name;
462 return newnode;
463}
464
[2d019af]465DeclarationNode * DeclarationNode::newDirectiveStmt( StatementNode * stmt ) {
466 DeclarationNode * newnode = new DeclarationNode;
467 newnode->directiveStmt = stmt;
468 return newnode;
469}
470
[e994912]471DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
472 DeclarationNode * newnode = new DeclarationNode;
473 newnode->asmStmt = stmt;
474 return newnode;
475}
476
[bb7422a]477DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, ast::Expr * message ) {
[f6e3e34]478 DeclarationNode * newnode = new DeclarationNode;
479 newnode->assert.condition = condition;
480 newnode->assert.message = message;
481 return newnode;
482}
483
[bb7422a]484static void appendError( string & dst, const string & src ) {
[5b639ee]485 if ( src.empty() ) return;
486 if ( dst.empty() ) { dst = src; return; }
487 dst += ", " + src;
488} // appendError
489
[ba7aa2d]490void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
[bb7422a]491 const ast::CV::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
492 const ast::CV::Qualifiers duplicates = qsrc & qdst;
493
494 if ( duplicates.any() ) {
495 std::stringstream str;
496 str << "duplicate ";
497 ast::print( str, duplicates );
498 str << "qualifier(s)";
499 appendError( error, str.str() );
[a7c90d4]500 } // for
[c1c1112]501} // DeclarationNode::checkQualifiers
502
[a7c90d4]503void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
[bb7422a]504 ast::Function::Specs fsDups = funcSpecs & src->funcSpecs;
505 if ( fsDups.any() ) {
506 std::stringstream str;
507 str << "duplicate ";
508 ast::print( str, fsDups );
509 str << "function specifier(s)";
510 appendError( error, str.str() );
[dd020c0]511 } // if
512
[bb7422a]513 // Skip if everything is unset.
514 if ( storageClasses.any() && src->storageClasses.any() ) {
515 ast::Storage::Classes dups = storageClasses & src->storageClasses;
516 // Check for duplicates.
517 if ( dups.any() ) {
518 std::stringstream str;
519 str << "duplicate ";
520 ast::print( str, dups );
521 str << "storage class(es)";
522 appendError( error, str.str() );
523 // Check for conflicts.
524 } else if ( !src->storageClasses.is_threadlocal_any() ) {
525 std::stringstream str;
526 str << "conflicting ";
527 ast::print( str, ast::Storage::Classes( 1 << storageClasses.ffs() ) );
528 str << "& ";
529 ast::print( str, ast::Storage::Classes( 1 << src->storageClasses.ffs() ) );
530 str << "storage classes";
531 appendError( error, str.str() );
532 // FIX to preserve invariant of one basic storage specifier
533 src->storageClasses.reset();
534 }
[b6424d9]535 } // if
[dd020c0]536
[a7c90d4]537 appendError( error, src->error );
538} // DeclarationNode::checkSpecifiers
[b6424d9]539
[4eb3a7c5]540DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q, bool copyattr ) {
[6f95000]541 funcSpecs |= q->funcSpecs;
542 storageClasses |= q->storageClasses;
[c0aa336]543
[4eb3a7c5]544 if ( copyattr ) {
545 std::vector<ast::ptr<ast::Attribute>> tmp;
546 tmp.reserve( q->attributes.size() );
547 for ( auto const & attr : q->attributes ) {
548 tmp.emplace_back( ast::shallowCopy( attr.get() ) );
549 } // for
550 spliceBegin( attributes, tmp );
551 } // if
[bb7422a]552
[b6424d9]553 return this;
[a7c90d4]554} // DeclarationNode::copySpecifiers
[b6424d9]555
[f2f512ba]556static void addQualifiersToType( TypeData *& src, TypeData * dst ) {
[101e0bd]557 if ( dst->base ) {
558 addQualifiersToType( src, dst->base );
559 } else if ( dst->kind == TypeData::Function ) {
560 dst->base = src;
[2298f728]561 src = nullptr;
[101e0bd]562 } else {
[6f95000]563 dst->qualifiers |= src->qualifiers;
[101e0bd]564 } // if
565} // addQualifiersToType
566
[ba7aa2d]567DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
[af9da5f]568 if ( ! q ) { return this; } // empty qualifier
[101e0bd]569
[a7c90d4]570 checkSpecifiers( q );
571 copySpecifiers( q );
[101e0bd]572
[c38ae92]573 if ( ! q->type ) { delete q; return this; }
[101e0bd]574
575 if ( ! type ) {
[c38ae92]576 type = q->type; // reuse structure
[1b772749]577 q->type = nullptr;
578 delete q;
[101e0bd]579 return this;
580 } // if
581
[c38ae92]582 if ( q->type->forall ) { // forall qualifier ?
583 if ( type->forall ) { // polymorphic routine ?
584 type->forall->appendList( q->type->forall ); // augment forall qualifier
[101e0bd]585 } else {
[c38ae92]586 if ( type->kind == TypeData::Aggregate ) { // struct/union ?
587 if ( type->aggregate.params ) { // polymorphic ?
588 type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier
589 } else { // not polymorphic
[a1c9ddd]590 type->aggregate.params = q->type->forall; // set forall qualifier
[c38ae92]591 } // if
592 } else { // not polymorphic
593 type->forall = q->type->forall; // make polymorphic routine
[68cd1ce]594 } // if
595 } // if
[c38ae92]596 q->type->forall = nullptr; // forall qualifier moved
[68cd1ce]597 } // if
[6ef2d81]598
[a7c90d4]599 checkQualifiers( type, q->type );
[78e2fca]600 if ( (builtin == Zero || builtin == One) && q->type->qualifiers.any() && error.length() == 0 ) {
[be00a2d]601 SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, builtinTypeNames[builtin] );
[9dc31c10]602 } // if
[6ef2d81]603 addQualifiersToType( q->type, type );
604
[b87a5ed]605 delete q;
606 return this;
[101e0bd]607} // addQualifiers
[3848e0e]608
[f2f512ba]609static void addTypeToType( TypeData *& src, TypeData *& dst ) {
[101e0bd]610 if ( src->forall && dst->kind == TypeData::Function ) {
611 if ( dst->forall ) {
612 dst->forall->appendList( src->forall );
[b87a5ed]613 } else {
[101e0bd]614 dst->forall = src->forall;
615 } // if
[2298f728]616 src->forall = nullptr;
[101e0bd]617 } // if
618 if ( dst->base ) {
619 addTypeToType( src, dst->base );
620 } else {
621 switch ( dst->kind ) {
[0d0931d]622 case TypeData::Unknown:
[6f95000]623 src->qualifiers |= dst->qualifiers;
[101e0bd]624 dst = src;
[2298f728]625 src = nullptr;
[101e0bd]626 break;
[0d0931d]627 case TypeData::Basic:
[6f95000]628 dst->qualifiers |= src->qualifiers;
[101e0bd]629 if ( src->kind != TypeData::Unknown ) {
630 assert( src->kind == TypeData::Basic );
631
632 if ( dst->basictype == DeclarationNode::NoBasicType ) {
633 dst->basictype = src->basictype;
634 } else if ( src->basictype != DeclarationNode::NoBasicType )
[ca9d65e]635 SemanticError( yylloc, "multiple declaration types \"%s\" and \"%s\".",
636 DeclarationNode::basicTypeNames[ dst->basictype ],
637 DeclarationNode::basicTypeNames[ src->basictype ] );
[101e0bd]638 if ( dst->complextype == DeclarationNode::NoComplexType ) {
639 dst->complextype = src->complextype;
640 } else if ( src->complextype != DeclarationNode::NoComplexType )
[ca9d65e]641 SemanticError( yylloc, "multiple declaration types \"%s\" and \"%s\".",
642 DeclarationNode::complexTypeNames[ src->complextype ],
643 DeclarationNode::complexTypeNames[ src->complextype ] );
[101e0bd]644 if ( dst->signedness == DeclarationNode::NoSignedness ) {
645 dst->signedness = src->signedness;
646 } else if ( src->signedness != DeclarationNode::NoSignedness )
[ca9d65e]647 SemanticError( yylloc, "conflicting type specifier \"%s\" and \"%s\".",
648 DeclarationNode::signednessNames[ dst->signedness ],
649 DeclarationNode::signednessNames[ src->signedness ] );
[101e0bd]650 if ( dst->length == DeclarationNode::NoLength ) {
651 dst->length = src->length;
652 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
653 dst->length = DeclarationNode::LongLong;
654 } else if ( src->length != DeclarationNode::NoLength )
[ca9d65e]655 SemanticError( yylloc, "conflicting type specifier \"%s\" and \"%s\".",
656 DeclarationNode::lengthNames[ dst->length ],
657 DeclarationNode::lengthNames[ src->length ] );
[101e0bd]658 } // if
659 break;
[0d0931d]660 default:
[101e0bd]661 switch ( src->kind ) {
[0d0931d]662 case TypeData::Aggregate:
663 case TypeData::Enum:
[101e0bd]664 dst->base = new TypeData( TypeData::AggregateInst );
665 dst->base->aggInst.aggregate = src;
666 if ( src->kind == TypeData::Aggregate ) {
[5bf685f]667 dst->base->aggInst.params = maybeCopy( src->aggregate.actuals );
[68cd1ce]668 } // if
[6f95000]669 dst->base->qualifiers |= src->qualifiers;
[2298f728]670 src = nullptr;
[b87a5ed]671 break;
[0d0931d]672 default:
[101e0bd]673 if ( dst->forall ) {
674 dst->forall->appendList( src->forall );
675 } else {
676 dst->forall = src->forall;
677 } // if
[2298f728]678 src->forall = nullptr;
[101e0bd]679 dst->base = src;
[2298f728]680 src = nullptr;
[68cd1ce]681 } // switch
[101e0bd]682 } // switch
[68cd1ce]683 } // if
[3848e0e]684}
685
[4eb3a7c5]686DeclarationNode * DeclarationNode::addType( DeclarationNode * o, bool copyattr ) {
[b87a5ed]687 if ( o ) {
[a7c90d4]688 checkSpecifiers( o );
[4eb3a7c5]689 copySpecifiers( o, copyattr );
[b87a5ed]690 if ( o->type ) {
691 if ( ! type ) {
692 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
[4eb3a7c5]693 // Hide type information aggregate instances.
[b87a5ed]694 type = new TypeData( TypeData::AggregateInst );
[4eb3a7c5]695 type->aggInst.aggregate = o->type; // change ownership
696 type->aggInst.aggregate->aggregate.attributes.swap( o->attributes ); // change ownership
[b87a5ed]697 if ( o->type->kind == TypeData::Aggregate ) {
[43c89a7]698 type->aggInst.hoistType = o->type->aggregate.body;
[5bf685f]699 type->aggInst.params = maybeCopy( o->type->aggregate.actuals );
[43c89a7]700 } else {
701 type->aggInst.hoistType = o->type->enumeration.body;
[68cd1ce]702 } // if
[6f95000]703 type->qualifiers |= o->type->qualifiers;
[b87a5ed]704 } else {
705 type = o->type;
[68cd1ce]706 } // if
[4eb3a7c5]707 o->type = nullptr; // change ownership
[b87a5ed]708 } else {
709 addTypeToType( o->type, type );
[68cd1ce]710 } // if
711 } // if
[b87a5ed]712 if ( o->bitfieldWidth ) {
713 bitfieldWidth = o->bitfieldWidth;
[68cd1ce]714 } // if
[71bd8c6]715
716 // there may be typedefs chained onto the type
[1d4580a]717 if ( o->get_next() ) {
718 set_last( o->get_next()->clone() );
[1db21619]719 } // if
[68cd1ce]720 } // if
[b87a5ed]721 delete o;
[66406f3]722
[b87a5ed]723 return this;
[3848e0e]724}
725
[f135b50]726DeclarationNode * DeclarationNode::addEnumBase( DeclarationNode * o ) {
[b38f6da]727 if ( o && o->type) {
[f135b50]728 type->base= o->type;
[b38f6da]729 } // if
[f135b50]730 delete o;
731 return this;
732}
733
[ba7aa2d]734DeclarationNode * DeclarationNode::addTypedef() {
735 TypeData * newtype = new TypeData( TypeData::Symbolic );
[2298f728]736 newtype->symbolic.params = nullptr;
[8f6f47d7]737 newtype->symbolic.isTypedef = true;
[2298f728]738 newtype->symbolic.name = name ? new string( *name ) : nullptr;
[b87a5ed]739 newtype->base = type;
740 type = newtype;
741 return this;
[3848e0e]742}
743
[ba7aa2d]744DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
[bb7422a]745 if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) {
[702e826]746 if ( variable.assertions ) {
747 variable.assertions->appendList( assertions );
748 } else {
749 variable.assertions = assertions;
750 } // if
751 return this;
[2298f728]752 } // if
753
[b87a5ed]754 assert( type );
755 switch ( type->kind ) {
[0d0931d]756 case TypeData::Symbolic:
[8f6f47d7]757 if ( type->symbolic.assertions ) {
758 type->symbolic.assertions->appendList( assertions );
[b87a5ed]759 } else {
[8f6f47d7]760 type->symbolic.assertions = assertions;
[68cd1ce]761 } // if
[b87a5ed]762 break;
[0d0931d]763 default:
[b87a5ed]764 assert( false );
[68cd1ce]765 } // switch
[974906e2]766
[b87a5ed]767 return this;
[51b73452]768}
769
[fb114fa1]770DeclarationNode * DeclarationNode::addName( string * newname ) {
[2298f728]771 assert( ! name );
772 name = newname;
[b87a5ed]773 return this;
[51b73452]774}
775
[c0aa336]776DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
[58dd019]777 assert( ! asmName );
[c0aa336]778 asmName = newname ? newname->asmName : nullptr;
779 return this->addQualifiers( newname );
[58dd019]780}
781
[ba7aa2d]782DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
[b87a5ed]783 bitfieldWidth = size;
784 return this;
[51b73452]785}
786
[ba7aa2d]787DeclarationNode * DeclarationNode::addVarArgs() {
[b87a5ed]788 assert( type );
789 hasEllipsis = true;
790 return this;
[51b73452]791}
792
[c453ac4]793DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
[b87a5ed]794 assert( type );
795 assert( type->kind == TypeData::Function );
[2298f728]796 assert( ! type->function.body );
[8f6f47d7]797 type->function.body = body;
[c453ac4]798 type->function.withExprs = withExprs;
[b87a5ed]799 return this;
[51b73452]800}
801
[ba7aa2d]802DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
[b87a5ed]803 assert( type );
804 assert( type->kind == TypeData::Function );
[2298f728]805 assert( ! type->function.oldDeclList );
[8f6f47d7]806 type->function.oldDeclList = list;
[b87a5ed]807 return this;
[51b73452]808}
809
[c0aa336]810DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
[b87a5ed]811 if ( type ) {
[ba7aa2d]812 TypeData * prevBase = type;
813 TypeData * curBase = type->base;
[2298f728]814 while ( curBase != nullptr ) {
[b87a5ed]815 prevBase = curBase;
816 curBase = curBase->base;
[68cd1ce]817 } // while
[b87a5ed]818 prevBase->base = newType;
819 } else {
820 type = newType;
[68cd1ce]821 } // if
[c0aa336]822 return this;
[3848e0e]823}
824
[c0aa336]825DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
826 if ( a ) {
[bb7422a]827 spliceBegin( attributes, a->attributes );
[c0aa336]828 a->attributes.clear();
829 } // if
830 return this;
831} // copyAttribute
832
[ba7aa2d]833DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
[b87a5ed]834 if ( p ) {
[6926a6d]835 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
[c0aa336]836 setBase( p->type );
[2298f728]837 p->type = nullptr;
[c0aa336]838 copyAttribute( p );
[b87a5ed]839 delete p;
[68cd1ce]840 } // if
[b87a5ed]841 return this;
[3848e0e]842}
843
[ba7aa2d]844DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
[b87a5ed]845 if ( a ) {
846 assert( a->type->kind == TypeData::Array );
[c0aa336]847 setBase( a->type );
[2298f728]848 a->type = nullptr;
[c0aa336]849 copyAttribute( a );
[b87a5ed]850 delete a;
[68cd1ce]851 } // if
[b87a5ed]852 return this;
[51b73452]853}
854
[ba7aa2d]855DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
[b87a5ed]856 if ( p ) {
[e6cee92]857 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
[b87a5ed]858 if ( type ) {
859 switch ( type->kind ) {
[0d0931d]860 case TypeData::Aggregate:
861 case TypeData::Enum:
[b87a5ed]862 p->type->base = new TypeData( TypeData::AggregateInst );
[8f6f47d7]863 p->type->base->aggInst.aggregate = type;
[b87a5ed]864 if ( type->kind == TypeData::Aggregate ) {
[5bf685f]865 p->type->base->aggInst.params = maybeCopy( type->aggregate.actuals );
[68cd1ce]866 } // if
[6f95000]867 p->type->base->qualifiers |= type->qualifiers;
[b87a5ed]868 break;
869
[0d0931d]870 default:
[b87a5ed]871 p->type->base = type;
[68cd1ce]872 } // switch
[2298f728]873 type = nullptr;
[68cd1ce]874 } // if
[b87a5ed]875 delete this;
876 return p;
877 } else {
878 return this;
[68cd1ce]879 } // if
[51b73452]880}
881
[ba7aa2d]882static TypeData * findLast( TypeData * a ) {
[b87a5ed]883 assert( a );
[ba7aa2d]884 TypeData * cur = a;
[a32b204]885 while ( cur->base ) {
[b87a5ed]886 cur = cur->base;
[68cd1ce]887 } // while
[b87a5ed]888 return cur;
[3848e0e]889}
890
[ba7aa2d]891DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
[0d0931d]892 if ( ! a ) return this;
[738e304]893 assert( a->type->kind == TypeData::Array );
894 TypeData * lastArray = findLast( a->type );
895 if ( type ) {
896 switch ( type->kind ) {
[0d0931d]897 case TypeData::Aggregate:
898 case TypeData::Enum:
[738e304]899 lastArray->base = new TypeData( TypeData::AggregateInst );
900 lastArray->base->aggInst.aggregate = type;
901 if ( type->kind == TypeData::Aggregate ) {
[5bf685f]902 lastArray->base->aggInst.params = maybeCopy( type->aggregate.actuals );
[738e304]903 } // if
[6f95000]904 lastArray->base->qualifiers |= type->qualifiers;
[738e304]905 break;
[0d0931d]906 default:
[738e304]907 lastArray->base = type;
908 } // switch
909 type = nullptr;
[68cd1ce]910 } // if
[738e304]911 delete this;
912 return a;
[51b73452]913}
[3848e0e]914
[ba7aa2d]915DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
916 TypeData * ftype = new TypeData( TypeData::Function );
[8f6f47d7]917 ftype->function.params = params;
[c0aa336]918 setBase( ftype );
[b87a5ed]919 return this;
[3848e0e]920}
921
[ba7aa2d]922static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
[b87a5ed]923 if ( type ) {
924 if ( type->kind != TypeData::Function ) {
925 type->base = addIdListToType( type->base, ids );
926 } else {
[8f6f47d7]927 type->function.idList = ids;
[68cd1ce]928 } // if
[b87a5ed]929 return type;
[3848e0e]930 } else {
[ba7aa2d]931 TypeData * newtype = new TypeData( TypeData::Function );
[8f6f47d7]932 newtype->function.idList = ids;
[b87a5ed]933 return newtype;
[68cd1ce]934 } // if
[2298f728]935} // addIdListToType
[974906e2]936
[ba7aa2d]937DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
[b87a5ed]938 type = addIdListToType( type, ids );
939 return this;
[3848e0e]940}
941
[ba7aa2d]942DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
[b87a5ed]943 initializer = init;
944 return this;
[3848e0e]945}
946
[67cf18c]947DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
[bb7422a]948 assertf( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS, "Called addTypeInitializer on something that isn't a type variable." );
[67cf18c]949 variable.initializer = init;
950 return this;
951}
952
[a46b69c]953DeclarationNode * DeclarationNode::cloneType( string * name ) {
954 DeclarationNode * newnode = newName( name );
[5bf685f]955 newnode->type = maybeCopy( type );
[a7c90d4]956 newnode->copySpecifiers( this );
[b87a5ed]957 return newnode;
[3848e0e]958}
959
[4eb3a7c5]960DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o, bool copyattr ) {
[2298f728]961 if ( ! o ) return nullptr;
962
[4eb3a7c5]963 o->copySpecifiers( this, copyattr );
[2298f728]964 if ( type ) {
965 TypeData * srcType = type;
966
[c0aa336]967 // search for the base type by scanning off pointers and array designators
[2298f728]968 while ( srcType->base ) {
969 srcType = srcType->base;
970 } // while
971
972 TypeData * newType = srcType->clone();
973 if ( newType->kind == TypeData::AggregateInst ) {
974 // don't duplicate members
975 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
976 delete newType->aggInst.aggregate->enumeration.constants;
977 newType->aggInst.aggregate->enumeration.constants = nullptr;
[b2da0574]978 newType->aggInst.aggregate->enumeration.body = false;
[b87a5ed]979 } else {
[2298f728]980 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
981 delete newType->aggInst.aggregate->aggregate.fields;
982 newType->aggInst.aggregate->aggregate.fields = nullptr;
[b2da0574]983 newType->aggInst.aggregate->aggregate.body = false;
[68cd1ce]984 } // if
[43c89a7]985 // don't hoist twice
986 newType->aggInst.hoistType = false;
[68cd1ce]987 } // if
[2298f728]988
[5bf685f]989 newType->forall = maybeCopy( type->forall );
[2298f728]990 if ( ! o->type ) {
991 o->type = newType;
992 } else {
993 addTypeToType( newType, o->type );
994 delete newType;
995 } // if
[68cd1ce]996 } // if
[b87a5ed]997 return o;
[51b73452]998}
999
[ba7aa2d]1000DeclarationNode * DeclarationNode::extractAggregate() const {
[b87a5ed]1001 if ( type ) {
[ba7aa2d]1002 TypeData * ret = typeextractAggregate( type );
[b87a5ed]1003 if ( ret ) {
[ba7aa2d]1004 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]1005 newnode->type = ret;
[4eb3a7c5]1006 if ( ret->kind == TypeData::Aggregate ) {
1007 newnode->attributes.swap( ret->aggregate.attributes );
1008 } // if
[b87a5ed]1009 return newnode;
[843054c2]1010 } // if
1011 } // if
[2298f728]1012 return nullptr;
[3848e0e]1013}
1014
[b38f6da]1015// If a typedef wraps an anonymous declaration, name the inner declaration so it has a consistent name across
1016// translation units.
[45e753c]1017static void nameTypedefedDecl(
1018 DeclarationNode * innerDecl,
1019 const DeclarationNode * outerDecl ) {
1020 TypeData * outer = outerDecl->type;
1021 assert( outer );
1022 // First make sure this is a typedef:
1023 if ( outer->kind != TypeData::Symbolic || !outer->symbolic.isTypedef ) {
1024 return;
1025 }
1026 TypeData * inner = innerDecl->type;
1027 assert( inner );
1028 // Always clear any CVs associated with the aggregate:
1029 inner->qualifiers.reset();
1030 // Handle anonymous aggregates: typedef struct { int i; } foo
1031 if ( inner->kind == TypeData::Aggregate && inner->aggregate.anon ) {
1032 delete inner->aggregate.name;
1033 inner->aggregate.name = new string( "__anonymous_" + *outerDecl->name );
1034 inner->aggregate.anon = false;
1035 assert( outer->base );
1036 delete outer->base->aggInst.aggregate->aggregate.name;
1037 outer->base->aggInst.aggregate->aggregate.name = new string( "__anonymous_" + *outerDecl->name );
1038 outer->base->aggInst.aggregate->aggregate.anon = false;
1039 outer->base->aggInst.aggregate->qualifiers.reset();
1040 // Handle anonymous enumeration: typedef enum { A, B, C } foo
1041 } else if ( inner->kind == TypeData::Enum && inner->enumeration.anon ) {
1042 delete inner->enumeration.name;
1043 inner->enumeration.name = new string( "__anonymous_" + *outerDecl->name );
1044 inner->enumeration.anon = false;
1045 assert( outer->base );
1046 delete outer->base->aggInst.aggregate->enumeration.name;
1047 outer->base->aggInst.aggregate->enumeration.name = new string( "__anonymous_" + *outerDecl->name );
1048 outer->base->aggInst.aggregate->enumeration.anon = false;
1049 // No qualifiers.reset() here.
1050 }
1051}
1052
1053// This code handles a special issue with the attribute transparent_union.
1054//
1055// typedef union U { int i; } typedef_name __attribute__(( aligned(16) )) __attribute__(( transparent_union ))
1056//
1057// Here the attribute aligned goes with the typedef_name, so variables declared of this type are
1058// aligned. However, the attribute transparent_union must be moved from the typedef_name to
1059// alias union U. Currently, this is the only know attribute that must be moved from typedef to
1060// alias.
1061static void moveUnionAttribute( ast::Decl * decl, ast::UnionDecl * unionDecl ) {
1062 if ( auto typedefDecl = dynamic_cast<ast::TypedefDecl *>( decl ) ) {
1063 // Is the typedef alias a union aggregate?
1064 if ( nullptr == unionDecl ) return;
1065
1066 // If typedef is an alias for a union, then its alias type was hoisted above and remembered.
1067 if ( auto unionInstType = typedefDecl->base.as<ast::UnionInstType>() ) {
1068 auto instType = ast::mutate( unionInstType );
1069 // Remove all transparent_union attributes from typedef and move to alias union.
1070 for ( auto attr = instType->attributes.begin() ; attr != instType->attributes.end() ; ) {
1071 assert( *attr );
1072 if ( (*attr)->name == "transparent_union" || (*attr)->name == "__transparent_union__" ) {
1073 unionDecl->attributes.emplace_back( attr->release() );
1074 attr = instType->attributes.erase( attr );
1075 } else {
1076 attr++;
1077 }
1078 }
1079 typedefDecl->base = instType;
1080 }
1081 }
1082}
1083
1084// Get the non-anonymous name of the instance type of the declaration,
1085// if one exists.
1086static const std::string * getInstTypeOfName( ast::Decl * decl ) {
1087 if ( auto dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) {
1088 if ( auto aggr = dynamic_cast<ast::BaseInstType const *>( dwt->get_type() ) ) {
1089 if ( aggr->name.find("anonymous") == std::string::npos ) {
1090 return &aggr->name;
1091 }
1092 }
1093 }
1094 return nullptr;
1095}
1096
[b38f6da]1097void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::Decl>> & outputList ) {
[a16764a6]1098 SemanticErrorException errors;
[bb7422a]1099 std::back_insert_iterator<std::vector<ast::ptr<ast::Decl>>> out( outputList );
[2298f728]1100
[45e753c]1101 for ( const DeclarationNode * cur = firstNode ; cur ; cur = strict_next( cur ) ) {
[b87a5ed]1102 try {
[45e753c]1103 bool extracted_named = false;
1104 ast::UnionDecl * unionDecl = nullptr;
[692c1cc]1105
[ba7aa2d]1106 if ( DeclarationNode * extr = cur->extractAggregate() ) {
[78e2fca]1107 assert( cur->type );
[45e753c]1108 nameTypedefedDecl( extr, cur );
[692c1cc]1109
[45e753c]1110 if ( ast::Decl * decl = extr->build() ) {
[692c1cc]1111 // Remember the declaration if it is a union aggregate ?
[bb7422a]1112 unionDecl = dynamic_cast<ast::UnionDecl *>( decl );
[692c1cc]1113
1114 *out++ = decl;
[3d7e53b]1115
1116 // need to remember the cases where a declaration contains an anonymous aggregate definition
1117 assert( extr->type );
1118 if ( extr->type->kind == TypeData::Aggregate ) {
[692c1cc]1119 // typedef struct { int A } B is the only case?
[4eb3a7c5]1120 extracted_named = ! extr->type->aggregate.anon;
[3d7e53b]1121 } else if ( extr->type->kind == TypeData::Enum ) {
[692c1cc]1122 // typedef enum { A } B is the only case?
[4eb3a7c5]1123 extracted_named = ! extr->type->enumeration.anon;
[45e753c]1124 } else {
1125 extracted_named = true;
[3d7e53b]1126 }
[843054c2]1127 } // if
[f39096c]1128 delete extr;
[843054c2]1129 } // if
[2298f728]1130
[45e753c]1131 if ( ast::Decl * decl = cur->build() ) {
1132 moveUnionAttribute( decl, unionDecl );
1133
1134 if ( "" == decl->name && !cur->get_inLine() ) {
1135 // Don't include anonymous declaration for named aggregates,
1136 // but do include them for anonymous aggregates, e.g.:
1137 // struct S {
1138 // struct T { int x; }; // no anonymous member
1139 // struct { int y; }; // anonymous member
1140 // struct T; // anonymous member
1141 // };
1142 if ( extracted_named ) {
1143 continue;
1144 }
[692c1cc]1145
[45e753c]1146 if ( auto name = getInstTypeOfName( decl ) ) {
1147 // Temporary: warn about anonymous member declarations of named types, since
1148 // this conflicts with the syntax for the forward declaration of an anonymous type.
1149 SemanticWarning( cur->location, Warning::AggrForwardDecl, name->c_str() );
1150 }
[e07caa2]1151 } // if
[45e753c]1152 *out++ = decl;
[843054c2]1153 } // if
[45e753c]1154 } catch ( SemanticErrorException & e ) {
[b87a5ed]1155 errors.append( e );
[843054c2]1156 } // try
[e07caa2]1157 } // for
[2298f728]1158
[b87a5ed]1159 if ( ! errors.isEmpty() ) {
1160 throw errors;
[843054c2]1161 } // if
[2298f728]1162} // buildList
[3848e0e]1163
[3d7e53b]1164// currently only builds assertions, function parameters, and return values
[6611177]1165void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList ) {
[a16764a6]1166 SemanticErrorException errors;
[bb7422a]1167 std::back_insert_iterator<std::vector<ast::ptr<ast::DeclWithType>>> out( outputList );
[43c89a7]1168
[45e753c]1169 for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur ) ) {
[b87a5ed]1170 try {
[bb7422a]1171 ast::Decl * decl = cur->build();
[45e753c]1172 assertf( decl, "buildList: build for ast::DeclWithType." );
[bb7422a]1173 if ( ast::DeclWithType * dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) {
[47498bd]1174 dwt->location = cur->location;
[3ca7ef3]1175 *out++ = dwt;
[bb7422a]1176 } else if ( ast::StructDecl * agg = dynamic_cast<ast::StructDecl *>( decl ) ) {
[3d7e53b]1177 // e.g., int foo(struct S) {}
[bb7422a]1178 auto inst = new ast::StructInstType( agg->name );
1179 auto obj = new ast::ObjectDecl( cur->location, "", inst );
1180 obj->linkage = linkage;
[3ca7ef3]1181 *out++ = obj;
[47498bd]1182 delete agg;
[bb7422a]1183 } else if ( ast::UnionDecl * agg = dynamic_cast<ast::UnionDecl *>( decl ) ) {
[3d7e53b]1184 // e.g., int foo(union U) {}
[bb7422a]1185 auto inst = new ast::UnionInstType( agg->name );
1186 auto obj = new ast::ObjectDecl( cur->location,
1187 "", inst, nullptr, ast::Storage::Classes(),
1188 linkage );
[3ca7ef3]1189 *out++ = obj;
[bb7422a]1190 } else if ( ast::EnumDecl * agg = dynamic_cast<ast::EnumDecl *>( decl ) ) {
[3d7e53b]1191 // e.g., int foo(enum E) {}
[bb7422a]1192 auto inst = new ast::EnumInstType( agg->name );
1193 auto obj = new ast::ObjectDecl( cur->location,
1194 "",
1195 inst,
1196 nullptr,
1197 ast::Storage::Classes(),
1198 linkage
1199 );
[3ca7ef3]1200 *out++ = obj;
[45e753c]1201 } else {
1202 assertf( false, "buildList: Could not convert to ast::DeclWithType." );
[843054c2]1203 } // if
[45e753c]1204 } catch ( SemanticErrorException & e ) {
[b87a5ed]1205 errors.append( e );
[843054c2]1206 } // try
[3a5131ed]1207 } // for
1208
[b87a5ed]1209 if ( ! errors.isEmpty() ) {
1210 throw errors;
[843054c2]1211 } // if
[2298f728]1212} // buildList
[3848e0e]1213
[bb7422a]1214void buildTypeList( const DeclarationNode * firstNode,
1215 std::vector<ast::ptr<ast::Type>> & outputList ) {
[a16764a6]1216 SemanticErrorException errors;
[bb7422a]1217 std::back_insert_iterator<std::vector<ast::ptr<ast::Type>>> out( outputList );
[2298f728]1218
[45e753c]1219 for ( const DeclarationNode * cur = firstNode ; cur ; cur = strict_next( cur ) ) {
[b87a5ed]1220 try {
[ba7aa2d]1221 * out++ = cur->buildType();
[45e753c]1222 } catch ( SemanticErrorException & e ) {
[b87a5ed]1223 errors.append( e );
[843054c2]1224 } // try
[45e753c]1225 } // for
[2298f728]1226
[b87a5ed]1227 if ( ! errors.isEmpty() ) {
1228 throw errors;
[843054c2]1229 } // if
[2298f728]1230} // buildTypeList
[51b73452]1231
[bb7422a]1232ast::Decl * DeclarationNode::build() const {
[a16764a6]1233 if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
[2298f728]1234
[e994912]1235 if ( asmStmt ) {
[bb7422a]1236 auto stmt = strict_dynamic_cast<ast::AsmStmt *>( asmStmt->build() );
1237 return new ast::AsmDecl( stmt->location, stmt );
[e994912]1238 } // if
[2d019af]1239 if ( directiveStmt ) {
[bb7422a]1240 auto stmt = strict_dynamic_cast<ast::DirectiveStmt *>( directiveStmt->build() );
1241 return new ast::DirectiveDecl( stmt->location, stmt );
[2d019af]1242 } // if
[e994912]1243
[bb7422a]1244 if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) {
[f0ecf9b]1245 // otype is internally converted to dtype + otype parameters
[bb7422a]1246 static const ast::TypeDecl::Kind kindMap[] = { ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Ftype, ast::TypeDecl::Ttype, ast::TypeDecl::Dimension };
1247 static_assert( sizeof(kindMap) / sizeof(kindMap[0]) == ast::TypeDecl::NUMBER_OF_KINDS, "DeclarationNode::build: kindMap is out of sync." );
[8f60f0b]1248 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
[bb7422a]1249 ast::TypeDecl * ret = new ast::TypeDecl( location,
1250 *name,
1251 ast::Storage::Classes(),
1252 (ast::Type *)nullptr,
1253 kindMap[ variable.tyClass ],
1254 variable.tyClass == ast::TypeDecl::Otype || variable.tyClass == ast::TypeDecl::DStype,
1255 variable.initializer ? variable.initializer->buildType() : nullptr
1256 );
1257 buildList( variable.assertions, ret->assertions );
[2298f728]1258 return ret;
1259 } // if
1260
[843054c2]1261 if ( type ) {
[dd020c0]1262 // Function specifiers can only appear on a function definition/declaration.
1263 //
1264 // inline _Noreturn int f(); // allowed
1265 // inline _Noreturn int g( int i ); // allowed
1266 // inline _Noreturn int i; // disallowed
[fb04321]1267 if ( type->kind != TypeData::Function && funcSpecs.any() ) {
[a16764a6]1268 SemanticError( this, "invalid function specifier for " );
[dd020c0]1269 } // if
[284da8c]1270 // Forall qualifier can only appear on a function/aggregate definition/declaration.
1271 //
1272 // forall int f(); // allowed
1273 // forall int g( int i ); // allowed
1274 // forall int i; // disallowed
1275 if ( type->kind != TypeData::Function && type->forall ) {
1276 SemanticError( this, "invalid type qualifier for " );
1277 } // if
[3ed994e]1278 bool isDelete = initializer && initializer->get_isDelete();
[bb7422a]1279 ast::Decl * decl = buildDecl(
1280 type,
1281 name ? *name : string( "" ),
1282 storageClasses,
1283 maybeBuild( bitfieldWidth ),
1284 funcSpecs,
1285 linkage,
1286 asmName,
1287 isDelete ? nullptr : maybeBuild( initializer ),
1288 copy( attributes )
1289 )->set_extension( extension );
[3ed994e]1290 if ( isDelete ) {
[bb7422a]1291 auto dwt = strict_dynamic_cast<ast::DeclWithType *>( decl );
[3ed994e]1292 dwt->isDeleted = true;
1293 }
1294 return decl;
[843054c2]1295 } // if
[2298f728]1296
[f6e3e34]1297 if ( assert.condition ) {
[bb7422a]1298 auto cond = maybeBuild( assert.condition );
1299 auto msg = strict_dynamic_cast<ast::ConstantExpr *>( maybeCopy( assert.message ) );
1300 return new ast::StaticAssertDecl( location, cond, msg );
[f6e3e34]1301 }
1302
[dd020c0]1303 // SUE's cannot have function specifiers, either
1304 //
[79aae15]1305 // inline _Noreturn struct S { ... }; // disallowed
1306 // inline _Noreturn enum E { ... }; // disallowed
[fb04321]1307 if ( funcSpecs.any() ) {
[a16764a6]1308 SemanticError( this, "invalid function specifier for " );
[843054c2]1309 } // if
[e874605]1310 if ( enumInLine ) {
[bb7422a]1311 return new ast::InlineMemberDecl( location,
1312 *name, (ast::Type*)nullptr, storageClasses, linkage );
[e874605]1313 } // if
[dd020c0]1314 assertf( name, "ObjectDecl must a have name\n" );
[bb7422a]1315 auto ret = new ast::ObjectDecl( location,
1316 *name,
1317 (ast::Type*)nullptr,
1318 maybeBuild( initializer ),
1319 storageClasses,
1320 linkage,
1321 maybeBuild( bitfieldWidth )
1322 );
1323 ret->asmName = asmName;
1324 ret->extension = extension;
1325 return ret;
[51b73452]1326}
1327
[bb7422a]1328ast::Type * DeclarationNode::buildType() const {
[b87a5ed]1329 assert( type );
[974906e2]1330
[b87a5ed]1331 switch ( type->kind ) {
[0d0931d]1332 case TypeData::Enum:
1333 case TypeData::Aggregate: {
[bb7422a]1334 ast::BaseInstType * ret =
1335 buildComAggInst( type, copy( attributes ), linkage );
1336 buildList( type->aggregate.actuals, ret->params );
[0d0931d]1337 return ret;
1338 }
1339 case TypeData::Symbolic: {
[bb7422a]1340 ast::TypeInstType * ret = new ast::TypeInstType(
1341 *type->symbolic.name,
1342 // This is just a default, the true value is not known yet.
1343 ast::TypeDecl::Dtype,
1344 buildQualifiers( type ),
1345 copy( attributes ) );
1346 buildList( type->symbolic.actuals, ret->params );
[0d0931d]1347 return ret;
1348 }
1349 default:
[bb7422a]1350 ast::Type * simpletypes = typebuild( type );
1351 // copy because member is const
1352 simpletypes->attributes = attributes;
[c0aa336]1353 return simpletypes;
[b87a5ed]1354 } // switch
[3848e0e]1355}
1356
[b87a5ed]1357// Local Variables: //
1358// tab-width: 4 //
1359// mode: c++ //
1360// compile-command: "make install" //
1361// End: //
Note: See TracBrowser for help on using the repository browser.