Changeset 0153dbd


Ignore:
Timestamp:
Apr 29, 2024, 6:35:39 PM (6 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
69867ad9
Parents:
5c27b6a
Message:

Updated hoistStruct so the nested mangled names are human readable. This does require more code to update instances, but I think it is reasonable in complexity and runtime.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Validate/HoistStruct.cpp

    r5c27b6a r0153dbd  
    2727namespace {
    2828
     29/// Is this a declaration can appear in a struct/union and should be hoisted?
    2930bool shouldHoist( ast::Decl const * decl ) {
    3031        return dynamic_cast< ast::StructDecl const * >( decl )
     
    3435}
    3536
    36 /* This pass also does some renaming and internal field alteration, but the
    37  * complex part is the actual hoisting. Hoisted declarations should always
     37/// Helper that updates an InstType if the base name could be updated.
     38template<typename InstType>
     39InstType const * preInstType( InstType const * type ) {
     40        assert( type->base );
     41        if ( nullptr == type->base->parent ) return type;
     42        auto mut = ast::mutate( type );
     43        mut->name = mut->base->name;
     44        return mut;
     45}
     46
     47/// Update StructInstType and UnionInstType names.
     48struct NameUpdater {
     49        ast::StructInstType const * previsit( ast::StructInstType const * type ) {
     50                return preInstType( type );
     51        }
     52
     53        ast::UnionInstType const * previsit( ast::UnionInstType const * type ) {
     54                return preInstType( type );
     55        }
     56};
     57
     58ast::Decl const * updateNames( ast::Decl const * decl ) {
     59        ast::Pass<NameUpdater> visitor;
     60        return decl->accept( visitor );
     61}
     62
     63/* This pass hoists from structs/unions. Hoisted declarations should always
    3864 * appear before the declaration they are hoisted out of and if two types are
    3965 * nested in the same declaration their order should not change.
     66 * It also sets up parent relationships, does name mangling of hoisted types
     67 * and updates instance types of the hoisted types.
    4068 */
    4169struct HoistStructCore final :
     
    96124                auto mut = ast::mutate( decl );
    97125                mut->parent = parent;
    98                 mut->name = qualifiedName( mut );
    99126                extendParams( mut->params, parent->params );
    100127                decl = mut;
     
    116143                }
    117144        }
     145        // Is this a nested type? Then update the name, after the parent's name
     146        // has been updated (hence the post visit).
     147        if ( mut->parent ) {
     148                mut->name = qualifiedName( mut );
     149        // Top level type that has hoisted? Then do a second pass subpass to make
     150        // sure we update instance type names after the declaration is renamed.
     151        } else if ( !declsToAddBefore.empty() ) {
     152                for ( ast::ptr<ast::Decl> & member : mut->members ) {
     153                        member = updateNames( member.get() );
     154                }
     155                for ( ast::ptr<ast::Decl> & declToAdd : declsToAddBefore ) {
     156                        declToAdd = updateNames( declToAdd );
     157                }
     158        }
    118159        return mut;
    119160}
     
    167208}
    168209
    169 template<typename InstType>
    170 InstType const * preInstType( InstType const * type ) {
    171         assert( type->base );
    172         auto mut = ast::mutate( type );
    173         mut->name = mut->base->name;
    174         return mut;
    175 }
    176 
    177210ast::StructInstType const * HoistStructCore::previsit( ast::StructInstType const * type ) {
    178211        return preInstType( preCollectionInstType( type ) );
Note: See TracChangeset for help on using the changeset viewer.