source: src/Parser/DeclarationNode.cc @ 6e9ffd1

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resnenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 6e9ffd1 was f2f512ba, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

formatting

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