Skip to content

Commit 2134f9f

Browse files
authored
[Parse] Move SyntaxParsingContext to Parse (#14360)
This is only for the Parser. Also, this resolves layering violation where libAST and libSyntax depends on each other.
1 parent 87f7b4e commit 2134f9f

15 files changed

+81
-80
lines changed

include/swift/AST/Module.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ namespace swift {
8181

8282
namespace syntax {
8383
class SourceFileSyntax;
84-
class SyntaxParsingContext;
85-
class SyntaxParsingContextRoot;
8684
}
8785

8886
/// Discriminator for file-units.

include/swift/Parse/Parser.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "swift/Parse/ParserPosition.h"
3434
#include "swift/Parse/ParserResult.h"
3535
#include "swift/Parse/SyntaxParserResult.h"
36-
#include "swift/Syntax/SyntaxParsingContext.h"
36+
#include "swift/Parse/SyntaxParsingContext.h"
3737
#include "swift/Config.h"
3838
#include "llvm/ADT/SetVector.h"
3939

@@ -344,7 +344,7 @@ class Parser {
344344
llvm::SmallVector<StructureMarker, 16> StructureMarkers;
345345

346346
/// Current syntax parsing context where call backs should be directed to.
347-
syntax::SyntaxParsingContext *SyntaxContext;
347+
SyntaxParsingContext *SyntaxContext;
348348

349349
public:
350350
Parser(unsigned BufferID, SourceFile &SF, SILParserTUStateBase *SIL,
@@ -409,7 +409,7 @@ class Parser {
409409
DiagnosticTransaction DT;
410410
/// This context immediately deconstructed with transparent accumulation
411411
/// on cancelBacktrack().
412-
llvm::Optional<syntax::SyntaxParsingContext> SynContext;
412+
llvm::Optional<SyntaxParsingContext> SynContext;
413413
bool Backtrack = true;
414414

415415
public:

include/swift/Syntax/SyntaxParsingContext.h renamed to include/swift/Parse/SyntaxParsingContext.h

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,25 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef SWIFT_SYNTAX_PARSING_CONTEXT_H
14-
#define SWIFT_SYNTAX_PARSING_CONTEXT_H
13+
#ifndef SWIFT_PARSE_SYNTAXPARSINGCONTEXT_H
14+
#define SWIFT_PARSE_SYNTAXPARSINGCONTEXT_H
1515

1616
#include "llvm/ADT/PointerUnion.h"
1717
#include "swift/Basic/SourceLoc.h"
1818
#include "swift/Syntax/Syntax.h"
1919
#include "swift/Syntax/TokenSyntax.h"
2020

2121
namespace swift {
22-
class SourceLoc;
2322
class SourceFile;
2423
class Token;
2524
class DiagnosticEngine;
2625

2726
namespace syntax {
2827
class RawSyntax;
2928
enum class SyntaxKind;
29+
}
30+
31+
using namespace swift::syntax;
3032

3133
enum class SyntaxContextKind {
3234
Decl,
@@ -39,53 +41,6 @@ enum class SyntaxContextKind {
3941

4042
constexpr size_t SyntaxAlignInBits = 3;
4143

42-
/// Indicates what action should be performed on the destruction of
43-
/// SyntaxParsingContext
44-
enum class AccumulationMode {
45-
// Coerece the result to one of ContextKind.
46-
// E.g. for ContextKind::Expr, passthroug if the result is CallExpr, whereas
47-
// <UnknownExpr><SomeDecl /></UnknownDecl> for non Exprs.
48-
CoerceKind,
49-
50-
// Construct a result Syntax with specified SyntaxKind.
51-
CreateSyntax,
52-
53-
// Pass through all parts to the parent context.
54-
Transparent,
55-
56-
// Discard all parts in the context.
57-
Discard,
58-
59-
// Construct SourceFile syntax to the specified SF.
60-
Root,
61-
62-
// Invalid.
63-
NotSet,
64-
};
65-
66-
/// The shared data for all syntax parsing contexts with the same root.
67-
/// This should be accessible from the root context only.
68-
struct alignas(1 << SyntaxAlignInBits) RootContextData {
69-
// The source file under parsing.
70-
SourceFile &SF;
71-
72-
// Where to issue diagnostics.
73-
DiagnosticEngine &Diags;
74-
75-
SourceManager &SourceMgr;
76-
77-
unsigned BufferID;
78-
79-
// Storage for Collected parts.
80-
std::vector<RC<RawSyntax>> Storage;
81-
82-
RootContextData(SourceFile &SF,
83-
DiagnosticEngine &Diags,
84-
SourceManager &SourceMgr,
85-
unsigned BufferID): SF(SF), Diags(Diags),
86-
SourceMgr(SourceMgr), BufferID(BufferID) {}
87-
};
88-
8944
/// RAII object which receive RawSyntax parts. On destruction, this constructs
9045
/// a specified syntax node from received parts and propagate it to the parent
9146
/// context.
@@ -102,6 +57,53 @@ struct alignas(1 << SyntaxAlignInBits) RootContextData {
10257
/// // From these parts, it creates ParenExpr node and add it to the parent.
10358
/// }
10459
class alignas(1 << SyntaxAlignInBits) SyntaxParsingContext {
60+
public:
61+
/// The shared data for all syntax parsing contexts with the same root.
62+
/// This should be accessible from the root context only.
63+
struct alignas(1 << SyntaxAlignInBits) RootContextData {
64+
// The source file under parsing.
65+
SourceFile &SF;
66+
67+
// Where to issue diagnostics.
68+
DiagnosticEngine &Diags;
69+
70+
SourceManager &SourceMgr;
71+
72+
unsigned BufferID;
73+
74+
// Storage for Collected parts.
75+
std::vector<RC<RawSyntax>> Storage;
76+
77+
RootContextData(SourceFile &SF, DiagnosticEngine &Diags,
78+
SourceManager &SourceMgr, unsigned BufferID)
79+
: SF(SF), Diags(Diags), SourceMgr(SourceMgr), BufferID(BufferID) {}
80+
};
81+
82+
private:
83+
/// Indicates what action should be performed on the destruction of
84+
/// SyntaxParsingContext
85+
enum class AccumulationMode {
86+
// Coerece the result to one of ContextKind.
87+
// E.g. for ContextKind::Expr, passthroug if the result is CallExpr, whereas
88+
// <UnknownExpr><SomeDecl /></UnknownDecl> for non Exprs.
89+
CoerceKind,
90+
91+
// Construct a result Syntax with specified SyntaxKind.
92+
CreateSyntax,
93+
94+
// Pass through all parts to the parent context.
95+
Transparent,
96+
97+
// Discard all parts in the context.
98+
Discard,
99+
100+
// Construct SourceFile syntax to the specified SF.
101+
Root,
102+
103+
// Invalid.
104+
NotSet,
105+
};
106+
105107
// When this context is a root, this points to an instance of RootContextData;
106108
// When this context isn't a root, this points to the parent context.
107109
const llvm::PointerUnion<RootContextData *, SyntaxParsingContext *>
@@ -255,6 +257,5 @@ class alignas(1 << SyntaxAlignInBits) SyntaxParsingContext {
255257

256258
};
257259

258-
} // namespace syntax
259260
} // namespace swift
260261
#endif // SWIFT_SYNTAX_PARSING_CONTEXT_H

lib/AST/Module.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "swift/Basic/SourceManager.h"
3737
#include "swift/Parse/Token.h"
3838
#include "swift/Syntax/SyntaxNodes.h"
39-
#include "swift/Syntax/SyntaxParsingContext.h"
4039
#include "clang/Basic/Module.h"
4140
#include "llvm/ADT/DenseMap.h"
4241
#include "llvm/ADT/DenseSet.h"

lib/Parse/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_swift_library(swiftParse STATIC
1111
ParseType.cpp
1212
PersistentParserState.cpp
1313
Scope.cpp
14+
SyntaxParsingContext.cpp
1415
LINK_LIBRARIES
1516
swiftAST
1617
swiftSyntax

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
#include "swift/Parse/CodeCompletionCallbacks.h"
1919
#include "swift/Parse/DelayedParsingCallbacks.h"
2020
#include "swift/Parse/ParseSILSupport.h"
21+
#include "swift/Parse/SyntaxParsingContext.h"
2122
#include "swift/Syntax/SyntaxFactory.h"
2223
#include "swift/Syntax/TokenSyntax.h"
23-
#include "swift/Syntax/SyntaxParsingContext.h"
2424
#include "swift/Subsystems.h"
2525
#include "swift/AST/Attr.h"
2626
#include "swift/AST/DebuggerClient.h"

lib/Parse/ParseExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
#include "swift/AST/DiagnosticsParse.h"
1919
#include "swift/Basic/EditorPlaceholder.h"
2020
#include "swift/Parse/CodeCompletionCallbacks.h"
21+
#include "swift/Parse/SyntaxParsingContext.h"
2122
#include "swift/Syntax/SyntaxBuilders.h"
2223
#include "swift/Syntax/SyntaxFactory.h"
2324
#include "swift/Syntax/TokenSyntax.h"
24-
#include "swift/Syntax/SyntaxParsingContext.h"
2525
#include "llvm/ADT/SmallString.h"
2626
#include "llvm/ADT/StringSwitch.h"
2727
#include "llvm/ADT/Twine.h"

lib/Parse/ParseGeneric.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
#include "swift/Parse/Parser.h"
1818
#include "swift/AST/DiagnosticsParse.h"
1919
#include "swift/Parse/CodeCompletionCallbacks.h"
20+
#include "swift/Parse/SyntaxParsingContext.h"
2021
#include "swift/Parse/Lexer.h"
2122
#include "swift/Syntax/SyntaxBuilders.h"
2223
#include "swift/Syntax/SyntaxNodes.h"
23-
#include "swift/Syntax/SyntaxParsingContext.h"
2424
using namespace swift;
2525
using namespace swift::syntax;
2626

lib/Parse/ParseIfConfig.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
#include "swift/AST/ASTVisitor.h"
1817
#include "swift/Parse/Parser.h"
18+
19+
#include "swift/AST/ASTVisitor.h"
1920
#include "swift/Basic/Defer.h"
2021
#include "swift/Basic/LangOptions.h"
2122
#include "swift/Basic/Version.h"
2223
#include "swift/Parse/Lexer.h"
24+
#include "swift/Parse/SyntaxParsingContext.h"
25+
#include "swift/Syntax/SyntaxFactory.h"
26+
#include "swift/Syntax/TokenSyntax.h"
2327
#include "llvm/ADT/StringSwitch.h"
2428
#include "llvm/Support/Compiler.h"
2529
#include "llvm/Support/SaveAndRestore.h"
26-
#include "swift/Syntax/SyntaxFactory.h"
27-
#include "swift/Syntax/TokenSyntax.h"
28-
#include "swift/Syntax/SyntaxParsingContext.h"
2930

3031
using namespace swift;
3132
using namespace swift::syntax;

lib/Parse/ParsePattern.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
#include "swift/Parse/CodeCompletionCallbacks.h"
1817
#include "swift/Parse/Parser.h"
18+
1919
#include "swift/AST/ASTWalker.h"
2020
#include "swift/AST/Initializer.h"
2121
#include "swift/Basic/StringExtras.h"
22+
#include "swift/Parse/CodeCompletionCallbacks.h"
23+
#include "swift/Parse/SyntaxParsingContext.h"
2224
#include "swift/Syntax/SyntaxFactory.h"
2325
#include "swift/Syntax/TokenSyntax.h"
24-
#include "swift/Syntax/SyntaxParsingContext.h"
2526
#include "llvm/ADT/SmallString.h"
2627
#include "llvm/ADT/StringMap.h"
2728
#include "llvm/Support/SaveAndRestore.h"

0 commit comments

Comments
 (0)