1 //===-- Materializer.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_EXPRESSION_MATERIALIZER_H
10 #define LLDB_EXPRESSION_MATERIALIZER_H
11 
12 #include <memory>
13 #include <vector>
14 
15 #include "lldb/Expression/IRMemoryMap.h"
16 #include "lldb/Symbol/TaggedASTType.h"
17 #include "lldb/Target/StackFrame.h"
18 #include "lldb/Utility/Status.h"
19 #include "lldb/lldb-private-types.h"
20 
21 namespace lldb_private {
22 
23 class Materializer {
24 public:
25   Materializer() = default;
26   ~Materializer();
27 
28   class Dematerializer {
29   public:
30     Dematerializer() = default;
31 
32     ~Dematerializer() { Wipe(); }
33 
34     void Dematerialize(Status &err, lldb::addr_t frame_top,
35                        lldb::addr_t frame_bottom);
36 
37     void Wipe();
38 
39     bool IsValid() {
40       return m_materializer && m_map &&
41              (m_process_address != LLDB_INVALID_ADDRESS);
42     }
43 
44   private:
45     friend class Materializer;
46 
47     Dematerializer(Materializer &materializer, lldb::StackFrameSP &frame_sp,
48                    IRMemoryMap &map, lldb::addr_t process_address)
49         : m_materializer(&materializer), m_map(&map),
50           m_process_address(process_address) {
51       if (frame_sp) {
52         m_thread_wp = frame_sp->GetThread();
53         m_stack_id = frame_sp->GetStackID();
54       }
55     }
56 
57     Materializer *m_materializer = nullptr;
58     lldb::ThreadWP m_thread_wp;
59     StackID m_stack_id;
60     IRMemoryMap *m_map = nullptr;
61     lldb::addr_t m_process_address = LLDB_INVALID_ADDRESS;
62   };
63 
64   typedef std::shared_ptr<Dematerializer> DematerializerSP;
65   typedef std::weak_ptr<Dematerializer> DematerializerWP;
66 
67   DematerializerSP Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
68                                lldb::addr_t process_address, Status &err);
69 
70   class PersistentVariableDelegate {
71   public:
72     virtual ~PersistentVariableDelegate();
73     virtual ConstString GetName() = 0;
74     virtual void DidDematerialize(lldb::ExpressionVariableSP &variable) = 0;
75   };
76 
77   uint32_t
78   AddPersistentVariable(lldb::ExpressionVariableSP &persistent_variable_sp,
79                         PersistentVariableDelegate *delegate, Status &err);
80   uint32_t AddVariable(lldb::VariableSP &variable_sp, Status &err);
81   uint32_t AddResultVariable(const CompilerType &type, bool is_lvalue,
82                              bool keep_in_memory,
83                              PersistentVariableDelegate *delegate, Status &err);
84   uint32_t AddSymbol(const Symbol &symbol_sp, Status &err);
85   uint32_t AddRegister(const RegisterInfo &register_info, Status &err);
86 
87   uint32_t GetStructAlignment() { return m_struct_alignment; }
88 
89   uint32_t GetStructByteSize() { return m_current_offset; }
90 
91   class Entity {
92   public:
93     Entity() : m_alignment(1), m_size(0), m_offset(0) {}
94 
95     virtual ~Entity() = default;
96 
97     virtual void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
98                              lldb::addr_t process_address, Status &err) = 0;
99     virtual void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
100                                lldb::addr_t process_address,
101                                lldb::addr_t frame_top,
102                                lldb::addr_t frame_bottom, Status &err) = 0;
103     virtual void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address,
104                            Log *log) = 0;
105     virtual void Wipe(IRMemoryMap &map, lldb::addr_t process_address) = 0;
106 
107     uint32_t GetAlignment() { return m_alignment; }
108 
109     uint32_t GetSize() { return m_size; }
110 
111     uint32_t GetOffset() { return m_offset; }
112 
113     void SetOffset(uint32_t offset) { m_offset = offset; }
114 
115   protected:
116     uint32_t m_alignment;
117     uint32_t m_size;
118     uint32_t m_offset;
119   };
120 
121 private:
122   uint32_t AddStructMember(Entity &entity);
123 
124   typedef std::unique_ptr<Entity> EntityUP;
125   typedef std::vector<EntityUP> EntityVector;
126 
127   DematerializerWP m_dematerializer_wp;
128   EntityVector m_entities;
129   uint32_t m_current_offset = 0;
130   uint32_t m_struct_alignment = 8;
131 };
132 
133 } // namespace lldb_private
134 
135 #endif // LLDB_EXPRESSION_MATERIALIZER_H
136