eae55865f28d9777584dc8dbd8fae1e4c75e1b09
[deliverable/binutils-gdb.git] / gdb / parse.c
1 /* Parse expressions for GDB.
2 Copyright 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 Modified from expread.y by the Department of Computer Science at the
5 State University of New York at Buffalo, 1991.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
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 <ctype.h>
34
35 #include "defs.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 "parser-defs.h"
45 #include "gdbcmd.h"
46 #include "symfile.h" /* for overlay functions */
47 #include "inferior.h" /* for NUM_PSEUDO_REGS. NOTE: replace
48 with "gdbarch.h" when appropriate. */
49 #include "doublest.h"
50 #include "builtin-regs.h"
51 #include "gdb_assert.h"
52
53 \f
54 /* Symbols which architectures can redefine. */
55
56 /* Some systems have routines whose names start with `$'. Giving this
57 macro a non-zero value tells GDB's expression parser to check for
58 such routines when parsing tokens that begin with `$'.
59
60 On HP-UX, certain system routines (millicode) have names beginning
61 with `$' or `$$'. For example, `$$dyncall' is a millicode routine
62 that handles inter-space procedure calls on PA-RISC. */
63 #ifndef SYMBOLS_CAN_START_WITH_DOLLAR
64 #define SYMBOLS_CAN_START_WITH_DOLLAR (0)
65 #endif
66
67
68 \f
69 /* Global variables declared in parser-defs.h (and commented there). */
70 struct expression *expout;
71 int expout_size;
72 int expout_ptr;
73 struct block *expression_context_block;
74 struct block *innermost_block;
75 int arglist_len;
76 union type_stack_elt *type_stack;
77 int type_stack_depth, type_stack_size;
78 char *lexptr;
79 char *namecopy;
80 int paren_depth;
81 int comma_terminates;
82 \f
83 static int expressiondebug = 0;
84
85 extern int hp_som_som_object_present;
86
87 static void free_funcalls (void *ignore);
88
89 static void prefixify_expression (struct expression *);
90
91 static void
92 prefixify_subexp (struct expression *, struct expression *, int, int);
93
94 void _initialize_parse (void);
95
96 /* Data structure for saving values of arglist_len for function calls whose
97 arguments contain other function calls. */
98
99 struct funcall
100 {
101 struct funcall *next;
102 int arglist_len;
103 };
104
105 static struct funcall *funcall_chain;
106
107 /* Assign machine-independent names to certain registers
108 (unless overridden by the REGISTER_NAMES table) */
109
110 unsigned num_std_regs = 0;
111 struct std_regs *std_regs;
112
113 /* The generic method for targets to specify how their registers are
114 named. The mapping can be derived from three sources:
115 REGISTER_NAME; std_regs; or a target specific alias hook. */
116
117 int
118 target_map_name_to_register (char *str, int len)
119 {
120 int i;
121
122 /* Search register name space. */
123 for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
124 if (REGISTER_NAME (i) && len == strlen (REGISTER_NAME (i))
125 && STREQN (str, REGISTER_NAME (i), len))
126 {
127 return i;
128 }
129
130 /* Try standard aliases. */
131 for (i = 0; i < num_std_regs; i++)
132 if (std_regs[i].name && len == strlen (std_regs[i].name)
133 && STREQN (str, std_regs[i].name, len))
134 {
135 return std_regs[i].regnum;
136 }
137
138 /* Try builtin registers. */
139 i = builtin_reg_map_name_to_regnum (str, len);
140 if (i >= 0)
141 {
142 gdb_assert (i >= NUM_REGS + NUM_PSEUDO_REGS);
143 return i;
144 }
145
146 return -1;
147 }
148
149 /* Begin counting arguments for a function call,
150 saving the data about any containing call. */
151
152 void
153 start_arglist (void)
154 {
155 register struct funcall *new;
156
157 new = (struct funcall *) xmalloc (sizeof (struct funcall));
158 new->next = funcall_chain;
159 new->arglist_len = arglist_len;
160 arglist_len = 0;
161 funcall_chain = new;
162 }
163
164 /* Return the number of arguments in a function call just terminated,
165 and restore the data for the containing function call. */
166
167 int
168 end_arglist (void)
169 {
170 register int val = arglist_len;
171 register struct funcall *call = funcall_chain;
172 funcall_chain = call->next;
173 arglist_len = call->arglist_len;
174 xfree (call);
175 return val;
176 }
177
178 /* Free everything in the funcall chain.
179 Used when there is an error inside parsing. */
180
181 static void
182 free_funcalls (void *ignore)
183 {
184 register struct funcall *call, *next;
185
186 for (call = funcall_chain; call; call = next)
187 {
188 next = call->next;
189 xfree (call);
190 }
191 }
192 \f
193 /* This page contains the functions for adding data to the struct expression
194 being constructed. */
195
196 /* Add one element to the end of the expression. */
197
198 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
199 a register through here */
200
201 void
202 write_exp_elt (union exp_element expelt)
203 {
204 if (expout_ptr >= expout_size)
205 {
206 expout_size *= 2;
207 expout = (struct expression *)
208 xrealloc ((char *) expout, sizeof (struct expression)
209 + EXP_ELEM_TO_BYTES (expout_size));
210 }
211 expout->elts[expout_ptr++] = expelt;
212 }
213
214 void
215 write_exp_elt_opcode (enum exp_opcode expelt)
216 {
217 union exp_element tmp;
218
219 tmp.opcode = expelt;
220
221 write_exp_elt (tmp);
222 }
223
224 void
225 write_exp_elt_sym (struct symbol *expelt)
226 {
227 union exp_element tmp;
228
229 tmp.symbol = expelt;
230
231 write_exp_elt (tmp);
232 }
233
234 void
235 write_exp_elt_block (struct block *b)
236 {
237 union exp_element tmp;
238 tmp.block = b;
239 write_exp_elt (tmp);
240 }
241
242 void
243 write_exp_elt_longcst (LONGEST expelt)
244 {
245 union exp_element tmp;
246
247 tmp.longconst = expelt;
248
249 write_exp_elt (tmp);
250 }
251
252 void
253 write_exp_elt_dblcst (DOUBLEST expelt)
254 {
255 union exp_element tmp;
256
257 tmp.doubleconst = expelt;
258
259 write_exp_elt (tmp);
260 }
261
262 void
263 write_exp_elt_type (struct type *expelt)
264 {
265 union exp_element tmp;
266
267 tmp.type = expelt;
268
269 write_exp_elt (tmp);
270 }
271
272 void
273 write_exp_elt_intern (struct internalvar *expelt)
274 {
275 union exp_element tmp;
276
277 tmp.internalvar = expelt;
278
279 write_exp_elt (tmp);
280 }
281
282 /* Add a string constant to the end of the expression.
283
284 String constants are stored by first writing an expression element
285 that contains the length of the string, then stuffing the string
286 constant itself into however many expression elements are needed
287 to hold it, and then writing another expression element that contains
288 the length of the string. I.E. an expression element at each end of
289 the string records the string length, so you can skip over the
290 expression elements containing the actual string bytes from either
291 end of the string. Note that this also allows gdb to handle
292 strings with embedded null bytes, as is required for some languages.
293
294 Don't be fooled by the fact that the string is null byte terminated,
295 this is strictly for the convenience of debugging gdb itself. Gdb
296 Gdb does not depend up the string being null terminated, since the
297 actual length is recorded in expression elements at each end of the
298 string. The null byte is taken into consideration when computing how
299 many expression elements are required to hold the string constant, of
300 course. */
301
302
303 void
304 write_exp_string (struct stoken str)
305 {
306 register int len = str.length;
307 register int lenelt;
308 register char *strdata;
309
310 /* Compute the number of expression elements required to hold the string
311 (including a null byte terminator), along with one expression element
312 at each end to record the actual string length (not including the
313 null byte terminator). */
314
315 lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
316
317 /* Ensure that we have enough available expression elements to store
318 everything. */
319
320 if ((expout_ptr + lenelt) >= expout_size)
321 {
322 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
323 expout = (struct expression *)
324 xrealloc ((char *) expout, (sizeof (struct expression)
325 + EXP_ELEM_TO_BYTES (expout_size)));
326 }
327
328 /* Write the leading length expression element (which advances the current
329 expression element index), then write the string constant followed by a
330 terminating null byte, and then write the trailing length expression
331 element. */
332
333 write_exp_elt_longcst ((LONGEST) len);
334 strdata = (char *) &expout->elts[expout_ptr];
335 memcpy (strdata, str.ptr, len);
336 *(strdata + len) = '\0';
337 expout_ptr += lenelt - 2;
338 write_exp_elt_longcst ((LONGEST) len);
339 }
340
341 /* Add a bitstring constant to the end of the expression.
342
343 Bitstring constants are stored by first writing an expression element
344 that contains the length of the bitstring (in bits), then stuffing the
345 bitstring constant itself into however many expression elements are
346 needed to hold it, and then writing another expression element that
347 contains the length of the bitstring. I.E. an expression element at
348 each end of the bitstring records the bitstring length, so you can skip
349 over the expression elements containing the actual bitstring bytes from
350 either end of the bitstring. */
351
352 void
353 write_exp_bitstring (struct stoken str)
354 {
355 register int bits = str.length; /* length in bits */
356 register int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
357 register int lenelt;
358 register char *strdata;
359
360 /* Compute the number of expression elements required to hold the bitstring,
361 along with one expression element at each end to record the actual
362 bitstring length in bits. */
363
364 lenelt = 2 + BYTES_TO_EXP_ELEM (len);
365
366 /* Ensure that we have enough available expression elements to store
367 everything. */
368
369 if ((expout_ptr + lenelt) >= expout_size)
370 {
371 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
372 expout = (struct expression *)
373 xrealloc ((char *) expout, (sizeof (struct expression)
374 + EXP_ELEM_TO_BYTES (expout_size)));
375 }
376
377 /* Write the leading length expression element (which advances the current
378 expression element index), then write the bitstring constant, and then
379 write the trailing length expression element. */
380
381 write_exp_elt_longcst ((LONGEST) bits);
382 strdata = (char *) &expout->elts[expout_ptr];
383 memcpy (strdata, str.ptr, len);
384 expout_ptr += lenelt - 2;
385 write_exp_elt_longcst ((LONGEST) bits);
386 }
387
388 /* Add the appropriate elements for a minimal symbol to the end of
389 the expression. The rationale behind passing in text_symbol_type and
390 data_symbol_type was so that Modula-2 could pass in WORD for
391 data_symbol_type. Perhaps it still is useful to have those types vary
392 based on the language, but they no longer have names like "int", so
393 the initial rationale is gone. */
394
395 static struct type *msym_text_symbol_type;
396 static struct type *msym_data_symbol_type;
397 static struct type *msym_unknown_symbol_type;
398
399 void
400 write_exp_msymbol (struct minimal_symbol *msymbol,
401 struct type *text_symbol_type,
402 struct type *data_symbol_type)
403 {
404 CORE_ADDR addr;
405
406 write_exp_elt_opcode (OP_LONG);
407 /* Let's make the type big enough to hold a 64-bit address. */
408 write_exp_elt_type (builtin_type_CORE_ADDR);
409
410 addr = SYMBOL_VALUE_ADDRESS (msymbol);
411 if (overlay_debugging)
412 addr = symbol_overlayed_address (addr, SYMBOL_BFD_SECTION (msymbol));
413 write_exp_elt_longcst ((LONGEST) addr);
414
415 write_exp_elt_opcode (OP_LONG);
416
417 write_exp_elt_opcode (UNOP_MEMVAL);
418 switch (msymbol->type)
419 {
420 case mst_text:
421 case mst_file_text:
422 case mst_solib_trampoline:
423 write_exp_elt_type (msym_text_symbol_type);
424 break;
425
426 case mst_data:
427 case mst_file_data:
428 case mst_bss:
429 case mst_file_bss:
430 write_exp_elt_type (msym_data_symbol_type);
431 break;
432
433 default:
434 write_exp_elt_type (msym_unknown_symbol_type);
435 break;
436 }
437 write_exp_elt_opcode (UNOP_MEMVAL);
438 }
439 \f
440 /* Recognize tokens that start with '$'. These include:
441
442 $regname A native register name or a "standard
443 register name".
444
445 $variable A convenience variable with a name chosen
446 by the user.
447
448 $digits Value history with index <digits>, starting
449 from the first value which has index 1.
450
451 $$digits Value history with index <digits> relative
452 to the last value. I.E. $$0 is the last
453 value, $$1 is the one previous to that, $$2
454 is the one previous to $$1, etc.
455
456 $ | $0 | $$0 The last value in the value history.
457
458 $$ An abbreviation for the second to the last
459 value in the value history, I.E. $$1
460
461 */
462
463 void
464 write_dollar_variable (struct stoken str)
465 {
466 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
467 and $$digits (equivalent to $<-digits> if you could type that). */
468
469 int negate = 0;
470 int i = 1;
471 /* Double dollar means negate the number and add -1 as well.
472 Thus $$ alone means -1. */
473 if (str.length >= 2 && str.ptr[1] == '$')
474 {
475 negate = 1;
476 i = 2;
477 }
478 if (i == str.length)
479 {
480 /* Just dollars (one or two) */
481 i = -negate;
482 goto handle_last;
483 }
484 /* Is the rest of the token digits? */
485 for (; i < str.length; i++)
486 if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
487 break;
488 if (i == str.length)
489 {
490 i = atoi (str.ptr + 1 + negate);
491 if (negate)
492 i = -i;
493 goto handle_last;
494 }
495
496 /* Handle tokens that refer to machine registers:
497 $ followed by a register name. */
498 i = target_map_name_to_register (str.ptr + 1, str.length - 1);
499 if (i >= 0)
500 goto handle_register;
501
502 if (SYMBOLS_CAN_START_WITH_DOLLAR)
503 {
504 struct symbol *sym = NULL;
505 struct minimal_symbol *msym = NULL;
506
507 /* On HP-UX, certain system routines (millicode) have names beginning
508 with $ or $$, e.g. $$dyncall, which handles inter-space procedure
509 calls on PA-RISC. Check for those, first. */
510
511 /* This code is not enabled on non HP-UX systems, since worst case
512 symbol table lookup performance is awful, to put it mildly. */
513
514 sym = lookup_symbol (copy_name (str), (struct block *) NULL,
515 VAR_NAMESPACE, (int *) NULL, (struct symtab **) NULL);
516 if (sym)
517 {
518 write_exp_elt_opcode (OP_VAR_VALUE);
519 write_exp_elt_block (block_found); /* set by lookup_symbol */
520 write_exp_elt_sym (sym);
521 write_exp_elt_opcode (OP_VAR_VALUE);
522 return;
523 }
524 msym = lookup_minimal_symbol (copy_name (str), NULL, NULL);
525 if (msym)
526 {
527 write_exp_msymbol (msym,
528 lookup_function_type (builtin_type_int),
529 builtin_type_int);
530 return;
531 }
532 }
533
534 /* Any other names starting in $ are debugger internal variables. */
535
536 write_exp_elt_opcode (OP_INTERNALVAR);
537 write_exp_elt_intern (lookup_internalvar (copy_name (str) + 1));
538 write_exp_elt_opcode (OP_INTERNALVAR);
539 return;
540 handle_last:
541 write_exp_elt_opcode (OP_LAST);
542 write_exp_elt_longcst ((LONGEST) i);
543 write_exp_elt_opcode (OP_LAST);
544 return;
545 handle_register:
546 write_exp_elt_opcode (OP_REGISTER);
547 write_exp_elt_longcst (i);
548 write_exp_elt_opcode (OP_REGISTER);
549 return;
550 }
551
552
553 /* Parse a string that is possibly a namespace / nested class
554 specification, i.e., something of the form A::B::C::x. Input
555 (NAME) is the entire string; LEN is the current valid length; the
556 output is a string, TOKEN, which points to the largest recognized
557 prefix which is a series of namespaces or classes. CLASS_PREFIX is
558 another output, which records whether a nested class spec was
559 recognized (= 1) or a fully qualified variable name was found (=
560 0). ARGPTR is side-effected (if non-NULL) to point to beyond the
561 string recognized and consumed by this routine.
562
563 The return value is a pointer to the symbol for the base class or
564 variable if found, or NULL if not found. Callers must check this
565 first -- if NULL, the outputs may not be correct.
566
567 This function is used c-exp.y. This is used specifically to get
568 around HP aCC (and possibly other compilers), which insists on
569 generating names with embedded colons for namespace or nested class
570 members.
571
572 (Argument LEN is currently unused. 1997-08-27)
573
574 Callers must free memory allocated for the output string TOKEN. */
575
576 static const char coloncolon[2] =
577 {':', ':'};
578
579 struct symbol *
580 parse_nested_classes_for_hpacc (char *name, int len, char **token,
581 int *class_prefix, char **argptr)
582 {
583 /* Comment below comes from decode_line_1 which has very similar
584 code, which is called for "break" command parsing. */
585
586 /* We have what looks like a class or namespace
587 scope specification (A::B), possibly with many
588 levels of namespaces or classes (A::B::C::D).
589
590 Some versions of the HP ANSI C++ compiler (as also possibly
591 other compilers) generate class/function/member names with
592 embedded double-colons if they are inside namespaces. To
593 handle this, we loop a few times, considering larger and
594 larger prefixes of the string as though they were single
595 symbols. So, if the initially supplied string is
596 A::B::C::D::foo, we have to look up "A", then "A::B",
597 then "A::B::C", then "A::B::C::D", and finally
598 "A::B::C::D::foo" as single, monolithic symbols, because
599 A, B, C or D may be namespaces.
600
601 Note that namespaces can nest only inside other
602 namespaces, and not inside classes. So we need only
603 consider *prefixes* of the string; there is no need to look up
604 "B::C" separately as a symbol in the previous example. */
605
606 register char *p;
607 char *start, *end;
608 char *prefix = NULL;
609 char *tmp;
610 struct symbol *sym_class = NULL;
611 struct symbol *sym_var = NULL;
612 struct type *t;
613 int prefix_len = 0;
614 int done = 0;
615 char *q;
616
617 /* Check for HP-compiled executable -- in other cases
618 return NULL, and caller must default to standard GDB
619 behaviour. */
620
621 if (!hp_som_som_object_present)
622 return (struct symbol *) NULL;
623
624 p = name;
625
626 /* Skip over whitespace and possible global "::" */
627 while (*p && (*p == ' ' || *p == '\t'))
628 p++;
629 if (p[0] == ':' && p[1] == ':')
630 p += 2;
631 while (*p && (*p == ' ' || *p == '\t'))
632 p++;
633
634 while (1)
635 {
636 /* Get to the end of the next namespace or class spec. */
637 /* If we're looking at some non-token, fail immediately */
638 start = p;
639 if (!(isalpha (*p) || *p == '$' || *p == '_'))
640 return (struct symbol *) NULL;
641 p++;
642 while (*p && (isalnum (*p) || *p == '$' || *p == '_'))
643 p++;
644
645 if (*p == '<')
646 {
647 /* If we have the start of a template specification,
648 scan right ahead to its end */
649 q = find_template_name_end (p);
650 if (q)
651 p = q;
652 }
653
654 end = p;
655
656 /* Skip over "::" and whitespace for next time around */
657 while (*p && (*p == ' ' || *p == '\t'))
658 p++;
659 if (p[0] == ':' && p[1] == ':')
660 p += 2;
661 while (*p && (*p == ' ' || *p == '\t'))
662 p++;
663
664 /* Done with tokens? */
665 if (!*p || !(isalpha (*p) || *p == '$' || *p == '_'))
666 done = 1;
667
668 tmp = (char *) alloca (prefix_len + end - start + 3);
669 if (prefix)
670 {
671 memcpy (tmp, prefix, prefix_len);
672 memcpy (tmp + prefix_len, coloncolon, 2);
673 memcpy (tmp + prefix_len + 2, start, end - start);
674 tmp[prefix_len + 2 + end - start] = '\000';
675 }
676 else
677 {
678 memcpy (tmp, start, end - start);
679 tmp[end - start] = '\000';
680 }
681
682 prefix = tmp;
683 prefix_len = strlen (prefix);
684
685 /* See if the prefix we have now is something we know about */
686
687 if (!done)
688 {
689 /* More tokens to process, so this must be a class/namespace */
690 sym_class = lookup_symbol (prefix, 0, STRUCT_NAMESPACE,
691 0, (struct symtab **) NULL);
692 }
693 else
694 {
695 /* No more tokens, so try as a variable first */
696 sym_var = lookup_symbol (prefix, 0, VAR_NAMESPACE,
697 0, (struct symtab **) NULL);
698 /* If failed, try as class/namespace */
699 if (!sym_var)
700 sym_class = lookup_symbol (prefix, 0, STRUCT_NAMESPACE,
701 0, (struct symtab **) NULL);
702 }
703
704 if (sym_var ||
705 (sym_class &&
706 (t = check_typedef (SYMBOL_TYPE (sym_class)),
707 (TYPE_CODE (t) == TYPE_CODE_STRUCT
708 || TYPE_CODE (t) == TYPE_CODE_UNION))))
709 {
710 /* We found a valid token */
711 *token = (char *) xmalloc (prefix_len + 1);
712 memcpy (*token, prefix, prefix_len);
713 (*token)[prefix_len] = '\000';
714 break;
715 }
716
717 /* No variable or class/namespace found, no more tokens */
718 if (done)
719 return (struct symbol *) NULL;
720 }
721
722 /* Out of loop, so we must have found a valid token */
723 if (sym_var)
724 *class_prefix = 0;
725 else
726 *class_prefix = 1;
727
728 if (argptr)
729 *argptr = done ? p : end;
730
731 return sym_var ? sym_var : sym_class; /* found */
732 }
733
734 char *
735 find_template_name_end (char *p)
736 {
737 int depth = 1;
738 int just_seen_right = 0;
739 int just_seen_colon = 0;
740 int just_seen_space = 0;
741
742 if (!p || (*p != '<'))
743 return 0;
744
745 while (*++p)
746 {
747 switch (*p)
748 {
749 case '\'':
750 case '\"':
751 case '{':
752 case '}':
753 /* In future, may want to allow these?? */
754 return 0;
755 case '<':
756 depth++; /* start nested template */
757 if (just_seen_colon || just_seen_right || just_seen_space)
758 return 0; /* but not after : or :: or > or space */
759 break;
760 case '>':
761 if (just_seen_colon || just_seen_right)
762 return 0; /* end a (nested?) template */
763 just_seen_right = 1; /* but not after : or :: */
764 if (--depth == 0) /* also disallow >>, insist on > > */
765 return ++p; /* if outermost ended, return */
766 break;
767 case ':':
768 if (just_seen_space || (just_seen_colon > 1))
769 return 0; /* nested class spec coming up */
770 just_seen_colon++; /* we allow :: but not :::: */
771 break;
772 case ' ':
773 break;
774 default:
775 if (!((*p >= 'a' && *p <= 'z') || /* allow token chars */
776 (*p >= 'A' && *p <= 'Z') ||
777 (*p >= '0' && *p <= '9') ||
778 (*p == '_') || (*p == ',') || /* commas for template args */
779 (*p == '&') || (*p == '*') || /* pointer and ref types */
780 (*p == '(') || (*p == ')') || /* function types */
781 (*p == '[') || (*p == ']'))) /* array types */
782 return 0;
783 }
784 if (*p != ' ')
785 just_seen_space = 0;
786 if (*p != ':')
787 just_seen_colon = 0;
788 if (*p != '>')
789 just_seen_right = 0;
790 }
791 return 0;
792 }
793 \f
794
795
796 /* Return a null-terminated temporary copy of the name
797 of a string token. */
798
799 char *
800 copy_name (struct stoken token)
801 {
802 memcpy (namecopy, token.ptr, token.length);
803 namecopy[token.length] = 0;
804 return namecopy;
805 }
806 \f
807 /* Reverse an expression from suffix form (in which it is constructed)
808 to prefix form (in which we can conveniently print or execute it). */
809
810 static void
811 prefixify_expression (register struct expression *expr)
812 {
813 register int len =
814 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
815 register struct expression *temp;
816 register int inpos = expr->nelts, outpos = 0;
817
818 temp = (struct expression *) alloca (len);
819
820 /* Copy the original expression into temp. */
821 memcpy (temp, expr, len);
822
823 prefixify_subexp (temp, expr, inpos, outpos);
824 }
825
826 /* Return the number of exp_elements in the subexpression of EXPR
827 whose last exp_element is at index ENDPOS - 1 in EXPR. */
828
829 int
830 length_of_subexp (register struct expression *expr, register int endpos)
831 {
832 register int oplen = 1;
833 register int args = 0;
834 register int i;
835
836 if (endpos < 1)
837 error ("?error in length_of_subexp");
838
839 i = (int) expr->elts[endpos - 1].opcode;
840
841 switch (i)
842 {
843 /* C++ */
844 case OP_SCOPE:
845 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
846 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
847 break;
848
849 case OP_LONG:
850 case OP_DOUBLE:
851 case OP_VAR_VALUE:
852 oplen = 4;
853 break;
854
855 case OP_TYPE:
856 case OP_BOOL:
857 case OP_LAST:
858 case OP_REGISTER:
859 case OP_INTERNALVAR:
860 oplen = 3;
861 break;
862
863 case OP_COMPLEX:
864 oplen = 1;
865 args = 2;
866 break;
867
868 case OP_FUNCALL:
869 case OP_F77_UNDETERMINED_ARGLIST:
870 oplen = 3;
871 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
872 break;
873
874 case UNOP_MAX:
875 case UNOP_MIN:
876 oplen = 3;
877 break;
878
879 case BINOP_VAL:
880 case UNOP_CAST:
881 case UNOP_MEMVAL:
882 oplen = 3;
883 args = 1;
884 break;
885
886 case UNOP_ABS:
887 case UNOP_CAP:
888 case UNOP_CHR:
889 case UNOP_FLOAT:
890 case UNOP_HIGH:
891 case UNOP_ODD:
892 case UNOP_ORD:
893 case UNOP_TRUNC:
894 oplen = 1;
895 args = 1;
896 break;
897
898 case OP_LABELED:
899 case STRUCTOP_STRUCT:
900 case STRUCTOP_PTR:
901 args = 1;
902 /* fall through */
903 case OP_M2_STRING:
904 case OP_STRING:
905 case OP_NAME:
906 case OP_EXPRSTRING:
907 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
908 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
909 break;
910
911 case OP_BITSTRING:
912 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
913 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
914 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
915 break;
916
917 case OP_ARRAY:
918 oplen = 4;
919 args = longest_to_int (expr->elts[endpos - 2].longconst);
920 args -= longest_to_int (expr->elts[endpos - 3].longconst);
921 args += 1;
922 break;
923
924 case TERNOP_COND:
925 case TERNOP_SLICE:
926 case TERNOP_SLICE_COUNT:
927 args = 3;
928 break;
929
930 /* Modula-2 */
931 case MULTI_SUBSCRIPT:
932 oplen = 3;
933 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
934 break;
935
936 case BINOP_ASSIGN_MODIFY:
937 oplen = 3;
938 args = 2;
939 break;
940
941 /* C++ */
942 case OP_THIS:
943 oplen = 2;
944 break;
945
946 default:
947 args = 1 + (i < (int) BINOP_END);
948 }
949
950 while (args > 0)
951 {
952 oplen += length_of_subexp (expr, endpos - oplen);
953 args--;
954 }
955
956 return oplen;
957 }
958
959 /* Copy the subexpression ending just before index INEND in INEXPR
960 into OUTEXPR, starting at index OUTBEG.
961 In the process, convert it from suffix to prefix form. */
962
963 static void
964 prefixify_subexp (register struct expression *inexpr,
965 struct expression *outexpr, register int inend, int outbeg)
966 {
967 register int oplen = 1;
968 register int args = 0;
969 register int i;
970 int *arglens;
971 enum exp_opcode opcode;
972
973 /* Compute how long the last operation is (in OPLEN),
974 and also how many preceding subexpressions serve as
975 arguments for it (in ARGS). */
976
977 opcode = inexpr->elts[inend - 1].opcode;
978 switch (opcode)
979 {
980 /* C++ */
981 case OP_SCOPE:
982 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
983 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
984 break;
985
986 case OP_LONG:
987 case OP_DOUBLE:
988 case OP_VAR_VALUE:
989 oplen = 4;
990 break;
991
992 case OP_TYPE:
993 case OP_BOOL:
994 case OP_LAST:
995 case OP_REGISTER:
996 case OP_INTERNALVAR:
997 oplen = 3;
998 break;
999
1000 case OP_COMPLEX:
1001 oplen = 1;
1002 args = 2;
1003 break;
1004
1005 case OP_FUNCALL:
1006 case OP_F77_UNDETERMINED_ARGLIST:
1007 oplen = 3;
1008 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
1009 break;
1010
1011 case UNOP_MIN:
1012 case UNOP_MAX:
1013 oplen = 3;
1014 break;
1015
1016 case UNOP_CAST:
1017 case UNOP_MEMVAL:
1018 oplen = 3;
1019 args = 1;
1020 break;
1021
1022 case UNOP_ABS:
1023 case UNOP_CAP:
1024 case UNOP_CHR:
1025 case UNOP_FLOAT:
1026 case UNOP_HIGH:
1027 case UNOP_ODD:
1028 case UNOP_ORD:
1029 case UNOP_TRUNC:
1030 oplen = 1;
1031 args = 1;
1032 break;
1033
1034 case STRUCTOP_STRUCT:
1035 case STRUCTOP_PTR:
1036 case OP_LABELED:
1037 args = 1;
1038 /* fall through */
1039 case OP_M2_STRING:
1040 case OP_STRING:
1041 case OP_NAME:
1042 case OP_EXPRSTRING:
1043 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
1044 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
1045 break;
1046
1047 case OP_BITSTRING:
1048 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
1049 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
1050 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
1051 break;
1052
1053 case OP_ARRAY:
1054 oplen = 4;
1055 args = longest_to_int (inexpr->elts[inend - 2].longconst);
1056 args -= longest_to_int (inexpr->elts[inend - 3].longconst);
1057 args += 1;
1058 break;
1059
1060 case TERNOP_COND:
1061 case TERNOP_SLICE:
1062 case TERNOP_SLICE_COUNT:
1063 args = 3;
1064 break;
1065
1066 case BINOP_ASSIGN_MODIFY:
1067 oplen = 3;
1068 args = 2;
1069 break;
1070
1071 /* Modula-2 */
1072 case MULTI_SUBSCRIPT:
1073 oplen = 3;
1074 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
1075 break;
1076
1077 /* C++ */
1078 case OP_THIS:
1079 oplen = 2;
1080 break;
1081
1082 default:
1083 args = 1 + ((int) opcode < (int) BINOP_END);
1084 }
1085
1086 /* Copy the final operator itself, from the end of the input
1087 to the beginning of the output. */
1088 inend -= oplen;
1089 memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
1090 EXP_ELEM_TO_BYTES (oplen));
1091 outbeg += oplen;
1092
1093 /* Find the lengths of the arg subexpressions. */
1094 arglens = (int *) alloca (args * sizeof (int));
1095 for (i = args - 1; i >= 0; i--)
1096 {
1097 oplen = length_of_subexp (inexpr, inend);
1098 arglens[i] = oplen;
1099 inend -= oplen;
1100 }
1101
1102 /* Now copy each subexpression, preserving the order of
1103 the subexpressions, but prefixifying each one.
1104 In this loop, inend starts at the beginning of
1105 the expression this level is working on
1106 and marches forward over the arguments.
1107 outbeg does similarly in the output. */
1108 for (i = 0; i < args; i++)
1109 {
1110 oplen = arglens[i];
1111 inend += oplen;
1112 prefixify_subexp (inexpr, outexpr, inend, outbeg);
1113 outbeg += oplen;
1114 }
1115 }
1116 \f
1117 /* This page contains the two entry points to this file. */
1118
1119 /* Read an expression from the string *STRINGPTR points to,
1120 parse it, and return a pointer to a struct expression that we malloc.
1121 Use block BLOCK as the lexical context for variable names;
1122 if BLOCK is zero, use the block of the selected stack frame.
1123 Meanwhile, advance *STRINGPTR to point after the expression,
1124 at the first nonwhite character that is not part of the expression
1125 (possibly a null character).
1126
1127 If COMMA is nonzero, stop if a comma is reached. */
1128
1129 struct expression *
1130 parse_exp_1 (char **stringptr, struct block *block, int comma)
1131 {
1132 struct cleanup *old_chain;
1133
1134 lexptr = *stringptr;
1135
1136 paren_depth = 0;
1137 type_stack_depth = 0;
1138
1139 comma_terminates = comma;
1140
1141 if (lexptr == 0 || *lexptr == 0)
1142 error_no_arg ("expression to compute");
1143
1144 old_chain = make_cleanup (free_funcalls, 0 /*ignore*/);
1145 funcall_chain = 0;
1146
1147 expression_context_block = block ? block : get_selected_block (0);
1148
1149 namecopy = (char *) alloca (strlen (lexptr) + 1);
1150 expout_size = 10;
1151 expout_ptr = 0;
1152 expout = (struct expression *)
1153 xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
1154 expout->language_defn = current_language;
1155 make_cleanup (free_current_contents, &expout);
1156
1157 if (current_language->la_parser ())
1158 current_language->la_error (NULL);
1159
1160 discard_cleanups (old_chain);
1161
1162 /* Record the actual number of expression elements, and then
1163 reallocate the expression memory so that we free up any
1164 excess elements. */
1165
1166 expout->nelts = expout_ptr;
1167 expout = (struct expression *)
1168 xrealloc ((char *) expout,
1169 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
1170
1171 /* Convert expression from postfix form as generated by yacc
1172 parser, to a prefix form. */
1173
1174 if (expressiondebug)
1175 dump_prefix_expression (expout, gdb_stdlog,
1176 "before conversion to prefix form");
1177
1178 prefixify_expression (expout);
1179
1180 if (expressiondebug)
1181 dump_postfix_expression (expout, gdb_stdlog,
1182 "after conversion to prefix form");
1183
1184 *stringptr = lexptr;
1185 return expout;
1186 }
1187
1188 /* Parse STRING as an expression, and complain if this fails
1189 to use up all of the contents of STRING. */
1190
1191 struct expression *
1192 parse_expression (char *string)
1193 {
1194 register struct expression *exp;
1195 exp = parse_exp_1 (&string, 0, 0);
1196 if (*string)
1197 error ("Junk after end of expression.");
1198 return exp;
1199 }
1200 \f
1201 /* Stuff for maintaining a stack of types. Currently just used by C, but
1202 probably useful for any language which declares its types "backwards". */
1203
1204 static void
1205 check_type_stack_depth (void)
1206 {
1207 if (type_stack_depth == type_stack_size)
1208 {
1209 type_stack_size *= 2;
1210 type_stack = (union type_stack_elt *)
1211 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
1212 }
1213 }
1214
1215 void
1216 push_type (enum type_pieces tp)
1217 {
1218 check_type_stack_depth ();
1219 type_stack[type_stack_depth++].piece = tp;
1220 }
1221
1222 void
1223 push_type_int (int n)
1224 {
1225 check_type_stack_depth ();
1226 type_stack[type_stack_depth++].int_val = n;
1227 }
1228
1229 void
1230 push_type_address_space (char *string)
1231 {
1232 push_type_int (address_space_name_to_int (string));
1233 }
1234
1235 enum type_pieces
1236 pop_type (void)
1237 {
1238 if (type_stack_depth)
1239 return type_stack[--type_stack_depth].piece;
1240 return tp_end;
1241 }
1242
1243 int
1244 pop_type_int (void)
1245 {
1246 if (type_stack_depth)
1247 return type_stack[--type_stack_depth].int_val;
1248 /* "Can't happen". */
1249 return 0;
1250 }
1251
1252 /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
1253 as modified by all the stuff on the stack. */
1254 struct type *
1255 follow_types (struct type *follow_type)
1256 {
1257 int done = 0;
1258 int make_const = 0;
1259 int make_volatile = 0;
1260 int make_addr_space = 0;
1261 int array_size;
1262 struct type *range_type;
1263
1264 while (!done)
1265 switch (pop_type ())
1266 {
1267 case tp_end:
1268 done = 1;
1269 if (make_const)
1270 follow_type = make_cv_type (make_const,
1271 TYPE_VOLATILE (follow_type),
1272 follow_type, 0);
1273 if (make_volatile)
1274 follow_type = make_cv_type (TYPE_CONST (follow_type),
1275 make_volatile,
1276 follow_type, 0);
1277 if (make_addr_space)
1278 follow_type = make_type_with_address_space (follow_type,
1279 make_addr_space);
1280 make_const = make_volatile = 0;
1281 make_addr_space = 0;
1282 break;
1283 case tp_const:
1284 make_const = 1;
1285 break;
1286 case tp_volatile:
1287 make_volatile = 1;
1288 break;
1289 case tp_space_identifier:
1290 make_addr_space = pop_type_int ();
1291 break;
1292 case tp_pointer:
1293 follow_type = lookup_pointer_type (follow_type);
1294 if (make_const)
1295 follow_type = make_cv_type (make_const,
1296 TYPE_VOLATILE (follow_type),
1297 follow_type, 0);
1298 if (make_volatile)
1299 follow_type = make_cv_type (TYPE_CONST (follow_type),
1300 make_volatile,
1301 follow_type, 0);
1302 if (make_addr_space)
1303 follow_type = make_type_with_address_space (follow_type,
1304 make_addr_space);
1305 make_const = make_volatile = 0;
1306 make_addr_space = 0;
1307 break;
1308 case tp_reference:
1309 follow_type = lookup_reference_type (follow_type);
1310 if (make_const)
1311 follow_type = make_cv_type (make_const,
1312 TYPE_VOLATILE (follow_type),
1313 follow_type, 0);
1314 if (make_volatile)
1315 follow_type = make_cv_type (TYPE_CONST (follow_type),
1316 make_volatile,
1317 follow_type, 0);
1318 if (make_addr_space)
1319 follow_type = make_type_with_address_space (follow_type,
1320 make_addr_space);
1321 make_const = make_volatile = 0;
1322 make_addr_space = 0;
1323 break;
1324 case tp_array:
1325 array_size = pop_type_int ();
1326 /* FIXME-type-allocation: need a way to free this type when we are
1327 done with it. */
1328 range_type =
1329 create_range_type ((struct type *) NULL,
1330 builtin_type_int, 0,
1331 array_size >= 0 ? array_size - 1 : 0);
1332 follow_type =
1333 create_array_type ((struct type *) NULL,
1334 follow_type, range_type);
1335 if (array_size < 0)
1336 TYPE_ARRAY_UPPER_BOUND_TYPE (follow_type)
1337 = BOUND_CANNOT_BE_DETERMINED;
1338 break;
1339 case tp_function:
1340 /* FIXME-type-allocation: need a way to free this type when we are
1341 done with it. */
1342 follow_type = lookup_function_type (follow_type);
1343 break;
1344 }
1345 return follow_type;
1346 }
1347 \f
1348 static void build_parse (void);
1349 static void
1350 build_parse (void)
1351 {
1352 int i;
1353
1354 msym_text_symbol_type =
1355 init_type (TYPE_CODE_FUNC, 1, 0, "<text variable, no debug info>", NULL);
1356 TYPE_TARGET_TYPE (msym_text_symbol_type) = builtin_type_int;
1357 msym_data_symbol_type =
1358 init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
1359 "<data variable, no debug info>", NULL);
1360 msym_unknown_symbol_type =
1361 init_type (TYPE_CODE_INT, 1, 0,
1362 "<variable (not text or data), no debug info>",
1363 NULL);
1364
1365 /* create the std_regs table */
1366
1367 num_std_regs = 0;
1368 #ifdef PC_REGNUM
1369 if (PC_REGNUM >= 0)
1370 num_std_regs++;
1371 #endif
1372 #ifdef FP_REGNUM
1373 if (FP_REGNUM >= 0)
1374 num_std_regs++;
1375 #endif
1376 #ifdef SP_REGNUM
1377 if (SP_REGNUM >= 0)
1378 num_std_regs++;
1379 #endif
1380 #ifdef PS_REGNUM
1381 if (PS_REGNUM >= 0)
1382 num_std_regs++;
1383 #endif
1384 /* create an empty table */
1385 std_regs = xmalloc ((num_std_regs + 1) * sizeof *std_regs);
1386 i = 0;
1387 /* fill it in */
1388 #ifdef PC_REGNUM
1389 if (PC_REGNUM >= 0)
1390 {
1391 std_regs[i].name = "pc";
1392 std_regs[i].regnum = PC_REGNUM;
1393 i++;
1394 }
1395 #endif
1396 #ifdef FP_REGNUM
1397 if (FP_REGNUM >= 0)
1398 {
1399 std_regs[i].name = "fp";
1400 std_regs[i].regnum = FP_REGNUM;
1401 i++;
1402 }
1403 #endif
1404 #ifdef SP_REGNUM
1405 if (SP_REGNUM >= 0)
1406 {
1407 std_regs[i].name = "sp";
1408 std_regs[i].regnum = SP_REGNUM;
1409 i++;
1410 }
1411 #endif
1412 #ifdef PS_REGNUM
1413 if (PS_REGNUM >= 0)
1414 {
1415 std_regs[i].name = "ps";
1416 std_regs[i].regnum = PS_REGNUM;
1417 i++;
1418 }
1419 #endif
1420 memset (&std_regs[i], 0, sizeof (std_regs[i]));
1421 }
1422
1423 void
1424 _initialize_parse (void)
1425 {
1426 type_stack_size = 80;
1427 type_stack_depth = 0;
1428 type_stack = (union type_stack_elt *)
1429 xmalloc (type_stack_size * sizeof (*type_stack));
1430
1431 build_parse ();
1432
1433 /* FIXME - For the moment, handle types by swapping them in and out.
1434 Should be using the per-architecture data-pointer and a large
1435 struct. */
1436 register_gdbarch_swap (&msym_text_symbol_type, sizeof (msym_text_symbol_type), NULL);
1437 register_gdbarch_swap (&msym_data_symbol_type, sizeof (msym_data_symbol_type), NULL);
1438 register_gdbarch_swap (&msym_unknown_symbol_type, sizeof (msym_unknown_symbol_type), NULL);
1439
1440 register_gdbarch_swap (&num_std_regs, sizeof (std_regs), NULL);
1441 register_gdbarch_swap (&std_regs, sizeof (std_regs), NULL);
1442 register_gdbarch_swap (NULL, 0, build_parse);
1443
1444 add_show_from_set (
1445 add_set_cmd ("expression", class_maintenance, var_zinteger,
1446 (char *) &expressiondebug,
1447 "Set expression debugging.\n\
1448 When non-zero, the internal representation of expressions will be printed.",
1449 &setdebuglist),
1450 &showdebuglist);
1451 }
This page took 0.058429 seconds and 4 git commands to generate.