Changeset 39f84a4 for src/InitTweak
- Timestamp:
- Jul 29, 2016, 11:40:28 AM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, 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:
- 4d2434a
- Parents:
- 29e8bf5
- Location:
- src/InitTweak
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/InitTweak/GenInit.cc ¶
r29e8bf5 r39f84a4 225 225 std::list< Statement * > dtor; 226 226 227 SymTab::genImplicitCall( NULL, new VariableExpr( objDecl ), "?{}", back_inserter( ctor ), objDecl ); 228 SymTab::genImplicitCall( NULL, new VariableExpr( objDecl ), "^?{}", front_inserter( dtor ), objDecl, false ); 227 InitExpander srcParam( (Expression *)NULL ); 228 SymTab::genImplicitCall( srcParam, new VariableExpr( objDecl ), "?{}", back_inserter( ctor ), objDecl ); 229 SymTab::genImplicitCall( srcParam, new VariableExpr( objDecl ), "^?{}", front_inserter( dtor ), objDecl, false ); 229 230 230 231 // Currently genImplicitCall produces a single Statement - a CompoundStmt -
TabularUnified src/InitTweak/InitTweak.cc ¶
r29e8bf5 r39f84a4 20 20 }; 21 21 22 class InitExpander : public Visitor {22 class InitExpander_OLD : public Visitor { 23 23 public: 24 InitExpander() {}25 24 virtual void visit( SingleInit * singleInit ); 26 25 virtual void visit( ListInit * listInit ); … … 28 27 }; 29 28 30 void InitExpander ::visit( SingleInit * singleInit ) {29 void InitExpander_OLD::visit( SingleInit * singleInit ) { 31 30 argList.push_back( singleInit->get_value()->clone() ); 32 31 } 33 32 34 void InitExpander ::visit( ListInit * listInit ) {33 void InitExpander_OLD::visit( ListInit * listInit ) { 35 34 // xxx - for now, assume no nested list inits 36 35 std::list<Initializer*>::iterator it = listInit->begin_initializers(); … … 42 41 43 42 std::list< Expression * > makeInitList( Initializer * init ) { 44 InitExpander expander;43 InitExpander_OLD expander; 45 44 maybeAccept( init, expander ); 46 45 return expander.argList; … … 51 50 maybeAccept( init, finder ); 52 51 return finder.hasDesignations; 52 } 53 54 class InitExpander::ExpanderImpl { 55 public: 56 virtual std::list< Expression * > next( std::list< Expression * > & indices ) = 0; 57 }; 58 59 class InitImpl : public InitExpander::ExpanderImpl { 60 public: 61 InitImpl( Initializer * init ) { 62 if ( init ) inits.push_back( init ); 63 } 64 65 virtual std::list< Expression * > next( std::list< Expression * > & indices ) { 66 // this is wrong, but just a placeholder for now 67 return ! inits.empty() ? makeInitList( inits.front() ) : std::list< Expression * >(); 68 } 69 private: 70 std::list< Initializer * > inits; 71 }; 72 73 class ExprImpl : public InitExpander::ExpanderImpl { 74 public: 75 ExprImpl( Expression * expr ) : arg( expr ) {} 76 77 virtual std::list< Expression * > next( std::list< Expression * > & indices ) { 78 std::list< Expression * > ret; 79 Expression * expr = maybeClone( arg ); 80 if ( expr ) { 81 for ( std::list< Expression * >::reverse_iterator it = indices.rbegin(); it != indices.rend(); ++it ) { 82 // go through indices and layer on subscript exprs ?[?] 83 ++it; 84 UntypedExpr * subscriptExpr = new UntypedExpr( new NameExpr( "?[?]") ); 85 subscriptExpr->get_args().push_back( expr ); 86 subscriptExpr->get_args().push_back( (*it)->clone() ); 87 expr = subscriptExpr; 88 } 89 ret.push_back( expr ); 90 } 91 return ret; 92 } 93 private: 94 Expression * arg; 95 }; 96 97 InitExpander::InitExpander( Initializer * init ) : expander( new InitImpl( init ) ) {} 98 99 InitExpander::InitExpander( Expression * expr ) : expander( new ExprImpl( expr ) ) {} 100 101 std::list< Expression * > InitExpander::operator*() { 102 return cur; 103 } 104 105 InitExpander & InitExpander::operator++() { 106 cur = expander->next( indices ); 107 return *this; 108 } 109 110 // use array indices list to build switch statement 111 void InitExpander::addArrayIndex( Expression * index, Expression * dimension ) { 112 indices.push_back( index ); 113 indices.push_back( dimension ); 114 } 115 116 template< typename OutIterator > 117 void build( UntypedExpr * callExpr, InitExpander::IndexList::iterator idx, InitExpander::IndexList::iterator end, OutIterator out ) { 118 if ( idx == end ) return; 119 Expression * index = *idx++; 120 assert( idx != end ); 121 Expression * dimension = *idx++; 122 123 // if ( idx == end ) { 124 // // loop through list of expressions belonging to the current initializer 125 // UntypedExpr * cond = new UntypedExpr( new NameExpr( "?<?") ); 126 // cond->get_args().push_back( index->clone() ); 127 // cond->get_args().push_back( dimension->clone() ); 128 129 // UntypedExpr * call = callExpr->clone(); 130 // std::list< Expression * > args = *++expander; // xxx - need a way to indentify the end of an init list 131 // call->get_args().splice( args ); 132 133 // *out++ = new IfStmt( noLabels, cond, new ExprStmt( call ), NULL ); 134 135 // UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) ); 136 // increment->get_args().push_back( index->clone() ); 137 // *out++ = new ExprStmt( increment ); 138 // } else { 139 // std::list< Statement * > branches; 140 // for (...) { // loop over conditions? 141 // std::list< Statement * > stmts; 142 // build( idx, end, back_inserter( stmts ) ); 143 // CaseStmt * caseStmt = new CaseStmt( noLabels, condition, stmts ); 144 // branches.push_back( caseStmt ); 145 // } 146 // *out++ = new SwitchStmt( noLabels, index->clone(), branches ); 147 // } 148 } 149 150 // generate switch statement, consuming all of expander's elements 151 Statement * InitExpander::buildListInit( UntypedExpr * dst ) { 152 std::list< Statement * > results; 153 build( dst, indices.begin(), indices.end(), back_inserter( results ) ); 154 assert( results.size() <= 1 ); 155 return ! results.empty() ? results.front() : NULL; 53 156 } 54 157 -
TabularUnified src/InitTweak/InitTweak.h ¶
r29e8bf5 r39f84a4 61 61 bool isConstExpr( Expression * expr ); 62 62 bool isConstExpr( Initializer * init ); 63 64 class InitExpander { 65 public: 66 // expand by stepping through init to get each list of arguments 67 InitExpander( Initializer * init ); 68 69 // always expand to expr 70 InitExpander( Expression * expr ); 71 72 // iterator-like interface 73 std::list< Expression * > operator*(); 74 InitExpander & operator++(); 75 76 // builds statement which has the same semantics as a C-style list initializer 77 // (for array initializers) using callExpr as the base expression to perform initialization 78 Statement * buildListInit( UntypedExpr * callExpr ); 79 void addArrayIndex( Expression * index, Expression * dimension ); 80 81 class ExpanderImpl; 82 private: 83 std::shared_ptr< ExpanderImpl > expander; 84 std::list< Expression * > cur; 85 86 // invariant: list of size 2N (elements come in pairs [index, dimension]) 87 typedef std::list< Expression * > IndexList; 88 IndexList indices; 89 }; 63 90 } // namespace 64 91
Note: See TracChangeset
for help on using the changeset viewer.