Changes in / [cda48b6:a8541d9]


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Resolver.cc

    rcda48b6 ra8541d9  
    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 Jun  7 21:50:37 2015
    13 // Update Count     : 23
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Jun 10 16:11:19 2015
     13// Update Count     : 77
    1414//
    1515
     
    3737                virtual void visit( ObjectDecl *functionDecl );
    3838                virtual void visit( TypeDecl *typeDecl );
     39                virtual void visit( ArrayType * at );
    3940
    4041                virtual void visit( ExprStmt *exprStmt );
     
    157158                initContext = new_type;
    158159                SymTab::Indexer::visit( objectDecl );
    159 
    160                 if ( ArrayType * at = dynamic_cast< ArrayType * >( new_type ) ){
    161                         if ( at->get_dimension() ) {
    162                                 BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
    163                                 CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() );
    164                                 Expression *newExpr = findSingleExpression( castExpr, *this );
    165                                 delete at->get_dimension();
    166                                 at->set_dimension( newExpr );
    167                         }
    168                 }
     160        }
     161
     162        void Resolver::visit( ArrayType * at ) {
     163                if ( at->get_dimension() ) {
     164                        BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
     165                        CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() );
     166                        Expression *newExpr = findSingleExpression( castExpr, *this );
     167                        delete at->get_dimension();
     168                        at->set_dimension( newExpr );
     169
     170                }
     171                Visitor::visit( at );
    169172        }
    170173 
     
    270273                        returnStmt->set_expr( newExpr );
    271274                } // if
     275        }
     276
     277        template< typename T >
     278        bool isCharType( T t ) {
     279                if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
     280                        return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
     281                                bt->get_kind() == BasicType::UnsignedChar;
     282                }
     283                return false;
    272284        }
    273285
     
    296308                        delete castExpr;
    297309                        singleInit->set_value( newExpr );
     310
     311                        // check if initializing type is char[]
     312                        if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
     313                                if ( isCharType( at->get_base() ) ) {
     314                                        // check if the resolved type is char *
     315                                        if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) {
     316                                                if ( isCharType( pt->get_base() ) ) {
     317                                                        // strip cast if we're initializing a char[] with a char *, e.g.
     318                                                        // char x[] = "hello";
     319                                                        CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );
     320                                                        singleInit->set_value( ce->get_arg() );
     321                                                        ce->set_arg( NULL );
     322                                                        delete ce;                                                                     
     323                                                }
     324                                        }
     325                                }
     326                        }
    298327                } // if
    299328//      singleInit->get_value()->accept( *this );
     
    301330
    302331        void Resolver::visit( ListInit *listInit ) {
    303                 Visitor::visit(listInit);
     332                if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
     333                        std::list< Initializer * >::iterator iter( listInit->begin_initializers() );
     334                        for ( ; iter != listInit->end_initializers(); ++iter ) {
     335                                initContext = at->get_base();
     336                                (*iter)->accept( *this );
     337                        } // for
     338                } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) {
     339                        DeclarationWithType *dt = dynamic_cast< DeclarationWithType * >( *st->get_baseUnion()->get_members().begin() );
     340                        initContext = dt->get_type();
     341                        (*listInit->begin_initializers())->accept( *this );
     342                } else {
     343                        // basic types are handled here
     344                        Visitor::visit( listInit );
     345                }
     346
    304347#if 0
    305348                if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) {
Note: See TracChangeset for help on using the changeset viewer.