Ignore:
Timestamp:
Jun 26, 2015, 4:00:26 PM (10 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
Children:
0df292b, e0ff3e6
Parents:
eb50842 (diff), 1869adf (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge pointer to pointer to qualified fix into master

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Resolver.cc

    reb50842 r937e51d  
    99// Author           : Richard C. Bilson
    1010// Created On       : Sun May 17 12:17:01 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun May 17 12:18:17 2015
    13 // Update Count     : 2
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Jun 24 16:20:35 2015
     13// Update Count     : 156
    1414//
    1515
     
    3838                virtual void visit( TypeDecl *typeDecl );
    3939
     40                virtual void visit( ArrayType * at );
     41
    4042                virtual void visit( ExprStmt *exprStmt );
    4143                virtual void visit( IfStmt *ifStmt );
     
    4547                virtual void visit( ChooseStmt *switchStmt );
    4648                virtual void visit( CaseStmt *caseStmt );
     49                virtual void visit( BranchStmt *branchStmt );
    4750                virtual void visit( ReturnStmt *returnStmt );
    4851
     
    5053                virtual void visit( ListInit *listInit );
    5154          private:
     55        typedef std::list< Initializer * >::iterator InitIterator;
     56
     57          void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & );
     58          void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & );
     59
    5260                std::list< Type * > functionReturn;
    5361                Type *initContext;
     
    158166                SymTab::Indexer::visit( objectDecl );
    159167        }
    160  
     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
    161180        void Resolver::visit( TypeDecl *typeDecl ) {
    162181                if ( typeDecl->get_base() ) {
     
    166185                SymTab::Indexer::visit( typeDecl );
    167186        }
    168  
     187
    169188        void Resolver::visit( FunctionDecl *functionDecl ) {
    170189#if 0
     
    252271        }
    253272
     273        void Resolver::visit( BranchStmt *branchStmt ) {
     274                // must resolve the argument for a computed goto
     275                if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
     276                        if ( NameExpr * arg = dynamic_cast< NameExpr * >( branchStmt->get_computedTarget() ) ) {
     277                                VoidType v = Type::Qualifiers();                // cast to void * for the alternative finder
     278                                PointerType pt( Type::Qualifiers(), v.clone() );
     279                                CastExpr * castExpr = new CastExpr( arg, pt.clone() );
     280                                Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression
     281                                branchStmt->set_target( newExpr );
     282                        } // if
     283                } // if
     284        }
     285
    254286        void Resolver::visit( ReturnStmt *returnStmt ) {
    255287                if ( returnStmt->get_expr() ) {
     
    260292                        returnStmt->set_expr( newExpr );
    261293                } // if
     294        }
     295
     296        template< typename T >
     297        bool isCharType( T t ) {
     298                if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
     299                        return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
     300                                bt->get_kind() == BasicType::UnsignedChar;
     301                }
     302                return false;
    262303        }
    263304
     
    286327                        delete castExpr;
    287328                        singleInit->set_value( newExpr );
     329
     330                        // check if initializing type is char[]
     331                        if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
     332                                if ( isCharType( at->get_base() ) ) {
     333                                        // check if the resolved type is char *
     334                                        if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) {
     335                                                if ( isCharType( pt->get_base() ) ) {
     336                                                        // strip cast if we're initializing a char[] with a char *, e.g.
     337                                                        // char x[] = "hello";
     338                                                        CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );
     339                                                        singleInit->set_value( ce->get_arg() );
     340                                                        ce->set_arg( NULL );
     341                                                        delete ce;                                                                     
     342                                                }
     343                                        }
     344                                }
     345                        }
    288346                } // if
    289347//      singleInit->get_value()->accept( *this );
    290348        }
    291349
    292         void Resolver::visit( ListInit *listInit ) {
    293                 Visitor::visit(listInit);
     350        void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) {
     351                DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl );
     352                assert( dt );
     353                initContext = dt->get_type();
     354                try {
     355                        if ( init == initEnd ) return; // stop when there are no more initializers
     356                        (*init)->accept( *this );
     357                        ++init; // made it past an initializer
     358                } catch( SemanticError & ) {
     359                        // need to delve deeper, if you can
     360                        if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) {
     361                                resolveAggrInit( sit->get_baseStruct(), init, initEnd );
     362                        } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) {
     363                                resolveAggrInit( uit->get_baseUnion(), init, initEnd );
     364                        } else {
     365                                // member is not an aggregate type, so can't go any deeper
     366
     367                                // might need to rethink what is being thrown
     368                                throw;
     369                        } // if
     370                }
     371        }
     372
     373        void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) {
     374                if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) {
     375                        // want to resolve each initializer to the members of the struct,
     376                        // but if there are more initializers than members we should stop
     377                        list< Declaration * >::iterator it = st->get_members().begin();
     378                        for ( ; it != st->get_members().end(); ++it) {
     379                                resolveSingleAggrInit( *it, init, initEnd );
     380                        }
     381                } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) {
     382                        // only resolve to the first member of a union
     383                        resolveSingleAggrInit( *un->get_members().begin(), init, initEnd );
     384                } // if
     385        }
     386
     387        void Resolver::visit( ListInit * listInit ) {
     388                InitIterator iter = listInit->begin_initializers();
     389                InitIterator end = listInit->end_initializers();
     390
     391                if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
     392                        // resolve each member to the base type of the array
     393                        for ( ; iter != end; ++iter ) {
     394                                initContext = at->get_base();
     395                                (*iter)->accept( *this );
     396                        } // for
     397                } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) {
     398                        resolveAggrInit( st->get_baseStruct(), iter, end );
     399                } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) {
     400                        resolveAggrInit( st->get_baseUnion(), iter, end );
     401                } else {
     402                        // basic types are handled here
     403                        Visitor::visit( listInit );
     404                }
     405
    294406#if 0
    295407                if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) {
Note: See TracChangeset for help on using the changeset viewer.