1 //===-- LibCxxOptional.cpp --------------------------------------*- 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 //===----------------------------------------------------------------------===//
10 #include "lldb/DataFormatters/FormattersHelpers.h"
13 using namespace lldb_private;
17 class OptionalFrontEnd : public SyntheticChildrenFrontEnd {
19 OptionalFrontEnd(ValueObject &valobj) : SyntheticChildrenFrontEnd(valobj) {
23 size_t GetIndexOfChildWithName(ConstString name) override {
24 return formatters::ExtractIndexFromString(name.GetCString());
27 bool MightHaveChildren() override { return true; }
28 bool Update() override;
29 size_t CalculateNumChildren() override { return m_size; }
30 ValueObjectSP GetChildAtIndex(size_t idx) override;
37 bool OptionalFrontEnd::Update() {
38 ValueObjectSP engaged_sp(
39 m_backend.GetChildMemberWithName(ConstString("__engaged_"), true));
44 // __engaged_ is a bool flag and is true if the optional contains a value.
45 // Converting it to unsigned gives us a size of 1 if it contains a value
47 m_size = engaged_sp->GetValueAsUnsigned(0);
52 ValueObjectSP OptionalFrontEnd::GetChildAtIndex(size_t idx) {
54 return ValueObjectSP();
56 // __val_ contains the underlying value of an optional if it has one.
57 // Currently because it is part of an anonymous union GetChildMemberWithName()
58 // does not peer through and find it unless we are at the parent itself.
59 // We can obtain the parent through __engaged_.
61 m_backend.GetChildMemberWithName(ConstString("__engaged_"), true)
63 ->GetChildAtIndex(0, true)
64 ->GetChildMemberWithName(ConstString("__val_"), true));
67 return ValueObjectSP();
69 CompilerType holder_type = val_sp->GetCompilerType();
72 return ValueObjectSP();
74 return val_sp->Clone(ConstString(llvm::formatv("Value").str()));
77 SyntheticChildrenFrontEnd *
78 formatters::LibcxxOptionalFrontEndCreator(CXXSyntheticChildren *,
79 lldb::ValueObjectSP valobj_sp) {
81 return new OptionalFrontEnd(*valobj_sp);