Skip to content

Commit b4759bc

Browse files
committed
[Parse] Split ParseSIL out into its own library.
...finally breaking the dependency of Parse on Sema. There are still some unfortunate dependencies here -- Xi's working on getting /AST/ not dependent on Sema -- but this is a step forward. It is a little strange that parseIntoSourceFile is in ParseSIL, and therefore that that's still a dependency for anyone trying to, well, parse. However, nearly all clients that parse want to type-check as well, and that requires Sema, Serialization, and the ClangImporter... and Serialization and SIL currently require each other as well (another circular dependency). So it's not actively causing us trouble right now.
1 parent 46d994f commit b4759bc

File tree

12 files changed

+145
-114
lines changed

12 files changed

+145
-114
lines changed

include/swift/Parse/ParseSILSupport.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===--- ParseSILSupport.h - Interface with ParseSIL ------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 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_PARSER_PARSESILSUPPORT_H
14+
#define SWIFT_PARSER_PARSESILSUPPORT_H
15+
16+
#include "llvm/Support/PrettyStackTrace.h"
17+
18+
namespace swift {
19+
class Parser;
20+
21+
/// Interface between the Parse and ParseSIL libraries, to avoid circular
22+
/// dependencies.
23+
class SILParserTUStateBase {
24+
virtual void anchor();
25+
protected:
26+
SILParserTUStateBase() = default;
27+
virtual ~SILParserTUStateBase() = default;
28+
public:
29+
virtual bool parseDeclSIL(Parser &P) = 0;
30+
virtual bool parseDeclSILStage(Parser &P) = 0;
31+
virtual bool parseSILVTable(Parser &P) = 0;
32+
virtual bool parseSILGlobal(Parser &P) = 0;
33+
virtual bool parseSILWitnessTable(Parser &P) = 0;
34+
virtual bool parseSILDefaultWitnessTable(Parser &P) = 0;
35+
virtual bool parseSILCoverageMap(Parser &P) = 0;
36+
virtual bool parseSILScope(Parser &P) = 0;
37+
};
38+
39+
/// To assist debugging parser crashes, tell us the location of the
40+
/// current token.
41+
class PrettyStackTraceParser : public llvm::PrettyStackTraceEntry {
42+
Parser &P;
43+
public:
44+
explicit PrettyStackTraceParser(Parser &P) : P(P) {}
45+
void print(llvm::raw_ostream &out) const override {
46+
out << "With parser at source location: ";
47+
P.Tok.getLoc().print(out, P.Context.SourceMgr);
48+
out << '\n';
49+
}
50+
};
51+
} // end namespace swift
52+
53+
#endif // SWIFT_PARSER_PARSESILSUPPORT_H
54+

