source: src/Parser/DeclarationNode.cc @ 836cf647

ADTast-experimentalpthread-emulation
Last change on this file since 836cf647 was ed9a1ae, checked in by Thierry Delisle <tdelisle@…>, 22 months ago

Cfa now distinguishes between thread and _Thread_local.

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