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