Skip to content

Commit

Permalink
[Parse] Split ParseSIL out into its own library.
Browse files Browse the repository at this point in the history
...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.
  • Loading branch information
jrose-apple committed Jul 17, 2017
1 parent 46d994f commit b4759bc
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 114 deletions.
54 changes: 54 additions & 0 deletions include/swift/Parse/ParseSILSupport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//===--- ParseSILSupport.h - Interface with ParseSIL ------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_PARSER_PARSESILSUPPORT_H
#define SWIFT_PARSER_PARSESILSUPPORT_H

#include "llvm/Support/PrettyStackTrace.h"

namespace swift {
class Parser;

/// Interface between the Parse and ParseSIL libraries, to avoid circular
/// dependencies.
class SILParserTUStateBase {
virtual void anchor();
protected:
SILParserTUStateBase() = default;
virtual ~SILParserTUStateBase() = default;
public:
virtual bool parseDeclSIL(Parser &P) = 0;
virtual bool parseDeclSILStage(Parser &P) = 0;
virtual bool parseSILVTable(Parser &P) = 0;
virtual bool parseSILGlobal(Parser &P) = 0;
virtual bool parseSILWitnessTable(Parser &P) = 0;
virtual bool parseSILDefaultWitnessTable(Parser &P) = 0;
virtual bool parseSILCoverageMap(Parser &P) = 0;
virtual bool parseSILScope(Parser &P) = 0;
};

/// To assist debugging parser crashes, tell us the location of the
/// current token.
class PrettyStackTraceParser : public llvm::PrettyStackTraceEntry {
Parser &P;
public:
explicit PrettyStackTraceParser(Parser &P) : P(P) {}
void print(llvm::raw_ostream &out) const override {
out << "With parser at source location: ";
P.Tok.getLoc().print(out, P.Context.SourceMgr);
out << '\n';
}
};
} // end namespace swift

#endif // SWIFT_PARSER_PARSESILSUPPORT_H

