source: src/ResolvExpr/Resolver.cc @ 11dbfe1

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 11dbfe1 was 0b150ec, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

minor zero_t/one_t prelude changes, simplify prelude generation, memory error fixes

  • Property mode set to 100644
File size: 22.9 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 23 17:23:14 2017
13// Update Count     : 211
14//
15
16#include "Resolver.h"
17#include "AlternativeFinder.h"
18#include "Alternative.h"
19#include "RenameVars.h"
20#include "ResolveTypeof.h"
21#include "typeops.h"
22#include "SynTree/Statement.h"
23#include "SynTree/Type.h"
24#include "SynTree/Expression.h"
25#include "SynTree/Initializer.h"
26#include "SymTab/Indexer.h"
27#include "SymTab/Autogen.h"
28#include "Common/utility.h"
29#include "InitTweak/InitTweak.h"
30
31#include <iostream>
32using namespace std;
33
34namespace ResolvExpr {
35        class Resolver final : public SymTab::Indexer {
36          public:
37                Resolver() : SymTab::Indexer( false ) {}
38                Resolver( const SymTab:: Indexer & other ) : SymTab::Indexer( other ) {
39                        if ( const Resolver * res = dynamic_cast< const Resolver * >( &other ) ) {
40                                functionReturn = res->functionReturn;
41                                initContext = res->initContext;
42                                inEnumDecl = res->inEnumDecl;
43                        }
44                }
45
46                typedef SymTab::Indexer Parent;
47                using Parent::visit;
48                virtual void visit( FunctionDecl *functionDecl ) override;
49                virtual void visit( ObjectDecl *functionDecl ) override;
50                virtual void visit( TypeDecl *typeDecl ) override;
51                virtual void visit( EnumDecl * enumDecl ) override;
52
53                virtual void visit( ArrayType * at ) override;
54                virtual void visit( PointerType * at ) override;
55
56                virtual void visit( ExprStmt *exprStmt ) override;
57                virtual void visit( AsmExpr *asmExpr ) override;
58                virtual void visit( AsmStmt *asmStmt ) override;
59                virtual void visit( IfStmt *ifStmt ) override;
60                virtual void visit( WhileStmt *whileStmt ) override;
61                virtual void visit( ForStmt *forStmt ) override;
62                virtual void visit( SwitchStmt *switchStmt ) override;
63                virtual void visit( CaseStmt *caseStmt ) override;
64                virtual void visit( BranchStmt *branchStmt ) override;
65                virtual void visit( ReturnStmt *returnStmt ) override;
66
67                virtual void visit( SingleInit *singleInit ) override;
68                virtual void visit( ListInit *listInit ) override;
69                virtual void visit( ConstructorInit *ctorInit ) override;
70          private:
71        typedef std::list< Initializer * >::iterator InitIterator;
72
73                template< typename PtrType >
74                void handlePtrType( PtrType * type );
75
76          void resolveAggrInit( ReferenceToType *, InitIterator &, InitIterator & );
77          void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator &, TypeSubstitution sub );
78          void fallbackInit( ConstructorInit * ctorInit );
79
80                Type * functionReturn = nullptr;
81                Type *initContext = nullptr;
82                bool inEnumDecl = false;
83        };
84
85        void resolve( std::list< Declaration * > translationUnit ) {
86                Resolver resolver;
87                acceptAll( translationUnit, resolver );
88        }
89
90        Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
91                TypeEnvironment env;
92                return resolveInVoidContext( expr, indexer, env );
93        }
94
95
96        namespace {
97                void finishExpr( Expression *expr, const TypeEnvironment &env ) {
98                        expr->set_env( new TypeSubstitution );
99                        env.makeSubstitution( *expr->get_env() );
100                }
101        } // namespace
102
103        Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
104                global_renamer.reset();
105                TypeEnvironment env;
106                Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
107                finishExpr( newExpr, env );
108                return newExpr;
109        }
110
111        namespace {
112                Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
113                        TypeEnvironment env;
114                        AlternativeFinder finder( indexer, env );
115                        finder.find( untyped );
116#if 0
117                        if ( finder.get_alternatives().size() != 1 ) {
118                                std::cout << "untyped expr is ";
119                                untyped->print( std::cout );
120                                std::cout << std::endl << "alternatives are:";
121                                for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
122                                        i->print( std::cout );
123                                } // for
124                        } // if
125#endif
126                        assertf( finder.get_alternatives().size() == 1, "findSingleExpression: must have exactly one alternative at the end." );
127                        Alternative &choice = finder.get_alternatives().front();
128                        Expression *newExpr = choice.expr->clone();
129                        finishExpr( newExpr, choice.env );
130                        return newExpr;
131                }
132
133                bool isIntegralType( Type *type ) {
134                        if ( dynamic_cast< EnumInstType * >( type ) ) {
135                                return true;
136                        } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) {
137                                return bt->isInteger();
138                        } else if ( dynamic_cast< ZeroType* >( type ) != nullptr || dynamic_cast< OneType* >( type ) != nullptr ) {
139                                return true;
140                        } else {
141                                return false;
142                        } // if
143                }
144
145                Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
146                        TypeEnvironment env;
147                        AlternativeFinder finder( indexer, env );
148                        finder.find( untyped );
149#if 0
150                        if ( finder.get_alternatives().size() != 1 ) {
151                                std::cout << "untyped expr is ";
152                                untyped->print( std::cout );
153                                std::cout << std::endl << "alternatives are:";
154                                for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
155                                        i->print( std::cout );
156                                } // for
157                        } // if
158#endif
159                        Expression *newExpr = 0;
160                        const TypeEnvironment *newEnv = 0;
161                        for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
162                                if ( i->expr->get_result()->size() == 1 && isIntegralType( i->expr->get_result() ) ) {
163                                        if ( newExpr ) {
164                                                throw SemanticError( "Too many interpretations for case control expression", untyped );
165                                        } else {
166                                                newExpr = i->expr->clone();
167                                                newEnv = &i->env;
168                                        } // if
169                                } // if
170                        } // for
171                        if ( ! newExpr ) {
172                                throw SemanticError( "No interpretations for case control expression", untyped );
173                        } // if
174                        finishExpr( newExpr, *newEnv );
175                        return newExpr;
176                }
177
178        }
179
180        void Resolver::visit( ObjectDecl *objectDecl ) {
181                Type *new_type = resolveTypeof( objectDecl->get_type(), *this );
182                objectDecl->set_type( new_type );
183                // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable
184                // initContext is changed multiple time because the LHS is analysed twice. The second analysis changes
185                // initContext because of a function type can contain object declarations in the return and parameter types. So
186                // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting
187                // the RHS.
188                Type *temp = initContext;
189                initContext = new_type;
190                if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) {
191                        // enumerator initializers should not use the enum type to initialize, since
192                        // the enum type is still incomplete at this point. Use signed int instead.
193                        initContext = new BasicType( Type::Qualifiers(), BasicType::SignedInt );
194                }
195                Parent::visit( objectDecl );
196                if ( inEnumDecl && dynamic_cast< EnumInstType * >( initContext ) ) {
197                        // delete newly created signed int type
198                        delete initContext;
199                }
200                initContext = temp;
201        }
202
203        template< typename PtrType >
204        void Resolver::handlePtrType( PtrType * type ) {
205                if ( type->get_dimension() ) {
206                        CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() );
207                        Expression *newExpr = findSingleExpression( castExpr, *this );
208                        delete type->get_dimension();
209                        type->set_dimension( newExpr );
210                }
211        }
212
213        void Resolver::visit( ArrayType * at ) {
214                handlePtrType( at );
215                Parent::visit( at );
216        }
217
218        void Resolver::visit( PointerType * pt ) {
219                handlePtrType( pt );
220                Parent::visit( pt );
221        }
222
223        void Resolver::visit( TypeDecl *typeDecl ) {
224                if ( typeDecl->get_base() ) {
225                        Type *new_type = resolveTypeof( typeDecl->get_base(), *this );
226                        typeDecl->set_base( new_type );
227                } // if
228                Parent::visit( typeDecl );
229        }
230
231        void Resolver::visit( FunctionDecl *functionDecl ) {
232#if 0
233                std::cout << "resolver visiting functiondecl ";
234                functionDecl->print( std::cout );
235                std::cout << std::endl;
236#endif
237                Type *new_type = resolveTypeof( functionDecl->get_type(), *this );
238                functionDecl->set_type( new_type );
239                ValueGuard< Type * > oldFunctionReturn( functionReturn );
240                functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() );
241                Parent::visit( functionDecl );
242
243                // default value expressions have an environment which shouldn't be there and trips up later passes.
244                // xxx - it might be necessary to somehow keep the information from this environment, but I can't currently
245                // see how it's useful.
246                for ( Declaration * d : functionDecl->get_functionType()->get_parameters() ) {
247                        if ( ObjectDecl * obj = dynamic_cast< ObjectDecl * >( d ) ) {
248                                if ( SingleInit * init = dynamic_cast< SingleInit * >( obj->get_init() ) ) {
249                                        delete init->get_value()->get_env();
250                                        init->get_value()->set_env( nullptr );
251                                }
252                        }
253                }
254        }
255
256        void Resolver::visit( EnumDecl * enumDecl ) {
257                // in case we decide to allow nested enums
258                ValueGuard< bool > oldInEnumDecl( inEnumDecl );
259                inEnumDecl = true;
260                Parent::visit( enumDecl );
261        }
262
263        void Resolver::visit( ExprStmt *exprStmt ) {
264                assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" );
265                Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this );
266                delete exprStmt->get_expr();
267                exprStmt->set_expr( newExpr );
268        }
269
270        void Resolver::visit( AsmExpr *asmExpr ) {
271                Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this );
272                delete asmExpr->get_operand();
273                asmExpr->set_operand( newExpr );
274                if ( asmExpr->get_inout() ) {
275                        newExpr = findVoidExpression( asmExpr->get_inout(), *this );
276                        delete asmExpr->get_inout();
277                        asmExpr->set_inout( newExpr );
278                } // if
279        }
280
281        void Resolver::visit( AsmStmt *asmStmt ) {
282                acceptAll( asmStmt->get_input(), *this);
283                acceptAll( asmStmt->get_output(), *this);
284        }
285
286        void Resolver::visit( IfStmt *ifStmt ) {
287                Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this );
288                delete ifStmt->get_condition();
289                ifStmt->set_condition( newExpr );
290                Parent::visit( ifStmt );
291        }
292
293        void Resolver::visit( WhileStmt *whileStmt ) {
294                Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this );
295                delete whileStmt->get_condition();
296                whileStmt->set_condition( newExpr );
297                Parent::visit( whileStmt );
298        }
299
300        void Resolver::visit( ForStmt *forStmt ) {
301                Parent::visit( forStmt );
302
303                if ( forStmt->get_condition() ) {
304                        Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this );
305                        delete forStmt->get_condition();
306                        forStmt->set_condition( newExpr );
307                } // if
308
309                if ( forStmt->get_increment() ) {
310                        Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this );
311                        delete forStmt->get_increment();
312                        forStmt->set_increment( newExpr );
313                } // if
314        }
315
316        void Resolver::visit( SwitchStmt *switchStmt ) {
317                ValueGuard< Type * > oldInitContext( initContext );
318                Expression *newExpr;
319                newExpr = findIntegralExpression( switchStmt->get_condition(), *this );
320                delete switchStmt->get_condition();
321                switchStmt->set_condition( newExpr );
322
323                initContext = newExpr->get_result();
324                Parent::visit( switchStmt );
325        }
326
327        void Resolver::visit( CaseStmt *caseStmt ) {
328                if ( caseStmt->get_condition() ) {
329                        assert( initContext );
330                        CastExpr * castExpr = new CastExpr( caseStmt->get_condition(), initContext->clone() );
331                        Expression * newExpr = findSingleExpression( castExpr, *this );
332                        castExpr = safe_dynamic_cast< CastExpr * >( newExpr );
333                        caseStmt->set_condition( castExpr->get_arg() );
334                        castExpr->set_arg( nullptr );
335                        delete castExpr;
336                }
337                Parent::visit( caseStmt );
338        }
339
340        void Resolver::visit( BranchStmt *branchStmt ) {
341                // must resolve the argument for a computed goto
342                if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
343                        if ( Expression * arg = branchStmt->get_computedTarget() ) {
344                                VoidType v = Type::Qualifiers();                // cast to void * for the alternative finder
345                                PointerType pt( Type::Qualifiers(), v.clone() );
346                                CastExpr * castExpr = new CastExpr( arg, pt.clone() );
347                                Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression
348                                branchStmt->set_target( newExpr );
349                        } // if
350                } // if
351        }
352
353        void Resolver::visit( ReturnStmt *returnStmt ) {
354                if ( returnStmt->get_expr() ) {
355                        CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() );
356                        Expression *newExpr = findSingleExpression( castExpr, *this );
357                        delete castExpr;
358                        returnStmt->set_expr( newExpr );
359                } // if
360        }
361
362        template< typename T >
363        bool isCharType( T t ) {
364                if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
365                        return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
366                                bt->get_kind() == BasicType::UnsignedChar;
367                }
368                return false;
369        }
370
371        void Resolver::visit( SingleInit *singleInit ) {
372                if ( singleInit->get_value() ) {
373                        // // find all the d's
374                        // std::list<Expression *> &designators = singleInit->get_designators();
375                        // std::list<Type *> types1{ initContext }, types2;
376                        // for ( Expression * expr: designators ) {
377                        //      cerr << expr << endl;
378                        //      if ( NameExpr * nexpr = dynamic_cast<NameExpr *>( expr ) ) {
379                        //              for ( Type * type: types1 ) {
380                        //                      cerr << type << endl;
381                        //                      ReferenceToType * fred = dynamic_cast<ReferenceToType *>(type);
382                        //                      std::list<Declaration *> members;
383                        //                      if ( fred ) {
384                        //                              fred->lookup( nexpr->get_name(), members ); // concatenate identical field name
385                        //                              for ( Declaration * mem: members ) {
386                        //                                      if ( DeclarationWithType * dwt = dynamic_cast<DeclarationWithType *>(mem) ) {
387                        //                                              types2.push_back( dwt->get_type() );
388                        //                                      } // if
389                        //                              } // for
390                        //                      } // if
391                        //              } // for
392                        //              types1 = types2;
393                        //              types2.clear();
394                        //      } // if
395                        // } // for
396                        // // for ( Type * type: types1 ) {
397                        // //   cerr << type << endl;
398                        // // } // for
399
400                        // // O(N^2) checks of d-types with f-types
401                        // // find the minimum cost
402                        CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() );
403                        Expression *newExpr = findSingleExpression( castExpr, *this );
404                        delete castExpr;
405                        singleInit->set_value( newExpr );
406
407                        // check if initializing type is char[]
408                        if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
409                                if ( isCharType( at->get_base() ) ) {
410                                        // check if the resolved type is char *
411                                        if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_result() ) ) {
412                                                if ( isCharType( pt->get_base() ) ) {
413                                                        // strip cast if we're initializing a char[] with a char *, e.g.  char x[] = "hello";
414                                                        CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );
415                                                        singleInit->set_value( ce->get_arg() );
416                                                        ce->set_arg( NULL );
417                                                        delete ce;
418                                                }
419                                        }
420                                }
421                        }
422                } // if
423        }
424
425        template< typename AggrInst >
426        TypeSubstitution makeGenericSubstitutuion( AggrInst * inst ) {
427                assert( inst );
428                assert( inst->get_baseParameters() );
429                std::list< TypeDecl * > baseParams = *inst->get_baseParameters();
430                std::list< Expression * > typeSubs = inst->get_parameters();
431                TypeSubstitution subs( baseParams.begin(), baseParams.end(), typeSubs.begin() );
432                return subs;
433        }
434
435        ReferenceToType * isStructOrUnion( Type * type ) {
436                if ( StructInstType * sit = dynamic_cast< StructInstType * >( type ) ) {
437                        return sit;
438                } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( type ) ) {
439                        return uit;
440                }
441                return nullptr;
442        }
443
444        void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd, TypeSubstitution sub ) {
445                DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl );
446                assert( dt );
447                // need to substitute for generic types, so that casts are to concrete types
448                initContext = dt->get_type()->clone();
449                sub.apply( initContext );
450
451                try {
452                        if ( init == initEnd ) return; // stop when there are no more initializers
453                        (*init)->accept( *this );
454                        ++init; // made it past an initializer
455                } catch( SemanticError & ) {
456                        // need to delve deeper, if you can
457                        if ( ReferenceToType * type = isStructOrUnion( initContext ) ) {
458                                resolveAggrInit( type, init, initEnd );
459                        } else {
460                                // member is not an aggregate type, so can't go any deeper
461
462                                // might need to rethink what is being thrown
463                                throw;
464                        } // if
465                }
466        }
467
468        void Resolver::resolveAggrInit( ReferenceToType * inst, InitIterator & init, InitIterator & initEnd ) {
469                if ( StructInstType * sit = dynamic_cast< StructInstType * >( inst ) ) {
470                        TypeSubstitution sub = makeGenericSubstitutuion( sit );
471                        StructDecl * st = sit->get_baseStruct();
472                        if(st->get_members().empty()) return;
473                        // want to resolve each initializer to the members of the struct,
474                        // but if there are more initializers than members we should stop
475                        list< Declaration * >::iterator it = st->get_members().begin();
476                        for ( ; it != st->get_members().end(); ++it) {
477                                resolveSingleAggrInit( *it, init, initEnd, sub );
478                        }
479                } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( inst ) ) {
480                        TypeSubstitution sub = makeGenericSubstitutuion( uit );
481                        UnionDecl * un = uit->get_baseUnion();
482                        if(un->get_members().empty()) return;
483                        // only resolve to the first member of a union
484                        resolveSingleAggrInit( *un->get_members().begin(), init, initEnd, sub );
485                } // if
486        }
487
488        void Resolver::visit( ListInit * listInit ) {
489                InitIterator iter = listInit->begin();
490                InitIterator end = listInit->end();
491
492                if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
493                        // resolve each member to the base type of the array
494                        for ( ; iter != end; ++iter ) {
495                                initContext = at->get_base();
496                                (*iter)->accept( *this );
497                        } // for
498                } else if ( TupleType * tt = dynamic_cast< TupleType * > ( initContext ) ) {
499                        for ( Type * t : *tt ) {
500                                if ( iter == end ) break;
501                                initContext = t;
502                                (*iter++)->accept( *this );
503                        }
504                } else if ( ReferenceToType * type = isStructOrUnion( initContext ) ) {
505                        resolveAggrInit( type, iter, end );
506                } else if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( initContext ) ) {
507                        Type * base = tt->get_baseType()->get_base();
508                        if ( base ) {
509                                // know the implementation type, so try using that as the initContext
510                                initContext = base;
511                                visit( listInit );
512                        } else {
513                                // missing implementation type -- might be an unknown type variable, so try proceeding with the current init context
514                                Parent::visit( listInit );
515                        }
516                } else {
517                        assert( dynamic_cast< BasicType * >( initContext ) || dynamic_cast< PointerType * >( initContext )
518                                || dynamic_cast< ZeroType * >( initContext ) || dynamic_cast< OneType * >( initContext ) || dynamic_cast < EnumInstType * > ( initContext ) );
519                        // basic types are handled here
520                        Parent::visit( listInit );
521                }
522
523#if 0
524                if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) {
525                        std::list<Initializer *>::iterator iter( listInit->begin_initializers() );
526                        for ( ; iter != listInit->end_initializers(); ++iter ) {
527                                initContext = at->get_base();
528                                (*iter)->accept( *this );
529                        } // for
530                } else if ( StructInstType *st = dynamic_cast<StructInstType*>(initContext) ) {
531                        StructDecl *baseStruct = st->get_baseStruct();
532                        std::list<Declaration *>::iterator iter1( baseStruct->get_members().begin() );
533                        std::list<Initializer *>::iterator iter2( listInit->begin_initializers() );
534                        for ( ; iter1 != baseStruct->get_members().end() && iter2 != listInit->end_initializers(); ++iter2 ) {
535                                if ( (*iter2)->get_designators().empty() ) {
536                                        DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *iter1 );
537                                        initContext = dt->get_type();
538                                        (*iter2)->accept( *this );
539                                        ++iter1;
540                                } else {
541                                        StructDecl *st = baseStruct;
542                                        iter1 = st->get_members().begin();
543                                        std::list<Expression *>::iterator iter3( (*iter2)->get_designators().begin() );
544                                        for ( ; iter3 != (*iter2)->get_designators().end(); ++iter3 ) {
545                                                NameExpr *key = dynamic_cast<NameExpr *>( *iter3 );
546                                                assert( key );
547                                                for ( ; iter1 != st->get_members().end(); ++iter1 ) {
548                                                        if ( key->get_name() == (*iter1)->get_name() ) {
549                                                                (*iter1)->print( cout );
550                                                                cout << key->get_name() << endl;
551                                                                ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
552                                                                assert( fred );
553                                                                StructInstType *mary = dynamic_cast<StructInstType*>( fred->get_type() );
554                                                                assert( mary );
555                                                                st = mary->get_baseStruct();
556                                                                iter1 = st->get_members().begin();
557                                                                break;
558                                                        } // if
559                                                }  // for
560                                        } // for
561                                        ObjectDecl *fred = dynamic_cast<ObjectDecl *>( *iter1 );
562                                        assert( fred );
563                                        initContext = fred->get_type();
564                                        (*listInit->begin_initializers())->accept( *this );
565                                } // if
566                        } // for
567                } else if ( UnionInstType *st = dynamic_cast<UnionInstType*>(initContext) ) {
568                        DeclarationWithType *dt = dynamic_cast<DeclarationWithType *>( *st->get_baseUnion()->get_members().begin() );
569                        initContext = dt->get_type();
570                        (*listInit->begin_initializers())->accept( *this );
571                } // if
572#endif
573        }
574
575        // ConstructorInit - fall back on C-style initializer
576        void Resolver::fallbackInit( ConstructorInit * ctorInit ) {
577                // could not find valid constructor, or found an intrinsic constructor
578                // fall back on C-style initializer
579                delete ctorInit->get_ctor();
580                ctorInit->set_ctor( NULL );
581                delete ctorInit->get_dtor();
582                ctorInit->set_dtor( NULL );
583                maybeAccept( ctorInit->get_init(), *this );
584        }
585
586        // needs to be callable from outside the resolver, so this is a standalone function
587        void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) {
588                assert( ctorInit );
589                Resolver resolver( indexer );
590                ctorInit->accept( resolver );
591        }
592
593        void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) {
594                assert( stmtExpr );
595                Resolver resolver( indexer );
596                stmtExpr->accept( resolver );
597        }
598
599        void Resolver::visit( ConstructorInit *ctorInit ) {
600                // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit
601                maybeAccept( ctorInit->get_ctor(), *this );
602                maybeAccept( ctorInit->get_dtor(), *this );
603
604                // found a constructor - can get rid of C-style initializer
605                delete ctorInit->get_init();
606                ctorInit->set_init( NULL );
607
608                // intrinsic single parameter constructors and destructors do nothing. Since this was
609                // implicitly generated, there's no way for it to have side effects, so get rid of it
610                // to clean up generated code.
611                if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_ctor() ) ) {
612                        delete ctorInit->get_ctor();
613                        ctorInit->set_ctor( NULL );
614                }
615
616                if ( InitTweak::isIntrinsicSingleArgCallStmt( ctorInit->get_dtor() ) ) {
617                        delete ctorInit->get_dtor();
618                        ctorInit->set_dtor( NULL );
619                }
620
621                // xxx - todo -- what about arrays?
622                // if ( dtor == NULL && InitTweak::isIntrinsicCallStmt( ctorInit->get_ctor() ) ) {
623                //      // can reduce the constructor down to a SingleInit using the
624                //      // second argument from the ctor call, since
625                //      delete ctorInit->get_ctor();
626                //      ctorInit->set_ctor( NULL );
627
628                //      Expression * arg =
629                //      ctorInit->set_init( new SingleInit( arg ) );
630                // }
631        }
632} // namespace ResolvExpr
633
634// Local Variables: //
635// tab-width: 4 //
636// mode: c++ //
637// compile-command: "make install" //
638// End: //
Note: See TracBrowser for help on using the repository browser.