Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Parse expressions for GDB. |
c4a172b5 | 2 | |
3666a048 | 3 | Copyright (C) 1986-2021 Free Software Foundation, Inc. |
c4a172b5 | 4 | |
c906108c SS |
5 | Modified from expread.y by the Department of Computer Science at the |
6 | State University of New York at Buffalo, 1991. | |
7 | ||
c5aa993b | 8 | This file is part of GDB. |
c906108c | 9 | |
c5aa993b JM |
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 | |
a9762ec7 | 12 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 13 | (at your option) any later version. |
c906108c | 14 | |
c5aa993b JM |
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. | |
c906108c | 19 | |
c5aa993b | 20 | You should have received a copy of the GNU General Public License |
a9762ec7 | 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
22 | |
23 | /* Parse an expression from text in a string, | |
ae0c443d | 24 | and return the result as a struct expression pointer. |
c906108c SS |
25 | That structure contains arithmetic operations in reverse polish, |
26 | with constants represented by operations that are followed by special data. | |
27 | See expression.h for the details of the format. | |
28 | What is important here is that it can be built up sequentially | |
29 | during the process of parsing; the lower levels of the tree always | |
30 | come first in the result. */ | |
c5aa993b | 31 | |
c906108c | 32 | #include "defs.h" |
12c89474 | 33 | #include <ctype.h> |
e17c207e | 34 | #include "arch-utils.h" |
c906108c SS |
35 | #include "symtab.h" |
36 | #include "gdbtypes.h" | |
37 | #include "frame.h" | |
38 | #include "expression.h" | |
39 | #include "value.h" | |
40 | #include "command.h" | |
41 | #include "language.h" | |
42 | #include "parser-defs.h" | |
43 | #include "gdbcmd.h" | |
c5aa993b | 44 | #include "symfile.h" /* for overlay functions */ |
f57d151a | 45 | #include "inferior.h" |
f69fdf9b | 46 | #include "target-float.h" |
fe898f56 | 47 | #include "block.h" |
59f92a09 | 48 | #include "source.h" |
9e35dae4 | 49 | #include "objfiles.h" |
029a67e4 | 50 | #include "user-regs.h" |
325fac50 | 51 | #include <algorithm> |
268a13a5 | 52 | #include "gdbsupport/gdb_optional.h" |
413403fc | 53 | #include "c-exp.h" |
e2305d34 | 54 | |
ccce17b0 | 55 | static unsigned int expressiondebug = 0; |
920d2a44 AC |
56 | static void |
57 | show_expressiondebug (struct ui_file *file, int from_tty, | |
58 | struct cmd_list_element *c, const char *value) | |
59 | { | |
60 | fprintf_filtered (file, _("Expression debugging is %s.\n"), value); | |
61 | } | |
c906108c | 62 | |
92981e24 | 63 | |
491144b5 CB |
64 | /* True if an expression parser should set yydebug. */ |
65 | bool parser_debug; | |
92981e24 TT |
66 | |
67 | static void | |
68 | show_parserdebug (struct ui_file *file, int from_tty, | |
69 | struct cmd_list_element *c, const char *value) | |
70 | { | |
71 | fprintf_filtered (file, _("Parser debugging is %s.\n"), value); | |
72 | } | |
73 | ||
74 | ||
4d01a485 PA |
75 | static expression_up parse_exp_in_context (const char **, CORE_ADDR, |
76 | const struct block *, int, | |
1eaebe02 | 77 | bool, innermost_block_tracker *, |
2a612529 | 78 | expr_completion_state *); |
e85c3284 | 79 | |
aee1fcdf AB |
80 | /* Documented at it's declaration. */ |
81 | ||
82 | void | |
ae451627 AB |
83 | innermost_block_tracker::update (const struct block *b, |
84 | innermost_block_tracker_types t) | |
aee1fcdf | 85 | { |
ae451627 AB |
86 | if ((m_types & t) != 0 |
87 | && (m_innermost_block == NULL | |
88 | || contained_in (b, m_innermost_block))) | |
aee1fcdf AB |
89 | m_innermost_block = b; |
90 | } | |
91 | ||
c906108c | 92 | \f |
c906108c | 93 | |
74ea4be4 PA |
94 | /* Return the type of MSYMBOL, a minimal symbol of OBJFILE. If |
95 | ADDRESS_P is not NULL, set it to the MSYMBOL's resolved | |
96 | address. */ | |
c906108c | 97 | |
74ea4be4 PA |
98 | type * |
99 | find_minsym_type_and_address (minimal_symbol *msymbol, | |
100 | struct objfile *objfile, | |
101 | CORE_ADDR *address_p) | |
c906108c | 102 | { |
74ea4be4 | 103 | bound_minimal_symbol bound_msym = {msymbol, objfile}; |
ebbc3a7d | 104 | struct obj_section *section = msymbol->obj_section (objfile); |
712f90be | 105 | enum minimal_symbol_type type = MSYMBOL_TYPE (msymbol); |
bccdca4a | 106 | |
fbd1b771 JK |
107 | bool is_tls = (section != NULL |
108 | && section->the_bfd_section->flags & SEC_THREAD_LOCAL); | |
109 | ||
bccdca4a UW |
110 | /* The minimal symbol might point to a function descriptor; |
111 | resolve it to the actual code address instead. */ | |
f50776aa PA |
112 | CORE_ADDR addr; |
113 | if (is_tls) | |
bccdca4a | 114 | { |
f50776aa PA |
115 | /* Addresses of TLS symbols are really offsets into a |
116 | per-objfile/per-thread storage block. */ | |
117 | addr = MSYMBOL_VALUE_RAW_ADDRESS (bound_msym.minsym); | |
118 | } | |
119 | else if (msymbol_is_function (objfile, msymbol, &addr)) | |
120 | { | |
121 | if (addr != BMSYMBOL_VALUE_ADDRESS (bound_msym)) | |
0875794a | 122 | { |
f50776aa PA |
123 | /* This means we resolved a function descriptor, and we now |
124 | have an address for a code/text symbol instead of a data | |
125 | symbol. */ | |
126 | if (MSYMBOL_TYPE (msymbol) == mst_data_gnu_ifunc) | |
127 | type = mst_text_gnu_ifunc; | |
128 | else | |
129 | type = mst_text; | |
130 | section = NULL; | |
0875794a | 131 | } |
bccdca4a | 132 | } |
f50776aa PA |
133 | else |
134 | addr = BMSYMBOL_VALUE_ADDRESS (bound_msym); | |
bccdca4a UW |
135 | |
136 | if (overlay_debugging) | |
714835d5 | 137 | addr = symbol_overlayed_address (addr, section); |
c906108c | 138 | |
fbd1b771 | 139 | if (is_tls) |
9e35dae4 | 140 | { |
74ea4be4 PA |
141 | /* Skip translation if caller does not need the address. */ |
142 | if (address_p != NULL) | |
143 | *address_p = target_translate_tls_address (objfile, addr); | |
144 | return objfile_type (objfile)->nodebug_tls_symbol; | |
9e35dae4 DJ |
145 | } |
146 | ||
74ea4be4 PA |
147 | if (address_p != NULL) |
148 | *address_p = addr; | |
149 | ||
bccdca4a | 150 | switch (type) |
c906108c SS |
151 | { |
152 | case mst_text: | |
153 | case mst_file_text: | |
154 | case mst_solib_trampoline: | |
74ea4be4 | 155 | return objfile_type (objfile)->nodebug_text_symbol; |
c906108c | 156 | |
0875794a | 157 | case mst_text_gnu_ifunc: |
74ea4be4 | 158 | return objfile_type (objfile)->nodebug_text_gnu_ifunc_symbol; |
0875794a | 159 | |
c906108c SS |
160 | case mst_data: |
161 | case mst_file_data: | |
162 | case mst_bss: | |
163 | case mst_file_bss: | |
74ea4be4 | 164 | return objfile_type (objfile)->nodebug_data_symbol; |
c906108c | 165 | |
0875794a | 166 | case mst_slot_got_plt: |
74ea4be4 | 167 | return objfile_type (objfile)->nodebug_got_plt_symbol; |
0875794a | 168 | |
c906108c | 169 | default: |
74ea4be4 | 170 | return objfile_type (objfile)->nodebug_unknown_symbol; |
c906108c | 171 | } |
74ea4be4 PA |
172 | } |
173 | ||
4933522d TT |
174 | /* See parser-defs.h. */ |
175 | ||
176 | void | |
177 | parser_state::mark_struct_expression (expr::structop_base_operation *op) | |
178 | { | |
179 | gdb_assert (parse_completion | |
180 | && (m_completion_state.expout_tag_completion_type | |
1eaebe02 | 181 | == TYPE_CODE_UNDEF)); |
4933522d TT |
182 | m_completion_state.expout_last_op = op; |
183 | } | |
184 | ||
2f68a895 TT |
185 | /* Indicate that the current parser invocation is completing a tag. |
186 | TAG is the type code of the tag, and PTR and LENGTH represent the | |
187 | start of the tag name. */ | |
188 | ||
189 | void | |
2a612529 TT |
190 | parser_state::mark_completion_tag (enum type_code tag, const char *ptr, |
191 | int length) | |
2f68a895 TT |
192 | { |
193 | gdb_assert (parse_completion | |
2a612529 TT |
194 | && (m_completion_state.expout_tag_completion_type |
195 | == TYPE_CODE_UNDEF) | |
196 | && m_completion_state.expout_completion_name == NULL | |
4933522d | 197 | && m_completion_state.expout_last_op == nullptr); |
2f68a895 TT |
198 | gdb_assert (tag == TYPE_CODE_UNION |
199 | || tag == TYPE_CODE_STRUCT | |
2f68a895 | 200 | || tag == TYPE_CODE_ENUM); |
2a612529 TT |
201 | m_completion_state.expout_tag_completion_type = tag; |
202 | m_completion_state.expout_completion_name.reset (xstrndup (ptr, length)); | |
2f68a895 TT |
203 | } |
204 | ||
8227d9e2 TT |
205 | /* See parser-defs.h. */ |
206 | ||
207 | void | |
208 | parser_state::push_c_string (int kind, struct stoken_vector *vec) | |
209 | { | |
210 | std::vector<std::string> data (vec->len); | |
211 | for (int i = 0; i < vec->len; ++i) | |
212 | data[i] = std::string (vec->tokens[i].ptr, vec->tokens[i].length); | |
213 | ||
214 | push_new<expr::c_string_operation> ((enum c_string_type_values) kind, | |
215 | std::move (data)); | |
216 | } | |
217 | ||
218 | /* See parser-defs.h. */ | |
219 | ||
220 | void | |
221 | parser_state::push_symbol (const char *name, block_symbol sym) | |
222 | { | |
223 | if (sym.symbol != nullptr) | |
224 | { | |
225 | if (symbol_read_needs_frame (sym.symbol)) | |
226 | block_tracker->update (sym); | |
9e5e03df | 227 | push_new<expr::var_value_operation> (sym); |
8227d9e2 TT |
228 | } |
229 | else | |
230 | { | |
231 | struct bound_minimal_symbol msymbol = lookup_bound_minimal_symbol (name); | |
232 | if (msymbol.minsym != NULL) | |
9c79936b | 233 | push_new<expr::var_msym_value_operation> (msymbol); |
8227d9e2 TT |
234 | else if (!have_full_symbols () && !have_partial_symbols ()) |
235 | error (_("No symbol table is loaded. Use the \"file\" command.")); | |
236 | else | |
237 | error (_("No symbol \"%s\" in current context."), name); | |
238 | } | |
239 | } | |
240 | ||
241 | /* See parser-defs.h. */ | |
242 | ||
243 | void | |
244 | parser_state::push_dollar (struct stoken str) | |
245 | { | |
246 | struct block_symbol sym; | |
247 | struct bound_minimal_symbol msym; | |
248 | struct internalvar *isym = NULL; | |
249 | std::string copy; | |
250 | ||
251 | /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1) | |
252 | and $$digits (equivalent to $<-digits> if you could type that). */ | |
253 | ||
254 | int negate = 0; | |
255 | int i = 1; | |
256 | /* Double dollar means negate the number and add -1 as well. | |
257 | Thus $$ alone means -1. */ | |
258 | if (str.length >= 2 && str.ptr[1] == '$') | |
259 | { | |
260 | negate = 1; | |
261 | i = 2; | |
262 | } | |
263 | if (i == str.length) | |
264 | { | |
265 | /* Just dollars (one or two). */ | |
266 | i = -negate; | |
267 | goto handle_last; | |
268 | } | |
269 | /* Is the rest of the token digits? */ | |
270 | for (; i < str.length; i++) | |
271 | if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9')) | |
272 | break; | |
273 | if (i == str.length) | |
274 | { | |
275 | i = atoi (str.ptr + 1 + negate); | |
276 | if (negate) | |
277 | i = -i; | |
278 | goto handle_last; | |
279 | } | |
280 | ||
281 | /* Handle tokens that refer to machine registers: | |
282 | $ followed by a register name. */ | |
283 | i = user_reg_map_name_to_regnum (gdbarch (), | |
284 | str.ptr + 1, str.length - 1); | |
285 | if (i >= 0) | |
286 | goto handle_register; | |
287 | ||
288 | /* Any names starting with $ are probably debugger internal variables. */ | |
289 | ||
290 | copy = copy_name (str); | |
291 | isym = lookup_only_internalvar (copy.c_str () + 1); | |
292 | if (isym) | |
293 | { | |
294 | push_new<expr::internalvar_operation> (isym); | |
295 | return; | |
296 | } | |
297 | ||
298 | /* On some systems, such as HP-UX and hppa-linux, certain system routines | |
299 | have names beginning with $ or $$. Check for those, first. */ | |
300 | ||
301 | sym = lookup_symbol (copy.c_str (), NULL, VAR_DOMAIN, NULL); | |
302 | if (sym.symbol) | |
303 | { | |
9e5e03df | 304 | push_new<expr::var_value_operation> (sym); |
8227d9e2 TT |
305 | return; |
306 | } | |
307 | msym = lookup_bound_minimal_symbol (copy.c_str ()); | |
308 | if (msym.minsym) | |
309 | { | |
9c79936b | 310 | push_new<expr::var_msym_value_operation> (msym); |
8227d9e2 TT |
311 | return; |
312 | } | |
313 | ||
314 | /* Any other names are assumed to be debugger internal variables. */ | |
315 | ||
316 | push_new<expr::internalvar_operation> | |
317 | (create_internalvar (copy.c_str () + 1)); | |
318 | return; | |
319 | handle_last: | |
320 | push_new<expr::last_operation> (i); | |
321 | return; | |
322 | handle_register: | |
323 | str.length--; | |
324 | str.ptr++; | |
325 | push_new<expr::register_operation> (copy_name (str)); | |
326 | block_tracker->update (expression_context_block, | |
327 | INNERMOST_BLOCK_FOR_REGISTERS); | |
328 | return; | |
329 | } | |
330 | ||
c906108c | 331 | \f |
c906108c | 332 | |
d7561cbb KS |
333 | const char * |
334 | find_template_name_end (const char *p) | |
c906108c SS |
335 | { |
336 | int depth = 1; | |
337 | int just_seen_right = 0; | |
338 | int just_seen_colon = 0; | |
339 | int just_seen_space = 0; | |
c5aa993b | 340 | |
c906108c SS |
341 | if (!p || (*p != '<')) |
342 | return 0; | |
343 | ||
344 | while (*++p) | |
345 | { | |
346 | switch (*p) | |
c5aa993b JM |
347 | { |
348 | case '\'': | |
349 | case '\"': | |
350 | case '{': | |
351 | case '}': | |
0df8b418 | 352 | /* In future, may want to allow these?? */ |
c5aa993b JM |
353 | return 0; |
354 | case '<': | |
355 | depth++; /* start nested template */ | |
356 | if (just_seen_colon || just_seen_right || just_seen_space) | |
357 | return 0; /* but not after : or :: or > or space */ | |
358 | break; | |
359 | case '>': | |
360 | if (just_seen_colon || just_seen_right) | |
361 | return 0; /* end a (nested?) template */ | |
362 | just_seen_right = 1; /* but not after : or :: */ | |
363 | if (--depth == 0) /* also disallow >>, insist on > > */ | |
364 | return ++p; /* if outermost ended, return */ | |
365 | break; | |
366 | case ':': | |
367 | if (just_seen_space || (just_seen_colon > 1)) | |
368 | return 0; /* nested class spec coming up */ | |
369 | just_seen_colon++; /* we allow :: but not :::: */ | |
370 | break; | |
371 | case ' ': | |
372 | break; | |
373 | default: | |
374 | if (!((*p >= 'a' && *p <= 'z') || /* allow token chars */ | |
375 | (*p >= 'A' && *p <= 'Z') || | |
376 | (*p >= '0' && *p <= '9') || | |
377 | (*p == '_') || (*p == ',') || /* commas for template args */ | |
378 | (*p == '&') || (*p == '*') || /* pointer and ref types */ | |
379 | (*p == '(') || (*p == ')') || /* function types */ | |
380 | (*p == '[') || (*p == ']'))) /* array types */ | |
381 | return 0; | |
382 | } | |
c906108c | 383 | if (*p != ' ') |
c5aa993b | 384 | just_seen_space = 0; |
c906108c | 385 | if (*p != ':') |
c5aa993b | 386 | just_seen_colon = 0; |
c906108c | 387 | if (*p != '>') |
c5aa993b | 388 | just_seen_right = 0; |
c906108c SS |
389 | } |
390 | return 0; | |
391 | } | |
c5aa993b | 392 | \f |
c906108c | 393 | |
1a4eeb98 | 394 | /* Return a null-terminated temporary copy of the name of a string token. |
c906108c | 395 | |
1a4eeb98 DE |
396 | Tokens that refer to names do so with explicit pointer and length, |
397 | so they can share the storage that lexptr is parsing. | |
398 | When it is necessary to pass a name to a function that expects | |
399 | a null-terminated string, the substring is copied out | |
61f4b350 | 400 | into a separate block of storage. */ |
1a4eeb98 | 401 | |
61f4b350 | 402 | std::string |
fba45db2 | 403 | copy_name (struct stoken token) |
c906108c | 404 | { |
61f4b350 | 405 | return std::string (token.ptr, token.length); |
c906108c SS |
406 | } |
407 | \f | |
55aa24fb | 408 | |
c906108c | 409 | /* Read an expression from the string *STRINGPTR points to, |
ae0c443d | 410 | parse it, and return a pointer to a struct expression that we malloc. |
c906108c SS |
411 | Use block BLOCK as the lexical context for variable names; |
412 | if BLOCK is zero, use the block of the selected stack frame. | |
413 | Meanwhile, advance *STRINGPTR to point after the expression, | |
414 | at the first nonwhite character that is not part of the expression | |
415 | (possibly a null character). | |
416 | ||
417 | If COMMA is nonzero, stop if a comma is reached. */ | |
418 | ||
4d01a485 | 419 | expression_up |
bbc13ae3 | 420 | parse_exp_1 (const char **stringptr, CORE_ADDR pc, const struct block *block, |
699bd4cf | 421 | int comma, innermost_block_tracker *tracker) |
6f937416 | 422 | { |
1eaebe02 | 423 | return parse_exp_in_context (stringptr, pc, block, comma, false, |
699bd4cf | 424 | tracker, nullptr); |
e85c3284 PH |
425 | } |
426 | ||
427 | /* As for parse_exp_1, except that if VOID_CONTEXT_P, then | |
1eaebe02 | 428 | no value is expected from the expression. */ |
e85c3284 | 429 | |
4d01a485 | 430 | static expression_up |
7ad417dd TT |
431 | parse_exp_in_context (const char **stringptr, CORE_ADDR pc, |
432 | const struct block *block, | |
1eaebe02 | 433 | int comma, bool void_context_p, |
699bd4cf | 434 | innermost_block_tracker *tracker, |
2a612529 | 435 | expr_completion_state *cstate) |
c906108c | 436 | { |
0cce5bd9 | 437 | const struct language_defn *lang = NULL; |
c906108c | 438 | |
5776fca3 | 439 | if (*stringptr == 0 || **stringptr == 0) |
e2e0b3e5 | 440 | error_no_arg (_("expression to compute")); |
c906108c | 441 | |
1e58a4a4 TT |
442 | const struct block *expression_context_block = block; |
443 | CORE_ADDR expression_context_pc = 0; | |
59f92a09 | 444 | |
699bd4cf TT |
445 | innermost_block_tracker local_tracker; |
446 | if (tracker == nullptr) | |
447 | tracker = &local_tracker; | |
448 | ||
d705c43c PA |
449 | /* If no context specified, try using the current frame, if any. */ |
450 | if (!expression_context_block) | |
451 | expression_context_block = get_selected_block (&expression_context_pc); | |
1bb9788d | 452 | else if (pc == 0) |
2b1ffcfd | 453 | expression_context_pc = BLOCK_ENTRY_PC (expression_context_block); |
1bb9788d TT |
454 | else |
455 | expression_context_pc = pc; | |
59f92a09 | 456 | |
d705c43c | 457 | /* Fall back to using the current source static context, if any. */ |
59f92a09 | 458 | |
d705c43c | 459 | if (!expression_context_block) |
59f92a09 FF |
460 | { |
461 | struct symtab_and_line cursal = get_current_source_symtab_and_line (); | |
462 | if (cursal.symtab) | |
d705c43c | 463 | expression_context_block |
439247b6 DE |
464 | = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (cursal.symtab), |
465 | STATIC_BLOCK); | |
d705c43c | 466 | if (expression_context_block) |
2b1ffcfd | 467 | expression_context_pc = BLOCK_ENTRY_PC (expression_context_block); |
84f0252a | 468 | } |
c906108c | 469 | |
0cce5bd9 JB |
470 | if (language_mode == language_mode_auto && block != NULL) |
471 | { | |
472 | /* Find the language associated to the given context block. | |
dda83cd7 SM |
473 | Default to the current language if it can not be determined. |
474 | ||
475 | Note that using the language corresponding to the current frame | |
476 | can sometimes give unexpected results. For instance, this | |
477 | routine is often called several times during the inferior | |
478 | startup phase to re-parse breakpoint expressions after | |
479 | a new shared library has been loaded. The language associated | |
480 | to the current frame at this moment is not relevant for | |
481 | the breakpoint. Using it would therefore be silly, so it seems | |
482 | better to rely on the current language rather than relying on | |
483 | the current frame language to parse the expression. That's why | |
484 | we do the following language detection only if the context block | |
485 | has been specifically provided. */ | |
0cce5bd9 JB |
486 | struct symbol *func = block_linkage_function (block); |
487 | ||
488 | if (func != NULL) | |
dda83cd7 | 489 | lang = language_def (func->language ()); |
0cce5bd9 | 490 | if (lang == NULL || lang->la_language == language_unknown) |
dda83cd7 | 491 | lang = current_language; |
0cce5bd9 JB |
492 | } |
493 | else | |
494 | lang = current_language; | |
495 | ||
5b12a61c JK |
496 | /* get_current_arch may reset CURRENT_LANGUAGE via select_frame. |
497 | While we need CURRENT_LANGUAGE to be set to LANG (for lookup_symbol | |
498 | and others called from *.y) ensure CURRENT_LANGUAGE gets restored | |
499 | to the value matching SELECTED_FRAME as set by get_current_arch. */ | |
410a0ff2 | 500 | |
1e58a4a4 | 501 | parser_state ps (lang, get_current_arch (), expression_context_block, |
2a612529 | 502 | expression_context_pc, comma, *stringptr, |
c5c41205 | 503 | cstate != nullptr, tracker, void_context_p); |
e3ad2841 TT |
504 | |
505 | scoped_restore_current_language lang_saver; | |
5b12a61c | 506 | set_language (lang->la_language); |
c906108c | 507 | |
a70b8144 | 508 | try |
65d12d83 | 509 | { |
87afa652 | 510 | lang->parser (&ps); |
65d12d83 | 511 | } |
230d2906 | 512 | catch (const gdb_exception &except) |
65d12d83 | 513 | { |
5e70ee09 TT |
514 | /* If parsing for completion, allow this to succeed; but if no |
515 | expression elements have been written, then there's nothing | |
516 | to do, so fail. */ | |
1eaebe02 | 517 | if (! ps.parse_completion || ps.expout->op == nullptr) |
eedc3f4f | 518 | throw; |
65d12d83 | 519 | } |
c906108c | 520 | |
e9d9f57e | 521 | expression_up result = ps.release (); |
1eaebe02 | 522 | result->op->set_outermost (); |
e85c3284 | 523 | |
c906108c | 524 | if (expressiondebug) |
e9d9f57e | 525 | dump_prefix_expression (result.get (), gdb_stdlog); |
c906108c | 526 | |
2a612529 TT |
527 | if (cstate != nullptr) |
528 | *cstate = std::move (ps.m_completion_state); | |
5776fca3 | 529 | *stringptr = ps.lexptr; |
e9d9f57e | 530 | return result; |
c906108c SS |
531 | } |
532 | ||
8fc48b79 TT |
533 | /* Parse STRING as an expression, and complain if this fails to use up |
534 | all of the contents of STRING. TRACKER, if non-null, will be | |
535 | updated by the parser. VOID_CONTEXT_P should be true to indicate | |
536 | that the expression may be expected to return a value with void | |
537 | type. Parsers are free to ignore this, or to use it to help with | |
538 | overload resolution decisions. */ | |
c906108c | 539 | |
4d01a485 | 540 | expression_up |
8fc48b79 TT |
541 | parse_expression (const char *string, innermost_block_tracker *tracker, |
542 | bool void_context_p) | |
c906108c | 543 | { |
8fc48b79 | 544 | expression_up exp = parse_exp_in_context (&string, 0, nullptr, 0, |
1eaebe02 | 545 | void_context_p, |
8fc48b79 | 546 | tracker, nullptr); |
c906108c | 547 | if (*string) |
8a3fe4f8 | 548 | error (_("Junk after end of expression.")); |
c906108c SS |
549 | return exp; |
550 | } | |
e85c3284 | 551 | |
429e1e81 JB |
552 | /* Same as parse_expression, but using the given language (LANG) |
553 | to parse the expression. */ | |
554 | ||
4d01a485 | 555 | expression_up |
429e1e81 JB |
556 | parse_expression_with_language (const char *string, enum language lang) |
557 | { | |
e3ad2841 | 558 | gdb::optional<scoped_restore_current_language> lang_saver; |
429e1e81 JB |
559 | if (current_language->la_language != lang) |
560 | { | |
e3ad2841 | 561 | lang_saver.emplace (); |
429e1e81 JB |
562 | set_language (lang); |
563 | } | |
564 | ||
e3ad2841 | 565 | return parse_expression (string); |
429e1e81 JB |
566 | } |
567 | ||
65d12d83 TT |
568 | /* Parse STRING as an expression. If parsing ends in the middle of a |
569 | field reference, return the type of the left-hand-side of the | |
570 | reference; furthermore, if the parsing ends in the field name, | |
c92817ce TT |
571 | return the field name in *NAME. If the parsing ends in the middle |
572 | of a field reference, but the reference is somehow invalid, throw | |
3eac2b65 | 573 | an exception. In all other cases, return NULL. */ |
65d12d83 TT |
574 | |
575 | struct type * | |
3eac2b65 TT |
576 | parse_expression_for_completion (const char *string, |
577 | gdb::unique_xmalloc_ptr<char> *name, | |
2f68a895 | 578 | enum type_code *code) |
65d12d83 | 579 | { |
4d01a485 | 580 | expression_up exp; |
2a612529 | 581 | expr_completion_state cstate; |
65d12d83 | 582 | |
a70b8144 | 583 | try |
65d12d83 | 584 | { |
1eaebe02 | 585 | exp = parse_exp_in_context (&string, 0, 0, 0, false, nullptr, &cstate); |
65d12d83 | 586 | } |
230d2906 | 587 | catch (const gdb_exception_error &except) |
7556d4a4 PA |
588 | { |
589 | /* Nothing, EXP remains NULL. */ | |
590 | } | |
591 | ||
7556d4a4 | 592 | if (exp == NULL) |
65d12d83 | 593 | return NULL; |
2f68a895 | 594 | |
2a612529 | 595 | if (cstate.expout_tag_completion_type != TYPE_CODE_UNDEF) |
2f68a895 | 596 | { |
2a612529 TT |
597 | *code = cstate.expout_tag_completion_type; |
598 | *name = std::move (cstate.expout_completion_name); | |
2f68a895 TT |
599 | return NULL; |
600 | } | |
601 | ||
1eaebe02 TT |
602 | if (cstate.expout_last_op == nullptr) |
603 | return nullptr; | |
4933522d | 604 | |
1eaebe02 TT |
605 | expr::structop_base_operation *op = cstate.expout_last_op; |
606 | const std::string &fld = op->get_string (); | |
607 | *name = make_unique_xstrdup (fld.c_str ()); | |
608 | return value_type (op->evaluate_lhs (exp.get ())); | |
65d12d83 TT |
609 | } |
610 | ||
d30f5e1f | 611 | /* Parse floating point value P of length LEN. |
edd079d9 UW |
612 | Return false if invalid, true if valid. |
613 | The successfully parsed number is stored in DATA in | |
614 | target format for floating-point type TYPE. | |
d30f5e1f DE |
615 | |
616 | NOTE: This accepts the floating point syntax that sscanf accepts. */ | |
617 | ||
edd079d9 UW |
618 | bool |
619 | parse_float (const char *p, int len, | |
620 | const struct type *type, gdb_byte *data) | |
d30f5e1f | 621 | { |
f69fdf9b | 622 | return target_float_from_string (data, type, std::string (p, len)); |
d30f5e1f | 623 | } |
c906108c | 624 | \f |
f461f5cf PM |
625 | /* This function avoids direct calls to fprintf |
626 | in the parser generated debug code. */ | |
627 | void | |
628 | parser_fprintf (FILE *x, const char *y, ...) | |
629 | { | |
630 | va_list args; | |
ad3bbd48 | 631 | |
f461f5cf PM |
632 | va_start (args, y); |
633 | if (x == stderr) | |
634 | vfprintf_unfiltered (gdb_stderr, y, args); | |
635 | else | |
636 | { | |
637 | fprintf_unfiltered (gdb_stderr, " Unknown FILE used.\n"); | |
638 | vfprintf_unfiltered (gdb_stderr, y, args); | |
639 | } | |
640 | va_end (args); | |
641 | } | |
642 | ||
40d07d07 TT |
643 | /* Return rue if EXP uses OBJFILE (and will become dangling when |
644 | OBJFILE is unloaded), otherwise return false. OBJFILE must not be | |
645 | a separate debug info file. */ | |
c0201579 | 646 | |
40d07d07 | 647 | bool |
c0201579 JK |
648 | exp_uses_objfile (struct expression *exp, struct objfile *objfile) |
649 | { | |
650 | gdb_assert (objfile->separate_debug_objfile_backlink == NULL); | |
651 | ||
1eaebe02 | 652 | return exp->op->uses_objfile (objfile); |
410a0ff2 SDJ |
653 | } |
654 | ||
6c265988 | 655 | void _initialize_parse (); |
ac9a91a7 | 656 | void |
6c265988 | 657 | _initialize_parse () |
ac9a91a7 | 658 | { |
ccce17b0 YQ |
659 | add_setshow_zuinteger_cmd ("expression", class_maintenance, |
660 | &expressiondebug, | |
661 | _("Set expression debugging."), | |
662 | _("Show expression debugging."), | |
663 | _("When non-zero, the internal representation " | |
664 | "of expressions will be printed."), | |
665 | NULL, | |
666 | show_expressiondebug, | |
667 | &setdebuglist, &showdebuglist); | |
92981e24 | 668 | add_setshow_boolean_cmd ("parser", class_maintenance, |
3e43a32a MS |
669 | &parser_debug, |
670 | _("Set parser debugging."), | |
671 | _("Show parser debugging."), | |
672 | _("When non-zero, expression parser " | |
673 | "tracing will be enabled."), | |
92981e24 TT |
674 | NULL, |
675 | show_parserdebug, | |
676 | &setdebuglist, &showdebuglist); | |
c906108c | 677 | } |