Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    r5b639ee r2298f728  
    1010// Created On       : Sat May 16 12:34:05 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Sep 12 21:03:18 2016
    13 // Update Count     : 491
     12// Last Modified On : Sat Sep 24 11:12:52 2016
     13// Update Count     : 627
    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( 0 ),
     48                type( nullptr ),
    4949                storageClass( NoStorageClass ),
    5050                isInline( false ),
    5151                isNoreturn( false ),
    52                 bitfieldWidth( 0 ),
    53                 initializer( 0 ),
     52                bitfieldWidth( nullptr ),
     53                initializer( nullptr ),
    5454                hasEllipsis( false ),
    5555                linkage( ::linkage ),
    5656                extension( false ) {
     57
     58        variable.name = nullptr;
    5759        variable.tyClass = DeclarationNode::Otype;
    5860        variable.assertions = nullptr;
    5961
     62        attr.name = nullptr;
    6063        attr.expr = nullptr;
    6164        attr.type = nullptr;
     
    6366
    6467DeclarationNode::~DeclarationNode() {
     68        delete attr.name;
    6569        delete attr.expr;
    6670        delete attr.type;
     71
     72        delete variable.name;
     73        delete variable.assertions;
     74
    6775        delete type;
    6876        delete bitfieldWidth;
     
    7078}
    7179
    72 DeclarationNode *DeclarationNode::clone() const {
    73         DeclarationNode *newnode = new DeclarationNode;
     80DeclarationNode * DeclarationNode::clone() const {
     81        DeclarationNode * newnode = new DeclarationNode;
    7482        newnode->type = maybeClone( type );
    75         newnode->name = name;
     83        newnode->name = name ? new string( *name ) : nullptr;
    7684        newnode->storageClass = storageClass;
    7785        newnode->isInline = isInline;
     
    8391        newnode->linkage = linkage;
    8492
     93        newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
    8594        newnode->variable.assertions = maybeClone( variable.assertions );
    86         newnode->variable.name = variable.name;
    8795        newnode->variable.tyClass = variable.tyClass;
    8896
     97        newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr;
    8998        newnode->attr.expr = maybeClone( attr.expr );
    9099        newnode->attr.type = maybeClone( attr.type );
     
    98107void DeclarationNode::print( std::ostream &os, int indent ) const {
    99108        os << string( indent, ' ' );
    100         if ( name == "" ) {
     109        if ( name ) {
     110                os << *name << ": ";
     111        } else {
    101112                os << "unnamed: ";
    102         } else {
    103                 os << name << ": ";
    104113        } // if
    105114
     
    122131        } // if
    123132
    124         if ( initializer != 0 ) {
     133        if ( initializer ) {
    125134                os << endl << string( indent + 2, ' ' ) << "with initializer ";
    126135                initializer->printOneLine( os );
     
    139148}
    140149
    141 DeclarationNode *DeclarationNode::newFunction( std::string *name, DeclarationNode *ret, DeclarationNode *param, StatementNode *body, bool newStyle ) {
    142         DeclarationNode *newnode = new DeclarationNode;
    143         newnode->name = assign_strptr( name );
     150DeclarationNode * DeclarationNode::newFunction( std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body, bool newStyle ) {
     151        DeclarationNode * newnode = new DeclarationNode;
     152        newnode->name = name;
    144153
    145154        newnode->type = new TypeData( TypeData::Function );
     
    147156        newnode->type->function.newStyle = newStyle;
    148157        newnode->type->function.body = body;
    149         typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID );
     158        // ignore unnamed routine declarations: void p( int (*)(int) );
     159        if ( newnode->name ) {
     160                typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );
     161        } // if
    150162
    151163        if ( body ) {
     
    155167        if ( ret ) {
    156168                newnode->type->base = ret->type;
    157                 ret->type = 0;
     169                ret->type = nullptr;
    158170                delete ret;
    159171        } // if
     
    163175
    164176DeclarationNode * DeclarationNode::newQualifier( Qualifier q ) {
    165         DeclarationNode *newnode = new DeclarationNode;
     177        DeclarationNode * newnode = new DeclarationNode;
    166178        newnode->type = new TypeData();
    167179        newnode->type->qualifiers[ q ] = 1;
     
    169181} // DeclarationNode::newQualifier
    170182
    171 DeclarationNode * DeclarationNode::newForall( DeclarationNode *forall ) {
    172         DeclarationNode *newnode = new DeclarationNode;
     183DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
     184        DeclarationNode * newnode = new DeclarationNode;
    173185        newnode->type = new TypeData( TypeData::Unknown );
    174186        newnode->type->forall = forall;
     
    177189
    178190DeclarationNode * DeclarationNode::newStorageClass( DeclarationNode::StorageClass sc ) {
    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         //}
     191        DeclarationNode * newnode = new DeclarationNode;
    185192        newnode->storageClass = sc;
    186193        return newnode;
     
    188195
    189196DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
    190         DeclarationNode *newnode = new DeclarationNode;
     197        DeclarationNode * newnode = new DeclarationNode;
    191198        newnode->type = new TypeData( TypeData::Basic );
    192199        newnode->type->basictype = bt;
     
    195202
    196203DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
    197         DeclarationNode *newnode = new DeclarationNode;
     204        DeclarationNode * newnode = new DeclarationNode;
    198205        newnode->type = new TypeData( TypeData::Basic );
    199206        newnode->type->complextype = ct;
     
    202209
    203210DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
    204         DeclarationNode *newnode = new DeclarationNode;
     211        DeclarationNode * newnode = new DeclarationNode;
    205212        newnode->type = new TypeData( TypeData::Basic );
    206213        newnode->type->signedness = sn;
     
    209216
    210217DeclarationNode * DeclarationNode::newLength( Length lnth ) {
    211         DeclarationNode *newnode = new DeclarationNode;
     218        DeclarationNode * newnode = new DeclarationNode;
    212219        newnode->type = new TypeData( TypeData::Basic );
    213220        newnode->type->length = lnth;
     
    215222} // DeclarationNode::newLength
    216223
    217 DeclarationNode * DeclarationNode::newFromTypedef( std::string *name ) {
    218         DeclarationNode *newnode = new DeclarationNode;
     224DeclarationNode * DeclarationNode::newFromTypedef( std::string * name ) {
     225        DeclarationNode * newnode = new DeclarationNode;
    219226        newnode->type = new TypeData( TypeData::SymbolicInst );
    220         newnode->type->symbolic.name = assign_strptr( name );
     227        newnode->type->symbolic.name = name ? new string( *name ) : nullptr;
    221228        newnode->type->symbolic.isTypedef = true;
    222         newnode->type->symbolic.params = 0;
     229        newnode->type->symbolic.params = nullptr;
    223230        return newnode;
    224231} // DeclarationNode::newFromTypedef
    225232
    226 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const std::string *name, ExpressionNode *actuals, DeclarationNode *fields, bool body ) {
    227         DeclarationNode *newnode = new DeclarationNode;
     233DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
     234        DeclarationNode * newnode = new DeclarationNode;
    228235        newnode->type = new TypeData( TypeData::Aggregate );
    229236        newnode->type->aggregate.kind = kind;
    230         newnode->type->aggregate.name = assign_strptr( name );
    231         if ( newnode->type->aggregate.name == "" ) {            // anonymous aggregate ?
    232                 newnode->type->aggregate.name = anonymous.newName();
     237        if ( name ) {
     238                newnode->type->aggregate.name = new string( *name );
     239        } else {                                                                                        // anonymous aggregate ?
     240                newnode->type->aggregate.name = new string( anonymous.newName() );
    233241        } // if
    234242        newnode->type->aggregate.actuals = actuals;
     
    238246} // DeclarationNode::newAggregate
    239247
    240 DeclarationNode *DeclarationNode::newEnum( std::string *name, DeclarationNode *constants ) {
    241         DeclarationNode *newnode = new DeclarationNode;
    242         newnode->name = assign_strptr( name );
     248DeclarationNode * DeclarationNode::newEnum( std::string * name, DeclarationNode * constants ) {
     249        DeclarationNode * newnode = new DeclarationNode;
     250        newnode->name = name;
    243251        newnode->type = new TypeData( TypeData::Enum );
    244         newnode->type->enumeration.name = newnode->name;
    245         if ( newnode->type->enumeration.name == "" ) {          // anonymous enumeration ?
    246                 newnode->type->enumeration.name = DeclarationNode::anonymous.newName();
     252        if ( name ) {
     253                newnode->type->enumeration.name = new string( *name );
     254        } else {                                                                                        // anonymous aggregate ?
     255                newnode->type->enumeration.name = new string( anonymous.newName() );
    247256        } // if
    248257        newnode->type->enumeration.constants = constants;
     
    250259} // DeclarationNode::newEnum
    251260
    252 DeclarationNode *DeclarationNode::newEnumConstant( std::string *name, ExpressionNode *constant ) {
    253         DeclarationNode *newnode = new DeclarationNode;
    254         newnode->name = assign_strptr( name );
     261DeclarationNode * DeclarationNode::newEnumConstant( std::string * name, ExpressionNode * constant ) {
     262        DeclarationNode * newnode = new DeclarationNode;
     263        newnode->name = name;
    255264        newnode->enumeratorValue.reset( constant );
    256         typedefTable.addToEnclosingScope( newnode->name, TypedefTable::ID );
     265        typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );
    257266        return newnode;
    258267} // DeclarationNode::newEnumConstant
    259268
    260 DeclarationNode *DeclarationNode::newName( std::string *name ) {
    261         DeclarationNode *newnode = new DeclarationNode;
    262         newnode->name = assign_strptr( name );
     269DeclarationNode * DeclarationNode::newName( std::string * name ) {
     270        DeclarationNode * newnode = new DeclarationNode;
     271        newnode->name = name;
    263272        return newnode;
    264273} // DeclarationNode::newName
    265274
    266 DeclarationNode *DeclarationNode::newFromTypeGen( std::string *name, ExpressionNode *params ) {
    267         DeclarationNode *newnode = new DeclarationNode;
     275DeclarationNode * DeclarationNode::newFromTypeGen( std::string * name, ExpressionNode * params ) {
     276        DeclarationNode * newnode = new DeclarationNode;
    268277        newnode->type = new TypeData( TypeData::SymbolicInst );
    269         newnode->type->symbolic.name = assign_strptr( name );
     278        newnode->type->symbolic.name = name ? new string( *name ) : nullptr;
    270279        newnode->type->symbolic.isTypedef = false;
    271280        newnode->type->symbolic.actuals = params;
     
    273282} // DeclarationNode::newFromTypeGen
    274283
    275 DeclarationNode *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 );
     284DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, std::string * name ) {
     285        DeclarationNode * newnode = new DeclarationNode;
     286        newnode->name = name;
     287//      newnode->type = new TypeData( TypeData::Variable );
     288        newnode->type = nullptr;
    279289        newnode->variable.tyClass = tc;
    280         newnode->variable.name = newnode->name;
     290        newnode->variable.name = newnode->name ? new string( *newnode->name ) : nullptr;
    281291        return newnode;
    282292} // DeclarationNode::newTypeParam
    283293
    284 DeclarationNode *DeclarationNode::newTrait( std::string *name, DeclarationNode *params, DeclarationNode *asserts ) {
    285         DeclarationNode *newnode = new DeclarationNode;
     294DeclarationNode * DeclarationNode::newTrait( const std::string * name, DeclarationNode * params, DeclarationNode * asserts ) {
     295        DeclarationNode * newnode = new DeclarationNode;
    286296        newnode->type = new TypeData( TypeData::Aggregate );
     297        newnode->type->aggregate.name = name;
    287298        newnode->type->aggregate.kind = Trait;
    288299        newnode->type->aggregate.params = params;
    289300        newnode->type->aggregate.fields = asserts;
    290         newnode->type->aggregate.name = assign_strptr( name );
    291301        return newnode;
    292302} // DeclarationNode::newTrait
    293303
    294 DeclarationNode *DeclarationNode::newTraitUse( std::string *name, ExpressionNode *params ) {
    295         DeclarationNode *newnode = new DeclarationNode;
     304DeclarationNode * DeclarationNode::newTraitUse( const std::string * name, ExpressionNode * params ) {
     305        DeclarationNode * newnode = new DeclarationNode;
    296306        newnode->type = new TypeData( TypeData::AggregateInst );
    297307        newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
    298308        newnode->type->aggInst.aggregate->aggregate.kind = Trait;
    299         newnode->type->aggInst.aggregate->aggregate.name = assign_strptr( name );
     309        newnode->type->aggInst.aggregate->aggregate.name = name;
    300310        newnode->type->aggInst.params = params;
    301311        return newnode;
    302312} // DeclarationNode::newTraitUse
    303313
    304 DeclarationNode *DeclarationNode::newTypeDecl( std::string *name, DeclarationNode *typeParams ) {
    305         DeclarationNode *newnode = new DeclarationNode;
    306         newnode->name = assign_strptr( name );
     314DeclarationNode * DeclarationNode::newTypeDecl( std::string * name, DeclarationNode * typeParams ) {
     315        DeclarationNode * newnode = new DeclarationNode;
     316        newnode->name = name;
    307317        newnode->type = new TypeData( TypeData::Symbolic );
    308318        newnode->type->symbolic.isTypedef = false;
     
    312322} // DeclarationNode::newTypeDecl
    313323
    314 DeclarationNode *DeclarationNode::newPointer( DeclarationNode *qualifiers ) {
    315         DeclarationNode *newnode = new DeclarationNode;
     324DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers ) {
     325        DeclarationNode * newnode = new DeclarationNode;
    316326        newnode->type = new TypeData( TypeData::Pointer );
    317327        return newnode->addQualifiers( qualifiers );
    318328} // DeclarationNode::newPointer
    319329
    320 DeclarationNode *DeclarationNode::newArray( ExpressionNode *size, DeclarationNode *qualifiers, bool isStatic ) {
    321         DeclarationNode *newnode = new DeclarationNode;
     330DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
     331        DeclarationNode * newnode = new DeclarationNode;
    322332        newnode->type = new TypeData( TypeData::Array );
    323333        newnode->type->array.dimension = size;
    324334        newnode->type->array.isStatic = isStatic;
    325         if ( newnode->type->array.dimension == 0 || newnode->type->array.dimension->isExpressionType<ConstantExpr *>() ) {
     335        if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) {
    326336                newnode->type->array.isVarLen = false;
    327337        } else {
     
    331341} // DeclarationNode::newArray
    332342
    333 DeclarationNode *DeclarationNode::newVarArray( DeclarationNode *qualifiers ) {
    334         DeclarationNode *newnode = new DeclarationNode;
     343DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
     344        DeclarationNode * newnode = new DeclarationNode;
    335345        newnode->type = new TypeData( TypeData::Array );
    336         newnode->type->array.dimension = 0;
     346        newnode->type->array.dimension = nullptr;
    337347        newnode->type->array.isStatic = false;
    338348        newnode->type->array.isVarLen = true;
     
    340350}
    341351
    342 DeclarationNode *DeclarationNode::newBitfield( ExpressionNode *size ) {
    343         DeclarationNode *newnode = new DeclarationNode;
     352DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
     353        DeclarationNode * newnode = new DeclarationNode;
    344354        newnode->bitfieldWidth = size;
    345355        return newnode;
    346356}
    347357
    348 DeclarationNode *DeclarationNode::newTuple( DeclarationNode *members ) {
    349         DeclarationNode *newnode = new DeclarationNode;
     358DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
     359        DeclarationNode * newnode = new DeclarationNode;
    350360        newnode->type = new TypeData( TypeData::Tuple );
    351361        newnode->type->tuple = members;
     
    353363}
    354364
    355 DeclarationNode *DeclarationNode::newTypeof( ExpressionNode *expr ) {
    356         DeclarationNode *newnode = new DeclarationNode;
     365DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
     366        DeclarationNode * newnode = new DeclarationNode;
    357367        newnode->type = new TypeData( TypeData::Typeof );
    358368        newnode->type->typeexpr = expr;
     
    361371
    362372DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
    363         DeclarationNode *newnode = new DeclarationNode;
     373        DeclarationNode * newnode = new DeclarationNode;
    364374        newnode->type = new TypeData( TypeData::Builtin );
    365375        newnode->builtin = bt;
     
    367377} // DeclarationNode::newBuiltinType
    368378
    369 DeclarationNode *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 );
     379DeclarationNode * DeclarationNode::newAttr( std::string * name, ExpressionNode * expr ) {
     380        DeclarationNode * newnode = new DeclarationNode;
     381//      newnode->type = new TypeData( TypeData::Attr );
     382        newnode->type = nullptr;
     383        newnode->attr.name = name;
    373384        newnode->attr.expr = expr;
    374385        return newnode;
    375386}
    376387
    377 DeclarationNode *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 );
     388DeclarationNode * DeclarationNode::newAttr( std::string * name, DeclarationNode * type ) {
     389        DeclarationNode * newnode = new DeclarationNode;
     390//      newnode->type = new TypeData( TypeData::Attr );
     391        newnode->type = nullptr;
     392        newnode->attr.name = name;
    381393        newnode->attr.type = type;
    382394        return newnode;
    383 }
    384 
    385 static 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
    404395}
    405396
     
    410401} // appendError
    411402
    412 void DeclarationNode::checkQualifiers( const TypeData *src, const TypeData *dst ) {
     403void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
    413404        TypeData::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
    414405
     
    422413} // DeclarationNode::checkQualifiers
    423414
    424 void DeclarationNode::checkStorageClasses( DeclarationNode *q ) {
     415void DeclarationNode::checkStorageClasses( DeclarationNode * q ) {
    425416        if ( storageClass != NoStorageClass && q->storageClass != NoStorageClass ) {
    426417                if ( storageClass == q->storageClass ) {                // duplicate qualifier
     
    434425} // DeclarationNode::copyStorageClasses
    435426
    436 DeclarationNode *DeclarationNode::copyStorageClasses( DeclarationNode *q ) {
     427DeclarationNode * DeclarationNode::copyStorageClasses( DeclarationNode * q ) {
    437428        isInline = isInline || q->isInline;
    438429        isNoreturn = isNoreturn || q->isNoreturn;
     
    445436} // DeclarationNode::copyStorageClasses
    446437
    447 DeclarationNode *DeclarationNode::addQualifiers( DeclarationNode *q ) {
    448         if ( q ) {
    449                 checkStorageClasses( q );
    450                 copyStorageClasses( q );
    451                 if ( q->type ) {
    452                         if ( ! type ) {
    453                                 type = new TypeData;
     438static void addQualifiersToType( TypeData *&src, TypeData * dst ) {
     439        if ( src->forall && dst->kind == TypeData::Function ) {
     440                if ( dst->forall ) {
     441                        dst->forall->appendList( src->forall );
     442                } else {
     443                        dst->forall = src->forall;
     444                } // if
     445                src->forall = nullptr;
     446        } // if
     447        if ( dst->base ) {
     448                addQualifiersToType( src, dst->base );
     449        } else if ( dst->kind == TypeData::Function ) {
     450                dst->base = src;
     451                src = nullptr;
     452        } else {
     453                dst->qualifiers |= src->qualifiers;
     454        } // if
     455} // addQualifiersToType
     456
     457DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
     458        if ( ! q ) { delete q; return this; }
     459
     460        checkStorageClasses( q );
     461        copyStorageClasses( q );
     462
     463        if ( ! q->type ) {
     464                delete q;
     465                return this;
     466        } // if
     467
     468        if ( ! type ) {
     469                type = q->type;                                                                 // reuse this structure
     470                q->type = nullptr;
     471                delete q;
     472                return this;
     473        } // if
     474
     475        checkQualifiers( q->type, type );
     476        addQualifiersToType( q->type, type );
     477
     478        if ( q->type->forall ) {
     479                if ( type->forall ) {
     480                        type->forall->appendList( q->type->forall );
     481                } else {
     482                        if ( type->kind == TypeData::Aggregate ) {
     483                                type->aggregate.params = q->type->forall;
     484                                // change implicit typedef from TYPEDEFname to TYPEGENname
     485                                typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG );
    454486                        } else {
    455                                 checkQualifiers( q->type, type );
     487                                type->forall = q->type->forall;
    456488                        } // 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
     489                } // if
     490                q->type->forall = nullptr;
     491        } // if
     492        delete q;
     493        return this;
     494} // addQualifiers
     495
     496static void addTypeToType( TypeData *&src, TypeData *&dst ) {
     497        if ( src->forall && dst->kind == TypeData::Function ) {
     498                if ( dst->forall ) {
     499                        dst->forall->appendList( src->forall );
     500                } else {
     501                        dst->forall = src->forall;
     502                } // if
     503                src->forall = nullptr;
     504        } // if
     505        if ( dst->base ) {
     506                addTypeToType( src, dst->base );
     507        } else {
     508                switch ( dst->kind ) {
     509                  case TypeData::Unknown:
     510                        src->qualifiers |= dst->qualifiers;
     511                        dst = src;
     512                        src = nullptr;
     513                        break;
     514                  case TypeData::Basic:
     515                        dst->qualifiers |= src->qualifiers;
     516                        if ( src->kind != TypeData::Unknown ) {
     517                                assert( src->kind == TypeData::Basic );
     518
     519                                if ( dst->basictype == DeclarationNode::NoBasicType ) {
     520                                        dst->basictype = src->basictype;
     521                                } else if ( src->basictype != DeclarationNode::NoBasicType )
     522                                        throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::basicTypeName[ src->basictype ] + " in type: ", src );
     523
     524                                if ( dst->complextype == DeclarationNode::NoComplexType ) {
     525                                        dst->complextype = src->complextype;
     526                                } else if ( src->complextype != DeclarationNode::NoComplexType )
     527                                        throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::complexTypeName[ src->complextype ] + " in type: ", src );
     528
     529                                if ( dst->signedness == DeclarationNode::NoSignedness ) {
     530                                        dst->signedness = src->signedness;
     531                                } else if ( src->signedness != DeclarationNode::NoSignedness )
     532                                        throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::signednessName[ src->signedness ] + " in type: ", src );
     533
     534                                if ( dst->length == DeclarationNode::NoLength ) {
     535                                        dst->length = src->length;
     536                                } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
     537                                        dst->length = DeclarationNode::LongLong;
     538                                } else if ( src->length != DeclarationNode::NoLength )
     539                                        throw SemanticError( std::string( "conflicting type specifier " ) + DeclarationNode::lengthName[ src->length ] + " in type: ", src );
     540                        } // if
     541                        break;
     542                  default:
     543                        switch ( src->kind ) {
     544                          case TypeData::Aggregate:
     545                          case TypeData::Enum:
     546                                dst->base = new TypeData( TypeData::AggregateInst );
     547                                dst->base->aggInst.aggregate = src;
     548                                if ( src->kind == TypeData::Aggregate ) {
     549                                        dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
    469550                                } // if
    470                                 q->type->forall = 0;
    471                         } // if
    472                 } // if
    473         } // if
    474         delete q;
    475         return this;
    476 }
    477 
    478 static 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 );
    490                 } else {
    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 );
    523                                 } // if
     551                                dst->base->qualifiers |= src->qualifiers;
     552                                src = nullptr;
    524553                                break;
    525554                          default:
    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
     555                                if ( dst->forall ) {
     556                                        dst->forall->appendList( src->forall );
     557                                } else {
     558                                        dst->forall = src->forall;
     559                                } // if
     560                                src->forall = nullptr;
     561                                dst->base = src;
     562                                src = nullptr;
    547563                        } // switch
    548                 } // if
    549         } // if
    550 }
    551 
    552 DeclarationNode *DeclarationNode::addType( DeclarationNode *o ) {
     564                } // switch
     565        } // if
     566}
     567
     568DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
    553569        if ( o ) {
    554570                checkStorageClasses( o );
     
    566582                                        type = o->type;
    567583                                } // if
    568                                 o->type = 0;
     584                                o->type = nullptr;
    569585                        } else {
    570586                                addTypeToType( o->type, type );
     
    584600}
    585601
    586 DeclarationNode *DeclarationNode::addTypedef() {
    587         TypeData *newtype = new TypeData( TypeData::Symbolic );
    588         newtype->symbolic.params = 0;
     602DeclarationNode * DeclarationNode::addTypedef() {
     603        TypeData * newtype = new TypeData( TypeData::Symbolic );
     604        newtype->symbolic.params = nullptr;
    589605        newtype->symbolic.isTypedef = true;
    590         newtype->symbolic.name = name;
     606        newtype->symbolic.name = name ? new string( *name ) : nullptr;
    591607        newtype->base = type;
    592608        type = newtype;
     
    594610}
    595611
    596 DeclarationNode *DeclarationNode::addAssertions( DeclarationNode *assertions ) {
     612DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
     613        if ( variable.name ) {
     614                if ( variable.assertions ) {
     615                        variable.assertions->appendList( assertions );
     616                } else {
     617                        variable.assertions = assertions;
     618                } // if
     619                return this;
     620        } // if
     621
    597622        assert( type );
    598623        switch ( type->kind ) {
     
    604629                } // if
    605630                break;
    606           case TypeData::Variable:
    607                 if ( variable.assertions ) {
    608                         variable.assertions->appendList( assertions );
    609                 } else {
    610                         variable.assertions = assertions;
    611                 } // if
    612                 break;
     631          // case TypeData::Variable:
     632          //    if ( variable.assertions ) {
     633          //            variable.assertions->appendList( assertions );
     634          //    } else {
     635          //            variable.assertions = assertions;
     636          //    } // if
     637          //    break;
    613638          default:
    614639                assert( false );
     
    618643}
    619644
    620 DeclarationNode *DeclarationNode::addName( std::string *newname ) {
    621         name = assign_strptr( newname );
    622         return this;
    623 }
    624 
    625 DeclarationNode *DeclarationNode::addBitfield( ExpressionNode *size ) {
     645DeclarationNode * DeclarationNode::addName( std::string * newname ) {
     646        assert( ! name );
     647        name = newname;
     648        return this;
     649}
     650
     651DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
    626652        bitfieldWidth = size;
    627653        return this;
    628654}
    629655
    630 DeclarationNode *DeclarationNode::addVarArgs() {
     656DeclarationNode * DeclarationNode::addVarArgs() {
    631657        assert( type );
    632658        hasEllipsis = true;
     
    634660}
    635661
    636 DeclarationNode *DeclarationNode::addFunctionBody( StatementNode *body ) {
     662DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body ) {
    637663        assert( type );
    638664        assert( type->kind == TypeData::Function );
    639         assert( type->function.body == 0 );
     665        assert( ! type->function.body );
    640666        type->function.body = body;
    641667        type->function.hasBody = true;
     
    643669}
    644670
    645 DeclarationNode *DeclarationNode::addOldDeclList( DeclarationNode *list ) {
     671DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
    646672        assert( type );
    647673        assert( type->kind == TypeData::Function );
    648         assert( type->function.oldDeclList == 0 );
     674        assert( ! type->function.oldDeclList );
    649675        type->function.oldDeclList = list;
    650676        return this;
    651677}
    652678
    653 static void setBase( TypeData *&type, TypeData *newType ) {
     679static void setBase( TypeData *&type, TypeData * newType ) {
    654680        if ( type ) {
    655                 TypeData *prevBase = type;
    656                 TypeData *curBase = type->base;
    657                 while ( curBase != 0 ) {
     681                TypeData * prevBase = type;
     682                TypeData * curBase = type->base;
     683                while ( curBase != nullptr ) {
    658684                        prevBase = curBase;
    659685                        curBase = curBase->base;
     
    665691}
    666692
    667 DeclarationNode *DeclarationNode::addPointer( DeclarationNode *p ) {
     693DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
    668694        if ( p ) {
    669695                assert( p->type->kind == TypeData::Pointer );
    670696                setBase( type, p->type );
    671                 p->type = 0;
     697                p->type = nullptr;
    672698                delete p;
    673699        } // if
     
    675701}
    676702
    677 DeclarationNode *DeclarationNode::addArray( DeclarationNode *a ) {
     703DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
    678704        if ( a ) {
    679705                assert( a->type->kind == TypeData::Array );
    680706                setBase( type, a->type );
    681                 a->type = 0;
     707                a->type = nullptr;
    682708                delete a;
    683709        } // if
     
    685711}
    686712
    687 DeclarationNode *DeclarationNode::addNewPointer( DeclarationNode *p ) {
     713DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
    688714        if ( p ) {
    689715                assert( p->type->kind == TypeData::Pointer );
     
    703729                                p->type->base = type;
    704730                        } // switch
    705                         type = 0;
     731                        type = nullptr;
    706732                } // if
    707733                delete this;
     
    712738}
    713739
    714 static TypeData *findLast( TypeData *a ) {
     740static TypeData * findLast( TypeData * a ) {
    715741        assert( a );
    716         TypeData *cur = a;
     742        TypeData * cur = a;
    717743        while ( cur->base ) {
    718744                cur = cur->base;
     
    721747}
    722748
    723 DeclarationNode *DeclarationNode::addNewArray( DeclarationNode *a ) {
     749DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
    724750        if ( a ) {
    725751                assert( a->type->kind == TypeData::Array );
    726                 TypeData *lastArray = findLast( a->type );
     752                TypeData * lastArray = findLast( a->type );
    727753                if ( type ) {
    728754                        switch ( type->kind ) {
     
    739765                                lastArray->base = type;
    740766                        } // switch
    741                         type = 0;
     767                        type = nullptr;
    742768                } // if
    743769                delete this;
     
    748774}
    749775
    750 DeclarationNode *DeclarationNode::addParamList( DeclarationNode *params ) {
    751         TypeData *ftype = new TypeData( TypeData::Function );
     776DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
     777        TypeData * ftype = new TypeData( TypeData::Function );
    752778        ftype->function.params = params;
    753779        setBase( type, ftype );
     
    755781}
    756782
    757 static TypeData *addIdListToType( TypeData *type, DeclarationNode *ids ) {
     783static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
    758784        if ( type ) {
    759785                if ( type->kind != TypeData::Function ) {
     
    764790                return type;
    765791        } else {
    766                 TypeData *newtype = new TypeData( TypeData::Function );
     792                TypeData * newtype = new TypeData( TypeData::Function );
    767793                newtype->function.idList = ids;
    768794                return newtype;
    769795        } // if
    770 }
    771 
    772 DeclarationNode *DeclarationNode::addIdList( DeclarationNode *ids ) {
     796} // addIdListToType
     797
     798DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
    773799        type = addIdListToType( type, ids );
    774800        return this;
    775801}
    776802
    777 DeclarationNode *DeclarationNode::addInitializer( InitializerNode *init ) {
    778         //assert
     803DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
    779804        initializer = init;
    780805        return this;
    781806}
    782807
    783 DeclarationNode *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 
    808 DeclarationNode *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 
    840 DeclarationNode *DeclarationNode::cloneType( string *newName ) {
    841         DeclarationNode *newnode = new DeclarationNode;
     808DeclarationNode * DeclarationNode::cloneType( string * newName ) {
     809        DeclarationNode * newnode = new DeclarationNode;
    842810        newnode->type = maybeClone( type );
    843811        assert( storageClass == NoStorageClass );
    844812        newnode->copyStorageClasses( this );
    845         newnode->name = assign_strptr( newName );
    846         return newnode;
    847 }
    848 
    849 DeclarationNode *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;
     813        assert( newName );
     814        newnode->name = newName;
     815        return newnode;
     816}
     817
     818DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
     819        if ( ! o ) return nullptr;
     820
     821        o->copyStorageClasses( this );
     822        if ( type ) {
     823                TypeData * srcType = type;
     824
     825                while ( srcType->base ) {
     826                        srcType = srcType->base;
     827                } // while
     828
     829                TypeData * newType = srcType->clone();
     830                if ( newType->kind == TypeData::AggregateInst ) {
     831                        // don't duplicate members
     832                        if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
     833                                delete newType->aggInst.aggregate->enumeration.constants;
     834                                newType->aggInst.aggregate->enumeration.constants = nullptr;
    857835                        } else {
    858                                 addTypeToType( newType, o->type );
    859                                 delete newType;
     836                                assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
     837                                delete newType->aggInst.aggregate->aggregate.fields;
     838                                newType->aggInst.aggregate->aggregate.fields = nullptr;
    860839                        } // if
    861840                } // if
    862         } // if
    863         delete o;
     841
     842                newType->forall = maybeClone( type->forall );
     843                if ( ! o->type ) {
     844                        o->type = newType;
     845                } else {
     846                        addTypeToType( newType, o->type );
     847                        delete newType;
     848                } // if
     849        } // if
    864850        return o;
    865851}
    866852
    867 DeclarationNode *DeclarationNode::extractAggregate() const {
     853DeclarationNode * DeclarationNode::extractAggregate() const {
    868854        if ( type ) {
    869                 TypeData *ret = typeextractAggregate( type );
     855                TypeData * ret = typeextractAggregate( type );
    870856                if ( ret ) {
    871                         DeclarationNode *newnode = new DeclarationNode;
     857                        DeclarationNode * newnode = new DeclarationNode;
    872858                        newnode->type = ret;
    873859                        return newnode;
    874860                } // if
    875861        } // if
    876         return 0;
    877 }
    878 
    879 void buildList( const DeclarationNode *firstNode, std::list< Declaration * > &outputList ) {
     862        return nullptr;
     863}
     864
     865void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
    880866        SemanticError errors;
    881867        std::back_insert_iterator< std::list< Declaration * > > out( outputList );
    882         const DeclarationNode *cur = firstNode;
     868        const DeclarationNode * cur = firstNode;
     869
    883870        while ( cur ) {
    884871                try {
    885                         if ( DeclarationNode *extr = cur->extractAggregate() ) {
     872                        if ( DeclarationNode * extr = cur->extractAggregate() ) {
    886873                                // handle the case where a structure declaration is contained within an object or type declaration
    887                                 Declaration *decl = extr->build();
     874                                Declaration * decl = extr->build();
    888875                                if ( decl ) {
    889                                         *out++ = decl;
     876                                        * out++ = decl;
    890877                                } // if
    891878                                delete extr;
    892879                        } // if
    893                         Declaration *decl = cur->build();
     880
     881                        Declaration * decl = cur->build();
    894882                        if ( decl ) {
    895                                 *out++ = decl;
     883                                * out++ = decl;
    896884                        } // if
    897885                } catch( SemanticError &e ) {
     
    900888                cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
    901889        } // while
     890
    902891        if ( ! errors.isEmpty() ) {
    903892                throw errors;
    904893        } // if
    905 }
    906 
    907 void buildList( const DeclarationNode *firstNode, std::list< DeclarationWithType * > &outputList ) {
     894} // buildList
     895
     896void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
    908897        SemanticError errors;
    909898        std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
    910         const DeclarationNode *cur = firstNode;
     899        const DeclarationNode * cur = firstNode;
    911900        while ( cur ) {
    912901                try {
    913                         Declaration *decl = cur->build();
     902                        Declaration * decl = cur->build();
    914903                        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 );
     904                                if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
     905                                        * out++ = dwt;
     906                                } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
     907                                        StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
     908                                        * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
    920909                                        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 );
     910                                } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
     911                                        UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
     912                                        * out++ = new ObjectDecl( "", DeclarationNode::NoStorageClass, linkage, nullptr, inst, nullptr );
    924913                                } // if
    925914                        } // if
     
    932921                throw errors;
    933922        } // if
    934 }
    935 
    936 void buildTypeList( const DeclarationNode *firstNode, std::list< Type * > &outputList ) {
     923} // buildList
     924
     925void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
    937926        SemanticError errors;
    938927        std::back_insert_iterator< std::list< Type * > > out( outputList );
    939         const DeclarationNode *cur = firstNode;
     928        const DeclarationNode * cur = firstNode;
     929
    940930        while ( cur ) {
    941931                try {
    942                         *out++ = cur->buildType();
     932                        * out++ = cur->buildType();
    943933                } catch( SemanticError &e ) {
    944934                        errors.append( e );
     
    946936                cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
    947937        } // while
     938
    948939        if ( ! errors.isEmpty() ) {
    949940                throw errors;
    950941        } // if
    951 }
    952 
    953 Declaration *DeclarationNode::build() const {
     942} // buildTypeList
     943
     944Declaration * DeclarationNode::build() const {
    954945        if ( ! error.empty() ) throw SemanticError( error + " in declaration of ", this );
     946
     947        if ( variable.name ) {
     948                static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Ftype, TypeDecl::Dtype };
     949                TypeDecl * ret = new TypeDecl( *variable.name, DeclarationNode::NoStorageClass, nullptr, kindMap[ variable.tyClass ] );
     950                buildList( variable.assertions, ret->get_assertions() );
     951                return ret;
     952        } // if
     953
    955954        if ( type ) {
    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;
     955                return buildDecl( type, name ? *name : string( "" ), storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );
     956        } // if
     957
     958        if ( ! isInline && ! isNoreturn ) {
     959                assertf( name, "ObjectDecl are assumed to have names\n" );
     960                return (new ObjectDecl( *name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );
     961        } // if
     962
     963        throw SemanticError( "invalid function specifier ", this );
     964}
     965
     966Type * DeclarationNode::buildType() const {
     967        assert( type );
     968
     969        if ( attr.name ) {
     970                AttrType * ret;
     971                if ( attr.expr ) {
     972                        ret = new AttrType( buildQualifiers( type ), *attr.name, attr.expr->build() );
    961973                } else {
    962                         return buildDecl( type, name, storageClass, maybeBuild< Expression >( bitfieldWidth ), isInline, isNoreturn, linkage, maybeBuild< Initializer >(initializer) )->set_extension( extension );
    963                 } // if
    964         } // if
    965         if ( ! isInline && ! isNoreturn ) {
    966                 return (new ObjectDecl( name, storageClass, linkage, maybeBuild< Expression >( bitfieldWidth ), 0, maybeBuild< Initializer >( initializer ) ))->set_extension( extension );
    967         } // if
    968         throw SemanticError( "invalid function specifier ", this );
    969 }
    970 
    971 Type *DeclarationNode::buildType() const {
    972         assert( type );
     974                        assert( attr.type );
     975                        ret = new AttrType( buildQualifiers( type ), *attr.name, attr.type->buildType() );
     976                } // if
     977                return ret;
     978        } // if
    973979
    974980        switch ( type->kind ) {
    975981          case TypeData::Enum:
    976                 return new EnumInstType( buildQualifiers( type ), type->enumeration.name );
     982                return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
    977983          case TypeData::Aggregate: {
    978                   ReferenceToType *ret;
     984                  ReferenceToType * ret;
    979985                  switch ( type->aggregate.kind ) {
    980986                        case DeclarationNode::Struct:
    981                           ret = new StructInstType( buildQualifiers( type ), type->aggregate.name );
     987                          ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
    982988                          break;
    983989                        case DeclarationNode::Union:
    984                           ret = new UnionInstType( buildQualifiers( type ), type->aggregate.name );
     990                          ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
    985991                          break;
    986992                        case DeclarationNode::Trait:
    987                           ret = new TraitInstType( buildQualifiers( type ), type->aggregate.name );
     993                          ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
    988994                          break;
    989995                        default:
     
    9941000          }
    9951001          case TypeData::Symbolic: {
    996                   TypeInstType *ret = new TypeInstType( buildQualifiers( type ), type->symbolic.name, false );
     1002                  TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false );
    9971003                  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
    10101004                  return ret;
    10111005          }
Note: See TracChangeset for help on using the changeset viewer.