source: src/Parser/DeclarationNode.cc@ 9a380e1a

ADT ast-experimental
Last change on this file since 9a380e1a was 692c1cc, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

update printing attributes, clean up anon flag setting, move attribute transparent_union from typedef to its union alias

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