|
20 | 20 | #include "swift/AST/Decl.h"
|
21 | 21 | #include "swift/AST/Expr.h"
|
22 | 22 | #include "swift/AST/Module.h"
|
| 23 | +#include "swift/AST/ModuleInterfacePrinting.h" |
| 24 | +#include "swift/AST/NameLookup.h" |
23 | 25 | #include "swift/AST/PrintOptions.h"
|
24 | 26 | #include "swift/AST/Stmt.h"
|
25 | 27 | #include "swift/AST/TypeVisitor.h"
|
@@ -1602,3 +1604,39 @@ void TypeBase::print(raw_ostream &OS, const PrintOptions &PO) const {
|
1602 | 1604 | Type(const_cast<TypeBase *>(this)).print(OS, PO);
|
1603 | 1605 | }
|
1604 | 1606 |
|
| 1607 | +void swift::printModuleInterface(Module *M, raw_ostream &OS, |
| 1608 | + const PrintOptions &Options) { |
| 1609 | + auto AdjustedOptions = Options; |
| 1610 | + // Don't print empty curly braces while printing the module interface. |
| 1611 | + AdjustedOptions.FunctionDefinitions = false; |
| 1612 | + |
| 1613 | + SmallVector<ValueDecl *, 32> Decls; |
| 1614 | + VectorDeclConsumer Consumer(Decls); |
| 1615 | + |
| 1616 | + M->lookupVisibleDecls(Module::AccessPathTy(), Consumer, |
| 1617 | + NLKind::UnqualifiedLookup); |
| 1618 | + |
| 1619 | + // Sort the declarations so that we print them in a consistent order. |
| 1620 | + std::sort(Decls.begin(), Decls.end(), |
| 1621 | + [](ValueDecl *LHS, ValueDecl *RHS) { |
| 1622 | + StringRef LHSName = LHS->getName().str(); |
| 1623 | + StringRef RHSName = RHS->getName().str(); |
| 1624 | + if (LHSName.compare(RHSName) < 0) |
| 1625 | + return true; |
| 1626 | + // FIXME: this is not sufficient to establish a total order for overloaded |
| 1627 | + // decls. |
| 1628 | + return RHS->getKind() < RHS->getKind(); |
| 1629 | + }); |
| 1630 | + |
| 1631 | + for (auto *VD : Decls) { |
| 1632 | + VD->print(OS, AdjustedOptions); |
| 1633 | + OS << '\n'; |
| 1634 | + if (auto NTD = dyn_cast<NominalTypeDecl>(VD)) { |
| 1635 | + for (auto Ext : NTD->getExtensions()) { |
| 1636 | + Ext->print(OS, AdjustedOptions); |
| 1637 | + OS << '\n'; |
| 1638 | + } |
| 1639 | + } |
| 1640 | + } |
| 1641 | +} |
| 1642 | + |
0 commit comments