Ignore:
Timestamp:
Aug 27, 2018, 4:40:34 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
b7c89aa
Parents:
f9feab8 (diff), 305581d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into cleanup-dtors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CurrentObject.cc

    rf9feab8 r90152a4  
    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 ) {
     
    7862                virtual ~MemberIterator() {}
    7963
     64                /// walks the current object using the given designators as a guide
    8065                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
    8168                virtual std::list<InitAlternative> operator*() const = 0;
     69
     70                /// true if the iterator is not currently at the end
    8271                virtual operator bool() const = 0;
     72
     73                /// moves the iterator by one member in the current object
    8374                virtual MemberIterator & bigStep() = 0;
     75
     76                /// moves the iterator by one member in the current subobject
    8477                virtual MemberIterator & smallStep() = 0;
     78
     79                /// the type of the current object
    8580                virtual Type * getType() = 0;
     81
     82                /// the type of the current subobject
    8683                virtual Type * getNext() = 0;
    8784
     85                /// printing for debug
    8886                virtual void print( std::ostream & out, Indenter indent ) const = 0;
    8987
     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        };
     
    139139                ArrayIterator( ArrayType * at ) : array( at ) {
    140140                        PRINT( std::cerr << "Creating array iterator: " << at << std::endl; )
    141                         base = at->get_base();
     141                        base = at->base;
    142142                        memberIter = createMemberIterator( base );
    143                         setSize( at->get_dimension() );
     143                        if ( at->isVarLen ) SemanticError( at, "VLA initialization does not support @=: " );
     144                        setSize( at->dimension );
    144145                }
    145146
     
    149150
    150151        private:
    151                 void setSize( Expression * expr ) {
    152                         if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
    153                                 size = getConstValue( constExpr );
    154                                 PRINT( std::cerr << "array type with size: " << size << std::endl; )
    155                         }       else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
    156                                 setSize( castExpr->get_arg() ); // xxx - need to perform the conversion specified by the cast
     152                void setSize( Expression * expr ) { // replace this logic with an eval call
     153                        auto res = eval(expr);
     154                        if (res.second) {
     155                                size = res.first;
    157156                        } else {
    158                                 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
     157                                SemanticError( expr->location, toString("Array designator must be a constant expression: ", expr) );
    159158                        }
    160159                }
     
    164163                        // need to permit integer-constant-expressions, including: integer constants, enumeration constants, character constants, sizeof expressions, _Alignof expressions, cast expressions
    165164                        if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
    166                                 index = getConstValue( constExpr );
     165                                try {
     166                                        index = constExpr->intValue();
     167                                } catch( SemanticErrorException & ) {
     168                                        SemanticError( expr, "Constant expression of non-integral type in array designator: " );
     169                                }
    167170                        } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
    168171                                setPosition( castExpr->get_arg() );
    169172                        } 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
     173                                EnumInstType * inst = dynamic_cast<EnumInstType *>( varExpr->get_result() );
     174                                assertf( inst, "ArrayIterator given variable that isn't an enum constant : %s", toString( expr ).c_str() );
     175                                long long int value;
     176                                if ( inst->baseEnum->valueOf( varExpr->var, value ) ) {
     177                                        index = value;
     178                                }
    172179                        } else if ( dynamic_cast< SizeofExpr * >( expr ) || dynamic_cast< AlignofExpr * >( expr ) ) {
    173180                                index = 0; // xxx - get actual sizeof/alignof value?
     
    350357                                }
    351358                        }
    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
     359                        if ( atbegin ) {
    354360                                // xxx - what about case of empty struct??
    355361                                // only add self if at the very beginning of the structure
     
    385391                        return *this;
    386392                }
    387                 virtual std::list<InitAlternative> first() const { return std::list<InitAlternative>{}; }
    388393        };
    389394
     
    439444                                return new UnionIterator( uit );
    440445                        } else {
    441                                 assertf( dynamic_cast< TypeInstType * >( type ), "some other reftotype" );
     446                                assertf( dynamic_cast< EnumInstType * >( type ) || dynamic_cast< TypeInstType * >( type ), "Encountered unhandled ReferenceToType in createMemberIterator: %s", toString( type ).c_str() );
    442447                                return new SimpleIterator( type );
    443448                        }
     
    514519                } // for
    515520                if ( desigAlts.size() > 1 ) {
    516                         throw SemanticError( toString("Too many alternatives (", desigAlts.size(), ") for designation: "), designation );
     521                        SemanticError( designation, toString("Too many alternatives (", desigAlts.size(), ") for designation: ") );
    517522                } else if ( desigAlts.size() == 0 ) {
    518                         throw SemanticError( "No reasonable alternatives for designation: ", designation );
     523                        SemanticError( designation, "No reasonable alternatives for designation: " );
    519524                }
    520525                DesignatorChain & d = desigAlts.back();
Note: See TracChangeset for help on using the changeset viewer.