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
13#include "llvm/Support/Error.h"
14#include <cstdint>
15#include <string>
16
18
19/// The various types DIL AST nodes (used by the DIL parser).
32
33/// The Unary operators recognized by DIL.
34enum class UnaryOpKind {
35 AddrOf, // "&"
36 Deref, // "*"
37 Minus, // "-"
38 Plus, // "+"
39};
40
41/// The type casts allowed by DIL.
42enum class CastKind {
43 eEnumeration, ///< Casting from a scalar to an enumeration type
44 eNullptr, ///< Casting to a nullptr type
45 eReference, ///< Casting to a reference type
46 eNone, ///< Type promotion casting
47};
48
49/// Forward declaration, for use in DIL AST nodes. Definition is at the very
50/// end of this file.
51class Visitor;
52
53/// The rest of the classes in this file, except for the Visitor class at the
54/// very end, define all the types of AST nodes used by the DIL parser and
55/// expression evaluator. The DIL parser parses the input string and creates
56/// the AST parse tree from the AST nodes. The resulting AST node tree gets
57/// passed to the DIL expression evaluator, which evaluates the DIL AST nodes
58/// and creates/returns a ValueObjectSP containing the result.
59
60/// Base class for AST nodes used by the Data Inspection Language (DIL) parser.
61/// All of the specialized types of AST nodes inherit from this (virtual) base
62/// class.
63class ASTNode {
64public:
65 ASTNode(uint32_t location, NodeKind kind)
66 : m_location(location), m_kind(kind) {}
67 virtual ~ASTNode() = default;
68
69 virtual llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const = 0;
70
71 uint32_t GetLocation() const { return m_location; }
72 NodeKind GetKind() const { return m_kind; }
73
74private:
75 uint32_t m_location;
77};
78
79using ASTNodeUP = std::unique_ptr<ASTNode>;
80
81class ErrorNode : public ASTNode {
82public:
84 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
85
86 static bool classof(const ASTNode *node) {
87 return node->GetKind() == NodeKind::eErrorNode;
88 }
89};
90
91class IdentifierNode : public ASTNode {
92public:
93 IdentifierNode(uint32_t location, std::string name)
94 : ASTNode(location, NodeKind::eIdentifierNode), m_name(std::move(name)) {}
95
96 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
97
98 std::string GetName() const { return m_name; }
99
100 static bool classof(const ASTNode *node) {
101 return node->GetKind() == NodeKind::eIdentifierNode;
102 }
103
104private:
105 std::string m_name;
106};
107
108class MemberOfNode : public ASTNode {
109public:
110 MemberOfNode(uint32_t location, ASTNodeUP base, bool is_arrow,
111 std::string name)
112 : ASTNode(location, NodeKind::eMemberOfNode), m_base(std::move(base)),
113 m_is_arrow(is_arrow), m_field_name(std::move(name)) {}
114
115 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
116
117 ASTNode *GetBase() const { return m_base.get(); }
118 bool GetIsArrow() const { return m_is_arrow; }
119 llvm::StringRef GetFieldName() const { return llvm::StringRef(m_field_name); }
120
121 static bool classof(const ASTNode *node) {
122 return node->GetKind() == NodeKind::eMemberOfNode;
123 }
124
125private:
128 std::string m_field_name;
129};
130
131class UnaryOpNode : public ASTNode {
132public:
133 UnaryOpNode(uint32_t location, UnaryOpKind kind, ASTNodeUP operand)
134 : ASTNode(location, NodeKind::eUnaryOpNode), m_kind(kind),
135 m_operand(std::move(operand)) {}
136
137 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
138
139 UnaryOpKind GetKind() const { return m_kind; }
140 ASTNode *GetOperand() const { return m_operand.get(); }
141
142 static bool classof(const ASTNode *node) {
143 return node->GetKind() == NodeKind::eUnaryOpNode;
144 }
145
146private:
149};
150
152public:
153 ArraySubscriptNode(uint32_t location, ASTNodeUP base, int64_t index)
155 m_base(std::move(base)), m_index(index) {}
156
157 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
158
159 ASTNode *GetBase() const { return m_base.get(); }
160 int64_t GetIndex() const { return m_index; }
161
162 static bool classof(const ASTNode *node) {
163 return node->GetKind() == NodeKind::eArraySubscriptNode;
164 }
165
166private:
168 int64_t m_index;
169};
170
172public:
173 BitFieldExtractionNode(uint32_t location, ASTNodeUP base, int64_t first_index,
174 int64_t last_index)
175 : ASTNode(location, NodeKind::eBitExtractionNode),
176 m_base(std::move(base)), m_first_index(first_index),
177 m_last_index(last_index) {}
178
179 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
180
181 ASTNode *GetBase() const { return m_base.get(); }
182 int64_t GetFirstIndex() const { return m_first_index; }
183 int64_t GetLastIndex() const { return m_last_index; }
184
185 static bool classof(const ASTNode *node) {
186 return node->GetKind() == NodeKind::eBitExtractionNode;
187 }
188
189private:
193};
194
196
198public:
199 IntegerLiteralNode(uint32_t location, llvm::APInt value, uint32_t radix,
200 bool is_unsigned, IntegerTypeSuffix type)
202 m_value(std::move(value)), m_radix(radix), m_is_unsigned(is_unsigned),
203 m_type(type) {}
204
205 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
206
207 const llvm::APInt &GetValue() const { return m_value; }
208 uint32_t GetRadix() const { return m_radix; }
209 bool IsUnsigned() const { return m_is_unsigned; }
211
212 static bool classof(const ASTNode *node) {
213 return node->GetKind() == NodeKind::eIntegerLiteralNode;
214 }
215
216private:
217 llvm::APInt m_value;
218 uint32_t m_radix;
221};
222
223class FloatLiteralNode : public ASTNode {
224public:
225 FloatLiteralNode(uint32_t location, llvm::APFloat value)
226 : ASTNode(location, NodeKind::eFloatLiteralNode),
227 m_value(std::move(value)) {}
228
229 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
230
231 const llvm::APFloat &GetValue() const { return m_value; }
232
233 static bool classof(const ASTNode *node) {
234 return node->GetKind() == NodeKind::eFloatLiteralNode;
235 }
236
237private:
238 llvm::APFloat m_value;
239};
240
242public:
243 BooleanLiteralNode(uint32_t location, bool value)
244 : ASTNode(location, NodeKind::eBooleanLiteralNode), m_value(value) {}
245
246 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
247
248 bool GetValue() const & { return m_value; }
249
250 static bool classof(const ASTNode *node) {
251 return node->GetKind() == NodeKind::eBooleanLiteralNode;
252 }
253
254private:
256};
257
258class CastNode : public ASTNode {
259public:
260 CastNode(uint32_t location, CompilerType type, ASTNodeUP operand,
261 CastKind kind)
262 : ASTNode(location, NodeKind::eCastNode), m_type(type),
263 m_operand(std::move(operand)), m_cast_kind(kind) {}
264
265 llvm::Expected<lldb::ValueObjectSP> Accept(Visitor *v) const override;
266
267 CompilerType GetType() const { return m_type; }
268 ASTNode *GetOperand() const { return m_operand.get(); }
269 CastKind GetCastKind() const { return m_cast_kind; }
270
271 static bool classof(const ASTNode *node) {
272 return node->GetKind() == NodeKind::eCastNode;
273 }
274
275private:
279};
280
281/// This class contains one Visit method for each specialized type of
282/// DIL AST node. The Visit methods are used to dispatch a DIL AST node to
283/// the correct function in the DIL expression evaluator for evaluating that
284/// type of AST node.
285class Visitor {
286public:
287 virtual ~Visitor() = default;
288 virtual llvm::Expected<lldb::ValueObjectSP>
289 Visit(const IdentifierNode *node) = 0;
290 virtual llvm::Expected<lldb::ValueObjectSP>
291 Visit(const MemberOfNode *node) = 0;
292 virtual llvm::Expected<lldb::ValueObjectSP>
293 Visit(const UnaryOpNode *node) = 0;
294 virtual llvm::Expected<lldb::ValueObjectSP>
295 Visit(const ArraySubscriptNode *node) = 0;
296 virtual llvm::Expected<lldb::ValueObjectSP>
298 virtual llvm::Expected<lldb::ValueObjectSP>
299 Visit(const IntegerLiteralNode *node) = 0;
300 virtual llvm::Expected<lldb::ValueObjectSP>
301 Visit(const FloatLiteralNode *node) = 0;
302 virtual llvm::Expected<lldb::ValueObjectSP>
303 Visit(const BooleanLiteralNode *node) = 0;
304 virtual llvm::Expected<lldb::ValueObjectSP> Visit(const CastNode *node) = 0;
305};
306
307} // namespace lldb_private::dil
308
309#endif // LLDB_VALUEOBJECT_DILAST_H
Generic representation of a type in a programming language.
uint32_t GetLocation() const
Definition DILAST.h:71
ASTNode(uint32_t location, NodeKind kind)
Definition DILAST.h:65
virtual ~ASTNode()=default
NodeKind GetKind() const
Definition DILAST.h:72
virtual llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const =0
const NodeKind m_kind
Definition DILAST.h:76
ArraySubscriptNode(uint32_t location, ASTNodeUP base, int64_t index)
Definition DILAST.h:153
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:31
static bool classof(const ASTNode *node)
Definition DILAST.h:162
BitFieldExtractionNode(uint32_t location, ASTNodeUP base, int64_t first_index, int64_t last_index)
Definition DILAST.h:173
static bool classof(const ASTNode *node)
Definition DILAST.h:185
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:36
static bool classof(const ASTNode *node)
Definition DILAST.h:250
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:50
BooleanLiteralNode(uint32_t location, bool value)
Definition DILAST.h:243
CompilerType GetType() const
Definition DILAST.h:267
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:54
CastKind GetCastKind() const
Definition DILAST.h:269
ASTNode * GetOperand() const
Definition DILAST.h:268
static bool classof(const ASTNode *node)
Definition DILAST.h:271
CastNode(uint32_t location, CompilerType type, ASTNodeUP operand, CastKind kind)
Definition DILAST.h:260
static bool classof(const ASTNode *node)
Definition DILAST.h:86
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:14
const llvm::APFloat & GetValue() const
Definition DILAST.h:231
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:45
FloatLiteralNode(uint32_t location, llvm::APFloat value)
Definition DILAST.h:225
static bool classof(const ASTNode *node)
Definition DILAST.h:233
static bool classof(const ASTNode *node)
Definition DILAST.h:100
IdentifierNode(uint32_t location, std::string name)
Definition DILAST.h:93
std::string GetName() const
Definition DILAST.h:98
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:18
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:41
IntegerLiteralNode(uint32_t location, llvm::APInt value, uint32_t radix, bool is_unsigned, IntegerTypeSuffix type)
Definition DILAST.h:199
static bool classof(const ASTNode *node)
Definition DILAST.h:212
IntegerTypeSuffix GetTypeSuffix() const
Definition DILAST.h:210
const llvm::APInt & GetValue() const
Definition DILAST.h:207
MemberOfNode(uint32_t location, ASTNodeUP base, bool is_arrow, std::string name)
Definition DILAST.h:110
static bool classof(const ASTNode *node)
Definition DILAST.h:121
llvm::StringRef GetFieldName() const
Definition DILAST.h:119
ASTNode * GetBase() const
Definition DILAST.h:117
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:22
ASTNode * GetOperand() const
Definition DILAST.h:140
static bool classof(const ASTNode *node)
Definition DILAST.h:142
UnaryOpNode(uint32_t location, UnaryOpKind kind, ASTNodeUP operand)
Definition DILAST.h:133
llvm::Expected< lldb::ValueObjectSP > Accept(Visitor *v) const override
Definition DILAST.cpp:26
UnaryOpKind GetKind() const
Definition DILAST.h:139
This class contains one Visit method for each specialized type of DIL AST node.
Definition DILAST.h:285
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const IntegerLiteralNode *node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const UnaryOpNode *node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const FloatLiteralNode *node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const CastNode *node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const BooleanLiteralNode *node)=0
virtual ~Visitor()=default
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const IdentifierNode *node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const ArraySubscriptNode *node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const MemberOfNode *node)=0
virtual llvm::Expected< lldb::ValueObjectSP > Visit(const BitFieldExtractionNode *node)=0
CastKind
The type casts allowed by DIL.
Definition DILAST.h:42
@ eNullptr
Casting to a nullptr type.
Definition DILAST.h:44
@ eEnumeration
Casting from a scalar to an enumeration type.
Definition DILAST.h:43
@ eNone
Type promotion casting.
Definition DILAST.h:46
@ eReference
Casting to a reference type.
Definition DILAST.h:45
UnaryOpKind
The Unary operators recognized by DIL.
Definition DILAST.h:34
std::unique_ptr< ASTNode > ASTNodeUP
Definition DILAST.h:79
NodeKind
The various types DIL AST nodes (used by the DIL parser).
Definition DILAST.h:20