Skip to content

Commit 95eeabb

Browse files
committed
Move the -frontend logic into its own library to allow re-use
and abstraction (to come).
1 parent 78db42e commit 95eeabb

File tree

7 files changed

+57
-19
lines changed

7 files changed

+57
-19
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ namespace swift {
4747

4848
class SerializedModuleLoader;
4949

50+
/// The abstract configuration of the compiler, including:
51+
/// - options for all stages of translation,
52+
/// - information about the build environment,
53+
/// - information about the job being performed, and
54+
/// - lists of inputs.
55+
///
56+
/// A CompilerInvocation can be built from a frontend command line
57+
/// using parseArgs. It can then be used to build a CompilerInstance,
58+
/// which manages the actual compiler execution.
5059
class CompilerInvocation {
5160
LangOptions LangOpts;
5261
FrontendOptions FrontendOpts;
@@ -282,6 +291,14 @@ class CompilerInvocation {
282291
}
283292
};
284293

294+
/// A class which manages the state and execution of the compiler.
295+
/// This owns the primary compiler singletons, such as the ASTContext,
296+
/// as well as various build products such as the SILModule.
297+
///
298+
/// Before a CompilerInstance can be used, it must be configured by
299+
/// calling \a setup. If successful, this will create an ASTContext
300+
/// and set up the basic compiler invariants. Calling \a setup multiple
301+
/// times on a single CompilerInstance is not permitted.
285302
class CompilerInstance {
286303
CompilerInvocation Invocation;
287304
SourceManager SourceMgr;

include/swift/Subsystems.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ namespace swift {
8888

8989
/// @}
9090

91+
/// Perform all the operations of the frontend, exactly as if invoked
92+
/// with -frontend.
93+
///
94+
/// \param args the arguments to use as the arguments to the frontend
95+
/// \param argv0 the name used as the frontend executable
96+
/// \param mainAddr an address from the main executable
97+
///
98+
/// \return the exit value of the frontend: 0 or 1 on success unless
99+
/// the frontend executes in immediate mode, in which case this will be
100+
/// the exit value of the script, assuming it exits normally
101+
int performFrontend(ArrayRef<const char *> args,
102+
const char *argv0,
103+
void *mainAddr);
104+
91105
/// \brief Parse a single buffer into the given source file.
92106
///
93107
/// If the source file is the main file, stop parsing after the next

lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ add_subdirectory(Basic)
44
add_subdirectory(ClangImporter)
55
add_subdirectory(Driver)
66
add_subdirectory(Frontend)
7+
add_subdirectory(FrontendTool)
78
add_subdirectory(Index)
89
add_subdirectory(IDE)
910
add_subdirectory(Immediate)

lib/FrontendTool/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
add_swift_library(swiftFrontendTool
2+
FrontendTool.cpp
3+
LINK_LIBRARIES
4+
swiftIDE
5+
swiftIRGen swiftSIL swiftSILGen swiftSILOptimizer
6+
swiftImmediate
7+
swiftSerialization
8+
swiftPrintAsObjC
9+
swiftFrontend
10+
swiftClangImporter
11+
swiftOption
12+
clangAPINotes
13+
)

tools/driver/frontend_main.cpp renamed to lib/FrontendTool/FrontendTool.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- frontend_main.cpp - Swift Compiler Frontend ----------------------===//
1+
//===--- FrontendTool.cpp - Swift Compiler Frontend ----------------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -15,6 +15,9 @@
1515
/// implements the core compiler functionality along with a number of additional
1616
/// tools for demonstration and testing purposes.
1717
///
18+
/// This is separate from the rest of libFrontend to reduce the dependencies
19+
/// required by that library.
20+
///
1821
//===----------------------------------------------------------------------===//
1922

2023
#include "swift/Subsystems.h"
@@ -993,8 +996,8 @@ static bool dumpAPI(Module *Mod, StringRef OutDir) {
993996
return false;
994997
}
995998

996-
int frontend_main(ArrayRef<const char *>Args,
997-
const char *Argv0, void *MainAddr) {
999+
int swift::performFrontend(ArrayRef<const char *> Args,
1000+
const char *Argv0, void *MainAddr) {
9981001
llvm::InitializeAllTargets();
9991002
llvm::InitializeAllTargetMCs();
10001003
llvm::InitializeAllAsmPrinters();

tools/driver/CMakeLists.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@ add_swift_executable(swift
22
api_notes.cpp
33
driver.cpp
44
autolink_extract_main.cpp
5-
frontend_main.cpp
65
modulewrap_main.cpp
76
LINK_LIBRARIES
8-
swiftIDE
9-
swiftDriver swiftIRGen swiftSIL swiftSILGen swiftSILOptimizer
10-
swiftImmediate
11-
swiftSerialization
12-
swiftPrintAsObjC
13-
swiftFrontend
14-
swiftClangImporter
15-
swiftOption
16-
clangAPINotes
7+
swiftDriver
8+
swiftFrontendTool
179
)
1810

1911
target_link_libraries(swift edit)

tools/driver/driver.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/Driver/Job.h"
2525
#include "swift/Frontend/Frontend.h"
2626
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
27+
#include "swift/Subsystems.h"
2728
#include "llvm/ADT/SmallVector.h"
2829
#include "llvm/Support/CommandLine.h"
2930
#include "llvm/Support/Errno.h"
@@ -49,9 +50,6 @@ std::string getExecutablePath(const char *FirstArg) {
4950
return llvm::sys::fs::getMainExecutable(FirstArg, P);
5051
}
5152

52-
extern int frontend_main(ArrayRef<const char *> Args, const char *Argv0,
53-
void *MainAddr);
54-
5553
/// Run 'swift-autolink-extract'.
5654
extern int autolink_extract_main(ArrayRef<const char *> Args, const char *Argv0,
5755
void *MainAddr);
@@ -155,9 +153,9 @@ int main(int argc_, const char **argv_) {
155153
if (argv.size() > 1){
156154
StringRef FirstArg(argv[1]);
157155
if (FirstArg == "-frontend") {
158-
return frontend_main(llvm::makeArrayRef(argv.data()+2,
159-
argv.data()+argv.size()),
160-
argv[0], (void *)(intptr_t)getExecutablePath);
156+
return performFrontend(llvm::makeArrayRef(argv.data()+2,
157+
argv.data()+argv.size()),
158+
argv[0], (void *)(intptr_t)getExecutablePath);
161159
}
162160
if (FirstArg == "-modulewrap") {
163161
return modulewrap_main(llvm::makeArrayRef(argv.data()+2,

0 commit comments

Comments
 (0)