1 //===-- LibCxxOptional.cpp --------------------------------------*- 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 //===----------------------------------------------------------------------===//
11 #include "lldb/DataFormatters/FormattersHelpers.h"
14 using namespace lldb_private;
18 class OptionalFrontEnd : public SyntheticChildrenFrontEnd {
20 OptionalFrontEnd(ValueObject &valobj) : SyntheticChildrenFrontEnd(valobj) {
24 size_t GetIndexOfChildWithName(const ConstString &name) override {
25 return formatters::ExtractIndexFromString(name.GetCString());
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;
35 ValueObjectSP m_base_sp;
39 bool OptionalFrontEnd::Update() {
40 ValueObjectSP engaged_sp(
41 m_backend.GetChildMemberWithName(ConstString("__engaged_"), true));
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
49 m_size = engaged_sp->GetValueAsUnsigned(0);
54 ValueObjectSP OptionalFrontEnd::GetChildAtIndex(size_t idx) {
56 return ValueObjectSP();
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_.
63 m_backend.GetChildMemberWithName(ConstString("__engaged_"), true)
65 ->GetChildAtIndex(0, true)
66 ->GetChildMemberWithName(ConstString("__val_"), true));
69 return ValueObjectSP();
71 CompilerType holder_type = val_sp->GetCompilerType();
74 return ValueObjectSP();
76 return val_sp->Clone(ConstString(llvm::formatv("Value").str()));
79 SyntheticChildrenFrontEnd *
80 formatters::LibcxxOptionalFrontEndCreator(CXXSyntheticChildren *,
81 lldb::ValueObjectSP valobj_sp) {
83 return new OptionalFrontEnd(*valobj_sp);