Skip to content

Commit fd1bfd1

Browse files
committed
Merge remote-tracking branch 'origin/master' into master-next
2 parents 3905e9e + 7320ad5 commit fd1bfd1

File tree

9 files changed

+111
-42
lines changed

9 files changed

+111
-42
lines changed

include/swift/AST/ASTTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
SWIFT_TYPEID_NAMED(NominalTypeDecl *, NominalTypeDecl)
1818
SWIFT_TYPEID_NAMED(VarDecl *, VarDecl)
1919
SWIFT_TYPEID_NAMED(ValueDecl *, ValueDecl)
20+
SWIFT_TYPEID_NAMED(ProtocolDecl *, ProtocolDecl)
2021
SWIFT_TYPEID_NAMED(Decl *, Decl)
2122
SWIFT_TYPEID(Type)
2223
SWIFT_TYPEID(PropertyWrapperBackingPropertyInfo)

include/swift/IDE/IDERequestIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ SWIFT_TYPEID(CursorInfoRequest)
1818
SWIFT_TYPEID(RangeInfoRequest)
1919
SWIFT_TYPEID(ProvideDefaultImplForRequest)
2020
SWIFT_TYPEID(CollectOverriddenDeclsRequest)
21+
SWIFT_TYPEID(ResolveProtocolNameRequest)

include/swift/IDE/IDERequests.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,58 @@ class CollectOverriddenDeclsRequest:
229229
SourceLoc getNearestLoc() const { return SourceLoc(); };
230230
};
231231

232+
//----------------------------------------------------------------------------//
233+
// ResolveProtocolNameRequest
234+
//----------------------------------------------------------------------------//
235+
struct ProtocolNameOwner {
236+
DeclContext *DC;
237+
StringRef Name;
238+
ProtocolNameOwner(DeclContext *DC, StringRef Name): DC(DC), Name(Name) {}
239+
240+
friend llvm::hash_code hash_value(const ProtocolNameOwner &CI) {
241+
return hash_value(CI.Name);
242+
}
243+
244+
friend bool operator==(const ProtocolNameOwner &lhs,
245+
const ProtocolNameOwner &rhs) {
246+
return lhs.Name == rhs.Name;
247+
}
248+
249+
friend bool operator!=(const ProtocolNameOwner &lhs,
250+
const ProtocolNameOwner &rhs) {
251+
return !(lhs == rhs);
252+
}
253+
254+
friend void simple_display(llvm::raw_ostream &out,
255+
const ProtocolNameOwner &owner) {
256+
out << "Resolve " << owner.Name << " from ";
257+
simple_display(out, owner.DC);
258+
}
259+
};
260+
261+
/// Resolve a protocol name to the protocol decl pointer inside the ASTContext
262+
class ResolveProtocolNameRequest:
263+
public SimpleRequest<ResolveProtocolNameRequest,
264+
ProtocolDecl*(ProtocolNameOwner),
265+
CacheKind::Cached>
266+
{
267+
public:
268+
using SimpleRequest::SimpleRequest;
269+
270+
private:
271+
friend SimpleRequest;
272+
273+
// Evaluation.
274+
llvm::Expected<ProtocolDecl*> evaluate(Evaluator &evaluator,
275+
ProtocolNameOwner Input) const;
276+
277+
public:
278+
// Caching
279+
bool isCached() const { return true; }
280+
// Source location
281+
SourceLoc getNearestLoc() const { return SourceLoc(); };
282+
};
283+
232284
/// The zone number for the IDE.
233285
#define SWIFT_IDE_REQUESTS_TYPEID_ZONE 137
234286
#define SWIFT_TYPEID_ZONE SWIFT_IDE_REQUESTS_TYPEID_ZONE

