Skip to content

Commit 50adc64

Browse files
committed
AST: Add a 'sorted' option to ConformanceLookupTable::getAllProtocols()
1 parent 8f6a2df commit 50adc64

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3495,7 +3495,9 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
34953495
SmallVectorImpl<ProtocolConformance *> &conformances) const;
34963496

34973497
/// Retrieve all of the protocols that this nominal type conforms to.
3498-
SmallVector<ProtocolDecl *, 2> getAllProtocols() const;
3498+
///
3499+
/// \param sorted Whether to sort the protocols in canonical order.
3500+
SmallVector<ProtocolDecl *, 2> getAllProtocols(bool sorted = false) const;
34993501

35003502
/// Retrieve all of the protocol conformances for this nominal type.
35013503
SmallVector<ProtocolConformance *, 2> getAllConformances(

lib/AST/ConformanceLookupTable.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,8 +1061,8 @@ void ConformanceLookupTable::lookupConformances(
10611061
}
10621062

10631063
void ConformanceLookupTable::getAllProtocols(
1064-
NominalTypeDecl *nominal,
1065-
SmallVectorImpl<ProtocolDecl *> &scratch) {
1064+
NominalTypeDecl *nominal, SmallVectorImpl<ProtocolDecl *> &scratch,
1065+
bool sorted) {
10661066
// We need to expand all implied conformances to find the complete
10671067
// set of protocols to which this nominal type conforms.
10681068
updateLookupTable(nominal, ConformanceStage::ExpandedImplied);
@@ -1075,7 +1075,9 @@ void ConformanceLookupTable::getAllProtocols(
10751075
scratch.push_back(conformance.first);
10761076
}
10771077

1078-
// FIXME: sort the protocols in some canonical order?
1078+
if (sorted) {
1079+
llvm::array_pod_sort(scratch.begin(), scratch.end(), TypeDecl::compare);
1080+
}
10791081
}
10801082

10811083
int ConformanceLookupTable::compareProtocolConformances(

lib/AST/ConformanceLookupTable.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,9 +451,12 @@ class ConformanceLookupTable : public ASTAllocated<ConformanceLookupTable> {
451451
SmallVectorImpl<ConformanceDiagnostic> *diagnostics);
452452

453453
/// Retrieve the complete set of protocols to which this nominal
454-
/// type conforms.
454+
/// type conforms (if the set contains a protocol, the same is true for any
455+
/// inherited protocols).
456+
///
457+
/// \param sorted Whether to sort the protocols in canonical order.
455458
void getAllProtocols(NominalTypeDecl *nominal,
456-
SmallVectorImpl<ProtocolDecl *> &scratch);
459+
SmallVectorImpl<ProtocolDecl *> &scratch, bool sorted);
457460

458461
/// Retrieve the complete set of protocol conformances for this
459462
/// nominal type.

lib/AST/ProtocolConformance.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,11 +1316,12 @@ bool NominalTypeDecl::lookupConformance(
13161316
conformances);
13171317
}
13181318

1319-
SmallVector<ProtocolDecl *, 2> NominalTypeDecl::getAllProtocols() const {
1319+
SmallVector<ProtocolDecl *, 2>
1320+
NominalTypeDecl::getAllProtocols(bool sorted) const {
13201321
prepareConformanceTable();
13211322
SmallVector<ProtocolDecl *, 2> result;
1322-
ConformanceTable->getAllProtocols(const_cast<NominalTypeDecl *>(this),
1323-
result);
1323+
ConformanceTable->getAllProtocols(const_cast<NominalTypeDecl *>(this), result,
1324+
sorted);
13241325
return result;
13251326
}
13261327

0 commit comments

Comments
 (0)