mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[NFC] Remove Syntax's dependency on Sema (#10984)
* Move LegacyASTTransformer and SyntaxASTMapping to AST * Fix import in swift-syntax-format * Update swift-syntax-test
This commit is contained in:
@@ -18,12 +18,12 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef SWIFT_SYNTAX_LEGACYASTTRANSFORMER_H
|
||||
#define SWIFT_SYNTAX_LEGACYASTTRANSFORMER_H
|
||||
#ifndef SWIFT_AST_LEGACYASTTRANSFORMER_H
|
||||
#define SWIFT_AST_LEGACYASTTRANSFORMER_H
|
||||
|
||||
#include "swift/AST/ASTNode.h"
|
||||
#include "swift/AST/ASTVisitor.h"
|
||||
#include "swift/Sema/Semantics.h"
|
||||
#include "swift/AST/SyntaxASTMap.h"
|
||||
#include "swift/Syntax/References.h"
|
||||
#include "swift/Syntax/Syntax.h"
|
||||
#include "swift/Syntax/TokenSyntax.h"
|
||||
@@ -47,16 +47,17 @@ class LegacyASTTransformer : public ASTVisitor<LegacyASTTransformer,
|
||||
RC<SyntaxData>, // TypeRepr return type
|
||||
RC<SyntaxData>> // Attribute return type
|
||||
{
|
||||
sema::Semantics &Sema;
|
||||
SyntaxASTMap &ASTMap;
|
||||
SourceManager &SourceMgr;
|
||||
const unsigned BufferID;
|
||||
const TokenPositionList &Tokens;
|
||||
public:
|
||||
LegacyASTTransformer(sema::Semantics &Sema,
|
||||
LegacyASTTransformer(SyntaxASTMap &ASTMap,
|
||||
SourceManager &SourceMgr,
|
||||
const unsigned BufferID,
|
||||
const TokenPositionList &Tokens)
|
||||
: Sema(Sema), SourceMgr(SourceMgr), BufferID(BufferID), Tokens(Tokens) {}
|
||||
: ASTMap(ASTMap), SourceMgr(SourceMgr),
|
||||
BufferID(BufferID), Tokens(Tokens) {}
|
||||
|
||||
/// If the Decl has attributes, provide the start SourceLoc for them;
|
||||
/// otherwise, just ask the Decl for its usual start SourceLoc.
|
||||
@@ -102,7 +103,7 @@ public:
|
||||
/// If the node isn't expressible in a `Syntax`, then `None` is returned.
|
||||
Optional<Syntax>
|
||||
transformAST(ASTNode Node,
|
||||
sema::Semantics &Sema,
|
||||
SyntaxASTMap &Sema,
|
||||
SourceManager &SourceMgr,
|
||||
const unsigned BufferID,
|
||||
const TokenPositionList &Tokens);
|
||||
@@ -121,4 +122,4 @@ TokenSyntax findTokenSyntax(tok ExpectedKind,
|
||||
} // end namespace syntax
|
||||
} // end namespace swift
|
||||
|
||||
#endif // SWIFT_SYNTAX_LEGACYASTTRANSFORMER_H
|
||||
#endif // SWIFT_AST_LEGACYASTTRANSFORMER_H
|
||||
@@ -1,4 +1,4 @@
|
||||
//===--- Semantics.h - Swift Container for Semantic Info --------*- C++ -*-===//
|
||||
//===--- SyntaxASTMap.h - Swift Container for Semantic Info -----*- C++ -*-===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
@@ -10,12 +10,12 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file declares the interface for Semantics, the top-level container
|
||||
// and manager for semantic analysis.
|
||||
// This file declares the interface for SyntaxASTMap, a container mapping Syntax
|
||||
// nodes to the Semantic AST.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef SWIFT_SEMA_SEMANTICMODEL_H
|
||||
#define SWIFT_SEMA_SEMANTICMODEL_H
|
||||
#ifndef SWIFT_AST_SYNTAXASTMAP_H
|
||||
#define SWIFT_AST_SYNTAXASTMAP_H
|
||||
|
||||
#include "swift/AST/ASTNode.h"
|
||||
#include "swift/Syntax/Syntax.h"
|
||||
@@ -28,15 +28,13 @@ namespace syntax {
|
||||
class LegacyASTTransformer;
|
||||
}
|
||||
|
||||
namespace sema {
|
||||
|
||||
/// The top-level container and manager for semantic analysis.
|
||||
///
|
||||
/// Eventually, this should contain cached semantic information such as
|
||||
/// resolved symbols and types for Syntax nodes. For now, it only maintains
|
||||
/// a mapping from lib/AST nodes to lib/Syntax nodes while we integrate
|
||||
/// the infrastructure into the compiler.
|
||||
class Semantics final {
|
||||
class SyntaxASTMap final {
|
||||
friend class LegacyASTTransformer;
|
||||
llvm::DenseMap<RC<syntax::SyntaxData>, ASTNode> SyntaxMap;
|
||||
public:
|
||||
@@ -59,7 +57,6 @@ public:
|
||||
void dumpSyntaxMap() const;
|
||||
};
|
||||
|
||||
} // end namespace sema
|
||||
} // end namespace swift
|
||||
|
||||
#endif // SWIFT_SEMA_SEMANTICMODEL_H
|
||||
#endif // SWIFT_AST_SYNTAXASTMAP_H
|
||||
@@ -32,9 +32,9 @@
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
|
||||
namespace swift {
|
||||
namespace sema {
|
||||
class Semantics;
|
||||
}
|
||||
|
||||
class SyntaxASTMap;
|
||||
|
||||
namespace syntax {
|
||||
|
||||
template <typename SyntaxNode>
|
||||
@@ -55,7 +55,7 @@ const auto NoParent = llvm::None;
|
||||
class Syntax {
|
||||
friend struct SyntaxFactory;
|
||||
friend class LegacyASTTransformer;
|
||||
friend class sema::Semantics;
|
||||
friend class swift::SyntaxASTMap;
|
||||
|
||||
#define SYNTAX(Id, Parent) friend class Id##Syntax;
|
||||
#include "swift/Syntax/SyntaxKinds.def"
|
||||
|
||||
@@ -33,6 +33,7 @@ add_swift_library(swiftAST STATIC
|
||||
GenericSignatureBuilder.cpp
|
||||
Identifier.cpp
|
||||
LayoutConstraint.cpp
|
||||
LegacyASTTransformer.cpp
|
||||
LookupVisibleDecls.cpp
|
||||
Module.cpp
|
||||
ModuleLoader.cpp
|
||||
@@ -44,6 +45,7 @@ add_swift_library(swiftAST STATIC
|
||||
PrettyStackTrace.cpp
|
||||
ProtocolConformance.cpp
|
||||
RawComment.cpp
|
||||
SyntaxASTMap.cpp
|
||||
SILLayout.cpp
|
||||
Stmt.cpp
|
||||
SourceEntityWalker.cpp
|
||||
|
||||
@@ -10,10 +10,10 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "swift/AST/LegacyASTTransformer.h"
|
||||
#include "swift/Syntax/DeclSyntax.h"
|
||||
#include "swift/Syntax/ExprSyntax.h"
|
||||
#include "swift/Syntax/GenericSyntax.h"
|
||||
#include "swift/Syntax/LegacyASTTransformer.h"
|
||||
#include "swift/Syntax/References.h"
|
||||
#include "swift/Syntax/StmtSyntax.h"
|
||||
#include "swift/Syntax/SyntaxFactory.h"
|
||||
@@ -74,11 +74,11 @@ namespace {
|
||||
|
||||
Optional<Syntax>
|
||||
syntax::transformAST(ASTNode Node,
|
||||
sema::Semantics &Sema,
|
||||
SyntaxASTMap &ASTMap,
|
||||
SourceManager &SourceMgr,
|
||||
const unsigned BufferID,
|
||||
const TokenPositionList &Tokens) {
|
||||
LegacyASTTransformer Transformer { Sema, SourceMgr, BufferID, Tokens };
|
||||
LegacyASTTransformer Transformer { ASTMap, SourceMgr, BufferID, Tokens };
|
||||
|
||||
if (Node.is<Expr *>()) {
|
||||
auto E = Node.get<Expr *>();
|
||||
@@ -86,7 +86,7 @@ syntax::transformAST(ASTNode Node,
|
||||
return None;
|
||||
}
|
||||
auto Transformed = Transformer.visit(E);
|
||||
Sema.recordSyntaxMapping(Transformed, Node);
|
||||
ASTMap.recordSyntaxMapping(Transformed, Node);
|
||||
return Syntax { Transformed, Transformed.get() };
|
||||
} else if (Node.is<Decl *>()) {
|
||||
auto D = Node.get<Decl *>();
|
||||
@@ -103,7 +103,7 @@ syntax::transformAST(ASTNode Node,
|
||||
return None;
|
||||
}
|
||||
auto Transformed = Transformer.visit(D);
|
||||
Sema.recordSyntaxMapping(Transformed, Node);
|
||||
ASTMap.recordSyntaxMapping(Transformed, Node);
|
||||
return Syntax { Transformed, Transformed.get() };
|
||||
} else if (Node.is<Stmt *>()) {
|
||||
auto S = Node.get<Stmt *>();
|
||||
@@ -111,7 +111,7 @@ syntax::transformAST(ASTNode Node,
|
||||
return None;
|
||||
}
|
||||
auto Transformed = Transformer.visit(S);
|
||||
Sema.recordSyntaxMapping(Transformed, Node);
|
||||
ASTMap.recordSyntaxMapping(Transformed, Node);
|
||||
return Syntax { Transformed, Transformed.get() };
|
||||
}
|
||||
return None;
|
||||
@@ -350,7 +350,7 @@ LegacyASTTransformer::visitStructDecl(StructDecl *D,
|
||||
|
||||
DeclMembersSyntaxBuilder MemberBuilder;
|
||||
for (auto Member : D->getMembers()) {
|
||||
auto TransformedMember = transformAST(Member, Sema,
|
||||
auto TransformedMember = transformAST(Member, ASTMap,
|
||||
SourceMgr, BufferID, Tokens);
|
||||
if (TransformedMember.hasValue()) {
|
||||
if (auto MD = TransformedMember.getValue().getAs<DeclSyntax>()) {
|
||||
@@ -454,7 +454,7 @@ LegacyASTTransformer::visitBraceStmt(BraceStmt *S,
|
||||
|
||||
std::vector<StmtSyntax> Stmts;
|
||||
for (auto Node : S->getElements()) {
|
||||
auto Transformed = transformAST(Node, Sema, SourceMgr, BufferID, Tokens);
|
||||
auto Transformed = transformAST(Node, ASTMap, SourceMgr, BufferID, Tokens);
|
||||
if (Transformed.hasValue()) {
|
||||
Stmts.push_back(Transformed.getValue().castTo<StmtSyntax>());
|
||||
}
|
||||
@@ -476,7 +476,7 @@ LegacyASTTransformer::visitReturnStmt(ReturnStmt *S,
|
||||
const CursorIndex IndexInParent) {
|
||||
auto ReturnKW = findTokenSyntax(tok::kw_return, "return", SourceMgr,
|
||||
S->getReturnLoc(), BufferID, Tokens);
|
||||
auto Result = transformAST(S->getResult(), Sema, SourceMgr, BufferID,
|
||||
auto Result = transformAST(S->getResult(), ASTMap, SourceMgr, BufferID,
|
||||
Tokens);
|
||||
|
||||
if (!Result.hasValue()) {
|
||||
@@ -1,4 +1,4 @@
|
||||
//===--- Semantics.cpp - Semantics manager --------------------------------===//
|
||||
//===--- SyntaxASTMap.cpp - Syntax -> AST Map manager ---------------------===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
@@ -10,19 +10,18 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "swift/Sema/Semantics.h"
|
||||
#include "swift/AST/SyntaxASTMap.h"
|
||||
#include "swift/AST/Expr.h"
|
||||
#include "swift/AST/Decl.h"
|
||||
#include "swift/AST/Stmt.h"
|
||||
#include "swift/Syntax/Syntax.h"
|
||||
|
||||
using namespace swift;
|
||||
using namespace swift::sema;
|
||||
using namespace swift::syntax;
|
||||
|
||||
void
|
||||
Semantics::recordSyntaxMapping(RC<syntax::SyntaxData> FromNode,
|
||||
ASTNode ToNode) {
|
||||
SyntaxASTMap::recordSyntaxMapping(RC<syntax::SyntaxData> FromNode,
|
||||
ASTNode ToNode) {
|
||||
if (FromNode->getKind() == SyntaxKind::Unknown) {
|
||||
return;
|
||||
}
|
||||
@@ -32,7 +31,7 @@ Semantics::recordSyntaxMapping(RC<syntax::SyntaxData> FromNode,
|
||||
|
||||
|
||||
llvm::Optional<ASTNode>
|
||||
Semantics::getNodeForSyntax(syntax::Syntax SyntaxNode) const {
|
||||
SyntaxASTMap::getNodeForSyntax(syntax::Syntax SyntaxNode) const {
|
||||
auto Found = SyntaxMap.find(SyntaxNode.Root);
|
||||
if (Found == SyntaxMap.end()) {
|
||||
return None;
|
||||
@@ -40,11 +39,11 @@ Semantics::getNodeForSyntax(syntax::Syntax SyntaxNode) const {
|
||||
return Found->getSecond();
|
||||
}
|
||||
|
||||
void Semantics::clearSyntaxMap() {
|
||||
void SyntaxASTMap::clearSyntaxMap() {
|
||||
SyntaxMap.shrink_and_clear();
|
||||
}
|
||||
|
||||
void Semantics::dumpSyntaxMap() const {
|
||||
void SyntaxASTMap::dumpSyntaxMap() const {
|
||||
for (const auto &SyntaxAndSemaNode : SyntaxMap) {
|
||||
auto SyntaxNode = SyntaxAndSemaNode.getFirst();
|
||||
auto SemanticNode = SyntaxAndSemaNode.getSecond();
|
||||
@@ -33,7 +33,6 @@ add_swift_library(swiftSema STATIC
|
||||
PCMacro.cpp
|
||||
PlaygroundTransform.cpp
|
||||
ResilienceDiagnostics.cpp
|
||||
Semantics.cpp
|
||||
SourceLoader.cpp
|
||||
TypeCheckAttr.cpp
|
||||
TypeCheckAvailability.cpp
|
||||
|
||||
@@ -4,7 +4,6 @@ add_swift_library(swiftSyntax STATIC
|
||||
Format.cpp
|
||||
RawTokenSyntax.cpp
|
||||
GenericSyntax.cpp
|
||||
LegacyASTTransformer.cpp
|
||||
Trivia.cpp
|
||||
RawSyntax.cpp
|
||||
StmtSyntax.cpp
|
||||
|
||||
@@ -19,10 +19,10 @@
|
||||
#include "swift/Basic/SourceManager.h"
|
||||
#include "swift/AST/DiagnosticEngine.h"
|
||||
#include "swift/AST/DiagnosticsFrontend.h"
|
||||
#include "swift/AST/LegacyASTTransformer.h"
|
||||
#include "swift/Frontend/Frontend.h"
|
||||
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
|
||||
#include "swift/Syntax/Format.h"
|
||||
#include "swift/Syntax/LegacyASTTransformer.h"
|
||||
#include "swift/Subsystems.h"
|
||||
#include "llvm/ADT/ArrayRef.h"
|
||||
#include "llvm/ADT/StringRef.h"
|
||||
@@ -69,12 +69,12 @@ int doFormat(ArrayRef<StringRef> InputFiles) {
|
||||
|
||||
SmallVector<Decl *, 256> FileDecls;
|
||||
SF.getTopLevelDecls(FileDecls);
|
||||
sema::Semantics Sema;
|
||||
SyntaxASTMap ASTMap;
|
||||
for (auto *Decl : FileDecls) {
|
||||
if (Decl->escapedFromIfConfig()) {
|
||||
continue;
|
||||
}
|
||||
auto NewNode = transformAST(ASTNode(Decl), Sema, SourceMgr,
|
||||
auto NewNode = transformAST(ASTNode(Decl), ASTMap, SourceMgr,
|
||||
BufferID, Tokens);
|
||||
if (NewNode.hasValue()) {
|
||||
auto Reformatted = format(NewNode.getValue());
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
|
||||
#include "swift/Parse/Lexer.h"
|
||||
#include "swift/Subsystems.h"
|
||||
#include "swift/Syntax/LegacyASTTransformer.h"
|
||||
#include "swift/AST/LegacyASTTransformer.h"
|
||||
#include "swift/Syntax/Serialization/SyntaxSerialization.h"
|
||||
#include "swift/Syntax/SyntaxData.h"
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
@@ -151,14 +151,14 @@ int getSyntaxTree(const char *MainExecutablePath,
|
||||
|
||||
SmallVector<Decl *, 256> FileDecls;
|
||||
SF->getTopLevelDecls(FileDecls);
|
||||
sema::Semantics Sema;
|
||||
SyntaxASTMap ASTMap;
|
||||
// Convert the old ASTs to the new full-fidelity syntax tree and print
|
||||
// them out.
|
||||
for (auto *Decl : FileDecls) {
|
||||
if (Decl->escapedFromIfConfig()) {
|
||||
continue;
|
||||
}
|
||||
auto NewNode = transformAST(ASTNode(Decl), Sema, SourceMgr,
|
||||
auto NewNode = transformAST(ASTNode(Decl), ASTMap, SourceMgr,
|
||||
BufferID, Tokens);
|
||||
if (NewNode.hasValue()) {
|
||||
TopLevelDecls.push_back(NewNode.getValue());
|
||||
|
||||
Reference in New Issue
Block a user