source: translator/ResolvExpr/Resolver.cc@ d4778a6

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since d4778a6 was bdd516a, checked in by Peter A. Buhr <pabuhr@…>, 11 years ago

fixed sizeof type variable, find lowest cost alternative for sizeof expression, removed unused classes, added compiler flag, remove temporary file for -CFA, formatting

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