rust-exp.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* Definitions for Rust expressions
  2. Copyright (C) 2020-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef RUST_EXP_H
  15. #define RUST_EXP_H
  16. #include "expop.h"
  17. extern struct value *eval_op_rust_complement (struct type *expect_type,
  18. struct expression *exp,
  19. enum noside noside,
  20. enum exp_opcode opcode,
  21. struct value *value);
  22. extern struct value *eval_op_rust_array (struct type *expect_type,
  23. struct expression *exp,
  24. enum noside noside,
  25. enum exp_opcode opcode,
  26. struct value *ncopies,
  27. struct value *elt);
  28. extern struct value *rust_subscript (struct type *expect_type,
  29. struct expression *exp,
  30. enum noside noside, bool for_addr,
  31. struct value *lhs, struct value *rhs);
  32. extern struct value *rust_range (struct type *expect_type,
  33. struct expression *exp,
  34. enum noside noside, enum range_flag kind,
  35. struct value *low, struct value *high);
  36. namespace expr
  37. {
  38. using rust_unop_compl_operation = unop_operation<UNOP_COMPLEMENT,
  39. eval_op_rust_complement>;
  40. using rust_array_operation = binop_operation<OP_RUST_ARRAY,
  41. eval_op_rust_array>;
  42. /* The Rust indirection operation. */
  43. class rust_unop_ind_operation
  44. : public unop_ind_operation
  45. {
  46. public:
  47. using unop_ind_operation::unop_ind_operation;
  48. value *evaluate (struct type *expect_type,
  49. struct expression *exp,
  50. enum noside noside) override;
  51. };
  52. /* Subscript operator for Rust. */
  53. class rust_subscript_operation
  54. : public tuple_holding_operation<operation_up, operation_up>
  55. {
  56. public:
  57. using tuple_holding_operation::tuple_holding_operation;
  58. value *evaluate (struct type *expect_type,
  59. struct expression *exp,
  60. enum noside noside) override
  61. {
  62. value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
  63. value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
  64. return rust_subscript (expect_type, exp, noside, false, arg1, arg2);
  65. }
  66. value *slice (struct type *expect_type,
  67. struct expression *exp,
  68. enum noside noside)
  69. {
  70. value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
  71. value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
  72. return rust_subscript (expect_type, exp, noside, true, arg1, arg2);
  73. }
  74. enum exp_opcode opcode () const override
  75. { return BINOP_SUBSCRIPT; }
  76. };
  77. class rust_unop_addr_operation
  78. : public tuple_holding_operation<operation_up>
  79. {
  80. public:
  81. using tuple_holding_operation::tuple_holding_operation;
  82. value *evaluate (struct type *expect_type,
  83. struct expression *exp,
  84. enum noside noside) override
  85. {
  86. operation *oper = std::get<0> (m_storage).get ();
  87. rust_subscript_operation *sub_op
  88. = dynamic_cast<rust_subscript_operation *> (oper);
  89. if (sub_op != nullptr)
  90. return sub_op->slice (expect_type, exp, noside);
  91. return oper->evaluate_for_address (exp, noside);
  92. }
  93. enum exp_opcode opcode () const override
  94. { return UNOP_ADDR; }
  95. };
  96. /* The Rust range operators. */
  97. class rust_range_operation
  98. : public tuple_holding_operation<enum range_flag, operation_up, operation_up>
  99. {
  100. public:
  101. using tuple_holding_operation::tuple_holding_operation;
  102. value *evaluate (struct type *expect_type,
  103. struct expression *exp,
  104. enum noside noside) override
  105. {
  106. auto kind = std::get<0> (m_storage);
  107. value *low = nullptr;
  108. if (std::get<1> (m_storage) != nullptr)
  109. low = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
  110. value *high = nullptr;
  111. if (std::get<2> (m_storage) != nullptr)
  112. high = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
  113. return rust_range (expect_type, exp, noside, kind, low, high);
  114. }
  115. enum exp_opcode opcode () const override
  116. { return OP_RANGE; }
  117. };
  118. /* Tuple field reference (using an integer). */
  119. class rust_struct_anon
  120. : public tuple_holding_operation<int, operation_up>
  121. {
  122. public:
  123. using tuple_holding_operation::tuple_holding_operation;
  124. value *evaluate (struct type *expect_type,
  125. struct expression *exp,
  126. enum noside noside) override;
  127. enum exp_opcode opcode () const override
  128. { return STRUCTOP_ANONYMOUS; }
  129. };
  130. /* Structure (or union or enum) field reference. */
  131. class rust_structop
  132. : public structop_base_operation
  133. {
  134. public:
  135. using structop_base_operation::structop_base_operation;
  136. value *evaluate (struct type *expect_type,
  137. struct expression *exp,
  138. enum noside noside) override;
  139. value *evaluate_funcall (struct type *expect_type,
  140. struct expression *exp,
  141. enum noside noside,
  142. const std::vector<operation_up> &args) override;
  143. enum exp_opcode opcode () const override
  144. { return STRUCTOP_STRUCT; }
  145. };
  146. /* Rust aggregate initialization. */
  147. class rust_aggregate_operation
  148. : public tuple_holding_operation<struct type *, operation_up,
  149. std::vector<std::pair<std::string,
  150. operation_up>>>
  151. {
  152. public:
  153. using tuple_holding_operation::tuple_holding_operation;
  154. value *evaluate (struct type *expect_type,
  155. struct expression *exp,
  156. enum noside noside) override;
  157. enum exp_opcode opcode () const override
  158. { return OP_AGGREGATE; }
  159. };
  160. /* Rust parenthesized operation. This is needed to distinguish
  161. between 'obj.f()', which is a method call, and '(obj.f)()', which
  162. is a call of a function-valued field 'f'. */
  163. class rust_parenthesized_operation
  164. : public tuple_holding_operation<operation_up>
  165. {
  166. public:
  167. explicit rust_parenthesized_operation (operation_up op)
  168. : tuple_holding_operation (std::move (op))
  169. {
  170. }
  171. value *evaluate (struct type *expect_type,
  172. struct expression *exp,
  173. enum noside noside) override
  174. {
  175. return std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
  176. }
  177. enum exp_opcode opcode () const override
  178. {
  179. /* A lie but this isn't worth introducing a new opcode for. */
  180. return UNOP_PLUS;
  181. }
  182. };
  183. } /* namespace expr */
  184. #endif /* RUST_EXP_H */