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