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 | // Resolver.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 12:17:01 2015 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Mon Jun 01 13:47:16 2015 |
---|
13 | // Update Count : 21 |
---|
14 | // |
---|
15 | |
---|
16 | #include "Resolver.h" |
---|
17 | #include "AlternativeFinder.h" |
---|
18 | #include "Alternative.h" |
---|
19 | #include "RenameVars.h" |
---|
20 | #include "ResolveTypeof.h" |
---|
21 | #include "SynTree/Statement.h" |
---|
22 | #include "SynTree/Type.h" |
---|
23 | #include "SynTree/Expression.h" |
---|
24 | #include "SynTree/Initializer.h" |
---|
25 | #include "SymTab/Indexer.h" |
---|
26 | #include "utility.h" |
---|
27 | |
---|
28 | #include <iostream> |
---|
29 | using namespace std; |
---|
30 | |
---|
31 | namespace ResolvExpr { |
---|
32 | class Resolver : public SymTab::Indexer { |
---|
33 | public: |
---|
34 | Resolver() : SymTab::Indexer( false ), switchType( 0 ) {} |
---|
35 | |
---|
36 | virtual void visit( FunctionDecl *functionDecl ); |
---|
37 | virtual void visit( ObjectDecl *functionDecl ); |
---|
38 | virtual void visit( TypeDecl *typeDecl ); |
---|
39 | |
---|
40 | virtual void visit( ExprStmt *exprStmt ); |
---|
41 | virtual void visit( IfStmt *ifStmt ); |
---|
42 | virtual void visit( WhileStmt *whileStmt ); |
---|
43 | virtual void visit( ForStmt *forStmt ); |
---|
44 | virtual void visit( SwitchStmt *switchStmt ); |
---|
45 | virtual void visit( ChooseStmt *switchStmt ); |
---|
46 | virtual void visit( CaseStmt *caseStmt ); |
---|
47 | virtual void visit( ReturnStmt *returnStmt ); |
---|
48 | |
---|
49 | virtual void visit( SingleInit *singleInit ); |
---|
50 | virtual void visit( ListInit *listInit ); |
---|
51 | private: |
---|
52 | std::list< Type * > functionReturn; |
---|
53 | Type *initContext; |
---|
54 | Type *switchType; |
---|
55 | }; |
---|
56 | |
---|
57 | void resolve( std::list< Declaration * > translationUnit ) { |
---|
58 | Resolver resolver; |
---|
59 | acceptAll( translationUnit, resolver ); |
---|
60 | #if 0 |
---|
61 | resolver.print( cerr ); |
---|
62 | for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { |
---|
63 | (*i)->print( std::cerr ); |
---|
64 | (*i)->accept( resolver ); |
---|
65 | } // for |
---|
66 | #endif |
---|
67 | } |
---|
68 | |
---|
69 | Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { |
---|
70 | TypeEnvironment env; |
---|
71 | return resolveInVoidContext( expr, indexer, env ); |
---|
72 | } |
---|
73 | |
---|
74 | namespace { |
---|
75 | void finishExpr( Expression *expr, const TypeEnvironment &env ) { |
---|
76 | expr->set_env( new TypeSubstitution ); |
---|
77 | env.makeSubstitution( *expr->get_env() ); |
---|
78 | } |
---|
79 | |
---|
80 | Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
81 | global_renamer.reset(); |
---|
82 | TypeEnvironment env; |
---|
83 | Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); |
---|
84 | finishExpr( newExpr, env ); |
---|
85 | return newExpr; |
---|
86 | } |
---|
87 | |
---|
88 | Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
89 | TypeEnvironment env; |
---|
90 | AlternativeFinder finder( indexer, env ); |
---|
91 | finder.find( untyped ); |
---|
92 | #if 0 |
---|
93 | if ( finder.get_alternatives().size() != 1 ) { |
---|
94 | std::cout << "untyped expr is "; |
---|
95 | untyped->print( std::cout ); |
---|
96 | std::cout << std::endl << "alternatives are:"; |
---|
97 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
98 | i->print( std::cout ); |
---|
99 | } // for |
---|
100 | } // if |
---|
101 | #endif |
---|
102 | assert( finder.get_alternatives().size() == 1 ); |
---|
103 | Alternative &choice = finder.get_alternatives().front(); |
---|
104 | Expression *newExpr = choice.expr->clone(); |
---|
105 | finishExpr( newExpr, choice.env ); |
---|
106 | return newExpr; |
---|
107 | } |
---|
108 | |
---|
109 | bool isIntegralType( Type *type ) { |
---|
110 | if ( dynamic_cast< EnumInstType * >( type ) ) { |
---|
111 | return true; |
---|
112 | } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) { |
---|
113 | return bt->isInteger(); |
---|
114 | } else { |
---|
115 | return false; |
---|
116 | } // if |
---|
117 | } |
---|
118 | |
---|
119 | Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { |
---|
120 | TypeEnvironment env; |
---|
121 | AlternativeFinder finder( indexer, env ); |
---|
122 | finder.find( untyped ); |
---|
123 | #if 0 |
---|
124 | if ( finder.get_alternatives().size() != 1 ) { |
---|
125 | std::cout << "untyped expr is "; |
---|
126 | untyped->print( std::cout ); |
---|
127 | std::cout << std::endl << "alternatives are:"; |
---|
128 | for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
129 | i->print( std::cout ); |
---|
130 | } // for |
---|
131 | } // if |
---|
132 | #endif |
---|
133 | Expression *newExpr = 0; |
---|
134 | const TypeEnvironment *newEnv = 0; |
---|
135 | for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { |
---|
136 | if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) { |
---|
137 | if ( newExpr ) { |
---|
138 | throw SemanticError( "Too many interpretations for case control expression", untyped ); |
---|
139 | } else { |
---|
140 | newExpr = i->expr->clone(); |
---|
141 | newEnv = &i->env; |
---|
142 | } // if |
---|
143 | } // if |
---|
144 | } // for |
---|
145 | if ( ! newExpr ) { |
---|
146 | throw SemanticError( "No interpretations for case control expression", untyped ); |
---|
147 | } // if |
---|
148 | finishExpr( newExpr, *newEnv ); |
---|
149 | return newExpr; |
---|
150 | } |
---|
151 | |
---|
152 | } |
---|
153 | |
---|
154 | void Resolver::visit( ObjectDecl *objectDecl ) { |
---|
155 | Type *new_type = resolveTypeof( objectDecl->get_type(), *this ); |
---|
156 | objectDecl->set_type( new_type ); |
---|
157 | initContext = new_type; |
---|
158 | SymTab::Indexer::visit( objectDecl ); |
---|
159 | |
---|
160 | if ( ArrayType * at = dynamic_cast< ArrayType * >( new_type ) ){ |
---|
161 | if ( at->get_dimension() ) { |
---|
162 | BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
163 | CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() ); |
---|
164 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
165 | delete at->get_dimension(); |
---|
166 | at->set_dimension( newExpr ); |
---|
167 | } |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | void Resolver::visit( TypeDecl *typeDecl ) { |
---|
172 | if ( typeDecl->get_base() ) { |
---|
173 | Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); |
---|
174 | typeDecl->set_base( new_type ); |
---|
175 | } // if |
---|
176 | SymTab::Indexer::visit( typeDecl ); |
---|
177 | } |
---|
178 | |
---|
179 | void Resolver::visit( FunctionDecl *functionDecl ) { |
---|
180 | #if 0 |
---|
181 | std::cout << "resolver visiting functiondecl "; |
---|
182 | functionDecl->print( std::cout ); |
---|
183 | std::cout << std::endl; |
---|
184 | #endif |
---|
185 | Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); |
---|
186 | functionDecl->set_type( new_type ); |
---|
187 | std::list< Type * > oldFunctionReturn = functionReturn; |
---|
188 | functionReturn.clear(); |
---|
189 | for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) { |
---|
190 | functionReturn.push_back( (*i)->get_type() ); |
---|
191 | } // for |
---|
192 | SymTab::Indexer::visit( functionDecl ); |
---|
193 | functionReturn = oldFunctionReturn; |
---|
194 | } |
---|
195 | |
---|
196 | void Resolver::visit( ExprStmt *exprStmt ) { |
---|
197 | if ( exprStmt->get_expr() ) { |
---|
198 | Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this ); |
---|
199 | delete exprStmt->get_expr(); |
---|
200 | exprStmt->set_expr( newExpr ); |
---|
201 | } // if |
---|
202 | } |
---|
203 | |
---|
204 | void Resolver::visit( IfStmt *ifStmt ) { |
---|
205 | Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this ); |
---|
206 | delete ifStmt->get_condition(); |
---|
207 | ifStmt->set_condition( newExpr ); |
---|
208 | Visitor::visit( ifStmt ); |
---|
209 | } |
---|
210 | |
---|
211 | void Resolver::visit( WhileStmt *whileStmt ) { |
---|
212 | Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this ); |
---|
213 | delete whileStmt->get_condition(); |
---|
214 | whileStmt->set_condition( newExpr ); |
---|
215 | Visitor::visit( whileStmt ); |
---|
216 | } |
---|
217 | |
---|
218 | void Resolver::visit( ForStmt *forStmt ) { |
---|
219 | // SymTab::Indexer::visit( forStmt ); |
---|
220 | Expression *newExpr; |
---|
221 | // for statements introduce a level of scope |
---|
222 | enterScope(); |
---|
223 | maybeAccept( forStmt->get_initialization(), *this ); |
---|
224 | if ( forStmt->get_condition() ) { |
---|
225 | newExpr = findSingleExpression( forStmt->get_condition(), *this ); |
---|
226 | delete forStmt->get_condition(); |
---|
227 | forStmt->set_condition( newExpr ); |
---|
228 | } // if |
---|
229 | |
---|
230 | if ( forStmt->get_increment() ) { |
---|
231 | newExpr = findVoidExpression( forStmt->get_increment(), *this ); |
---|
232 | delete forStmt->get_increment(); |
---|
233 | forStmt->set_increment( newExpr ); |
---|
234 | } // if |
---|
235 | |
---|
236 | maybeAccept( forStmt->get_condition(), *this ); |
---|
237 | maybeAccept( forStmt->get_increment(), *this ); |
---|
238 | maybeAccept( forStmt->get_body(), *this ); |
---|
239 | leaveScope(); |
---|
240 | } |
---|
241 | |
---|
242 | template< typename SwitchClass > |
---|
243 | void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) { |
---|
244 | Expression *newExpr; |
---|
245 | newExpr = findIntegralExpression( switchStmt->get_condition(), visitor ); |
---|
246 | delete switchStmt->get_condition(); |
---|
247 | switchStmt->set_condition( newExpr ); |
---|
248 | |
---|
249 | visitor.Visitor::visit( switchStmt ); |
---|
250 | } |
---|
251 | |
---|
252 | void Resolver::visit( SwitchStmt *switchStmt ) { |
---|
253 | handleSwitchStmt( switchStmt, *this ); |
---|
254 | } |
---|
255 | |
---|
256 | void Resolver::visit( ChooseStmt *switchStmt ) { |
---|
257 | handleSwitchStmt( switchStmt, *this ); |
---|
258 | } |
---|
259 | |
---|
260 | void Resolver::visit( CaseStmt *caseStmt ) { |
---|
261 | Visitor::visit( caseStmt ); |
---|
262 | } |
---|
263 | |
---|
264 | void Resolver::visit( ReturnStmt *returnStmt ) { |
---|
265 | if ( returnStmt->get_expr() ) { |
---|
266 | CastExpr *castExpr = new CastExpr( returnStmt->get_expr() ); |
---|
267 | cloneAll( functionReturn, castExpr->get_results() ); |
---|
268 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
269 | delete castExpr; |
---|
270 | returnStmt->set_expr( newExpr ); |
---|
271 | } // if |
---|
272 | } |
---|
273 | |
---|
274 | void Resolver::visit( SingleInit *singleInit ) { |
---|
275 | if ( singleInit->get_value() ) { |
---|
276 | #if 0 |
---|
277 | if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) { |
---|
278 | string n = ne->get_name(); |
---|
279 | if (n == "0") { |
---|
280 | initContext = new BasicType(Type::Qualifiers(), |
---|
281 | BasicType::SignedInt); |
---|
282 | } else { |
---|
283 | DeclarationWithType * decl = lookupId(n); |
---|
284 | initContext = decl->get_type(); |
---|
285 | } |
---|
286 | } else if (ConstantExpr * e = |
---|
287 | dynamic_cast<ConstantExpr*>(singleInit->get_value())) { |
---|
288 | Constant *c = e->get_constant(); |
---|
289 | initContext = c->get_type(); |
---|
290 | } else { |
---|
291 | assert(0); |
---|
292 | } |
---|
293 | #endif |
---|
294 | CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); |
---|
295 | Expression *newExpr = findSingleExpression( castExpr, *this ); |
---|
296 | delete castExpr; |
---|
297 | singleInit->set_value( newExpr ); |
---|
298 | } // if |
---|
299 | // singleInit->get_value()->accept( *this ); |
---|
300 | } |
---|
301 | |
---|
302 | void Resolver::visit( ListInit *listInit ) { |
---|
303 | Visitor::visit(listInit); |
---|
304 | #if 0 |
---|
305 | if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { |
---|
306 | std::list<Initializer *>::iterator iter( listInit->begin_initializers() ); |
---|
307 | for ( ; iter != listInit->end_initializers(); ++iter ) { |
---|
308 | initContext = at->get_base(); |
---|
309 | (*iter)->accept( *this ); |
---|
310 | } // for |
---|
311 | } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) { |
---|
312 | StructDecl *baseStruct = st->get_baseStruct(); |
---|
313 | std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() ); |
---|
314 | std::list<Initializer *>::iterator iter2( listInit->begin_initializers() ); |
---|
315 | for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) { |
---|
316 | if ( (*iter2)->get_designators().empty() ) { |
---|
317 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 ); |
---|
318 | initContext = dt->get_type(); |
---|
319 | (*iter2)->accept( *this ); |
---|
320 | ++iter1; |
---|
321 | } else { |
---|
322 | StructDecl *st = baseStruct; |
---|
323 | iter1 = st->get_members().begin(); |
---|
324 | std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() ); |
---|
325 | for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) { |
---|
326 | NameExpr *key = dynamic_cast<NameExpr *>( *iter3 ); |
---|
327 | assert( key ); |
---|
328 | for ( ; iter1 != st->get_members().end(); ++iter1 ) { |
---|
329 | if ( key->get_name() == (*iter1)->get_name() ) { |
---|
330 | (*iter1)->print( cout ); |
---|
331 | cout << key->get_name() << endl; |
---|
332 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
333 | assert( fred ); |
---|
334 | StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() ); |
---|
335 | assert( mary ); |
---|
336 | st = mary->get_baseStruct(); |
---|
337 | iter1 = st->get_members().begin(); |
---|
338 | break; |
---|
339 | } // if |
---|
340 | } // for |
---|
341 | } // for |
---|
342 | ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 ); |
---|
343 | assert( fred ); |
---|
344 | initContext = fred->get_type(); |
---|
345 | (*listInit->begin_initializers())->accept( *this ); |
---|
346 | } // if |
---|
347 | } // for |
---|
348 | } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) { |
---|
349 | DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() ); |
---|
350 | initContext = dt->get_type(); |
---|
351 | (*listInit->begin_initializers())->accept( *this ); |
---|
352 | } // if |
---|
353 | #endif |
---|
354 | } |
---|
355 | } // namespace ResolvExpr |
---|
356 | |
---|
357 | // Local Variables: // |
---|
358 | // tab-width: 4 // |
---|
359 | // mode: c++ // |
---|
360 | // compile-command: "make install" // |
---|
361 | // End: // |
---|