source: src/Parser/DeclarationNode.cc @ 3a55d9f

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 3a55d9f was 033ff37, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

remove attribute expression '@'name mechanism

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