Ignore:
Timestamp:
Nov 21, 2023, 4:47:58 PM (5 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
53dac82
Parents:
c36a419
Message:

Round of significant clean-up and reindentation of InitTweak? directory.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/GenInit.cc

    rc36a419 r8984003  
    206206        };
    207207
    208 
    209 
    210 
    211208        struct ReturnFixer final :
    212209                        public ast::WithStmtsToAdd<>, ast::WithGuards, ast::WithShortCircuiting {
     
    262259} // namespace
    263260
    264         void genInit( ast::TranslationUnit & transUnit ) {
    265                 ast::Pass<HoistArrayDimension_NoResolve>::run( transUnit );
    266                 ast::Pass<ReturnFixer>::run( transUnit );
    267         }
    268 
    269         void fixReturnStatements( ast::TranslationUnit & transUnit ) {
    270                 ast::Pass<ReturnFixer>::run( transUnit );
    271         }
    272 
    273         bool ManagedTypes::isManaged( const ast::Type * type ) const {
    274                 // references are never constructed
    275                 if ( dynamic_cast< const ast::ReferenceType * >( type ) ) return false;
    276                 if ( auto tupleType = dynamic_cast< const ast::TupleType * > ( type ) ) {
    277                         // tuple is also managed if any of its components are managed
    278                         for (auto & component : tupleType->types) {
    279                                 if (isManaged(component)) return true;
    280                         }
    281                 }
    282                 // need to clear and reset qualifiers when determining if a type is managed
    283                 // ValueGuard< Type::Qualifiers > qualifiers( type->get_qualifiers() );
    284                 auto tmp = shallowCopy(type);
    285                 tmp->qualifiers = {};
    286                 // delete tmp at return
    287                 ast::ptr<ast::Type> guard = tmp;
    288                 // a type is managed if it appears in the map of known managed types, or if it contains any polymorphism (is a type variable or generic type containing a type variable)
    289                 return managedTypes.find( Mangle::mangle( tmp, {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) ) != managedTypes.end() || GenPoly::isPolyType( tmp );
    290         }
    291 
    292         bool ManagedTypes::isManaged( const ast::ObjectDecl * objDecl ) const {
    293                 const ast::Type * type = objDecl->type;
    294                 while ( auto at = dynamic_cast< const ast::ArrayType * >( type ) ) {
    295                         // must always construct VLAs with an initializer, since this is an error in C
    296                         if ( at->isVarLen && objDecl->init ) return true;
    297                         type = at->base;
    298                 }
    299                 return isManaged( type );
    300         }
    301 
    302         void ManagedTypes::handleDWT( const ast::DeclWithType * dwt ) {
    303                 // if this function is a user-defined constructor or destructor, mark down the type as "managed"
    304                 if ( ! dwt->linkage.is_overrideable && CodeGen::isCtorDtor( dwt->name ) ) {
    305                         auto & params = GenPoly::getFunctionType( dwt->get_type())->params;
    306                         assert( ! params.empty() );
    307                         // Type * type = InitTweak::getPointerBase( params.front() );
    308                         // assert( type );
    309                         managedTypes.insert( Mangle::mangle( params.front(), {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) );
    310                 }
    311         }
    312 
    313         void ManagedTypes::handleStruct( const ast::StructDecl * aggregateDecl ) {
    314                 // don't construct members, but need to take note if there is a managed member,
    315                 // because that means that this type is also managed
    316                 for ( auto & member : aggregateDecl->members ) {
    317                         if ( auto field = member.as<ast::ObjectDecl>() ) {
    318                                 if ( isManaged( field ) ) {
    319                                         // generic parameters should not play a role in determining whether a generic type is constructed - construct all generic types, so that
    320                                         // polymorphic constructors make generic types managed types
    321                                         ast::StructInstType inst( aggregateDecl );
    322                                         managedTypes.insert( Mangle::mangle( &inst, {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) );
    323                                         break;
    324                                 }
    325                         }
    326                 }
    327         }
    328 
    329         void ManagedTypes::beginScope() { managedTypes.beginScope(); }
    330         void ManagedTypes::endScope() { managedTypes.endScope(); }
    331 
    332         ast::ptr<ast::Stmt> genCtorDtor (const CodeLocation & loc, const std::string & fname, const ast::ObjectDecl * objDecl, const ast::Expr * arg) {
    333                 assertf(objDecl, "genCtorDtor passed null objDecl");
    334                 InitExpander srcParam(arg);
    335                 return SymTab::genImplicitCall(srcParam, new ast::VariableExpr(loc, objDecl), loc, fname, objDecl);
    336         }
     261void genInit( ast::TranslationUnit & transUnit ) {
     262        ast::Pass<HoistArrayDimension_NoResolve>::run( transUnit );
     263        ast::Pass<ReturnFixer>::run( transUnit );
     264}
     265
     266void fixReturnStatements( ast::TranslationUnit & transUnit ) {
     267        ast::Pass<ReturnFixer>::run( transUnit );
     268}
     269
     270bool ManagedTypes::isManaged( const ast::Type * type ) const {
     271        // references are never constructed
     272        if ( dynamic_cast< const ast::ReferenceType * >( type ) ) return false;
     273        if ( auto tupleType = dynamic_cast< const ast::TupleType * > ( type ) ) {
     274                // tuple is also managed if any of its components are managed
     275                for (auto & component : tupleType->types) {
     276                        if (isManaged(component)) return true;
     277                }
     278        }
     279        // need to clear and reset qualifiers when determining if a type is managed
     280        // ValueGuard< Type::Qualifiers > qualifiers( type->get_qualifiers() );
     281        auto tmp = shallowCopy(type);
     282        tmp->qualifiers = {};
     283        // delete tmp at return
     284        ast::ptr<ast::Type> guard = tmp;
     285        // a type is managed if it appears in the map of known managed types, or if it contains any polymorphism (is a type variable or generic type containing a type variable)
     286        return managedTypes.find( Mangle::mangle( tmp, {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) ) != managedTypes.end() || GenPoly::isPolyType( tmp );
     287}
     288
     289bool ManagedTypes::isManaged( const ast::ObjectDecl * objDecl ) const {
     290        const ast::Type * type = objDecl->type;
     291        while ( auto at = dynamic_cast< const ast::ArrayType * >( type ) ) {
     292                // must always construct VLAs with an initializer, since this is an error in C
     293                if ( at->isVarLen && objDecl->init ) return true;
     294                type = at->base;
     295        }
     296        return isManaged( type );
     297}
     298
     299void ManagedTypes::handleDWT( const ast::DeclWithType * dwt ) {
     300        // if this function is a user-defined constructor or destructor, mark down the type as "managed"
     301        if ( ! dwt->linkage.is_overrideable && CodeGen::isCtorDtor( dwt->name ) ) {
     302                auto & params = GenPoly::getFunctionType( dwt->get_type())->params;
     303                assert( ! params.empty() );
     304                // Type * type = InitTweak::getPointerBase( params.front() );
     305                // assert( type );
     306                managedTypes.insert( Mangle::mangle( params.front(), {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) );
     307        }
     308}
     309
     310void ManagedTypes::handleStruct( const ast::StructDecl * aggregateDecl ) {
     311        // don't construct members, but need to take note if there is a managed member,
     312        // because that means that this type is also managed
     313        for ( auto & member : aggregateDecl->members ) {
     314                if ( auto field = member.as<ast::ObjectDecl>() ) {
     315                        if ( isManaged( field ) ) {
     316                                // generic parameters should not play a role in determining whether a generic type is constructed - construct all generic types, so that
     317                                // polymorphic constructors make generic types managed types
     318                                ast::StructInstType inst( aggregateDecl );
     319                                managedTypes.insert( Mangle::mangle( &inst, {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) );
     320                                break;
     321                        }
     322                }
     323        }
     324}
     325
     326void ManagedTypes::beginScope() { managedTypes.beginScope(); }
     327void ManagedTypes::endScope() { managedTypes.endScope(); }
     328
     329ast::ptr<ast::Stmt> genCtorDtor (const CodeLocation & loc, const std::string & fname, const ast::ObjectDecl * objDecl, const ast::Expr * arg) {
     330        assertf(objDecl, "genCtorDtor passed null objDecl");
     331        InitExpander srcParam(arg);
     332        return SymTab::genImplicitCall(srcParam, new ast::VariableExpr(loc, objDecl), loc, fname, objDecl);
     333}
    337334
    338335ast::ConstructorInit * genCtorInit( const CodeLocation & loc, const ast::ObjectDecl * objDecl ) {
Note: See TracChangeset for help on using the changeset viewer.