Changeset a08ba92 for translator/InitTweak/BasicInit.h
- Timestamp:
- May 19, 2015, 4:58:14 PM (9 years ago)
- 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, string, with_gc
- Children:
- 843054c2
- Parents:
- 01aeade
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
translator/InitTweak/BasicInit.h
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc--7 // BasicInit.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 0 14 // 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 16:32:21 2015 13 // Update Count : 3 14 // 15 15 16 #ifndef _BASINIT_H_ 16 17 #define _BASINIT_H_ … … 28 29 29 30 namespace InitTweak { 30 31 bool isDeclStmtP(Statement *stmt); 32 33 class BreakInitializer; 34 class BreakDesignator; 35 36 class BasicInit: public Mutator 37 { 38 public: 39 BasicInit() : bindings( 0 ) {} 40 BasicInit( SymTab::Indexer &_index ) : bindings( 0 ), index( _index ) {} 41 BasicInit( const BasicInit &other ) { 42 bindings = other.get_bindings(); 43 index = other.index; 44 } 45 46 ~BasicInit() { /* delete bindings; bindings = 0; */ } 47 48 NameAssociation< Expression *, BreakInitializer > *get_bindings() const { return bindings; } 49 void set_bindings( NameAssociation< Expression *, BreakInitializer > *newValue ) { 50 bindings = newValue; 51 } 52 53 bool has_bindings() { 54 return ( get_bindings() != 0 || ! stmts.empty() ); 55 } 56 57 virtual ObjectDecl *mutate( ObjectDecl *objectDecl ) 58 { index.visit( objectDecl ); return objectDecl; } 59 virtual TypeDecl *mutate( TypeDecl *typeDecl ) 60 { index.visit( typeDecl ); return typeDecl; } 61 virtual TypedefDecl *mutate( TypedefDecl *typeDecl ) 62 { index.visit( typeDecl ); return typeDecl; } 63 virtual StructDecl *mutate( StructDecl *aggregateDecl ) 64 { index.visit( aggregateDecl ); return aggregateDecl; } 65 virtual UnionDecl *mutate( UnionDecl *aggregateDecl ) 66 { index.visit( aggregateDecl ); return aggregateDecl; } 67 virtual EnumDecl *mutate( EnumDecl *aggregateDecl ) 68 { index.visit( aggregateDecl ); return aggregateDecl; } 69 70 virtual Type *mutate( StructInstType *aggrInst ) 71 { index.visit( aggrInst ); return aggrInst; } 72 virtual Type *mutate( UnionInstType *aggrInst ) 73 { index.visit( aggrInst ); return aggrInst; } 74 75 virtual CompoundStmt *mutate(CompoundStmt *compoundStmt); 76 virtual Statement *mutate(DeclStmt *declStmt); 77 78 std::list< Statement *> get_statements() const { return stmts; } 79 80 static void build_statements( NameAssociation< Expression *, BreakInitializer > *assoc, 81 std::string aggName, std::list< Statement *> &stmts ); 82 private: 83 NameAssociation< Expression *, BreakInitializer > *bindings; 84 Statement *assignFromDecl( DeclStmt *declStmt ); 85 SymTab::Indexer index; 86 std::list< Statement *> stmts; 87 88 class Classify { 89 public: 90 enum TypeKind { NULL_T, SINGLE_T, COMPOUND_T }; 91 enum InitKind { NULL_I, SINGLE_I, COMPOUND_I }; 92 93 static TypeKind type( Type * ); 94 static InitKind initializer( Initializer *); 95 96 static NameInCollection *declaration( ObjectDecl *objdecl, SymTab::Indexer *index ); 97 static std::list< Statement * > 98 matchInit( NameInCollection *, ObjectDecl *, Initializer * ); 99 static Statement *constructAssgn( std::string membname, ObjectDecl *toInit, SingleInit *sinit ); 100 101 // static std::list< Statement * > constructListAssgn( NameAssociation<Expression *, BreakDesignator > assoc ); 102 }; 103 }; 104 105 class BreakInitializer { 106 enum InitKind { EMPTY, SINGLE, COMPOUND }; 107 108 class BreakDesignator; 109 typedef BreakDesignator NameSplitter; 110 111 public: 112 typedef std::list<Initializer *>::iterator element_iterator; 113 typedef std::list< NameSplitter >::iterator name_iterator; 114 115 BreakInitializer ( Initializer *_init ) : kind( EMPTY ), sinit(0), cinit(0) { 116 std::list<Expression *> temp; 117 118 if ( ( sinit=dynamic_cast< SingleInit * >(_init) ) != 0 ) { 119 kind = SINGLE; 120 temp = sinit->get_designators(); 121 } else if ( ( cinit=dynamic_cast< ListInit * >(_init) ) != 0 ) { 122 kind = COMPOUND; 123 temp = cinit->get_designators(); 124 } 125 126 std::transform( temp.begin(), temp.end(), std::back_inserter( designators ), 127 ctor_noptr<NameSplitter, Expression *> ); 128 129 } 130 131 //BreakInitializer( const BreakInitializer &other ) { this.col = other.col; } 132 ~BreakInitializer () {} 133 134 BreakInitializer set_name( NameSplitter &name ) { 135 designators.clear(); 136 designators.push_back( name ); 137 138 return *this; 139 } 140 141 element_iterator element_begin() { 142 assert( cinit != 0 ); 143 return cinit->begin_initializers(); 144 } 145 element_iterator element_end() { 146 assert( cinit != 0 ); 147 return cinit->end_initializers(); 148 } 149 150 name_iterator names_begin() { return designators.begin(); } 151 name_iterator names_end() { return designators.end(); } 152 153 int names_size() const { return designators.size(); } 154 155 bool has_index() const { return ! designators.empty(); } 156 bool is_single() const { return kind == SINGLE; } 157 bool is_composite() const { return kind == COMPOUND; } 158 159 Expression *get_value() { 160 switch ( kind ) { 161 case EMPTY: 162 return 0; 163 break; 164 case SINGLE: 165 return sinit->get_value(); 166 break; 167 case COMPOUND: 168 assert(false); 169 break; 170 default: 171 assert(false); 172 } 173 return 0; 174 } 175 176 // attributes 177 private: 178 InitKind kind; 179 SingleInit *sinit; 180 ListInit *cinit; 181 std::list< BreakDesignator > designators; 182 183 // helper classes 184 public: 185 class BreakDesignator { 186 public: 187 BreakDesignator( Expression *exp ) { 188 Expression *prfx = exp; 189 UntypedMemberExpr *me = 0; 190 191 do { 192 if ( (me=dynamic_cast< UntypedMemberExpr * >( prfx )) == 0 ) break; 193 blown_struct.push_front( me->get_member() ); 194 prfx = me->get_aggregate(); 195 } while ( prfx != 0 ); 196 197 NameExpr *ne; 198 if ( (ne=dynamic_cast< NameExpr * >( prfx )) != 0 ) 199 blown_struct.push_front( ne->get_name() ); 200 } 201 202 BreakDesignator( std::string name ) { 203 blown_struct.push_front( name ); 204 } 205 206 bool is_flat() const { return blown_struct.size() == 1; } 207 bool is_nested() const { return blown_struct.size() > 1; } 208 209 std::string get_name() { return blown_struct.front(); } 210 211 BreakDesignator &name_remainder() { 212 blown_struct.pop_front(); 213 return *this; 214 } 215 216 private: 217 std::list< std::string > blown_struct; 218 }; 219 220 }; 221 31 bool isDeclStmtP(Statement *stmt); 32 33 class BreakInitializer; 34 class BreakDesignator; 35 36 class BasicInit: public Mutator { 37 public: 38 BasicInit() : bindings( 0 ) {} 39 BasicInit( SymTab::Indexer &_index ) : bindings( 0 ), index( _index ) {} 40 BasicInit( const BasicInit &other ) { 41 bindings = other.get_bindings(); 42 index = other.index; 43 } 44 45 ~BasicInit() { /* delete bindings; bindings = 0; */ } 46 47 NameAssociation< Expression *, BreakInitializer > *get_bindings() const { return bindings; } 48 void set_bindings( NameAssociation< Expression *, BreakInitializer > *newValue ) { 49 bindings = newValue; 50 } 51 52 bool has_bindings() { 53 return ( get_bindings() != 0 || ! stmts.empty() ); 54 } 55 56 virtual ObjectDecl *mutate( ObjectDecl *objectDecl ) 57 { index.visit( objectDecl ); return objectDecl; } 58 virtual TypeDecl *mutate( TypeDecl *typeDecl ) 59 { index.visit( typeDecl ); return typeDecl; } 60 virtual TypedefDecl *mutate( TypedefDecl *typeDecl ) 61 { index.visit( typeDecl ); return typeDecl; } 62 virtual StructDecl *mutate( StructDecl *aggregateDecl ) 63 { index.visit( aggregateDecl ); return aggregateDecl; } 64 virtual UnionDecl *mutate( UnionDecl *aggregateDecl ) 65 { index.visit( aggregateDecl ); return aggregateDecl; } 66 virtual EnumDecl *mutate( EnumDecl *aggregateDecl ) 67 { index.visit( aggregateDecl ); return aggregateDecl; } 68 69 virtual Type *mutate( StructInstType *aggrInst ) 70 { index.visit( aggrInst ); return aggrInst; } 71 virtual Type *mutate( UnionInstType *aggrInst ) 72 { index.visit( aggrInst ); return aggrInst; } 73 74 virtual CompoundStmt *mutate(CompoundStmt *compoundStmt); 75 virtual Statement *mutate(DeclStmt *declStmt); 76 77 std::list< Statement *> get_statements() const { return stmts; } 78 79 static void build_statements( NameAssociation< Expression *, BreakInitializer > *assoc, std::string aggName, std::list< Statement *> &stmts ); 80 private: 81 NameAssociation< Expression *, BreakInitializer > *bindings; 82 Statement *assignFromDecl( DeclStmt *declStmt ); 83 SymTab::Indexer index; 84 std::list< Statement *> stmts; 85 86 class Classify { 87 public: 88 enum TypeKind { NULL_T, SINGLE_T, COMPOUND_T }; 89 enum InitKind { NULL_I, SINGLE_I, COMPOUND_I }; 90 91 static TypeKind type( Type * ); 92 static InitKind initializer( Initializer *); 93 94 static NameInCollection *declaration( ObjectDecl *objdecl, SymTab::Indexer *index ); 95 static std::list< Statement * > 96 matchInit( NameInCollection *, ObjectDecl *, Initializer * ); 97 static Statement *constructAssgn( std::string membname, ObjectDecl *toInit, SingleInit *sinit ); 98 99 // static std::list< Statement * > constructListAssgn( NameAssociation<Expression *, BreakDesignator > assoc ); 100 }; 101 }; 102 103 class BreakInitializer { 104 enum InitKind { EMPTY, SINGLE, COMPOUND }; 105 106 class BreakDesignator; 107 typedef BreakDesignator NameSplitter; 108 109 public: 110 typedef std::list<Initializer *>::iterator element_iterator; 111 typedef std::list< NameSplitter >::iterator name_iterator; 112 113 BreakInitializer ( Initializer *_init ) : kind( EMPTY ), sinit(0), cinit(0) { 114 std::list<Expression *> temp; 115 116 if ( ( sinit=dynamic_cast< SingleInit * >(_init) ) != 0 ) { 117 kind = SINGLE; 118 temp = sinit->get_designators(); 119 } else if ( ( cinit=dynamic_cast< ListInit * >(_init) ) != 0 ) { 120 kind = COMPOUND; 121 temp = cinit->get_designators(); 122 } // if 123 124 std::transform( temp.begin(), temp.end(), std::back_inserter( designators ), ctor_noptr<NameSplitter, Expression *> ); 125 } 126 127 //BreakInitializer( const BreakInitializer &other ) { this.col = other.col; } 128 ~BreakInitializer () {} 129 130 BreakInitializer set_name( NameSplitter &name ) { 131 designators.clear(); 132 designators.push_back( name ); 133 134 return *this; 135 } 136 137 element_iterator element_begin() { 138 assert( cinit != 0 ); 139 return cinit->begin_initializers(); 140 } 141 element_iterator element_end() { 142 assert( cinit != 0 ); 143 return cinit->end_initializers(); 144 } 145 146 name_iterator names_begin() { return designators.begin(); } 147 name_iterator names_end() { return designators.end(); } 148 149 int names_size() const { return designators.size(); } 150 151 bool has_index() const { return ! designators.empty(); } 152 bool is_single() const { return kind == SINGLE; } 153 bool is_composite() const { return kind == COMPOUND; } 154 155 Expression *get_value() { 156 switch ( kind ) { 157 case EMPTY: 158 return 0; 159 break; 160 case SINGLE: 161 return sinit->get_value(); 162 break; 163 case COMPOUND: 164 assert(false); 165 break; 166 default: 167 assert(false); 168 } // switch 169 return 0; 170 } 171 // attributes 172 private: 173 InitKind kind; 174 SingleInit *sinit; 175 ListInit *cinit; 176 std::list< BreakDesignator > designators; 177 // helper classes 178 public: 179 class BreakDesignator { 180 public: 181 BreakDesignator( Expression *exp ) { 182 Expression *prfx = exp; 183 UntypedMemberExpr *me = 0; 184 185 do { 186 if ( (me=dynamic_cast< UntypedMemberExpr * >( prfx )) == 0 ) break; 187 blown_struct.push_front( me->get_member() ); 188 prfx = me->get_aggregate(); 189 } while ( prfx != 0 ); 190 191 NameExpr *ne; 192 if ( (ne=dynamic_cast< NameExpr * >( prfx )) != 0 ) 193 blown_struct.push_front( ne->get_name() ); 194 } 195 196 BreakDesignator( std::string name ) { 197 blown_struct.push_front( name ); 198 } 199 200 bool is_flat() const { return blown_struct.size() == 1; } 201 bool is_nested() const { return blown_struct.size() > 1; } 202 203 std::string get_name() { return blown_struct.front(); } 204 205 BreakDesignator &name_remainder() { 206 blown_struct.pop_front(); 207 return *this; 208 } 209 210 private: 211 std::list< std::string > blown_struct; 212 }; 213 }; 222 214 } // namespace InitTweak 223 215 224 #endif // #ifndef _BASINIT_H_ 225 226 /* 227 Local Variables: 228 mode: c++ 229 End: 230 */ 216 #endif // _BASINIT_H_ 217 231 218 // Local Variables: // 232 219 // tab-width: 4 //
Note: See TracChangeset
for help on using the changeset viewer.