13 changes: 8 additions & 5 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace swift {
class ScopeInfo;
struct TypeLoc;
class TupleType;
class SILParserTUState;
class SILParserTUStateBase;
class SourceManager;
class PersistentParserState;
class CodeCompletionCallbacks;
Expand All @@ -71,7 +71,10 @@ namespace swift {
ActiveConditionalBlock,
};


/// The main class used for parsing a source file (.swift or .sil).
///
/// Rather than instantiating a Parser yourself, use one of the parsing APIs
/// provided in Subsystems.h.
class Parser {
Parser(const Parser&) = delete;
void operator=(const Parser&) = delete;
Expand All @@ -85,7 +88,7 @@ class Parser {
DiagnosticEngine &Diags;
SourceFile &SF;
Lexer *L;
SILParserTUState *SIL; // Non-null when parsing a .sil file.
SILParserTUStateBase *SIL; // Non-null when parsing a .sil file.
PersistentParserState *State;
std::unique_ptr<PersistentParserState> OwnedState;
DeclContext *CurDeclContext;
Expand Down Expand Up @@ -304,10 +307,10 @@ class Parser {
llvm::SmallVector<StructureMarker, 16> StructureMarkers;

public:
Parser(unsigned BufferID, SourceFile &SF, SILParserTUState *SIL,
Parser(unsigned BufferID, SourceFile &SF, SILParserTUStateBase *SIL,
PersistentParserState *PersistentState = nullptr);
Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
SILParserTUState *SIL = nullptr,
SILParserTUStateBase *SIL = nullptr,
PersistentParserState *PersistentState = nullptr);
~Parser();

Expand Down
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_subdirectory(Markup)
add_subdirectory(Migrator)
add_subdirectory(Option)
add_subdirectory(Parse)
add_subdirectory(ParseSIL)
add_subdirectory(PrintAsObjC)
add_subdirectory(RemoteAST)
add_subdirectory(Sema)
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ add_swift_library(swiftFrontend STATIC
swiftSIL
swiftMigrator
swiftOption
swiftParse
swiftParseSIL
swiftSema
swiftSerialization)

2 changes: 0 additions & 2 deletions lib/Parse/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ add_swift_library(swiftParse STATIC
ParseIfConfig.cpp
ParsePattern.cpp
Parser.cpp
ParseSIL.cpp
ParseStmt.cpp
ParseType.cpp
PersistentParserState.cpp
Scope.cpp
LINK_LIBRARIES
swiftAST
swiftSIL
swiftSyntax
)

2 changes: 1 addition & 1 deletion lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
//===----------------------------------------------------------------------===//

#include "swift/Parse/Parser.h"
#include "ParseSIL.h"
#include "swift/Parse/CodeCompletionCallbacks.h"
#include "swift/Parse/DelayedParsingCallbacks.h"
#include "swift/Parse/ParseSILSupport.h"
#include "swift/Subsystems.h"
#include "swift/AST/Attr.h"
#include "swift/AST/DebuggerClient.h"
Expand Down
60 changes: 0 additions & 60 deletions lib/Parse/ParseSIL.h

This file was deleted.

42 changes: 5 additions & 37 deletions lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
//===----------------------------------------------------------------------===//

#include "swift/Parse/Parser.h"
#include "ParseSIL.h"
#include "swift/Subsystems.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/DiagnosticsParse.h"
Expand All @@ -25,6 +24,7 @@
#include "swift/Parse/Lexer.h"
#include "swift/Parse/CodeCompletionCallbacks.h"
#include "swift/Parse/DelayedParsingCallbacks.h"
#include "swift/Parse/ParseSILSupport.h"
#include "swift/Syntax/TokenSyntax.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MemoryBuffer.h"
Expand All @@ -36,21 +36,9 @@
using namespace swift;

void DelayedParsingCallbacks::anchor() { }
void SILParserTUStateBase::anchor() { }

namespace {
/// To assist debugging parser crashes, tell us the location of the
/// current token.
class PrettyStackTraceParser : public llvm::PrettyStackTraceEntry {
Parser &P;
public:
PrettyStackTraceParser(Parser &P) : P(P) {}
void print(llvm::raw_ostream &out) const override {
out << "With parser at source location: ";
P.Tok.getLoc().print(out, P.Context.SourceMgr);
out << '\n';
}
};

/// A visitor that does delayed parsing of function bodies.
class ParseDelayedFunctionBodies : public ASTWalker {
PersistentParserState &ParserState;
Expand Down Expand Up @@ -135,27 +123,6 @@ static void parseDelayedDecl(
}
} // unnamed namespace

bool swift::parseIntoSourceFile(SourceFile &SF,
unsigned BufferID,
bool *Done,
SILParserState *SIL,
PersistentParserState *PersistentState,
DelayedParsingCallbacks *DelayedParseCB) {
SharedTimer timer("Parsing");
Parser P(BufferID, SF, SIL ? SIL->Impl.get() : nullptr, PersistentState);
PrettyStackTraceParser StackTrace(P);

llvm::SaveAndRestore<bool> S(P.IsParsingInterfaceTokens, true);

if (DelayedParseCB)
P.setDelayedParsingCallbacks(DelayedParseCB);

bool FoundSideEffects = P.parseTopLevel();
*Done = P.Tok.is(tok::eof);

return FoundSideEffects;
}

void swift::performDelayedParsing(
DeclContext *DC, PersistentParserState &PersistentState,
CodeCompletionCallbacksFactory *CodeCompletionFactory) {
Expand Down Expand Up @@ -313,7 +280,7 @@ swift::tokenizeWithTrivia(const LangOptions &LangOpts,
// Setup and Helper Methods
//===----------------------------------------------------------------------===//

Parser::Parser(unsigned BufferID, SourceFile &SF, SILParserTUState *SIL,
Parser::Parser(unsigned BufferID, SourceFile &SF, SILParserTUStateBase *SIL,
PersistentParserState *PersistentState)
: Parser(std::unique_ptr<Lexer>(
new Lexer(SF.getASTContext().LangOpts, SF.getASTContext().SourceMgr,
Expand All @@ -325,7 +292,8 @@ Parser::Parser(unsigned BufferID, SourceFile &SF, SILParserTUState *SIL,
}

Parser::Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
SILParserTUState *SIL, PersistentParserState *PersistentState)
SILParserTUStateBase *SIL,
PersistentParserState *PersistentState)
: SourceMgr(SF.getASTContext().SourceMgr),
Diags(SF.getASTContext().Diags),
SF(SF),
Expand Down
8 changes: 8 additions & 0 deletions lib/ParseSIL/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_swift_library(swiftParseSIL STATIC
ParseSIL.cpp
LINK_LIBRARIES
swiftParse
swiftSema
swiftSIL
)

Loading

0 comments on commit b4759bc

Please sign in to comment.