c-exp.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Definitions for C 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 C_EXP_H
  15. #define C_EXP_H
  16. #include "expop.h"
  17. #include "objc-lang.h"
  18. extern struct value *eval_op_objc_selector (struct type *expect_type,
  19. struct expression *exp,
  20. enum noside noside,
  21. const char *sel);
  22. extern struct value *opencl_value_cast (struct type *type, struct value *arg);
  23. extern struct value *eval_opencl_assign (struct type *expect_type,
  24. struct expression *exp,
  25. enum noside noside,
  26. enum exp_opcode op,
  27. struct value *arg1,
  28. struct value *arg2);
  29. extern struct value *opencl_relop (struct type *expect_type,
  30. struct expression *exp,
  31. enum noside noside, enum exp_opcode op,
  32. struct value *arg1, struct value *arg2);
  33. extern struct value *opencl_logical_not (struct type *expect_type,
  34. struct expression *exp,
  35. enum noside noside,
  36. enum exp_opcode op,
  37. struct value *arg);
  38. namespace expr
  39. {
  40. class c_string_operation
  41. : public tuple_holding_operation<enum c_string_type_values,
  42. std::vector<std::string>>
  43. {
  44. public:
  45. using tuple_holding_operation::tuple_holding_operation;
  46. value *evaluate (struct type *expect_type,
  47. struct expression *exp,
  48. enum noside noside) override;
  49. enum exp_opcode opcode () const override
  50. { return OP_STRING; }
  51. };
  52. class objc_nsstring_operation
  53. : public tuple_holding_operation<std::string>
  54. {
  55. public:
  56. using tuple_holding_operation::tuple_holding_operation;
  57. value *evaluate (struct type *expect_type,
  58. struct expression *exp,
  59. enum noside noside) override
  60. {
  61. const std::string &str = std::get<0> (m_storage);
  62. return value_nsstring (exp->gdbarch, str.c_str (), str.size () + 1);
  63. }
  64. enum exp_opcode opcode () const override
  65. { return OP_OBJC_NSSTRING; }
  66. };
  67. class objc_selector_operation
  68. : public tuple_holding_operation<std::string>
  69. {
  70. public:
  71. using tuple_holding_operation::tuple_holding_operation;
  72. value *evaluate (struct type *expect_type,
  73. struct expression *exp,
  74. enum noside noside) override
  75. {
  76. return eval_op_objc_selector (expect_type, exp, noside,
  77. std::get<0> (m_storage).c_str ());
  78. }
  79. enum exp_opcode opcode () const override
  80. { return OP_OBJC_SELECTOR; }
  81. };
  82. /* An Objective C message call. */
  83. class objc_msgcall_operation
  84. : public tuple_holding_operation<CORE_ADDR, operation_up,
  85. std::vector<operation_up>>
  86. {
  87. public:
  88. using tuple_holding_operation::tuple_holding_operation;
  89. value *evaluate (struct type *expect_type,
  90. struct expression *exp,
  91. enum noside noside) override;
  92. enum exp_opcode opcode () const override
  93. { return OP_OBJC_MSGCALL; }
  94. };
  95. using opencl_cast_type_operation = cxx_cast_operation<UNOP_CAST_TYPE,
  96. opencl_value_cast>;
  97. /* Binary operations, as needed for OpenCL. */
  98. template<enum exp_opcode OP, binary_ftype FUNC,
  99. typename BASE = maybe_constant_operation<operation_up, operation_up>>
  100. class opencl_binop_operation
  101. : public BASE
  102. {
  103. public:
  104. using BASE::BASE;
  105. value *evaluate (struct type *expect_type,
  106. struct expression *exp,
  107. enum noside noside) override
  108. {
  109. value *lhs
  110. = std::get<0> (this->m_storage)->evaluate (nullptr, exp, noside);
  111. value *rhs
  112. = std::get<1> (this->m_storage)->evaluate (value_type (lhs), exp,
  113. noside);
  114. return FUNC (expect_type, exp, noside, OP, lhs, rhs);
  115. }
  116. enum exp_opcode opcode () const override
  117. { return OP; }
  118. };
  119. using opencl_assign_operation = opencl_binop_operation<BINOP_ASSIGN,
  120. eval_opencl_assign,
  121. assign_operation>;
  122. using opencl_equal_operation = opencl_binop_operation<BINOP_EQUAL,
  123. opencl_relop>;
  124. using opencl_notequal_operation = opencl_binop_operation<BINOP_NOTEQUAL,
  125. opencl_relop>;
  126. using opencl_less_operation = opencl_binop_operation<BINOP_LESS,
  127. opencl_relop>;
  128. using opencl_gtr_operation = opencl_binop_operation<BINOP_GTR,
  129. opencl_relop>;
  130. using opencl_geq_operation = opencl_binop_operation<BINOP_GEQ,
  131. opencl_relop>;
  132. using opencl_leq_operation = opencl_binop_operation<BINOP_LEQ,
  133. opencl_relop>;
  134. using opencl_not_operation = unop_operation<UNOP_LOGICAL_NOT,
  135. opencl_logical_not>;
  136. /* STRUCTOP_STRUCT implementation for OpenCL. */
  137. class opencl_structop_operation
  138. : public structop_base_operation
  139. {
  140. public:
  141. using structop_base_operation::structop_base_operation;
  142. value *evaluate (struct type *expect_type,
  143. struct expression *exp,
  144. enum noside noside) override;
  145. enum exp_opcode opcode () const override
  146. { return STRUCTOP_STRUCT; }
  147. };
  148. /* This handles the "&&" and "||" operations for OpenCL. */
  149. class opencl_logical_binop_operation
  150. : public tuple_holding_operation<enum exp_opcode,
  151. operation_up, operation_up>
  152. {
  153. public:
  154. using tuple_holding_operation::tuple_holding_operation;
  155. value *evaluate (struct type *expect_type,
  156. struct expression *exp,
  157. enum noside noside) override;
  158. enum exp_opcode opcode () const override
  159. { return std::get<0> (m_storage); }
  160. };
  161. /* The ?: ternary operator for OpenCL. */
  162. class opencl_ternop_cond_operation
  163. : public tuple_holding_operation<operation_up, operation_up, operation_up>
  164. {
  165. public:
  166. using tuple_holding_operation::tuple_holding_operation;
  167. value *evaluate (struct type *expect_type,
  168. struct expression *exp,
  169. enum noside noside) override;
  170. enum exp_opcode opcode () const override
  171. { return TERNOP_COND; }
  172. };
  173. }/* namespace expr */
  174. #endif /* C_EXP_H */