Changeset f1e012b for src/InitTweak


Ignore:
Timestamp:
Jan 19, 2016, 1:28:25 PM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
ca1c11f
Parents:
71f4e4f
Message:

added intrinsic ctor/dtors to prelude, modified MakeLibCfa? to build prelude ctor/dtors, added ctor/dtor to polymorphic object type constraints, rudimentary fallback on initializer nodes if chosen ctor is intrinsic, remove intrinsic destructor statements to reduce output pollution

Location:
src/InitTweak
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/FixInit.cc

    r71f4e4f rf1e012b  
    1010// Created On       : Wed Jan 13 16:29:30 2016
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Wed Jan 13 16:45:37 2016
    13 // Update Count     : 17
     12// Last Modified On : Tue Jan 19 13:25:13 2016
     13// Update Count     : 27
    1414//
    1515
     
    3535
    3636                virtual ObjectDecl * mutate( ObjectDecl *objDecl );
     37
     38                virtual CompoundStmt * mutate( CompoundStmt * compoundStmt );
    3739        };
    3840
     
    5052        ObjectDecl *FixInit::mutate( ObjectDecl *objDecl ) {
    5153                if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) {
    52                         // a decision should have been made by the resolver, so either ctor is NULL or init is NULL
     54                        // a decision should have been made by the resolver, so ctor and init are not both non-NULL
    5355                        assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() );
    5456                        if ( Expression * ctor = ctorInit->get_ctor() ) {
     
    6163                                ctorInit->set_init( NULL );
    6264                        } else {
    63                                 // one of them should be non-NULL
    64                                 assert( ctorInit->get_ctor() || ctorInit->get_init() );
     65                                // no constructor and no initializer, which is okay
     66                                objDecl->set_init( NULL );
    6567                        }
    6668                        delete ctorInit;
     
    6870                return objDecl;
    6971        }
     72
     73        CompoundStmt * FixInit::mutate( CompoundStmt * compoundStmt ) {
     74                std::list< Statement * > & statements = compoundStmt->get_kids();
     75                for ( std::list< Statement * >::iterator it = statements.begin(); it != statements.end(); ++it ) {
     76                        // remove if instrinsic destructor statement
     77                        // xxx - test user manually calling intrinsic functions - what happens?
     78                        if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( *it ) ) {
     79                                if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( exprStmt->get_expr() ) ) {
     80                                        if ( VariableExpr * function = dynamic_cast< VariableExpr * >( appExpr->get_function() ) ) {
     81                                                if ( function->get_var()->get_name() == "^?{}" && function->get_var()->get_linkage() == LinkageSpec::Intrinsic ) {
     82                                                        statements.erase(it++);
     83                                                }
     84                                        }
     85                                }
     86                        }
     87                }
     88                // mutate non-destructor statements
     89                return Mutator::mutate( compoundStmt );
     90        }
     91
    7092} // namespace InitTweak
    7193
  • src/InitTweak/RemoveInit.cc

    r71f4e4f rf1e012b  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Wed Jan 13 15:19:35 2016
    13 // Update Count     : 154
     12// Last Modified On : Tue Jan 19 11:12:49 2016
     13// Update Count     : 165
    1414//
    1515
     
    5959                static void generateCtorDtor( std::list< Declaration * > &translationUnit );
    6060
     61                CtorDtor() : inFunction( false ) {}
     62
    6163                virtual ObjectDecl * mutate( ObjectDecl * );
    6264                virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
     
    7173
    7274          protected:
     75                bool inFunction;
    7376
    7477                // to be added before block ends - use push_front so order is correct
     
    9396                if (objDecl->get_init() && dynamic_cast<TypeInstType*>(objDecl->get_type())) {
    9497                        if (SingleInit * single = dynamic_cast<SingleInit*>(objDecl->get_init())) {
    95                                 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
     98                                // xxx this can be more complicated - consider ListInit
     99                                UntypedExpr *assign = new UntypedExpr( new NameExpr( "?{}" ) );
    96100                                assign->get_args().push_back( new AddressExpr (new NameExpr( objDecl->get_name() ) ) );
    97101                                assign->get_args().push_back( single->get_value()->clone() );
     
    107111                // hands off if the function returns an lvalue - we don't want to allocate a temporary if a variable's address
    108112                // is being returned
     113                // xxx - this should construct rather than assign
    109114                if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue()  ) {
    110115                        ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), 0 );
     
    186191                // hands off if designated or if @=
    187192                if ( tryConstruct( objDecl ) ) {
    188                         Expression * ctor = makeCtorDtorExpr( "?{}", objDecl, makeInitList( objDecl->get_init() ) );
    189                         Expression * dtor = makeCtorDtorExpr( "^?{}", objDecl, std::list< Expression * >() );
    190 
    191                         // need to remember init expression, in case no ctors exist
    192                         // if ctor does exist, want to use ctor expression instead of init
    193                         // push this decision to the resolver
    194                         objDecl->set_init( new ConstructorInit( ctor, objDecl->get_init() ) );
    195                         destructorStmts.push_front( new ExprStmt( noLabels, dtor ) );
     193                        if ( inFunction ) {
     194                                Expression * ctor = makeCtorDtorExpr( "?{}", objDecl, makeInitList( objDecl->get_init() ) );
     195                                Expression * dtor = makeCtorDtorExpr( "^?{}", objDecl, std::list< Expression * >() );
     196
     197                                // need to remember init expression, in case no ctors exist
     198                                // if ctor does exist, want to use ctor expression instead of init
     199                                // push this decision to the resolver
     200                                objDecl->set_init( new ConstructorInit( ctor, objDecl->get_init() ) );
     201                                destructorStmts.push_front( new ExprStmt( noLabels, dtor ) );
     202                        } else {
     203                                // xxx - find a way to construct/destruct globals
     204                                // hack: implicit "static" initialization routine for each struct type? or something similar?
     205                                // --ties into module system
     206                        }
    196207                }
    197208                return objDecl;
     
    200211        DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) {
    201212                // parameters should not be constructed and destructed, so don't mutate FunctionType
     213                bool oldInFunc = inFunction;
    202214                mutateAll( functionDecl->get_oldDecls(), *this );
     215                inFunction = true;
    203216                functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
     217                inFunction = oldInFunc;
    204218                return functionDecl;
    205219        }
Note: See TracChangeset for help on using the changeset viewer.