blob: 2f6c26ca691251276634eb58e21f3cd8b54ef303 [file] [log] [blame]
Sam McCall489cc582019-09-03 11:35:50 +00001//===--- FindTarget.h - What does an AST node refer to? ---------*- C++ -*-===//
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// Many clangd features are concerned with references in the AST:
10// - xrefs, go-to-definition, explicitly talk about references
11// - hover and code actions relate to things you "target" in the editor
12// - refactoring actions need to know about entities that are referenced
13// to determine whether/how the edit can be applied.
14//
15// Historically, we have used libIndex (IndexDataConsumer) to tie source
16// locations to referenced declarations. This file defines a more decoupled
17// approach based around AST nodes (DynTypedNode), and can be combined with
18// SelectionTree or other traversals.
19//
20//===----------------------------------------------------------------------===//
21
Haojian Wuc00627f2019-09-25 12:54:53 +000022#ifndef LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_FINDTARGET_H
23#define LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_FINDTARGET_H
24
Ilya Biryukov87e0cb42019-11-05 19:06:12 +010025#include "clang/AST/ASTContext.h"
Sam McCall489cc582019-09-03 11:35:50 +000026#include "clang/AST/ASTTypeTraits.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000027#include "clang/AST/NestedNameSpecifier.h"
28#include "clang/AST/Stmt.h"
Sam McCall489cc582019-09-03 11:35:50 +000029#include "clang/Basic/SourceLocation.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000030#include "llvm/ADT/Optional.h"
31#include "llvm/ADT/STLExtras.h"
Sam McCall489cc582019-09-03 11:35:50 +000032#include "llvm/ADT/SmallPtrSet.h"
Ilya Biryukovf96d2e12019-09-25 12:40:22 +000033#include "llvm/ADT/SmallVector.h"
34#include "llvm/Support/raw_ostream.h"
Sam McCall489cc582019-09-03 11:35:50 +000035
36#include <bitset>
37
38namespace clang {
39namespace clangd {
40/// Describes the link between an AST node and a Decl it refers to.
41enum class DeclRelation : unsigned;
42/// A bitfield of DeclRelations.
43class DeclRelationSet;
44
45/// targetDecl() finds the declaration referred to by an AST node.
46/// For example a RecordTypeLoc refers to the RecordDecl for the type.
47///
48/// In some cases there are multiple results, e.g. a dependent unresolved
49/// OverloadExpr may have several candidates. All will be returned:
50///
51/// void foo(int); <-- candidate
52/// void foo(double); <-- candidate
53/// template <typename T> callFoo() { foo(T()); }
54/// ^ OverloadExpr
55///
56/// In other cases, there may be choices about what "referred to" means.
57/// e.g. does naming a typedef refer to the underlying type?
58/// The results are marked with a set of DeclRelations, and can be filtered.
59///
60/// struct S{}; <-- candidate (underlying)
61/// using T = S{}; <-- candidate (alias)
62/// T x;
63/// ^ TypedefTypeLoc
64///
65/// Formally, we walk a graph starting at the provided node, and return the
66/// decls that were found. Certain edges in the graph have labels, and for each
67/// decl we return the set of labels seen on a path to the decl.
68/// For the previous example:
69///
70/// TypedefTypeLoc T
71/// |
72/// TypedefType T
73/// / \
74/// [underlying] [alias]
75/// / \
76/// RecordDecl S TypeAliasDecl T
77///
78/// FIXME: some AST nodes cannot be DynTypedNodes, these cannot be specified.
79llvm::SmallVector<const Decl *, 1>
80targetDecl(const ast_type_traits::DynTypedNode &, DeclRelationSet Mask);
81
Sam McCallacc4ffb2020-01-02 17:59:10 +010082/// Similar to targetDecl(), however instead of applying a filter, all possible
83/// decls are returned along with their DeclRelationSets.
84/// This is suitable for indexing, where everything is recorded and filtering
85/// is applied later.
86llvm::SmallVector<std::pair<const Decl *, DeclRelationSet>, 1>
87allTargetDecls(const ast_type_traits::DynTypedNode &);
88
89enum class DeclRelation : unsigned {
90 // Template options apply when the declaration is an instantiated template.
91 // e.g. [[vector<int>]] vec;
92
93 /// This is the template instantiation that was referred to.
94 /// e.g. template<> class vector<int> (the implicit specialization)
95 TemplateInstantiation,
96 /// This is the pattern the template specialization was instantiated from.
97 /// e.g. class vector<T> (the pattern within the primary template)
98 TemplatePattern,
99
100 // Alias options apply when the declaration is an alias.
101 // e.g. namespace clang { [[StringRef]] S; }
102
103 /// This declaration is an alias that was referred to.
104 /// e.g. using llvm::StringRef (the UsingDecl directly referenced).
105 Alias,
106 /// This is the underlying declaration for an alias, decltype etc.
107 /// e.g. class llvm::StringRef (the underlying declaration referenced).
108 Underlying,
109};
110llvm::raw_ostream &operator<<(llvm::raw_ostream &, DeclRelation);
111
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000112/// Information about a reference written in the source code, independent of the
113/// actual AST node that this reference lives in.
114/// Useful for tools that are source-aware, e.g. refactorings.
115struct ReferenceLoc {
116 /// Contains qualifier written in the code, if any, e.g. 'ns::' for 'ns::foo'.
117 NestedNameSpecifierLoc Qualifier;
118 /// Start location of the last name part, i.e. 'foo' in 'ns::foo<int>'.
119 SourceLocation NameLoc;
Haojian Wu65f61c02019-10-18 12:07:19 +0000120 /// True if the reference is a declaration or definition;
121 bool IsDecl = false;
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000122 // FIXME: add info about template arguments.
123 /// A list of targets referenced by this name. Normally this has a single
124 /// element, but multiple is also possible, e.g. in case of using declarations
125 /// or unresolved overloaded functions.
126 /// For dependent and unresolved references, Targets can also be empty.
127 llvm::SmallVector<const NamedDecl *, 1> Targets;
128};
129llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ReferenceLoc R);
130
131/// Recursively traverse \p S and report all references explicitly written in
132/// the code. The main use-case is refactorings that need to process all
133/// references in some subrange of the file and apply simple edits, e.g. add
134/// qualifiers.
135/// FIXME: currently this does not report references to overloaded operators.
136/// FIXME: extend to report location information about declaration names too.
Kadir Cetinkaya007e4fe2019-09-25 15:44:26 +0000137void findExplicitReferences(const Stmt *S,
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000138 llvm::function_ref<void(ReferenceLoc)> Out);
Kadir Cetinkaya007e4fe2019-09-25 15:44:26 +0000139void findExplicitReferences(const Decl *D,
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000140 llvm::function_ref<void(ReferenceLoc)> Out);
Ilya Biryukov87e0cb42019-11-05 19:06:12 +0100141void findExplicitReferences(const ASTContext &AST,
142 llvm::function_ref<void(ReferenceLoc)> Out);
Ilya Biryukovf96d2e12019-09-25 12:40:22 +0000143
Sam McCallacc4ffb2020-01-02 17:59:10 +0100144/// Find declarations explicitly referenced in the source code defined by \p N.
145/// For templates, will prefer to return a template instantiation whenever
146/// possible. However, can also return a template pattern if the specialization
147/// cannot be picked, e.g. in dependent code or when there is no corresponding
148/// Decl for a template instantitation, e.g. for templated using decls:
149/// template <class T> using Ptr = T*;
150/// Ptr<int> x;
151/// ^~~ there is no Decl for 'Ptr<int>', so we return the template pattern.
152/// \p Mask should not contain TemplatePattern or TemplateInstantiation.
153llvm::SmallVector<const NamedDecl *, 1>
154explicitReferenceTargets(ast_type_traits::DynTypedNode N,
155 DeclRelationSet Mask);
Sam McCall489cc582019-09-03 11:35:50 +0000156
Sam McCallacc4ffb2020-01-02 17:59:10 +0100157// Boring implementation details of bitfield.
Sam McCall489cc582019-09-03 11:35:50 +0000158
159class DeclRelationSet {
160 using Set = std::bitset<static_cast<unsigned>(DeclRelation::Underlying) + 1>;
161 Set S;
162 DeclRelationSet(Set S) : S(S) {}
163
164public:
165 DeclRelationSet() = default;
166 DeclRelationSet(DeclRelation R) { S.set(static_cast<unsigned>(R)); }
167
168 explicit operator bool() const { return S.any(); }
169 friend DeclRelationSet operator&(DeclRelationSet L, DeclRelationSet R) {
170 return L.S & R.S;
171 }
172 friend DeclRelationSet operator|(DeclRelationSet L, DeclRelationSet R) {
173 return L.S | R.S;
174 }
175 friend bool operator==(DeclRelationSet L, DeclRelationSet R) {
176 return L.S == R.S;
177 }
178 friend DeclRelationSet operator~(DeclRelationSet R) { return ~R.S; }
179 DeclRelationSet &operator|=(DeclRelationSet Other) {
180 S |= Other.S;
181 return *this;
182 }
183 DeclRelationSet &operator&=(DeclRelationSet Other) {
184 S &= Other.S;
185 return *this;
186 }
187 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &, DeclRelationSet);
188};
189// The above operators can't be looked up if both sides are enums.
190// over.match.oper.html#3.2
191inline DeclRelationSet operator|(DeclRelation L, DeclRelation R) {
192 return DeclRelationSet(L) | DeclRelationSet(R);
193}
194inline DeclRelationSet operator&(DeclRelation L, DeclRelation R) {
195 return DeclRelationSet(L) & DeclRelationSet(R);
196}
197inline DeclRelationSet operator~(DeclRelation R) { return ~DeclRelationSet(R); }
198llvm::raw_ostream &operator<<(llvm::raw_ostream &, DeclRelationSet);
199
200} // namespace clangd
201} // namespace clang
Haojian Wuc00627f2019-09-25 12:54:53 +0000202
203#endif // LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_FINDTARGET_H