source: src/Parser/DeclarationNode.cc @ c38ae92

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since c38ae92 was c38ae92, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

fix addQualifiers bug for multiple forall qualifiers, rebind forall from inline struct to routine

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