blob: 762b824f262a8f26a4060c009875757f8cbe03d6 [file] [log] [blame]
Adrian Prantlc53d3682018-08-15 22:48:48 +00001//===-- LibCxxOptional.cpp --------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "LibCxx.h"
11#include "lldb/DataFormatters/FormattersHelpers.h"
12
13using namespace lldb;
14using namespace lldb_private;
15
16namespace {
17
18class OptionalFrontEnd : public SyntheticChildrenFrontEnd {
19public:
20 OptionalFrontEnd(ValueObject &valobj) : SyntheticChildrenFrontEnd(valobj) {
21 Update();
22 }
23
24 size_t GetIndexOfChildWithName(const ConstString &name) override {
25 return formatters::ExtractIndexFromString(name.GetCString());
26 }
27
28 bool MightHaveChildren() override { return true; }
29 bool Update() override;
30 size_t CalculateNumChildren() override { return m_size; }
31 ValueObjectSP GetChildAtIndex(size_t idx) override;
32
33private:
34 size_t m_size = 0;
35 ValueObjectSP m_base_sp;
36};
37} // namespace
38
39bool OptionalFrontEnd::Update() {
40 ValueObjectSP engaged_sp(
41 m_backend.GetChildMemberWithName(ConstString("__engaged_"), true));
42
43 if (!engaged_sp)
44 return false;
45
46 // __engaged_ is a bool flag and is true if the optional contains a value.
47 // Converting it to unsigned gives us a size of 1 if it contains a value
48 // and 0 if not.
49 m_size = engaged_sp->GetValueAsUnsigned(0);
50
51 return false;
52}
53
54ValueObjectSP OptionalFrontEnd::GetChildAtIndex(size_t idx) {
55 if (idx >= m_size)
56 return ValueObjectSP();
57
58 // __val_ contains the underlying value of an optional if it has one.
59 // Currently because it is part of an anonymous union GetChildMemberWithName()
60 // does not peer through and find it unless we are at the parent itself.
61 // We can obtain the parent through __engaged_.
62 ValueObjectSP val_sp(
63 m_backend.GetChildMemberWithName(ConstString("__engaged_"), true)
64 ->GetParent()
65 ->GetChildAtIndex(0, true)
66 ->GetChildMemberWithName(ConstString("__val_"), true));
67
68 if (!val_sp)
69 return ValueObjectSP();
70
71 CompilerType holder_type = val_sp->GetCompilerType();
72
73 if (!holder_type)
74 return ValueObjectSP();
75
76 return val_sp->Clone(ConstString(llvm::formatv("Value").str()));
77}
78
79SyntheticChildrenFrontEnd *
80formatters::LibcxxOptionalFrontEndCreator(CXXSyntheticChildren *,
81 lldb::ValueObjectSP valobj_sp) {
82 if (valobj_sp)
83 return new OptionalFrontEnd(*valobj_sp);
84 return nullptr;
85}