Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    rfaddbd8 r5b639ee  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Oct  3 18:03:08 2016
    13 // Update Count     : 651
     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;
    59         variable.tyClass = NoTypeClass;
     57        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
    115106        if ( linkage != LinkageSpec::Cforall ) {
    116                 os << LinkageSpec::linkageName( linkage ) << " ";
     107                os << LinkageSpec::toString( linkage ) << " ";
    117108        } // if
    118109
     
    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;
    285         assert( ! newnode->name );
    286 //      newnode->variable.name = name;
    287         newnode->name = name;
     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 );
    288279        newnode->variable.tyClass = tc;
    289         newnode->variable.assertions = nullptr;
     280        newnode->variable.name = newnode->name;
    290281        return newnode;
    291282} // DeclarationNode::newTypeParam
    292283
    293 DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
    294         DeclarationNode * newnode = new DeclarationNode;
     284DeclarationNode *DeclarationNode::newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts ) {
     285        DeclarationNode *newnode = new DeclarationNode;
    295286        newnode->type = new TypeData( TypeData::Aggregate );
    296         newnode->type->aggregate.name = name;
    297287        newnode->type->aggregate.kind = Trait;
    298288        newnode->type->aggregate.params = params;
    299289        newnode->type->aggregate.fields = asserts;
     290        newnode->type->aggregate.name = assign_strptr( name );
    300291        return newnode;
    301292} // DeclarationNode::newTrait
    302293
    303 DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
    304         DeclarationNode * newnode = new DeclarationNode;
     294DeclarationNode *DeclarationNode::newTraitUse( std::string *name, ExpressionNode *params ) {
     295        DeclarationNode *newnode = new DeclarationNode;
    305296        newnode->type = new TypeData( TypeData::AggregateInst );
    306297        newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
    307298        newnode->type->aggInst.aggregate->aggregate.kind = Trait;
    308         newnode->type->aggInst.aggregate->aggregate.name = name;
     299        newnode->type->aggInst.aggregate->aggregate.name = assign_strptr( name );
    309300        newnode->type->aggInst.params = params;
    310301        return newnode;
    311302} // DeclarationNode::newTraitUse
    312303
    313 DeclarationNode * DeclarationNode::newTypeDecl( string * name, DeclarationNode * typeParams ) {
    314         DeclarationNode * newnode = new DeclarationNode;
     304DeclarationNode *DeclarationNode::newTypeDecl( std::string *name, DeclarationNode *typeParams ) {
     305        DeclarationNode *newnode = new DeclarationNode;
     306        newnode->name = assign_strptr( name );
    315307        newnode->type = new TypeData( TypeData::Symbolic );
    316308        newnode->type->symbolic.isTypedef = false;
    317309        newnode->type->symbolic.params = typeParams;
    318         newnode->type->symbolic.name = name;
     310        newnode->type->symbolic.name = newnode->name;
    319311        return newnode;
    320312} // DeclarationNode::newTypeDecl
    321313
    322 DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers ) {
    323         DeclarationNode * newnode = new DeclarationNode;
     314DeclarationNode *DeclarationNode::newPointer( DeclarationNode *qualifiers ) {
     315        DeclarationNode *newnode = new DeclarationNode;
    324316        newnode->type = new TypeData( TypeData::Pointer );
    325317        return newnode->addQualifiers( qualifiers );
    326318} // DeclarationNode::newPointer
    327319
    328 DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
    329         DeclarationNode * newnode = new DeclarationNode;
     320DeclarationNode *DeclarationNode::newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic ) {
     321        DeclarationNode *newnode = new DeclarationNode;
    330322        newnode->type = new TypeData( TypeData::Array );
    331323        newnode->type->array.dimension = size;
    332324        newnode->type->array.isStatic = isStatic;
    333         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 *>() ) {
    334326                newnode->type->array.isVarLen = false;
    335327        } else {
     
    339331} // DeclarationNode::newArray
    340332
    341 DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
    342         DeclarationNode * newnode = new DeclarationNode;
     333DeclarationNode *DeclarationNode::newVarArray( DeclarationNode *qualifiers ) {
     334        DeclarationNode *newnode = new DeclarationNode;
    343335        newnode->type = new TypeData( TypeData::Array );
    344         newnode->type->array.dimension = nullptr;
     336        newnode->type->array.dimension = 0;
    345337        newnode->type->array.isStatic = false;
    346338        newnode->type->array.isVarLen = true;
     
    348340}
    349341
    350 DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
    351         DeclarationNode * newnode = new DeclarationNode;
     342DeclarationNode *DeclarationNode::newBitfield( ExpressionNode *size ) {
     343        DeclarationNode *newnode = new DeclarationNode;
    352344        newnode->bitfieldWidth = size;
    353345        return newnode;
    354346}
    355347
    356 DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
    357         DeclarationNode * newnode = new DeclarationNode;
     348DeclarationNode *DeclarationNode::newTuple( DeclarationNode *members ) {
     349        DeclarationNode *newnode = new DeclarationNode;
    358350        newnode->type = new TypeData( TypeData::Tuple );
    359351        newnode->type->tuple = members;
     
    361353}
    362354
    363 DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
    364         DeclarationNode * newnode = new DeclarationNode;
     355DeclarationNode *DeclarationNode::newTypeof( ExpressionNode *expr ) {
     356        DeclarationNode *newnode = new DeclarationNode;
    365357        newnode->type = new TypeData( TypeData::Typeof );
    366358        newnode->type->typeexpr = expr;
     
    369361
    370362DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
    371         DeclarationNode * newnode = new DeclarationNode;
     363        DeclarationNode *newnode = new DeclarationNode;
    372364        newnode->type = new TypeData( TypeData::Builtin );
    373365        newnode->builtin = bt;
     
    375367} // DeclarationNode::newBuiltinType
    376368
    377 DeclarationNode * DeclarationNode::newAttr( string * name, ExpressionNode * expr ) {
    378         DeclarationNode * newnode = new DeclarationNode;
    379         newnode->type = nullptr;
    380 //      newnode->attr.name = name;
    381         newnode->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 );
    382373        newnode->attr.expr = expr;
    383374        return newnode;
    384375}
    385376
    386 DeclarationNode * DeclarationNode::newAttr( string * name, DeclarationNode * type ) {
    387         DeclarationNode * newnode = new DeclarationNode;
    388         newnode->type = nullptr;
    389 //      newnode->attr.name = name;
    390         newnode->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 );
    391381        newnode->attr.type = type;
    392382        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
    393404}
    394405
     
    399410} // appendError
    400411
    401 void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
     412void DeclarationNode::checkQualifiers( const TypeData *src, const TypeData *dst ) {
    402413        TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
    403414
     
    411422} // DeclarationNode::checkQualifiers
    412423
    413 void DeclarationNode::checkStorageClasses( DeclarationNode * q ) {
     424void DeclarationNode::checkStorageClasses( DeclarationNode *q ) {
    414425        if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) {
    415426                if ( storageClass == q->storageClass ) {                // duplicate qualifier
     
    423434} // DeclarationNode::copyStorageClasses
    424435
    425 DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) {
     436DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) {
    426437        isInline = isInline || q->isInline;
    427438        isNoreturn = isNoreturn || q->isNoreturn;
     
    434445} // DeclarationNode::copyStorageClasses
    435446
    436 static void addQualifiersToType( TypeData *&src, TypeData * dst ) {
    437         if ( src->forall && dst->kind == TypeData::Function ) {
    438                 if ( dst->forall ) {
    439                         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 );
    440490                } else {
    441                         dst->forall = src->forall;
    442                 } // if
    443                 src->forall = nullptr;
    444         } // if
    445         if ( dst->base ) {
    446                 addQualifiersToType( src, dst->base );
    447         } else if ( dst->kind == TypeData::Function ) {
    448                 dst->base = src;
    449                 src = nullptr;
    450         } else {
    451                 dst->qualifiers |= src->qualifiers;
    452         } // if
    453 } // addQualifiersToType
    454 
    455 DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
    456         if ( ! q ) { delete q; return this; }
    457 
    458         checkStorageClasses( q );
    459         copyStorageClasses( q );
    460 
    461         if ( ! q->type ) {
    462                 delete q;
    463                 return this;
    464         } // if
    465 
    466         if ( ! type ) {
    467                 type = q->type;                                                                 // reuse this structure
    468                 q->type = nullptr;
    469                 delete q;
    470                 return this;
    471         } // if
    472 
    473         checkQualifiers( q->type, type );
    474         addQualifiersToType( q->type, type );
    475 
    476         if ( q->type->forall ) {
    477                 if ( type->forall ) {
    478                         type->forall->appendList( q->type->forall );
    479                 } else {
    480                         if ( type->kind == TypeData::Aggregate ) {
    481                                 type->aggregate.params = q->type->forall;
    482                                 // change implicit typedef from TYPEDEFname to TYPEGENname
    483                                 typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG );
    484                         } else {
    485                                 type->forall = q->type->forall;
    486                         } // if
    487                 } // if
    488                 q->type->forall = nullptr;
    489         } // if
    490         delete q;
    491         return this;
    492 } // addQualifiers
    493 
    494 static void addTypeToType( TypeData *&src, TypeData *&dst ) {
    495         if ( src->forall && dst->kind == TypeData::Function ) {
    496                 if ( dst->forall ) {
    497                         dst->forall->appendList( src->forall );
    498                 } else {
    499                         dst->forall = src->forall;
    500                 } // if
    501                 src->forall = nullptr;
    502         } // if
    503         if ( dst->base ) {
    504                 addTypeToType( src, dst->base );
    505         } else {
    506                 switch ( dst->kind ) {
    507                   case TypeData::Unknown:
    508                         src->qualifiers |= dst->qualifiers;
    509                         dst = src;
    510                         src = nullptr;
    511                         break;
    512                   case TypeData::Basic:
    513                         dst->qualifiers |= src->qualifiers;
    514                         if ( src->kind != TypeData::Unknown ) {
    515                                 assert( src->kind == TypeData::Basic );
    516 
    517                                 if ( dst->basictype == DeclarationNode::NoBasicType ) {
    518                                         dst->basictype = src->basictype;
    519                                 } else if ( src->basictype != DeclarationNode::NoBasicType )
    520                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src );
    521 
    522                                 if ( dst->complextype == DeclarationNode::NoComplexType ) {
    523                                         dst->complextype = src->complextype;
    524                                 } else if ( src->complextype != DeclarationNode::NoComplexType )
    525                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src );
    526 
    527                                 if ( dst->signedness == DeclarationNode::NoSignedness ) {
    528                                         dst->signedness = src->signedness;
    529                                 } else if ( src->signedness != DeclarationNode::NoSignedness )
    530                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src );
    531 
    532                                 if ( dst->length == DeclarationNode::NoLength ) {
    533                                         dst->length = src->length;
    534                                 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
    535                                         dst->length = DeclarationNode::LongLong;
    536                                 } else if ( src->length != DeclarationNode::NoLength )
    537                                         throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src );
    538                         } // if
    539                         break;
    540                   default:
    541                         switch ( src->kind ) {
    542                           case TypeData::Aggregate:
    543                           case TypeData::Enum:
    544                                 dst->base = new TypeData( TypeData::AggregateInst );
    545                                 dst->base->aggInst.aggregate = src;
    546                                 if ( src->kind == TypeData::Aggregate ) {
    547                                         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 );
    548523                                } // if
    549                                 dst->base->qualifiers |= src->qualifiers;
    550                                 src = nullptr;
    551524                                break;
    552525                          default:
    553                                 if ( dst->forall ) {
    554                                         dst->forall->appendList( src->forall );
    555                                 } else {
    556                                         dst->forall = src->forall;
    557                                 } // if
    558                                 src->forall = nullptr;
    559                                 dst->base = src;
    560                                 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
    561547                        } // switch
    562                 } // switch
    563         } // if
    564 }
    565 
    566 DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
     548                } // if
     549        } // if
     550}
     551
     552DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) {
    567553        if ( o ) {
    568554                checkStorageClasses( o );
     
    580566                                        type = o->type;
    581567                                } // if
    582                                 o->type = nullptr;
     568                                o->type = 0;
    583569                        } else {
    584570                                addTypeToType( o->type, type );
     
    598584}
    599585
    600 DeclarationNode * DeclarationNode::addTypedef() {
    601         TypeData * newtype = new TypeData( TypeData::Symbolic );
    602         newtype->symbolic.params = nullptr;
     586DeclarationNode *DeclarationNode::addTypedef() {
     587        TypeData *newtype = new TypeData( TypeData::Symbolic );
     588        newtype->symbolic.params = 0;
    603589        newtype->symbolic.isTypedef = true;
    604         newtype->symbolic.name = name ? new string( *name ) : nullptr;
     590        newtype->symbolic.name = name;
    605591        newtype->base = type;
    606592        type = newtype;
     
    608594}
    609595
    610 DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
    611         if ( variable.tyClass != NoTypeClass ) {
    612                 if ( variable.assertions ) {
    613                         variable.assertions->appendList( assertions );
    614                 } else {
    615                         variable.assertions = assertions;
    616                 } // if
    617                 return this;
    618         } // if
    619 
     596DeclarationNode *DeclarationNode::addAssertions( DeclarationNode *assertions ) {
    620597        assert( type );
    621598        switch ( type->kind ) {
     
    627604                } // if
    628605                break;
     606          case TypeData::Variable:
     607                if ( variable.assertions ) {
     608                        variable.assertions->appendList( assertions );
     609                } else {
     610                        variable.assertions = assertions;
     611                } // if
     612                break;
    629613          default:
    630614                assert( false );
     
    634618}
    635619
    636 DeclarationNode * DeclarationNode::addName( string * newname ) {
    637         assert( ! name );
    638         name = newname;
    639         return this;
    640 }
    641 
    642 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 ) {
    643626        bitfieldWidth = size;
    644627        return this;
    645628}
    646629
    647 DeclarationNode * DeclarationNode::addVarArgs() {
     630DeclarationNode *DeclarationNode::addVarArgs() {
    648631        assert( type );
    649632        hasEllipsis = true;
     
    651634}
    652635
    653 DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body ) {
     636DeclarationNode *DeclarationNode::addFunctionBody( StatementNode *body ) {
    654637        assert( type );
    655638        assert( type->kind == TypeData::Function );
    656         assert( ! type->function.body );
     639        assert( type->function.body == 0 );
    657640        type->function.body = body;
    658641        type->function.hasBody = true;
     
    660643}
    661644
    662 DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
     645DeclarationNode *DeclarationNode::addOldDeclList( DeclarationNode *list ) {
    663646        assert( type );
    664647        assert( type->kind == TypeData::Function );
    665         assert( ! type->function.oldDeclList );
     648        assert( type->function.oldDeclList == 0 );
    666649        type->function.oldDeclList = list;
    667650        return this;
    668651}
    669652
    670 static void setBase( TypeData *&type, TypeData * newType ) {
     653static void setBase( TypeData *&type, TypeData *newType ) {
    671654        if ( type ) {
    672                 TypeData * prevBase = type;
    673                 TypeData * curBase = type->base;
    674                 while ( curBase != nullptr ) {
     655                TypeData *prevBase = type;
     656                TypeData *curBase = type->base;
     657                while ( curBase != 0 ) {
    675658                        prevBase = curBase;
    676659                        curBase = curBase->base;
     
    682665}
    683666
    684 DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
     667DeclarationNode *DeclarationNode::addPointer( DeclarationNode *p ) {
    685668        if ( p ) {
    686669                assert( p->type->kind == TypeData::Pointer );
    687670                setBase( type, p->type );
    688                 p->type = nullptr;
     671                p->type = 0;
    689672                delete p;
    690673        } // if
     
    692675}
    693676
    694 DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
     677DeclarationNode *DeclarationNode::addArray( DeclarationNode *a ) {
    695678        if ( a ) {
    696679                assert( a->type->kind == TypeData::Array );
    697680                setBase( type, a->type );
    698                 a->type = nullptr;
     681                a->type = 0;
    699682                delete a;
    700683        } // if
     
    702685}
    703686
    704 DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
     687DeclarationNode *DeclarationNode::addNewPointer( DeclarationNode *p ) {
    705688        if ( p ) {
    706689                assert( p->type->kind == TypeData::Pointer );
     
    720703                                p->type->base = type;
    721704                        } // switch
    722                         type = nullptr;
     705                        type = 0;
    723706                } // if
    724707                delete this;
     
    729712}
    730713
    731 static TypeData * findLast( TypeData * a ) {
     714static TypeData *findLast( TypeData *a ) {
    732715        assert( a );
    733         TypeData * cur = a;
     716        TypeData *cur = a;
    734717        while ( cur->base ) {
    735718                cur = cur->base;
     
    738721}
    739722
    740 DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
     723DeclarationNode *DeclarationNode::addNewArray( DeclarationNode *a ) {
    741724        if ( a ) {
    742725                assert( a->type->kind == TypeData::Array );
    743                 TypeData * lastArray = findLast( a->type );
     726                TypeData *lastArray = findLast( a->type );
    744727                if ( type ) {
    745728                        switch ( type->kind ) {
     
    756739                                lastArray->base = type;
    757740                        } // switch
    758                         type = nullptr;
     741                        type = 0;
    759742                } // if
    760743                delete this;
     
    765748}
    766749
    767 DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
    768         TypeData * ftype = new TypeData( TypeData::Function );
     750DeclarationNode *DeclarationNode::addParamList( DeclarationNode *params ) {
     751        TypeData *ftype = new TypeData( TypeData::Function );
    769752        ftype->function.params = params;
    770753        setBase( type, ftype );
     
    772755}
    773756
    774 static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
     757static TypeData *addIdListToType( TypeData *type, DeclarationNode *ids ) {
    775758        if ( type ) {
    776759                if ( type->kind != TypeData::Function ) {
     
    781764                return type;
    782765        } else {
    783                 TypeData * newtype = new TypeData( TypeData::Function );
     766                TypeData *newtype = new TypeData( TypeData::Function );
    784767                newtype->function.idList = ids;
    785768                return newtype;
    786769        } // if
    787 } // addIdListToType
    788 
    789 DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
     770}
     771
     772DeclarationNode *DeclarationNode::addIdList( DeclarationNode *ids ) {
    790773        type = addIdListToType( type, ids );
    791774        return this;
    792775}
    793776
    794 DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
     777DeclarationNode *DeclarationNode::addInitializer( InitializerNode *init ) {
     778        //assert
    795779        initializer = init;
    796780        return this;
    797781}
    798782
    799 DeclarationNode * DeclarationNode::cloneType( string * newName ) {
    800         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;
    801842        newnode->type = maybeClone( type );
    802843        assert( storageClass == NoStorageClass );
    803844        newnode->copyStorageClasses( this );
    804         assert( newName );
    805         newnode->name = newName;
    806         return newnode;
    807 }
    808 
    809 DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
    810         if ( ! o ) return nullptr;
    811 
    812         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 {
    813868        if ( type ) {
    814                 TypeData * srcType = type;
    815 
    816                 while ( srcType->base ) {
    817                         srcType = srcType->base;
    818                 } // while
    819 
    820                 TypeData * newType = srcType->clone();
    821                 if ( newType->kind == TypeData::AggregateInst ) {
    822                         // don't duplicate members
    823                         if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
    824                                 delete newType->aggInst.aggregate->enumeration.constants;
    825                                 newType->aggInst.aggregate->enumeration.constants = nullptr;
    826                         } else {
    827                                 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
    828                                 delete newType->aggInst.aggregate->aggregate.fields;
    829                                 newType->aggInst.aggregate->aggregate.fields = nullptr;
    830                         } // if
    831                 } // if
    832 
    833                 newType->forall = maybeClone( type->forall );
    834                 if ( ! o->type ) {
    835                         o->type = newType;
    836                 } else {
    837                         addTypeToType( newType, o->type );
    838                         delete newType;
    839                 } // if
    840         } // if
    841         return o;
    842 }
    843 
    844 DeclarationNode * DeclarationNode::extractAggregate() const {
    845         if ( type ) {
    846                 TypeData * ret = typeextractAggregate( type );
     869                TypeData *ret = typeextractAggregate( type );
    847870                if ( ret ) {
    848                         DeclarationNode * newnode = new DeclarationNode;
     871                        DeclarationNode *newnode = new DeclarationNode;
    849872                        newnode->type = ret;
    850873                        return newnode;
    851874                } // if
    852875        } // if
    853         return nullptr;
    854 }
    855 
    856 void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
     876        return 0;
     877}
     878
     879void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) {
    857880        SemanticError errors;
    858881        std::back_insert_iterator< std::list< Declaration * > > out( outputList );
    859         const DeclarationNode * cur = firstNode;
    860 
     882        const DeclarationNode *cur = firstNode;
    861883        while ( cur ) {
    862884                try {
    863                         if ( DeclarationNode * extr = cur->extractAggregate() ) {
     885                        if ( DeclarationNode *extr = cur->extractAggregate() ) {
    864886                                // handle the case where a structure declaration is contained within an object or type declaration
    865                                 Declaration * decl = extr->build();
     887                                Declaration *decl = extr->build();
    866888                                if ( decl ) {
    867                                         * out++ = decl;
     889                                        *out++ = decl;
    868890                                } // if
    869891                                delete extr;
    870892                        } // if
    871 
    872                         Declaration * decl = cur->build();
     893                        Declaration *decl = cur->build();
    873894                        if ( decl ) {
    874                                 * out++ = decl;
    875                         } // if
    876                 } catch( SemanticError &e ) {
    877                         errors.append( e );
    878                 } // try
    879                 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
    880         } // while
    881 
    882         if ( ! errors.isEmpty() ) {
    883                 throw errors;
    884         } // if
    885 } // buildList
    886 
    887 void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
    888         SemanticError errors;
    889         std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
    890         const DeclarationNode * cur = firstNode;
    891         while ( cur ) {
    892                 try {
    893                         Declaration * decl = cur->build();
    894                         if ( decl ) {
    895                                 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
    896                                         * out++ = dwt;
    897                                 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
    898                                         StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
    899                                         * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
    900                                         delete agg;
    901                                 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
    902                                         UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
    903                                         * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
    904                                 } // if
     895                                *out++ = decl;
    905896                        } // if
    906897                } catch( SemanticError &e ) {
     
    912903                throw errors;
    913904        } // if
    914 } // buildList
    915 
    916 void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
     905}
     906
     907void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList ) {
    917908        SemanticError errors;
    918         std::back_insert_iterator< std::list< Type * > > out( outputList );
    919         const DeclarationNode * cur = firstNode;
    920 
     909        std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
     910        const DeclarationNode *cur = firstNode;
    921911        while ( cur ) {
    922912                try {
    923                         * 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
    924926                } catch( SemanticError &e ) {
    925927                        errors.append( e );
     
    927929                cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
    928930        } // while
    929 
    930931        if ( ! errors.isEmpty() ) {
    931932                throw errors;
    932933        } // if
    933 } // buildTypeList
    934 
    935 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 {
    936954        if ( ! error.empty() ) throw SemanticError( error + " in declaration of ", this );
    937 
    938 //      if ( variable.name ) {
    939         if ( variable.tyClass != NoTypeClass ) {
    940                 static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype };
    941 //              TypeDecl * ret = new TypeDecl( *variable.name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );
    942                 TypeDecl * ret = new TypeDecl( *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.expr ) {
    963 //              return new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() );
    964                 return new AttrType( buildQualifiers( type ), *name, attr.expr->build() );
    965         } else if ( attr.type ) {
    966 //              return new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() );
    967                 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType() );
    968         } // if
    969973
    970974        switch ( type->kind ) {
    971975          case TypeData::Enum:
    972                 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
     976                return new EnumInstType( buildQualifiers( type ), type->enumeration.name );
    973977          case TypeData::Aggregate: {
    974                   ReferenceToType * ret;
     978                  ReferenceToType *ret;
    975979                  switch ( type->aggregate.kind ) {
    976980                        case DeclarationNode::Struct:
    977                           ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
     981                          ret = new StructInstType( buildQualifiers( type ), type->aggregate.name );
    978982                          break;
    979983                        case DeclarationNode::Union:
    980                           ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
     984                          ret = new UnionInstType( buildQualifiers( type ), type->aggregate.name );
    981985                          break;
    982986                        case DeclarationNode::Trait:
    983                           ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
     987                          ret = new TraitInstType( buildQualifiers( type ), type->aggregate.name );
    984988                          break;
    985989                        default:
     
    990994          }
    991995          case TypeData::Symbolic: {
    992                   TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false );
     996                  TypeInstType *ret = new TypeInstType( buildQualifiers( type ), type->symbolic.name, false );
    993997                  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
    9941010                  return ret;
    9951011          }
Note: See TracChangeset for help on using the changeset viewer.