Changeset fc12f05 for src/GenPoly/BoxNew.cpp
- Timestamp:
- Nov 13, 2023, 3:43:43 AM (2 years ago)
- Branches:
- master
- Children:
- 25f2798
- Parents:
- 0030b508 (diff), 2174191 (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
r0030b508 rfc12f05 39 39 40 40 namespace { 41 42 /// Common field of several sub-passes of box.43 struct BoxPass {44 TypeVarMap scopeTypeVars;45 BoxPass() : scopeTypeVars( ast::TypeData() ) {}46 };47 48 // TODO: Could this be a common helper somewhere?49 ast::FunctionType * makeFunctionType( ast::FunctionDecl const * decl ) {50 ast::FunctionType * type = new ast::FunctionType(51 decl->type->isVarArgs, decl->type->qualifiers52 );53 for ( auto type_param : decl->type_params ) {54 type->forall.emplace_back( new ast::TypeInstType( type_param ) );55 }56 for ( auto assertion : decl->assertions ) {57 type->assertions.emplace_back( new ast::VariableExpr(58 assertion->location, assertion ) );59 }60 for ( auto param : decl->params ) {61 type->params.emplace_back( param->get_type() );62 }63 for ( auto retval : decl->returns ) {64 type->returns.emplace_back( retval->get_type() );65 }66 return type;67 }68 41 69 42 // -------------------------------------------------------------------------- … … 359 332 /// * Adds appropriate type variables to the function calls. 360 333 struct CallAdapter final : 361 public BoxPass,362 334 public ast::WithConstTypeSubstitution, 363 335 public ast::WithGuards, … … 376 348 ast::Expr const * postvisit( ast::AddressExpr const * expr ); 377 349 ast::ReturnStmt const * previsit( ast::ReturnStmt const * stmt ); 378 void previsit( ast::PointerType const * type );379 void previsit( ast::FunctionType const * type );380 350 381 351 void beginScope(); … … 440 410 CodeLocation const & location, ast::Type const * type ); 441 411 442 /// Set of adapter functions in the current scope.412 TypeVarMap scopeTypeVars; 443 413 ScopedMap< std::string, ast::DeclWithType const * > adapters; 444 414 std::map< ast::ApplicationExpr const *, ast::Expr const * > retVals; … … 553 523 554 524 ast::FunctionDecl const * CallAdapter::previsit( ast::FunctionDecl const * decl ) { 525 // Prevent type declaration information from leaking out. 526 GuardScope( scopeTypeVars ); 527 555 528 if ( nullptr == decl->stmts ) { 556 // This may keep TypeDecls we don't ever want from sneaking in.557 // Not visiting child nodes might just be faster.558 GuardScope( scopeTypeVars );559 529 return decl; 560 530 } 561 531 562 GuardScope( scopeTypeVars );563 532 GuardValue( retval ); 564 533 … … 662 631 ptrdiff_t initArgCount = mutExpr->args.size(); 663 632 664 TypeVarMap exprTypeVars = { ast::TypeData() };633 TypeVarMap exprTypeVars; 665 634 // TODO: Should this take into account the variables already bound in 666 635 // scopeTypeVars ([ex] remove them from exprTypeVars)? … … 687 656 688 657 assert( typeSubs ); 689 ast::Type const * concRetType = replaceWithConcrete( dynRetType, *typeSubs );690 // Used to use dynRetType instead of concRetType; this changed so that691 // the correct type parameters are passed for return types (it should be692 // the concrete type's parameters, not the formal type's).693 658 ast::vector<ast::Expr>::iterator argIt = 694 659 passTypeVars( mutExpr, function ); … … 768 733 } 769 734 return stmt; 770 }771 772 void CallAdapter::previsit( ast::PointerType const * type ) {773 GuardScope( scopeTypeVars );774 makeTypeVarMap( type, scopeTypeVars );775 }776 777 void CallAdapter::previsit( ast::FunctionType const * type ) {778 GuardScope( scopeTypeVars );779 makeTypeVarMap( type, scopeTypeVars );780 735 } 781 736 … … 1427 1382 1428 1383 ast::FunctionDecl const * DeclAdapter::previsit( ast::FunctionDecl const * decl ) { 1429 TypeVarMap localTypeVars = { ast::TypeData() };1384 TypeVarMap localTypeVars; 1430 1385 makeTypeVarMap( decl, localTypeVars ); 1431 1386 … … 1458 1413 layoutParams.emplace_back( alignParam ); 1459 1414 } 1460 // TODO: These should possibly all be gone. 1461 // More all assertions into parameter list. 1462 for ( ast::ptr<ast::DeclWithType> & assert : mutParam->assertions ) { 1463 // Assertion parameters may not be used in body, 1464 // pass along with unused attribute. 1465 assert.get_and_mutate()->attributes.push_back( 1466 new ast::Attribute( "unused" ) ); 1467 inferredParams.push_back( assert ); 1468 } 1469 mutParam->assertions.clear(); 1415 // Assertions should be stored in the main list. 1416 assert( mutParam->assertions.empty() ); 1470 1417 typeParam = mutParam; 1471 1418 } 1472 // TODO: New version of inner loop.1473 1419 for ( ast::ptr<ast::DeclWithType> & assert : mutDecl->assertions ) { 1474 1420 // Assertion parameters may not be used in body, … … 1485 1431 spliceBegin( mutDecl->params, layoutParams ); 1486 1432 addAdapters( mutDecl, localTypeVars ); 1433 1434 // Now have to update the type to match the declaration. 1435 ast::FunctionType * type = new ast::FunctionType( 1436 mutDecl->type->isVarArgs, mutDecl->type->qualifiers ); 1437 for ( auto type_param : mutDecl->type_params ) { 1438 type->forall.emplace_back( new ast::TypeInstType( type_param ) ); 1439 } 1440 for ( auto param : mutDecl->params ) { 1441 type->params.emplace_back( param->get_type() ); 1442 } 1443 for ( auto retval : mutDecl->returns ) { 1444 type->returns.emplace_back( retval->get_type() ); 1445 } 1446 mutDecl->type = type; 1487 1447 1488 1448 return mutDecl; … … 1518 1478 } 1519 1479 } 1520 // TODO: Can this be updated as we go along?1521 mutDecl->type = makeFunctionType( mutDecl );1522 1480 return mutDecl; 1523 1481 } … … 1575 1533 assertf( it != adapters.end(), "Could not correct floating node." ); 1576 1534 return ast::mutate_field( expr, &ast::VariableExpr::var, it->second ); 1577 1578 1535 } 1579 1536 … … 1587 1544 /// * Inserts dynamic calculation of polymorphic type layouts where needed. 1588 1545 struct PolyGenericCalculator final : 1589 public BoxPass,1590 1546 public ast::WithConstTypeSubstitution, 1591 1547 public ast::WithDeclsToAdd<>, … … 1595 1551 PolyGenericCalculator(); 1596 1552 1597 void previsit( ast::ObjectDecl const * decl );1598 1553 void previsit( ast::FunctionDecl const * decl ); 1599 1554 void previsit( ast::TypedefDecl const * decl ); … … 1602 1557 ast::StructDecl const * previsit( ast::StructDecl const * decl ); 1603 1558 ast::UnionDecl const * previsit( ast::UnionDecl const * decl ); 1604 void previsit( ast::PointerType const * type );1605 void previsit( ast::FunctionType const * type );1606 1559 ast::DeclStmt const * previsit( ast::DeclStmt const * stmt ); 1607 1560 ast::Expr const * postvisit( ast::MemberExpr const * expr ); … … 1635 1588 /// C sizeof(). 1636 1589 ast::Expr const * genSizeof( CodeLocation const &, ast::Type const * ); 1637 1638 1590 /// Enters a new scope for type-variables, 1639 1591 /// adding the type variables from the provided type. 1640 1592 void beginTypeScope( ast::Type const * ); 1641 /// Enters a new scope for known layouts and offsets, and queues exit calls. 1642 void beginGenericScope();1643 1593 1594 /// The type variables and polymorphic parameters currently in scope. 1595 TypeVarMap scopeTypeVars; 1644 1596 /// Set of generic type layouts known in the current scope, 1645 1597 /// indexed by sizeofName. … … 1652 1604 /// If the argument of an AddressExpr is MemberExpr, it is stored here. 1653 1605 ast::MemberExpr const * addrMember = nullptr; 1654 /// Used to avoid recursing too deep in type declarations.1655 bool expect_func_type = false;1656 1606 }; 1657 1607 … … 1675 1625 } 1676 1626 1677 void PolyGenericCalculator::previsit( ast::ObjectDecl const * decl ) {1678 beginTypeScope( decl->type );1679 }1680 1681 1627 void PolyGenericCalculator::previsit( ast::FunctionDecl const * decl ) { 1682 beginGenericScope();1628 GuardScope( *this ); 1683 1629 beginTypeScope( decl->type ); 1684 1630 } … … 1696 1642 ast::TypeDecl const * decl ) { 1697 1643 ast::Type const * base = decl->base; 1698 if ( nullptr == base ) return decl;1644 if ( nullptr == base ) return decl; 1699 1645 1700 1646 // Add size/align variables for opaque type declarations. … … 1721 1667 alignDecl->accept( *visitor ); 1722 1668 1723 // Can't use [makeVar], because it inserts into stmtsToAdd and TypeDecls1724 // can occur at global scope.1669 // A little trick to replace this with two declarations. 1670 // Adding after makes sure that there is no conflict with adding stmts. 1725 1671 declsToAddAfter.push_back( alignDecl ); 1726 // replace with sizeDecl.1727 1672 return sizeDecl; 1728 1673 } … … 1742 1687 } 1743 1688 1744 void PolyGenericCalculator::previsit( ast::PointerType const * type ) {1745 beginTypeScope( type );1746 }1747 1748 void PolyGenericCalculator::previsit( ast::FunctionType const * type ) {1749 beginTypeScope( type );1750 1751 GuardValue( expect_func_type );1752 GuardScope( *this );1753 1754 // The other functions type we will see in this scope are probably1755 // function parameters they don't help us with the layout and offsets so1756 // don't mark them as known in this scope.1757 expect_func_type = false;1758 }1759 1760 //void PolyGenericCalculator::previsit( ast::DeclStmt const * stmt ) {1761 1689 ast::DeclStmt const * PolyGenericCalculator::previsit( ast::DeclStmt const * stmt ) { 1762 1690 ast::ObjectDecl const * decl = stmt->decl.as<ast::ObjectDecl>(); … … 1766 1694 1767 1695 // Change initialization of a polymorphic value object to allocate via a 1768 // variable-length-array (alloca was previouly used, but it cannot be 1769 // safely used in loops). 1696 // variable-length-array (alloca cannot be safely used in loops). 1770 1697 ast::ObjectDecl * newBuf = new ast::ObjectDecl( decl->location, 1771 1698 bufNamer.newName(), … … 2248 2175 } 2249 2176 2250 void PolyGenericCalculator::beginGenericScope() {2251 GuardScope( *this );2252 // We expect the first function type see to be the type relating to this2253 // scope but any further type is probably some unrelated function pointer2254 // keep track of whrich is the first.2255 GuardValue( expect_func_type ) = true;2256 }2257 2258 2177 // -------------------------------------------------------------------------- 2259 /// No common theme found.2178 /// Removes unneeded or incorrect type information. 2260 2179 /// * Replaces initialization of polymorphic values with alloca. 2261 2180 /// * Replaces declaration of dtype/ftype with appropriate void expression. … … 2263 2182 /// * Strips fields from generic structure declarations. 2264 2183 struct Eraser final : 2265 public BoxPass,2266 2184 public ast::WithGuards { 2267 2185 void guardTypeVarMap( ast::Type const * type ) { … … 2278 2196 void previsit( ast::PointerType const * type ); 2279 2197 void previsit( ast::FunctionType const * type ); 2198 public: 2199 TypeVarMap scopeTypeVars; 2280 2200 }; 2281 2201
Note:
See TracChangeset
for help on using the changeset viewer.