source: src/Parser/DeclarationNode.cc @ 1b7b604

ADTast-experimental
Last change on this file since 1b7b604 was e4d7c1c, checked in by JiadaL <j82liang@…>, 22 months ago

Implement enum Hiding

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