source: src/ResolvExpr/Resolver.cc @ 3cfe27f

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 3cfe27f was 3cfe27f, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

fix initialization of routine pointers, fix one random routine

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