include/swift/Sema/IDETypeChecking.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ namespace swift {
207207

208208
/// Resolve a list of mangled names to accessible protocol decls from
209209
/// the decl context.
210-
bool resolveProtocolNames(DeclContext *DC, ArrayRef<const char *> names,
211-
llvm::MapVector<ProtocolDecl*, StringRef> &result);
210+
ProtocolDecl *resolveProtocolName(DeclContext *dc, StringRef Name);
212211

213212
/// FIXME: All of the below goes away once CallExpr directly stores its
214213
/// arguments.

lib/IDE/ConformingMethodList.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ConformingMethodListCallbacks : public CodeCompletionCallbacks {
3333
DeclContext *CurDeclContext = nullptr;
3434

3535
void getMatchingMethods(Type T,
36-
llvm::MapVector<ProtocolDecl*, StringRef> &expectedTypes,
36+
llvm::SmallPtrSetImpl<ProtocolDecl*> &expectedTypes,
3737
SmallVectorImpl<ValueDecl *> &result);
3838

3939
public:
@@ -91,8 +91,12 @@ void ConformingMethodListCallbacks::doneParsing() {
9191
if (T->hasArchetype())
9292
T = T->mapTypeOutOfContext();
9393

94-
llvm::MapVector<ProtocolDecl*, StringRef> expectedProtocols;
95-
resolveProtocolNames(CurDeclContext, ExpectedTypeNames, expectedProtocols);
94+
llvm::SmallPtrSet<ProtocolDecl*, 8> expectedProtocols;
95+
for (auto Name: ExpectedTypeNames) {
96+
if (auto *PD = resolveProtocolName(CurDeclContext, Name)) {
97+
expectedProtocols.insert(PD);
98+
}
99+
}
96100

97101
// Collect the matching methods.
98102
ConformingMethodListResult result(CurDeclContext, T);
@@ -102,7 +106,7 @@ void ConformingMethodListCallbacks::doneParsing() {
102106
}
103107

104108
void ConformingMethodListCallbacks::getMatchingMethods(
105-
Type T, llvm::MapVector<ProtocolDecl*, StringRef> &expectedTypes,
109+
Type T, llvm::SmallPtrSetImpl<ProtocolDecl*> &expectedTypes,
106110
SmallVectorImpl<ValueDecl *> &result) {
107111
if (!T->mayHaveMembers())
108112
return;
@@ -114,7 +118,7 @@ void ConformingMethodListCallbacks::getMatchingMethods(
114118
Type T;
115119

116120
/// The list of expected types.
117-
llvm::MapVector<ProtocolDecl*, StringRef> &ExpectedTypes;
121+
llvm::SmallPtrSetImpl<ProtocolDecl*> &ExpectedTypes;
118122

119123
/// Result sink to populate.
120124
SmallVectorImpl<ValueDecl *> &Result;
@@ -137,7 +141,7 @@ void ConformingMethodListCallbacks::getMatchingMethods(
137141

138142
// The return type conforms to any of the requested protocols.
139143
for (auto Proto : ExpectedTypes) {
140-
if (CurModule->conformsToProtocol(declTy, Proto.first))
144+
if (CurModule->conformsToProtocol(declTy, Proto))
141145
return true;
142146
}
143147

@@ -146,7 +150,7 @@ void ConformingMethodListCallbacks::getMatchingMethods(
146150

147151
public:
148152
LocalConsumer(DeclContext *DC, Type T,
149-
llvm::MapVector<ProtocolDecl*, StringRef> &expectedTypes,
153+
llvm::SmallPtrSetImpl<ProtocolDecl*> &expectedTypes,
150154
SmallVectorImpl<ValueDecl *> &result)
151155
: CurModule(DC->getParentModule()), T(T), ExpectedTypes(expectedTypes),
152156
Result(result) {}

lib/IDE/IDERequests.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "swift/AST/ASTPrinter.h"
1414
#include "swift/AST/Decl.h"
1515
#include "swift/AST/NameLookup.h"
16+
#include "swift/AST/ASTDemangler.h"
1617
#include "swift/Basic/SourceManager.h"
1718
#include "swift/Frontend/Frontend.h"
1819
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
@@ -1144,3 +1145,28 @@ CollectOverriddenDeclsRequest::evaluate(Evaluator &evaluator,
11441145

11451146
return copyToContext(VD->getASTContext(), llvm::makeArrayRef(results));
11461147
}
1148+
1149+
1150+
//----------------------------------------------------------------------------//
1151+
// ResolveProtocolNameRequest
1152+
//----------------------------------------------------------------------------//
1153+
llvm::Expected<ProtocolDecl*>
1154+
ResolveProtocolNameRequest::evaluate(Evaluator &evaluator,
1155+
ProtocolNameOwner Input) const {
1156+
auto &ctx = Input.DC->getASTContext();
1157+
auto name = Input.Name;
1158+
// First try to solve by usr
1159+
ProtocolDecl *pd = dyn_cast_or_null<ProtocolDecl>(Demangle::
1160+
getTypeDeclForUSR(ctx, name));
1161+
if (!pd) {
1162+
// Second try to solve by mangled symbol name
1163+
pd = dyn_cast_or_null<ProtocolDecl>(Demangle::getTypeDeclForMangling(ctx, name));
1164+
}
1165+
if (!pd) {
1166+
// Thirdly try to solve by mangled type name
1167+
if (auto ty = Demangle::getTypeForMangling(ctx, name)) {
1168+
pd = dyn_cast_or_null<ProtocolDecl>(ty->getAnyGeneric());
1169+
}
1170+
}
1171+
return pd;
1172+
}

lib/IDE/IDETypeChecking.cpp

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -699,33 +699,10 @@ class ExpressionTypeCollector: public SourceEntityWalker {
699699
}
700700
};
701701

702-
bool swift::resolveProtocolNames(DeclContext *DC,
703-
ArrayRef<const char *> names,
704-
llvm::MapVector<ProtocolDecl*, StringRef> &result) {
705-
assert(result.empty());
706-
auto &ctx = DC->getASTContext();
707-
for (auto name : names) {
708-
// First try to solve by usr
709-
ProtocolDecl *pd = dyn_cast_or_null<ProtocolDecl>(Demangle::
710-
getTypeDeclForUSR(ctx, name));
711-
if (!pd) {
712-
// Second try to solve by mangled symbol name
713-
pd = dyn_cast_or_null<ProtocolDecl>(Demangle::getTypeDeclForMangling(ctx, name));
714-
}
715-
if (!pd) {
716-
// Thirdly try to solve by mangled type name
717-
if (auto ty = Demangle::getTypeForMangling(ctx, name)) {
718-
pd = dyn_cast_or_null<ProtocolDecl>(ty->getAnyGeneric());
719-
}
720-
}
721-
if (pd) {
722-
result.insert({pd, name});
723-
}
724-
}
725-
if (names.size() == result.size())
726-
return false;
727-
// If we resolved none but the given names are not empty, return true for failure.
728-
return result.size() == 0;
702+
ProtocolDecl* swift::resolveProtocolName(DeclContext *dc, StringRef name) {
703+
return evaluateOrDefault(dc->getASTContext().evaluator,
704+
ResolveProtocolNameRequest(ProtocolNameOwner(dc, name)),
705+
nullptr);
729706
}
730707

731708
ArrayRef<ExpressionTypeInfo>
@@ -735,8 +712,13 @@ swift::collectExpressionType(SourceFile &SF,
735712
bool CanonicalType,
736713
llvm::raw_ostream &OS) {
737714
llvm::MapVector<ProtocolDecl*, StringRef> InterestedProtocols;
738-
if (resolveProtocolNames(&SF, ExpectedProtocols, InterestedProtocols))
739-
return {};
715+
for (auto Name: ExpectedProtocols) {
716+
if (auto *pd = resolveProtocolName(&SF, Name)) {
717+
InterestedProtocols.insert({pd, Name});
718+
} else {
719+
return {};
720+
}
721+
}
740722
ExpressionTypeCollector Walker(SF, InterestedProtocols, Scratch,
741723
CanonicalType, OS);
742724
Walker.walk(SF);

lib/SILOptimizer/Utils/CFG.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,13 @@ void swift::erasePhiArgument(SILBasicBlock *block, unsigned argIndex) {
140140
// Determine the set of predecessors in case any predecessor has
141141
// two edges to this block (e.g. a conditional branch where both
142142
// sides reach this block).
143-
SmallVector<SILBasicBlock *, 8> predBlocks(block->pred_begin(),
144-
block->pred_end());
145-
sortUnique(predBlocks);
143+
//
144+
// NOTE: This needs to be a SmallSetVector since we need both uniqueness /and/
145+
// insertion order. Otherwise non-determinism can result.
146+
SmallSetVector<SILBasicBlock *, 8> predBlocks;
147+
148+
for (auto *pred : block->getPredecessorBlocks())
149+
predBlocks.insert(pred);
146150

147151
for (auto *pred : predBlocks)
148152
deleteEdgeValue(pred->getTerminator(), block, argIndex);

tools/SourceKit/lib/SwiftLang/SwiftConformingMethodList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static bool swiftConformingMethodListImpl(
8181
// FIXME: error?
8282
return true;
8383
}
84-
registerIDETypeCheckRequestFunctions(CI.getASTContext().evaluator);
84+
registerIDERequestFunctions(CI.getASTContext().evaluator);
8585
CI.performSema();
8686

8787
return true;

0 commit comments

Comments
 (0)