1 //===-- TypeSystem.h ------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_TypeSystem_h_
10 #define liblldb_TypeSystem_h_
17 #include "llvm/ADT/APSInt.h"
18 #include "llvm/Support/Casting.h"
20 #include "lldb/Core/PluginInterface.h"
21 #include "lldb/Expression/Expression.h"
22 #include "lldb/Symbol/CompilerDecl.h"
23 #include "lldb/Symbol/CompilerDeclContext.h"
24 #include "lldb/lldb-private.h"
30 namespace lldb_private {
32 // Interface for representing the Type Systems in different languages.
33 class TypeSystem : public PluginInterface {
35 // Intrusive type system that allows us to use llvm casting.
37 // To add a new type system:
39 // 1 - Add a new enumeration for llvm casting below for your TypeSystem
40 // subclass, here we will use eKindFoo
42 // 2 - Your TypeSystem subclass will inherit from TypeSystem and needs
43 // to implement a static classof() function that returns your
46 // class Foo : public lldb_private::TypeSystem
48 // static bool classof(const TypeSystem *ts)
50 // return ts->getKind() == TypeSystem::eKindFoo;
54 // 3 - Contruct your TypeSystem subclass with the enumeration from below
57 // TypeSystem(TypeSystem::eKindFoo),
62 // Then you can use the llvm casting on any "TypeSystem *" to get an instance
71 // Constructors and Destructors
72 TypeSystem(LLVMCastKind kind);
74 ~TypeSystem() override;
76 LLVMCastKind getKind() const { return m_kind; }
78 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language,
81 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language,
84 // Free up any resources associated with this TypeSystem. Done before
85 // removing all the TypeSystems from the TypeSystemMap.
86 virtual void Finalize() {}
88 virtual DWARFASTParser *GetDWARFParser() { return nullptr; }
89 virtual PDBASTParser *GetPDBParser() { return nullptr; }
91 virtual SymbolFile *GetSymbolFile() const { return m_sym_file; }
93 // Returns true if the symbol file changed during the set accessor.
94 virtual void SetSymbolFile(SymbolFile *sym_file) { m_sym_file = sym_file; }
96 // CompilerDecl functions
97 virtual ConstString DeclGetName(void *opaque_decl) = 0;
99 virtual ConstString DeclGetMangledName(void *opaque_decl);
101 virtual CompilerDeclContext DeclGetDeclContext(void *opaque_decl);
103 virtual CompilerType DeclGetFunctionReturnType(void *opaque_decl);
105 virtual size_t DeclGetFunctionNumArguments(void *opaque_decl);
107 virtual CompilerType DeclGetFunctionArgumentType(void *opaque_decl,
110 // CompilerDeclContext functions
112 virtual std::vector<CompilerDecl>
113 DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name,
114 const bool ignore_imported_decls);
116 virtual bool DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) = 0;
118 virtual ConstString DeclContextGetName(void *opaque_decl_ctx) = 0;
121 DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) = 0;
123 virtual bool DeclContextIsClassMethod(
124 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
125 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) = 0;
127 virtual bool DeclContextIsContainedInLookup(void *opaque_decl_ctx,
128 void *other_opaque_decl_ctx) = 0;
132 virtual bool IsArrayType(lldb::opaque_compiler_type_t type,
133 CompilerType *element_type, uint64_t *size,
134 bool *is_incomplete) = 0;
136 virtual bool IsAggregateType(lldb::opaque_compiler_type_t type) = 0;
138 virtual bool IsAnonymousType(lldb::opaque_compiler_type_t type);
140 virtual bool IsCharType(lldb::opaque_compiler_type_t type) = 0;
142 virtual bool IsCompleteType(lldb::opaque_compiler_type_t type) = 0;
144 virtual bool IsDefined(lldb::opaque_compiler_type_t type) = 0;
146 virtual bool IsFloatingPointType(lldb::opaque_compiler_type_t type,
147 uint32_t &count, bool &is_complex) = 0;
149 virtual bool IsFunctionType(lldb::opaque_compiler_type_t type,
150 bool *is_variadic_ptr) = 0;
153 GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) = 0;
156 GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
157 const size_t index) = 0;
159 virtual bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) = 0;
161 virtual bool IsBlockPointerType(lldb::opaque_compiler_type_t type,
162 CompilerType *function_pointer_type_ptr) = 0;
164 virtual bool IsIntegerType(lldb::opaque_compiler_type_t type,
165 bool &is_signed) = 0;
167 virtual bool IsEnumerationType(lldb::opaque_compiler_type_t type,
173 virtual bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
174 CompilerType *target_type, // Can pass NULL
175 bool check_cplusplus, bool check_objc) = 0;
177 virtual bool IsPointerType(lldb::opaque_compiler_type_t type,
178 CompilerType *pointee_type) = 0;
180 virtual bool IsScalarType(lldb::opaque_compiler_type_t type) = 0;
182 virtual bool IsVoidType(lldb::opaque_compiler_type_t type) = 0;
184 virtual bool CanPassInRegisters(const CompilerType &type) = 0;
186 // TypeSystems can support more than one language
187 virtual bool SupportsLanguage(lldb::LanguageType language) = 0;
191 virtual bool GetCompleteType(lldb::opaque_compiler_type_t type) = 0;
193 // AST related queries
195 virtual uint32_t GetPointerByteSize() = 0;
199 virtual ConstString GetTypeName(lldb::opaque_compiler_type_t type) = 0;
202 GetTypeInfo(lldb::opaque_compiler_type_t type,
203 CompilerType *pointee_or_element_compiler_type) = 0;
205 virtual lldb::LanguageType
206 GetMinimumLanguage(lldb::opaque_compiler_type_t type) = 0;
208 virtual lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) = 0;
210 // Creating related types
212 virtual CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type,
213 uint64_t *stride) = 0;
215 virtual CompilerType GetArrayType(lldb::opaque_compiler_type_t type,
218 virtual CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) = 0;
220 // Returns -1 if this isn't a function of if the function doesn't have a
221 // prototype Returns a value >= 0 if there is a prototype.
222 virtual int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) = 0;
225 GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type,
229 GetFunctionReturnType(lldb::opaque_compiler_type_t type) = 0;
231 virtual size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) = 0;
233 virtual TypeMemberFunctionImpl
234 GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, size_t idx) = 0;
236 virtual CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) = 0;
238 virtual CompilerType GetPointerType(lldb::opaque_compiler_type_t type) = 0;
241 GetLValueReferenceType(lldb::opaque_compiler_type_t type);
244 GetRValueReferenceType(lldb::opaque_compiler_type_t type);
246 virtual CompilerType AddConstModifier(lldb::opaque_compiler_type_t type);
248 virtual CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type);
250 virtual CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type);
252 virtual CompilerType CreateTypedef(lldb::opaque_compiler_type_t type,
254 const CompilerDeclContext &decl_ctx);
256 // Exploring the type
258 virtual llvm::Optional<uint64_t>
259 GetBitSize(lldb::opaque_compiler_type_t type,
260 ExecutionContextScope *exe_scope) = 0;
262 virtual lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type,
263 uint64_t &count) = 0;
265 virtual lldb::Format GetFormat(lldb::opaque_compiler_type_t type) = 0;
267 virtual uint32_t GetNumChildren(lldb::opaque_compiler_type_t type,
268 bool omit_empty_base_classes,
269 const ExecutionContext *exe_ctx) = 0;
271 virtual CompilerType GetBuiltinTypeByName(ConstString name);
273 virtual lldb::BasicType
274 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) = 0;
276 virtual void ForEachEnumerator(
277 lldb::opaque_compiler_type_t type,
278 std::function<bool(const CompilerType &integer_type,
280 const llvm::APSInt &value)> const &callback) {}
282 virtual uint32_t GetNumFields(lldb::opaque_compiler_type_t type) = 0;
284 virtual CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type,
285 size_t idx, std::string &name,
286 uint64_t *bit_offset_ptr,
287 uint32_t *bitfield_bit_size_ptr,
288 bool *is_bitfield_ptr) = 0;
291 GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) = 0;
294 GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) = 0;
297 GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
298 uint32_t *bit_offset_ptr) = 0;
301 GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
302 uint32_t *bit_offset_ptr) = 0;
304 virtual CompilerType GetChildCompilerTypeAtIndex(
305 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
306 bool transparent_pointers, bool omit_empty_base_classes,
307 bool ignore_array_bounds, std::string &child_name,
308 uint32_t &child_byte_size, int32_t &child_byte_offset,
309 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
310 bool &child_is_base_class, bool &child_is_deref_of_parent,
311 ValueObject *valobj, uint64_t &language_flags) = 0;
313 // Lookup a child given a name. This function will match base class names and
314 // member member names in "clang_type" only, not descendants.
315 virtual uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
317 bool omit_empty_base_classes) = 0;
319 // Lookup a child member given a name. This function will match member names
320 // only and will descend into "clang_type" children in search for the first
321 // member in this class, or any base class that matches "name".
322 // TODO: Return all matches for a given name by returning a
323 // vector<vector<uint32_t>>
324 // so we catch all names that match a given child name, not just the first.
326 GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type,
327 const char *name, bool omit_empty_base_classes,
328 std::vector<uint32_t> &child_indexes) = 0;
330 virtual size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type);
332 virtual lldb::TemplateArgumentKind
333 GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, size_t idx);
334 virtual CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
336 virtual llvm::Optional<CompilerType::IntegralTemplateArgument>
337 GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx);
342 /// Convenience LLVM-style dump method for use in the debugger only.
343 LLVM_DUMP_METHOD virtual void
344 dump(lldb::opaque_compiler_type_t type) const = 0;
347 virtual void DumpValue(lldb::opaque_compiler_type_t type,
348 ExecutionContext *exe_ctx, Stream *s,
349 lldb::Format format, const DataExtractor &data,
350 lldb::offset_t data_offset, size_t data_byte_size,
351 uint32_t bitfield_bit_size,
352 uint32_t bitfield_bit_offset, bool show_types,
353 bool show_summary, bool verbose, uint32_t depth) = 0;
355 virtual bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s,
356 lldb::Format format, const DataExtractor &data,
357 lldb::offset_t data_offset, size_t data_byte_size,
358 uint32_t bitfield_bit_size,
359 uint32_t bitfield_bit_offset,
360 ExecutionContextScope *exe_scope) = 0;
363 DumpTypeDescription(lldb::opaque_compiler_type_t type) = 0; // Dump to stdout
365 virtual void DumpTypeDescription(lldb::opaque_compiler_type_t type,
368 // TODO: These methods appear unused. Should they be removed?
370 virtual bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) = 0;
372 virtual void DumpSummary(lldb::opaque_compiler_type_t type,
373 ExecutionContext *exe_ctx, Stream *s,
374 const DataExtractor &data,
375 lldb::offset_t data_offset,
376 size_t data_byte_size) = 0;
378 // Converts "s" to a floating point value and place resulting floating point
379 // bytes in the "dst" buffer.
380 virtual size_t ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
381 const char *s, uint8_t *dst,
382 size_t dst_size) = 0;
384 // TODO: Determine if these methods should move to ClangASTContext.
386 virtual bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type,
387 CompilerType *pointee_type) = 0;
389 virtual unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) = 0;
391 virtual bool IsCStringType(lldb::opaque_compiler_type_t type,
392 uint32_t &length) = 0;
394 virtual size_t GetTypeBitAlign(lldb::opaque_compiler_type_t type) = 0;
396 virtual CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) = 0;
399 GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding,
400 size_t bit_size) = 0;
402 virtual bool IsBeingDefined(lldb::opaque_compiler_type_t type) = 0;
404 virtual bool IsConst(lldb::opaque_compiler_type_t type) = 0;
406 virtual uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
407 CompilerType *base_type_ptr) = 0;
409 virtual bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) = 0;
411 virtual bool IsTypedefType(lldb::opaque_compiler_type_t type) = 0;
413 // If the current object represents a typedef type, get the underlying type
414 virtual CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) = 0;
416 virtual bool IsVectorType(lldb::opaque_compiler_type_t type,
417 CompilerType *element_type, uint64_t *size) = 0;
420 GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) = 0;
423 GetNonReferenceType(lldb::opaque_compiler_type_t type) = 0;
425 virtual bool IsReferenceType(lldb::opaque_compiler_type_t type,
426 CompilerType *pointee_type, bool *is_rvalue) = 0;
429 ShouldTreatScalarValueAsAddress(lldb::opaque_compiler_type_t type) {
430 return IsPointerOrReferenceType(type, nullptr);
433 virtual UserExpression *
434 GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix,
435 lldb::LanguageType language,
436 Expression::ResultType desired_type,
437 const EvaluateExpressionOptions &options,
438 ValueObject *ctx_obj) {
442 virtual FunctionCaller *GetFunctionCaller(const CompilerType &return_type,
443 const Address &function_address,
444 const ValueList &arg_value_list,
449 virtual UtilityFunction *GetUtilityFunction(const char *text,
454 virtual PersistentExpressionState *GetPersistentExpressionState() {
458 virtual CompilerType GetTypeForFormatters(void *type);
460 virtual LazyBool ShouldPrintAsOneLiner(void *type, ValueObject *valobj);
462 // Type systems can have types that are placeholder types, which are meant to
463 // indicate the presence of a type, but offer no actual information about
464 // said types, and leave the burden of actually figuring type information out
465 // to dynamic type resolution. For instance a language with a generics
466 // system, can use placeholder types to indicate "type argument goes here",
467 // without promising uniqueness of the placeholder, nor attaching any
468 // actually idenfiable information to said placeholder. This API allows type
469 // systems to tell LLDB when such a type has been encountered In response,
470 // the debugger can react by not using this type as a cache entry in any
471 // type-specific way For instance, LLDB will currently not cache any
472 // formatters that are discovered on such a type as attributable to the
473 // meaningless type itself, instead preferring to use the dynamic type
474 virtual bool IsMeaninglessWithoutDynamicResolution(void *type);
477 const LLVMCastKind m_kind; // Support for llvm casting
478 SymbolFile *m_sym_file;
481 class TypeSystemMap {
486 // Clear calls Finalize on all the TypeSystems managed by this map, and then
490 // Iterate through all of the type systems that are created. Return true from
491 // callback to keep iterating, false to stop iterating.
492 void ForEach(std::function<bool(TypeSystem *)> const &callback);
494 TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language,
495 Module *module, bool can_create);
497 TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language,
498 Target *target, bool can_create);
501 // This function does not take the map mutex, and should only be called from
502 // functions that do take the mutex.
503 void AddToMap(lldb::LanguageType language,
504 lldb::TypeSystemSP const &type_system_sp);
506 typedef std::map<lldb::LanguageType, lldb::TypeSystemSP> collection;
507 mutable std::mutex m_mutex; ///< A mutex to keep this object happy in
508 ///multi-threaded environments.
510 bool m_clear_in_progress;
513 } // namespace lldb_private
515 #endif // liblldb_TypeSystem_h_