Skip to content

Commit 8fcb7c4

Browse files
committed
AST printer: print declarations from a module in alphabetical order
Also, move module interface printing to libAST to make it reuseable. Swift SVN r10932
1 parent 7d9c88d commit 8fcb7c4

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===--- ModuleInterfacePrinting.h - Routines to print module interface ---===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_AST_MODULE_INTERFACE_PRINTING_H
14+
#define SWIFT_AST_MODULE_INTERFACE_PRINTING_H
15+
16+
#include "swift/Basic/LLVM.h"
17+
18+
namespace swift {
19+
class Module;
20+
struct PrintOptions;
21+
22+
void printModuleInterface(Module *M, raw_ostream &OS,
23+
const PrintOptions &Options);
24+
25+
} // namespace swift
26+
27+
#endif // SWIFT_AST_MODULE_INTERFACE_PRINTING_H
28+

lib/AST/ASTPrinter.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "swift/AST/Decl.h"
2121
#include "swift/AST/Expr.h"
2222
#include "swift/AST/Module.h"
23+
#include "swift/AST/ModuleInterfacePrinting.h"
24+
#include "swift/AST/NameLookup.h"
2325
#include "swift/AST/PrintOptions.h"
2426
#include "swift/AST/Stmt.h"
2527
#include "swift/AST/TypeVisitor.h"
@@ -1602,3 +1604,39 @@ void TypeBase::print(raw_ostream &OS, const PrintOptions &PO) const {
16021604
Type(const_cast<TypeBase *>(this)).print(OS, PO);
16031605
}
16041606

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

Comments
 (0)