source: src/Parser/DeclarationNode.cc @ d94195d

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 d94195d was 6ea87486, checked in by Andrew Beach <ajbeach@…>, 7 years ago

That should be all the base code for 'tree structures' to work.

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