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).
36
37/// The Unary operators recognized by DIL.
38enum class UnaryOpKind {
39 AddrOf, ///< "&"
40 Deref, ///< "*"
41 Minus, ///< "-"
42 Plus, ///< "+"
43 Not, ///< "~"
44 LNot, ///< "!"
45};
46
47/// The binary operators recognized by DIL.
48enum class BinaryOpKind {
49 Assign, ///< "="
50 Add, ///< "+"
51 Sub, ///< "-"
52 Mul, ///< "*"
53 Div, ///< "/"
54 Rem, ///< "%"
55 And, ///< "&"
56 Xor, ///< "^"
57 Or, ///< "|"
58 Shl, ///< "<<"
59 Shr, ///< ">>"
60 AddAssign, ///< "+="
61 SubAssign, ///< "-="
62 MulAssign, ///< "*="
63 DivAssign, ///< "/="
64 RemAssign, ///< "%="
65 AndAssign, ///< "&="
66 XorAssign, ///< "^="
67 OrAssign, ///< "|="
68 ShlAssign, ///< "<<="
69 ShrAssign, ///< ">>="
70 LAnd, ///< "&&"
71 LOr, ///< "||"
72 LT, ///< "<"
73 GT, ///< ">"
74 LE, ///< "<="
75 GE, ///< ">="
76 EQ, ///< "=="
77 NE, ///< "!="
78};
79
80/// Translates DIL tokens to BinaryOpKind.
81BinaryOpKind GetBinaryOpKindFromToken(Token::Kind token_kind);
82
83/// The type casts allowed by DIL.
84enum class CastKind {
85 eArithmetic, ///< Casting to a scalar.
86 eEnumeration, ///< Casting from a scalar to an enumeration type
87 ePointer, ///< Casting to a pointer type.
88 eNone, ///< Invalid promotion type (results in error).
89};
90
91/// Forward declaration, for use in DIL AST nodes. Definition is at the very
92/// end of this file.
93class Visitor;
94
95/// The rest of the classes in this file, except for the Visitor class at the
96/// very end, define all the types of AST nodes used by the DIL parser and
97/// expression evaluator. The DIL parser parses the input string and creates
98/// the AST parse tree from the AST nodes. The resulting AST node tree gets
99/// passed to the DIL expression evaluator, which evaluates the DIL AST nodes
100/// and creates/returns a ValueObjectSP containing the result.
101
102/// Base class for AST nodes used by the Data Inspection Language (DIL) parser.
103/// All of the specialized types of AST nodes inherit from this (virtual) base
104/// class.
105class ASTNode {
106public:
107 ASTNode(uint32_t location, NodeKind kind)
108 : m_location(location), m_kind(kind) {}
109 virtual ~ASTNode() = default;
110
111 virtual llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const = 0;
112
113 virtual bool IsConstLiteral() const { return false; }
114
115 uint32_t GetLocation() const { return m_location; }
116 NodeKind GetKind() const { return m_kind; }
117
118private:
119 uint32_t m_location;
121};
122
123using ASTNodeUP = std::unique_ptr<ASTNode>;
124
125class ErrorNode : public ASTNode {
126public:
128 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
129
130 static bool classof(const ASTNode &node) {
131 return node.GetKind() == NodeKind::eErrorNode;
132 }
133};
134
135class IdentifierNode : public ASTNode {
136public:
137 IdentifierNode(uint32_t location, std::string name)
138 : ASTNode(location, NodeKind::eIdentifierNode), m_name(std::move(name)) {}
139
140 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
141
142 std::string GetName() const { return m_name; }
143
144 static bool classof(const ASTNode &node) {
145 return node.GetKind() == NodeKind::eIdentifierNode;
146 }
147
148private:
149 std::string m_name;
150};
151
152class MemberOfNode : public ASTNode {
153public:
154 MemberOfNode(uint32_t location, ASTNodeUP base, bool is_arrow,
155 std::string name)
156 : ASTNode(location, NodeKind::eMemberOfNode), m_base(std::move(base)),
157 m_is_arrow(is_arrow), m_field_name(std::move(name)) {}
158
159 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
160
161 ASTNode &GetBase() const { return *m_base; }
162 bool GetIsArrow() const { return m_is_arrow; }
163 llvm::StringRef GetFieldName() const { return llvm::StringRef(m_field_name); }
164
165 static bool classof(const ASTNode &node) {
166 return node.GetKind() == NodeKind::eMemberOfNode;
167 }
168
169private:
172 std::string m_field_name;
173};
174
175class UnaryOpNode : public ASTNode {
176public:
177 UnaryOpNode(uint32_t location, UnaryOpKind kind, ASTNodeUP operand)
178 : ASTNode(location, NodeKind::eUnaryOpNode), m_kind(kind),
179 m_operand(std::move(operand)) {}
180
181 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
182
183 UnaryOpKind GetKind() const { return m_kind; }
184 ASTNode &GetOperand() const { return *m_operand; }
185
186 static bool classof(const ASTNode &node) {
187 return node.GetKind() == NodeKind::eUnaryOpNode;
188 }
189
190private:
193};
194
195class BinaryOpNode : public ASTNode {
196public:
197 BinaryOpNode(uint32_t location, BinaryOpKind kind, ASTNodeUP lhs,
198 ASTNodeUP rhs)
199 : ASTNode(location, NodeKind::eBinaryOpNode), m_kind(kind),
200 m_lhs(std::move(lhs)), m_rhs(std::move(rhs)) {}
201
202 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
203
204 BinaryOpKind GetKind() const { return m_kind; }
205 ASTNode &GetLHS() const { return *m_lhs; }
206 ASTNode &GetRHS() const { return *m_rhs; }
207
208 static bool classof(const ASTNode *node) {
209 return node->GetKind() == NodeKind::eBinaryOpNode;
210 }
211
212private:
216};
217
219public:
220 ArraySubscriptNode(uint32_t location, ASTNodeUP base, ASTNodeUP index)
222 m_base(std::move(base)), m_index(std::move(index)) {}
223
224 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
225
226 ASTNode &GetBase() const { return *m_base; }
227 ASTNode &GetIndex() const { return *m_index; }
228
229 static bool classof(const ASTNode &node) {
230 return node.GetKind() == NodeKind::eArraySubscriptNode;
231 }
232
233private:
236};
237
239public:
240 BitFieldExtractionNode(uint32_t location, ASTNodeUP base,
241 ASTNodeUP first_index, ASTNodeUP last_index)
242 : ASTNode(location, NodeKind::eBitExtractionNode),
243 m_base(std::move(base)), m_first_index(std::move(first_index)),
244 m_last_index(std::move(last_index)) {}
245
246 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
247
248 ASTNode &GetBase() const { return *m_base; }
249 ASTNode &GetFirstIndex() const { return *m_first_index; }
250 ASTNode &GetLastIndex() const { return *m_last_index; }
251
252 static bool classof(const ASTNode &node) {
253 return node.GetKind() == NodeKind::eBitExtractionNode;
254 }
255
256private:
260};
261
263
265public:
266 IntegerLiteralNode(uint32_t location, llvm::APInt value, uint32_t radix,
267 bool is_unsigned, IntegerTypeSuffix type)
269 m_value(std::move(value)), m_radix(radix), m_is_unsigned(is_unsigned),
270 m_type(type) {}
271
272 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
273
274 bool IsConstLiteral() const override { return true; }
275 const llvm::APInt &GetValue() const { return m_value; }
276 uint32_t GetRadix() const { return m_radix; }
277 bool IsUnsigned() const { return m_is_unsigned; }
279
280 static bool classof(const ASTNode &node) {
281 return node.GetKind() == NodeKind::eIntegerLiteralNode;
282 }
283
284private:
285 llvm::APInt m_value;
286 uint32_t m_radix;
289};
290
291class FloatLiteralNode : public ASTNode {
292public:
293 FloatLiteralNode(uint32_t location, llvm::APFloat value)
294 : ASTNode(location, NodeKind::eFloatLiteralNode),
295 m_value(std::move(value)) {}
296
297 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
298
299 bool IsConstLiteral() const override { return true; }
300 const llvm::APFloat &GetValue() const { return m_value; }
301
302 static bool classof(const ASTNode &node) {
303 return node.GetKind() == NodeKind::eFloatLiteralNode;
304 }
305
306private:
307 llvm::APFloat m_value;
308};
309
311public:
312 BooleanLiteralNode(uint32_t location, bool value)
313 : ASTNode(location, NodeKind::eBooleanLiteralNode), m_value(value) {}
314
315 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
316
317 bool IsConstLiteral() const override { return true; }
318 bool GetValue() const & { return m_value; }
319
320 static bool classof(const ASTNode &node) {
321 return node.GetKind() == NodeKind::eBooleanLiteralNode;
322 }
323
324private:
326};
327
328class CastNode : public ASTNode {
329public:
330 CastNode(uint32_t location, CompilerType type, ASTNodeUP operand,
331 CastKind kind)
332 : ASTNode(location, NodeKind::eCastNode), m_type(type),
333 m_operand(std::move(operand)), m_cast_kind(kind) {}
334
335 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
336
337 CompilerType GetType() const { return m_type; }
338 ASTNode &GetOperand() const { return *m_operand; }
339 CastKind GetCastKind() const { return m_cast_kind; }
340
341 static bool classof(const ASTNode &node) {
342 return node.GetKind() == NodeKind::eCastNode;
343 }
344
345private:
349};
350
351class ConditionalNode : public ASTNode {
352public:
353 ConditionalNode(uint32_t location, ASTNodeUP condition, ASTNodeUP true_op,
354 ASTNodeUP false_op)
355 : ASTNode(location, NodeKind::eConditionalNode),
356 m_condition(std::move(condition)), m_true_op(std::move(true_op)),
357 m_false_op(std::move(false_op)) {}
358
359 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
360
361 ASTNode &GetCondition() const { return *m_condition; }
362 ASTNode &GetTrueOperand() const { return *m_true_op; }
363 ASTNode &GetFalseOperand() const { return *m_false_op; }
364
365 static bool classof(const ASTNode &node) {
366 return node.GetKind() == NodeKind::eConditionalNode;
367 }
368
369private:
373};
374
375class SizeOfNode : public ASTNode {
376public:
377 SizeOfNode(uint32_t location, ASTNodeUP node)
378 : ASTNode(location, NodeKind::eSizeOfNode), m_node_arg(std::move(node)) {}
379
380 SizeOfNode(uint32_t location, CompilerType type)
381 : ASTNode(location, NodeKind::eSizeOfNode), m_type_arg(type) {}
382
383 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
384
385 ASTNode &GetNodeArg() const { return *m_node_arg; }
387
388 static bool classof(const ASTNode &node) {
389 return node.GetKind() == NodeKind::eSizeOfNode;
390 }
391
392private:
393 std::string m_name;
396};
397
398/// This class contains one Visit method for each specialized type of
399/// DIL AST node. The Visit methods are used to dispatch a DIL AST node to
400/// the correct function in the DIL expression evaluator for evaluating that
401/// type of AST node.
402class Visitor {
403public:
404 virtual ~Visitor() = default;
405 virtual llvm::Expected<lldb::ValueObjectSP>
406 Visit(const IdentifierNode &node) = 0;
407 virtual llvm::Expected<lldb::ValueObjectSP>
408 Visit(const MemberOfNode &node) = 0;
409 virtual llvm::Expected<lldb::ValueObjectSP>
410 Visit(const UnaryOpNode &node) = 0;
411 virtual llvm::Expected<lldb::ValueObjectSP>
412 Visit(const BinaryOpNode &node) = 0;
413 virtual llvm::Expected<lldb::ValueObjectSP>
414 Visit(const ArraySubscriptNode &node) = 0;
415 virtual llvm::Expected<lldb::ValueObjectSP>
417 virtual llvm::Expected<lldb::ValueObjectSP>
418 Visit(const IntegerLiteralNode &node) = 0;
419 virtual llvm::Expected<lldb::ValueObjectSP>
420 Visit(const FloatLiteralNode &node) = 0;
421 virtual llvm::Expected<lldb::ValueObjectSP>
422 Visit(const BooleanLiteralNode &node) = 0;
423 virtual llvm::Expected<lldb::ValueObjectSP> Visit(const CastNode &node) = 0;
424 virtual llvm::Expected<lldb::ValueObjectSP>
425 Visit(const ConditionalNode &node) = 0;
426 virtual llvm::Expected<lldb::ValueObjectSP> Visit(const SizeOfNode &node) = 0;
427};
428
429} // namespace lldb_private::dil
430
431#endif // LLDB_VALUEOBJECT_DILAST_H
Generic representation of a type in a programming language.
uint32_t GetLocation() const
Definition DILAST.h:115
ASTNode(uint32_t location, NodeKind kind)
Definition DILAST.h:107
virtual ~ASTNode()=default
NodeKind GetKind() const
Definition DILAST.h:116
virtual bool IsConstLiteral() const
Definition DILAST.h:113
virtual llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const =0
const NodeKind m_kind
Definition DILAST.h:120
ArraySubscriptNode(uint32_t location, ASTNodeUP base, ASTNodeUP index)
Definition DILAST.h:220
static bool classof(const ASTNode &node)
Definition DILAST.h:229
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:101
ASTNode & GetLHS() const
Definition DILAST.h:205
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:96
BinaryOpKind GetKind() const
Definition DILAST.h:204
static bool classof(const ASTNode *node)
Definition DILAST.h:208
BinaryOpNode(uint32_t location, BinaryOpKind kind, ASTNodeUP lhs, ASTNodeUP rhs)
Definition DILAST.h:197
ASTNode & GetRHS() const
Definition DILAST.h:206
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:106
static bool classof(const ASTNode &node)
Definition DILAST.h:252
BitFieldExtractionNode(uint32_t location, ASTNodeUP base, ASTNodeUP first_index, ASTNodeUP last_index)
Definition DILAST.h:240
bool IsConstLiteral() const override
Definition DILAST.h:317
static bool classof(const ASTNode &node)
Definition DILAST.h:320
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:120
BooleanLiteralNode(uint32_t location, bool value)
Definition DILAST.h:312
ASTNode & GetOperand() const
Definition DILAST.h:338
CompilerType GetType() const
Definition DILAST.h:337
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:124
static bool classof(const ASTNode &node)
Definition DILAST.h:341
CastKind GetCastKind() const
Definition DILAST.h:339
CastNode(uint32_t location, CompilerType type, ASTNodeUP operand, CastKind kind)
Definition DILAST.h:330
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:128
ASTNode & GetFalseOperand() const
Definition DILAST.h:363
ASTNode & GetTrueOperand() const
Definition DILAST.h:362
static bool classof(const ASTNode &node)
Definition DILAST.h:365
ASTNode & GetCondition() const
Definition DILAST.h:361
ConditionalNode(uint32_t location, ASTNodeUP condition, ASTNodeUP true_op, ASTNodeUP false_op)
Definition DILAST.h:353
static bool classof(const ASTNode &node)
Definition DILAST.h:130
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:80
static bool classof(const ASTNode &node)
Definition DILAST.h:302
const llvm::APFloat & GetValue() const
Definition DILAST.h:300
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:115
FloatLiteralNode(uint32_t location, llvm::APFloat value)
Definition DILAST.h:293
bool IsConstLiteral() const override
Definition DILAST.h:299
IdentifierNode(uint32_t location, std::string name)
Definition DILAST.h:137
std::string GetName() const
Definition DILAST.h:142
static bool classof(const ASTNode &node)
Definition DILAST.h:144
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:84
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:111
IntegerLiteralNode(uint32_t location, llvm::APInt value, uint32_t radix, bool is_unsigned, IntegerTypeSuffix type)
Definition DILAST.h:266
bool IsConstLiteral() const override
Definition DILAST.h:274
IntegerTypeSuffix GetTypeSuffix() const
Definition DILAST.h:278
static bool classof(const ASTNode &node)
Definition DILAST.h:280
const llvm::APInt & GetValue() const
Definition DILAST.h:275
MemberOfNode(uint32_t location, ASTNodeUP base, bool is_arrow, std::string name)
Definition DILAST.h:154
static bool classof(const ASTNode &node)
Definition DILAST.h:165
llvm::StringRef GetFieldName() const
Definition DILAST.h:163
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:88
ASTNode & GetBase() const
Definition DILAST.h:161
static bool classof(const ASTNode &node)
Definition DILAST.h:388
ASTNode & GetNodeArg() const
Definition DILAST.h:385
SizeOfNode(uint32_t location, CompilerType type)
Definition DILAST.h:380
SizeOfNode(uint32_t location, ASTNodeUP node)
Definition DILAST.h:377
CompilerType GetTypeArg() const
Definition DILAST.h:386
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:132
UnaryOpNode(uint32_t location, UnaryOpKind kind, ASTNodeUP operand)
Definition DILAST.h:177
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:92
static bool classof(const ASTNode &node)
Definition DILAST.h:186
UnaryOpKind GetKind() const
Definition DILAST.h:183
ASTNode & GetOperand() const
Definition DILAST.h:184
This class contains one Visit method for each specialized type of DIL AST node.
Definition DILAST.h:402
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const MemberOfNode &node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const SizeOfNode &node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const IntegerLiteralNode &node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const ConditionalNode &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:84
@ eEnumeration
Casting from a scalar to an enumeration type.
Definition DILAST.h:86
@ ePointer
Casting to a pointer type.
Definition DILAST.h:87
@ eNone
Invalid promotion type (results in error).
Definition DILAST.h:88
@ eArithmetic
Casting to a scalar.
Definition DILAST.h:85
UnaryOpKind
The Unary operators recognized by DIL.
Definition DILAST.h:38
std::unique_ptr< ASTNode > ASTNodeUP
Definition DILAST.h:123
BinaryOpKind
The binary operators recognized by DIL.
Definition DILAST.h:48
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