1 //===-- ClangASTSource.h ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ClangASTSource_h_
11 #define liblldb_ClangASTSource_h_
15 #include "clang/Basic/IdentifierTable.h"
16 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
17 #include "lldb/Symbol/ClangASTImporter.h"
18 #include "lldb/Target/Target.h"
20 #include "llvm/ADT/SmallSet.h"
22 namespace lldb_private {
24 //----------------------------------------------------------------------
25 /// @class ClangASTSource ClangASTSource.h "lldb/Expression/ClangASTSource.h"
26 /// @brief Provider for named objects defined in the debug info for Clang
28 /// As Clang parses an expression, it may encounter names that are not
29 /// defined inside the expression, including variables, functions, and
30 /// types. Clang knows the name it is looking for, but nothing else.
31 /// The ExternalSemaSource class provides Decls (VarDecl, FunDecl, TypeDecl)
32 /// to Clang for these names, consulting the ClangExpressionDeclMap to do
33 /// the actual lookups.
34 //----------------------------------------------------------------------
35 class ClangASTSource :
36 public ClangExternalASTSourceCommon,
37 public ClangASTImporter::MapCompleter
40 //------------------------------------------------------------------
43 /// Initializes class variables.
45 /// @param[in] declMap
46 /// A reference to the LLDB object that handles entity lookup.
47 //------------------------------------------------------------------
48 ClangASTSource (const lldb::TargetSP &target) :
49 m_import_in_progress (false),
50 m_lookups_enabled (false),
55 m_ast_importer = m_target->GetClangASTImporter();
58 //------------------------------------------------------------------
60 //------------------------------------------------------------------
63 //------------------------------------------------------------------
65 //------------------------------------------------------------------
66 clang::Decl *GetExternalDecl (uint32_t) { return NULL; }
67 clang::Stmt *GetExternalDeclStmt (uint64_t) { return NULL; }
68 clang::Selector GetExternalSelector (uint32_t) { return clang::Selector(); }
69 uint32_t GetNumExternalSelectors () { return 0; }
70 clang::CXXBaseSpecifier *GetExternalCXXBaseSpecifiers (uint64_t Offset)
72 void MaterializeVisibleDecls (const clang::DeclContext *DC)
75 void InstallASTContext (clang::ASTContext *ast_context)
77 m_ast_context = ast_context;
78 m_ast_importer->InstallMapCompleter(ast_context, *this);
82 // APIs for ExternalASTSource
85 //------------------------------------------------------------------
86 /// Look up all Decls that match a particular name. Only handles
87 /// Identifiers and DeclContexts that are either NamespaceDecls or
88 /// TranslationUnitDecls. Calls SetExternalVisibleDeclsForName with
91 /// The work for this function is done by
92 /// void FindExternalVisibleDecls (NameSearchContext &);
95 /// The DeclContext to register the found Decls in.
98 /// The name to find entries for.
101 /// Whatever SetExternalVisibleDeclsForName returns.
102 //------------------------------------------------------------------
104 FindExternalVisibleDeclsByName (const clang::DeclContext *DC,
105 clang::DeclarationName Name);
107 //------------------------------------------------------------------
108 /// Enumerate all Decls in a given lexical context.
111 /// The DeclContext being searched.
113 /// @param[in] isKindWeWant
114 /// If non-NULL, a callback function that returns true given the
115 /// DeclKinds of desired Decls, and false otherwise.
118 /// A vector that is filled in with matching Decls.
119 //------------------------------------------------------------------
120 clang::ExternalLoadResult
121 FindExternalLexicalDecls (const clang::DeclContext *DC,
122 bool (*isKindWeWant)(clang::Decl::Kind),
123 llvm::SmallVectorImpl<clang::Decl*> &Decls);
125 //------------------------------------------------------------------
126 /// Specify the layout of the contents of a RecordDecl.
128 /// @param[in] Record
129 /// The record (in the parser's AST context) that needs to be
133 /// The total size of the record in bits.
135 /// @param[out] Alignment
136 /// The alignment of the record in bits.
138 /// @param[in] FieldOffsets
139 /// A map that must be populated with pairs of the record's
140 /// fields (in the parser's AST context) and their offsets
141 /// (measured in bits).
143 /// @param[in] BaseOffsets
144 /// A map that must be populated with pairs of the record's
145 /// C++ concrete base classes (in the parser's AST context,
146 /// and only if the record is a CXXRecordDecl and has base
147 /// classes) and their offsets (measured in bytes).
149 /// @param[in] VirtualBaseOffsets
150 /// A map that must be populated with pairs of the record's
151 /// C++ virtual base classes (in the parser's AST context,
152 /// and only if the record is a CXXRecordDecl and has base
153 /// classes) and their offsets (measured in bytes).
156 /// True <=> the layout is valid.
157 //-----------------------------------------------------------------
159 layoutRecordType(const clang::RecordDecl *Record,
162 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &FieldOffsets,
163 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &BaseOffsets,
164 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &VirtualBaseOffsets);
166 //------------------------------------------------------------------
167 /// Complete a TagDecl.
170 /// The Decl to be completed in place.
171 //------------------------------------------------------------------
173 CompleteType (clang::TagDecl *Tag);
175 //------------------------------------------------------------------
176 /// Complete an ObjCInterfaceDecl.
179 /// The Decl to be completed in place.
180 //------------------------------------------------------------------
182 CompleteType (clang::ObjCInterfaceDecl *Class);
184 //------------------------------------------------------------------
185 /// Called on entering a translation unit. Tells Clang by calling
186 /// setHasExternalVisibleStorage() and setHasExternalLexicalStorage()
187 /// that this object has something to say about undefined names.
189 /// @param[in] ASTConsumer
191 //------------------------------------------------------------------
192 void StartTranslationUnit (clang::ASTConsumer *Consumer);
195 // APIs for NamespaceMapCompleter
198 //------------------------------------------------------------------
199 /// Look up the modules containing a given namespace and put the
200 /// appropriate entries in the namespace map.
202 /// @param[in] namespace_map
203 /// The map to be completed.
206 /// The name of the namespace to be found.
208 /// @param[in] parent_map
209 /// The map for the namespace's parent namespace, if there is
211 //------------------------------------------------------------------
212 void CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
213 const ConstString &name,
214 ClangASTImporter::NamespaceMapSP &parent_map) const;
220 clang::NamespaceDecl *
221 AddNamespace (NameSearchContext &context,
222 ClangASTImporter::NamespaceMapSP &namespace_decls);
224 //------------------------------------------------------------------
225 /// The worker function for FindExternalVisibleDeclsByName.
227 /// @param[in] context
228 /// The NameSearchContext to use when filing results.
229 //------------------------------------------------------------------
230 virtual void FindExternalVisibleDecls (NameSearchContext &context);
232 void SetImportInProgress (bool import_in_progress) { m_import_in_progress = import_in_progress; }
233 bool GetImportInProgress () { return m_import_in_progress; }
235 void SetLookupsEnabled (bool lookups_enabled) { m_lookups_enabled = lookups_enabled; }
236 bool GetLookupsEnabled () { return m_lookups_enabled; }
238 //----------------------------------------------------------------------
239 /// @class ClangASTSourceProxy ClangASTSource.h "lldb/Expression/ClangASTSource.h"
240 /// @brief Proxy for ClangASTSource
242 /// Clang AST contexts like to own their AST sources, so this is a
243 /// state-free proxy object.
244 //----------------------------------------------------------------------
245 class ClangASTSourceProxy : public ClangExternalASTSourceCommon
248 ClangASTSourceProxy (ClangASTSource &original) :
254 FindExternalVisibleDeclsByName (const clang::DeclContext *DC,
255 clang::DeclarationName Name)
257 return m_original.FindExternalVisibleDeclsByName(DC, Name);
260 clang::ExternalLoadResult
261 FindExternalLexicalDecls (const clang::DeclContext *DC,
262 bool (*isKindWeWant)(clang::Decl::Kind),
263 llvm::SmallVectorImpl<clang::Decl*> &Decls)
265 return m_original.FindExternalLexicalDecls(DC, isKindWeWant, Decls);
269 CompleteType (clang::TagDecl *Tag)
271 return m_original.CompleteType(Tag);
275 CompleteType (clang::ObjCInterfaceDecl *Class)
277 return m_original.CompleteType(Class);
281 layoutRecordType(const clang::RecordDecl *Record,
284 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &FieldOffsets,
285 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &BaseOffsets,
286 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &VirtualBaseOffsets)
288 return m_original.layoutRecordType(Record,
296 void StartTranslationUnit (clang::ASTConsumer *Consumer)
298 return m_original.StartTranslationUnit(Consumer);
302 GetMetadata(const void * object)
304 return m_original.GetMetadata(object);
308 SetMetadata(const void * object, ClangASTMetadata &metadata)
310 return m_original.SetMetadata(object, metadata);
314 HasMetadata(const void * object)
316 return m_original.HasMetadata(object);
319 ClangASTSource &m_original;
322 clang::ExternalASTSource *CreateProxy()
324 return new ClangASTSourceProxy(*this);
328 //------------------------------------------------------------------
329 /// Look for the complete version of an Objective-C interface, and
330 /// return it if found.
332 /// @param[in] interface_decl
333 /// An ObjCInterfaceDecl that may not be the complete one.
336 /// NULL if the complete interface couldn't be found;
337 /// the complete interface otherwise.
338 //------------------------------------------------------------------
339 clang::ObjCInterfaceDecl *
340 GetCompleteObjCInterface (clang::ObjCInterfaceDecl *interface_decl);
342 //------------------------------------------------------------------
343 /// Find all entities matching a given name in a given module,
344 /// using a NameSearchContext to make Decls for them.
346 /// @param[in] context
347 /// The NameSearchContext that can construct Decls for this name.
349 /// @param[in] module
350 /// If non-NULL, the module to query.
352 /// @param[in] namespace_decl
353 /// If valid and module is non-NULL, the parent namespace.
355 /// @param[in] current_id
356 /// The ID for the current FindExternalVisibleDecls invocation,
357 /// for logging purposes.
360 /// True on success; false otherwise.
361 //------------------------------------------------------------------
363 FindExternalVisibleDecls (NameSearchContext &context,
364 lldb::ModuleSP module,
365 ClangNamespaceDecl &namespace_decl,
366 unsigned int current_id);
368 //------------------------------------------------------------------
369 /// Find all Objective-C methods matching a given selector.
371 /// @param[in] context
372 /// The NameSearchContext that can construct Decls for this name.
373 /// Its m_decl_name contains the selector and its m_decl_context
374 /// is the containing object.
375 //------------------------------------------------------------------
377 FindObjCMethodDecls (NameSearchContext &context);
379 //------------------------------------------------------------------
380 /// Find all Objective-C properties and ivars with a given name.
382 /// @param[in] context
383 /// The NameSearchContext that can construct Decls for this name.
384 /// Its m_decl_name contains the name and its m_decl_context
385 /// is the containing object.
386 //------------------------------------------------------------------
388 FindObjCPropertyAndIvarDecls (NameSearchContext &context);
390 //------------------------------------------------------------------
391 /// A wrapper for ClangASTContext::CopyType that sets a flag that
392 /// indicates that we should not respond to queries during import.
394 /// @param[in] dest_context
395 /// The target AST context, typically the parser's AST context.
397 /// @param[in] source_context
398 /// The source AST context, typically the AST context of whatever
399 /// symbol file the type was found in.
401 /// @param[in] clang_type
405 /// The imported type.
406 //------------------------------------------------------------------
408 GuardedCopyType (const ClangASTType &src_type);
410 friend struct NameSearchContext;
412 bool m_import_in_progress;
413 bool m_lookups_enabled;
415 const lldb::TargetSP m_target; ///< The target to use in finding variables and types.
416 clang::ASTContext *m_ast_context; ///< The AST context requests are coming in for.
417 ClangASTImporter *m_ast_importer; ///< The target's AST importer.
418 std::set<const char *> m_active_lookups;
421 //----------------------------------------------------------------------
422 /// @class NameSearchContext ClangASTSource.h "lldb/Expression/ClangASTSource.h"
423 /// @brief Container for all objects relevant to a single name lookup
425 /// LLDB needs to create Decls for entities it finds. This class communicates
426 /// what name is being searched for and provides helper functions to construct
427 /// Decls given appropriate type information.
428 //----------------------------------------------------------------------
429 struct NameSearchContext {
430 ClangASTSource &m_ast_source; ///< The AST source making the request
431 llvm::SmallVectorImpl<clang::NamedDecl*> &m_decls; ///< The list of declarations already constructed
432 ClangASTImporter::NamespaceMapSP m_namespace_map; ///< The mapping of all namespaces found for this request back to their modules
433 const clang::DeclarationName &m_decl_name; ///< The name being looked for
434 const clang::DeclContext *m_decl_context; ///< The DeclContext to put declarations into
435 llvm::SmallSet <ClangASTType, 5> m_function_types; ///< All the types of functions that have been reported, so we don't report conflicts
439 bool function_with_type_info : 1;
443 //------------------------------------------------------------------
446 /// Initializes class variables.
448 /// @param[in] astSource
449 /// A reference to the AST source making a request.
452 /// A reference to a list into which new Decls will be placed. This
453 /// list is typically empty when the function is called.
456 /// The name being searched for (always an Identifier).
459 /// The DeclContext to register Decls in.
460 //------------------------------------------------------------------
461 NameSearchContext (ClangASTSource &astSource,
462 llvm::SmallVectorImpl<clang::NamedDecl*> &decls,
463 clang::DeclarationName &name,
464 const clang::DeclContext *dc) :
465 m_ast_source(astSource),
470 memset(&m_found, 0, sizeof(m_found));
473 //------------------------------------------------------------------
474 /// Create a VarDecl with the name being searched for and the provided
475 /// type and register it in the right places.
478 /// The opaque QualType for the VarDecl being registered.
479 //------------------------------------------------------------------
480 clang::NamedDecl *AddVarDecl(const ClangASTType &type);
482 //------------------------------------------------------------------
483 /// Create a FunDecl with the name being searched for and the provided
484 /// type and register it in the right places.
487 /// The opaque QualType for the FunDecl being registered.
488 //------------------------------------------------------------------
489 clang::NamedDecl *AddFunDecl(const ClangASTType &type);
491 //------------------------------------------------------------------
492 /// Create a FunDecl with the name being searched for and generic
493 /// type (i.e. intptr_t NAME_GOES_HERE(...)) and register it in the
495 //------------------------------------------------------------------
496 clang::NamedDecl *AddGenericFunDecl();
498 //------------------------------------------------------------------
499 /// Create a TypeDecl with the name being searched for and the provided
500 /// type and register it in the right places.
503 /// The opaque QualType for the TypeDecl being registered.
504 //------------------------------------------------------------------
505 clang::NamedDecl *AddTypeDecl(const ClangASTType &clang_type);
508 //------------------------------------------------------------------
509 /// Add Decls from the provided DeclContextLookupResult to the list
512 /// @param[in] result
513 /// The DeclContextLookupResult, usually returned as the result
514 /// of querying a DeclContext.
515 //------------------------------------------------------------------
516 void AddLookupResult (clang::DeclContextLookupConstResult result);
518 //------------------------------------------------------------------
519 /// Add a NamedDecl to the list of results.
522 /// The NamedDecl, usually returned as the result
523 /// of querying a DeclContext.
524 //------------------------------------------------------------------
525 void AddNamedDecl (clang::NamedDecl *decl);