CompilerType.cpp revision 360784
1//===-- CompilerType.cpp ----------------------------------------*- 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#include "lldb/Symbol/CompilerType.h"
10
11#include "lldb/Core/Debugger.h"
12#include "lldb/Core/StreamFile.h"
13#include "lldb/Symbol/Type.h"
14#include "lldb/Target/ExecutionContext.h"
15#include "lldb/Target/Process.h"
16#include "lldb/Utility/ConstString.h"
17#include "lldb/Utility/DataBufferHeap.h"
18#include "lldb/Utility/DataExtractor.h"
19#include "lldb/Utility/Scalar.h"
20#include "lldb/Utility/Stream.h"
21#include "lldb/Utility/StreamString.h"
22
23#include <iterator>
24#include <mutex>
25
26using namespace lldb;
27using namespace lldb_private;
28
29// Tests
30
31bool CompilerType::IsAggregateType() const {
32  if (IsValid())
33    return m_type_system->IsAggregateType(m_type);
34  return false;
35}
36
37bool CompilerType::IsAnonymousType() const {
38  if (IsValid())
39    return m_type_system->IsAnonymousType(m_type);
40  return false;
41}
42
43bool CompilerType::IsArrayType(CompilerType *element_type_ptr, uint64_t *size,
44                               bool *is_incomplete) const {
45  if (IsValid())
46    return m_type_system->IsArrayType(m_type, element_type_ptr, size,
47                                      is_incomplete);
48
49  if (element_type_ptr)
50    element_type_ptr->Clear();
51  if (size)
52    *size = 0;
53  if (is_incomplete)
54    *is_incomplete = false;
55  return false;
56}
57
58bool CompilerType::IsVectorType(CompilerType *element_type,
59                                uint64_t *size) const {
60  if (IsValid())
61    return m_type_system->IsVectorType(m_type, element_type, size);
62  return false;
63}
64
65bool CompilerType::IsRuntimeGeneratedType() const {
66  if (IsValid())
67    return m_type_system->IsRuntimeGeneratedType(m_type);
68  return false;
69}
70
71bool CompilerType::IsCharType() const {
72  if (IsValid())
73    return m_type_system->IsCharType(m_type);
74  return false;
75}
76
77bool CompilerType::IsCompleteType() const {
78  if (IsValid())
79    return m_type_system->IsCompleteType(m_type);
80  return false;
81}
82
83bool CompilerType::IsConst() const {
84  if (IsValid())
85    return m_type_system->IsConst(m_type);
86  return false;
87}
88
89bool CompilerType::IsCStringType(uint32_t &length) const {
90  if (IsValid())
91    return m_type_system->IsCStringType(m_type, length);
92  return false;
93}
94
95bool CompilerType::IsFunctionType(bool *is_variadic_ptr) const {
96  if (IsValid())
97    return m_type_system->IsFunctionType(m_type, is_variadic_ptr);
98  return false;
99}
100
101// Used to detect "Homogeneous Floating-point Aggregates"
102uint32_t
103CompilerType::IsHomogeneousAggregate(CompilerType *base_type_ptr) const {
104  if (IsValid())
105    return m_type_system->IsHomogeneousAggregate(m_type, base_type_ptr);
106  return 0;
107}
108
109size_t CompilerType::GetNumberOfFunctionArguments() const {
110  if (IsValid())
111    return m_type_system->GetNumberOfFunctionArguments(m_type);
112  return 0;
113}
114
115CompilerType
116CompilerType::GetFunctionArgumentAtIndex(const size_t index) const {
117  if (IsValid())
118    return m_type_system->GetFunctionArgumentAtIndex(m_type, index);
119  return CompilerType();
120}
121
122bool CompilerType::IsFunctionPointerType() const {
123  if (IsValid())
124    return m_type_system->IsFunctionPointerType(m_type);
125  return false;
126}
127
128bool CompilerType::IsBlockPointerType(
129    CompilerType *function_pointer_type_ptr) const {
130  if (IsValid())
131    return m_type_system->IsBlockPointerType(m_type, function_pointer_type_ptr);
132  return false;
133}
134
135bool CompilerType::IsIntegerType(bool &is_signed) const {
136  if (IsValid())
137    return m_type_system->IsIntegerType(m_type, is_signed);
138  return false;
139}
140
141bool CompilerType::IsEnumerationType(bool &is_signed) const {
142  if (IsValid())
143    return m_type_system->IsEnumerationType(m_type, is_signed);
144  return false;
145}
146
147bool CompilerType::IsIntegerOrEnumerationType(bool &is_signed) const {
148  return IsIntegerType(is_signed) || IsEnumerationType(is_signed);
149}
150
151bool CompilerType::IsPointerType(CompilerType *pointee_type) const {
152  if (IsValid()) {
153    return m_type_system->IsPointerType(m_type, pointee_type);
154  }
155  if (pointee_type)
156    pointee_type->Clear();
157  return false;
158}
159
160bool CompilerType::IsPointerOrReferenceType(CompilerType *pointee_type) const {
161  if (IsValid()) {
162    return m_type_system->IsPointerOrReferenceType(m_type, pointee_type);
163  }
164  if (pointee_type)
165    pointee_type->Clear();
166  return false;
167}
168
169bool CompilerType::IsReferenceType(CompilerType *pointee_type,
170                                   bool *is_rvalue) const {
171  if (IsValid()) {
172    return m_type_system->IsReferenceType(m_type, pointee_type, is_rvalue);
173  }
174  if (pointee_type)
175    pointee_type->Clear();
176  return false;
177}
178
179bool CompilerType::ShouldTreatScalarValueAsAddress() const {
180  if (IsValid())
181    return m_type_system->ShouldTreatScalarValueAsAddress(m_type);
182  return false;
183}
184
185bool CompilerType::IsFloatingPointType(uint32_t &count,
186                                       bool &is_complex) const {
187  if (IsValid()) {
188    return m_type_system->IsFloatingPointType(m_type, count, is_complex);
189  }
190  count = 0;
191  is_complex = false;
192  return false;
193}
194
195bool CompilerType::IsDefined() const {
196  if (IsValid())
197    return m_type_system->IsDefined(m_type);
198  return true;
199}
200
201bool CompilerType::IsPolymorphicClass() const {
202  if (IsValid()) {
203    return m_type_system->IsPolymorphicClass(m_type);
204  }
205  return false;
206}
207
208bool CompilerType::IsPossibleDynamicType(CompilerType *dynamic_pointee_type,
209                                         bool check_cplusplus,
210                                         bool check_objc) const {
211  if (IsValid())
212    return m_type_system->IsPossibleDynamicType(m_type, dynamic_pointee_type,
213                                                check_cplusplus, check_objc);
214  return false;
215}
216
217bool CompilerType::IsScalarType() const {
218  if (!IsValid())
219    return false;
220
221  return m_type_system->IsScalarType(m_type);
222}
223
224bool CompilerType::IsTypedefType() const {
225  if (!IsValid())
226    return false;
227  return m_type_system->IsTypedefType(m_type);
228}
229
230bool CompilerType::IsVoidType() const {
231  if (!IsValid())
232    return false;
233  return m_type_system->IsVoidType(m_type);
234}
235
236bool CompilerType::IsPointerToScalarType() const {
237  if (!IsValid())
238    return false;
239
240  return IsPointerType() && GetPointeeType().IsScalarType();
241}
242
243bool CompilerType::IsArrayOfScalarType() const {
244  CompilerType element_type;
245  if (IsArrayType(&element_type, nullptr, nullptr))
246    return element_type.IsScalarType();
247  return false;
248}
249
250bool CompilerType::IsBeingDefined() const {
251  if (!IsValid())
252    return false;
253  return m_type_system->IsBeingDefined(m_type);
254}
255
256// Type Completion
257
258bool CompilerType::GetCompleteType() const {
259  if (!IsValid())
260    return false;
261  return m_type_system->GetCompleteType(m_type);
262}
263
264// AST related queries
265size_t CompilerType::GetPointerByteSize() const {
266  if (m_type_system)
267    return m_type_system->GetPointerByteSize();
268  return 0;
269}
270
271ConstString CompilerType::GetConstQualifiedTypeName() const {
272  return GetConstTypeName();
273}
274
275ConstString CompilerType::GetConstTypeName() const {
276  if (IsValid()) {
277    ConstString type_name(GetTypeName());
278    if (type_name)
279      return type_name;
280  }
281  return ConstString("<invalid>");
282}
283
284ConstString CompilerType::GetTypeName() const {
285  if (IsValid()) {
286    return m_type_system->GetTypeName(m_type);
287  }
288  return ConstString("<invalid>");
289}
290
291ConstString CompilerType::GetDisplayTypeName() const { return GetTypeName(); }
292
293uint32_t CompilerType::GetTypeInfo(
294    CompilerType *pointee_or_element_compiler_type) const {
295  if (!IsValid())
296    return 0;
297
298  return m_type_system->GetTypeInfo(m_type, pointee_or_element_compiler_type);
299}
300
301lldb::LanguageType CompilerType::GetMinimumLanguage() {
302  if (!IsValid())
303    return lldb::eLanguageTypeC;
304
305  return m_type_system->GetMinimumLanguage(m_type);
306}
307
308lldb::TypeClass CompilerType::GetTypeClass() const {
309  if (!IsValid())
310    return lldb::eTypeClassInvalid;
311
312  return m_type_system->GetTypeClass(m_type);
313}
314
315void CompilerType::SetCompilerType(TypeSystem *type_system,
316                                   lldb::opaque_compiler_type_t type) {
317  m_type_system = type_system;
318  m_type = type;
319}
320
321unsigned CompilerType::GetTypeQualifiers() const {
322  if (IsValid())
323    return m_type_system->GetTypeQualifiers(m_type);
324  return 0;
325}
326
327// Creating related types
328
329CompilerType CompilerType::GetArrayElementType(uint64_t *stride) const {
330  if (IsValid()) {
331    return m_type_system->GetArrayElementType(m_type, stride);
332  }
333  return CompilerType();
334}
335
336CompilerType CompilerType::GetArrayType(uint64_t size) const {
337  if (IsValid()) {
338    return m_type_system->GetArrayType(m_type, size);
339  }
340  return CompilerType();
341}
342
343CompilerType CompilerType::GetCanonicalType() const {
344  if (IsValid())
345    return m_type_system->GetCanonicalType(m_type);
346  return CompilerType();
347}
348
349CompilerType CompilerType::GetFullyUnqualifiedType() const {
350  if (IsValid())
351    return m_type_system->GetFullyUnqualifiedType(m_type);
352  return CompilerType();
353}
354
355int CompilerType::GetFunctionArgumentCount() const {
356  if (IsValid()) {
357    return m_type_system->GetFunctionArgumentCount(m_type);
358  }
359  return -1;
360}
361
362CompilerType CompilerType::GetFunctionArgumentTypeAtIndex(size_t idx) const {
363  if (IsValid()) {
364    return m_type_system->GetFunctionArgumentTypeAtIndex(m_type, idx);
365  }
366  return CompilerType();
367}
368
369CompilerType CompilerType::GetFunctionReturnType() const {
370  if (IsValid()) {
371    return m_type_system->GetFunctionReturnType(m_type);
372  }
373  return CompilerType();
374}
375
376size_t CompilerType::GetNumMemberFunctions() const {
377  if (IsValid()) {
378    return m_type_system->GetNumMemberFunctions(m_type);
379  }
380  return 0;
381}
382
383TypeMemberFunctionImpl CompilerType::GetMemberFunctionAtIndex(size_t idx) {
384  if (IsValid()) {
385    return m_type_system->GetMemberFunctionAtIndex(m_type, idx);
386  }
387  return TypeMemberFunctionImpl();
388}
389
390CompilerType CompilerType::GetNonReferenceType() const {
391  if (IsValid())
392    return m_type_system->GetNonReferenceType(m_type);
393  return CompilerType();
394}
395
396CompilerType CompilerType::GetPointeeType() const {
397  if (IsValid()) {
398    return m_type_system->GetPointeeType(m_type);
399  }
400  return CompilerType();
401}
402
403CompilerType CompilerType::GetPointerType() const {
404  if (IsValid()) {
405    return m_type_system->GetPointerType(m_type);
406  }
407  return CompilerType();
408}
409
410CompilerType CompilerType::GetLValueReferenceType() const {
411  if (IsValid())
412    return m_type_system->GetLValueReferenceType(m_type);
413  else
414    return CompilerType();
415}
416
417CompilerType CompilerType::GetRValueReferenceType() const {
418  if (IsValid())
419    return m_type_system->GetRValueReferenceType(m_type);
420  else
421    return CompilerType();
422}
423
424CompilerType CompilerType::GetAtomicType() const {
425  if (IsValid())
426    return m_type_system->GetAtomicType(m_type);
427  return CompilerType();
428}
429
430CompilerType CompilerType::AddConstModifier() const {
431  if (IsValid())
432    return m_type_system->AddConstModifier(m_type);
433  else
434    return CompilerType();
435}
436
437CompilerType CompilerType::AddVolatileModifier() const {
438  if (IsValid())
439    return m_type_system->AddVolatileModifier(m_type);
440  else
441    return CompilerType();
442}
443
444CompilerType CompilerType::AddRestrictModifier() const {
445  if (IsValid())
446    return m_type_system->AddRestrictModifier(m_type);
447  else
448    return CompilerType();
449}
450
451CompilerType
452CompilerType::CreateTypedef(const char *name,
453                            const CompilerDeclContext &decl_ctx) const {
454  if (IsValid())
455    return m_type_system->CreateTypedef(m_type, name, decl_ctx);
456  else
457    return CompilerType();
458}
459
460CompilerType CompilerType::GetTypedefedType() const {
461  if (IsValid())
462    return m_type_system->GetTypedefedType(m_type);
463  else
464    return CompilerType();
465}
466
467// Create related types using the current type's AST
468
469CompilerType
470CompilerType::GetBasicTypeFromAST(lldb::BasicType basic_type) const {
471  if (IsValid())
472    return m_type_system->GetBasicTypeFromAST(basic_type);
473  return CompilerType();
474}
475// Exploring the type
476
477llvm::Optional<uint64_t>
478CompilerType::GetBitSize(ExecutionContextScope *exe_scope) const {
479  if (IsValid())
480    return m_type_system->GetBitSize(m_type, exe_scope);
481  return {};
482}
483
484llvm::Optional<uint64_t>
485CompilerType::GetByteSize(ExecutionContextScope *exe_scope) const {
486  if (llvm::Optional<uint64_t> bit_size = GetBitSize(exe_scope))
487    return (*bit_size + 7) / 8;
488  return {};
489}
490
491llvm::Optional<size_t> CompilerType::GetTypeBitAlign(ExecutionContextScope *exe_scope) const {
492  if (IsValid())
493    return m_type_system->GetTypeBitAlign(m_type, exe_scope);
494  return {};
495}
496
497lldb::Encoding CompilerType::GetEncoding(uint64_t &count) const {
498  if (!IsValid())
499    return lldb::eEncodingInvalid;
500
501  return m_type_system->GetEncoding(m_type, count);
502}
503
504lldb::Format CompilerType::GetFormat() const {
505  if (!IsValid())
506    return lldb::eFormatDefault;
507
508  return m_type_system->GetFormat(m_type);
509}
510
511uint32_t CompilerType::GetNumChildren(bool omit_empty_base_classes,
512                                      const ExecutionContext *exe_ctx) const {
513  if (!IsValid())
514    return 0;
515  return m_type_system->GetNumChildren(m_type, omit_empty_base_classes,
516                                       exe_ctx);
517}
518
519lldb::BasicType CompilerType::GetBasicTypeEnumeration() const {
520  if (IsValid())
521    return m_type_system->GetBasicTypeEnumeration(m_type);
522  return eBasicTypeInvalid;
523}
524
525void CompilerType::ForEachEnumerator(
526    std::function<bool(const CompilerType &integer_type,
527                       ConstString name,
528                       const llvm::APSInt &value)> const &callback) const {
529  if (IsValid())
530    return m_type_system->ForEachEnumerator(m_type, callback);
531}
532
533uint32_t CompilerType::GetNumFields() const {
534  if (!IsValid())
535    return 0;
536  return m_type_system->GetNumFields(m_type);
537}
538
539CompilerType CompilerType::GetFieldAtIndex(size_t idx, std::string &name,
540                                           uint64_t *bit_offset_ptr,
541                                           uint32_t *bitfield_bit_size_ptr,
542                                           bool *is_bitfield_ptr) const {
543  if (!IsValid())
544    return CompilerType();
545  return m_type_system->GetFieldAtIndex(m_type, idx, name, bit_offset_ptr,
546                                        bitfield_bit_size_ptr, is_bitfield_ptr);
547}
548
549uint32_t CompilerType::GetNumDirectBaseClasses() const {
550  if (IsValid())
551    return m_type_system->GetNumDirectBaseClasses(m_type);
552  return 0;
553}
554
555uint32_t CompilerType::GetNumVirtualBaseClasses() const {
556  if (IsValid())
557    return m_type_system->GetNumVirtualBaseClasses(m_type);
558  return 0;
559}
560
561CompilerType
562CompilerType::GetDirectBaseClassAtIndex(size_t idx,
563                                        uint32_t *bit_offset_ptr) const {
564  if (IsValid())
565    return m_type_system->GetDirectBaseClassAtIndex(m_type, idx,
566                                                    bit_offset_ptr);
567  return CompilerType();
568}
569
570CompilerType
571CompilerType::GetVirtualBaseClassAtIndex(size_t idx,
572                                         uint32_t *bit_offset_ptr) const {
573  if (IsValid())
574    return m_type_system->GetVirtualBaseClassAtIndex(m_type, idx,
575                                                     bit_offset_ptr);
576  return CompilerType();
577}
578
579uint32_t CompilerType::GetIndexOfFieldWithName(
580    const char *name, CompilerType *field_compiler_type_ptr,
581    uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr,
582    bool *is_bitfield_ptr) const {
583  unsigned count = GetNumFields();
584  std::string field_name;
585  for (unsigned index = 0; index < count; index++) {
586    CompilerType field_compiler_type(
587        GetFieldAtIndex(index, field_name, bit_offset_ptr,
588                        bitfield_bit_size_ptr, is_bitfield_ptr));
589    if (strcmp(field_name.c_str(), name) == 0) {
590      if (field_compiler_type_ptr)
591        *field_compiler_type_ptr = field_compiler_type;
592      return index;
593    }
594  }
595  return UINT32_MAX;
596}
597
598CompilerType CompilerType::GetChildCompilerTypeAtIndex(
599    ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
600    bool omit_empty_base_classes, bool ignore_array_bounds,
601    std::string &child_name, uint32_t &child_byte_size,
602    int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
603    uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
604    bool &child_is_deref_of_parent, ValueObject *valobj,
605    uint64_t &language_flags) const {
606  if (!IsValid())
607    return CompilerType();
608  return m_type_system->GetChildCompilerTypeAtIndex(
609      m_type, exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
610      ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
611      child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
612      child_is_deref_of_parent, valobj, language_flags);
613}
614
615// Look for a child member (doesn't include base classes, but it does include
616// their members) in the type hierarchy. Returns an index path into
617// "clang_type" on how to reach the appropriate member.
618//
619//    class A
620//    {
621//    public:
622//        int m_a;
623//        int m_b;
624//    };
625//
626//    class B
627//    {
628//    };
629//
630//    class C :
631//        public B,
632//        public A
633//    {
634//    };
635//
636// If we have a clang type that describes "class C", and we wanted to looked
637// "m_b" in it:
638//
639// With omit_empty_base_classes == false we would get an integer array back
640// with: { 1,  1 } The first index 1 is the child index for "class A" within
641// class C The second index 1 is the child index for "m_b" within class A
642//
643// With omit_empty_base_classes == true we would get an integer array back
644// with: { 0,  1 } The first index 0 is the child index for "class A" within
645// class C (since class B doesn't have any members it doesn't count) The second
646// index 1 is the child index for "m_b" within class A
647
648size_t CompilerType::GetIndexOfChildMemberWithName(
649    const char *name, bool omit_empty_base_classes,
650    std::vector<uint32_t> &child_indexes) const {
651  if (IsValid() && name && name[0]) {
652    return m_type_system->GetIndexOfChildMemberWithName(
653        m_type, name, omit_empty_base_classes, child_indexes);
654  }
655  return 0;
656}
657
658size_t CompilerType::GetNumTemplateArguments() const {
659  if (IsValid()) {
660    return m_type_system->GetNumTemplateArguments(m_type);
661  }
662  return 0;
663}
664
665TemplateArgumentKind CompilerType::GetTemplateArgumentKind(size_t idx) const {
666  if (IsValid())
667    return m_type_system->GetTemplateArgumentKind(m_type, idx);
668  return eTemplateArgumentKindNull;
669}
670
671CompilerType CompilerType::GetTypeTemplateArgument(size_t idx) const {
672  if (IsValid()) {
673    return m_type_system->GetTypeTemplateArgument(m_type, idx);
674  }
675  return CompilerType();
676}
677
678llvm::Optional<CompilerType::IntegralTemplateArgument>
679CompilerType::GetIntegralTemplateArgument(size_t idx) const {
680  if (IsValid())
681    return m_type_system->GetIntegralTemplateArgument(m_type, idx);
682  return llvm::None;
683}
684
685CompilerType CompilerType::GetTypeForFormatters() const {
686  if (IsValid())
687    return m_type_system->GetTypeForFormatters(m_type);
688  return CompilerType();
689}
690
691LazyBool CompilerType::ShouldPrintAsOneLiner(ValueObject *valobj) const {
692  if (IsValid())
693    return m_type_system->ShouldPrintAsOneLiner(m_type, valobj);
694  return eLazyBoolCalculate;
695}
696
697bool CompilerType::IsMeaninglessWithoutDynamicResolution() const {
698  if (IsValid())
699    return m_type_system->IsMeaninglessWithoutDynamicResolution(m_type);
700  return false;
701}
702
703// Get the index of the child of "clang_type" whose name matches. This function
704// doesn't descend into the children, but only looks one level deep and name
705// matches can include base class names.
706
707uint32_t
708CompilerType::GetIndexOfChildWithName(const char *name,
709                                      bool omit_empty_base_classes) const {
710  if (IsValid() && name && name[0]) {
711    return m_type_system->GetIndexOfChildWithName(m_type, name,
712                                                  omit_empty_base_classes);
713  }
714  return UINT32_MAX;
715}
716
717// Dumping types
718#define DEPTH_INCREMENT 2
719
720void CompilerType::DumpValue(ExecutionContext *exe_ctx, Stream *s,
721                             lldb::Format format, const DataExtractor &data,
722                             lldb::offset_t data_byte_offset,
723                             size_t data_byte_size, uint32_t bitfield_bit_size,
724                             uint32_t bitfield_bit_offset, bool show_types,
725                             bool show_summary, bool verbose, uint32_t depth) {
726  if (!IsValid())
727    return;
728  m_type_system->DumpValue(m_type, exe_ctx, s, format, data, data_byte_offset,
729                           data_byte_size, bitfield_bit_size,
730                           bitfield_bit_offset, show_types, show_summary,
731                           verbose, depth);
732}
733
734bool CompilerType::DumpTypeValue(Stream *s, lldb::Format format,
735                                 const DataExtractor &data,
736                                 lldb::offset_t byte_offset, size_t byte_size,
737                                 uint32_t bitfield_bit_size,
738                                 uint32_t bitfield_bit_offset,
739                                 ExecutionContextScope *exe_scope) {
740  if (!IsValid())
741    return false;
742  return m_type_system->DumpTypeValue(m_type, s, format, data, byte_offset,
743                                      byte_size, bitfield_bit_size,
744                                      bitfield_bit_offset, exe_scope);
745}
746
747void CompilerType::DumpSummary(ExecutionContext *exe_ctx, Stream *s,
748                               const DataExtractor &data,
749                               lldb::offset_t data_byte_offset,
750                               size_t data_byte_size) {
751  if (IsValid())
752    m_type_system->DumpSummary(m_type, exe_ctx, s, data, data_byte_offset,
753                               data_byte_size);
754}
755
756void CompilerType::DumpTypeDescription() const {
757  if (IsValid())
758    m_type_system->DumpTypeDescription(m_type);
759}
760
761void CompilerType::DumpTypeDescription(Stream *s) const {
762  if (IsValid()) {
763    m_type_system->DumpTypeDescription(m_type, s);
764  }
765}
766
767#ifndef NDEBUG
768LLVM_DUMP_METHOD void CompilerType::dump() const {
769  if (IsValid())
770    m_type_system->dump(m_type);
771  else
772    llvm::errs() << "<invalid>\n";
773}
774#endif
775
776bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data,
777                                    lldb::offset_t data_byte_offset,
778                                    size_t data_byte_size,
779                                    Scalar &value) const {
780  if (!IsValid())
781    return false;
782
783  if (IsAggregateType()) {
784    return false; // Aggregate types don't have scalar values
785  } else {
786    uint64_t count = 0;
787    lldb::Encoding encoding = GetEncoding(count);
788
789    if (encoding == lldb::eEncodingInvalid || count != 1)
790      return false;
791
792    llvm::Optional<uint64_t> byte_size = GetByteSize(nullptr);
793    if (!byte_size)
794      return false;
795    lldb::offset_t offset = data_byte_offset;
796    switch (encoding) {
797    case lldb::eEncodingInvalid:
798      break;
799    case lldb::eEncodingVector:
800      break;
801    case lldb::eEncodingUint:
802      if (*byte_size <= sizeof(unsigned long long)) {
803        uint64_t uval64 = data.GetMaxU64(&offset, *byte_size);
804        if (*byte_size <= sizeof(unsigned int)) {
805          value = (unsigned int)uval64;
806          return true;
807        } else if (*byte_size <= sizeof(unsigned long)) {
808          value = (unsigned long)uval64;
809          return true;
810        } else if (*byte_size <= sizeof(unsigned long long)) {
811          value = (unsigned long long)uval64;
812          return true;
813        } else
814          value.Clear();
815      }
816      break;
817
818    case lldb::eEncodingSint:
819      if (*byte_size <= sizeof(long long)) {
820        int64_t sval64 = data.GetMaxS64(&offset, *byte_size);
821        if (*byte_size <= sizeof(int)) {
822          value = (int)sval64;
823          return true;
824        } else if (*byte_size <= sizeof(long)) {
825          value = (long)sval64;
826          return true;
827        } else if (*byte_size <= sizeof(long long)) {
828          value = (long long)sval64;
829          return true;
830        } else
831          value.Clear();
832      }
833      break;
834
835    case lldb::eEncodingIEEE754:
836      if (*byte_size <= sizeof(long double)) {
837        uint32_t u32;
838        uint64_t u64;
839        if (*byte_size == sizeof(float)) {
840          if (sizeof(float) == sizeof(uint32_t)) {
841            u32 = data.GetU32(&offset);
842            value = *((float *)&u32);
843            return true;
844          } else if (sizeof(float) == sizeof(uint64_t)) {
845            u64 = data.GetU64(&offset);
846            value = *((float *)&u64);
847            return true;
848          }
849        } else if (*byte_size == sizeof(double)) {
850          if (sizeof(double) == sizeof(uint32_t)) {
851            u32 = data.GetU32(&offset);
852            value = *((double *)&u32);
853            return true;
854          } else if (sizeof(double) == sizeof(uint64_t)) {
855            u64 = data.GetU64(&offset);
856            value = *((double *)&u64);
857            return true;
858          }
859        } else if (*byte_size == sizeof(long double)) {
860          if (sizeof(long double) == sizeof(uint32_t)) {
861            u32 = data.GetU32(&offset);
862            value = *((long double *)&u32);
863            return true;
864          } else if (sizeof(long double) == sizeof(uint64_t)) {
865            u64 = data.GetU64(&offset);
866            value = *((long double *)&u64);
867            return true;
868          }
869        }
870      }
871      break;
872    }
873  }
874  return false;
875}
876
877bool lldb_private::operator==(const lldb_private::CompilerType &lhs,
878                              const lldb_private::CompilerType &rhs) {
879  return lhs.GetTypeSystem() == rhs.GetTypeSystem() &&
880         lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType();
881}
882
883bool lldb_private::operator!=(const lldb_private::CompilerType &lhs,
884                              const lldb_private::CompilerType &rhs) {
885  return !(lhs == rhs);
886}
887