Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    rfb114fa1 r5b639ee  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Sep 26 22:18:40 2016
    13 // Update Count     : 640
     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;
     85        newnode->variable.assertions = maybeClone( variable.assertions );
     86        newnode->variable.name = variable.name;
    9487        newnode->variable.tyClass = variable.tyClass;
    95         newnode->variable.assertions = maybeClone( variable.assertions );
    96 
    97         newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr;
     88
    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( 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 );
     144
    153145        newnode->type = new TypeData( TypeData::Function );
    154146        newnode->type->function.params = param;
    155147        newnode->type->function.newStyle = newStyle;
    156148        newnode->type->function.body = body;
    157         // ignore unnamed routine declarations: void p( int (*)(int) );
    158         if ( newnode->name ) {
    159                 typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );
    160         } // if
     149        typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID );
    161150
    162151        if ( body ) {
     
    166155        if ( ret ) {
    167156                newnode->type->base = ret->type;
    168                 ret->type = nullptr;
     157                ret->type = 0;
    169158                delete ret;
    170159        } // if
     
    174163
    175164DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) {
    176         DeclarationNode * newnode = new DeclarationNode;
     165        DeclarationNode *newnode = new DeclarationNode;
    177166        newnode->type = new TypeData();
    178167        newnode->type->qualifiers[ q ] = 1;
     
    180169} // DeclarationNode::newQualifier
    181170
    182 DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
    183         DeclarationNode * newnode = new DeclarationNode;
     171DeclarationNode * DeclarationNode::newForall( DeclarationNode *forall ) {
     172        DeclarationNode *newnode = new DeclarationNode;
    184173        newnode->type = new TypeData( TypeData::Unknown );
    185174        newnode->type->forall = forall;
     
    188177
    189178DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
    190         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        //}
    191185        newnode->storageClass = sc;
    192186        return newnode;
     
    194188
    195189DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
    196         DeclarationNode * newnode = new DeclarationNode;
     190        DeclarationNode *newnode = new DeclarationNode;
    197191        newnode->type = new TypeData( TypeData::Basic );
    198192        newnode->type->basictype = bt;
     
    201195
    202196DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
    203         DeclarationNode * newnode = new DeclarationNode;
     197        DeclarationNode *newnode = new DeclarationNode;
    204198        newnode->type = new TypeData( TypeData::Basic );
    205199        newnode->type->complextype = ct;
     
    208202
    209203DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
    210         DeclarationNode * newnode = new DeclarationNode;
     204        DeclarationNode *newnode = new DeclarationNode;
    211205        newnode->type = new TypeData( TypeData::Basic );
    212206        newnode->type->signedness = sn;
     
    215209
    216210DeclarationNode * DeclarationNode::newLength( Length lnth ) {
    217         DeclarationNode * newnode = new DeclarationNode;
     211        DeclarationNode *newnode = new DeclarationNode;
    218212        newnode->type = new TypeData( TypeData::Basic );
    219213        newnode->type->length = lnth;
     
    221215} // DeclarationNode::newLength
    222216
    223 DeclarationNode * DeclarationNode::newFromTypedef( string * name ) {
    224         DeclarationNode * newnode = new DeclarationNode;
     217DeclarationNode * DeclarationNode::newFromTypedef( std::string *name ) {
     218        DeclarationNode *newnode = new DeclarationNode;
    225219        newnode->type = new TypeData( TypeData::SymbolicInst );
    226         newnode->type->symbolic.name = name;
     220        newnode->type->symbolic.name = assign_strptr( name );
    227221        newnode->type->symbolic.isTypedef = true;
    228         newnode->type->symbolic.params = nullptr;
     222        newnode->type->symbolic.params = 0;
    229223        return newnode;
    230224} // DeclarationNode::newFromTypedef
    231225
    232 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
    233         DeclarationNode * newnode = new DeclarationNode;
     226DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ) {
     227        DeclarationNode *newnode = new DeclarationNode;
    234228        newnode->type = new TypeData( TypeData::Aggregate );
    235229        newnode->type->aggregate.kind = kind;
    236         if ( name ) {
    237                 newnode->type->aggregate.name = name;
    238         } else {                                                                                        // anonymous aggregate ?
    239                 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();
    240233        } // if
    241234        newnode->type->aggregate.actuals = actuals;
     
    245238} // DeclarationNode::newAggregate
    246239
    247 DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants ) {
    248         DeclarationNode * newnode = new DeclarationNode;
     240DeclarationNode *DeclarationNode::newEnum( std::string *name, DeclarationNode *constants ) {
     241        DeclarationNode *newnode = new DeclarationNode;
     242        newnode->name = assign_strptr( name );
    249243        newnode->type = new TypeData( TypeData::Enum );
    250         if ( name ) {
    251                 newnode->type->enumeration.name = name;
    252         } else {                                                                                        // anonymous aggregate ?
    253                 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();
    254247        } // if
    255248        newnode->type->enumeration.constants = constants;
     
    257250} // DeclarationNode::newEnum
    258251
    259 DeclarationNode * DeclarationNode::newEnumConstant( string * name, ExpressionNode * constant ) {
    260         DeclarationNode * newnode = new DeclarationNode;
    261         newnode->name = name;
     252DeclarationNode *DeclarationNode::newEnumConstant( std::string *name, ExpressionNode *constant ) {
     253        DeclarationNode *newnode = new DeclarationNode;
     254        newnode->name = assign_strptr( name );
    262255        newnode->enumeratorValue.reset( constant );
    263         typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );
     256        typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID );
    264257        return newnode;
    265258} // DeclarationNode::newEnumConstant
    266259
    267 DeclarationNode * DeclarationNode::newName( string * name ) {
    268         DeclarationNode * newnode = new DeclarationNode;
    269         newnode->name = name;
     260DeclarationNode *DeclarationNode::newName( std::string *name ) {
     261        DeclarationNode *newnode = new DeclarationNode;
     262        newnode->name = assign_strptr( name );
    270263        return newnode;
    271264} // DeclarationNode::newName
    272265
    273 DeclarationNode * DeclarationNode::newFromTypeGen( string * name, ExpressionNode * params ) {
    274         DeclarationNode * newnode = new DeclarationNode;
     266DeclarationNode *DeclarationNode::newFromTypeGen( std::string *name, ExpressionNode *params ) {
     267        DeclarationNode *newnode = new DeclarationNode;
    275268        newnode->type = new TypeData( TypeData::SymbolicInst );
    276         newnode->type->symbolic.name = name;
     269        newnode->type->symbolic.name = assign_strptr( name );
    277270        newnode->type->symbolic.isTypedef = false;
    278271        newnode->type->symbolic.actuals = params;
     
    280273} // DeclarationNode::newFromTypeGen
    281274
    282 DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, string * name ) {
    283         DeclarationNode * newnode = new DeclarationNode;
    284         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 );
    285279        newnode->variable.tyClass = tc;
    286         newnode->variable.name = name;
     280        newnode->variable.name = newnode->name;
    287281        return newnode;
    288282} // DeclarationNode::newTypeParam
    289283
    290 DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
    291         DeclarationNode * newnode = new DeclarationNode;
     284DeclarationNode *DeclarationNode::newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts ) {
     285        DeclarationNode *newnode = new DeclarationNode;
    292286        newnode->type = new TypeData( TypeData::Aggregate );
    293         newnode->type->aggregate.name = name;
    294287        newnode->type->aggregate.kind = Trait;
    295288        newnode->type->aggregate.params = params;
    296289        newnode->type->aggregate.fields = asserts;
     290        newnode->type->aggregate.name = assign_strptr( name );
    297291        return newnode;
    298292} // DeclarationNode::newTrait
    299293
    300 DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
    301         DeclarationNode * newnode = new DeclarationNode;
     294DeclarationNode *DeclarationNode::newTraitUse( std::string *name, ExpressionNode *params ) {
     295        DeclarationNode *newnode = new DeclarationNode;
    302296        newnode->type = new TypeData( TypeData::AggregateInst );
    303297        newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
    304298        newnode->type->aggInst.aggregate->aggregate.kind = Trait;
    305         newnode->type->aggInst.aggregate->aggregate.name = name;
     299        newnode->type->aggInst.aggregate->aggregate.name = assign_strptr( name );
    306300        newnode->type->aggInst.params = params;
    307301        return newnode;
    308302} // DeclarationNode::newTraitUse
    309303
    310 DeclarationNode * DeclarationNode::newTypeDecl( string * name, DeclarationNode * typeParams ) {
    311         DeclarationNode * newnode = new DeclarationNode;
     304DeclarationNode *DeclarationNode::newTypeDecl( std::string *name, DeclarationNode *typeParams ) {
     305        DeclarationNode *newnode = new DeclarationNode;
     306        newnode->name = assign_strptr( name );
    312307        newnode->type = new TypeData( TypeData::Symbolic );
    313308        newnode->type->symbolic.isTypedef = false;
    314309        newnode->type->symbolic.params = typeParams;
    315         newnode->type->symbolic.name = name;
     310        newnode->type->symbolic.name = newnode->name;
    316311        return newnode;
    317312} // DeclarationNode::newTypeDecl
    318313
    319 DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers ) {
    320         DeclarationNode * newnode = new DeclarationNode;
     314DeclarationNode *DeclarationNode::newPointer( DeclarationNode *qualifiers ) {
     315        DeclarationNode *newnode = new DeclarationNode;
    321316        newnode->type = new TypeData( TypeData::Pointer );
    322317        return newnode->addQualifiers( qualifiers );
    323318} // DeclarationNode::newPointer
    324319
    325 DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
    326         DeclarationNode * newnode = new DeclarationNode;
     320DeclarationNode *DeclarationNode::newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic ) {
     321        DeclarationNode *newnode = new DeclarationNode;
    327322        newnode->type = new TypeData( TypeData::Array );
    328323        newnode->type->array.dimension = size;
    329324        newnode->type->array.isStatic = isStatic;
    330         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 *>() ) {
    331326                newnode->type->array.isVarLen = false;
    332327        } else {
     
    336331} // DeclarationNode::newArray
    337332
    338 DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
    339         DeclarationNode * newnode = new DeclarationNode;
     333DeclarationNode *DeclarationNode::newVarArray( DeclarationNode *qualifiers ) {
     334        DeclarationNode *newnode = new DeclarationNode;
    340335        newnode->type = new TypeData( TypeData::Array );
    341         newnode->type->array.dimension = nullptr;
     336        newnode->type->array.dimension = 0;
    342337        newnode->type->array.isStatic = false;
    343338        newnode->type->array.isVarLen = true;
     
    345340}
    346341
    347 DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
    348         DeclarationNode * newnode = new DeclarationNode;
     342DeclarationNode *DeclarationNode::newBitfield( ExpressionNode *size ) {
     343        DeclarationNode *newnode = new DeclarationNode;
    349344        newnode->bitfieldWidth = size;
    350345        return newnode;
    351346}
    352347
    353 DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
    354         DeclarationNode * newnode = new DeclarationNode;
     348DeclarationNode *DeclarationNode::newTuple( DeclarationNode *members ) {
     349        DeclarationNode *newnode = new DeclarationNode;
    355350        newnode->type = new TypeData( TypeData::Tuple );
    356351        newnode->type->tuple = members;
     
    358353}
    359354
    360 DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
    361         DeclarationNode * newnode = new DeclarationNode;
     355DeclarationNode *DeclarationNode::newTypeof( ExpressionNode *expr ) {
     356        DeclarationNode *newnode = new DeclarationNode;
    362357        newnode->type = new TypeData( TypeData::Typeof );
    363358        newnode->type->typeexpr = expr;
     
    366361
    367362DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
    368         DeclarationNode * newnode = new DeclarationNode;
     363        DeclarationNode *newnode = new DeclarationNode;
    369364        newnode->type = new TypeData( TypeData::Builtin );
    370365        newnode->builtin = bt;
     
    372367} // DeclarationNode::newBuiltinType
    373368
    374 DeclarationNode * DeclarationNode::newAttr( string * name, ExpressionNode * expr ) {
    375         DeclarationNode * newnode = new DeclarationNode;
    376         newnode->type = nullptr;
    377         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 );
    378373        newnode->attr.expr = expr;
    379374        return newnode;
    380375}
    381376
    382 DeclarationNode * DeclarationNode::newAttr( string * name, DeclarationNode * type ) {
    383         DeclarationNode * newnode = new DeclarationNode;
    384         newnode->type = nullptr;
    385         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 );
    386381        newnode->attr.type = type;
    387382        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
    388404}
    389405
     
    394410} // appendError
    395411
    396 void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
     412void DeclarationNode::checkQualifiers( const TypeData *src, const TypeData *dst ) {
    397413        TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
    398414
     
    406422} // DeclarationNode::checkQualifiers
    407423
    408 void DeclarationNode::checkStorageClasses( DeclarationNode * q ) {
     424void DeclarationNode::checkStorageClasses( DeclarationNode *q ) {
    409425        if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) {
    410426                if ( storageClass == q->storageClass ) {                // duplicate qualifier
     
    418434} // DeclarationNode::copyStorageClasses
    419435
    420 DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) {
     436DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) {
    421437        isInline = isInline || q->isInline;
    422438        isNoreturn = isNoreturn || q->isNoreturn;
     
    429445} // DeclarationNode::copyStorageClasses
    430446
    431 static void addQualifiersToType( TypeData *&src, TypeData * dst ) {
    432         if ( src->forall && dst->kind == TypeData::Function ) {
    433                 if ( dst->forall ) {
    434                         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 );
    435490                } else {
    436                         dst->forall = src->forall;
    437                 } // if
    438                 src->forall = nullptr;
    439         } // if
    440         if ( dst->base ) {
    441                 addQualifiersToType( src, dst->base );
    442         } else if ( dst->kind == TypeData::Function ) {
    443                 dst->base = src;
    444                 src = nullptr;
    445         } else {
    446                 dst->qualifiers |= src->qualifiers;
    447         } // if
    448 } // addQualifiersToType
    449 
    450 DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
    451         if ( ! q ) { delete q; return this; }
    452 
    453         checkStorageClasses( q );
    454         copyStorageClasses( q );
    455 
    456         if ( ! q->type ) {
    457                 delete q;
    458                 return this;
    459         } // if
    460 
    461         if ( ! type ) {
    462                 type = q->type;                                                                 // reuse this structure
    463                 q->type = nullptr;
    464                 delete q;
    465                 return this;
    466         } // if
    467 
    468         checkQualifiers( q->type, type );
    469         addQualifiersToType( q->type, type );
    470 
    471         if ( q->type->forall ) {
    472                 if ( type->forall ) {
    473                         type->forall->appendList( q->type->forall );
    474                 } else {
    475                         if ( type->kind == TypeData::Aggregate ) {
    476                                 type->aggregate.params = q->type->forall;
    477                                 // change implicit typedef from TYPEDEFname to TYPEGENname
    478                                 typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG );
    479                         } else {
    480                                 type->forall = q->type->forall;
    481                         } // if
    482                 } // if
    483                 q->type->forall = nullptr;
    484         } // if
    485         delete q;
    486         return this;
    487 } // addQualifiers
    488 
    489 static void addTypeToType( TypeData *&src, TypeData *&dst ) {
    490         if ( src->forall && dst->kind == TypeData::Function ) {
    491                 if ( dst->forall ) {
    492                         dst->forall->appendList( src->forall );
    493                 } else {
    494                         dst->forall = src->forall;
    495                 } // if
    496                 src->forall = nullptr;
    497         } // if
    498         if ( dst->base ) {
    499                 addTypeToType( src, dst->base );
    500         } else {
    501                 switch ( dst->kind ) {
    502                   case TypeData::Unknown:
    503                         src->qualifiers |= dst->qualifiers;
    504                         dst = src;
    505                         src = nullptr;
    506                         break;
    507                   case TypeData::Basic:
    508                         dst->qualifiers |= src->qualifiers;
    509                         if ( src->kind != TypeData::Unknown ) {
    510                                 assert( src->kind == TypeData::Basic );
    511 
    512                                 if ( dst->basictype == DeclarationNode::NoBasicType ) {
    513                                         dst->basictype = src->basictype;
    514                                 } else if ( src->basictype != DeclarationNode::NoBasicType )
    515                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src );
    516 
    517                                 if ( dst->complextype == DeclarationNode::NoComplexType ) {
    518                                         dst->complextype = src->complextype;
    519                                 } else if ( src->complextype != DeclarationNode::NoComplexType )
    520                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src );
    521 
    522                                 if ( dst->signedness == DeclarationNode::NoSignedness ) {
    523                                         dst->signedness = src->signedness;
    524                                 } else if ( src->signedness != DeclarationNode::NoSignedness )
    525                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src );
    526 
    527                                 if ( dst->length == DeclarationNode::NoLength ) {
    528                                         dst->length = src->length;
    529                                 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
    530                                         dst->length = DeclarationNode::LongLong;
    531                                 } else if ( src->length != DeclarationNode::NoLength )
    532                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src );
    533                         } // if
    534                         break;
    535                   default:
    536                         switch ( src->kind ) {
    537                           case TypeData::Aggregate:
    538                           case TypeData::Enum:
    539                                 dst->base = new TypeData( TypeData::AggregateInst );
    540                                 dst->base->aggInst.aggregate = src;
    541                                 if ( src->kind == TypeData::Aggregate ) {
    542                                         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 );
    543523                                } // if
    544                                 dst->base->qualifiers |= src->qualifiers;
    545                                 src = nullptr;
    546524                                break;
    547525                          default:
    548                                 if ( dst->forall ) {
    549                                         dst->forall->appendList( src->forall );
    550                                 } else {
    551                                         dst->forall = src->forall;
    552                                 } // if
    553                                 src->forall = nullptr;
    554                                 dst->base = src;
    555                                 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
    556547                        } // switch
    557                 } // switch
    558         } // if
    559 }
    560 
    561 DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
     548                } // if
     549        } // if
     550}
     551
     552DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) {
    562553        if ( o ) {
    563554                checkStorageClasses( o );
     
    575566                                        type = o->type;
    576567                                } // if
    577                                 o->type = nullptr;
     568                                o->type = 0;
    578569                        } else {
    579570                                addTypeToType( o->type, type );
     
    593584}
    594585
    595 DeclarationNode * DeclarationNode::addTypedef() {
    596         TypeData * newtype = new TypeData( TypeData::Symbolic );
    597         newtype->symbolic.params = nullptr;
     586DeclarationNode *DeclarationNode::addTypedef() {
     587        TypeData *newtype = new TypeData( TypeData::Symbolic );
     588        newtype->symbolic.params = 0;
    598589        newtype->symbolic.isTypedef = true;
    599         newtype->symbolic.name = name ? new string( *name ) : nullptr;
     590        newtype->symbolic.name = name;
    600591        newtype->base = type;
    601592        type = newtype;
     
    603594}
    604595
    605 DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
    606         if ( variable.name ) {
    607                 if ( variable.assertions ) {
    608                         variable.assertions->appendList( assertions );
    609                 } else {
    610                         variable.assertions = assertions;
    611                 } // if
    612                 return this;
    613         } // if
    614 
     596DeclarationNode *DeclarationNode::addAssertions( DeclarationNode *assertions ) {
    615597        assert( type );
    616598        switch ( type->kind ) {
     
    622604                } // if
    623605                break;
    624           // case TypeData::Variable:
    625           //    if ( variable.assertions ) {
    626           //            variable.assertions->appendList( assertions );
    627           //    } else {
    628           //            variable.assertions = assertions;
    629           //    } // if
    630           //    break;
     606          case TypeData::Variable:
     607                if ( variable.assertions ) {
     608                        variable.assertions->appendList( assertions );
     609                } else {
     610                        variable.assertions = assertions;
     611                } // if
     612                break;
    631613          default:
    632614                assert( false );
     
    636618}
    637619
    638 DeclarationNode * DeclarationNode::addName( string * newname ) {
    639         assert( ! name );
    640         name = newname;
    641         return this;
    642 }
    643 
    644 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 ) {
    645626        bitfieldWidth = size;
    646627        return this;
    647628}
    648629
    649 DeclarationNode * DeclarationNode::addVarArgs() {
     630DeclarationNode *DeclarationNode::addVarArgs() {
    650631        assert( type );
    651632        hasEllipsis = true;
     
    653634}
    654635
    655 DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body ) {
     636DeclarationNode *DeclarationNode::addFunctionBody( StatementNode *body ) {
    656637        assert( type );
    657638        assert( type->kind == TypeData::Function );
    658         assert( ! type->function.body );
     639        assert( type->function.body == 0 );
    659640        type->function.body = body;
    660641        type->function.hasBody = true;
     
    662643}
    663644
    664 DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
     645DeclarationNode *DeclarationNode::addOldDeclList( DeclarationNode *list ) {
    665646        assert( type );
    666647        assert( type->kind == TypeData::Function );
    667         assert( ! type->function.oldDeclList );
     648        assert( type->function.oldDeclList == 0 );
    668649        type->function.oldDeclList = list;
    669650        return this;
    670651}
    671652
    672 static void setBase( TypeData *&type, TypeData * newType ) {
     653static void setBase( TypeData *&type, TypeData *newType ) {
    673654        if ( type ) {
    674                 TypeData * prevBase = type;
    675                 TypeData * curBase = type->base;
    676                 while ( curBase != nullptr ) {
     655                TypeData *prevBase = type;
     656                TypeData *curBase = type->base;
     657                while ( curBase != 0 ) {
    677658                        prevBase = curBase;
    678659                        curBase = curBase->base;
     
    684665}
    685666
    686 DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
     667DeclarationNode *DeclarationNode::addPointer( DeclarationNode *p ) {
    687668        if ( p ) {
    688669                assert( p->type->kind == TypeData::Pointer );
    689670                setBase( type, p->type );
    690                 p->type = nullptr;
     671                p->type = 0;
    691672                delete p;
    692673        } // if
     
    694675}
    695676
    696 DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
     677DeclarationNode *DeclarationNode::addArray( DeclarationNode *a ) {
    697678        if ( a ) {
    698679                assert( a->type->kind == TypeData::Array );
    699680                setBase( type, a->type );
    700                 a->type = nullptr;
     681                a->type = 0;
    701682                delete a;
    702683        } // if
     
    704685}
    705686
    706 DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
     687DeclarationNode *DeclarationNode::addNewPointer( DeclarationNode *p ) {
    707688        if ( p ) {
    708689                assert( p->type->kind == TypeData::Pointer );
     
    722703                                p->type->base = type;
    723704                        } // switch
    724                         type = nullptr;
     705                        type = 0;
    725706                } // if
    726707                delete this;
     
    731712}
    732713
    733 static TypeData * findLast( TypeData * a ) {
     714static TypeData *findLast( TypeData *a ) {
    734715        assert( a );
    735         TypeData * cur = a;
     716        TypeData *cur = a;
    736717        while ( cur->base ) {
    737718                cur = cur->base;
     
    740721}
    741722
    742 DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
     723DeclarationNode *DeclarationNode::addNewArray( DeclarationNode *a ) {
    743724        if ( a ) {
    744725                assert( a->type->kind == TypeData::Array );
    745                 TypeData * lastArray = findLast( a->type );
     726                TypeData *lastArray = findLast( a->type );
    746727                if ( type ) {
    747728                        switch ( type->kind ) {
     
    758739                                lastArray->base = type;
    759740                        } // switch
    760                         type = nullptr;
     741                        type = 0;
    761742                } // if
    762743                delete this;
     
    767748}
    768749
    769 DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
    770         TypeData * ftype = new TypeData( TypeData::Function );
     750DeclarationNode *DeclarationNode::addParamList( DeclarationNode *params ) {
     751        TypeData *ftype = new TypeData( TypeData::Function );
    771752        ftype->function.params = params;
    772753        setBase( type, ftype );
     
    774755}
    775756
    776 static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
     757static TypeData *addIdListToType( TypeData *type, DeclarationNode *ids ) {
    777758        if ( type ) {
    778759                if ( type->kind != TypeData::Function ) {
     
    783764                return type;
    784765        } else {
    785                 TypeData * newtype = new TypeData( TypeData::Function );
     766                TypeData *newtype = new TypeData( TypeData::Function );
    786767                newtype->function.idList = ids;
    787768                return newtype;
    788769        } // if
    789 } // addIdListToType
    790 
    791 DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
     770}
     771
     772DeclarationNode *DeclarationNode::addIdList( DeclarationNode *ids ) {
    792773        type = addIdListToType( type, ids );
    793774        return this;
    794775}
    795776
    796 DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
     777DeclarationNode *DeclarationNode::addInitializer( InitializerNode *init ) {
     778        //assert
    797779        initializer = init;
    798780        return this;
    799781}
    800782
    801 DeclarationNode * DeclarationNode::cloneType( string * newName ) {
    802         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;
    803842        newnode->type = maybeClone( type );
    804843        assert( storageClass == NoStorageClass );
    805844        newnode->copyStorageClasses( this );
    806         assert( newName );
    807         newnode->name = newName;
    808         return newnode;
    809 }
    810 
    811 DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
    812         if ( ! o ) return nullptr;
    813 
    814         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 {
    815868        if ( type ) {
    816                 TypeData * srcType = type;
    817 
    818                 while ( srcType->base ) {
    819                         srcType = srcType->base;
    820                 } // while
    821 
    822                 TypeData * newType = srcType->clone();
    823                 if ( newType->kind == TypeData::AggregateInst ) {
    824                         // don't duplicate members
    825                         if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
    826                                 delete newType->aggInst.aggregate->enumeration.constants;
    827                                 newType->aggInst.aggregate->enumeration.constants = nullptr;
    828                         } else {
    829                                 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
    830                                 delete newType->aggInst.aggregate->aggregate.fields;
    831                                 newType->aggInst.aggregate->aggregate.fields = nullptr;
    832                         } // if
    833                 } // if
    834 
    835                 newType->forall = maybeClone( type->forall );
    836                 if ( ! o->type ) {
    837                         o->type = newType;
    838                 } else {
    839                         addTypeToType( newType, o->type );
    840                         delete newType;
    841                 } // if
    842         } // if
    843         return o;
    844 }
    845 
    846 DeclarationNode * DeclarationNode::extractAggregate() const {
    847         if ( type ) {
    848                 TypeData * ret = typeextractAggregate( type );
     869                TypeData *ret = typeextractAggregate( type );
    849870                if ( ret ) {
    850                         DeclarationNode * newnode = new DeclarationNode;
     871                        DeclarationNode *newnode = new DeclarationNode;
    851872                        newnode->type = ret;
    852873                        return newnode;
    853874                } // if
    854875        } // if
    855         return nullptr;
    856 }
    857 
    858 void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
     876        return 0;
     877}
     878
     879void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) {
    859880        SemanticError errors;
    860881        std::back_insert_iterator< std::list< Declaration * > > out( outputList );
    861         const DeclarationNode * cur = firstNode;
    862 
     882        const DeclarationNode *cur = firstNode;
    863883        while ( cur ) {
    864884                try {
    865                         if ( DeclarationNode * extr = cur->extractAggregate() ) {
     885                        if ( DeclarationNode *extr = cur->extractAggregate() ) {
    866886                                // handle the case where a structure declaration is contained within an object or type declaration
    867                                 Declaration * decl = extr->build();
     887                                Declaration *decl = extr->build();
    868888                                if ( decl ) {
    869                                         * out++ = decl;
     889                                        *out++ = decl;
    870890                                } // if
    871891                                delete extr;
    872892                        } // if
    873 
    874                         Declaration * decl = cur->build();
     893                        Declaration *decl = cur->build();
    875894                        if ( decl ) {
    876                                 * out++ = decl;
    877                         } // if
    878                 } catch( SemanticError &e ) {
    879                         errors.append( e );
    880                 } // try
    881                 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
    882         } // while
    883 
    884         if ( ! errors.isEmpty() ) {
    885                 throw errors;
    886         } // if
    887 } // buildList
    888 
    889 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
    890         SemanticError errors;
    891         std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
    892         const DeclarationNode * cur = firstNode;
    893         while ( cur ) {
    894                 try {
    895                         Declaration * decl = cur->build();
    896                         if ( decl ) {
    897                                 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
    898                                         * out++ = dwt;
    899                                 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
    900                                         StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
    901                                         * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
    902                                         delete agg;
    903                                 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
    904                                         UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
    905                                         * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
    906                                 } // if
     895                                *out++ = decl;
    907896                        } // if
    908897                } catch( SemanticError &e ) {
     
    914903                throw errors;
    915904        } // if
    916 } // buildList
    917 
    918 void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
     905}
     906
     907void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList ) {
    919908        SemanticError errors;
    920         std::back_insert_iterator< std::list< Type * > > out( outputList );
    921         const DeclarationNode * cur = firstNode;
    922 
     909        std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
     910        const DeclarationNode *cur = firstNode;
    923911        while ( cur ) {
    924912                try {
    925                         * 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
    926926                } catch( SemanticError &e ) {
    927927                        errors.append( e );
     
    929929                cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
    930930        } // while
    931 
    932931        if ( ! errors.isEmpty() ) {
    933932                throw errors;
    934933        } // if
    935 } // buildTypeList
    936 
    937 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 {
    938954        if ( ! error.empty() ) throw SemanticError( error + " in declaration of ", this );
    939 
    940         if ( variable.name ) {
    941                 static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype };
    942                 TypeDecl * ret = new TypeDecl( *variable.name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );
    943                 buildList( variable.assertions, ret->get_assertions() );
    944                 return ret;
    945         } // if
    946 
    947955        if ( type ) {
    948                 return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );
    949         } // if
    950 
     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
    951965        if ( ! isInline && ! isNoreturn ) {
    952                 assertf( name, "ObjectDecl are assumed to have names\n" );
    953                 return (new ObjectDecl( *name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );
    954         } // if
    955 
     966                return (new ObjectDecl( name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );
     967        } // if
    956968        throw SemanticError( "invalid function specifier ", this );
    957969}
    958970
    959 Type * DeclarationNode::buildType() const {
     971Type *DeclarationNode::buildType() const {
    960972        assert( type );
    961 
    962         if ( attr.name ) {
    963                 AttrType * ret;
    964                 if ( attr.expr ) {
    965                         ret = new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() );
    966                 } else {
    967                         assert( attr.type );
    968                         ret = new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() );
    969                 } // if
    970                 return ret;
    971         } // if
    972973
    973974        switch ( type->kind ) {
    974975          case TypeData::Enum:
    975                 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
     976                return new EnumInstType( buildQualifiers( type ), type->enumeration.name );
    976977          case TypeData::Aggregate: {
    977                   ReferenceToType * ret;
     978                  ReferenceToType *ret;
    978979                  switch ( type->aggregate.kind ) {
    979980                        case DeclarationNode::Struct:
    980                           ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
     981                          ret = new StructInstType( buildQualifiers( type ), type->aggregate.name );
    981982                          break;
    982983                        case DeclarationNode::Union:
    983                           ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
     984                          ret = new UnionInstType( buildQualifiers( type ), type->aggregate.name );
    984985                          break;
    985986                        case DeclarationNode::Trait:
    986                           ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
     987                          ret = new TraitInstType( buildQualifiers( type ), type->aggregate.name );
    987988                          break;
    988989                        default:
     
    993994          }
    994995          case TypeData::Symbolic: {
    995                   TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false );
     996                  TypeInstType *ret = new TypeInstType( buildQualifiers( type ), type->symbolic.name, false );
    996997                  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
    9971010                  return ret;
    9981011          }
Note: See TracChangeset for help on using the changeset viewer.