[51587aa] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[974906e2] | 7 | // InitModel.cc -- |
---|
[51587aa] | 8 | // |
---|
[843054c2] | 9 | // Author : Rodolfo G. Esteves |
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[974906e2] | 11 | // Last Modified By : Rob Schluntz |
---|
| 12 | // Last Modified On : Thu Jan 07 13:38:46 2016 |
---|
| 13 | // Update Count : 5 |
---|
[51587aa] | 14 | // |
---|
[a08ba92] | 15 | |
---|
[51b7345] | 16 | #include "SynTree/Constant.h" |
---|
| 17 | #include "InitModel.h" |
---|
| 18 | |
---|
| 19 | #include <cassert> |
---|
| 20 | #include <cstdlib> |
---|
| 21 | #include <algorithm> |
---|
| 22 | |
---|
| 23 | namespace InitTweak { |
---|
[a08ba92] | 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; |
---|
[974906e2] | 200 | init = new SingleInit( single->get_expr(), empty, false ); // cannot be constructed |
---|
[a08ba92] | 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 | |
---|
[974906e2] | 216 | std::list< Expression * > desig; |
---|
| 217 | init = new ListInit( contents, desig, false ); // cannot be constructed |
---|
[a08ba92] | 218 | return; |
---|
| 219 | } |
---|
[51b7345] | 220 | } // namespace InitTweak |
---|
[a08ba92] | 221 | |
---|
[51587aa] | 222 | // Local Variables: // |
---|
| 223 | // tab-width: 4 // |
---|
| 224 | // mode: c++ // |
---|
| 225 | // compile-command: "make install" // |
---|
| 226 | // End: // |
---|