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