Warning: This file is not a C or C++ file. It does not have highlighting.

1//===-- TypeSystem.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_TYPESYSTEM_H
10#define LLDB_SYMBOL_TYPESYSTEM_H
11
12#include <functional>
13#include <map>
14#include <mutex>
15#include <string>
16
17#include "llvm/ADT/APFloat.h"
18#include "llvm/ADT/APSInt.h"
19#include "llvm/ADT/SmallBitVector.h"
20#include "llvm/Support/Casting.h"
21#include "llvm/Support/Error.h"
22
23#include "lldb/Core/PluginInterface.h"
24#include "lldb/Expression/Expression.h"
25#include "lldb/Symbol/CompilerDecl.h"
26#include "lldb/Symbol/CompilerDeclContext.h"
27#include "lldb/lldb-private.h"
28
29class DWARFDIE;
30class DWARFASTParser;
31class PDBASTParser;
32
33namespace lldb_private {
34
35/// A SmallBitVector that represents a set of source languages (\p
36/// lldb::LanguageType). Each lldb::LanguageType is represented by
37/// the bit with the position of its enumerator. The largest
38/// LanguageType is < 64, so this is space-efficient and on 64-bit
39/// architectures a LanguageSet can be completely stack-allocated.
40struct LanguageSet {
41 llvm::SmallBitVector bitvector;
42 LanguageSet();
43
44 /// If the set contains a single language only, return it.
45 llvm::Optional<lldb::LanguageType> GetSingularLanguage();
46 void Insert(lldb::LanguageType language);
47 bool Empty() const;
48 size_t Size() const;
49 bool operator[](unsigned i) const;
50};
51
52/// Interface for representing a type system.
53///
54/// Implemented by language plugins to define the type system for a given
55/// language.
56///
57/// This interface extensively used opaque pointers to prevent that generic
58/// LLDB code has dependencies on language plugins. The type and semantics of
59/// these opaque pointers are defined by the TypeSystem implementation inside
60/// the respective language plugin. Opaque pointers from one TypeSystem
61/// instance should never be passed to a different TypeSystem instance (even
62/// when the language plugin for both TypeSystem instances is the same).
63///
64/// Most of the functions in this class should not be called directly but only
65/// called by their respective counterparts in CompilerType, CompilerDecl and
66/// CompilerDeclContext.
67///
68/// \see lldb_private::CompilerType
69/// \see lldb_private::CompilerDecl
70/// \see lldb_private::CompilerDeclContext
71class TypeSystem : public PluginInterface {
72public:
73 // Constructors and Destructors
74 ~TypeSystem() override;
75
76 // LLVM RTTI support
77 virtual bool isA(const void *ClassID) const = 0;
78
79 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language,
80 Module *module);
81
82 static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language,
83 Target *target);
84
85 // Free up any resources associated with this TypeSystem. Done before
86 // removing all the TypeSystems from the TypeSystemMap.
87 virtual void Finalize() {}
88
89 virtual DWARFASTParser *GetDWARFParser() { return nullptr; }
90 virtual PDBASTParser *GetPDBParser() { return nullptr; }
91
92 virtual SymbolFile *GetSymbolFile() const { return m_sym_file; }
93
94 virtual void SetSymbolFile(SymbolFile *sym_file) { m_sym_file = sym_file; }
95
96 // CompilerDecl functions
97 virtual ConstString DeclGetName(void *opaque_decl) = 0;
98
99 virtual ConstString DeclGetMangledName(void *opaque_decl);
100
101 virtual CompilerDeclContext DeclGetDeclContext(void *opaque_decl);
102
103 virtual CompilerType DeclGetFunctionReturnType(void *opaque_decl);
104
105 virtual size_t DeclGetFunctionNumArguments(void *opaque_decl);
106
107 virtual CompilerType DeclGetFunctionArgumentType(void *opaque_decl,
108 size_t arg_idx);
109
110 virtual CompilerType GetTypeForDecl(void *opaque_decl) = 0;
111
112 // CompilerDeclContext functions
113
114 virtual std::vector<CompilerDecl>
115 DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name,
116 const bool ignore_imported_decls);
117
118 virtual ConstString DeclContextGetName(void *opaque_decl_ctx) = 0;
119
120 virtual ConstString
121 DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) = 0;
122
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;
126
127 virtual bool DeclContextIsContainedInLookup(void *opaque_decl_ctx,
128 void *other_opaque_decl_ctx) = 0;
129
130 // Tests
131#ifndef NDEBUG
132 /// Verify the integrity of the type to catch CompilerTypes that mix
133 /// and match invalid TypeSystem/Opaque type pairs.
134 virtual bool Verify(lldb::opaque_compiler_type_t type) = 0;
135#endif
136
137 virtual bool IsArrayType(lldb::opaque_compiler_type_t type,
138 CompilerType *element_type, uint64_t *size,
139 bool *is_incomplete) = 0;
140
141 virtual bool IsAggregateType(lldb::opaque_compiler_type_t type) = 0;
142
143 virtual bool IsAnonymousType(lldb::opaque_compiler_type_t type);
144
145 virtual bool IsCharType(lldb::opaque_compiler_type_t type) = 0;
146
147 virtual bool IsCompleteType(lldb::opaque_compiler_type_t type) = 0;
148
149 virtual bool IsDefined(lldb::opaque_compiler_type_t type) = 0;
150
151 virtual bool IsFloatingPointType(lldb::opaque_compiler_type_t type,
152 uint32_t &count, bool &is_complex) = 0;
153
154 virtual bool IsFunctionType(lldb::opaque_compiler_type_t type) = 0;
155
156 virtual size_t
157 GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) = 0;
158
159 virtual CompilerType
160 GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
161 const size_t index) = 0;
162
163 virtual bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) = 0;
164
165 virtual bool IsBlockPointerType(lldb::opaque_compiler_type_t type,
166 CompilerType *function_pointer_type_ptr) = 0;
167
168 virtual bool IsIntegerType(lldb::opaque_compiler_type_t type,
169 bool &is_signed) = 0;
170
171 virtual bool IsEnumerationType(lldb::opaque_compiler_type_t type,
172 bool &is_signed) {
173 is_signed = false;
174 return false;
175 }
176
177 virtual bool IsScopedEnumerationType(lldb::opaque_compiler_type_t type) = 0;
178
179 virtual bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
180 CompilerType *target_type, // Can pass NULL
181 bool check_cplusplus, bool check_objc) = 0;
182
183 virtual bool IsPointerType(lldb::opaque_compiler_type_t type,
184 CompilerType *pointee_type) = 0;
185
186 virtual bool IsScalarType(lldb::opaque_compiler_type_t type) = 0;
187
188 virtual bool IsVoidType(lldb::opaque_compiler_type_t type) = 0;
189
190 virtual bool CanPassInRegisters(const CompilerType &type) = 0;
191
192 // TypeSystems can support more than one language
193 virtual bool SupportsLanguage(lldb::LanguageType language) = 0;
194
195 // Type Completion
196
197 virtual bool GetCompleteType(lldb::opaque_compiler_type_t type) = 0;
198
199 // AST related queries
200
201 virtual uint32_t GetPointerByteSize() = 0;
202
203 // Accessors
204
205 virtual ConstString GetTypeName(lldb::opaque_compiler_type_t type) = 0;
206
207 virtual ConstString GetDisplayTypeName(lldb::opaque_compiler_type_t type) = 0;
208
209 virtual uint32_t
210 GetTypeInfo(lldb::opaque_compiler_type_t type,
211 CompilerType *pointee_or_element_compiler_type) = 0;
212
213 virtual lldb::LanguageType
214 GetMinimumLanguage(lldb::opaque_compiler_type_t type) = 0;
215
216 virtual lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) = 0;
217
218 // Creating related types
219
220 virtual CompilerType
221 GetArrayElementType(lldb::opaque_compiler_type_t type,
222 ExecutionContextScope *exe_scope) = 0;
223
224 virtual CompilerType GetArrayType(lldb::opaque_compiler_type_t type,
225 uint64_t size);
226
227 virtual CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) = 0;
228
229 virtual CompilerType
230 GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) = 0;
231
232 // Returns -1 if this isn't a function of if the function doesn't have a
233 // prototype Returns a value >= 0 if there is a prototype.
234 virtual int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) = 0;
235
236 virtual CompilerType
237 GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type,
238 size_t idx) = 0;
239
240 virtual CompilerType
241 GetFunctionReturnType(lldb::opaque_compiler_type_t type) = 0;
242
243 virtual size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) = 0;
244
245 virtual TypeMemberFunctionImpl
246 GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, size_t idx) = 0;
247
248 virtual CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) = 0;
249
250 virtual CompilerType GetPointerType(lldb::opaque_compiler_type_t type) = 0;
251
252 virtual CompilerType
253 GetLValueReferenceType(lldb::opaque_compiler_type_t type);
254
255 virtual CompilerType
256 GetRValueReferenceType(lldb::opaque_compiler_type_t type);
257
258 virtual CompilerType GetAtomicType(lldb::opaque_compiler_type_t type);
259
260 virtual CompilerType AddConstModifier(lldb::opaque_compiler_type_t type);
261
262 virtual CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type);
263
264 virtual CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type);
265
266 /// \param opaque_payload The m_payload field of Type, which may
267 /// carry TypeSystem-specific extra information.
268 virtual CompilerType CreateTypedef(lldb::opaque_compiler_type_t type,
269 const char *name,
270 const CompilerDeclContext &decl_ctx,
271 uint32_t opaque_payload);
272
273 // Exploring the type
274
275 virtual const llvm::fltSemantics &GetFloatTypeSemantics(size_t byte_size) = 0;
276
277 virtual llvm::Optional<uint64_t>
278 GetBitSize(lldb::opaque_compiler_type_t type,
279 ExecutionContextScope *exe_scope) = 0;
280
281 virtual lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type,
282 uint64_t &count) = 0;
283
284 virtual lldb::Format GetFormat(lldb::opaque_compiler_type_t type) = 0;
285
286 virtual uint32_t GetNumChildren(lldb::opaque_compiler_type_t type,
287 bool omit_empty_base_classes,
288 const ExecutionContext *exe_ctx) = 0;
289
290 virtual CompilerType GetBuiltinTypeByName(ConstString name);
291
292 virtual lldb::BasicType
293 GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) = 0;
294
295 virtual void ForEachEnumerator(
296 lldb::opaque_compiler_type_t type,
297 std::function<bool(const CompilerType &integer_type,
298 ConstString name,
299 const llvm::APSInt &value)> const &callback) {}
300
301 virtual uint32_t GetNumFields(lldb::opaque_compiler_type_t type) = 0;
302
303 virtual CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type,
304 size_t idx, std::string &name,
305 uint64_t *bit_offset_ptr,
306 uint32_t *bitfield_bit_size_ptr,
307 bool *is_bitfield_ptr) = 0;
308
309 virtual uint32_t
310 GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) = 0;
311
312 virtual uint32_t
313 GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) = 0;
314
315 virtual CompilerType
316 GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
317 uint32_t *bit_offset_ptr) = 0;
318
319 virtual CompilerType
320 GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
321 uint32_t *bit_offset_ptr) = 0;
322
323 virtual CompilerType GetChildCompilerTypeAtIndex(
324 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
325 bool transparent_pointers, bool omit_empty_base_classes,
326 bool ignore_array_bounds, std::string &child_name,
327 uint32_t &child_byte_size, int32_t &child_byte_offset,
328 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
329 bool &child_is_base_class, bool &child_is_deref_of_parent,
330 ValueObject *valobj, uint64_t &language_flags) = 0;
331
332 // Lookup a child given a name. This function will match base class names and
333 // member member names in "clang_type" only, not descendants.
334 virtual uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
335 const char *name,
336 bool omit_empty_base_classes) = 0;
337
338 // Lookup a child member given a name. This function will match member names
339 // only and will descend into "clang_type" children in search for the first
340 // member in this class, or any base class that matches "name".
341 // TODO: Return all matches for a given name by returning a
342 // vector<vector<uint32_t>>
343 // so we catch all names that match a given child name, not just the first.
344 virtual size_t
345 GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type,
346 const char *name, bool omit_empty_base_classes,
347 std::vector<uint32_t> &child_indexes) = 0;
348
349 virtual size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type);
350
351 virtual lldb::TemplateArgumentKind
352 GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, size_t idx);
353 virtual CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
354 size_t idx);
355 virtual llvm::Optional<CompilerType::IntegralTemplateArgument>
356 GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx);
357
358 // Dumping types
359
360#ifndef NDEBUG
361 /// Convenience LLVM-style dump method for use in the debugger only.
362 LLVM_DUMP_METHOD virtual void
363 dump(lldb::opaque_compiler_type_t type) const = 0;
364#endif
365
366 virtual void DumpValue(lldb::opaque_compiler_type_t type,
367 ExecutionContext *exe_ctx, Stream *s,
368 lldb::Format format, const DataExtractor &data,
369 lldb::offset_t data_offset, size_t data_byte_size,
370 uint32_t bitfield_bit_size,
371 uint32_t bitfield_bit_offset, bool show_types,
372 bool show_summary, bool verbose, uint32_t depth) = 0;
373
374 virtual bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s,
375 lldb::Format format, const DataExtractor &data,
376 lldb::offset_t data_offset, size_t data_byte_size,
377 uint32_t bitfield_bit_size,
378 uint32_t bitfield_bit_offset,
379 ExecutionContextScope *exe_scope) = 0;
380
381 /// Dump the type to stdout.
382 virtual void DumpTypeDescription(
383 lldb::opaque_compiler_type_t type,
384 lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) = 0;
385
386 /// Print a description of the type to a stream. The exact implementation
387 /// varies, but the expectation is that eDescriptionLevelFull returns a
388 /// source-like representation of the type, whereas eDescriptionLevelVerbose
389 /// does a dump of the underlying AST if applicable.
390 virtual void DumpTypeDescription(
391 lldb::opaque_compiler_type_t type, Stream *s,
392 lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) = 0;
393
394 /// Dump a textual representation of the internal TypeSystem state to the
395 /// given stream.
396 ///
397 /// This should not modify the state of the TypeSystem if possible.
398 virtual void Dump(llvm::raw_ostream &output) = 0;
399
400 // TODO: These methods appear unused. Should they be removed?
401
402 virtual bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) = 0;
403
404 virtual void DumpSummary(lldb::opaque_compiler_type_t type,
405 ExecutionContext *exe_ctx, Stream *s,
406 const DataExtractor &data,
407 lldb::offset_t data_offset,
408 size_t data_byte_size) = 0;
409
410 // TODO: Determine if these methods should move to TypeSystemClang.
411
412 virtual bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type,
413 CompilerType *pointee_type) = 0;
414
415 virtual unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) = 0;
416
417 virtual bool IsCStringType(lldb::opaque_compiler_type_t type,
418 uint32_t &length) = 0;
419
420 virtual llvm::Optional<size_t>
421 GetTypeBitAlign(lldb::opaque_compiler_type_t type,
422 ExecutionContextScope *exe_scope) = 0;
423
424 virtual CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) = 0;
425
426 virtual CompilerType
427 GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding,
428 size_t bit_size) = 0;
429
430 virtual bool IsBeingDefined(lldb::opaque_compiler_type_t type) = 0;
431
432 virtual bool IsConst(lldb::opaque_compiler_type_t type) = 0;
433
434 virtual uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
435 CompilerType *base_type_ptr) = 0;
436
437 virtual bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) = 0;
438
439 virtual bool IsTypedefType(lldb::opaque_compiler_type_t type) = 0;
440
441 // If the current object represents a typedef type, get the underlying type
442 virtual CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) = 0;
443
444 virtual bool IsVectorType(lldb::opaque_compiler_type_t type,
445 CompilerType *element_type, uint64_t *size) = 0;
446
447 virtual CompilerType
448 GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) = 0;
449
450 virtual CompilerType
451 GetNonReferenceType(lldb::opaque_compiler_type_t type) = 0;
452
453 virtual bool IsReferenceType(lldb::opaque_compiler_type_t type,
454 CompilerType *pointee_type, bool *is_rvalue) = 0;
455
456 virtual bool
457 ShouldTreatScalarValueAsAddress(lldb::opaque_compiler_type_t type) {
458 return IsPointerOrReferenceType(type, nullptr);
459 }
460
461 virtual UserExpression *
462 GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix,
463 lldb::LanguageType language,
464 Expression::ResultType desired_type,
465 const EvaluateExpressionOptions &options,
466 ValueObject *ctx_obj) {
467 return nullptr;
468 }
469
470 virtual FunctionCaller *GetFunctionCaller(const CompilerType &return_type,
471 const Address &function_address,
472 const ValueList &arg_value_list,
473 const char *name) {
474 return nullptr;
475 }
476
477 virtual std::unique_ptr<UtilityFunction>
478 CreateUtilityFunction(std::string text, std::string name);
479
480 virtual PersistentExpressionState *GetPersistentExpressionState() {
481 return nullptr;
482 }
483
484 virtual CompilerType GetTypeForFormatters(void *type);
485
486 virtual LazyBool ShouldPrintAsOneLiner(void *type, ValueObject *valobj);
487
488 // Type systems can have types that are placeholder types, which are meant to
489 // indicate the presence of a type, but offer no actual information about
490 // said types, and leave the burden of actually figuring type information out
491 // to dynamic type resolution. For instance a language with a generics
492 // system, can use placeholder types to indicate "type argument goes here",
493 // without promising uniqueness of the placeholder, nor attaching any
494 // actually idenfiable information to said placeholder. This API allows type
495 // systems to tell LLDB when such a type has been encountered In response,
496 // the debugger can react by not using this type as a cache entry in any
497 // type-specific way For instance, LLDB will currently not cache any
498 // formatters that are discovered on such a type as attributable to the
499 // meaningless type itself, instead preferring to use the dynamic type
500 virtual bool IsMeaninglessWithoutDynamicResolution(void *type);
501
502protected:
503 SymbolFile *m_sym_file = nullptr;
504};
505
506class TypeSystemMap {
507public:
508 TypeSystemMap();
509 ~TypeSystemMap();
510
511 // Clear calls Finalize on all the TypeSystems managed by this map, and then
512 // empties the map.
513 void Clear();
514
515 // Iterate through all of the type systems that are created. Return true from
516 // callback to keep iterating, false to stop iterating.
517 void ForEach(std::function<bool(TypeSystem *)> const &callback);
518
519 llvm::Expected<TypeSystem &>
520 GetTypeSystemForLanguage(lldb::LanguageType language, Module *module,
521 bool can_create);
522
523 llvm::Expected<TypeSystem &>
524 GetTypeSystemForLanguage(lldb::LanguageType language, Target *target,
525 bool can_create);
526
527protected:
528 typedef std::map<lldb::LanguageType, lldb::TypeSystemSP> collection;
529 mutable std::mutex m_mutex; ///< A mutex to keep this object happy in
530 ///multi-threaded environments.
531 collection m_map;
532 bool m_clear_in_progress = false;
533
534private:
535 typedef llvm::function_ref<lldb::TypeSystemSP()> CreateCallback;
536 /// Finds the type system for the given language. If no type system could be
537 /// found for a language and a CreateCallback was provided, the value returned
538 /// by the callback will be treated as the TypeSystem for the language.
539 ///
540 /// \param language The language for which the type system should be found.
541 /// \param create_callback A callback that will be called if no previously
542 /// created TypeSystem that fits the given language
543 /// could found. Can be omitted if a non-existent
544 /// type system should be treated as an error instead.
545 /// \return The found type system or an error.
546 llvm::Expected<TypeSystem &> GetTypeSystemForLanguage(
547 lldb::LanguageType language,
548 llvm::Optional<CreateCallback> create_callback = llvm::None);
549};
550
551} // namespace lldb_private
552
553#endif // LLDB_SYMBOL_TYPESYSTEM_H
554

Warning: This file is not a C or C++ file. It does not have highlighting.

source code of lldb/include/lldb/Symbol/TypeSystem.h