1 /* Definitions for Rust expressions
3 Copyright (C) 2020 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 extern struct value
*eval_op_rust_complement (struct type
*expect_type
,
26 struct expression
*exp
,
28 enum exp_opcode opcode
,
30 extern struct value
*eval_op_rust_array (struct type
*expect_type
,
31 struct expression
*exp
,
33 enum exp_opcode opcode
,
34 struct value
*ncopies
,
36 extern struct value
*eval_op_rust_ind (struct type
*expect_type
,
37 struct expression
*exp
,
39 enum exp_opcode opcode
,
41 extern struct value
*rust_subscript (struct type
*expect_type
,
42 struct expression
*exp
,
43 enum noside noside
, bool for_addr
,
44 struct value
*lhs
, struct value
*rhs
);
45 extern struct value
*rust_range (struct type
*expect_type
,
46 struct expression
*exp
,
47 enum noside noside
, enum range_flag kind
,
48 struct value
*low
, struct value
*high
);
49 extern struct value
*eval_op_rust_struct_anon (struct type
*expect_type
,
50 struct expression
*exp
,
54 extern struct value
*eval_op_rust_structop (struct type
*expect_type
,
55 struct expression
*exp
,
58 const char *field_name
);
63 using rust_unop_compl_operation
= unop_operation
<UNOP_COMPLEMENT
,
64 eval_op_rust_complement
>;
65 using rust_array_operation
= binop_operation
<OP_RUST_ARRAY
,
68 /* The Rust indirection operation. */
69 class rust_unop_ind_operation
70 : public unop_ind_operation
74 using unop_ind_operation::unop_ind_operation
;
76 value
*evaluate (struct type
*expect_type
,
77 struct expression
*exp
,
78 enum noside noside
) override
80 if (noside
!= EVAL_NORMAL
)
81 return unop_ind_operation::evaluate (expect_type
, exp
, noside
);
83 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
84 return eval_op_rust_ind (expect_type
, exp
, noside
, UNOP_IND
, arg1
);
88 /* Subscript operator for Rust. */
89 class rust_subscript_operation
90 : public tuple_holding_operation
<operation_up
, operation_up
>
94 using tuple_holding_operation::tuple_holding_operation
;
96 value
*evaluate (struct type
*expect_type
,
97 struct expression
*exp
,
98 enum noside noside
) override
100 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
101 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
102 return rust_subscript (expect_type
, exp
, noside
, false, arg1
, arg2
);
105 value
*slice (struct type
*expect_type
,
106 struct expression
*exp
,
109 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
110 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
111 return rust_subscript (expect_type
, exp
, noside
, true, arg1
, arg2
);
114 enum exp_opcode
opcode () const override
115 { return BINOP_SUBSCRIPT
; }
118 class rust_unop_addr_operation
119 : public tuple_holding_operation
<operation_up
>
123 using tuple_holding_operation::tuple_holding_operation
;
125 value
*evaluate (struct type
*expect_type
,
126 struct expression
*exp
,
127 enum noside noside
) override
129 operation
*oper
= std::get
<0> (m_storage
).get ();
130 rust_subscript_operation
*sub_op
131 = dynamic_cast<rust_subscript_operation
*> (oper
);
132 if (sub_op
!= nullptr)
133 return sub_op
->slice (expect_type
, exp
, noside
);
134 return oper
->evaluate_for_address (exp
, noside
);
137 enum exp_opcode
opcode () const override
138 { return UNOP_ADDR
; }
141 /* The Rust range operators. */
142 class rust_range_operation
143 : public tuple_holding_operation
<enum range_flag
, operation_up
, operation_up
>
147 using tuple_holding_operation::tuple_holding_operation
;
149 value
*evaluate (struct type
*expect_type
,
150 struct expression
*exp
,
151 enum noside noside
) override
153 auto kind
= std::get
<0> (m_storage
);
154 value
*low
= nullptr;
155 if (std::get
<1> (m_storage
) != nullptr)
156 low
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
157 value
*high
= nullptr;
158 if (std::get
<2> (m_storage
) != nullptr)
159 high
= std::get
<2> (m_storage
)->evaluate (nullptr, exp
, noside
);
160 return rust_range (expect_type
, exp
, noside
, kind
, low
, high
);
163 enum exp_opcode
opcode () const override
167 /* Tuple field reference (using an integer). */
168 class rust_struct_anon
169 : public tuple_holding_operation
<int, operation_up
>
173 using tuple_holding_operation::tuple_holding_operation
;
175 value
*evaluate (struct type
*expect_type
,
176 struct expression
*exp
,
177 enum noside noside
) override
179 value
*lhs
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
180 return eval_op_rust_struct_anon (expect_type
, exp
, noside
,
181 std::get
<0> (m_storage
), lhs
);
185 enum exp_opcode
opcode () const override
186 { return STRUCTOP_ANONYMOUS
; }
189 /* Structure (or union or enum) field reference. */
191 : public structop_base_operation
195 using structop_base_operation::structop_base_operation
;
197 value
*evaluate (struct type
*expect_type
,
198 struct expression
*exp
,
199 enum noside noside
) override
201 value
*lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
202 return eval_op_rust_structop (expect_type
, exp
, noside
, lhs
,
203 std::get
<1> (m_storage
).c_str ());
206 value
*evaluate_funcall (struct type
*expect_type
,
207 struct expression
*exp
,
209 const std::vector
<operation_up
> &args
) override
;
211 enum exp_opcode
opcode () const override
212 { return STRUCTOP_STRUCT
; }
215 /* Rust aggregate initialization. */
216 class rust_aggregate_operation
217 : public tuple_holding_operation
<struct type
*, operation_up
,
218 std::vector
<std::pair
<std::string
,
223 using tuple_holding_operation::tuple_holding_operation
;
225 value
*evaluate (struct type
*expect_type
,
226 struct expression
*exp
,
227 enum noside noside
) override
;
229 enum exp_opcode
opcode () const override
230 { return OP_AGGREGATE
; }
233 } /* namespace expr */
235 #endif /* RUST_EXP_H */