Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Virtual/ExpandCasts.cc

    rc19bc90 r8e6214f  
    1010// Created On       : Mon Jul 24 13:59:00 2017
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Tue May 26 14:37:00 2020
    13 // Update Count     : 2
     12// Last Modified On : Tus Aug  2 14:59:00 2017
     13// Update Count     : 1
    1414//
    1515
     
    111111        }
    112112
    113         // Better error locations for generated casts.
    114         static CodeLocation castLocation( VirtualCastExpr * castExpr ) {
    115                 if ( castExpr->location.isSet() ) {
    116                         return castExpr->location;
    117                 } else if ( castExpr->arg->location.isSet() ) {
    118                         return castExpr->arg->location;
    119                 } else if ( castExpr->result->location.isSet() ) {
    120                         return castExpr->result->location;
    121                 } else {
    122                         return CodeLocation();
    123                 }
    124         }
    125 
    126113        Expression * VirtualCastCore::postmutate( VirtualCastExpr * castExpr ) {
    127114                assertf( castExpr->get_result(), "Virtual Cast target not found before expansion." );
     
    130117                assert( pvt_decl );
    131118
    132                 // Get the base type of the pointer/reference.
    133                 Type * base;
    134                 Type * result_type = castExpr->result;
    135                 if ( PointerType * target = dynamic_cast<PointerType *>( result_type ) ) {
    136                         base = target->base;
    137                 } else if ( ReferenceType * target = dynamic_cast<ReferenceType *>( result_type ) ) {
    138                         base = target->base;
    139                 } else {
    140                         SemanticError( castLocation( castExpr ),
    141                                 "Virtual cast type must be a pointer or reference type." );
    142                 }
     119                // May only cast to a pointer or reference type.
     120                // A earlier validation should give a syntax error, this is
     121                // just to make sure errors don't creep during translation.
     122                // Move to helper with more detailed error messages.
     123                PointerType * target_type =
     124                        dynamic_cast<PointerType *>( castExpr->get_result() );
     125                assert( target_type );
    143126
    144                 StructInstType * target_struct = dynamic_cast<StructInstType *>( base );
    145                 if ( nullptr == target_struct ) {
    146                         SemanticError( castLocation( castExpr ),
    147                                 "Virtual cast type must refer to a structure type." );
    148                 }
     127                StructInstType * target_struct =
     128                        dynamic_cast<StructInstType *>( target_type->get_base() );
     129                assert( target_struct );
     130
    149131                StructDecl * target_decl = target_struct->get_baseStruct();
    150132
    151133                std::map<std::string, ObjectDecl *>::iterator found =
    152                         vtable_instances.find( get_vtable_inst_name( target_decl->get_name() ) );
     134                        vtable_instances.find(
     135                                get_vtable_inst_name( target_decl->get_name() ) );
    153136                if ( vtable_instances.end() == found ) {
    154                         SemanticError( castLocation( castExpr ),
    155                                 "Virtual cast type does not have a virtual table instance." );
     137                        assertf( false, "virtual table instance not found." );
    156138                }
    157139                ObjectDecl * table = found->second;
    158140
    159141                Expression * result = new CastExpr(
     142                        //new ApplicationExpr(
     143                                //new AddressExpr( new VariableExpr( vcast_decl ) ),
     144                                //new CastExpr( new VariableExpr( vcast_decl ),
     145                                //      new PointerType( noQualifiers,
     146                                //              vcast_decl->get_type()->clone()
     147                                //              )
     148                                //      ),
    160149                        new ApplicationExpr( VariableExpr::functionPointer( vcast_decl ), {
    161150                                        new CastExpr(
Note: See TracChangeset for help on using the changeset viewer.