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