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
14#include "llvm/Support/Error.h"
15#include <cstdint>
16#include <string>
17
19
20/// The various types DIL AST nodes (used by the DIL parser).
34
35/// The Unary operators recognized by DIL.
36enum class UnaryOpKind {
37 AddrOf, // "&"
38 Deref, // "*"
39 Minus, // "-"
40 Plus, // "+"
41};
42
43/// The binary operators recognized by DIL.
44enum class BinaryOpKind {
45 Add, // "+"
46 Sub, // "-"
47 Mul, // "*"
48 Div, // "/"
49 Rem, // "%"
50};
51
52/// Translates DIL tokens to BinaryOpKind.
53BinaryOpKind GetBinaryOpKindFromToken(Token::Kind token_kind);
54
55/// The type casts allowed by DIL.
56enum class CastKind {
57 eArithmetic, ///< Casting to a scalar.
58 eEnumeration, ///< Casting from a scalar to an enumeration type
59 ePointer, ///< Casting to a pointer type.
60 eNone, ///< Invalid promotion type (results in error).
61};
62
63/// Forward declaration, for use in DIL AST nodes. Definition is at the very
64/// end of this file.
65class Visitor;
66
67/// The rest of the classes in this file, except for the Visitor class at the
68/// very end, define all the types of AST nodes used by the DIL parser and
69/// expression evaluator. The DIL parser parses the input string and creates
70/// the AST parse tree from the AST nodes. The resulting AST node tree gets
71/// passed to the DIL expression evaluator, which evaluates the DIL AST nodes
72/// and creates/returns a ValueObjectSP containing the result.
73
74/// Base class for AST nodes used by the Data Inspection Language (DIL) parser.
75/// All of the specialized types of AST nodes inherit from this (virtual) base
76/// class.
77class ASTNode {
78public:
79 ASTNode(uint32_t location, NodeKind kind)
80 : m_location(location), m_kind(kind) {}
81 virtual ~ASTNode() = default;
82
83 virtual llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const = 0;
84
85 uint32_t GetLocation() const { return m_location; }
86 NodeKind GetKind() const { return m_kind; }
87
88private:
89 uint32_t m_location;
91};
92
93using ASTNodeUP = std::unique_ptr<ASTNode>;
94
95class ErrorNode : public ASTNode {
96public:
98 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
99
100 static bool classof(const ASTNode &node) {
101 return node.GetKind() == NodeKind::eErrorNode;
102 }
103};
104
105class IdentifierNode : public ASTNode {
106public:
107 IdentifierNode(uint32_t location, std::string name)
108 : ASTNode(location, NodeKind::eIdentifierNode), m_name(std::move(name)) {}
109
110 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
111
112 std::string GetName() const { return m_name; }
113
114 static bool classof(const ASTNode &node) {
115 return node.GetKind() == NodeKind::eIdentifierNode;
116 }
117
118private:
119 std::string m_name;
120};
121
122class MemberOfNode : public ASTNode {
123public:
124 MemberOfNode(uint32_t location, ASTNodeUP base, bool is_arrow,
125 std::string name)
126 : ASTNode(location, NodeKind::eMemberOfNode), m_base(std::move(base)),
127 m_is_arrow(is_arrow), m_field_name(std::move(name)) {}
128
129 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
130
131 ASTNode &GetBase() const { return *m_base; }
132 bool GetIsArrow() const { return m_is_arrow; }
133 llvm::StringRef GetFieldName() const { return llvm::StringRef(m_field_name); }
134
135 static bool classof(const ASTNode &node) {
136 return node.GetKind() == NodeKind::eMemberOfNode;
137 }
138
139private:
142 std::string m_field_name;
143};
144
145class UnaryOpNode : public ASTNode {
146public:
147 UnaryOpNode(uint32_t location, UnaryOpKind kind, ASTNodeUP operand)
148 : ASTNode(location, NodeKind::eUnaryOpNode), m_kind(kind),
149 m_operand(std::move(operand)) {}
150
151 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
152
153 UnaryOpKind GetKind() const { return m_kind; }
154 ASTNode &GetOperand() const { return *m_operand; }
155
156 static bool classof(const ASTNode &node) {
157 return node.GetKind() == NodeKind::eUnaryOpNode;
158 }
159
160private:
163};
164
165class BinaryOpNode : public ASTNode {
166public:
167 BinaryOpNode(uint32_t location, BinaryOpKind kind, ASTNodeUP lhs,
168 ASTNodeUP rhs)
169 : ASTNode(location, NodeKind::eBinaryOpNode), m_kind(kind),
170 m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {}
171
172 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
173
174 BinaryOpKind GetKind() const { return m_kind; }
175 ASTNode &GetLHS() const { return *m_lhs; }
176 ASTNode &GetRHS() const { return *m_rhs; }
177
178 static bool classof(const ASTNode *node) {
179 return node->GetKind() == NodeKind::eBinaryOpNode;
180 }
181
182private:
186};
187
189public:
190 ArraySubscriptNode(uint32_t location, ASTNodeUP base, ASTNodeUP index)
192 m_base(std::move(base)), m_index(std::move(index)) {}
193
194 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
195
196 ASTNode &GetBase() const { return *m_base; }
197 ASTNode &GetIndex() const { return *m_index; }
198
199 static bool classof(const ASTNode &node) {
200 return node.GetKind() == NodeKind::eArraySubscriptNode;
201 }
202
203private:
206};
207
209public:
210 BitFieldExtractionNode(uint32_t location, ASTNodeUP base,
211 ASTNodeUP first_index, ASTNodeUP last_index)
212 : ASTNode(location, NodeKind::eBitExtractionNode),
213 m_base(std::move(base)), m_first_index(std::move(first_index)),
214 m_last_index(std::move(last_index)) {}
215
216 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
217
218 ASTNode &GetBase() const { return *m_base; }
219 ASTNode &GetFirstIndex() const { return *m_first_index; }
220 ASTNode &GetLastIndex() const { return *m_last_index; }
221
222 static bool classof(const ASTNode &node) {
223 return node.GetKind() == NodeKind::eBitExtractionNode;
224 }
225
226private:
230};
231
233
235public:
236 IntegerLiteralNode(uint32_t location, llvm::APInt value, uint32_t radix,
237 bool is_unsigned, IntegerTypeSuffix type)
239 m_value(std::move(value)), m_radix(radix), m_is_unsigned(is_unsigned),
240 m_type(type) {}
241
242 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
243
244 const llvm::APInt &GetValue() const { return m_value; }
245 uint32_t GetRadix() const { return m_radix; }
246 bool IsUnsigned() const { return m_is_unsigned; }
248
249 static bool classof(const ASTNode &node) {
250 return node.GetKind() == NodeKind::eIntegerLiteralNode;
251 }
252
253private:
254 llvm::APInt m_value;
255 uint32_t m_radix;
258};
259
260class FloatLiteralNode : public ASTNode {
261public:
262 FloatLiteralNode(uint32_t location, llvm::APFloat value)
263 : ASTNode(location, NodeKind::eFloatLiteralNode),
264 m_value(std::move(value)) {}
265
266 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
267
268 const llvm::APFloat &GetValue() const { return m_value; }
269
270 static bool classof(const ASTNode &node) {
271 return node.GetKind() == NodeKind::eFloatLiteralNode;
272 }
273
274private:
275 llvm::APFloat m_value;
276};
277
279public:
280 BooleanLiteralNode(uint32_t location, bool value)
281 : ASTNode(location, NodeKind::eBooleanLiteralNode), m_value(value) {}
282
283 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
284
285 bool GetValue() const & { return m_value; }
286
287 static bool classof(const ASTNode &node) {
288 return node.GetKind() == NodeKind::eBooleanLiteralNode;
289 }
290
291private:
293};
294
295class CastNode : public ASTNode {
296public:
297 CastNode(uint32_t location, CompilerType type, ASTNodeUP operand,
298 CastKind kind)
299 : ASTNode(location, NodeKind::eCastNode), m_type(type),
300 m_operand(std::move(operand)), m_cast_kind(kind) {}
301
302 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
303
304 CompilerType GetType() const { return m_type; }
305 ASTNode &GetOperand() const { return *m_operand; }
306 CastKind GetCastKind() const { return m_cast_kind; }
307
308 static bool classof(const ASTNode &node) {
309 return node.GetKind() == NodeKind::eCastNode;
310 }
311
312private:
316};
317
318/// This class contains one Visit method for each specialized type of
319/// DIL AST node. The Visit methods are used to dispatch a DIL AST node to
320/// the correct function in the DIL expression evaluator for evaluating that
321/// type of AST node.
322class Visitor {
323public:
324 virtual ~Visitor() = default;
325 virtual llvm::Expected<lldb::ValueObjectSP>
326 Visit(const IdentifierNode &node) = 0;
327 virtual llvm::Expected<lldb::ValueObjectSP>
328 Visit(const MemberOfNode &node) = 0;
329 virtual llvm::Expected<lldb::ValueObjectSP>
330 Visit(const UnaryOpNode &node) = 0;
331 virtual llvm::Expected<lldb::ValueObjectSP>
332 Visit(const BinaryOpNode &node) = 0;
333 virtual llvm::Expected<lldb::ValueObjectSP>
334 Visit(const ArraySubscriptNode &node) = 0;
335 virtual llvm::Expected<lldb::ValueObjectSP>
337 virtual llvm::Expected<lldb::ValueObjectSP>
338 Visit(const IntegerLiteralNode &node) = 0;
339 virtual llvm::Expected<lldb::ValueObjectSP>
340 Visit(const FloatLiteralNode &node) = 0;
341 virtual llvm::Expected<lldb::ValueObjectSP>
342 Visit(const BooleanLiteralNode &node) = 0;
343 virtual llvm::Expected<lldb::ValueObjectSP> Visit(const CastNode &node) = 0;
344};
345
346} // namespace lldb_private::dil
347
348#endif // LLDB_VALUEOBJECT_DILAST_H
Generic representation of a type in a programming language.
uint32_t GetLocation() const
Definition DILAST.h:85
ASTNode(uint32_t location, NodeKind kind)
Definition DILAST.h:79
virtual ~ASTNode()=default
NodeKind GetKind() const
Definition DILAST.h:86
virtual llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const =0
const NodeKind m_kind
Definition DILAST.h:90
ArraySubscriptNode(uint32_t location, ASTNodeUP base, ASTNodeUP index)
Definition DILAST.h:190
static bool classof(const ASTNode &node)
Definition DILAST.h:199
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:53
ASTNode & GetLHS() const
Definition DILAST.h:175
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:48
BinaryOpKind GetKind() const
Definition DILAST.h:174
static bool classof(const ASTNode *node)
Definition DILAST.h:178
BinaryOpNode(uint32_t location, BinaryOpKind kind, ASTNodeUP lhs, ASTNodeUP rhs)
Definition DILAST.h:167
ASTNode & GetRHS() const
Definition DILAST.h:176
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:58
static bool classof(const ASTNode &node)
Definition DILAST.h:222
BitFieldExtractionNode(uint32_t location, ASTNodeUP base, ASTNodeUP first_index, ASTNodeUP last_index)
Definition DILAST.h:210
static bool classof(const ASTNode &node)
Definition DILAST.h:287
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:72
BooleanLiteralNode(uint32_t location, bool value)
Definition DILAST.h:280
ASTNode & GetOperand() const
Definition DILAST.h:305
CompilerType GetType() const
Definition DILAST.h:304
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:76
static bool classof(const ASTNode &node)
Definition DILAST.h:308
CastKind GetCastKind() const
Definition DILAST.h:306
CastNode(uint32_t location, CompilerType type, ASTNodeUP operand, CastKind kind)
Definition DILAST.h:297
static bool classof(const ASTNode &node)
Definition DILAST.h:100
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:32
static bool classof(const ASTNode &node)
Definition DILAST.h:270
const llvm::APFloat & GetValue() const
Definition DILAST.h:268
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:67
FloatLiteralNode(uint32_t location, llvm::APFloat value)
Definition DILAST.h:262
IdentifierNode(uint32_t location, std::string name)
Definition DILAST.h:107
std::string GetName() const
Definition DILAST.h:112
static bool classof(const ASTNode &node)
Definition DILAST.h:114
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:36
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:63
IntegerLiteralNode(uint32_t location, llvm::APInt value, uint32_t radix, bool is_unsigned, IntegerTypeSuffix type)
Definition DILAST.h:236
IntegerTypeSuffix GetTypeSuffix() const
Definition DILAST.h:247
static bool classof(const ASTNode &node)
Definition DILAST.h:249
const llvm::APInt & GetValue() const
Definition DILAST.h:244
MemberOfNode(uint32_t location, ASTNodeUP base, bool is_arrow, std::string name)
Definition DILAST.h:124
static bool classof(const ASTNode &node)
Definition DILAST.h:135
llvm::StringRef GetFieldName() const
Definition DILAST.h:133
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:40
ASTNode & GetBase() const
Definition DILAST.h:131
UnaryOpNode(uint32_t location, UnaryOpKind kind, ASTNodeUP operand)
Definition DILAST.h:147
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:44
static bool classof(const ASTNode &node)
Definition DILAST.h:156
UnaryOpKind GetKind() const
Definition DILAST.h:153
ASTNode & GetOperand() const
Definition DILAST.h:154
This class contains one Visit method for each specialized type of DIL AST node.
Definition DILAST.h:322
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const MemberOfNode &node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const IntegerLiteralNode &node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const IdentifierNode &node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const CastNode &node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const FloatLiteralNode &node)=0
virtual ~Visitor()=default
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const BinaryOpNode &node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const BitFieldExtractionNode &node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const BooleanLiteralNode &node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const ArraySubscriptNode &node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const UnaryOpNode &node)=0
CastKind
The type casts allowed by DIL.
Definition DILAST.h:56
@ eEnumeration
Casting from a scalar to an enumeration type.
Definition DILAST.h:58
@ ePointer
Casting to a pointer type.
Definition DILAST.h:59
@ eNone
Invalid promotion type (results in error).
Definition DILAST.h:60
@ eArithmetic
Casting to a scalar.
Definition DILAST.h:57
UnaryOpKind
The Unary operators recognized by DIL.
Definition DILAST.h:36
std::unique_ptr< ASTNode > ASTNodeUP
Definition DILAST.h:93
BinaryOpKind
The binary operators recognized by DIL.
Definition DILAST.h:44
NodeKind
The various types DIL AST nodes (used by the DIL parser).
Definition DILAST.h:21
BinaryOpKind GetBinaryOpKindFromToken(Token::Kind token_kind)
Translates DIL tokens to BinaryOpKind.
Definition DILAST.cpp:14