source: src/ResolvExpr/Resolver.cc@ e6cee92

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox 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 e6cee92 was 307a732, checked in by Andrew Beach <ajbeach@…>, 8 years ago

The exception handling code compilers and translates, but the translation crashes.

  • Property mode set to 100644
File size: 18.7 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 : Thu Mar 23 17:23:14 2017
13// Update Count : 211
14//
15
16#include <iostream>
17
18#include "Alternative.h"
19#include "AlternativeFinder.h"
20#include "CurrentObject.h"
21#include "RenameVars.h"
22#include "Resolver.h"
23#include "ResolveTypeof.h"
24#include "typeops.h"
25
26#include "SynTree/Expression.h"
27#include "SynTree/Initializer.h"
28#include "SynTree/Statement.h"
29#include "SynTree/Type.h"
30
31#include "SymTab/Autogen.h"
32#include "SymTab/Indexer.h"
33
34#include "Common/utility.h"
35
36#include "InitTweak/InitTweak.h"
37
38using namespace std;
39
40namespace ResolvExpr {
41 class Resolver final : public SymTab::Indexer {
42 public:
43 Resolver() : SymTab::Indexer( false ) {}
44 Resolver( const SymTab:: Indexer & other ) : SymTab::Indexer( other ) {
45 if ( const Resolver * res = dynamic_cast< const Resolver * >( &other ) ) {
46 functionReturn = res->functionReturn;
47 currentObject = res->currentObject;
48 inEnumDecl = res->inEnumDecl;
49 }
50 }
51
52 typedef SymTab::Indexer Parent;
53 using Parent::visit;
54 virtual void visit( FunctionDecl *functionDecl ) override;
55 virtual void visit( ObjectDecl *functionDecl ) override;
56 virtual void visit( TypeDecl *typeDecl ) override;
57 virtual void visit( EnumDecl * enumDecl ) override;
58
59 virtual void visit( ArrayType * at ) override;
60 virtual void visit( PointerType * at ) override;
61
62 virtual void visit( ExprStmt *exprStmt ) override;
63 virtual void visit( AsmExpr *asmExpr ) override;
64 virtual void visit( AsmStmt *asmStmt ) override;
65 virtual void visit( IfStmt *ifStmt ) override;
66 virtual void visit( WhileStmt *whileStmt ) override;
67 virtual void visit( ForStmt *forStmt ) override;
68 virtual void visit( SwitchStmt *switchStmt ) override;
69 virtual void visit( CaseStmt *caseStmt ) override;
70 virtual void visit( BranchStmt *branchStmt ) override;
71 virtual void visit( ReturnStmt *returnStmt ) override;
72 virtual void visit( ThrowStmt *throwStmt ) override;
73
74 virtual void visit( SingleInit *singleInit ) override;
75 virtual void visit( ListInit *listInit ) override;
76 virtual void visit( ConstructorInit *ctorInit ) override;
77 private:
78 typedef std::list< Initializer * >::iterator InitIterator;
79
80 template< typename PtrType >
81 void handlePtrType( PtrType * type );
82
83 void resolveAggrInit( ReferenceToType *, InitIterator &, InitIterator & );
84 void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator &, TypeSubstitution sub );
85 void fallbackInit( ConstructorInit * ctorInit );
86
87 Type * functionReturn = nullptr;
88 CurrentObject currentObject = nullptr;
89 bool inEnumDecl = false;
90 };
91
92 void resolve( std::list< Declaration * > translationUnit ) {
93 Resolver resolver;
94 acceptAll( translationUnit, resolver );
95 }
96
97 Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
98 TypeEnvironment env;
99 return resolveInVoidContext( expr, indexer, env );
100 }
101
102
103 namespace {
104 void finishExpr( Expression *expr, const TypeEnvironment &env ) {
105 expr->set_env( new TypeSubstitution );
106 env.makeSubstitution( *expr->get_env() );
107 }
108 } // namespace
109
110 Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
111 global_renamer.reset();
112 TypeEnvironment env;
113 Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
114 finishExpr( newExpr, env );
115 return newExpr;
116 }
117
118 namespace {
119 Expression *findSingleExpression( 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 assertf( finder.get_alternatives().size() == 1, "findSingleExpression: must have exactly one alternative at the end." );
134 Alternative &choice = finder.get_alternatives().front();
135 Expression *newExpr = choice.expr->clone();
136 finishExpr( newExpr, choice.env );
137 return newExpr;
138 }
139
140 bool isIntegralType( Type *type ) {
141 if ( dynamic_cast< EnumInstType * >( type ) ) {
142 return true;
143 } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) {
144 return bt->isInteger();
145 } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) {
146 return true;
147 } else {
148 return false;
149 } // if
150 }
151
152 Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
153 TypeEnvironment env;
154 AlternativeFinder finder( indexer, env );
155 finder.find( untyped );
156#if 0
157 if ( finder.get_alternatives().size() != 1 ) {
158 std::cout << "untyped expr is ";
159 untyped->print( std::cout );
160 std::cout << std::endl << "alternatives are:";
161 for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
162 i->print( std::cout );
163 } // for
164 } // if
165#endif
166 Expression *newExpr = 0;
167 const TypeEnvironment *newEnv = 0;
168 for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
169 if ( i->expr->get_result()->size() == 1 && isIntegralType( i->expr->get_result() ) ) {
170 if ( newExpr ) {
171 throw SemanticError( "Too many interpretations for case control expression", untyped );
172 } else {
173 newExpr = i->expr->clone();
174 newEnv = &i->env;
175 } // if
176 } // if
177 } // for
178 if ( ! newExpr ) {
179 throw SemanticError( "No interpretations for case control expression", untyped );
180 } // if
181 finishExpr( newExpr, *newEnv );
182 return newExpr;
183 }
184
185 }
186
187 void Resolver::visit( ObjectDecl *objectDecl ) {
188 Type *new_type = resolveTypeof( objectDecl->get_type(), *this );
189 objectDecl->set_type( new_type );
190 // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable
191 // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes
192 // initContext because of a function type can contain object declarations in the return and parameter types. So
193 // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting
194 // the RHS.
195 ValueGuard<CurrentObject> temp( currentObject );
196 currentObject = CurrentObject( objectDecl->get_type() );
197 if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) {
198 // enumerator initializers should not use the enum type to initialize, since
199 // the enum type is still incomplete at this point. Use signed int instead.
200 currentObject = CurrentObject( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
201 }
202 Parent::visit( objectDecl );
203 if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) {
204 // delete newly created signed int type
205 // delete currentObject.getType();
206 }
207 }
208
209 template< typename PtrType >
210 void Resolver::handlePtrType( PtrType * type ) {
211 if ( type->get_dimension() ) {
212 CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() );
213 Expression *newExpr = findSingleExpression( castExpr, *this );
214 delete type->get_dimension();
215 type->set_dimension( newExpr );
216 }
217 }
218
219 void Resolver::visit( ArrayType * at ) {
220 handlePtrType( at );
221 Parent::visit( at );
222 }
223
224 void Resolver::visit( PointerType * pt ) {
225 handlePtrType( pt );
226 Parent::visit( pt );
227 }
228
229 void Resolver::visit( TypeDecl *typeDecl ) {
230 if ( typeDecl->get_base() ) {
231 Type *new_type = resolveTypeof( typeDecl->get_base(), *this );
232 typeDecl->set_base( new_type );
233 } // if
234 Parent::visit( typeDecl );
235 }
236
237 void Resolver::visit( FunctionDecl *functionDecl ) {
238#if 0
239 std::cout << "resolver visiting functiondecl ";
240 functionDecl->print( std::cout );
241 std::cout << std::endl;
242#endif
243 Type *new_type = resolveTypeof( functionDecl->get_type(), *this );
244 functionDecl->set_type( new_type );
245 ValueGuard< Type * > oldFunctionReturn( functionReturn );
246 functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() );
247 Parent::visit( functionDecl );
248
249 // default value expressions have an environment which shouldn't be there and trips up later passes.
250 // xxx - it might be necessary to somehow keep the information from this environment, but I can't currently
251 // see how it's useful.
252 for ( Declaration * d : functionDecl->get_functionType()->get_parameters() ) {
253 if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( d ) ) {
254 if ( SingleInit * init = dynamic_cast< SingleInit * >( obj->get_init() ) ) {
255 delete init->get_value()->get_env();
256 init->get_value()->set_env( nullptr );
257 }
258 }
259 }
260 }
261
262 void Resolver::visit( EnumDecl * enumDecl ) {
263 // in case we decide to allow nested enums
264 ValueGuard< bool > oldInEnumDecl( inEnumDecl );
265 inEnumDecl = true;
266 Parent::visit( enumDecl );
267 }
268
269 void Resolver::visit( ExprStmt *exprStmt ) {
270 assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" );
271 Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this );
272 delete exprStmt->get_expr();
273 exprStmt->set_expr( newExpr );
274 }
275
276 void Resolver::visit( AsmExpr *asmExpr ) {
277 Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this );
278 delete asmExpr->get_operand();
279 asmExpr->set_operand( newExpr );
280 if ( asmExpr->get_inout() ) {
281 newExpr = findVoidExpression( asmExpr->get_inout(), *this );
282 delete asmExpr->get_inout();
283 asmExpr->set_inout( newExpr );
284 } // if
285 }
286
287 void Resolver::visit( AsmStmt *asmStmt ) {
288 acceptAll( asmStmt->get_input(), *this);
289 acceptAll( asmStmt->get_output(), *this);
290 }
291
292 void Resolver::visit( IfStmt *ifStmt ) {
293 Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this );
294 delete ifStmt->get_condition();
295 ifStmt->set_condition( newExpr );
296 Parent::visit( ifStmt );
297 }
298
299 void Resolver::visit( WhileStmt *whileStmt ) {
300 Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this );
301 delete whileStmt->get_condition();
302 whileStmt->set_condition( newExpr );
303 Parent::visit( whileStmt );
304 }
305
306 void Resolver::visit( ForStmt *forStmt ) {
307 Parent::visit( forStmt );
308
309 if ( forStmt->get_condition() ) {
310 Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this );
311 delete forStmt->get_condition();
312 forStmt->set_condition( newExpr );
313 } // if
314
315 if ( forStmt->get_increment() ) {
316 Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this );
317 delete forStmt->get_increment();
318 forStmt->set_increment( newExpr );
319 } // if
320 }
321
322 void Resolver::visit( SwitchStmt *switchStmt ) {
323 ValueGuard< CurrentObject > oldCurrentObject( currentObject );
324 Expression *newExpr;
325 newExpr = findIntegralExpression( switchStmt->get_condition(), *this );
326 delete switchStmt->get_condition();
327 switchStmt->set_condition( newExpr );
328
329 currentObject = CurrentObject( newExpr->get_result() );
330 Parent::visit( switchStmt );
331 }
332
333 void Resolver::visit( CaseStmt *caseStmt ) {
334 if ( caseStmt->get_condition() ) {
335 std::list< InitAlternative > initAlts = currentObject.getOptions();
336 assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." );
337 CastExpr * castExpr = new CastExpr( caseStmt->get_condition(), initAlts.front().type->clone() );
338 Expression * newExpr = findSingleExpression( castExpr, *this );
339 castExpr = safe_dynamic_cast< CastExpr * >( newExpr );
340 caseStmt->set_condition( castExpr->get_arg() );
341 castExpr->set_arg( nullptr );
342 delete castExpr;
343 }
344 Parent::visit( caseStmt );
345 }
346
347 void Resolver::visit( BranchStmt *branchStmt ) {
348 // must resolve the argument for a computed goto
349 if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
350 if ( Expression * arg = branchStmt->get_computedTarget() ) {
351 VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder
352 PointerType pt( Type::Qualifiers(), v.clone() );
353 CastExpr * castExpr = new CastExpr( arg, pt.clone() );
354 Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression
355 branchStmt->set_target( newExpr );
356 } // if
357 } // if
358 }
359
360 void Resolver::visit( ReturnStmt *returnStmt ) {
361 if ( returnStmt->get_expr() ) {
362 CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() );
363 Expression *newExpr = findSingleExpression( castExpr, *this );
364 delete castExpr;
365 returnStmt->set_expr( newExpr );
366 } // if
367 }
368
369 void Resolver::visit( ThrowStmt *throwStmt ) {
370 if ( throwStmt->get_expr() ) {
371 Expression * wrapped = new CastExpr( throwStmt->get_expr(), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
372 Expression * newExpr = findSingleExpression( wrapped, *this );
373 throwStmt->set_expr( newExpr );
374 }
375 }
376
377 template< typename T >
378 bool isCharType( T t ) {
379 if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
380 return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
381 bt->get_kind() == BasicType::UnsignedChar;
382 }
383 return false;
384 }
385
386 void Resolver::visit( SingleInit *singleInit ) {
387 // resolve initialization using the possibilities as determined by the currentObject cursor
388 UntypedInitExpr * untyped = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() );
389 Expression * newExpr = findSingleExpression( untyped, *this );
390 InitExpr * initExpr = safe_dynamic_cast< InitExpr * >( newExpr );
391
392 // move cursor to the object that is actually initialized
393 currentObject.setNext( initExpr->get_designation() );
394
395 // discard InitExpr wrapper and retain relevant pieces
396 newExpr = initExpr->get_expr();
397 newExpr->set_env( initExpr->get_env() );
398 initExpr->set_expr( nullptr );
399 initExpr->set_env( nullptr );
400 delete initExpr;
401
402 // get the actual object's type (may not exactly match what comes back from the resolver due to conversions)
403 Type * initContext = currentObject.getCurrentType();
404
405 // check if actual object's type is char[]
406 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
407 if ( isCharType( at->get_base() ) ) {
408 // check if the resolved type is char *
409 if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) {
410 if ( isCharType( pt->get_base() ) ) {
411 // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello";
412 CastExpr *ce = safe_dynamic_cast< CastExpr * >( newExpr );
413 newExpr = ce->get_arg();
414 ce->set_arg( nullptr );
415 delete ce;
416 }
417 }
418 }
419 }
420
421 // set initializer expr to resolved express
422 singleInit->set_value( newExpr );
423
424 // move cursor to next object in preparation for next initializer
425 currentObject.increment();
426 }
427
428 void Resolver::visit( ListInit * listInit ) {
429 // move cursor into brace-enclosed initializer-list
430 currentObject.enterListInit();
431 // xxx - fix this so that the list isn't copied, iterator should be used to change current element
432 std::list<Designation *> newDesignations;
433 for ( auto p : group_iterate(listInit->get_designations(), listInit->get_initializers()) ) {
434 // iterate designations and initializers in pairs, moving the cursor to the current designated object and resolving
435 // the initializer against that object.
436 Designation * des = std::get<0>(p);
437 Initializer * init = std::get<1>(p);
438 newDesignations.push_back( currentObject.findNext( des ) );
439 init->accept( *this );
440 }
441 // set the set of 'resolved' designations and leave the brace-enclosed initializer-list
442 listInit->get_designations() = newDesignations; // xxx - memory management
443 currentObject.exitListInit();
444
445 // xxx - this part has not be folded into CurrentObject yet
446 // } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) {
447 // Type * base = tt->get_baseType()->get_base();
448 // if ( base ) {
449 // // know the implementation type, so try using that as the initContext
450 // ObjectDecl tmpObj( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, base->clone(), nullptr );
451 // currentObject = &tmpObj;
452 // visit( listInit );
453 // } else {
454 // // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context
455 // Parent::visit( listInit );
456 // }
457 // } else {
458 }
459
460 // ConstructorInit - fall back on C-style initializer
461 void Resolver::fallbackInit( ConstructorInit * ctorInit ) {
462 // could not find valid constructor, or found an intrinsic constructor
463 // fall back on C-style initializer
464 delete ctorInit->get_ctor();
465 ctorInit->set_ctor( NULL );
466 delete ctorInit->get_dtor();
467 ctorInit->set_dtor( NULL );
468 maybeAccept( ctorInit->get_init(), *this );
469 }
470
471 // needs to be callable from outside the resolver, so this is a standalone function
472 void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) {
473 assert( ctorInit );
474 Resolver resolver( indexer );
475 ctorInit->accept( resolver );
476 }
477
478 void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) {
479 assert( stmtExpr );
480 Resolver resolver( indexer );
481 stmtExpr->accept( resolver );
482 }
483
484 void Resolver::visit( ConstructorInit *ctorInit ) {
485 // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit
486 maybeAccept( ctorInit->get_ctor(), *this );
487 maybeAccept( ctorInit->get_dtor(), *this );
488
489 // found a constructor - can get rid of C-style initializer
490 delete ctorInit->get_init();
491 ctorInit->set_init( NULL );
492
493 // intrinsic single parameter constructors and destructors do nothing. Since this was
494 // implicitly generated, there's no way for it to have side effects, so get rid of it
495 // to clean up generated code.
496 if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) {
497 delete ctorInit->get_ctor();
498 ctorInit->set_ctor( NULL );
499 }
500
501 if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) {
502 delete ctorInit->get_dtor();
503 ctorInit->set_dtor( NULL );
504 }
505
506 // xxx - todo -- what about arrays?
507 // if ( dtor == NULL && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) {
508 // // can reduce the constructor down to a SingleInit using the
509 // // second argument from the ctor call, since
510 // delete ctorInit->get_ctor();
511 // ctorInit->set_ctor( NULL );
512
513 // Expression * arg =
514 // ctorInit->set_init( new SingleInit( arg ) );
515 // }
516 }
517} // namespace ResolvExpr
518
519// Local Variables: //
520// tab-width: 4 //
521// mode: c++ //
522// compile-command: "make install" //
523// End: //
Note: See TracBrowser for help on using the repository browser.