Tue Nov 5 10:21:02 1996 Michael Snyder <msnyder@cleaver.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / parse.c
1 /* Parse expressions for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991, 1994 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
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 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 "symtab.h"
34 #include "gdbtypes.h"
35 #include "frame.h"
36 #include "expression.h"
37 #include "value.h"
38 #include "command.h"
39 #include "language.h"
40 #include "parser-defs.h"
41 \f
42 /* Global variables declared in parser-defs.h (and commented there). */
43 struct expression *expout;
44 int expout_size;
45 int expout_ptr;
46 struct block *expression_context_block;
47 struct block *innermost_block;
48 int arglist_len;
49 union type_stack_elt *type_stack;
50 int type_stack_depth, type_stack_size;
51 char *lexptr;
52 char *namecopy;
53 int paren_depth;
54 int comma_terminates;
55 \f
56 static void
57 free_funcalls PARAMS ((void));
58
59 static void
60 prefixify_expression PARAMS ((struct expression *));
61
62 static int
63 length_of_subexp PARAMS ((struct expression *, int));
64
65 static void
66 prefixify_subexp PARAMS ((struct expression *, struct expression *, int, int));
67
68 /* Data structure for saving values of arglist_len for function calls whose
69 arguments contain other function calls. */
70
71 struct funcall
72 {
73 struct funcall *next;
74 int arglist_len;
75 };
76
77 static struct funcall *funcall_chain;
78
79 /* Assign machine-independent names to certain registers
80 (unless overridden by the REGISTER_NAMES table) */
81
82 #ifdef NO_STD_REGS
83 unsigned num_std_regs = 0;
84 struct std_regs std_regs[1];
85 #else
86 struct std_regs std_regs[] = {
87
88 #ifdef PC_REGNUM
89 { "pc", PC_REGNUM },
90 #endif
91 #ifdef FP_REGNUM
92 { "fp", FP_REGNUM },
93 #endif
94 #ifdef SP_REGNUM
95 { "sp", SP_REGNUM },
96 #endif
97 #ifdef PS_REGNUM
98 { "ps", PS_REGNUM },
99 #endif
100
101 };
102
103 unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]);
104
105 #endif
106
107
108 /* Begin counting arguments for a function call,
109 saving the data about any containing call. */
110
111 void
112 start_arglist ()
113 {
114 register struct funcall *new;
115
116 new = (struct funcall *) xmalloc (sizeof (struct funcall));
117 new->next = funcall_chain;
118 new->arglist_len = arglist_len;
119 arglist_len = 0;
120 funcall_chain = new;
121 }
122
123 /* Return the number of arguments in a function call just terminated,
124 and restore the data for the containing function call. */
125
126 int
127 end_arglist ()
128 {
129 register int val = arglist_len;
130 register struct funcall *call = funcall_chain;
131 funcall_chain = call->next;
132 arglist_len = call->arglist_len;
133 free ((PTR)call);
134 return val;
135 }
136
137 /* Free everything in the funcall chain.
138 Used when there is an error inside parsing. */
139
140 static void
141 free_funcalls ()
142 {
143 register struct funcall *call, *next;
144
145 for (call = funcall_chain; call; call = next)
146 {
147 next = call->next;
148 free ((PTR)call);
149 }
150 }
151 \f
152 /* This page contains the functions for adding data to the struct expression
153 being constructed. */
154
155 /* Add one element to the end of the expression. */
156
157 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
158 a register through here */
159
160 void
161 write_exp_elt (expelt)
162 union exp_element expelt;
163 {
164 if (expout_ptr >= expout_size)
165 {
166 expout_size *= 2;
167 expout = (struct expression *)
168 xrealloc ((char *) expout, sizeof (struct expression)
169 + EXP_ELEM_TO_BYTES (expout_size));
170 }
171 expout->elts[expout_ptr++] = expelt;
172 }
173
174 void
175 write_exp_elt_opcode (expelt)
176 enum exp_opcode expelt;
177 {
178 union exp_element tmp;
179
180 tmp.opcode = expelt;
181
182 write_exp_elt (tmp);
183 }
184
185 void
186 write_exp_elt_sym (expelt)
187 struct symbol *expelt;
188 {
189 union exp_element tmp;
190
191 tmp.symbol = expelt;
192
193 write_exp_elt (tmp);
194 }
195
196 void
197 write_exp_elt_block (b)
198 struct block *b;
199 {
200 union exp_element tmp;
201 tmp.block = b;
202 write_exp_elt (tmp);
203 }
204
205 void
206 write_exp_elt_longcst (expelt)
207 LONGEST expelt;
208 {
209 union exp_element tmp;
210
211 tmp.longconst = expelt;
212
213 write_exp_elt (tmp);
214 }
215
216 void
217 write_exp_elt_dblcst (expelt)
218 DOUBLEST expelt;
219 {
220 union exp_element tmp;
221
222 tmp.doubleconst = expelt;
223
224 write_exp_elt (tmp);
225 }
226
227 void
228 write_exp_elt_type (expelt)
229 struct type *expelt;
230 {
231 union exp_element tmp;
232
233 tmp.type = expelt;
234
235 write_exp_elt (tmp);
236 }
237
238 void
239 write_exp_elt_intern (expelt)
240 struct internalvar *expelt;
241 {
242 union exp_element tmp;
243
244 tmp.internalvar = expelt;
245
246 write_exp_elt (tmp);
247 }
248
249 /* Add a string constant to the end of the expression.
250
251 String constants are stored by first writing an expression element
252 that contains the length of the string, then stuffing the string
253 constant itself into however many expression elements are needed
254 to hold it, and then writing another expression element that contains
255 the length of the string. I.E. an expression element at each end of
256 the string records the string length, so you can skip over the
257 expression elements containing the actual string bytes from either
258 end of the string. Note that this also allows gdb to handle
259 strings with embedded null bytes, as is required for some languages.
260
261 Don't be fooled by the fact that the string is null byte terminated,
262 this is strictly for the convenience of debugging gdb itself. Gdb
263 Gdb does not depend up the string being null terminated, since the
264 actual length is recorded in expression elements at each end of the
265 string. The null byte is taken into consideration when computing how
266 many expression elements are required to hold the string constant, of
267 course. */
268
269
270 void
271 write_exp_string (str)
272 struct stoken str;
273 {
274 register int len = str.length;
275 register int lenelt;
276 register char *strdata;
277
278 /* Compute the number of expression elements required to hold the string
279 (including a null byte terminator), along with one expression element
280 at each end to record the actual string length (not including the
281 null byte terminator). */
282
283 lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
284
285 /* Ensure that we have enough available expression elements to store
286 everything. */
287
288 if ((expout_ptr + lenelt) >= expout_size)
289 {
290 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
291 expout = (struct expression *)
292 xrealloc ((char *) expout, (sizeof (struct expression)
293 + EXP_ELEM_TO_BYTES (expout_size)));
294 }
295
296 /* Write the leading length expression element (which advances the current
297 expression element index), then write the string constant followed by a
298 terminating null byte, and then write the trailing length expression
299 element. */
300
301 write_exp_elt_longcst ((LONGEST) len);
302 strdata = (char *) &expout->elts[expout_ptr];
303 memcpy (strdata, str.ptr, len);
304 *(strdata + len) = '\0';
305 expout_ptr += lenelt - 2;
306 write_exp_elt_longcst ((LONGEST) len);
307 }
308
309 /* Add a bitstring constant to the end of the expression.
310
311 Bitstring constants are stored by first writing an expression element
312 that contains the length of the bitstring (in bits), then stuffing the
313 bitstring constant itself into however many expression elements are
314 needed to hold it, and then writing another expression element that
315 contains the length of the bitstring. I.E. an expression element at
316 each end of the bitstring records the bitstring length, so you can skip
317 over the expression elements containing the actual bitstring bytes from
318 either end of the bitstring. */
319
320 void
321 write_exp_bitstring (str)
322 struct stoken str;
323 {
324 register int bits = str.length; /* length in bits */
325 register int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
326 register int lenelt;
327 register char *strdata;
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
364 static struct type *msym_text_symbol_type;
365 static struct type *msym_data_symbol_type;
366 static struct type *msym_unknown_symbol_type;
367
368 void
369 write_exp_msymbol (msymbol, text_symbol_type, data_symbol_type)
370 struct minimal_symbol *msymbol;
371 struct type *text_symbol_type;
372 struct type *data_symbol_type;
373 {
374 write_exp_elt_opcode (OP_LONG);
375 write_exp_elt_type (lookup_pointer_type (builtin_type_void));
376 write_exp_elt_longcst ((LONGEST) SYMBOL_VALUE_ADDRESS (msymbol));
377 write_exp_elt_opcode (OP_LONG);
378
379 write_exp_elt_opcode (UNOP_MEMVAL);
380 switch (msymbol -> type)
381 {
382 case mst_text:
383 case mst_file_text:
384 case mst_solib_trampoline:
385 write_exp_elt_type (msym_text_symbol_type);
386 break;
387
388 case mst_data:
389 case mst_file_data:
390 case mst_bss:
391 case mst_file_bss:
392 write_exp_elt_type (msym_data_symbol_type);
393 break;
394
395 default:
396 write_exp_elt_type (msym_unknown_symbol_type);
397 break;
398 }
399 write_exp_elt_opcode (UNOP_MEMVAL);
400 }
401 \f
402 /* Recognize tokens that start with '$'. These include:
403
404 $regname A native register name or a "standard
405 register name".
406
407 $variable A convenience variable with a name chosen
408 by the user.
409
410 $digits Value history with index <digits>, starting
411 from the first value which has index 1.
412
413 $$digits Value history with index <digits> relative
414 to the last value. I.E. $$0 is the last
415 value, $$1 is the one previous to that, $$2
416 is the one previous to $$1, etc.
417
418 $ | $0 | $$0 The last value in the value history.
419
420 $$ An abbreviation for the second to the last
421 value in the value history, I.E. $$1
422
423 */
424
425 void
426 write_dollar_variable (str)
427 struct stoken str;
428 {
429 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
430 and $$digits (equivalent to $<-digits> if you could type that). */
431
432 int negate = 0;
433 int i = 1;
434 /* Double dollar means negate the number and add -1 as well.
435 Thus $$ alone means -1. */
436 if (str.length >= 2 && str.ptr[1] == '$')
437 {
438 negate = 1;
439 i = 2;
440 }
441 if (i == str.length)
442 {
443 /* Just dollars (one or two) */
444 i = - negate;
445 goto handle_last;
446 }
447 /* Is the rest of the token digits? */
448 for (; i < str.length; i++)
449 if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
450 break;
451 if (i == str.length)
452 {
453 i = atoi (str.ptr + 1 + negate);
454 if (negate)
455 i = - i;
456 goto handle_last;
457 }
458
459 /* Handle tokens that refer to machine registers:
460 $ followed by a register name. */
461 for (i = 0; i < NUM_REGS; i++)
462 if (reg_names[i] && str.length - 1 == strlen (reg_names[i])
463 && STREQN (str.ptr + 1, reg_names[i], str.length - 1))
464 {
465 goto handle_register;
466 }
467 for (i = 0; i < num_std_regs; i++)
468 if (std_regs[i].name && str.length - 1 == strlen (std_regs[i].name)
469 && STREQN (str.ptr + 1, std_regs[i].name, str.length - 1))
470 {
471 i = std_regs[i].regnum;
472 goto handle_register;
473 }
474
475 /* Any other names starting in $ are debugger internal variables. */
476
477 write_exp_elt_opcode (OP_INTERNALVAR);
478 write_exp_elt_intern (lookup_internalvar (copy_name (str) + 1));
479 write_exp_elt_opcode (OP_INTERNALVAR);
480 return;
481 handle_last:
482 write_exp_elt_opcode (OP_LAST);
483 write_exp_elt_longcst ((LONGEST) i);
484 write_exp_elt_opcode (OP_LAST);
485 return;
486 handle_register:
487 write_exp_elt_opcode (OP_REGISTER);
488 write_exp_elt_longcst (i);
489 write_exp_elt_opcode (OP_REGISTER);
490 return;
491 }
492 \f
493 /* Return a null-terminated temporary copy of the name
494 of a string token. */
495
496 char *
497 copy_name (token)
498 struct stoken token;
499 {
500 memcpy (namecopy, token.ptr, token.length);
501 namecopy[token.length] = 0;
502 return namecopy;
503 }
504 \f
505 /* Reverse an expression from suffix form (in which it is constructed)
506 to prefix form (in which we can conveniently print or execute it). */
507
508 static void
509 prefixify_expression (expr)
510 register struct expression *expr;
511 {
512 register int len =
513 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
514 register struct expression *temp;
515 register int inpos = expr->nelts, outpos = 0;
516
517 temp = (struct expression *) alloca (len);
518
519 /* Copy the original expression into temp. */
520 memcpy (temp, expr, len);
521
522 prefixify_subexp (temp, expr, inpos, outpos);
523 }
524
525 /* Return the number of exp_elements in the subexpression of EXPR
526 whose last exp_element is at index ENDPOS - 1 in EXPR. */
527
528 static int
529 length_of_subexp (expr, endpos)
530 register struct expression *expr;
531 register int endpos;
532 {
533 register int oplen = 1;
534 register int args = 0;
535 register int i;
536
537 if (endpos < 1)
538 error ("?error in length_of_subexp");
539
540 i = (int) expr->elts[endpos - 1].opcode;
541
542 switch (i)
543 {
544 /* C++ */
545 case OP_SCOPE:
546 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
547 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
548 break;
549
550 case OP_LONG:
551 case OP_DOUBLE:
552 case OP_VAR_VALUE:
553 oplen = 4;
554 break;
555
556 case OP_TYPE:
557 case OP_BOOL:
558 case OP_LAST:
559 case OP_REGISTER:
560 case OP_INTERNALVAR:
561 oplen = 3;
562 break;
563
564 case OP_COMPLEX:
565 oplen = 1;
566 args = 2;
567 break;
568
569 case OP_FUNCALL:
570 case OP_F77_UNDETERMINED_ARGLIST:
571 oplen = 3;
572 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
573 break;
574
575 case UNOP_MAX:
576 case UNOP_MIN:
577 oplen = 3;
578 break;
579
580 case BINOP_VAL:
581 case UNOP_CAST:
582 case UNOP_MEMVAL:
583 oplen = 3;
584 args = 1;
585 break;
586
587 case UNOP_ABS:
588 case UNOP_CAP:
589 case UNOP_CHR:
590 case UNOP_FLOAT:
591 case UNOP_HIGH:
592 case UNOP_ODD:
593 case UNOP_ORD:
594 case UNOP_TRUNC:
595 oplen = 1;
596 args = 1;
597 break;
598
599 case OP_LABELED:
600 case STRUCTOP_STRUCT:
601 case STRUCTOP_PTR:
602 /* start-sanitize-gm */
603 #ifdef GENERAL_MAGIC
604 case STRUCTOP_FIELD:
605 #endif /* GENERAL_MAGIC */
606 /* end-sanitize-gm */
607 args = 1;
608 /* fall through */
609 case OP_M2_STRING:
610 case OP_STRING:
611 case OP_NAME:
612 case OP_EXPRSTRING:
613 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
614 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
615 break;
616
617 case OP_BITSTRING:
618 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
619 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
620 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
621 break;
622
623 case OP_ARRAY:
624 oplen = 4;
625 args = longest_to_int (expr->elts[endpos - 2].longconst);
626 args -= longest_to_int (expr->elts[endpos - 3].longconst);
627 args += 1;
628 break;
629
630 case TERNOP_COND:
631 case TERNOP_SLICE:
632 case TERNOP_SLICE_COUNT:
633 args = 3;
634 break;
635
636 /* Modula-2 */
637 case MULTI_SUBSCRIPT:
638 oplen = 3;
639 args = 1 + longest_to_int (expr->elts[endpos- 2].longconst);
640 break;
641
642 case BINOP_ASSIGN_MODIFY:
643 oplen = 3;
644 args = 2;
645 break;
646
647 /* C++ */
648 case OP_THIS:
649 oplen = 2;
650 break;
651
652 default:
653 args = 1 + (i < (int) BINOP_END);
654 }
655
656 while (args > 0)
657 {
658 oplen += length_of_subexp (expr, endpos - oplen);
659 args--;
660 }
661
662 return oplen;
663 }
664
665 /* Copy the subexpression ending just before index INEND in INEXPR
666 into OUTEXPR, starting at index OUTBEG.
667 In the process, convert it from suffix to prefix form. */
668
669 static void
670 prefixify_subexp (inexpr, outexpr, inend, outbeg)
671 register struct expression *inexpr;
672 struct expression *outexpr;
673 register int inend;
674 int outbeg;
675 {
676 register int oplen = 1;
677 register int args = 0;
678 register int i;
679 int *arglens;
680 enum exp_opcode opcode;
681
682 /* Compute how long the last operation is (in OPLEN),
683 and also how many preceding subexpressions serve as
684 arguments for it (in ARGS). */
685
686 opcode = inexpr->elts[inend - 1].opcode;
687 switch (opcode)
688 {
689 /* C++ */
690 case OP_SCOPE:
691 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
692 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
693 break;
694
695 case OP_LONG:
696 case OP_DOUBLE:
697 case OP_VAR_VALUE:
698 oplen = 4;
699 break;
700
701 case OP_TYPE:
702 case OP_BOOL:
703 case OP_LAST:
704 case OP_REGISTER:
705 case OP_INTERNALVAR:
706 oplen = 3;
707 break;
708
709 case OP_COMPLEX:
710 oplen = 1;
711 args = 2;
712 break;
713
714 case OP_FUNCALL:
715 case OP_F77_UNDETERMINED_ARGLIST:
716 oplen = 3;
717 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
718 break;
719
720 case UNOP_MIN:
721 case UNOP_MAX:
722 oplen = 3;
723 break;
724
725 case UNOP_CAST:
726 case UNOP_MEMVAL:
727 oplen = 3;
728 args = 1;
729 break;
730
731 case UNOP_ABS:
732 case UNOP_CAP:
733 case UNOP_CHR:
734 case UNOP_FLOAT:
735 case UNOP_HIGH:
736 case UNOP_ODD:
737 case UNOP_ORD:
738 case UNOP_TRUNC:
739 oplen=1;
740 args=1;
741 break;
742
743 case STRUCTOP_STRUCT:
744 case STRUCTOP_PTR:
745 case OP_LABELED:
746 args = 1;
747 /* fall through */
748 case OP_M2_STRING:
749 case OP_STRING:
750 case OP_NAME:
751 case OP_EXPRSTRING:
752 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
753 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
754 break;
755
756 case OP_BITSTRING:
757 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
758 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
759 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
760 break;
761
762 case OP_ARRAY:
763 oplen = 4;
764 args = longest_to_int (inexpr->elts[inend - 2].longconst);
765 args -= longest_to_int (inexpr->elts[inend - 3].longconst);
766 args += 1;
767 break;
768
769 case TERNOP_COND:
770 case TERNOP_SLICE:
771 case TERNOP_SLICE_COUNT:
772 args = 3;
773 break;
774
775 case BINOP_ASSIGN_MODIFY:
776 oplen = 3;
777 args = 2;
778 break;
779
780 /* Modula-2 */
781 case MULTI_SUBSCRIPT:
782 oplen = 3;
783 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
784 break;
785
786 /* C++ */
787 case OP_THIS:
788 oplen = 2;
789 break;
790
791 default:
792 args = 1 + ((int) opcode < (int) BINOP_END);
793 }
794
795 /* Copy the final operator itself, from the end of the input
796 to the beginning of the output. */
797 inend -= oplen;
798 memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
799 EXP_ELEM_TO_BYTES (oplen));
800 outbeg += oplen;
801
802 /* Find the lengths of the arg subexpressions. */
803 arglens = (int *) alloca (args * sizeof (int));
804 for (i = args - 1; i >= 0; i--)
805 {
806 oplen = length_of_subexp (inexpr, inend);
807 arglens[i] = oplen;
808 inend -= oplen;
809 }
810
811 /* Now copy each subexpression, preserving the order of
812 the subexpressions, but prefixifying each one.
813 In this loop, inend starts at the beginning of
814 the expression this level is working on
815 and marches forward over the arguments.
816 outbeg does similarly in the output. */
817 for (i = 0; i < args; i++)
818 {
819 oplen = arglens[i];
820 inend += oplen;
821 prefixify_subexp (inexpr, outexpr, inend, outbeg);
822 outbeg += oplen;
823 }
824 }
825 \f
826 /* This page contains the two entry points to this file. */
827
828 /* Read an expression from the string *STRINGPTR points to,
829 parse it, and return a pointer to a struct expression that we malloc.
830 Use block BLOCK as the lexical context for variable names;
831 if BLOCK is zero, use the block of the selected stack frame.
832 Meanwhile, advance *STRINGPTR to point after the expression,
833 at the first nonwhite character that is not part of the expression
834 (possibly a null character).
835
836 If COMMA is nonzero, stop if a comma is reached. */
837
838 struct expression *
839 parse_exp_1 (stringptr, block, comma)
840 char **stringptr;
841 struct block *block;
842 int comma;
843 {
844 struct cleanup *old_chain;
845
846 lexptr = *stringptr;
847
848 paren_depth = 0;
849 type_stack_depth = 0;
850
851 comma_terminates = comma;
852
853 if (lexptr == 0 || *lexptr == 0)
854 error_no_arg ("expression to compute");
855
856 old_chain = make_cleanup (free_funcalls, 0);
857 funcall_chain = 0;
858
859 expression_context_block = block ? block : get_selected_block ();
860
861 namecopy = (char *) alloca (strlen (lexptr) + 1);
862 expout_size = 10;
863 expout_ptr = 0;
864 expout = (struct expression *)
865 xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
866 expout->language_defn = current_language;
867 make_cleanup (free_current_contents, &expout);
868
869 if (current_language->la_parser ())
870 current_language->la_error (NULL);
871
872 discard_cleanups (old_chain);
873
874 /* Record the actual number of expression elements, and then
875 reallocate the expression memory so that we free up any
876 excess elements. */
877
878 expout->nelts = expout_ptr;
879 expout = (struct expression *)
880 xrealloc ((char *) expout,
881 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
882
883 /* Convert expression from postfix form as generated by yacc
884 parser, to a prefix form. */
885
886 DUMP_EXPRESSION (expout, gdb_stdout, "before conversion to prefix form");
887 prefixify_expression (expout);
888 DUMP_EXPRESSION (expout, gdb_stdout, "after conversion to prefix form");
889
890 *stringptr = lexptr;
891 return expout;
892 }
893
894 /* Parse STRING as an expression, and complain if this fails
895 to use up all of the contents of STRING. */
896
897 struct expression *
898 parse_expression (string)
899 char *string;
900 {
901 register struct expression *exp;
902 exp = parse_exp_1 (&string, 0, 0);
903 if (*string)
904 error ("Junk after end of expression.");
905 return exp;
906 }
907 \f
908 /* Stuff for maintaining a stack of types. Currently just used by C, but
909 probably useful for any language which declares its types "backwards". */
910
911 void
912 push_type (tp)
913 enum type_pieces tp;
914 {
915 if (type_stack_depth == type_stack_size)
916 {
917 type_stack_size *= 2;
918 type_stack = (union type_stack_elt *)
919 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
920 }
921 type_stack[type_stack_depth++].piece = tp;
922 }
923
924 void
925 push_type_int (n)
926 int n;
927 {
928 if (type_stack_depth == type_stack_size)
929 {
930 type_stack_size *= 2;
931 type_stack = (union type_stack_elt *)
932 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
933 }
934 type_stack[type_stack_depth++].int_val = n;
935 }
936
937 enum type_pieces
938 pop_type ()
939 {
940 if (type_stack_depth)
941 return type_stack[--type_stack_depth].piece;
942 return tp_end;
943 }
944
945 int
946 pop_type_int ()
947 {
948 if (type_stack_depth)
949 return type_stack[--type_stack_depth].int_val;
950 /* "Can't happen". */
951 return 0;
952 }
953
954 /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
955 as modified by all the stuff on the stack. */
956 struct type *
957 follow_types (follow_type)
958 struct type *follow_type;
959 {
960 int done = 0;
961 int array_size;
962 struct type *range_type;
963
964 while (!done)
965 switch (pop_type ())
966 {
967 case tp_end:
968 done = 1;
969 break;
970 case tp_pointer:
971 follow_type = lookup_pointer_type (follow_type);
972 break;
973 case tp_reference:
974 follow_type = lookup_reference_type (follow_type);
975 break;
976 case tp_array:
977 array_size = pop_type_int ();
978 /* FIXME-type-allocation: need a way to free this type when we are
979 done with it. */
980 range_type =
981 create_range_type ((struct type *) NULL,
982 builtin_type_int, 0,
983 array_size >= 0 ? array_size - 1 : 0);
984 follow_type =
985 create_array_type ((struct type *) NULL,
986 follow_type, range_type);
987 if (array_size < 0)
988 TYPE_ARRAY_UPPER_BOUND_TYPE(follow_type)
989 = BOUND_CANNOT_BE_DETERMINED;
990 break;
991 case tp_function:
992 /* FIXME-type-allocation: need a way to free this type when we are
993 done with it. */
994 follow_type = lookup_function_type (follow_type);
995 break;
996 }
997 return follow_type;
998 }
999 \f
1000 void
1001 _initialize_parse ()
1002 {
1003 type_stack_size = 80;
1004 type_stack_depth = 0;
1005 type_stack = (union type_stack_elt *)
1006 xmalloc (type_stack_size * sizeof (*type_stack));
1007
1008 msym_text_symbol_type =
1009 init_type (TYPE_CODE_FUNC, 1, 0, "<text variable, no debug info>", NULL);
1010 TYPE_TARGET_TYPE (msym_text_symbol_type) = builtin_type_int;
1011 msym_data_symbol_type =
1012 init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
1013 "<data variable, no debug info>", NULL);
1014 msym_unknown_symbol_type =
1015 init_type (TYPE_CODE_INT, 1, 0,
1016 "<variable (not text or data), no debug info>",
1017 NULL);
1018 }
This page took 0.054079 seconds and 4 git commands to generate.