Ignore:
Timestamp:
Mar 16, 2022, 4:41:01 PM (2 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
f7496c5
Parents:
9d8124f
Message:

Thread global information through resolution. Non-top-level calls to the resolver have a bit of a hack but improvements would require changes to the Pass helpers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/FixInitNew.cpp

    r9d8124f r39d8950  
    8686        /// generate/resolve copy construction expressions for each, and generate/resolve destructors for both
    8787        /// arguments and return value temporaries
    88         struct ResolveCopyCtors final : public ast::WithGuards, public ast::WithStmtsToAdd<>, public ast::WithSymbolTable, public ast::WithShortCircuiting, public ast::WithVisitorRef<ResolveCopyCtors> {
     88        struct ResolveCopyCtors final : public ast::WithGuards, public ast::WithStmtsToAdd<>, public ast::WithSymbolTable, public ast::WithShortCircuiting, public ast::WithVisitorRef<ResolveCopyCtors>, public ast::WithConstTranslationUnit {
    8989                const ast::Expr * postvisit( const ast::ImplicitCopyCtorExpr * impCpCtorExpr );
    9090                const ast::StmtExpr * previsit( const ast::StmtExpr * stmtExpr );
     
    190190        /// for any member that is missing a corresponding ctor/dtor call.
    191191        /// error if a member is used before constructed
    192         struct GenStructMemberCalls final : public ast::WithGuards, public ast::WithShortCircuiting, public ast::WithSymbolTable, public ast::WithVisitorRef<GenStructMemberCalls> {
     192        struct GenStructMemberCalls final : public ast::WithGuards, public ast::WithShortCircuiting, public ast::WithSymbolTable, public ast::WithVisitorRef<GenStructMemberCalls>, public ast::WithConstTranslationUnit {
    193193                void previsit( const ast::FunctionDecl * funcDecl );
    194194                const ast::DeclWithType * postvisit( const ast::FunctionDecl * funcDecl );
     
    215215
    216216        /// expands ConstructorExpr nodes into comma expressions, using a temporary for the first argument
    217         struct FixCtorExprs final : public ast::WithDeclsToAdd<>, public ast::WithSymbolTable, public ast::WithShortCircuiting {
     217        struct FixCtorExprs final : public ast::WithDeclsToAdd<>, public ast::WithSymbolTable, public ast::WithShortCircuiting, public ast::WithConstTranslationUnit {
    218218                const ast::Expr * postvisit( const ast::ConstructorExpr * ctorExpr );
    219219        };
     
    510510                // (VariableExpr and already resolved expression)
    511511                CP_CTOR_PRINT( std::cerr << "ResolvingCtorDtor " << untyped << std::endl; )
    512                 ast::ptr<ast::Expr> resolved = ResolvExpr::findVoidExpression(untyped, symtab);
     512                ast::ptr<ast::Expr> resolved = ResolvExpr::findVoidExpression(untyped, { symtab, transUnit().global } );
    513513                assert( resolved );
    514514                if ( resolved->env ) {
     
    588588
    589589        ast::Expr * ResolveCopyCtors::destructRet( const ast::ObjectDecl * ret, const ast::Expr * arg ) {
     590                auto global = transUnit().global;
    590591                // TODO: refactor code for generating cleanup attribute, since it's common and reused in ~3-4 places
    591592                // check for existing cleanup attribute before adding another(?)
    592593                // need to add __Destructor for _tmp_cp variables as well
    593594
    594                 assertf( ast::dtorStruct, "Destructor generation requires __Destructor definition." );
    595                 assertf( ast::dtorStruct->members.size() == 2, "__Destructor definition does not have expected fields." );
    596                 assertf( ast::dtorStructDestroy, "Destructor generation requires __destroy_Destructor." );
     595                assertf( global.dtorStruct, "Destructor generation requires __Destructor definition." );
     596                assertf( global.dtorStruct->members.size() == 2, "__Destructor definition does not have expected fields." );
     597                assertf( global.dtorDestroy, "Destructor generation requires __destroy_Destructor." );
    597598
    598599                const CodeLocation loc = ret->location;
     
    611612                auto dtorFunc = getDtorFunc( ret, new ast::ExprStmt(loc, dtor ), stmtsToAddBefore );
    612613
    613                 auto dtorStructType = new ast::StructInstType(ast::dtorStruct);
     614                auto dtorStructType = new ast::StructInstType( global.dtorStruct );
    614615
    615616                // what does this do???
     
    623624                static UniqueName namer( "_ret_dtor" );
    624625                auto retDtor = new ast::ObjectDecl(loc, namer.newName(), dtorStructType, new ast::ListInit(loc, { new ast::SingleInit(loc, ast::ConstantExpr::null(loc) ), new ast::SingleInit(loc, new ast::CastExpr( new ast::VariableExpr(loc, dtorFunc ), dtorType ) ) } ) );
    625                 retDtor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr(loc, ast::dtorStructDestroy ) } ) );
     626                retDtor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr(loc, global.dtorDestroy ) } ) );
    626627                stmtsToAddBefore.push_back( new ast::DeclStmt(loc, retDtor ) );
    627628
    628629                if ( arg ) {
    629                         auto member = new ast::MemberExpr(loc, ast::dtorStruct->members.front().strict_as<ast::DeclWithType>(), new ast::VariableExpr(loc, retDtor ) );
     630                        auto member = new ast::MemberExpr(loc, global.dtorStruct->members.front().strict_as<ast::DeclWithType>(), new ast::VariableExpr(loc, retDtor ) );
    630631                        auto object = new ast::CastExpr( new ast::AddressExpr( new ast::VariableExpr(loc, ret ) ), new ast::PointerType(new ast::VoidType() ) );
    631632                        ast::Expr * assign = createBitwiseAssignment( member, object );
     
    11791180                        auto guard = makeFuncGuard( [this]() { symtab.enterScope(); }, [this]() { symtab.leaveScope(); } );
    11801181                        symtab.addFunction( function );
     1182                        auto global = transUnit().global;
    11811183
    11821184                        // need to iterate through members in reverse in order for
     
    12241226
    12251227                                                        static UniqueName memberDtorNamer = { "__memberDtor" };
    1226                                                         assertf( ast::dtorStruct, "builtin __Destructor not found." );
    1227                                                         assertf( ast::dtorStructDestroy, "builtin __destroy_Destructor not found." );
     1228                                                        assertf( global.dtorStruct, "builtin __Destructor not found." );
     1229                                                        assertf( global.dtorDestroy, "builtin __destroy_Destructor not found." );
    12281230
    12291231                                                        ast::Expr * thisExpr = new ast::CastExpr( new ast::AddressExpr( new ast::VariableExpr(loc, thisParam ) ), new ast::PointerType( new ast::VoidType(), ast::CV::Qualifiers() ) );
     
    12351237                                                        auto dtorType = new ast::PointerType( dtorFtype );
    12361238
    1237                                                         auto destructor = new ast::ObjectDecl(loc, memberDtorNamer.newName(), new ast::StructInstType( ast::dtorStruct ), new ast::ListInit(loc, { new ast::SingleInit(loc, thisExpr ), new ast::SingleInit(loc, new ast::CastExpr( dtorExpr, dtorType ) ) } ) );
    1238                                                         destructor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr( loc, ast::dtorStructDestroy ) } ) );
     1239                                                        auto destructor = new ast::ObjectDecl(loc, memberDtorNamer.newName(), new ast::StructInstType( global.dtorStruct ), new ast::ListInit(loc, { new ast::SingleInit(loc, thisExpr ), new ast::SingleInit(loc, new ast::CastExpr( dtorExpr, dtorType ) ) } ) );
     1240                                                        destructor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr( loc, global.dtorDestroy ) } ) );
    12391241                                                        mutStmts->push_front( new ast::DeclStmt(loc, destructor ) );
    12401242                                                        mutStmts->kids.splice( mutStmts->kids.begin(), stmtsToAdd );
     
    13311333        const ast::Expr * GenStructMemberCalls::postvisit( const ast::UntypedExpr * untypedExpr ) {
    13321334                // xxx - functions returning ast::ptr seems wrong...
    1333                 auto res = ResolvExpr::findVoidExpression( untypedExpr, symtab );
     1335                auto res = ResolvExpr::findVoidExpression( untypedExpr, { symtab, transUnit().global } );
    13341336                // Fix CodeLocation (at least until resolver is fixed).
    13351337                auto fix = localFillCodeLocations( untypedExpr->location, res.release() );
     
    13681370
    13691371                // resolve assignment and dispose of new env
    1370                 auto resolved = ResolvExpr::findVoidExpression( assign, symtab );
     1372                auto resolved = ResolvExpr::findVoidExpression( assign, { symtab, transUnit().global } );
    13711373                auto mut = resolved.get_and_mutate();
    13721374                assertf(resolved.get() == mut, "newly resolved expression must be unique");
Note: See TracChangeset for help on using the changeset viewer.