source: src/ResolvExpr/Resolver.cc @ 94e0864d

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

Merge branch 'master' into resolver

Conflicts:

src/CodeGen/CodeGenerator.cc
src/Parser/ExpressionNode.cc
src/ResolvExpr/Resolver.cc

  • Property mode set to 100644
File size: 15.9 KB
RevLine 
[a32b204]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
[b5c5684]11// Last Modified By : Rob Schluntz
[94e0864d]12// Last Modified On : Wed Jun 24 16:08:49 2015
13// Update Count     : 155
[a32b204]14//
15
[51b7345]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 "utility.h"
27
[d9a0e76]28#include <iostream>
29using namespace std;
[51b7345]30
[d9a0e76]31namespace ResolvExpr {
[a32b204]32        class Resolver : public SymTab::Indexer {
33          public:
34                Resolver() : SymTab::Indexer( false ), switchType( 0 ) {}
[51b7345]35 
[a32b204]36                virtual void visit( FunctionDecl *functionDecl );
37                virtual void visit( ObjectDecl *functionDecl );
38                virtual void visit( TypeDecl *typeDecl );
[94b4364]39
[bfbf97f]40                virtual void visit( ArrayType * at );
[d9a0e76]41
[a32b204]42                virtual void visit( ExprStmt *exprStmt );
43                virtual void visit( IfStmt *ifStmt );
44                virtual void visit( WhileStmt *whileStmt );
45                virtual void visit( ForStmt *forStmt );
46                virtual void visit( SwitchStmt *switchStmt );
47                virtual void visit( ChooseStmt *switchStmt );
48                virtual void visit( CaseStmt *caseStmt );
[de62360d]49                virtual void visit( BranchStmt *branchStmt );
[a32b204]50                virtual void visit( ReturnStmt *returnStmt );
[d9a0e76]51
[a32b204]52                virtual void visit( SingleInit *singleInit );
53                virtual void visit( ListInit *listInit );
54          private:
[94b4364]55        typedef std::list< Initializer * >::iterator InitIterator;
56
57          void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & );
58          void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & );
59
[a32b204]60                std::list< Type * > functionReturn;
61                Type *initContext;
62                Type *switchType;
63        };
[d9a0e76]64
[a32b204]65        void resolve( std::list< Declaration * > translationUnit ) {
66                Resolver resolver;
67                acceptAll( translationUnit, resolver );
[d9a0e76]68#if 0
[a32b204]69                resolver.print( cerr );
70                for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) {
71                        (*i)->print( std::cerr );
72                        (*i)->accept( resolver );
73                } // for
[d9a0e76]74#endif
75        }
76
[a32b204]77        Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
78                TypeEnvironment env;
79                return resolveInVoidContext( expr, indexer, env );
[d9a0e76]80        }
[a32b204]81
82        namespace {
83                void finishExpr( Expression *expr, const TypeEnvironment &env ) {
84                        expr->set_env( new TypeSubstitution );
85                        env.makeSubstitution( *expr->get_env() );
86                }
87
88                Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
89                        global_renamer.reset();
90                        TypeEnvironment env;
91                        Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
92                        finishExpr( newExpr, env );
93                        return newExpr;
94                }
[51b7345]95 
[a32b204]96                Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
97                        TypeEnvironment env;
98                        AlternativeFinder finder( indexer, env );
99                        finder.find( untyped );
[d9a0e76]100#if 0
[a32b204]101                        if ( finder.get_alternatives().size() != 1 ) {
102                                std::cout << "untyped expr is ";
103                                untyped->print( std::cout );
104                                std::cout << std::endl << "alternatives are:";
105                                for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
106                                        i->print( std::cout );
107                                } // for
108                        } // if
[d9a0e76]109#endif
[a32b204]110                        assert( finder.get_alternatives().size() == 1 );
111                        Alternative &choice = finder.get_alternatives().front();
112                        Expression *newExpr = choice.expr->clone();
113                        finishExpr( newExpr, choice.env );
114                        return newExpr;
115                }
[d9a0e76]116
[a32b204]117                bool isIntegralType( Type *type ) {
118                        if ( dynamic_cast< EnumInstType * >( type ) ) {
119                                return true;
120                        } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) {
121                                return bt->isInteger();
122                        } else {
123                                return false;
124                        } // if
125                }
[51b7345]126 
[a32b204]127                Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
128                        TypeEnvironment env;
129                        AlternativeFinder finder( indexer, env );
130                        finder.find( untyped );
[d9a0e76]131#if 0
[a32b204]132                        if ( finder.get_alternatives().size() != 1 ) {
133                                std::cout << "untyped expr is ";
134                                untyped->print( std::cout );
135                                std::cout << std::endl << "alternatives are:";
136                                for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
137                                        i->print( std::cout );
138                                } // for
139                        } // if
[d9a0e76]140#endif
[a32b204]141                        Expression *newExpr = 0;
142                        const TypeEnvironment *newEnv = 0;
143                        for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
144                                if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) {
145                                        if ( newExpr ) {
146                                                throw SemanticError( "Too many interpretations for case control expression", untyped );
147                                        } else {
148                                                newExpr = i->expr->clone();
149                                                newEnv = &i->env;
150                                        } // if
151                                } // if
152                        } // for
153                        if ( ! newExpr ) {
154                                throw SemanticError( "No interpretations for case control expression", untyped );
155                        } // if
156                        finishExpr( newExpr, *newEnv );
157                        return newExpr;
158                }
[51b7345]159 
[a32b204]160        }
[51b7345]161 
[a32b204]162        void Resolver::visit( ObjectDecl *objectDecl ) {
163                Type *new_type = resolveTypeof( objectDecl->get_type(), *this );
164                objectDecl->set_type( new_type );
165                initContext = new_type;
166                SymTab::Indexer::visit( objectDecl );
[bfbf97f]167        }
168
169        void Resolver::visit( ArrayType * at ) {
170                if ( at->get_dimension() ) {
171                        BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
172                        CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() );
173                        Expression *newExpr = findSingleExpression( castExpr, *this );
174                        delete at->get_dimension();
175                        at->set_dimension( newExpr );
[d1d17f5]176                }
[bfbf97f]177                Visitor::visit( at );
[a32b204]178        }
[94b4364]179
[a32b204]180        void Resolver::visit( TypeDecl *typeDecl ) {
181                if ( typeDecl->get_base() ) {
182                        Type *new_type = resolveTypeof( typeDecl->get_base(), *this );
183                        typeDecl->set_base( new_type );
184                } // if
185                SymTab::Indexer::visit( typeDecl );
186        }
[94b4364]187
[a32b204]188        void Resolver::visit( FunctionDecl *functionDecl ) {
[d9a0e76]189#if 0
[a32b204]190                std::cout << "resolver visiting functiondecl ";
191                functionDecl->print( std::cout );
192                std::cout << std::endl;
[d9a0e76]193#endif
[a32b204]194                Type *new_type = resolveTypeof( functionDecl->get_type(), *this );
195                functionDecl->set_type( new_type );
196                std::list< Type * > oldFunctionReturn = functionReturn;
197                functionReturn.clear();
198                for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) {
199                        functionReturn.push_back( (*i)->get_type() );
200                } // for
201                SymTab::Indexer::visit( functionDecl );
202                functionReturn = oldFunctionReturn;
203        }
[51b7345]204
[a32b204]205        void Resolver::visit( ExprStmt *exprStmt ) {
206                if ( exprStmt->get_expr() ) {
207                        Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this );
208                        delete exprStmt->get_expr();
209                        exprStmt->set_expr( newExpr );
210                } // if
211        }
[51b7345]212
[a32b204]213        void Resolver::visit( IfStmt *ifStmt ) {
214                Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this );
215                delete ifStmt->get_condition();
216                ifStmt->set_condition( newExpr );
217                Visitor::visit( ifStmt );
218        }
[51b7345]219
[a32b204]220        void Resolver::visit( WhileStmt *whileStmt ) {
221                Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this );
222                delete whileStmt->get_condition();
223                whileStmt->set_condition( newExpr );
224                Visitor::visit( whileStmt );
225        }
[51b7345]226
[a32b204]227        void Resolver::visit( ForStmt *forStmt ) {
228            // SymTab::Indexer::visit( forStmt );
229                Expression *newExpr;
230            // for statements introduce a level of scope
231            enterScope();
232            maybeAccept( forStmt->get_initialization(), *this );
233                if ( forStmt->get_condition() ) {
234                        newExpr = findSingleExpression( forStmt->get_condition(), *this );
235                        delete forStmt->get_condition();
236                        forStmt->set_condition( newExpr );
237                } // if
[51b7345]238 
[a32b204]239                if ( forStmt->get_increment() ) {
240                        newExpr = findVoidExpression( forStmt->get_increment(), *this );
241                        delete forStmt->get_increment();
242                        forStmt->set_increment( newExpr );
243                } // if
[b1a6d6b]244
[a32b204]245            maybeAccept( forStmt->get_condition(), *this );
246            maybeAccept( forStmt->get_increment(), *this );
247            maybeAccept( forStmt->get_body(), *this );
248            leaveScope();
249        }
[51b7345]250
[a32b204]251        template< typename SwitchClass >
252        void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) {
253                Expression *newExpr;
254                newExpr = findIntegralExpression( switchStmt->get_condition(), visitor );
255                delete switchStmt->get_condition();
256                switchStmt->set_condition( newExpr );
[51b7345]257 
[a32b204]258                visitor.Visitor::visit( switchStmt );
259        }
[51b7345]260
[a32b204]261        void Resolver::visit( SwitchStmt *switchStmt ) {
262                handleSwitchStmt( switchStmt, *this );
263        }
[51b7345]264
[a32b204]265        void Resolver::visit( ChooseStmt *switchStmt ) {
266                handleSwitchStmt( switchStmt, *this );
267        }
[51b7345]268
[a32b204]269        void Resolver::visit( CaseStmt *caseStmt ) {
270                Visitor::visit( caseStmt );
271        }
[51b7345]272
[de62360d]273        void Resolver::visit( BranchStmt *branchStmt ) {
274                // must resolve the argument for a computed goto
275                if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
276                        if ( NameExpr * arg = dynamic_cast< NameExpr * >( branchStmt->get_computedTarget() ) ) {
277                                VoidType v = Type::Qualifiers();                // cast to void * for the alternative finder
278                                PointerType pt( Type::Qualifiers(), v.clone() );
279                                CastExpr * castExpr = new CastExpr( arg, pt.clone() );
280                                Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression
281                                branchStmt->set_target( newExpr );
282                        } // if
283                } // if
284        }
285
[a32b204]286        void Resolver::visit( ReturnStmt *returnStmt ) {
287                if ( returnStmt->get_expr() ) {
288                        CastExpr *castExpr = new CastExpr( returnStmt->get_expr() );
289                        cloneAll( functionReturn, castExpr->get_results() );
290                        Expression *newExpr = findSingleExpression( castExpr, *this );
291                        delete castExpr;
292                        returnStmt->set_expr( newExpr );
293                } // if
294        }
[51b7345]295
[b5c5684]296        template< typename T >
297        bool isCharType( T t ) {
298                if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
299                        return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || 
300                                bt->get_kind() == BasicType::UnsignedChar;
301                }
302                return false;
303        }
304
[a32b204]305        void Resolver::visit( SingleInit *singleInit ) {
306                if ( singleInit->get_value() ) {
[bdd516a]307#if 0
[a32b204]308                        if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) {
309                                string n = ne->get_name();
310                                if (n == "0") {
311                                        initContext = new BasicType(Type::Qualifiers(),
312                                                                                                BasicType::SignedInt);
313                                } else {
314                                        DeclarationWithType * decl = lookupId(n);
315                                        initContext = decl->get_type();
316                                }
317                        } else if (ConstantExpr * e =
318                                           dynamic_cast<ConstantExpr*>(singleInit->get_value())) {
319                                Constant *c = e->get_constant();
320                                initContext = c->get_type();
321                        } else {
322                                assert(0);
323                        }
[bdd516a]324#endif
[a32b204]325                        CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() );
326                        Expression *newExpr = findSingleExpression( castExpr, *this );
327                        delete castExpr;
328                        singleInit->set_value( newExpr );
[b5c5684]329
330                        // check if initializing type is char[]
331                        if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
332                                if ( isCharType( at->get_base() ) ) {
333                                        // check if the resolved type is char *
334                                        if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) {
335                                                if ( isCharType( pt->get_base() ) ) {
336                                                        // strip cast if we're initializing a char[] with a char *, e.g.
337                                                        // char x[] = "hello";
338                                                        CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );
339                                                        singleInit->set_value( ce->get_arg() );
340                                                        ce->set_arg( NULL );
341                                                        delete ce;                                                                     
342                                                }
343                                        }
344                                }
345                        }
[a32b204]346                } // if
[6c3744e]347//      singleInit->get_value()->accept( *this );
[a32b204]348        }
[51b7345]349
[94b4364]350        void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) {
351                DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl );
352                assert( dt );
353                initContext = dt->get_type();
354                try {
355                        if ( init == initEnd ) return; // stop when there are no more initializers
356                        (*init)->accept( *this );
357                        ++init; // made it past an initializer
358                } catch( SemanticError & ) {
359                        // need to delve deeper, if you can
360                        if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) {
361                                resolveAggrInit( sit->get_baseStruct(), init, initEnd );
362                        } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) {
363                                resolveAggrInit( uit->get_baseUnion(), init, initEnd );
364                        } else {
365                                // might need to rethink what is being thrown
366                                throw;
367                        } // if
368                }
369        }
370
371        void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) {
372                if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) {
373                        // want to resolve each initializer to the members of the struct,
374                        // but if there are more initializers than members we should stop
375                        list< Declaration * >::iterator it = st->get_members().begin();
376                        for ( ; it != st->get_members().end(); ++it) {
377                                resolveSingleAggrInit( *it, init, initEnd );
378                        }
379                } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) {
380                        // only resolve to the first member of a union
381                        resolveSingleAggrInit( *un->get_members().begin(), init, initEnd );
382                } // if
383        }
384
385        void Resolver::visit( ListInit * listInit ) {
386                InitIterator iter = listInit->begin_initializers();
387                InitIterator end = listInit->end_initializers();
388
[b5c5684]389                if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
[94b4364]390                        // resolve each member to the base type of the array
391                        for ( ; iter != end; ++iter ) {
[b5c5684]392                                initContext = at->get_base();
393                                (*iter)->accept( *this );
394                        } // for
[94b4364]395                } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) {
396                        resolveAggrInit( st->get_baseStruct(), iter, end );
[b5c5684]397                } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) {
[94b4364]398                        resolveAggrInit( st->get_baseUnion(), iter, end );
[b5c5684]399                } else {
400                        // basic types are handled here
401                        Visitor::visit( listInit );
402                }
403
[bdd516a]404#if 0
[a32b204]405                if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) {
406                        std::list<Initializer *>::iterator iter( listInit->begin_initializers() );
407                        for ( ; iter != listInit->end_initializers(); ++iter ) {
408                                initContext = at->get_base();
409                                (*iter)->accept( *this );
410                        } // for
411                } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) {
412                        StructDecl *baseStruct = st->get_baseStruct();
413                        std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() );
414                        std::list<Initializer *>::iterator iter2( listInit->begin_initializers() );
415                        for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) {
416                                if ( (*iter2)->get_designators().empty() ) {
417                                        DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 );
418                                        initContext = dt->get_type();
419                                        (*iter2)->accept( *this );
420                                        ++iter1;
421                                } else {
422                                        StructDecl *st = baseStruct;
423                                        iter1 = st->get_members().begin();
424                                        std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() );
425                                        for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) {
426                                                NameExpr *key = dynamic_cast<NameExpr *>( *iter3 );
427                                                assert( key );
428                                                for ( ; iter1 != st->get_members().end(); ++iter1 ) {
429                                                        if ( key->get_name() == (*iter1)->get_name() ) {
430                                                                (*iter1)->print( cout );
431                                                                cout << key->get_name() << endl;
432                                                                ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
433                                                                assert( fred );
434                                                                StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() );
435                                                                assert( mary );
436                                                                st = mary->get_baseStruct();
437                                                                iter1 = st->get_members().begin();
438                                                                break;
439                                                        } // if
440                                                }  // for
441                                        } // for
442                                        ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
443                                        assert( fred );
444                                        initContext = fred->get_type();
445                                        (*listInit->begin_initializers())->accept( *this );
446                                } // if
447                        } // for
448                } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) {
449                        DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() );
450                        initContext = dt->get_type();
451                        (*listInit->begin_initializers())->accept( *this );
[2c2242c]452                } // if
[bdd516a]453#endif
[a32b204]454        }
[51b7345]455} // namespace ResolvExpr
[a32b204]456
457// Local Variables: //
458// tab-width: 4 //
459// mode: c++ //
460// compile-command: "make install" //
461// End: //
Note: See TracBrowser for help on using the repository browser.