1 //===-- TypeSystem.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_TypeSystem_h_
11 #define liblldb_TypeSystem_h_
20 // Other libraries and framework includes
21 #include "llvm/ADT/APSInt.h"
22 #include "llvm/Support/Casting.h"
25 #include "lldb/Core/PluginInterface.h"
26 #include "lldb/Expression/Expression.h"
27 #include "lldb/Symbol/CompilerDecl.h"
28 #include "lldb/Symbol/CompilerDeclContext.h"
29 #include "lldb/lldb-private.h"
34 namespace lldb_private {
36 //----------------------------------------------------------------------
37 // Interface for representing the Type Systems in different languages.
38 //----------------------------------------------------------------------
39 class TypeSystem : public PluginInterface {
41 //----------------------------------------------------------------------
42 // Intrusive type system that allows us to use llvm casting.
44 // To add a new type system:
46 // 1 - Add a new enumeration for llvm casting below for your TypeSystem
47 // subclass, here we will use eKindFoo
49 // 2 - Your TypeSystem subclass will inherit from TypeSystem and needs
50 // to implement a static classof() function that returns your
53 // class Foo : public lldb_private::TypeSystem
55 // static bool classof(const TypeSystem *ts)
57 // return ts->getKind() == TypeSystem::eKindFoo;
61 // 3 - Contruct your TypeSystem subclass with the enumeration from below
64 // TypeSystem(TypeSystem::eKindFoo),
69 // Then you can use the llvm casting on any "TypeSystem *" to get an
70 // instance of your subclass.
71 //----------------------------------------------------------------------
81 //----------------------------------------------------------------------
82 // Constructors and Destructors
83 //----------------------------------------------------------------------
84 TypeSystem(LLVMCastKind kind);
86 ~TypeSystem() override;
88 LLVMCastKind getKind() const { return m_kind; }
90 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language,
93 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language,
96 // Free up any resources associated with this TypeSystem. Done before
98 // all the TypeSystems from the TypeSystemMap.
99 virtual void Finalize() {}
101 virtual DWARFASTParser *GetDWARFParser() { return nullptr; }
103 virtual SymbolFile *GetSymbolFile() const { return m_sym_file; }
105 // Returns true if the symbol file changed during the set accessor.
106 virtual void SetSymbolFile(SymbolFile *sym_file) { m_sym_file = sym_file; }
108 //----------------------------------------------------------------------
109 // CompilerDecl functions
110 //----------------------------------------------------------------------
111 virtual ConstString DeclGetName(void *opaque_decl) = 0;
113 virtual ConstString DeclGetMangledName(void *opaque_decl);
115 virtual CompilerDeclContext DeclGetDeclContext(void *opaque_decl);
117 virtual CompilerType DeclGetFunctionReturnType(void *opaque_decl);
119 virtual size_t DeclGetFunctionNumArguments(void *opaque_decl);
121 virtual CompilerType DeclGetFunctionArgumentType(void *opaque_decl,
124 //----------------------------------------------------------------------
125 // CompilerDeclContext functions
126 //----------------------------------------------------------------------
128 virtual std::vector<CompilerDecl>
129 DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name,
130 const bool ignore_imported_decls);
132 virtual bool DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) = 0;
134 virtual ConstString DeclContextGetName(void *opaque_decl_ctx) = 0;
137 DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) = 0;
139 virtual bool DeclContextIsClassMethod(
140 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
141 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) = 0;
143 //----------------------------------------------------------------------
145 //----------------------------------------------------------------------
147 virtual bool IsArrayType(lldb::opaque_compiler_type_t type,
148 CompilerType *element_type, uint64_t *size,
149 bool *is_incomplete) = 0;
151 virtual bool IsAggregateType(lldb::opaque_compiler_type_t type) = 0;
153 virtual bool IsAnonymousType(lldb::opaque_compiler_type_t type);
155 virtual bool IsCharType(lldb::opaque_compiler_type_t type) = 0;
157 virtual bool IsCompleteType(lldb::opaque_compiler_type_t type) = 0;
159 virtual bool IsDefined(lldb::opaque_compiler_type_t type) = 0;
161 virtual bool IsFloatingPointType(lldb::opaque_compiler_type_t type,
162 uint32_t &count, bool &is_complex) = 0;
164 virtual bool IsFunctionType(lldb::opaque_compiler_type_t type,
165 bool *is_variadic_ptr) = 0;
168 GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) = 0;
171 GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
172 const size_t index) = 0;
174 virtual bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) = 0;
176 virtual bool IsBlockPointerType(lldb::opaque_compiler_type_t type,
177 CompilerType *function_pointer_type_ptr) = 0;
179 virtual bool IsIntegerType(lldb::opaque_compiler_type_t type,
180 bool &is_signed) = 0;
182 virtual bool IsEnumerationType(lldb::opaque_compiler_type_t type,
188 virtual bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
189 CompilerType *target_type, // Can pass NULL
190 bool check_cplusplus, bool check_objc) = 0;
192 virtual bool IsPointerType(lldb::opaque_compiler_type_t type,
193 CompilerType *pointee_type) = 0;
195 virtual bool IsScalarType(lldb::opaque_compiler_type_t type) = 0;
197 virtual bool IsVoidType(lldb::opaque_compiler_type_t type) = 0;
199 // TypeSystems can support more than one language
200 virtual bool SupportsLanguage(lldb::LanguageType language) = 0;
202 //----------------------------------------------------------------------
204 //----------------------------------------------------------------------
206 virtual bool GetCompleteType(lldb::opaque_compiler_type_t type) = 0;
208 //----------------------------------------------------------------------
209 // AST related queries
210 //----------------------------------------------------------------------
212 virtual uint32_t GetPointerByteSize() = 0;
214 //----------------------------------------------------------------------
216 //----------------------------------------------------------------------
218 virtual ConstString GetTypeName(lldb::opaque_compiler_type_t type) = 0;
221 GetTypeInfo(lldb::opaque_compiler_type_t type,
222 CompilerType *pointee_or_element_compiler_type) = 0;
224 virtual lldb::LanguageType
225 GetMinimumLanguage(lldb::opaque_compiler_type_t type) = 0;
227 virtual lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) = 0;
229 //----------------------------------------------------------------------
230 // Creating related types
231 //----------------------------------------------------------------------
233 virtual CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type,
234 uint64_t *stride) = 0;
236 virtual CompilerType GetArrayType(lldb::opaque_compiler_type_t type,
239 virtual CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) = 0;
241 // Returns -1 if this isn't a function of if the function doesn't have a
243 // Returns a value >= 0 if there is a prototype.
244 virtual int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) = 0;
247 GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type,
251 GetFunctionReturnType(lldb::opaque_compiler_type_t type) = 0;
253 virtual size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) = 0;
255 virtual TypeMemberFunctionImpl
256 GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, size_t idx) = 0;
258 virtual CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) = 0;
260 virtual CompilerType GetPointerType(lldb::opaque_compiler_type_t type) = 0;
263 GetLValueReferenceType(lldb::opaque_compiler_type_t type);
266 GetRValueReferenceType(lldb::opaque_compiler_type_t type);
268 virtual CompilerType AddConstModifier(lldb::opaque_compiler_type_t type);
270 virtual CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type);
272 virtual CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type);
274 virtual CompilerType CreateTypedef(lldb::opaque_compiler_type_t type,
276 const CompilerDeclContext &decl_ctx);
278 //----------------------------------------------------------------------
279 // Exploring the type
280 //----------------------------------------------------------------------
282 virtual uint64_t GetBitSize(lldb::opaque_compiler_type_t type,
283 ExecutionContextScope *exe_scope) = 0;
285 virtual lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type,
286 uint64_t &count) = 0;
288 virtual lldb::Format GetFormat(lldb::opaque_compiler_type_t type) = 0;
290 virtual uint32_t GetNumChildren(lldb::opaque_compiler_type_t type,
291 bool omit_empty_base_classes) = 0;
293 virtual CompilerType GetBuiltinTypeByName(const ConstString &name);
295 virtual lldb::BasicType
296 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) = 0;
298 virtual void ForEachEnumerator(
299 lldb::opaque_compiler_type_t type,
300 std::function<bool(const CompilerType &integer_type,
301 const ConstString &name,
302 const llvm::APSInt &value)> const &callback) {}
304 virtual uint32_t GetNumFields(lldb::opaque_compiler_type_t type) = 0;
306 virtual CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type,
307 size_t idx, std::string &name,
308 uint64_t *bit_offset_ptr,
309 uint32_t *bitfield_bit_size_ptr,
310 bool *is_bitfield_ptr) = 0;
313 GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) = 0;
316 GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) = 0;
319 GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
320 uint32_t *bit_offset_ptr) = 0;
323 GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
324 uint32_t *bit_offset_ptr) = 0;
326 virtual CompilerType GetChildCompilerTypeAtIndex(
327 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
328 bool transparent_pointers, bool omit_empty_base_classes,
329 bool ignore_array_bounds, std::string &child_name,
330 uint32_t &child_byte_size, int32_t &child_byte_offset,
331 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
332 bool &child_is_base_class, bool &child_is_deref_of_parent,
333 ValueObject *valobj, uint64_t &language_flags) = 0;
335 // Lookup a child given a name. This function will match base class names
336 // and member member names in "clang_type" only, not descendants.
337 virtual uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
339 bool omit_empty_base_classes) = 0;
341 // Lookup a child member given a name. This function will match member names
342 // only and will descend into "clang_type" children in search for the first
343 // member in this class, or any base class that matches "name".
344 // TODO: Return all matches for a given name by returning a
345 // vector<vector<uint32_t>>
346 // so we catch all names that match a given child name, not just the first.
348 GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type,
349 const char *name, bool omit_empty_base_classes,
350 std::vector<uint32_t> &child_indexes) = 0;
352 virtual size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type) = 0;
354 virtual lldb::TemplateArgumentKind
355 GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, size_t idx);
356 virtual CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
358 virtual llvm::Optional<CompilerType::IntegralTemplateArgument>
359 GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx);
361 //----------------------------------------------------------------------
363 //----------------------------------------------------------------------
365 virtual void DumpValue(lldb::opaque_compiler_type_t type,
366 ExecutionContext *exe_ctx, Stream *s,
367 lldb::Format format, const DataExtractor &data,
368 lldb::offset_t data_offset, size_t data_byte_size,
369 uint32_t bitfield_bit_size,
370 uint32_t bitfield_bit_offset, bool show_types,
371 bool show_summary, bool verbose, uint32_t depth) = 0;
373 virtual bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s,
374 lldb::Format format, const DataExtractor &data,
375 lldb::offset_t data_offset, size_t data_byte_size,
376 uint32_t bitfield_bit_size,
377 uint32_t bitfield_bit_offset,
378 ExecutionContextScope *exe_scope) = 0;
381 DumpTypeDescription(lldb::opaque_compiler_type_t type) = 0; // Dump to stdout
383 virtual void DumpTypeDescription(lldb::opaque_compiler_type_t type,
386 //----------------------------------------------------------------------
387 // TODO: These methods appear unused. Should they be removed?
388 //----------------------------------------------------------------------
390 virtual bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) = 0;
392 virtual void DumpSummary(lldb::opaque_compiler_type_t type,
393 ExecutionContext *exe_ctx, Stream *s,
394 const DataExtractor &data,
395 lldb::offset_t data_offset,
396 size_t data_byte_size) = 0;
398 // Converts "s" to a floating point value and place resulting floating
399 // point bytes in the "dst" buffer.
400 virtual size_t ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
401 const char *s, uint8_t *dst,
402 size_t dst_size) = 0;
404 //----------------------------------------------------------------------
405 // TODO: Determine if these methods should move to ClangASTContext.
406 //----------------------------------------------------------------------
408 virtual bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type,
409 CompilerType *pointee_type) = 0;
411 virtual unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) = 0;
413 virtual bool IsCStringType(lldb::opaque_compiler_type_t type,
414 uint32_t &length) = 0;
416 virtual size_t GetTypeBitAlign(lldb::opaque_compiler_type_t type) = 0;
418 virtual CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) = 0;
421 GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding,
422 size_t bit_size) = 0;
424 virtual bool IsBeingDefined(lldb::opaque_compiler_type_t type) = 0;
426 virtual bool IsConst(lldb::opaque_compiler_type_t type) = 0;
428 virtual uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
429 CompilerType *base_type_ptr) = 0;
431 virtual bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) = 0;
433 virtual bool IsTypedefType(lldb::opaque_compiler_type_t type) = 0;
435 // If the current object represents a typedef type, get the underlying type
436 virtual CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) = 0;
438 virtual bool IsVectorType(lldb::opaque_compiler_type_t type,
439 CompilerType *element_type, uint64_t *size) = 0;
442 GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) = 0;
445 GetNonReferenceType(lldb::opaque_compiler_type_t type) = 0;
447 virtual bool IsReferenceType(lldb::opaque_compiler_type_t type,
448 CompilerType *pointee_type, bool *is_rvalue) = 0;
451 ShouldTreatScalarValueAsAddress(lldb::opaque_compiler_type_t type) {
452 return IsPointerOrReferenceType(type, nullptr);
455 virtual UserExpression *
456 GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix,
457 lldb::LanguageType language,
458 Expression::ResultType desired_type,
459 const EvaluateExpressionOptions &options) {
463 virtual FunctionCaller *GetFunctionCaller(const CompilerType &return_type,
464 const Address &function_address,
465 const ValueList &arg_value_list,
470 virtual UtilityFunction *GetUtilityFunction(const char *text,
475 virtual PersistentExpressionState *GetPersistentExpressionState() {
479 virtual CompilerType GetTypeForFormatters(void *type);
481 virtual LazyBool ShouldPrintAsOneLiner(void *type, ValueObject *valobj);
483 // Type systems can have types that are placeholder types, which are meant to
485 // the presence of a type, but offer no actual information about said types,
487 // the burden of actually figuring type information out to dynamic type
488 // resolution. For instance
489 // a language with a generics system, can use placeholder types to indicate
490 // "type argument goes here",
491 // without promising uniqueness of the placeholder, nor attaching any actually
492 // idenfiable information
493 // to said placeholder. This API allows type systems to tell LLDB when such a
494 // type has been encountered
495 // In response, the debugger can react by not using this type as a cache entry
496 // in any type-specific way
497 // For instance, LLDB will currently not cache any formatters that are
498 // discovered on such a type as
499 // attributable to the meaningless type itself, instead preferring to use the
501 virtual bool IsMeaninglessWithoutDynamicResolution(void *type);
504 const LLVMCastKind m_kind; // Support for llvm casting
505 SymbolFile *m_sym_file;
508 class TypeSystemMap {
513 // Clear calls Finalize on all the TypeSystems managed by this map, and then
517 // Iterate through all of the type systems that are created. Return true
518 // from callback to keep iterating, false to stop iterating.
519 void ForEach(std::function<bool(TypeSystem *)> const &callback);
521 TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language,
522 Module *module, bool can_create);
524 TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language,
525 Target *target, bool can_create);
528 // This function does not take the map mutex, and should only be called from
529 // functions that do take the mutex.
530 void AddToMap(lldb::LanguageType language,
531 lldb::TypeSystemSP const &type_system_sp);
533 typedef std::map<lldb::LanguageType, lldb::TypeSystemSP> collection;
534 mutable std::mutex m_mutex; ///< A mutex to keep this object happy in
535 ///multi-threaded environments.
537 bool m_clear_in_progress;
540 } // namespace lldb_private
542 #endif // liblldb_TypeSystem_h_