Skip to content

Commit 6f21263

Browse files
committed
Add getOperatorDecls to FileUnit and ModuleDecl
Query the SourceLookupCache for the operator decls, and use ModuleDecl::getOperatorDecls for both frontend stats and to clean up some code completion logic. In addition, this commit switches getPrecedenceGroups over to querying SourceLookupCache.
1 parent 6c3362f commit 6f21263

File tree

13 files changed

+83
-70
lines changed

13 files changed

+83
-70
lines changed

include/swift/AST/FileUnit.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ class FileUnit : public DeclContext {
196196
SmallVectorImpl<Decl*> &Results,
197197
llvm::function_ref<bool(DeclAttributes)> matchAttributes) const;
198198

199+
/// Finds all operator decls in this file.
200+
///
201+
/// This does a simple local lookup, not recursively looking through imports.
202+
/// The order of the results is not guaranteed to be meaningful.
203+
virtual void
204+
getOperatorDecls(SmallVectorImpl<OperatorDecl *> &results) const {}
205+
199206
/// Finds all precedence group decls in this file.
200207
///
201208
/// This does a simple local lookup, not recursively looking through imports.

include/swift/AST/Module.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,12 @@ class ModuleDecl : public DeclContext, public TypeDecl {
620620
/// The order of the results is not guaranteed to be meaningful.
621621
void getLocalTypeDecls(SmallVectorImpl<TypeDecl*> &Results) const;
622622

623+
/// Finds all operator decls of this module.
624+
///
625+
/// This does a simple local lookup, not recursively looking through imports.
626+
/// The order of the results is not guaranteed to be meaningful.
627+
void getOperatorDecls(SmallVectorImpl<OperatorDecl *> &results) const;
628+
623629
/// Finds all precedence group decls of this module.
624630
///
625631
/// This does a simple local lookup, not recursively looking through imports.

include/swift/AST/SourceFile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,9 @@ class SourceFile final : public FileUnit {
448448
public:
449449
virtual void getTopLevelDecls(SmallVectorImpl<Decl*> &results) const override;
450450

451+
virtual void
452+
getOperatorDecls(SmallVectorImpl<OperatorDecl *> &results) const override;
453+
451454
virtual void
452455
getPrecedenceGroups(SmallVectorImpl<PrecedenceGroupDecl*> &results) const override;
453456

include/swift/Basic/Statistics.def

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,8 @@ FRONTEND_STATISTIC(AST, NumLocalTypeDecls)
130130
/// Number of Objective-C declarations in the AST context.
131131
FRONTEND_STATISTIC(AST, NumObjCMethods)
132132

133-
/// Number of infix, postfix, and prefix operators in the AST context.
134-
FRONTEND_STATISTIC(AST, NumInfixOperators)
135-
FRONTEND_STATISTIC(AST, NumPostfixOperators)
136-
FRONTEND_STATISTIC(AST, NumPrefixOperators)
133+
/// Number of operators in the AST context.
134+
FRONTEND_STATISTIC(AST, NumOperators)
137135

138136
/// Number of precedence groups in the AST context.
139137
FRONTEND_STATISTIC(AST, NumPrecedenceGroups)

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ class SerializedASTFile final : public LoadedFile {
384384
SmallVectorImpl<Decl*> &Results,
385385
llvm::function_ref<bool(DeclAttributes)> matchAttributes) const override;
386386

387+
virtual void
388+
getOperatorDecls(SmallVectorImpl<OperatorDecl *> &results) const override;
389+
387390
virtual void
388391
getPrecedenceGroups(SmallVectorImpl<PrecedenceGroupDecl*> &Results) const override;
389392

lib/AST/Module.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -745,18 +745,36 @@ void SourceFile::getTopLevelDecls(SmallVectorImpl<Decl*> &Results) const {
745745
Results.append(decls.begin(), decls.end());
746746
}
747747

748+
void ModuleDecl::getOperatorDecls(
749+
SmallVectorImpl<OperatorDecl *> &results) const {
750+
// For a parsed module, we can check the source cache on the module rather
751+
// than doing an O(N) search over the source files.
752+
if (isParsedModule(this)) {
753+
getSourceLookupCache().getOperatorDecls(results);
754+
return;
755+
}
756+
FORWARD(getOperatorDecls, (results));
757+
}
758+
759+
void SourceFile::getOperatorDecls(
760+
SmallVectorImpl<OperatorDecl*> &results) const {
761+
getCache().getOperatorDecls(results);
762+
}
763+
748764
void ModuleDecl::getPrecedenceGroups(
749-
SmallVectorImpl<PrecedenceGroupDecl*> &Results) const {
750-
FORWARD(getPrecedenceGroups, (Results));
765+
SmallVectorImpl<PrecedenceGroupDecl*> &results) const {
766+
// For a parsed module, we can check the source cache on the module rather
767+
// than doing an O(N) search over the source files.
768+
if (isParsedModule(this)) {
769+
getSourceLookupCache().getPrecedenceGroups(results);
770+
return;
771+
}
772+
FORWARD(getPrecedenceGroups, (results));
751773
}
752774

753775
void SourceFile::getPrecedenceGroups(
754-
SmallVectorImpl<PrecedenceGroupDecl*> &Results) const {
755-
for (auto pair : PrecedenceGroups) {
756-
if (pair.second.getPointer() && pair.second.getInt()) {
757-
Results.push_back(pair.second.getPointer());
758-
}
759-
}
776+
SmallVectorImpl<PrecedenceGroupDecl*> &results) const {
777+
getCache().getPrecedenceGroups(results);
760778
}
761779

762780
void SourceFile::getLocalTypeDecls(SmallVectorImpl<TypeDecl*> &Results) const {

lib/FrontendTool/FrontendTool.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,10 +655,14 @@ static void countStatsOfSourceFile(UnifiedStatsReporter &Stats,
655655
C.NumDecls += SF->getTopLevelDecls().size();
656656
C.NumLocalTypeDecls += SF->LocalTypeDecls.size();
657657
C.NumObjCMethods += SF->ObjCMethods.size();
658-
C.NumInfixOperators += SF->InfixOperators.size();
659-
C.NumPostfixOperators += SF->PostfixOperators.size();
660-
C.NumPrefixOperators += SF->PrefixOperators.size();
661-
C.NumPrecedenceGroups += SF->PrecedenceGroups.size();
658+
659+
SmallVector<OperatorDecl *, 2> operators;
660+
SF->getOperatorDecls(operators);
661+
C.NumOperators += operators.size();
662+
663+
SmallVector<PrecedenceGroupDecl *, 2> groups;
664+
SF->getPrecedenceGroups(groups);
665+
C.NumPrecedenceGroups += groups.size();
662666

663667
auto bufID = SF->getBufferID();
664668
if (bufID.hasValue()) {

lib/IDE/CodeCompletion.cpp

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3421,56 +3421,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
34213421
IncludeInstanceMembers);
34223422
}
34233423

3424-
template <typename T>
3425-
void collectOperatorsFromMap(SourceFile::OperatorMap<T> &map,
3426-
bool includePrivate,
3427-
std::vector<OperatorDecl *> &results) {
3428-
for (auto &pair : map) {
3429-
if (pair.second.getPointer() &&
3430-
(pair.second.getInt() || includePrivate)) {
3431-
results.push_back(pair.second.getPointer());
3432-
}
3433-
}
3434-
}
3435-
3436-
void collectOperatorsFrom(SourceFile *SF,
3437-
std::vector<OperatorDecl *> &results) {
3438-
bool includePrivate = CurrDeclContext->getParentSourceFile() == SF;
3439-
collectOperatorsFromMap(SF->PrefixOperators, includePrivate, results);
3440-
collectOperatorsFromMap(SF->PostfixOperators, includePrivate, results);
3441-
collectOperatorsFromMap(SF->InfixOperators, includePrivate, results);
3442-
}
3443-
3444-
void collectOperatorsFrom(LoadedFile *F,
3445-
std::vector<OperatorDecl *> &results) {
3446-
SmallVector<Decl *, 64> topLevelDecls;
3447-
F->getTopLevelDecls(topLevelDecls);
3448-
for (auto D : topLevelDecls) {
3449-
if (auto op = dyn_cast<OperatorDecl>(D))
3450-
results.push_back(op);
3451-
}
3452-
}
3453-
3454-
std::vector<OperatorDecl *> collectOperators() {
3455-
std::vector<OperatorDecl *> results;
3424+
void collectOperators(SmallVectorImpl<OperatorDecl *> &results) {
34563425
assert(CurrDeclContext);
3457-
for (auto import : namelookup::getAllImports(CurrDeclContext)) {
3458-
for (auto fileUnit : import.second->getFiles()) {
3459-
switch (fileUnit->getKind()) {
3460-
case FileUnitKind::Builtin:
3461-
case FileUnitKind::ClangModule:
3462-
case FileUnitKind::DWARFModule:
3463-
continue;
3464-
case FileUnitKind::Source:
3465-
collectOperatorsFrom(cast<SourceFile>(fileUnit), results);
3466-
break;
3467-
case FileUnitKind::SerializedAST:
3468-
collectOperatorsFrom(cast<LoadedFile>(fileUnit), results);
3469-
break;
3470-
}
3471-
}
3472-
}
3473-
return results;
3426+
for (auto import : namelookup::getAllImports(CurrDeclContext))
3427+
import.second->getOperatorDecls(results);
34743428
}
34753429

34763430
void addPostfixBang(Type resultType) {
@@ -3618,7 +3572,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
36183572

36193573
Expr *foldedExpr = typeCheckLeadingSequence(LHS, leadingSequence);
36203574

3621-
std::vector<OperatorDecl *> operators = collectOperators();
3575+
SmallVector<OperatorDecl *, 16> operators;
3576+
collectOperators(operators);
36223577
// FIXME: this always chooses the first operator with the given name.
36233578
llvm::DenseSet<Identifier> seenPostfixOperators;
36243579
llvm::DenseSet<Identifier> seenInfixOperators;

lib/Serialization/ModuleFile.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2694,6 +2694,17 @@ void ModuleFile::getTopLevelDecls(
26942694
}
26952695
}
26962696

2697+
void ModuleFile::getOperatorDecls(SmallVectorImpl<OperatorDecl *> &results) {
2698+
PrettyStackTraceModuleFile stackEntry(*this);
2699+
if (!OperatorDecls)
2700+
return;
2701+
2702+
for (auto entry : OperatorDecls->data()) {
2703+
for (auto item : entry)
2704+
results.push_back(cast<OperatorDecl>(getDecl(item.second)));
2705+
}
2706+
}
2707+
26972708
void ModuleFile::getPrecedenceGroups(
26982709
SmallVectorImpl<PrecedenceGroupDecl*> &results) {
26992710
PrettyStackTraceModuleFile stackEntry(*this);

lib/Serialization/ModuleFile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,9 @@ class ModuleFile
829829
SmallVectorImpl<Decl*> &Results,
830830
llvm::function_ref<bool(DeclAttributes)> matchAttributes = nullptr);
831831

832+
/// Adds all operators to the given vector.
833+
void getOperatorDecls(SmallVectorImpl<OperatorDecl *> &Results);
834+
832835
/// Adds all precedence groups to the given vector.
833836
void getPrecedenceGroups(SmallVectorImpl<PrecedenceGroupDecl*> &Results);
834837

0 commit comments

Comments
 (0)