blob: 75ea78c44a4ee72883a6d2f5b3e341d4c6ceed86 [file] [log] [blame]
Sam McCall489cc582019-09-03 11:35:50 +00001//===--- 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"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000013#include "clang/AST/Decl.h"
Sam McCall489cc582019-09-03 11:35:50 +000014#include "clang/AST/DeclCXX.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/DeclVisitor.h"
17#include "clang/AST/DeclarationName.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000018#include "clang/AST/Expr.h"
Ilya Biryukov27744572019-09-27 09:39:10 +000019#include "clang/AST/ExprCXX.h"
Sam McCall489cc582019-09-03 11:35:50 +000020#include "clang/AST/ExprObjC.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000021#include "clang/AST/NestedNameSpecifier.h"
22#include "clang/AST/PrettyPrinter.h"
23#include "clang/AST/RecursiveASTVisitor.h"
Sam McCall489cc582019-09-03 11:35:50 +000024#include "clang/AST/StmtVisitor.h"
Ilya Biryukov1d32da82019-10-01 10:02:23 +000025#include "clang/AST/TemplateBase.h"
Sam McCall489cc582019-09-03 11:35:50 +000026#include "clang/AST/Type.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000027#include "clang/AST/TypeLoc.h"
Sam McCall489cc582019-09-03 11:35:50 +000028#include "clang/AST/TypeLocVisitor.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000029#include "clang/Basic/LangOptions.h"
Sam McCall489cc582019-09-03 11:35:50 +000030#include "clang/Basic/SourceLocation.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000031#include "llvm/ADT/STLExtras.h"
32#include "llvm/ADT/SmallVector.h"
Sam McCall489cc582019-09-03 11:35:50 +000033#include "llvm/Support/Casting.h"
34#include "llvm/Support/Compiler.h"
35#include "llvm/Support/raw_ostream.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000036#include <utility>
Sam McCall489cc582019-09-03 11:35:50 +000037
38namespace clang {
39namespace clangd {
40namespace {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000041using ast_type_traits::DynTypedNode;
Sam McCall489cc582019-09-03 11:35:50 +000042
Sam McCall80195e72019-09-03 13:54:27 +000043LLVM_ATTRIBUTE_UNUSED std::string
Sam McCall489cc582019-09-03 11:35:50 +000044nodeToString(const ast_type_traits::DynTypedNode &N) {
45 std::string S = N.getNodeKind().asStringRef();
46 {
47 llvm::raw_string_ostream OS(S);
48 OS << ": ";
49 N.print(OS, PrintingPolicy(LangOptions()));
50 }
51 std::replace(S.begin(), S.end(), '\n', ' ');
52 return S;
53}
54
Nathan Ridgeecaa9362019-12-05 18:29:32 -050055// Given a dependent type and a member name, heuristically resolve the
56// name to one or more declarations.
57// The current heuristic is simply to look up the name in the primary
58// template. This is a heuristic because the template could potentially
59// have specializations that declare different members.
60// Multiple declarations could be returned if the name is overloaded
61// (e.g. an overloaded method in the primary template).
62// This heuristic will give the desired answer in many cases, e.g.
63// for a call to vector<T>::size().
64std::vector<const NamedDecl *>
65getMembersReferencedViaDependentName(const Type *T, const DeclarationName &Name,
66 bool IsNonstaticMember) {
67 if (!T)
68 return {};
69 if (auto *ICNT = T->getAs<InjectedClassNameType>()) {
70 T = ICNT->getInjectedSpecializationType().getTypePtrOrNull();
71 }
72 auto *TST = T->getAs<TemplateSpecializationType>();
73 if (!TST)
74 return {};
75 const ClassTemplateDecl *TD = dyn_cast_or_null<ClassTemplateDecl>(
76 TST->getTemplateName().getAsTemplateDecl());
77 if (!TD)
78 return {};
79 CXXRecordDecl *RD = TD->getTemplatedDecl();
80 if (!RD->hasDefinition())
81 return {};
82 RD = RD->getDefinition();
83 return RD->lookupDependentName(Name, [=](const NamedDecl *D) {
84 return IsNonstaticMember ? D->isCXXInstanceMember()
85 : !D->isCXXInstanceMember();
86 });
87}
88
Sam McCall489cc582019-09-03 11:35:50 +000089// TargetFinder locates the entities that an AST node refers to.
90//
91// Typically this is (possibly) one declaration and (possibly) one type, but
92// may be more:
93// - for ambiguous nodes like OverloadExpr
94// - if we want to include e.g. both typedefs and the underlying type
95//
96// This is organized as a set of mutually recursive helpers for particular node
97// types, but for most nodes this is a short walk rather than a deep traversal.
98//
99// It's tempting to do e.g. typedef resolution as a second normalization step,
100// after finding the 'primary' decl etc. But we do this monolithically instead
101// because:
102// - normalization may require these traversals again (e.g. unwrapping a
103// typedef reveals a decltype which must be traversed)
104// - it doesn't simplify that much, e.g. the first stage must still be able
105// to yield multiple decls to handle OverloadExpr
106// - there are cases where it's required for correctness. e.g:
107// template<class X> using pvec = vector<x*>; pvec<int> x;
108// There's no Decl `pvec<int>`, we must choose `pvec<X>` or `vector<int*>`
109// and both are lossy. We must know upfront what the caller ultimately wants.
110//
111// FIXME: improve common dependent scope using name lookup in primary templates.
112// e.g. template<typename T> int foo() { return std::vector<T>().size(); }
113// formally size() is unresolved, but the primary template is a good guess.
114// This affects:
115// - DependentTemplateSpecializationType,
Sam McCall489cc582019-09-03 11:35:50 +0000116// - DependentNameType
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500117// - UnresolvedUsingValueDecl
118// - UnresolvedUsingTypenameDecl
Sam McCall489cc582019-09-03 11:35:50 +0000119struct TargetFinder {
120 using RelSet = DeclRelationSet;
121 using Rel = DeclRelation;
Sam McCallf06f4392020-01-03 17:26:33 +0100122 llvm::SmallDenseMap<const NamedDecl *, RelSet> Decls;
Sam McCall489cc582019-09-03 11:35:50 +0000123 RelSet Flags;
124
Sam McCallf06f4392020-01-03 17:26:33 +0100125 static const NamedDecl *getTemplatePattern(const NamedDecl *D) {
Sam McCall489cc582019-09-03 11:35:50 +0000126 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
127 return CRD->getTemplateInstantiationPattern();
128 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
129 return FD->getTemplateInstantiationPattern();
130 } else if (auto *VD = dyn_cast<VarDecl>(D)) {
131 // Hmm: getTIP returns its arg if it's not an instantiation?!
132 VarDecl *T = VD->getTemplateInstantiationPattern();
133 return (T == D) ? nullptr : T;
134 } else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
135 return ED->getInstantiatedFromMemberEnum();
136 } else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D)) {
Sam McCallf06f4392020-01-03 17:26:33 +0100137 if (const auto *Parent = llvm::dyn_cast<NamedDecl>(D->getDeclContext()))
138 if (const DeclContext *ParentPat =
139 dyn_cast_or_null<DeclContext>(getTemplatePattern(Parent)))
140 for (const NamedDecl *BaseND : ParentPat->lookup(D->getDeclName()))
141 if (!BaseND->isImplicit() && BaseND->getKind() == D->getKind())
142 return BaseND;
Sam McCall489cc582019-09-03 11:35:50 +0000143 } else if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
144 if (const auto *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) {
145 if (const EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
146 for (const NamedDecl *BaseECD : Pattern->lookup(ECD->getDeclName()))
147 return BaseECD;
148 }
149 }
150 }
151 return nullptr;
152 }
153
154 template <typename T> void debug(T &Node, RelSet Flags) {
155 dlog("visit [{0}] {1}", Flags,
156 nodeToString(ast_type_traits::DynTypedNode::create(Node)));
157 }
158
Sam McCallf06f4392020-01-03 17:26:33 +0100159 void report(const NamedDecl *D, RelSet Flags) {
Sam McCall489cc582019-09-03 11:35:50 +0000160 dlog("--> [{0}] {1}", Flags,
161 nodeToString(ast_type_traits::DynTypedNode::create(*D)));
162 Decls[D] |= Flags;
163 }
164
165public:
Sam McCallf06f4392020-01-03 17:26:33 +0100166 void add(const Decl *Dcl, RelSet Flags) {
167 const NamedDecl *D = llvm::dyn_cast<NamedDecl>(Dcl);
Sam McCall489cc582019-09-03 11:35:50 +0000168 if (!D)
169 return;
170 debug(*D, Flags);
171 if (const UsingDirectiveDecl *UDD = llvm::dyn_cast<UsingDirectiveDecl>(D))
172 D = UDD->getNominatedNamespaceAsWritten();
173
174 if (const TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D)) {
175 add(TND->getUnderlyingType(), Flags | Rel::Underlying);
176 Flags |= Rel::Alias; // continue with the alias.
177 } else if (const UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
178 for (const UsingShadowDecl *S : UD->shadows())
179 add(S->getUnderlyingDecl(), Flags | Rel::Underlying);
180 Flags |= Rel::Alias; // continue with the alias.
181 } else if (const auto *NAD = dyn_cast<NamespaceAliasDecl>(D)) {
182 add(NAD->getUnderlyingDecl(), Flags | Rel::Underlying);
183 Flags |= Rel::Alias; // continue with the alias
184 } else if (const UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) {
185 // Include the using decl, but don't traverse it. This may end up
186 // including *all* shadows, which we don't want.
187 report(USD->getUsingDecl(), Flags | Rel::Alias);
188 // Shadow decls are synthetic and not themselves interesting.
189 // Record the underlying decl instead, if allowed.
190 D = USD->getTargetDecl();
191 Flags |= Rel::Underlying; // continue with the underlying decl.
192 }
193
194 if (const Decl *Pat = getTemplatePattern(D)) {
195 assert(Pat != D);
196 add(Pat, Flags | Rel::TemplatePattern);
197 // Now continue with the instantiation.
198 Flags |= Rel::TemplateInstantiation;
199 }
200
201 report(D, Flags);
202 }
203
204 void add(const Stmt *S, RelSet Flags) {
205 if (!S)
206 return;
207 debug(*S, Flags);
208 struct Visitor : public ConstStmtVisitor<Visitor> {
209 TargetFinder &Outer;
210 RelSet Flags;
211 Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
212
Haojian Wu939544a2019-11-27 16:22:16 +0100213 void VisitCallExpr(const CallExpr *CE) {
214 Outer.add(CE->getCalleeDecl(), Flags);
215 }
Sam McCall489cc582019-09-03 11:35:50 +0000216 void VisitDeclRefExpr(const DeclRefExpr *DRE) {
217 const Decl *D = DRE->getDecl();
218 // UsingShadowDecl allows us to record the UsingDecl.
219 // getFoundDecl() returns the wrong thing in other cases (templates).
220 if (auto *USD = llvm::dyn_cast<UsingShadowDecl>(DRE->getFoundDecl()))
221 D = USD;
222 Outer.add(D, Flags);
223 }
224 void VisitMemberExpr(const MemberExpr *ME) {
225 const Decl *D = ME->getMemberDecl();
226 if (auto *USD =
227 llvm::dyn_cast<UsingShadowDecl>(ME->getFoundDecl().getDecl()))
228 D = USD;
229 Outer.add(D, Flags);
230 }
Ilya Biryukova160a0b2019-10-01 07:27:55 +0000231 void VisitOverloadExpr(const OverloadExpr *OE) {
232 for (auto *D : OE->decls())
233 Outer.add(D, Flags);
234 }
Nathan Ridge1a4ee4c2019-12-05 14:27:23 -0500235 void VisitSizeOfPackExpr(const SizeOfPackExpr *SE) {
236 Outer.add(SE->getPack(), Flags);
237 }
Sam McCall489cc582019-09-03 11:35:50 +0000238 void VisitCXXConstructExpr(const CXXConstructExpr *CCE) {
239 Outer.add(CCE->getConstructor(), Flags);
240 }
241 void VisitDesignatedInitExpr(const DesignatedInitExpr *DIE) {
242 for (const DesignatedInitExpr::Designator &D :
243 llvm::reverse(DIE->designators()))
244 if (D.isFieldDesignator()) {
245 Outer.add(D.getField(), Flags);
246 // We don't know which designator was intended, we assume the outer.
247 break;
248 }
249 }
Nathan Ridgeecaa9362019-12-05 18:29:32 -0500250 void
251 VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
252 const Type *BaseType = E->getBaseType().getTypePtrOrNull();
253 if (E->isArrow()) {
254 // FIXME: Handle smart pointer types by looking up operator->
255 // in the primary template.
256 if (!BaseType || !BaseType->isPointerType()) {
257 return;
258 }
259 BaseType = BaseType->getAs<PointerType>()
260 ->getPointeeType()
261 .getTypePtrOrNull();
262 }
263 for (const NamedDecl *D :
264 getMembersReferencedViaDependentName(BaseType, E->getMember(),
265 /*IsNonstaticMember=*/true)) {
266 Outer.add(D, Flags);
267 }
268 }
269 void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) {
270 for (const NamedDecl *D : getMembersReferencedViaDependentName(
271 E->getQualifier()->getAsType(), E->getDeclName(),
272 /*IsNonstaticMember=*/false)) {
273 Outer.add(D, Flags);
274 }
275 }
Sam McCall489cc582019-09-03 11:35:50 +0000276 void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *OIRE) {
277 Outer.add(OIRE->getDecl(), Flags);
278 }
279 void VisitObjCMessageExpr(const ObjCMessageExpr *OME) {
280 Outer.add(OME->getMethodDecl(), Flags);
281 }
282 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *OPRE) {
283 if (OPRE->isExplicitProperty())
284 Outer.add(OPRE->getExplicitProperty(), Flags);
285 else {
286 if (OPRE->isMessagingGetter())
287 Outer.add(OPRE->getImplicitPropertyGetter(), Flags);
288 if (OPRE->isMessagingSetter())
289 Outer.add(OPRE->getImplicitPropertySetter(), Flags);
290 }
291 }
292 void VisitObjCProtocolExpr(const ObjCProtocolExpr *OPE) {
293 Outer.add(OPE->getProtocol(), Flags);
294 }
295 };
296 Visitor(*this, Flags).Visit(S);
297 }
298
299 void add(QualType T, RelSet Flags) {
300 if (T.isNull())
301 return;
302 debug(T, Flags);
303 struct Visitor : public TypeVisitor<Visitor> {
304 TargetFinder &Outer;
305 RelSet Flags;
306 Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
307
308 void VisitTagType(const TagType *TT) {
309 Outer.add(TT->getAsTagDecl(), Flags);
310 }
311 void VisitDecltypeType(const DecltypeType *DTT) {
312 Outer.add(DTT->getUnderlyingType(), Flags | Rel::Underlying);
313 }
314 void VisitDeducedType(const DeducedType *DT) {
315 // FIXME: In practice this doesn't work: the AutoType you find inside
316 // TypeLoc never has a deduced type. https://llvm.org/PR42914
317 Outer.add(DT->getDeducedType(), Flags | Rel::Underlying);
318 }
319 void VisitTypedefType(const TypedefType *TT) {
320 Outer.add(TT->getDecl(), Flags);
321 }
322 void
323 VisitTemplateSpecializationType(const TemplateSpecializationType *TST) {
324 // Have to handle these case-by-case.
325
326 // templated type aliases: there's no specialized/instantiated using
327 // decl to point to. So try to find a decl for the underlying type
328 // (after substitution), and failing that point to the (templated) using
329 // decl.
330 if (TST->isTypeAlias()) {
331 Outer.add(TST->getAliasedType(), Flags | Rel::Underlying);
332 // Don't *traverse* the alias, which would result in traversing the
333 // template of the underlying type.
334 Outer.report(
335 TST->getTemplateName().getAsTemplateDecl()->getTemplatedDecl(),
336 Flags | Rel::Alias | Rel::TemplatePattern);
337 }
338 // specializations of template template parameters aren't instantiated
339 // into decls, so they must refer to the parameter itself.
340 else if (const auto *Parm =
341 llvm::dyn_cast_or_null<TemplateTemplateParmDecl>(
342 TST->getTemplateName().getAsTemplateDecl()))
343 Outer.add(Parm, Flags);
344 // class template specializations have a (specialized) CXXRecordDecl.
345 else if (const CXXRecordDecl *RD = TST->getAsCXXRecordDecl())
346 Outer.add(RD, Flags); // add(Decl) will despecialize if needed.
347 else {
348 // fallback: the (un-specialized) declaration from primary template.
349 if (auto *TD = TST->getTemplateName().getAsTemplateDecl())
350 Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern);
351 }
352 }
353 void VisitTemplateTypeParmType(const TemplateTypeParmType *TTPT) {
354 Outer.add(TTPT->getDecl(), Flags);
355 }
356 void VisitObjCInterfaceType(const ObjCInterfaceType *OIT) {
357 Outer.add(OIT->getDecl(), Flags);
358 }
359 void VisitObjCObjectType(const ObjCObjectType *OOT) {
360 // FIXME: ObjCObjectTypeLoc has no children for the protocol list, so
361 // there is no node in id<Foo> that refers to ObjCProtocolDecl Foo.
362 if (OOT->isObjCQualifiedId() && OOT->getNumProtocols() == 1)
363 Outer.add(OOT->getProtocol(0), Flags);
364 }
365 };
366 Visitor(*this, Flags).Visit(T.getTypePtr());
367 }
368
369 void add(const NestedNameSpecifier *NNS, RelSet Flags) {
370 if (!NNS)
371 return;
372 debug(*NNS, Flags);
373 switch (NNS->getKind()) {
374 case NestedNameSpecifier::Identifier:
375 return;
376 case NestedNameSpecifier::Namespace:
377 add(NNS->getAsNamespace(), Flags);
378 return;
379 case NestedNameSpecifier::NamespaceAlias:
380 add(NNS->getAsNamespaceAlias(), Flags);
381 return;
382 case NestedNameSpecifier::TypeSpec:
383 case NestedNameSpecifier::TypeSpecWithTemplate:
384 add(QualType(NNS->getAsType(), 0), Flags);
385 return;
386 case NestedNameSpecifier::Global:
387 // This should be TUDecl, but we can't get a pointer to it!
388 return;
389 case NestedNameSpecifier::Super:
390 add(NNS->getAsRecordDecl(), Flags);
391 return;
392 }
393 llvm_unreachable("unhandled NestedNameSpecifier::SpecifierKind");
394 }
395
396 void add(const CXXCtorInitializer *CCI, RelSet Flags) {
397 if (!CCI)
398 return;
399 debug(*CCI, Flags);
400
401 if (CCI->isAnyMemberInitializer())
402 add(CCI->getAnyMember(), Flags);
403 // Constructor calls contain a TypeLoc node, so we don't handle them here.
404 }
405};
406
407} // namespace
408
Sam McCallf06f4392020-01-03 17:26:33 +0100409llvm::SmallVector<std::pair<const NamedDecl *, DeclRelationSet>, 1>
Sam McCall489cc582019-09-03 11:35:50 +0000410allTargetDecls(const ast_type_traits::DynTypedNode &N) {
411 dlog("allTargetDecls({0})", nodeToString(N));
412 TargetFinder Finder;
413 DeclRelationSet Flags;
414 if (const Decl *D = N.get<Decl>())
415 Finder.add(D, Flags);
416 else if (const Stmt *S = N.get<Stmt>())
417 Finder.add(S, Flags);
418 else if (const NestedNameSpecifierLoc *NNSL = N.get<NestedNameSpecifierLoc>())
419 Finder.add(NNSL->getNestedNameSpecifier(), Flags);
420 else if (const NestedNameSpecifier *NNS = N.get<NestedNameSpecifier>())
421 Finder.add(NNS, Flags);
422 else if (const TypeLoc *TL = N.get<TypeLoc>())
423 Finder.add(TL->getType(), Flags);
424 else if (const QualType *QT = N.get<QualType>())
425 Finder.add(*QT, Flags);
426 else if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>())
427 Finder.add(CCI, Flags);
428
429 return {Finder.Decls.begin(), Finder.Decls.end()};
430}
431
Sam McCallf06f4392020-01-03 17:26:33 +0100432llvm::SmallVector<const NamedDecl *, 1>
Sam McCall489cc582019-09-03 11:35:50 +0000433targetDecl(const ast_type_traits::DynTypedNode &N, DeclRelationSet Mask) {
Sam McCallf06f4392020-01-03 17:26:33 +0100434 llvm::SmallVector<const NamedDecl *, 1> Result;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000435 for (const auto &Entry : allTargetDecls(N)) {
Sam McCall489cc582019-09-03 11:35:50 +0000436 if (!(Entry.second & ~Mask))
437 Result.push_back(Entry.first);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000438 }
Sam McCall489cc582019-09-03 11:35:50 +0000439 return Result;
440}
441
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000442llvm::SmallVector<const NamedDecl *, 1>
Kadir Cetinkayaac3f9e42019-12-17 12:13:28 +0100443explicitReferenceTargets(DynTypedNode N, DeclRelationSet Mask) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000444 assert(!(Mask & (DeclRelation::TemplatePattern |
445 DeclRelation::TemplateInstantiation)) &&
446 "explicitRefenceTargets handles templates on its own");
447 auto Decls = allTargetDecls(N);
448
449 // We prefer to return template instantiation, but fallback to template
450 // pattern if instantiation is not available.
451 Mask |= DeclRelation::TemplatePattern | DeclRelation::TemplateInstantiation;
452
453 llvm::SmallVector<const NamedDecl *, 1> TemplatePatterns;
454 llvm::SmallVector<const NamedDecl *, 1> Targets;
455 bool SeenTemplateInstantiations = false;
456 for (auto &D : Decls) {
457 if (D.second & ~Mask)
458 continue;
459 if (D.second & DeclRelation::TemplatePattern) {
Sam McCallf06f4392020-01-03 17:26:33 +0100460 TemplatePatterns.push_back(D.first);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000461 continue;
462 }
463 if (D.second & DeclRelation::TemplateInstantiation)
464 SeenTemplateInstantiations = true;
Sam McCallf06f4392020-01-03 17:26:33 +0100465 Targets.push_back(D.first);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000466 }
467 if (!SeenTemplateInstantiations)
468 Targets.insert(Targets.end(), TemplatePatterns.begin(),
469 TemplatePatterns.end());
470 return Targets;
471}
472
Kadir Cetinkayaac3f9e42019-12-17 12:13:28 +0100473namespace {
Haojian Wu65f61c02019-10-18 12:07:19 +0000474llvm::SmallVector<ReferenceLoc, 2> refInDecl(const Decl *D) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000475 struct Visitor : ConstDeclVisitor<Visitor> {
Haojian Wu65f61c02019-10-18 12:07:19 +0000476 llvm::SmallVector<ReferenceLoc, 2> Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000477
478 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000479 // We want to keep it as non-declaration references, as the
480 // "using namespace" declaration doesn't have a name.
481 Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
482 D->getIdentLocation(),
483 /*IsDecl=*/false,
484 {D->getNominatedNamespaceAsWritten()}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000485 }
486
487 void VisitUsingDecl(const UsingDecl *D) {
Kazuaki Ishizakib7ecf1c2020-01-04 10:28:41 -0500488 // "using ns::identifier;" is a non-declaration reference.
Haojian Wu65f61c02019-10-18 12:07:19 +0000489 Refs.push_back(
490 ReferenceLoc{D->getQualifierLoc(), D->getLocation(), /*IsDecl=*/false,
491 explicitReferenceTargets(DynTypedNode::create(*D),
492 DeclRelation::Underlying)});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000493 }
494
495 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000496 // For namespace alias, "namespace Foo = Target;", we add two references.
497 // Add a declaration reference for Foo.
498 VisitNamedDecl(D);
499 // Add a non-declaration reference for Target.
500 Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
501 D->getTargetNameLoc(),
502 /*IsDecl=*/false,
503 {D->getAliasedNamespace()}});
504 }
505
506 void VisitNamedDecl(const NamedDecl *ND) {
507 // FIXME: decide on how to surface destructors when we need them.
508 if (llvm::isa<CXXDestructorDecl>(ND))
509 return;
Ilya Biryukov4c430a72019-10-28 14:41:06 +0100510 // Filter anonymous decls, name location will point outside the name token
511 // and the clients are not prepared to handle that.
512 if (ND->getDeclName().isIdentifier() &&
513 !ND->getDeclName().getAsIdentifierInfo())
514 return;
515 Refs.push_back(ReferenceLoc{getQualifierLoc(*ND),
516 ND->getLocation(),
517 /*IsDecl=*/true,
518 {ND}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000519 }
520 };
521
522 Visitor V;
523 V.Visit(D);
Haojian Wu65f61c02019-10-18 12:07:19 +0000524 return V.Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000525}
526
Haojian Wu65f61c02019-10-18 12:07:19 +0000527llvm::SmallVector<ReferenceLoc, 2> refInExpr(const Expr *E) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000528 struct Visitor : ConstStmtVisitor<Visitor> {
529 // FIXME: handle more complicated cases, e.g. ObjC, designated initializers.
Haojian Wu65f61c02019-10-18 12:07:19 +0000530 llvm::SmallVector<ReferenceLoc, 2> Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000531
532 void VisitDeclRefExpr(const DeclRefExpr *E) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000533 Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
534 E->getNameInfo().getLoc(),
535 /*IsDecl=*/false,
536 {E->getFoundDecl()}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000537 }
538
539 void VisitMemberExpr(const MemberExpr *E) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000540 Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
541 E->getMemberNameInfo().getLoc(),
542 /*IsDecl=*/false,
543 {E->getFoundDecl()}});
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000544 }
Ilya Biryukov27744572019-09-27 09:39:10 +0000545
546 void VisitOverloadExpr(const OverloadExpr *E) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000547 Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
548 E->getNameInfo().getLoc(),
549 /*IsDecl=*/false,
550 llvm::SmallVector<const NamedDecl *, 1>(
551 E->decls().begin(), E->decls().end())});
Ilya Biryukov27744572019-09-27 09:39:10 +0000552 }
Nathan Ridge1a4ee4c2019-12-05 14:27:23 -0500553
554 void VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
555 Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
556 E->getPackLoc(),
557 /*IsDecl=*/false,
558 {E->getPack()}});
559 }
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000560 };
561
562 Visitor V;
563 V.Visit(E);
Haojian Wu65f61c02019-10-18 12:07:19 +0000564 return V.Refs;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000565}
566
Haojian Wu65f61c02019-10-18 12:07:19 +0000567llvm::SmallVector<ReferenceLoc, 2> refInTypeLoc(TypeLoc L) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000568 struct Visitor : TypeLocVisitor<Visitor> {
569 llvm::Optional<ReferenceLoc> Ref;
570
571 void VisitElaboratedTypeLoc(ElaboratedTypeLoc L) {
572 // We only know about qualifier, rest if filled by inner locations.
573 Visit(L.getNamedTypeLoc().getUnqualifiedLoc());
574 // Fill in the qualifier.
575 if (!Ref)
576 return;
577 assert(!Ref->Qualifier.hasQualifier() && "qualifier already set");
578 Ref->Qualifier = L.getQualifierLoc();
579 }
580
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000581 void VisitTagTypeLoc(TagTypeLoc L) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000582 Ref = ReferenceLoc{NestedNameSpecifierLoc(),
583 L.getNameLoc(),
584 /*IsDecl=*/false,
585 {L.getDecl()}};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000586 }
587
Ilya Biryukovc3835092019-09-27 10:55:53 +0000588 void VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc L) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000589 Ref = ReferenceLoc{NestedNameSpecifierLoc(),
590 L.getNameLoc(),
591 /*IsDecl=*/false,
592 {L.getDecl()}};
Ilya Biryukovc3835092019-09-27 10:55:53 +0000593 }
594
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000595 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) {
Ilya Biryukov4ae23812019-09-27 17:55:46 +0000596 // We must ensure template type aliases are included in results if they
597 // were written in the source code, e.g. in
598 // template <class T> using valias = vector<T>;
599 // ^valias<int> x;
600 // 'explicitReferenceTargets' will return:
601 // 1. valias with mask 'Alias'.
602 // 2. 'vector<int>' with mask 'Underlying'.
603 // we want to return only #1 in this case.
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000604 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000605 NestedNameSpecifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false,
Ilya Biryukov4ae23812019-09-27 17:55:46 +0000606 explicitReferenceTargets(DynTypedNode::create(L.getType()),
607 DeclRelation::Alias)};
608 }
609 void VisitDeducedTemplateSpecializationTypeLoc(
610 DeducedTemplateSpecializationTypeLoc L) {
611 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000612 NestedNameSpecifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
Ilya Biryukov4ae23812019-09-27 17:55:46 +0000613 explicitReferenceTargets(DynTypedNode::create(L.getType()),
614 DeclRelation::Alias)};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000615 }
616
617 void VisitDependentTemplateSpecializationTypeLoc(
618 DependentTemplateSpecializationTypeLoc L) {
619 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000620 L.getQualifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false,
Sam McCallacc4ffb2020-01-02 17:59:10 +0100621 explicitReferenceTargets(DynTypedNode::create(L.getType()), {})};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000622 }
623
624 void VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
625 Ref = ReferenceLoc{
Haojian Wu65f61c02019-10-18 12:07:19 +0000626 L.getQualifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
Sam McCallacc4ffb2020-01-02 17:59:10 +0100627 explicitReferenceTargets(DynTypedNode::create(L.getType()), {})};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000628 }
629
630 void VisitTypedefTypeLoc(TypedefTypeLoc L) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000631 Ref = ReferenceLoc{NestedNameSpecifierLoc(),
632 L.getNameLoc(),
633 /*IsDecl=*/false,
634 {L.getTypedefNameDecl()}};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000635 }
636 };
637
638 Visitor V;
639 V.Visit(L.getUnqualifiedLoc());
Haojian Wu65f61c02019-10-18 12:07:19 +0000640 if (!V.Ref)
641 return {};
642 return {*V.Ref};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000643}
644
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100645class ExplicitReferenceCollector
646 : public RecursiveASTVisitor<ExplicitReferenceCollector> {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000647public:
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100648 ExplicitReferenceCollector(llvm::function_ref<void(ReferenceLoc)> Out)
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000649 : Out(Out) {
650 assert(Out);
651 }
652
653 bool VisitTypeLoc(TypeLoc TTL) {
654 if (TypeLocsToSkip.count(TTL.getBeginLoc().getRawEncoding()))
655 return true;
656 visitNode(DynTypedNode::create(TTL));
657 return true;
658 }
659
660 bool TraverseElaboratedTypeLoc(ElaboratedTypeLoc L) {
661 // ElaboratedTypeLoc will reports information for its inner type loc.
662 // Otherwise we loose information about inner types loc's qualifier.
663 TypeLoc Inner = L.getNamedTypeLoc().getUnqualifiedLoc();
664 TypeLocsToSkip.insert(Inner.getBeginLoc().getRawEncoding());
665 return RecursiveASTVisitor::TraverseElaboratedTypeLoc(L);
666 }
667
668 bool VisitExpr(Expr *E) {
669 visitNode(DynTypedNode::create(*E));
670 return true;
671 }
672
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000673 // We re-define Traverse*, since there's no corresponding Visit*.
674 // TemplateArgumentLoc is the only way to get locations for references to
675 // template template parameters.
676 bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) {
677 switch (A.getArgument().getKind()) {
678 case TemplateArgument::Template:
679 case TemplateArgument::TemplateExpansion:
680 reportReference(ReferenceLoc{A.getTemplateQualifierLoc(),
681 A.getTemplateNameLoc(),
Haojian Wu65f61c02019-10-18 12:07:19 +0000682 /*IsDecl=*/false,
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000683 {A.getArgument()
684 .getAsTemplateOrTemplatePattern()
685 .getAsTemplateDecl()}},
686 DynTypedNode::create(A.getArgument()));
687 break;
688 case TemplateArgument::Declaration:
689 break; // FIXME: can this actually happen in TemplateArgumentLoc?
690 case TemplateArgument::Integral:
691 case TemplateArgument::Null:
692 case TemplateArgument::NullPtr:
693 break; // no references.
694 case TemplateArgument::Pack:
695 case TemplateArgument::Type:
696 case TemplateArgument::Expression:
697 break; // Handled by VisitType and VisitExpression.
698 };
699 return RecursiveASTVisitor::TraverseTemplateArgumentLoc(A);
700 }
701
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000702 bool VisitDecl(Decl *D) {
703 visitNode(DynTypedNode::create(*D));
704 return true;
705 }
706
707 // We have to use Traverse* because there is no corresponding Visit*.
708 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc L) {
709 if (!L.getNestedNameSpecifier())
710 return true;
711 visitNode(DynTypedNode::create(L));
712 // Inner type is missing information about its qualifier, skip it.
713 if (auto TL = L.getTypeLoc())
714 TypeLocsToSkip.insert(TL.getBeginLoc().getRawEncoding());
715 return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L);
716 }
717
Haojian Wu13fc8992019-10-21 10:11:30 +0200718 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
719 visitNode(DynTypedNode::create(*Init));
720 return RecursiveASTVisitor::TraverseConstructorInitializer(Init);
721 }
722
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000723private:
724 /// Obtain information about a reference directly defined in \p N. Does not
725 /// recurse into child nodes, e.g. do not expect references for constructor
726 /// initializers
727 ///
728 /// Any of the fields in the returned structure can be empty, but not all of
729 /// them, e.g.
730 /// - for implicitly generated nodes (e.g. MemberExpr from range-based-for),
731 /// source location information may be missing,
732 /// - for dependent code, targets may be empty.
733 ///
734 /// (!) For the purposes of this function declarations are not considered to
735 /// be references. However, declarations can have references inside them,
736 /// e.g. 'namespace foo = std' references namespace 'std' and this
737 /// function will return the corresponding reference.
Haojian Wu65f61c02019-10-18 12:07:19 +0000738 llvm::SmallVector<ReferenceLoc, 2> explicitReference(DynTypedNode N) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000739 if (auto *D = N.get<Decl>())
740 return refInDecl(D);
741 if (auto *E = N.get<Expr>())
742 return refInExpr(E);
Ilya Biryukov733777a2019-10-31 11:58:57 +0100743 if (auto *NNSL = N.get<NestedNameSpecifierLoc>()) {
744 // (!) 'DeclRelation::Alias' ensures we do not loose namespace aliases.
745 return {ReferenceLoc{
746 NNSL->getPrefix(), NNSL->getLocalBeginLoc(), false,
747 explicitReferenceTargets(
748 DynTypedNode::create(*NNSL->getNestedNameSpecifier()),
749 DeclRelation::Alias)}};
750 }
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000751 if (const TypeLoc *TL = N.get<TypeLoc>())
752 return refInTypeLoc(*TL);
753 if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) {
Haojian Wu13fc8992019-10-21 10:11:30 +0200754 // Other type initializers (e.g. base initializer) are handled by visiting
755 // the typeLoc.
756 if (CCI->isAnyMemberInitializer()) {
757 return {ReferenceLoc{NestedNameSpecifierLoc(),
758 CCI->getMemberLocation(),
759 /*IsDecl=*/false,
760 {CCI->getAnyMember()}}};
761 }
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000762 }
763 // We do not have location information for other nodes (QualType, etc)
Haojian Wu65f61c02019-10-18 12:07:19 +0000764 return {};
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000765 }
766
767 void visitNode(DynTypedNode N) {
Haojian Wu65f61c02019-10-18 12:07:19 +0000768 for (const auto &R : explicitReference(N))
769 reportReference(R, N);
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000770 }
771
772 void reportReference(const ReferenceLoc &Ref, DynTypedNode N) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000773 // Our promise is to return only references from the source code. If we lack
774 // location information, skip these nodes.
775 // Normally this should not happen in practice, unless there are bugs in the
776 // traversals or users started the traversal at an implicit node.
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000777 if (Ref.NameLoc.isInvalid()) {
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000778 dlog("invalid location at node {0}", nodeToString(N));
779 return;
780 }
Ilya Biryukov1d32da82019-10-01 10:02:23 +0000781 Out(Ref);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000782 }
783
784 llvm::function_ref<void(ReferenceLoc)> Out;
785 /// TypeLocs starting at these locations must be skipped, see
786 /// TraverseElaboratedTypeSpecifierLoc for details.
787 llvm::DenseSet</*SourceLocation*/ unsigned> TypeLocsToSkip;
788};
789} // namespace
790
Kadir Cetinkaya007e4fe2019-09-25 15:44:26 +0000791void findExplicitReferences(const Stmt *S,
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000792 llvm::function_ref<void(ReferenceLoc)> Out) {
793 assert(S);
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100794 ExplicitReferenceCollector(Out).TraverseStmt(const_cast<Stmt *>(S));
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000795}
Kadir Cetinkaya007e4fe2019-09-25 15:44:26 +0000796void findExplicitReferences(const Decl *D,
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000797 llvm::function_ref<void(ReferenceLoc)> Out) {
798 assert(D);
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100799 ExplicitReferenceCollector(Out).TraverseDecl(const_cast<Decl *>(D));
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000800}
Ilya Biryukov87e0cb42019-11-05 19:06:12 +0100801void findExplicitReferences(const ASTContext &AST,
802 llvm::function_ref<void(ReferenceLoc)> Out) {
Kirill Bobyrev3b9715c2019-12-16 10:33:56 +0100803 ExplicitReferenceCollector(Out).TraverseAST(const_cast<ASTContext &>(AST));
Ilya Biryukov87e0cb42019-11-05 19:06:12 +0100804}
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000805
Sam McCall489cc582019-09-03 11:35:50 +0000806llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelation R) {
807 switch (R) {
808#define REL_CASE(X) \
809 case DeclRelation::X: \
810 return OS << #X;
811 REL_CASE(Alias);
812 REL_CASE(Underlying);
813 REL_CASE(TemplateInstantiation);
814 REL_CASE(TemplatePattern);
815#undef REL_CASE
Michael Liaode524032019-09-03 15:02:46 +0000816 }
Simon Pilgrim54b989c2019-09-03 13:05:13 +0000817 llvm_unreachable("Unhandled DeclRelation enum");
Sam McCall489cc582019-09-03 11:35:50 +0000818}
819llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelationSet RS) {
820 const char *Sep = "";
821 for (unsigned I = 0; I < RS.S.size(); ++I) {
822 if (RS.S.test(I)) {
823 OS << Sep << static_cast<DeclRelation>(I);
824 Sep = "|";
825 }
826 }
827 return OS;
828}
829
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000830llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ReferenceLoc R) {
831 // note we cannot print R.NameLoc without a source manager.
832 OS << "targets = {";
833 bool First = true;
834 for (const NamedDecl *T : R.Targets) {
835 if (!First)
836 OS << ", ";
837 else
838 First = false;
839 OS << printQualifiedName(*T) << printTemplateSpecializationArgs(*T);
840 }
841 OS << "}";
842 if (R.Qualifier) {
843 OS << ", qualifier = '";
844 R.Qualifier.getNestedNameSpecifier()->print(OS,
845 PrintingPolicy(LangOptions()));
846 OS << "'";
847 }
Haojian Wu65f61c02019-10-18 12:07:19 +0000848 if (R.IsDecl)
849 OS << ", decl";
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000850 return OS;
851}
852
Sam McCall489cc582019-09-03 11:35:50 +0000853} // namespace clangd
854} // namespace clang