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"
24 namespace lldb_private {
28 //----------------------------------------------------------------------
29 // A class that can carry around a clang ASTContext and a opaque clang
30 // QualType. A clang::QualType can be easily reconstructed from an
31 // opaque clang type and often the ASTContext is needed when doing
32 // various type related tasks, so this class allows both items to travel
33 // in a single very lightweight class that can be used. There are many
34 // static equivalents of the member functions that allow the ASTContext
35 // and the opaque clang QualType to be specified for ease of use and
36 // 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
209 // Returns a value >= 0 if there is a prototype.
210 int GetFunctionArgumentCount() const;
212 CompilerType GetFunctionArgumentTypeAtIndex(size_t idx) const;
214 CompilerType GetFunctionReturnType() const;
216 size_t GetNumMemberFunctions() const;
218 TypeMemberFunctionImpl GetMemberFunctionAtIndex(size_t idx);
220 //----------------------------------------------------------------------
221 // If this type is a reference to a type (L value or R value reference),
222 // return a new type with the reference removed, else return the current
224 //----------------------------------------------------------------------
225 CompilerType GetNonReferenceType() const;
227 //----------------------------------------------------------------------
228 // If this type is a pointer type, return the type that the pointer
229 // points to, else return an invalid type.
230 //----------------------------------------------------------------------
231 CompilerType GetPointeeType() const;
233 //----------------------------------------------------------------------
234 // Return a new CompilerType that is a pointer to this type
235 //----------------------------------------------------------------------
236 CompilerType GetPointerType() const;
238 //----------------------------------------------------------------------
239 // Return a new CompilerType that is a L value reference to this type if
240 // this type is valid and the type system supports L value references,
241 // else return an invalid type.
242 //----------------------------------------------------------------------
243 CompilerType GetLValueReferenceType() const;
245 //----------------------------------------------------------------------
246 // Return a new CompilerType that is a R value reference to this type if
247 // this type is valid and the type system supports R value references,
248 // else return an invalid type.
249 //----------------------------------------------------------------------
250 CompilerType GetRValueReferenceType() const;
252 //----------------------------------------------------------------------
253 // Return a new CompilerType adds a const modifier to this type if
254 // this type is valid and the type system supports const modifiers,
255 // else return an invalid type.
256 //----------------------------------------------------------------------
257 CompilerType AddConstModifier() const;
259 //----------------------------------------------------------------------
260 // Return a new CompilerType adds a volatile modifier to this type if
261 // this type is valid and the type system supports volatile modifiers,
262 // else return an invalid type.
263 //----------------------------------------------------------------------
264 CompilerType AddVolatileModifier() const;
266 //----------------------------------------------------------------------
267 // Return a new CompilerType adds a restrict modifier to this type if
268 // this type is valid and the type system supports restrict modifiers,
269 // else return an invalid type.
270 //----------------------------------------------------------------------
271 CompilerType AddRestrictModifier() const;
273 //----------------------------------------------------------------------
274 // Create a typedef to this type using "name" as the name of the typedef
275 // this type is valid and the type system supports typedefs, else return
277 //----------------------------------------------------------------------
278 CompilerType CreateTypedef(const char *name,
279 const CompilerDeclContext &decl_ctx) const;
281 // If the current object represents a typedef type, get the underlying type
282 CompilerType GetTypedefedType() const;
284 //----------------------------------------------------------------------
285 // Create related types using the current type's AST
286 //----------------------------------------------------------------------
287 CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const;
289 //----------------------------------------------------------------------
290 // Exploring the type
291 //----------------------------------------------------------------------
293 uint64_t GetByteSize(ExecutionContextScope *exe_scope) const;
295 uint64_t GetBitSize(ExecutionContextScope *exe_scope) const;
297 lldb::Encoding GetEncoding(uint64_t &count) const;
299 lldb::Format GetFormat() const;
301 size_t GetTypeBitAlign() const;
303 uint32_t GetNumChildren(bool omit_empty_base_classes) const;
305 lldb::BasicType GetBasicTypeEnumeration() const;
307 static lldb::BasicType GetBasicTypeEnumeration(const ConstString &name);
309 //----------------------------------------------------------------------
310 // If this type is an enumeration, iterate through all of its enumerators
311 // using a callback. If the callback returns true, keep iterating, else
312 // abort the iteration.
313 //----------------------------------------------------------------------
314 void ForEachEnumerator(
315 std::function<bool(const CompilerType &integer_type,
316 const ConstString &name,
317 const llvm::APSInt &value)> const &callback) const;
319 uint32_t GetNumFields() const;
321 CompilerType GetFieldAtIndex(size_t idx, std::string &name,
322 uint64_t *bit_offset_ptr,
323 uint32_t *bitfield_bit_size_ptr,
324 bool *is_bitfield_ptr) const;
326 uint32_t GetNumDirectBaseClasses() const;
328 uint32_t GetNumVirtualBaseClasses() const;
330 CompilerType GetDirectBaseClassAtIndex(size_t idx,
331 uint32_t *bit_offset_ptr) const;
333 CompilerType GetVirtualBaseClassAtIndex(size_t idx,
334 uint32_t *bit_offset_ptr) const;
336 uint32_t GetIndexOfFieldWithName(const char *name,
337 CompilerType *field_compiler_type = nullptr,
338 uint64_t *bit_offset_ptr = nullptr,
339 uint32_t *bitfield_bit_size_ptr = nullptr,
340 bool *is_bitfield_ptr = nullptr) const;
342 CompilerType GetChildCompilerTypeAtIndex(
343 ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
344 bool omit_empty_base_classes, bool ignore_array_bounds,
345 std::string &child_name, uint32_t &child_byte_size,
346 int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
347 uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
348 bool &child_is_deref_of_parent, ValueObject *valobj,
349 uint64_t &language_flags) const;
351 // Lookup a child given a name. This function will match base class names
352 // and member member names in "clang_type" only, not descendants.
353 uint32_t GetIndexOfChildWithName(const char *name,
354 bool omit_empty_base_classes) const;
356 // Lookup a child member given a name. This function will match member names
357 // only and will descend into "clang_type" children in search for the first
358 // member in this class, or any base class that matches "name".
359 // TODO: Return all matches for a given name by returning a
360 // vector<vector<uint32_t>>
361 // so we catch all names that match a given child name, not just the first.
363 GetIndexOfChildMemberWithName(const char *name, bool omit_empty_base_classes,
364 std::vector<uint32_t> &child_indexes) const;
366 size_t GetNumTemplateArguments() const;
368 CompilerType GetTemplateArgument(size_t idx,
369 lldb::TemplateArgumentKind &kind) const;
371 CompilerType GetTypeForFormatters() const;
373 LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const;
375 bool IsMeaninglessWithoutDynamicResolution() const;
377 //------------------------------------------------------------------
378 // Pointers & References
379 //------------------------------------------------------------------
381 // Converts "s" to a floating point value and place resulting floating
382 // point bytes in the "dst" buffer.
383 size_t ConvertStringToFloatValue(const char *s, uint8_t *dst,
384 size_t dst_size) const;
386 //----------------------------------------------------------------------
388 //----------------------------------------------------------------------
389 void DumpValue(ExecutionContext *exe_ctx, Stream *s, lldb::Format format,
390 const DataExtractor &data, lldb::offset_t data_offset,
391 size_t data_byte_size, uint32_t bitfield_bit_size,
392 uint32_t bitfield_bit_offset, bool show_types,
393 bool show_summary, bool verbose, uint32_t depth);
395 bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data,
396 lldb::offset_t data_offset, size_t data_byte_size,
397 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
398 ExecutionContextScope *exe_scope);
400 void DumpSummary(ExecutionContext *exe_ctx, Stream *s,
401 const DataExtractor &data, lldb::offset_t data_offset,
402 size_t data_byte_size);
404 void DumpTypeDescription() const; // Dump to stdout
406 void DumpTypeDescription(Stream *s) const;
408 bool GetValueAsScalar(const DataExtractor &data, lldb::offset_t data_offset,
409 size_t data_byte_size, Scalar &value) const;
411 bool SetValueFromScalar(const Scalar &value, Stream &strm);
413 bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
414 AddressType address_type, DataExtractor &data);
416 bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
417 AddressType address_type, StreamString &new_value);
421 m_type_system = nullptr;
425 lldb::opaque_compiler_type_t m_type;
426 TypeSystem *m_type_system;
429 bool operator==(const CompilerType &lhs, const CompilerType &rhs);
430 bool operator!=(const CompilerType &lhs, const CompilerType &rhs);
432 } // namespace lldb_private
434 #endif // liblldb_CompilerType_h_