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 | // BasicInit.cc -- |
---|
8 | // |
---|
9 | // Author : Rodolfo G. Esteves |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Tue May 19 16:30:43 2015 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #include <list> |
---|
17 | #include <cassert> |
---|
18 | #include <iostream> |
---|
19 | #include <iterator> |
---|
20 | #include <algorithm> |
---|
21 | |
---|
22 | #include "Common/utility.h" |
---|
23 | |
---|
24 | #include "SynTree/Type.h" |
---|
25 | #include "SynTree/Statement.h" |
---|
26 | #include "SynTree/Expression.h" |
---|
27 | #include "SynTree/Declaration.h" |
---|
28 | #include "SynTree/Initializer.h" |
---|
29 | |
---|
30 | #include "BasicInit.h" |
---|
31 | #include "NameCollector.h" |
---|
32 | #include "NameAssociation.h" |
---|
33 | |
---|
34 | namespace InitTweak { |
---|
35 | CompoundStmt* BasicInit::mutate(CompoundStmt *compoundStmt) { |
---|
36 | index.visit( compoundStmt ); |
---|
37 | |
---|
38 | std::list< Statement * > &kids = compoundStmt->get_kids(); |
---|
39 | std::list< Statement * > newKids; |
---|
40 | |
---|
41 | for ( std::list< Statement * >::iterator i = kids.begin(); i!= kids.end(); i++ ) { |
---|
42 | //BasicInit newMut( ); |
---|
43 | (*i)->acceptMutator( *this ); |
---|
44 | newKids.push_back( *i ); |
---|
45 | |
---|
46 | if ( has_bindings() ) { // if ( get_bindings() != 0 ) { |
---|
47 | std::list< Statement *> newSt = get_statements(); |
---|
48 | //newSt.push_back( *i ); |
---|
49 | newKids.splice( newKids.end(), newSt ); |
---|
50 | bindings = 0; |
---|
51 | stmts.clear(); |
---|
52 | } // if |
---|
53 | } // for |
---|
54 | |
---|
55 | compoundStmt->get_kids() = newKids; |
---|
56 | return compoundStmt; |
---|
57 | } |
---|
58 | |
---|
59 | Statement * BasicInit::mutate(DeclStmt *declStmt) { |
---|
60 | declStmt->accept( index ); |
---|
61 | |
---|
62 | ObjectDecl *odecl = 0; |
---|
63 | |
---|
64 | if ( ( odecl = dynamic_cast<ObjectDecl *>(declStmt->get_decl()) ) != 0 ) { |
---|
65 | Initializer *init = odecl->get_init(); |
---|
66 | if ( init == 0 ) return declStmt; |
---|
67 | |
---|
68 | if ( Classify::type( odecl->get_type() ) == Classify::COMPOUND_T ) |
---|
69 | if ( Classify::initializer(init) == Classify::SINGLE_I ) |
---|
70 | throw( 0 ); // mismatch of type and initializer |
---|
71 | else { |
---|
72 | NameInCollection *col = Classify::declaration( odecl, &index ); |
---|
73 | bindings = NameAssociation< Expression *, BreakInitializer >::createNameAssoc(col); |
---|
74 | bindings->add_value( std::string(""), BreakInitializer(init) ); |
---|
75 | BasicInit::build_statements( bindings, odecl->get_name(), stmts ); |
---|
76 | } // if |
---|
77 | else |
---|
78 | if ( Classify::initializer(init) == Classify::COMPOUND_I ) |
---|
79 | throw( 0 ); // mismatch of type and initializer |
---|
80 | else { |
---|
81 | // Single inits |
---|
82 | SingleInit *sinit = dynamic_cast< SingleInit * > ( init ); |
---|
83 | assert( sinit != 0); |
---|
84 | |
---|
85 | std::list<Expression *> args; |
---|
86 | args.push_back( new AddressExpr( new NameExpr( odecl->get_name() )) ); // have to get address of object |
---|
87 | args.push_back( sinit->get_value() ); |
---|
88 | // replace declaration with initialization |
---|
89 | stmts.push_back(new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args))); |
---|
90 | } // if |
---|
91 | |
---|
92 | delete init; |
---|
93 | odecl->set_init( 0 ); |
---|
94 | } else { |
---|
95 | // no declaration statement |
---|
96 | } // if |
---|
97 | |
---|
98 | return declStmt; |
---|
99 | } |
---|
100 | |
---|
101 | ExprStmt *assignFromDecl( DeclStmt *declStmt ) { |
---|
102 | ObjectDecl *decl; |
---|
103 | if ( (decl = dynamic_cast<ObjectDecl *>( declStmt->get_decl() )) != 0 ) { |
---|
104 | SingleInit *init; |
---|
105 | if ( (init = dynamic_cast<SingleInit *>(decl->get_init())) != 0 ) { |
---|
106 | } // if |
---|
107 | } // if |
---|
108 | |
---|
109 | return 0; |
---|
110 | } |
---|
111 | |
---|
112 | bool isDeclStmtP(Statement *stmt) { |
---|
113 | return ( dynamic_cast< DeclStmt *>(stmt) != 0 ); |
---|
114 | } |
---|
115 | |
---|
116 | BasicInit::Classify::TypeKind BasicInit::Classify::type( Type *toClassify ) { |
---|
117 | if ( toClassify == 0 ) return NULL_T; |
---|
118 | |
---|
119 | if ( dynamic_cast< StructInstType * >(toClassify) || |
---|
120 | dynamic_cast< UnionInstType * >(toClassify) || |
---|
121 | dynamic_cast< ArrayType * >(toClassify) ) |
---|
122 | return COMPOUND_T; |
---|
123 | else |
---|
124 | return SINGLE_T; |
---|
125 | } |
---|
126 | |
---|
127 | BasicInit::Classify::InitKind BasicInit::Classify::initializer( Initializer *init) { |
---|
128 | if ( init == 0 ) return NULL_I; |
---|
129 | if ( dynamic_cast< ListInit * >(init) ) |
---|
130 | return COMPOUND_I; |
---|
131 | if ( dynamic_cast< SingleInit * >(init) ) |
---|
132 | return SINGLE_I; |
---|
133 | |
---|
134 | return NULL_I; // shouldn't be here anyways |
---|
135 | } |
---|
136 | |
---|
137 | NameInCollection * BasicInit::Classify::declaration( ObjectDecl *objdecl, SymTab::Indexer *index ) { |
---|
138 | assert ( index != 0 ); |
---|
139 | |
---|
140 | ReferenceToType *reftype; |
---|
141 | if ( (reftype = dynamic_cast< StructInstType * >( objdecl->get_type() )) != 0 ) { |
---|
142 | StructDecl *strDecl = index->lookupStruct( reftype->get_name() ); |
---|
143 | if ( strDecl != 0 ) { |
---|
144 | NameCollectionBuilder bld; |
---|
145 | NameCollector nc( bld ); |
---|
146 | strDecl->accept( nc ); |
---|
147 | NameInCollection *col = nc.get_builder().get_collection(); |
---|
148 | nc.get_builder().set_collection( 0 ); |
---|
149 | |
---|
150 | return col; |
---|
151 | } else |
---|
152 | throw( SemanticError( std::string("No structure of name: ") + reftype->get_name() ) ); |
---|
153 | } else { |
---|
154 | throw(SemanticError( reftype->get_name() + std::string("is not a reference to type") )); |
---|
155 | return 0; |
---|
156 | } // if |
---|
157 | } |
---|
158 | |
---|
159 | std::list< Statement * > |
---|
160 | BasicInit::Classify::matchInit( NameInCollection *col, ObjectDecl *toInitialize, Initializer *init ) { |
---|
161 | assert ( col != 0 ); |
---|
162 | |
---|
163 | std::list< Statement * > arranged(0); //( col->size() ); |
---|
164 | std::fill( arranged.begin(), arranged.end(), (Statement *)0 ); |
---|
165 | int current = 0; |
---|
166 | |
---|
167 | if ( init == 0 ) |
---|
168 | // no initializer... shouldn't even bother... fix this. |
---|
169 | return arranged; |
---|
170 | |
---|
171 | ListInit *li = dynamic_cast< ListInit * >( init ); |
---|
172 | |
---|
173 | if ( li != 0 ) { |
---|
174 | for ( std::list<Initializer *>::iterator i = li->begin_initializers(); |
---|
175 | i != li->end_initializers(); |
---|
176 | i++) { |
---|
177 | std::list<Expression *> d_orig = (*i)->get_designators(); |
---|
178 | |
---|
179 | NameInCollection *corrsp; // corresponding element to this initializer |
---|
180 | if ( ! d_orig.empty() ) { |
---|
181 | // 1) has designators |
---|
182 | std::list<NameExpr *> des; |
---|
183 | std::transform( d_orig.begin(), d_orig.end(), |
---|
184 | std::back_inserter( des ), cast_ptr<Expression, NameExpr > ); |
---|
185 | |
---|
186 | for ( std::list<NameExpr *>::iterator j = des.begin(); j != des.end(); j++ ) { |
---|
187 | // check for existence of the element |
---|
188 | |
---|
189 | if ( (corrsp = (*col)[ (*j)->get_name() ]) != 0 ) { |
---|
190 | // current++; |
---|
191 | SingleInit *sinit; |
---|
192 | if ( (sinit = dynamic_cast< SingleInit * >( *i )) != 0 ) |
---|
193 | arranged.push_back( constructAssgn( corrsp->get_name(), toInitialize, sinit ) ); |
---|
194 | else |
---|
195 | ; // recursive call to matchInit |
---|
196 | } else |
---|
197 | // error, member doesn't exist |
---|
198 | return arranged; // throw( 0 ); // XXX |
---|
199 | } |
---|
200 | } else { |
---|
201 | // 2) doesn't have designators |
---|
202 | if ( (corrsp = (*col)[ current++ ]) != 0 ) { |
---|
203 | SingleInit *sinit; |
---|
204 | if ( (sinit = dynamic_cast< SingleInit * >( *i )) != 0 ) |
---|
205 | arranged.push_back( constructAssgn( corrsp->get_name(), toInitialize, sinit ) ); |
---|
206 | else |
---|
207 | ; // recursive call to matchInit |
---|
208 | } else { |
---|
209 | // Shouldn't be here... probably too many elements in initializer? |
---|
210 | } // if |
---|
211 | } // if |
---|
212 | } // for |
---|
213 | } // if |
---|
214 | |
---|
215 | return arranged; |
---|
216 | } |
---|
217 | |
---|
218 | Statement *BasicInit::Classify::constructAssgn( std::string membName, ObjectDecl *toInit, SingleInit *sinit ) { |
---|
219 | std::list< Expression * > args; |
---|
220 | args.push_back(new AddressExpr( new UntypedMemberExpr( membName, new NameExpr(toInit->get_name()) ))); |
---|
221 | args.push_back( sinit->get_value() ); |
---|
222 | Statement *ret = new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args)); |
---|
223 | return ret; |
---|
224 | } |
---|
225 | |
---|
226 | void BasicInit::build_statements( NameAssociation< Expression *, BreakInitializer > *assoc, std::string aggName, std::list< Statement *> &stmts ) { |
---|
227 | assert( assoc != 0 ); |
---|
228 | static std::list< std::string > prefix; |
---|
229 | |
---|
230 | NameInCollection *col = assoc->get_collection(); |
---|
231 | if ( col->isComposite() ) { |
---|
232 | VariousNames *vc = dynamic_cast< VariousNames * >( col ); |
---|
233 | for ( VariousNames::key_iterator it = vc->keys_begin(); it != vc->keys_end(); it++ ) { |
---|
234 | prefix.push_back( *it ); |
---|
235 | if ( (*assoc)[ *it ] != 0 ) |
---|
236 | build_statements( (*assoc)[ *it ], aggName, stmts ); |
---|
237 | |
---|
238 | prefix.pop_back(); |
---|
239 | } |
---|
240 | } else { |
---|
241 | SingleNameAssoc< Expression *, BreakInitializer > *sa = \ |
---|
242 | dynamic_cast< SingleNameAssoc< Expression *, BreakInitializer > * >( assoc ); |
---|
243 | assert( sa != 0 ); |
---|
244 | |
---|
245 | Expression * rh = sa->get_data(); |
---|
246 | |
---|
247 | if (rh != 0) { |
---|
248 | // construct assignment statement list |
---|
249 | Expression *lh = new NameExpr ( aggName ); |
---|
250 | for ( std::list< std::string >::iterator i = prefix.begin(); i != prefix.end(); i++ ) |
---|
251 | lh = new UntypedMemberExpr( *i, lh ); |
---|
252 | |
---|
253 | std::list< Expression * > args; |
---|
254 | args.push_back( new AddressExpr(lh) ); args.push_back( rh ); |
---|
255 | |
---|
256 | stmts.push_back( new ExprStmt(std::list<Label>(), new UntypedExpr(new NameExpr("?=?"), args)) ); |
---|
257 | } // if |
---|
258 | } // if |
---|
259 | |
---|
260 | return; |
---|
261 | } |
---|
262 | } // namespace InitTweak |
---|
263 | |
---|
264 | // Local Variables: // |
---|
265 | // tab-width: 4 // |
---|
266 | // mode: c++ // |
---|
267 | // compile-command: "make install" // |
---|
268 | // End: // |
---|