Changeset b67b632
- Timestamp:
- Oct 5, 2023, 2:49:21 PM (14 months ago)
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/BoxNew.cpp
r8cffa4f rb67b632 1521 1521 /// * Move polymorphic returns in function types to pointer-type parameters. 1522 1522 /// * 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 ); 1523 struct DeclAdapter final { 1533 1524 ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl ); 1534 1525 ast::FunctionDecl const * postvisit( ast::FunctionDecl const * decl ); 1535 void previsit( ast::CompoundStmt const * stmt );1536 1526 private: 1537 ast::FunctionDecl * addAdapters( ast::FunctionDecl * decl ); 1538 1539 std::map<UniqueId, std::string> adapterName; 1527 void addAdapters( ast::FunctionDecl * decl, TypeVarMap & localTypeVars ); 1540 1528 }; 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 }1588 1529 1589 1530 // size/align/offset parameters may not be used, so add the unused attribute. … … 1604 1545 1605 1546 ast::FunctionDecl const * DeclAdapter::previsit( ast::FunctionDecl const * decl ) { 1606 GuardScope( scopeTypeVars );1607 makeTypeVarMap( decl, scopeTypeVars );1547 TypeVarMap localTypeVars = { ast::TypeData() }; 1548 makeTypeVarMap( decl, localTypeVars ); 1608 1549 1609 1550 auto mutDecl = mutate( decl ); … … 1620 1561 1621 1562 // Add size/align and assertions for type parameters to parameter list. 1622 ast::vector<ast::DeclWithType>::iterator last = mutDecl->params.begin();1623 1563 ast::vector<ast::DeclWithType> inferredParams; 1564 ast::vector<ast::DeclWithType> layoutParams; 1624 1565 for ( ast::ptr<ast::TypeDecl> & typeParam : mutDecl->type_params ) { 1625 1566 auto mutParam = mutate( typeParam.get() ); … … 1630 1571 1631 1572 auto sizeParam = makeObj( typeParam->location, sizeofName( paramName ) ); 1632 last = mutDecl->params.insert( last, sizeParam ); 1633 ++last; 1573 layoutParams.emplace_back( sizeParam ); 1634 1574 1635 1575 auto alignParam = makeObj( typeParam->location, alignofName( paramName ) ); 1636 last = mutDecl->params.insert( last, alignParam ); 1637 ++last; 1576 layoutParams.emplace_back( alignParam ); 1638 1577 } 1639 1578 // TODO: These should possibly all be gone. … … 1663 1602 ast::vector<ast::DeclWithType> otypeParams; 1664 1603 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 ); 1666 1605 if ( !polyType || dynamic_cast<ast::TypeInstType const *>( polyType ) ) { 1667 1606 continue; … … 1669 1608 std::string typeName = Mangle::mangleType( polyType ); 1670 1609 if ( seenTypes.count( typeName ) ) continue; 1610 seenTypes.insert( typeName ); 1671 1611 1672 1612 auto sizeParam = makeObj( funcParam->location, sizeofName( typeName ) ); … … 1676 1616 otypeParams.emplace_back( alignParam ); 1677 1617 1618 // Zero-length arrays are illegal in C, so empty structs have no 1619 // offset array. 1678 1620 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 ); 1696 1634 1697 1635 return mutDecl; … … 1732 1670 } 1733 1671 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 ) { 1672 void DeclAdapter::addAdapters( 1673 ast::FunctionDecl * mutDecl, TypeVarMap & localTypeVars ) { 1749 1674 ast::vector<ast::FunctionType> functions; 1750 1675 for ( ast::ptr<ast::DeclWithType> & arg : mutDecl->params ) { 1751 1676 ast::Type const * type = arg->get_type(); 1752 type = findAndReplaceFunction( type, functions, scopeTypeVars, needsAdapter );1677 type = findAndReplaceFunction( type, functions, localTypeVars, needsAdapter ); 1753 1678 arg.get_and_mutate()->set_type( type ); 1754 1679 } 1755 1680 std::set<std::string> adaptersDone; 1756 1681 for ( ast::ptr<ast::FunctionType> const & func : functions ) { 1757 std::string mangleName = mangleAdapterName( func, scopeTypeVars );1682 std::string mangleName = mangleAdapterName( func, localTypeVars ); 1758 1683 if ( adaptersDone.find( mangleName ) != adaptersDone.end() ) { 1759 1684 continue; … … 1763 1688 mutDecl->params.insert( mutDecl->params.begin(), new ast::ObjectDecl( 1764 1689 mutDecl->location, adapterName, 1765 new ast::PointerType( makeAdapterType( func, scopeTypeVars ) ),1690 new ast::PointerType( makeAdapterType( func, localTypeVars ) ), 1766 1691 nullptr, {}, {}, nullptr, 1767 1692 { new ast::Attribute( "unused" ) } ) ); 1768 1693 adaptersDone.insert( adaptersDone.begin(), mangleName ); 1769 1694 } 1770 return mutDecl;1771 1695 } 1772 1696
Note: See TracChangeset
for help on using the changeset viewer.