source: src/ResolvExpr/Resolver.cc @ ec79847

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since ec79847 was ec79847, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

don't include global init/destroy functions if they're empty, remove all implicit single parameter intrinsic ctor/dtor calls

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