Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/GenInit.cc

    r18ca28e rac74057  
    8585                // should not have a ConstructorInit generated.
    8686
    87                 ManagedTypes managedTypes;
     87                bool isManaged( ObjectDecl * objDecl ) const ; // determine if object is managed
     88                bool isManaged( Type * type ) const; // determine if type is managed
     89                void handleDWT( DeclarationWithType * dwt ); // add type to managed if ctor/dtor
     90                GenPoly::ScopedSet< std::string > managedTypes;
    8891                bool inFunction = false;
    8992        };
     
    126129                // hands off if the function returns a reference - we don't want to allocate a temporary if a variable's address
    127130                // is being returned
    128                 if ( returnStmt->expr && returnVals.size() == 1 && isConstructable( returnVals.front()->get_type() ) ) {
     131                if ( returnStmt->get_expr() && returnVals.size() == 1 && isConstructable( returnVals.front()->get_type() ) ) {
    129132                        // explicitly construct the return value using the return expression and the retVal object
    130                         assertf( returnVals.front()->name != "", "Function %s has unnamed return value\n", funcName.c_str() );
    131 
    132                         ObjectDecl * retVal = strict_dynamic_cast< ObjectDecl * >( returnVals.front() );
    133                         if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( returnStmt->expr ) ) {
    134                                 // return statement has already been mutated - don't need to do it again
    135                                 if ( varExpr->var == retVal ) return;
    136                         }
    137                         stmtsToAddBefore.push_back( genCtorDtor( "?{}", retVal, returnStmt->get_expr() ) );
     133                        assertf( returnVals.front()->get_name() != "", "Function %s has unnamed return value\n", funcName.c_str() );
     134
     135                        stmtsToAddBefore.push_back( genCtorDtor( "?{}", dynamic_cast< ObjectDecl *>( returnVals.front() ), returnStmt->get_expr() ) );
    138136
    139137                        // return the retVal object
    140                         returnStmt->expr = new VariableExpr( returnVals.front() );
     138                        returnStmt->set_expr( new VariableExpr( returnVals.front() ) );
    141139                } // if
    142140        }
     
    201199        }
    202200
    203         bool ManagedTypes::isManaged( Type * type ) const {
     201        bool CtorDtor::isManaged( Type * type ) const {
    204202                // references are never constructed
    205203                if ( dynamic_cast< ReferenceType * >( type ) ) return false;
     
    217215        }
    218216
    219         bool ManagedTypes::isManaged( ObjectDecl * objDecl ) const {
     217        bool CtorDtor::isManaged( ObjectDecl * objDecl ) const {
    220218                Type * type = objDecl->get_type();
    221219                while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
     
    225223        }
    226224
    227         void ManagedTypes::handleDWT( DeclarationWithType * dwt ) {
     225        void CtorDtor::handleDWT( DeclarationWithType * dwt ) {
    228226                // if this function is a user-defined constructor or destructor, mark down the type as "managed"
    229227                if ( ! LinkageSpec::isOverridable( dwt->get_linkage() ) && CodeGen::isCtorDtor( dwt->get_name() ) ) {
     
    235233                }
    236234        }
    237 
    238         void ManagedTypes::handleStruct( StructDecl * aggregateDecl ) {
    239                 // don't construct members, but need to take note if there is a managed member,
    240                 // because that means that this type is also managed
    241                 for ( Declaration * member : aggregateDecl->get_members() ) {
    242                         if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) {
    243                                 if ( isManaged( field ) ) {
    244                                         StructInstType inst( Type::Qualifiers(), aggregateDecl );
    245                                         managedTypes.insert( SymTab::Mangler::mangle( &inst ) );
    246                                         break;
    247                                 }
    248                         }
    249                 }
    250         }
    251 
    252         void ManagedTypes::beginScope() { managedTypes.beginScope(); }
    253         void ManagedTypes::endScope() { managedTypes.endScope(); }
    254235
    255236        ImplicitCtorDtorStmt * genCtorDtor( const std::string & fname, ObjectDecl * objDecl, Expression * arg ) {
     
    296277
    297278        void CtorDtor::previsit( ObjectDecl * objDecl ) {
    298                 managedTypes.handleDWT( objDecl );
     279                handleDWT( objDecl );
    299280                // hands off if @=, extern, builtin, etc.
    300281                // even if unmanaged, try to construct global or static if initializer is not constexpr, since this is not legal C
    301                 if ( tryConstruct( objDecl ) && ( managedTypes.isManaged( objDecl ) || ((! inFunction || objDecl->get_storageClasses().is_static ) && ! isConstExpr( objDecl->get_init() ) ) ) ) {
     282                if ( tryConstruct( objDecl ) && ( isManaged( objDecl ) || ((! inFunction || objDecl->get_storageClasses().is_static ) && ! isConstExpr( objDecl->get_init() ) ) ) ) {
    302283                        // constructed objects cannot be designated
    303284                        if ( isDesignated( objDecl->get_init() ) ) throw SemanticError( "Cannot include designations in the initializer for a managed Object. If this is really what you want, then initialize with @=.\n", objDecl );
     
    314295                inFunction = true;
    315296
    316                 managedTypes.handleDWT( functionDecl );
     297                handleDWT( functionDecl );
    317298
    318299                GuardScope( managedTypes );
     
    320301                for ( auto & tyDecl : functionDecl->get_functionType()->get_forall() ) {
    321302                        for ( DeclarationWithType *& assertion : tyDecl->get_assertions() ) {
    322                                 managedTypes.handleDWT( assertion );
     303                                handleDWT( assertion );
    323304                        }
    324305                }
     
    330311                visit_children = false; // do not try to construct and destruct aggregate members
    331312
    332                 managedTypes.handleStruct( aggregateDecl );
     313                // don't construct members, but need to take note if there is a managed member,
     314                // because that means that this type is also managed
     315                for ( Declaration * member : aggregateDecl->get_members() ) {
     316                        if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) {
     317                                if ( isManaged( field ) ) {
     318                                        StructInstType inst( Type::Qualifiers(), aggregateDecl );
     319                                        managedTypes.insert( SymTab::Mangler::mangle( &inst ) );
     320                                        break;
     321                                }
     322                        }
     323                }
    333324        }
    334325
Note: See TracChangeset for help on using the changeset viewer.