Changeset b67b632


Ignore:
Timestamp:
Oct 5, 2023, 2:49:21 PM (7 months ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
master
Children:
0860d9c
Parents:
8cffa4f (diff), 097c8d0 (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/GenPoly/BoxNew.cpp

    r8cffa4f rb67b632  
    15211521/// * Move polymorphic returns in function types to pointer-type parameters.
    15221522/// * Adds type size and assertion parameters to parameter lists.
    1523 struct DeclAdapter final :
    1524                 public BoxPass,
    1525                 public ast::WithGuards {
    1526         void handleAggrDecl();
    1527 
    1528         void previsit( ast::StructDecl const * decl );
    1529         void previsit( ast::UnionDecl const * decl );
    1530         void previsit( ast::TraitDecl const * decl );
    1531         void previsit( ast::TypeDecl const * decl );
    1532         void previsit( ast::PointerType const * type );
     1523struct DeclAdapter final {
    15331524        ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl );
    15341525        ast::FunctionDecl const * postvisit( ast::FunctionDecl const * decl );
    1535         void previsit( ast::CompoundStmt const * stmt );
    15361526private:
    1537         ast::FunctionDecl * addAdapters( ast::FunctionDecl * decl );
    1538 
    1539         std::map<UniqueId, std::string> adapterName;
     1527        void addAdapters( ast::FunctionDecl * decl, TypeVarMap & localTypeVars );
    15401528};
    1541 
    1542 // at must point within [dst.begin(), dst.end()].
    1543 template< typename T >
    1544 void spliceAt( std::vector< T > & dst, typename std::vector< T >::iterator at,
    1545                 std::vector< T > & src ) {
    1546         std::vector< T > tmp;
    1547         tmp.reserve( dst.size() + src.size() );
    1548         typename std::vector< T >::iterator it = dst.begin();
    1549         while ( it != at ) {
    1550                 assert( it != dst.end() );
    1551                 tmp.emplace_back( std::move( *it ) );
    1552                 ++it;
    1553         }
    1554         for ( T & x : src ) { tmp.emplace_back( std::move( x ) ); }
    1555         while ( it != dst.end() ) {
    1556                 tmp.emplace_back( std::move( *it ) );
    1557                 ++it;
    1558         }
    1559 
    1560         dst.clear();
    1561         src.clear();
    1562         tmp.swap( dst );
    1563 }
    1564 
    1565 void DeclAdapter::previsit( ast::StructDecl const * ) {
    1566         // Prevent type vars from leaking into the containing scope.
    1567         GuardScope( scopeTypeVars );
    1568 }
    1569 
    1570 void DeclAdapter::previsit( ast::UnionDecl const * ) {
    1571         // Prevent type vars from leaking into the containing scope.
    1572         GuardScope( scopeTypeVars );
    1573 }
    1574 
    1575 void DeclAdapter::previsit( ast::TraitDecl const * ) {
    1576         // Prevent type vars from leaking into the containing scope.
    1577         GuardScope( scopeTypeVars );
    1578 }
    1579 
    1580 void DeclAdapter::previsit( ast::TypeDecl const * decl ) {
    1581         addToTypeVarMap( decl, scopeTypeVars );
    1582 }
    1583 
    1584 void DeclAdapter::previsit( ast::PointerType const * type ) {
    1585         GuardScope( scopeTypeVars );
    1586         makeTypeVarMap( type, scopeTypeVars );
    1587 }
    15881529
    15891530// size/align/offset parameters may not be used, so add the unused attribute.
     
    16041545
    16051546ast::FunctionDecl const * DeclAdapter::previsit( ast::FunctionDecl const * decl ) {
    1606         GuardScope( scopeTypeVars );
    1607         makeTypeVarMap( decl, scopeTypeVars );
     1547        TypeVarMap localTypeVars = { ast::TypeData() };
     1548        makeTypeVarMap( decl, localTypeVars );
    16081549
    16091550        auto mutDecl = mutate( decl );
     
    16201561
    16211562        // Add size/align and assertions for type parameters to parameter list.
    1622         ast::vector<ast::DeclWithType>::iterator last = mutDecl->params.begin();
    16231563        ast::vector<ast::DeclWithType> inferredParams;
     1564        ast::vector<ast::DeclWithType> layoutParams;
    16241565        for ( ast::ptr<ast::TypeDecl> & typeParam : mutDecl->type_params ) {
    16251566                auto mutParam = mutate( typeParam.get() );
     
    16301571
    16311572                        auto sizeParam = makeObj( typeParam->location, sizeofName( paramName ) );
    1632                         last = mutDecl->params.insert( last, sizeParam );
    1633                         ++last;
     1573                        layoutParams.emplace_back( sizeParam );
    16341574
    16351575                        auto alignParam = makeObj( typeParam->location, alignofName( paramName ) );
    1636                         last = mutDecl->params.insert( last, alignParam );
    1637                         ++last;
     1576                        layoutParams.emplace_back( alignParam );
    16381577                }
    16391578                // TODO: These should possibly all be gone.
     
    16631602        ast::vector<ast::DeclWithType> otypeParams;
    16641603        for ( ast::ptr<ast::DeclWithType> & funcParam : mutDecl->params ) {
    1665                 ast::Type const * polyType = isPolyType( funcParam->get_type(), scopeTypeVars );
     1604                ast::Type const * polyType = isPolyType( funcParam->get_type(), localTypeVars );
    16661605                if ( !polyType || dynamic_cast<ast::TypeInstType const *>( polyType ) ) {
    16671606                        continue;
     
    16691608                std::string typeName = Mangle::mangleType( polyType );
    16701609                if ( seenTypes.count( typeName ) ) continue;
     1610                seenTypes.insert( typeName );
    16711611
    16721612                auto sizeParam = makeObj( funcParam->location, sizeofName( typeName ) );
     
    16761616                otypeParams.emplace_back( alignParam );
    16771617
     1618                // Zero-length arrays are illegal in C, so empty structs have no
     1619                // offset array.
    16781620                if ( auto * polyStruct =
    1679                                 dynamic_cast<ast::StructInstType const *>( polyType ) ) {
    1680                         // Zero-length arrays are illegal in C, so empty structs have no
    1681                         // offset array.
    1682                         if ( !polyStruct->base->members.empty() ) {
    1683                                 auto offsetParam = makePtr( funcParam->location, offsetofName( typeName ) );
    1684                                 otypeParams.emplace_back( offsetParam );
    1685                         }
    1686                 }
    1687                 seenTypes.insert( typeName );
    1688         }
    1689 
    1690         // TODO: A unified way of putting these together might be nice.
    1691         // Put the list together: adapters (in helper) otype parameters,
    1692         // inferred params., layout params. (done) and finally explicit params.
    1693         spliceBegin( inferredParams, otypeParams );
    1694         spliceAt( mutDecl->params, last, inferredParams );
    1695         mutDecl = addAdapters( mutDecl );
     1621                                dynamic_cast<ast::StructInstType const *>( polyType ) ;
     1622                                polyStruct && !polyStruct->base->members.empty() ) {
     1623                        auto offsetParam = makePtr( funcParam->location, offsetofName( typeName ) );
     1624                        otypeParams.emplace_back( offsetParam );
     1625                }
     1626        }
     1627
     1628        // Prepend each argument group. From last group to first. addAdapters
     1629        // does do the same, it just does it itself and see all other parameters.
     1630        spliceBegin( mutDecl->params, inferredParams );
     1631        spliceBegin( mutDecl->params, otypeParams );
     1632        spliceBegin( mutDecl->params, layoutParams );
     1633        addAdapters( mutDecl, localTypeVars );
    16961634
    16971635        return mutDecl;
     
    17321670}
    17331671
    1734 void DeclAdapter::previsit( ast::CompoundStmt const * ) {
    1735         GuardScope( scopeTypeVars );
    1736         // TODO: It is entirely possible the scope doesn't need to spread
    1737         // across multiple functions. Otherwise, find a better clear.
    1738         std::set<TypeVarMap::key_type> keys;
    1739         for ( auto pair : const_cast<TypeVarMap const &>( scopeTypeVars ) ) {
    1740                 keys.insert( pair.first );
    1741         }
    1742         for ( auto key : keys ) {
    1743                 scopeTypeVars.erase( key );
    1744         }
    1745 }
    1746 
    1747 // It actually does mutate in-place, but does the return for consistency.
    1748 ast::FunctionDecl * DeclAdapter::addAdapters( ast::FunctionDecl * mutDecl ) {
     1672void DeclAdapter::addAdapters(
     1673                ast::FunctionDecl * mutDecl, TypeVarMap & localTypeVars ) {
    17491674        ast::vector<ast::FunctionType> functions;
    17501675        for ( ast::ptr<ast::DeclWithType> & arg : mutDecl->params ) {
    17511676                ast::Type const * type = arg->get_type();
    1752                 type = findAndReplaceFunction( type, functions, scopeTypeVars, needsAdapter );
     1677                type = findAndReplaceFunction( type, functions, localTypeVars, needsAdapter );
    17531678                arg.get_and_mutate()->set_type( type );
    17541679        }
    17551680        std::set<std::string> adaptersDone;
    17561681        for ( ast::ptr<ast::FunctionType> const & func : functions ) {
    1757                 std::string mangleName = mangleAdapterName( func, scopeTypeVars );
     1682                std::string mangleName = mangleAdapterName( func, localTypeVars );
    17581683                if ( adaptersDone.find( mangleName ) != adaptersDone.end() ) {
    17591684                        continue;
     
    17631688                mutDecl->params.insert( mutDecl->params.begin(), new ast::ObjectDecl(
    17641689                        mutDecl->location, adapterName,
    1765                         new ast::PointerType( makeAdapterType( func, scopeTypeVars ) ),
     1690                        new ast::PointerType( makeAdapterType( func, localTypeVars ) ),
    17661691                        nullptr, {}, {}, nullptr,
    17671692                        { new ast::Attribute( "unused" ) } ) );
    17681693                adaptersDone.insert( adaptersDone.begin(), mangleName );
    17691694        }
    1770         return mutDecl;
    17711695}
    17721696
Note: See TracChangeset for help on using the changeset viewer.