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