Changeset b5c5684


Ignore:
Timestamp:
Jun 10, 2015, 3:37:34 PM (9 years ago)
Author:
Rob Schluntz <rschlunt@…>
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:
bfbf97f
Parents:
8a95629
Message:

removes casts from char[] initialized with strings, correctly casts array initializers, correctly casts union initializer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Resolver.cc

    r8a95629 rb5c5684  
    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 15:32:16 2015
     13// Update Count     : 45
    1414//
    1515
     
    272272        }
    273273
     274        template< typename T >
     275        bool isCharType( T t ) {
     276                if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
     277                        return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
     278                                bt->get_kind() == BasicType::UnsignedChar;
     279                }
     280                return false;
     281        }
     282
    274283        void Resolver::visit( SingleInit *singleInit ) {
    275284                if ( singleInit->get_value() ) {
     
    296305                        delete castExpr;
    297306                        singleInit->set_value( newExpr );
     307
     308                        // check if initializing type is char[]
     309                        if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
     310                                if ( isCharType( at->get_base() ) ) {
     311                                        // check if the resolved type is char *
     312                                        if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) {
     313                                                if ( isCharType( pt->get_base() ) ) {
     314                                                        // strip cast if we're initializing a char[] with a char *, e.g.
     315                                                        // char x[] = "hello";
     316                                                        CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );
     317                                                        singleInit->set_value( ce->get_arg() );
     318                                                        ce->set_arg( NULL );
     319                                                        delete ce;                                                                     
     320                                                }
     321                                        }
     322                                }
     323                        }
    298324                } // if
    299325//      singleInit->get_value()->accept( *this );
     
    301327
    302328        void Resolver::visit( ListInit *listInit ) {
    303                 Visitor::visit(listInit);
     329                if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
     330                        std::list< Initializer * >::iterator iter( listInit->begin_initializers() );
     331                        for ( ; iter != listInit->end_initializers(); ++iter ) {
     332                                initContext = at->get_base();
     333                                (*iter)->accept( *this );
     334                        } // for
     335                } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) {
     336                        DeclarationWithType *dt = dynamic_cast< DeclarationWithType * >( *st->get_baseUnion()->get_members().begin() );
     337                        initContext = dt->get_type();
     338                        (*listInit->begin_initializers())->accept( *this );
     339                } else {
     340                        // basic types are handled here
     341                        Visitor::visit( listInit );
     342                }
     343
    304344#if 0
    305345                if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) {
Note: See TracChangeset for help on using the changeset viewer.