Skip to content

Commit 6c3362f

Browse files
committed
Add precedencegroup and operator decls to SourceLookupCache
1 parent 1f904ca commit 6c3362f

File tree

1 file changed

+70
-5
lines changed

1 file changed

+70
-5
lines changed

lib/AST/Module.cpp

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ SourceFile::~SourceFile() = default;
136136
class swift::SourceLookupCache {
137137
/// A lookup map for value decls. When declarations are added they are added
138138
/// under all variants of the name they can be found under.
139-
class DeclMap {
140-
llvm::DenseMap<DeclName, TinyPtrVector<ValueDecl*>> Members;
139+
class ValueDeclMap {
140+
llvm::DenseMap<DeclName, TinyPtrVector<ValueDecl *>> Members;
141141

142142
public:
143143
void add(ValueDecl *VD) {
@@ -156,10 +156,15 @@ class swift::SourceLookupCache {
156156
}
157157
};
158158

159-
DeclMap TopLevelValues;
160-
DeclMap ClassMembers;
159+
ValueDeclMap TopLevelValues;
160+
ValueDeclMap ClassMembers;
161161
bool MemberCachePopulated = false;
162162

163+
template<typename T>
164+
using OperatorMap = llvm::DenseMap<Identifier, TinyPtrVector<T *>>;
165+
OperatorMap<OperatorDecl> Operators;
166+
OperatorMap<PrecedenceGroupDecl> PrecedenceGroups;
167+
163168
template<typename Range>
164169
void addToUnqualifiedLookupCache(Range decls, bool onlyOperators);
165170
template<typename Range>
@@ -175,7 +180,28 @@ class swift::SourceLookupCache {
175180

176181
void lookupValue(DeclName Name, NLKind LookupKind,
177182
SmallVectorImpl<ValueDecl*> &Result);
178-
183+
184+
/// Retrieves all the operator decls. The order of the results is not
185+
/// guaranteed to be meaningful.
186+
void getOperatorDecls(SmallVectorImpl<OperatorDecl *> &results);
187+
188+
/// Retrieves all the precedence groups. The order of the results is not
189+
/// guaranteed to be meaningful.
190+
void getPrecedenceGroups(SmallVectorImpl<PrecedenceGroupDecl *> &results);
191+
192+
/// Look up an operator declaration.
193+
///
194+
/// \param name The operator name ("+", ">>", etc.)
195+
/// \param fixity The fixity of the operator (infix, prefix or postfix).
196+
void lookupOperator(Identifier name, OperatorFixity fixity,
197+
TinyPtrVector<OperatorDecl *> &results);
198+
199+
/// Look up a precedence group.
200+
///
201+
/// \param name The operator name ("+", ">>", etc.)
202+
void lookupPrecedenceGroup(Identifier name,
203+
TinyPtrVector<PrecedenceGroupDecl *> &results);
204+
179205
void lookupVisibleDecls(AccessPathTy AccessPath,
180206
VisibleDeclConsumer &Consumer,
181207
NLKind LookupKind);
@@ -224,6 +250,12 @@ void SourceLookupCache::addToUnqualifiedLookupCache(Range decls,
224250
if (!ED->hasUnparsedMembers() || ED->maybeHasOperatorDeclarations())
225251
addToUnqualifiedLookupCache(ED->getMembers(), true);
226252
}
253+
254+
if (auto *OD = dyn_cast<OperatorDecl>(D))
255+
Operators[OD->getName()].push_back(OD);
256+
257+
if (auto *PG = dyn_cast<PrecedenceGroupDecl>(D))
258+
PrecedenceGroups[PG->getName()].push_back(PG);
227259
}
228260
}
229261

@@ -300,6 +332,39 @@ void SourceLookupCache::lookupValue(DeclName Name, NLKind LookupKind,
300332
Result.push_back(Elt);
301333
}
302334

335+
void SourceLookupCache::getPrecedenceGroups(
336+
SmallVectorImpl<PrecedenceGroupDecl *> &results) {
337+
for (auto &groups : PrecedenceGroups)
338+
results.append(groups.second.begin(), groups.second.end());
339+
}
340+
341+
void SourceLookupCache::getOperatorDecls(
342+
SmallVectorImpl<OperatorDecl *> &results) {
343+
for (auto &ops : Operators)
344+
results.append(ops.second.begin(), ops.second.end());
345+
}
346+
347+
void SourceLookupCache::lookupOperator(Identifier name, OperatorFixity fixity,
348+
TinyPtrVector<OperatorDecl *> &results) {
349+
auto ops = Operators.find(name);
350+
if (ops == Operators.end())
351+
return;
352+
353+
for (auto *op : ops->second)
354+
if (op->getFixity() == fixity)
355+
results.push_back(op);
356+
}
357+
358+
void SourceLookupCache::lookupPrecedenceGroup(
359+
Identifier name, TinyPtrVector<PrecedenceGroupDecl *> &results) {
360+
auto groups = PrecedenceGroups.find(name);
361+
if (groups == PrecedenceGroups.end())
362+
return;
363+
364+
for (auto *group : groups->second)
365+
results.push_back(group);
366+
}
367+
303368
void SourceLookupCache::lookupVisibleDecls(AccessPathTy AccessPath,
304369
VisibleDeclConsumer &Consumer,
305370
NLKind LookupKind) {

0 commit comments

Comments
 (0)