Skip to content

Commit e4794e8

Browse files
committed
[Parse] Simplify SILParserState and its use in Parser.
Preparation for separating ParseSIL out into its own library, but a nice cleanup regardless.
1 parent e7ec1db commit e4794e8

File tree

5 files changed

+87
-60
lines changed

5 files changed

+87
-60
lines changed

include/swift/Parse/Parser.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace swift {
4545
class ScopeInfo;
4646
struct TypeLoc;
4747
class TupleType;
48-
class SILParserState;
48+
class SILParserTUState;
4949
class SourceManager;
5050
class PersistentParserState;
5151
class CodeCompletionCallbacks;
@@ -85,7 +85,7 @@ class Parser {
8585
DiagnosticEngine &Diags;
8686
SourceFile &SF;
8787
Lexer *L;
88-
SILParserState *SIL; // Non-null when parsing a .sil file.
88+
SILParserTUState *SIL; // Non-null when parsing a .sil file.
8989
PersistentParserState *State;
9090
std::unique_ptr<PersistentParserState> OwnedState;
9191
DeclContext *CurDeclContext;
@@ -304,10 +304,10 @@ class Parser {
304304
llvm::SmallVector<StructureMarker, 16> StructureMarkers;
305305

306306
public:
307-
Parser(unsigned BufferID, SourceFile &SF, SILParserState *SIL,
307+
Parser(unsigned BufferID, SourceFile &SF, SILParserTUState *SIL,
308308
PersistentParserState *PersistentState = nullptr);
309309
Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
310-
SILParserState *SIL = nullptr,
310+
SILParserTUState *SIL = nullptr,
311311
PersistentParserState *PersistentState = nullptr);
312312
~Parser();
313313

include/swift/Subsystems.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ namespace swift {
6565
struct TypeLoc;
6666
class UnifiedStatsReporter;
6767

68-
/// SILParserState - This is a context object used to optionally maintain SIL
69-
/// parsing context for the parser.
68+
/// Used to optionally maintain SIL parsing context for the parser.
69+
///
70+
/// When not parsing SIL, this has no overhead.
7071
class SILParserState {
7172
public:
72-
SILModule *M;
73-
SILParserTUState *S;
73+
std::unique_ptr<SILParserTUState> Impl;
7474

7575
explicit SILParserState(SILModule *M);
7676
~SILParserState();

lib/Parse/ParseSIL.cpp

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

13+
#include "ParseSIL.h"
1314
#include "swift/AST/ASTWalker.h"
1415
#include "swift/AST/ExistentialLayout.h"
1516
#include "swift/AST/GenericEnvironment.h"
@@ -34,38 +35,10 @@ using namespace swift;
3435
// SILParserState implementation
3536
//===----------------------------------------------------------------------===//
3637

37-
namespace swift {
38-
class SILParserTUState {
39-
public:
40-
SILParserTUState(SILModule &M) : M(M) {}
41-
~SILParserTUState();
42-
43-
SILModule &M;
44-
45-
/// This is all of the forward referenced functions with
46-
/// the location for where the reference is.
47-
llvm::DenseMap<Identifier,
48-
std::pair<SILFunction*, SourceLoc>> ForwardRefFns;
49-
/// A list of all functions forward-declared by a sil_scope.
50-
llvm::DenseSet<SILFunction *> PotentialZombieFns;
51-
52-
/// A map from textual .sil scope number to SILDebugScopes.
53-
llvm::DenseMap<unsigned, SILDebugScope *> ScopeSlots;
54-
55-
/// Did we parse a sil_stage for this module?
56-
bool DidParseSILStage = false;
57-
58-
DiagnosticEngine *Diags = nullptr;
59-
};
60-
} // namespace swift
61-
62-
SILParserState::SILParserState(SILModule *M) : M(M) {
63-
S = M ? new SILParserTUState(*M) : nullptr;
64-
}
38+
SILParserState::SILParserState(SILModule *M)
39+
: Impl(M ? new SILParserTUState(*M) : nullptr) { }
6540

66-
SILParserState::~SILParserState() {
67-
delete S;
68-
}
41+
SILParserState::~SILParserState() = default;
6942

7043
SILParserTUState::~SILParserTUState() {
7144
if (!ForwardRefFns.empty())
@@ -138,7 +111,7 @@ namespace {
138111
bool localScope);
139112
public:
140113
SILParser(Parser &P)
141-
: P(P), SILMod(*P.SIL->M), TUState(*P.SIL->S),
114+
: P(P), SILMod(P.SIL->M), TUState(*P.SIL),
142115
ParsedTypeCallback([](Type ty) {}) {}
143116

144117
/// diagnoseProblems - After a function is fully parse, emit any diagnostics
@@ -4885,13 +4858,13 @@ bool Parser::parseDeclSILStage() {
48854858
return true;
48864859
}
48874860

4888-
if (SIL->S->DidParseSILStage) {
4861+
if (SIL->DidParseSILStage) {
48894862
diagnose(stageLoc, diag::multiple_sil_stage_decls);
48904863
return false;
48914864
}
48924865

4893-
SIL->M->setStage(stage);
4894-
SIL->S->DidParseSILStage = true;
4866+
SIL->M.setStage(stage);
4867+
SIL->DidParseSILStage = true;
48954868
return false;
48964869
}
48974870

@@ -4927,7 +4900,7 @@ bool Parser::parseSILGlobal() {
49274900
GlobalLinkage = SILLinkage::DefaultForDefinition;
49284901

49294902
// FIXME: check for existing global variable?
4930-
auto *GV = SILGlobalVariable::create(*SIL->M, GlobalLinkage.getValue(),
4903+
auto *GV = SILGlobalVariable::create(SIL->M, GlobalLinkage.getValue(),
49314904
isSerialized,
49324905
GlobalName.str(),GlobalType,
49334906
RegularLocation(NameLoc));
@@ -5011,7 +4984,7 @@ bool Parser::parseSILVTable() {
50114984
VTableState.parseSILIdentifier(FuncName, FuncLoc,
50124985
diag::expected_sil_value_name))
50134986
return true;
5014-
Func = SIL->M->lookUpFunction(FuncName.str());
4987+
Func = SIL->M.lookUpFunction(FuncName.str());
50154988
if (!Func) {
50164989
diagnose(FuncLoc, diag::sil_vtable_func_not_found, FuncName);
50174990
return true;
@@ -5027,7 +5000,7 @@ bool Parser::parseSILVTable() {
50275000
parseMatchingToken(tok::r_brace, RBraceLoc, diag::expected_sil_rbrace,
50285001
LBraceLoc);
50295002

5030-
SILVTable::create(*SIL->M, theClass, vtableEntries);
5003+
SILVTable::create(SIL->M, theClass, vtableEntries);
50315004
return false;
50325005
}
50335006

@@ -5309,7 +5282,7 @@ bool Parser::parseSILWitnessTable() {
53095282

53105283
SILWitnessTable *wt = nullptr;
53115284
if (theConformance) {
5312-
wt = SIL->M->lookUpWitnessTable(theConformance, false);
5285+
wt = SIL->M.lookUpWitnessTable(theConformance, false);
53135286
assert((!wt || wt->isDeclaration()) &&
53145287
"Attempting to create duplicate witness table.");
53155288
}
@@ -5321,7 +5294,7 @@ bool Parser::parseSILWitnessTable() {
53215294
Linkage = SILLinkage::PublicExternal;
53225295
// We ignore empty witness table without normal protocol conformance.
53235296
if (!wt && theConformance)
5324-
wt = SILWitnessTable::create(*SIL->M, *Linkage, theConformance);
5297+
wt = SILWitnessTable::create(SIL->M, *Linkage, theConformance);
53255298
BodyScope.reset();
53265299
return false;
53275300
}
@@ -5441,7 +5414,7 @@ bool Parser::parseSILWitnessTable() {
54415414
diag::expected_sil_value_name))
54425415
return true;
54435416

5444-
Func = SIL->M->lookUpFunction(FuncName.str());
5417+
Func = SIL->M.lookUpFunction(FuncName.str());
54455418
if (!Func) {
54465419
diagnose(FuncLoc, diag::sil_witness_func_not_found, FuncName);
54475420
return true;
@@ -5462,7 +5435,7 @@ bool Parser::parseSILWitnessTable() {
54625435
Linkage = SILLinkage::Public;
54635436

54645437
if (!wt)
5465-
wt = SILWitnessTable::create(*SIL->M, *Linkage, theConformance);
5438+
wt = SILWitnessTable::create(SIL->M, *Linkage, theConformance);
54665439
wt->convertToDefinition(witnessEntries, isSerialized);
54675440
BodyScope.reset();
54685441
return false;
@@ -5532,7 +5505,7 @@ bool Parser::parseSILDefaultWitnessTable() {
55325505
diag::expected_sil_value_name))
55335506
return true;
55345507

5535-
SILFunction *Func = SIL->M->lookUpFunction(FuncName.str());
5508+
SILFunction *Func = SIL->M.lookUpFunction(FuncName.str());
55365509
if (!Func) {
55375510
diagnose(FuncLoc, diag::sil_witness_func_not_found, FuncName);
55385511
return true;
@@ -5549,7 +5522,7 @@ bool Parser::parseSILDefaultWitnessTable() {
55495522
if (!Linkage)
55505523
Linkage = SILLinkage::Public;
55515524

5552-
SILDefaultWitnessTable::create(*SIL->M, *Linkage, protocol, witnessEntries);
5525+
SILDefaultWitnessTable::create(SIL->M, *Linkage, protocol, witnessEntries);
55535526
BodyScope.reset();
55545527
return false;
55555528
}
@@ -5632,7 +5605,7 @@ bool Parser::parseSILCoverageMap() {
56325605
diag::expected_sil_value_name))
56335606
return true;
56345607

5635-
SILFunction *Func = SIL->M->lookUpFunction(FuncName.str());
5608+
SILFunction *Func = SIL->M.lookUpFunction(FuncName.str());
56365609
if (!Func) {
56375610
diagnose(FuncLoc, diag::sil_coverage_func_not_found, FuncName);
56385611
return true;
@@ -5688,7 +5661,7 @@ bool Parser::parseSILCoverageMap() {
56885661
LBraceLoc);
56895662

56905663
if (!BodyHasError)
5691-
SILCoverageMap::create(*SIL->M, Filename.str(), FuncName.str(),
5664+
SILCoverageMap::create(SIL->M, Filename.str(), FuncName.str(),
56925665
Func->isPossiblyUsedExternally(), Hash, Regions,
56935666
Builder.getExpressions());
56945667
return false;
@@ -5763,12 +5736,12 @@ bool Parser::parseSILScope() {
57635736
parseMatchingToken(tok::r_brace, RBraceLoc, diag::expected_sil_rbrace,
57645737
LBraceLoc);
57655738

5766-
auto &Scope = SIL->S->ScopeSlots[Slot];
5739+
auto &Scope = SIL->ScopeSlots[Slot];
57675740
if (Scope) {
57685741
diagnose(SlotLoc, diag::sil_scope_redefined, Slot);
57695742
return true;
57705743
}
57715744

5772-
Scope = new (*SIL->M) SILDebugScope(Loc, ParentFn, Parent, InlinedAt);
5745+
Scope = new (SIL->M) SILDebugScope(Loc, ParentFn, Parent, InlinedAt);
57735746
return false;
57745747
}

lib/Parse/ParseSIL.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//===--- ParseSIL.h - Internal interface to SIL parsing ---------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_PARSER_PARSESIL_H
14+
#define SWIFT_PARSER_PARSESIL_H
15+
16+
#include "swift/AST/Identifier.h"
17+
#include "swift/Basic/SourceManager.h"
18+
#include "llvm/ADT/DenseMap.h"
19+
#include "llvm/ADT/DenseSet.h"
20+
#include <memory>
21+
22+
namespace swift {
23+
class DiagnosticEngine;
24+
class SILDebugScope;
25+
class SILFunction;
26+
class SILModule;
27+
28+
class SILParserTUState {
29+
public:
30+
explicit SILParserTUState(SILModule &M) : M(M) {}
31+
~SILParserTUState();
32+
33+
SILModule &M;
34+
35+
/// This is all of the forward referenced functions with
36+
/// the location for where the reference is.
37+
llvm::DenseMap<Identifier,
38+
std::pair<SILFunction*, SourceLoc>> ForwardRefFns;
39+
/// A list of all functions forward-declared by a sil_scope.
40+
llvm::DenseSet<SILFunction *> PotentialZombieFns;
41+
42+
/// A map from textual .sil scope number to SILDebugScopes.
43+
llvm::DenseMap<unsigned, SILDebugScope *> ScopeSlots;
44+
45+
/// Did we parse a sil_stage for this module?
46+
bool DidParseSILStage = false;
47+
48+
DiagnosticEngine *Diags = nullptr;
49+
};
50+
} // end namespace swift
51+
52+
#endif // SWIFT_PARSER_PARSESIL_H
53+

lib/Parse/Parser.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "swift/Parse/Parser.h"
18+
#include "ParseSIL.h"
1819
#include "swift/Subsystems.h"
1920
#include "swift/AST/ASTWalker.h"
2021
#include "swift/AST/DiagnosticsParse.h"
@@ -141,7 +142,7 @@ bool swift::parseIntoSourceFile(SourceFile &SF,
141142
PersistentParserState *PersistentState,
142143
DelayedParsingCallbacks *DelayedParseCB) {
143144
SharedTimer timer("Parsing");
144-
Parser P(BufferID, SF, SIL, PersistentState);
145+
Parser P(BufferID, SF, SIL ? SIL->Impl.get() : nullptr, PersistentState);
145146
PrettyStackTraceParser StackTrace(P);
146147

147148
llvm::SaveAndRestore<bool> S(P.IsParsingInterfaceTokens, true);
@@ -312,19 +313,19 @@ swift::tokenizeWithTrivia(const LangOptions &LangOpts,
312313
// Setup and Helper Methods
313314
//===----------------------------------------------------------------------===//
314315

315-
Parser::Parser(unsigned BufferID, SourceFile &SF, SILParserState *SIL,
316+
Parser::Parser(unsigned BufferID, SourceFile &SF, SILParserTUState *SIL,
316317
PersistentParserState *PersistentState)
317318
: Parser(std::unique_ptr<Lexer>(
318319
new Lexer(SF.getASTContext().LangOpts, SF.getASTContext().SourceMgr,
319320
BufferID, &SF.getASTContext().Diags,
320321
/*InSILMode=*/SIL != nullptr,
321322
SF.getASTContext().LangOpts.AttachCommentsToDecls
322323
? CommentRetentionMode::AttachToNextToken
323-
: CommentRetentionMode::None)), SF, SIL, PersistentState) {
324+
: CommentRetentionMode::None)), SF, SIL, PersistentState){
324325
}
325326

326327
Parser::Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
327-
SILParserState *SIL, PersistentParserState *PersistentState)
328+
SILParserTUState *SIL, PersistentParserState *PersistentState)
328329
: SourceMgr(SF.getASTContext().SourceMgr),
329330
Diags(SF.getASTContext().Diags),
330331
SF(SF),

0 commit comments

Comments
 (0)