Ignore:
Timestamp:
Mar 26, 2024, 3:17:51 PM (3 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
544b799
Parents:
84886499
Message:

Fused TypeData::Enum and TypeData::Aggregate, an enumeration is a kind of aggregate after all. There will always be some unused fields in Aggregate_t (but less in TypeData? overall) but the code is almost always simpler.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/DeclarationNode.cc

    r84886499 r67467a3  
    189189
    190190DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body, bool typed, DeclarationNode * base, EnumHiding hiding ) {
    191         DeclarationNode * newnode = new DeclarationNode;
    192         newnode->type = new TypeData( TypeData::Enum );
    193         newnode->type->enumeration.anon = name == nullptr;
    194         newnode->type->enumeration.name = newnode->type->enumeration.anon ? new string( DeclarationNode::anonymous.newName() ) : name;
    195         newnode->type->enumeration.constants = constants;
    196         newnode->type->enumeration.body = body;
    197         newnode->type->enumeration.typed = typed;
    198         newnode->type->enumeration.hiding = hiding;
     191        DeclarationNode * newnode = newAggregate( ast::AggregateDecl::Enum, name, nullptr, constants, body );
     192        newnode->type->aggregate.typed = typed;
     193        newnode->type->aggregate.hiding = hiding;
    199194        if ( base ) {
    200195                assert( typed );
     
    549544        newaggr->aggregate.body = false;
    550545        newaggr->aggregate.anon = oldaggr->aggregate.anon;
     546        newaggr->aggregate.typed = oldaggr->aggregate.typed;
     547        newaggr->aggregate.hiding = oldaggr->aggregate.hiding;
    551548        swap( newaggr, oldaggr );
    552549
     
    558555
    559556        moveUnionAttribute( olddecl, newdecl );
    560 
    561         return newdecl;
    562 }
    563 
    564 // Helper for addTypedef, handles the case where the typedef wraps an
    565 // enumeration declaration (not a type), returns a chain of nodes.
    566 static DeclarationNode * addTypedefEnum(
    567                 DeclarationNode * olddecl, TypeData * newtype ) {
    568         TypeData *& oldenum = olddecl->type->aggInst.aggregate;
    569 
    570         // Handle anonymous enumeration: typedef enum { A, B, C } foo
    571         // Give the typedefed type a consistent name across translation units.
    572         if ( oldenum->enumeration.anon ) {
    573                 delete oldenum->enumeration.name;
    574                 oldenum->enumeration.name = new string( "__anonymous_" + *olddecl->name );
    575                 oldenum->enumeration.anon = false;
    576                 oldenum->qualifiers.reset();
    577         }
    578 
    579         // Replace the wrapped TypeData with a forward declaration.
    580         TypeData * newenum = new TypeData( TypeData::Enum );
    581         newenum->enumeration.name = oldenum->enumeration.name ? new string( *oldenum->enumeration.name ) : nullptr;
    582         newenum->enumeration.body = false;
    583         newenum->enumeration.anon = oldenum->enumeration.anon;
    584         newenum->enumeration.typed = oldenum->enumeration.typed;
    585         newenum->enumeration.hiding = oldenum->enumeration.hiding;
    586         swap( newenum, oldenum );
    587 
    588         newtype->base = olddecl->type;
    589         olddecl->type = newtype;
    590         DeclarationNode * newdecl = new DeclarationNode;
    591         newdecl->type = newenum;
    592         newdecl->next = olddecl;
    593557
    594558        return newdecl;
     
    610574                        && type->aggInst.aggregate->aggregate.body ) {
    611575                return addTypedefAggr( this, newtype );
    612         // If this typedef is wrapping an enumeration, separate them out.
    613         } else if ( TypeData::AggregateInst == type->kind
    614                         && TypeData::Enum == type->aggInst.aggregate->kind
    615                         && type->aggInst.aggregate->enumeration.body ) {
    616                 return addTypedefEnum( this, newtype );
    617576        // There is no internal declaration, just a type.
    618577        } else {
     
    854813                                                // typedef struct { int A } B is the only case?
    855814                                                extracted_named = ! extr->type->aggregate.anon;
    856                                         } else if ( extr->type->kind == TypeData::Enum ) {
    857                                                 // typedef enum { A } B is the only case?
    858                                                 extracted_named = ! extr->type->enumeration.anon;
    859815                                        } else {
    860816                                                extracted_named = true;
     
    10631019
    10641020        switch ( type->kind ) {
    1065         case TypeData::Enum:
    10661021        case TypeData::Aggregate: {
    10671022                ast::BaseInstType * ret =
Note: See TracChangeset for help on using the changeset viewer.