source: src/Parser/DeclarationNode.cc @ 3d26610

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 3d26610 was 3ed994e, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Push deleted decls through the system

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