Ignore:
Timestamp:
Dec 1, 2024, 9:04:12 PM (10 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
eae8b37
Parents:
3e2e9b2 (diff), 1c0a3a4 (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' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Validate/ReplaceTypedef.cpp

    r3e2e9b2 r509ec82  
    120120        }
    121121}
    122 
    123122struct VarLenChecker : public ast::WithShortCircuiting {
    124123        bool result = false;
     
    126125        void previsit( ast::ArrayType const * at ) { result |= at->isVarLen; }
    127126};
    128 
     127static bool hasVarLen( const ast::Type * t ) {
     128        return ast::Pass<VarLenChecker>::read( t );
     129}
     130struct ArrayTypeExtractor {
     131        std::vector<const ast::ArrayType *> result;
     132        void postvisit( const ast::ArrayType * at ) {
     133                result.push_back( at );
     134        }
     135};
     136static bool dimensionPresenceMismatched( const ast::Type * t0, const ast::Type * t1) {
     137        std::vector<const ast::ArrayType *> at0s = std::move(
     138                ast::Pass<ArrayTypeExtractor>::read( t0 ) );
     139        std::vector<const ast::ArrayType *> at1s = std::move(
     140                ast::Pass<ArrayTypeExtractor>::read( t1 ) );
     141        assert( at0s.size() == at1s.size() );
     142        for (size_t i = 0; i < at0s.size(); i++) {
     143                const ast::ArrayType * at0 = at0s[i];
     144                const ast::ArrayType * at1 = at1s[i];
     145                assert( ResolvExpr::typesCompatible( at0, at1 ) );
     146                if ( (at0->dimension != nullptr) != (at1->dimension != nullptr) ) return true;
     147        }
     148        return false;
     149}
    129150ast::Decl const * ReplaceTypedefCore::postvisit(
    130151                ast::TypedefDecl const * decl ) {
     
    133154                ast::Type const * t0 = decl->base;
    134155                ast::Type const * t1 = typedefNames[ decl->name ].first->base;
     156                // [hasVarLen]
    135157                // Cannot redefine VLA typedefs. Note: this is slightly incorrect,
    136158                // because our notion of VLAs at this point in the translator is
     
    139161                // constant/enumerator. The effort required to fix this corner case
    140162                // likely outweighs the utility of allowing it.
     163                // [dimensionPresenceMismatched]
     164                // Core typesCompatible logic interprets absent dimensions as wildcards,
     165                // i.e. float[][*] matches float[][42].
     166                // For detecting incompatible typedefs, we have to interpret them verbatim,
     167                // i.e. float[] is different than float[42].
     168                // But typesCompatible does assure that the pair of types is structurally
     169                // consistent, outside of the dimension expressions.  This assurance guards
     170                // the dimension-presence traversal.  So this traversal logic can (and does)
     171                // assume that ArrayTypes will be encountered in analogous places.
    141172                if ( !ResolvExpr::typesCompatible( t0, t1 )
    142                                 || ast::Pass<VarLenChecker>::read( t0 )
    143                                 || ast::Pass<VarLenChecker>::read( t1 ) ) {
     173                                || hasVarLen( t0 )
     174                                || hasVarLen( t1 )
     175                                || dimensionPresenceMismatched( t0, t1 ) ) {
    144176                        SemanticError( decl->location, "Cannot redefine typedef %s", decl->name.c_str() );
    145177                }
Note: See TracChangeset for help on using the changeset viewer.