Ignore:
Timestamp:
Sep 21, 2022, 9:22:24 AM (22 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, master, pthread-emulation
Children:
20737104
Parents:
b6c3688
Message:

Used the WithCodeLocation? helper in more passes. This cleans up some code and should improve efficiency.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Validate/ReplaceTypedef.cpp

    rb6c3688 re9e9f56  
    1010// Created On       : Tue Jun 29 14:59:00 2022
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Wed Jul 13 14:45:00 2022
    13 // Update Count     : 1
     12// Last Modified On : Tue Sep 20 17:00:00 2022
     13// Update Count     : 2
    1414//
    1515
     
    3939
    4040struct ReplaceTypedefCore final :
    41                 public ast::WithVisitorRef<ReplaceTypedefCore>,
     41                public ast::WithCodeLocation,
     42                public ast::WithDeclsToAdd<>,
    4243                public ast::WithGuards,
    4344                public ast::WithShortCircuiting,
    44                 public ast::WithDeclsToAdd<> {
    45 
    46         void previsit( ast::ParseNode const * node );
     45                public ast::WithVisitorRef<ReplaceTypedefCore> {
     46
    4747        void previsit( ast::QualifiedType const * );
    4848        ast::Type const * postvisit( ast::QualifiedType const * );
     
    7474        TypedefMap typedefNames;
    7575        TypeDeclMap typedeclNames;
    76         CodeLocation const * nearestLocation = nullptr;
    7776        int scopeLevel;
    7877        bool isAtFunctionTop = false;
    7978};
    80 
    81 void ReplaceTypedefCore::previsit( ast::ParseNode const * node ) {
    82         GuardValue( nearestLocation ) = &node->location;
    83 }
    8479
    8580void ReplaceTypedefCore::previsit( ast::QualifiedType const * ) {
     
    115110                        auto rtt = dynamic_cast<ast::BaseInstType *>( ret );
    116111                        if ( !rtt ) {
    117                                 assert( nearestLocation );
    118                                 SemanticError( *nearestLocation, "Cannot apply type parameters to base type of " + type->name );
     112                                assert( location );
     113                                SemanticError( *location, "Cannot apply type parameters to base type of " + type->name );
    119114                        }
    120115                        rtt->params.clear();
     
    129124                TypeDeclMap::const_iterator base = typedeclNames.find( type->name );
    130125                if ( base == typedeclNames.end() ) {
    131                         assert( nearestLocation );
    132                         SemanticError( *nearestLocation, toString( "Use of undefined type ", type->name ) );
     126                        assert( location );
     127                        SemanticError( *location, toString( "Use of undefined type ", type->name ) );
    133128                }
    134129                return ast::mutate_field( type, &ast::TypeInstType::base, base->second );
     
    191186
    192187void ReplaceTypedefCore::previsit( ast::TypeDecl const * decl ) {
    193         previsit( (ast::ParseNode const *)decl );
    194188        TypedefMap::iterator iter = typedefNames.find( decl->name );
    195189        if ( iter != typedefNames.end() ) {
     
    200194
    201195void ReplaceTypedefCore::previsit( ast::FunctionDecl const * decl ) {
    202         previsit( (ast::ParseNode const *)decl );
    203196        GuardScope( typedefNames );
    204197        GuardScope( typedeclNames );
     
    207200
    208201void ReplaceTypedefCore::previsit( ast::ObjectDecl const * decl ) {
    209         previsit( (ast::ParseNode const *)decl );
    210202        GuardScope( typedefNames );
    211203        GuardScope( typedeclNames );
     
    217209                using DWTVector = std::vector<ast::ptr<ast::DeclWithType>>;
    218210                using DeclVector = std::vector<ast::ptr<ast::TypeDecl>>;
    219                 CodeLocation const & location = decl->location;
     211                CodeLocation const & declLocation = decl->location;
    220212                UniqueName paramNamer( decl->name + "Param" );
    221213
    222214                // Replace the current object declaration with a function declaration.
    223215                ast::FunctionDecl const * newDecl = new ast::FunctionDecl(
    224                         location,
     216                        declLocation,
    225217                        decl->name,
    226218                        map_range<DeclVector>( type->forall, []( const ast::TypeInstType * inst ) {
     
    230222                                return ast::deepCopy( expr->var );
    231223                        } ),
    232                         map_range<DWTVector>( type->params, [&location, &paramNamer]( const ast::Type * type ) {
     224                        map_range<DWTVector>( type->params, [&declLocation, &paramNamer]( const ast::Type * type ) {
    233225                                assert( type );
    234                                 return new ast::ObjectDecl( location, paramNamer.newName(), ast::deepCopy( type ) );
     226                                return new ast::ObjectDecl( declLocation, paramNamer.newName(), ast::deepCopy( type ) );
    235227                        } ),
    236                         map_range<DWTVector>( type->returns, [&location, &paramNamer]( const ast::Type * type ) {
     228                        map_range<DWTVector>( type->returns, [&declLocation, &paramNamer]( const ast::Type * type ) {
    237229                                assert( type );
    238                                 return new ast::ObjectDecl( location, paramNamer.newName(), ast::deepCopy( type ) );
     230                                return new ast::ObjectDecl( declLocation, paramNamer.newName(), ast::deepCopy( type ) );
    239231                        } ),
    240232                        nullptr,
     
    250242
    251243void ReplaceTypedefCore::previsit( ast::CastExpr const * expr ) {
    252         previsit( (ast::ParseNode const *)expr );
    253244        GuardScope( typedefNames );
    254245        GuardScope( typedeclNames );
     
    256247
    257248void ReplaceTypedefCore::previsit( ast::CompoundStmt const * expr ) {
    258         previsit( (ast::ParseNode const *)expr );
    259249        GuardScope( typedefNames );
    260250        GuardScope( typedeclNames );
     
    268258
    269259ast::StructDecl const * ReplaceTypedefCore::previsit( ast::StructDecl const * decl ) {
    270         previsit( (ast::ParseNode const *)decl );
    271260        visit_children = false;
    272261        addImplicitTypedef( decl );
     
    275264
    276265ast::UnionDecl const * ReplaceTypedefCore::previsit( ast::UnionDecl const * decl ) {
    277         previsit( (ast::ParseNode const *)decl );
    278266        visit_children = false;
    279267        addImplicitTypedef( decl );
     
    282270
    283271void ReplaceTypedefCore::previsit( ast::EnumDecl const * decl ) {
    284         previsit( (ast::ParseNode const *)decl );
    285272        addImplicitTypedef( decl );
    286273}
    287274
    288275void ReplaceTypedefCore::previsit( ast::TraitDecl const * decl ) {
    289         previsit( (ast::ParseNode const *)decl );
    290276        GuardScope( typedefNames );
    291277        GuardScope( typedeclNames );
Note: See TracChangeset for help on using the changeset viewer.