expression.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* Definitions for expressions stored in reversed prefix form, for GDB.
  2. Copyright (C) 1986-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. #if !defined (EXPRESSION_H)
  15. #define EXPRESSION_H 1
  16. #include "gdbtypes.h"
  17. /* While parsing expressions we need to track the innermost lexical block
  18. that we encounter. In some situations we need to track the innermost
  19. block just for symbols, and in other situations we want to track the
  20. innermost block for symbols and registers. These flags are used by the
  21. innermost block tracker to control which blocks we consider for the
  22. innermost block. These flags can be combined together as needed. */
  23. enum innermost_block_tracker_type
  24. {
  25. /* Track the innermost block for symbols within an expression. */
  26. INNERMOST_BLOCK_FOR_SYMBOLS = (1 << 0),
  27. /* Track the innermost block for registers within an expression. */
  28. INNERMOST_BLOCK_FOR_REGISTERS = (1 << 1)
  29. };
  30. DEF_ENUM_FLAGS_TYPE (enum innermost_block_tracker_type,
  31. innermost_block_tracker_types);
  32. enum exp_opcode : uint8_t
  33. {
  34. #define OP(name) name ,
  35. #include "std-operator.def"
  36. #undef OP
  37. };
  38. /* Values of NOSIDE argument to eval_subexp. */
  39. enum noside
  40. {
  41. EVAL_NORMAL,
  42. EVAL_AVOID_SIDE_EFFECTS /* Don't modify any variables or
  43. call any functions. The value
  44. returned will have the correct
  45. type, and will have an
  46. approximately correct lvalue
  47. type (inaccuracy: anything that is
  48. listed as being in a register in
  49. the function in which it was
  50. declared will be lval_register).
  51. Ideally this would not even read
  52. target memory, but currently it
  53. does in many situations. */
  54. };
  55. struct expression;
  56. struct agent_expr;
  57. struct axs_value;
  58. struct type;
  59. struct ui_file;
  60. namespace expr
  61. {
  62. class operation;
  63. typedef std::unique_ptr<operation> operation_up;
  64. /* Base class for an operation. An operation is a single component of
  65. an expression. */
  66. class operation
  67. {
  68. protected:
  69. operation () = default;
  70. DISABLE_COPY_AND_ASSIGN (operation);
  71. public:
  72. virtual ~operation () = default;
  73. /* Evaluate this operation. */
  74. virtual value *evaluate (struct type *expect_type,
  75. struct expression *exp,
  76. enum noside noside) = 0;
  77. /* Evaluate this operation in a context where C-like coercion is
  78. needed. */
  79. virtual value *evaluate_with_coercion (struct expression *exp,
  80. enum noside noside)
  81. {
  82. return evaluate (nullptr, exp, noside);
  83. }
  84. /* Evaluate this expression in the context of a cast to
  85. EXPECT_TYPE. */
  86. virtual value *evaluate_for_cast (struct type *expect_type,
  87. struct expression *exp,
  88. enum noside noside);
  89. /* Evaluate this expression in the context of a sizeof
  90. operation. */
  91. virtual value *evaluate_for_sizeof (struct expression *exp,
  92. enum noside noside);
  93. /* Evaluate this expression in the context of an address-of
  94. operation. Must return the address. */
  95. virtual value *evaluate_for_address (struct expression *exp,
  96. enum noside noside);
  97. /* Evaluate a function call, with this object as the callee.
  98. EXPECT_TYPE, EXP, and NOSIDE have the same meaning as in
  99. 'evaluate'. ARGS holds the operations that should be evaluated
  100. to get the arguments to the call. */
  101. virtual value *evaluate_funcall (struct type *expect_type,
  102. struct expression *exp,
  103. enum noside noside,
  104. const std::vector<operation_up> &args)
  105. {
  106. /* Defer to the helper overload. */
  107. return evaluate_funcall (expect_type, exp, noside, nullptr, args);
  108. }
  109. /* True if this is a constant expression. */
  110. virtual bool constant_p () const
  111. { return false; }
  112. /* Return true if this operation uses OBJFILE (and will become
  113. dangling when OBJFILE is unloaded), otherwise return false.
  114. OBJFILE must not be a separate debug info file. */
  115. virtual bool uses_objfile (struct objfile *objfile) const
  116. { return false; }
  117. /* Generate agent expression bytecodes for this operation. */
  118. void generate_ax (struct expression *exp, struct agent_expr *ax,
  119. struct axs_value *value,
  120. struct type *cast_type = nullptr);
  121. /* Return the opcode that is implemented by this operation. */
  122. virtual enum exp_opcode opcode () const = 0;
  123. /* Print this operation to STREAM. */
  124. virtual void dump (struct ui_file *stream, int depth) const = 0;
  125. /* Call to indicate that this is the outermost operation in the
  126. expression. This should almost never be overridden. */
  127. virtual void set_outermost () { }
  128. protected:
  129. /* A helper overload that wraps evaluate_subexp_do_call. */
  130. value *evaluate_funcall (struct type *expect_type,
  131. struct expression *exp,
  132. enum noside noside,
  133. const char *function_name,
  134. const std::vector<operation_up> &args);
  135. /* Called by generate_ax to do the work for this particular
  136. operation. */
  137. virtual void do_generate_ax (struct expression *exp,
  138. struct agent_expr *ax,
  139. struct axs_value *value,
  140. struct type *cast_type)
  141. {
  142. error (_("Cannot translate to agent expression"));
  143. }
  144. };
  145. /* A helper function for creating an operation_up, given a type. */
  146. template<typename T, typename... Arg>
  147. operation_up
  148. make_operation (Arg... args)
  149. {
  150. return operation_up (new T (std::forward<Arg> (args)...));
  151. }
  152. }
  153. struct expression
  154. {
  155. expression (const struct language_defn *lang, struct gdbarch *arch)
  156. : language_defn (lang),
  157. gdbarch (arch)
  158. {
  159. }
  160. DISABLE_COPY_AND_ASSIGN (expression);
  161. /* Return the opcode for the outermost sub-expression of this
  162. expression. */
  163. enum exp_opcode first_opcode () const
  164. {
  165. return op->opcode ();
  166. }
  167. /* Evaluate the expression. EXPECT_TYPE is the context type of the
  168. expression; normally this should be nullptr. NOSIDE controls how
  169. evaluation is performed. */
  170. struct value *evaluate (struct type *expect_type, enum noside noside);
  171. /* Language it was entered in. */
  172. const struct language_defn *language_defn;
  173. /* Architecture it was parsed in. */
  174. struct gdbarch *gdbarch;
  175. expr::operation_up op;
  176. };
  177. typedef std::unique_ptr<expression> expression_up;
  178. /* From parse.c */
  179. class innermost_block_tracker;
  180. extern expression_up parse_expression (const char *,
  181. innermost_block_tracker * = nullptr,
  182. bool void_context_p = false);
  183. extern expression_up parse_expression_with_language (const char *string,
  184. enum language lang);
  185. class completion_tracker;
  186. /* Base class for expression completion. An instance of this
  187. represents a completion request from the parser. */
  188. struct expr_completion_base
  189. {
  190. /* Perform this object's completion. EXP is the expression in which
  191. the completion occurs. TRACKER is the tracker to update with the
  192. results. Return true if completion was possible (even if no
  193. completions were found), false to fall back to ordinary
  194. expression completion (i.e., symbol names). */
  195. virtual bool complete (struct expression *exp,
  196. completion_tracker &tracker) = 0;
  197. virtual ~expr_completion_base () = default;
  198. };
  199. extern expression_up parse_expression_for_completion
  200. (const char *, std::unique_ptr<expr_completion_base> *completer);
  201. class innermost_block_tracker;
  202. extern expression_up parse_exp_1 (const char **, CORE_ADDR pc,
  203. const struct block *, int,
  204. innermost_block_tracker * = nullptr);
  205. /* From eval.c */
  206. /* Evaluate a function call. The function to be called is in CALLEE and
  207. the arguments passed to the function are in ARGVEC.
  208. FUNCTION_NAME is the name of the function, if known.
  209. DEFAULT_RETURN_TYPE is used as the function's return type if the return
  210. type is unknown. */
  211. extern struct value *evaluate_subexp_do_call (expression *exp,
  212. enum noside noside,
  213. value *callee,
  214. gdb::array_view<value *> argvec,
  215. const char *function_name,
  216. type *default_return_type);
  217. /* From expprint.c */
  218. extern const char *op_name (enum exp_opcode opcode);
  219. extern void dump_prefix_expression (struct expression *, struct ui_file *);
  220. /* In an OP_RANGE expression, either bound could be empty, indicating
  221. that its value is by default that of the corresponding bound of the
  222. array or string. Also, the upper end of the range can be exclusive
  223. or inclusive. So we have six sorts of subrange. This enumeration
  224. type is to identify this. */
  225. enum range_flag : unsigned
  226. {
  227. /* This is a standard range. Both the lower and upper bounds are
  228. defined, and the bounds are inclusive. */
  229. RANGE_STANDARD = 0,
  230. /* The low bound was not given. */
  231. RANGE_LOW_BOUND_DEFAULT = 1 << 0,
  232. /* The high bound was not given. */
  233. RANGE_HIGH_BOUND_DEFAULT = 1 << 1,
  234. /* The high bound of this range is exclusive. */
  235. RANGE_HIGH_BOUND_EXCLUSIVE = 1 << 2,
  236. /* The range has a stride. */
  237. RANGE_HAS_STRIDE = 1 << 3,
  238. };
  239. DEF_ENUM_FLAGS_TYPE (enum range_flag, range_flags);
  240. #endif /* !defined (EXPRESSION_H) */