1 //===-- CompilerType.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_CompilerType_h_
11 #define liblldb_CompilerType_h_
19 // Other libraries and framework includes
21 #include "lldb/Core/ClangForward.h"
22 #include "lldb/lldb-private.h"
23 #include "llvm/ADT/APSInt.h"
25 namespace lldb_private {
29 //----------------------------------------------------------------------
30 // A class that can carry around a clang ASTContext and a opaque clang
31 // QualType. A clang::QualType can be easily reconstructed from an opaque clang
32 // type and often the ASTContext is needed when doing various type related
33 // tasks, so this class allows both items to travel in a single very
34 // lightweight class that can be used. There are many static equivalents of the
35 // member functions that allow the ASTContext and the opaque clang QualType to
36 // be specified for ease of use and to avoid code duplication.
37 //----------------------------------------------------------------------
40 //----------------------------------------------------------------------
41 // Constructors and Destructors
42 //----------------------------------------------------------------------
43 CompilerType(TypeSystem *type_system, lldb::opaque_compiler_type_t type);
44 CompilerType(clang::ASTContext *ast_context, clang::QualType qual_type);
46 CompilerType(const CompilerType &rhs)
47 : m_type(rhs.m_type), m_type_system(rhs.m_type_system) {}
49 CompilerType() : m_type(nullptr), m_type_system(nullptr) {}
53 //----------------------------------------------------------------------
55 //----------------------------------------------------------------------
57 const CompilerType &operator=(const CompilerType &rhs) {
59 m_type_system = rhs.m_type_system;
63 //----------------------------------------------------------------------
65 //----------------------------------------------------------------------
67 explicit operator bool() const {
68 return m_type != nullptr && m_type_system != nullptr;
71 bool operator<(const CompilerType &rhs) const {
72 if (m_type_system == rhs.m_type_system)
73 return m_type < rhs.m_type;
74 return m_type_system < rhs.m_type_system;
77 bool IsValid() const { return m_type != nullptr && m_type_system != nullptr; }
79 bool IsArrayType(CompilerType *element_type, uint64_t *size,
80 bool *is_incomplete) const;
82 bool IsVectorType(CompilerType *element_type, uint64_t *size) const;
84 bool IsArrayOfScalarType() const;
86 bool IsAggregateType() const;
88 bool IsAnonymousType() const;
90 bool IsBeingDefined() const;
92 bool IsCharType() const;
94 bool IsCompleteType() const;
98 bool IsCStringType(uint32_t &length) const;
100 bool IsDefined() const;
102 bool IsFloatingPointType(uint32_t &count, bool &is_complex) const;
104 bool IsFunctionType(bool *is_variadic_ptr = nullptr) const;
106 uint32_t IsHomogeneousAggregate(CompilerType *base_type_ptr) const;
108 size_t GetNumberOfFunctionArguments() const;
110 CompilerType GetFunctionArgumentAtIndex(const size_t index) const;
112 bool IsVariadicFunctionType() const;
114 bool IsFunctionPointerType() const;
116 bool IsBlockPointerType(CompilerType *function_pointer_type_ptr) const;
118 bool IsIntegerType(bool &is_signed) const;
120 bool IsEnumerationType(bool &is_signed) const;
122 bool IsIntegerOrEnumerationType(bool &is_signed) const;
124 bool IsPolymorphicClass() const;
127 IsPossibleCPlusPlusDynamicType(CompilerType *target_type = nullptr) const {
128 return IsPossibleDynamicType(target_type, true, false);
131 bool IsPossibleDynamicType(CompilerType *target_type, // Can pass nullptr
132 bool check_cplusplus, bool check_objc) const;
134 bool IsPointerToScalarType() const;
136 bool IsRuntimeGeneratedType() const;
138 bool IsPointerType(CompilerType *pointee_type = nullptr) const;
140 bool IsPointerOrReferenceType(CompilerType *pointee_type = nullptr) const;
142 bool IsReferenceType(CompilerType *pointee_type = nullptr,
143 bool *is_rvalue = nullptr) const;
145 bool ShouldTreatScalarValueAsAddress() const;
147 bool IsScalarType() const;
149 bool IsTypedefType() const;
151 bool IsVoidType() const;
153 //----------------------------------------------------------------------
155 //----------------------------------------------------------------------
157 bool GetCompleteType() const;
159 //----------------------------------------------------------------------
160 // AST related queries
161 //----------------------------------------------------------------------
163 size_t GetPointerByteSize() const;
165 //----------------------------------------------------------------------
167 //----------------------------------------------------------------------
169 TypeSystem *GetTypeSystem() const { return m_type_system; }
171 ConstString GetConstQualifiedTypeName() const;
173 ConstString GetConstTypeName() const;
175 ConstString GetTypeName() const;
177 ConstString GetDisplayTypeName() const;
180 GetTypeInfo(CompilerType *pointee_or_element_compiler_type = nullptr) const;
182 lldb::LanguageType GetMinimumLanguage();
184 lldb::opaque_compiler_type_t GetOpaqueQualType() const { return m_type; }
186 lldb::TypeClass GetTypeClass() const;
188 void SetCompilerType(TypeSystem *type_system,
189 lldb::opaque_compiler_type_t type);
191 void SetCompilerType(clang::ASTContext *ast, clang::QualType qual_type);
193 unsigned GetTypeQualifiers() const;
195 //----------------------------------------------------------------------
196 // Creating related types
197 //----------------------------------------------------------------------
199 CompilerType GetArrayElementType(uint64_t *stride = nullptr) const;
201 CompilerType GetArrayType(uint64_t size) const;
203 CompilerType GetCanonicalType() const;
205 CompilerType GetFullyUnqualifiedType() const;
207 // Returns -1 if this isn't a function of if the function doesn't have a
208 // prototype Returns a value >= 0 if there is a prototype.
209 int GetFunctionArgumentCount() const;
211 CompilerType GetFunctionArgumentTypeAtIndex(size_t idx) const;
213 CompilerType GetFunctionReturnType() const;
215 size_t GetNumMemberFunctions() const;
217 TypeMemberFunctionImpl GetMemberFunctionAtIndex(size_t idx);
219 //----------------------------------------------------------------------
220 // If this type is a reference to a type (L value or R value reference),
221 // return a new type with the reference removed, else return the current type
223 //----------------------------------------------------------------------
224 CompilerType GetNonReferenceType() const;
226 //----------------------------------------------------------------------
227 // If this type is a pointer type, return the type that the pointer points
228 // to, else return an invalid type.
229 //----------------------------------------------------------------------
230 CompilerType GetPointeeType() const;
232 //----------------------------------------------------------------------
233 // Return a new CompilerType that is a pointer to this type
234 //----------------------------------------------------------------------
235 CompilerType GetPointerType() const;
237 //----------------------------------------------------------------------
238 // Return a new CompilerType that is a L value reference to this type if this
239 // type is valid and the type system supports L value references, else return
241 //----------------------------------------------------------------------
242 CompilerType GetLValueReferenceType() const;
244 //----------------------------------------------------------------------
245 // Return a new CompilerType that is a R value reference to this type if this
246 // type is valid and the type system supports R value references, else return
248 //----------------------------------------------------------------------
249 CompilerType GetRValueReferenceType() const;
251 //----------------------------------------------------------------------
252 // Return a new CompilerType adds a const modifier to this type if this type
253 // is valid and the type system supports const modifiers, else return an
255 //----------------------------------------------------------------------
256 CompilerType AddConstModifier() const;
258 //----------------------------------------------------------------------
259 // Return a new CompilerType adds a volatile modifier to this type if this
260 // type is valid and the type system supports volatile modifiers, else return
262 //----------------------------------------------------------------------
263 CompilerType AddVolatileModifier() const;
265 //----------------------------------------------------------------------
266 // Return a new CompilerType adds a restrict modifier to this type if this
267 // type is valid and the type system supports restrict modifiers, else return
269 //----------------------------------------------------------------------
270 CompilerType AddRestrictModifier() const;
272 //----------------------------------------------------------------------
273 // Create a typedef to this type using "name" as the name of the typedef this
274 // type is valid and the type system supports typedefs, else return an
276 //----------------------------------------------------------------------
277 CompilerType CreateTypedef(const char *name,
278 const CompilerDeclContext &decl_ctx) const;
280 // If the current object represents a typedef type, get the underlying type
281 CompilerType GetTypedefedType() const;
283 //----------------------------------------------------------------------
284 // Create related types using the current type's AST
285 //----------------------------------------------------------------------
286 CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const;
288 //----------------------------------------------------------------------
289 // Exploring the type
290 //----------------------------------------------------------------------
292 struct IntegralTemplateArgument;
294 uint64_t GetByteSize(ExecutionContextScope *exe_scope) const;
296 uint64_t GetBitSize(ExecutionContextScope *exe_scope) const;
298 lldb::Encoding GetEncoding(uint64_t &count) const;
300 lldb::Format GetFormat() const;
302 size_t GetTypeBitAlign() const;
304 uint32_t GetNumChildren(bool omit_empty_base_classes) const;
306 lldb::BasicType GetBasicTypeEnumeration() const;
308 static lldb::BasicType GetBasicTypeEnumeration(const ConstString &name);
310 //----------------------------------------------------------------------
311 // If this type is an enumeration, iterate through all of its enumerators
312 // using a callback. If the callback returns true, keep iterating, else abort
314 //----------------------------------------------------------------------
315 void ForEachEnumerator(
316 std::function<bool(const CompilerType &integer_type,
317 const ConstString &name,
318 const llvm::APSInt &value)> const &callback) const;
320 uint32_t GetNumFields() const;
322 CompilerType GetFieldAtIndex(size_t idx, std::string &name,
323 uint64_t *bit_offset_ptr,
324 uint32_t *bitfield_bit_size_ptr,
325 bool *is_bitfield_ptr) const;
327 uint32_t GetNumDirectBaseClasses() const;
329 uint32_t GetNumVirtualBaseClasses() const;
331 CompilerType GetDirectBaseClassAtIndex(size_t idx,
332 uint32_t *bit_offset_ptr) const;
334 CompilerType GetVirtualBaseClassAtIndex(size_t idx,
335 uint32_t *bit_offset_ptr) const;
337 uint32_t GetIndexOfFieldWithName(const char *name,
338 CompilerType *field_compiler_type = nullptr,
339 uint64_t *bit_offset_ptr = nullptr,
340 uint32_t *bitfield_bit_size_ptr = nullptr,
341 bool *is_bitfield_ptr = nullptr) const;
343 CompilerType GetChildCompilerTypeAtIndex(
344 ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
345 bool omit_empty_base_classes, bool ignore_array_bounds,
346 std::string &child_name, uint32_t &child_byte_size,
347 int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
348 uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
349 bool &child_is_deref_of_parent, ValueObject *valobj,
350 uint64_t &language_flags) const;
352 // Lookup a child given a name. This function will match base class names and
353 // member member names in "clang_type" only, not descendants.
354 uint32_t GetIndexOfChildWithName(const char *name,
355 bool omit_empty_base_classes) const;
357 // Lookup a child member given a name. This function will match member names
358 // only and will descend into "clang_type" children in search for the first
359 // member in this class, or any base class that matches "name".
360 // TODO: Return all matches for a given name by returning a
361 // vector<vector<uint32_t>>
362 // so we catch all names that match a given child name, not just the first.
364 GetIndexOfChildMemberWithName(const char *name, bool omit_empty_base_classes,
365 std::vector<uint32_t> &child_indexes) const;
367 size_t GetNumTemplateArguments() const;
369 lldb::TemplateArgumentKind GetTemplateArgumentKind(size_t idx) const;
370 CompilerType GetTypeTemplateArgument(size_t idx) const;
372 // Returns the value of the template argument and its type.
373 llvm::Optional<IntegralTemplateArgument>
374 GetIntegralTemplateArgument(size_t idx) const;
376 CompilerType GetTypeForFormatters() const;
378 LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const;
380 bool IsMeaninglessWithoutDynamicResolution() const;
382 //------------------------------------------------------------------
383 // Pointers & References
384 //------------------------------------------------------------------
386 // Converts "s" to a floating point value and place resulting floating point
387 // bytes in the "dst" buffer.
388 size_t ConvertStringToFloatValue(const char *s, uint8_t *dst,
389 size_t dst_size) const;
391 //----------------------------------------------------------------------
393 //----------------------------------------------------------------------
394 void DumpValue(ExecutionContext *exe_ctx, Stream *s, lldb::Format format,
395 const DataExtractor &data, lldb::offset_t data_offset,
396 size_t data_byte_size, uint32_t bitfield_bit_size,
397 uint32_t bitfield_bit_offset, bool show_types,
398 bool show_summary, bool verbose, uint32_t depth);
400 bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data,
401 lldb::offset_t data_offset, size_t data_byte_size,
402 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
403 ExecutionContextScope *exe_scope);
405 void DumpSummary(ExecutionContext *exe_ctx, Stream *s,
406 const DataExtractor &data, lldb::offset_t data_offset,
407 size_t data_byte_size);
409 void DumpTypeDescription() const; // Dump to stdout
411 void DumpTypeDescription(Stream *s) const;
413 bool GetValueAsScalar(const DataExtractor &data, lldb::offset_t data_offset,
414 size_t data_byte_size, Scalar &value) const;
416 bool SetValueFromScalar(const Scalar &value, Stream &strm);
418 bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
419 AddressType address_type, DataExtractor &data);
421 bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
422 AddressType address_type, StreamString &new_value);
426 m_type_system = nullptr;
430 lldb::opaque_compiler_type_t m_type;
431 TypeSystem *m_type_system;
434 bool operator==(const CompilerType &lhs, const CompilerType &rhs);
435 bool operator!=(const CompilerType &lhs, const CompilerType &rhs);
437 struct CompilerType::IntegralTemplateArgument {
442 } // namespace lldb_private
444 #endif // liblldb_CompilerType_h_