Skip to content

Commit 152d71c

Browse files
author
marcrasi
authored
Cherry pick autocomplete fixes to tensorflow (#29707)
Cherry-picks some PRs necessary for https://bugs.swift.org/browse/SR-12076 into `tensorflow`. This PR is only cherry-picks and llvm-project commit hash update, no additional changes. Full explanation of the fix is in the PR description of the corresponding llvm-project PR: swiftlang/llvm-project#751.
1 parent e485f11 commit 152d71c

33 files changed

+342
-350
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ ERROR(expected_precedencegroup_relation,none,
470470
(StringRef))
471471

472472
// SIL
473+
ERROR(expected_sil_keyword,none,
474+
"expected SIL keyword", ())
473475
ERROR(inout_not_attribute, none,
474476
"@inout is no longer an attribute", ())
475477
ERROR(only_allowed_in_sil,none,

include/swift/AST/TypeCheckRequests.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,17 +1959,15 @@ class DynamicallyReplacedDeclRequest
19591959

19601960
class TypeCheckSourceFileRequest :
19611961
public SimpleRequest<TypeCheckSourceFileRequest,
1962-
bool (SourceFile *, unsigned),
1963-
CacheKind::SeparatelyCached> {
1962+
bool (SourceFile *), CacheKind::SeparatelyCached> {
19641963
public:
19651964
using SimpleRequest::SimpleRequest;
19661965

19671966
private:
19681967
friend SimpleRequest;
19691968

19701969
// Evaluation.
1971-
llvm::Expected<bool> evaluate(Evaluator &evaluator,
1972-
SourceFile *SF, unsigned StartElem) const;
1970+
llvm::Expected<bool> evaluate(Evaluator &evaluator, SourceFile *SF) const;
19731971

19741972
public:
19751973
// Separate caching.

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ SWIFT_REQUEST(TypeChecker, HasDefaultInitRequest,
211211
SWIFT_REQUEST(TypeChecker, SynthesizeDefaultInitRequest,
212212
ConstructorDecl *(NominalTypeDecl *), Cached, NoLocationInfo)
213213
SWIFT_REQUEST(TypeChecker, TypeCheckSourceFileRequest,
214-
bool(SouceFile *, unsigned), SeparatelyCached, NoLocationInfo)
214+
bool(SouceFile *), SeparatelyCached, NoLocationInfo)
215215
SWIFT_REQUEST(TypeChecker, TypeWitnessRequest,
216216
TypeWitnessAndDecl(NormalProtocolConformance *,
217217
AssociatedTypeDecl *),

include/swift/Frontend/Frontend.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,6 @@ class CompilerInstance {
653653
};
654654

655655
private:
656-
void createREPLFile(const ImplicitImports &implicitImports);
657-
658656
void addMainFileToModule(const ImplicitImports &implicitImports);
659657

660658
void performSemaUpTo(SourceFile::ASTStage_t LimitStage);

include/swift/Parse/Parser.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,8 @@ class Parser {
413413
bool DelayBodyParsing = true);
414414
~Parser();
415415

416-
bool isInSILMode() const { return SIL != nullptr; }
416+
/// Returns true if the buffer being parsed is allowed to contain SIL.
417+
bool isInSILMode() const;
417418

418419
/// Calling this function to finalize libSyntax tree creation without destroying
419420
/// the parser instance.
@@ -430,9 +431,8 @@ class Parser {
430431
PreviousLoc);
431432
}
432433

433-
ParserPosition getParserPosition(const PersistentParserState::ParserPos &Pos){
434-
return ParserPosition(L->getStateForBeginningOfTokenLoc(Pos.Loc),
435-
Pos.PrevLoc);
434+
ParserPosition getParserPosition(SourceLoc loc, SourceLoc previousLoc) {
435+
return ParserPosition(L->getStateForBeginningOfTokenLoc(loc), previousLoc);
436436
}
437437

438438
void restoreParserPosition(ParserPosition PP, bool enableDiagnostics = false) {
@@ -661,6 +661,9 @@ class Parser {
661661
/// Returns \c true if the parser hit the eof before finding matched '}'.
662662
bool skipBracedBlock();
663663

664+
/// Skip over SIL decls until we encounter the start of a Swift decl or eof.
665+
void skipSILUntilSwiftDecl();
666+
664667
/// If the parser is generating only a syntax tree, try loading the current
665668
/// node from a previously generated syntax tree.
666669
/// Returns \c true if the node has been loaded and inserted into the current
@@ -880,11 +883,18 @@ class Parser {
880883
//===--------------------------------------------------------------------===//
881884
// Decl Parsing
882885

883-
/// Return true if parser is at the start of a decl or decl-import.
884-
bool isStartOfDecl();
886+
/// Returns true if parser is at the start of a Swift decl or decl-import.
887+
bool isStartOfSwiftDecl();
888+
889+
/// Returns true if the parser is at the start of a SIL decl.
890+
bool isStartOfSILDecl();
885891

892+
/// Parse the top-level Swift decls into the source file.
886893
void parseTopLevel();
887894

895+
/// Parse the top-level SIL decls into the SIL module.
896+
void parseTopLevelSIL();
897+
888898
/// Flags that control the parsing of declarations.
889899
enum ParseDeclFlags {
890900
PD_Default = 0,

include/swift/Parse/PersistentParserState.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,12 @@ class CodeCompletionDelayedDeclState {
5959
/// Parser state persistent across multiple parses.
6060
class PersistentParserState {
6161
public:
62-
struct ParserPos {
63-
SourceLoc Loc;
64-
SourceLoc PrevLoc;
65-
66-
bool isValid() const { return Loc.isValid(); }
67-
};
68-
69-
bool InPoundLineEnvironment = false;
7062
// FIXME: When condition evaluation moves to a later phase, remove this bit
7163
// and adjust the client call 'performParseOnly'.
7264
bool PerformConditionEvaluation = true;
7365
private:
7466
swift::ScopeInfo ScopeInfo;
7567

76-
/// Parser sets this if it stopped parsing before the buffer ended.
77-
ParserPosition MarkedPos;
78-
7968
std::unique_ptr<CodeCompletionDelayedDeclState> CodeCompletionDelayedDeclStat;
8069

8170
/// The local context for all top-level code.
@@ -113,19 +102,6 @@ class PersistentParserState {
113102
TopLevelContext &getTopLevelContext() {
114103
return TopLevelCode;
115104
}
116-
117-
void markParserPosition(ParserPosition Pos,
118-
bool InPoundLineEnvironment) {
119-
MarkedPos = Pos;
120-
this->InPoundLineEnvironment = InPoundLineEnvironment;
121-
}
122-
123-
/// Returns the marked parser position and resets it.
124-
ParserPosition takeParserPosition() {
125-
ParserPosition Pos = MarkedPos;
126-
MarkedPos = ParserPosition();
127-
return Pos;
128-
}
129105
};
130106

131107
} // end namespace swift

include/swift/Subsystems.h

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -106,29 +106,21 @@ namespace swift {
106106

107107
/// Parse a single buffer into the given source file.
108108
///
109-
/// If the source file is the main file, stop parsing after the next
110-
/// stmt-brace-item with side-effects.
109+
/// \param SF The file within the module being parsed.
111110
///
112-
/// \param SF the file within the module being parsed.
111+
/// \param BufferID The buffer to parse from.
113112
///
114-
/// \param BufferID the buffer to parse from.
113+
/// \param PersistentState If non-null the same PersistentState object can be
114+
/// used to save parser state for code completion.
115115
///
116-
/// \param[out] Done set to \c true if end of the buffer was reached.
117-
///
118-
/// \param SIL if non-null, we're parsing a SIL file.
119-
///
120-
/// \param PersistentState if non-null the same PersistentState object can
121-
/// be used to resume parsing or parse delayed function bodies.
122-
void parseIntoSourceFile(SourceFile &SF, unsigned BufferID, bool *Done,
123-
SILParserState *SIL = nullptr,
116+
/// \param DelayBodyParsing Whether parsing of type and function bodies can be
117+
/// delayed.
118+
void parseIntoSourceFile(SourceFile &SF, unsigned BufferID,
124119
PersistentParserState *PersistentState = nullptr,
125120
bool DelayBodyParsing = true);
126121

127-
/// Parse a single buffer into the given source file, until the full source
128-
/// contents are parsed.
129-
void parseIntoSourceFileFull(SourceFile &SF, unsigned BufferID,
130-
PersistentParserState *PersistentState = nullptr,
131-
bool DelayBodyParsing = true);
122+
/// Parse a source file's SIL declarations into a given SIL module.
123+
void parseSourceFileSIL(SourceFile &SF, SILParserState *sil);
132124

133125
/// Finish the code completion.
134126
void performCodeCompletionSecondPass(PersistentParserState &PersistentState,
@@ -145,10 +137,7 @@ namespace swift {
145137

146138
/// Once parsing is complete, this walks the AST to resolve imports, record
147139
/// operators, and do other top-level validation.
148-
///
149-
/// \param StartElem Where to start for incremental name binding in the main
150-
/// source file.
151-
void performNameBinding(SourceFile &SF, unsigned StartElem = 0);
140+
void performNameBinding(SourceFile &SF);
152141

153142
/// Once type-checking is complete, this instruments code with calls to an
154143
/// intrinsic that record the expected values of local variables so they can
@@ -179,10 +168,7 @@ namespace swift {
179168

180169
/// Once parsing and name-binding are complete, this walks the AST to resolve
181170
/// types and diagnose problems therein.
182-
///
183-
/// \param StartElem Where to start for incremental type-checking in the main
184-
/// source file.
185-
void performTypeChecking(SourceFile &SF, unsigned StartElem = 0);
171+
void performTypeChecking(SourceFile &SF);
186172

187173
/// Now that we have type-checked an entire module, perform any type
188174
/// checking that requires the full module, e.g., Objective-C method

lib/Frontend/Frontend.cpp

Lines changed: 32 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,15 @@ void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage) {
727727
const ImplicitImports implicitImports(*this);
728728

729729
if (Invocation.getInputKind() == InputFileKind::SwiftREPL) {
730-
createREPLFile(implicitImports);
730+
// Create the initial empty REPL file. This only exists to feed in the
731+
// implicit imports such as the standard library.
732+
auto *replFile = createSourceFileForMainModule(
733+
SourceFileKind::REPL, implicitImports.kind, /*BufferID*/ None);
734+
addAdditionalInitialImportsTo(replFile, implicitImports);
735+
736+
// Given this file is empty, we can go ahead and just mark it as having been
737+
// type checked.
738+
replFile->ASTStage = SourceFile::TypeChecked;
731739
return;
732740
}
733741

@@ -824,12 +832,6 @@ void CompilerInstance::getImplicitlyImportedModules(
824832
}
825833
}
826834

827-
void CompilerInstance::createREPLFile(const ImplicitImports &implicitImports) {
828-
auto *SingleInputFile = createSourceFileForMainModule(
829-
Invocation.getSourceFileKind(), implicitImports.kind, None);
830-
addAdditionalInitialImportsTo(SingleInputFile, implicitImports);
831-
}
832-
833835
void CompilerInstance::addMainFileToModule(
834836
const ImplicitImports &implicitImports) {
835837
auto *MainFile = createSourceFileForMainModule(
@@ -913,14 +915,8 @@ void CompilerInstance::parseLibraryFile(
913915
auto DidSuppressWarnings = Diags.getSuppressWarnings();
914916
Diags.setSuppressWarnings(DidSuppressWarnings || !IsPrimary);
915917

916-
bool Done;
917-
do {
918-
// Parser may stop at some erroneous constructions like #else, #endif
919-
// or '}' in some cases, continue parsing until we are done
920-
parseIntoSourceFile(*NextInput, BufferID, &Done, nullptr,
921-
PersistentState.get(),
922-
/*DelayedBodyParsing=*/!IsPrimary);
923-
} while (!Done);
918+
parseIntoSourceFile(*NextInput, BufferID, PersistentState.get(),
919+
/*DelayedBodyParsing=*/!IsPrimary);
924920

925921
Diags.setSuppressWarnings(DidSuppressWarnings);
926922

@@ -953,6 +949,7 @@ bool CompilerInstance::parsePartialModulesAndLibraryFiles(
953949

954950
void CompilerInstance::parseAndTypeCheckMainFileUpTo(
955951
SourceFile::ASTStage_t LimitStage) {
952+
assert(LimitStage >= SourceFile::NameBound);
956953
FrontendStatsTracer tracer(Context->Stats,
957954
"parse-and-typecheck-main-file");
958955
bool mainIsPrimary =
@@ -965,45 +962,23 @@ void CompilerInstance::parseAndTypeCheckMainFileUpTo(
965962
auto DidSuppressWarnings = Diags.getSuppressWarnings();
966963
Diags.setSuppressWarnings(DidSuppressWarnings || !mainIsPrimary);
967964

968-
SILParserState SILContext(TheSILModule.get());
969-
unsigned CurTUElem = 0;
970-
bool Done;
971-
do {
972-
// Pump the parser multiple times if necessary. It will return early
973-
// after parsing any top level code in a main module, or in SIL mode when
974-
// there are chunks of swift decls (e.g. imports and types) interspersed
975-
// with 'sil' definitions.
976-
parseIntoSourceFile(MainFile, MainFile.getBufferID().getValue(), &Done,
977-
TheSILModule ? &SILContext : nullptr,
978-
PersistentState.get(),
979-
!mainIsPrimary);
980-
981-
// For SIL we actually have to interleave parsing and type checking
982-
// because the SIL parser expects to see fully type checked declarations.
983-
if (TheSILModule) {
984-
if (Done || CurTUElem < MainFile.getTopLevelDecls().size()) {
985-
assert(mainIsPrimary);
986-
performTypeChecking(MainFile, CurTUElem);
987-
}
988-
}
965+
// Parse the Swift decls into the source file.
966+
parseIntoSourceFile(MainFile, MainBufferID, PersistentState.get(),
967+
/*delayBodyParsing*/ !mainIsPrimary);
989968

990-
CurTUElem = MainFile.getTopLevelDecls().size();
991-
} while (!Done);
992-
993-
if (!TheSILModule) {
994-
if (mainIsPrimary) {
995-
switch (LimitStage) {
996-
case SourceFile::Parsing:
997-
case SourceFile::Parsed:
998-
llvm_unreachable("invalid limit stage");
999-
case SourceFile::NameBound:
1000-
performNameBinding(MainFile);
1001-
break;
1002-
case SourceFile::TypeChecked:
1003-
performTypeChecking(MainFile);
1004-
break;
1005-
}
1006-
}
969+
// For a primary, also perform type checking if needed. Otherwise, just do
970+
// name binding.
971+
if (mainIsPrimary && LimitStage >= SourceFile::TypeChecked) {
972+
performTypeChecking(MainFile);
973+
} else {
974+
assert(!TheSILModule && "Should perform type checking for SIL");
975+
performNameBinding(MainFile);
976+
}
977+
978+
// Parse the SIL decls if needed.
979+
if (TheSILModule) {
980+
SILParserState SILContext(TheSILModule.get());
981+
parseSourceFileSIL(MainFile, &SILContext);
1007982
}
1008983

1009984
Diags.setSuppressWarnings(DidSuppressWarnings);
@@ -1012,12 +987,6 @@ void CompilerInstance::parseAndTypeCheckMainFileUpTo(
1012987
Invocation.getFrontendOptions().DebuggerTestingTransform) {
1013988
performDebuggerTestingTransform(MainFile);
1014989
}
1015-
1016-
if (!TheSILModule) {
1017-
if (!mainIsPrimary) {
1018-
performNameBinding(MainFile);
1019-
}
1020-
}
1021990
}
1022991

1023992
static void
@@ -1109,8 +1078,8 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
11091078
SourceFileKind::Library, SourceFile::ImplicitModuleImportKind::None,
11101079
BufferID);
11111080

1112-
parseIntoSourceFileFull(*NextInput, BufferID, PersistentState.get(),
1113-
shouldDelayBodies(BufferID));
1081+
parseIntoSourceFile(*NextInput, BufferID, PersistentState.get(),
1082+
shouldDelayBodies(BufferID));
11141083
}
11151084

11161085
// Now parse the main file.
@@ -1120,8 +1089,8 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
11201089
MainFile.SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache();
11211090
assert(MainBufferID == MainFile.getBufferID());
11221091

1123-
parseIntoSourceFileFull(MainFile, MainBufferID, PersistentState.get(),
1124-
shouldDelayBodies(MainBufferID));
1092+
parseIntoSourceFile(MainFile, MainBufferID, PersistentState.get(),
1093+
shouldDelayBodies(MainBufferID));
11251094
}
11261095

11271096
assert(Context->LoadedModules.size() == 1 &&

lib/IDE/CompletionInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
217217
newSF->enableInterfaceHash();
218218
// Ensure all non-function-body tokens are hashed into the interface hash
219219
Ctx->LangOpts.EnableTypeFingerprints = false;
220-
parseIntoSourceFileFull(*newSF, tmpBufferID, &newState);
220+
parseIntoSourceFile(*newSF, tmpBufferID, &newState);
221221
// Couldn't find any completion token?
222222
if (!newState.hasCodeCompletionDelayedDeclState())
223223
return false;

0 commit comments

Comments
 (0)