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