1 //===-- ClangASTType.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_ClangASTType_h_
11 #define liblldb_ClangASTType_h_
14 #include "lldb/lldb-private.h"
15 #include "lldb/Core/ClangForward.h"
16 #include "clang/AST/Type.h"
18 namespace lldb_private {
20 //----------------------------------------------------------------------
21 // A class that can carry around a clang ASTContext and a opaque clang
22 // QualType. A clang::QualType can be easily reconstructed from an
23 // opaque clang type and often the ASTContext is needed when doing
24 // various type related tasks, so this class allows both items to travel
25 // in a single very lightweight class that can be used. There are many
26 // static equivalents of the member functions that allow the ASTContext
27 // and the opaque clang QualType to be specified for ease of use and
28 // to avoid code duplication.
29 //----------------------------------------------------------------------
34 eTypeHasChildren = (1u << 0),
35 eTypeHasValue = (1u << 1),
36 eTypeIsArray = (1u << 2),
37 eTypeIsBlock = (1u << 3),
38 eTypeIsBuiltIn = (1u << 4),
39 eTypeIsClass = (1u << 5),
40 eTypeIsCPlusPlus = (1u << 6),
41 eTypeIsEnumeration = (1u << 7),
42 eTypeIsFuncPrototype = (1u << 8),
43 eTypeIsMember = (1u << 9),
44 eTypeIsObjC = (1u << 10),
45 eTypeIsPointer = (1u << 11),
46 eTypeIsReference = (1u << 12),
47 eTypeIsStructUnion = (1u << 13),
48 eTypeIsTemplate = (1u << 14),
49 eTypeIsTypedef = (1u << 15),
50 eTypeIsVector = (1u << 16),
51 eTypeIsScalar = (1u << 17),
52 eTypeIsInteger = (1u << 18),
53 eTypeIsFloat = (1u << 19),
54 eTypeIsComplex = (1u << 20),
55 eTypeIsSigned = (1u << 21)
59 //----------------------------------------------------------------------
60 // Constructors and Destructors
61 //----------------------------------------------------------------------
62 ClangASTType (clang::ASTContext *ast_context, lldb::clang_type_t type) :
68 ClangASTType (clang::ASTContext *ast_context, clang::QualType qual_type);
70 ClangASTType (const ClangASTType &rhs) :
84 //----------------------------------------------------------------------
86 //----------------------------------------------------------------------
89 operator= (const ClangASTType &rhs)
97 //----------------------------------------------------------------------
99 //----------------------------------------------------------------------
101 explicit operator bool () const
103 return m_type != NULL && m_ast != NULL;
107 operator < (const ClangASTType &rhs) const
109 if (m_ast == rhs.m_ast)
110 return m_type < rhs.m_type;
111 return m_ast < rhs.m_ast;
117 return m_type != NULL && m_ast != NULL;
121 IsArrayType (ClangASTType *element_type,
123 bool *is_incomplete) const;
126 IsArrayOfScalarType () const;
129 IsAggregateType () const;
132 IsBeingDefined () const;
138 IsCompleteType () const;
144 IsCStringType (uint32_t &length) const;
147 IsCXXClassType () const;
153 IsFloatingPointType (uint32_t &count, bool &is_complex) const;
156 IsFunctionType (bool *is_variadic_ptr = NULL) const;
159 IsHomogeneousAggregate (ClangASTType* base_type_ptr) const;
162 GetNumberOfFunctionArguments () const;
165 GetFunctionArgumentAtIndex (const size_t index);
168 IsVariadicFunctionType () const;
171 IsFunctionPointerType () const;
174 IsIntegerType (bool &is_signed) const;
177 IsObjCClassType () const;
180 IsObjCClassTypeAndHasIVars (bool check_superclass) const;
183 IsObjCObjectOrInterfaceType () const;
186 IsObjCObjectPointerType (ClangASTType *target_type = NULL);
189 IsPolymorphicClass () const;
192 IsPossibleCPlusPlusDynamicType (ClangASTType *target_type = NULL) const
194 return IsPossibleDynamicType (target_type, true, false);
198 IsPossibleDynamicType (ClangASTType *target_type, // Can pass NULL
199 bool check_cplusplus,
200 bool check_objc) const;
204 IsPointerToScalarType () const;
207 IsRuntimeGeneratedType () const;
210 IsPointerType (ClangASTType *pointee_type = NULL) const;
213 IsPointerOrReferenceType (ClangASTType *pointee_type = NULL) const;
216 IsReferenceType (ClangASTType *pointee_type = nullptr, bool* is_rvalue = nullptr) const;
219 IsScalarType () const;
222 IsTypedefType () const;
228 GetCXXClassName (std::string &class_name) const;
231 GetObjCClassName (std::string &class_name);
234 //----------------------------------------------------------------------
236 //----------------------------------------------------------------------
239 GetCompleteType () const;
241 //----------------------------------------------------------------------
242 // AST related queries
243 //----------------------------------------------------------------------
246 GetPointerByteSize () const;
248 //----------------------------------------------------------------------
250 //----------------------------------------------------------------------
253 GetASTContext() const
259 GetConstQualifiedTypeName () const;
262 GetConstTypeName () const;
265 GetTypeName () const;
268 GetDisplayTypeName () const;
271 GetTypeInfo (ClangASTType *pointee_or_element_clang_type = NULL) const;
274 GetMinimumLanguage ();
277 GetOpaqueQualType() const
283 GetTypeClass () const;
286 SetClangType (clang::ASTContext *ast, lldb::clang_type_t type)
293 SetClangType (clang::ASTContext *ast, clang::QualType qual_type);
296 GetTypeQualifiers() const;
298 //----------------------------------------------------------------------
299 // Creating related types
300 //----------------------------------------------------------------------
303 AddConstModifier () const;
306 AddRestrictModifier () const;
309 AddVolatileModifier () const;
311 // Using the current type, create a new typedef to that type using "typedef_name"
312 // as the name and "decl_ctx" as the decl context.
314 CreateTypedefType (const char *typedef_name,
315 clang::DeclContext *decl_ctx) const;
318 GetArrayElementType (uint64_t *stride = nullptr) const;
321 GetCanonicalType () const;
324 GetFullyUnqualifiedType () const;
326 // Returns -1 if this isn't a function of if the function doesn't have a prototype
327 // Returns a value >= 0 if there is a prototype.
329 GetFunctionArgumentCount () const;
332 GetFunctionArgumentTypeAtIndex (size_t idx);
335 GetFunctionReturnType () const;
338 GetLValueReferenceType () const;
341 GetNonReferenceType () const;
344 GetPointeeType () const;
347 GetPointerType () const;
350 GetRValueReferenceType () const;
352 // If the current object represents a typedef type, get the underlying type
354 GetTypedefedType () const;
357 RemoveFastQualifiers () const;
359 //----------------------------------------------------------------------
360 // Create related types using the current type's AST
361 //----------------------------------------------------------------------
363 GetBasicTypeFromAST (lldb::BasicType basic_type) const;
365 //----------------------------------------------------------------------
366 // Exploring the type
367 //----------------------------------------------------------------------
370 GetByteSize () const;
376 GetEncoding (uint64_t &count) const;
382 GetTypeBitAlign () const;
385 GetNumChildren (bool omit_empty_base_classes) const;
388 GetBasicTypeEnumeration () const;
390 static lldb::BasicType
391 GetBasicTypeEnumeration (const ConstString &name);
394 GetNumDirectBaseClasses () const;
397 GetNumVirtualBaseClasses () const;
400 GetNumFields () const;
403 GetDirectBaseClassAtIndex (size_t idx,
404 uint32_t *bit_offset_ptr) const;
407 GetVirtualBaseClassAtIndex (size_t idx,
408 uint32_t *bit_offset_ptr) const;
411 GetFieldAtIndex (size_t idx,
413 uint64_t *bit_offset_ptr,
414 uint32_t *bitfield_bit_size_ptr,
415 bool *is_bitfield_ptr) const;
418 GetIndexOfFieldWithName (const char* name,
419 ClangASTType* field_clang_type = NULL,
420 uint64_t *bit_offset_ptr = NULL,
421 uint32_t *bitfield_bit_size_ptr = NULL,
422 bool *is_bitfield_ptr = NULL) const;
425 GetNumPointeeChildren () const;
428 GetChildClangTypeAtIndex (ExecutionContext *exe_ctx,
430 bool transparent_pointers,
431 bool omit_empty_base_classes,
432 bool ignore_array_bounds,
433 std::string& child_name,
434 uint32_t &child_byte_size,
435 int32_t &child_byte_offset,
436 uint32_t &child_bitfield_bit_size,
437 uint32_t &child_bitfield_bit_offset,
438 bool &child_is_base_class,
439 bool &child_is_deref_of_parent,
440 ValueObject *valobj) const;
442 // Lookup a child given a name. This function will match base class names
443 // and member member names in "clang_type" only, not descendants.
445 GetIndexOfChildWithName (const char *name,
446 bool omit_empty_base_classes) const;
448 // Lookup a child member given a name. This function will match member names
449 // only and will descend into "clang_type" children in search for the first
450 // member in this class, or any base class that matches "name".
451 // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>>
452 // so we catch all names that match a given child name, not just the first.
454 GetIndexOfChildMemberWithName (const char *name,
455 bool omit_empty_base_classes,
456 std::vector<uint32_t>& child_indexes) const;
459 GetNumTemplateArguments () const;
462 GetTemplateArgument (size_t idx,
463 lldb::TemplateArgumentKind &kind) const;
466 //----------------------------------------------------------------------
467 // Modifying RecordType
468 //----------------------------------------------------------------------
470 AddFieldToRecordType (const char *name,
471 const ClangASTType &field_type,
472 lldb::AccessType access,
473 uint32_t bitfield_bit_size);
476 BuildIndirectFields ();
479 AddVariableToRecordType (const char *name,
480 const ClangASTType &var_type,
481 lldb::AccessType access);
483 clang::CXXMethodDecl *
484 AddMethodToCXXRecordType (const char *name,
485 const ClangASTType &method_type,
486 lldb::AccessType access,
495 clang::CXXBaseSpecifier *
496 CreateBaseClassSpecifier (lldb::AccessType access,
501 DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes,
502 unsigned num_base_classes);
505 SetBaseClassesForClassType (clang::CXXBaseSpecifier const * const *base_classes,
506 unsigned num_base_classes);
510 SetObjCSuperClass (const ClangASTType &superclass_clang_type);
513 AddObjCClassProperty (const char *property_name,
514 const ClangASTType &property_clang_type,
515 clang::ObjCIvarDecl *ivar_decl,
516 const char *property_setter_name,
517 const char *property_getter_name,
518 uint32_t property_attributes,
519 ClangASTMetadata *metadata);
521 clang::ObjCMethodDecl *
522 AddMethodToObjCObjectType (const char *name, // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
523 const ClangASTType &method_clang_type,
524 lldb::AccessType access,
528 GetDeclContextForType () const;
532 SetDefaultAccessForRecordFields (int default_accessibility,
533 int *assigned_accessibilities,
534 size_t num_assigned_accessibilities);
537 SetHasExternalStorage (bool has_extern);
540 //------------------------------------------------------------------
542 //------------------------------------------------------------------
545 SetTagTypeKind (int kind) const;
547 //------------------------------------------------------------------
549 //------------------------------------------------------------------
551 StartTagDeclarationDefinition ();
554 CompleteTagDeclarationDefinition ();
556 //----------------------------------------------------------------------
557 // Modifying Enumeration types
558 //----------------------------------------------------------------------
560 AddEnumerationValueToEnumerationType (const ClangASTType &enumerator_qual_type,
561 const Declaration &decl,
564 uint32_t enum_value_bit_size);
569 GetEnumerationIntegerType () const;
572 //------------------------------------------------------------------
573 // Pointers & References
574 //------------------------------------------------------------------
576 // Call this function using the class type when you want to make a
577 // member pointer type to pointee_type.
579 CreateMemberPointerType (const ClangASTType &pointee_type) const;
582 // Converts "s" to a floating point value and place resulting floating
583 // point bytes in the "dst" buffer.
585 ConvertStringToFloatValue (const char *s,
587 size_t dst_size) const;
588 //----------------------------------------------------------------------
590 //----------------------------------------------------------------------
592 DumpValue (ExecutionContext *exe_ctx,
595 const DataExtractor &data,
596 lldb::offset_t data_offset,
597 size_t data_byte_size,
598 uint32_t bitfield_bit_size,
599 uint32_t bitfield_bit_offset,
606 DumpTypeValue (Stream *s,
608 const DataExtractor &data,
609 lldb::offset_t data_offset,
610 size_t data_byte_size,
611 uint32_t bitfield_bit_size,
612 uint32_t bitfield_bit_offset,
613 ExecutionContextScope *exe_scope);
616 DumpSummary (ExecutionContext *exe_ctx,
618 const DataExtractor &data,
619 lldb::offset_t data_offset,
620 size_t data_byte_size);
623 DumpTypeDescription () const; // Dump to stdout
626 DumpTypeDescription (Stream *s) const;
629 GetValueAsScalar (const DataExtractor &data,
630 lldb::offset_t data_offset,
631 size_t data_byte_size,
632 Scalar &value) const;
635 SetValueFromScalar (const Scalar &value,
639 ReadFromMemory (ExecutionContext *exe_ctx,
641 AddressType address_type,
642 DataExtractor &data);
645 WriteToMemory (ExecutionContext *exe_ctx,
647 AddressType address_type,
648 StreamString &new_value);
651 GetAsEnumDecl () const;
655 GetAsRecordDecl () const;
657 clang::CXXRecordDecl *
658 GetAsCXXRecordDecl () const;
660 clang::ObjCInterfaceDecl *
661 GetAsObjCInterfaceDecl () const;
674 return clang::QualType::getFromOpaquePtr(m_type);
675 return clang::QualType();
678 GetCanonicalQualType () const
681 return clang::QualType::getFromOpaquePtr(m_type).getCanonicalType();
682 return clang::QualType();
686 lldb::clang_type_t m_type;
687 clang::ASTContext *m_ast;
691 bool operator == (const ClangASTType &lhs, const ClangASTType &rhs);
692 bool operator != (const ClangASTType &lhs, const ClangASTType &rhs);
695 } // namespace lldb_private
697 #endif // #ifndef liblldb_ClangASTType_h_