Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CurrentObject.cc

    rc6747a1 r4b97770  
    3838
    3939namespace ResolvExpr {
    40         long long int getConstValue( ConstantExpr * constExpr ) {
    41                 if ( BasicType * basicType = dynamic_cast< BasicType * >( constExpr->get_result() ) ) {
    42                         if ( basicType->isInteger() ) {
    43                                 return constExpr->get_constant()->get_ival();
    44                         } else {
    45                                 assertf( false, "Non-integer constant expression in getConstValue %s", toString( constExpr ).c_str() ); // xxx - might be semantic error
    46                         }
    47                 } else if ( dynamic_cast< OneType * >( constExpr->get_result() ) ) {
    48                         return 1;
    49                 } else if ( dynamic_cast< ZeroType * >( constExpr->get_result() ) ) {
    50                         return 0;
    51                 } else {
    52                         assertf( false, "unhandled type on getConstValue %s", toString( constExpr->get_result() ).c_str() ); // xxx - might be semantic error
    53                 }
    54         }
    55 
    5640        template< typename AggrInst >
    5741        TypeSubstitution makeGenericSubstitution( AggrInst * inst ) {
     
    141125                        base = at->get_base();
    142126                        memberIter = createMemberIterator( base );
     127                        if ( at->isVarLen ) throw SemanticError( "VLA initialization does not support @=", at );
    143128                        setSize( at->get_dimension() );
    144129                }
     
    151136                void setSize( Expression * expr ) {
    152137                        if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
    153                                 size = getConstValue( constExpr );
    154                                 PRINT( std::cerr << "array type with size: " << size << std::endl; )
     138                                try {
     139                                        size = constExpr->intValue();
     140                                        PRINT( std::cerr << "array type with size: " << size << std::endl; )
     141                                } catch ( SemanticError & ) {
     142                                        throw SemanticError( "Constant expression of non-integral type in array dimension: ", expr );
     143                                }
    155144                        }       else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
    156145                                setSize( castExpr->get_arg() ); // xxx - need to perform the conversion specified by the cast
     146                        } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) {
     147                                if ( EnumInstType * inst = dynamic_cast< EnumInstType * > ( varExpr->result ) ) {
     148                                        long long int value;
     149                                        if ( inst->baseEnum->valueOf( varExpr->var, value ) ) {
     150                                                size = value;
     151                                        }
     152                                }
    157153                        } else {
    158154                                assertf( false, "unhandled expression in setSize: %s", toString( expr ).c_str() ); // xxx - if not a constant expression, it's not simple to determine how long the array actually is, which is necessary for initialization to be done correctly -- fix this
     
    164160                        // need to permit integer-constant-expressions, including: integer constants, enumeration constants, character constants, sizeof expressions, _Alignof expressions, cast expressions
    165161                        if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
    166                                 index = getConstValue( constExpr );
     162                                try {
     163                                        index = constExpr->intValue();
     164                                } catch( SemanticError & ) {
     165                                        throw SemanticError( "Constant expression of non-integral type in array designator: ", expr );
     166                                }
    167167                        } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
    168168                                setPosition( castExpr->get_arg() );
    169169                        } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) {
    170                                 assertf( dynamic_cast<EnumInstType *> ( varExpr->get_result() ), "ArrayIterator given variable that isn't an enum constant : %s", toString( expr ).c_str() );
    171                                 index = 0; // xxx - get actual value of enum constant
     170                                EnumInstType * inst = dynamic_cast<EnumInstType *>( varExpr->get_result() );
     171                                assertf( inst, "ArrayIterator given variable that isn't an enum constant : %s", toString( expr ).c_str() );
     172                                long long int value;
     173                                if ( inst->baseEnum->valueOf( varExpr->var, value ) ) {
     174                                        index = value;
     175                                }
    172176                        } else if ( dynamic_cast< SizeofExpr * >( expr ) || dynamic_cast< AlignofExpr * >( expr ) ) {
    173177                                index = 0; // xxx - get actual sizeof/alignof value?
Note: See TracChangeset for help on using the changeset viewer.