e7a614c299cc0eedb4767eed28d2f7677d706c9a
[platform/upstream/llvm.git] / clang-tools-extra / clangd / AST.h
1 //===--- AST.h - Utility AST functions  -------------------------*- 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 // Various code that examines C++ source code using AST.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_AST_H_
14 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_AST_H_
15
16 #include "index/SymbolID.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Lex/MacroInfo.h"
20
21 namespace clang {
22 class SourceManager;
23 class Decl;
24
25 namespace clangd {
26
27 /// Returns true if the declaration is considered implementation detail based on
28 /// heuristics. For example, a declaration whose name is not explicitly spelled
29 /// in code is considered implementation detail.
30 bool isImplementationDetail(const Decl *D);
31
32 /// Find the identifier source location of the given D.
33 ///
34 /// The returned location is usually the spelling location where the name of the
35 /// decl occurs in the code.
36 SourceLocation findName(const clang::Decl *D);
37
38 /// Returns the qualified name of ND. The scope doesn't contain unwritten scopes
39 /// like inline namespaces.
40 std::string printQualifiedName(const NamedDecl &ND);
41
42 /// Returns the first enclosing namespace scope starting from \p DC.
43 std::string printNamespaceScope(const DeclContext &DC);
44
45 /// Prints unqualified name of the decl for the purpose of displaying it to the
46 /// user. Anonymous decls return names of the form "(anonymous {kind})", e.g.
47 /// "(anonymous struct)" or "(anonymous namespace)".
48 std::string printName(const ASTContext &Ctx, const NamedDecl &ND);
49
50 /// Prints template arguments of a decl as written in the source code, including
51 /// enclosing '<' and '>', e.g for a partial specialization like: template
52 /// <typename U> struct Foo<int, U> will return '<int, U>'. Returns an empty
53 /// string if decl is not a template specialization.
54 std::string printTemplateSpecializationArgs(const NamedDecl &ND);
55
56 /// Gets the symbol ID for a declaration, if possible.
57 llvm::Optional<SymbolID> getSymbolID(const Decl *D);
58
59 /// Gets the symbol ID for a macro, if possible.
60 /// Currently, this is an encoded USR of the macro, which incorporates macro
61 /// locations (e.g. file name, offset in file).
62 /// FIXME: the USR semantics might not be stable enough as the ID for index
63 /// macro (e.g. a change in definition offset can result in a different USR). We
64 /// could change these semantics in the future by reimplementing this funcure
65 /// (e.g. avoid USR for macros).
66 llvm::Optional<SymbolID> getSymbolID(const IdentifierInfo &II,
67                                      const MacroInfo *MI,
68                                      const SourceManager &SM);
69
70 /// Returns a QualType as string. The result doesn't contain unwritten scopes
71 /// like annoymous/inline namespace.
72 std::string printType(const QualType QT, const DeclContext & Context);
73
74 /// Try to shorten the OriginalName by removing namespaces from the left of
75 /// the string that are redundant in the CurrentNamespace. This way the type
76 /// idenfier become shorter and easier to read.
77 /// Limitation: It only handles the qualifier of the type itself, not that of
78 /// templates.
79 /// FIXME: change type of parameter CurrentNamespace to DeclContext ,
80 /// take in to account using directives etc
81 /// Example: shortenNamespace("ns1::MyClass<ns1::OtherClass>", "ns1")
82 ///    --> "MyClass<ns1::OtherClass>"
83 std::string shortenNamespace(const llvm::StringRef OriginalName,
84                              const llvm::StringRef CurrentNamespace);
85
86 /// Indicates if \p D is a template instantiation implicitly generated by the
87 /// compiler, e.g.
88 ///     template <class T> struct vector {};
89 ///     vector<int> v; // 'vector<int>' is an implicit instantiation
90 bool isImplicitTemplateInstantiation(const NamedDecl *D);
91 /// Indicates if \p D is an explicit template specialization, e.g.
92 ///   template <class T> struct vector {};
93 ///   template <> struct vector<bool> {}; // <-- explicit specialization
94 ///
95 /// Note that explicit instantiations are NOT explicit specializations, albeit
96 /// they look similar.
97 ///   template struct vector<bool>; // <-- explicit instantiation, NOT an
98 ///   explicit specialization.
99 bool isExplicitTemplateSpecialization(const NamedDecl *D);
100
101 } // namespace clangd
102 } // namespace clang
103
104 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_AST_H_