Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    r2298f728 r5b639ee  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Sep 24 11:12:52 2016
    13 // Update Count     : 627
     12// Last Modified On : Mon Sep 12 21:03:18 2016
     13// Update Count     : 491
    1414//
    1515
     
    3131
    3232// These must remain in the same order as the corresponding DeclarationNode enumerations.
    33 const char * DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "NoStorageClass" };
    34 const char * DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoQualifier" };
    35 const char * DeclarationNode::basicTypeName[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicType" };
    36 const char * DeclarationNode::complexTypeName[] = { "_Complex", "_Imaginary", "NoComplexType" };
    37 const char * DeclarationNode::signednessName[] = { "signed", "unsigned", "NoSignedness" };
    38 const char * DeclarationNode::lengthName[] = { "short", "long", "long long", "NoLength" };
    39 const char * DeclarationNode::aggregateName[] = { "struct", "union", "context" };
    40 const char * DeclarationNode::typeClassName[] = { "otype", "dtype", "ftype" };
    41 const char * DeclarationNode::builtinTypeName[] = { "__builtin_va_list" };
     33const char *DeclarationNode::storageName[] = { "extern", "static", "auto", "register", "inline", "fortran", "_Noreturn", "_Thread_local", "NoStorageClass" };
     34const char *DeclarationNode::qualifierName[] = { "const", "restrict", "volatile", "lvalue", "_Atomic", "NoQualifier" };
     35const char *DeclarationNode::basicTypeName[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicType" };
     36const char *DeclarationNode::complexTypeName[] = { "_Complex", "_Imaginary", "NoComplexType" };
     37const char *DeclarationNode::signednessName[] = { "signed", "unsigned", "NoSignedness" };
     38const char *DeclarationNode::lengthName[] = { "short", "long", "long long", "NoLength" };
     39const char *DeclarationNode::aggregateName[] = { "struct", "union", "context" };
     40const char *DeclarationNode::typeClassName[] = { "otype", "dtype", "ftype" };
     41const char *DeclarationNode::builtinTypeName[] = { "__builtin_va_list" };
    4242
    4343UniqueName DeclarationNode::anonymous( "__anonymous" );
     
    4646
    4747DeclarationNode::DeclarationNode() :
    48                 type( nullptr ),
     48                type( 0 ),
    4949                storageClass( NoStorageClass ),
    5050                isInline( false ),
    5151                isNoreturn( false ),
    52                 bitfieldWidth( nullptr ),
    53                 initializer( nullptr ),
     52                bitfieldWidth( 0 ),
     53                initializer( 0 ),
    5454                hasEllipsis( false ),
    5555                linkage( ::linkage ),
    5656                extension( false ) {
    57 
    58         variable.name = nullptr;
    5957        variable.tyClass = DeclarationNode::Otype;
    6058        variable.assertions = nullptr;
    6159
    62         attr.name = nullptr;
    6360        attr.expr = nullptr;
    6461        attr.type = nullptr;
     
    6663
    6764DeclarationNode::~DeclarationNode() {
    68         delete attr.name;
    6965        delete attr.expr;
    7066        delete attr.type;
    71 
    72         delete variable.name;
    73         delete variable.assertions;
    74 
    7567        delete type;
    7668        delete bitfieldWidth;
     
    7870}
    7971
    80 DeclarationNode * DeclarationNode::clone() const {
    81         DeclarationNode * newnode = new DeclarationNode;
     72DeclarationNode *DeclarationNode::clone() const {
     73        DeclarationNode *newnode = new DeclarationNode;
    8274        newnode->type = maybeClone( type );
    83         newnode->name = name ? new string( *name ) : nullptr;
     75        newnode->name = name;
    8476        newnode->storageClass = storageClass;
    8577        newnode->isInline = isInline;
     
    9183        newnode->linkage = linkage;
    9284
    93         newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
    9485        newnode->variable.assertions = maybeClone( variable.assertions );
     86        newnode->variable.name = variable.name;
    9587        newnode->variable.tyClass = variable.tyClass;
    9688
    97         newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr;
    9889        newnode->attr.expr = maybeClone( attr.expr );
    9990        newnode->attr.type = maybeClone( attr.type );
     
    10798void DeclarationNode::print( std::ostream &os, int indent ) const {
    10899        os << string( indent, ' ' );
    109         if ( name ) {
    110                 os << *name << ": ";
     100        if ( name == "" ) {
     101                os << "unnamed: ";
    111102        } else {
    112                 os << "unnamed: ";
     103                os << name << ": ";
    113104        } // if
    114105
     
    131122        } // if
    132123
    133         if ( initializer ) {
     124        if ( initializer != 0 ) {
    134125                os << endl << string( indent + 2, ' ' ) << "with initializer ";
    135126                initializer->printOneLine( os );
     
    148139}
    149140
    150 DeclarationNode * DeclarationNode::newFunction( std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body, bool newStyle ) {
    151         DeclarationNode * newnode = new DeclarationNode;
    152         newnode->name = name;
     141DeclarationNode *DeclarationNode::newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle ) {
     142        DeclarationNode *newnode = new DeclarationNode;
     143        newnode->name = assign_strptr( name );
    153144
    154145        newnode->type = new TypeData( TypeData::Function );
     
    156147        newnode->type->function.newStyle = newStyle;
    157148        newnode->type->function.body = body;
    158         // ignore unnamed routine declarations: void p( int (*)(int) );
    159         if ( newnode->name ) {
    160                 typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );
    161         } // if
     149        typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID );
    162150
    163151        if ( body ) {
     
    167155        if ( ret ) {
    168156                newnode->type->base = ret->type;
    169                 ret->type = nullptr;
     157                ret->type = 0;
    170158                delete ret;
    171159        } // if
     
    175163
    176164DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) {
    177         DeclarationNode * newnode = new DeclarationNode;
     165        DeclarationNode *newnode = new DeclarationNode;
    178166        newnode->type = new TypeData();
    179167        newnode->type->qualifiers[ q ] = 1;
     
    181169} // DeclarationNode::newQualifier
    182170
    183 DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
    184         DeclarationNode * newnode = new DeclarationNode;
     171DeclarationNode * DeclarationNode::newForall( DeclarationNode *forall ) {
     172        DeclarationNode *newnode = new DeclarationNode;
    185173        newnode->type = new TypeData( TypeData::Unknown );
    186174        newnode->type->forall = forall;
     
    189177
    190178DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
    191         DeclarationNode * newnode = new DeclarationNode;
     179        DeclarationNode *newnode = new DeclarationNode;
     180        //switch (sc) {
     181        //      case Inline: newnode->isInline = true; break;
     182        //      case Noreturn: newnode->isNoreturn = true; break;
     183        //      default: newnode->storageClass = sc; break;
     184        //}
    192185        newnode->storageClass = sc;
    193186        return newnode;
     
    195188
    196189DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
    197         DeclarationNode * newnode = new DeclarationNode;
     190        DeclarationNode *newnode = new DeclarationNode;
    198191        newnode->type = new TypeData( TypeData::Basic );
    199192        newnode->type->basictype = bt;
     
    202195
    203196DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
    204         DeclarationNode * newnode = new DeclarationNode;
     197        DeclarationNode *newnode = new DeclarationNode;
    205198        newnode->type = new TypeData( TypeData::Basic );
    206199        newnode->type->complextype = ct;
     
    209202
    210203DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
    211         DeclarationNode * newnode = new DeclarationNode;
     204        DeclarationNode *newnode = new DeclarationNode;
    212205        newnode->type = new TypeData( TypeData::Basic );
    213206        newnode->type->signedness = sn;
     
    216209
    217210DeclarationNode * DeclarationNode::newLength( Length lnth ) {
    218         DeclarationNode * newnode = new DeclarationNode;
     211        DeclarationNode *newnode = new DeclarationNode;
    219212        newnode->type = new TypeData( TypeData::Basic );
    220213        newnode->type->length = lnth;
     
    222215} // DeclarationNode::newLength
    223216
    224 DeclarationNode * DeclarationNode::newFromTypedef( std::string * name ) {
    225         DeclarationNode * newnode = new DeclarationNode;
     217DeclarationNode * DeclarationNode::newFromTypedef( std::string *name ) {
     218        DeclarationNode *newnode = new DeclarationNode;
    226219        newnode->type = new TypeData( TypeData::SymbolicInst );
    227         newnode->type->symbolic.name = name ? new string( *name ) : nullptr;
     220        newnode->type->symbolic.name = assign_strptr( name );
    228221        newnode->type->symbolic.isTypedef = true;
    229         newnode->type->symbolic.params = nullptr;
     222        newnode->type->symbolic.params = 0;
    230223        return newnode;
    231224} // DeclarationNode::newFromTypedef
    232225
    233 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
    234         DeclarationNode * newnode = new DeclarationNode;
     226DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ) {
     227        DeclarationNode *newnode = new DeclarationNode;
    235228        newnode->type = new TypeData( TypeData::Aggregate );
    236229        newnode->type->aggregate.kind = kind;
    237         if ( name ) {
    238                 newnode->type->aggregate.name = new string( *name );
    239         } else {                                                                                        // anonymous aggregate ?
    240                 newnode->type->aggregate.name = new string( anonymous.newName() );
     230        newnode->type->aggregate.name = assign_strptr( name );
     231        if ( newnode->type->aggregate.name == "" ) {            // anonymous aggregate ?
     232                newnode->type->aggregate.name = anonymous.newName();
    241233        } // if
    242234        newnode->type->aggregate.actuals = actuals;
     
    246238} // DeclarationNode::newAggregate
    247239
    248 DeclarationNode * DeclarationNode::newEnum( std::string * name, DeclarationNode * constants ) {
    249         DeclarationNode * newnode = new DeclarationNode;
    250         newnode->name = name;
     240DeclarationNode *DeclarationNode::newEnum( std::string *name, DeclarationNode *constants ) {
     241        DeclarationNode *newnode = new DeclarationNode;
     242        newnode->name = assign_strptr( name );
    251243        newnode->type = new TypeData( TypeData::Enum );
    252         if ( name ) {
    253                 newnode->type->enumeration.name = new string( *name );
    254         } else {                                                                                        // anonymous aggregate ?
    255                 newnode->type->enumeration.name = new string( anonymous.newName() );
     244        newnode->type->enumeration.name = newnode->name;
     245        if ( newnode->type->enumeration.name == "" ) {          // anonymous enumeration ?
     246                newnode->type->enumeration.name = DeclarationNode::anonymous.newName();
    256247        } // if
    257248        newnode->type->enumeration.constants = constants;
     
    259250} // DeclarationNode::newEnum
    260251
    261 DeclarationNode * DeclarationNode::newEnumConstant( std::string * name, ExpressionNode * constant ) {
    262         DeclarationNode * newnode = new DeclarationNode;
    263         newnode->name = name;
     252DeclarationNode *DeclarationNode::newEnumConstant( std::string *name, ExpressionNode *constant ) {
     253        DeclarationNode *newnode = new DeclarationNode;
     254        newnode->name = assign_strptr( name );
    264255        newnode->enumeratorValue.reset( constant );
    265         typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );
     256        typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID );
    266257        return newnode;
    267258} // DeclarationNode::newEnumConstant
    268259
    269 DeclarationNode * DeclarationNode::newName( std::string * name ) {
    270         DeclarationNode * newnode = new DeclarationNode;
    271         newnode->name = name;
     260DeclarationNode *DeclarationNode::newName( std::string *name ) {
     261        DeclarationNode *newnode = new DeclarationNode;
     262        newnode->name = assign_strptr( name );
    272263        return newnode;
    273264} // DeclarationNode::newName
    274265
    275 DeclarationNode * DeclarationNode::newFromTypeGen( std::string * name, ExpressionNode * params ) {
    276         DeclarationNode * newnode = new DeclarationNode;
     266DeclarationNode *DeclarationNode::newFromTypeGen( std::string *name, ExpressionNode *params ) {
     267        DeclarationNode *newnode = new DeclarationNode;
    277268        newnode->type = new TypeData( TypeData::SymbolicInst );
    278         newnode->type->symbolic.name = name ? new string( *name ) : nullptr;
     269        newnode->type->symbolic.name = assign_strptr( name );
    279270        newnode->type->symbolic.isTypedef = false;
    280271        newnode->type->symbolic.actuals = params;
     
    282273} // DeclarationNode::newFromTypeGen
    283274
    284 DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, std::string * name ) {
    285         DeclarationNode * newnode = new DeclarationNode;
    286         newnode->name = name;
    287 //      newnode->type = new TypeData( TypeData::Variable );
    288         newnode->type = nullptr;
     275DeclarationNode *DeclarationNode::newTypeParam( TypeClass tc, std::string *name ) {
     276        DeclarationNode *newnode = new DeclarationNode;
     277        newnode->name = assign_strptr( name );
     278        newnode->type = new TypeData( TypeData::Variable );
    289279        newnode->variable.tyClass = tc;
    290         newnode->variable.name = newnode->name ? new string( *newnode->name ) : nullptr;
     280        newnode->variable.name = newnode->name;
    291281        return newnode;
    292282} // DeclarationNode::newTypeParam
    293283
    294 DeclarationNode * DeclarationNode::newTrait( const std::string * name, DeclarationNode * params, DeclarationNode * asserts ) {
    295         DeclarationNode * newnode = new DeclarationNode;
     284DeclarationNode *DeclarationNode::newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts ) {
     285        DeclarationNode *newnode = new DeclarationNode;
    296286        newnode->type = new TypeData( TypeData::Aggregate );
    297         newnode->type->aggregate.name = name;
    298287        newnode->type->aggregate.kind = Trait;
    299288        newnode->type->aggregate.params = params;
    300289        newnode->type->aggregate.fields = asserts;
     290        newnode->type->aggregate.name = assign_strptr( name );
    301291        return newnode;
    302292} // DeclarationNode::newTrait
    303293
    304 DeclarationNode * DeclarationNode::newTraitUse( const std::string * name, ExpressionNode * params ) {
    305         DeclarationNode * newnode = new DeclarationNode;
     294DeclarationNode *DeclarationNode::newTraitUse( std::string *name, ExpressionNode *params ) {
     295        DeclarationNode *newnode = new DeclarationNode;
    306296        newnode->type = new TypeData( TypeData::AggregateInst );
    307297        newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
    308298        newnode->type->aggInst.aggregate->aggregate.kind = Trait;
    309         newnode->type->aggInst.aggregate->aggregate.name = name;
     299        newnode->type->aggInst.aggregate->aggregate.name = assign_strptr( name );
    310300        newnode->type->aggInst.params = params;
    311301        return newnode;
    312302} // DeclarationNode::newTraitUse
    313303
    314 DeclarationNode * DeclarationNode::newTypeDecl( std::string * name, DeclarationNode * typeParams ) {
    315         DeclarationNode * newnode = new DeclarationNode;
    316         newnode->name = name;
     304DeclarationNode *DeclarationNode::newTypeDecl( std::string *name, DeclarationNode *typeParams ) {
     305        DeclarationNode *newnode = new DeclarationNode;
     306        newnode->name = assign_strptr( name );
    317307        newnode->type = new TypeData( TypeData::Symbolic );
    318308        newnode->type->symbolic.isTypedef = false;
     
    322312} // DeclarationNode::newTypeDecl
    323313
    324 DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers ) {
    325         DeclarationNode * newnode = new DeclarationNode;
     314DeclarationNode *DeclarationNode::newPointer( DeclarationNode *qualifiers ) {
     315        DeclarationNode *newnode = new DeclarationNode;
    326316        newnode->type = new TypeData( TypeData::Pointer );
    327317        return newnode->addQualifiers( qualifiers );
    328318} // DeclarationNode::newPointer
    329319
    330 DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
    331         DeclarationNode * newnode = new DeclarationNode;
     320DeclarationNode *DeclarationNode::newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic ) {
     321        DeclarationNode *newnode = new DeclarationNode;
    332322        newnode->type = new TypeData( TypeData::Array );
    333323        newnode->type->array.dimension = size;
    334324        newnode->type->array.isStatic = isStatic;
    335         if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) {
     325        if ( newnode->type->array.dimension == 0 || newnode->type->array.dimension->isExpressionType<ConstantExpr *>() ) {
    336326                newnode->type->array.isVarLen = false;
    337327        } else {
     
    341331} // DeclarationNode::newArray
    342332
    343 DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
    344         DeclarationNode * newnode = new DeclarationNode;
     333DeclarationNode *DeclarationNode::newVarArray( DeclarationNode *qualifiers ) {
     334        DeclarationNode *newnode = new DeclarationNode;
    345335        newnode->type = new TypeData( TypeData::Array );
    346         newnode->type->array.dimension = nullptr;
     336        newnode->type->array.dimension = 0;
    347337        newnode->type->array.isStatic = false;
    348338        newnode->type->array.isVarLen = true;
     
    350340}
    351341
    352 DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
    353         DeclarationNode * newnode = new DeclarationNode;
     342DeclarationNode *DeclarationNode::newBitfield( ExpressionNode *size ) {
     343        DeclarationNode *newnode = new DeclarationNode;
    354344        newnode->bitfieldWidth = size;
    355345        return newnode;
    356346}
    357347
    358 DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
    359         DeclarationNode * newnode = new DeclarationNode;
     348DeclarationNode *DeclarationNode::newTuple( DeclarationNode *members ) {
     349        DeclarationNode *newnode = new DeclarationNode;
    360350        newnode->type = new TypeData( TypeData::Tuple );
    361351        newnode->type->tuple = members;
     
    363353}
    364354
    365 DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
    366         DeclarationNode * newnode = new DeclarationNode;
     355DeclarationNode *DeclarationNode::newTypeof( ExpressionNode *expr ) {
     356        DeclarationNode *newnode = new DeclarationNode;
    367357        newnode->type = new TypeData( TypeData::Typeof );
    368358        newnode->type->typeexpr = expr;
     
    371361
    372362DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
    373         DeclarationNode * newnode = new DeclarationNode;
     363        DeclarationNode *newnode = new DeclarationNode;
    374364        newnode->type = new TypeData( TypeData::Builtin );
    375365        newnode->builtin = bt;
     
    377367} // DeclarationNode::newBuiltinType
    378368
    379 DeclarationNode * DeclarationNode::newAttr( std::string * name, ExpressionNode * expr ) {
    380         DeclarationNode * newnode = new DeclarationNode;
    381 //      newnode->type = new TypeData( TypeData::Attr );
    382         newnode->type = nullptr;
    383         newnode->attr.name = name;
     369DeclarationNode *DeclarationNode::newAttr( std::string *name, ExpressionNode *expr ) {
     370        DeclarationNode *newnode = new DeclarationNode;
     371        newnode->type = new TypeData( TypeData::Attr );
     372        newnode->attr.name = assign_strptr( name );
    384373        newnode->attr.expr = expr;
    385374        return newnode;
    386375}
    387376
    388 DeclarationNode * DeclarationNode::newAttr( std::string * name, DeclarationNode * type ) {
    389         DeclarationNode * newnode = new DeclarationNode;
    390 //      newnode->type = new TypeData( TypeData::Attr );
    391         newnode->type = nullptr;
    392         newnode->attr.name = name;
     377DeclarationNode *DeclarationNode::newAttr( std::string *name, DeclarationNode *type ) {
     378        DeclarationNode *newnode = new DeclarationNode;
     379        newnode->type = new TypeData( TypeData::Attr );
     380        newnode->attr.name = assign_strptr( name );
    393381        newnode->attr.type = type;
    394382        return newnode;
     383}
     384
     385static void addQualifiersToType( TypeData *&src, TypeData *dst ) {
     386        if ( src && dst ) {
     387                if ( src->forall && dst->kind == TypeData::Function ) {
     388                        if ( dst->forall ) {
     389                                dst->forall->appendList( src->forall );
     390                        } else {
     391                                dst->forall = src->forall;
     392                        } // if
     393                        src->forall = 0;
     394                } // if
     395                if ( dst->base ) {
     396                        addQualifiersToType( src, dst->base );
     397                } else if ( dst->kind == TypeData::Function ) {
     398                        dst->base = src;
     399                        src = 0;
     400                } else {
     401                        dst->qualifiers |= src->qualifiers;
     402                } // if
     403        } // if
    395404}
    396405
     
    401410} // appendError
    402411
    403 void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
     412void DeclarationNode::checkQualifiers( const TypeData *src, const TypeData *dst ) {
    404413        TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
    405414
     
    413422} // DeclarationNode::checkQualifiers
    414423
    415 void DeclarationNode::checkStorageClasses( DeclarationNode * q ) {
     424void DeclarationNode::checkStorageClasses( DeclarationNode *q ) {
    416425        if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) {
    417426                if ( storageClass == q->storageClass ) {                // duplicate qualifier
     
    425434} // DeclarationNode::copyStorageClasses
    426435
    427 DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) {
     436DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) {
    428437        isInline = isInline || q->isInline;
    429438        isNoreturn = isNoreturn || q->isNoreturn;
     
    436445} // DeclarationNode::copyStorageClasses
    437446
    438 static void addQualifiersToType( TypeData *&src, TypeData * dst ) {
    439         if ( src->forall && dst->kind == TypeData::Function ) {
    440                 if ( dst->forall ) {
    441                         dst->forall->appendList( src->forall );
     447DeclarationNode *DeclarationNode::addQualifiers( DeclarationNode *q ) {
     448        if ( q ) {
     449                checkStorageClasses( q );
     450                copyStorageClasses( q );
     451                if ( q->type ) {
     452                        if ( ! type ) {
     453                                type = new TypeData;
     454                        } else {
     455                                checkQualifiers( q->type, type );
     456                        } // if
     457                        addQualifiersToType( q->type, type );
     458                        if ( q->type && q->type->forall ) {
     459                                if ( type->forall ) {
     460                                        type->forall->appendList( q->type->forall );
     461                                } else {
     462                                        if ( type->kind == TypeData::Aggregate ) {
     463                                                type->aggregate.params = q->type->forall;
     464                                                // change implicit typedef from TYPEDEFname to TYPEGENname
     465                                                typedefTable.changeKind( type->aggregate.name, TypedefTable::TG );
     466                                        } else {
     467                                                type->forall = q->type->forall;
     468                                        } // if
     469                                } // if
     470                                q->type->forall = 0;
     471                        } // if
     472                } // if
     473        } // if
     474        delete q;
     475        return this;
     476}
     477
     478static void addTypeToType( TypeData *&src, TypeData *&dst ) {
     479        if ( src && dst ) {
     480                if ( src->forall && dst->kind == TypeData::Function ) {
     481                        if ( dst->forall ) {
     482                                dst->forall->appendList( src->forall );
     483                        } else {
     484                                dst->forall = src->forall;
     485                        } // if
     486                        src->forall = 0;
     487                } // if
     488                if ( dst->base ) {
     489                        addTypeToType( src, dst->base );
    442490                } else {
    443                         dst->forall = src->forall;
    444                 } // if
    445                 src->forall = nullptr;
    446         } // if
    447         if ( dst->base ) {
    448                 addQualifiersToType( src, dst->base );
    449         } else if ( dst->kind == TypeData::Function ) {
    450                 dst->base = src;
    451                 src = nullptr;
    452         } else {
    453                 dst->qualifiers |= src->qualifiers;
    454         } // if
    455 } // addQualifiersToType
    456 
    457 DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
    458         if ( ! q ) { delete q; return this; }
    459 
    460         checkStorageClasses( q );
    461         copyStorageClasses( q );
    462 
    463         if ( ! q->type ) {
    464                 delete q;
    465                 return this;
    466         } // if
    467 
    468         if ( ! type ) {
    469                 type = q->type;                                                                 // reuse this structure
    470                 q->type = nullptr;
    471                 delete q;
    472                 return this;
    473         } // if
    474 
    475         checkQualifiers( q->type, type );
    476         addQualifiersToType( q->type, type );
    477 
    478         if ( q->type->forall ) {
    479                 if ( type->forall ) {
    480                         type->forall->appendList( q->type->forall );
    481                 } else {
    482                         if ( type->kind == TypeData::Aggregate ) {
    483                                 type->aggregate.params = q->type->forall;
    484                                 // change implicit typedef from TYPEDEFname to TYPEGENname
    485                                 typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG );
    486                         } else {
    487                                 type->forall = q->type->forall;
    488                         } // if
    489                 } // if
    490                 q->type->forall = nullptr;
    491         } // if
    492         delete q;
    493         return this;
    494 } // addQualifiers
    495 
    496 static void addTypeToType( TypeData *&src, TypeData *&dst ) {
    497         if ( src->forall && dst->kind == TypeData::Function ) {
    498                 if ( dst->forall ) {
    499                         dst->forall->appendList( src->forall );
    500                 } else {
    501                         dst->forall = src->forall;
    502                 } // if
    503                 src->forall = nullptr;
    504         } // if
    505         if ( dst->base ) {
    506                 addTypeToType( src, dst->base );
    507         } else {
    508                 switch ( dst->kind ) {
    509                   case TypeData::Unknown:
    510                         src->qualifiers |= dst->qualifiers;
    511                         dst = src;
    512                         src = nullptr;
    513                         break;
    514                   case TypeData::Basic:
    515                         dst->qualifiers |= src->qualifiers;
    516                         if ( src->kind != TypeData::Unknown ) {
    517                                 assert( src->kind == TypeData::Basic );
    518 
    519                                 if ( dst->basictype == DeclarationNode::NoBasicType ) {
    520                                         dst->basictype = src->basictype;
    521                                 } else if ( src->basictype != DeclarationNode::NoBasicType )
    522                                         throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src );
    523 
    524                                 if ( dst->complextype == DeclarationNode::NoComplexType ) {
    525                                         dst->complextype = src->complextype;
    526                                 } else if ( src->complextype != DeclarationNode::NoComplexType )
    527                                         throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src );
    528 
    529                                 if ( dst->signedness == DeclarationNode::NoSignedness ) {
    530                                         dst->signedness = src->signedness;
    531                                 } else if ( src->signedness != DeclarationNode::NoSignedness )
    532                                         throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src );
    533 
    534                                 if ( dst->length == DeclarationNode::NoLength ) {
    535                                         dst->length = src->length;
    536                                 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
    537                                         dst->length = DeclarationNode::LongLong;
    538                                 } else if ( src->length != DeclarationNode::NoLength )
    539                                         throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src );
    540                         } // if
    541                         break;
    542                   default:
    543                         switch ( src->kind ) {
    544                           case TypeData::Aggregate:
    545                           case TypeData::Enum:
    546                                 dst->base = new TypeData( TypeData::AggregateInst );
    547                                 dst->base->aggInst.aggregate = src;
    548                                 if ( src->kind == TypeData::Aggregate ) {
    549                                         dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
     491                        switch ( dst->kind ) {
     492                          case TypeData::Unknown:
     493                                src->qualifiers |= dst->qualifiers;
     494                                dst = src;
     495                                src = 0;
     496                                break;
     497                          case TypeData::Basic:
     498                                dst->qualifiers |= src->qualifiers;
     499                                if ( src->kind != TypeData::Unknown ) {
     500                                        assert( src->kind == TypeData::Basic );
     501
     502                                        if ( dst->basictype == DeclarationNode::NoBasicType ) {
     503                                                dst->basictype = src->basictype;
     504                                        } else if ( src->basictype != DeclarationNode::NoBasicType )
     505                                                throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src );
     506
     507                                        if ( dst->complextype == DeclarationNode::NoComplexType ) {
     508                                                dst->complextype = src->complextype;
     509                                        } else if ( src->complextype != DeclarationNode::NoComplexType )
     510                                                throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src );
     511
     512                                        if ( dst->signedness == DeclarationNode::NoSignedness ) {
     513                                                dst->signedness = src->signedness;
     514                                        } else if ( src->signedness != DeclarationNode::NoSignedness )
     515                                                throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src );
     516
     517                                        if ( dst->length == DeclarationNode::NoLength ) {
     518                                                dst->length = src->length;
     519                                        } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
     520                                                dst->length = DeclarationNode::LongLong;
     521                                        } else if ( src->length != DeclarationNode::NoLength )
     522                                                throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src );
    550523                                } // if
    551                                 dst->base->qualifiers |= src->qualifiers;
    552                                 src = nullptr;
    553524                                break;
    554525                          default:
    555                                 if ( dst->forall ) {
    556                                         dst->forall->appendList( src->forall );
    557                                 } else {
    558                                         dst->forall = src->forall;
    559                                 } // if
    560                                 src->forall = nullptr;
    561                                 dst->base = src;
    562                                 src = nullptr;
     526                                switch ( src->kind ) {
     527                                  case TypeData::Aggregate:
     528                                  case TypeData::Enum:
     529                                        dst->base = new TypeData( TypeData::AggregateInst );
     530                                        dst->base->aggInst.aggregate = src;
     531                                        if ( src->kind == TypeData::Aggregate ) {
     532                                                dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
     533                                        } // if
     534                                        dst->base->qualifiers |= src->qualifiers;
     535                                        src = 0;
     536                                        break;
     537                                  default:
     538                                        if ( dst->forall ) {
     539                                                dst->forall->appendList( src->forall );
     540                                        } else {
     541                                                dst->forall = src->forall;
     542                                        } // if
     543                                        src->forall = 0;
     544                                        dst->base = src;
     545                                        src = 0;
     546                                } // switch
    563547                        } // switch
    564                 } // switch
    565         } // if
    566 }
    567 
    568 DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
     548                } // if
     549        } // if
     550}
     551
     552DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) {
    569553        if ( o ) {
    570554                checkStorageClasses( o );
     
    582566                                        type = o->type;
    583567                                } // if
    584                                 o->type = nullptr;
     568                                o->type = 0;
    585569                        } else {
    586570                                addTypeToType( o->type, type );
     
    600584}
    601585
    602 DeclarationNode * DeclarationNode::addTypedef() {
    603         TypeData * newtype = new TypeData( TypeData::Symbolic );
    604         newtype->symbolic.params = nullptr;
     586DeclarationNode *DeclarationNode::addTypedef() {
     587        TypeData *newtype = new TypeData( TypeData::Symbolic );
     588        newtype->symbolic.params = 0;
    605589        newtype->symbolic.isTypedef = true;
    606         newtype->symbolic.name = name ? new string( *name ) : nullptr;
     590        newtype->symbolic.name = name;
    607591        newtype->base = type;
    608592        type = newtype;
     
    610594}
    611595
    612 DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
    613         if ( variable.name ) {
    614                 if ( variable.assertions ) {
    615                         variable.assertions->appendList( assertions );
    616                 } else {
    617                         variable.assertions = assertions;
    618                 } // if
    619                 return this;
    620         } // if
    621 
     596DeclarationNode *DeclarationNode::addAssertions( DeclarationNode *assertions ) {
    622597        assert( type );
    623598        switch ( type->kind ) {
     
    629604                } // if
    630605                break;
    631           // case TypeData::Variable:
    632           //    if ( variable.assertions ) {
    633           //            variable.assertions->appendList( assertions );
    634           //    } else {
    635           //            variable.assertions = assertions;
    636           //    } // if
    637           //    break;
     606          case TypeData::Variable:
     607                if ( variable.assertions ) {
     608                        variable.assertions->appendList( assertions );
     609                } else {
     610                        variable.assertions = assertions;
     611                } // if
     612                break;
    638613          default:
    639614                assert( false );
     
    643618}
    644619
    645 DeclarationNode * DeclarationNode::addName( std::string * newname ) {
    646         assert( ! name );
    647         name = newname;
    648         return this;
    649 }
    650 
    651 DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
     620DeclarationNode *DeclarationNode::addName( std::string *newname ) {
     621        name = assign_strptr( newname );
     622        return this;
     623}
     624
     625DeclarationNode *DeclarationNode::addBitfield( ExpressionNode *size ) {
    652626        bitfieldWidth = size;
    653627        return this;
    654628}
    655629
    656 DeclarationNode * DeclarationNode::addVarArgs() {
     630DeclarationNode *DeclarationNode::addVarArgs() {
    657631        assert( type );
    658632        hasEllipsis = true;
     
    660634}
    661635
    662 DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body ) {
     636DeclarationNode *DeclarationNode::addFunctionBody( StatementNode *body ) {
    663637        assert( type );
    664638        assert( type->kind == TypeData::Function );
    665         assert( ! type->function.body );
     639        assert( type->function.body == 0 );
    666640        type->function.body = body;
    667641        type->function.hasBody = true;
     
    669643}
    670644
    671 DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
     645DeclarationNode *DeclarationNode::addOldDeclList( DeclarationNode *list ) {
    672646        assert( type );
    673647        assert( type->kind == TypeData::Function );
    674         assert( ! type->function.oldDeclList );
     648        assert( type->function.oldDeclList == 0 );
    675649        type->function.oldDeclList = list;
    676650        return this;
    677651}
    678652
    679 static void setBase( TypeData *&type, TypeData * newType ) {
     653static void setBase( TypeData *&type, TypeData *newType ) {
    680654        if ( type ) {
    681                 TypeData * prevBase = type;
    682                 TypeData * curBase = type->base;
    683                 while ( curBase != nullptr ) {
     655                TypeData *prevBase = type;
     656                TypeData *curBase = type->base;
     657                while ( curBase != 0 ) {
    684658                        prevBase = curBase;
    685659                        curBase = curBase->base;
     
    691665}
    692666
    693 DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
     667DeclarationNode *DeclarationNode::addPointer( DeclarationNode *p ) {
    694668        if ( p ) {
    695669                assert( p->type->kind == TypeData::Pointer );
    696670                setBase( type, p->type );
    697                 p->type = nullptr;
     671                p->type = 0;
    698672                delete p;
    699673        } // if
     
    701675}
    702676
    703 DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
     677DeclarationNode *DeclarationNode::addArray( DeclarationNode *a ) {
    704678        if ( a ) {
    705679                assert( a->type->kind == TypeData::Array );
    706680                setBase( type, a->type );
    707                 a->type = nullptr;
     681                a->type = 0;
    708682                delete a;
    709683        } // if
     
    711685}
    712686
    713 DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
     687DeclarationNode *DeclarationNode::addNewPointer( DeclarationNode *p ) {
    714688        if ( p ) {
    715689                assert( p->type->kind == TypeData::Pointer );
     
    729703                                p->type->base = type;
    730704                        } // switch
    731                         type = nullptr;
     705                        type = 0;
    732706                } // if
    733707                delete this;
     
    738712}
    739713
    740 static TypeData * findLast( TypeData * a ) {
     714static TypeData *findLast( TypeData *a ) {
    741715        assert( a );
    742         TypeData * cur = a;
     716        TypeData *cur = a;
    743717        while ( cur->base ) {
    744718                cur = cur->base;
     
    747721}
    748722
    749 DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
     723DeclarationNode *DeclarationNode::addNewArray( DeclarationNode *a ) {
    750724        if ( a ) {
    751725                assert( a->type->kind == TypeData::Array );
    752                 TypeData * lastArray = findLast( a->type );
     726                TypeData *lastArray = findLast( a->type );
    753727                if ( type ) {
    754728                        switch ( type->kind ) {
     
    765739                                lastArray->base = type;
    766740                        } // switch
    767                         type = nullptr;
     741                        type = 0;
    768742                } // if
    769743                delete this;
     
    774748}
    775749
    776 DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
    777         TypeData * ftype = new TypeData( TypeData::Function );
     750DeclarationNode *DeclarationNode::addParamList( DeclarationNode *params ) {
     751        TypeData *ftype = new TypeData( TypeData::Function );
    778752        ftype->function.params = params;
    779753        setBase( type, ftype );
     
    781755}
    782756
    783 static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
     757static TypeData *addIdListToType( TypeData *type, DeclarationNode *ids ) {
    784758        if ( type ) {
    785759                if ( type->kind != TypeData::Function ) {
     
    790764                return type;
    791765        } else {
    792                 TypeData * newtype = new TypeData( TypeData::Function );
     766                TypeData *newtype = new TypeData( TypeData::Function );
    793767                newtype->function.idList = ids;
    794768                return newtype;
    795769        } // if
    796 } // addIdListToType
    797 
    798 DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
     770}
     771
     772DeclarationNode *DeclarationNode::addIdList( DeclarationNode *ids ) {
    799773        type = addIdListToType( type, ids );
    800774        return this;
    801775}
    802776
    803 DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
     777DeclarationNode *DeclarationNode::addInitializer( InitializerNode *init ) {
     778        //assert
    804779        initializer = init;
    805780        return this;
    806781}
    807782
    808 DeclarationNode * DeclarationNode::cloneType( string * newName ) {
    809         DeclarationNode * newnode = new DeclarationNode;
     783DeclarationNode *DeclarationNode::cloneBaseType( string *newName ) {
     784        DeclarationNode *newnode = new DeclarationNode;
     785        TypeData *srcType = type;
     786        while ( srcType->base ) {
     787                srcType = srcType->base;
     788        } // while
     789        newnode->type = maybeClone( srcType );
     790        if ( newnode->type->kind == TypeData::AggregateInst ) {
     791                // don't duplicate members
     792                if ( newnode->type->aggInst.aggregate->kind == TypeData::Enum ) {
     793                        delete newnode->type->aggInst.aggregate->enumeration.constants;
     794                        newnode->type->aggInst.aggregate->enumeration.constants = 0;
     795                } else {
     796                        assert( newnode->type->aggInst.aggregate->kind == TypeData::Aggregate );
     797                        delete newnode->type->aggInst.aggregate->aggregate.fields;
     798                        newnode->type->aggInst.aggregate->aggregate.fields = 0;
     799                } // if
     800        } // if
     801        newnode->type->forall = maybeClone( type->forall );
     802        assert( storageClass == NoStorageClass );
     803        newnode->copyStorageClasses( this );
     804        newnode->name = assign_strptr( newName );
     805        return newnode;
     806}
     807
     808DeclarationNode *DeclarationNode::cloneBaseType( DeclarationNode *o ) {
     809        if ( o ) {
     810                o->copyStorageClasses( this );
     811                if ( type ) {
     812                        TypeData *srcType = type;
     813                        while ( srcType->base ) {
     814                                srcType = srcType->base;
     815                        } // while
     816                        TypeData *newType = srcType->clone();
     817                        if ( newType->kind == TypeData::AggregateInst ) {
     818                                // don't duplicate members
     819                                if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
     820                                        delete newType->aggInst.aggregate->enumeration.constants;
     821                                        newType->aggInst.aggregate->enumeration.constants = 0;
     822                                } else {
     823                                        assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
     824                                        delete newType->aggInst.aggregate->aggregate.fields;
     825                                        newType->aggInst.aggregate->aggregate.fields = 0;
     826                                } // if
     827                        } // if
     828                        newType->forall = maybeClone( type->forall );
     829                        if ( ! o->type ) {
     830                                o->type = newType;
     831                        } else {
     832                                addTypeToType( newType, o->type );
     833                                delete newType;
     834                        } // if
     835                } // if
     836        } // if
     837        return o;
     838}
     839
     840DeclarationNode *DeclarationNode::cloneType( string *newName ) {
     841        DeclarationNode *newnode = new DeclarationNode;
    810842        newnode->type = maybeClone( type );
    811843        assert( storageClass == NoStorageClass );
    812844        newnode->copyStorageClasses( this );
    813         assert( newName );
    814         newnode->name = newName;
    815         return newnode;
    816 }
    817 
    818 DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
    819         if ( ! o ) return nullptr;
    820 
    821         o->copyStorageClasses( this );
     845        newnode->name = assign_strptr( newName );
     846        return newnode;
     847}
     848
     849DeclarationNode *DeclarationNode::cloneType( DeclarationNode *o ) {
     850        if ( o ) {
     851                assert( storageClass == NoStorageClass );
     852                o->copyStorageClasses( this );
     853                if ( type ) {
     854                        TypeData *newType = type->clone();
     855                        if ( ! o->type ) {
     856                                o->type = newType;
     857                        } else {
     858                                addTypeToType( newType, o->type );
     859                                delete newType;
     860                        } // if
     861                } // if
     862        } // if
     863        delete o;
     864        return o;
     865}
     866
     867DeclarationNode *DeclarationNode::extractAggregate() const {
    822868        if ( type ) {
    823                 TypeData * srcType = type;
    824 
    825                 while ( srcType->base ) {
    826                         srcType = srcType->base;
    827                 } // while
    828 
    829                 TypeData * newType = srcType->clone();
    830                 if ( newType->kind == TypeData::AggregateInst ) {
    831                         // don't duplicate members
    832                         if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
    833                                 delete newType->aggInst.aggregate->enumeration.constants;
    834                                 newType->aggInst.aggregate->enumeration.constants = nullptr;
    835                         } else {
    836                                 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
    837                                 delete newType->aggInst.aggregate->aggregate.fields;
    838                                 newType->aggInst.aggregate->aggregate.fields = nullptr;
    839                         } // if
    840                 } // if
    841 
    842                 newType->forall = maybeClone( type->forall );
    843                 if ( ! o->type ) {
    844                         o->type = newType;
    845                 } else {
    846                         addTypeToType( newType, o->type );
    847                         delete newType;
    848                 } // if
    849         } // if
    850         return o;
    851 }
    852 
    853 DeclarationNode * DeclarationNode::extractAggregate() const {
    854         if ( type ) {
    855                 TypeData * ret = typeextractAggregate( type );
     869                TypeData *ret = typeextractAggregate( type );
    856870                if ( ret ) {
    857                         DeclarationNode * newnode = new DeclarationNode;
     871                        DeclarationNode *newnode = new DeclarationNode;
    858872                        newnode->type = ret;
    859873                        return newnode;
    860874                } // if
    861875        } // if
    862         return nullptr;
    863 }
    864 
    865 void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
     876        return 0;
     877}
     878
     879void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) {
    866880        SemanticError errors;
    867881        std::back_insert_iterator< std::list< Declaration * > > out( outputList );
    868         const DeclarationNode * cur = firstNode;
    869 
     882        const DeclarationNode *cur = firstNode;
    870883        while ( cur ) {
    871884                try {
    872                         if ( DeclarationNode * extr = cur->extractAggregate() ) {
     885                        if ( DeclarationNode *extr = cur->extractAggregate() ) {
    873886                                // handle the case where a structure declaration is contained within an object or type declaration
    874                                 Declaration * decl = extr->build();
     887                                Declaration *decl = extr->build();
    875888                                if ( decl ) {
    876                                         * out++ = decl;
     889                                        *out++ = decl;
    877890                                } // if
    878891                                delete extr;
    879892                        } // if
    880 
    881                         Declaration * decl = cur->build();
     893                        Declaration *decl = cur->build();
    882894                        if ( decl ) {
    883                                 * out++ = decl;
    884                         } // if
    885                 } catch( SemanticError &e ) {
    886                         errors.append( e );
    887                 } // try
    888                 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
    889         } // while
    890 
    891         if ( ! errors.isEmpty() ) {
    892                 throw errors;
    893         } // if
    894 } // buildList
    895 
    896 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
    897         SemanticError errors;
    898         std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
    899         const DeclarationNode * cur = firstNode;
    900         while ( cur ) {
    901                 try {
    902                         Declaration * decl = cur->build();
    903                         if ( decl ) {
    904                                 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
    905                                         * out++ = dwt;
    906                                 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
    907                                         StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
    908                                         * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
    909                                         delete agg;
    910                                 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
    911                                         UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
    912                                         * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
    913                                 } // if
     895                                *out++ = decl;
    914896                        } // if
    915897                } catch( SemanticError &e ) {
     
    921903                throw errors;
    922904        } // if
    923 } // buildList
    924 
    925 void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
     905}
     906
     907void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList ) {
    926908        SemanticError errors;
    927         std::back_insert_iterator< std::list< Type * > > out( outputList );
    928         const DeclarationNode * cur = firstNode;
    929 
     909        std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
     910        const DeclarationNode *cur = firstNode;
    930911        while ( cur ) {
    931912                try {
    932                         * out++ = cur->buildType();
     913                        Declaration *decl = cur->build();
     914                        if ( decl ) {
     915                                if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
     916                                        *out++ = dwt;
     917                                } else if ( StructDecl *agg = dynamic_cast< StructDecl * >( decl ) ) {
     918                                        StructInstType *inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
     919                                        *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
     920                                        delete agg;
     921                                } else if ( UnionDecl *agg = dynamic_cast< UnionDecl * >( decl ) ) {
     922                                        UnionInstType *inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
     923                                        *out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, 0, inst, 0 );
     924                                } // if
     925                        } // if
    933926                } catch( SemanticError &e ) {
    934927                        errors.append( e );
     
    936929                cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
    937930        } // while
    938 
    939931        if ( ! errors.isEmpty() ) {
    940932                throw errors;
    941933        } // if
    942 } // buildTypeList
    943 
    944 Declaration * DeclarationNode::build() const {
     934}
     935
     936void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList ) {
     937        SemanticError errors;
     938        std::back_insert_iterator< std::list< Type * > > out( outputList );
     939        const DeclarationNode *cur = firstNode;
     940        while ( cur ) {
     941                try {
     942                        *out++ = cur->buildType();
     943                } catch( SemanticError &e ) {
     944                        errors.append( e );
     945                } // try
     946                cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
     947        } // while
     948        if ( ! errors.isEmpty() ) {
     949                throw errors;
     950        } // if
     951}
     952
     953Declaration *DeclarationNode::build() const {
    945954        if ( ! error.empty() ) throw SemanticError( error + " in declaration of ", this );
    946 
    947         if ( variable.name ) {
    948                 static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype };
    949                 TypeDecl * ret = new TypeDecl( *variable.name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );
    950                 buildList( variable.assertions, ret->get_assertions() );
    951                 return ret;
    952         } // if
    953 
    954955        if ( type ) {
    955                 return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );
    956         } // if
    957 
     956                if ( type->kind == TypeData::Variable ) {
     957                        static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype };
     958                        TypeDecl * ret = new TypeDecl( variable.name, DeclarationNode::NoStorageClass, 0, kindMap[ variable.tyClass ] );
     959                        buildList( variable.assertions, ret->get_assertions() );
     960                        return ret;
     961                } else {
     962                        return buildDecl( type, name, storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );
     963                } // if
     964        } // if
    958965        if ( ! isInline && ! isNoreturn ) {
    959                 assertf( name, "ObjectDecl are assumed to have names\n" );
    960                 return (new ObjectDecl( *name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );
    961         } // if
    962 
     966                return (new ObjectDecl( name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );
     967        } // if
    963968        throw SemanticError( "invalid function specifier ", this );
    964969}
    965970
    966 Type * DeclarationNode::buildType() const {
     971Type *DeclarationNode::buildType() const {
    967972        assert( type );
    968 
    969         if ( attr.name ) {
    970                 AttrType * ret;
    971                 if ( attr.expr ) {
    972                         ret = new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() );
    973                 } else {
    974                         assert( attr.type );
    975                         ret = new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() );
    976                 } // if
    977                 return ret;
    978         } // if
    979973
    980974        switch ( type->kind ) {
    981975          case TypeData::Enum:
    982                 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
     976                return new EnumInstType( buildQualifiers( type ), type->enumeration.name );
    983977          case TypeData::Aggregate: {
    984                   ReferenceToType * ret;
     978                  ReferenceToType *ret;
    985979                  switch ( type->aggregate.kind ) {
    986980                        case DeclarationNode::Struct:
    987                           ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
     981                          ret = new StructInstType( buildQualifiers( type ), type->aggregate.name );
    988982                          break;
    989983                        case DeclarationNode::Union:
    990                           ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
     984                          ret = new UnionInstType( buildQualifiers( type ), type->aggregate.name );
    991985                          break;
    992986                        case DeclarationNode::Trait:
    993                           ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
     987                          ret = new TraitInstType( buildQualifiers( type ), type->aggregate.name );
    994988                          break;
    995989                        default:
     
    1000994          }
    1001995          case TypeData::Symbolic: {
    1002                   TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false );
     996                  TypeInstType *ret = new TypeInstType( buildQualifiers( type ), type->symbolic.name, false );
    1003997                  buildList( type->symbolic.actuals, ret->get_parameters() );
     998                  return ret;
     999          }
     1000          case TypeData::Attr: {
     1001                  assert( type->kind == TypeData::Attr );
     1002                  // assert( type->attr );
     1003                  AttrType * ret;
     1004                  if ( attr.expr ) {
     1005                          ret = new AttrType( buildQualifiers( type ), attr.name, attr.expr->build() );
     1006                  } else {
     1007                          assert( attr.type );
     1008                          ret = new AttrType( buildQualifiers( type ), attr.name, attr.type->buildType() );
     1009                  } // if
    10041010                  return ret;
    10051011          }
Note: See TracChangeset for help on using the changeset viewer.