1 //===-- ClangASTContext.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_ClangASTContext_h_
11 #define liblldb_ClangASTContext_h_
18 #include <initializer_list>
26 // Other libraries and framework includes
27 #include "clang/AST/ASTContext.h"
28 #include "clang/AST/ExternalASTMerger.h"
29 #include "clang/AST/TemplateBase.h"
30 #include "llvm/ADT/SmallVector.h"
33 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
34 #include "lldb/Core/ClangForward.h"
35 #include "lldb/Symbol/CompilerType.h"
36 #include "lldb/Symbol/TypeSystem.h"
37 #include "lldb/Utility/ConstString.h"
38 #include "lldb/lldb-enumerations.h"
40 class DWARFASTParserClang;
41 #ifdef LLDB_ENABLE_ALL
43 #endif // LLDB_ENABLE_ALL
45 namespace lldb_private {
49 class ClangASTContext : public TypeSystem {
51 typedef void (*CompleteTagDeclCallback)(void *baton, clang::TagDecl *);
52 typedef void (*CompleteObjCInterfaceDeclCallback)(void *baton,
53 clang::ObjCInterfaceDecl *);
55 //------------------------------------------------------------------
56 // llvm casting support
57 //------------------------------------------------------------------
58 static bool classof(const TypeSystem *ts) {
59 return ts->getKind() == TypeSystem::eKindClang;
62 //------------------------------------------------------------------
63 // Constructors and Destructors
64 //------------------------------------------------------------------
65 ClangASTContext(const char *triple = nullptr);
67 ~ClangASTContext() override;
69 void Finalize() override;
71 //------------------------------------------------------------------
72 // PluginInterface functions
73 //------------------------------------------------------------------
74 ConstString GetPluginName() override;
76 uint32_t GetPluginVersion() override;
78 static ConstString GetPluginNameStatic();
80 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language,
81 Module *module, Target *target);
83 static void EnumerateSupportedLanguages(
84 std::set<lldb::LanguageType> &languages_for_types,
85 std::set<lldb::LanguageType> &languages_for_expressions);
87 static void Initialize();
89 static void Terminate();
91 static ClangASTContext *GetASTContext(clang::ASTContext *ast_ctx);
93 clang::ASTContext *getASTContext();
95 void setASTContext(clang::ASTContext *ast_ctx);
97 clang::Builtin::Context *getBuiltinContext();
99 clang::IdentifierTable *getIdentifierTable();
101 clang::LangOptions *getLanguageOptions();
103 clang::SelectorTable *getSelectorTable();
105 clang::FileManager *getFileManager();
107 clang::SourceManager *getSourceManager();
109 clang::DiagnosticsEngine *getDiagnosticsEngine();
111 clang::DiagnosticConsumer *getDiagnosticConsumer();
113 clang::MangleContext *getMangleContext();
115 std::shared_ptr<clang::TargetOptions> &getTargetOptions();
117 clang::TargetInfo *getTargetInfo();
121 const char *GetTargetTriple();
123 void SetTargetTriple(const char *target_triple);
125 void SetArchitecture(const ArchSpec &arch);
127 bool HasExternalSource();
129 void SetExternalSource(
130 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> &ast_source_ap);
132 void RemoveExternalSource();
134 bool GetCompleteDecl(clang::Decl *decl) {
135 return ClangASTContext::GetCompleteDecl(getASTContext(), decl);
138 static void DumpDeclHiearchy(clang::Decl *decl);
140 static void DumpDeclContextHiearchy(clang::DeclContext *decl_ctx);
142 static bool DeclsAreEquivalent(clang::Decl *lhs_decl, clang::Decl *rhs_decl);
144 static bool GetCompleteDecl(clang::ASTContext *ast, clang::Decl *decl);
146 void SetMetadataAsUserID(const void *object, lldb::user_id_t user_id);
148 void SetMetadata(const void *object, ClangASTMetadata &meta_data) {
149 SetMetadata(getASTContext(), object, meta_data);
152 static void SetMetadata(clang::ASTContext *ast, const void *object,
153 ClangASTMetadata &meta_data);
155 ClangASTMetadata *GetMetadata(const void *object) {
156 return GetMetadata(getASTContext(), object);
159 static ClangASTMetadata *GetMetadata(clang::ASTContext *ast,
162 //------------------------------------------------------------------
164 //------------------------------------------------------------------
165 CompilerType GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding,
166 size_t bit_size) override;
168 static CompilerType GetBuiltinTypeForEncodingAndBitSize(
169 clang::ASTContext *ast, lldb::Encoding encoding, uint32_t bit_size);
171 CompilerType GetBasicType(lldb::BasicType type);
173 static CompilerType GetBasicType(clang::ASTContext *ast,
174 lldb::BasicType type);
176 static CompilerType GetBasicType(clang::ASTContext *ast,
177 const ConstString &name);
179 static lldb::BasicType GetBasicTypeEnumeration(const ConstString &name);
181 CompilerType GetBuiltinTypeForDWARFEncodingAndBitSize(const char *type_name,
185 CompilerType GetCStringType(bool is_const);
187 static CompilerType GetUnknownAnyType(clang::ASTContext *ast);
189 CompilerType GetUnknownAnyType() {
190 return ClangASTContext::GetUnknownAnyType(getASTContext());
193 static clang::DeclContext *GetDeclContextForType(clang::QualType type);
195 static clang::DeclContext *GetDeclContextForType(const CompilerType &type);
197 uint32_t GetPointerByteSize() override;
199 static clang::DeclContext *GetTranslationUnitDecl(clang::ASTContext *ast);
201 clang::DeclContext *GetTranslationUnitDecl() {
202 return GetTranslationUnitDecl(getASTContext());
205 static clang::Decl *CopyDecl(clang::ASTContext *dest_context,
206 clang::ASTContext *source_context,
207 clang::Decl *source_decl);
209 static bool AreTypesSame(CompilerType type1, CompilerType type2,
210 bool ignore_qualifiers = false);
212 static CompilerType GetTypeForDecl(clang::NamedDecl *decl);
214 static CompilerType GetTypeForDecl(clang::TagDecl *decl);
216 static CompilerType GetTypeForDecl(clang::ObjCInterfaceDecl *objc_decl);
218 template <typename RecordDeclType>
219 CompilerType GetTypeForIdentifier(const ConstString &type_name) {
220 CompilerType compiler_type;
222 if (type_name.GetLength()) {
223 clang::ASTContext *ast = getASTContext();
225 clang::IdentifierInfo &myIdent =
226 ast->Idents.get(type_name.GetCString());
227 clang::DeclarationName myName =
228 ast->DeclarationNames.getIdentifier(&myIdent);
230 clang::DeclContext::lookup_result result =
231 ast->getTranslationUnitDecl()->lookup(myName);
233 if (!result.empty()) {
234 clang::NamedDecl *named_decl = result[0];
235 if (const RecordDeclType *record_decl =
236 llvm::dyn_cast<RecordDeclType>(named_decl))
237 compiler_type.SetCompilerType(
238 ast, clang::QualType(record_decl->getTypeForDecl(), 0));
243 return compiler_type;
246 CompilerType CreateStructForIdentifier(
247 const ConstString &type_name,
248 const std::initializer_list<std::pair<const char *, CompilerType>>
250 bool packed = false);
252 CompilerType GetOrCreateStructForIdentifier(
253 const ConstString &type_name,
254 const std::initializer_list<std::pair<const char *, CompilerType>>
256 bool packed = false);
258 //------------------------------------------------------------------
259 // Structure, Unions, Classes
260 //------------------------------------------------------------------
262 static clang::AccessSpecifier
263 ConvertAccessTypeToAccessSpecifier(lldb::AccessType access);
265 static clang::AccessSpecifier
266 UnifyAccessSpecifiers(clang::AccessSpecifier lhs, clang::AccessSpecifier rhs);
268 static uint32_t GetNumBaseClasses(const clang::CXXRecordDecl *cxx_record_decl,
269 bool omit_empty_base_classes);
271 CompilerType CreateRecordType(clang::DeclContext *decl_ctx,
272 lldb::AccessType access_type, const char *name,
273 int kind, lldb::LanguageType language,
274 ClangASTMetadata *metadata = nullptr);
276 class TemplateParameterInfos {
278 bool IsValid() const {
281 return args.size() == names.size() &&
282 ((bool)pack_name == (bool)packed_args) &&
283 (!packed_args || !packed_args->packed_args);
286 llvm::SmallVector<const char *, 2> names;
287 llvm::SmallVector<clang::TemplateArgument, 2> args;
289 const char * pack_name = nullptr;
290 std::unique_ptr<TemplateParameterInfos> packed_args;
293 clang::FunctionTemplateDecl *
294 CreateFunctionTemplateDecl(clang::DeclContext *decl_ctx,
295 clang::FunctionDecl *func_decl, const char *name,
296 const TemplateParameterInfos &infos);
298 void CreateFunctionTemplateSpecializationInfo(
299 clang::FunctionDecl *func_decl, clang::FunctionTemplateDecl *Template,
300 const TemplateParameterInfos &infos);
302 clang::ClassTemplateDecl *
303 CreateClassTemplateDecl(clang::DeclContext *decl_ctx,
304 lldb::AccessType access_type, const char *class_name,
305 int kind, const TemplateParameterInfos &infos);
307 clang::ClassTemplateSpecializationDecl *CreateClassTemplateSpecializationDecl(
308 clang::DeclContext *decl_ctx,
309 clang::ClassTemplateDecl *class_template_decl, int kind,
310 const TemplateParameterInfos &infos);
313 CreateClassTemplateSpecializationType(clang::ClassTemplateSpecializationDecl *
314 class_template_specialization_decl);
316 static clang::DeclContext *
317 GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl);
319 static clang::DeclContext *
320 GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl);
322 static bool CheckOverloadedOperatorKindParameterCount(
323 bool is_method, clang::OverloadedOperatorKind op_kind,
324 uint32_t num_params);
326 bool FieldIsBitfield(clang::FieldDecl *field, uint32_t &bitfield_bit_size);
328 static bool FieldIsBitfield(clang::ASTContext *ast, clang::FieldDecl *field,
329 uint32_t &bitfield_bit_size);
331 static bool RecordHasFields(const clang::RecordDecl *record_decl);
333 CompilerType CreateObjCClass(const char *name, clang::DeclContext *decl_ctx,
334 bool isForwardDecl, bool isInternal,
335 ClangASTMetadata *metadata = nullptr);
337 bool SetTagTypeKind(clang::QualType type, int kind) const;
339 bool SetDefaultAccessForRecordFields(clang::RecordDecl *record_decl,
340 int default_accessibility,
341 int *assigned_accessibilities,
342 size_t num_assigned_accessibilities);
344 // Returns a mask containing bits from the ClangASTContext::eTypeXXX
347 //------------------------------------------------------------------
348 // Namespace Declarations
349 //------------------------------------------------------------------
351 clang::NamespaceDecl *
352 GetUniqueNamespaceDeclaration(const char *name, clang::DeclContext *decl_ctx);
354 static clang::NamespaceDecl *
355 GetUniqueNamespaceDeclaration(clang::ASTContext *ast, const char *name,
356 clang::DeclContext *decl_ctx);
358 //------------------------------------------------------------------
360 //------------------------------------------------------------------
362 clang::FunctionDecl *
363 CreateFunctionDeclaration(clang::DeclContext *decl_ctx, const char *name,
364 const CompilerType &function_Type, int storage,
367 static CompilerType CreateFunctionType(clang::ASTContext *ast,
368 const CompilerType &result_type,
369 const CompilerType *args,
370 unsigned num_args, bool is_variadic,
371 unsigned type_quals);
373 CompilerType CreateFunctionType(const CompilerType &result_type,
374 const CompilerType *args, unsigned num_args,
375 bool is_variadic, unsigned type_quals) {
376 return ClangASTContext::CreateFunctionType(
377 getASTContext(), result_type, args, num_args, is_variadic, type_quals);
380 clang::ParmVarDecl *CreateParameterDeclaration(const char *name,
381 const CompilerType ¶m_type,
384 void SetFunctionParameters(clang::FunctionDecl *function_decl,
385 clang::ParmVarDecl **params, unsigned num_params);
387 CompilerType CreateBlockPointerType(const CompilerType &function_type);
389 //------------------------------------------------------------------
391 //------------------------------------------------------------------
393 CompilerType CreateArrayType(const CompilerType &element_type,
394 size_t element_count, bool is_vector);
396 //------------------------------------------------------------------
398 //------------------------------------------------------------------
399 CompilerType CreateEnumerationType(const char *name,
400 clang::DeclContext *decl_ctx,
401 const Declaration &decl,
402 const CompilerType &integer_qual_type,
405 //------------------------------------------------------------------
406 // Integer type functions
407 //------------------------------------------------------------------
409 static CompilerType GetIntTypeFromBitSize(clang::ASTContext *ast,
410 size_t bit_size, bool is_signed);
412 CompilerType GetPointerSizedIntType(bool is_signed) {
413 return GetPointerSizedIntType(getASTContext(), is_signed);
416 static CompilerType GetPointerSizedIntType(clang::ASTContext *ast,
419 //------------------------------------------------------------------
420 // Floating point functions
421 //------------------------------------------------------------------
423 static CompilerType GetFloatTypeFromBitSize(clang::ASTContext *ast,
426 //------------------------------------------------------------------
427 // TypeSystem methods
428 //------------------------------------------------------------------
429 DWARFASTParser *GetDWARFParser() override;
430 #ifdef LLDB_ENABLE_ALL
431 PDBASTParser *GetPDBParser();
432 #endif // LLDB_ENABLE_ALL
434 //------------------------------------------------------------------
435 // ClangASTContext callbacks for external source lookups.
436 //------------------------------------------------------------------
437 static void CompleteTagDecl(void *baton, clang::TagDecl *);
439 static void CompleteObjCInterfaceDecl(void *baton,
440 clang::ObjCInterfaceDecl *);
442 static bool LayoutRecordType(
443 void *baton, const clang::RecordDecl *record_decl, uint64_t &size,
445 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
446 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
448 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
451 //----------------------------------------------------------------------
452 // CompilerDecl override functions
453 //----------------------------------------------------------------------
454 ConstString DeclGetName(void *opaque_decl) override;
456 ConstString DeclGetMangledName(void *opaque_decl) override;
458 CompilerDeclContext DeclGetDeclContext(void *opaque_decl) override;
460 CompilerType DeclGetFunctionReturnType(void *opaque_decl) override;
462 size_t DeclGetFunctionNumArguments(void *opaque_decl) override;
464 CompilerType DeclGetFunctionArgumentType(void *opaque_decl,
465 size_t arg_idx) override;
467 //----------------------------------------------------------------------
468 // CompilerDeclContext override functions
469 //----------------------------------------------------------------------
471 std::vector<CompilerDecl>
472 DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name,
473 const bool ignore_using_decls) override;
475 bool DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) override;
477 ConstString DeclContextGetName(void *opaque_decl_ctx) override;
479 ConstString DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) override;
481 bool DeclContextIsClassMethod(void *opaque_decl_ctx,
482 lldb::LanguageType *language_ptr,
483 bool *is_instance_method_ptr,
484 ConstString *language_object_name_ptr) override;
486 //----------------------------------------------------------------------
487 // Clang specific clang::DeclContext functions
488 //----------------------------------------------------------------------
490 static clang::DeclContext *
491 DeclContextGetAsDeclContext(const CompilerDeclContext &dc);
493 static clang::ObjCMethodDecl *
494 DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc);
496 static clang::CXXMethodDecl *
497 DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc);
499 static clang::FunctionDecl *
500 DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc);
502 static clang::NamespaceDecl *
503 DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc);
505 static ClangASTMetadata *DeclContextGetMetaData(const CompilerDeclContext &dc,
508 static clang::ASTContext *
509 DeclContextGetClangASTContext(const CompilerDeclContext &dc);
511 //----------------------------------------------------------------------
513 //----------------------------------------------------------------------
515 bool IsArrayType(lldb::opaque_compiler_type_t type,
516 CompilerType *element_type, uint64_t *size,
517 bool *is_incomplete) override;
519 bool IsVectorType(lldb::opaque_compiler_type_t type,
520 CompilerType *element_type, uint64_t *size) override;
522 bool IsAggregateType(lldb::opaque_compiler_type_t type) override;
524 bool IsAnonymousType(lldb::opaque_compiler_type_t type) override;
526 bool IsBeingDefined(lldb::opaque_compiler_type_t type) override;
528 bool IsCharType(lldb::opaque_compiler_type_t type) override;
530 bool IsCompleteType(lldb::opaque_compiler_type_t type) override;
532 bool IsConst(lldb::opaque_compiler_type_t type) override;
534 bool IsCStringType(lldb::opaque_compiler_type_t type,
535 uint32_t &length) override;
537 static bool IsCXXClassType(const CompilerType &type);
539 bool IsDefined(lldb::opaque_compiler_type_t type) override;
541 bool IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count,
542 bool &is_complex) override;
544 bool IsFunctionType(lldb::opaque_compiler_type_t type,
545 bool *is_variadic_ptr) override;
547 uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
548 CompilerType *base_type_ptr) override;
551 GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) override;
553 CompilerType GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
554 const size_t index) override;
556 bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) override;
558 bool IsBlockPointerType(lldb::opaque_compiler_type_t type,
559 CompilerType *function_pointer_type_ptr) override;
561 bool IsIntegerType(lldb::opaque_compiler_type_t type,
562 bool &is_signed) override;
564 bool IsEnumerationType(lldb::opaque_compiler_type_t type,
565 bool &is_signed) override;
567 static bool IsObjCClassType(const CompilerType &type);
569 static bool IsObjCClassTypeAndHasIVars(const CompilerType &type,
570 bool check_superclass);
572 static bool IsObjCObjectOrInterfaceType(const CompilerType &type);
574 static bool IsObjCObjectPointerType(const CompilerType &type,
575 CompilerType *target_type = nullptr);
577 bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) override;
579 static bool IsClassType(lldb::opaque_compiler_type_t type);
581 static bool IsEnumType(lldb::opaque_compiler_type_t type);
583 bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
584 CompilerType *target_type, // Can pass nullptr
585 bool check_cplusplus, bool check_objc) override;
587 bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) override;
589 bool IsPointerType(lldb::opaque_compiler_type_t type,
590 CompilerType *pointee_type) override;
592 bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type,
593 CompilerType *pointee_type) override;
595 bool IsReferenceType(lldb::opaque_compiler_type_t type,
596 CompilerType *pointee_type, bool *is_rvalue) override;
598 bool IsScalarType(lldb::opaque_compiler_type_t type) override;
600 bool IsTypedefType(lldb::opaque_compiler_type_t type) override;
602 bool IsVoidType(lldb::opaque_compiler_type_t type) override;
604 bool SupportsLanguage(lldb::LanguageType language) override;
606 static bool GetCXXClassName(const CompilerType &type,
607 std::string &class_name);
609 static bool GetObjCClassName(const CompilerType &type,
610 std::string &class_name);
612 //----------------------------------------------------------------------
614 //----------------------------------------------------------------------
616 bool GetCompleteType(lldb::opaque_compiler_type_t type) override;
618 //----------------------------------------------------------------------
620 //----------------------------------------------------------------------
622 ConstString GetTypeName(lldb::opaque_compiler_type_t type) override;
624 uint32_t GetTypeInfo(lldb::opaque_compiler_type_t type,
625 CompilerType *pointee_or_element_compiler_type) override;
628 GetMinimumLanguage(lldb::opaque_compiler_type_t type) override;
630 lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) override;
632 unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) override;
634 //----------------------------------------------------------------------
635 // Creating related types
636 //----------------------------------------------------------------------
638 // Using the current type, create a new typedef to that type using
640 // as the name and "decl_ctx" as the decl context.
642 CreateTypedefType(const CompilerType &type, const char *typedef_name,
643 const CompilerDeclContext &compiler_decl_ctx);
645 CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type,
646 uint64_t *stride) override;
648 CompilerType GetArrayType(lldb::opaque_compiler_type_t type,
649 uint64_t size) override;
651 CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) override;
654 GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) override;
656 // Returns -1 if this isn't a function of if the function doesn't have a
658 // Returns a value >= 0 if there is a prototype.
659 int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) override;
661 CompilerType GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type,
662 size_t idx) override;
665 GetFunctionReturnType(lldb::opaque_compiler_type_t type) override;
667 size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) override;
669 TypeMemberFunctionImpl
670 GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
671 size_t idx) override;
673 CompilerType GetNonReferenceType(lldb::opaque_compiler_type_t type) override;
675 CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) override;
677 CompilerType GetPointerType(lldb::opaque_compiler_type_t type) override;
680 GetLValueReferenceType(lldb::opaque_compiler_type_t type) override;
683 GetRValueReferenceType(lldb::opaque_compiler_type_t type) override;
685 CompilerType AddConstModifier(lldb::opaque_compiler_type_t type) override;
687 CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type) override;
689 CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type) override;
691 CompilerType CreateTypedef(lldb::opaque_compiler_type_t type,
693 const CompilerDeclContext &decl_ctx) override;
695 // If the current object represents a typedef type, get the underlying type
696 CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) override;
698 //----------------------------------------------------------------------
699 // Create related types using the current type's AST
700 //----------------------------------------------------------------------
701 CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) override;
703 //----------------------------------------------------------------------
704 // Exploring the type
705 //----------------------------------------------------------------------
707 uint64_t GetByteSize(lldb::opaque_compiler_type_t type,
708 ExecutionContextScope *exe_scope) {
709 return (GetBitSize(type, exe_scope) + 7) / 8;
712 uint64_t GetBitSize(lldb::opaque_compiler_type_t type,
713 ExecutionContextScope *exe_scope) override;
715 lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type,
716 uint64_t &count) override;
718 lldb::Format GetFormat(lldb::opaque_compiler_type_t type) override;
720 size_t GetTypeBitAlign(lldb::opaque_compiler_type_t type) override;
722 uint32_t GetNumChildren(lldb::opaque_compiler_type_t type,
723 bool omit_empty_base_classes) override;
725 CompilerType GetBuiltinTypeByName(const ConstString &name) override;
728 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) override;
730 static lldb::BasicType
731 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type,
732 const ConstString &name);
734 void ForEachEnumerator(
735 lldb::opaque_compiler_type_t type,
736 std::function<bool(const CompilerType &integer_type,
737 const ConstString &name,
738 const llvm::APSInt &value)> const &callback) override;
740 uint32_t GetNumFields(lldb::opaque_compiler_type_t type) override;
742 CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
743 std::string &name, uint64_t *bit_offset_ptr,
744 uint32_t *bitfield_bit_size_ptr,
745 bool *is_bitfield_ptr) override;
747 uint32_t GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) override;
749 uint32_t GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) override;
751 CompilerType GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type,
753 uint32_t *bit_offset_ptr) override;
755 CompilerType GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type,
757 uint32_t *bit_offset_ptr) override;
759 static uint32_t GetNumPointeeChildren(clang::QualType type);
761 CompilerType GetChildCompilerTypeAtIndex(
762 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
763 bool transparent_pointers, bool omit_empty_base_classes,
764 bool ignore_array_bounds, std::string &child_name,
765 uint32_t &child_byte_size, int32_t &child_byte_offset,
766 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
767 bool &child_is_base_class, bool &child_is_deref_of_parent,
768 ValueObject *valobj, uint64_t &language_flags) override;
770 // Lookup a child given a name. This function will match base class names
771 // and member member names in "clang_type" only, not descendants.
772 uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
774 bool omit_empty_base_classes) override;
776 // Lookup a child member given a name. This function will match member names
777 // only and will descend into "clang_type" children in search for the first
778 // member in this class, or any base class that matches "name".
779 // TODO: Return all matches for a given name by returning a
780 // vector<vector<uint32_t>>
781 // so we catch all names that match a given child name, not just the first.
783 GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type,
784 const char *name, bool omit_empty_base_classes,
785 std::vector<uint32_t> &child_indexes) override;
787 size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type) override;
789 lldb::TemplateArgumentKind
790 GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
791 size_t idx) override;
792 CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
793 size_t idx) override;
794 llvm::Optional<CompilerType::IntegralTemplateArgument>
795 GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
796 size_t idx) override;
798 CompilerType GetTypeForFormatters(void *type) override;
800 #define LLDB_INVALID_DECL_LEVEL UINT32_MAX
801 // LLDB_INVALID_DECL_LEVEL is returned by CountDeclLevels if
802 // child_decl_ctx could not be found in decl_ctx.
803 uint32_t CountDeclLevels(clang::DeclContext *frame_decl_ctx,
804 clang::DeclContext *child_decl_ctx,
805 ConstString *child_name = nullptr,
806 CompilerType *child_type = nullptr);
808 //----------------------------------------------------------------------
809 // Modifying RecordType
810 //----------------------------------------------------------------------
811 static clang::FieldDecl *AddFieldToRecordType(const CompilerType &type,
813 const CompilerType &field_type,
814 lldb::AccessType access,
815 uint32_t bitfield_bit_size);
817 static void BuildIndirectFields(const CompilerType &type);
819 static void SetIsPacked(const CompilerType &type);
821 static clang::VarDecl *AddVariableToRecordType(const CompilerType &type,
823 const CompilerType &var_type,
824 lldb::AccessType access);
826 clang::CXXMethodDecl *
827 AddMethodToCXXRecordType(lldb::opaque_compiler_type_t type, const char *name,
828 const CompilerType &method_type,
829 lldb::AccessType access, bool is_virtual,
830 bool is_static, bool is_inline, bool is_explicit,
831 bool is_attr_used, bool is_artificial);
834 clang::CXXBaseSpecifier *
835 CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
836 lldb::AccessType access, bool is_virtual,
839 static void DeleteBaseClassSpecifiers(clang::CXXBaseSpecifier **base_classes,
840 unsigned num_base_classes);
843 SetBaseClassesForClassType(lldb::opaque_compiler_type_t type,
844 clang::CXXBaseSpecifier const *const *base_classes,
845 unsigned num_base_classes);
847 static bool SetObjCSuperClass(const CompilerType &type,
848 const CompilerType &superclass_compiler_type);
850 static bool AddObjCClassProperty(const CompilerType &type,
851 const char *property_name,
852 const CompilerType &property_compiler_type,
853 clang::ObjCIvarDecl *ivar_decl,
854 const char *property_setter_name,
855 const char *property_getter_name,
856 uint32_t property_attributes,
857 ClangASTMetadata *metadata);
859 static clang::ObjCMethodDecl *AddMethodToObjCObjectType(
860 const CompilerType &type,
861 const char *name, // the full symbol name as seen in the symbol table
862 // (lldb::opaque_compiler_type_t type, "-[NString
863 // stringWithCString:]")
864 const CompilerType &method_compiler_type, lldb::AccessType access,
865 bool is_artificial, bool is_variadic);
867 static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type,
870 static bool GetHasExternalStorage(const CompilerType &type);
871 //------------------------------------------------------------------
873 //------------------------------------------------------------------
874 static bool StartTagDeclarationDefinition(const CompilerType &type);
876 static bool CompleteTagDeclarationDefinition(const CompilerType &type);
878 //----------------------------------------------------------------------
879 // Modifying Enumeration types
880 //----------------------------------------------------------------------
881 bool AddEnumerationValueToEnumerationType(
882 lldb::opaque_compiler_type_t type,
883 const CompilerType &enumerator_qual_type, const Declaration &decl,
884 const char *name, int64_t enum_value, uint32_t enum_value_bit_size);
886 CompilerType GetEnumerationIntegerType(lldb::opaque_compiler_type_t type);
888 //------------------------------------------------------------------
889 // Pointers & References
890 //------------------------------------------------------------------
892 // Call this function using the class type when you want to make a
893 // member pointer type to pointee_type.
894 static CompilerType CreateMemberPointerType(const CompilerType &type,
895 const CompilerType &pointee_type);
897 // Converts "s" to a floating point value and place resulting floating
898 // point bytes in the "dst" buffer.
899 size_t ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
900 const char *s, uint8_t *dst,
901 size_t dst_size) override;
903 //----------------------------------------------------------------------
905 //----------------------------------------------------------------------
906 void DumpValue(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
907 Stream *s, lldb::Format format, const DataExtractor &data,
908 lldb::offset_t data_offset, size_t data_byte_size,
909 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
910 bool show_types, bool show_summary, bool verbose,
911 uint32_t depth) override;
913 bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s,
914 lldb::Format format, const DataExtractor &data,
915 lldb::offset_t data_offset, size_t data_byte_size,
916 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
917 ExecutionContextScope *exe_scope) override;
919 void DumpSummary(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
920 Stream *s, const DataExtractor &data,
921 lldb::offset_t data_offset, size_t data_byte_size) override;
923 void DumpTypeDescription(
924 lldb::opaque_compiler_type_t type) override; // Dump to stdout
926 void DumpTypeDescription(lldb::opaque_compiler_type_t type,
929 static void DumpTypeName(const CompilerType &type);
931 static clang::EnumDecl *GetAsEnumDecl(const CompilerType &type);
933 static clang::RecordDecl *GetAsRecordDecl(const CompilerType &type);
935 static clang::TagDecl *GetAsTagDecl(const CompilerType &type);
937 clang::CXXRecordDecl *GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type);
939 static clang::ObjCInterfaceDecl *
940 GetAsObjCInterfaceDecl(const CompilerType &type);
942 clang::ClassTemplateDecl *ParseClassTemplateDecl(
943 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
944 const char *parent_name, int tag_decl_kind,
945 const ClangASTContext::TemplateParameterInfos &template_param_infos);
947 clang::BlockDecl *CreateBlockDeclaration(clang::DeclContext *ctx);
949 clang::UsingDirectiveDecl *
950 CreateUsingDirectiveDeclaration(clang::DeclContext *decl_ctx,
951 clang::NamespaceDecl *ns_decl);
953 clang::UsingDecl *CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
954 clang::NamedDecl *target);
956 clang::VarDecl *CreateVariableDeclaration(clang::DeclContext *decl_context,
958 clang::QualType type);
960 static lldb::opaque_compiler_type_t
961 GetOpaqueCompilerType(clang::ASTContext *ast, lldb::BasicType basic_type);
963 static clang::QualType GetQualType(lldb::opaque_compiler_type_t type) {
965 return clang::QualType::getFromOpaquePtr(type);
966 return clang::QualType();
969 static clang::QualType
970 GetCanonicalQualType(lldb::opaque_compiler_type_t type) {
972 return clang::QualType::getFromOpaquePtr(type).getCanonicalType();
973 return clang::QualType();
976 clang::DeclarationName
977 GetDeclarationName(const char *name, const CompilerType &function_clang_type);
979 virtual const clang::ExternalASTMerger::OriginMap &GetOriginMap() {
983 const clang::ClassTemplateSpecializationDecl *
984 GetAsTemplateSpecialization(lldb::opaque_compiler_type_t type);
986 //------------------------------------------------------------------
987 // Classes that inherit from ClangASTContext can see and modify these
988 //------------------------------------------------------------------
990 std::string m_target_triple;
991 std::unique_ptr<clang::ASTContext> m_ast_ap;
992 std::unique_ptr<clang::LangOptions> m_language_options_ap;
993 std::unique_ptr<clang::FileManager> m_file_manager_ap;
994 std::unique_ptr<clang::FileSystemOptions> m_file_system_options_ap;
995 std::unique_ptr<clang::SourceManager> m_source_manager_ap;
996 std::unique_ptr<clang::DiagnosticsEngine> m_diagnostics_engine_ap;
997 std::unique_ptr<clang::DiagnosticConsumer> m_diagnostic_consumer_ap;
998 std::shared_ptr<clang::TargetOptions> m_target_options_rp;
999 std::unique_ptr<clang::TargetInfo> m_target_info_ap;
1000 std::unique_ptr<clang::IdentifierTable> m_identifier_table_ap;
1001 std::unique_ptr<clang::SelectorTable> m_selector_table_ap;
1002 std::unique_ptr<clang::Builtin::Context> m_builtins_ap;
1003 std::unique_ptr<DWARFASTParserClang> m_dwarf_ast_parser_ap;
1004 #ifdef LLDB_ENABLE_ALL
1005 std::unique_ptr<PDBASTParser> m_pdb_ast_parser_ap;
1006 #endif // LLDB_ENABLE_ALL
1007 std::unique_ptr<ClangASTSource> m_scratch_ast_source_ap;
1008 std::unique_ptr<clang::MangleContext> m_mangle_ctx_ap;
1009 CompleteTagDeclCallback m_callback_tag_decl;
1010 CompleteObjCInterfaceDeclCallback m_callback_objc_decl;
1011 void * m_callback_baton;
1012 clang::ExternalASTMerger::OriginMap m_origins;
1013 uint32_t m_pointer_byte_size;
1015 bool m_can_evaluate_expressions;
1018 //------------------------------------------------------------------
1019 // For ClangASTContext only
1020 //------------------------------------------------------------------
1021 ClangASTContext(const ClangASTContext &);
1022 const ClangASTContext &operator=(const ClangASTContext &);
1025 class ClangASTContextForExpressions : public ClangASTContext {
1027 ClangASTContextForExpressions(Target &target);
1029 ~ClangASTContextForExpressions() override = default;
1032 GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix,
1033 lldb::LanguageType language,
1034 Expression::ResultType desired_type,
1035 const EvaluateExpressionOptions &options) override;
1037 FunctionCaller *GetFunctionCaller(const CompilerType &return_type,
1038 const Address &function_address,
1039 const ValueList &arg_value_list,
1040 const char *name) override;
1042 UtilityFunction *GetUtilityFunction(const char *text,
1043 const char *name) override;
1045 PersistentExpressionState *GetPersistentExpressionState() override;
1047 clang::ExternalASTMerger &GetMergerUnchecked();
1049 const clang::ExternalASTMerger::OriginMap &GetOriginMap() override {
1050 return GetMergerUnchecked().GetOrigins();
1053 lldb::TargetWP m_target_wp;
1054 lldb::ClangPersistentVariablesUP m_persistent_variables; ///< These are the
1063 } // namespace lldb_private
1065 #endif // liblldb_ClangASTContext_h_