LLDB mainline
ClangASTSource.h
Go to the documentation of this file.
1//===-- ClangASTSource.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_CLANGASTSOURCE_H
10#define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGASTSOURCE_H
11
12#include <set>
13
17#include "lldb/Target/Target.h"
18#include "clang/AST/ExternalASTSource.h"
19#include "clang/Basic/IdentifierTable.h"
20
21#include "llvm/ADT/SmallSet.h"
22
23namespace lldb_private {
24
25/// \class ClangASTSource ClangASTSource.h "lldb/Expression/ClangASTSource.h"
26/// Provider for named objects defined in the debug info for Clang
27///
28/// As Clang parses an expression, it may encounter names that are not defined
29/// inside the expression, including variables, functions, and types. Clang
30/// knows the name it is looking for, but nothing else. The ExternalSemaSource
31/// class provides Decls (VarDecl, FunDecl, TypeDecl) to Clang for these
32/// names, consulting the ClangExpressionDeclMap to do the actual lookups.
33class ClangASTSource : public clang::ExternalASTSource,
35public:
36 /// Constructor
37 ///
38 /// Initializes class variables.
39 ///
40 /// \param[in] target
41 /// A reference to the target containing debug information to use.
42 ///
43 /// \param[in] importer
44 /// The ClangASTImporter to use.
45 ClangASTSource(const lldb::TargetSP &target,
46 const std::shared_ptr<ClangASTImporter> &importer);
47
48 /// Destructor
49 ~ClangASTSource() override;
50
51 /// Interface stubs.
52 clang::Decl *GetExternalDecl(clang::GlobalDeclID) override { return nullptr; }
53 clang::Stmt *GetExternalDeclStmt(uint64_t) override { return nullptr; }
54 clang::Selector GetExternalSelector(uint32_t) override {
55 return clang::Selector();
56 }
57 uint32_t GetNumExternalSelectors() override { return 0; }
58 clang::CXXBaseSpecifier *
59 GetExternalCXXBaseSpecifiers(uint64_t Offset) override {
60 return nullptr;
61 }
62 void MaterializeVisibleDecls(const clang::DeclContext *DC) {}
63
64 void InstallASTContext(TypeSystemClang &ast_context);
65
66 //
67 // APIs for ExternalASTSource
68 //
69
70 /// Look up all Decls that match a particular name. Only handles
71 /// Identifiers and DeclContexts that are either NamespaceDecls or
72 /// TranslationUnitDecls. Calls SetExternalVisibleDeclsForName with the
73 /// result.
74 ///
75 /// The work for this function is done by
76 /// void FindExternalVisibleDecls (NameSearchContext &);
77 ///
78 /// \param[in] DC
79 /// The DeclContext to register the found Decls in.
80 ///
81 /// \param[in] Name
82 /// The name to find entries for.
83 ///
84 /// \return
85 /// Whatever SetExternalVisibleDeclsForName returns.
86 bool
87 FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
88 clang::DeclarationName Name,
89 const clang::DeclContext *OriginalDC) override;
90
91 /// Enumerate all Decls in a given lexical context.
92 ///
93 /// \param[in] DC
94 /// The DeclContext being searched.
95 ///
96 /// \param[in] IsKindWeWant
97 /// A callback function that returns true given the
98 /// DeclKinds of desired Decls, and false otherwise.
99 ///
100 /// \param[in] Decls
101 /// A vector that is filled in with matching Decls.
103 const clang::DeclContext *DC,
104 llvm::function_ref<bool(clang::Decl::Kind)> IsKindWeWant,
106
107 /// Specify the layout of the contents of a RecordDecl.
108 ///
109 /// \param[in] Record
110 /// The record (in the parser's AST context) that needs to be
111 /// laid out.
112 ///
113 /// \param[out] Size
114 /// The total size of the record in bits.
115 ///
116 /// \param[out] Alignment
117 /// The alignment of the record in bits.
118 ///
119 /// \param[in] FieldOffsets
120 /// A map that must be populated with pairs of the record's
121 /// fields (in the parser's AST context) and their offsets
122 /// (measured in bits).
123 ///
124 /// \param[in] BaseOffsets
125 /// A map that must be populated with pairs of the record's
126 /// C++ concrete base classes (in the parser's AST context,
127 /// and only if the record is a CXXRecordDecl and has base
128 /// classes) and their offsets (measured in bytes).
129 ///
130 /// \param[in] VirtualBaseOffsets
131 /// A map that must be populated with pairs of the record's
132 /// C++ virtual base classes (in the parser's AST context,
133 /// and only if the record is a CXXRecordDecl and has base
134 /// classes) and their offsets (measured in bytes).
135 ///
136 /// \return
137 /// True <=> the layout is valid.
138 bool layoutRecordType(
139 const clang::RecordDecl *Record, uint64_t &Size, uint64_t &Alignment,
140 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &FieldOffsets,
141 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
142 &BaseOffsets,
143 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
144 &VirtualBaseOffsets) override;
145
146 /// Complete a TagDecl.
147 ///
148 /// \param[in] Tag
149 /// The Decl to be completed in place.
150 void CompleteType(clang::TagDecl *Tag) override;
151
152 /// Complete an ObjCInterfaceDecl.
153 ///
154 /// \param[in] Class
155 /// The Decl to be completed in place.
156 void CompleteType(clang::ObjCInterfaceDecl *Class) override;
157
158 /// Called on entering a translation unit. Tells Clang by calling
159 /// setHasExternalVisibleStorage() and setHasExternalLexicalStorage() that
160 /// this object has something to say about undefined names.
161 ///
162 /// \param[in] Consumer
163 /// Unused.
164 void StartTranslationUnit(clang::ASTConsumer *Consumer) override;
165
166 //
167 // APIs for NamespaceMapCompleter
168 //
169
170 /// Look up the modules containing a given namespace and put the appropriate
171 /// entries in the namespace map.
172 ///
173 /// \param[in] namespace_map
174 /// The map to be completed.
175 ///
176 /// \param[in] name
177 /// The name of the namespace to be found.
178 ///
179 /// \param[in] parent_map
180 /// The map for the namespace's parent namespace, if there is
181 /// one.
184 ClangASTImporter::NamespaceMapSP &parent_map) const override;
185
186 //
187 // Helper APIs
188 //
189
190 clang::NamespaceDecl *AddNamespace(NameSearchContext &context);
191
192 /// The worker function for FindExternalVisibleDeclsByName.
193 ///
194 /// \param[in] context
195 /// The NameSearchContext to use when filing results.
196 virtual void FindExternalVisibleDecls(NameSearchContext &context);
197
198 clang::Sema *getSema();
199
200 void SetLookupsEnabled(bool lookups_enabled) {
201 m_lookups_enabled = lookups_enabled;
202 }
204
205 /// \class ClangASTSourceProxy ClangASTSource.h
206 /// "lldb/Expression/ClangASTSource.h" Proxy for ClangASTSource
207 ///
208 /// Clang AST contexts like to own their AST sources, so this is a state-
209 /// free proxy object.
210 class ClangASTSourceProxy : public clang::ExternalASTSource {
211 public:
213
215 const clang::DeclContext *DC, clang::DeclarationName Name,
216 const clang::DeclContext *OriginalDC) override {
217 return m_original.FindExternalVisibleDeclsByName(DC, Name, OriginalDC);
218 }
219
221 const clang::DeclContext *DC,
222 llvm::function_ref<bool(clang::Decl::Kind)> IsKindWeWant,
223 llvm::SmallVectorImpl<clang::Decl *> &Decls) override {
224 return m_original.FindExternalLexicalDecls(DC, IsKindWeWant, Decls);
225 }
226
227 void CompleteType(clang::TagDecl *Tag) override {
228 return m_original.CompleteType(Tag);
229 }
230
231 void CompleteType(clang::ObjCInterfaceDecl *Class) override {
232 return m_original.CompleteType(Class);
233 }
234
236 const clang::RecordDecl *Record, uint64_t &Size, uint64_t &Alignment,
237 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &FieldOffsets,
238 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
239 &BaseOffsets,
240 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
241 &VirtualBaseOffsets) override {
242 return m_original.layoutRecordType(Record, Size, Alignment, FieldOffsets,
243 BaseOffsets, VirtualBaseOffsets);
244 }
245
246 void StartTranslationUnit(clang::ASTConsumer *Consumer) override {
247 return m_original.StartTranslationUnit(Consumer);
248 }
249
250 private:
252 };
253
254 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> CreateProxy() {
255 return llvm::makeIntrusiveRefCnt<ClangASTSourceProxy>(*this);
256 }
257
258protected:
259 /// Look for the complete version of an Objective-C interface, and return it
260 /// if found.
261 ///
262 /// \param[in] interface_decl
263 /// An ObjCInterfaceDecl that may not be the complete one.
264 ///
265 /// \return
266 /// NULL if the complete interface couldn't be found;
267 /// the complete interface otherwise.
268 clang::ObjCInterfaceDecl *
269 GetCompleteObjCInterface(const clang::ObjCInterfaceDecl *interface_decl);
270
271 /// Find all entities matching a given name in a given module, using a
272 /// NameSearchContext to make Decls for them.
273 ///
274 /// \param[in] context
275 /// The NameSearchContext that can construct Decls for this name.
276 ///
277 /// \param[in] module
278 /// If non-NULL, the module to query.
279 ///
280 /// \param[in] namespace_decl
281 /// If valid and module is non-NULL, the parent namespace.
283 lldb::ModuleSP module,
284 CompilerDeclContext &namespace_decl);
285
286 /// Find all Objective-C methods matching a given selector.
287 ///
288 /// \param[in] context
289 /// The NameSearchContext that can construct Decls for this name.
290 /// Its m_decl_name contains the selector and its m_decl_context
291 /// is the containing object.
293
294 /// Find all Objective-C properties and ivars with a given name.
295 ///
296 /// \param[in] context
297 /// The NameSearchContext that can construct Decls for this name.
298 /// Its m_decl_name contains the name and its m_decl_context
299 /// is the containing object.
301
302 /// Performs lookup into a namespace.
303 ///
304 /// \param context
305 /// The NameSearchContext for a lookup inside a namespace.
307
308 /// A wrapper for TypeSystemClang::CopyType that sets a flag that
309 /// indicates that we should not respond to queries during import.
310 ///
311 /// \param[in] src_type
312 /// The source type.
313 ///
314 /// \return
315 /// The imported type.
317
318 std::shared_ptr<ClangModulesDeclVendor> GetClangModulesDeclVendor();
319
320public:
321 /// Returns true if a name should be ignored by name lookup.
322 ///
323 /// \param[in] name
324 /// The name to be considered.
325 ///
326 /// \param[in] ignore_all_dollar_names
327 /// True if $-names of all sorts should be ignored.
328 ///
329 /// \return
330 /// True if the name is one of a class of names that are ignored by
331 /// global lookup for performance reasons.
332 bool IgnoreName(const ConstString name, bool ignore_all_dollar_names);
333
334 /// Copies a single Decl into the parser's AST context.
335 ///
336 /// \param[in] src_decl
337 /// The Decl to copy.
338 ///
339 /// \return
340 /// A copy of the Decl in m_ast_context, or NULL if the copy failed.
341 clang::Decl *CopyDecl(clang::Decl *src_decl);
342
343 /// Determined the origin of a single Decl, if it can be found.
344 ///
345 /// \param[in] decl
346 /// The Decl whose origin is to be found.
347 ///
348 /// \return
349 /// True if lookup succeeded; false otherwise.
350 ClangASTImporter::DeclOrigin GetDeclOrigin(const clang::Decl *decl);
351
352 /// Returns the TypeSystem that uses this ClangASTSource instance as it's
353 /// ExternalASTSource.
355
356private:
358 NameSearchContext &context,
360
361protected:
363 NameSearchContext &context,
364 clang::ObjCInterfaceDecl *original_interface_decl, const char *log_info);
365
368
369 /// Fills the namespace map of the given NameSearchContext.
370 ///
371 /// \param context The NameSearchContext with the namespace map to fill.
372 /// \param module_sp The module to search for namespaces or a nullptr if
373 /// the current target should be searched.
374 /// \param namespace_decl The DeclContext in which to search for namespaces.
375 void FillNamespaceMap(NameSearchContext &context, lldb::ModuleSP module_sp,
376 const CompilerDeclContext &namespace_decl);
377
378 clang::TagDecl *FindCompleteType(const clang::TagDecl *decl);
379
380 friend struct NameSearchContext;
381
383
384 /// The target to use in finding variables and types.
386 /// The AST context requests are coming in for.
387 clang::ASTContext *m_ast_context;
388 /// The TypeSystemClang for m_ast_context.
390 /// The file manager paired with the AST context.
391 clang::FileManager *m_file_manager;
392 /// The target's AST importer.
393 std::shared_ptr<ClangASTImporter> m_ast_importer_sp;
394 std::set<const clang::Decl *> m_active_lexical_decls;
395 std::set<const char *> m_active_lookups;
396};
397
398} // namespace lldb_private
399
400#endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGASTSOURCE_H
std::shared_ptr< NamespaceMap > NamespaceMapSP
bool FindExternalVisibleDeclsByName(const clang::DeclContext *DC, clang::DeclarationName Name, const clang::DeclContext *OriginalDC) override
void CompleteType(clang::TagDecl *Tag) override
bool layoutRecordType(const clang::RecordDecl *Record, uint64_t &Size, uint64_t &Alignment, llvm::DenseMap< const clang::FieldDecl *, uint64_t > &FieldOffsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &BaseOffsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &VirtualBaseOffsets) override
void CompleteType(clang::ObjCInterfaceDecl *Class) override
void FindExternalLexicalDecls(const clang::DeclContext *DC, llvm::function_ref< bool(clang::Decl::Kind)> IsKindWeWant, llvm::SmallVectorImpl< clang::Decl * > &Decls) override
void StartTranslationUnit(clang::ASTConsumer *Consumer) override
std::set< const char * > m_active_lookups
void LookupInNamespace(NameSearchContext &context)
Performs lookup into a namespace.
uint32_t GetNumExternalSelectors() override
void SetLookupsEnabled(bool lookups_enabled)
clang::TagDecl * FindCompleteType(const clang::TagDecl *decl)
clang::Decl * GetExternalDecl(clang::GlobalDeclID) override
Interface stubs.
clang::ASTContext * m_ast_context
The AST context requests are coming in for.
ClangASTSource(const lldb::TargetSP &target, const std::shared_ptr< ClangASTImporter > &importer)
Constructor.
clang::Selector GetExternalSelector(uint32_t) override
std::set< const clang::Decl * > m_active_lexical_decls
void FindExternalLexicalDecls(const clang::DeclContext *DC, llvm::function_ref< bool(clang::Decl::Kind)> IsKindWeWant, llvm::SmallVectorImpl< clang::Decl * > &Decls) override
Enumerate all Decls in a given lexical context.
clang::CXXBaseSpecifier * GetExternalCXXBaseSpecifiers(uint64_t Offset) override
bool layoutRecordType(const clang::RecordDecl *Record, uint64_t &Size, uint64_t &Alignment, llvm::DenseMap< const clang::FieldDecl *, uint64_t > &FieldOffsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &BaseOffsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &VirtualBaseOffsets) override
Specify the layout of the contents of a RecordDecl.
clang::Decl * CopyDecl(clang::Decl *src_decl)
Copies a single Decl into the parser's AST context.
bool IgnoreName(const ConstString name, bool ignore_all_dollar_names)
Returns true if a name should be ignored by name lookup.
virtual void FindExternalVisibleDecls(NameSearchContext &context)
The worker function for FindExternalVisibleDeclsByName.
void FindObjCPropertyAndIvarDecls(NameSearchContext &context)
Find all Objective-C properties and ivars with a given name.
clang::NamespaceDecl * AddNamespace(NameSearchContext &context)
TypeSystemClang * GetTypeSystem() const
Returns the TypeSystem that uses this ClangASTSource instance as it's ExternalASTSource.
ClangASTImporter::DeclOrigin GetDeclOrigin(const clang::Decl *decl)
Determined the origin of a single Decl, if it can be found.
bool FindObjCPropertyAndIvarDeclsWithOrigin(NameSearchContext &context, DeclFromUser< const clang::ObjCInterfaceDecl > &origin_iface_decl)
bool FindObjCMethodDeclsWithOrigin(NameSearchContext &context, clang::ObjCInterfaceDecl *original_interface_decl, const char *log_info)
std::shared_ptr< ClangModulesDeclVendor > GetClangModulesDeclVendor()
void CompleteNamespaceMap(ClangASTImporter::NamespaceMapSP &namespace_map, ConstString name, ClangASTImporter::NamespaceMapSP &parent_map) const override
Look up the modules containing a given namespace and put the appropriate entries in the namespace map...
void FindDeclInObjCRuntime(NameSearchContext &context, ConstString name)
void CompleteType(clang::TagDecl *Tag) override
Complete a TagDecl.
~ClangASTSource() override
Destructor.
void FillNamespaceMap(NameSearchContext &context, lldb::ModuleSP module_sp, const CompilerDeclContext &namespace_decl)
Fills the namespace map of the given NameSearchContext.
void MaterializeVisibleDecls(const clang::DeclContext *DC)
CompilerType GuardedCopyType(const CompilerType &src_type)
A wrapper for TypeSystemClang::CopyType that sets a flag that indicates that we should not respond to...
void FindDeclInModules(NameSearchContext &context, ConstString name)
bool FindExternalVisibleDeclsByName(const clang::DeclContext *DC, clang::DeclarationName Name, const clang::DeclContext *OriginalDC) override
Look up all Decls that match a particular name.
llvm::IntrusiveRefCntPtr< clang::ExternalASTSource > CreateProxy()
std::shared_ptr< ClangASTImporter > m_ast_importer_sp
The target's AST importer.
void InstallASTContext(TypeSystemClang &ast_context)
TypeSystemClang * m_clang_ast_context
The TypeSystemClang for m_ast_context.
void FindObjCMethodDecls(NameSearchContext &context)
Find all Objective-C methods matching a given selector.
const lldb::TargetSP m_target
The target to use in finding variables and types.
clang::ObjCInterfaceDecl * GetCompleteObjCInterface(const clang::ObjCInterfaceDecl *interface_decl)
Look for the complete version of an Objective-C interface, and return it if found.
void StartTranslationUnit(clang::ASTConsumer *Consumer) override
Called on entering a translation unit.
clang::FileManager * m_file_manager
The file manager paired with the AST context.
clang::Stmt * GetExternalDeclStmt(uint64_t) override
Represents a generic declaration context in a program.
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
A TypeSystem implementation based on Clang.
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP
"lldb/Expression/ClangASTSource.h" Container for all objects relevant to a single name lookup