Skip to content

Commit 0204561

Browse files
committed
[Frontend] Factor out textual interface generation into its own file
No functionality change.
1 parent 1dd415a commit 0204561

File tree

6 files changed

+83
-12
lines changed

6 files changed

+83
-12
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,12 @@ struct PrintOptions {
424424
return result;
425425
}
426426

427+
/// Retrieve the set of options suitable for stable textual interfaces.
428+
///
429+
/// This is a format that will be parsed again later, so the output must be
430+
/// consistent and well-formed.
431+
///
432+
/// \see swift::emitModuleInterface
427433
static PrintOptions printTextualInterfaceFile();
428434

429435
static PrintOptions printModuleInterface();

include/swift/Basic/SupplementaryOutputPaths.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ struct SupplementaryOutputPaths {
121121
///
122122
/// Currently only makes sense when the compiler has whole-module knowledge.
123123
///
124-
/// \sa ModuleOutputPath
124+
/// \sa swift::emitModuleInterface
125125
std::string ModuleInterfaceOutputPath;
126126

127127
SupplementaryOutputPaths() = default;

lib/FrontendTool/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_swift_library(swiftFrontendTool STATIC
33
ImportedModules.cpp
44
ReferenceDependencies.cpp
55
TBD.cpp
6+
TextualInterfaceGeneration.cpp
67
DEPENDS
78
swift-syntax-generated-headers SwiftOptions
89
LINK_LIBRARIES

lib/FrontendTool/FrontendTool.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "ImportedModules.h"
2525
#include "ReferenceDependencies.h"
2626
#include "TBD.h"
27+
#include "TextualInterfaceGeneration.h"
2728

2829
#include "swift/Subsystems.h"
2930
#include "swift/AST/ASTScope.h"
@@ -358,21 +359,14 @@ static bool printAsObjCIfNeeded(StringRef outputPath, ModuleDecl *M,
358359
/// ...unless \p outputPath is empty, in which case it does nothing.
359360
///
360361
/// \returns true if there were any errors
362+
///
363+
/// \see swift::emitModuleInterface
361364
static bool printModuleInterfaceIfNeeded(StringRef outputPath, ModuleDecl *M) {
362365
if (outputPath.empty())
363366
return false;
364367
return atomicallyWritingToTextFile(outputPath, M->getDiags(),
365-
[&](raw_ostream &out) -> bool {
366-
auto printOptions = PrintOptions::printTextualInterfaceFile();
367-
SmallVector<Decl *, 16> topLevelDecls;
368-
M->getTopLevelDecls(topLevelDecls);
369-
for (const Decl *D : topLevelDecls) {
370-
if (!D->shouldPrintInContext(printOptions))
371-
continue;
372-
D->print(out, printOptions);
373-
out << "\n";
374-
}
375-
return false;
368+
[M](raw_ostream &out) -> bool {
369+
return swift::emitModuleInterface(out, M);
376370
});
377371
}
378372

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===--- TextualInterfaceGeneration.cpp - swiftinterface files ------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "TextualInterfaceGeneration.h"
14+
15+
#include "swift/AST/Decl.h"
16+
#include "swift/AST/Module.h"
17+
18+
using namespace swift;
19+
20+
bool swift::emitModuleInterface(raw_ostream &out, ModuleDecl *M) {
21+
const PrintOptions printOptions = PrintOptions::printTextualInterfaceFile();
22+
SmallVector<Decl *, 16> topLevelDecls;
23+
M->getTopLevelDecls(topLevelDecls);
24+
for (const Decl *D : topLevelDecls) {
25+
if (!D->shouldPrintInContext(printOptions))
26+
continue;
27+
D->print(out, printOptions);
28+
out << "\n";
29+
}
30+
return false;
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===--- TextualInterfaceGeneration.h - swiftinterface files ----*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_FRONTENDTOOL_TEXTUALINTERFACEGENERATION_H
14+
#define SWIFT_FRONTENDTOOL_TEXTUALINTERFACEGENERATION_H
15+
16+
#include "swift/Basic/LLVM.h"
17+
18+
namespace swift {
19+
20+
class ModuleDecl;
21+
22+
/// Emit a stable, textual interface for \p M, which can be used by a client
23+
/// source file to import this module.
24+
///
25+
/// Unlike a serialized module, the textual format generated by
26+
/// emitModuleInterface is intended to be stable across compiler versions while
27+
/// still describing the full ABI of the module in question.
28+
///
29+
/// The initial plan for this format can be found at
30+
/// https://forums.swift.org/t/plan-for-module-stability/14551/
31+
///
32+
/// \return true if an error occurred
33+
///
34+
/// \sa swift::serialize
35+
bool emitModuleInterface(raw_ostream &out, ModuleDecl *M);
36+
37+
} // end namespace swift
38+
39+
#endif

0 commit comments

Comments
 (0)