source: src/Parser/DeclarationNode.cc@ b86d14c0

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

change BadQualifiersZeroOne warning from dubious print of specified qualifer(s) to a generic message

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