Changeset 44f6341


Ignore:
Timestamp:
Sep 2, 2016, 1:30:36 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, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
85517ddb
Parents:
4d4882a
git-author:
Rob Schluntz <rschlunt@…> (09/01/16 17:14:32)
git-committer:
Rob Schluntz <rschlunt@…> (09/02/16 13:30:36)
Message:

fix implicit member ctor/dtor generation order, fix corresponding test

Location:
src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/Common/utility.h

    r4d4882a r44f6341  
    260260};
    261261
     262template< typename T >
     263struct ReverseIter {
     264        T& ref;
     265
     266        ReverseIter( T & ref ) : ref(ref) {}
     267
     268        typedef typename T::reverse_iterator iterator;
     269        iterator begin() { return ref.rbegin(); }
     270        iterator end() { return ref.rend(); }
     271};
     272
     273template< typename T >
     274ReverseIter< T > ReverseIterate( T & ref ) {
     275        return ReverseIter< T >( ref );
     276}
     277
    262278#endif // _UTILITY_H
    263279
  • src/InitTweak/FixInit.cc

    r4d4882a r44f6341  
    200200                        ObjectDecl * thisParam = 0;
    201201                        bool isCtor = false; // true if current function is a constructor
     202                        StructDecl * structDecl = 0;
    202203                };
    203204
     
    744745                        ValueGuard< ObjectDecl * > oldThisParam( thisParam );
    745746                        ValueGuard< bool > oldIsCtor( isCtor );
     747                        ValueGuard< StructDecl * > oldStructDecl( structDecl );
    746748
    747749                        // need to start with fresh sets
     
    758760                                StructInstType * structType = dynamic_cast< StructInstType * >( ptrType->get_base() );
    759761                                if ( structType ) {
    760                                         StructDecl * structDecl = structType->get_baseStruct();
     762                                        structDecl = structType->get_baseStruct();
    761763                                        for ( Declaration * member : structDecl->get_members() ) {
    762764                                                if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) {
     
    775777                        std::set_difference( usedUninit.begin(), usedUninit.end(), unhandled.begin(), unhandled.end(), std::inserter( diff, diff.begin() ) );
    776778                        for ( DeclarationWithType * member : diff ) {
    777                                 emit( "in ", CodeGen::genType( function->get_functionType(), function->get_name(), false ), ", member ", member->get_name(), " used before being constructed" );
     779                                emit( "in ", CodeGen::genType( function->get_functionType(), function->get_name(), false ), ", field ", member->get_name(), " used before being constructed" );
    778780                        }
    779781
     
    782784                                enterScope();
    783785                                // maybeAccept( function->get_functionType(), *this );
    784                                 for ( DeclarationWithType * member : unhandled ) {
    785                                         // insert and resolve default/copy constructor call for each member that's unhandled
     786
     787                                // need to iterate through members in reverse in order for
     788                                // ctor/dtor statements to come out in the right order
     789                                for ( Declaration * member : ReverseIterate( structDecl->get_members() ) ) {
     790                                        DeclarationWithType * field = dynamic_cast< DeclarationWithType * >( member );
     791                                        // skip non-DWT members
     792                                        if ( ! field ) continue;
     793                                        // skip handled members
     794                                        if ( ! unhandled.count( field ) ) continue;
     795
     796                                        // insert and resolve default/copy constructor call for each field that's unhandled
    786797                                        std::list< Statement * > stmt;
    787798                                        UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
     
    790801                                        Expression * arg2 = 0;
    791802                                        if ( isCopyConstructor( function ) ) {
    792                                                 // if copy ctor, need to pass second-param-of-this-function.member
     803                                                // if copy ctor, need to pass second-param-of-this-function.field
    793804                                                std::list< DeclarationWithType * > & params = function->get_functionType()->get_parameters();
    794805                                                assert( params.size() == 2 );
    795                                                 arg2 = new MemberExpr( member, new VariableExpr( params.back() ) );
     806                                                arg2 = new MemberExpr( field, new VariableExpr( params.back() ) );
    796807                                        }
    797808                                        InitExpander srcParam( arg2 );
    798                                         SymTab::genImplicitCall( srcParam, new MemberExpr( member, deref ), function->get_name(), back_inserter( stmt ), member, isCtor );
     809                                        SymTab::genImplicitCall( srcParam, new MemberExpr( field, deref ), function->get_name(), back_inserter( stmt ), field, isCtor );
    799810
    800811                                        assert( stmt.size() <= 1 );
     
    813824                                                        }
    814825                                                } catch ( SemanticError & error ) {
    815                                                         emit( "in ", CodeGen::genType( function->get_functionType(), function->get_name(), false ), ", member ", member->get_name(), " not explicitly ", isCtor ? "constructed" : "destructed",  " and no ", isCtor ? "default constructor" : "destructor", " found" );
     826                                                        emit( "in ", CodeGen::genType( function->get_functionType(), function->get_name(), false ), ", field ", field->get_name(), " not explicitly ", isCtor ? "constructed" : "destructed",  " and no ", isCtor ? "default constructor" : "destructor", " found" );
    816827                                                }
    817828                                        }
  • src/tests/.expect/memberCtors-ERR1.txt

    r4d4882a r44f6341  
    11CFA Version 1.0.0 (debug)
    2 Error: in void ?{}(struct B *b), member a2 used before being constructed
     2Error: in void ?{}(struct B *b), field a2 used before being constructed
    33make: *** [memberCtors-ERR1] Error 1
  • src/tests/.expect/memberCtors.txt

    r4d4882a r44f6341  
     1Before declaration of b1
    12constructing int
    23constructing int
     
    1617destructing int: 0
    1718destructing int: 1001
     19Before declaration of b2
    1820copy constructing int: 0
    1921copy constructing int: 0
     
    2527copy constructing int: 0
    2628copy constructing int: 0
     29End of main
    2730constructing int
    2831constructing int
     
    3740destructing int: 0
    3841destructing int: 0
    39 destructing int: 999
    4042destructing int: 0
    4143destructing int: 0
    4244destructing int: 0
     45destructing int: 999
    4346constructing int
    4447constructing int
     
    5356destructing int: 0
    5457destructing int: 0
    55 destructing int: 999
    5658destructing int: 0
    5759destructing int: 0
    5860destructing int: 0
     61destructing int: 999
  • src/tests/memberCtors.c

    r4d4882a r44f6341  
    5959
    6060int main() {
     61  printf("Before declaration of b1\n");
    6162  B b1;
     63  printf("Before declaration of b2\n");
    6264  B b2 = b1;
     65  printf("End of main\n");
    6366}
Note: See TracChangeset for help on using the changeset viewer.