Ignore:
Timestamp:
Jun 9, 2023, 4:56:02 PM (12 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ast-experimental, master
Children:
38e266c
Parents:
758c9ef
Message:

Improved hoisting. However, I had to change some code because of other errors, so more work will have to be done. Also folded in another invariant for MemberExpr?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Validate/HoistStruct.cpp

    r758c9ef r0658672  
    1818#include <sstream>
    1919
     20#include "AST/DeclReplacer.hpp"
    2021#include "AST/Pass.hpp"
    2122#include "AST/TranslationUnit.hpp"
     23#include "AST/Vector.hpp"
    2224
    2325namespace Validate {
     
    5153        template<typename AggrDecl>
    5254        AggrDecl const * postAggregate( AggrDecl const * );
     55        template<typename InstType>
     56        InstType const * preCollectionInstType( InstType const * type );
    5357
    5458        ast::AggregateDecl const * parent = nullptr;
     
    6670        qualifiedName( decl, ss );
    6771        return ss.str();
     72}
     73
     74void extendParams( ast::vector<ast::TypeDecl> & dstParams,
     75                ast::vector<ast::TypeDecl> const & srcParams ) {
     76        if ( srcParams.empty() ) return;
     77
     78        ast::DeclReplacer::TypeMap newToOld;
     79        ast::vector<ast::TypeDecl> params;
     80        for ( ast::ptr<ast::TypeDecl> const & srcParam : srcParams ) {
     81                ast::TypeDecl * dstParam = ast::deepCopy( srcParam.get() );
     82                dstParam->init = nullptr;
     83                newToOld.emplace( srcParam, dstParam );
     84                for ( auto assertion : dstParam->assertions ) {
     85                        assertion = ast::DeclReplacer::replace( assertion, newToOld );
     86                }
     87                params.emplace_back( dstParam );
     88        }
     89        spliceBegin( dstParams, params );
    6890}
    6991
     
    7496                mut->parent = parent;
    7597                mut->name = qualifiedName( mut );
    76                 return mut;
    77         } else {
    78                 GuardValue( parent ) = decl;
    79                 return decl;
    80         }
     98                extendParams( mut->params, parent->params );
     99                decl = mut;
     100        }
     101        GuardValue( parent ) = decl;
     102        return decl;
    81103}
    82104
     
    112134}
    113135
     136ast::AggregateDecl const * commonParent(
     137                ast::AggregateDecl const * lhs, ast::AggregateDecl const * rhs ) {
     138        for ( auto outer = lhs ; outer ; outer = outer->parent ) {
     139                for ( auto inner = rhs ; inner ; inner = inner->parent ) {
     140                        if ( outer == inner ) {
     141                                return outer;
     142                        }
     143                }
     144        }
     145        return nullptr;
     146}
     147
     148template<typename InstType>
     149InstType const * HoistStructCore::preCollectionInstType( InstType const * type ) {
     150    if ( !type->base->parent ) return type;
     151    if ( type->base->params.empty() ) return type;
     152
     153    InstType * mut = ast::mutate( type );
     154    ast::AggregateDecl const * parent =
     155        commonParent( this->parent, mut->base->parent );
     156    assert( parent );
     157
     158    std::vector<ast::ptr<ast::Expr>> args;
     159    for ( const ast::ptr<ast::TypeDecl> & param : parent->params ) {
     160        args.emplace_back( new ast::TypeExpr( param->location,
     161            new ast::TypeInstType( param )
     162        ) );
     163    }
     164    spliceBegin( mut->params, args );
     165    return mut;
     166}
     167
    114168template<typename InstType>
    115169InstType const * preInstType( InstType const * type ) {
     
    121175
    122176ast::StructInstType const * HoistStructCore::previsit( ast::StructInstType const * type ) {
    123         return preInstType( type );
     177        return preInstType( preCollectionInstType( type ) );
    124178}
    125179
    126180ast::UnionInstType const * HoistStructCore::previsit( ast::UnionInstType const * type ) {
    127         return preInstType( type );
     181        return preInstType( preCollectionInstType( type ) );
    128182}
    129183
Note: See TracChangeset for help on using the changeset viewer.