source: src/ResolvExpr/Resolver.cc @ f1e012b

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

added intrinsic ctor/dtors to prelude, modified MakeLibCfa? to build prelude ctor/dtors, added ctor/dtor to polymorphic object type constraints, rudimentary fallback on initializer nodes if chosen ctor is intrinsic, remove intrinsic destructor statements to reduce output pollution

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