source: src/Parser/DeclarationNode.cc @ 738e304

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

merge qualifier types and use the one in Type

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