source: src/Parser/DeclarationNode.cc @ 702e826

ADTast-experimental
Last change on this file since 702e826 was 702e826, checked in by Andrew Beach <ajbeach@…>, 20 months ago

Pre-translation pass on the parser. Entirely code readability improvements, no behaviour (on a larger scale) should be effected.

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