source: src/Parser/DeclarationNode.cc @ c468150

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

Split up ParseNode?.h so that headers match implementation. May have a bit less to include total because of it.

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