source: src/Parser/DeclarationNode.cc @ e496303

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 e496303 was e496303, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change type of type qualifiers to bit fields and fix uninitialized fields in aggregate

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