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_
18 #include "llvm/ADT/APSInt.h"
19 #include "llvm/Support/Casting.h"
21 #include "lldb/Core/PluginInterface.h"
22 #include "lldb/Expression/Expression.h"
23 #include "lldb/Symbol/CompilerDecl.h"
24 #include "lldb/Symbol/CompilerDeclContext.h"
25 #include "lldb/lldb-private.h"
30 namespace lldb_private {
32 //----------------------------------------------------------------------
33 // Interface for representing the Type Systems in different languages.
34 //----------------------------------------------------------------------
35 class TypeSystem : public PluginInterface {
37 //----------------------------------------------------------------------
38 // Intrusive type system that allows us to use llvm casting.
40 // To add a new type system:
42 // 1 - Add a new enumeration for llvm casting below for your TypeSystem
43 // subclass, here we will use eKindFoo
45 // 2 - Your TypeSystem subclass will inherit from TypeSystem and needs
46 // to implement a static classof() function that returns your
49 // class Foo : public lldb_private::TypeSystem
51 // static bool classof(const TypeSystem *ts)
53 // return ts->getKind() == TypeSystem::eKindFoo;
57 // 3 - Contruct your TypeSystem subclass with the enumeration from below
60 // TypeSystem(TypeSystem::eKindFoo),
65 // Then you can use the llvm casting on any "TypeSystem *" to get an instance
67 //----------------------------------------------------------------------
75 //----------------------------------------------------------------------
76 // Constructors and Destructors
77 //----------------------------------------------------------------------
78 TypeSystem(LLVMCastKind kind);
80 ~TypeSystem() override;
82 LLVMCastKind getKind() const { return m_kind; }
84 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language,
87 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language,
90 // Free up any resources associated with this TypeSystem. Done before
91 // removing all the TypeSystems from the TypeSystemMap.
92 virtual void Finalize() {}
94 virtual DWARFASTParser *GetDWARFParser() { return nullptr; }
96 virtual SymbolFile *GetSymbolFile() const { return m_sym_file; }
98 // Returns true if the symbol file changed during the set accessor.
99 virtual void SetSymbolFile(SymbolFile *sym_file) { m_sym_file = sym_file; }
101 //----------------------------------------------------------------------
102 // CompilerDecl functions
103 //----------------------------------------------------------------------
104 virtual ConstString DeclGetName(void *opaque_decl) = 0;
106 virtual ConstString DeclGetMangledName(void *opaque_decl);
108 virtual CompilerDeclContext DeclGetDeclContext(void *opaque_decl);
110 virtual CompilerType DeclGetFunctionReturnType(void *opaque_decl);
112 virtual size_t DeclGetFunctionNumArguments(void *opaque_decl);
114 virtual CompilerType DeclGetFunctionArgumentType(void *opaque_decl,
117 //----------------------------------------------------------------------
118 // CompilerDeclContext functions
119 //----------------------------------------------------------------------
121 virtual std::vector<CompilerDecl>
122 DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name,
123 const bool ignore_imported_decls);
125 virtual bool DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) = 0;
127 virtual ConstString DeclContextGetName(void *opaque_decl_ctx) = 0;
130 DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) = 0;
132 virtual bool DeclContextIsClassMethod(
133 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
134 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) = 0;
136 //----------------------------------------------------------------------
138 //----------------------------------------------------------------------
140 virtual bool IsArrayType(lldb::opaque_compiler_type_t type,
141 CompilerType *element_type, uint64_t *size,
142 bool *is_incomplete) = 0;
144 virtual bool IsAggregateType(lldb::opaque_compiler_type_t type) = 0;
146 virtual bool IsAnonymousType(lldb::opaque_compiler_type_t type);
148 virtual bool IsCharType(lldb::opaque_compiler_type_t type) = 0;
150 virtual bool IsCompleteType(lldb::opaque_compiler_type_t type) = 0;
152 virtual bool IsDefined(lldb::opaque_compiler_type_t type) = 0;
154 virtual bool IsFloatingPointType(lldb::opaque_compiler_type_t type,
155 uint32_t &count, bool &is_complex) = 0;
157 virtual bool IsFunctionType(lldb::opaque_compiler_type_t type,
158 bool *is_variadic_ptr) = 0;
161 GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) = 0;
164 GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
165 const size_t index) = 0;
167 virtual bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) = 0;
169 virtual bool IsBlockPointerType(lldb::opaque_compiler_type_t type,
170 CompilerType *function_pointer_type_ptr) = 0;
172 virtual bool IsIntegerType(lldb::opaque_compiler_type_t type,
173 bool &is_signed) = 0;
175 virtual bool IsEnumerationType(lldb::opaque_compiler_type_t type,
181 virtual bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
182 CompilerType *target_type, // Can pass NULL
183 bool check_cplusplus, bool check_objc) = 0;
185 virtual bool IsPointerType(lldb::opaque_compiler_type_t type,
186 CompilerType *pointee_type) = 0;
188 virtual bool IsScalarType(lldb::opaque_compiler_type_t type) = 0;
190 virtual bool IsVoidType(lldb::opaque_compiler_type_t type) = 0;
192 // TypeSystems can support more than one language
193 virtual bool SupportsLanguage(lldb::LanguageType language) = 0;
195 //----------------------------------------------------------------------
197 //----------------------------------------------------------------------
199 virtual bool GetCompleteType(lldb::opaque_compiler_type_t type) = 0;
201 //----------------------------------------------------------------------
202 // AST related queries
203 //----------------------------------------------------------------------
205 virtual uint32_t GetPointerByteSize() = 0;
207 //----------------------------------------------------------------------
209 //----------------------------------------------------------------------
211 virtual ConstString GetTypeName(lldb::opaque_compiler_type_t type) = 0;
214 GetTypeInfo(lldb::opaque_compiler_type_t type,
215 CompilerType *pointee_or_element_compiler_type) = 0;
217 virtual lldb::LanguageType
218 GetMinimumLanguage(lldb::opaque_compiler_type_t type) = 0;
220 virtual lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) = 0;
222 //----------------------------------------------------------------------
223 // Creating related types
224 //----------------------------------------------------------------------
226 virtual CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type,
227 uint64_t *stride) = 0;
229 virtual CompilerType GetArrayType(lldb::opaque_compiler_type_t type,
232 virtual CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) = 0;
234 // Returns -1 if this isn't a function of if the function doesn't have a
235 // prototype Returns a value >= 0 if there is a prototype.
236 virtual int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) = 0;
239 GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type,
243 GetFunctionReturnType(lldb::opaque_compiler_type_t type) = 0;
245 virtual size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) = 0;
247 virtual TypeMemberFunctionImpl
248 GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, size_t idx) = 0;
250 virtual CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) = 0;
252 virtual CompilerType GetPointerType(lldb::opaque_compiler_type_t type) = 0;
255 GetLValueReferenceType(lldb::opaque_compiler_type_t type);
258 GetRValueReferenceType(lldb::opaque_compiler_type_t type);
260 virtual CompilerType AddConstModifier(lldb::opaque_compiler_type_t type);
262 virtual CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type);
264 virtual CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type);
266 virtual CompilerType CreateTypedef(lldb::opaque_compiler_type_t type,
268 const CompilerDeclContext &decl_ctx);
270 //----------------------------------------------------------------------
271 // Exploring the type
272 //----------------------------------------------------------------------
274 virtual uint64_t GetBitSize(lldb::opaque_compiler_type_t type,
275 ExecutionContextScope *exe_scope) = 0;
277 virtual lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type,
278 uint64_t &count) = 0;
280 virtual lldb::Format GetFormat(lldb::opaque_compiler_type_t type) = 0;
282 virtual uint32_t GetNumChildren(lldb::opaque_compiler_type_t type,
283 bool omit_empty_base_classes,
284 const ExecutionContext *exe_ctx) = 0;
286 virtual CompilerType GetBuiltinTypeByName(const ConstString &name);
288 virtual lldb::BasicType
289 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) = 0;
291 virtual void ForEachEnumerator(
292 lldb::opaque_compiler_type_t type,
293 std::function<bool(const CompilerType &integer_type,
294 const ConstString &name,
295 const llvm::APSInt &value)> const &callback) {}
297 virtual uint32_t GetNumFields(lldb::opaque_compiler_type_t type) = 0;
299 virtual CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type,
300 size_t idx, std::string &name,
301 uint64_t *bit_offset_ptr,
302 uint32_t *bitfield_bit_size_ptr,
303 bool *is_bitfield_ptr) = 0;
306 GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) = 0;
309 GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) = 0;
312 GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
313 uint32_t *bit_offset_ptr) = 0;
316 GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
317 uint32_t *bit_offset_ptr) = 0;
319 virtual CompilerType GetChildCompilerTypeAtIndex(
320 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
321 bool transparent_pointers, bool omit_empty_base_classes,
322 bool ignore_array_bounds, std::string &child_name,
323 uint32_t &child_byte_size, int32_t &child_byte_offset,
324 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
325 bool &child_is_base_class, bool &child_is_deref_of_parent,
326 ValueObject *valobj, uint64_t &language_flags) = 0;
328 // Lookup a child given a name. This function will match base class names and
329 // member member names in "clang_type" only, not descendants.
330 virtual uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
332 bool omit_empty_base_classes) = 0;
334 // Lookup a child member given a name. This function will match member names
335 // only and will descend into "clang_type" children in search for the first
336 // member in this class, or any base class that matches "name".
337 // TODO: Return all matches for a given name by returning a
338 // vector<vector<uint32_t>>
339 // so we catch all names that match a given child name, not just the first.
341 GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type,
342 const char *name, bool omit_empty_base_classes,
343 std::vector<uint32_t> &child_indexes) = 0;
345 virtual size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type);
347 virtual lldb::TemplateArgumentKind
348 GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, size_t idx);
349 virtual CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
351 virtual llvm::Optional<CompilerType::IntegralTemplateArgument>
352 GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx);
354 //----------------------------------------------------------------------
356 //----------------------------------------------------------------------
358 virtual void DumpValue(lldb::opaque_compiler_type_t type,
359 ExecutionContext *exe_ctx, Stream *s,
360 lldb::Format format, const DataExtractor &data,
361 lldb::offset_t data_offset, size_t data_byte_size,
362 uint32_t bitfield_bit_size,
363 uint32_t bitfield_bit_offset, bool show_types,
364 bool show_summary, bool verbose, uint32_t depth) = 0;
366 virtual bool DumpTypeValue(lldb::opaque_compiler_type_t type, 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,
371 ExecutionContextScope *exe_scope) = 0;
374 DumpTypeDescription(lldb::opaque_compiler_type_t type) = 0; // Dump to stdout
376 virtual void DumpTypeDescription(lldb::opaque_compiler_type_t type,
379 //----------------------------------------------------------------------
380 // TODO: These methods appear unused. Should they be removed?
381 //----------------------------------------------------------------------
383 virtual bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) = 0;
385 virtual void DumpSummary(lldb::opaque_compiler_type_t type,
386 ExecutionContext *exe_ctx, Stream *s,
387 const DataExtractor &data,
388 lldb::offset_t data_offset,
389 size_t data_byte_size) = 0;
391 // Converts "s" to a floating point value and place resulting floating point
392 // bytes in the "dst" buffer.
393 virtual size_t ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
394 const char *s, uint8_t *dst,
395 size_t dst_size) = 0;
397 //----------------------------------------------------------------------
398 // TODO: Determine if these methods should move to ClangASTContext.
399 //----------------------------------------------------------------------
401 virtual bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type,
402 CompilerType *pointee_type) = 0;
404 virtual unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) = 0;
406 virtual bool IsCStringType(lldb::opaque_compiler_type_t type,
407 uint32_t &length) = 0;
409 virtual size_t GetTypeBitAlign(lldb::opaque_compiler_type_t type) = 0;
411 virtual CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) = 0;
414 GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding,
415 size_t bit_size) = 0;
417 virtual bool IsBeingDefined(lldb::opaque_compiler_type_t type) = 0;
419 virtual bool IsConst(lldb::opaque_compiler_type_t type) = 0;
421 virtual uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
422 CompilerType *base_type_ptr) = 0;
424 virtual bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) = 0;
426 virtual bool IsTypedefType(lldb::opaque_compiler_type_t type) = 0;
428 // If the current object represents a typedef type, get the underlying type
429 virtual CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) = 0;
431 virtual bool IsVectorType(lldb::opaque_compiler_type_t type,
432 CompilerType *element_type, uint64_t *size) = 0;
435 GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) = 0;
438 GetNonReferenceType(lldb::opaque_compiler_type_t type) = 0;
440 virtual bool IsReferenceType(lldb::opaque_compiler_type_t type,
441 CompilerType *pointee_type, bool *is_rvalue) = 0;
444 ShouldTreatScalarValueAsAddress(lldb::opaque_compiler_type_t type) {
445 return IsPointerOrReferenceType(type, nullptr);
448 virtual UserExpression *
449 GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix,
450 lldb::LanguageType language,
451 Expression::ResultType desired_type,
452 const EvaluateExpressionOptions &options) {
456 virtual FunctionCaller *GetFunctionCaller(const CompilerType &return_type,
457 const Address &function_address,
458 const ValueList &arg_value_list,
463 virtual UtilityFunction *GetUtilityFunction(const char *text,
468 virtual PersistentExpressionState *GetPersistentExpressionState() {
472 virtual CompilerType GetTypeForFormatters(void *type);
474 virtual LazyBool ShouldPrintAsOneLiner(void *type, ValueObject *valobj);
476 // Type systems can have types that are placeholder types, which are meant to
477 // indicate the presence of a type, but offer no actual information about
478 // said types, and leave the burden of actually figuring type information out
479 // to dynamic type resolution. For instance a language with a generics
480 // system, can use placeholder types to indicate "type argument goes here",
481 // without promising uniqueness of the placeholder, nor attaching any
482 // actually idenfiable information to said placeholder. This API allows type
483 // systems to tell LLDB when such a type has been encountered In response,
484 // the debugger can react by not using this type as a cache entry in any
485 // type-specific way For instance, LLDB will currently not cache any
486 // formatters that are discovered on such a type as attributable to the
487 // meaningless type itself, instead preferring to use the dynamic type
488 virtual bool IsMeaninglessWithoutDynamicResolution(void *type);
491 const LLVMCastKind m_kind; // Support for llvm casting
492 SymbolFile *m_sym_file;
495 class TypeSystemMap {
500 // Clear calls Finalize on all the TypeSystems managed by this map, and then
504 // Iterate through all of the type systems that are created. Return true from
505 // callback to keep iterating, false to stop iterating.
506 void ForEach(std::function<bool(TypeSystem *)> const &callback);
508 TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language,
509 Module *module, bool can_create);
511 TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language,
512 Target *target, bool can_create);
515 // This function does not take the map mutex, and should only be called from
516 // functions that do take the mutex.
517 void AddToMap(lldb::LanguageType language,
518 lldb::TypeSystemSP const &type_system_sp);
520 typedef std::map<lldb::LanguageType, lldb::TypeSystemSP> collection;
521 mutable std::mutex m_mutex; ///< A mutex to keep this object happy in
522 ///multi-threaded environments.
524 bool m_clear_in_progress;
527 } // namespace lldb_private
529 #endif // liblldb_TypeSystem_h_