1 //===--- FindTarget.cpp - What does an AST node refer to? -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "FindTarget.h"
10 #include "AST.h"
11 #include "Logger.h"
12 #include "clang/AST/ASTTypeTraits.h"
13 #include "clang/AST/Decl.h"
14 #include "clang/AST/DeclCXX.h"
15 #include "clang/AST/DeclTemplate.h"
16 #include "clang/AST/DeclVisitor.h"
17 #include "clang/AST/DeclarationName.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/ExprCXX.h"
20 #include "clang/AST/ExprObjC.h"
21 #include "clang/AST/NestedNameSpecifier.h"
22 #include "clang/AST/PrettyPrinter.h"
23 #include "clang/AST/RecursiveASTVisitor.h"
24 #include "clang/AST/StmtVisitor.h"
25 #include "clang/AST/TemplateBase.h"
26 #include "clang/AST/Type.h"
27 #include "clang/AST/TypeLoc.h"
28 #include "clang/AST/TypeLocVisitor.h"
29 #include "clang/Basic/LangOptions.h"
30 #include "clang/Basic/OperatorKinds.h"
31 #include "clang/Basic/SourceLocation.h"
32 #include "llvm/ADT/STLExtras.h"
33 #include "llvm/ADT/SmallVector.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include <utility>
38 #include <vector>
39 
40 namespace clang {
41 namespace clangd {
42 namespace {
43 using ast_type_traits::DynTypedNode;
44 
45 LLVM_ATTRIBUTE_UNUSED std::string
46 nodeToString(const ast_type_traits::DynTypedNode &N) {
47   std::string S = N.getNodeKind().asStringRef();
48   {
49     llvm::raw_string_ostream OS(S);
50     OS << ": ";
51     N.print(OS, PrintingPolicy(LangOptions()));
52   }
53   std::replace(S.begin(), S.end(), '\n', ' ');
54   return S;
55 }
56 
57 // Given a dependent type and a member name, heuristically resolve the
58 // name to one or more declarations.
59 // The current heuristic is simply to look up the name in the primary
60 // template. This is a heuristic because the template could potentially
61 // have specializations that declare different members.
62 // Multiple declarations could be returned if the name is overloaded
63 // (e.g. an overloaded method in the primary template).
64 // This heuristic will give the desired answer in many cases, e.g.
65 // for a call to vector<T>::size().
66 // The name to look up is provided in the form of a factory that takes
67 // an ASTContext, because an ASTContext may be needed to obtain the
68 // name (e.g. if it's an operator name), but the caller may not have
69 // access to an ASTContext.
70 std::vector<const NamedDecl *> getMembersReferencedViaDependentName(
71     const Type *T,
72     llvm::function_ref<DeclarationName(ASTContext &)> NameFactory,
73     bool IsNonstaticMember) {
74   if (!T)
75     return {};
76   if (auto *ICNT = T->getAs<InjectedClassNameType>()) {
77     T = ICNT->getInjectedSpecializationType().getTypePtrOrNull();
78   }
79   auto *TST = T->getAs<TemplateSpecializationType>();
80   if (!TST)
81     return {};
82   const ClassTemplateDecl *TD = dyn_cast_or_null<ClassTemplateDecl>(
83       TST->getTemplateName().getAsTemplateDecl());
84   if (!TD)
85     return {};
86   CXXRecordDecl *RD = TD->getTemplatedDecl();
87   if (!RD->hasDefinition())
88     return {};
89   RD = RD->getDefinition();
90   DeclarationName Name = NameFactory(RD->getASTContext());
91   return RD->lookupDependentName(Name, [=](const NamedDecl *D) {
92     return IsNonstaticMember ? D->isCXXInstanceMember()
93                              : !D->isCXXInstanceMember();
94   });
95 }
96 
97 // Given the type T of a dependent expression that appears of the LHS of a "->",
98 // heuristically find a corresponding pointee type in whose scope we could look
99 // up the name appearing on the RHS.
100 const Type *getPointeeType(const Type *T) {
101   if (!T)
102     return nullptr;
103 
104   if (T->isPointerType()) {
105     return T->getAs<PointerType>()->getPointeeType().getTypePtrOrNull();
106   }
107 
108   // Try to handle smart pointer types.
109 
110   // Look up operator-> in the primary template. If we find one, it's probably a
111   // smart pointer type.
112   auto ArrowOps = getMembersReferencedViaDependentName(
113       T,
114       [](ASTContext &Ctx) {
115         return Ctx.DeclarationNames.getCXXOperatorName(OO_Arrow);
116       },
117       /*IsNonStaticMember=*/true);
118   if (ArrowOps.empty())
119     return nullptr;
120 
121   // Getting the return type of the found operator-> method decl isn't useful,
122   // because we discarded template arguments to perform lookup in the primary
123   // template scope, so the return type would just have the form U* where U is a
124   // template parameter type.
125   // Instead, just handle the common case where the smart pointer type has the
126   // form of SmartPtr<X, ...>, and assume X is the pointee type.
127   auto *TST = T->getAs<TemplateSpecializationType>();
128   if (!TST)
129     return nullptr;
130   if (TST->getNumArgs() == 0)
131     return nullptr;
132   const TemplateArgument &FirstArg = TST->getArg(0);
133   if (FirstArg.getKind() != TemplateArgument::Type)
134     return nullptr;
135   return FirstArg.getAsType().getTypePtrOrNull();
136 }
137 
138 const NamedDecl *getTemplatePattern(const NamedDecl *D) {
139   if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
140     return CRD->getTemplateInstantiationPattern();
141   } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
142     return FD->getTemplateInstantiationPattern();
143   } else if (auto *VD = dyn_cast<VarDecl>(D)) {
144     // Hmm: getTIP returns its arg if it's not an instantiation?!
145     VarDecl *T = VD->getTemplateInstantiationPattern();
146     return (T == D) ? nullptr : T;
147   } else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
148     return ED->getInstantiatedFromMemberEnum();
149   } else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D)) {
150     if (const auto *Parent = llvm::dyn_cast<NamedDecl>(D->getDeclContext()))
151       if (const DeclContext *ParentPat =
152               dyn_cast_or_null<DeclContext>(getTemplatePattern(Parent)))
153         for (const NamedDecl *BaseND : ParentPat->lookup(D->getDeclName()))
154           if (!BaseND->isImplicit() && BaseND->getKind() == D->getKind())
155             return BaseND;
156   } else if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
157     if (const auto *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) {
158       if (const EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
159         for (const NamedDecl *BaseECD : Pattern->lookup(ECD->getDeclName()))
160           return BaseECD;
161       }
162     }
163   }
164   return nullptr;
165 }
166 
167 // TargetFinder locates the entities that an AST node refers to.
168 //
169 // Typically this is (possibly) one declaration and (possibly) one type, but
170 // may be more:
171 //  - for ambiguous nodes like OverloadExpr
172 //  - if we want to include e.g. both typedefs and the underlying type
173 //
174 // This is organized as a set of mutually recursive helpers for particular node
175 // types, but for most nodes this is a short walk rather than a deep traversal.
176 //
177 // It's tempting to do e.g. typedef resolution as a second normalization step,
178 // after finding the 'primary' decl etc. But we do this monolithically instead
179 // because:
180 //  - normalization may require these traversals again (e.g. unwrapping a
181 //    typedef reveals a decltype which must be traversed)
182 //  - it doesn't simplify that much, e.g. the first stage must still be able
183 //    to yield multiple decls to handle OverloadExpr
184 //  - there are cases where it's required for correctness. e.g:
185 //      template<class X> using pvec = vector<x*>; pvec<int> x;
186 //    There's no Decl `pvec<int>`, we must choose `pvec<X>` or `vector<int*>`
187 //    and both are lossy. We must know upfront what the caller ultimately wants.
188 //
189 // FIXME: improve common dependent scope using name lookup in primary templates.
190 // e.g. template<typename T> int foo() { return std::vector<T>().size(); }
191 // formally size() is unresolved, but the primary template is a good guess.
192 // This affects:
193 //  - DependentTemplateSpecializationType,
194 //  - DependentNameType
195 //  - UnresolvedUsingValueDecl
196 //  - UnresolvedUsingTypenameDecl
197 struct TargetFinder {
198   using RelSet = DeclRelationSet;
199   using Rel = DeclRelation;
200 
201 private:
202   llvm::SmallDenseMap<const NamedDecl *,
203                       std::pair<RelSet, /*InsertionOrder*/ size_t>>
204       Decls;
205   RelSet Flags;
206 
207   template <typename T> void debug(T &Node, RelSet Flags) {
208     dlog("visit [{0}] {1}", Flags,
209          nodeToString(ast_type_traits::DynTypedNode::create(Node)));
210   }
211 
212   void report(const NamedDecl *D, RelSet Flags) {
213     dlog("--> [{0}] {1}", Flags,
214          nodeToString(ast_type_traits::DynTypedNode::create(*D)));
215     auto It = Decls.try_emplace(D, std::make_pair(Flags, Decls.size()));
216     // If already exists, update the flags.
217     if (!It.second)
218       It.first->second.first |= Flags;
219   }
220 
221 public:
222   llvm::SmallVector<std::pair<const NamedDecl *, RelSet>, 1> takeDecls() const {
223     using ValTy = std::pair<const NamedDecl *, RelSet>;
224     llvm::SmallVector<ValTy, 1> Result;
225     Result.resize(Decls.size());
226     for (const auto &Elem : Decls)
227       Result[Elem.second.second] = {Elem.first, Elem.second.first};
228     return Result;
229   }
230 
231   void add(const Decl *Dcl, RelSet Flags) {
232     const NamedDecl *D = llvm::dyn_cast<NamedDecl>(Dcl);
233     if (!D)
234       return;
235     debug(*D, Flags);
236     if (const UsingDirectiveDecl *UDD = llvm::dyn_cast<UsingDirectiveDecl>(D))
237       D = UDD->getNominatedNamespaceAsWritten();
238 
239     if (const TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D)) {
240       add(TND->getUnderlyingType(), Flags | Rel::Underlying);
241       Flags |= Rel::Alias; // continue with the alias.
242     } else if (const UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
243       for (const UsingShadowDecl *S : UD->shadows())
244         add(S->getUnderlyingDecl(), Flags | Rel::Underlying);
245       Flags |= Rel::Alias; // continue with the alias.
246     } else if (const auto *NAD = dyn_cast<NamespaceAliasDecl>(D)) {
247       add(NAD->getUnderlyingDecl(), Flags | Rel::Underlying);
248       Flags |= Rel::Alias; // continue with the alias
249     } else if (const UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) {
250       // Include the using decl, but don't traverse it. This may end up
251       // including *all* shadows, which we don't want.
252       report(USD->getUsingDecl(), Flags | Rel::Alias);
253       // Shadow decls are synthetic and not themselves interesting.
254       // Record the underlying decl instead, if allowed.
255       D = USD->getTargetDecl();
256       Flags |= Rel::Underlying; // continue with the underlying decl.
257     }
258 
259     if (const Decl *Pat = getTemplatePattern(D)) {
260       assert(Pat != D);
261       add(Pat, Flags | Rel::TemplatePattern);
262       // Now continue with the instantiation.
263       Flags |= Rel::TemplateInstantiation;
264     }
265 
266     report(D, Flags);
267   }
268 
269   void add(const Stmt *S, RelSet Flags) {
270     if (!S)
271       return;
272     debug(*S, Flags);
273     struct Visitor : public ConstStmtVisitor<Visitor> {
274       TargetFinder &Outer;
275       RelSet Flags;
276       Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
277 
278       void VisitCallExpr(const CallExpr *CE) {
279         Outer.add(CE->getCalleeDecl(), Flags);
280       }
281       void VisitDeclRefExpr(const DeclRefExpr *DRE) {
282         const Decl *D = DRE->getDecl();
283         // UsingShadowDecl allows us to record the UsingDecl.
284         // getFoundDecl() returns the wrong thing in other cases (templates).
285         if (auto *USD = llvm::dyn_cast<UsingShadowDecl>(DRE->getFoundDecl()))
286           D = USD;
287         Outer.add(D, Flags);
288       }
289       void VisitMemberExpr(const MemberExpr *ME) {
290         const Decl *D = ME->getMemberDecl();
291         if (auto *USD =
292                 llvm::dyn_cast<UsingShadowDecl>(ME->getFoundDecl().getDecl()))
293           D = USD;
294         Outer.add(D, Flags);
295       }
296       void VisitOverloadExpr(const OverloadExpr *OE) {
297         for (auto *D : OE->decls())
298           Outer.add(D, Flags);
299       }
300       void VisitSizeOfPackExpr(const SizeOfPackExpr *SE) {
301         Outer.add(SE->getPack(), Flags);
302       }
303       void VisitCXXConstructExpr(const CXXConstructExpr *CCE) {
304         Outer.add(CCE->getConstructor(), Flags);
305       }
306       void VisitDesignatedInitExpr(const DesignatedInitExpr *DIE) {
307         for (const DesignatedInitExpr::Designator &D :
308              llvm::reverse(DIE->designators()))
309           if (D.isFieldDesignator()) {
310             Outer.add(D.getField(), Flags);
311             // We don't know which designator was intended, we assume the outer.
312             break;
313           }
314       }
315       void
316       VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
317         const Type *BaseType = E->getBaseType().getTypePtrOrNull();
318         if (E->isArrow()) {
319           BaseType = getPointeeType(BaseType);
320         }
321         for (const NamedDecl *D : getMembersReferencedViaDependentName(
322                  BaseType, [E](ASTContext &) { return E->getMember(); },
323                  /*IsNonstaticMember=*/true)) {
324           Outer.add(D, Flags);
325         }
326       }
327       void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) {
328         for (const NamedDecl *D : getMembersReferencedViaDependentName(
329                  E->getQualifier()->getAsType(),
330                  [E](ASTContext &) { return E->getDeclName(); },
331                  /*IsNonstaticMember=*/false)) {
332           Outer.add(D, Flags);
333         }
334       }
335       void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *OIRE) {
336         Outer.add(OIRE->getDecl(), Flags);
337       }
338       void VisitObjCMessageExpr(const ObjCMessageExpr *OME) {
339         Outer.add(OME->getMethodDecl(), Flags);
340       }
341       void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *OPRE) {
342         if (OPRE->isExplicitProperty())
343           Outer.add(OPRE->getExplicitProperty(), Flags);
344         else {
345           if (OPRE->isMessagingGetter())
346             Outer.add(OPRE->getImplicitPropertyGetter(), Flags);
347           if (OPRE->isMessagingSetter())
348             Outer.add(OPRE->getImplicitPropertySetter(), Flags);
349         }
350       }
351       void VisitObjCProtocolExpr(const ObjCProtocolExpr *OPE) {
352         Outer.add(OPE->getProtocol(), Flags);
353       }
354       void VisitOpaqueValueExpr(const OpaqueValueExpr *OVE) {
355         Outer.add(OVE->getSourceExpr(), Flags);
356       }
357       void VisitPseudoObjectExpr(const PseudoObjectExpr *POE) {
358         Outer.add(POE->getSyntacticForm(), Flags);
359       }
360     };
361     Visitor(*this, Flags).Visit(S);
362   }
363 
364   void add(QualType T, RelSet Flags) {
365     if (T.isNull())
366       return;
367     debug(T, Flags);
368     struct Visitor : public TypeVisitor<Visitor> {
369       TargetFinder &Outer;
370       RelSet Flags;
371       Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
372 
373       void VisitTagType(const TagType *TT) {
374         Outer.add(TT->getAsTagDecl(), Flags);
375       }
376       void VisitDecltypeType(const DecltypeType *DTT) {
377         Outer.add(DTT->getUnderlyingType(), Flags | Rel::Underlying);
378       }
379       void VisitDeducedType(const DeducedType *DT) {
380         // FIXME: In practice this doesn't work: the AutoType you find inside
381         // TypeLoc never has a deduced type. https://llvm.org/PR42914
382         Outer.add(DT->getDeducedType(), Flags | Rel::Underlying);
383       }
384       void VisitDeducedTemplateSpecializationType(
385           const DeducedTemplateSpecializationType *DTST) {
386         // FIXME: This is a workaround for https://llvm.org/PR42914,
387         // which is causing DTST->getDeducedType() to be empty. We
388         // fall back to the template pattern and miss the instantiation
389         // even when it's known in principle. Once that bug is fixed,
390         // this method can be removed (the existing handling in
391         // VisitDeducedType() is sufficient).
392         if (auto *TD = DTST->getTemplateName().getAsTemplateDecl())
393           Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern);
394       }
395       void VisitTypedefType(const TypedefType *TT) {
396         Outer.add(TT->getDecl(), Flags);
397       }
398       void
399       VisitTemplateSpecializationType(const TemplateSpecializationType *TST) {
400         // Have to handle these case-by-case.
401 
402         // templated type aliases: there's no specialized/instantiated using
403         // decl to point to. So try to find a decl for the underlying type
404         // (after substitution), and failing that point to the (templated) using
405         // decl.
406         if (TST->isTypeAlias()) {
407           Outer.add(TST->getAliasedType(), Flags | Rel::Underlying);
408           // Don't *traverse* the alias, which would result in traversing the
409           // template of the underlying type.
410           Outer.report(
411               TST->getTemplateName().getAsTemplateDecl()->getTemplatedDecl(),
412               Flags | Rel::Alias | Rel::TemplatePattern);
413         }
414         // specializations of template template parameters aren't instantiated
415         // into decls, so they must refer to the parameter itself.
416         else if (const auto *Parm =
417                      llvm::dyn_cast_or_null<TemplateTemplateParmDecl>(
418                          TST->getTemplateName().getAsTemplateDecl()))
419           Outer.add(Parm, Flags);
420         // class template specializations have a (specialized) CXXRecordDecl.
421         else if (const CXXRecordDecl *RD = TST->getAsCXXRecordDecl())
422           Outer.add(RD, Flags); // add(Decl) will despecialize if needed.
423         else {
424           // fallback: the (un-specialized) declaration from primary template.
425           if (auto *TD = TST->getTemplateName().getAsTemplateDecl())
426             Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern);
427         }
428       }
429       void VisitTemplateTypeParmType(const TemplateTypeParmType *TTPT) {
430         Outer.add(TTPT->getDecl(), Flags);
431       }
432       void VisitObjCInterfaceType(const ObjCInterfaceType *OIT) {
433         Outer.add(OIT->getDecl(), Flags);
434       }
435       void VisitObjCObjectType(const ObjCObjectType *OOT) {
436         // FIXME: ObjCObjectTypeLoc has no children for the protocol list, so
437         // there is no node in id<Foo> that refers to ObjCProtocolDecl Foo.
438         if (OOT->isObjCQualifiedId() && OOT->getNumProtocols() == 1)
439           Outer.add(OOT->getProtocol(0), Flags);
440       }
441     };
442     Visitor(*this, Flags).Visit(T.getTypePtr());
443   }
444 
445   void add(const NestedNameSpecifier *NNS, RelSet Flags) {
446     if (!NNS)
447       return;
448     debug(*NNS, Flags);
449     switch (NNS->getKind()) {
450     case NestedNameSpecifier::Identifier:
451       return;
452     case NestedNameSpecifier::Namespace:
453       add(NNS->getAsNamespace(), Flags);
454       return;
455     case NestedNameSpecifier::NamespaceAlias:
456       add(NNS->getAsNamespaceAlias(), Flags);
457       return;
458     case NestedNameSpecifier::TypeSpec:
459     case NestedNameSpecifier::TypeSpecWithTemplate:
460       add(QualType(NNS->getAsType(), 0), Flags);
461       return;
462     case NestedNameSpecifier::Global:
463       // This should be TUDecl, but we can't get a pointer to it!
464       return;
465     case NestedNameSpecifier::Super:
466       add(NNS->getAsRecordDecl(), Flags);
467       return;
468     }
469     llvm_unreachable("unhandled NestedNameSpecifier::SpecifierKind");
470   }
471 
472   void add(const CXXCtorInitializer *CCI, RelSet Flags) {
473     if (!CCI)
474       return;
475     debug(*CCI, Flags);
476 
477     if (CCI->isAnyMemberInitializer())
478       add(CCI->getAnyMember(), Flags);
479     // Constructor calls contain a TypeLoc node, so we don't handle them here.
480   }
481 };
482 
483 } // namespace
484 
485 llvm::SmallVector<std::pair<const NamedDecl *, DeclRelationSet>, 1>
486 allTargetDecls(const ast_type_traits::DynTypedNode &N) {
487   dlog("allTargetDecls({0})", nodeToString(N));
488   TargetFinder Finder;
489   DeclRelationSet Flags;
490   if (const Decl *D = N.get<Decl>())
491     Finder.add(D, Flags);
492   else if (const Stmt *S = N.get<Stmt>())
493     Finder.add(S, Flags);
494   else if (const NestedNameSpecifierLoc *NNSL = N.get<NestedNameSpecifierLoc>())
495     Finder.add(NNSL->getNestedNameSpecifier(), Flags);
496   else if (const NestedNameSpecifier *NNS = N.get<NestedNameSpecifier>())
497     Finder.add(NNS, Flags);
498   else if (const TypeLoc *TL = N.get<TypeLoc>())
499     Finder.add(TL->getType(), Flags);
500   else if (const QualType *QT = N.get<QualType>())
501     Finder.add(*QT, Flags);
502   else if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>())
503     Finder.add(CCI, Flags);
504 
505   return Finder.takeDecls();
506 }
507 
508 llvm::SmallVector<const NamedDecl *, 1>
509 targetDecl(const ast_type_traits::DynTypedNode &N, DeclRelationSet Mask) {
510   llvm::SmallVector<const NamedDecl *, 1> Result;
511   for (const auto &Entry : allTargetDecls(N)) {
512     if (!(Entry.second & ~Mask))
513       Result.push_back(Entry.first);
514   }
515   return Result;
516 }
517 
518 llvm::SmallVector<const NamedDecl *, 1>
519 explicitReferenceTargets(DynTypedNode N, DeclRelationSet Mask) {
520   assert(!(Mask & (DeclRelation::TemplatePattern |
521                    DeclRelation::TemplateInstantiation)) &&
522          "explicitRefenceTargets handles templates on its own");
523   auto Decls = allTargetDecls(N);
524 
525   // We prefer to return template instantiation, but fallback to template
526   // pattern if instantiation is not available.
527   Mask |= DeclRelation::TemplatePattern | DeclRelation::TemplateInstantiation;
528 
529   llvm::SmallVector<const NamedDecl *, 1> TemplatePatterns;
530   llvm::SmallVector<const NamedDecl *, 1> Targets;
531   bool SeenTemplateInstantiations = false;
532   for (auto &D : Decls) {
533     if (D.second & ~Mask)
534       continue;
535     if (D.second & DeclRelation::TemplatePattern) {
536       TemplatePatterns.push_back(D.first);
537       continue;
538     }
539     if (D.second & DeclRelation::TemplateInstantiation)
540       SeenTemplateInstantiations = true;
541     Targets.push_back(D.first);
542   }
543   if (!SeenTemplateInstantiations)
544     Targets.insert(Targets.end(), TemplatePatterns.begin(),
545                    TemplatePatterns.end());
546   return Targets;
547 }
548 
549 namespace {
550 llvm::SmallVector<ReferenceLoc, 2> refInDecl(const Decl *D) {
551   struct Visitor : ConstDeclVisitor<Visitor> {
552     llvm::SmallVector<ReferenceLoc, 2> Refs;
553 
554     void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
555       // We want to keep it as non-declaration references, as the
556       // "using namespace" declaration doesn't have a name.
557       Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
558                                   D->getIdentLocation(),
559                                   /*IsDecl=*/false,
560                                   {D->getNominatedNamespaceAsWritten()}});
561     }
562 
563     void VisitUsingDecl(const UsingDecl *D) {
564       // "using ns::identifier;" is a non-declaration reference.
565       Refs.push_back(
566           ReferenceLoc{D->getQualifierLoc(), D->getLocation(), /*IsDecl=*/false,
567                        explicitReferenceTargets(DynTypedNode::create(*D),
568                                                 DeclRelation::Underlying)});
569     }
570 
571     void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
572       // For namespace alias, "namespace Foo = Target;", we add two references.
573       // Add a declaration reference for Foo.
574       VisitNamedDecl(D);
575       // Add a non-declaration reference for Target.
576       Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
577                                   D->getTargetNameLoc(),
578                                   /*IsDecl=*/false,
579                                   {D->getAliasedNamespace()}});
580     }
581 
582     void VisitNamedDecl(const NamedDecl *ND) {
583       // FIXME: decide on how to surface destructors when we need them.
584       if (llvm::isa<CXXDestructorDecl>(ND))
585         return;
586       // Filter anonymous decls, name location will point outside the name token
587       // and the clients are not prepared to handle that.
588       if (ND->getDeclName().isIdentifier() &&
589           !ND->getDeclName().getAsIdentifierInfo())
590         return;
591       Refs.push_back(ReferenceLoc{getQualifierLoc(*ND),
592                                   ND->getLocation(),
593                                   /*IsDecl=*/true,
594                                   {ND}});
595     }
596   };
597 
598   Visitor V;
599   V.Visit(D);
600   return V.Refs;
601 }
602 
603 llvm::SmallVector<ReferenceLoc, 2> refInExpr(const Expr *E) {
604   struct Visitor : ConstStmtVisitor<Visitor> {
605     // FIXME: handle more complicated cases, e.g. ObjC, designated initializers.
606     llvm::SmallVector<ReferenceLoc, 2> Refs;
607 
608     void VisitDeclRefExpr(const DeclRefExpr *E) {
609       Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
610                                   E->getNameInfo().getLoc(),
611                                   /*IsDecl=*/false,
612                                   {E->getFoundDecl()}});
613     }
614 
615     void VisitMemberExpr(const MemberExpr *E) {
616       Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
617                                   E->getMemberNameInfo().getLoc(),
618                                   /*IsDecl=*/false,
619                                   {E->getFoundDecl()}});
620     }
621 
622     void VisitOverloadExpr(const OverloadExpr *E) {
623       Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
624                                   E->getNameInfo().getLoc(),
625                                   /*IsDecl=*/false,
626                                   llvm::SmallVector<const NamedDecl *, 1>(
627                                       E->decls().begin(), E->decls().end())});
628     }
629 
630     void VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
631       Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
632                                   E->getPackLoc(),
633                                   /*IsDecl=*/false,
634                                   {E->getPack()}});
635     }
636   };
637 
638   Visitor V;
639   V.Visit(E);
640   return V.Refs;
641 }
642 
643 llvm::SmallVector<ReferenceLoc, 2> refInTypeLoc(TypeLoc L) {
644   struct Visitor : TypeLocVisitor<Visitor> {
645     llvm::Optional<ReferenceLoc> Ref;
646 
647     void VisitElaboratedTypeLoc(ElaboratedTypeLoc L) {
648       // We only know about qualifier, rest if filled by inner locations.
649       Visit(L.getNamedTypeLoc().getUnqualifiedLoc());
650       // Fill in the qualifier.
651       if (!Ref)
652         return;
653       assert(!Ref->Qualifier.hasQualifier() && "qualifier already set");
654       Ref->Qualifier = L.getQualifierLoc();
655     }
656 
657     void VisitTagTypeLoc(TagTypeLoc L) {
658       Ref = ReferenceLoc{NestedNameSpecifierLoc(),
659                          L.getNameLoc(),
660                          /*IsDecl=*/false,
661                          {L.getDecl()}};
662     }
663 
664     void VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc L) {
665       Ref = ReferenceLoc{NestedNameSpecifierLoc(),
666                          L.getNameLoc(),
667                          /*IsDecl=*/false,
668                          {L.getDecl()}};
669     }
670 
671     void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) {
672       // We must ensure template type aliases are included in results if they
673       // were written in the source code, e.g. in
674       //    template <class T> using valias = vector<T>;
675       //    ^valias<int> x;
676       // 'explicitReferenceTargets' will return:
677       //    1. valias with mask 'Alias'.
678       //    2. 'vector<int>' with mask 'Underlying'.
679       //  we want to return only #1 in this case.
680       Ref = ReferenceLoc{
681           NestedNameSpecifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false,
682           explicitReferenceTargets(DynTypedNode::create(L.getType()),
683                                    DeclRelation::Alias)};
684     }
685     void VisitDeducedTemplateSpecializationTypeLoc(
686         DeducedTemplateSpecializationTypeLoc L) {
687       Ref = ReferenceLoc{
688           NestedNameSpecifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
689           explicitReferenceTargets(DynTypedNode::create(L.getType()),
690                                    DeclRelation::Alias)};
691     }
692 
693     void VisitDependentTemplateSpecializationTypeLoc(
694         DependentTemplateSpecializationTypeLoc L) {
695       Ref = ReferenceLoc{
696           L.getQualifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false,
697           explicitReferenceTargets(DynTypedNode::create(L.getType()), {})};
698     }
699 
700     void VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
701       Ref = ReferenceLoc{
702           L.getQualifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
703           explicitReferenceTargets(DynTypedNode::create(L.getType()), {})};
704     }
705 
706     void VisitTypedefTypeLoc(TypedefTypeLoc L) {
707       Ref = ReferenceLoc{NestedNameSpecifierLoc(),
708                          L.getNameLoc(),
709                          /*IsDecl=*/false,
710                          {L.getTypedefNameDecl()}};
711     }
712   };
713 
714   Visitor V;
715   V.Visit(L.getUnqualifiedLoc());
716   if (!V.Ref)
717     return {};
718   return {*V.Ref};
719 }
720 
721 class ExplicitReferenceCollector
722     : public RecursiveASTVisitor<ExplicitReferenceCollector> {
723 public:
724   ExplicitReferenceCollector(llvm::function_ref<void(ReferenceLoc)> Out)
725       : Out(Out) {
726     assert(Out);
727   }
728 
729   bool VisitTypeLoc(TypeLoc TTL) {
730     if (TypeLocsToSkip.count(TTL.getBeginLoc().getRawEncoding()))
731       return true;
732     visitNode(DynTypedNode::create(TTL));
733     return true;
734   }
735 
736   bool TraverseElaboratedTypeLoc(ElaboratedTypeLoc L) {
737     // ElaboratedTypeLoc will reports information for its inner type loc.
738     // Otherwise we loose information about inner types loc's qualifier.
739     TypeLoc Inner = L.getNamedTypeLoc().getUnqualifiedLoc();
740     TypeLocsToSkip.insert(Inner.getBeginLoc().getRawEncoding());
741     return RecursiveASTVisitor::TraverseElaboratedTypeLoc(L);
742   }
743 
744   bool VisitExpr(Expr *E) {
745     visitNode(DynTypedNode::create(*E));
746     return true;
747   }
748 
749   // We re-define Traverse*, since there's no corresponding Visit*.
750   // TemplateArgumentLoc is the only way to get locations for references to
751   // template template parameters.
752   bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) {
753     switch (A.getArgument().getKind()) {
754     case TemplateArgument::Template:
755     case TemplateArgument::TemplateExpansion:
756       reportReference(ReferenceLoc{A.getTemplateQualifierLoc(),
757                                    A.getTemplateNameLoc(),
758                                    /*IsDecl=*/false,
759                                    {A.getArgument()
760                                         .getAsTemplateOrTemplatePattern()
761                                         .getAsTemplateDecl()}},
762                       DynTypedNode::create(A.getArgument()));
763       break;
764     case TemplateArgument::Declaration:
765       break; // FIXME: can this actually happen in TemplateArgumentLoc?
766     case TemplateArgument::Integral:
767     case TemplateArgument::Null:
768     case TemplateArgument::NullPtr:
769       break; // no references.
770     case TemplateArgument::Pack:
771     case TemplateArgument::Type:
772     case TemplateArgument::Expression:
773       break; // Handled by VisitType and VisitExpression.
774     };
775     return RecursiveASTVisitor::TraverseTemplateArgumentLoc(A);
776   }
777 
778   bool VisitDecl(Decl *D) {
779     visitNode(DynTypedNode::create(*D));
780     return true;
781   }
782 
783   // We have to use Traverse* because there is no corresponding Visit*.
784   bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc L) {
785     if (!L.getNestedNameSpecifier())
786       return true;
787     visitNode(DynTypedNode::create(L));
788     // Inner type is missing information about its qualifier, skip it.
789     if (auto TL = L.getTypeLoc())
790       TypeLocsToSkip.insert(TL.getBeginLoc().getRawEncoding());
791     return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L);
792   }
793 
794   bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
795     visitNode(DynTypedNode::create(*Init));
796     return RecursiveASTVisitor::TraverseConstructorInitializer(Init);
797   }
798 
799 private:
800   /// Obtain information about a reference directly defined in \p N. Does not
801   /// recurse into child nodes, e.g. do not expect references for constructor
802   /// initializers
803   ///
804   /// Any of the fields in the returned structure can be empty, but not all of
805   /// them, e.g.
806   ///   - for implicitly generated nodes (e.g. MemberExpr from range-based-for),
807   ///     source location information may be missing,
808   ///   - for dependent code, targets may be empty.
809   ///
810   /// (!) For the purposes of this function declarations are not considered to
811   ///     be references. However, declarations can have references inside them,
812   ///     e.g. 'namespace foo = std' references namespace 'std' and this
813   ///     function will return the corresponding reference.
814   llvm::SmallVector<ReferenceLoc, 2> explicitReference(DynTypedNode N) {
815     if (auto *D = N.get<Decl>())
816       return refInDecl(D);
817     if (auto *E = N.get<Expr>())
818       return refInExpr(E);
819     if (auto *NNSL = N.get<NestedNameSpecifierLoc>()) {
820       // (!) 'DeclRelation::Alias' ensures we do not loose namespace aliases.
821       return {ReferenceLoc{
822           NNSL->getPrefix(), NNSL->getLocalBeginLoc(), false,
823           explicitReferenceTargets(
824               DynTypedNode::create(*NNSL->getNestedNameSpecifier()),
825               DeclRelation::Alias)}};
826     }
827     if (const TypeLoc *TL = N.get<TypeLoc>())
828       return refInTypeLoc(*TL);
829     if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) {
830       // Other type initializers (e.g. base initializer) are handled by visiting
831       // the typeLoc.
832       if (CCI->isAnyMemberInitializer()) {
833         return {ReferenceLoc{NestedNameSpecifierLoc(),
834                              CCI->getMemberLocation(),
835                              /*IsDecl=*/false,
836                              {CCI->getAnyMember()}}};
837       }
838     }
839     // We do not have location information for other nodes (QualType, etc)
840     return {};
841   }
842 
843   void visitNode(DynTypedNode N) {
844     for (const auto &R : explicitReference(N))
845       reportReference(R, N);
846   }
847 
848   void reportReference(const ReferenceLoc &Ref, DynTypedNode N) {
849     // Our promise is to return only references from the source code. If we lack
850     // location information, skip these nodes.
851     // Normally this should not happen in practice, unless there are bugs in the
852     // traversals or users started the traversal at an implicit node.
853     if (Ref.NameLoc.isInvalid()) {
854       dlog("invalid location at node {0}", nodeToString(N));
855       return;
856     }
857     Out(Ref);
858   }
859 
860   llvm::function_ref<void(ReferenceLoc)> Out;
861   /// TypeLocs starting at these locations must be skipped, see
862   /// TraverseElaboratedTypeSpecifierLoc for details.
863   llvm::DenseSet</*SourceLocation*/ unsigned> TypeLocsToSkip;
864 };
865 } // namespace
866 
867 void findExplicitReferences(const Stmt *S,
868                             llvm::function_ref<void(ReferenceLoc)> Out) {
869   assert(S);
870   ExplicitReferenceCollector(Out).TraverseStmt(const_cast<Stmt *>(S));
871 }
872 void findExplicitReferences(const Decl *D,
873                             llvm::function_ref<void(ReferenceLoc)> Out) {
874   assert(D);
875   ExplicitReferenceCollector(Out).TraverseDecl(const_cast<Decl *>(D));
876 }
877 void findExplicitReferences(const ASTContext &AST,
878                             llvm::function_ref<void(ReferenceLoc)> Out) {
879   ExplicitReferenceCollector(Out).TraverseAST(const_cast<ASTContext &>(AST));
880 }
881 
882 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelation R) {
883   switch (R) {
884 #define REL_CASE(X)                                                            \
885   case DeclRelation::X:                                                        \
886     return OS << #X;
887     REL_CASE(Alias);
888     REL_CASE(Underlying);
889     REL_CASE(TemplateInstantiation);
890     REL_CASE(TemplatePattern);
891 #undef REL_CASE
892   }
893   llvm_unreachable("Unhandled DeclRelation enum");
894 }
895 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelationSet RS) {
896   const char *Sep = "";
897   for (unsigned I = 0; I < RS.S.size(); ++I) {
898     if (RS.S.test(I)) {
899       OS << Sep << static_cast<DeclRelation>(I);
900       Sep = "|";
901     }
902   }
903   return OS;
904 }
905 
906 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ReferenceLoc R) {
907   // note we cannot print R.NameLoc without a source manager.
908   OS << "targets = {";
909   bool First = true;
910   for (const NamedDecl *T : R.Targets) {
911     if (!First)
912       OS << ", ";
913     else
914       First = false;
915     OS << printQualifiedName(*T) << printTemplateSpecializationArgs(*T);
916   }
917   OS << "}";
918   if (R.Qualifier) {
919     OS << ", qualifier = '";
920     R.Qualifier.getNestedNameSpecifier()->print(OS,
921                                                 PrintingPolicy(LangOptions()));
922     OS << "'";
923   }
924   if (R.IsDecl)
925     OS << ", decl";
926   return OS;
927 }
928 
929 } // namespace clangd
930 } // namespace clang
931