source: src/ResolvExpr/Resolver.cc @ 145f1fc

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 145f1fc was 145f1fc, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

modified ForStmt? to have a list of statements for the initialization portion, and reverted to hoisting the initialization

  • Property mode set to 100644
File size: 15.6 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 : Wed Jul 15 14:54:04 2015
13// Update Count     : 167
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 "utility.h"
27
28#include <iostream>
29using namespace std;
30
31namespace ResolvExpr {
32        class Resolver : public SymTab::Indexer {
33          public:
34                Resolver() : SymTab::Indexer( false ), switchType( 0 ) {}
35 
36                virtual void visit( FunctionDecl *functionDecl );
37                virtual void visit( ObjectDecl *functionDecl );
38                virtual void visit( TypeDecl *typeDecl );
39
40                virtual void visit( ArrayType * at );
41
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 );
49                virtual void visit( BranchStmt *branchStmt );
50                virtual void visit( ReturnStmt *returnStmt );
51
52                virtual void visit( SingleInit *singleInit );
53                virtual void visit( ListInit *listInit );
54          private:
55        typedef std::list< Initializer * >::iterator InitIterator;
56
57          void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & );
58          void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & );
59
60                std::list< Type * > functionReturn;
61                Type *initContext;
62                Type *switchType;
63        };
64
65        void resolve( std::list< Declaration * > translationUnit ) {
66                Resolver resolver;
67                acceptAll( translationUnit, resolver );
68#if 0
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
74#endif
75        }
76
77        Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
78                TypeEnvironment env;
79                return resolveInVoidContext( expr, indexer, env );
80        }
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                }
95 
96                Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
97                        TypeEnvironment env;
98                        AlternativeFinder finder( indexer, env );
99                        finder.find( untyped );
100#if 0
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
109#endif
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                }
116
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                }
126 
127                Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
128                        TypeEnvironment env;
129                        AlternativeFinder finder( indexer, env );
130                        finder.find( untyped );
131#if 0
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
140#endif
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                }
159 
160        }
161 
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 );
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 );
176                }
177                Visitor::visit( at );
178        }
179
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        }
187
188        void Resolver::visit( FunctionDecl *functionDecl ) {
189#if 0
190                std::cout << "resolver visiting functiondecl ";
191                functionDecl->print( std::cout );
192                std::cout << std::endl;
193#endif
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        }
204
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        }
212
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        }
219
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        }
226
227        void Resolver::visit( ForStmt *forStmt ) {
228                SymTab::Indexer::visit( forStmt );
229
230                if ( forStmt->get_condition() ) {
231                        Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this );
232                        delete forStmt->get_condition();
233                        forStmt->set_condition( newExpr );
234                } // if
235               
236                if ( forStmt->get_increment() ) {
237                        Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this );
238                        delete forStmt->get_increment();
239                        forStmt->set_increment( newExpr );
240                } // if
241        }
242
243        template< typename SwitchClass >
244        void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) {
245                Expression *newExpr;
246                newExpr = findIntegralExpression( switchStmt->get_condition(), visitor );
247                delete switchStmt->get_condition();
248                switchStmt->set_condition( newExpr );
249 
250                visitor.Visitor::visit( switchStmt );
251        }
252
253        void Resolver::visit( SwitchStmt *switchStmt ) {
254                handleSwitchStmt( switchStmt, *this );
255        }
256
257        void Resolver::visit( ChooseStmt *switchStmt ) {
258                handleSwitchStmt( switchStmt, *this );
259        }
260
261        void Resolver::visit( CaseStmt *caseStmt ) {
262                Visitor::visit( caseStmt );
263        }
264
265        void Resolver::visit( BranchStmt *branchStmt ) {
266                // must resolve the argument for a computed goto
267                if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
268                        if ( Expression * arg = branchStmt->get_computedTarget() ) {
269                                VoidType v = Type::Qualifiers();                // cast to void * for the alternative finder
270                                PointerType pt( Type::Qualifiers(), v.clone() );
271                                CastExpr * castExpr = new CastExpr( arg, pt.clone() );
272                                Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression
273                                branchStmt->set_target( newExpr );
274                        } // if
275                } // if
276        }
277
278        void Resolver::visit( ReturnStmt *returnStmt ) {
279                if ( returnStmt->get_expr() ) {
280                        CastExpr *castExpr = new CastExpr( returnStmt->get_expr() );
281                        cloneAll( functionReturn, castExpr->get_results() );
282                        Expression *newExpr = findSingleExpression( castExpr, *this );
283                        delete castExpr;
284                        returnStmt->set_expr( newExpr );
285                } // if
286        }
287
288        template< typename T >
289        bool isCharType( T t ) {
290                if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
291                        return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || 
292                                bt->get_kind() == BasicType::UnsignedChar;
293                }
294                return false;
295        }
296
297        void Resolver::visit( SingleInit *singleInit ) {
298                if ( singleInit->get_value() ) {
299#if 0
300                        if (NameExpr * ne = dynamic_cast<NameExpr*>(singleInit->get_value())) {
301                                string n = ne->get_name();
302                                if (n == "0") {
303                                        initContext = new BasicType(Type::Qualifiers(),
304                                                                                                BasicType::SignedInt);
305                                } else {
306                                        DeclarationWithType * decl = lookupId(n);
307                                        initContext = decl->get_type();
308                                }
309                        } else if (ConstantExpr * e =
310                                           dynamic_cast<ConstantExpr*>(singleInit->get_value())) {
311                                Constant *c = e->get_constant();
312                                initContext = c->get_type();
313                        } else {
314                                assert(0);
315                        }
316#endif
317                        CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() );
318                        Expression *newExpr = findSingleExpression( castExpr, *this );
319                        delete castExpr;
320                        singleInit->set_value( newExpr );
321
322                        // check if initializing type is char[]
323                        if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
324                                if ( isCharType( at->get_base() ) ) {
325                                        // check if the resolved type is char *
326                                        if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) {
327                                                if ( isCharType( pt->get_base() ) ) {
328                                                        // strip cast if we're initializing a char[] with a char *, e.g.
329                                                        // char x[] = "hello";
330                                                        CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );
331                                                        singleInit->set_value( ce->get_arg() );
332                                                        ce->set_arg( NULL );
333                                                        delete ce;                                                                     
334                                                }
335                                        }
336                                }
337                        }
338                } // if
339//      singleInit->get_value()->accept( *this );
340        }
341
342        void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) {
343                DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl );
344                assert( dt );
345                initContext = dt->get_type();
346                try {
347                        if ( init == initEnd ) return; // stop when there are no more initializers
348                        (*init)->accept( *this );
349                        ++init; // made it past an initializer
350                } catch( SemanticError & ) {
351                        // need to delve deeper, if you can
352                        if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) {
353                                resolveAggrInit( sit->get_baseStruct(), init, initEnd );
354                        } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) {
355                                resolveAggrInit( uit->get_baseUnion(), init, initEnd );
356                        } else {
357                                // member is not an aggregate type, so can't go any deeper
358
359                                // might need to rethink what is being thrown
360                                throw;
361                        } // if
362                }
363        }
364
365        void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) {
366                if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) {
367                        // want to resolve each initializer to the members of the struct,
368                        // but if there are more initializers than members we should stop
369                        list< Declaration * >::iterator it = st->get_members().begin();
370                        for ( ; it != st->get_members().end(); ++it) {
371                                resolveSingleAggrInit( *it, init, initEnd );
372                        }
373                } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) {
374                        // only resolve to the first member of a union
375                        resolveSingleAggrInit( *un->get_members().begin(), init, initEnd );
376                } // if
377        }
378
379        void Resolver::visit( ListInit * listInit ) {
380                InitIterator iter = listInit->begin_initializers();
381                InitIterator end = listInit->end_initializers();
382
383                if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
384                        // resolve each member to the base type of the array
385                        for ( ; iter != end; ++iter ) {
386                                initContext = at->get_base();
387                                (*iter)->accept( *this );
388                        } // for
389                } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) {
390                        resolveAggrInit( st->get_baseStruct(), iter, end );
391                } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) {
392                        resolveAggrInit( st->get_baseUnion(), iter, end );
393                } else {
394                        // basic types are handled here
395                        Visitor::visit( listInit );
396                }
397
398#if 0
399                if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) {
400                        std::list<Initializer *>::iterator iter( listInit->begin_initializers() );
401                        for ( ; iter != listInit->end_initializers(); ++iter ) {
402                                initContext = at->get_base();
403                                (*iter)->accept( *this );
404                        } // for
405                } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) {
406                        StructDecl *baseStruct = st->get_baseStruct();
407                        std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() );
408                        std::list<Initializer *>::iterator iter2( listInit->begin_initializers() );
409                        for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) {
410                                if ( (*iter2)->get_designators().empty() ) {
411                                        DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 );
412                                        initContext = dt->get_type();
413                                        (*iter2)->accept( *this );
414                                        ++iter1;
415                                } else {
416                                        StructDecl *st = baseStruct;
417                                        iter1 = st->get_members().begin();
418                                        std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() );
419                                        for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) {
420                                                NameExpr *key = dynamic_cast<NameExpr *>( *iter3 );
421                                                assert( key );
422                                                for ( ; iter1 != st->get_members().end(); ++iter1 ) {
423                                                        if ( key->get_name() == (*iter1)->get_name() ) {
424                                                                (*iter1)->print( cout );
425                                                                cout << key->get_name() << endl;
426                                                                ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
427                                                                assert( fred );
428                                                                StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() );
429                                                                assert( mary );
430                                                                st = mary->get_baseStruct();
431                                                                iter1 = st->get_members().begin();
432                                                                break;
433                                                        } // if
434                                                }  // for
435                                        } // for
436                                        ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
437                                        assert( fred );
438                                        initContext = fred->get_type();
439                                        (*listInit->begin_initializers())->accept( *this );
440                                } // if
441                        } // for
442                } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) {
443                        DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() );
444                        initContext = dt->get_type();
445                        (*listInit->begin_initializers())->accept( *this );
446                } // if
447#endif
448        }
449} // namespace ResolvExpr
450
451// Local Variables: //
452// tab-width: 4 //
453// mode: c++ //
454// compile-command: "make install" //
455// End: //
Note: See TracBrowser for help on using the repository browser.