include/swift/Parse/Parser.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace swift {
4545
class ScopeInfo;
4646
struct TypeLoc;
4747
class TupleType;
48-
class SILParserTUState;
48+
class SILParserTUStateBase;
4949
class SourceManager;
5050
class PersistentParserState;
5151
class CodeCompletionCallbacks;
@@ -71,7 +71,10 @@ namespace swift {
7171
ActiveConditionalBlock,
7272
};
7373

74-
74+
/// The main class used for parsing a source file (.swift or .sil).
75+
///
76+
/// Rather than instantiating a Parser yourself, use one of the parsing APIs
77+
/// provided in Subsystems.h.
7578
class Parser {
7679
Parser(const Parser&) = delete;
7780
void operator=(const Parser&) = delete;
@@ -85,7 +88,7 @@ class Parser {
8588
DiagnosticEngine &Diags;
8689
SourceFile &SF;
8790
Lexer *L;
88-
SILParserTUState *SIL; // Non-null when parsing a .sil file.
91+
SILParserTUStateBase *SIL; // Non-null when parsing a .sil file.
8992
PersistentParserState *State;
9093
std::unique_ptr<PersistentParserState> OwnedState;
9194
DeclContext *CurDeclContext;
@@ -304,10 +307,10 @@ class Parser {
304307
llvm::SmallVector<StructureMarker, 16> StructureMarkers;
305308

306309
public:
307-
Parser(unsigned BufferID, SourceFile &SF, SILParserTUState *SIL,
310+
Parser(unsigned BufferID, SourceFile &SF, SILParserTUStateBase *SIL,
308311
PersistentParserState *PersistentState = nullptr);
309312
Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
310-
SILParserTUState *SIL = nullptr,
313+
SILParserTUStateBase *SIL = nullptr,
311314
PersistentParserState *PersistentState = nullptr);
312315
~Parser();
313316

lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_subdirectory(Markup)
1515
add_subdirectory(Migrator)
1616
add_subdirectory(Option)
1717
add_subdirectory(Parse)
18+
add_subdirectory(ParseSIL)
1819
add_subdirectory(PrintAsObjC)
1920
add_subdirectory(RemoteAST)
2021
add_subdirectory(Sema)

lib/Frontend/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ add_swift_library(swiftFrontend STATIC
1717
swiftSIL
1818
swiftMigrator
1919
swiftOption
20-
swiftParse
20+
swiftParseSIL
2121
swiftSema
2222
swiftSerialization)
2323

lib/Parse/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ add_swift_library(swiftParse STATIC
77
ParseIfConfig.cpp
88
ParsePattern.cpp
99
Parser.cpp
10-
ParseSIL.cpp
1110
ParseStmt.cpp
1211
ParseType.cpp
1312
PersistentParserState.cpp
1413
Scope.cpp
1514
LINK_LIBRARIES
1615
swiftAST
17-
swiftSIL
1816
swiftSyntax
1917
)
2018

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "swift/Parse/Parser.h"
18-
#include "ParseSIL.h"
1918
#include "swift/Parse/CodeCompletionCallbacks.h"
2019
#include "swift/Parse/DelayedParsingCallbacks.h"
20+
#include "swift/Parse/ParseSILSupport.h"
2121
#include "swift/Subsystems.h"
2222
#include "swift/AST/Attr.h"
2323
#include "swift/AST/DebuggerClient.h"

lib/Parse/ParseSIL.h

Lines changed: 0 additions & 60 deletions
This file was deleted.

lib/Parse/Parser.cpp

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "swift/Parse/Parser.h"
18-
#include "ParseSIL.h"
1918
#include "swift/Subsystems.h"
2019
#include "swift/AST/ASTWalker.h"
2120
#include "swift/AST/DiagnosticsParse.h"
@@ -25,6 +24,7 @@
2524
#include "swift/Parse/Lexer.h"
2625
#include "swift/Parse/CodeCompletionCallbacks.h"
2726
#include "swift/Parse/DelayedParsingCallbacks.h"
27+
#include "swift/Parse/ParseSILSupport.h"
2828
#include "swift/Syntax/TokenSyntax.h"
2929
#include "llvm/Support/Compiler.h"
3030
#include "llvm/Support/MemoryBuffer.h"
@@ -36,21 +36,9 @@
3636
using namespace swift;
3737

3838
void DelayedParsingCallbacks::anchor() { }
39+
void SILParserTUStateBase::anchor() { }
3940

4041
namespace {
41-
/// To assist debugging parser crashes, tell us the location of the
42-
/// current token.
43-
class PrettyStackTraceParser : public llvm::PrettyStackTraceEntry {
44-
Parser &P;
45-
public:
46-
PrettyStackTraceParser(Parser &P) : P(P) {}
47-
void print(llvm::raw_ostream &out) const override {
48-
out << "With parser at source location: ";
49-
P.Tok.getLoc().print(out, P.Context.SourceMgr);
50-
out << '\n';
51-
}
52-
};
53-
5442
/// A visitor that does delayed parsing of function bodies.
5543
class ParseDelayedFunctionBodies : public ASTWalker {
5644
PersistentParserState &ParserState;
@@ -135,27 +123,6 @@ static void parseDelayedDecl(
135123
}
136124
} // unnamed namespace
137125

138-
bool swift::parseIntoSourceFile(SourceFile &SF,
139-
unsigned BufferID,
140-
bool *Done,
141-
SILParserState *SIL,
142-
PersistentParserState *PersistentState,
143-
DelayedParsingCallbacks *DelayedParseCB) {
144-
SharedTimer timer("Parsing");
145-
Parser P(BufferID, SF, SIL ? SIL->Impl.get() : nullptr, PersistentState);
146-
PrettyStackTraceParser StackTrace(P);
147-
148-
llvm::SaveAndRestore<bool> S(P.IsParsingInterfaceTokens, true);
149-
150-
if (DelayedParseCB)
151-
P.setDelayedParsingCallbacks(DelayedParseCB);
152-
153-
bool FoundSideEffects = P.parseTopLevel();
154-
*Done = P.Tok.is(tok::eof);
155-
156-
return FoundSideEffects;
157-
}
158-
159126
void swift::performDelayedParsing(
160127
DeclContext *DC, PersistentParserState &PersistentState,
161128
CodeCompletionCallbacksFactory *CodeCompletionFactory) {
@@ -313,7 +280,7 @@ swift::tokenizeWithTrivia(const LangOptions &LangOpts,
313280
// Setup and Helper Methods
314281
//===----------------------------------------------------------------------===//
315282

316-
Parser::Parser(unsigned BufferID, SourceFile &SF, SILParserTUState *SIL,
283+
Parser::Parser(unsigned BufferID, SourceFile &SF, SILParserTUStateBase *SIL,
317284
PersistentParserState *PersistentState)
318285
: Parser(std::unique_ptr<Lexer>(
319286
new Lexer(SF.getASTContext().LangOpts, SF.getASTContext().SourceMgr,
@@ -325,7 +292,8 @@ Parser::Parser(unsigned BufferID, SourceFile &SF, SILParserTUState *SIL,
325292
}
326293

327294
Parser::Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
328-
SILParserTUState *SIL, PersistentParserState *PersistentState)
295+
SILParserTUStateBase *SIL,
296+
PersistentParserState *PersistentState)
329297
: SourceMgr(SF.getASTContext().SourceMgr),
330298
Diags(SF.getASTContext().Diags),
331299
SF(SF),

lib/ParseSIL/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_swift_library(swiftParseSIL STATIC
2+
ParseSIL.cpp
3+
LINK_LIBRARIES
4+
swiftParse
5+
swiftSema
6+
swiftSIL
7+
)
8+

0 commit comments

Comments
 (0)