source: src/Parser/DeclarationNode.cc@ 679e644

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 679e644 was 679e644, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

extend plan 9, anonymous declarations, change token for default argument

  • Property mode set to 100644
File size: 40.4 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
[679e644]12// Last Modified On : Wed Jul 18 22:50:27 2018
13// Update Count : 1096
[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/LinkageSpec.h" // for Spec, linkageName, Cforall
27#include "Parser/ParseNode.h" // for DeclarationNode, ExpressionNode
28#include "SynTree/Attribute.h" // for Attribute
29#include "SynTree/Declaration.h" // for TypeDecl, ObjectDecl, Declaration
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.
43const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "int128", "float80", "float128", "NoBasicTypeNames" };
[dd020c0]44const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" };
45const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" };
46const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" };
[409433da]47const char * DeclarationNode::aggregateNames[] = { "struct", "union", "trait", "coroutine", "monitor", "thread", "NoAggregateNames" };
[dd020c0]48const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" };
[9dc31c10]49const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "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() :
[2e5fa345]56 builtin( NoBuiltinType ),
[2298f728]57 type( nullptr ),
[679e644]58 inLine( false ),
[58dd019]59 bitfieldWidth( nullptr ),
[7d05e7e]60 hasEllipsis( false ),
61 linkage( ::linkage ),
[58dd019]62 asmName( nullptr ),
63 initializer( nullptr ),
[e994912]64 extension( false ),
65 asmStmt( nullptr ) {
[2298f728]66
[faddbd8]67// variable.name = nullptr;
68 variable.tyClass = NoTypeClass;
[28307be]69 variable.assertions = nullptr;
[67cf18c]70 variable.initializer = nullptr;
[7d05e7e]71
[faddbd8]72// attr.name = nullptr;
[7d05e7e]73 attr.expr = nullptr;
74 attr.type = nullptr;
[f6e3e34]75
76 assert.condition = nullptr;
77 assert.message = nullptr;
[28307be]78}
79
80DeclarationNode::~DeclarationNode() {
[faddbd8]81// delete attr.name;
[28307be]82 delete attr.expr;
83 delete attr.type;
[2298f728]84
[faddbd8]85// delete variable.name;
[2298f728]86 delete variable.assertions;
[67cf18c]87 delete variable.initializer;
[2298f728]88
[28307be]89 delete type;
90 delete bitfieldWidth;
[e994912]91
92 delete asmStmt;
[58dd019]93 // asmName, no delete, passed to next stage
[28307be]94 delete initializer;
[f6e3e34]95
96 delete assert.condition;
97 delete assert.message;
[28307be]98}
99
[ba7aa2d]100DeclarationNode * DeclarationNode::clone() const {
101 DeclarationNode * newnode = new DeclarationNode;
[c0aa336]102 newnode->set_next( maybeClone( get_next() ) );
[2298f728]103 newnode->name = name ? new string( *name ) : nullptr;
[c0aa336]104
[2e5fa345]105 newnode->builtin = NoBuiltinType;
[c0aa336]106 newnode->type = maybeClone( type );
[679e644]107 newnode->inLine = inLine;
[a7c90d4]108 newnode->storageClasses = storageClasses;
109 newnode->funcSpecs = funcSpecs;
[08d5507b]110 newnode->bitfieldWidth = maybeClone( bitfieldWidth );
[c0aa336]111 newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) );
[b87a5ed]112 newnode->hasEllipsis = hasEllipsis;
113 newnode->linkage = linkage;
[58dd019]114 newnode->asmName = maybeClone( asmName );
[c0aa336]115 cloneAll( attributes, newnode->attributes );
116 newnode->initializer = maybeClone( initializer );
117 newnode->extension = extension;
[e994912]118 newnode->asmStmt = maybeClone( asmStmt );
[c0aa336]119 newnode->error = error;
[3848e0e]120
[faddbd8]121// newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
[28307be]122 newnode->variable.tyClass = variable.tyClass;
[fb114fa1]123 newnode->variable.assertions = maybeClone( variable.assertions );
[67cf18c]124 newnode->variable.initializer = maybeClone( variable.initializer );
[3848e0e]125
[faddbd8]126// newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr;
[28307be]127 newnode->attr.expr = maybeClone( attr.expr );
128 newnode->attr.type = maybeClone( attr.type );
[f6e3e34]129
130 newnode->assert.condition = maybeClone( assert.condition );
131 newnode->assert.message = maybeClone( assert.message );
[28307be]132 return newnode;
133} // DeclarationNode::clone
[3848e0e]134
135void DeclarationNode::print( std::ostream &os, int indent ) const {
[59db689]136 os << string( indent, ' ' );
[2298f728]137 if ( name ) {
138 os << *name << ": ";
[b87a5ed]139 } else {
[2298f728]140 os << "unnamed: ";
[68cd1ce]141 } // if
[51b73452]142
[b87a5ed]143 if ( linkage != LinkageSpec::Cforall ) {
[faddbd8]144 os << LinkageSpec::linkageName( linkage ) << " ";
[68cd1ce]145 } // if
[3848e0e]146
[6e8bd43]147 storageClasses.print( os );
148 funcSpecs.print( os );
[dd020c0]149
[b87a5ed]150 if ( type ) {
151 type->print( os, indent );
152 } else {
153 os << "untyped entity ";
[68cd1ce]154 } // if
[3848e0e]155
[b87a5ed]156 if ( bitfieldWidth ) {
[59db689]157 os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
[b87a5ed]158 bitfieldWidth->printOneLine( os );
[68cd1ce]159 } // if
[3848e0e]160
[2298f728]161 if ( initializer ) {
[59db689]162 os << endl << string( indent + 2, ' ' ) << "with initializer ";
[b87a5ed]163 initializer->printOneLine( os );
[974906e2]164 os << " maybe constructed? " << initializer->get_maybeConstructed();
165
[68cd1ce]166 } // if
[3848e0e]167
[b87a5ed]168 os << endl;
[51b73452]169}
170
[3848e0e]171void DeclarationNode::printList( std::ostream &os, int indent ) const {
[b87a5ed]172 ParseNode::printList( os, indent );
173 if ( hasEllipsis ) {
174 os << string( indent, ' ' ) << "and a variable number of other arguments" << endl;
[68cd1ce]175 } // if
[51b73452]176}
177
[25bca42]178DeclarationNode * DeclarationNode::newFunction( const string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ) {
[ba7aa2d]179 DeclarationNode * newnode = new DeclarationNode;
[2298f728]180 newnode->name = name;
[b87a5ed]181 newnode->type = new TypeData( TypeData::Function );
[8f6f47d7]182 newnode->type->function.params = param;
183 newnode->type->function.body = body;
[c0aa336]184
[b87a5ed]185 if ( ret ) {
186 newnode->type->base = ret->type;
[2298f728]187 ret->type = nullptr;
[b87a5ed]188 delete ret;
[68cd1ce]189 } // if
[51b73452]190
[b87a5ed]191 return newnode;
[984dce6]192} // DeclarationNode::newFunction
[3848e0e]193
[dd020c0]194
[68fe077a]195DeclarationNode * DeclarationNode::newStorageClass( Type::StorageClasses sc ) {
[ba7aa2d]196 DeclarationNode * newnode = new DeclarationNode;
[08d5507b]197 newnode->storageClasses = sc;
[b87a5ed]198 return newnode;
[dd020c0]199} // DeclarationNode::newStorageClass
[3848e0e]200
[ddfd945]201DeclarationNode * DeclarationNode::newFuncSpecifier( Type::FuncSpecifiers fs ) {
[ba7aa2d]202 DeclarationNode * newnode = new DeclarationNode;
[08d5507b]203 newnode->funcSpecs = fs;
[c1c1112]204 return newnode;
[dd020c0]205} // DeclarationNode::newFuncSpecifier
[c1c1112]206
[738e304]207DeclarationNode * DeclarationNode::newTypeQualifier( Type::Qualifiers tq ) {
[ba7aa2d]208 DeclarationNode * newnode = new DeclarationNode;
[dd020c0]209 newnode->type = new TypeData();
[738e304]210 newnode->type->qualifiers = tq;
[b87a5ed]211 return newnode;
[dd020c0]212} // DeclarationNode::newQualifier
[3848e0e]213
[c1c1112]214DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
[ba7aa2d]215 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]216 newnode->type = new TypeData( TypeData::Basic );
[5b639ee]217 newnode->type->basictype = bt;
[b87a5ed]218 return newnode;
[984dce6]219} // DeclarationNode::newBasicType
[3848e0e]220
[5b639ee]221DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
[ba7aa2d]222 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]223 newnode->type = new TypeData( TypeData::Basic );
[5b639ee]224 newnode->type->complextype = ct;
[b87a5ed]225 return newnode;
[5b639ee]226} // DeclarationNode::newComplexType
227
228DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
[ba7aa2d]229 DeclarationNode * newnode = new DeclarationNode;
[5b639ee]230 newnode->type = new TypeData( TypeData::Basic );
231 newnode->type->signedness = sn;
232 return newnode;
233} // DeclarationNode::newSignedNess
234
235DeclarationNode * DeclarationNode::newLength( Length lnth ) {
[ba7aa2d]236 DeclarationNode * newnode = new DeclarationNode;
[5b639ee]237 newnode->type = new TypeData( TypeData::Basic );
238 newnode->type->length = lnth;
239 return newnode;
240} // DeclarationNode::newLength
[3848e0e]241
[dd020c0]242DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
243 DeclarationNode * newnode = new DeclarationNode;
244 newnode->type = new TypeData( TypeData::Unknown );
245 newnode->type->forall = forall;
246 return newnode;
247} // DeclarationNode::newForall
248
[25bca42]249DeclarationNode * DeclarationNode::newFromTypedef( const string * name ) {
[ba7aa2d]250 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]251 newnode->type = new TypeData( TypeData::SymbolicInst );
[fb114fa1]252 newnode->type->symbolic.name = name;
[8f6f47d7]253 newnode->type->symbolic.isTypedef = true;
[2298f728]254 newnode->type->symbolic.params = nullptr;
[b87a5ed]255 return newnode;
[984dce6]256} // DeclarationNode::newFromTypedef
[3848e0e]257
[47498bd]258DeclarationNode * DeclarationNode::newFromGlobalScope() {
259 DeclarationNode * newnode = new DeclarationNode;
260 newnode->type = new TypeData( TypeData::GlobalScope );
261 return newnode;
262}
263
[c5d7701]264DeclarationNode * DeclarationNode::newQualifiedType( DeclarationNode * parent, DeclarationNode * child) {
[c194661]265 DeclarationNode * newnode = new DeclarationNode;
266 newnode->type = new TypeData( TypeData::Qualified );
267 newnode->type->qualified.parent = parent->type;
268 newnode->type->qualified.child = child->type;
269 parent->type = nullptr;
270 child->type = nullptr;
271 delete parent;
272 delete child;
273 return newnode;
[c5d7701]274}
275
[fb114fa1]276DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
[ba7aa2d]277 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]278 newnode->type = new TypeData( TypeData::Aggregate );
[8f6f47d7]279 newnode->type->aggregate.kind = kind;
[3d7e53b]280 newnode->type->aggregate.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name;
[8f6f47d7]281 newnode->type->aggregate.actuals = actuals;
282 newnode->type->aggregate.fields = fields;
283 newnode->type->aggregate.body = body;
[6ea87486]284 newnode->type->aggregate.tagged = false;
285 newnode->type->aggregate.parent = nullptr;
[3d7e53b]286 newnode->type->aggregate.anon = name == nullptr;
[b87a5ed]287 return newnode;
[984dce6]288} // DeclarationNode::newAggregate
[3848e0e]289
[25bca42]290DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body ) {
[ba7aa2d]291 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]292 newnode->type = new TypeData( TypeData::Enum );
[3d7e53b]293 newnode->type->enumeration.name = name == nullptr ? new string( DeclarationNode::anonymous.newName() ) : name;
[8f6f47d7]294 newnode->type->enumeration.constants = constants;
[ca1a547]295 newnode->type->enumeration.body = body;
[3d7e53b]296 newnode->type->enumeration.anon = name == nullptr;
[b87a5ed]297 return newnode;
[984dce6]298} // DeclarationNode::newEnum
[3848e0e]299
[25bca42]300DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) {
[ba7aa2d]301 DeclarationNode * newnode = new DeclarationNode;
[2298f728]302 newnode->name = name;
[4f147cc]303 newnode->enumeratorValue.reset( constant );
[b87a5ed]304 return newnode;
[984dce6]305} // DeclarationNode::newEnumConstant
[3848e0e]306
[25bca42]307DeclarationNode * DeclarationNode::newName( const string * name ) {
[ba7aa2d]308 DeclarationNode * newnode = new DeclarationNode;
[2298f728]309 newnode->name = name;
[b87a5ed]310 return newnode;
[984dce6]311} // DeclarationNode::newName
[3848e0e]312
[25bca42]313DeclarationNode * DeclarationNode::newFromTypeGen( const string * name, ExpressionNode * params ) {
[ba7aa2d]314 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]315 newnode->type = new TypeData( TypeData::SymbolicInst );
[fb114fa1]316 newnode->type->symbolic.name = name;
[8f6f47d7]317 newnode->type->symbolic.isTypedef = false;
318 newnode->type->symbolic.actuals = params;
[b87a5ed]319 return newnode;
[984dce6]320} // DeclarationNode::newFromTypeGen
[3848e0e]321
[25bca42]322DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, const string * name ) {
[ba7aa2d]323 DeclarationNode * newnode = new DeclarationNode;
[2298f728]324 newnode->type = nullptr;
[148f7290]325 assert( ! newnode->name );
[faddbd8]326// newnode->variable.name = name;
327 newnode->name = name;
[28307be]328 newnode->variable.tyClass = tc;
[faddbd8]329 newnode->variable.assertions = nullptr;
[b87a5ed]330 return newnode;
[984dce6]331} // DeclarationNode::newTypeParam
[3848e0e]332
[fb114fa1]333DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
[ba7aa2d]334 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]335 newnode->type = new TypeData( TypeData::Aggregate );
[2298f728]336 newnode->type->aggregate.name = name;
[8f6f47d7]337 newnode->type->aggregate.kind = Trait;
338 newnode->type->aggregate.params = params;
339 newnode->type->aggregate.fields = asserts;
[b87a5ed]340 return newnode;
[984dce6]341} // DeclarationNode::newTrait
[3848e0e]342
[fb114fa1]343DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
[ba7aa2d]344 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]345 newnode->type = new TypeData( TypeData::AggregateInst );
[8f6f47d7]346 newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
347 newnode->type->aggInst.aggregate->aggregate.kind = Trait;
[2298f728]348 newnode->type->aggInst.aggregate->aggregate.name = name;
[8f6f47d7]349 newnode->type->aggInst.params = params;
[b87a5ed]350 return newnode;
[984dce6]351} // DeclarationNode::newTraitUse
[3848e0e]352
[25bca42]353DeclarationNode * DeclarationNode::newTypeDecl( const string * name, DeclarationNode * typeParams ) {
[ba7aa2d]354 DeclarationNode * newnode = new DeclarationNode;
[1395748]355 newnode->name = name;
[b87a5ed]356 newnode->type = new TypeData( TypeData::Symbolic );
[8f6f47d7]357 newnode->type->symbolic.isTypedef = false;
358 newnode->type->symbolic.params = typeParams;
[b87a5ed]359 return newnode;
[984dce6]360} // DeclarationNode::newTypeDecl
[3848e0e]361
[ce8c12f]362DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers, OperKinds kind ) {
[ba7aa2d]363 DeclarationNode * newnode = new DeclarationNode;
[ce8c12f]364 newnode->type = new TypeData( kind == OperKinds::PointTo ? TypeData::Pointer : TypeData::Reference );
[71d0eab]365 if ( kind == OperKinds::And ) {
366 // T && is parsed as 'And' operator rather than two references => add a second reference type
367 TypeData * td = new TypeData( TypeData::Reference );
368 td->base = newnode->type;
369 newnode->type = td;
370 }
[c3396e0]371 if ( qualifiers ) {
372 return newnode->addQualifiers( qualifiers );
373 } else {
374 return newnode;
375 } // if
[984dce6]376} // DeclarationNode::newPointer
[3848e0e]377
[ba7aa2d]378DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
379 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]380 newnode->type = new TypeData( TypeData::Array );
[8f6f47d7]381 newnode->type->array.dimension = size;
382 newnode->type->array.isStatic = isStatic;
[2298f728]383 if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) {
[8f6f47d7]384 newnode->type->array.isVarLen = false;
[71bd8c6]385 } else {
[8f6f47d7]386 newnode->type->array.isVarLen = true;
[71bd8c6]387 } // if
[b87a5ed]388 return newnode->addQualifiers( qualifiers );
[984dce6]389} // DeclarationNode::newArray
[3848e0e]390
[ba7aa2d]391DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
392 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]393 newnode->type = new TypeData( TypeData::Array );
[2298f728]394 newnode->type->array.dimension = nullptr;
[8f6f47d7]395 newnode->type->array.isStatic = false;
396 newnode->type->array.isVarLen = true;
[b87a5ed]397 return newnode->addQualifiers( qualifiers );
[3848e0e]398}
399
[ba7aa2d]400DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
401 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]402 newnode->bitfieldWidth = size;
403 return newnode;
[3848e0e]404}
405
[ba7aa2d]406DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
407 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]408 newnode->type = new TypeData( TypeData::Tuple );
[8f6f47d7]409 newnode->type->tuple = members;
[b87a5ed]410 return newnode;
[3848e0e]411}
412
[ba7aa2d]413DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
414 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]415 newnode->type = new TypeData( TypeData::Typeof );
[8f6f47d7]416 newnode->type->typeexpr = expr;
[b87a5ed]417 return newnode;
[3848e0e]418}
419
[8f6f47d7]420DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
[ba7aa2d]421 DeclarationNode * newnode = new DeclarationNode;
[8f6f47d7]422 newnode->type = new TypeData( TypeData::Builtin );
423 newnode->builtin = bt;
[148f7290]424 newnode->type->builtintype = newnode->builtin;
[8f6f47d7]425 return newnode;
426} // DeclarationNode::newBuiltinType
427
[25bca42]428DeclarationNode * DeclarationNode::newAttr( const string * name, ExpressionNode * expr ) {
[ba7aa2d]429 DeclarationNode * newnode = new DeclarationNode;
[2298f728]430 newnode->type = nullptr;
[faddbd8]431// newnode->attr.name = name;
432 newnode->name = name;
[28307be]433 newnode->attr.expr = expr;
[b87a5ed]434 return newnode;
[3848e0e]435}
436
[25bca42]437DeclarationNode * DeclarationNode::newAttr( const string * name, DeclarationNode * type ) {
[ba7aa2d]438 DeclarationNode * newnode = new DeclarationNode;
[2298f728]439 newnode->type = nullptr;
[faddbd8]440// newnode->attr.name = name;
441 newnode->name = name;
[28307be]442 newnode->attr.type = type;
[b87a5ed]443 return newnode;
[3848e0e]444}
445
[25bca42]446DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) {
[44a81853]447 DeclarationNode * newnode = new DeclarationNode;
448 newnode->type = nullptr;
449 std::list< Expression * > exprs;
450 buildList( expr, exprs );
451 newnode->attributes.push_back( new Attribute( *name, exprs ) );
452 delete name;
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
[08d5507b]505 } else if ( ! src->storageClasses.is_threadlocal ) { // 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
519 for ( Attribute *attr: reverseIterate( q->attributes ) ) {
520 attributes.push_front( attr->clone() );
521 } // for
[b6424d9]522 return this;
[a7c90d4]523} // DeclarationNode::copySpecifiers
[b6424d9]524
[ba7aa2d]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 );
[af9da5f]569 if ( (builtin == Zero || builtin == One) && q->type->qualifiers.val != 0 && error.length() == 0 ) {
[f14d956]570 SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, Type::QualifiersNames[ilog2( q->type->qualifiers.val )], builtinTypeNames[builtin] );
[9dc31c10]571 } // if
[6ef2d81]572 addQualifiersToType( q->type, type );
573
[b87a5ed]574 delete q;
575 return this;
[101e0bd]576} // addQualifiers
[3848e0e]577
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;
684 return this;
[3848e0e]685}
686
[ba7aa2d]687DeclarationNode * DeclarationNode::addTypedef() {
688 TypeData * newtype = new TypeData( TypeData::Symbolic );
[2298f728]689 newtype->symbolic.params = nullptr;
[8f6f47d7]690 newtype->symbolic.isTypedef = true;
[2298f728]691 newtype->symbolic.name = name ? new string( *name ) : nullptr;
[b87a5ed]692 newtype->base = type;
693 type = newtype;
694 return this;
[3848e0e]695}
696
[ba7aa2d]697DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
[faddbd8]698 if ( variable.tyClass != NoTypeClass ) {
[2298f728]699 if ( variable.assertions ) {
700 variable.assertions->appendList( assertions );
701 } else {
702 variable.assertions = assertions;
703 } // if
704 return this;
705 } // if
706
[b87a5ed]707 assert( type );
708 switch ( type->kind ) {
709 case TypeData::Symbolic:
[8f6f47d7]710 if ( type->symbolic.assertions ) {
711 type->symbolic.assertions->appendList( assertions );
[b87a5ed]712 } else {
[8f6f47d7]713 type->symbolic.assertions = assertions;
[68cd1ce]714 } // if
[b87a5ed]715 break;
716 default:
717 assert( false );
[68cd1ce]718 } // switch
[974906e2]719
[b87a5ed]720 return this;
[51b73452]721}
722
[fb114fa1]723DeclarationNode * DeclarationNode::addName( string * newname ) {
[2298f728]724 assert( ! name );
725 name = newname;
[b87a5ed]726 return this;
[51b73452]727}
728
[c0aa336]729DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
[58dd019]730 assert( ! asmName );
[c0aa336]731 asmName = newname ? newname->asmName : nullptr;
732 return this->addQualifiers( newname );
[58dd019]733}
734
[ba7aa2d]735DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
[b87a5ed]736 bitfieldWidth = size;
737 return this;
[51b73452]738}
739
[ba7aa2d]740DeclarationNode * DeclarationNode::addVarArgs() {
[b87a5ed]741 assert( type );
742 hasEllipsis = true;
743 return this;
[51b73452]744}
745
[c453ac4]746DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
[b87a5ed]747 assert( type );
748 assert( type->kind == TypeData::Function );
[2298f728]749 assert( ! type->function.body );
[8f6f47d7]750 type->function.body = body;
[c453ac4]751 type->function.withExprs = withExprs;
[b87a5ed]752 return this;
[51b73452]753}
754
[ba7aa2d]755DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
[b87a5ed]756 assert( type );
757 assert( type->kind == TypeData::Function );
[2298f728]758 assert( ! type->function.oldDeclList );
[8f6f47d7]759 type->function.oldDeclList = list;
[b87a5ed]760 return this;
[51b73452]761}
762
[c0aa336]763DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
[b87a5ed]764 if ( type ) {
[ba7aa2d]765 TypeData * prevBase = type;
766 TypeData * curBase = type->base;
[2298f728]767 while ( curBase != nullptr ) {
[b87a5ed]768 prevBase = curBase;
769 curBase = curBase->base;
[68cd1ce]770 } // while
[b87a5ed]771 prevBase->base = newType;
772 } else {
773 type = newType;
[68cd1ce]774 } // if
[c0aa336]775 return this;
[3848e0e]776}
777
[c0aa336]778DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
779 if ( a ) {
780 for ( Attribute *attr: reverseIterate( a->attributes ) ) {
781 attributes.push_front( attr );
782 } // for
783 a->attributes.clear();
784 } // if
785 return this;
786} // copyAttribute
787
[ba7aa2d]788DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
[b87a5ed]789 if ( p ) {
[6926a6d]790 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
[c0aa336]791 setBase( p->type );
[2298f728]792 p->type = nullptr;
[c0aa336]793 copyAttribute( p );
[b87a5ed]794 delete p;
[68cd1ce]795 } // if
[b87a5ed]796 return this;
[3848e0e]797}
798
[ba7aa2d]799DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
[b87a5ed]800 if ( a ) {
801 assert( a->type->kind == TypeData::Array );
[c0aa336]802 setBase( a->type );
[2298f728]803 a->type = nullptr;
[c0aa336]804 copyAttribute( a );
[b87a5ed]805 delete a;
[68cd1ce]806 } // if
[b87a5ed]807 return this;
[51b73452]808}
809
[ba7aa2d]810DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
[b87a5ed]811 if ( p ) {
[e6cee92]812 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
[b87a5ed]813 if ( type ) {
814 switch ( type->kind ) {
815 case TypeData::Aggregate:
816 case TypeData::Enum:
817 p->type->base = new TypeData( TypeData::AggregateInst );
[8f6f47d7]818 p->type->base->aggInst.aggregate = type;
[b87a5ed]819 if ( type->kind == TypeData::Aggregate ) {
[8f6f47d7]820 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
[68cd1ce]821 } // if
[6f95000]822 p->type->base->qualifiers |= type->qualifiers;
[b87a5ed]823 break;
824
825 default:
826 p->type->base = type;
[68cd1ce]827 } // switch
[2298f728]828 type = nullptr;
[68cd1ce]829 } // if
[b87a5ed]830 delete this;
831 return p;
832 } else {
833 return this;
[68cd1ce]834 } // if
[51b73452]835}
836
[ba7aa2d]837static TypeData * findLast( TypeData * a ) {
[b87a5ed]838 assert( a );
[ba7aa2d]839 TypeData * cur = a;
[a32b204]840 while ( cur->base ) {
[b87a5ed]841 cur = cur->base;
[68cd1ce]842 } // while
[b87a5ed]843 return cur;
[3848e0e]844}
845
[ba7aa2d]846DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
[738e304]847 if ( ! a ) return this;
848 assert( a->type->kind == TypeData::Array );
849 TypeData * lastArray = findLast( a->type );
850 if ( type ) {
851 switch ( type->kind ) {
852 case TypeData::Aggregate:
853 case TypeData::Enum:
854 lastArray->base = new TypeData( TypeData::AggregateInst );
855 lastArray->base->aggInst.aggregate = type;
856 if ( type->kind == TypeData::Aggregate ) {
857 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
858 } // if
[6f95000]859 lastArray->base->qualifiers |= type->qualifiers;
[738e304]860 break;
861 default:
862 lastArray->base = type;
863 } // switch
864 type = nullptr;
[68cd1ce]865 } // if
[738e304]866 delete this;
867 return a;
[51b73452]868}
[3848e0e]869
[ba7aa2d]870DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
871 TypeData * ftype = new TypeData( TypeData::Function );
[8f6f47d7]872 ftype->function.params = params;
[c0aa336]873 setBase( ftype );
[b87a5ed]874 return this;
[3848e0e]875}
876
[ba7aa2d]877static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
[b87a5ed]878 if ( type ) {
879 if ( type->kind != TypeData::Function ) {
880 type->base = addIdListToType( type->base, ids );
881 } else {
[8f6f47d7]882 type->function.idList = ids;
[68cd1ce]883 } // if
[b87a5ed]884 return type;
[3848e0e]885 } else {
[ba7aa2d]886 TypeData * newtype = new TypeData( TypeData::Function );
[8f6f47d7]887 newtype->function.idList = ids;
[b87a5ed]888 return newtype;
[68cd1ce]889 } // if
[2298f728]890} // addIdListToType
[974906e2]891
[ba7aa2d]892DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
[b87a5ed]893 type = addIdListToType( type, ids );
894 return this;
[3848e0e]895}
896
[ba7aa2d]897DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
[b87a5ed]898 initializer = init;
899 return this;
[3848e0e]900}
901
[67cf18c]902DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
903 assertf( variable.tyClass != NoTypeClass, "Called addTypeInitializer on something that isn't a type variable." );
904 variable.initializer = init;
905 return this;
906}
907
[ba7aa2d]908DeclarationNode * DeclarationNode::cloneType( string * newName ) {
909 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]910 newnode->type = maybeClone( type );
[a7c90d4]911 newnode->copySpecifiers( this );
[ba7aa2d]912 assert( newName );
[2298f728]913 newnode->name = newName;
[b87a5ed]914 return newnode;
[3848e0e]915}
916
[2298f728]917DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
918 if ( ! o ) return nullptr;
919
[a7c90d4]920 o->copySpecifiers( this );
[2298f728]921 if ( type ) {
922 TypeData * srcType = type;
923
[c0aa336]924 // search for the base type by scanning off pointers and array designators
[2298f728]925 while ( srcType->base ) {
926 srcType = srcType->base;
927 } // while
928
929 TypeData * newType = srcType->clone();
930 if ( newType->kind == TypeData::AggregateInst ) {
931 // don't duplicate members
932 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
933 delete newType->aggInst.aggregate->enumeration.constants;
934 newType->aggInst.aggregate->enumeration.constants = nullptr;
[b2da0574]935 newType->aggInst.aggregate->enumeration.body = false;
[b87a5ed]936 } else {
[2298f728]937 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
938 delete newType->aggInst.aggregate->aggregate.fields;
939 newType->aggInst.aggregate->aggregate.fields = nullptr;
[b2da0574]940 newType->aggInst.aggregate->aggregate.body = false;
[68cd1ce]941 } // if
[43c89a7]942 // don't hoist twice
943 newType->aggInst.hoistType = false;
[68cd1ce]944 } // if
[2298f728]945
946 newType->forall = maybeClone( type->forall );
947 if ( ! o->type ) {
948 o->type = newType;
949 } else {
950 addTypeToType( newType, o->type );
951 delete newType;
952 } // if
[68cd1ce]953 } // if
[b87a5ed]954 return o;
[51b73452]955}
956
[ba7aa2d]957DeclarationNode * DeclarationNode::extractAggregate() const {
[b87a5ed]958 if ( type ) {
[ba7aa2d]959 TypeData * ret = typeextractAggregate( type );
[b87a5ed]960 if ( ret ) {
[ba7aa2d]961 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]962 newnode->type = ret;
963 return newnode;
[843054c2]964 } // if
965 } // if
[2298f728]966 return nullptr;
[3848e0e]967}
968
[ba7aa2d]969void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
[a16764a6]970 SemanticErrorException errors;
[7880579]971 std::back_insert_iterator< std::list< Declaration * > > out( outputList );
[2298f728]972
[3a5131ed]973 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
[b87a5ed]974 try {
[3d7e53b]975 bool extracted = false;
976 bool anon = false;
[ba7aa2d]977 if ( DeclarationNode * extr = cur->extractAggregate() ) {
[b87a5ed]978 // handle the case where a structure declaration is contained within an object or type declaration
[ba7aa2d]979 Declaration * decl = extr->build();
[b87a5ed]980 if ( decl ) {
[3d7e53b]981 // hoist the structure declaration
[294647b]982 decl->location = cur->location;
[ba7aa2d]983 * out++ = decl;
[3d7e53b]984
985 // need to remember the cases where a declaration contains an anonymous aggregate definition
986 extracted = true;
987 assert( extr->type );
988 if ( extr->type->kind == TypeData::Aggregate ) {
989 anon = extr->type->aggregate.anon;
990 } else if ( extr->type->kind == TypeData::Enum ) {
991 // xxx - is it useful to have an implicit anonymous enum member?
992 anon = extr->type->enumeration.anon;
993 }
[843054c2]994 } // if
[f39096c]995 delete extr;
[843054c2]996 } // if
[2298f728]997
[ba7aa2d]998 Declaration * decl = cur->build();
[b87a5ed]999 if ( decl ) {
[3d7e53b]1000 // don't include anonymous declaration for named aggregates, but do include them for anonymous aggregates, e.g.:
1001 // struct S {
1002 // struct T { int x; }; // no anonymous member
1003 // struct { int y; }; // anonymous member
1004 // struct T; // anonymous member
1005 // };
1006 if ( ! (extracted && decl->name == "" && ! anon) ) {
[2e02851]1007 if (decl->name == "") {
1008 if ( DeclarationWithType * dwt = dynamic_cast<DeclarationWithType *>( decl ) ) {
1009 if ( ReferenceToType * aggr = dynamic_cast<ReferenceToType *>( dwt->get_type() ) ) {
1010 if ( aggr->name.find("anonymous") == std::string::npos ) {
[679e644]1011 // bool isInline = false;
1012 // if (cur->type->kind == TypeData::Aggregate || cur->type->kind == TypeData::AggregateInst) {
1013 // if (cur->type->kind == TypeData::Aggregate) {
1014 // isInline = cur->type->aggregate.inLine;
1015 // } else {
1016 // isInline = cur->type->aggInst.inLine;
1017 // if ( TypeData * aggr = cur->type->aggInst.aggregate ) {
1018 // if ( aggr->kind == TypeData::Aggregate ) {
1019 // isInline = isInline || aggr->aggregate.inLine;
1020 // }
1021 // }
1022 // }
1023 // }
1024 // if (! isInline) {
1025 if ( ! cur->inLine ) {
[2e02851]1026 // temporary: warn about anonymous member declarations of named types, since this conflicts with the syntax for the forward declaration of an anonymous type
1027 SemanticWarning( cur->location, Warning::AggrForwardDecl, aggr->name.c_str() );
1028 }
1029 }
1030 }
1031 }
1032 }
[3d7e53b]1033 decl->location = cur->location;
1034 * out++ = decl;
1035 }
[843054c2]1036 } // if
[a16764a6]1037 } catch( SemanticErrorException &e ) {
[b87a5ed]1038 errors.append( e );
[843054c2]1039 } // try
1040 } // while
[2298f728]1041
[b87a5ed]1042 if ( ! errors.isEmpty() ) {
1043 throw errors;
[843054c2]1044 } // if
[2298f728]1045} // buildList
[3848e0e]1046
[3d7e53b]1047// currently only builds assertions, function parameters, and return values
[ba7aa2d]1048void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
[a16764a6]1049 SemanticErrorException errors;
[7880579]1050 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
[43c89a7]1051
[3a5131ed]1052 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
[b87a5ed]1053 try {
[ba7aa2d]1054 Declaration * decl = cur->build();
[47498bd]1055 assert( decl );
1056 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
1057 dwt->location = cur->location;
1058 * out++ = dwt;
1059 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
[3d7e53b]1060 // e.g., int foo(struct S) {}
[47498bd]1061 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->name );
1062 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1063 obj->location = cur->location;
1064 * out++ = obj;
1065 delete agg;
1066 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
[3d7e53b]1067 // e.g., int foo(union U) {}
[47498bd]1068 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->name );
1069 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1070 obj->location = cur->location;
1071 * out++ = obj;
[3d7e53b]1072 } else if ( EnumDecl * agg = dynamic_cast< EnumDecl * >( decl ) ) {
1073 // e.g., int foo(enum E) {}
1074 EnumInstType * inst = new EnumInstType( Type::Qualifiers(), agg->name );
1075 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1076 obj->location = cur->location;
1077 * out++ = obj;
[843054c2]1078 } // if
[a16764a6]1079 } catch( SemanticErrorException &e ) {
[b87a5ed]1080 errors.append( e );
[843054c2]1081 } // try
[3a5131ed]1082 } // for
1083
[b87a5ed]1084 if ( ! errors.isEmpty() ) {
1085 throw errors;
[843054c2]1086 } // if
[2298f728]1087} // buildList
[3848e0e]1088
[ba7aa2d]1089void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
[a16764a6]1090 SemanticErrorException errors;
[7880579]1091 std::back_insert_iterator< std::list< Type * > > out( outputList );
[ba7aa2d]1092 const DeclarationNode * cur = firstNode;
[2298f728]1093
[a32b204]1094 while ( cur ) {
[b87a5ed]1095 try {
[ba7aa2d]1096 * out++ = cur->buildType();
[a16764a6]1097 } catch( SemanticErrorException &e ) {
[b87a5ed]1098 errors.append( e );
[843054c2]1099 } // try
[7880579]1100 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
[843054c2]1101 } // while
[2298f728]1102
[b87a5ed]1103 if ( ! errors.isEmpty() ) {
1104 throw errors;
[843054c2]1105 } // if
[2298f728]1106} // buildTypeList
[51b73452]1107
[ba7aa2d]1108Declaration * DeclarationNode::build() const {
[a16764a6]1109 if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
[2298f728]1110
[e994912]1111 if ( asmStmt ) {
[e3e16bc]1112 return new AsmDecl( strict_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
[e994912]1113 } // if
1114
[faddbd8]1115 if ( variable.tyClass != NoTypeClass ) {
[f0ecf9b]1116 // otype is internally converted to dtype + otype parameters
1117 static const TypeDecl::Kind kindMap[] = { TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
1118 assertf( sizeof(kindMap)/sizeof(kindMap[0]) == NoTypeClass, "DeclarationNode::build: kindMap is out of sync." );
[8f60f0b]1119 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
[f0ecf9b]1120 TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ], variable.tyClass == Otype, variable.initializer ? variable.initializer->buildType() : nullptr );
[2298f728]1121 buildList( variable.assertions, ret->get_assertions() );
1122 return ret;
1123 } // if
1124
[843054c2]1125 if ( type ) {
[dd020c0]1126 // Function specifiers can only appear on a function definition/declaration.
1127 //
1128 // inline _Noreturn int f(); // allowed
1129 // inline _Noreturn int g( int i ); // allowed
1130 // inline _Noreturn int i; // disallowed
[fb04321]1131 if ( type->kind != TypeData::Function && funcSpecs.any() ) {
[a16764a6]1132 SemanticError( this, "invalid function specifier for " );
[dd020c0]1133 } // if
[284da8c]1134 // Forall qualifier can only appear on a function/aggregate definition/declaration.
1135 //
1136 // forall int f(); // allowed
1137 // forall int g( int i ); // allowed
1138 // forall int i; // disallowed
1139 if ( type->kind != TypeData::Function && type->forall ) {
1140 SemanticError( this, "invalid type qualifier for " );
1141 } // if
[3ed994e]1142 bool isDelete = initializer && initializer->get_isDelete();
1143 Declaration * decl = buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, isDelete ? nullptr : maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
1144 if ( isDelete ) {
1145 DeclarationWithType * dwt = strict_dynamic_cast<DeclarationWithType *>( decl );
1146 dwt->isDeleted = true;
1147 }
1148 return decl;
[843054c2]1149 } // if
[2298f728]1150
[f6e3e34]1151 if ( assert.condition ) {
1152 return new StaticAssertDecl( maybeBuild< Expression >( assert.condition ), strict_dynamic_cast< ConstantExpr * >( maybeClone( assert.message ) ) );
1153 }
1154
[dd020c0]1155 // SUE's cannot have function specifiers, either
1156 //
1157 // inlne _Noreturn struct S { ... }; // disallowed
1158 // inlne _Noreturn enum E { ... }; // disallowed
[fb04321]1159 if ( funcSpecs.any() ) {
[a16764a6]1160 SemanticError( this, "invalid function specifier for " );
[843054c2]1161 } // if
[dd020c0]1162 assertf( name, "ObjectDecl must a have name\n" );
[a7c90d4]1163 return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
[51b73452]1164}
1165
[ba7aa2d]1166Type * DeclarationNode::buildType() const {
[b87a5ed]1167 assert( type );
[974906e2]1168
[faddbd8]1169 if ( attr.expr ) {
[c0aa336]1170 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
[faddbd8]1171 } else if ( attr.type ) {
[c0aa336]1172 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
[2298f728]1173 } // if
1174
[b87a5ed]1175 switch ( type->kind ) {
[43c89a7]1176 case TypeData::Enum:
[b87a5ed]1177 case TypeData::Aggregate: {
[fa4805f]1178 ReferenceToType * ret = buildComAggInst( type, attributes, linkage );
[8f6f47d7]1179 buildList( type->aggregate.actuals, ret->get_parameters() );
[b87a5ed]1180 return ret;
1181 }
1182 case TypeData::Symbolic: {
[c0aa336]1183 TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
[8f6f47d7]1184 buildList( type->symbolic.actuals, ret->get_parameters() );
[b87a5ed]1185 return ret;
1186 }
1187 default:
[c0aa336]1188 Type * simpletypes = typebuild( type );
1189 simpletypes->get_attributes() = attributes; // copy because member is const
1190 return simpletypes;
[b87a5ed]1191 } // switch
[3848e0e]1192}
1193
[b87a5ed]1194// Local Variables: //
1195// tab-width: 4 //
1196// mode: c++ //
1197// compile-command: "make install" //
1198// End: //
Note: See TracBrowser for help on using the repository browser.