source: src/Parser/DeclarationNode.cc @ bdad6eb7

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 bdad6eb7 was 71d0eab, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Build AST nodes for '&&' reference correctly

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