Changeset f64d9bc for src


Ignore:
Timestamp:
Dec 22, 2017, 4:20:00 PM (6 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
f3458a8
Parents:
caab997
Message:

Handle initialization of arrays with enumerator dimension and VLAs [fixes #75]

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CurrentObject.cc

    rcaab997 rf64d9bc  
    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 ) {
     
    151135                void setSize( Expression * expr ) {
    152136                        if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
    153                                 size = getConstValue( constExpr );
     137                                size = constExpr->intValue();
     138                                isVLA = false;
    154139                                PRINT( std::cerr << "array type with size: " << size << std::endl; )
    155140                        }       else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
    156141                                setSize( castExpr->get_arg() ); // xxx - need to perform the conversion specified by the cast
     142                        } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) {
     143                                if ( EnumInstType * inst = dynamic_cast< EnumInstType * > ( varExpr->result ) ) {
     144                                        long long int value;
     145                                        if ( inst->baseEnum->valueOf( varExpr->var, value ) ) {
     146                                                size = value;
     147                                                isVLA = false;
     148                                        }
     149                                }
    157150                        } else {
    158151                                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
     
    164157                        // need to permit integer-constant-expressions, including: integer constants, enumeration constants, character constants, sizeof expressions, _Alignof expressions, cast expressions
    165158                        if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
    166                                 index = getConstValue( constExpr );
     159                                index = constExpr->intValue();
    167160                        } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
    168161                                setPosition( castExpr->get_arg() );
    169162                        } 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
     163                                EnumInstType * inst = dynamic_cast<EnumInstType *>( varExpr->get_result() );
     164                                assertf( inst, "ArrayIterator given variable that isn't an enum constant : %s", toString( expr ).c_str() );
     165                                long long int value;
     166                                if ( inst->baseEnum->valueOf( varExpr->var, value ) ) {
     167                                        index = value;
     168                                }
    172169                        } else if ( dynamic_cast< SizeofExpr * >( expr ) || dynamic_cast< AlignofExpr * >( expr ) ) {
    173170                                index = 0; // xxx - get actual sizeof/alignof value?
     
    189186                }
    190187
    191                 virtual operator bool() const { return index < size; }
     188                virtual operator bool() const { return ! isVLA && index < size; }
    192189
    193190                virtual MemberIterator & bigStep() {
     
    195192                        ++index;
    196193                        delete memberIter;
    197                         if ( index < size ) memberIter = createMemberIterator( base );
     194                        if ( ! isVLA && index < size ) memberIter = createMemberIterator( base );
    198195                        else memberIter = nullptr;
    199196                        return *this;
     
    242239                size_t index = 0;
    243240                size_t size = 0;
     241                bool isVLA = true;
    244242                MemberIterator * memberIter = nullptr;
    245243        };
Note: See TracChangeset for help on using the changeset viewer.