Skip to content

Commit b9f468e

Browse files
author
Harlan
authored
[NFC] Remove Syntax's dependency on Sema (#10984)
* Move LegacyASTTransformer and SyntaxASTMapping to AST * Fix import in swift-syntax-format * Update swift-syntax-test
1 parent adf1e2e commit b9f468e

File tree

10 files changed

+44
-47
lines changed

10 files changed

+44
-47
lines changed

include/swift/Syntax/LegacyASTTransformer.h renamed to include/swift/AST/LegacyASTTransformer.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
//
1919
//===----------------------------------------------------------------------===//
2020

21-
#ifndef SWIFT_SYNTAX_LEGACYASTTRANSFORMER_H
22-
#define SWIFT_SYNTAX_LEGACYASTTRANSFORMER_H
21+
#ifndef SWIFT_AST_LEGACYASTTRANSFORMER_H
22+
#define SWIFT_AST_LEGACYASTTRANSFORMER_H
2323

2424
#include "swift/AST/ASTNode.h"
2525
#include "swift/AST/ASTVisitor.h"
26-
#include "swift/Sema/Semantics.h"
26+
#include "swift/AST/SyntaxASTMap.h"
2727
#include "swift/Syntax/References.h"
2828
#include "swift/Syntax/Syntax.h"
2929
#include "swift/Syntax/TokenSyntax.h"
@@ -47,16 +47,17 @@ class LegacyASTTransformer : public ASTVisitor<LegacyASTTransformer,
4747
RC<SyntaxData>, // TypeRepr return type
4848
RC<SyntaxData>> // Attribute return type
4949
{
50-
sema::Semantics &Sema;
50+
SyntaxASTMap &ASTMap;
5151
SourceManager &SourceMgr;
5252
const unsigned BufferID;
5353
const TokenPositionList &Tokens;
5454
public:
55-
LegacyASTTransformer(sema::Semantics &Sema,
55+
LegacyASTTransformer(SyntaxASTMap &ASTMap,
5656
SourceManager &SourceMgr,
5757
const unsigned BufferID,
5858
const TokenPositionList &Tokens)
59-
: Sema(Sema), SourceMgr(SourceMgr), BufferID(BufferID), Tokens(Tokens) {}
59+
: ASTMap(ASTMap), SourceMgr(SourceMgr),
60+
BufferID(BufferID), Tokens(Tokens) {}
6061

6162
/// If the Decl has attributes, provide the start SourceLoc for them;
6263
/// otherwise, just ask the Decl for its usual start SourceLoc.
@@ -102,7 +103,7 @@ class LegacyASTTransformer : public ASTVisitor<LegacyASTTransformer,
102103
/// If the node isn't expressible in a `Syntax`, then `None` is returned.
103104
Optional<Syntax>
104105
transformAST(ASTNode Node,
105-
sema::Semantics &Sema,
106+
SyntaxASTMap &Sema,
106107
SourceManager &SourceMgr,
107108
const unsigned BufferID,
108109
const TokenPositionList &Tokens);
@@ -121,4 +122,4 @@ TokenSyntax findTokenSyntax(tok ExpectedKind,
121122
} // end namespace syntax
122123
} // end namespace swift
123124

124-
#endif // SWIFT_SYNTAX_LEGACYASTTRANSFORMER_H
125+
#endif // SWIFT_AST_LEGACYASTTRANSFORMER_H

include/swift/Sema/Semantics.h renamed to include/swift/AST/SyntaxASTMap.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- Semantics.h - Swift Container for Semantic Info --------*- C++ -*-===//
1+
//===--- SyntaxASTMap.h - Swift Container for Semantic Info -----*- C++ -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -10,12 +10,12 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
//
13-
// This file declares the interface for Semantics, the top-level container
14-
// and manager for semantic analysis.
13+
// This file declares the interface for SyntaxASTMap, a container mapping Syntax
14+
// nodes to the Semantic AST.
1515
//===----------------------------------------------------------------------===//
1616

17-
#ifndef SWIFT_SEMA_SEMANTICMODEL_H
18-
#define SWIFT_SEMA_SEMANTICMODEL_H
17+
#ifndef SWIFT_AST_SYNTAXASTMAP_H
18+
#define SWIFT_AST_SYNTAXASTMAP_H
1919

2020
#include "swift/AST/ASTNode.h"
2121
#include "swift/Syntax/Syntax.h"
@@ -28,15 +28,13 @@ namespace syntax {
2828
class LegacyASTTransformer;
2929
}
3030

31-
namespace sema {
32-
3331
/// The top-level container and manager for semantic analysis.
3432
///
3533
/// Eventually, this should contain cached semantic information such as
3634
/// resolved symbols and types for Syntax nodes. For now, it only maintains
3735
/// a mapping from lib/AST nodes to lib/Syntax nodes while we integrate
3836
/// the infrastructure into the compiler.
39-
class Semantics final {
37+
class SyntaxASTMap final {
4038
friend class LegacyASTTransformer;
4139
llvm::DenseMap<RC<syntax::SyntaxData>, ASTNode> SyntaxMap;
4240
public:
@@ -59,7 +57,6 @@ class Semantics final {
5957
void dumpSyntaxMap() const;
6058
};
6159

62-
} // end namespace sema
6360
} // end namespace swift
6461

65-
#endif // SWIFT_SEMA_SEMANTICMODEL_H
62+
#endif // SWIFT_AST_SYNTAXASTMAP_H

include/swift/Syntax/Syntax.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
#include "llvm/Support/raw_ostream.h"
3333

3434
namespace swift {
35-
namespace sema {
36-
class Semantics;
37-
}
35+
36+
class SyntaxASTMap;
37+
3838
namespace syntax {
3939

4040
template <typename SyntaxNode>
@@ -55,7 +55,7 @@ const auto NoParent = llvm::None;
5555
class Syntax {
5656
friend struct SyntaxFactory;
5757
friend class LegacyASTTransformer;
58-
friend class sema::Semantics;
58+
friend class swift::SyntaxASTMap;
5959

6060
#define SYNTAX(Id, Parent) friend class Id##Syntax;
6161
#include "swift/Syntax/SyntaxKinds.def"

lib/AST/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ add_swift_library(swiftAST STATIC
3333
GenericSignatureBuilder.cpp
3434
Identifier.cpp
3535
LayoutConstraint.cpp
36+
LegacyASTTransformer.cpp
3637
LookupVisibleDecls.cpp
3738
Module.cpp
3839
ModuleLoader.cpp
@@ -44,6 +45,7 @@ add_swift_library(swiftAST STATIC
4445
PrettyStackTrace.cpp
4546
ProtocolConformance.cpp
4647
RawComment.cpp
48+
SyntaxASTMap.cpp
4749
SILLayout.cpp
4850
Stmt.cpp
4951
SourceEntityWalker.cpp

lib/Syntax/LegacyASTTransformer.cpp renamed to lib/AST/LegacyASTTransformer.cpp

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

13+
#include "swift/AST/LegacyASTTransformer.h"
1314
#include "swift/Syntax/DeclSyntax.h"
1415
#include "swift/Syntax/ExprSyntax.h"
1516
#include "swift/Syntax/GenericSyntax.h"
16-
#include "swift/Syntax/LegacyASTTransformer.h"
1717
#include "swift/Syntax/References.h"
1818
#include "swift/Syntax/StmtSyntax.h"
1919
#include "swift/Syntax/SyntaxFactory.h"
@@ -74,19 +74,19 @@ namespace {
7474

7575
Optional<Syntax>
7676
syntax::transformAST(ASTNode Node,
77-
sema::Semantics &Sema,
77+
SyntaxASTMap &ASTMap,
7878
SourceManager &SourceMgr,
7979
const unsigned BufferID,
8080
const TokenPositionList &Tokens) {
81-
LegacyASTTransformer Transformer { Sema, SourceMgr, BufferID, Tokens };
81+
LegacyASTTransformer Transformer { ASTMap, SourceMgr, BufferID, Tokens };
8282

8383
if (Node.is<Expr *>()) {
8484
auto E = Node.get<Expr *>();
8585
if (E->isImplicit() || E->getSourceRange().isInvalid()) {
8686
return None;
8787
}
8888
auto Transformed = Transformer.visit(E);
89-
Sema.recordSyntaxMapping(Transformed, Node);
89+
ASTMap.recordSyntaxMapping(Transformed, Node);
9090
return Syntax { Transformed, Transformed.get() };
9191
} else if (Node.is<Decl *>()) {
9292
auto D = Node.get<Decl *>();
@@ -103,15 +103,15 @@ syntax::transformAST(ASTNode Node,
103103
return None;
104104
}
105105
auto Transformed = Transformer.visit(D);
106-
Sema.recordSyntaxMapping(Transformed, Node);
106+
ASTMap.recordSyntaxMapping(Transformed, Node);
107107
return Syntax { Transformed, Transformed.get() };
108108
} else if (Node.is<Stmt *>()) {
109109
auto S = Node.get<Stmt *>();
110110
if (S->isImplicit() || S->getSourceRange().isInvalid()) {
111111
return None;
112112
}
113113
auto Transformed = Transformer.visit(S);
114-
Sema.recordSyntaxMapping(Transformed, Node);
114+
ASTMap.recordSyntaxMapping(Transformed, Node);
115115
return Syntax { Transformed, Transformed.get() };
116116
}
117117
return None;
@@ -350,7 +350,7 @@ LegacyASTTransformer::visitStructDecl(StructDecl *D,
350350

351351
DeclMembersSyntaxBuilder MemberBuilder;
352352
for (auto Member : D->getMembers()) {
353-
auto TransformedMember = transformAST(Member, Sema,
353+
auto TransformedMember = transformAST(Member, ASTMap,
354354
SourceMgr, BufferID, Tokens);
355355
if (TransformedMember.hasValue()) {
356356
if (auto MD = TransformedMember.getValue().getAs<DeclSyntax>()) {
@@ -454,7 +454,7 @@ LegacyASTTransformer::visitBraceStmt(BraceStmt *S,
454454

455455
std::vector<StmtSyntax> Stmts;
456456
for (auto Node : S->getElements()) {
457-
auto Transformed = transformAST(Node, Sema, SourceMgr, BufferID, Tokens);
457+
auto Transformed = transformAST(Node, ASTMap, SourceMgr, BufferID, Tokens);
458458
if (Transformed.hasValue()) {
459459
Stmts.push_back(Transformed.getValue().castTo<StmtSyntax>());
460460
}
@@ -476,7 +476,7 @@ LegacyASTTransformer::visitReturnStmt(ReturnStmt *S,
476476
const CursorIndex IndexInParent) {
477477
auto ReturnKW = findTokenSyntax(tok::kw_return, "return", SourceMgr,
478478
S->getReturnLoc(), BufferID, Tokens);
479-
auto Result = transformAST(S->getResult(), Sema, SourceMgr, BufferID,
479+
auto Result = transformAST(S->getResult(), ASTMap, SourceMgr, BufferID,
480480
Tokens);
481481

482482
if (!Result.hasValue()) {

lib/Sema/Semantics.cpp renamed to lib/AST/SyntaxASTMap.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- Semantics.cpp - Semantics manager --------------------------------===//
1+
//===--- SyntaxASTMap.cpp - Syntax -> AST Map manager ---------------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -10,19 +10,18 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "swift/Sema/Semantics.h"
13+
#include "swift/AST/SyntaxASTMap.h"
1414
#include "swift/AST/Expr.h"
1515
#include "swift/AST/Decl.h"
1616
#include "swift/AST/Stmt.h"
1717
#include "swift/Syntax/Syntax.h"
1818

1919
using namespace swift;
20-
using namespace swift::sema;
2120
using namespace swift::syntax;
2221

2322
void
24-
Semantics::recordSyntaxMapping(RC<syntax::SyntaxData> FromNode,
25-
ASTNode ToNode) {
23+
SyntaxASTMap::recordSyntaxMapping(RC<syntax::SyntaxData> FromNode,
24+
ASTNode ToNode) {
2625
if (FromNode->getKind() == SyntaxKind::Unknown) {
2726
return;
2827
}
@@ -32,19 +31,19 @@ Semantics::recordSyntaxMapping(RC<syntax::SyntaxData> FromNode,
3231

3332

3433
llvm::Optional<ASTNode>
35-
Semantics::getNodeForSyntax(syntax::Syntax SyntaxNode) const {
34+
SyntaxASTMap::getNodeForSyntax(syntax::Syntax SyntaxNode) const {
3635
auto Found = SyntaxMap.find(SyntaxNode.Root);
3736
if (Found == SyntaxMap.end()) {
3837
return None;
3938
}
4039
return Found->getSecond();
4140
}
4241

43-
void Semantics::clearSyntaxMap() {
42+
void SyntaxASTMap::clearSyntaxMap() {
4443
SyntaxMap.shrink_and_clear();
4544
}
4645

47-
void Semantics::dumpSyntaxMap() const {
46+
void SyntaxASTMap::dumpSyntaxMap() const {
4847
for (const auto &SyntaxAndSemaNode : SyntaxMap) {
4948
auto SyntaxNode = SyntaxAndSemaNode.getFirst();
5049
auto SemanticNode = SyntaxAndSemaNode.getSecond();

lib/Sema/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ add_swift_library(swiftSema STATIC
3333
PCMacro.cpp
3434
PlaygroundTransform.cpp
3535
ResilienceDiagnostics.cpp
36-
Semantics.cpp
3736
SourceLoader.cpp
3837
TypeCheckAttr.cpp
3938
TypeCheckAvailability.cpp

lib/Syntax/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ add_swift_library(swiftSyntax STATIC
44
Format.cpp
55
RawTokenSyntax.cpp
66
GenericSyntax.cpp
7-
LegacyASTTransformer.cpp
87
Trivia.cpp
98
RawSyntax.cpp
109
StmtSyntax.cpp

tools/swift-syntax-format/swift-syntax-format.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
#include "swift/Basic/SourceManager.h"
2020
#include "swift/AST/DiagnosticEngine.h"
2121
#include "swift/AST/DiagnosticsFrontend.h"
22+
#include "swift/AST/LegacyASTTransformer.h"
2223
#include "swift/Frontend/Frontend.h"
2324
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
2425
#include "swift/Syntax/Format.h"
25-
#include "swift/Syntax/LegacyASTTransformer.h"
2626
#include "swift/Subsystems.h"
2727
#include "llvm/ADT/ArrayRef.h"
2828
#include "llvm/ADT/StringRef.h"
@@ -69,12 +69,12 @@ int doFormat(ArrayRef<StringRef> InputFiles) {
6969

7070
SmallVector<Decl *, 256> FileDecls;
7171
SF.getTopLevelDecls(FileDecls);
72-
sema::Semantics Sema;
72+
SyntaxASTMap ASTMap;
7373
for (auto *Decl : FileDecls) {
7474
if (Decl->escapedFromIfConfig()) {
7575
continue;
7676
}
77-
auto NewNode = transformAST(ASTNode(Decl), Sema, SourceMgr,
77+
auto NewNode = transformAST(ASTNode(Decl), ASTMap, SourceMgr,
7878
BufferID, Tokens);
7979
if (NewNode.hasValue()) {
8080
auto Reformatted = format(NewNode.getValue());

tools/swift-syntax-test/swift-syntax-test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
2727
#include "swift/Parse/Lexer.h"
2828
#include "swift/Subsystems.h"
29-
#include "swift/Syntax/LegacyASTTransformer.h"
29+
#include "swift/AST/LegacyASTTransformer.h"
3030
#include "swift/Syntax/Serialization/SyntaxSerialization.h"
3131
#include "swift/Syntax/SyntaxData.h"
3232
#include "llvm/Support/CommandLine.h"
@@ -151,14 +151,14 @@ int getSyntaxTree(const char *MainExecutablePath,
151151

152152
SmallVector<Decl *, 256> FileDecls;
153153
SF->getTopLevelDecls(FileDecls);
154-
sema::Semantics Sema;
154+
SyntaxASTMap ASTMap;
155155
// Convert the old ASTs to the new full-fidelity syntax tree and print
156156
// them out.
157157
for (auto *Decl : FileDecls) {
158158
if (Decl->escapedFromIfConfig()) {
159159
continue;
160160
}
161-
auto NewNode = transformAST(ASTNode(Decl), Sema, SourceMgr,
161+
auto NewNode = transformAST(ASTNode(Decl), ASTMap, SourceMgr,
162162
BufferID, Tokens);
163163
if (NewNode.hasValue()) {
164164
TopLevelDecls.push_back(NewNode.getValue());

0 commit comments

Comments
 (0)