1 //===-- Type.h --------------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef liblldb_Type_h_
10 #define liblldb_Type_h_
11 
12 #include "lldb/Symbol/CompilerDecl.h"
13 #include "lldb/Symbol/CompilerType.h"
14 #include "lldb/Symbol/Declaration.h"
15 #include "lldb/Utility/ConstString.h"
16 #include "lldb/Utility/UserID.h"
17 #include "lldb/lldb-private.h"
18 
19 #include "llvm/ADT/APSInt.h"
20 
21 #include <set>
22 
23 namespace lldb_private {
24 
25 /// CompilerContext allows an array of these items to be passed to perform
26 /// detailed lookups in SymbolVendor and SymbolFile functions.
27 struct CompilerContext {
28   CompilerContext(CompilerContextKind t, ConstString n) : kind(t), name(n) {}
29 
30   bool operator==(const CompilerContext &rhs) const {
31     return kind == rhs.kind && name == rhs.name;
32   }
33   bool operator!=(const CompilerContext &rhs) const { return !(*this == rhs); }
34 
35   void Dump() const;
36 
37   CompilerContextKind kind;
38   ConstString name;
39 };
40 
41 /// Match \p context_chain against \p pattern, which may contain "Any"
42 /// kinds. The \p context_chain should *not* contain any "Any" kinds.
43 bool contextMatches(llvm::ArrayRef<CompilerContext> context_chain,
44                     llvm::ArrayRef<CompilerContext> pattern);
45 
46 class SymbolFileType : public std::enable_shared_from_this<SymbolFileType>,
47                        public UserID {
48 public:
49   SymbolFileType(SymbolFile &symbol_file, lldb::user_id_t uid)
50       : UserID(uid), m_symbol_file(symbol_file) {}
51 
52   SymbolFileType(SymbolFile &symbol_file, const lldb::TypeSP &type_sp);
53 
54   ~SymbolFileType() {}
55 
56   Type *operator->() { return GetType(); }
57 
58   Type *GetType();
59 
60 protected:
61   SymbolFile &m_symbol_file;
62   lldb::TypeSP m_type_sp;
63 };
64 
65 class Type : public std::enable_shared_from_this<Type>, public UserID {
66 public:
67   enum EncodingDataType {
68     eEncodingInvalid,
69     eEncodingIsUID,      ///< This type is the type whose UID is m_encoding_uid
70     eEncodingIsConstUID, ///< This type is the type whose UID is m_encoding_uid
71                          /// with the const qualifier added
72     eEncodingIsRestrictUID, ///< This type is the type whose UID is
73                             /// m_encoding_uid with the restrict qualifier added
74     eEncodingIsVolatileUID, ///< This type is the type whose UID is
75                             /// m_encoding_uid with the volatile qualifier added
76     eEncodingIsTypedefUID,  ///< This type is pointer to a type whose UID is
77                             /// m_encoding_uid
78     eEncodingIsPointerUID,  ///< This type is pointer to a type whose UID is
79                             /// m_encoding_uid
80     eEncodingIsLValueReferenceUID, ///< This type is L value reference to a type
81                                    /// whose UID is m_encoding_uid
82     eEncodingIsRValueReferenceUID, ///< This type is R value reference to a type
83                                    /// whose UID is m_encoding_uid,
84     eEncodingIsAtomicUID,          ///< This type is the type whose UID is
85                                    /// m_encoding_uid as an atomic type.
86     eEncodingIsSyntheticUID
87   };
88 
89   enum class ResolveState : unsigned char {
90     Unresolved = 0,
91     Forward = 1,
92     Layout = 2,
93     Full = 3
94   };
95 
96   Type(lldb::user_id_t uid, SymbolFile *symbol_file, ConstString name,
97        llvm::Optional<uint64_t> byte_size, SymbolContextScope *context,
98        lldb::user_id_t encoding_uid, EncodingDataType encoding_uid_type,
99        const Declaration &decl, const CompilerType &compiler_qual_type,
100        ResolveState compiler_type_resolve_state);
101 
102   // This makes an invalid type.  Used for functions that return a Type when
103   // they get an error.
104   Type();
105 
106   void Dump(Stream *s, bool show_context);
107 
108   void DumpTypeName(Stream *s);
109 
110   // Since Type instances only keep a "SymbolFile *" internally, other classes
111   // like TypeImpl need make sure the module is still around before playing
112   // with
113   // Type instances. They can store a weak pointer to the Module;
114   lldb::ModuleSP GetModule();
115 
116   void GetDescription(Stream *s, lldb::DescriptionLevel level, bool show_name);
117 
118   SymbolFile *GetSymbolFile() { return m_symbol_file; }
119   const SymbolFile *GetSymbolFile() const { return m_symbol_file; }
120 
121   ConstString GetName();
122 
123   llvm::Optional<uint64_t> GetByteSize();
124 
125   uint32_t GetNumChildren(bool omit_empty_base_classes);
126 
127   bool IsAggregateType();
128 
129   bool IsValidType() { return m_encoding_uid_type != eEncodingInvalid; }
130 
131   bool IsTypedef() { return m_encoding_uid_type == eEncodingIsTypedefUID; }
132 
133   lldb::TypeSP GetTypedefType();
134 
135   ConstString GetName() const { return m_name; }
136 
137   ConstString GetQualifiedName();
138 
139   void DumpValue(ExecutionContext *exe_ctx, Stream *s,
140                  const DataExtractor &data, uint32_t data_offset,
141                  bool show_type, bool show_summary, bool verbose,
142                  lldb::Format format = lldb::eFormatDefault);
143 
144   bool DumpValueInMemory(ExecutionContext *exe_ctx, Stream *s,
145                          lldb::addr_t address, AddressType address_type,
146                          bool show_types, bool show_summary, bool verbose);
147 
148   bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t address,
149                       AddressType address_type, DataExtractor &data);
150 
151   bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t address,
152                      AddressType address_type, DataExtractor &data);
153 
154   bool GetIsDeclaration() const;
155 
156   void SetIsDeclaration(bool b);
157 
158   bool GetIsExternal() const;
159 
160   void SetIsExternal(bool b);
161 
162   lldb::Format GetFormat();
163 
164   lldb::Encoding GetEncoding(uint64_t &count);
165 
166   SymbolContextScope *GetSymbolContextScope() { return m_context; }
167   const SymbolContextScope *GetSymbolContextScope() const { return m_context; }
168   void SetSymbolContextScope(SymbolContextScope *context) {
169     m_context = context;
170   }
171 
172   const lldb_private::Declaration &GetDeclaration() const;
173 
174   // Get the clang type, and resolve definitions for any
175   // class/struct/union/enum types completely.
176   CompilerType GetFullCompilerType();
177 
178   // Get the clang type, and resolve definitions enough so that the type could
179   // have layout performed. This allows ptrs and refs to
180   // class/struct/union/enum types remain forward declarations.
181   CompilerType GetLayoutCompilerType();
182 
183   // Get the clang type and leave class/struct/union/enum types as forward
184   // declarations if they haven't already been fully defined.
185   CompilerType GetForwardCompilerType();
186 
187   static int Compare(const Type &a, const Type &b);
188 
189   // From a fully qualified typename, split the type into the type basename and
190   // the remaining type scope (namespaces/classes).
191   static bool GetTypeScopeAndBasename(const llvm::StringRef& name,
192                                       llvm::StringRef &scope,
193                                       llvm::StringRef &basename,
194                                       lldb::TypeClass &type_class);
195   void SetEncodingType(Type *encoding_type) { m_encoding_type = encoding_type; }
196 
197   uint32_t GetEncodingMask();
198 
199   bool IsCompleteObjCClass() { return m_is_complete_objc_class; }
200 
201   void SetIsCompleteObjCClass(bool is_complete_objc_class) {
202     m_is_complete_objc_class = is_complete_objc_class;
203   }
204 
205 protected:
206   ConstString m_name;
207   SymbolFile *m_symbol_file;
208   /// The symbol context in which this type is defined.
209   SymbolContextScope *m_context;
210   Type *m_encoding_type;
211   lldb::user_id_t m_encoding_uid;
212   EncodingDataType m_encoding_uid_type;
213   uint64_t m_byte_size : 63;
214   uint64_t m_byte_size_has_value : 1;
215   Declaration m_decl;
216   CompilerType m_compiler_type;
217   ResolveState m_compiler_type_resolve_state;
218   bool m_is_complete_objc_class;
219 
220   Type *GetEncodingType();
221 
222   bool ResolveClangType(ResolveState compiler_type_resolve_state);
223 };
224 
225 // the two classes here are used by the public API as a backend to the SBType
226 // and SBTypeList classes
227 
228 class TypeImpl {
229 public:
230   TypeImpl() = default;
231 
232   ~TypeImpl() {}
233 
234   TypeImpl(const lldb::TypeSP &type_sp);
235 
236   TypeImpl(const CompilerType &compiler_type);
237 
238   TypeImpl(const lldb::TypeSP &type_sp, const CompilerType &dynamic);
239 
240   TypeImpl(const CompilerType &compiler_type, const CompilerType &dynamic);
241 
242   void SetType(const lldb::TypeSP &type_sp);
243 
244   void SetType(const CompilerType &compiler_type);
245 
246   void SetType(const lldb::TypeSP &type_sp, const CompilerType &dynamic);
247 
248   void SetType(const CompilerType &compiler_type, const CompilerType &dynamic);
249 
250   bool operator==(const TypeImpl &rhs) const;
251 
252   bool operator!=(const TypeImpl &rhs) const;
253 
254   bool IsValid() const;
255 
256   explicit operator bool() const;
257 
258   void Clear();
259 
260   ConstString GetName() const;
261 
262   ConstString GetDisplayTypeName() const;
263 
264   TypeImpl GetPointerType() const;
265 
266   TypeImpl GetPointeeType() const;
267 
268   TypeImpl GetReferenceType() const;
269 
270   TypeImpl GetTypedefedType() const;
271 
272   TypeImpl GetDereferencedType() const;
273 
274   TypeImpl GetUnqualifiedType() const;
275 
276   TypeImpl GetCanonicalType() const;
277 
278   CompilerType GetCompilerType(bool prefer_dynamic);
279 
280   TypeSystem *GetTypeSystem(bool prefer_dynamic);
281 
282   bool GetDescription(lldb_private::Stream &strm,
283                       lldb::DescriptionLevel description_level);
284 
285 private:
286   bool CheckModule(lldb::ModuleSP &module_sp) const;
287 
288   lldb::ModuleWP m_module_wp;
289   CompilerType m_static_type;
290   CompilerType m_dynamic_type;
291 };
292 
293 class TypeListImpl {
294 public:
295   TypeListImpl() : m_content() {}
296 
297   void Append(const lldb::TypeImplSP &type) { m_content.push_back(type); }
298 
299   class AppendVisitor {
300   public:
301     AppendVisitor(TypeListImpl &type_list) : m_type_list(type_list) {}
302 
303     void operator()(const lldb::TypeImplSP &type) { m_type_list.Append(type); }
304 
305   private:
306     TypeListImpl &m_type_list;
307   };
308 
309   void Append(const lldb_private::TypeList &type_list);
310 
311   lldb::TypeImplSP GetTypeAtIndex(size_t idx) {
312     lldb::TypeImplSP type_sp;
313     if (idx < GetSize())
314       type_sp = m_content[idx];
315     return type_sp;
316   }
317 
318   size_t GetSize() { return m_content.size(); }
319 
320 private:
321   std::vector<lldb::TypeImplSP> m_content;
322 };
323 
324 class TypeMemberImpl {
325 public:
326   TypeMemberImpl()
327       : m_type_impl_sp(), m_bit_offset(0), m_name(), m_bitfield_bit_size(0),
328         m_is_bitfield(false)
329 
330   {}
331 
332   TypeMemberImpl(const lldb::TypeImplSP &type_impl_sp, uint64_t bit_offset,
333                  ConstString name, uint32_t bitfield_bit_size = 0,
334                  bool is_bitfield = false)
335       : m_type_impl_sp(type_impl_sp), m_bit_offset(bit_offset), m_name(name),
336         m_bitfield_bit_size(bitfield_bit_size), m_is_bitfield(is_bitfield) {}
337 
338   TypeMemberImpl(const lldb::TypeImplSP &type_impl_sp, uint64_t bit_offset)
339       : m_type_impl_sp(type_impl_sp), m_bit_offset(bit_offset), m_name(),
340         m_bitfield_bit_size(0), m_is_bitfield(false) {
341     if (m_type_impl_sp)
342       m_name = m_type_impl_sp->GetName();
343   }
344 
345   const lldb::TypeImplSP &GetTypeImpl() { return m_type_impl_sp; }
346 
347   ConstString GetName() const { return m_name; }
348 
349   uint64_t GetBitOffset() const { return m_bit_offset; }
350 
351   uint32_t GetBitfieldBitSize() const { return m_bitfield_bit_size; }
352 
353   void SetBitfieldBitSize(uint32_t bitfield_bit_size) {
354     m_bitfield_bit_size = bitfield_bit_size;
355   }
356 
357   bool GetIsBitfield() const { return m_is_bitfield; }
358 
359   void SetIsBitfield(bool is_bitfield) { m_is_bitfield = is_bitfield; }
360 
361 protected:
362   lldb::TypeImplSP m_type_impl_sp;
363   uint64_t m_bit_offset;
364   ConstString m_name;
365   uint32_t m_bitfield_bit_size; // Bit size for bitfield members only
366   bool m_is_bitfield;
367 };
368 
369 ///
370 /// Sometimes you can find the name of the type corresponding to an object, but
371 /// we don't have debug
372 /// information for it.  If that is the case, you can return one of these
373 /// objects, and then if it
374 /// has a full type, you can use that, but if not at least you can print the
375 /// name for informational
376 /// purposes.
377 ///
378 
379 class TypeAndOrName {
380 public:
381   TypeAndOrName() = default;
382   TypeAndOrName(lldb::TypeSP &type_sp);
383   TypeAndOrName(const CompilerType &compiler_type);
384   TypeAndOrName(const char *type_str);
385   TypeAndOrName(ConstString &type_const_string);
386 
387   bool operator==(const TypeAndOrName &other) const;
388 
389   bool operator!=(const TypeAndOrName &other) const;
390 
391   ConstString GetName() const;
392 
393   CompilerType GetCompilerType() const { return m_compiler_type; }
394 
395   void SetName(ConstString type_name);
396 
397   void SetName(const char *type_name_cstr);
398 
399   void SetTypeSP(lldb::TypeSP type_sp);
400 
401   void SetCompilerType(CompilerType compiler_type);
402 
403   bool IsEmpty() const;
404 
405   bool HasName() const;
406 
407   bool HasCompilerType() const;
408 
409   bool HasType() const { return HasCompilerType(); }
410 
411   void Clear();
412 
413   explicit operator bool() { return !IsEmpty(); }
414 
415 private:
416   CompilerType m_compiler_type;
417   ConstString m_type_name;
418 };
419 
420 class TypeMemberFunctionImpl {
421 public:
422   TypeMemberFunctionImpl()
423       : m_type(), m_decl(), m_name(), m_kind(lldb::eMemberFunctionKindUnknown) {
424   }
425 
426   TypeMemberFunctionImpl(const CompilerType &type, const CompilerDecl &decl,
427                          const std::string &name,
428                          const lldb::MemberFunctionKind &kind)
429       : m_type(type), m_decl(decl), m_name(name), m_kind(kind) {}
430 
431   bool IsValid();
432 
433   ConstString GetName() const;
434 
435   ConstString GetMangledName() const;
436 
437   CompilerType GetType() const;
438 
439   CompilerType GetReturnType() const;
440 
441   size_t GetNumArguments() const;
442 
443   CompilerType GetArgumentAtIndex(size_t idx) const;
444 
445   lldb::MemberFunctionKind GetKind() const;
446 
447   bool GetDescription(Stream &stream);
448 
449 protected:
450   std::string GetPrintableTypeName();
451 
452 private:
453   CompilerType m_type;
454   CompilerDecl m_decl;
455   ConstString m_name;
456   lldb::MemberFunctionKind m_kind;
457 };
458 
459 class TypeEnumMemberImpl {
460 public:
461   TypeEnumMemberImpl()
462       : m_integer_type_sp(), m_name("<invalid>"), m_value(), m_valid(false) {}
463 
464   TypeEnumMemberImpl(const lldb::TypeImplSP &integer_type_sp,
465                      ConstString name, const llvm::APSInt &value);
466 
467   TypeEnumMemberImpl(const TypeEnumMemberImpl &rhs) = default;
468 
469   TypeEnumMemberImpl &operator=(const TypeEnumMemberImpl &rhs);
470 
471   bool IsValid() { return m_valid; }
472 
473   ConstString GetName() const { return m_name; }
474 
475   const lldb::TypeImplSP &GetIntegerType() const { return m_integer_type_sp; }
476 
477   uint64_t GetValueAsUnsigned() const { return m_value.getZExtValue(); }
478 
479   int64_t GetValueAsSigned() const { return m_value.getSExtValue(); }
480 
481 protected:
482   lldb::TypeImplSP m_integer_type_sp;
483   ConstString m_name;
484   llvm::APSInt m_value;
485   bool m_valid;
486 };
487 
488 class TypeEnumMemberListImpl {
489 public:
490   TypeEnumMemberListImpl() : m_content() {}
491 
492   void Append(const lldb::TypeEnumMemberImplSP &type) {
493     m_content.push_back(type);
494   }
495 
496   void Append(const lldb_private::TypeEnumMemberListImpl &type_list);
497 
498   lldb::TypeEnumMemberImplSP GetTypeEnumMemberAtIndex(size_t idx) {
499     lldb::TypeEnumMemberImplSP enum_member;
500     if (idx < GetSize())
501       enum_member = m_content[idx];
502     return enum_member;
503   }
504 
505   size_t GetSize() { return m_content.size(); }
506 
507 private:
508   std::vector<lldb::TypeEnumMemberImplSP> m_content;
509 };
510 
511 } // namespace lldb_private
512 
513 #endif // liblldb_Type_h_
514