Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CurrentObject.cc

    r4b97770 rc6747a1  
    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
    4056        template< typename AggrInst >
    4157        TypeSubstitution makeGenericSubstitution( AggrInst * inst ) {
     
    125141                        base = at->get_base();
    126142                        memberIter = createMemberIterator( base );
    127                         if ( at->isVarLen ) throw SemanticError( "VLA initialization does not support @=", at );
    128143                        setSize( at->get_dimension() );
    129144                }
     
    136151                void setSize( Expression * expr ) {
    137152                        if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
    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                                 }
     153                                size = getConstValue( constExpr );
     154                                PRINT( std::cerr << "array type with size: " << size << std::endl; )
    144155                        }       else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
    145156                                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                                 }
    153157                        } else {
    154158                                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
     
    160164                        // need to permit integer-constant-expressions, including: integer constants, enumeration constants, character constants, sizeof expressions, _Alignof expressions, cast expressions
    161165                        if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
    162                                 try {
    163                                         index = constExpr->intValue();
    164                                 } catch( SemanticError & ) {
    165                                         throw SemanticError( "Constant expression of non-integral type in array designator: ", expr );
    166                                 }
     166                                index = getConstValue( constExpr );
    167167                        } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
    168168                                setPosition( castExpr->get_arg() );
    169169                        } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) {
    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                                 }
     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
    176172                        } else if ( dynamic_cast< SizeofExpr * >( expr ) || dynamic_cast< AlignofExpr * >( expr ) ) {
    177173                                index = 0; // xxx - get actual sizeof/alignof value?
Note: See TracChangeset for help on using the changeset viewer.