1 //===-- CompilerType.h ------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_CompilerType_h_
10 #define liblldb_CompilerType_h_
16 #include "lldb/lldb-private.h"
17 #include "llvm/ADT/APSInt.h"
19 namespace lldb_private {
23 /// Represents a generic type in a programming language.
25 /// This class serves as an abstraction for a type inside one of the TypeSystems
26 /// implemented by the language plugins. It does not have any actual logic in it
27 /// but only stores an opaque pointer and a pointer to the TypeSystem that
28 /// gives meaning to this opaque pointer. All methods of this class should call
29 /// their respective method in the TypeSystem interface and pass the opaque
32 /// \see lldb_private::TypeSystem
35 /// Creates a CompilerType with the given TypeSystem and opaque compiler type.
37 /// This constructor should only be called from the respective TypeSystem
40 /// \see lldb_private::ClangASTContext::GetType(clang::QualType)
41 CompilerType(TypeSystem *type_system, lldb::opaque_compiler_type_t type)
42 : m_type(type), m_type_system(type_system) {}
44 CompilerType(const CompilerType &rhs)
45 : m_type(rhs.m_type), m_type_system(rhs.m_type_system) {}
47 CompilerType() = default;
51 const CompilerType &operator=(const CompilerType &rhs) {
53 m_type_system = rhs.m_type_system;
59 explicit operator bool() const {
60 return m_type != nullptr && m_type_system != nullptr;
63 bool operator<(const CompilerType &rhs) const {
64 if (m_type_system == rhs.m_type_system)
65 return m_type < rhs.m_type;
66 return m_type_system < rhs.m_type_system;
69 bool IsValid() const { return m_type != nullptr && m_type_system != nullptr; }
71 bool IsArrayType(CompilerType *element_type, uint64_t *size,
72 bool *is_incomplete) const;
74 bool IsVectorType(CompilerType *element_type, uint64_t *size) const;
76 bool IsArrayOfScalarType() const;
78 bool IsAggregateType() const;
80 bool IsAnonymousType() const;
82 bool IsBeingDefined() const;
84 bool IsCharType() const;
86 bool IsCompleteType() const;
90 bool IsCStringType(uint32_t &length) const;
92 bool IsDefined() const;
94 bool IsFloatingPointType(uint32_t &count, bool &is_complex) const;
96 bool IsFunctionType(bool *is_variadic_ptr = nullptr) const;
98 uint32_t IsHomogeneousAggregate(CompilerType *base_type_ptr) const;
100 size_t GetNumberOfFunctionArguments() const;
102 CompilerType GetFunctionArgumentAtIndex(const size_t index) const;
104 bool IsVariadicFunctionType() const;
106 bool IsFunctionPointerType() const;
108 bool IsBlockPointerType(CompilerType *function_pointer_type_ptr) const;
110 bool IsIntegerType(bool &is_signed) const;
112 bool IsEnumerationType(bool &is_signed) const;
114 bool IsIntegerOrEnumerationType(bool &is_signed) const;
116 bool IsPolymorphicClass() const;
118 bool IsPossibleDynamicType(CompilerType *target_type, // Can pass nullptr
119 bool check_cplusplus, bool check_objc) const;
121 bool IsPointerToScalarType() const;
123 bool IsRuntimeGeneratedType() const;
125 bool IsPointerType(CompilerType *pointee_type = nullptr) const;
127 bool IsPointerOrReferenceType(CompilerType *pointee_type = nullptr) const;
129 bool IsReferenceType(CompilerType *pointee_type = nullptr,
130 bool *is_rvalue = nullptr) const;
132 bool ShouldTreatScalarValueAsAddress() const;
134 bool IsScalarType() const;
136 bool IsTypedefType() const;
138 bool IsVoidType() const;
142 bool GetCompleteType() const;
144 // AST related queries
146 size_t GetPointerByteSize() const;
150 TypeSystem *GetTypeSystem() const { return m_type_system; }
152 ConstString GetConstQualifiedTypeName() const;
154 ConstString GetConstTypeName() const;
156 ConstString GetTypeName() const;
158 ConstString GetDisplayTypeName() const;
161 GetTypeInfo(CompilerType *pointee_or_element_compiler_type = nullptr) const;
163 lldb::LanguageType GetMinimumLanguage();
165 lldb::opaque_compiler_type_t GetOpaqueQualType() const { return m_type; }
167 lldb::TypeClass GetTypeClass() const;
169 void SetCompilerType(TypeSystem *type_system,
170 lldb::opaque_compiler_type_t type);
172 unsigned GetTypeQualifiers() const;
174 // Creating related types
176 CompilerType GetArrayElementType(uint64_t *stride = nullptr) const;
178 CompilerType GetArrayType(uint64_t size) const;
180 CompilerType GetCanonicalType() const;
182 CompilerType GetFullyUnqualifiedType() const;
184 // Returns -1 if this isn't a function of if the function doesn't have a
185 // prototype Returns a value >= 0 if there is a prototype.
186 int GetFunctionArgumentCount() const;
188 CompilerType GetFunctionArgumentTypeAtIndex(size_t idx) const;
190 CompilerType GetFunctionReturnType() const;
192 size_t GetNumMemberFunctions() const;
194 TypeMemberFunctionImpl GetMemberFunctionAtIndex(size_t idx);
196 // If this type is a reference to a type (L value or R value reference),
197 // return a new type with the reference removed, else return the current type
199 CompilerType GetNonReferenceType() const;
201 // If this type is a pointer type, return the type that the pointer points
202 // to, else return an invalid type.
203 CompilerType GetPointeeType() const;
205 // Return a new CompilerType that is a pointer to this type
206 CompilerType GetPointerType() const;
208 // Return a new CompilerType that is a L value reference to this type if this
209 // type is valid and the type system supports L value references, else return
211 CompilerType GetLValueReferenceType() const;
213 // Return a new CompilerType that is a R value reference to this type if this
214 // type is valid and the type system supports R value references, else return
216 CompilerType GetRValueReferenceType() const;
218 // Return a new CompilerType adds a const modifier to this type if this type
219 // is valid and the type system supports const modifiers, else return an
221 CompilerType AddConstModifier() const;
223 // Return a new CompilerType adds a volatile modifier to this type if this
224 // type is valid and the type system supports volatile modifiers, else return
226 CompilerType AddVolatileModifier() const;
228 // Return a new CompilerType that is the atomic type of this type. If this
229 // type is not valid or the type system doesn't support atomic types, this
230 // returns an invalid type.
231 CompilerType GetAtomicType() const;
233 // Return a new CompilerType adds a restrict modifier to this type if this
234 // type is valid and the type system supports restrict modifiers, else return
236 CompilerType AddRestrictModifier() const;
238 // Create a typedef to this type using "name" as the name of the typedef this
239 // type is valid and the type system supports typedefs, else return an
241 CompilerType CreateTypedef(const char *name,
242 const CompilerDeclContext &decl_ctx) const;
244 // If the current object represents a typedef type, get the underlying type
245 CompilerType GetTypedefedType() const;
247 // Create related types using the current type's AST
248 CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const;
250 // Exploring the type
252 struct IntegralTemplateArgument;
254 /// Return the size of the type in bytes.
255 llvm::Optional<uint64_t> GetByteSize(ExecutionContextScope *exe_scope) const;
256 /// Return the size of the type in bits.
257 llvm::Optional<uint64_t> GetBitSize(ExecutionContextScope *exe_scope) const;
259 lldb::Encoding GetEncoding(uint64_t &count) const;
261 lldb::Format GetFormat() const;
263 llvm::Optional<size_t> GetTypeBitAlign(ExecutionContextScope *exe_scope) const;
265 uint32_t GetNumChildren(bool omit_empty_base_classes,
266 const ExecutionContext *exe_ctx) const;
268 lldb::BasicType GetBasicTypeEnumeration() const;
270 static lldb::BasicType GetBasicTypeEnumeration(ConstString name);
272 // If this type is an enumeration, iterate through all of its enumerators
273 // using a callback. If the callback returns true, keep iterating, else abort
275 void ForEachEnumerator(
276 std::function<bool(const CompilerType &integer_type,
278 const llvm::APSInt &value)> const &callback) const;
280 uint32_t GetNumFields() const;
282 CompilerType GetFieldAtIndex(size_t idx, std::string &name,
283 uint64_t *bit_offset_ptr,
284 uint32_t *bitfield_bit_size_ptr,
285 bool *is_bitfield_ptr) const;
287 uint32_t GetNumDirectBaseClasses() const;
289 uint32_t GetNumVirtualBaseClasses() const;
291 CompilerType GetDirectBaseClassAtIndex(size_t idx,
292 uint32_t *bit_offset_ptr) const;
294 CompilerType GetVirtualBaseClassAtIndex(size_t idx,
295 uint32_t *bit_offset_ptr) const;
297 uint32_t GetIndexOfFieldWithName(const char *name,
298 CompilerType *field_compiler_type = nullptr,
299 uint64_t *bit_offset_ptr = nullptr,
300 uint32_t *bitfield_bit_size_ptr = nullptr,
301 bool *is_bitfield_ptr = nullptr) const;
303 CompilerType GetChildCompilerTypeAtIndex(
304 ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
305 bool omit_empty_base_classes, bool ignore_array_bounds,
306 std::string &child_name, uint32_t &child_byte_size,
307 int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
308 uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
309 bool &child_is_deref_of_parent, ValueObject *valobj,
310 uint64_t &language_flags) const;
312 // Lookup a child given a name. This function will match base class names and
313 // member member names in "clang_type" only, not descendants.
314 uint32_t GetIndexOfChildWithName(const char *name,
315 bool omit_empty_base_classes) const;
317 // Lookup a child member given a name. This function will match member names
318 // only and will descend into "clang_type" children in search for the first
319 // member in this class, or any base class that matches "name".
320 // TODO: Return all matches for a given name by returning a
321 // vector<vector<uint32_t>>
322 // so we catch all names that match a given child name, not just the first.
324 GetIndexOfChildMemberWithName(const char *name, bool omit_empty_base_classes,
325 std::vector<uint32_t> &child_indexes) const;
327 size_t GetNumTemplateArguments() const;
329 lldb::TemplateArgumentKind GetTemplateArgumentKind(size_t idx) const;
330 CompilerType GetTypeTemplateArgument(size_t idx) const;
332 // Returns the value of the template argument and its type.
333 llvm::Optional<IntegralTemplateArgument>
334 GetIntegralTemplateArgument(size_t idx) const;
336 CompilerType GetTypeForFormatters() const;
338 LazyBool ShouldPrintAsOneLiner(ValueObject *valobj) const;
340 bool IsMeaninglessWithoutDynamicResolution() const;
345 /// Convenience LLVM-style dump method for use in the debugger only.
346 /// Don't call this function from actual code.
347 LLVM_DUMP_METHOD void dump() const;
350 void DumpValue(ExecutionContext *exe_ctx, Stream *s, lldb::Format format,
351 const DataExtractor &data, lldb::offset_t data_offset,
352 size_t data_byte_size, uint32_t bitfield_bit_size,
353 uint32_t bitfield_bit_offset, bool show_types,
354 bool show_summary, bool verbose, uint32_t depth);
356 bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data,
357 lldb::offset_t data_offset, size_t data_byte_size,
358 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
359 ExecutionContextScope *exe_scope);
361 void DumpSummary(ExecutionContext *exe_ctx, Stream *s,
362 const DataExtractor &data, lldb::offset_t data_offset,
363 size_t data_byte_size);
365 void DumpTypeDescription() const; // Dump to stdout
367 void DumpTypeDescription(Stream *s) const;
369 bool GetValueAsScalar(const DataExtractor &data, lldb::offset_t data_offset,
370 size_t data_byte_size, Scalar &value) const;
374 m_type_system = nullptr;
378 lldb::opaque_compiler_type_t m_type = nullptr;
379 TypeSystem *m_type_system = nullptr;
382 bool operator==(const CompilerType &lhs, const CompilerType &rhs);
383 bool operator!=(const CompilerType &lhs, const CompilerType &rhs);
385 struct CompilerType::IntegralTemplateArgument {
390 } // namespace lldb_private
392 #endif // liblldb_CompilerType_h_