source: src/ResolvExpr/Resolver.cc@ a1d6d80

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 with_gc
Last change on this file since a1d6d80 was 21ae786, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

remove fallback to C style initializer if overridable constructor is found during expression resolution

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