Skip to content

Commit 96f637a

Browse files
authored
Merge pull request #12727 from nkcsgexi/revert-lib-syntax
2 parents 1aed6d2 + 4d1249a commit 96f637a

File tree

11 files changed

+126
-264
lines changed

11 files changed

+126
-264
lines changed

include/swift/AST/Module.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ namespace syntax {
8383
class SourceFileSyntax;
8484
class SyntaxParsingContext;
8585
class SyntaxParsingContextRoot;
86-
struct RawSyntaxInfo;
86+
struct RawTokenInfo;
8787
}
8888

8989
/// Discriminator for file-units.
@@ -1092,7 +1092,7 @@ class SourceFile final : public FileUnit {
10921092
Optional<std::vector<Token>> AllCorrectedTokens;
10931093

10941094
/// All of the raw token syntax nodes in the underlying source.
1095-
std::vector<syntax::RawSyntaxInfo> AllRawTokenSyntax;
1095+
std::vector<syntax::RawTokenInfo> AllRawTokenSyntax;
10961096

10971097
SourceFileSyntaxInfo &SyntaxInfo;
10981098

include/swift/Parse/Lexer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class Lexer {
246246
}
247247

248248
/// Lex a full token including leading and trailing trivia.
249-
syntax::RawSyntaxInfo fullLex();
249+
syntax::RawTokenInfo fullLex();
250250

251251
bool isKeepingComments() const {
252252
return RetainComments == CommentRetentionMode::ReturnAsTokens;

include/swift/Parse/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ tokenizeWithTrivia(const LangOptions &LangOpts,
14291429
void populateTokenSyntaxMap(const LangOptions &LangOpts,
14301430
const SourceManager &SM,
14311431
unsigned BufferID,
1432-
std::vector<syntax::RawSyntaxInfo> &Result);
1432+
std::vector<syntax::RawTokenInfo> &Result);
14331433
} // end namespace swift
14341434

14351435
#endif

include/swift/Syntax/SyntaxParsingContext.h

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,12 @@
2323

2424
namespace swift {
2525
class SourceFile;
26-
class Token;
2726

2827
namespace syntax {
2928

30-
/// The handler for parser to generate libSyntax entities.
31-
struct RawSyntaxInfo {
32-
/// Start location of this syntax node.
33-
SourceLoc StartLoc;
34-
35-
/// The number of tokens belong to the syntax node.
36-
unsigned TokCount;
37-
38-
/// The raw node.
39-
RC<RawSyntax> RawNode;
40-
RawSyntaxInfo(SourceLoc StartLoc, RC<RawSyntax> RawNode):
41-
RawSyntaxInfo(StartLoc, 1, RawNode) {}
42-
RawSyntaxInfo(SourceLoc StartLoc, unsigned TokCount, RC<RawSyntax> RawNode);
43-
template <typename SyntaxNode>
44-
SyntaxNode makeSyntax() const { return make<SyntaxNode>(RawNode); }
45-
46-
template <typename RawSyntaxNode>
47-
RC<RawSyntaxNode> getRaw() const {
48-
return RC<RawSyntaxNode>(cast<RawSyntaxNode>(RawNode));
49-
}
29+
struct RawTokenInfo {
30+
SourceLoc Loc;
31+
RC<RawTokenSyntax> Token;
5032
};
5133

5234
enum class SyntaxParsingContextKind: uint8_t {
@@ -58,12 +40,11 @@ enum class SyntaxParsingContextKind: uint8_t {
5840
/// create syntax nodes.
5941
class SyntaxParsingContext {
6042
protected:
61-
SyntaxParsingContext(SourceFile &SF, unsigned BufferID, Token &Tok);
43+
SyntaxParsingContext(bool Enabled);
6244
SyntaxParsingContext(SyntaxParsingContext &Another);
6345
public:
6446
struct ContextInfo;
6547
ContextInfo &ContextData;
66-
const Token &Tok;
6748

6849
// Add a token syntax at the given source location to the context; this
6950
// token node can be used to build more complex syntax nodes in later call
@@ -85,10 +66,12 @@ class SyntaxParsingContext {
8566
// of all other entity-specific contexts. This is the context Parser
8667
// has when the parser instance is firstly created.
8768
class SyntaxParsingContextRoot: public SyntaxParsingContext {
88-
SourceFile &File;
8969
public:
90-
SyntaxParsingContextRoot(SourceFile &File, unsigned BufferID, Token &Tok):
91-
SyntaxParsingContext(File, BufferID, Tok), File(File) {}
70+
struct GlobalInfo;
71+
72+
// Contains global information of the source file under parsing.
73+
GlobalInfo &GlobalData;
74+
SyntaxParsingContextRoot(SourceFile &SF, unsigned BufferID);
9275
~SyntaxParsingContextRoot();
9376
void addTokenSyntax(SourceLoc Loc) override {};
9477
void makeNode(SyntaxKind Kind) override {};
@@ -97,22 +80,21 @@ class SyntaxParsingContextRoot: public SyntaxParsingContext {
9780
};
9881
};
9982

100-
enum class SyntaxContextKind: uint8_t{
101-
Expr,
102-
Decl,
103-
};
104-
10583
// The base class for contexts that are created from a parent context.
10684
// The stack instance will set the context holder when the context
10785
// is firstly created and reset the context holder to the parent when
10886
// it's destructed.
10987
class SyntaxParsingContextChild: public SyntaxParsingContext {
11088
SyntaxParsingContext *Parent;
11189
SyntaxParsingContext *&ContextHolder;
112-
const SyntaxContextKind Kind;
90+
const SyntaxKind FinalKind;
11391
public:
11492
SyntaxParsingContextChild(SyntaxParsingContext *&ContextHolder,
115-
SyntaxContextKind Kind);
93+
SyntaxKind FinalKind):
94+
SyntaxParsingContext(*ContextHolder), Parent(ContextHolder),
95+
ContextHolder(ContextHolder), FinalKind(FinalKind) {
96+
ContextHolder = this;
97+
}
11698
~SyntaxParsingContextChild();
11799
void makeNode(SyntaxKind Kind) override;
118100
void addTokenSyntax(SourceLoc Loc) override;

lib/Parse/Lexer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,15 +737,15 @@ static bool rangeContainsPlaceholderEnd(const char *CurPtr,
737737
return false;
738738
}
739739

740-
syntax::RawSyntaxInfo Lexer::fullLex() {
740+
syntax::RawTokenInfo Lexer::fullLex() {
741741
if (NextToken.isEscapedIdentifier()) {
742742
LeadingTrivia.push_back(syntax::TriviaPiece::backtick());
743743
TrailingTrivia.insert(TrailingTrivia.begin(),
744744
syntax::TriviaPiece::backtick());
745745
}
746746
auto Loc = NextToken.getLoc();
747747
auto Result = syntax::RawTokenSyntax::make(NextToken.getKind(),
748-
OwnedString(NextToken.getText()),
748+
OwnedString(NextToken.getText()).copy(),
749749
syntax::SourcePresence::Present,
750750
{LeadingTrivia}, {TrailingTrivia});
751751
LeadingTrivia.clear();

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include <algorithm>
3838

3939
using namespace swift;
40-
using namespace syntax;
4140

4241
namespace {
4342
/// A RAII object for deciding whether this DeclKind needs special
@@ -2147,8 +2146,7 @@ void Parser::delayParseFromBeginningToHere(ParserPosition BeginParserPosition,
21472146
ParserResult<Decl>
21482147
Parser::parseDecl(ParseDeclOptions Flags,
21492148
llvm::function_ref<void(Decl*)> Handler) {
2150-
SyntaxParsingContextChild DeclParsingContext(SyntaxContext,
2151-
SyntaxContextKind::Decl);
2149+
21522150
if (Tok.is(tok::pound_if)) {
21532151
auto IfConfigResult = parseIfConfig(
21542152
[&](SmallVectorImpl<ASTNode> &Decls, bool IsActive) {

lib/Parse/ParseExpr.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ using namespace swift::syntax;
4040
/// \param isExprBasic Whether we're only parsing an expr-basic.
4141
ParserResult<Expr> Parser::parseExprImpl(Diag<> Message, bool isExprBasic) {
4242
// Start a context for creating expression syntax.
43-
SyntaxParsingContextChild ExprParsingContext(SyntaxContext,
44-
SyntaxContextKind::Expr);
43+
SyntaxParsingContextChild ExprParsingContext(SyntaxContext, SyntaxKind::Expr);
4544

4645
// If we are parsing a refutable pattern, check to see if this is the start
4746
// of a let/var/is pattern. If so, parse it to an UnresolvedPatternExpr and
@@ -1805,8 +1804,7 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
18051804
// Create a syntax node for string literal.
18061805
SyntaxContext->addTokenSyntax(Tok.getLoc());
18071806
SyntaxContext->makeNode(SyntaxKind::StringLiteralExpr);
1808-
SyntaxParsingContextChild LocalContext(SyntaxContext,
1809-
SyntaxContextKind::Expr);
1807+
SyntaxParsingContextChild LocalContext(SyntaxContext, SyntaxKind::Expr);
18101808

18111809
// FIXME: Avoid creating syntax nodes for string interpolation.
18121810
LocalContext.disable();

lib/Parse/Parser.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ swift::tokenizeWithTrivia(const LangOptions &LangOpts,
268268
syntax::AbsolutePosition>> Tokens;
269269
syntax::AbsolutePosition RunningPos;
270270
do {
271-
auto ThisToken = L.fullLex().getRaw<syntax::RawTokenSyntax>();
271+
auto ThisToken = L.fullLex().Token;
272272
auto ThisTokenPos = ThisToken->accumulateAbsolutePosition(RunningPos);
273273
Tokens.push_back({ThisToken, ThisTokenPos});
274274
} while (Tokens.back().first->isNot(tok::eof));
@@ -279,15 +279,15 @@ swift::tokenizeWithTrivia(const LangOptions &LangOpts,
279279
void swift::populateTokenSyntaxMap(const LangOptions &LangOpts,
280280
const SourceManager &SM,
281281
unsigned BufferID,
282-
std::vector<syntax::RawSyntaxInfo> &Result) {
282+
std::vector<syntax::RawTokenInfo> &Result) {
283283
if (!Result.empty())
284284
return;
285285
Lexer L(LangOpts, SM, BufferID, /*Diags=*/nullptr, /*InSILMode=*/false,
286286
CommentRetentionMode::AttachToNextToken,
287287
TriviaRetentionMode::WithTrivia);
288288
do {
289289
Result.emplace_back(L.fullLex());
290-
if (Result.back().getRaw<syntax::RawTokenSyntax>()->is(tok::eof))
290+
if (Result.back().Token->is(tok::eof))
291291
return;
292292
} while (true);
293293
}
@@ -434,7 +434,7 @@ Parser::Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
434434
TokReceiver(SF.shouldKeepTokens() ?
435435
new TokenRecorder(SF) :
436436
new ConsumeTokenReceiver()),
437-
SyntaxContext(new syntax::SyntaxParsingContextRoot(SF, L->getBufferID(), Tok)) {
437+
SyntaxContext(new syntax::SyntaxParsingContextRoot(SF, L->getBufferID())) {
438438

439439
State = PersistentState;
440440
if (!State) {

0 commit comments

Comments
 (0)