source: src/ResolvExpr/Resolver.cc@ 258eb5c9

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 258eb5c9 was 7f5566b, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

asm statement, memory leaks

  • Property mode set to 100644
File size: 16.2 KB
Line 
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 : Peter A. Buhr
12// Last Modified On : Fri Jul 24 17:33:54 2015
13// Update Count : 178
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>
29using namespace std;
30
31namespace 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( ArrayType * at );
41
42 virtual void visit( ExprStmt *exprStmt );
43 virtual void visit( AsmExpr *asmExpr );
44 virtual void visit( AsmStmt *asmStmt );
45 virtual void visit( IfStmt *ifStmt );
46 virtual void visit( WhileStmt *whileStmt );
47 virtual void visit( ForStmt *forStmt );
48 virtual void visit( SwitchStmt *switchStmt );
49 virtual void visit( ChooseStmt *switchStmt );
50 virtual void visit( CaseStmt *caseStmt );
51 virtual void visit( BranchStmt *branchStmt );
52 virtual void visit( ReturnStmt *returnStmt );
53
54 virtual void visit( SingleInit *singleInit );
55 virtual void visit( ListInit *listInit );
56 private:
57 typedef std::list< Initializer * >::iterator InitIterator;
58
59 void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & );
60 void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & );
61
62 std::list< Type * > functionReturn;
63 Type *initContext;
64 Type *switchType;
65 };
66
67 void resolve( std::list< Declaration * > translationUnit ) {
68 Resolver resolver;
69 acceptAll( translationUnit, resolver );
70#if 0
71 resolver.print( cerr );
72 for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) {
73 (*i)->print( std::cerr );
74 (*i)->accept( resolver );
75 } // for
76#endif
77 }
78
79 Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
80 TypeEnvironment env;
81 return resolveInVoidContext( expr, indexer, env );
82 }
83
84 namespace {
85 void finishExpr( Expression *expr, const TypeEnvironment &env ) {
86 expr->set_env( new TypeSubstitution );
87 env.makeSubstitution( *expr->get_env() );
88 }
89
90 Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
91 global_renamer.reset();
92 TypeEnvironment env;
93 Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
94 finishExpr( newExpr, env );
95 return newExpr;
96 }
97
98 Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
99 TypeEnvironment env;
100 AlternativeFinder finder( indexer, env );
101 finder.find( untyped );
102#if 0
103 if ( finder.get_alternatives().size() != 1 ) {
104 std::cout << "untyped expr is ";
105 untyped->print( std::cout );
106 std::cout << std::endl << "alternatives are:";
107 for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
108 i->print( std::cout );
109 } // for
110 } // if
111#endif
112 assert( finder.get_alternatives().size() == 1 );
113 Alternative &choice = finder.get_alternatives().front();
114 Expression *newExpr = choice.expr->clone();
115 finishExpr( newExpr, choice.env );
116 return newExpr;
117 }
118
119 bool isIntegralType( Type *type ) {
120 if ( dynamic_cast< EnumInstType * >( type ) ) {
121 return true;
122 } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) {
123 return bt->isInteger();
124 } else {
125 return false;
126 } // if
127 }
128
129 Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
130 TypeEnvironment env;
131 AlternativeFinder finder( indexer, env );
132 finder.find( untyped );
133#if 0
134 if ( finder.get_alternatives().size() != 1 ) {
135 std::cout << "untyped expr is ";
136 untyped->print( std::cout );
137 std::cout << std::endl << "alternatives are:";
138 for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
139 i->print( std::cout );
140 } // for
141 } // if
142#endif
143 Expression *newExpr = 0;
144 const TypeEnvironment *newEnv = 0;
145 for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
146 if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) {
147 if ( newExpr ) {
148 throw SemanticError( "Too many interpretations for case control expression", untyped );
149 } else {
150 newExpr = i->expr->clone();
151 newEnv = &i->env;
152 } // if
153 } // if
154 } // for
155 if ( ! newExpr ) {
156 throw SemanticError( "No interpretations for case control expression", untyped );
157 } // if
158 finishExpr( newExpr, *newEnv );
159 return newExpr;
160 }
161
162 }
163
164 void Resolver::visit( ObjectDecl *objectDecl ) {
165 Type *new_type = resolveTypeof( objectDecl->get_type(), *this );
166 objectDecl->set_type( new_type );
167 initContext = new_type;
168 SymTab::Indexer::visit( objectDecl );
169 }
170
171 void Resolver::visit( ArrayType * at ) {
172 if ( at->get_dimension() ) {
173 BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
174 CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() );
175 Expression *newExpr = findSingleExpression( castExpr, *this );
176 delete at->get_dimension();
177 at->set_dimension( newExpr );
178 }
179 Visitor::visit( at );
180 }
181
182 void Resolver::visit( TypeDecl *typeDecl ) {
183 if ( typeDecl->get_base() ) {
184 Type *new_type = resolveTypeof( typeDecl->get_base(), *this );
185 typeDecl->set_base( new_type );
186 } // if
187 SymTab::Indexer::visit( typeDecl );
188 }
189
190 void Resolver::visit( FunctionDecl *functionDecl ) {
191#if 0
192 std::cout << "resolver visiting functiondecl ";
193 functionDecl->print( std::cout );
194 std::cout << std::endl;
195#endif
196 Type *new_type = resolveTypeof( functionDecl->get_type(), *this );
197 functionDecl->set_type( new_type );
198 std::list< Type * > oldFunctionReturn = functionReturn;
199 functionReturn.clear();
200 for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) {
201 functionReturn.push_back( (*i)->get_type() );
202 } // for
203 SymTab::Indexer::visit( functionDecl );
204 functionReturn = oldFunctionReturn;
205 }
206
207 void Resolver::visit( ExprStmt *exprStmt ) {
208 if ( exprStmt->get_expr() ) {
209 Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this );
210 delete exprStmt->get_expr();
211 exprStmt->set_expr( newExpr );
212 } // if
213 }
214
215 void Resolver::visit( AsmExpr *asmExpr ) {
216 Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this );
217 delete asmExpr->get_operand();
218 asmExpr->set_operand( newExpr );
219 if ( asmExpr->get_inout() ) {
220 newExpr = findVoidExpression( asmExpr->get_inout(), *this );
221 delete asmExpr->get_inout();
222 asmExpr->set_inout( newExpr );
223 } // if
224 }
225
226 void Resolver::visit( AsmStmt *asmStmt ) {
227 acceptAll( asmStmt->get_input(), *this);
228 acceptAll( asmStmt->get_output(), *this);
229 }
230
231 void Resolver::visit( IfStmt *ifStmt ) {
232 Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this );
233 delete ifStmt->get_condition();
234 ifStmt->set_condition( newExpr );
235 Visitor::visit( ifStmt );
236 }
237
238 void Resolver::visit( WhileStmt *whileStmt ) {
239 Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this );
240 delete whileStmt->get_condition();
241 whileStmt->set_condition( newExpr );
242 Visitor::visit( whileStmt );
243 }
244
245 void Resolver::visit( ForStmt *forStmt ) {
246 SymTab::Indexer::visit( forStmt );
247
248 if ( forStmt->get_condition() ) {
249 Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this );
250 delete forStmt->get_condition();
251 forStmt->set_condition( newExpr );
252 } // if
253
254 if ( forStmt->get_increment() ) {
255 Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this );
256 delete forStmt->get_increment();
257 forStmt->set_increment( newExpr );
258 } // if
259 }
260
261 template< typename SwitchClass >
262 void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) {
263 Expression *newExpr;
264 newExpr = findIntegralExpression( switchStmt->get_condition(), visitor );
265 delete switchStmt->get_condition();
266 switchStmt->set_condition( newExpr );
267
268 visitor.Visitor::visit( switchStmt );
269 }
270
271 void Resolver::visit( SwitchStmt *switchStmt ) {
272 handleSwitchStmt( switchStmt, *this );
273 }
274
275 void Resolver::visit( ChooseStmt *switchStmt ) {
276 handleSwitchStmt( switchStmt, *this );
277 }
278
279 void Resolver::visit( CaseStmt *caseStmt ) {
280 Visitor::visit( caseStmt );
281 }
282
283 void Resolver::visit( BranchStmt *branchStmt ) {
284 // must resolve the argument for a computed goto
285 if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
286 if ( Expression * arg = branchStmt->get_computedTarget() ) {
287 VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder
288 PointerType pt( Type::Qualifiers(), v.clone() );
289 CastExpr * castExpr = new CastExpr( arg, pt.clone() );
290 Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression
291 branchStmt->set_target( newExpr );
292 } // if
293 } // if
294 }
295
296 void Resolver::visit( ReturnStmt *returnStmt ) {
297 if ( returnStmt->get_expr() ) {
298 CastExpr *castExpr = new CastExpr( returnStmt->get_expr() );
299 cloneAll( functionReturn, castExpr->get_results() );
300 Expression *newExpr = findSingleExpression( castExpr, *this );
301 delete castExpr;
302 returnStmt->set_expr( newExpr );
303 } // if
304 }
305
306 template< typename T >
307 bool isCharType( T t ) {
308 if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
309 return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
310 bt->get_kind() == BasicType::UnsignedChar;
311 }
312 return false;
313 }
314
315 void Resolver::visit( SingleInit *singleInit ) {
316 if ( singleInit->get_value() ) {
317#if 0
318 if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) {
319 string n = ne->get_name();
320 if (n == "0") {
321 initContext = new BasicType(Type::Qualifiers(),
322 BasicType::SignedInt);
323 } else {
324 DeclarationWithType * decl = lookupId(n);
325 initContext = decl->get_type();
326 }
327 } else if (ConstantExpr * e =
328 dynamic_cast<ConstantExpr*>(singleInit->get_value())) {
329 Constant *c = e->get_constant();
330 initContext = c->get_type();
331 } else {
332 assert(0);
333 }
334#endif
335 CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() );
336 Expression *newExpr = findSingleExpression( castExpr, *this );
337 delete castExpr;
338 singleInit->set_value( newExpr );
339
340 // check if initializing type is char[]
341 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
342 if ( isCharType( at->get_base() ) ) {
343 // check if the resolved type is char *
344 if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) {
345 if ( isCharType( pt->get_base() ) ) {
346 // strip cast if we're initializing a char[] with a char *, e.g.
347 // char x[] = "hello";
348 CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );
349 singleInit->set_value( ce->get_arg() );
350 ce->set_arg( NULL );
351 delete ce;
352 }
353 }
354 }
355 }
356 } // if
357// singleInit->get_value()->accept( *this );
358 }
359
360 void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) {
361 DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl );
362 assert( dt );
363 initContext = dt->get_type();
364 try {
365 if ( init == initEnd ) return; // stop when there are no more initializers
366 (*init)->accept( *this );
367 ++init; // made it past an initializer
368 } catch( SemanticError & ) {
369 // need to delve deeper, if you can
370 if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) {
371 resolveAggrInit( sit->get_baseStruct(), init, initEnd );
372 } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) {
373 resolveAggrInit( uit->get_baseUnion(), init, initEnd );
374 } else {
375 // member is not an aggregate type, so can't go any deeper
376
377 // might need to rethink what is being thrown
378 throw;
379 } // if
380 }
381 }
382
383 void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) {
384 if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) {
385 // want to resolve each initializer to the members of the struct,
386 // but if there are more initializers than members we should stop
387 list< Declaration * >::iterator it = st->get_members().begin();
388 for ( ; it != st->get_members().end(); ++it) {
389 resolveSingleAggrInit( *it, init, initEnd );
390 }
391 } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) {
392 // only resolve to the first member of a union
393 resolveSingleAggrInit( *un->get_members().begin(), init, initEnd );
394 } // if
395 }
396
397 void Resolver::visit( ListInit * listInit ) {
398 InitIterator iter = listInit->begin_initializers();
399 InitIterator end = listInit->end_initializers();
400
401 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
402 // resolve each member to the base type of the array
403 for ( ; iter != end; ++iter ) {
404 initContext = at->get_base();
405 (*iter)->accept( *this );
406 } // for
407 } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) {
408 resolveAggrInit( st->get_baseStruct(), iter, end );
409 } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) {
410 resolveAggrInit( st->get_baseUnion(), iter, end );
411 } else {
412 // basic types are handled here
413 Visitor::visit( listInit );
414 }
415
416#if 0
417 if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) {
418 std::list<Initializer *>::iterator iter( listInit->begin_initializers() );
419 for ( ; iter != listInit->end_initializers(); ++iter ) {
420 initContext = at->get_base();
421 (*iter)->accept( *this );
422 } // for
423 } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) {
424 StructDecl *baseStruct = st->get_baseStruct();
425 std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() );
426 std::list<Initializer *>::iterator iter2( listInit->begin_initializers() );
427 for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) {
428 if ( (*iter2)->get_designators().empty() ) {
429 DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 );
430 initContext = dt->get_type();
431 (*iter2)->accept( *this );
432 ++iter1;
433 } else {
434 StructDecl *st = baseStruct;
435 iter1 = st->get_members().begin();
436 std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() );
437 for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) {
438 NameExpr *key = dynamic_cast<NameExpr *>( *iter3 );
439 assert( key );
440 for ( ; iter1 != st->get_members().end(); ++iter1 ) {
441 if ( key->get_name() == (*iter1)->get_name() ) {
442 (*iter1)->print( cout );
443 cout << key->get_name() << endl;
444 ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
445 assert( fred );
446 StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() );
447 assert( mary );
448 st = mary->get_baseStruct();
449 iter1 = st->get_members().begin();
450 break;
451 } // if
452 } // for
453 } // for
454 ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
455 assert( fred );
456 initContext = fred->get_type();
457 (*listInit->begin_initializers())->accept( *this );
458 } // if
459 } // for
460 } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) {
461 DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() );
462 initContext = dt->get_type();
463 (*listInit->begin_initializers())->accept( *this );
464 } // if
465#endif
466 }
467} // namespace ResolvExpr
468
469// Local Variables: //
470// tab-width: 4 //
471// mode: c++ //
472// compile-command: "make install" //
473// End: //
Note: See TracBrowser for help on using the repository browser.