CompilerType.h revision 321369
1192625Sedwin//===-- CompilerType.h ------------------------------------------*- C++ -*-===// 2192625Sedwin// 3192625Sedwin// The LLVM Compiler Infrastructure 4192625Sedwin// 52702Swollman// This file is distributed under the University of Illinois Open Source 6214411Sedwin// License. See LICENSE.TXT for details. 7192625Sedwin// 830829Scharnier//===----------------------------------------------------------------------===// 930829Scharnier 1050479Speter#ifndef liblldb_CompilerType_h_ 1130829Scharnier#define liblldb_CompilerType_h_ 1230829Scharnier 132702Swollman// C Includes 142702Swollman// C++ Includes 1530829Scharnier#include <functional> 1630829Scharnier#include <string> 179937Swollman#include <vector> 189937Swollman 199937Swollman// Other libraries and framework includes 202702Swollman// Project includes 21192625Sedwin#include "lldb/Core/ClangForward.h" 22192625Sedwin#include "lldb/lldb-private.h" 23192625Sedwin 24192625Sedwinnamespace lldb_private { 25192625Sedwin 26192625Sedwinclass DataExtractor; 27192625Sedwin 28192625Sedwin//---------------------------------------------------------------------- 29130819Sstefanf// A class that can carry around a clang ASTContext and a opaque clang 30130819Sstefanf// QualType. A clang::QualType can be easily reconstructed from an 3117214Swollman// opaque clang type and often the ASTContext is needed when doing 3217214Swollman// various type related tasks, so this class allows both items to travel 33192625Sedwin// in a single very lightweight class that can be used. There are many 3417214Swollman// static equivalents of the member functions that allow the ASTContext 3569315Scharnier// and the opaque clang QualType to be specified for ease of use and 3617214Swollman// to avoid code duplication. 3717214Swollman//---------------------------------------------------------------------- 3817214Swollmanclass CompilerType { 3917214Swollmanpublic: 4017214Swollman //---------------------------------------------------------------------- 4117214Swollman // Constructors and Destructors 4217214Swollman //---------------------------------------------------------------------- 4317214Swollman CompilerType(TypeSystem *type_system, lldb::opaque_compiler_type_t type); 44192625Sedwin CompilerType(clang::ASTContext *ast_context, clang::QualType qual_type); 45192625Sedwin 46192625Sedwin CompilerType(const CompilerType &rhs) 47192625Sedwin : m_type(rhs.m_type), m_type_system(rhs.m_type_system) {} 48192625Sedwin 492702Swollman CompilerType() : m_type(nullptr), m_type_system(nullptr) {} 502702Swollman 512702Swollman ~CompilerType(); 522702Swollman 532702Swollman //---------------------------------------------------------------------- 542702Swollman // Operators 552702Swollman //---------------------------------------------------------------------- 562702Swollman 57192625Sedwin const CompilerType &operator=(const CompilerType &rhs) { 58192625Sedwin m_type = rhs.m_type; 592702Swollman m_type_system = rhs.m_type_system; 602702Swollman return *this; 612702Swollman } 622702Swollman 632702Swollman //---------------------------------------------------------------------- 642702Swollman // Tests 652702Swollman //---------------------------------------------------------------------- 662702Swollman 672702Swollman explicit operator bool() const { 682702Swollman return m_type != nullptr && m_type_system != nullptr; 699937Swollman } 709937Swollman 712702Swollman bool operator<(const CompilerType &rhs) const { 722702Swollman if (m_type_system == rhs.m_type_system) 732702Swollman return m_type < rhs.m_type; 742702Swollman return m_type_system < rhs.m_type_system; 75192625Sedwin } 762702Swollman 772702Swollman bool IsValid() const { return m_type != nullptr && m_type_system != nullptr; } 782702Swollman 792702Swollman bool IsArrayType(CompilerType *element_type, uint64_t *size, 802702Swollman bool *is_incomplete) const; 812702Swollman 822702Swollman bool IsVectorType(CompilerType *element_type, uint64_t *size) const; 832702Swollman 842702Swollman bool IsArrayOfScalarType() const; 852702Swollman 862702Swollman bool IsAggregateType() const; 872702Swollman 882702Swollman bool IsAnonymousType() const; 892702Swollman 902702Swollman bool IsBeingDefined() const; 912702Swollman 922702Swollman bool IsCharType() const; 932702Swollman 942702Swollman bool IsCompleteType() const; 952702Swollman 962702Swollman bool IsConst() const; 972702Swollman 982702Swollman bool IsCStringType(uint32_t &length) const; 992702Swollman 1002702Swollman bool IsDefined() const; 101192625Sedwin 1022702Swollman bool IsFloatingPointType(uint32_t &count, bool &is_complex) const; 1032702Swollman 104192625Sedwin bool IsFunctionType(bool *is_variadic_ptr = nullptr) const; 105192625Sedwin 106192625Sedwin uint32_t IsHomogeneousAggregate(CompilerType *base_type_ptr) const; 107192625Sedwin 108192625Sedwin size_t GetNumberOfFunctionArguments() const; 109192625Sedwin 110192625Sedwin CompilerType GetFunctionArgumentAtIndex(const size_t index) const; 111192625Sedwin 112192625Sedwin bool IsVariadicFunctionType() const; 113192625Sedwin 114192625Sedwin bool IsFunctionPointerType() const; 115192625Sedwin 116192625Sedwin bool IsBlockPointerType(CompilerType *function_pointer_type_ptr) const; 117192625Sedwin 118192625Sedwin bool IsIntegerType(bool &is_signed) const; 119192625Sedwin 120192625Sedwin bool IsEnumerationType(bool &is_signed) const; 121192625Sedwin 122192625Sedwin bool IsIntegerOrEnumerationType(bool &is_signed) const; 123192625Sedwin 124192625Sedwin bool IsPolymorphicClass() const; 125192625Sedwin 126192625Sedwin bool 127192625Sedwin IsPossibleCPlusPlusDynamicType(CompilerType *target_type = nullptr) const { 128192625Sedwin return IsPossibleDynamicType(target_type, true, false); 129192625Sedwin } 130192625Sedwin 131192625Sedwin bool IsPossibleDynamicType(CompilerType *target_type, // Can pass nullptr 132192625Sedwin bool check_cplusplus, bool check_objc) const; 133192625Sedwin 134192625Sedwin bool IsPointerToScalarType() const; 135192625Sedwin 136192625Sedwin bool IsRuntimeGeneratedType() const; 137192625Sedwin 138192625Sedwin bool IsPointerType(CompilerType *pointee_type = nullptr) const; 139192625Sedwin 140192625Sedwin bool IsPointerOrReferenceType(CompilerType *pointee_type = nullptr) const; 141192625Sedwin 142192625Sedwin bool IsReferenceType(CompilerType *pointee_type = nullptr, 143192625Sedwin bool *is_rvalue = nullptr) const; 144192625Sedwin 1459937Swollman bool ShouldTreatScalarValueAsAddress() const; 1469937Swollman 147192625Sedwin bool IsScalarType() const; 148192625Sedwin 149192625Sedwin bool IsTypedefType() const; 150192625Sedwin 151192625Sedwin bool IsVoidType() const; 152192625Sedwin 153192625Sedwin //---------------------------------------------------------------------- 154192625Sedwin // Type Completion 155192625Sedwin //---------------------------------------------------------------------- 156192625Sedwin 157192625Sedwin bool GetCompleteType() const; 158192625Sedwin 159192625Sedwin //---------------------------------------------------------------------- 1602702Swollman // AST related queries 1612702Swollman //---------------------------------------------------------------------- 1622702Swollman 1632702Swollman size_t GetPointerByteSize() const; 1642702Swollman 165192625Sedwin //---------------------------------------------------------------------- 166192625Sedwin // Accessors 167192625Sedwin //---------------------------------------------------------------------- 1682702Swollman 169192625Sedwin TypeSystem *GetTypeSystem() const { return m_type_system; } 170192625Sedwin 171192625Sedwin ConstString GetConstQualifiedTypeName() const; 1722702Swollman 173192625Sedwin ConstString GetConstTypeName() const; 1742702Swollman 175192625Sedwin ConstString GetTypeName() const; 1762702Swollman 1772702Swollman ConstString GetDisplayTypeName() const; 1782702Swollman 1792702Swollman uint32_t 1802702Swollman GetTypeInfo(CompilerType *pointee_or_element_compiler_type = nullptr) const; 1812702Swollman 1822702Swollman lldb::LanguageType GetMinimumLanguage(); 1832702Swollman 1842702Swollman lldb::opaque_compiler_type_t GetOpaqueQualType() const { return m_type; } 1852702Swollman 1862702Swollman lldb::TypeClass GetTypeClass() const; 1872702Swollman 1882702Swollman void SetCompilerType(TypeSystem *type_system, 1892702Swollman lldb::opaque_compiler_type_t type); 1902702Swollman 1912702Swollman void SetCompilerType(clang::ASTContext *ast, clang::QualType qual_type); 1922702Swollman 1932702Swollman unsigned GetTypeQualifiers() const; 1942702Swollman 1952702Swollman //---------------------------------------------------------------------- 1962702Swollman // Creating related types 1972702Swollman //---------------------------------------------------------------------- 1982702Swollman 1992702Swollman CompilerType GetArrayElementType(uint64_t *stride = nullptr) const; 2002702Swollman 2012702Swollman CompilerType GetArrayType(uint64_t size) const; 2022702Swollman 2032702Swollman CompilerType GetCanonicalType() const; 2042702Swollman 2052702Swollman CompilerType GetFullyUnqualifiedType() const; 2062702Swollman 2072702Swollman // Returns -1 if this isn't a function of if the function doesn't have a 2082702Swollman // prototype 2092702Swollman // Returns a value >= 0 if there is a prototype. 2102702Swollman int GetFunctionArgumentCount() const; 2112702Swollman 2122702Swollman CompilerType GetFunctionArgumentTypeAtIndex(size_t idx) const; 2132702Swollman 2142702Swollman CompilerType GetFunctionReturnType() const; 2152702Swollman 2162702Swollman size_t GetNumMemberFunctions() const; 2172702Swollman 2182702Swollman TypeMemberFunctionImpl GetMemberFunctionAtIndex(size_t idx); 2192702Swollman 2202702Swollman //---------------------------------------------------------------------- 2212702Swollman // If this type is a reference to a type (L value or R value reference), 2222702Swollman // return a new type with the reference removed, else return the current 2232702Swollman // type itself. 2242702Swollman //---------------------------------------------------------------------- 2252702Swollman CompilerType GetNonReferenceType() const; 2262702Swollman 2272702Swollman //---------------------------------------------------------------------- 2282702Swollman // If this type is a pointer type, return the type that the pointer 2292702Swollman // points to, else return an invalid type. 2302702Swollman //---------------------------------------------------------------------- 2312702Swollman CompilerType GetPointeeType() const; 2322702Swollman 2332702Swollman //---------------------------------------------------------------------- 2342702Swollman // Return a new CompilerType that is a pointer to this type 2352702Swollman //---------------------------------------------------------------------- 2362702Swollman CompilerType GetPointerType() const; 2372702Swollman 2382702Swollman //---------------------------------------------------------------------- 2392702Swollman // Return a new CompilerType that is a L value reference to this type if 2402702Swollman // this type is valid and the type system supports L value references, 2412702Swollman // else return an invalid type. 2422702Swollman //---------------------------------------------------------------------- 2432702Swollman CompilerType GetLValueReferenceType() const; 2442702Swollman 2452702Swollman //---------------------------------------------------------------------- 2462702Swollman // Return a new CompilerType that is a R value reference to this type if 2472702Swollman // this type is valid and the type system supports R value references, 2482702Swollman // else return an invalid type. 2492702Swollman //---------------------------------------------------------------------- 2502702Swollman CompilerType GetRValueReferenceType() const; 2512702Swollman 2522702Swollman //---------------------------------------------------------------------- 2532702Swollman // Return a new CompilerType adds a const modifier to this type if 2542702Swollman // this type is valid and the type system supports const modifiers, 2552702Swollman // else return an invalid type. 2562702Swollman //---------------------------------------------------------------------- 2572702Swollman CompilerType AddConstModifier() const; 2582702Swollman 2592702Swollman //---------------------------------------------------------------------- 2602702Swollman // Return a new CompilerType adds a volatile modifier to this type if 2612702Swollman // this type is valid and the type system supports volatile modifiers, 2622702Swollman // else return an invalid type. 2632702Swollman //---------------------------------------------------------------------- 2642702Swollman CompilerType AddVolatileModifier() const; 2652702Swollman 2662702Swollman //---------------------------------------------------------------------- 2672702Swollman // Return a new CompilerType adds a restrict modifier to this type if 2682702Swollman // this type is valid and the type system supports restrict modifiers, 2692702Swollman // else return an invalid type. 2702702Swollman //---------------------------------------------------------------------- 2712702Swollman CompilerType AddRestrictModifier() const; 2722702Swollman 2732702Swollman //---------------------------------------------------------------------- 2742702Swollman // Create a typedef to this type using "name" as the name of the typedef 2752702Swollman // this type is valid and the type system supports typedefs, else return 2762702Swollman // an invalid type. 2772702Swollman //---------------------------------------------------------------------- 2782702Swollman CompilerType CreateTypedef(const char *name, 2792702Swollman const CompilerDeclContext &decl_ctx) const; 2802702Swollman 2812702Swollman // If the current object represents a typedef type, get the underlying type 2822702Swollman CompilerType GetTypedefedType() const; 2832702Swollman 284192625Sedwin //---------------------------------------------------------------------- 285192625Sedwin // Create related types using the current type's AST 2862702Swollman //---------------------------------------------------------------------- 2872702Swollman CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const; 2882702Swollman 2892702Swollman //---------------------------------------------------------------------- 2902702Swollman // Exploring the type 2912702Swollman //---------------------------------------------------------------------- 2922702Swollman 2932702Swollman uint64_t GetByteSize(ExecutionContextScope *exe_scope) const; 2942702Swollman 2952702Swollman uint64_t GetBitSize(ExecutionContextScope *exe_scope) const; 2962702Swollman 2972702Swollman lldb::Encoding GetEncoding(uint64_t &count) const; 2982702Swollman 2992702Swollman lldb::Format GetFormat() const; 3002702Swollman 3012702Swollman size_t GetTypeBitAlign() const; 3022702Swollman 3032702Swollman uint32_t GetNumChildren(bool omit_empty_base_classes) const; 3042702Swollman 3052702Swollman lldb::BasicType GetBasicTypeEnumeration() const; 3062702Swollman 3072702Swollman static lldb::BasicType GetBasicTypeEnumeration(const ConstString &name); 3082702Swollman 3092702Swollman //---------------------------------------------------------------------- 3102702Swollman // If this type is an enumeration, iterate through all of its enumerators 3112702Swollman // using a callback. If the callback returns true, keep iterating, else 3122702Swollman // abort the iteration. 3132702Swollman //---------------------------------------------------------------------- 3142702Swollman void ForEachEnumerator( 3152702Swollman std::function<bool(const CompilerType &integer_type, 3162702Swollman const ConstString &name, 3172702Swollman const llvm::APSInt &value)> const &callback) const; 3182702Swollman 3192702Swollman uint32_t GetNumFields() const; 3202702Swollman 3212702Swollman CompilerType GetFieldAtIndex(size_t idx, std::string &name, 3222702Swollman uint64_t *bit_offset_ptr, 3232702Swollman uint32_t *bitfield_bit_size_ptr, 3242702Swollman bool *is_bitfield_ptr) const; 3252702Swollman 3262702Swollman uint32_t GetNumDirectBaseClasses() const; 3272702Swollman 3282702Swollman uint32_t GetNumVirtualBaseClasses() const; 3292702Swollman 3302702Swollman CompilerType GetDirectBaseClassAtIndex(size_t idx, 3312702Swollman uint32_t *bit_offset_ptr) const; 3322702Swollman 3332702Swollman CompilerType GetVirtualBaseClassAtIndex(size_t idx, 3342702Swollman uint32_t *bit_offset_ptr) const; 3352702Swollman 3362702Swollman uint32_t GetIndexOfFieldWithName(const char *name, 3372702Swollman CompilerType *field_compiler_type = nullptr, 3382702Swollman uint64_t *bit_offset_ptr = nullptr, 3392702Swollman uint32_t *bitfield_bit_size_ptr = nullptr, 3402702Swollman bool *is_bitfield_ptr = nullptr) const; 3412702Swollman 3422702Swollman CompilerType GetChildCompilerTypeAtIndex( 3432702Swollman ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers, 3442702Swollman bool omit_empty_base_classes, bool ignore_array_bounds, 3452702Swollman std::string &child_name, uint32_t &child_byte_size, 3462702Swollman int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size, 3472702Swollman uint32_t &child_bitfield_bit_offset, bool &child_is_base_class, 3482702Swollman bool &child_is_deref_of_parent, ValueObject *valobj, 3492702Swollman uint64_t &language_flags) const; 3502702Swollman 3512702Swollman // Lookup a child given a name. This function will match base class names 3522702Swollman // and member member names in "clang_type" only, not descendants. 3532702Swollman uint32_t GetIndexOfChildWithName(const char *name, 3542702Swollman bool omit_empty_base_classes) const; 3552702Swollman 3562702Swollman // Lookup a child member given a name. This function will match member names 3572702Swollman // only and will descend into "clang_type" children in search for the first 3582702Swollman // member in this class, or any base class that matches "name". 3592702Swollman // TODO: Return all matches for a given name by returning a 3602702Swollman // vector<vector<uint32_t>> 36117214Swollman // so we catch all names that match a given child name, not just the first. 362192625Sedwin size_t 36317214Swollman GetIndexOfChildMemberWithName(const char *name, bool omit_empty_base_classes, 36417214Swollman std::vector<uint32_t> &child_indexes) const; 3652702Swollman 3662702Swollman size_t GetNumTemplateArguments() const; 3672702Swollman 3682702Swollman CompilerType GetTemplateArgument(size_t idx, 3699937Swollman lldb::TemplateArgumentKind &kind) const; 3702702Swollman 371192625Sedwin CompilerType GetTypeForFormatters() const; 3722702Swollman 3732702Swollman LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const; 3742702Swollman 3752702Swollman bool IsMeaninglessWithoutDynamicResolution() const; 3762702Swollman 3772702Swollman //------------------------------------------------------------------ 3782702Swollman // Pointers & References 3792702Swollman //------------------------------------------------------------------ 3802702Swollman 3812702Swollman // Converts "s" to a floating point value and place resulting floating 3822702Swollman // point bytes in the "dst" buffer. 38330829Scharnier size_t ConvertStringToFloatValue(const char *s, uint8_t *dst, 38430829Scharnier size_t dst_size) const; 3852702Swollman 3862702Swollman //---------------------------------------------------------------------- 3872702Swollman // Dumping types 3882702Swollman //---------------------------------------------------------------------- 3899937Swollman void DumpValue(ExecutionContext *exe_ctx, Stream *s, lldb::Format format, 3902702Swollman const DataExtractor &data, lldb::offset_t data_offset, 3919937Swollman size_t data_byte_size, uint32_t bitfield_bit_size, 3922702Swollman uint32_t bitfield_bit_offset, bool show_types, 3932702Swollman bool show_summary, bool verbose, uint32_t depth); 3942702Swollman 3952702Swollman bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data, 3962702Swollman lldb::offset_t data_offset, size_t data_byte_size, 3972702Swollman uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, 3982702Swollman ExecutionContextScope *exe_scope); 3992702Swollman 4002702Swollman void DumpSummary(ExecutionContext *exe_ctx, Stream *s, 4012702Swollman const DataExtractor &data, lldb::offset_t data_offset, 4022702Swollman size_t data_byte_size); 4032702Swollman 4042702Swollman void DumpTypeDescription() const; // Dump to stdout 4052702Swollman 4062702Swollman void DumpTypeDescription(Stream *s) const; 4072702Swollman 4082702Swollman bool GetValueAsScalar(const DataExtractor &data, lldb::offset_t data_offset, 4092702Swollman size_t data_byte_size, Scalar &value) const; 4102702Swollman 4112702Swollman bool SetValueFromScalar(const Scalar &value, Stream &strm); 4122702Swollman 4132702Swollman bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t addr, 4142702Swollman AddressType address_type, DataExtractor &data); 4152702Swollman 4162702Swollman bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr, 4172702Swollman AddressType address_type, StreamString &new_value); 4182702Swollman 4192702Swollman void Clear() { 4202702Swollman m_type = nullptr; 4212702Swollman m_type_system = nullptr; 4222702Swollman } 4232702Swollman 4249937Swollmanprivate: 4252702Swollman lldb::opaque_compiler_type_t m_type; 4262702Swollman TypeSystem *m_type_system; 42717214Swollman}; 4282702Swollman 4292702Swollmanbool operator==(const CompilerType &lhs, const CompilerType &rhs); 43017214Swollmanbool operator!=(const CompilerType &lhs, const CompilerType &rhs); 4312702Swollman 4322702Swollman} // namespace lldb_private 4332702Swollman 4342702Swollman#endif // liblldb_CompilerType_h_ 4352702Swollman