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 instance
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
97 // removing all the TypeSystems from the TypeSystemMap.
98 virtual void Finalize() {}
100 virtual DWARFASTParser *GetDWARFParser() { return nullptr; }
102 virtual SymbolFile *GetSymbolFile() const { return m_sym_file; }
104 // Returns true if the symbol file changed during the set accessor.
105 virtual void SetSymbolFile(SymbolFile *sym_file) { m_sym_file = sym_file; }
107 //----------------------------------------------------------------------
108 // CompilerDecl functions
109 //----------------------------------------------------------------------
110 virtual ConstString DeclGetName(void *opaque_decl) = 0;
112 virtual ConstString DeclGetMangledName(void *opaque_decl);
114 virtual CompilerDeclContext DeclGetDeclContext(void *opaque_decl);
116 virtual CompilerType DeclGetFunctionReturnType(void *opaque_decl);
118 virtual size_t DeclGetFunctionNumArguments(void *opaque_decl);
120 virtual CompilerType DeclGetFunctionArgumentType(void *opaque_decl,
123 //----------------------------------------------------------------------
124 // CompilerDeclContext functions
125 //----------------------------------------------------------------------
127 virtual std::vector<CompilerDecl>
128 DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name,
129 const bool ignore_imported_decls);
131 virtual bool DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) = 0;
133 virtual ConstString DeclContextGetName(void *opaque_decl_ctx) = 0;
136 DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) = 0;
138 virtual bool DeclContextIsClassMethod(
139 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
140 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) = 0;
142 //----------------------------------------------------------------------
144 //----------------------------------------------------------------------
146 virtual bool IsArrayType(lldb::opaque_compiler_type_t type,
147 CompilerType *element_type, uint64_t *size,
148 bool *is_incomplete) = 0;
150 virtual bool IsAggregateType(lldb::opaque_compiler_type_t type) = 0;
152 virtual bool IsAnonymousType(lldb::opaque_compiler_type_t type);
154 virtual bool IsCharType(lldb::opaque_compiler_type_t type) = 0;
156 virtual bool IsCompleteType(lldb::opaque_compiler_type_t type) = 0;
158 virtual bool IsDefined(lldb::opaque_compiler_type_t type) = 0;
160 virtual bool IsFloatingPointType(lldb::opaque_compiler_type_t type,
161 uint32_t &count, bool &is_complex) = 0;
163 virtual bool IsFunctionType(lldb::opaque_compiler_type_t type,
164 bool *is_variadic_ptr) = 0;
167 GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) = 0;
170 GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
171 const size_t index) = 0;
173 virtual bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) = 0;
175 virtual bool IsBlockPointerType(lldb::opaque_compiler_type_t type,
176 CompilerType *function_pointer_type_ptr) = 0;
178 virtual bool IsIntegerType(lldb::opaque_compiler_type_t type,
179 bool &is_signed) = 0;
181 virtual bool IsEnumerationType(lldb::opaque_compiler_type_t type,
187 virtual bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
188 CompilerType *target_type, // Can pass NULL
189 bool check_cplusplus, bool check_objc) = 0;
191 virtual bool IsPointerType(lldb::opaque_compiler_type_t type,
192 CompilerType *pointee_type) = 0;
194 virtual bool IsScalarType(lldb::opaque_compiler_type_t type) = 0;
196 virtual bool IsVoidType(lldb::opaque_compiler_type_t type) = 0;
198 // TypeSystems can support more than one language
199 virtual bool SupportsLanguage(lldb::LanguageType language) = 0;
201 //----------------------------------------------------------------------
203 //----------------------------------------------------------------------
205 virtual bool GetCompleteType(lldb::opaque_compiler_type_t type) = 0;
207 //----------------------------------------------------------------------
208 // AST related queries
209 //----------------------------------------------------------------------
211 virtual uint32_t GetPointerByteSize() = 0;
213 //----------------------------------------------------------------------
215 //----------------------------------------------------------------------
217 virtual ConstString GetTypeName(lldb::opaque_compiler_type_t type) = 0;
220 GetTypeInfo(lldb::opaque_compiler_type_t type,
221 CompilerType *pointee_or_element_compiler_type) = 0;
223 virtual lldb::LanguageType
224 GetMinimumLanguage(lldb::opaque_compiler_type_t type) = 0;
226 virtual lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) = 0;
228 //----------------------------------------------------------------------
229 // Creating related types
230 //----------------------------------------------------------------------
232 virtual CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type,
233 uint64_t *stride) = 0;
235 virtual CompilerType GetArrayType(lldb::opaque_compiler_type_t type,
238 virtual CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) = 0;
240 // Returns -1 if this isn't a function of if the function doesn't have a
241 // prototype Returns a value >= 0 if there is a prototype.
242 virtual int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) = 0;
245 GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type,
249 GetFunctionReturnType(lldb::opaque_compiler_type_t type) = 0;
251 virtual size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) = 0;
253 virtual TypeMemberFunctionImpl
254 GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, size_t idx) = 0;
256 virtual CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) = 0;
258 virtual CompilerType GetPointerType(lldb::opaque_compiler_type_t type) = 0;
261 GetLValueReferenceType(lldb::opaque_compiler_type_t type);
264 GetRValueReferenceType(lldb::opaque_compiler_type_t type);
266 virtual CompilerType AddConstModifier(lldb::opaque_compiler_type_t type);
268 virtual CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type);
270 virtual CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type);
272 virtual CompilerType CreateTypedef(lldb::opaque_compiler_type_t type,
274 const CompilerDeclContext &decl_ctx);
276 //----------------------------------------------------------------------
277 // Exploring the type
278 //----------------------------------------------------------------------
280 virtual uint64_t GetBitSize(lldb::opaque_compiler_type_t type,
281 ExecutionContextScope *exe_scope) = 0;
283 virtual lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type,
284 uint64_t &count) = 0;
286 virtual lldb::Format GetFormat(lldb::opaque_compiler_type_t type) = 0;
288 virtual uint32_t GetNumChildren(lldb::opaque_compiler_type_t type,
289 bool omit_empty_base_classes) = 0;
291 virtual CompilerType GetBuiltinTypeByName(const ConstString &name);
293 virtual lldb::BasicType
294 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) = 0;
296 virtual void ForEachEnumerator(
297 lldb::opaque_compiler_type_t type,
298 std::function<bool(const CompilerType &integer_type,
299 const ConstString &name,
300 const llvm::APSInt &value)> const &callback) {}
302 virtual uint32_t GetNumFields(lldb::opaque_compiler_type_t type) = 0;
304 virtual CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type,
305 size_t idx, std::string &name,
306 uint64_t *bit_offset_ptr,
307 uint32_t *bitfield_bit_size_ptr,
308 bool *is_bitfield_ptr) = 0;
311 GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) = 0;
314 GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) = 0;
317 GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
318 uint32_t *bit_offset_ptr) = 0;
321 GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
322 uint32_t *bit_offset_ptr) = 0;
324 virtual CompilerType GetChildCompilerTypeAtIndex(
325 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
326 bool transparent_pointers, bool omit_empty_base_classes,
327 bool ignore_array_bounds, std::string &child_name,
328 uint32_t &child_byte_size, int32_t &child_byte_offset,
329 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
330 bool &child_is_base_class, bool &child_is_deref_of_parent,
331 ValueObject *valobj, uint64_t &language_flags) = 0;
333 // Lookup a child given a name. This function will match base class names and
334 // member member names in "clang_type" only, not descendants.
335 virtual uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
337 bool omit_empty_base_classes) = 0;
339 // Lookup a child member given a name. This function will match member names
340 // only and will descend into "clang_type" children in search for the first
341 // member in this class, or any base class that matches "name".
342 // TODO: Return all matches for a given name by returning a
343 // vector<vector<uint32_t>>
344 // so we catch all names that match a given child name, not just the first.
346 GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type,
347 const char *name, bool omit_empty_base_classes,
348 std::vector<uint32_t> &child_indexes) = 0;
350 virtual size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type) = 0;
352 virtual lldb::TemplateArgumentKind
353 GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, size_t idx);
354 virtual CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
356 virtual llvm::Optional<CompilerType::IntegralTemplateArgument>
357 GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx);
359 //----------------------------------------------------------------------
361 //----------------------------------------------------------------------
363 virtual void DumpValue(lldb::opaque_compiler_type_t type,
364 ExecutionContext *exe_ctx, Stream *s,
365 lldb::Format format, const DataExtractor &data,
366 lldb::offset_t data_offset, size_t data_byte_size,
367 uint32_t bitfield_bit_size,
368 uint32_t bitfield_bit_offset, bool show_types,
369 bool show_summary, bool verbose, uint32_t depth) = 0;
371 virtual bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s,
372 lldb::Format format, const DataExtractor &data,
373 lldb::offset_t data_offset, size_t data_byte_size,
374 uint32_t bitfield_bit_size,
375 uint32_t bitfield_bit_offset,
376 ExecutionContextScope *exe_scope) = 0;
379 DumpTypeDescription(lldb::opaque_compiler_type_t type) = 0; // Dump to stdout
381 virtual void DumpTypeDescription(lldb::opaque_compiler_type_t type,
384 //----------------------------------------------------------------------
385 // TODO: These methods appear unused. Should they be removed?
386 //----------------------------------------------------------------------
388 virtual bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) = 0;
390 virtual void DumpSummary(lldb::opaque_compiler_type_t type,
391 ExecutionContext *exe_ctx, Stream *s,
392 const DataExtractor &data,
393 lldb::offset_t data_offset,
394 size_t data_byte_size) = 0;
396 // Converts "s" to a floating point value and place resulting floating point
397 // bytes in the "dst" buffer.
398 virtual size_t ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
399 const char *s, uint8_t *dst,
400 size_t dst_size) = 0;
402 //----------------------------------------------------------------------
403 // TODO: Determine if these methods should move to ClangASTContext.
404 //----------------------------------------------------------------------
406 virtual bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type,
407 CompilerType *pointee_type) = 0;
409 virtual unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) = 0;
411 virtual bool IsCStringType(lldb::opaque_compiler_type_t type,
412 uint32_t &length) = 0;
414 virtual size_t GetTypeBitAlign(lldb::opaque_compiler_type_t type) = 0;
416 virtual CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) = 0;
419 GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding,
420 size_t bit_size) = 0;
422 virtual bool IsBeingDefined(lldb::opaque_compiler_type_t type) = 0;
424 virtual bool IsConst(lldb::opaque_compiler_type_t type) = 0;
426 virtual uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
427 CompilerType *base_type_ptr) = 0;
429 virtual bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) = 0;
431 virtual bool IsTypedefType(lldb::opaque_compiler_type_t type) = 0;
433 // If the current object represents a typedef type, get the underlying type
434 virtual CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) = 0;
436 virtual bool IsVectorType(lldb::opaque_compiler_type_t type,
437 CompilerType *element_type, uint64_t *size) = 0;
440 GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) = 0;
443 GetNonReferenceType(lldb::opaque_compiler_type_t type) = 0;
445 virtual bool IsReferenceType(lldb::opaque_compiler_type_t type,
446 CompilerType *pointee_type, bool *is_rvalue) = 0;
449 ShouldTreatScalarValueAsAddress(lldb::opaque_compiler_type_t type) {
450 return IsPointerOrReferenceType(type, nullptr);
453 virtual UserExpression *
454 GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix,
455 lldb::LanguageType language,
456 Expression::ResultType desired_type,
457 const EvaluateExpressionOptions &options) {
461 virtual FunctionCaller *GetFunctionCaller(const CompilerType &return_type,
462 const Address &function_address,
463 const ValueList &arg_value_list,
468 virtual UtilityFunction *GetUtilityFunction(const char *text,
473 virtual PersistentExpressionState *GetPersistentExpressionState() {
477 virtual CompilerType GetTypeForFormatters(void *type);
479 virtual LazyBool ShouldPrintAsOneLiner(void *type, ValueObject *valobj);
481 // Type systems can have types that are placeholder types, which are meant to
482 // indicate the presence of a type, but offer no actual information about
483 // said types, and leave the burden of actually figuring type information out
484 // to dynamic type resolution. For instance a language with a generics
485 // system, can use placeholder types to indicate "type argument goes here",
486 // without promising uniqueness of the placeholder, nor attaching any
487 // actually idenfiable information to said placeholder. This API allows type
488 // systems to tell LLDB when such a type has been encountered In response,
489 // the debugger can react by not using this type as a cache entry in any
490 // type-specific way For instance, LLDB will currently not cache any
491 // formatters that are discovered on such a type as attributable to the
492 // meaningless type itself, instead preferring to use the dynamic type
493 virtual bool IsMeaninglessWithoutDynamicResolution(void *type);
496 const LLVMCastKind m_kind; // Support for llvm casting
497 SymbolFile *m_sym_file;
500 class TypeSystemMap {
505 // Clear calls Finalize on all the TypeSystems managed by this map, and then
509 // Iterate through all of the type systems that are created. Return true from
510 // callback to keep iterating, false to stop iterating.
511 void ForEach(std::function<bool(TypeSystem *)> const &callback);
513 TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language,
514 Module *module, bool can_create);
516 TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language,
517 Target *target, bool can_create);
520 // This function does not take the map mutex, and should only be called from
521 // functions that do take the mutex.
522 void AddToMap(lldb::LanguageType language,
523 lldb::TypeSystemSP const &type_system_sp);
525 typedef std::map<lldb::LanguageType, lldb::TypeSystemSP> collection;
526 mutable std::mutex m_mutex; ///< A mutex to keep this object happy in
527 ///multi-threaded environments.
529 bool m_clear_in_progress;
532 } // namespace lldb_private
534 #endif // liblldb_TypeSystem_h_