parser-defs.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* Parser definitions for GDB.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. Modified from expread.y by the Department of Computer Science at the
  4. State University of New York at Buffalo.
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #if !defined (PARSER_DEFS_H)
  17. #define PARSER_DEFS_H 1
  18. #include "expression.h"
  19. #include "symtab.h"
  20. #include "expop.h"
  21. struct block;
  22. struct language_defn;
  23. struct internalvar;
  24. class innermost_block_tracker;
  25. extern bool parser_debug;
  26. /* A class that can be used to build a "struct expression". */
  27. struct expr_builder
  28. {
  29. /* Constructor. LANG is the language used to parse the expression.
  30. And GDBARCH is the gdbarch to use during parsing. */
  31. expr_builder (const struct language_defn *lang,
  32. struct gdbarch *gdbarch)
  33. : expout (new expression (lang, gdbarch))
  34. {
  35. }
  36. DISABLE_COPY_AND_ASSIGN (expr_builder);
  37. /* Resize the allocated expression to the correct size, and return
  38. it as an expression_up -- passing ownership to the caller. */
  39. ATTRIBUTE_UNUSED_RESULT expression_up release ()
  40. {
  41. return std::move (expout);
  42. }
  43. /* Return the gdbarch that was passed to the constructor. */
  44. struct gdbarch *gdbarch ()
  45. {
  46. return expout->gdbarch;
  47. }
  48. /* Return the language that was passed to the constructor. */
  49. const struct language_defn *language ()
  50. {
  51. return expout->language_defn;
  52. }
  53. /* Set the root operation of the expression that is currently being
  54. built. */
  55. void set_operation (expr::operation_up &&op)
  56. {
  57. expout->op = std::move (op);
  58. }
  59. /* The expression related to this parser state. */
  60. expression_up expout;
  61. };
  62. /* Complete an expression that references a field, like "x->y". */
  63. struct expr_complete_structop : public expr_completion_base
  64. {
  65. explicit expr_complete_structop (expr::structop_base_operation *op)
  66. : m_op (op)
  67. {
  68. }
  69. bool complete (struct expression *exp,
  70. completion_tracker &tracker) override
  71. {
  72. return m_op->complete (exp, tracker);
  73. }
  74. private:
  75. /* The last struct expression directly before a '.' or '->'. This
  76. is set when parsing and is only used when completing a field
  77. name. It is nullptr if no dereference operation was found. */
  78. expr::structop_base_operation *m_op = nullptr;
  79. };
  80. /* Complete a tag name in an expression. This is used for something
  81. like "enum abc<TAB>". */
  82. struct expr_complete_tag : public expr_completion_base
  83. {
  84. expr_complete_tag (enum type_code code,
  85. gdb::unique_xmalloc_ptr<char> name)
  86. : m_code (code),
  87. m_name (std::move (name))
  88. {
  89. /* Parsers should enforce this statically. */
  90. gdb_assert (code == TYPE_CODE_ENUM
  91. || code == TYPE_CODE_UNION
  92. || code == TYPE_CODE_STRUCT);
  93. }
  94. bool complete (struct expression *exp,
  95. completion_tracker &tracker) override;
  96. private:
  97. /* The kind of tag to complete. */
  98. enum type_code m_code;
  99. /* The token for tagged type name completion. */
  100. gdb::unique_xmalloc_ptr<char> m_name;
  101. };
  102. /* An instance of this type is instantiated during expression parsing,
  103. and passed to the appropriate parser. It holds both inputs to the
  104. parser, and result. */
  105. struct parser_state : public expr_builder
  106. {
  107. /* Constructor. LANG is the language used to parse the expression.
  108. And GDBARCH is the gdbarch to use during parsing. */
  109. parser_state (const struct language_defn *lang,
  110. struct gdbarch *gdbarch,
  111. const struct block *context_block,
  112. CORE_ADDR context_pc,
  113. int comma,
  114. const char *input,
  115. bool completion,
  116. innermost_block_tracker *tracker,
  117. bool void_p)
  118. : expr_builder (lang, gdbarch),
  119. expression_context_block (context_block),
  120. expression_context_pc (context_pc),
  121. comma_terminates (comma),
  122. lexptr (input),
  123. parse_completion (completion),
  124. block_tracker (tracker),
  125. void_context_p (void_p)
  126. {
  127. }
  128. DISABLE_COPY_AND_ASSIGN (parser_state);
  129. /* Begin counting arguments for a function call,
  130. saving the data about any containing call. */
  131. void start_arglist ()
  132. {
  133. m_funcall_chain.push_back (arglist_len);
  134. arglist_len = 0;
  135. }
  136. /* Return the number of arguments in a function call just terminated,
  137. and restore the data for the containing function call. */
  138. int end_arglist ()
  139. {
  140. int val = arglist_len;
  141. arglist_len = m_funcall_chain.back ();
  142. m_funcall_chain.pop_back ();
  143. return val;
  144. }
  145. /* Mark the given operation as the starting location of a structure
  146. expression. This is used when completing on field names. */
  147. void mark_struct_expression (expr::structop_base_operation *op);
  148. /* Indicate that the current parser invocation is completing a tag.
  149. TAG is the type code of the tag, and PTR and LENGTH represent the
  150. start of the tag name. */
  151. void mark_completion_tag (enum type_code tag, const char *ptr, int length);
  152. /* Mark for completion, using an arbitrary completer. */
  153. void mark_completion (std::unique_ptr<expr_completion_base> completer)
  154. {
  155. gdb_assert (m_completion_state == nullptr);
  156. m_completion_state = std::move (completer);
  157. }
  158. /* Push an operation on the stack. */
  159. void push (expr::operation_up &&op)
  160. {
  161. m_operations.push_back (std::move (op));
  162. }
  163. /* Create a new operation and push it on the stack. */
  164. template<typename T, typename... Arg>
  165. void push_new (Arg... args)
  166. {
  167. m_operations.emplace_back (new T (std::forward<Arg> (args)...));
  168. }
  169. /* Push a new C string operation. */
  170. void push_c_string (int, struct stoken_vector *vec);
  171. /* Push a symbol reference. If SYM is nullptr, look for a minimal
  172. symbol. */
  173. void push_symbol (const char *name, block_symbol sym);
  174. /* Push a reference to $mumble. This may result in a convenience
  175. variable, a history reference, or a register. */
  176. void push_dollar (struct stoken str);
  177. /* Pop an operation from the stack. */
  178. expr::operation_up pop ()
  179. {
  180. expr::operation_up result = std::move (m_operations.back ());
  181. m_operations.pop_back ();
  182. return result;
  183. }
  184. /* Pop N elements from the stack and return a vector. */
  185. std::vector<expr::operation_up> pop_vector (int n)
  186. {
  187. std::vector<expr::operation_up> result (n);
  188. for (int i = 1; i <= n; ++i)
  189. result[n - i] = pop ();
  190. return result;
  191. }
  192. /* A helper that pops an operation, wraps it in some other
  193. operation, and pushes it again. */
  194. template<typename T>
  195. void wrap ()
  196. {
  197. using namespace expr;
  198. operation_up v = ::expr::make_operation<T> (pop ());
  199. push (std::move (v));
  200. }
  201. /* A helper that pops two operations, wraps them in some other
  202. operation, and pushes the result. */
  203. template<typename T>
  204. void wrap2 ()
  205. {
  206. expr::operation_up rhs = pop ();
  207. expr::operation_up lhs = pop ();
  208. push (expr::make_operation<T> (std::move (lhs), std::move (rhs)));
  209. }
  210. /* If this is nonzero, this block is used as the lexical context for
  211. symbol names. */
  212. const struct block * const expression_context_block;
  213. /* If expression_context_block is non-zero, then this is the PC
  214. within the block that we want to evaluate expressions at. When
  215. debugging C or C++ code, we use this to find the exact line we're
  216. at, and then look up the macro definitions active at that
  217. point. */
  218. const CORE_ADDR expression_context_pc;
  219. /* Nonzero means stop parsing on first comma (if not within parentheses). */
  220. int comma_terminates;
  221. /* During parsing of a C expression, the pointer to the next character
  222. is in this variable. */
  223. const char *lexptr;
  224. /* After a token has been recognized, this variable points to it.
  225. Currently used only for error reporting. */
  226. const char *prev_lexptr = nullptr;
  227. /* Number of arguments seen so far in innermost function call. */
  228. int arglist_len = 0;
  229. /* True if parsing an expression to attempt completion. */
  230. bool parse_completion;
  231. /* Completion state is updated here. */
  232. std::unique_ptr<expr_completion_base> m_completion_state;
  233. /* The innermost block tracker. */
  234. innermost_block_tracker *block_tracker;
  235. /* True if no value is expected from the expression. */
  236. bool void_context_p;
  237. private:
  238. /* Data structure for saving values of arglist_len for function calls whose
  239. arguments contain other function calls. */
  240. std::vector<int> m_funcall_chain;
  241. /* Stack of operations. */
  242. std::vector<expr::operation_up> m_operations;
  243. };
  244. /* When parsing expressions we track the innermost block that was
  245. referenced. */
  246. class innermost_block_tracker
  247. {
  248. public:
  249. innermost_block_tracker (innermost_block_tracker_types types
  250. = INNERMOST_BLOCK_FOR_SYMBOLS)
  251. : m_types (types),
  252. m_innermost_block (NULL)
  253. { /* Nothing. */ }
  254. /* Update the stored innermost block if the new block B is more inner
  255. than the currently stored block, or if no block is stored yet. The
  256. type T tells us whether the block B was for a symbol or for a
  257. register. The stored innermost block is only updated if the type T is
  258. a type we are interested in, the types we are interested in are held
  259. in M_TYPES and set during RESET. */
  260. void update (const struct block *b, innermost_block_tracker_types t);
  261. /* Overload of main UPDATE method which extracts the block from BS. */
  262. void update (const struct block_symbol &bs)
  263. {
  264. update (bs.block, INNERMOST_BLOCK_FOR_SYMBOLS);
  265. }
  266. /* Return the stored innermost block. Can be nullptr if no symbols or
  267. registers were found during an expression parse, and so no innermost
  268. block was defined. */
  269. const struct block *block () const
  270. {
  271. return m_innermost_block;
  272. }
  273. private:
  274. /* The type of innermost block being looked for. */
  275. innermost_block_tracker_types m_types;
  276. /* The currently stored innermost block found while parsing an
  277. expression. */
  278. const struct block *m_innermost_block;
  279. };
  280. /* A string token, either a char-string or bit-string. Char-strings are
  281. used, for example, for the names of symbols. */
  282. struct stoken
  283. {
  284. /* Pointer to first byte of char-string or first bit of bit-string. */
  285. const char *ptr;
  286. /* Length of string in bytes for char-string or bits for bit-string. */
  287. int length;
  288. };
  289. struct typed_stoken
  290. {
  291. /* A language-specific type field. */
  292. int type;
  293. /* Pointer to first byte of char-string or first bit of bit-string. */
  294. char *ptr;
  295. /* Length of string in bytes for char-string or bits for bit-string. */
  296. int length;
  297. };
  298. struct stoken_vector
  299. {
  300. int len;
  301. struct typed_stoken *tokens;
  302. };
  303. struct ttype
  304. {
  305. struct stoken stoken;
  306. struct type *type;
  307. };
  308. struct symtoken
  309. {
  310. struct stoken stoken;
  311. struct block_symbol sym;
  312. int is_a_field_of_this;
  313. };
  314. struct objc_class_str
  315. {
  316. struct stoken stoken;
  317. struct type *type;
  318. int theclass;
  319. };
  320. extern const char *find_template_name_end (const char *);
  321. extern std::string copy_name (struct stoken);
  322. extern bool parse_float (const char *p, int len,
  323. const struct type *type, gdb_byte *data);
  324. /* Function used to avoid direct calls to fprintf
  325. in the code generated by the bison parser. */
  326. extern void parser_fprintf (FILE *, const char *, ...) ATTRIBUTE_PRINTF (2, 3);
  327. extern bool exp_uses_objfile (struct expression *exp, struct objfile *objfile);
  328. #endif /* PARSER_DEFS_H */