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 | // |
---|
7 | // XXX.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : |
---|
12 | // Last Modified On : |
---|
13 | // Update Count : 0 |
---|
14 | // |
---|
15 | #include "SynTree/Constant.h" |
---|
16 | #include "InitModel.h" |
---|
17 | |
---|
18 | #include <cassert> |
---|
19 | #include <cstdlib> |
---|
20 | #include <algorithm> |
---|
21 | |
---|
22 | 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 | |
---|
223 | } // namespace InitTweak |
---|
224 | // Local Variables: // |
---|
225 | // tab-width: 4 // |
---|
226 | // mode: c++ // |
---|
227 | // compile-command: "make install" // |
---|
228 | // End: // |
---|