Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CurrentObject.cc

    r84e8423 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 ) {
     
    6278                virtual ~MemberIterator() {}
    6379
    64                 /// walks the current object using the given designators as a guide
    6580                virtual void setPosition( std::list< Expression * > & designators ) = 0;
    66 
    67                 /// retrieve the list of possible Type/Designaton pairs for the current position in the currect object
    6881                virtual std::list<InitAlternative> operator*() const = 0;
    69 
    70                 /// true if the iterator is not currently at the end
    7182                virtual operator bool() const = 0;
    72 
    73                 /// moves the iterator by one member in the current object
    7483                virtual MemberIterator & bigStep() = 0;
    75 
    76                 /// moves the iterator by one member in the current subobject
    7784                virtual MemberIterator & smallStep() = 0;
    78 
    79                 /// the type of the current object
    8085                virtual Type * getType() = 0;
    81 
    82                 /// the type of the current subobject
    8386                virtual Type * getNext() = 0;
    8487
    85                 /// printing for debug
    8688                virtual void print( std::ostream & out, Indenter indent ) const = 0;
    8789
    88                 /// helper for operator*; aggregates must add designator to each init alternative, but
    89                 /// adding designators in operator* creates duplicates.
    9090                virtual std::list<InitAlternative> first() const = 0; // should be protected
    9191        };
     
    141141                        base = at->get_base();
    142142                        memberIter = createMemberIterator( base );
    143                         if ( at->isVarLen ) throw SemanticError( "VLA initialization does not support @=", at );
    144143                        setSize( at->get_dimension() );
    145144                }
     
    152151                void setSize( Expression * expr ) {
    153152                        if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
    154                                 try {
    155                                         size = constExpr->intValue();
    156                                         PRINT( std::cerr << "array type with size: " << size << std::endl; )
    157                                 } catch ( SemanticError & ) {
    158                                         throw SemanticError( "Constant expression of non-integral type in array dimension: ", expr );
    159                                 }
     153                                size = getConstValue( constExpr );
     154                                PRINT( std::cerr << "array type with size: " << size << std::endl; )
    160155                        }       else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
    161156                                setSize( castExpr->get_arg() ); // xxx - need to perform the conversion specified by the cast
    162                         } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) {
    163                                 if ( EnumInstType * inst = dynamic_cast< EnumInstType * > ( varExpr->result ) ) {
    164                                         long long int value;
    165                                         if ( inst->baseEnum->valueOf( varExpr->var, value ) ) {
    166                                                 size = value;
    167                                         }
    168                                 }
    169157                        } else {
    170158                                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
     
    176164                        // need to permit integer-constant-expressions, including: integer constants, enumeration constants, character constants, sizeof expressions, _Alignof expressions, cast expressions
    177165                        if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
    178                                 try {
    179                                         index = constExpr->intValue();
    180                                 } catch( SemanticError & ) {
    181                                         throw SemanticError( "Constant expression of non-integral type in array designator: ", expr );
    182                                 }
     166                                index = getConstValue( constExpr );
    183167                        } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
    184168                                setPosition( castExpr->get_arg() );
    185169                        } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) {
    186                                 EnumInstType * inst = dynamic_cast<EnumInstType *>( varExpr->get_result() );
    187                                 assertf( inst, "ArrayIterator given variable that isn't an enum constant : %s", toString( expr ).c_str() );
    188                                 long long int value;
    189                                 if ( inst->baseEnum->valueOf( varExpr->var, value ) ) {
    190                                         index = value;
    191                                 }
     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
    192172                        } else if ( dynamic_cast< SizeofExpr * >( expr ) || dynamic_cast< AlignofExpr * >( expr ) ) {
    193173                                index = 0; // xxx - get actual sizeof/alignof value?
     
    370350                                }
    371351                        }
    372                         if ( atbegin ) {
     352                        // if ( curMember == std::next( decl->get_members().begin(), 1 ) ) { // xxx - this never triggers because curMember is incremented immediately on construction
     353                        if ( atbegin ) { // xxx - this never triggers because curMember is incremented immediately on construction
    373354                                // xxx - what about case of empty struct??
    374355                                // only add self if at the very beginning of the structure
     
    404385                        return *this;
    405386                }
     387                virtual std::list<InitAlternative> first() const { return std::list<InitAlternative>{}; }
    406388        };
    407389
     
    457439                                return new UnionIterator( uit );
    458440                        } else {
    459                                 assertf( dynamic_cast< EnumInstType * >( type ) || dynamic_cast< TypeInstType * >( type ), "Encountered unhandled ReferenceToType in createMemberIterator: %s", toString( type ).c_str() );
     441                                assertf( dynamic_cast< TypeInstType * >( type ), "some other reftotype" );
    460442                                return new SimpleIterator( type );
    461443                        }
Note: See TracChangeset for help on using the changeset viewer.