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/TemplateBase.h"
29 #include "llvm/ADT/SmallVector.h"
32 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
33 #include "lldb/Core/ClangForward.h"
34 #include "lldb/Symbol/CompilerType.h"
35 #include "lldb/Symbol/TypeSystem.h"
36 #include "lldb/Utility/ConstString.h"
37 #include "lldb/lldb-enumerations.h"
39 class DWARFASTParserClang;
42 namespace lldb_private {
46 class ClangASTContext : public TypeSystem {
48 typedef void (*CompleteTagDeclCallback)(void *baton, clang::TagDecl *);
49 typedef void (*CompleteObjCInterfaceDeclCallback)(void *baton,
50 clang::ObjCInterfaceDecl *);
52 //------------------------------------------------------------------
53 // llvm casting support
54 //------------------------------------------------------------------
55 static bool classof(const TypeSystem *ts) {
56 return ts->getKind() == TypeSystem::eKindClang;
59 //------------------------------------------------------------------
60 // Constructors and Destructors
61 //------------------------------------------------------------------
62 ClangASTContext(const char *triple = nullptr);
64 ~ClangASTContext() override;
66 void Finalize() override;
68 //------------------------------------------------------------------
69 // PluginInterface functions
70 //------------------------------------------------------------------
71 ConstString GetPluginName() override;
73 uint32_t GetPluginVersion() override;
75 static ConstString GetPluginNameStatic();
77 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language,
78 Module *module, Target *target);
80 static void EnumerateSupportedLanguages(
81 std::set<lldb::LanguageType> &languages_for_types,
82 std::set<lldb::LanguageType> &languages_for_expressions);
84 static void Initialize();
86 static void Terminate();
88 static ClangASTContext *GetASTContext(clang::ASTContext *ast_ctx);
90 clang::ASTContext *getASTContext();
92 void setASTContext(clang::ASTContext *ast_ctx);
94 clang::Builtin::Context *getBuiltinContext();
96 clang::IdentifierTable *getIdentifierTable();
98 clang::LangOptions *getLanguageOptions();
100 clang::SelectorTable *getSelectorTable();
102 clang::FileManager *getFileManager();
104 clang::SourceManager *getSourceManager();
106 clang::DiagnosticsEngine *getDiagnosticsEngine();
108 clang::DiagnosticConsumer *getDiagnosticConsumer();
110 clang::MangleContext *getMangleContext();
112 std::shared_ptr<clang::TargetOptions> &getTargetOptions();
114 clang::TargetInfo *getTargetInfo();
118 const char *GetTargetTriple();
120 void SetTargetTriple(const char *target_triple);
122 void SetArchitecture(const ArchSpec &arch);
124 bool HasExternalSource();
126 void SetExternalSource(
127 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> &ast_source_ap);
129 void RemoveExternalSource();
131 bool GetCompleteDecl(clang::Decl *decl) {
132 return ClangASTContext::GetCompleteDecl(getASTContext(), decl);
135 static void DumpDeclHiearchy(clang::Decl *decl);
137 static void DumpDeclContextHiearchy(clang::DeclContext *decl_ctx);
139 static bool DeclsAreEquivalent(clang::Decl *lhs_decl, clang::Decl *rhs_decl);
141 static bool GetCompleteDecl(clang::ASTContext *ast, clang::Decl *decl);
143 void SetMetadataAsUserID(const void *object, lldb::user_id_t user_id);
145 void SetMetadata(const void *object, ClangASTMetadata &meta_data) {
146 SetMetadata(getASTContext(), object, meta_data);
149 static void SetMetadata(clang::ASTContext *ast, const void *object,
150 ClangASTMetadata &meta_data);
152 ClangASTMetadata *GetMetadata(const void *object) {
153 return GetMetadata(getASTContext(), object);
156 static ClangASTMetadata *GetMetadata(clang::ASTContext *ast,
159 //------------------------------------------------------------------
161 //------------------------------------------------------------------
162 CompilerType GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding,
163 size_t bit_size) override;
165 static CompilerType GetBuiltinTypeForEncodingAndBitSize(
166 clang::ASTContext *ast, lldb::Encoding encoding, uint32_t bit_size);
168 CompilerType GetBasicType(lldb::BasicType type);
170 static CompilerType GetBasicType(clang::ASTContext *ast,
171 lldb::BasicType type);
173 static CompilerType GetBasicType(clang::ASTContext *ast,
174 const ConstString &name);
176 static lldb::BasicType GetBasicTypeEnumeration(const ConstString &name);
178 CompilerType GetBuiltinTypeForDWARFEncodingAndBitSize(const char *type_name,
182 CompilerType GetCStringType(bool is_const);
184 static CompilerType GetUnknownAnyType(clang::ASTContext *ast);
186 CompilerType GetUnknownAnyType() {
187 return ClangASTContext::GetUnknownAnyType(getASTContext());
190 static clang::DeclContext *GetDeclContextForType(clang::QualType type);
192 static clang::DeclContext *GetDeclContextForType(const CompilerType &type);
194 uint32_t GetPointerByteSize() override;
196 static clang::DeclContext *GetTranslationUnitDecl(clang::ASTContext *ast);
198 clang::DeclContext *GetTranslationUnitDecl() {
199 return GetTranslationUnitDecl(getASTContext());
202 static clang::Decl *CopyDecl(clang::ASTContext *dest_context,
203 clang::ASTContext *source_context,
204 clang::Decl *source_decl);
206 static bool AreTypesSame(CompilerType type1, CompilerType type2,
207 bool ignore_qualifiers = false);
209 static CompilerType GetTypeForDecl(clang::NamedDecl *decl);
211 static CompilerType GetTypeForDecl(clang::TagDecl *decl);
213 static CompilerType GetTypeForDecl(clang::ObjCInterfaceDecl *objc_decl);
215 template <typename RecordDeclType>
216 CompilerType GetTypeForIdentifier(const ConstString &type_name) {
217 CompilerType compiler_type;
219 if (type_name.GetLength()) {
220 clang::ASTContext *ast = getASTContext();
222 clang::IdentifierInfo &myIdent =
223 ast->Idents.get(type_name.GetCString());
224 clang::DeclarationName myName =
225 ast->DeclarationNames.getIdentifier(&myIdent);
227 clang::DeclContext::lookup_result result =
228 ast->getTranslationUnitDecl()->lookup(myName);
230 if (!result.empty()) {
231 clang::NamedDecl *named_decl = result[0];
232 if (const RecordDeclType *record_decl =
233 llvm::dyn_cast<RecordDeclType>(named_decl))
234 compiler_type.SetCompilerType(
235 ast, clang::QualType(record_decl->getTypeForDecl(), 0));
240 return compiler_type;
243 CompilerType CreateStructForIdentifier(
244 const ConstString &type_name,
245 const std::initializer_list<std::pair<const char *, CompilerType>>
247 bool packed = false);
249 CompilerType GetOrCreateStructForIdentifier(
250 const ConstString &type_name,
251 const std::initializer_list<std::pair<const char *, CompilerType>>
253 bool packed = false);
255 //------------------------------------------------------------------
256 // Structure, Unions, Classes
257 //------------------------------------------------------------------
259 static clang::AccessSpecifier
260 ConvertAccessTypeToAccessSpecifier(lldb::AccessType access);
262 static clang::AccessSpecifier
263 UnifyAccessSpecifiers(clang::AccessSpecifier lhs, clang::AccessSpecifier rhs);
265 static uint32_t GetNumBaseClasses(const clang::CXXRecordDecl *cxx_record_decl,
266 bool omit_empty_base_classes);
268 CompilerType CreateRecordType(clang::DeclContext *decl_ctx,
269 lldb::AccessType access_type, const char *name,
270 int kind, lldb::LanguageType language,
271 ClangASTMetadata *metadata = nullptr);
273 class TemplateParameterInfos {
275 bool IsValid() const {
278 return args.size() == names.size();
281 size_t GetSize() const {
287 llvm::SmallVector<const char *, 2> names;
288 llvm::SmallVector<clang::TemplateArgument, 2> args;
291 clang::FunctionTemplateDecl *
292 CreateFunctionTemplateDecl(clang::DeclContext *decl_ctx,
293 clang::FunctionDecl *func_decl, const char *name,
294 const TemplateParameterInfos &infos);
296 void CreateFunctionTemplateSpecializationInfo(
297 clang::FunctionDecl *func_decl, clang::FunctionTemplateDecl *Template,
298 const TemplateParameterInfos &infos);
300 clang::ClassTemplateDecl *
301 CreateClassTemplateDecl(clang::DeclContext *decl_ctx,
302 lldb::AccessType access_type, const char *class_name,
303 int kind, const TemplateParameterInfos &infos);
305 clang::ClassTemplateSpecializationDecl *CreateClassTemplateSpecializationDecl(
306 clang::DeclContext *decl_ctx,
307 clang::ClassTemplateDecl *class_template_decl, int kind,
308 const TemplateParameterInfos &infos);
311 CreateClassTemplateSpecializationType(clang::ClassTemplateSpecializationDecl *
312 class_template_specialization_decl);
314 static clang::DeclContext *
315 GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl);
317 static clang::DeclContext *
318 GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl);
320 static bool CheckOverloadedOperatorKindParameterCount(
321 bool is_method, clang::OverloadedOperatorKind op_kind,
322 uint32_t num_params);
324 bool FieldIsBitfield(clang::FieldDecl *field, uint32_t &bitfield_bit_size);
326 static bool FieldIsBitfield(clang::ASTContext *ast, clang::FieldDecl *field,
327 uint32_t &bitfield_bit_size);
329 static bool RecordHasFields(const clang::RecordDecl *record_decl);
331 CompilerType CreateObjCClass(const char *name, clang::DeclContext *decl_ctx,
332 bool isForwardDecl, bool isInternal,
333 ClangASTMetadata *metadata = nullptr);
335 bool SetTagTypeKind(clang::QualType type, int kind) const;
337 bool SetDefaultAccessForRecordFields(clang::RecordDecl *record_decl,
338 int default_accessibility,
339 int *assigned_accessibilities,
340 size_t num_assigned_accessibilities);
342 // Returns a mask containing bits from the ClangASTContext::eTypeXXX
345 //------------------------------------------------------------------
346 // Namespace Declarations
347 //------------------------------------------------------------------
349 clang::NamespaceDecl *
350 GetUniqueNamespaceDeclaration(const char *name, clang::DeclContext *decl_ctx);
352 static clang::NamespaceDecl *
353 GetUniqueNamespaceDeclaration(clang::ASTContext *ast, const char *name,
354 clang::DeclContext *decl_ctx);
356 //------------------------------------------------------------------
358 //------------------------------------------------------------------
360 clang::FunctionDecl *
361 CreateFunctionDeclaration(clang::DeclContext *decl_ctx, const char *name,
362 const CompilerType &function_Type, int storage,
365 static CompilerType CreateFunctionType(clang::ASTContext *ast,
366 const CompilerType &result_type,
367 const CompilerType *args,
368 unsigned num_args, bool is_variadic,
369 unsigned type_quals);
371 CompilerType CreateFunctionType(const CompilerType &result_type,
372 const CompilerType *args, unsigned num_args,
373 bool is_variadic, unsigned type_quals) {
374 return ClangASTContext::CreateFunctionType(
375 getASTContext(), result_type, args, num_args, is_variadic, type_quals);
378 clang::ParmVarDecl *CreateParameterDeclaration(const char *name,
379 const CompilerType ¶m_type,
382 void SetFunctionParameters(clang::FunctionDecl *function_decl,
383 clang::ParmVarDecl **params, unsigned num_params);
385 CompilerType CreateBlockPointerType(const CompilerType &function_type);
387 //------------------------------------------------------------------
389 //------------------------------------------------------------------
391 CompilerType CreateArrayType(const CompilerType &element_type,
392 size_t element_count, bool is_vector);
394 //------------------------------------------------------------------
396 //------------------------------------------------------------------
397 CompilerType CreateEnumerationType(const char *name,
398 clang::DeclContext *decl_ctx,
399 const Declaration &decl,
400 const CompilerType &integer_qual_type);
402 //------------------------------------------------------------------
403 // Integer type functions
404 //------------------------------------------------------------------
406 static CompilerType GetIntTypeFromBitSize(clang::ASTContext *ast,
407 size_t bit_size, bool is_signed);
409 CompilerType GetPointerSizedIntType(bool is_signed) {
410 return GetPointerSizedIntType(getASTContext(), is_signed);
413 static CompilerType GetPointerSizedIntType(clang::ASTContext *ast,
416 //------------------------------------------------------------------
417 // Floating point functions
418 //------------------------------------------------------------------
420 static CompilerType GetFloatTypeFromBitSize(clang::ASTContext *ast,
423 //------------------------------------------------------------------
424 // TypeSystem methods
425 //------------------------------------------------------------------
426 DWARFASTParser *GetDWARFParser() override;
427 //PDBASTParser *GetPDBParser();
429 //------------------------------------------------------------------
430 // ClangASTContext callbacks for external source lookups.
431 //------------------------------------------------------------------
432 static void CompleteTagDecl(void *baton, clang::TagDecl *);
434 static void CompleteObjCInterfaceDecl(void *baton,
435 clang::ObjCInterfaceDecl *);
437 static bool LayoutRecordType(
438 void *baton, const clang::RecordDecl *record_decl, uint64_t &size,
440 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
441 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
443 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
446 //----------------------------------------------------------------------
447 // CompilerDecl override functions
448 //----------------------------------------------------------------------
449 ConstString DeclGetName(void *opaque_decl) override;
451 ConstString DeclGetMangledName(void *opaque_decl) override;
453 CompilerDeclContext DeclGetDeclContext(void *opaque_decl) override;
455 CompilerType DeclGetFunctionReturnType(void *opaque_decl) override;
457 size_t DeclGetFunctionNumArguments(void *opaque_decl) override;
459 CompilerType DeclGetFunctionArgumentType(void *opaque_decl,
460 size_t arg_idx) override;
462 //----------------------------------------------------------------------
463 // CompilerDeclContext override functions
464 //----------------------------------------------------------------------
466 std::vector<CompilerDecl>
467 DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name,
468 const bool ignore_using_decls) override;
470 bool DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) override;
472 ConstString DeclContextGetName(void *opaque_decl_ctx) override;
474 ConstString DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) override;
476 bool DeclContextIsClassMethod(void *opaque_decl_ctx,
477 lldb::LanguageType *language_ptr,
478 bool *is_instance_method_ptr,
479 ConstString *language_object_name_ptr) override;
481 //----------------------------------------------------------------------
482 // Clang specific clang::DeclContext functions
483 //----------------------------------------------------------------------
485 static clang::DeclContext *
486 DeclContextGetAsDeclContext(const CompilerDeclContext &dc);
488 static clang::ObjCMethodDecl *
489 DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc);
491 static clang::CXXMethodDecl *
492 DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc);
494 static clang::FunctionDecl *
495 DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc);
497 static clang::NamespaceDecl *
498 DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc);
500 static ClangASTMetadata *DeclContextGetMetaData(const CompilerDeclContext &dc,
503 static clang::ASTContext *
504 DeclContextGetClangASTContext(const CompilerDeclContext &dc);
506 //----------------------------------------------------------------------
508 //----------------------------------------------------------------------
510 bool IsArrayType(lldb::opaque_compiler_type_t type,
511 CompilerType *element_type, uint64_t *size,
512 bool *is_incomplete) override;
514 bool IsVectorType(lldb::opaque_compiler_type_t type,
515 CompilerType *element_type, uint64_t *size) override;
517 bool IsAggregateType(lldb::opaque_compiler_type_t type) override;
519 bool IsAnonymousType(lldb::opaque_compiler_type_t type) override;
521 bool IsBeingDefined(lldb::opaque_compiler_type_t type) override;
523 bool IsCharType(lldb::opaque_compiler_type_t type) override;
525 bool IsCompleteType(lldb::opaque_compiler_type_t type) override;
527 bool IsConst(lldb::opaque_compiler_type_t type) override;
529 bool IsCStringType(lldb::opaque_compiler_type_t type,
530 uint32_t &length) override;
532 static bool IsCXXClassType(const CompilerType &type);
534 bool IsDefined(lldb::opaque_compiler_type_t type) override;
536 bool IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count,
537 bool &is_complex) override;
539 bool IsFunctionType(lldb::opaque_compiler_type_t type,
540 bool *is_variadic_ptr) override;
542 uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
543 CompilerType *base_type_ptr) override;
546 GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) override;
548 CompilerType GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
549 const size_t index) override;
551 bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) override;
553 bool IsBlockPointerType(lldb::opaque_compiler_type_t type,
554 CompilerType *function_pointer_type_ptr) override;
556 bool IsIntegerType(lldb::opaque_compiler_type_t type,
557 bool &is_signed) override;
559 bool IsEnumerationType(lldb::opaque_compiler_type_t type,
560 bool &is_signed) override;
562 static bool IsObjCClassType(const CompilerType &type);
564 static bool IsObjCClassTypeAndHasIVars(const CompilerType &type,
565 bool check_superclass);
567 static bool IsObjCObjectOrInterfaceType(const CompilerType &type);
569 static bool IsObjCObjectPointerType(const CompilerType &type,
570 CompilerType *target_type = nullptr);
572 bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) override;
574 static bool IsClassType(lldb::opaque_compiler_type_t type);
576 static bool IsEnumType(lldb::opaque_compiler_type_t type);
578 bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
579 CompilerType *target_type, // Can pass nullptr
580 bool check_cplusplus, bool check_objc) override;
582 bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) override;
584 bool IsPointerType(lldb::opaque_compiler_type_t type,
585 CompilerType *pointee_type) override;
587 bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type,
588 CompilerType *pointee_type) override;
590 bool IsReferenceType(lldb::opaque_compiler_type_t type,
591 CompilerType *pointee_type, bool *is_rvalue) override;
593 bool IsScalarType(lldb::opaque_compiler_type_t type) override;
595 bool IsTypedefType(lldb::opaque_compiler_type_t type) override;
597 bool IsVoidType(lldb::opaque_compiler_type_t type) override;
599 bool SupportsLanguage(lldb::LanguageType language) override;
601 static bool GetCXXClassName(const CompilerType &type,
602 std::string &class_name);
604 static bool GetObjCClassName(const CompilerType &type,
605 std::string &class_name);
607 //----------------------------------------------------------------------
609 //----------------------------------------------------------------------
611 bool GetCompleteType(lldb::opaque_compiler_type_t type) override;
613 //----------------------------------------------------------------------
615 //----------------------------------------------------------------------
617 ConstString GetTypeName(lldb::opaque_compiler_type_t type) override;
619 uint32_t GetTypeInfo(lldb::opaque_compiler_type_t type,
620 CompilerType *pointee_or_element_compiler_type) override;
623 GetMinimumLanguage(lldb::opaque_compiler_type_t type) override;
625 lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) override;
627 unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) override;
629 //----------------------------------------------------------------------
630 // Creating related types
631 //----------------------------------------------------------------------
633 // Using the current type, create a new typedef to that type using
635 // as the name and "decl_ctx" as the decl context.
637 CreateTypedefType(const CompilerType &type, const char *typedef_name,
638 const CompilerDeclContext &compiler_decl_ctx);
640 CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type,
641 uint64_t *stride) override;
643 CompilerType GetArrayType(lldb::opaque_compiler_type_t type,
644 uint64_t size) override;
646 CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) override;
649 GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) override;
651 // Returns -1 if this isn't a function of if the function doesn't have a
653 // Returns a value >= 0 if there is a prototype.
654 int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) override;
656 CompilerType GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type,
657 size_t idx) override;
660 GetFunctionReturnType(lldb::opaque_compiler_type_t type) override;
662 size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) override;
664 TypeMemberFunctionImpl
665 GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
666 size_t idx) override;
668 CompilerType GetNonReferenceType(lldb::opaque_compiler_type_t type) override;
670 CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) override;
672 CompilerType GetPointerType(lldb::opaque_compiler_type_t type) override;
675 GetLValueReferenceType(lldb::opaque_compiler_type_t type) override;
678 GetRValueReferenceType(lldb::opaque_compiler_type_t type) override;
680 CompilerType AddConstModifier(lldb::opaque_compiler_type_t type) override;
682 CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type) override;
684 CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type) override;
686 CompilerType CreateTypedef(lldb::opaque_compiler_type_t type,
688 const CompilerDeclContext &decl_ctx) override;
690 // If the current object represents a typedef type, get the underlying type
691 CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) override;
693 //----------------------------------------------------------------------
694 // Create related types using the current type's AST
695 //----------------------------------------------------------------------
696 CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) override;
698 //----------------------------------------------------------------------
699 // Exploring the type
700 //----------------------------------------------------------------------
702 uint64_t GetByteSize(lldb::opaque_compiler_type_t type,
703 ExecutionContextScope *exe_scope) {
704 return (GetBitSize(type, exe_scope) + 7) / 8;
707 uint64_t GetBitSize(lldb::opaque_compiler_type_t type,
708 ExecutionContextScope *exe_scope) override;
710 lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type,
711 uint64_t &count) override;
713 lldb::Format GetFormat(lldb::opaque_compiler_type_t type) override;
715 size_t GetTypeBitAlign(lldb::opaque_compiler_type_t type) override;
717 uint32_t GetNumChildren(lldb::opaque_compiler_type_t type,
718 bool omit_empty_base_classes) override;
720 CompilerType GetBuiltinTypeByName(const ConstString &name) override;
723 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) override;
725 static lldb::BasicType
726 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type,
727 const ConstString &name);
729 void ForEachEnumerator(
730 lldb::opaque_compiler_type_t type,
731 std::function<bool(const CompilerType &integer_type,
732 const ConstString &name,
733 const llvm::APSInt &value)> const &callback) override;
735 uint32_t GetNumFields(lldb::opaque_compiler_type_t type) override;
737 CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
738 std::string &name, uint64_t *bit_offset_ptr,
739 uint32_t *bitfield_bit_size_ptr,
740 bool *is_bitfield_ptr) override;
742 uint32_t GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) override;
744 uint32_t GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) override;
746 CompilerType GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type,
748 uint32_t *bit_offset_ptr) override;
750 CompilerType GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type,
752 uint32_t *bit_offset_ptr) override;
754 static uint32_t GetNumPointeeChildren(clang::QualType type);
756 CompilerType GetChildCompilerTypeAtIndex(
757 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
758 bool transparent_pointers, bool omit_empty_base_classes,
759 bool ignore_array_bounds, std::string &child_name,
760 uint32_t &child_byte_size, int32_t &child_byte_offset,
761 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
762 bool &child_is_base_class, bool &child_is_deref_of_parent,
763 ValueObject *valobj, uint64_t &language_flags) override;
765 // Lookup a child given a name. This function will match base class names
766 // and member member names in "clang_type" only, not descendants.
767 uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
769 bool omit_empty_base_classes) override;
771 // Lookup a child member given a name. This function will match member names
772 // only and will descend into "clang_type" children in search for the first
773 // member in this class, or any base class that matches "name".
774 // TODO: Return all matches for a given name by returning a
775 // vector<vector<uint32_t>>
776 // so we catch all names that match a given child name, not just the first.
778 GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type,
779 const char *name, bool omit_empty_base_classes,
780 std::vector<uint32_t> &child_indexes) override;
782 size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type) override;
784 CompilerType GetTemplateArgument(lldb::opaque_compiler_type_t type,
786 lldb::TemplateArgumentKind &kind) override;
788 CompilerType GetTypeForFormatters(void *type) override;
790 #define LLDB_INVALID_DECL_LEVEL UINT32_MAX
791 // LLDB_INVALID_DECL_LEVEL is returned by CountDeclLevels if
792 // child_decl_ctx could not be found in decl_ctx.
793 uint32_t CountDeclLevels(clang::DeclContext *frame_decl_ctx,
794 clang::DeclContext *child_decl_ctx,
795 ConstString *child_name = nullptr,
796 CompilerType *child_type = nullptr);
798 //----------------------------------------------------------------------
799 // Modifying RecordType
800 //----------------------------------------------------------------------
801 static clang::FieldDecl *AddFieldToRecordType(const CompilerType &type,
803 const CompilerType &field_type,
804 lldb::AccessType access,
805 uint32_t bitfield_bit_size);
807 static void BuildIndirectFields(const CompilerType &type);
809 static void SetIsPacked(const CompilerType &type);
811 static clang::VarDecl *AddVariableToRecordType(const CompilerType &type,
813 const CompilerType &var_type,
814 lldb::AccessType access);
816 clang::CXXMethodDecl *
817 AddMethodToCXXRecordType(lldb::opaque_compiler_type_t type, const char *name,
818 const CompilerType &method_type,
819 lldb::AccessType access, bool is_virtual,
820 bool is_static, bool is_inline, bool is_explicit,
821 bool is_attr_used, bool is_artificial);
824 clang::CXXBaseSpecifier *
825 CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
826 lldb::AccessType access, bool is_virtual,
829 static void DeleteBaseClassSpecifiers(clang::CXXBaseSpecifier **base_classes,
830 unsigned num_base_classes);
833 SetBaseClassesForClassType(lldb::opaque_compiler_type_t type,
834 clang::CXXBaseSpecifier const *const *base_classes,
835 unsigned num_base_classes);
837 static bool SetObjCSuperClass(const CompilerType &type,
838 const CompilerType &superclass_compiler_type);
840 static bool AddObjCClassProperty(const CompilerType &type,
841 const char *property_name,
842 const CompilerType &property_compiler_type,
843 clang::ObjCIvarDecl *ivar_decl,
844 const char *property_setter_name,
845 const char *property_getter_name,
846 uint32_t property_attributes,
847 ClangASTMetadata *metadata);
849 static clang::ObjCMethodDecl *AddMethodToObjCObjectType(
850 const CompilerType &type,
851 const char *name, // the full symbol name as seen in the symbol table
852 // (lldb::opaque_compiler_type_t type, "-[NString
853 // stringWithCString:]")
854 const CompilerType &method_compiler_type, lldb::AccessType access,
855 bool is_artificial, bool is_variadic);
857 static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type,
860 static bool GetHasExternalStorage(const CompilerType &type);
861 //------------------------------------------------------------------
863 //------------------------------------------------------------------
864 static bool StartTagDeclarationDefinition(const CompilerType &type);
866 static bool CompleteTagDeclarationDefinition(const CompilerType &type);
868 //----------------------------------------------------------------------
869 // Modifying Enumeration types
870 //----------------------------------------------------------------------
871 bool AddEnumerationValueToEnumerationType(
872 lldb::opaque_compiler_type_t type,
873 const CompilerType &enumerator_qual_type, const Declaration &decl,
874 const char *name, int64_t enum_value, uint32_t enum_value_bit_size);
876 CompilerType GetEnumerationIntegerType(lldb::opaque_compiler_type_t type);
878 //------------------------------------------------------------------
879 // Pointers & References
880 //------------------------------------------------------------------
882 // Call this function using the class type when you want to make a
883 // member pointer type to pointee_type.
884 static CompilerType CreateMemberPointerType(const CompilerType &type,
885 const CompilerType &pointee_type);
887 // Converts "s" to a floating point value and place resulting floating
888 // point bytes in the "dst" buffer.
889 size_t ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
890 const char *s, uint8_t *dst,
891 size_t dst_size) override;
893 //----------------------------------------------------------------------
895 //----------------------------------------------------------------------
896 void DumpValue(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
897 Stream *s, lldb::Format format, const DataExtractor &data,
898 lldb::offset_t data_offset, size_t data_byte_size,
899 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
900 bool show_types, bool show_summary, bool verbose,
901 uint32_t depth) override;
903 bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s,
904 lldb::Format format, const DataExtractor &data,
905 lldb::offset_t data_offset, size_t data_byte_size,
906 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
907 ExecutionContextScope *exe_scope) override;
909 void DumpSummary(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
910 Stream *s, const DataExtractor &data,
911 lldb::offset_t data_offset, size_t data_byte_size) override;
913 void DumpTypeDescription(
914 lldb::opaque_compiler_type_t type) override; // Dump to stdout
916 void DumpTypeDescription(lldb::opaque_compiler_type_t type,
919 static void DumpTypeName(const CompilerType &type);
921 static clang::EnumDecl *GetAsEnumDecl(const CompilerType &type);
923 static clang::RecordDecl *GetAsRecordDecl(const CompilerType &type);
925 static clang::TagDecl *GetAsTagDecl(const CompilerType &type);
927 clang::CXXRecordDecl *GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type);
929 static clang::ObjCInterfaceDecl *
930 GetAsObjCInterfaceDecl(const CompilerType &type);
932 clang::ClassTemplateDecl *ParseClassTemplateDecl(
933 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
934 const char *parent_name, int tag_decl_kind,
935 const ClangASTContext::TemplateParameterInfos &template_param_infos);
937 clang::BlockDecl *CreateBlockDeclaration(clang::DeclContext *ctx);
939 clang::UsingDirectiveDecl *
940 CreateUsingDirectiveDeclaration(clang::DeclContext *decl_ctx,
941 clang::NamespaceDecl *ns_decl);
943 clang::UsingDecl *CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
944 clang::NamedDecl *target);
946 clang::VarDecl *CreateVariableDeclaration(clang::DeclContext *decl_context,
948 clang::QualType type);
950 static lldb::opaque_compiler_type_t
951 GetOpaqueCompilerType(clang::ASTContext *ast, lldb::BasicType basic_type);
953 static clang::QualType GetQualType(lldb::opaque_compiler_type_t type) {
955 return clang::QualType::getFromOpaquePtr(type);
956 return clang::QualType();
959 static clang::QualType
960 GetCanonicalQualType(lldb::opaque_compiler_type_t type) {
962 return clang::QualType::getFromOpaquePtr(type).getCanonicalType();
963 return clang::QualType();
966 clang::DeclarationName
967 GetDeclarationName(const char *name, const CompilerType &function_clang_type);
970 //------------------------------------------------------------------
971 // Classes that inherit from ClangASTContext can see and modify these
972 //------------------------------------------------------------------
974 std::string m_target_triple;
975 std::unique_ptr<clang::ASTContext> m_ast_ap;
976 std::unique_ptr<clang::LangOptions> m_language_options_ap;
977 std::unique_ptr<clang::FileManager> m_file_manager_ap;
978 std::unique_ptr<clang::FileSystemOptions> m_file_system_options_ap;
979 std::unique_ptr<clang::SourceManager> m_source_manager_ap;
980 std::unique_ptr<clang::DiagnosticsEngine> m_diagnostics_engine_ap;
981 std::unique_ptr<clang::DiagnosticConsumer> m_diagnostic_consumer_ap;
982 std::shared_ptr<clang::TargetOptions> m_target_options_rp;
983 std::unique_ptr<clang::TargetInfo> m_target_info_ap;
984 std::unique_ptr<clang::IdentifierTable> m_identifier_table_ap;
985 std::unique_ptr<clang::SelectorTable> m_selector_table_ap;
986 std::unique_ptr<clang::Builtin::Context> m_builtins_ap;
987 std::unique_ptr<DWARFASTParserClang> m_dwarf_ast_parser_ap;
988 // std::unique_ptr<PDBASTParser> m_pdb_ast_parser_ap;
989 std::unique_ptr<ClangASTSource> m_scratch_ast_source_ap;
990 std::unique_ptr<clang::MangleContext> m_mangle_ctx_ap;
991 CompleteTagDeclCallback m_callback_tag_decl;
992 CompleteObjCInterfaceDeclCallback m_callback_objc_decl;
993 void * m_callback_baton;
994 uint32_t m_pointer_byte_size;
996 bool m_can_evaluate_expressions;
999 //------------------------------------------------------------------
1000 // For ClangASTContext only
1001 //------------------------------------------------------------------
1002 ClangASTContext(const ClangASTContext &);
1003 const ClangASTContext &operator=(const ClangASTContext &);
1006 class ClangASTContextForExpressions : public ClangASTContext {
1008 ClangASTContextForExpressions(Target &target);
1010 ~ClangASTContextForExpressions() override = default;
1013 GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix,
1014 lldb::LanguageType language,
1015 Expression::ResultType desired_type,
1016 const EvaluateExpressionOptions &options) override;
1018 FunctionCaller *GetFunctionCaller(const CompilerType &return_type,
1019 const Address &function_address,
1020 const ValueList &arg_value_list,
1021 const char *name) override;
1023 UtilityFunction *GetUtilityFunction(const char *text,
1024 const char *name) override;
1026 PersistentExpressionState *GetPersistentExpressionState() override;
1029 lldb::TargetWP m_target_wp;
1030 lldb::ClangPersistentVariablesUP m_persistent_variables; ///< These are the
1039 } // namespace lldb_private
1041 #endif // liblldb_ClangASTContext_h_