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