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
32 // opaque clang type and often the ASTContext is needed when doing
33 // various type related tasks, so this class allows both items to travel
34 // in a single very lightweight class that can be used. There are many
35 // static equivalents of the member functions that allow the ASTContext
36 // and the opaque clang QualType to be specified for ease of use and
37 // to avoid code duplication.
38 //----------------------------------------------------------------------
41 //----------------------------------------------------------------------
42 // Constructors and Destructors
43 //----------------------------------------------------------------------
44 CompilerType(TypeSystem *type_system, lldb::opaque_compiler_type_t type);
45 CompilerType(clang::ASTContext *ast_context, clang::QualType qual_type);
47 CompilerType(const CompilerType &rhs)
48 : m_type(rhs.m_type), m_type_system(rhs.m_type_system) {}
50 CompilerType() : m_type(nullptr), m_type_system(nullptr) {}
54 //----------------------------------------------------------------------
56 //----------------------------------------------------------------------
58 const CompilerType &operator=(const CompilerType &rhs) {
60 m_type_system = rhs.m_type_system;
64 //----------------------------------------------------------------------
66 //----------------------------------------------------------------------
68 explicit operator bool() const {
69 return m_type != nullptr && m_type_system != nullptr;
72 bool operator<(const CompilerType &rhs) const {
73 if (m_type_system == rhs.m_type_system)
74 return m_type < rhs.m_type;
75 return m_type_system < rhs.m_type_system;
78 bool IsValid() const { return m_type != nullptr && m_type_system != nullptr; }
80 bool IsArrayType(CompilerType *element_type, uint64_t *size,
81 bool *is_incomplete) const;
83 bool IsVectorType(CompilerType *element_type, uint64_t *size) const;
85 bool IsArrayOfScalarType() const;
87 bool IsAggregateType() const;
89 bool IsAnonymousType() const;
91 bool IsBeingDefined() const;
93 bool IsCharType() const;
95 bool IsCompleteType() const;
99 bool IsCStringType(uint32_t &length) const;
101 bool IsDefined() const;
103 bool IsFloatingPointType(uint32_t &count, bool &is_complex) const;
105 bool IsFunctionType(bool *is_variadic_ptr = nullptr) const;
107 uint32_t IsHomogeneousAggregate(CompilerType *base_type_ptr) const;
109 size_t GetNumberOfFunctionArguments() const;
111 CompilerType GetFunctionArgumentAtIndex(const size_t index) const;
113 bool IsVariadicFunctionType() const;
115 bool IsFunctionPointerType() const;
117 bool IsBlockPointerType(CompilerType *function_pointer_type_ptr) const;
119 bool IsIntegerType(bool &is_signed) const;
121 bool IsEnumerationType(bool &is_signed) const;
123 bool IsIntegerOrEnumerationType(bool &is_signed) const;
125 bool IsPolymorphicClass() const;
128 IsPossibleCPlusPlusDynamicType(CompilerType *target_type = nullptr) const {
129 return IsPossibleDynamicType(target_type, true, false);
132 bool IsPossibleDynamicType(CompilerType *target_type, // Can pass nullptr
133 bool check_cplusplus, bool check_objc) const;
135 bool IsPointerToScalarType() const;
137 bool IsRuntimeGeneratedType() const;
139 bool IsPointerType(CompilerType *pointee_type = nullptr) const;
141 bool IsPointerOrReferenceType(CompilerType *pointee_type = nullptr) const;
143 bool IsReferenceType(CompilerType *pointee_type = nullptr,
144 bool *is_rvalue = nullptr) const;
146 bool ShouldTreatScalarValueAsAddress() const;
148 bool IsScalarType() const;
150 bool IsTypedefType() const;
152 bool IsVoidType() const;
154 //----------------------------------------------------------------------
156 //----------------------------------------------------------------------
158 bool GetCompleteType() const;
160 //----------------------------------------------------------------------
161 // AST related queries
162 //----------------------------------------------------------------------
164 size_t GetPointerByteSize() const;
166 //----------------------------------------------------------------------
168 //----------------------------------------------------------------------
170 TypeSystem *GetTypeSystem() const { return m_type_system; }
172 ConstString GetConstQualifiedTypeName() const;
174 ConstString GetConstTypeName() const;
176 ConstString GetTypeName() const;
178 ConstString GetDisplayTypeName() const;
181 GetTypeInfo(CompilerType *pointee_or_element_compiler_type = nullptr) const;
183 lldb::LanguageType GetMinimumLanguage();
185 lldb::opaque_compiler_type_t GetOpaqueQualType() const { return m_type; }
187 lldb::TypeClass GetTypeClass() const;
189 void SetCompilerType(TypeSystem *type_system,
190 lldb::opaque_compiler_type_t type);
192 void SetCompilerType(clang::ASTContext *ast, clang::QualType qual_type);
194 unsigned GetTypeQualifiers() const;
196 //----------------------------------------------------------------------
197 // Creating related types
198 //----------------------------------------------------------------------
200 CompilerType GetArrayElementType(uint64_t *stride = nullptr) const;
202 CompilerType GetArrayType(uint64_t size) const;
204 CompilerType GetCanonicalType() const;
206 CompilerType GetFullyUnqualifiedType() const;
208 // Returns -1 if this isn't a function of if the function doesn't have a
210 // Returns a value >= 0 if there is a prototype.
211 int GetFunctionArgumentCount() const;
213 CompilerType GetFunctionArgumentTypeAtIndex(size_t idx) const;
215 CompilerType GetFunctionReturnType() const;
217 size_t GetNumMemberFunctions() const;
219 TypeMemberFunctionImpl GetMemberFunctionAtIndex(size_t idx);
221 //----------------------------------------------------------------------
222 // If this type is a reference to a type (L value or R value reference),
223 // return a new type with the reference removed, else return the current
225 //----------------------------------------------------------------------
226 CompilerType GetNonReferenceType() const;
228 //----------------------------------------------------------------------
229 // If this type is a pointer type, return the type that the pointer
230 // points to, else return an invalid type.
231 //----------------------------------------------------------------------
232 CompilerType GetPointeeType() const;
234 //----------------------------------------------------------------------
235 // Return a new CompilerType that is a pointer to this type
236 //----------------------------------------------------------------------
237 CompilerType GetPointerType() const;
239 //----------------------------------------------------------------------
240 // Return a new CompilerType that is a L value reference to this type if
241 // this type is valid and the type system supports L value references,
242 // else return an invalid type.
243 //----------------------------------------------------------------------
244 CompilerType GetLValueReferenceType() const;
246 //----------------------------------------------------------------------
247 // Return a new CompilerType that is a R value reference to this type if
248 // this type is valid and the type system supports R value references,
249 // else return an invalid type.
250 //----------------------------------------------------------------------
251 CompilerType GetRValueReferenceType() const;
253 //----------------------------------------------------------------------
254 // Return a new CompilerType adds a const modifier to this type if
255 // this type is valid and the type system supports const modifiers,
256 // else return an invalid type.
257 //----------------------------------------------------------------------
258 CompilerType AddConstModifier() const;
260 //----------------------------------------------------------------------
261 // Return a new CompilerType adds a volatile modifier to this type if
262 // this type is valid and the type system supports volatile modifiers,
263 // else return an invalid type.
264 //----------------------------------------------------------------------
265 CompilerType AddVolatileModifier() const;
267 //----------------------------------------------------------------------
268 // Return a new CompilerType adds a restrict modifier to this type if
269 // this type is valid and the type system supports restrict modifiers,
270 // else return an invalid type.
271 //----------------------------------------------------------------------
272 CompilerType AddRestrictModifier() const;
274 //----------------------------------------------------------------------
275 // Create a typedef to this type using "name" as the name of the typedef
276 // this type is valid and the type system supports typedefs, else return
278 //----------------------------------------------------------------------
279 CompilerType CreateTypedef(const char *name,
280 const CompilerDeclContext &decl_ctx) const;
282 // If the current object represents a typedef type, get the underlying type
283 CompilerType GetTypedefedType() const;
285 //----------------------------------------------------------------------
286 // Create related types using the current type's AST
287 //----------------------------------------------------------------------
288 CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const;
290 //----------------------------------------------------------------------
291 // Exploring the type
292 //----------------------------------------------------------------------
294 struct IntegralTemplateArgument;
296 uint64_t GetByteSize(ExecutionContextScope *exe_scope) const;
298 uint64_t GetBitSize(ExecutionContextScope *exe_scope) const;
300 lldb::Encoding GetEncoding(uint64_t &count) const;
302 lldb::Format GetFormat() const;
304 size_t GetTypeBitAlign() const;
306 uint32_t GetNumChildren(bool omit_empty_base_classes) const;
308 lldb::BasicType GetBasicTypeEnumeration() const;
310 static lldb::BasicType GetBasicTypeEnumeration(const ConstString &name);
312 //----------------------------------------------------------------------
313 // If this type is an enumeration, iterate through all of its enumerators
314 // using a callback. If the callback returns true, keep iterating, else
315 // abort the iteration.
316 //----------------------------------------------------------------------
317 void ForEachEnumerator(
318 std::function<bool(const CompilerType &integer_type,
319 const ConstString &name,
320 const llvm::APSInt &value)> const &callback) const;
322 uint32_t GetNumFields() const;
324 CompilerType GetFieldAtIndex(size_t idx, std::string &name,
325 uint64_t *bit_offset_ptr,
326 uint32_t *bitfield_bit_size_ptr,
327 bool *is_bitfield_ptr) const;
329 uint32_t GetNumDirectBaseClasses() const;
331 uint32_t GetNumVirtualBaseClasses() const;
333 CompilerType GetDirectBaseClassAtIndex(size_t idx,
334 uint32_t *bit_offset_ptr) const;
336 CompilerType GetVirtualBaseClassAtIndex(size_t idx,
337 uint32_t *bit_offset_ptr) const;
339 uint32_t GetIndexOfFieldWithName(const char *name,
340 CompilerType *field_compiler_type = nullptr,
341 uint64_t *bit_offset_ptr = nullptr,
342 uint32_t *bitfield_bit_size_ptr = nullptr,
343 bool *is_bitfield_ptr = nullptr) const;
345 CompilerType GetChildCompilerTypeAtIndex(
346 ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
347 bool omit_empty_base_classes, bool ignore_array_bounds,
348 std::string &child_name, uint32_t &child_byte_size,
349 int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
350 uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
351 bool &child_is_deref_of_parent, ValueObject *valobj,
352 uint64_t &language_flags) const;
354 // Lookup a child given a name. This function will match base class names
355 // and member member names in "clang_type" only, not descendants.
356 uint32_t GetIndexOfChildWithName(const char *name,
357 bool omit_empty_base_classes) const;
359 // Lookup a child member given a name. This function will match member names
360 // only and will descend into "clang_type" children in search for the first
361 // member in this class, or any base class that matches "name".
362 // TODO: Return all matches for a given name by returning a
363 // vector<vector<uint32_t>>
364 // so we catch all names that match a given child name, not just the first.
366 GetIndexOfChildMemberWithName(const char *name, bool omit_empty_base_classes,
367 std::vector<uint32_t> &child_indexes) const;
369 size_t GetNumTemplateArguments() const;
371 lldb::TemplateArgumentKind GetTemplateArgumentKind(size_t idx) const;
372 CompilerType GetTypeTemplateArgument(size_t idx) const;
374 // Returns the value of the template argument and its type.
375 llvm::Optional<IntegralTemplateArgument>
376 GetIntegralTemplateArgument(size_t idx) const;
378 CompilerType GetTypeForFormatters() const;
380 LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const;
382 bool IsMeaninglessWithoutDynamicResolution() const;
384 //------------------------------------------------------------------
385 // Pointers & References
386 //------------------------------------------------------------------
388 // Converts "s" to a floating point value and place resulting floating
389 // point bytes in the "dst" buffer.
390 size_t ConvertStringToFloatValue(const char *s, uint8_t *dst,
391 size_t dst_size) const;
393 //----------------------------------------------------------------------
395 //----------------------------------------------------------------------
396 void DumpValue(ExecutionContext *exe_ctx, Stream *s, lldb::Format format,
397 const DataExtractor &data, lldb::offset_t data_offset,
398 size_t data_byte_size, uint32_t bitfield_bit_size,
399 uint32_t bitfield_bit_offset, bool show_types,
400 bool show_summary, bool verbose, uint32_t depth);
402 bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data,
403 lldb::offset_t data_offset, size_t data_byte_size,
404 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
405 ExecutionContextScope *exe_scope);
407 void DumpSummary(ExecutionContext *exe_ctx, Stream *s,
408 const DataExtractor &data, lldb::offset_t data_offset,
409 size_t data_byte_size);
411 void DumpTypeDescription() const; // Dump to stdout
413 void DumpTypeDescription(Stream *s) const;
415 bool GetValueAsScalar(const DataExtractor &data, lldb::offset_t data_offset,
416 size_t data_byte_size, Scalar &value) const;
418 bool SetValueFromScalar(const Scalar &value, Stream &strm);
420 bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
421 AddressType address_type, DataExtractor &data);
423 bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t addr,
424 AddressType address_type, StreamString &new_value);
428 m_type_system = nullptr;
432 lldb::opaque_compiler_type_t m_type;
433 TypeSystem *m_type_system;
436 bool operator==(const CompilerType &lhs, const CompilerType &rhs);
437 bool operator!=(const CompilerType &lhs, const CompilerType &rhs);
439 struct CompilerType::IntegralTemplateArgument {
444 } // namespace lldb_private
446 #endif // liblldb_CompilerType_h_