xref: /freebsd/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/NameSearchContext.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===-- NameSearchContext.h -------------------------------------*- 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 #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_NAME_SEARCH_CONTEXT_H
10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_NAME_SEARCH_CONTEXT_H
11 
12 #include "Plugins/ExpressionParser/Clang/ClangASTImporter.h"
13 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
14 #include "lldb/Symbol/CompilerType.h"
15 #include "llvm/ADT/SmallSet.h"
16 
17 namespace lldb_private {
18 
19 /// \class NameSearchContext ClangASTSource.h
20 /// "lldb/Expression/ClangASTSource.h" Container for all objects relevant to a
21 /// single name lookup
22 ///
23 /// LLDB needs to create Decls for entities it finds.  This class communicates
24 /// what name is being searched for and provides helper functions to construct
25 /// Decls given appropriate type information.
26 struct NameSearchContext {
27   /// The type system of the AST from which the lookup originated.
28   TypeSystemClang &m_clang_ts;
29   /// The list of declarations already constructed.
30   llvm::SmallVectorImpl<clang::NamedDecl *> &m_decls;
31   /// The mapping of all namespaces found for this request back to their
32   /// modules.
33   ClangASTImporter::NamespaceMapSP m_namespace_map;
34   /// The name being looked for.
35   const clang::DeclarationName m_decl_name;
36   /// The DeclContext to put declarations into.
37   const clang::DeclContext *m_decl_context;
38   /// All the types of functions that have been reported, so we don't
39   /// report conflicts.
40   llvm::SmallSet<CompilerType, 5> m_function_types;
41 
42   bool m_found_variable = false;
43   bool m_found_local_vars_nsp = false;
44   bool m_found_type = false;
45 
46   /// Constructor
47   ///
48   /// Initializes class variables.
49   ///
50   /// \param[in] clang_ts
51   ///     The TypeSystemClang from which the request originates.
52   ///
53   /// \param[in] decls
54   ///     A reference to a list into which new Decls will be placed.  This
55   ///     list is typically empty when the function is called.
56   ///
57   /// \param[in] name
58   ///     The name being searched for (always an Identifier).
59   ///
60   /// \param[in] dc
61   ///     The DeclContext to register Decls in.
NameSearchContextNameSearchContext62   NameSearchContext(TypeSystemClang &clang_ts,
63                     llvm::SmallVectorImpl<clang::NamedDecl *> &decls,
64                     clang::DeclarationName name, const clang::DeclContext *dc)
65       : m_clang_ts(clang_ts), m_decls(decls),
66         m_namespace_map(std::make_shared<ClangASTImporter::NamespaceMap>()),
67         m_decl_name(name), m_decl_context(dc) {
68     ;
69   }
70 
71   /// Create a VarDecl with the name being searched for and the provided type
72   /// and register it in the right places.
73   ///
74   /// \param[in] type
75   ///     The opaque QualType for the VarDecl being registered.
76   clang::NamedDecl *AddVarDecl(const CompilerType &type);
77 
78   /// Create a FunDecl with the name being searched for and the provided type
79   /// and register it in the right places.
80   ///
81   /// \param[in] type
82   ///     The opaque QualType for the FunDecl being registered.
83   ///
84   /// \param[in] extern_c
85   ///     If true, build an extern "C" linkage specification for this.
86   clang::NamedDecl *AddFunDecl(const CompilerType &type, bool extern_c = false);
87 
88   /// Create a FunDecl with the name being searched for and generic type (i.e.
89   /// intptr_t NAME_GOES_HERE(...)) and register it in the right places.
90   clang::NamedDecl *AddGenericFunDecl();
91 
92   /// Create a TypeDecl with the name being searched for and the provided type
93   /// and register it in the right places.
94   ///
95   /// \param[in] compiler_type
96   ///     The opaque QualType for the TypeDecl being registered.
97   clang::NamedDecl *AddTypeDecl(const CompilerType &compiler_type);
98 
99   /// Add Decls from the provided DeclContextLookupResult to the list of
100   /// results.
101   ///
102   /// \param[in] result
103   ///     The DeclContextLookupResult, usually returned as the result
104   ///     of querying a DeclContext.
105   void AddLookupResult(clang::DeclContextLookupResult result);
106 
107   /// Add a NamedDecl to the list of results.
108   ///
109   /// \param[in] decl
110   ///     The NamedDecl, usually returned as the result
111   ///     of querying a DeclContext.
112   void AddNamedDecl(clang::NamedDecl *decl);
113 
114 private:
GetASTContextNameSearchContext115   clang::ASTContext &GetASTContext() const {
116     return m_clang_ts.getASTContext();
117   }
118 };
119 
120 } // namespace lldb_private
121 
122 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_NAME_SEARCH_CONTEXT_H
123