LLDB mainline
DILAST.h
Go to the documentation of this file.
1//===-- DILAST.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_VALUEOBJECT_DILAST_H
10#define LLDB_VALUEOBJECT_DILAST_H
11
13#include "llvm/Support/Error.h"
14#include <cstdint>
15#include <string>
16
18
19/// The various types DIL AST nodes (used by the DIL parser).
31
32/// The Unary operators recognized by DIL.
33enum class UnaryOpKind {
34 AddrOf, // "&"
35 Deref, // "*"
36};
37
38/// Forward declaration, for use in DIL AST nodes. Definition is at the very
39/// end of this file.
40class Visitor;
41
42/// The rest of the classes in this file, except for the Visitor class at the
43/// very end, define all the types of AST nodes used by the DIL parser and
44/// expression evaluator. The DIL parser parses the input string and creates
45/// the AST parse tree from the AST nodes. The resulting AST node tree gets
46/// passed to the DIL expression evaluator, which evaluates the DIL AST nodes
47/// and creates/returns a ValueObjectSP containing the result.
48
49/// Base class for AST nodes used by the Data Inspection Language (DIL) parser.
50/// All of the specialized types of AST nodes inherit from this (virtual) base
51/// class.
52class ASTNode {
53public:
54 ASTNode(uint32_t location, NodeKind kind)
55 : m_location(location), m_kind(kind) {}
56 virtual ~ASTNode() = default;
57
58 virtual llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const = 0;
59
60 uint32_t GetLocation() const { return m_location; }
61 NodeKind GetKind() const { return m_kind; }
62
63private:
64 uint32_t m_location;
66};
67
68using ASTNodeUP = std::unique_ptr<ASTNode>;
69
70class ErrorNode : public ASTNode {
71public:
73 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
74
75 static bool classof(const ASTNode *node) {
76 return node->GetKind() == NodeKind::eErrorNode;
77 }
78};
79
80class IdentifierNode : public ASTNode {
81public:
82 IdentifierNode(uint32_t location, std::string name)
83 : ASTNode(location, NodeKind::eIdentifierNode), m_name(std::move(name)) {}
84
85 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
86
87 std::string GetName() const { return m_name; }
88
89 static bool classof(const ASTNode *node) {
90 return node->GetKind() == NodeKind::eIdentifierNode;
91 }
92
93private:
94 std::string m_name;
95};
96
97class MemberOfNode : public ASTNode {
98public:
99 MemberOfNode(uint32_t location, ASTNodeUP base, bool is_arrow,
100 std::string name)
101 : ASTNode(location, NodeKind::eMemberOfNode), m_base(std::move(base)),
102 m_is_arrow(is_arrow), m_field_name(std::move(name)) {}
103
104 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
105
106 ASTNode *GetBase() const { return m_base.get(); }
107 bool GetIsArrow() const { return m_is_arrow; }
108 llvm::StringRef GetFieldName() const { return llvm::StringRef(m_field_name); }
109
110 static bool classof(const ASTNode *node) {
111 return node->GetKind() == NodeKind::eMemberOfNode;
112 }
113
114private:
117 std::string m_field_name;
118};
119
120class UnaryOpNode : public ASTNode {
121public:
122 UnaryOpNode(uint32_t location, UnaryOpKind kind, ASTNodeUP operand)
123 : ASTNode(location, NodeKind::eUnaryOpNode), m_kind(kind),
124 m_operand(std::move(operand)) {}
125
126 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
127
128 UnaryOpKind GetKind() const { return m_kind; }
129 ASTNode *GetOperand() const { return m_operand.get(); }
130
131 static bool classof(const ASTNode *node) {
132 return node->GetKind() == NodeKind::eUnaryOpNode;
133 }
134
135private:
138};
139
141public:
142 ArraySubscriptNode(uint32_t location, ASTNodeUP base, int64_t index)
144 m_base(std::move(base)), m_index(index) {}
145
146 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
147
148 ASTNode *GetBase() const { return m_base.get(); }
149 int64_t GetIndex() const { return m_index; }
150
151 static bool classof(const ASTNode *node) {
152 return node->GetKind() == NodeKind::eArraySubscriptNode;
153 }
154
155private:
157 int64_t m_index;
158};
159
161public:
162 BitFieldExtractionNode(uint32_t location, ASTNodeUP base, int64_t first_index,
163 int64_t last_index)
164 : ASTNode(location, NodeKind::eBitExtractionNode),
165 m_base(std::move(base)), m_first_index(first_index),
166 m_last_index(last_index) {}
167
168 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
169
170 ASTNode *GetBase() const { return m_base.get(); }
171 int64_t GetFirstIndex() const { return m_first_index; }
172 int64_t GetLastIndex() const { return m_last_index; }
173
174 static bool classof(const ASTNode *node) {
175 return node->GetKind() == NodeKind::eBitExtractionNode;
176 }
177
178private:
182};
183
185
187public:
188 IntegerLiteralNode(uint32_t location, llvm::APInt value, uint32_t radix,
189 bool is_unsigned, IntegerTypeSuffix type)
191 m_value(std::move(value)), m_radix(radix), m_is_unsigned(is_unsigned),
192 m_type(type) {}
193
194 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
195
196 const llvm::APInt &GetValue() const { return m_value; }
197 uint32_t GetRadix() const { return m_radix; }
198 bool IsUnsigned() const { return m_is_unsigned; }
200
201 static bool classof(const ASTNode *node) {
202 return node->GetKind() == NodeKind::eIntegerLiteralNode;
203 }
204
205private:
206 llvm::APInt m_value;
207 uint32_t m_radix;
210};
211
212class FloatLiteralNode : public ASTNode {
213public:
214 FloatLiteralNode(uint32_t location, llvm::APFloat value)
215 : ASTNode(location, NodeKind::eFloatLiteralNode),
216 m_value(std::move(value)) {}
217
218 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
219
220 const llvm::APFloat &GetValue() const { return m_value; }
221
222 static bool classof(const ASTNode *node) {
223 return node->GetKind() == NodeKind::eFloatLiteralNode;
224 }
225
226private:
227 llvm::APFloat m_value;
228};
229
231public:
232 BooleanLiteralNode(uint32_t location, bool value)
233 : ASTNode(location, NodeKind::eBooleanLiteralNode), m_value(value) {}
234
235 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
236
237 bool GetValue() const & { return m_value; }
238
239 static bool classof(const ASTNode *node) {
240 return node->GetKind() == NodeKind::eBooleanLiteralNode;
241 }
242
243private:
245};
246
247/// This class contains one Visit method for each specialized type of
248/// DIL AST node. The Visit methods are used to dispatch a DIL AST node to
249/// the correct function in the DIL expression evaluator for evaluating that
250/// type of AST node.
251class Visitor {
252public:
253 virtual ~Visitor() = default;
254 virtual llvm::Expected<lldb::ValueObjectSP>
255 Visit(const IdentifierNode *node) = 0;
256 virtual llvm::Expected<lldb::ValueObjectSP>
257 Visit(const MemberOfNode *node) = 0;
258 virtual llvm::Expected<lldb::ValueObjectSP>
259 Visit(const UnaryOpNode *node) = 0;
260 virtual llvm::Expected<lldb::ValueObjectSP>
261 Visit(const ArraySubscriptNode *node) = 0;
262 virtual llvm::Expected<lldb::ValueObjectSP>
264 virtual llvm::Expected<lldb::ValueObjectSP>
265 Visit(const IntegerLiteralNode *node) = 0;
266 virtual llvm::Expected<lldb::ValueObjectSP>
267 Visit(const FloatLiteralNode *node) = 0;
268 virtual llvm::Expected<lldb::ValueObjectSP>
269 Visit(const BooleanLiteralNode *node) = 0;
270};
271
272} // namespace lldb_private::dil
273
274#endif // LLDB_VALUEOBJECT_DILAST_H
uint32_t GetLocation() const
Definition DILAST.h:60
ASTNode(uint32_t location, NodeKind kind)
Definition DILAST.h:54
virtual ~ASTNode()=default
NodeKind GetKind() const
Definition DILAST.h:61
virtual llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const =0
const NodeKind m_kind
Definition DILAST.h:65
ArraySubscriptNode(uint32_t location, ASTNodeUP base, int64_t index)
Definition DILAST.h:142
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:31
static bool classof(const ASTNode *node)
Definition DILAST.h:151
BitFieldExtractionNode(uint32_t location, ASTNodeUP base, int64_t first_index, int64_t last_index)
Definition DILAST.h:162
static bool classof(const ASTNode *node)
Definition DILAST.h:174
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:36
static bool classof(const ASTNode *node)
Definition DILAST.h:239
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:50
BooleanLiteralNode(uint32_t location, bool value)
Definition DILAST.h:232
static bool classof(const ASTNode *node)
Definition DILAST.h:75
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:14
const llvm::APFloat & GetValue() const
Definition DILAST.h:220
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:45
FloatLiteralNode(uint32_t location, llvm::APFloat value)
Definition DILAST.h:214
static bool classof(const ASTNode *node)
Definition DILAST.h:222
static bool classof(const ASTNode *node)
Definition DILAST.h:89
IdentifierNode(uint32_t location, std::string name)
Definition DILAST.h:82
std::string GetName() const
Definition DILAST.h:87
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:18
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:41
IntegerLiteralNode(uint32_t location, llvm::APInt value, uint32_t radix, bool is_unsigned, IntegerTypeSuffix type)
Definition DILAST.h:188
static bool classof(const ASTNode *node)
Definition DILAST.h:201
IntegerTypeSuffix GetTypeSuffix() const
Definition DILAST.h:199
const llvm::APInt & GetValue() const
Definition DILAST.h:196
MemberOfNode(uint32_t location, ASTNodeUP base, bool is_arrow, std::string name)
Definition DILAST.h:99
static bool classof(const ASTNode *node)
Definition DILAST.h:110
llvm::StringRef GetFieldName() const
Definition DILAST.h:108
ASTNode * GetBase() const
Definition DILAST.h:106
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:22
ASTNode * GetOperand() const
Definition DILAST.h:129
static bool classof(const ASTNode *node)
Definition DILAST.h:131
UnaryOpNode(uint32_t location, UnaryOpKind kind, ASTNodeUP operand)
Definition DILAST.h:122
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:26
UnaryOpKind GetKind() const
Definition DILAST.h:128
This class contains one Visit method for each specialized type of DIL AST node.
Definition DILAST.h:251
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const IntegerLiteralNode *node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const UnaryOpNode *node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const FloatLiteralNode *node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const BooleanLiteralNode *node)=0
virtual ~Visitor()=default
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const IdentifierNode *node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const ArraySubscriptNode *node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const MemberOfNode *node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const BitFieldExtractionNode *node)=0
UnaryOpKind
The Unary operators recognized by DIL.
Definition DILAST.h:33
std::unique_ptr< ASTNode > ASTNodeUP
Definition DILAST.h:68
NodeKind
The various types DIL AST nodes (used by the DIL parser).
Definition DILAST.h:20