source: src/ResolvExpr/Resolver.cc@ 7cddf77

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 7cddf77 was 8b11840, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Cleanup pass through several files

  • Property mode set to 100644
File size: 20.0 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 strict_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/PassVisitor.h" // for PassVisitor
24#include "Common/SemanticError.h" // for SemanticError
25#include "Common/utility.h" // for ValueGuard, group_iterate
26#include "CurrentObject.h" // for CurrentObject
27#include "InitTweak/InitTweak.h" // for isIntrinsicSingleArgCallStmt
28#include "RenameVars.h" // for RenameVars, global_renamer
29#include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment
30#include "ResolveTypeof.h" // for resolveTypeof
31#include "Resolver.h"
32#include "SymTab/Autogen.h" // for SizeType
33#include "SymTab/Indexer.h" // for Indexer
34#include "SynTree/Declaration.h" // for ObjectDecl, TypeDecl, Declar...
35#include "SynTree/Expression.h" // for Expression, CastExpr, InitExpr
36#include "SynTree/Initializer.h" // for ConstructorInit, SingleInit
37#include "SynTree/Statement.h" // for ForStmt, Statement, BranchStmt
38#include "SynTree/Type.h" // for Type, BasicType, PointerType
39#include "SynTree/TypeSubstitution.h" // for TypeSubstitution
40#include "SynTree/Visitor.h" // for acceptAll, maybeAccept
41#include "typeops.h" // for extractResultType
42
43using namespace std;
44
45namespace ResolvExpr {
46 struct Resolver final : public WithIndexer, public WithGuards, public WithVisitorRef<Resolver>, public WithShortCircuiting {
47 Resolver() {}
48 Resolver( const SymTab::Indexer & other ) {
49 indexer = other;
50 }
51
52 void previsit( FunctionDecl *functionDecl );
53 void postvisit( FunctionDecl *functionDecl );
54 void previsit( ObjectDecl *functionDecl );
55 void previsit( TypeDecl *typeDecl );
56 void previsit( EnumDecl * enumDecl );
57
58 void previsit( ArrayType * at );
59 void previsit( PointerType * at );
60
61 void previsit( ExprStmt *exprStmt );
62 void previsit( AsmExpr *asmExpr );
63 void previsit( AsmStmt *asmStmt );
64 void previsit( IfStmt *ifStmt );
65 void previsit( WhileStmt *whileStmt );
66 void previsit( ForStmt *forStmt );
67 void previsit( SwitchStmt *switchStmt );
68 void previsit( CaseStmt *caseStmt );
69 void previsit( BranchStmt *branchStmt );
70 void previsit( ReturnStmt *returnStmt );
71 void previsit( ThrowStmt *throwStmt );
72 void previsit( CatchStmt *catchStmt );
73
74 void previsit( SingleInit *singleInit );
75 void previsit( ListInit *listInit );
76 void previsit( ConstructorInit *ctorInit );
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 PassVisitor<Resolver> resolver;
94 acceptAll( translationUnit, resolver );
95 }
96
97 void resolveDecl( Declaration * decl, const SymTab::Indexer &indexer ) {
98 PassVisitor<Resolver> resolver( indexer );
99 maybeAccept( decl, resolver );
100 }
101
102 // used in resolveTypeof
103 Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
104 TypeEnvironment env;
105 return resolveInVoidContext( expr, indexer, env );
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::previsit( ObjectDecl *objectDecl ) {
193 Type *new_type = resolveTypeof( objectDecl->get_type(), indexer );
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 GuardValue( 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 }
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, indexer );
214 delete type->get_dimension();
215 type->set_dimension( newExpr );
216 }
217 }
218
219 void Resolver::previsit( ArrayType * at ) {
220 handlePtrType( at );
221 }
222
223 void Resolver::previsit( PointerType * pt ) {
224 handlePtrType( pt );
225 }
226
227 void Resolver::previsit( TypeDecl *typeDecl ) {
228 if ( typeDecl->get_base() ) {
229 Type *new_type = resolveTypeof( typeDecl->get_base(), indexer );
230 typeDecl->set_base( new_type );
231 } // if
232 }
233
234 void Resolver::previsit( FunctionDecl *functionDecl ) {
235#if 0
236 std::cerr << "resolver visiting functiondecl ";
237 functionDecl->print( std::cerr );
238 std::cerr << std::endl;
239#endif
240 Type *new_type = resolveTypeof( functionDecl->get_type(), indexer );
241 functionDecl->set_type( new_type );
242 GuardValue( functionReturn );
243 functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() );
244 }
245
246
247 void Resolver::postvisit( FunctionDecl *functionDecl ) {
248 // default value expressions have an environment which shouldn't be there and trips up later passes.
249 // xxx - it might be necessary to somehow keep the information from this environment, but I can't currently
250 // see how it's useful.
251 for ( Declaration * d : functionDecl->get_functionType()->get_parameters() ) {
252 if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( d ) ) {
253 if ( SingleInit * init = dynamic_cast< SingleInit * >( obj->get_init() ) ) {
254 delete init->get_value()->get_env();
255 init->get_value()->set_env( nullptr );
256 }
257 }
258 }
259 }
260
261 void Resolver::previsit( EnumDecl * ) {
262 // in case we decide to allow nested enums
263 GuardValue( inEnumDecl );
264 inEnumDecl = true;
265 }
266
267 void Resolver::previsit( ExprStmt *exprStmt ) {
268 visit_children = false;
269 assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" );
270 Expression *newExpr = findVoidExpression( exprStmt->get_expr(), indexer );
271 delete exprStmt->get_expr();
272 exprStmt->set_expr( newExpr );
273 }
274
275 void Resolver::previsit( AsmExpr *asmExpr ) {
276 visit_children = false;
277 Expression *newExpr = findVoidExpression( asmExpr->get_operand(), indexer );
278 delete asmExpr->get_operand();
279 asmExpr->set_operand( newExpr );
280 if ( asmExpr->get_inout() ) {
281 newExpr = findVoidExpression( asmExpr->get_inout(), indexer );
282 delete asmExpr->get_inout();
283 asmExpr->set_inout( newExpr );
284 } // if
285 }
286
287 void Resolver::previsit( AsmStmt *asmStmt ) {
288 visit_children = false;
289 acceptAll( asmStmt->get_input(), *visitor );
290 acceptAll( asmStmt->get_output(), *visitor );
291 }
292
293 void Resolver::previsit( IfStmt *ifStmt ) {
294 Expression *newExpr = findSingleExpression( ifStmt->get_condition(), indexer );
295 delete ifStmt->get_condition();
296 ifStmt->set_condition( newExpr );
297 }
298
299 void Resolver::previsit( WhileStmt *whileStmt ) {
300 Expression *newExpr = findSingleExpression( whileStmt->get_condition(), indexer );
301 delete whileStmt->get_condition();
302 whileStmt->set_condition( newExpr );
303 }
304
305 void Resolver::previsit( ForStmt *forStmt ) {
306 if ( forStmt->get_condition() ) {
307 Expression * newExpr = findSingleExpression( forStmt->get_condition(), indexer );
308 delete forStmt->get_condition();
309 forStmt->set_condition( newExpr );
310 } // if
311
312 if ( forStmt->get_increment() ) {
313 Expression * newExpr = findVoidExpression( forStmt->get_increment(), indexer );
314 delete forStmt->get_increment();
315 forStmt->set_increment( newExpr );
316 } // if
317 }
318
319 void Resolver::previsit( SwitchStmt *switchStmt ) {
320 GuardValue( currentObject );
321 Expression *newExpr;
322 newExpr = findIntegralExpression( switchStmt->get_condition(), indexer );
323 delete switchStmt->get_condition();
324 switchStmt->set_condition( newExpr );
325
326 currentObject = CurrentObject( newExpr->get_result() );
327 }
328
329 void Resolver::previsit( CaseStmt *caseStmt ) {
330 if ( caseStmt->get_condition() ) {
331 std::list< InitAlternative > initAlts = currentObject.getOptions();
332 assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." );
333 CastExpr * castExpr = new CastExpr( caseStmt->get_condition(), initAlts.front().type->clone() );
334 Expression * newExpr = findSingleExpression( castExpr, indexer );
335 castExpr = strict_dynamic_cast< CastExpr * >( newExpr );
336 caseStmt->set_condition( castExpr->get_arg() );
337 castExpr->set_arg( nullptr );
338 delete castExpr;
339 }
340 }
341
342 void Resolver::previsit( BranchStmt *branchStmt ) {
343 visit_children = false;
344 // must resolve the argument for a computed goto
345 if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
346 if ( Expression * arg = branchStmt->get_computedTarget() ) {
347 VoidType v = Type::Qualifiers(); // cast to void * for the alternative finder
348 PointerType pt( Type::Qualifiers(), v.clone() );
349 CastExpr * castExpr = new CastExpr( arg, pt.clone() );
350 Expression * newExpr = findSingleExpression( castExpr, indexer ); // find best expression
351 branchStmt->set_target( newExpr );
352 } // if
353 } // if
354 }
355
356 void Resolver::previsit( ReturnStmt *returnStmt ) {
357 visit_children = false;
358 if ( returnStmt->get_expr() ) {
359 CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() );
360 Expression *newExpr = findSingleExpression( castExpr, indexer );
361 delete castExpr;
362 returnStmt->set_expr( newExpr );
363 } // if
364 }
365
366 void Resolver::previsit( ThrowStmt *throwStmt ) {
367 visit_children = false;
368 // TODO: Replace *exception type with &exception type.
369 if ( throwStmt->get_expr() ) {
370 StructDecl * exception_decl =
371 indexer.lookupStruct( "__cfaehm__base_exception_t" );
372 assert( exception_decl );
373 Expression * wrapped = new CastExpr(
374 throwStmt->get_expr(),
375 new PointerType(
376 noQualifiers,
377 new StructInstType(
378 noQualifiers,
379 exception_decl
380 )
381 )
382 );
383 Expression * newExpr = findSingleExpression( wrapped, indexer );
384 throwStmt->set_expr( newExpr );
385 }
386 }
387
388 void Resolver::previsit( CatchStmt *catchStmt ) {
389 if ( catchStmt->get_cond() ) {
390 Expression * wrapped = new CastExpr(
391 catchStmt->get_cond(),
392 new BasicType( noQualifiers, BasicType::Bool )
393 );
394 catchStmt->set_cond( findSingleExpression( wrapped, indexer ) );
395 }
396 }
397
398 template< typename T >
399 bool isCharType( T t ) {
400 if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
401 return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
402 bt->get_kind() == BasicType::UnsignedChar;
403 }
404 return false;
405 }
406
407 void Resolver::previsit( SingleInit *singleInit ) {
408 visit_children = false;
409 // resolve initialization using the possibilities as determined by the currentObject cursor
410 UntypedInitExpr * untyped = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() );
411 Expression * newExpr = findSingleExpression( untyped, indexer );
412 InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr );
413
414 // move cursor to the object that is actually initialized
415 currentObject.setNext( initExpr->get_designation() );
416
417 // discard InitExpr wrapper and retain relevant pieces
418 newExpr = initExpr->get_expr();
419 newExpr->set_env( initExpr->get_env() );
420 initExpr->set_expr( nullptr );
421 initExpr->set_env( nullptr );
422 delete initExpr;
423
424 // get the actual object's type (may not exactly match what comes back from the resolver due to conversions)
425 Type * initContext = currentObject.getCurrentType();
426
427 // check if actual object's type is char[]
428 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
429 if ( isCharType( at->get_base() ) ) {
430 // check if the resolved type is char *
431 if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) {
432 if ( isCharType( pt->get_base() ) ) {
433 // strip cast if we're initializing a char[] with a char *, e.g. char x[] = "hello";
434 CastExpr *ce = strict_dynamic_cast< CastExpr * >( newExpr );
435 newExpr = ce->get_arg();
436 ce->set_arg( nullptr );
437 delete ce;
438 }
439 }
440 }
441 }
442
443 // set initializer expr to resolved express
444 singleInit->set_value( newExpr );
445
446 // move cursor to next object in preparation for next initializer
447 currentObject.increment();
448 }
449
450 void Resolver::previsit( ListInit * listInit ) {
451 visit_children = false;
452 // move cursor into brace-enclosed initializer-list
453 currentObject.enterListInit();
454 // xxx - fix this so that the list isn't copied, iterator should be used to change current element
455 std::list<Designation *> newDesignations;
456 for ( auto p : group_iterate(listInit->get_designations(), listInit->get_initializers()) ) {
457 // iterate designations and initializers in pairs, moving the cursor to the current designated object and resolving
458 // the initializer against that object.
459 Designation * des = std::get<0>(p);
460 Initializer * init = std::get<1>(p);
461 newDesignations.push_back( currentObject.findNext( des ) );
462 init->accept( *visitor );
463 }
464 // set the set of 'resolved' designations and leave the brace-enclosed initializer-list
465 listInit->get_designations() = newDesignations; // xxx - memory management
466 currentObject.exitListInit();
467
468 // xxx - this part has not be folded into CurrentObject yet
469 // } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) {
470 // Type * base = tt->get_baseType()->get_base();
471 // if ( base ) {
472 // // know the implementation type, so try using that as the initContext
473 // ObjectDecl tmpObj( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, base->clone(), nullptr );
474 // currentObject = &tmpObj;
475 // visit( listInit );
476 // } else {
477 // // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context
478 // Parent::visit( listInit );
479 // }
480 // } else {
481 }
482
483 // ConstructorInit - fall back on C-style initializer
484 void Resolver::fallbackInit( ConstructorInit * ctorInit ) {
485 // could not find valid constructor, or found an intrinsic constructor
486 // fall back on C-style initializer
487 delete ctorInit->get_ctor();
488 ctorInit->set_ctor( NULL );
489 delete ctorInit->get_dtor();
490 ctorInit->set_dtor( NULL );
491 maybeAccept( ctorInit->get_init(), *visitor );
492 }
493
494 // needs to be callable from outside the resolver, so this is a standalone function
495 void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) {
496 assert( ctorInit );
497 PassVisitor<Resolver> resolver( indexer );
498 ctorInit->accept( resolver );
499 }
500
501 void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) {
502 assert( stmtExpr );
503 PassVisitor<Resolver> resolver( indexer );
504 stmtExpr->accept( resolver );
505 }
506
507 void Resolver::previsit( ConstructorInit *ctorInit ) {
508 visit_children = false;
509 // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit
510 maybeAccept( ctorInit->get_ctor(), *visitor );
511 maybeAccept( ctorInit->get_dtor(), *visitor );
512
513 // found a constructor - can get rid of C-style initializer
514 delete ctorInit->get_init();
515 ctorInit->set_init( NULL );
516
517 // intrinsic single parameter constructors and destructors do nothing. Since this was
518 // implicitly generated, there's no way for it to have side effects, so get rid of it
519 // to clean up generated code.
520 if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) {
521 delete ctorInit->get_ctor();
522 ctorInit->set_ctor( NULL );
523 }
524
525 if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) {
526 delete ctorInit->get_dtor();
527 ctorInit->set_dtor( NULL );
528 }
529
530 // xxx - todo -- what about arrays?
531 // if ( dtor == NULL && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) {
532 // // can reduce the constructor down to a SingleInit using the
533 // // second argument from the ctor call, since
534 // delete ctorInit->get_ctor();
535 // ctorInit->set_ctor( NULL );
536
537 // Expression * arg =
538 // ctorInit->set_init( new SingleInit( arg ) );
539 // }
540 }
541} // namespace ResolvExpr
542
543// Local Variables: //
544// tab-width: 4 //
545// mode: c++ //
546// compile-command: "make install" //
547// End: //
Note: See TracBrowser for help on using the repository browser.