Changes in / [4ee36bf0:e35f30a]
- Location:
- src
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
src/InitTweak/FixInit.cc
r4ee36bf0 re35f30a 54 54 #include "SynTree/Type.h" // for Type, Type::StorageClasses 55 55 #include "SynTree/TypeSubstitution.h" // for TypeSubstitution, operator<< 56 #include "SynTree/VarExprReplacer.h" // for VarExprReplacer57 56 #include "SynTree/Visitor.h" // for acceptAll, maybeAccept 58 57 … … 159 158 using Parent::previsit; 160 159 160 void previsit( ObjectDecl * objDecl ); 161 161 void previsit( FunctionDecl * funcDecl ); 162 162 163 void previsit( CompoundStmt * compoundStmt ); 164 void postvisit( CompoundStmt * compoundStmt ); 165 void previsit( ReturnStmt * returnStmt ); 163 166 void previsit( BranchStmt * stmt ); 164 167 private: … … 200 203 static void generate( std::list< Declaration * > & translationUnit ); 201 204 202 void previsit( StructDecl * structDecl );203 204 205 void previsit( FunctionDecl * funcDecl ); 205 206 void postvisit( FunctionDecl * funcDecl ); … … 219 220 bool isCtor = false; // true if current function is a constructor 220 221 StructDecl * structDecl = nullptr; 221 222 // special built-in functions necessary for this to work223 StructDecl * dtorStruct = nullptr;224 FunctionDecl * dtorStructDestroy = nullptr;225 222 }; 226 223 … … 653 650 } 654 651 655 DeclarationWithType * getDtorFunc( ObjectDecl * objDecl, Statement * dtor, std::list< Statement * > & stmtsToAdd ) {656 if ( dynamic_cast< ExprStmt * >( dtor ) ) {657 if ( DeclarationWithType * func = getFunction( getCtorDtorCall( dtor ) ) ) {658 // cleanup argument must be a function, not an object (including function pointer)659 if ( FunctionDecl * dtorFunc = dynamic_cast< FunctionDecl * > ( func ) ) {660 if ( dtorFunc->type->forall.empty() ) {661 // simple case where the destructor is a monomorphic function call - can simply662 // use that function as the cleanup function.663 delete dtor;664 return func;665 }666 }667 }668 }669 670 // otherwise the cleanup is more complicated - need to build a single argument cleanup function that671 // wraps the more complicated code.672 static UniqueName dtorNamer( "__cleanup_dtor" );673 FunctionDecl * dtorFunc = FunctionDecl::newFunction( dtorNamer.newName(), SymTab::genDefaultType( objDecl->type->stripReferences(), false ), new CompoundStmt( noLabels ) );674 stmtsToAdd.push_back( new DeclStmt( noLabels, dtorFunc ) );675 676 // the original code contains uses of objDecl - replace them with the newly generated 'this' parameter.677 ObjectDecl * thisParam = getParamThis( dtorFunc->type );678 VarExprReplacer::replace( dtor, { std::make_pair( objDecl, thisParam ) } );679 dtorFunc->statements->push_back( dtor );680 681 return dtorFunc;682 }683 684 652 DeclarationWithType * FixInit::postmutate( ObjectDecl *objDecl ) { 685 653 // since this removes the init field from objDecl, it must occur after children are mutated (i.e. postmutate) … … 794 762 ctorInit->ctor = nullptr; 795 763 } 796 797 Statement * dtor = ctorInit->dtor;798 if ( dtor ) {799 ImplicitCtorDtorStmt * implicit = strict_dynamic_cast< ImplicitCtorDtorStmt * >( dtor );800 Statement * dtorStmt = implicit->callStmt;801 // don't need to call intrinsic dtor, because it does nothing, but802 // non-intrinsic dtors must be called803 if ( ! isIntrinsicSingleArgCallStmt( dtorStmt ) ) {804 // set dtor location to the object's location for error messages805 DeclarationWithType * dtorFunc = getDtorFunc( objDecl, dtorStmt, stmtsToAddBefore );806 objDecl->attributes.push_back( new Attribute( "cleanup", { new VariableExpr( dtorFunc ) } ) );807 // objDecl->attributes.push_back( new Attribute( "cleanup", { new NameExpr( dtorFunc->name ) } ) );808 ctorInit->dtor = nullptr;809 } // if810 }811 764 } // if 812 765 } else if ( Initializer * init = ctorInit->init ) { … … 851 804 852 805 806 template<typename Iterator, typename OutputIterator> 807 void insertDtors( Iterator begin, Iterator end, OutputIterator out ) { 808 for ( Iterator it = begin ; it != end ; ++it ) { 809 // extract destructor statement from the object decl and insert it into the output. Note that this is 810 // only called on lists of non-static objects with implicit non-intrinsic dtors, so if the user manually 811 // calls an intrinsic dtor then the call must (and will) still be generated since the argument may 812 // contain side effects. 813 ObjectDecl * objDecl = *it; 814 ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ); 815 assert( ctorInit && ctorInit->get_dtor() ); 816 *out++ = ctorInit->get_dtor()->clone(); 817 } // for 818 } 819 820 void InsertDtors::previsit( ObjectDecl * objDecl ) { 821 // remember non-static destructed objects so that their destructors can be inserted later 822 if ( ! objDecl->get_storageClasses().is_static ) { 823 if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) { 824 // a decision should have been made by the resolver, so ctor and init are not both non-NULL 825 assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() ); 826 Statement * dtor = ctorInit->get_dtor(); 827 // don't need to call intrinsic dtor, because it does nothing, but 828 // non-intrinsic dtors must be called 829 if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) { 830 // set dtor location to the object's location for error messages 831 ctorInit->dtor->location = objDecl->location; 832 reverseDeclOrder.front().push_front( objDecl ); 833 } // if 834 } // if 835 } // if 836 } 837 853 838 void InsertDtors::previsit( FunctionDecl * funcDecl ) { 854 839 // each function needs to have its own set of labels … … 863 848 } 864 849 850 void InsertDtors::previsit( CompoundStmt * compoundStmt ) { 851 // visit statements - this will also populate reverseDeclOrder list. don't want to dump all destructors 852 // when block is left, just the destructors associated with variables defined in this block, so push a new 853 // list to the top of the stack so that we can differentiate scopes 854 reverseDeclOrder.push_front( OrderedDecls() ); 855 Parent::previsit( compoundStmt ); 856 } 857 858 void InsertDtors::postvisit( CompoundStmt * compoundStmt ) { 859 // add destructors for the current scope that we're exiting, unless the last statement is a return, which 860 // causes unreachable code warnings 861 std::list< Statement * > & statements = compoundStmt->get_kids(); 862 if ( ! statements.empty() && ! dynamic_cast< ReturnStmt * >( statements.back() ) ) { 863 insertDtors( reverseDeclOrder.front().begin(), reverseDeclOrder.front().end(), back_inserter( statements ) ); 864 } 865 reverseDeclOrder.pop_front(); 866 } 867 868 void InsertDtors::previsit( ReturnStmt * ) { 869 // return exits all scopes, so dump destructors for all scopes 870 for ( OrderedDecls & od : reverseDeclOrder ) { 871 insertDtors( od.begin(), od.end(), back_inserter( stmtsToAddBefore ) ); 872 } // for 873 } 874 865 875 // Handle break/continue/goto in the same manner as C++. Basic idea: any objects that are in scope at the 866 876 // BranchStmt but not at the labelled (target) statement must be destructed. If there are any objects in scope … … 890 900 if ( ! diff.empty() ) { 891 901 throw SemanticError( std::string("jump to label '") + stmt->get_target().get_name() + "' crosses initialization of " + (*diff.begin())->get_name() + " ", stmt ); 902 } // if 903 // S_G-S_L results in set of objects that must be destructed 904 diff.clear(); 905 std::set_difference( curVars.begin(), curVars.end(), lvars.begin(), lvars.end(), std::inserter( diff, diff.end() ) ); 906 DTOR_PRINT( 907 std::cerr << "S_G-S_L = " << printSet( diff ) << std::endl; 908 ) 909 if ( ! diff.empty() ) { 910 // create an auxilliary set for fast lookup -- can't make diff a set, because diff ordering should be consistent for error messages. 911 std::unordered_set<ObjectDecl *> needsDestructor( diff.begin(), diff.end() ); 912 913 // go through decl ordered list of objectdecl. for each element that occurs in diff, output destructor 914 OrderedDecls ordered; 915 for ( OrderedDecls & rdo : reverseDeclOrder ) { 916 // add elements from reverseDeclOrder into ordered if they occur in diff - it is key that this happens in reverse declaration order. 917 copy_if( rdo.begin(), rdo.end(), back_inserter( ordered ), [&]( ObjectDecl * objDecl ) { return needsDestructor.count( objDecl ); } ); 918 } // for 919 insertDtors( ordered.begin(), ordered.end(), back_inserter( stmtsToAddBefore ) ); 892 920 } // if 893 921 } … … 928 956 } 929 957 930 void GenStructMemberCalls::previsit( StructDecl * structDecl ) {931 if ( ! dtorStruct && structDecl->name == "__Destructor" ) {932 dtorStruct = structDecl;933 }934 }935 936 958 void GenStructMemberCalls::previsit( FunctionDecl * funcDecl ) { 937 959 GuardValue( function ); … … 946 968 unhandled.clear(); 947 969 usedUninit.clear(); 948 949 if ( ! dtorStructDestroy && funcDecl->name == "__destroy_Destructor" ) {950 dtorStructDestroy = funcDecl;951 return;952 }953 970 954 971 function = funcDecl; … … 962 979 if ( structType ) { 963 980 structDecl = structType->get_baseStruct(); 964 if ( structDecl == dtorStruct ) return;965 981 for ( Declaration * member : structDecl->get_members() ) { 966 982 if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) { … … 1034 1050 callStmt->acceptMutator( resolver ); 1035 1051 if ( isCtor ) { 1036 function-> statements->push_front( callStmt );1052 function->get_statements()->push_front( callStmt ); 1037 1053 } else { 1038 1054 // destructor statements should be added at the end 1039 // function->get_statements()->push_back( callStmt ); 1040 1041 // Destructor _dtor0 = { &b.a1, _destroy_A }; 1042 std::list< Statement * > stmtsToAdd; 1043 1044 static UniqueName memberDtorNamer = { "__memberDtor" }; 1045 assertf( dtorStruct, "builtin __Destructor not found." ); 1046 assertf( dtorStructDestroy, "builtin __destroy_Destructor not found." ); 1047 1048 Expression * thisExpr = new AddressExpr( new VariableExpr( thisParam ) ); 1049 Expression * dtorExpr = new VariableExpr( getDtorFunc( thisParam, callStmt, stmtsToAdd ) ); 1050 1051 ObjectDecl * destructor = ObjectDecl::newObject( memberDtorNamer.newName(), new StructInstType( Type::Qualifiers(), dtorStruct ), new ListInit( { new SingleInit( thisExpr ), new SingleInit( dtorExpr ) } ) ); 1052 function->statements->push_front( new DeclStmt( noLabels, destructor ) ); 1053 destructor->attributes.push_back( new Attribute( "cleanup", { new VariableExpr( dtorStructDestroy ) } ) ); 1054 1055 function->statements->kids.splice( function->statements->kids.begin(), stmtsToAdd ); 1055 function->get_statements()->push_back( callStmt ); 1056 1056 } 1057 1057 } catch ( SemanticError & error ) { -
src/SymTab/Autogen.cc
r4ee36bf0 re35f30a 46 46 /// Data used to generate functions generically. Specifically, the name of the generated function and a function which generates the routine protoype 47 47 struct FuncData { 48 typedef FunctionType * (*TypeGen)( Type * , bool);48 typedef FunctionType * (*TypeGen)( Type * ); 49 49 FuncData( const std::string & fname, const TypeGen & genType ) : fname( fname ), genType( genType ) {} 50 50 std::string fname; … … 236 236 237 237 /// given type T, generate type of default ctor/dtor, i.e. function type void (*) (T *) 238 FunctionType * genDefaultType( Type * paramType, bool maybePolymorphic ) { 238 FunctionType * genDefaultType( Type * paramType ) { 239 const auto & typeParams = getGenericParams( paramType ); 239 240 FunctionType *ftype = new FunctionType( Type::Qualifiers(), false ); 240 if ( maybePolymorphic ) { 241 // only copy in 242 const auto & typeParams = getGenericParams( paramType ); 243 cloneAll( typeParams, ftype->forall ); 244 } 241 cloneAll( typeParams, ftype->forall ); 245 242 ObjectDecl *dstParam = new ObjectDecl( "_dst", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), paramType->clone() ), nullptr ); 246 243 ftype->parameters.push_back( dstParam ); … … 249 246 250 247 /// given type T, generate type of copy ctor, i.e. function type void (*) (T *, T) 251 FunctionType * genCopyType( Type * paramType , bool maybePolymorphic) {252 FunctionType *ftype = genDefaultType( paramType , maybePolymorphic);248 FunctionType * genCopyType( Type * paramType ) { 249 FunctionType *ftype = genDefaultType( paramType ); 253 250 ObjectDecl *srcParam = new ObjectDecl( "_src", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, paramType->clone(), nullptr ); 254 251 ftype->parameters.push_back( srcParam ); … … 257 254 258 255 /// given type T, generate type of assignment, i.e. function type T (*) (T *, T) 259 FunctionType * genAssignType( Type * paramType , bool maybePolymorphic) {260 FunctionType *ftype = genCopyType( paramType , maybePolymorphic);256 FunctionType * genAssignType( Type * paramType ) { 257 FunctionType *ftype = genCopyType( paramType ); 261 258 ObjectDecl *returnVal = new ObjectDecl( "_ret", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, paramType->clone(), nullptr ); 262 259 ftype->returnVals.push_back( returnVal ); … … 316 313 for ( const FuncData & d : data ) { 317 314 // generate a function (?{}, ?=?, ^?{}) based on the current FuncData. 318 FunctionType * ftype = d.genType( type , true);315 FunctionType * ftype = d.genType( type ); 319 316 320 317 // destructor for concurrent type must be mutex -
src/SymTab/Autogen.h
r4ee36bf0 re35f30a 45 45 extern FunctionDecl * dereferenceOperator; 46 46 47 /// generate the type of an assignment function for paramType. 48 /// maybePolymorphic is true if the resulting FunctionType is allowed to be polymorphic 49 FunctionType * genAssignType( Type * paramType, bool maybePolymorphic = true ); 50 51 /// generate the type of a default constructor or destructor for paramType. 52 /// maybePolymorphic is true if the resulting FunctionType is allowed to be polymorphic 53 FunctionType * genDefaultType( Type * paramType, bool maybePolymorphic = true ); 54 55 /// generate the type of a copy constructor for paramType. 56 /// maybePolymorphic is true if the resulting FunctionType is allowed to be polymorphic 57 FunctionType * genCopyType( Type * paramType, bool maybePolymorphic = true ); 47 // generate the type of an assignment function for paramType 48 FunctionType * genAssignType( Type * paramType ); 49 50 // generate the type of a default constructor or destructor for paramType 51 FunctionType * genDefaultType( Type * paramType ); 52 53 // generate the type of a copy constructor for paramType 54 FunctionType * genCopyType( Type * paramType ); 58 55 59 56 /// inserts into out a generated call expression to function fname with arguments dstParam and srcParam. Intended to be used with generated ?=?, ?{}, and ^?{} calls. -
src/SynTree/Mutator.h
r4ee36bf0 re35f30a 119 119 120 120 virtual TypeSubstitution * mutate( TypeSubstitution * sub ); 121 122 121 private: 123 122 virtual Declaration * handleAggregateDecl(AggregateDecl * aggregateDecl ); -
src/prelude/builtins.c
r4ee36bf0 re35f30a 91 91 static inline unsigned int ?\=?( unsigned int & x, unsigned long int y ) { x = x \ y; return x; } 92 92 93 // type that wraps a pointer and a destructor-like function - used in generating implicit destructor calls for struct members in user-defined functions94 forall(dtype T)95 struct __Destructor {96 T * object;97 void (*dtor)(T *);98 };99 100 // defined destructor in the case that non-generated code wants to use __Destructor101 forall(dtype T)102 static inline void ^?{}(__Destructor(T) & x) {103 x.dtor(x.object);104 }105 106 // easy interface into __Destructor's destructor for easy codegen purposes107 extern "C" {108 forall(dtype T)109 static inline void __destroy_Destructor(__Destructor(T) * dtor) {110 ^(*dtor){};111 }112 }113 114 93 // Local Variables: // 115 94 // mode: c // -
src/tests/.expect/memberCtors-ERR1.txt
r4ee36bf0 re35f30a 1 memberCtors.c:7 8:1 error: in void ?{}(B &b), field a2 used before being constructed1 memberCtors.c:71:1 error: in void ?{}(B &b), field a2 used before being constructed -
src/tests/.expect/memberCtors.txt
r4ee36bf0 re35f30a 98 98 end copy construct A 99 99 End of main 100 begin destruct B101 100 constructing int 102 101 constructing int … … 147 146 destructing int: 0 148 147 destructing int: 1000 149 end destruct B 150 destructing int: 0 151 destructing int: 0 152 destructing int: 999 153 destructing int: 0 154 destructing int: 0 155 destructing int: 0 156 destructing int: 0 157 destructing int: 0 158 destructing int: 999 159 begin destruct B 148 destructing int: 0 149 destructing int: 0 150 destructing int: 999 151 destructing int: 0 152 destructing int: 0 153 destructing int: 0 154 destructing int: 0 155 destructing int: 0 156 destructing int: 999 160 157 constructing int 161 158 constructing int … … 206 203 destructing int: 0 207 204 destructing int: 1000 208 end destruct B 209 destructing int: 0 210 destructing int: 0 211 destructing int: 999 212 destructing int: 0 213 destructing int: 0 214 destructing int: 0 215 destructing int: 0 216 destructing int: 0 217 destructing int: 999 205 destructing int: 0 206 destructing int: 0 207 destructing int: 999 208 destructing int: 0 209 destructing int: 0 210 destructing int: 0 211 destructing int: 0 212 destructing int: 0 213 destructing int: 999 -
src/tests/memberCtors.c
r4ee36bf0 re35f30a 22 22 } 23 23 24 /* WrappedInt */void ?=?(WrappedInt & this, int x) {24 void ?=?(WrappedInt & this, int x) { 25 25 printf("assigning int: %d %d\n", this.x, x); 26 26 this.x = x; 27 // return this;28 27 } 29 30 // WrappedInt ?=?(WrappedInt & this, WrappedInt other) {31 // printf("assigning int: %d %d\n", this.x, other.x);32 // this.x = other.x;33 // return this;34 // }35 28 36 29 struct A { … … 86 79 87 80 void ^?{}(B & b) { 88 printf("begin destruct B\n");89 81 b.a2 = (A) { 0 }; 90 82 ^(b.a1){}; 91 printf("end destruct B\n");92 83 } // a2, a3 never destructed - will be automatically destructed 93 84 94 85 int main() { 95 86 printf("Before declaration of b1\n"); 96 B b1; // b1 = { { 1000, 0, 0 }, { 1001, 0, 0 }, { 0, 0, 0 } }87 B b1; 97 88 printf("Before declaration of b2\n"); 98 89 B b2 = b1; -
src/tests/multiDimension.c
r4ee36bf0 re35f30a 32 32 } 33 33 34 //X global[10][10] = {35 //{ 1, { 2 }, { 3 }, { 4 }, 5, 6, 7, 8, 9, 10, 11, 12 },36 //{ 1, 2, 3, 4 },37 //{ { 1234567 } }38 //};34 X global[10][10] = { 35 { 1, { 2 }, { 3 }, { 4 }, 5, 6, 7, 8, 9, 10, 11, 12 }, 36 { 1, 2, 3, 4 }, 37 { { 1234567 } } 38 }; 39 39 40 //X global2[3][3][3] = {41 //{42 //{ 1, 2, 3 },43 //{ 4, 5, 6 },44 //{ 7, 8, 9 },45 //{ 10, 11, 12 }46 //},47 //{48 //{ 0, 0, 0 }49 //}50 //};40 X global2[3][3][3] = { 41 { 42 { 1, 2, 3 }, 43 { 4, 5, 6 }, 44 { 7, 8, 9 }, 45 { 10, 11, 12 } 46 }, 47 { 48 { 0, 0, 0 } 49 } 50 }; 51 51 52 //int foo() {53 //static X abc[3][3] = {54 //{ 11, 22, 33, 44 },55 //{ 55, 66 },56 //{ 77 },57 //{ 88, 99, 1010 }58 //};59 //}52 int foo() { 53 static X abc[3][3] = { 54 { 11, 22, 33, 44 }, 55 { 55, 66 }, 56 { 77 }, 57 { 88, 99, 1010 } 58 }; 59 } 60 60 61 61 // ensure constructed const arrays continue to compile 62 //const int global[1] = { -2 };62 const int global[1] = { -2 }; 63 63 64 64 int main() { 65 X a; 66 X abc[2]; 67 // X abc[4]/*[4]*/ = { 68 // /*{*/ 999, 1111 /*}*/, 69 // // { 1, 2, 3, 4, 5 }, 70 // // {}, 71 // // { 0 }, 72 // // { 88 } 73 // }; 65 X abc[4][4] = { 66 { 999, 1111 }, 67 { 1, 2, 3, 4, 5 }, 68 {}, 69 { 0 }, 70 { 88 } 71 }; 74 72 75 //foo();76 //foo();73 foo(); 74 foo(); 77 75 }
Note: See TracChangeset
for help on using the changeset viewer.