* expression.h (parse_expression_for_completion): Rename
[deliverable/binutils-gdb.git] / gdb / parse.c
1 /* Parse expressions for GDB.
2
3 Copyright (C) 1986, 1989-2001, 2004-2005, 2007-2012 Free Software
4 Foundation, Inc.
5
6 Modified from expread.y by the Department of Computer Science at the
7 State University of New York at Buffalo, 1991.
8
9 This file is part of GDB.
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23
24 /* Parse an expression from text in a string,
25 and return the result as a struct expression pointer.
26 That structure contains arithmetic operations in reverse polish,
27 with constants represented by operations that are followed by special data.
28 See expression.h for the details of the format.
29 What is important here is that it can be built up sequentially
30 during the process of parsing; the lower levels of the tree always
31 come first in the result. */
32
33 #include "defs.h"
34 #include <ctype.h>
35 #include "arch-utils.h"
36 #include "gdb_string.h"
37 #include "symtab.h"
38 #include "gdbtypes.h"
39 #include "frame.h"
40 #include "expression.h"
41 #include "value.h"
42 #include "command.h"
43 #include "language.h"
44 #include "f-lang.h"
45 #include "parser-defs.h"
46 #include "gdbcmd.h"
47 #include "symfile.h" /* for overlay functions */
48 #include "inferior.h"
49 #include "doublest.h"
50 #include "gdb_assert.h"
51 #include "block.h"
52 #include "source.h"
53 #include "objfiles.h"
54 #include "exceptions.h"
55 #include "user-regs.h"
56
57 /* Standard set of definitions for printing, dumping, prefixifying,
58 * and evaluating expressions. */
59
60 const struct exp_descriptor exp_descriptor_standard =
61 {
62 print_subexp_standard,
63 operator_length_standard,
64 operator_check_standard,
65 op_name_standard,
66 dump_subexp_body_standard,
67 evaluate_subexp_standard
68 };
69 \f
70 /* Global variables declared in parser-defs.h (and commented there). */
71 struct expression *expout;
72 int expout_size;
73 int expout_ptr;
74 const struct block *expression_context_block;
75 CORE_ADDR expression_context_pc;
76 const struct block *innermost_block;
77 int arglist_len;
78 static struct type_stack type_stack;
79 char *lexptr;
80 char *prev_lexptr;
81 int paren_depth;
82 int comma_terminates;
83
84 /* True if parsing an expression to attempt completion. */
85 int parse_completion;
86
87 /* The index of the last struct expression directly before a '.' or
88 '->'. This is set when parsing and is only used when completing a
89 field name. It is -1 if no dereference operation was found. */
90 static int expout_last_struct = -1;
91 \f
92 static unsigned int expressiondebug = 0;
93 static void
94 show_expressiondebug (struct ui_file *file, int from_tty,
95 struct cmd_list_element *c, const char *value)
96 {
97 fprintf_filtered (file, _("Expression debugging is %s.\n"), value);
98 }
99
100
101 /* Non-zero if an expression parser should set yydebug. */
102 int parser_debug;
103
104 static void
105 show_parserdebug (struct ui_file *file, int from_tty,
106 struct cmd_list_element *c, const char *value)
107 {
108 fprintf_filtered (file, _("Parser debugging is %s.\n"), value);
109 }
110
111
112 static void free_funcalls (void *ignore);
113
114 static int prefixify_subexp (struct expression *, struct expression *, int,
115 int);
116
117 static struct expression *parse_exp_in_context (char **, CORE_ADDR,
118 const struct block *, int,
119 int, int *);
120
121 void _initialize_parse (void);
122
123 /* Data structure for saving values of arglist_len for function calls whose
124 arguments contain other function calls. */
125
126 struct funcall
127 {
128 struct funcall *next;
129 int arglist_len;
130 };
131
132 static struct funcall *funcall_chain;
133
134 /* Begin counting arguments for a function call,
135 saving the data about any containing call. */
136
137 void
138 start_arglist (void)
139 {
140 struct funcall *new;
141
142 new = (struct funcall *) xmalloc (sizeof (struct funcall));
143 new->next = funcall_chain;
144 new->arglist_len = arglist_len;
145 arglist_len = 0;
146 funcall_chain = new;
147 }
148
149 /* Return the number of arguments in a function call just terminated,
150 and restore the data for the containing function call. */
151
152 int
153 end_arglist (void)
154 {
155 int val = arglist_len;
156 struct funcall *call = funcall_chain;
157
158 funcall_chain = call->next;
159 arglist_len = call->arglist_len;
160 xfree (call);
161 return val;
162 }
163
164 /* Free everything in the funcall chain.
165 Used when there is an error inside parsing. */
166
167 static void
168 free_funcalls (void *ignore)
169 {
170 struct funcall *call, *next;
171
172 for (call = funcall_chain; call; call = next)
173 {
174 next = call->next;
175 xfree (call);
176 }
177 }
178 \f
179 /* This page contains the functions for adding data to the struct expression
180 being constructed. */
181
182 /* See definition in parser-defs.h. */
183
184 void
185 initialize_expout (int initial_size, const struct language_defn *lang,
186 struct gdbarch *gdbarch)
187 {
188 expout_size = initial_size;
189 expout_ptr = 0;
190 expout = xmalloc (sizeof (struct expression)
191 + EXP_ELEM_TO_BYTES (expout_size));
192 expout->language_defn = lang;
193 expout->gdbarch = gdbarch;
194 }
195
196 /* See definition in parser-defs.h. */
197
198 void
199 reallocate_expout (void)
200 {
201 /* Record the actual number of expression elements, and then
202 reallocate the expression memory so that we free up any
203 excess elements. */
204
205 expout->nelts = expout_ptr;
206 expout = xrealloc ((char *) expout,
207 sizeof (struct expression)
208 + EXP_ELEM_TO_BYTES (expout_ptr));
209 }
210
211 /* Add one element to the end of the expression. */
212
213 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
214 a register through here. */
215
216 static void
217 write_exp_elt (const union exp_element *expelt)
218 {
219 if (expout_ptr >= expout_size)
220 {
221 expout_size *= 2;
222 expout = (struct expression *)
223 xrealloc ((char *) expout, sizeof (struct expression)
224 + EXP_ELEM_TO_BYTES (expout_size));
225 }
226 expout->elts[expout_ptr++] = *expelt;
227 }
228
229 void
230 write_exp_elt_opcode (enum exp_opcode expelt)
231 {
232 union exp_element tmp;
233
234 memset (&tmp, 0, sizeof (union exp_element));
235 tmp.opcode = expelt;
236 write_exp_elt (&tmp);
237 }
238
239 void
240 write_exp_elt_sym (struct symbol *expelt)
241 {
242 union exp_element tmp;
243
244 memset (&tmp, 0, sizeof (union exp_element));
245 tmp.symbol = expelt;
246 write_exp_elt (&tmp);
247 }
248
249 void
250 write_exp_elt_block (const struct block *b)
251 {
252 union exp_element tmp;
253
254 memset (&tmp, 0, sizeof (union exp_element));
255 tmp.block = b;
256 write_exp_elt (&tmp);
257 }
258
259 void
260 write_exp_elt_objfile (struct objfile *objfile)
261 {
262 union exp_element tmp;
263
264 memset (&tmp, 0, sizeof (union exp_element));
265 tmp.objfile = objfile;
266 write_exp_elt (&tmp);
267 }
268
269 void
270 write_exp_elt_longcst (LONGEST expelt)
271 {
272 union exp_element tmp;
273
274 memset (&tmp, 0, sizeof (union exp_element));
275 tmp.longconst = expelt;
276 write_exp_elt (&tmp);
277 }
278
279 void
280 write_exp_elt_dblcst (DOUBLEST expelt)
281 {
282 union exp_element tmp;
283
284 memset (&tmp, 0, sizeof (union exp_element));
285 tmp.doubleconst = expelt;
286 write_exp_elt (&tmp);
287 }
288
289 void
290 write_exp_elt_decfloatcst (gdb_byte expelt[16])
291 {
292 union exp_element tmp;
293 int index;
294
295 for (index = 0; index < 16; index++)
296 tmp.decfloatconst[index] = expelt[index];
297
298 write_exp_elt (&tmp);
299 }
300
301 void
302 write_exp_elt_type (struct type *expelt)
303 {
304 union exp_element tmp;
305
306 memset (&tmp, 0, sizeof (union exp_element));
307 tmp.type = expelt;
308 write_exp_elt (&tmp);
309 }
310
311 void
312 write_exp_elt_intern (struct internalvar *expelt)
313 {
314 union exp_element tmp;
315
316 memset (&tmp, 0, sizeof (union exp_element));
317 tmp.internalvar = expelt;
318 write_exp_elt (&tmp);
319 }
320
321 /* Add a string constant to the end of the expression.
322
323 String constants are stored by first writing an expression element
324 that contains the length of the string, then stuffing the string
325 constant itself into however many expression elements are needed
326 to hold it, and then writing another expression element that contains
327 the length of the string. I.e. an expression element at each end of
328 the string records the string length, so you can skip over the
329 expression elements containing the actual string bytes from either
330 end of the string. Note that this also allows gdb to handle
331 strings with embedded null bytes, as is required for some languages.
332
333 Don't be fooled by the fact that the string is null byte terminated,
334 this is strictly for the convenience of debugging gdb itself.
335 Gdb does not depend up the string being null terminated, since the
336 actual length is recorded in expression elements at each end of the
337 string. The null byte is taken into consideration when computing how
338 many expression elements are required to hold the string constant, of
339 course. */
340
341
342 void
343 write_exp_string (struct stoken str)
344 {
345 int len = str.length;
346 int lenelt;
347 char *strdata;
348
349 /* Compute the number of expression elements required to hold the string
350 (including a null byte terminator), along with one expression element
351 at each end to record the actual string length (not including the
352 null byte terminator). */
353
354 lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
355
356 /* Ensure that we have enough available expression elements to store
357 everything. */
358
359 if ((expout_ptr + lenelt) >= expout_size)
360 {
361 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
362 expout = (struct expression *)
363 xrealloc ((char *) expout, (sizeof (struct expression)
364 + EXP_ELEM_TO_BYTES (expout_size)));
365 }
366
367 /* Write the leading length expression element (which advances the current
368 expression element index), then write the string constant followed by a
369 terminating null byte, and then write the trailing length expression
370 element. */
371
372 write_exp_elt_longcst ((LONGEST) len);
373 strdata = (char *) &expout->elts[expout_ptr];
374 memcpy (strdata, str.ptr, len);
375 *(strdata + len) = '\0';
376 expout_ptr += lenelt - 2;
377 write_exp_elt_longcst ((LONGEST) len);
378 }
379
380 /* Add a vector of string constants to the end of the expression.
381
382 This adds an OP_STRING operation, but encodes the contents
383 differently from write_exp_string. The language is expected to
384 handle evaluation of this expression itself.
385
386 After the usual OP_STRING header, TYPE is written into the
387 expression as a long constant. The interpretation of this field is
388 up to the language evaluator.
389
390 Next, each string in VEC is written. The length is written as a
391 long constant, followed by the contents of the string. */
392
393 void
394 write_exp_string_vector (int type, struct stoken_vector *vec)
395 {
396 int i, n_slots, len;
397
398 /* Compute the size. We compute the size in number of slots to
399 avoid issues with string padding. */
400 n_slots = 0;
401 for (i = 0; i < vec->len; ++i)
402 {
403 /* One slot for the length of this element, plus the number of
404 slots needed for this string. */
405 n_slots += 1 + BYTES_TO_EXP_ELEM (vec->tokens[i].length);
406 }
407
408 /* One more slot for the type of the string. */
409 ++n_slots;
410
411 /* Now compute a phony string length. */
412 len = EXP_ELEM_TO_BYTES (n_slots) - 1;
413
414 n_slots += 4;
415 if ((expout_ptr + n_slots) >= expout_size)
416 {
417 expout_size = max (expout_size * 2, expout_ptr + n_slots + 10);
418 expout = (struct expression *)
419 xrealloc ((char *) expout, (sizeof (struct expression)
420 + EXP_ELEM_TO_BYTES (expout_size)));
421 }
422
423 write_exp_elt_opcode (OP_STRING);
424 write_exp_elt_longcst (len);
425 write_exp_elt_longcst (type);
426
427 for (i = 0; i < vec->len; ++i)
428 {
429 write_exp_elt_longcst (vec->tokens[i].length);
430 memcpy (&expout->elts[expout_ptr], vec->tokens[i].ptr,
431 vec->tokens[i].length);
432 expout_ptr += BYTES_TO_EXP_ELEM (vec->tokens[i].length);
433 }
434
435 write_exp_elt_longcst (len);
436 write_exp_elt_opcode (OP_STRING);
437 }
438
439 /* Add a bitstring constant to the end of the expression.
440
441 Bitstring constants are stored by first writing an expression element
442 that contains the length of the bitstring (in bits), then stuffing the
443 bitstring constant itself into however many expression elements are
444 needed to hold it, and then writing another expression element that
445 contains the length of the bitstring. I.e. an expression element at
446 each end of the bitstring records the bitstring length, so you can skip
447 over the expression elements containing the actual bitstring bytes from
448 either end of the bitstring. */
449
450 void
451 write_exp_bitstring (struct stoken str)
452 {
453 int bits = str.length; /* length in bits */
454 int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
455 int lenelt;
456 char *strdata;
457
458 /* Compute the number of expression elements required to hold the bitstring,
459 along with one expression element at each end to record the actual
460 bitstring length in bits. */
461
462 lenelt = 2 + BYTES_TO_EXP_ELEM (len);
463
464 /* Ensure that we have enough available expression elements to store
465 everything. */
466
467 if ((expout_ptr + lenelt) >= expout_size)
468 {
469 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
470 expout = (struct expression *)
471 xrealloc ((char *) expout, (sizeof (struct expression)
472 + EXP_ELEM_TO_BYTES (expout_size)));
473 }
474
475 /* Write the leading length expression element (which advances the current
476 expression element index), then write the bitstring constant, and then
477 write the trailing length expression element. */
478
479 write_exp_elt_longcst ((LONGEST) bits);
480 strdata = (char *) &expout->elts[expout_ptr];
481 memcpy (strdata, str.ptr, len);
482 expout_ptr += lenelt - 2;
483 write_exp_elt_longcst ((LONGEST) bits);
484 }
485
486 /* Add the appropriate elements for a minimal symbol to the end of
487 the expression. */
488
489 void
490 write_exp_msymbol (struct minimal_symbol *msymbol)
491 {
492 struct objfile *objfile = msymbol_objfile (msymbol);
493 struct gdbarch *gdbarch = get_objfile_arch (objfile);
494
495 CORE_ADDR addr = SYMBOL_VALUE_ADDRESS (msymbol);
496 struct obj_section *section = SYMBOL_OBJ_SECTION (msymbol);
497 enum minimal_symbol_type type = MSYMBOL_TYPE (msymbol);
498 CORE_ADDR pc;
499
500 /* The minimal symbol might point to a function descriptor;
501 resolve it to the actual code address instead. */
502 pc = gdbarch_convert_from_func_ptr_addr (gdbarch, addr, &current_target);
503 if (pc != addr)
504 {
505 struct minimal_symbol *ifunc_msym = lookup_minimal_symbol_by_pc (pc);
506
507 /* In this case, assume we have a code symbol instead of
508 a data symbol. */
509
510 if (ifunc_msym != NULL && MSYMBOL_TYPE (ifunc_msym) == mst_text_gnu_ifunc
511 && SYMBOL_VALUE_ADDRESS (ifunc_msym) == pc)
512 {
513 /* A function descriptor has been resolved but PC is still in the
514 STT_GNU_IFUNC resolver body (such as because inferior does not
515 run to be able to call it). */
516
517 type = mst_text_gnu_ifunc;
518 }
519 else
520 type = mst_text;
521 section = NULL;
522 addr = pc;
523 }
524
525 if (overlay_debugging)
526 addr = symbol_overlayed_address (addr, section);
527
528 write_exp_elt_opcode (OP_LONG);
529 /* Let's make the type big enough to hold a 64-bit address. */
530 write_exp_elt_type (objfile_type (objfile)->builtin_core_addr);
531 write_exp_elt_longcst ((LONGEST) addr);
532 write_exp_elt_opcode (OP_LONG);
533
534 if (section && section->the_bfd_section->flags & SEC_THREAD_LOCAL)
535 {
536 write_exp_elt_opcode (UNOP_MEMVAL_TLS);
537 write_exp_elt_objfile (objfile);
538 write_exp_elt_type (objfile_type (objfile)->nodebug_tls_symbol);
539 write_exp_elt_opcode (UNOP_MEMVAL_TLS);
540 return;
541 }
542
543 write_exp_elt_opcode (UNOP_MEMVAL);
544 switch (type)
545 {
546 case mst_text:
547 case mst_file_text:
548 case mst_solib_trampoline:
549 write_exp_elt_type (objfile_type (objfile)->nodebug_text_symbol);
550 break;
551
552 case mst_text_gnu_ifunc:
553 write_exp_elt_type (objfile_type (objfile)
554 ->nodebug_text_gnu_ifunc_symbol);
555 break;
556
557 case mst_data:
558 case mst_file_data:
559 case mst_bss:
560 case mst_file_bss:
561 write_exp_elt_type (objfile_type (objfile)->nodebug_data_symbol);
562 break;
563
564 case mst_slot_got_plt:
565 write_exp_elt_type (objfile_type (objfile)->nodebug_got_plt_symbol);
566 break;
567
568 default:
569 write_exp_elt_type (objfile_type (objfile)->nodebug_unknown_symbol);
570 break;
571 }
572 write_exp_elt_opcode (UNOP_MEMVAL);
573 }
574
575 /* Mark the current index as the starting location of a structure
576 expression. This is used when completing on field names. */
577
578 void
579 mark_struct_expression (void)
580 {
581 expout_last_struct = expout_ptr;
582 }
583
584 \f
585 /* Recognize tokens that start with '$'. These include:
586
587 $regname A native register name or a "standard
588 register name".
589
590 $variable A convenience variable with a name chosen
591 by the user.
592
593 $digits Value history with index <digits>, starting
594 from the first value which has index 1.
595
596 $$digits Value history with index <digits> relative
597 to the last value. I.e. $$0 is the last
598 value, $$1 is the one previous to that, $$2
599 is the one previous to $$1, etc.
600
601 $ | $0 | $$0 The last value in the value history.
602
603 $$ An abbreviation for the second to the last
604 value in the value history, I.e. $$1 */
605
606 void
607 write_dollar_variable (struct stoken str)
608 {
609 struct symbol *sym = NULL;
610 struct minimal_symbol *msym = NULL;
611 struct internalvar *isym = NULL;
612
613 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
614 and $$digits (equivalent to $<-digits> if you could type that). */
615
616 int negate = 0;
617 int i = 1;
618 /* Double dollar means negate the number and add -1 as well.
619 Thus $$ alone means -1. */
620 if (str.length >= 2 && str.ptr[1] == '$')
621 {
622 negate = 1;
623 i = 2;
624 }
625 if (i == str.length)
626 {
627 /* Just dollars (one or two). */
628 i = -negate;
629 goto handle_last;
630 }
631 /* Is the rest of the token digits? */
632 for (; i < str.length; i++)
633 if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
634 break;
635 if (i == str.length)
636 {
637 i = atoi (str.ptr + 1 + negate);
638 if (negate)
639 i = -i;
640 goto handle_last;
641 }
642
643 /* Handle tokens that refer to machine registers:
644 $ followed by a register name. */
645 i = user_reg_map_name_to_regnum (parse_gdbarch,
646 str.ptr + 1, str.length - 1);
647 if (i >= 0)
648 goto handle_register;
649
650 /* Any names starting with $ are probably debugger internal variables. */
651
652 isym = lookup_only_internalvar (copy_name (str) + 1);
653 if (isym)
654 {
655 write_exp_elt_opcode (OP_INTERNALVAR);
656 write_exp_elt_intern (isym);
657 write_exp_elt_opcode (OP_INTERNALVAR);
658 return;
659 }
660
661 /* On some systems, such as HP-UX and hppa-linux, certain system routines
662 have names beginning with $ or $$. Check for those, first. */
663
664 sym = lookup_symbol (copy_name (str), (struct block *) NULL,
665 VAR_DOMAIN, (int *) NULL);
666 if (sym)
667 {
668 write_exp_elt_opcode (OP_VAR_VALUE);
669 write_exp_elt_block (block_found); /* set by lookup_symbol */
670 write_exp_elt_sym (sym);
671 write_exp_elt_opcode (OP_VAR_VALUE);
672 return;
673 }
674 msym = lookup_minimal_symbol (copy_name (str), NULL, NULL);
675 if (msym)
676 {
677 write_exp_msymbol (msym);
678 return;
679 }
680
681 /* Any other names are assumed to be debugger internal variables. */
682
683 write_exp_elt_opcode (OP_INTERNALVAR);
684 write_exp_elt_intern (create_internalvar (copy_name (str) + 1));
685 write_exp_elt_opcode (OP_INTERNALVAR);
686 return;
687 handle_last:
688 write_exp_elt_opcode (OP_LAST);
689 write_exp_elt_longcst ((LONGEST) i);
690 write_exp_elt_opcode (OP_LAST);
691 return;
692 handle_register:
693 write_exp_elt_opcode (OP_REGISTER);
694 str.length--;
695 str.ptr++;
696 write_exp_string (str);
697 write_exp_elt_opcode (OP_REGISTER);
698 return;
699 }
700
701
702 char *
703 find_template_name_end (char *p)
704 {
705 int depth = 1;
706 int just_seen_right = 0;
707 int just_seen_colon = 0;
708 int just_seen_space = 0;
709
710 if (!p || (*p != '<'))
711 return 0;
712
713 while (*++p)
714 {
715 switch (*p)
716 {
717 case '\'':
718 case '\"':
719 case '{':
720 case '}':
721 /* In future, may want to allow these?? */
722 return 0;
723 case '<':
724 depth++; /* start nested template */
725 if (just_seen_colon || just_seen_right || just_seen_space)
726 return 0; /* but not after : or :: or > or space */
727 break;
728 case '>':
729 if (just_seen_colon || just_seen_right)
730 return 0; /* end a (nested?) template */
731 just_seen_right = 1; /* but not after : or :: */
732 if (--depth == 0) /* also disallow >>, insist on > > */
733 return ++p; /* if outermost ended, return */
734 break;
735 case ':':
736 if (just_seen_space || (just_seen_colon > 1))
737 return 0; /* nested class spec coming up */
738 just_seen_colon++; /* we allow :: but not :::: */
739 break;
740 case ' ':
741 break;
742 default:
743 if (!((*p >= 'a' && *p <= 'z') || /* allow token chars */
744 (*p >= 'A' && *p <= 'Z') ||
745 (*p >= '0' && *p <= '9') ||
746 (*p == '_') || (*p == ',') || /* commas for template args */
747 (*p == '&') || (*p == '*') || /* pointer and ref types */
748 (*p == '(') || (*p == ')') || /* function types */
749 (*p == '[') || (*p == ']'))) /* array types */
750 return 0;
751 }
752 if (*p != ' ')
753 just_seen_space = 0;
754 if (*p != ':')
755 just_seen_colon = 0;
756 if (*p != '>')
757 just_seen_right = 0;
758 }
759 return 0;
760 }
761 \f
762
763 /* Return a null-terminated temporary copy of the name of a string token.
764
765 Tokens that refer to names do so with explicit pointer and length,
766 so they can share the storage that lexptr is parsing.
767 When it is necessary to pass a name to a function that expects
768 a null-terminated string, the substring is copied out
769 into a separate block of storage.
770
771 N.B. A single buffer is reused on each call. */
772
773 char *
774 copy_name (struct stoken token)
775 {
776 /* A temporary buffer for identifiers, so we can null-terminate them.
777 We allocate this with xrealloc. parse_exp_1 used to allocate with
778 alloca, using the size of the whole expression as a conservative
779 estimate of the space needed. However, macro expansion can
780 introduce names longer than the original expression; there's no
781 practical way to know beforehand how large that might be. */
782 static char *namecopy;
783 static size_t namecopy_size;
784
785 /* Make sure there's enough space for the token. */
786 if (namecopy_size < token.length + 1)
787 {
788 namecopy_size = token.length + 1;
789 namecopy = xrealloc (namecopy, token.length + 1);
790 }
791
792 memcpy (namecopy, token.ptr, token.length);
793 namecopy[token.length] = 0;
794
795 return namecopy;
796 }
797 \f
798
799 /* See comments on parser-defs.h. */
800
801 int
802 prefixify_expression (struct expression *expr)
803 {
804 int len = sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
805 struct expression *temp;
806 int inpos = expr->nelts, outpos = 0;
807
808 temp = (struct expression *) alloca (len);
809
810 /* Copy the original expression into temp. */
811 memcpy (temp, expr, len);
812
813 return prefixify_subexp (temp, expr, inpos, outpos);
814 }
815
816 /* Return the number of exp_elements in the postfix subexpression
817 of EXPR whose operator is at index ENDPOS - 1 in EXPR. */
818
819 int
820 length_of_subexp (struct expression *expr, int endpos)
821 {
822 int oplen, args;
823
824 operator_length (expr, endpos, &oplen, &args);
825
826 while (args > 0)
827 {
828 oplen += length_of_subexp (expr, endpos - oplen);
829 args--;
830 }
831
832 return oplen;
833 }
834
835 /* Sets *OPLENP to the length of the operator whose (last) index is
836 ENDPOS - 1 in EXPR, and sets *ARGSP to the number of arguments that
837 operator takes. */
838
839 void
840 operator_length (const struct expression *expr, int endpos, int *oplenp,
841 int *argsp)
842 {
843 expr->language_defn->la_exp_desc->operator_length (expr, endpos,
844 oplenp, argsp);
845 }
846
847 /* Default value for operator_length in exp_descriptor vectors. */
848
849 void
850 operator_length_standard (const struct expression *expr, int endpos,
851 int *oplenp, int *argsp)
852 {
853 int oplen = 1;
854 int args = 0;
855 enum f90_range_type range_type;
856 int i;
857
858 if (endpos < 1)
859 error (_("?error in operator_length_standard"));
860
861 i = (int) expr->elts[endpos - 1].opcode;
862
863 switch (i)
864 {
865 /* C++ */
866 case OP_SCOPE:
867 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
868 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
869 break;
870
871 case OP_LONG:
872 case OP_DOUBLE:
873 case OP_DECFLOAT:
874 case OP_VAR_VALUE:
875 oplen = 4;
876 break;
877
878 case OP_TYPE:
879 case OP_BOOL:
880 case OP_LAST:
881 case OP_INTERNALVAR:
882 case OP_VAR_ENTRY_VALUE:
883 oplen = 3;
884 break;
885
886 case OP_COMPLEX:
887 oplen = 3;
888 args = 2;
889 break;
890
891 case OP_FUNCALL:
892 case OP_F77_UNDETERMINED_ARGLIST:
893 oplen = 3;
894 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
895 break;
896
897 case TYPE_INSTANCE:
898 oplen = 4 + longest_to_int (expr->elts[endpos - 2].longconst);
899 args = 1;
900 break;
901
902 case OP_OBJC_MSGCALL: /* Objective C message (method) call. */
903 oplen = 4;
904 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
905 break;
906
907 case UNOP_MAX:
908 case UNOP_MIN:
909 oplen = 3;
910 break;
911
912 case UNOP_CAST_TYPE:
913 case UNOP_DYNAMIC_CAST:
914 case UNOP_REINTERPRET_CAST:
915 case UNOP_MEMVAL_TYPE:
916 oplen = 1;
917 args = 2;
918 break;
919
920 case BINOP_VAL:
921 case UNOP_CAST:
922 case UNOP_MEMVAL:
923 oplen = 3;
924 args = 1;
925 break;
926
927 case UNOP_MEMVAL_TLS:
928 oplen = 4;
929 args = 1;
930 break;
931
932 case UNOP_ABS:
933 case UNOP_CAP:
934 case UNOP_CHR:
935 case UNOP_FLOAT:
936 case UNOP_HIGH:
937 case UNOP_ODD:
938 case UNOP_ORD:
939 case UNOP_TRUNC:
940 case OP_TYPEOF:
941 case OP_DECLTYPE:
942 oplen = 1;
943 args = 1;
944 break;
945
946 case OP_ADL_FUNC:
947 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
948 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
949 oplen++;
950 oplen++;
951 break;
952
953 case STRUCTOP_STRUCT:
954 case STRUCTOP_PTR:
955 args = 1;
956 /* fall through */
957 case OP_REGISTER:
958 case OP_M2_STRING:
959 case OP_STRING:
960 case OP_OBJC_NSSTRING: /* Objective C Foundation Class
961 NSString constant. */
962 case OP_OBJC_SELECTOR: /* Objective C "@selector" pseudo-op. */
963 case OP_NAME:
964 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
965 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
966 break;
967
968 case OP_ARRAY:
969 oplen = 4;
970 args = longest_to_int (expr->elts[endpos - 2].longconst);
971 args -= longest_to_int (expr->elts[endpos - 3].longconst);
972 args += 1;
973 break;
974
975 case TERNOP_COND:
976 case TERNOP_SLICE:
977 args = 3;
978 break;
979
980 /* Modula-2 */
981 case MULTI_SUBSCRIPT:
982 oplen = 3;
983 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
984 break;
985
986 case BINOP_ASSIGN_MODIFY:
987 oplen = 3;
988 args = 2;
989 break;
990
991 /* C++ */
992 case OP_THIS:
993 oplen = 2;
994 break;
995
996 case OP_F90_RANGE:
997 oplen = 3;
998
999 range_type = longest_to_int (expr->elts[endpos - 2].longconst);
1000 switch (range_type)
1001 {
1002 case LOW_BOUND_DEFAULT:
1003 case HIGH_BOUND_DEFAULT:
1004 args = 1;
1005 break;
1006 case BOTH_BOUND_DEFAULT:
1007 args = 0;
1008 break;
1009 case NONE_BOUND_DEFAULT:
1010 args = 2;
1011 break;
1012 }
1013
1014 break;
1015
1016 default:
1017 args = 1 + (i < (int) BINOP_END);
1018 }
1019
1020 *oplenp = oplen;
1021 *argsp = args;
1022 }
1023
1024 /* Copy the subexpression ending just before index INEND in INEXPR
1025 into OUTEXPR, starting at index OUTBEG.
1026 In the process, convert it from suffix to prefix form.
1027 If EXPOUT_LAST_STRUCT is -1, then this function always returns -1.
1028 Otherwise, it returns the index of the subexpression which is the
1029 left-hand-side of the expression at EXPOUT_LAST_STRUCT. */
1030
1031 static int
1032 prefixify_subexp (struct expression *inexpr,
1033 struct expression *outexpr, int inend, int outbeg)
1034 {
1035 int oplen;
1036 int args;
1037 int i;
1038 int *arglens;
1039 int result = -1;
1040
1041 operator_length (inexpr, inend, &oplen, &args);
1042
1043 /* Copy the final operator itself, from the end of the input
1044 to the beginning of the output. */
1045 inend -= oplen;
1046 memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
1047 EXP_ELEM_TO_BYTES (oplen));
1048 outbeg += oplen;
1049
1050 if (expout_last_struct == inend)
1051 result = outbeg - oplen;
1052
1053 /* Find the lengths of the arg subexpressions. */
1054 arglens = (int *) alloca (args * sizeof (int));
1055 for (i = args - 1; i >= 0; i--)
1056 {
1057 oplen = length_of_subexp (inexpr, inend);
1058 arglens[i] = oplen;
1059 inend -= oplen;
1060 }
1061
1062 /* Now copy each subexpression, preserving the order of
1063 the subexpressions, but prefixifying each one.
1064 In this loop, inend starts at the beginning of
1065 the expression this level is working on
1066 and marches forward over the arguments.
1067 outbeg does similarly in the output. */
1068 for (i = 0; i < args; i++)
1069 {
1070 int r;
1071
1072 oplen = arglens[i];
1073 inend += oplen;
1074 r = prefixify_subexp (inexpr, outexpr, inend, outbeg);
1075 if (r != -1)
1076 {
1077 /* Return immediately. We probably have only parsed a
1078 partial expression, so we don't want to try to reverse
1079 the other operands. */
1080 return r;
1081 }
1082 outbeg += oplen;
1083 }
1084
1085 return result;
1086 }
1087 \f
1088 /* Read an expression from the string *STRINGPTR points to,
1089 parse it, and return a pointer to a struct expression that we malloc.
1090 Use block BLOCK as the lexical context for variable names;
1091 if BLOCK is zero, use the block of the selected stack frame.
1092 Meanwhile, advance *STRINGPTR to point after the expression,
1093 at the first nonwhite character that is not part of the expression
1094 (possibly a null character).
1095
1096 If COMMA is nonzero, stop if a comma is reached. */
1097
1098 struct expression *
1099 parse_exp_1 (char **stringptr, CORE_ADDR pc, const struct block *block,
1100 int comma)
1101 {
1102 return parse_exp_in_context (stringptr, pc, block, comma, 0, NULL);
1103 }
1104
1105 /* As for parse_exp_1, except that if VOID_CONTEXT_P, then
1106 no value is expected from the expression.
1107 OUT_SUBEXP is set when attempting to complete a field name; in this
1108 case it is set to the index of the subexpression on the
1109 left-hand-side of the struct op. If not doing such completion, it
1110 is left untouched. */
1111
1112 static struct expression *
1113 parse_exp_in_context (char **stringptr, CORE_ADDR pc, const struct block *block,
1114 int comma, int void_context_p, int *out_subexp)
1115 {
1116 volatile struct gdb_exception except;
1117 struct cleanup *old_chain;
1118 const struct language_defn *lang = NULL;
1119 int subexp;
1120
1121 lexptr = *stringptr;
1122 prev_lexptr = NULL;
1123
1124 paren_depth = 0;
1125 type_stack.depth = 0;
1126 expout_last_struct = -1;
1127
1128 comma_terminates = comma;
1129
1130 if (lexptr == 0 || *lexptr == 0)
1131 error_no_arg (_("expression to compute"));
1132
1133 old_chain = make_cleanup (free_funcalls, 0 /*ignore*/);
1134 funcall_chain = 0;
1135
1136 expression_context_block = block;
1137
1138 /* If no context specified, try using the current frame, if any. */
1139 if (!expression_context_block)
1140 expression_context_block = get_selected_block (&expression_context_pc);
1141 else if (pc == 0)
1142 expression_context_pc = BLOCK_START (expression_context_block);
1143 else
1144 expression_context_pc = pc;
1145
1146 /* Fall back to using the current source static context, if any. */
1147
1148 if (!expression_context_block)
1149 {
1150 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
1151 if (cursal.symtab)
1152 expression_context_block
1153 = BLOCKVECTOR_BLOCK (BLOCKVECTOR (cursal.symtab), STATIC_BLOCK);
1154 if (expression_context_block)
1155 expression_context_pc = BLOCK_START (expression_context_block);
1156 }
1157
1158 if (language_mode == language_mode_auto && block != NULL)
1159 {
1160 /* Find the language associated to the given context block.
1161 Default to the current language if it can not be determined.
1162
1163 Note that using the language corresponding to the current frame
1164 can sometimes give unexpected results. For instance, this
1165 routine is often called several times during the inferior
1166 startup phase to re-parse breakpoint expressions after
1167 a new shared library has been loaded. The language associated
1168 to the current frame at this moment is not relevant for
1169 the breakpoint. Using it would therefore be silly, so it seems
1170 better to rely on the current language rather than relying on
1171 the current frame language to parse the expression. That's why
1172 we do the following language detection only if the context block
1173 has been specifically provided. */
1174 struct symbol *func = block_linkage_function (block);
1175
1176 if (func != NULL)
1177 lang = language_def (SYMBOL_LANGUAGE (func));
1178 if (lang == NULL || lang->la_language == language_unknown)
1179 lang = current_language;
1180 }
1181 else
1182 lang = current_language;
1183
1184 initialize_expout (10, lang, get_current_arch ());
1185
1186 TRY_CATCH (except, RETURN_MASK_ALL)
1187 {
1188 if (lang->la_parser ())
1189 lang->la_error (NULL);
1190 }
1191 if (except.reason < 0)
1192 {
1193 if (! parse_completion)
1194 {
1195 xfree (expout);
1196 throw_exception (except);
1197 }
1198 }
1199
1200 discard_cleanups (old_chain);
1201
1202 reallocate_expout ();
1203
1204 /* Convert expression from postfix form as generated by yacc
1205 parser, to a prefix form. */
1206
1207 if (expressiondebug)
1208 dump_raw_expression (expout, gdb_stdlog,
1209 "before conversion to prefix form");
1210
1211 subexp = prefixify_expression (expout);
1212 if (out_subexp)
1213 *out_subexp = subexp;
1214
1215 lang->la_post_parser (&expout, void_context_p);
1216
1217 if (expressiondebug)
1218 dump_prefix_expression (expout, gdb_stdlog);
1219
1220 *stringptr = lexptr;
1221 return expout;
1222 }
1223
1224 /* Parse STRING as an expression, and complain if this fails
1225 to use up all of the contents of STRING. */
1226
1227 struct expression *
1228 parse_expression (char *string)
1229 {
1230 struct expression *exp;
1231
1232 exp = parse_exp_1 (&string, 0, 0, 0);
1233 if (*string)
1234 error (_("Junk after end of expression."));
1235 return exp;
1236 }
1237
1238 /* Parse STRING as an expression. If parsing ends in the middle of a
1239 field reference, return the type of the left-hand-side of the
1240 reference; furthermore, if the parsing ends in the field name,
1241 return the field name in *NAME. If the parsing ends in the middle
1242 of a field reference, but the reference is somehow invalid, throw
1243 an exception. In all other cases, return NULL. Returned non-NULL
1244 *NAME must be freed by the caller. */
1245
1246 struct type *
1247 parse_expression_for_completion (char *string, char **name)
1248 {
1249 struct expression *exp = NULL;
1250 struct value *val;
1251 int subexp;
1252 volatile struct gdb_exception except;
1253
1254 TRY_CATCH (except, RETURN_MASK_ERROR)
1255 {
1256 parse_completion = 1;
1257 exp = parse_exp_in_context (&string, 0, 0, 0, 0, &subexp);
1258 }
1259 parse_completion = 0;
1260 if (except.reason < 0 || ! exp)
1261 return NULL;
1262 if (expout_last_struct == -1)
1263 {
1264 xfree (exp);
1265 return NULL;
1266 }
1267
1268 *name = extract_field_op (exp, &subexp);
1269 if (!*name)
1270 {
1271 xfree (exp);
1272 return NULL;
1273 }
1274
1275 /* This might throw an exception. If so, we want to let it
1276 propagate. */
1277 val = evaluate_subexpression_type (exp, subexp);
1278 /* (*NAME) is a part of the EXP memory block freed below. */
1279 *name = xstrdup (*name);
1280 xfree (exp);
1281
1282 return value_type (val);
1283 }
1284
1285 /* A post-parser that does nothing. */
1286
1287 void
1288 null_post_parser (struct expression **exp, int void_context_p)
1289 {
1290 }
1291
1292 /* Parse floating point value P of length LEN.
1293 Return 0 (false) if invalid, 1 (true) if valid.
1294 The successfully parsed number is stored in D.
1295 *SUFFIX points to the suffix of the number in P.
1296
1297 NOTE: This accepts the floating point syntax that sscanf accepts. */
1298
1299 int
1300 parse_float (const char *p, int len, DOUBLEST *d, const char **suffix)
1301 {
1302 char *copy;
1303 int n, num;
1304
1305 copy = xmalloc (len + 1);
1306 memcpy (copy, p, len);
1307 copy[len] = 0;
1308
1309 num = sscanf (copy, "%" DOUBLEST_SCAN_FORMAT "%n", d, &n);
1310 xfree (copy);
1311
1312 /* The sscanf man page suggests not making any assumptions on the effect
1313 of %n on the result, so we don't.
1314 That is why we simply test num == 0. */
1315 if (num == 0)
1316 return 0;
1317
1318 *suffix = p + n;
1319 return 1;
1320 }
1321
1322 /* Parse floating point value P of length LEN, using the C syntax for floats.
1323 Return 0 (false) if invalid, 1 (true) if valid.
1324 The successfully parsed number is stored in *D.
1325 Its type is taken from builtin_type (gdbarch) and is stored in *T. */
1326
1327 int
1328 parse_c_float (struct gdbarch *gdbarch, const char *p, int len,
1329 DOUBLEST *d, struct type **t)
1330 {
1331 const char *suffix;
1332 int suffix_len;
1333 const struct builtin_type *builtin_types = builtin_type (gdbarch);
1334
1335 if (! parse_float (p, len, d, &suffix))
1336 return 0;
1337
1338 suffix_len = p + len - suffix;
1339
1340 if (suffix_len == 0)
1341 *t = builtin_types->builtin_double;
1342 else if (suffix_len == 1)
1343 {
1344 /* Handle suffixes: 'f' for float, 'l' for long double. */
1345 if (tolower (*suffix) == 'f')
1346 *t = builtin_types->builtin_float;
1347 else if (tolower (*suffix) == 'l')
1348 *t = builtin_types->builtin_long_double;
1349 else
1350 return 0;
1351 }
1352 else
1353 return 0;
1354
1355 return 1;
1356 }
1357 \f
1358 /* Stuff for maintaining a stack of types. Currently just used by C, but
1359 probably useful for any language which declares its types "backwards". */
1360
1361 /* Ensure that there are HOWMUCH open slots on the type stack STACK. */
1362
1363 static void
1364 type_stack_reserve (struct type_stack *stack, int howmuch)
1365 {
1366 if (stack->depth + howmuch >= stack->size)
1367 {
1368 stack->size *= 2;
1369 if (stack->size < howmuch)
1370 stack->size = howmuch;
1371 stack->elements = xrealloc (stack->elements,
1372 stack->size * sizeof (union type_stack_elt));
1373 }
1374 }
1375
1376 /* Ensure that there is a single open slot in the global type stack. */
1377
1378 static void
1379 check_type_stack_depth (void)
1380 {
1381 type_stack_reserve (&type_stack, 1);
1382 }
1383
1384 /* A helper function for insert_type and insert_type_address_space.
1385 This does work of expanding the type stack and inserting the new
1386 element, ELEMENT, into the stack at location SLOT. */
1387
1388 static void
1389 insert_into_type_stack (int slot, union type_stack_elt element)
1390 {
1391 check_type_stack_depth ();
1392
1393 if (slot < type_stack.depth)
1394 memmove (&type_stack.elements[slot + 1], &type_stack.elements[slot],
1395 (type_stack.depth - slot) * sizeof (union type_stack_elt));
1396 type_stack.elements[slot] = element;
1397 ++type_stack.depth;
1398 }
1399
1400 /* Insert a new type, TP, at the bottom of the type stack. If TP is
1401 tp_pointer or tp_reference, it is inserted at the bottom. If TP is
1402 a qualifier, it is inserted at slot 1 (just above a previous
1403 tp_pointer) if there is anything on the stack, or simply pushed if
1404 the stack is empty. Other values for TP are invalid. */
1405
1406 void
1407 insert_type (enum type_pieces tp)
1408 {
1409 union type_stack_elt element;
1410 int slot;
1411
1412 gdb_assert (tp == tp_pointer || tp == tp_reference
1413 || tp == tp_const || tp == tp_volatile);
1414
1415 /* If there is anything on the stack (we know it will be a
1416 tp_pointer), insert the qualifier above it. Otherwise, simply
1417 push this on the top of the stack. */
1418 if (type_stack.depth && (tp == tp_const || tp == tp_volatile))
1419 slot = 1;
1420 else
1421 slot = 0;
1422
1423 element.piece = tp;
1424 insert_into_type_stack (slot, element);
1425 }
1426
1427 void
1428 push_type (enum type_pieces tp)
1429 {
1430 check_type_stack_depth ();
1431 type_stack.elements[type_stack.depth++].piece = tp;
1432 }
1433
1434 void
1435 push_type_int (int n)
1436 {
1437 check_type_stack_depth ();
1438 type_stack.elements[type_stack.depth++].int_val = n;
1439 }
1440
1441 /* Insert a tp_space_identifier and the corresponding address space
1442 value into the stack. STRING is the name of an address space, as
1443 recognized by address_space_name_to_int. If the stack is empty,
1444 the new elements are simply pushed. If the stack is not empty,
1445 this function assumes that the first item on the stack is a
1446 tp_pointer, and the new values are inserted above the first
1447 item. */
1448
1449 void
1450 insert_type_address_space (char *string)
1451 {
1452 union type_stack_elt element;
1453 int slot;
1454
1455 /* If there is anything on the stack (we know it will be a
1456 tp_pointer), insert the address space qualifier above it.
1457 Otherwise, simply push this on the top of the stack. */
1458 if (type_stack.depth)
1459 slot = 1;
1460 else
1461 slot = 0;
1462
1463 element.piece = tp_space_identifier;
1464 insert_into_type_stack (slot, element);
1465 element.int_val = address_space_name_to_int (parse_gdbarch, string);
1466 insert_into_type_stack (slot, element);
1467 }
1468
1469 enum type_pieces
1470 pop_type (void)
1471 {
1472 if (type_stack.depth)
1473 return type_stack.elements[--type_stack.depth].piece;
1474 return tp_end;
1475 }
1476
1477 int
1478 pop_type_int (void)
1479 {
1480 if (type_stack.depth)
1481 return type_stack.elements[--type_stack.depth].int_val;
1482 /* "Can't happen". */
1483 return 0;
1484 }
1485
1486 /* Pop a type list element from the global type stack. */
1487
1488 static VEC (type_ptr) *
1489 pop_typelist (void)
1490 {
1491 gdb_assert (type_stack.depth);
1492 return type_stack.elements[--type_stack.depth].typelist_val;
1493 }
1494
1495 /* Pop a type_stack element from the global type stack. */
1496
1497 static struct type_stack *
1498 pop_type_stack (void)
1499 {
1500 gdb_assert (type_stack.depth);
1501 return type_stack.elements[--type_stack.depth].stack_val;
1502 }
1503
1504 /* Append the elements of the type stack FROM to the type stack TO.
1505 Always returns TO. */
1506
1507 struct type_stack *
1508 append_type_stack (struct type_stack *to, struct type_stack *from)
1509 {
1510 type_stack_reserve (to, from->depth);
1511
1512 memcpy (&to->elements[to->depth], &from->elements[0],
1513 from->depth * sizeof (union type_stack_elt));
1514 to->depth += from->depth;
1515
1516 return to;
1517 }
1518
1519 /* Push the type stack STACK as an element on the global type stack. */
1520
1521 void
1522 push_type_stack (struct type_stack *stack)
1523 {
1524 check_type_stack_depth ();
1525 type_stack.elements[type_stack.depth++].stack_val = stack;
1526 push_type (tp_type_stack);
1527 }
1528
1529 /* Copy the global type stack into a newly allocated type stack and
1530 return it. The global stack is cleared. The returned type stack
1531 must be freed with type_stack_cleanup. */
1532
1533 struct type_stack *
1534 get_type_stack (void)
1535 {
1536 struct type_stack *result = XNEW (struct type_stack);
1537
1538 *result = type_stack;
1539 type_stack.depth = 0;
1540 type_stack.size = 0;
1541 type_stack.elements = NULL;
1542
1543 return result;
1544 }
1545
1546 /* A cleanup function that destroys a single type stack. */
1547
1548 void
1549 type_stack_cleanup (void *arg)
1550 {
1551 struct type_stack *stack = arg;
1552
1553 xfree (stack->elements);
1554 xfree (stack);
1555 }
1556
1557 /* Push a function type with arguments onto the global type stack.
1558 LIST holds the argument types. If the final item in LIST is NULL,
1559 then the function will be varargs. */
1560
1561 void
1562 push_typelist (VEC (type_ptr) *list)
1563 {
1564 check_type_stack_depth ();
1565 type_stack.elements[type_stack.depth++].typelist_val = list;
1566 push_type (tp_function_with_arguments);
1567 }
1568
1569 /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
1570 as modified by all the stuff on the stack. */
1571 struct type *
1572 follow_types (struct type *follow_type)
1573 {
1574 int done = 0;
1575 int make_const = 0;
1576 int make_volatile = 0;
1577 int make_addr_space = 0;
1578 int array_size;
1579
1580 while (!done)
1581 switch (pop_type ())
1582 {
1583 case tp_end:
1584 done = 1;
1585 if (make_const)
1586 follow_type = make_cv_type (make_const,
1587 TYPE_VOLATILE (follow_type),
1588 follow_type, 0);
1589 if (make_volatile)
1590 follow_type = make_cv_type (TYPE_CONST (follow_type),
1591 make_volatile,
1592 follow_type, 0);
1593 if (make_addr_space)
1594 follow_type = make_type_with_address_space (follow_type,
1595 make_addr_space);
1596 make_const = make_volatile = 0;
1597 make_addr_space = 0;
1598 break;
1599 case tp_const:
1600 make_const = 1;
1601 break;
1602 case tp_volatile:
1603 make_volatile = 1;
1604 break;
1605 case tp_space_identifier:
1606 make_addr_space = pop_type_int ();
1607 break;
1608 case tp_pointer:
1609 follow_type = lookup_pointer_type (follow_type);
1610 if (make_const)
1611 follow_type = make_cv_type (make_const,
1612 TYPE_VOLATILE (follow_type),
1613 follow_type, 0);
1614 if (make_volatile)
1615 follow_type = make_cv_type (TYPE_CONST (follow_type),
1616 make_volatile,
1617 follow_type, 0);
1618 if (make_addr_space)
1619 follow_type = make_type_with_address_space (follow_type,
1620 make_addr_space);
1621 make_const = make_volatile = 0;
1622 make_addr_space = 0;
1623 break;
1624 case tp_reference:
1625 follow_type = lookup_reference_type (follow_type);
1626 if (make_const)
1627 follow_type = make_cv_type (make_const,
1628 TYPE_VOLATILE (follow_type),
1629 follow_type, 0);
1630 if (make_volatile)
1631 follow_type = make_cv_type (TYPE_CONST (follow_type),
1632 make_volatile,
1633 follow_type, 0);
1634 if (make_addr_space)
1635 follow_type = make_type_with_address_space (follow_type,
1636 make_addr_space);
1637 make_const = make_volatile = 0;
1638 make_addr_space = 0;
1639 break;
1640 case tp_array:
1641 array_size = pop_type_int ();
1642 /* FIXME-type-allocation: need a way to free this type when we are
1643 done with it. */
1644 follow_type =
1645 lookup_array_range_type (follow_type,
1646 0, array_size >= 0 ? array_size - 1 : 0);
1647 if (array_size < 0)
1648 TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (follow_type) = 1;
1649 break;
1650 case tp_function:
1651 /* FIXME-type-allocation: need a way to free this type when we are
1652 done with it. */
1653 follow_type = lookup_function_type (follow_type);
1654 break;
1655
1656 case tp_function_with_arguments:
1657 {
1658 VEC (type_ptr) *args = pop_typelist ();
1659
1660 follow_type
1661 = lookup_function_type_with_arguments (follow_type,
1662 VEC_length (type_ptr, args),
1663 VEC_address (type_ptr,
1664 args));
1665 VEC_free (type_ptr, args);
1666 }
1667 break;
1668
1669 case tp_type_stack:
1670 {
1671 struct type_stack *stack = pop_type_stack ();
1672 /* Sort of ugly, but not really much worse than the
1673 alternatives. */
1674 struct type_stack save = type_stack;
1675
1676 type_stack = *stack;
1677 follow_type = follow_types (follow_type);
1678 gdb_assert (type_stack.depth == 0);
1679
1680 type_stack = save;
1681 }
1682 break;
1683 default:
1684 gdb_assert_not_reached ("unrecognized tp_ value in follow_types");
1685 }
1686 return follow_type;
1687 }
1688 \f
1689 /* This function avoids direct calls to fprintf
1690 in the parser generated debug code. */
1691 void
1692 parser_fprintf (FILE *x, const char *y, ...)
1693 {
1694 va_list args;
1695
1696 va_start (args, y);
1697 if (x == stderr)
1698 vfprintf_unfiltered (gdb_stderr, y, args);
1699 else
1700 {
1701 fprintf_unfiltered (gdb_stderr, " Unknown FILE used.\n");
1702 vfprintf_unfiltered (gdb_stderr, y, args);
1703 }
1704 va_end (args);
1705 }
1706
1707 /* Implementation of the exp_descriptor method operator_check. */
1708
1709 int
1710 operator_check_standard (struct expression *exp, int pos,
1711 int (*objfile_func) (struct objfile *objfile,
1712 void *data),
1713 void *data)
1714 {
1715 const union exp_element *const elts = exp->elts;
1716 struct type *type = NULL;
1717 struct objfile *objfile = NULL;
1718
1719 /* Extended operators should have been already handled by exp_descriptor
1720 iterate method of its specific language. */
1721 gdb_assert (elts[pos].opcode < OP_EXTENDED0);
1722
1723 /* Track the callers of write_exp_elt_type for this table. */
1724
1725 switch (elts[pos].opcode)
1726 {
1727 case BINOP_VAL:
1728 case OP_COMPLEX:
1729 case OP_DECFLOAT:
1730 case OP_DOUBLE:
1731 case OP_LONG:
1732 case OP_SCOPE:
1733 case OP_TYPE:
1734 case UNOP_CAST:
1735 case UNOP_MAX:
1736 case UNOP_MEMVAL:
1737 case UNOP_MIN:
1738 type = elts[pos + 1].type;
1739 break;
1740
1741 case TYPE_INSTANCE:
1742 {
1743 LONGEST arg, nargs = elts[pos + 1].longconst;
1744
1745 for (arg = 0; arg < nargs; arg++)
1746 {
1747 struct type *type = elts[pos + 2 + arg].type;
1748 struct objfile *objfile = TYPE_OBJFILE (type);
1749
1750 if (objfile && (*objfile_func) (objfile, data))
1751 return 1;
1752 }
1753 }
1754 break;
1755
1756 case UNOP_MEMVAL_TLS:
1757 objfile = elts[pos + 1].objfile;
1758 type = elts[pos + 2].type;
1759 break;
1760
1761 case OP_VAR_VALUE:
1762 {
1763 const struct block *const block = elts[pos + 1].block;
1764 const struct symbol *const symbol = elts[pos + 2].symbol;
1765
1766 /* Check objfile where the variable itself is placed.
1767 SYMBOL_OBJ_SECTION (symbol) may be NULL. */
1768 if ((*objfile_func) (SYMBOL_SYMTAB (symbol)->objfile, data))
1769 return 1;
1770
1771 /* Check objfile where is placed the code touching the variable. */
1772 objfile = lookup_objfile_from_block (block);
1773
1774 type = SYMBOL_TYPE (symbol);
1775 }
1776 break;
1777 }
1778
1779 /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL. */
1780
1781 if (type && TYPE_OBJFILE (type)
1782 && (*objfile_func) (TYPE_OBJFILE (type), data))
1783 return 1;
1784 if (objfile && (*objfile_func) (objfile, data))
1785 return 1;
1786
1787 return 0;
1788 }
1789
1790 /* Call OBJFILE_FUNC for any TYPE and OBJFILE found being referenced by EXP.
1791 The functions are never called with NULL OBJFILE. Functions get passed an
1792 arbitrary caller supplied DATA pointer. If any of the functions returns
1793 non-zero value then (any other) non-zero value is immediately returned to
1794 the caller. Otherwise zero is returned after iterating through whole EXP.
1795 */
1796
1797 static int
1798 exp_iterate (struct expression *exp,
1799 int (*objfile_func) (struct objfile *objfile, void *data),
1800 void *data)
1801 {
1802 int endpos;
1803
1804 for (endpos = exp->nelts; endpos > 0; )
1805 {
1806 int pos, args, oplen = 0;
1807
1808 operator_length (exp, endpos, &oplen, &args);
1809 gdb_assert (oplen > 0);
1810
1811 pos = endpos - oplen;
1812 if (exp->language_defn->la_exp_desc->operator_check (exp, pos,
1813 objfile_func, data))
1814 return 1;
1815
1816 endpos = pos;
1817 }
1818
1819 return 0;
1820 }
1821
1822 /* Helper for exp_uses_objfile. */
1823
1824 static int
1825 exp_uses_objfile_iter (struct objfile *exp_objfile, void *objfile_voidp)
1826 {
1827 struct objfile *objfile = objfile_voidp;
1828
1829 if (exp_objfile->separate_debug_objfile_backlink)
1830 exp_objfile = exp_objfile->separate_debug_objfile_backlink;
1831
1832 return exp_objfile == objfile;
1833 }
1834
1835 /* Return 1 if EXP uses OBJFILE (and will become dangling when OBJFILE
1836 is unloaded), otherwise return 0. OBJFILE must not be a separate debug info
1837 file. */
1838
1839 int
1840 exp_uses_objfile (struct expression *exp, struct objfile *objfile)
1841 {
1842 gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
1843
1844 return exp_iterate (exp, exp_uses_objfile_iter, objfile);
1845 }
1846
1847 void
1848 _initialize_parse (void)
1849 {
1850 type_stack.size = 0;
1851 type_stack.depth = 0;
1852 type_stack.elements = NULL;
1853
1854 add_setshow_zuinteger_cmd ("expression", class_maintenance,
1855 &expressiondebug,
1856 _("Set expression debugging."),
1857 _("Show expression debugging."),
1858 _("When non-zero, the internal representation "
1859 "of expressions will be printed."),
1860 NULL,
1861 show_expressiondebug,
1862 &setdebuglist, &showdebuglist);
1863 add_setshow_boolean_cmd ("parser", class_maintenance,
1864 &parser_debug,
1865 _("Set parser debugging."),
1866 _("Show parser debugging."),
1867 _("When non-zero, expression parser "
1868 "tracing will be enabled."),
1869 NULL,
1870 show_parserdebug,
1871 &setdebuglist, &showdebuglist);
1872 }
This page took 0.066757 seconds and 5 git commands to generate.