Changeset a08ba92 for translator/InitTweak/InitModel.cc
- 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/InitModel.cc
r01aeade ra08ba92 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // InitModel.cc -- 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:37:08 2015 13 // Update Count : 1 14 // 15 15 16 #include "SynTree/Constant.h" 16 17 #include "InitModel.h" … … 21 22 22 23 namespace InitTweak { 23 24 InitModelBuilder::InitModelBuilder( Declaration *_decl ) 25 : taken( false ), decl( 0 ), building(0) { 26 27 ObjectDecl *_odecl = dynamic_cast< ObjectDecl * >( _decl ); 28 assert( _odecl != 0 ); 29 Type *objectType = _odecl->get_type(); 30 31 /* this to be replaced by dynamic dispatch */ 32 if ( dynamic_cast< BasicType * >(objectType) != 0 ) { 33 if ( building == 0 ) building = new SingleName; 34 } else if ( ReferenceToType *rt = dynamic_cast< ReferenceToType * >(objectType) ) { 35 rt->accept( *this ); 36 } else if ( ArrayType *at = dynamic_cast< ArrayType * >(objectType) ) { 37 at->accept( *this ); 38 } else // if (tuples) 39 std::cerr << "Got something else" << std::endl; 40 41 if ( decl != 0 ) init(); 42 } 43 44 InitModelBuilder::~InitModelBuilder() { if ( ! taken ) { delete building; building = 0; } } 45 46 void InitModelBuilder::init() { 47 assert( decl != 0 ); 48 decl->accept( *this ); 49 } 50 51 // Visitor interface 52 void InitModelBuilder::visit( ArrayType *at ) { 53 if ( building == 0 ) building = new RangeAssociation(interpretDimension( at->get_dimension() )); 54 decl = 0; 55 return; 56 } 57 58 void InitModelBuilder::visit( StructInstType *st ) { 59 if ( building == 0 ) building = new PointAssociation; 60 decl = st->get_baseStruct(); 61 return; 62 } 63 64 void InitModelBuilder::visit( UnionInstType *ut ) { 65 decl = ut->get_baseUnion(); 66 return; 67 } 68 void InitModelBuilder::visit( EnumInstType * ) {} 69 70 void InitModelBuilder::visit( StructDecl *aggregateDecl) { 71 PointAssociation *pa = dynamic_cast< PointAssociation * >( building ); 72 assert( pa != 0 ); 73 std::list< Declaration * > mem = aggregateDecl->get_members(); 74 75 for ( std::list<Declaration *>::iterator i = mem.begin(); i != mem.end(); i++ ) { 76 pa->add_member( (*i)->get_name() ); 77 InitModelBuilder rec(*i); 78 pa->set_member( (*i)->get_name(), rec.grab_assoc() ); 79 } 80 81 return; 82 } 83 84 void InitModelBuilder::visit( UnionDecl *) {} 85 void InitModelBuilder::visit( EnumDecl *) {} 86 87 // InitModelBuilder::ConstantFolder 88 void InitModelBuilder::ConstantFolder::visit( ConstantExpr *cnst ) { 89 Constant *c = cnst->get_constant(); 90 assert (c != 0); 91 if ( BasicType *bt = dynamic_cast<BasicType *>( c->get_type() ) ) { 92 if ( bt->isInteger() ) { 93 // need more intelligence here, not necessarily base 10 94 value = std::strtol( c->get_value().c_str(), NULL, 10 ); 95 return; 96 } else 97 std::cerr << "Basic type but not integer" << std::endl; 98 } 99 throw 0; 100 } 101 102 // InitModelFiller 103 InitModelFiller::InitModelFiller( Association *_model, Initializer *_init, bool _topLevel ) 104 : model( _model ), orgInit( _init ), topLevel( _topLevel ), next( 0 ) { 105 //next = model.begin(); 106 if ( orgInit != 0 ) init(); 107 } 108 109 void InitModelFiller::init() { 110 assert( model != 0 ); // change it into a reference 111 assert( orgInit != 0 ); 112 orgInit->accept( *this ); 113 } 114 115 void InitModelFiller::visit( SingleInit *singleInit ) { 116 std::list< Expression *> &des = singleInit->get_designators(); 117 118 if ( topLevel ) { 119 assert ( des.empty() ); 120 assert ( dynamic_cast< SingleName * >(model) != 0 ); 121 try { 122 model->add_single( next++, singleInit->get_value() ); 123 } catch (...) { 124 std::cerr << "Illegal initialization" << std::endl; 125 } 126 return; 127 } 128 129 if ( des.empty() ) { 130 assert( model != 0 ); 131 try { 132 model->add_single( next++, singleInit->get_value() ); 133 } catch ( AssocException &e ) { 134 throw SemanticError( "Illegal initialization: " + e.get_what() ); 135 } catch ( ... ) { 136 std::cerr << "Shouldn't be here" << std::endl; 137 } 138 return; 139 } 140 141 // not general enough (does not contend well with designated arrays) 142 std::list<std::string> desnames; 143 std::transform( des.begin(), des.end(), back_inserter(desnames), Initializer::designator_name ); 144 145 for ( std::list<std::string>::iterator i = desnames.begin(); i != desnames.end(); i++ ) { 146 try { 147 next = model->add_single( *i, singleInit->get_value() ); 148 next++; 149 } catch ( AssocException &e ) { 150 throw SemanticError( "Illegal initialization: " + e.get_what() ); 151 } catch ( ... ) { 152 std::cerr << "Shouldn't happen, check association" << std::endl; 153 } 154 } 155 156 return; 157 } 158 159 void InitModelFiller::visit( ListInit *listInit ) { 160 assert( listInit != 0 ); 161 162 // designators 163 std::list< Expression *> &des = listInit->get_designators(); 164 std::list< Initializer *> &ini = listInit->get_initializers(); 165 166 if (! des.empty() ) { 167 if (topLevel) 168 throw SemanticError( "Invalid initializer: designated at top level." ); 169 170 std::list<Expression *> des2; 171 std::copy (des.begin(), des.end(), back_inserter( des2 )); 172 std::list< Expression * > empty; 173 listInit->set_designators( empty ); 174 for ( std::list<Expression *>::iterator i = des2.begin(); i != des2.end(); i++ ) { 175 Association * newModel = 0; 176 if ( NameExpr *n = dynamic_cast< NameExpr * >( *i ) ) 177 try { 178 newModel = (*model)[ n->get_name() ]; 179 } catch( AssocException &e ) { 180 std::cerr << "Didn't find member: " << e.get_what() << std::endl; 181 } 182 else // if ( RangeExpr *r = dynamic_cast< RangeExpr * >( *i ) ) 183 std::cerr << "Invalid designator specification" << std::endl; 184 185 InitModelFiller rec( newModel, listInit, true ); 186 } 187 188 } else 189 if (topLevel) { 190 topLevel = false; 191 for ( std::list<Initializer*>::iterator i = ini.begin(); i != ini.end(); i++ ) 192 (*i)->accept(*this); 193 } else 194 // next available uninitialized member 195 InitModelFiller rec( (*model)[next++], listInit, true ); 196 } 197 198 void InitUnspooler::visit( SingleName *single ) { 199 assert(init == 0 && single != 0); 200 std::list< Expression * > empty; 201 init = new SingleInit( single->get_expr(), empty ); 202 return; 203 } 204 205 void InitUnspooler::visit( PointAssociation *pa ) { 206 assert( pa != 0 ); 207 208 std::list< Initializer * > contents; 209 for ( int i = 0; i < pa->size(); i++ ) 210 if ( (*pa)[i] != 0 ) { 211 InitUnspooler rec; 212 (*pa)[i]->accept( rec ); 213 assert( rec.get_initializer() != 0 ); 214 contents.push_back( rec.grab_initializer() ); 215 } 216 217 init = new ListInit( contents ); 218 return; 219 } 220 221 222 24 InitModelBuilder::InitModelBuilder( Declaration *_decl ) 25 : taken( false ), decl( 0 ), building(0) { 26 27 ObjectDecl *_odecl = dynamic_cast< ObjectDecl * >( _decl ); 28 assert( _odecl != 0 ); 29 Type *objectType = _odecl->get_type(); 30 31 /* this to be replaced by dynamic dispatch */ 32 if ( dynamic_cast< BasicType * >(objectType) != 0 ) { 33 if ( building == 0 ) building = new SingleName; 34 } else if ( ReferenceToType *rt = dynamic_cast< ReferenceToType * >(objectType) ) { 35 rt->accept( *this ); 36 } else if ( ArrayType *at = dynamic_cast< ArrayType * >(objectType) ) { 37 at->accept( *this ); 38 } else // if (tuples) 39 std::cerr << "Got something else" << std::endl; 40 41 if ( decl != 0 ) init(); 42 } 43 44 InitModelBuilder::~InitModelBuilder() { if ( ! taken ) { delete building; building = 0; } } 45 46 void InitModelBuilder::init() { 47 assert( decl != 0 ); 48 decl->accept( *this ); 49 } 50 51 // Visitor interface 52 void InitModelBuilder::visit( ArrayType *at ) { 53 if ( building == 0 ) building = new RangeAssociation(interpretDimension( at->get_dimension() )); 54 decl = 0; 55 return; 56 } 57 58 void InitModelBuilder::visit( StructInstType *st ) { 59 if ( building == 0 ) building = new PointAssociation; 60 decl = st->get_baseStruct(); 61 return; 62 } 63 64 void InitModelBuilder::visit( UnionInstType *ut ) { 65 decl = ut->get_baseUnion(); 66 return; 67 } 68 void InitModelBuilder::visit( EnumInstType * ) {} 69 70 void InitModelBuilder::visit( StructDecl *aggregateDecl) { 71 PointAssociation *pa = dynamic_cast< PointAssociation * >( building ); 72 assert( pa != 0 ); 73 std::list< Declaration * > mem = aggregateDecl->get_members(); 74 75 for ( std::list<Declaration *>::iterator i = mem.begin(); i != mem.end(); i++ ) { 76 pa->add_member( (*i)->get_name() ); 77 InitModelBuilder rec(*i); 78 pa->set_member( (*i)->get_name(), rec.grab_assoc() ); 79 } // for 80 81 return; 82 } 83 84 void InitModelBuilder::visit( UnionDecl *) {} 85 void InitModelBuilder::visit( EnumDecl *) {} 86 87 // InitModelBuilder::ConstantFolder 88 void InitModelBuilder::ConstantFolder::visit( ConstantExpr *cnst ) { 89 Constant *c = cnst->get_constant(); 90 assert (c != 0); 91 if ( BasicType *bt = dynamic_cast<BasicType *>( c->get_type() ) ) { 92 if ( bt->isInteger() ) { 93 // need more intelligence here, not necessarily base 10 94 value = std::strtol( c->get_value().c_str(), NULL, 10 ); 95 return; 96 } else 97 std::cerr << "Basic type but not integer" << std::endl; 98 } // if 99 throw 0; 100 } 101 102 // InitModelFiller 103 InitModelFiller::InitModelFiller( Association *_model, Initializer *_init, bool _topLevel ) 104 : model( _model ), orgInit( _init ), topLevel( _topLevel ), next( 0 ) { 105 //next = model.begin(); 106 if ( orgInit != 0 ) init(); 107 } 108 109 void InitModelFiller::init() { 110 assert( model != 0 ); // change it into a reference 111 assert( orgInit != 0 ); 112 orgInit->accept( *this ); 113 } 114 115 void InitModelFiller::visit( SingleInit *singleInit ) { 116 std::list< Expression *> &des = singleInit->get_designators(); 117 118 if ( topLevel ) { 119 assert ( des.empty() ); 120 assert ( dynamic_cast< SingleName * >(model) != 0 ); 121 try { 122 model->add_single( next++, singleInit->get_value() ); 123 } catch (...) { 124 std::cerr << "Illegal initialization" << std::endl; 125 } 126 return; 127 } // if 128 129 if ( des.empty() ) { 130 assert( model != 0 ); 131 try { 132 model->add_single( next++, singleInit->get_value() ); 133 } catch ( AssocException &e ) { 134 throw SemanticError( "Illegal initialization: " + e.get_what() ); 135 } catch ( ... ) { 136 std::cerr << "Shouldn't be here" << std::endl; 137 } // try 138 return; 139 } // if 140 141 // not general enough (does not contend well with designated arrays) 142 std::list<std::string> desnames; 143 std::transform( des.begin(), des.end(), back_inserter(desnames), Initializer::designator_name ); 144 145 for ( std::list<std::string>::iterator i = desnames.begin(); i != desnames.end(); i++ ) { 146 try { 147 next = model->add_single( *i, singleInit->get_value() ); 148 next++; 149 } catch ( AssocException &e ) { 150 throw SemanticError( "Illegal initialization: " + e.get_what() ); 151 } catch ( ... ) { 152 std::cerr << "Shouldn't happen, check association" << std::endl; 153 } // try 154 } // for 155 156 return; 157 } 158 159 void InitModelFiller::visit( ListInit *listInit ) { 160 assert( listInit != 0 ); 161 162 // designators 163 std::list< Expression *> &des = listInit->get_designators(); 164 std::list< Initializer *> &ini = listInit->get_initializers(); 165 166 if ( ! des.empty() ) { 167 if (topLevel) 168 throw SemanticError( "Invalid initializer: designated at top level." ); 169 170 std::list<Expression *> des2; 171 std::copy (des.begin(), des.end(), back_inserter( des2 )); 172 std::list< Expression * > empty; 173 listInit->set_designators( empty ); 174 for ( std::list<Expression *>::iterator i = des2.begin(); i != des2.end(); i++ ) { 175 Association * newModel = 0; 176 if ( NameExpr *n = dynamic_cast< NameExpr * >( *i ) ) 177 try { 178 newModel = (*model)[ n->get_name() ]; 179 } catch( AssocException &e ) { 180 std::cerr << "Didn't find member: " << e.get_what() << std::endl; 181 } 182 else // if ( RangeExpr *r = dynamic_cast< RangeExpr * >( *i ) ) 183 std::cerr << "Invalid designator specification" << std::endl; 184 185 InitModelFiller rec( newModel, listInit, true ); 186 } // for 187 } else 188 if (topLevel) { 189 topLevel = false; 190 for ( std::list<Initializer*>::iterator i = ini.begin(); i != ini.end(); i++ ) 191 (*i)->accept(*this); 192 } else 193 // next available uninitialized member 194 InitModelFiller rec( (*model)[next++], listInit, true ); 195 } 196 197 void InitUnspooler::visit( SingleName *single ) { 198 assert(init == 0 && single != 0); 199 std::list< Expression * > empty; 200 init = new SingleInit( single->get_expr(), empty ); 201 return; 202 } 203 204 void InitUnspooler::visit( PointAssociation *pa ) { 205 assert( pa != 0 ); 206 207 std::list< Initializer * > contents; 208 for ( int i = 0; i < pa->size(); i++ ) 209 if ( (*pa)[i] != 0 ) { 210 InitUnspooler rec; 211 (*pa)[i]->accept( rec ); 212 assert( rec.get_initializer() != 0 ); 213 contents.push_back( rec.grab_initializer() ); 214 } // if 215 216 init = new ListInit( contents ); 217 return; 218 } 223 219 } // namespace InitTweak 220 224 221 // Local Variables: // 225 222 // tab-width: 4 //
Note: See TracChangeset
for help on using the changeset viewer.