Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/FixInitNew.cpp

    r39d8950 rce36b55  
    1616#include "CodeGen/GenType.h"           // for genPrettyType
    1717#include "CodeGen/OperatorTable.h"
    18 #include "Common/CodeLocationTools.hpp"
    1918#include "Common/PassVisitor.h"        // for PassVisitor, WithStmtsToAdd
    2019#include "Common/SemanticError.h"      // for SemanticError
     
    8685        /// generate/resolve copy construction expressions for each, and generate/resolve destructors for both
    8786        /// 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>, public ast::WithConstTranslationUnit {
     87        struct ResolveCopyCtors final : public ast::WithGuards, public ast::WithStmtsToAdd<>, public ast::WithSymbolTable, public ast::WithShortCircuiting, public ast::WithVisitorRef<ResolveCopyCtors> {
    8988                const ast::Expr * postvisit( const ast::ImplicitCopyCtorExpr * impCpCtorExpr );
    9089                const ast::StmtExpr * previsit( const ast::StmtExpr * stmtExpr );
     
    190189        /// for any member that is missing a corresponding ctor/dtor call.
    191190        /// 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>, public ast::WithConstTranslationUnit {
     191        struct GenStructMemberCalls final : public ast::WithGuards, public ast::WithShortCircuiting, public ast::WithSymbolTable, public ast::WithVisitorRef<GenStructMemberCalls> {
    193192                void previsit( const ast::FunctionDecl * funcDecl );
    194193                const ast::DeclWithType * postvisit( const ast::FunctionDecl * funcDecl );
     
    215214
    216215        /// 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, public ast::WithConstTranslationUnit {
     216        struct FixCtorExprs final : public ast::WithDeclsToAdd<>, public ast::WithSymbolTable, public ast::WithShortCircuiting {
    218217                const ast::Expr * postvisit( const ast::ConstructorExpr * ctorExpr );
    219218        };
     
    510509                // (VariableExpr and already resolved expression)
    511510                CP_CTOR_PRINT( std::cerr << "ResolvingCtorDtor " << untyped << std::endl; )
    512                 ast::ptr<ast::Expr> resolved = ResolvExpr::findVoidExpression(untyped, { symtab, transUnit().global } );
     511                ast::ptr<ast::Expr> resolved = ResolvExpr::findVoidExpression(untyped, symtab);
    513512                assert( resolved );
    514513                if ( resolved->env ) {
     
    554553                ast::ptr<ast::Expr> guard = mutArg;
    555554
    556                 ast::ptr<ast::ObjectDecl> tmp = new ast::ObjectDecl(loc, "__tmp", mutResult, nullptr );
     555                ast::ptr<ast::ObjectDecl> tmp = new ast::ObjectDecl({}, "__tmp", mutResult, nullptr );
    557556
    558557                // create and resolve copy constructor
     
    588587
    589588        ast::Expr * ResolveCopyCtors::destructRet( const ast::ObjectDecl * ret, const ast::Expr * arg ) {
    590                 auto global = transUnit().global;
    591589                // TODO: refactor code for generating cleanup attribute, since it's common and reused in ~3-4 places
    592590                // check for existing cleanup attribute before adding another(?)
    593591                // need to add __Destructor for _tmp_cp variables as well
    594592
    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." );
     593                assertf( ast::dtorStruct, "Destructor generation requires __Destructor definition." );
     594                assertf( ast::dtorStruct->members.size() == 2, "__Destructor definition does not have expected fields." );
     595                assertf( ast::dtorStructDestroy, "Destructor generation requires __destroy_Destructor." );
    598596
    599597                const CodeLocation loc = ret->location;
     
    612610                auto dtorFunc = getDtorFunc( ret, new ast::ExprStmt(loc, dtor ), stmtsToAddBefore );
    613611
    614                 auto dtorStructType = new ast::StructInstType( global.dtorStruct );
     612                auto dtorStructType = new ast::StructInstType(ast::dtorStruct);
    615613
    616614                // what does this do???
     
    624622                static UniqueName namer( "_ret_dtor" );
    625623                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 ) ) } ) );
    626                 retDtor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr(loc, global.dtorDestroy ) } ) );
     624                retDtor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr(loc, ast::dtorStructDestroy ) } ) );
    627625                stmtsToAddBefore.push_back( new ast::DeclStmt(loc, retDtor ) );
    628626
    629627                if ( arg ) {
    630                         auto member = new ast::MemberExpr(loc, global.dtorStruct->members.front().strict_as<ast::DeclWithType>(), new ast::VariableExpr(loc, retDtor ) );
     628                        auto member = new ast::MemberExpr(loc, ast::dtorStruct->members.front().strict_as<ast::DeclWithType>(), new ast::VariableExpr(loc, retDtor ) );
    631629                        auto object = new ast::CastExpr( new ast::AddressExpr( new ast::VariableExpr(loc, ret ) ), new ast::PointerType(new ast::VoidType() ) );
    632630                        ast::Expr * assign = createBitwiseAssignment( member, object );
     
    801799        // to prevent warnings ('_unq0' may be used uninitialized in this function),
    802800        // insert an appropriate zero initializer for UniqueExpr temporaries.
    803         ast::Init * makeInit( const ast::Type * t, CodeLocation const & loc ) {
     801        ast::Init * makeInit( const ast::Type * t ) {
    804802                if ( auto inst = dynamic_cast< const ast::StructInstType * >( t ) ) {
    805803                        // initizer for empty struct must be empty
    806                         if ( inst->base->members.empty() ) {
    807                                 return new ast::ListInit( loc, {} );
    808                         }
     804                        if ( inst->base->members.empty() ) return new ast::ListInit({}, {});
    809805                } else if ( auto inst = dynamic_cast< const ast::UnionInstType * >( t ) ) {
    810806                        // initizer for empty union must be empty
    811                         if ( inst->base->members.empty() ) {
    812                                 return new ast::ListInit( loc, {} );
    813                         }
    814                 }
    815 
    816                 return new ast::ListInit( loc, {
    817                         new ast::SingleInit( loc, ast::ConstantExpr::from_int( loc, 0 ) )
    818                 } );
     807                        if ( inst->base->members.empty() ) return new ast::ListInit({}, {});
     808                }
     809
     810                return new ast::ListInit( {}, { new ast::SingleInit( {}, ast::ConstantExpr::from_int({}, 0) ) } );
    819811        }
    820812
     
    840832                        } else {
    841833                                // expr isn't a call expr, so create a new temporary variable to use to hold the value of the unique expression
    842                                 mutExpr->object = new ast::ObjectDecl( mutExpr->location, toString("_unq", mutExpr->id), mutExpr->result, makeInit( mutExpr->result, mutExpr->location ) );
     834                                mutExpr->object = new ast::ObjectDecl( mutExpr->location, toString("_unq", mutExpr->id), mutExpr->result, makeInit( mutExpr->result ) );
    843835                                mutExpr->var = new ast::VariableExpr( mutExpr->location, mutExpr->object );
    844836                        }
     
    11801172                        auto guard = makeFuncGuard( [this]() { symtab.enterScope(); }, [this]() { symtab.leaveScope(); } );
    11811173                        symtab.addFunction( function );
    1182                         auto global = transUnit().global;
    11831174
    11841175                        // need to iterate through members in reverse in order for
     
    12261217
    12271218                                                        static UniqueName memberDtorNamer = { "__memberDtor" };
    1228                                                         assertf( global.dtorStruct, "builtin __Destructor not found." );
    1229                                                         assertf( global.dtorDestroy, "builtin __destroy_Destructor not found." );
     1219                                                        assertf( ast::dtorStruct, "builtin __Destructor not found." );
     1220                                                        assertf( ast::dtorStructDestroy, "builtin __destroy_Destructor not found." );
    12301221
    12311222                                                        ast::Expr * thisExpr = new ast::CastExpr( new ast::AddressExpr( new ast::VariableExpr(loc, thisParam ) ), new ast::PointerType( new ast::VoidType(), ast::CV::Qualifiers() ) );
     
    12371228                                                        auto dtorType = new ast::PointerType( dtorFtype );
    12381229
    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 ) } ) );
     1230                                                        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 ) ) } ) );
     1231                                                        destructor->attributes.push_back( new ast::Attribute( "cleanup", { new ast::VariableExpr({}, ast::dtorStructDestroy ) } ) );
    12411232                                                        mutStmts->push_front( new ast::DeclStmt(loc, destructor ) );
    12421233                                                        mutStmts->kids.splice( mutStmts->kids.begin(), stmtsToAdd );
     
    13321323
    13331324        const ast::Expr * GenStructMemberCalls::postvisit( const ast::UntypedExpr * untypedExpr ) {
     1325                // Expression * newExpr = untypedExpr;
    13341326                // xxx - functions returning ast::ptr seems wrong...
    1335                 auto res = ResolvExpr::findVoidExpression( untypedExpr, { symtab, transUnit().global } );
    1336                 // Fix CodeLocation (at least until resolver is fixed).
    1337                 auto fix = localFillCodeLocations( untypedExpr->location, res.release() );
    1338                 return strict_dynamic_cast<const ast::Expr *>( fix );
     1327                auto res = ResolvExpr::findVoidExpression( untypedExpr, symtab );
     1328                return res.release();
     1329                // return newExpr;
    13391330        }
    13401331
     
    13701361
    13711362                // resolve assignment and dispose of new env
    1372                 auto resolved = ResolvExpr::findVoidExpression( assign, { symtab, transUnit().global } );
     1363                auto resolved = ResolvExpr::findVoidExpression( assign, symtab );
    13731364                auto mut = resolved.get_and_mutate();
    13741365                assertf(resolved.get() == mut, "newly resolved expression must be unique");
Note: See TracChangeset for help on using the changeset viewer.