source: src/Parser/DeclarationNode.cc @ deda7e6

Last change on this file since deda7e6 was c1e66d9, checked in by JiadaL <j82liang@…>, 12 months ago

Fix designator value in enumerated array and implemented enumerated array with inlined enume declaration

  • Property mode set to 100644
File size: 44.9 KB
RevLine 
[b87a5ed]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[974906e2]7// DeclarationNode.cc --
[b87a5ed]8//
9// Author           : Rodolfo G. Esteves
10// Created On       : Sat May 16 12:34:05 2015
[b38f6da]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat Jun 17 14:41:48 2023
13// Update Count     : 1405
[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
[c1e66d9]397DeclarationNode * DeclarationNode::newInlineEnumeratedArray( DeclarationNode * enumDecl, DeclarationNode * qualifiers ) {
398        DeclarationNode * newnode = new DeclarationNode;
399        newnode->type = new TypeData( TypeData::Array );
400        newnode->type->array.isStatic = false;
401        newnode->type->array.isVarLen = false;
402
403        if ( enumDecl ) {
404                newnode->type->declType = enumDecl->type;
405                enumDecl->type = nullptr;
406                delete enumDecl;
407        } // if
408       
409        return newnode->addQualifiers( qualifiers );
410}
411
[ba7aa2d]412DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
413        DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]414        newnode->type = new TypeData( TypeData::Array );
[2298f728]415        newnode->type->array.dimension = nullptr;
[8f6f47d7]416        newnode->type->array.isStatic = false;
417        newnode->type->array.isVarLen = true;
[b87a5ed]418        return newnode->addQualifiers( qualifiers );
[3848e0e]419}
420
[ba7aa2d]421DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
422        DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]423        newnode->bitfieldWidth = size;
424        return newnode;
[3848e0e]425}
426
[ba7aa2d]427DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
428        DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]429        newnode->type = new TypeData( TypeData::Tuple );
[8f6f47d7]430        newnode->type->tuple = members;
[b87a5ed]431        return newnode;
[3848e0e]432}
433
[f855545]434DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr, bool basetypeof ) {
[ba7aa2d]435        DeclarationNode * newnode = new DeclarationNode;
[f855545]436        newnode->type = new TypeData( basetypeof ? TypeData::Basetypeof : TypeData::Typeof );
[8f6f47d7]437        newnode->type->typeexpr = expr;
[b87a5ed]438        return newnode;
[3848e0e]439}
440
[93bbbc4]441DeclarationNode * DeclarationNode::newVtableType( DeclarationNode * decl ) {
442        DeclarationNode * newnode = new DeclarationNode;
443        newnode->type = new TypeData( TypeData::Vtable );
444        newnode->setBase( decl->type );
445        return newnode;
446}
447
[8f6f47d7]448DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
[ba7aa2d]449        DeclarationNode * newnode = new DeclarationNode;
[8f6f47d7]450        newnode->type = new TypeData( TypeData::Builtin );
451        newnode->builtin = bt;
[148f7290]452        newnode->type->builtintype = newnode->builtin;
[8f6f47d7]453        return newnode;
454} // DeclarationNode::newBuiltinType
455
[a46b69c]456DeclarationNode * DeclarationNode::newFunction( const string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ) {
457        DeclarationNode * newnode = newName( name );
458        newnode->type = new TypeData( TypeData::Function );
459        newnode->type->function.params = param;
460        newnode->type->function.body = body;
461
462        if ( ret ) {
463                newnode->type->base = ret->type;
464                ret->type = nullptr;
465                delete ret;
466        } // if
467
468        return newnode;
469} // DeclarationNode::newFunction
470
[25bca42]471DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) {
[44a81853]472        DeclarationNode * newnode = new DeclarationNode;
473        newnode->type = nullptr;
[bb7422a]474        std::vector<ast::ptr<ast::Expr>> exprs;
[44a81853]475        buildList( expr, exprs );
[b38f6da]476        newnode->attributes.push_back( new ast::Attribute( *name, std::move( exprs ) ) );
[44a81853]477        delete name;
478        return newnode;
479}
480
[2d019af]481DeclarationNode * DeclarationNode::newDirectiveStmt( StatementNode * stmt ) {
482        DeclarationNode * newnode = new DeclarationNode;
483        newnode->directiveStmt = stmt;
484        return newnode;
485}
486
[e994912]487DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
488        DeclarationNode * newnode = new DeclarationNode;
489        newnode->asmStmt = stmt;
490        return newnode;
491}
492
[bb7422a]493DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, ast::Expr * message ) {
[f6e3e34]494        DeclarationNode * newnode = new DeclarationNode;
495        newnode->assert.condition = condition;
496        newnode->assert.message = message;
497        return newnode;
498}
499
[bb7422a]500static void appendError( string & dst, const string & src ) {
[5b639ee]501        if ( src.empty() ) return;
502        if ( dst.empty() ) { dst = src; return; }
503        dst += ", " + src;
504} // appendError
505
[ba7aa2d]506void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
[bb7422a]507        const ast::CV::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
508        const ast::CV::Qualifiers duplicates = qsrc & qdst;
509
510        if ( duplicates.any() ) {
511                std::stringstream str;
512                str << "duplicate ";
513                ast::print( str, duplicates );
514                str << "qualifier(s)";
515                appendError( error, str.str() );
[a7c90d4]516        } // for
[c1c1112]517} // DeclarationNode::checkQualifiers
518
[a7c90d4]519void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
[bb7422a]520        ast::Function::Specs fsDups = funcSpecs & src->funcSpecs;
521        if ( fsDups.any() ) {
522                std::stringstream str;
523                str << "duplicate ";
524                ast::print( str, fsDups );
525                str << "function specifier(s)";
526                appendError( error, str.str() );
[dd020c0]527        } // if
528
[bb7422a]529        // Skip if everything is unset.
530        if ( storageClasses.any() && src->storageClasses.any() ) {
531                ast::Storage::Classes dups = storageClasses & src->storageClasses;
532                // Check for duplicates.
533                if ( dups.any() ) {
534                        std::stringstream str;
535                        str << "duplicate ";
536                        ast::print( str, dups );
537                        str << "storage class(es)";
538                        appendError( error, str.str() );
539                // Check for conflicts.
540                } else if ( !src->storageClasses.is_threadlocal_any() ) {
541                        std::stringstream str;
542                        str << "conflicting ";
543                        ast::print( str, ast::Storage::Classes( 1 << storageClasses.ffs() ) );
544                        str << "& ";
545                        ast::print( str, ast::Storage::Classes( 1 << src->storageClasses.ffs() ) );
546                        str << "storage classes";
547                        appendError( error, str.str() );
548                        // FIX to preserve invariant of one basic storage specifier
549                        src->storageClasses.reset();
550                }
[b6424d9]551        } // if
[dd020c0]552
[a7c90d4]553        appendError( error, src->error );
554} // DeclarationNode::checkSpecifiers
[b6424d9]555
[a7c90d4]556DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) {
[6f95000]557        funcSpecs |= q->funcSpecs;
558        storageClasses |= q->storageClasses;
[c0aa336]559
[bb7422a]560        std::vector<ast::ptr<ast::Attribute>> tmp;
561        tmp.reserve( q->attributes.size() );
562        for ( auto const & attr : q->attributes ) {
563                tmp.emplace_back( ast::shallowCopy( attr.get() ) );
564        }
565        spliceBegin( attributes, tmp );
566
[b6424d9]567        return this;
[a7c90d4]568} // DeclarationNode::copySpecifiers
[b6424d9]569
[f2f512ba]570static void addQualifiersToType( TypeData *& src, TypeData * dst ) {
[101e0bd]571        if ( dst->base ) {
572                addQualifiersToType( src, dst->base );
573        } else if ( dst->kind == TypeData::Function ) {
574                dst->base = src;
[2298f728]575                src = nullptr;
[101e0bd]576        } else {
[6f95000]577                dst->qualifiers |= src->qualifiers;
[101e0bd]578        } // if
579} // addQualifiersToType
580
[ba7aa2d]581DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
[af9da5f]582        if ( ! q ) { return this; }                                                     // empty qualifier
[101e0bd]583
[a7c90d4]584        checkSpecifiers( q );
585        copySpecifiers( q );
[101e0bd]586
[c38ae92]587        if ( ! q->type ) { delete q; return this; }
[101e0bd]588
589        if ( ! type ) {
[c38ae92]590                type = q->type;                                                                 // reuse structure
[1b77274]591                q->type = nullptr;
592                delete q;
[101e0bd]593                return this;
594        } // if
595
[c38ae92]596        if ( q->type->forall ) {                                                        // forall qualifier ?
597                if ( type->forall ) {                                                   // polymorphic routine ?
598                        type->forall->appendList( q->type->forall ); // augment forall qualifier
[101e0bd]599                } else {
[c38ae92]600                        if ( type->kind == TypeData::Aggregate ) {      // struct/union ?
601                                if ( type->aggregate.params ) {                 // polymorphic ?
602                                        type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier
603                                } else {                                                                // not polymorphic
[a1c9ddd]604                                        type->aggregate.params = q->type->forall; // set forall qualifier
[c38ae92]605                                } // if
606                        } else {                                                                        // not polymorphic
607                                type->forall = q->type->forall;                 // make polymorphic routine
[68cd1ce]608                        } // if
609                } // if
[c38ae92]610                q->type->forall = nullptr;                                              // forall qualifier moved
[68cd1ce]611        } // if
[6ef2d81]612
[a7c90d4]613        checkQualifiers( type, q->type );
[78e2fca]614        if ( (builtin == Zero || builtin == One) && q->type->qualifiers.any() && error.length() == 0 ) {
[be00a2d]615                SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, builtinTypeNames[builtin] );
[9dc31c10]616        } // if
[6ef2d81]617        addQualifiersToType( q->type, type );
618
[b87a5ed]619        delete q;
620        return this;
[101e0bd]621} // addQualifiers
[3848e0e]622
[f2f512ba]623static void addTypeToType( TypeData *& src, TypeData *& dst ) {
[101e0bd]624        if ( src->forall && dst->kind == TypeData::Function ) {
625                if ( dst->forall ) {
626                        dst->forall->appendList( src->forall );
[b87a5ed]627                } else {
[101e0bd]628                        dst->forall = src->forall;
629                } // if
[2298f728]630                src->forall = nullptr;
[101e0bd]631        } // if
632        if ( dst->base ) {
633                addTypeToType( src, dst->base );
634        } else {
635                switch ( dst->kind ) {
[0d0931d]636                case TypeData::Unknown:
[6f95000]637                        src->qualifiers |= dst->qualifiers;
[101e0bd]638                        dst = src;
[2298f728]639                        src = nullptr;
[101e0bd]640                        break;
[0d0931d]641                case TypeData::Basic:
[6f95000]642                        dst->qualifiers |= src->qualifiers;
[101e0bd]643                        if ( src->kind != TypeData::Unknown ) {
644                                assert( src->kind == TypeData::Basic );
645
646                                if ( dst->basictype == DeclarationNode::NoBasicType ) {
647                                        dst->basictype = src->basictype;
648                                } else if ( src->basictype != DeclarationNode::NoBasicType )
[b38f6da]649                                        SemanticError( yylloc, string( "multiple declaration types \"" ) + DeclarationNode::basicTypeNames[ dst->basictype ] +
650                                                                   "\" and \"" + DeclarationNode::basicTypeNames[ src->basictype ] + "\"." );
[101e0bd]651
652                                if ( dst->complextype == DeclarationNode::NoComplexType ) {
653                                        dst->complextype = src->complextype;
654                                } else if ( src->complextype != DeclarationNode::NoComplexType )
[b38f6da]655                                        SemanticError( yylloc, string( "multiple declaration types \"" ) + DeclarationNode::complexTypeNames[ src->complextype ] +
656                                                                   "\" and \"" + DeclarationNode::complexTypeNames[ src->complextype ] + "\"." );
[101e0bd]657
658                                if ( dst->signedness == DeclarationNode::NoSignedness ) {
659                                        dst->signedness = src->signedness;
660                                } else if ( src->signedness != DeclarationNode::NoSignedness )
[b38f6da]661                                        SemanticError( yylloc, string( "conflicting type specifier \"" ) + DeclarationNode::signednessNames[ dst->signedness ] +
662                                                                   "\" and \"" + DeclarationNode::signednessNames[ src->signedness ] + "\"." );
[101e0bd]663
664                                if ( dst->length == DeclarationNode::NoLength ) {
665                                        dst->length = src->length;
666                                } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
667                                        dst->length = DeclarationNode::LongLong;
668                                } else if ( src->length != DeclarationNode::NoLength )
[b38f6da]669                                        SemanticError( yylloc, string( "conflicting type specifier \"" ) + DeclarationNode::lengthNames[ dst->length ] +
670                                                                   "\" and \"" + DeclarationNode::lengthNames[ src->length ] + "\"." );
[101e0bd]671                        } // if
672                        break;
[0d0931d]673                default:
[101e0bd]674                        switch ( src->kind ) {
[0d0931d]675                        case TypeData::Aggregate:
676                        case TypeData::Enum:
[101e0bd]677                                dst->base = new TypeData( TypeData::AggregateInst );
678                                dst->base->aggInst.aggregate = src;
679                                if ( src->kind == TypeData::Aggregate ) {
680                                        dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
[68cd1ce]681                                } // if
[6f95000]682                                dst->base->qualifiers |= src->qualifiers;
[2298f728]683                                src = nullptr;
[b87a5ed]684                                break;
[0d0931d]685                        default:
[101e0bd]686                                if ( dst->forall ) {
687                                        dst->forall->appendList( src->forall );
688                                } else {
689                                        dst->forall = src->forall;
690                                } // if
[2298f728]691                                src->forall = nullptr;
[101e0bd]692                                dst->base = src;
[2298f728]693                                src = nullptr;
[68cd1ce]694                        } // switch
[101e0bd]695                } // switch
[68cd1ce]696        } // if
[3848e0e]697}
698
[ba7aa2d]699DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
[b87a5ed]700        if ( o ) {
[a7c90d4]701                checkSpecifiers( o );
702                copySpecifiers( o );
[b87a5ed]703                if ( o->type ) {
704                        if ( ! type ) {
705                                if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
706                                        type = new TypeData( TypeData::AggregateInst );
[8f6f47d7]707                                        type->aggInst.aggregate = o->type;
[b87a5ed]708                                        if ( o->type->kind == TypeData::Aggregate ) {
[43c89a7]709                                                type->aggInst.hoistType = o->type->aggregate.body;
[8f6f47d7]710                                                type->aggInst.params = maybeClone( o->type->aggregate.actuals );
[43c89a7]711                                        } else {
712                                                type->aggInst.hoistType = o->type->enumeration.body;
[68cd1ce]713                                        } // if
[6f95000]714                                        type->qualifiers |= o->type->qualifiers;
[b87a5ed]715                                } else {
716                                        type = o->type;
[68cd1ce]717                                } // if
[2298f728]718                                o->type = nullptr;
[b87a5ed]719                        } else {
720                                addTypeToType( o->type, type );
[68cd1ce]721                        } // if
722                } // if
[b87a5ed]723                if ( o->bitfieldWidth ) {
724                        bitfieldWidth = o->bitfieldWidth;
[68cd1ce]725                } // if
[71bd8c6]726
727                // there may be typedefs chained onto the type
[1d4580a]728                if ( o->get_next() ) {
729                        set_last( o->get_next()->clone() );
[1db21619]730                } // if
[68cd1ce]731        } // if
[b87a5ed]732        delete o;
[66406f3]733
[b87a5ed]734        return this;
[3848e0e]735}
736
[f135b50]737DeclarationNode * DeclarationNode::addEnumBase( DeclarationNode * o ) {
[b38f6da]738        if ( o && o->type)  {
[f135b50]739                type->base= o->type;
[b38f6da]740        } // if
[f135b50]741        delete o;
742        return this;
743}
744
[ba7aa2d]745DeclarationNode * DeclarationNode::addTypedef() {
746        TypeData * newtype = new TypeData( TypeData::Symbolic );
[2298f728]747        newtype->symbolic.params = nullptr;
[8f6f47d7]748        newtype->symbolic.isTypedef = true;
[2298f728]749        newtype->symbolic.name = name ? new string( *name ) : nullptr;
[b87a5ed]750        newtype->base = type;
751        type = newtype;
752        return this;
[3848e0e]753}
754
[ba7aa2d]755DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
[bb7422a]756        if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) {
[702e826]757                if ( variable.assertions ) {
758                        variable.assertions->appendList( assertions );
759                } else {
760                        variable.assertions = assertions;
761                } // if
762                return this;
[2298f728]763        } // if
764
[b87a5ed]765        assert( type );
766        switch ( type->kind ) {
[0d0931d]767        case TypeData::Symbolic:
[8f6f47d7]768                if ( type->symbolic.assertions ) {
769                        type->symbolic.assertions->appendList( assertions );
[b87a5ed]770                } else {
[8f6f47d7]771                        type->symbolic.assertions = assertions;
[68cd1ce]772                } // if
[b87a5ed]773                break;
[0d0931d]774        default:
[b87a5ed]775                assert( false );
[68cd1ce]776        } // switch
[974906e2]777
[b87a5ed]778        return this;
[51b7345]779}
780
[fb114fa1]781DeclarationNode * DeclarationNode::addName( string * newname ) {
[2298f728]782        assert( ! name );
783        name = newname;
[b87a5ed]784        return this;
[51b7345]785}
786
[c0aa336]787DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
[58dd019]788        assert( ! asmName );
[c0aa336]789        asmName = newname ? newname->asmName : nullptr;
790        return this->addQualifiers( newname );
[58dd019]791}
792
[ba7aa2d]793DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
[b87a5ed]794        bitfieldWidth = size;
795        return this;
[51b7345]796}
797
[ba7aa2d]798DeclarationNode * DeclarationNode::addVarArgs() {
[b87a5ed]799        assert( type );
800        hasEllipsis = true;
801        return this;
[51b7345]802}
803
[c453ac4]804DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
[b87a5ed]805        assert( type );
806        assert( type->kind == TypeData::Function );
[2298f728]807        assert( ! type->function.body );
[8f6f47d7]808        type->function.body = body;
[c453ac4]809        type->function.withExprs = withExprs;
[b87a5ed]810        return this;
[51b7345]811}
812
[ba7aa2d]813DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
[b87a5ed]814        assert( type );
815        assert( type->kind == TypeData::Function );
[2298f728]816        assert( ! type->function.oldDeclList );
[8f6f47d7]817        type->function.oldDeclList = list;
[b87a5ed]818        return this;
[51b7345]819}
820
[c0aa336]821DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
[b87a5ed]822        if ( type ) {
[ba7aa2d]823                TypeData * prevBase = type;
824                TypeData * curBase = type->base;
[2298f728]825                while ( curBase != nullptr ) {
[b87a5ed]826                        prevBase = curBase;
827                        curBase = curBase->base;
[68cd1ce]828                } // while
[b87a5ed]829                prevBase->base = newType;
830        } else {
831                type = newType;
[68cd1ce]832        } // if
[c0aa336]833        return this;
[3848e0e]834}
835
[c0aa336]836DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
837        if ( a ) {
[bb7422a]838                spliceBegin( attributes, a->attributes );
[c0aa336]839                a->attributes.clear();
840        } // if
841        return this;
842} // copyAttribute
843
[ba7aa2d]844DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
[b87a5ed]845        if ( p ) {
[6926a6d]846                assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
[c0aa336]847                setBase( p->type );
[2298f728]848                p->type = nullptr;
[c0aa336]849                copyAttribute( p );
[b87a5ed]850                delete p;
[68cd1ce]851        } // if
[b87a5ed]852        return this;
[3848e0e]853}
854
[ba7aa2d]855DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
[b87a5ed]856        if ( a ) {
857                assert( a->type->kind == TypeData::Array );
[c0aa336]858                setBase( a->type );
[2298f728]859                a->type = nullptr;
[c0aa336]860                copyAttribute( a );
[b87a5ed]861                delete a;
[68cd1ce]862        } // if
[b87a5ed]863        return this;
[51b7345]864}
865
[ba7aa2d]866DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
[b87a5ed]867        if ( p ) {
[e6cee92]868                assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
[b87a5ed]869                if ( type ) {
870                        switch ( type->kind ) {
[0d0931d]871                        case TypeData::Aggregate:
872                        case TypeData::Enum:
[b87a5ed]873                                p->type->base = new TypeData( TypeData::AggregateInst );
[8f6f47d7]874                                p->type->base->aggInst.aggregate = type;
[b87a5ed]875                                if ( type->kind == TypeData::Aggregate ) {
[8f6f47d7]876                                        p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
[68cd1ce]877                                } // if
[6f95000]878                                p->type->base->qualifiers |= type->qualifiers;
[b87a5ed]879                                break;
880
[0d0931d]881                        default:
[b87a5ed]882                                p->type->base = type;
[68cd1ce]883                        } // switch
[2298f728]884                        type = nullptr;
[68cd1ce]885                } // if
[b87a5ed]886                delete this;
887                return p;
888        } else {
889                return this;
[68cd1ce]890        } // if
[51b7345]891}
892
[ba7aa2d]893static TypeData * findLast( TypeData * a ) {
[b87a5ed]894        assert( a );
[ba7aa2d]895        TypeData * cur = a;
[a32b204]896        while ( cur->base ) {
[b87a5ed]897                cur = cur->base;
[68cd1ce]898        } // while
[b87a5ed]899        return cur;
[3848e0e]900}
901
[ba7aa2d]902DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
[0d0931d]903        if ( ! a ) return this;
[738e304]904        assert( a->type->kind == TypeData::Array );
905        TypeData * lastArray = findLast( a->type );
906        if ( type ) {
907                switch ( type->kind ) {
[0d0931d]908                case TypeData::Aggregate:
909                case TypeData::Enum:
[738e304]910                        lastArray->base = new TypeData( TypeData::AggregateInst );
911                        lastArray->base->aggInst.aggregate = type;
912                        if ( type->kind == TypeData::Aggregate ) {
913                                lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
914                        } // if
[6f95000]915                        lastArray->base->qualifiers |= type->qualifiers;
[738e304]916                        break;
[0d0931d]917                default:
[738e304]918                        lastArray->base = type;
919                } // switch
920                type = nullptr;
[68cd1ce]921        } // if
[738e304]922        delete this;
923        return a;
[51b7345]924}
[3848e0e]925
[ba7aa2d]926DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
927        TypeData * ftype = new TypeData( TypeData::Function );
[8f6f47d7]928        ftype->function.params = params;
[c0aa336]929        setBase( ftype );
[b87a5ed]930        return this;
[3848e0e]931}
932
[ba7aa2d]933static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
[b87a5ed]934        if ( type ) {
935                if ( type->kind != TypeData::Function ) {
936                        type->base = addIdListToType( type->base, ids );
937                } else {
[8f6f47d7]938                        type->function.idList = ids;
[68cd1ce]939                } // if
[b87a5ed]940                return type;
[3848e0e]941        } else {
[ba7aa2d]942                TypeData * newtype = new TypeData( TypeData::Function );
[8f6f47d7]943                newtype->function.idList = ids;
[b87a5ed]944                return newtype;
[68cd1ce]945        } // if
[2298f728]946} // addIdListToType
[974906e2]947
[ba7aa2d]948DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
[b87a5ed]949        type = addIdListToType( type, ids );
950        return this;
[3848e0e]951}
952
[ba7aa2d]953DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
[b87a5ed]954        initializer = init;
955        return this;
[3848e0e]956}
957
[67cf18c]958DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
[bb7422a]959        assertf( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS, "Called addTypeInitializer on something that isn't a type variable." );
[67cf18c]960        variable.initializer = init;
961        return this;
962}
963
[a46b69c]964DeclarationNode * DeclarationNode::cloneType( string * name ) {
965        DeclarationNode * newnode = newName( name );
[b87a5ed]966        newnode->type = maybeClone( type );
[a7c90d4]967        newnode->copySpecifiers( this );
[b87a5ed]968        return newnode;
[3848e0e]969}
970
[2298f728]971DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
972        if ( ! o ) return nullptr;
973
[a7c90d4]974        o->copySpecifiers( this );
[2298f728]975        if ( type ) {
976                TypeData * srcType = type;
977
[c0aa336]978                // search for the base type by scanning off pointers and array designators
[2298f728]979                while ( srcType->base ) {
980                        srcType = srcType->base;
981                } // while
982
983                TypeData * newType = srcType->clone();
984                if ( newType->kind == TypeData::AggregateInst ) {
985                        // don't duplicate members
986                        if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
987                                delete newType->aggInst.aggregate->enumeration.constants;
988                                newType->aggInst.aggregate->enumeration.constants = nullptr;
[b2da0574]989                                newType->aggInst.aggregate->enumeration.body = false;
[b87a5ed]990                        } else {
[2298f728]991                                assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
992                                delete newType->aggInst.aggregate->aggregate.fields;
993                                newType->aggInst.aggregate->aggregate.fields = nullptr;
[b2da0574]994                                newType->aggInst.aggregate->aggregate.body = false;
[68cd1ce]995                        } // if
[43c89a7]996                        // don't hoist twice
997                        newType->aggInst.hoistType = false;
[68cd1ce]998                } // if
[2298f728]999
1000                newType->forall = maybeClone( type->forall );
1001                if ( ! o->type ) {
1002                        o->type = newType;
1003                } else {
1004                        addTypeToType( newType, o->type );
1005                        delete newType;
1006                } // if
[68cd1ce]1007        } // if
[b87a5ed]1008        return o;
[51b7345]1009}
1010
[ba7aa2d]1011DeclarationNode * DeclarationNode::extractAggregate() const {
[b87a5ed]1012        if ( type ) {
[ba7aa2d]1013                TypeData * ret = typeextractAggregate( type );
[b87a5ed]1014                if ( ret ) {
[ba7aa2d]1015                        DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]1016                        newnode->type = ret;
1017                        return newnode;
[843054c2]1018                } // if
1019        } // if
[2298f728]1020        return nullptr;
[3848e0e]1021}
1022
[b38f6da]1023// If a typedef wraps an anonymous declaration, name the inner declaration so it has a consistent name across
1024// translation units.
[45e753c]1025static void nameTypedefedDecl(
1026                DeclarationNode * innerDecl,
1027                const DeclarationNode * outerDecl ) {
1028        TypeData * outer = outerDecl->type;
1029        assert( outer );
1030        // First make sure this is a typedef:
1031        if ( outer->kind != TypeData::Symbolic || !outer->symbolic.isTypedef ) {
1032                return;
1033        }
1034        TypeData * inner = innerDecl->type;
1035        assert( inner );
1036        // Always clear any CVs associated with the aggregate:
1037        inner->qualifiers.reset();
1038        // Handle anonymous aggregates: typedef struct { int i; } foo
1039        if ( inner->kind == TypeData::Aggregate && inner->aggregate.anon ) {
1040                delete inner->aggregate.name;
1041                inner->aggregate.name = new string( "__anonymous_" + *outerDecl->name );
1042                inner->aggregate.anon = false;
1043                assert( outer->base );
1044                delete outer->base->aggInst.aggregate->aggregate.name;
1045                outer->base->aggInst.aggregate->aggregate.name = new string( "__anonymous_" + *outerDecl->name );
1046                outer->base->aggInst.aggregate->aggregate.anon = false;
1047                outer->base->aggInst.aggregate->qualifiers.reset();
1048        // Handle anonymous enumeration: typedef enum { A, B, C } foo
1049        } else if ( inner->kind == TypeData::Enum && inner->enumeration.anon ) {
1050                delete inner->enumeration.name;
1051                inner->enumeration.name = new string( "__anonymous_" + *outerDecl->name );
1052                inner->enumeration.anon = false;
1053                assert( outer->base );
1054                delete outer->base->aggInst.aggregate->enumeration.name;
1055                outer->base->aggInst.aggregate->enumeration.name = new string( "__anonymous_" + *outerDecl->name );
1056                outer->base->aggInst.aggregate->enumeration.anon = false;
1057                // No qualifiers.reset() here.
1058        }
1059}
1060
1061// This code handles a special issue with the attribute transparent_union.
1062//
1063//    typedef union U { int i; } typedef_name __attribute__(( aligned(16) )) __attribute__(( transparent_union ))
1064//
1065// Here the attribute aligned goes with the typedef_name, so variables declared of this type are
1066// aligned.  However, the attribute transparent_union must be moved from the typedef_name to
1067// alias union U.  Currently, this is the only know attribute that must be moved from typedef to
1068// alias.
1069static void moveUnionAttribute( ast::Decl * decl, ast::UnionDecl * unionDecl ) {
1070        if ( auto typedefDecl = dynamic_cast<ast::TypedefDecl *>( decl ) ) {
1071                // Is the typedef alias a union aggregate?
1072                if ( nullptr == unionDecl ) return;
1073
1074                // If typedef is an alias for a union, then its alias type was hoisted above and remembered.
1075                if ( auto unionInstType = typedefDecl->base.as<ast::UnionInstType>() ) {
1076                        auto instType = ast::mutate( unionInstType );
1077                        // Remove all transparent_union attributes from typedef and move to alias union.
1078                        for ( auto attr = instType->attributes.begin() ; attr != instType->attributes.end() ; ) {
1079                                assert( *attr );
1080                                if ( (*attr)->name == "transparent_union" || (*attr)->name == "__transparent_union__" ) {
1081                                        unionDecl->attributes.emplace_back( attr->release() );
1082                                        attr = instType->attributes.erase( attr );
1083                                } else {
1084                                        attr++;
1085                                }
1086                        }
1087                        typedefDecl->base = instType;
1088                }
1089        }
1090}
1091
1092// Get the non-anonymous name of the instance type of the declaration,
1093// if one exists.
1094static const std::string * getInstTypeOfName( ast::Decl * decl ) {
1095        if ( auto dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) {
1096                if ( auto aggr = dynamic_cast<ast::BaseInstType const *>( dwt->get_type() ) ) {
1097                        if ( aggr->name.find("anonymous") == std::string::npos ) {
1098                                return &aggr->name;
1099                        }
1100                }
1101        }
1102        return nullptr;
1103}
1104
[b38f6da]1105void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::Decl>> & outputList ) {
[a16764a6]1106        SemanticErrorException errors;
[bb7422a]1107        std::back_insert_iterator<std::vector<ast::ptr<ast::Decl>>> out( outputList );
[2298f728]1108
[45e753c]1109        for ( const DeclarationNode * cur = firstNode ; cur ; cur = strict_next( cur ) ) {
[b87a5ed]1110                try {
[45e753c]1111                        bool extracted_named = false;
1112                        ast::UnionDecl * unionDecl = nullptr;
[692c1cc]1113
[ba7aa2d]1114                        if ( DeclarationNode * extr = cur->extractAggregate() ) {
[78e2fca]1115                                assert( cur->type );
[45e753c]1116                                nameTypedefedDecl( extr, cur );
[692c1cc]1117
[45e753c]1118                                if ( ast::Decl * decl = extr->build() ) {
[692c1cc]1119                                        // Remember the declaration if it is a union aggregate ?
[bb7422a]1120                                        unionDecl = dynamic_cast<ast::UnionDecl *>( decl );
[692c1cc]1121
1122                                        *out++ = decl;
[3d7e53b]1123
1124                                        // need to remember the cases where a declaration contains an anonymous aggregate definition
1125                                        assert( extr->type );
1126                                        if ( extr->type->kind == TypeData::Aggregate ) {
[692c1cc]1127                                                // typedef struct { int A } B is the only case?
[45e753c]1128                                                extracted_named = !extr->type->aggregate.anon;
[3d7e53b]1129                                        } else if ( extr->type->kind == TypeData::Enum ) {
[692c1cc]1130                                                // typedef enum { A } B is the only case?
[45e753c]1131                                                extracted_named = !extr->type->enumeration.anon;
1132                                        } else {
1133                                                extracted_named = true;
[3d7e53b]1134                                        }
[843054c2]1135                                } // if
[f39096c]1136                                delete extr;
[843054c2]1137                        } // if
[2298f728]1138
[45e753c]1139                        if ( ast::Decl * decl = cur->build() ) {
1140                                moveUnionAttribute( decl, unionDecl );
1141
1142                                if ( "" == decl->name && !cur->get_inLine() ) {
1143                                        // Don't include anonymous declaration for named aggregates,
1144                                        // but do include them for anonymous aggregates, e.g.:
1145                                        // struct S {
1146                                        //   struct T { int x; }; // no anonymous member
1147                                        //   struct { int y; };   // anonymous member
1148                                        //   struct T;            // anonymous member
1149                                        // };
1150                                        if ( extracted_named ) {
1151                                                continue;
1152                                        }
[692c1cc]1153
[45e753c]1154                                        if ( auto name = getInstTypeOfName( decl ) ) {
1155                                                // Temporary: warn about anonymous member declarations of named types, since
1156                                                // this conflicts with the syntax for the forward declaration of an anonymous type.
1157                                                SemanticWarning( cur->location, Warning::AggrForwardDecl, name->c_str() );
1158                                        }
[e07caa2]1159                                } // if
[45e753c]1160                                *out++ = decl;
[843054c2]1161                        } // if
[45e753c]1162                } catch ( SemanticErrorException & e ) {
[b87a5ed]1163                        errors.append( e );
[843054c2]1164                } // try
[e07caa2]1165        } // for
[2298f728]1166
[b87a5ed]1167        if ( ! errors.isEmpty() ) {
1168                throw errors;
[843054c2]1169        } // if
[2298f728]1170} // buildList
[3848e0e]1171
[3d7e53b]1172// currently only builds assertions, function parameters, and return values
[6611177]1173void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList ) {
[a16764a6]1174        SemanticErrorException errors;
[bb7422a]1175        std::back_insert_iterator<std::vector<ast::ptr<ast::DeclWithType>>> out( outputList );
[43c89a7]1176
[45e753c]1177        for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur ) ) {
[b87a5ed]1178                try {
[bb7422a]1179                        ast::Decl * decl = cur->build();
[45e753c]1180                        assertf( decl, "buildList: build for ast::DeclWithType." );
[bb7422a]1181                        if ( ast::DeclWithType * dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) {
[47498bd]1182                                dwt->location = cur->location;
[3ca7ef3]1183                                *out++ = dwt;
[bb7422a]1184                        } else if ( ast::StructDecl * agg = dynamic_cast<ast::StructDecl *>( decl ) ) {
[3d7e53b]1185                                // e.g., int foo(struct S) {}
[bb7422a]1186                                auto inst = new ast::StructInstType( agg->name );
1187                                auto obj = new ast::ObjectDecl( cur->location, "", inst );
1188                                obj->linkage = linkage;
[3ca7ef3]1189                                *out++ = obj;
[47498bd]1190                                delete agg;
[bb7422a]1191                        } else if ( ast::UnionDecl * agg = dynamic_cast<ast::UnionDecl *>( decl ) ) {
[3d7e53b]1192                                // e.g., int foo(union U) {}
[bb7422a]1193                                auto inst = new ast::UnionInstType( agg->name );
1194                                auto obj = new ast::ObjectDecl( cur->location,
1195                                        "", inst, nullptr, ast::Storage::Classes(),
1196                                        linkage );
[3ca7ef3]1197                                *out++ = obj;
[bb7422a]1198                        } else if ( ast::EnumDecl * agg = dynamic_cast<ast::EnumDecl *>( decl ) ) {
[3d7e53b]1199                                // e.g., int foo(enum E) {}
[bb7422a]1200                                auto inst = new ast::EnumInstType( agg->name );
1201                                auto obj = new ast::ObjectDecl( cur->location,
1202                                        "",
1203                                        inst,
1204                                        nullptr,
1205                                        ast::Storage::Classes(),
1206                                        linkage
1207                                );
[3ca7ef3]1208                                *out++ = obj;
[45e753c]1209                        } else {
1210                                assertf( false, "buildList: Could not convert to ast::DeclWithType." );
[843054c2]1211                        } // if
[45e753c]1212                } catch ( SemanticErrorException & e ) {
[b87a5ed]1213                        errors.append( e );
[843054c2]1214                } // try
[3a5131ed]1215        } // for
1216
[b87a5ed]1217        if ( ! errors.isEmpty() ) {
1218                throw errors;
[843054c2]1219        } // if
[2298f728]1220} // buildList
[3848e0e]1221
[bb7422a]1222void buildTypeList( const DeclarationNode * firstNode,
1223                std::vector<ast::ptr<ast::Type>> & outputList ) {
[a16764a6]1224        SemanticErrorException errors;
[bb7422a]1225        std::back_insert_iterator<std::vector<ast::ptr<ast::Type>>> out( outputList );
[2298f728]1226
[45e753c]1227        for ( const DeclarationNode * cur = firstNode ; cur ; cur = strict_next( cur ) ) {
[b87a5ed]1228                try {
[ba7aa2d]1229                        * out++ = cur->buildType();
[45e753c]1230                } catch ( SemanticErrorException & e ) {
[b87a5ed]1231                        errors.append( e );
[843054c2]1232                } // try
[45e753c]1233        } // for
[2298f728]1234
[b87a5ed]1235        if ( ! errors.isEmpty() ) {
1236                throw errors;
[843054c2]1237        } // if
[2298f728]1238} // buildTypeList
[51b7345]1239
[bb7422a]1240ast::Decl * DeclarationNode::build() const {
[a16764a6]1241        if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
[2298f728]1242
[e994912]1243        if ( asmStmt ) {
[bb7422a]1244                auto stmt = strict_dynamic_cast<ast::AsmStmt *>( asmStmt->build() );
1245                return new ast::AsmDecl( stmt->location, stmt );
[e994912]1246        } // if
[2d019af]1247        if ( directiveStmt ) {
[bb7422a]1248                auto stmt = strict_dynamic_cast<ast::DirectiveStmt *>( directiveStmt->build() );
1249                return new ast::DirectiveDecl( stmt->location, stmt );
[2d019af]1250        } // if
[e994912]1251
[bb7422a]1252        if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) {
[f0ecf9b]1253                // otype is internally converted to dtype + otype parameters
[bb7422a]1254                static const ast::TypeDecl::Kind kindMap[] = { ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Ftype, ast::TypeDecl::Ttype, ast::TypeDecl::Dimension };
1255                static_assert( sizeof(kindMap) / sizeof(kindMap[0]) == ast::TypeDecl::NUMBER_OF_KINDS, "DeclarationNode::build: kindMap is out of sync." );
[8f60f0b]1256                assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
[bb7422a]1257                ast::TypeDecl * ret = new ast::TypeDecl( location,
1258                        *name,
1259                        ast::Storage::Classes(),
1260                        (ast::Type *)nullptr,
1261                        kindMap[ variable.tyClass ],
1262                        variable.tyClass == ast::TypeDecl::Otype || variable.tyClass == ast::TypeDecl::DStype,
1263                        variable.initializer ? variable.initializer->buildType() : nullptr
1264                );
1265                buildList( variable.assertions, ret->assertions );
[2298f728]1266                return ret;
1267        } // if
1268
[843054c2]1269        if ( type ) {
[dd020c0]1270                // Function specifiers can only appear on a function definition/declaration.
1271                //
1272                //    inline _Noreturn int f();                 // allowed
1273                //    inline _Noreturn int g( int i );  // allowed
1274                //    inline _Noreturn int i;                   // disallowed
[fb04321]1275                if ( type->kind != TypeData::Function && funcSpecs.any() ) {
[a16764a6]1276                        SemanticError( this, "invalid function specifier for " );
[dd020c0]1277                } // if
[284da8c]1278                // Forall qualifier can only appear on a function/aggregate definition/declaration.
1279                //
1280                //    forall int f();                                   // allowed
1281                //    forall int g( int i );                    // allowed
1282                //    forall int i;                                             // disallowed
1283                if ( type->kind != TypeData::Function && type->forall ) {
1284                        SemanticError( this, "invalid type qualifier for " );
1285                } // if
[3ed994e]1286                bool isDelete = initializer && initializer->get_isDelete();
[bb7422a]1287                ast::Decl * decl = buildDecl(
1288                        type,
1289                        name ? *name : string( "" ),
1290                        storageClasses,
1291                        maybeBuild( bitfieldWidth ),
1292                        funcSpecs,
1293                        linkage,
1294                        asmName,
1295                        isDelete ? nullptr : maybeBuild( initializer ),
1296                        copy( attributes )
1297                )->set_extension( extension );
[3ed994e]1298                if ( isDelete ) {
[bb7422a]1299                        auto dwt = strict_dynamic_cast<ast::DeclWithType *>( decl );
[3ed994e]1300                        dwt->isDeleted = true;
1301                }
1302                return decl;
[843054c2]1303        } // if
[2298f728]1304
[f6e3e34]1305        if ( assert.condition ) {
[bb7422a]1306                auto cond = maybeBuild( assert.condition );
1307                auto msg = strict_dynamic_cast<ast::ConstantExpr *>( maybeCopy( assert.message ) );
1308                return new ast::StaticAssertDecl( location, cond, msg );
[f6e3e34]1309        }
1310
[dd020c0]1311        // SUE's cannot have function specifiers, either
1312        //
[79aae15]1313        //    inline _Noreturn struct S { ... };                // disallowed
1314        //    inline _Noreturn enum   E { ... };                // disallowed
[fb04321]1315        if ( funcSpecs.any() ) {
[a16764a6]1316                SemanticError( this, "invalid function specifier for " );
[843054c2]1317        } // if
[e874605]1318        if ( enumInLine ) {
[bb7422a]1319                return new ast::InlineMemberDecl( location,
1320                        *name, (ast::Type*)nullptr, storageClasses, linkage );
[e874605]1321        } // if
[dd020c0]1322        assertf( name, "ObjectDecl must a have name\n" );
[bb7422a]1323        auto ret = new ast::ObjectDecl( location,
1324                *name,
1325                (ast::Type*)nullptr,
1326                maybeBuild( initializer ),
1327                storageClasses,
1328                linkage,
1329                maybeBuild( bitfieldWidth )
1330        );
1331        ret->asmName = asmName;
1332        ret->extension = extension;
1333        return ret;
[51b7345]1334}
1335
[bb7422a]1336ast::Type * DeclarationNode::buildType() const {
[b87a5ed]1337        assert( type );
[974906e2]1338
[b87a5ed]1339        switch ( type->kind ) {
[0d0931d]1340        case TypeData::Enum:
1341        case TypeData::Aggregate: {
[bb7422a]1342                ast::BaseInstType * ret =
1343                        buildComAggInst( type, copy( attributes ), linkage );
1344                buildList( type->aggregate.actuals, ret->params );
[0d0931d]1345                return ret;
1346        }
1347        case TypeData::Symbolic: {
[bb7422a]1348                ast::TypeInstType * ret = new ast::TypeInstType(
1349                        *type->symbolic.name,
1350                        // This is just a default, the true value is not known yet.
1351                        ast::TypeDecl::Dtype,
1352                        buildQualifiers( type ),
1353                        copy( attributes ) );
1354                buildList( type->symbolic.actuals, ret->params );
[0d0931d]1355                return ret;
1356        }
1357        default:
[bb7422a]1358                ast::Type * simpletypes = typebuild( type );
1359                // copy because member is const
1360                simpletypes->attributes = attributes;
[c0aa336]1361                return simpletypes;
[b87a5ed]1362        } // switch
[3848e0e]1363}
1364
[b87a5ed]1365// Local Variables: //
1366// tab-width: 4 //
1367// mode: c++ //
1368// compile-command: "make install" //
1369// End: //
Note: See TracBrowser for help on using the repository browser.