Changeset c19bc90 for src


Ignore:
Timestamp:
May 26, 2020, 4:03:30 PM (4 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
ada0246d
Parents:
b2de2e0
Message:

Better error messages in ExpandCasts?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Virtual/ExpandCasts.cc

    rb2de2e0 rc19bc90  
    1010// Created On       : Mon Jul 24 13:59:00 2017
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Tus Aug  2 14:59:00 2017
    13 // Update Count     : 1
     12// Last Modified On : Tue May 26 14:37:00 2020
     13// Update Count     : 2
    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
    113126        Expression * VirtualCastCore::postmutate( VirtualCastExpr * castExpr ) {
    114127                assertf( castExpr->get_result(), "Virtual Cast target not found before expansion." );
     
    117130                assert( pvt_decl );
    118131
    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 );
     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                }
    126143
    127                 StructInstType * target_struct =
    128                         dynamic_cast<StructInstType *>( target_type->get_base() );
    129                 assert( target_struct );
    130 
     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                }
    131149                StructDecl * target_decl = target_struct->get_baseStruct();
    132150
    133151                std::map<std::string, ObjectDecl *>::iterator found =
    134                         vtable_instances.find(
    135                                 get_vtable_inst_name( target_decl->get_name() ) );
     152                        vtable_instances.find( get_vtable_inst_name( target_decl->get_name() ) );
    136153                if ( vtable_instances.end() == found ) {
    137                         assertf( false, "virtual table instance not found." );
     154                        SemanticError( castLocation( castExpr ),
     155                                "Virtual cast type does not have a virtual table instance." );
    138156                }
    139157                ObjectDecl * table = found->second;
    140158
    141159                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                                 //      ),
    149160                        new ApplicationExpr( VariableExpr::functionPointer( vcast_decl ), {
    150161                                        new CastExpr(
Note: See TracChangeset for help on using the changeset viewer.