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