source: src/ResolvExpr/Resolver.cc@ 32b018e

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 32b018e was 62e5546, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Removed warnings when compiling with clang

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