source: src/ResolvExpr/Resolver.cc@ c6c6f2ae

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 c6c6f2ae was ea6332d, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Big header cleaning pass - commit 3

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