keep h8300 tests
[deliverable/binutils-gdb.git] / gdb / parse.c
CommitLineData
3d6b6a90 1/* Parse expressions for GDB.
d92f3f08 2 Copyright (C) 1986, 1989, 1990, 1991, 1994 Free Software Foundation, Inc.
3d6b6a90
JG
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., 675 Mass Ave, Cambridge, MA 02139, 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
3d6b6a90 31#include "defs.h"
ba47c66a 32#include <string.h>
3d6b6a90 33#include "symtab.h"
1ab3bf1b 34#include "gdbtypes.h"
3d6b6a90
JG
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"
79448221
JK
41\f
42/* Global variables declared in parser-defs.h (and commented there). */
43struct expression *expout;
44int expout_size;
45int expout_ptr;
46struct block *expression_context_block;
47struct block *innermost_block;
79448221
JK
48int arglist_len;
49union type_stack_elt *type_stack;
50int type_stack_depth, type_stack_size;
51char *lexptr;
52char *namecopy;
53int paren_depth;
54int comma_terminates;
55\f
9da75ad3
FF
56static void
57free_funcalls PARAMS ((void));
58
1ab3bf1b
JG
59static void
60prefixify_expression PARAMS ((struct expression *));
61
62static int
63length_of_subexp PARAMS ((struct expression *, int));
64
65static void
66prefixify_subexp PARAMS ((struct expression *, struct expression *, int, int));
67
9da75ad3
FF
68/* Data structure for saving values of arglist_len for function calls whose
69 arguments contain other function calls. */
70
71struct funcall
72 {
73 struct funcall *next;
74 int arglist_len;
75 };
76
77static struct funcall *funcall_chain;
78
3d6b6a90
JG
79/* Assign machine-independent names to certain registers
80 (unless overridden by the REGISTER_NAMES table) */
81
a332e593
SC
82#ifdef NO_STD_REGS
83unsigned num_std_regs = 0;
84struct std_regs std_regs[1];
85#else
3d6b6a90 86struct std_regs std_regs[] = {
a332e593 87
3d6b6a90
JG
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
a332e593 100
3d6b6a90
JG
101};
102
103unsigned num_std_regs = (sizeof std_regs / sizeof std_regs[0]);
104
a332e593
SC
105#endif
106
3d6b6a90
JG
107
108/* Begin counting arguments for a function call,
109 saving the data about any containing call. */
110
111void
112start_arglist ()
113{
9da75ad3 114 register struct funcall *new;
3d6b6a90 115
9da75ad3 116 new = (struct funcall *) xmalloc (sizeof (struct funcall));
3d6b6a90
JG
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
126int
127end_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;
be772100 133 free ((PTR)call);
3d6b6a90
JG
134 return val;
135}
136
137/* Free everything in the funcall chain.
138 Used when there is an error inside parsing. */
139
9da75ad3 140static void
3d6b6a90
JG
141free_funcalls ()
142{
143 register struct funcall *call, *next;
144
145 for (call = funcall_chain; call; call = next)
146 {
147 next = call->next;
be772100 148 free ((PTR)call);
3d6b6a90
JG
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
160void
161write_exp_elt (expelt)
162 union exp_element expelt;
163{
164 if (expout_ptr >= expout_size)
165 {
166 expout_size *= 2;
81028ab0
FF
167 expout = (struct expression *)
168 xrealloc ((char *) expout, sizeof (struct expression)
169 + EXP_ELEM_TO_BYTES (expout_size));
3d6b6a90
JG
170 }
171 expout->elts[expout_ptr++] = expelt;
172}
173
174void
175write_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
185void
186write_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
479fdd26
JK
196void
197write_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
3d6b6a90
JG
205void
206write_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
216void
217write_exp_elt_dblcst (expelt)
218 double expelt;
219{
220 union exp_element tmp;
221
222 tmp.doubleconst = expelt;
223
224 write_exp_elt (tmp);
225}
226
227void
228write_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
238void
239write_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.
d1065385
FF
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
3d6b6a90
JG
269
270void
271write_exp_string (str)
272 struct stoken str;
273{
274 register int len = str.length;
d1065385
FF
275 register int lenelt;
276 register char *strdata;
3d6b6a90 277
d1065385
FF
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). */
3d6b6a90 282
81028ab0 283 lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
d1065385
FF
284
285 /* Ensure that we have enough available expression elements to store
286 everything. */
287
288 if ((expout_ptr + lenelt) >= expout_size)
3d6b6a90 289 {
d1065385 290 expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
3d6b6a90 291 expout = (struct expression *)
1ab3bf1b 292 xrealloc ((char *) expout, (sizeof (struct expression)
81028ab0 293 + EXP_ELEM_TO_BYTES (expout_size)));
3d6b6a90 294 }
d1065385
FF
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;
3d6b6a90
JG
306 write_exp_elt_longcst ((LONGEST) len);
307}
81028ab0
FF
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
320void
321write_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}
abe28b92
JK
356
357/* Add the appropriate elements for a minimal symbol to the end of
3fb93d86
JK
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;
abe28b92
JK
367
368void
369write_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);
4461196e 375 write_exp_elt_type (lookup_pointer_type (builtin_type_void));
abe28b92
JK
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:
ae6d035d 384 case mst_solib_trampoline:
3fb93d86 385 write_exp_elt_type (msym_text_symbol_type);
abe28b92
JK
386 break;
387
388 case mst_data:
389 case mst_file_data:
390 case mst_bss:
391 case mst_file_bss:
3fb93d86 392 write_exp_elt_type (msym_data_symbol_type);
abe28b92
JK
393 break;
394
395 default:
3fb93d86 396 write_exp_elt_type (msym_unknown_symbol_type);
abe28b92
JK
397 break;
398 }
399 write_exp_elt_opcode (UNOP_MEMVAL);
400}
3d6b6a90
JG
401\f
402/* Return a null-terminated temporary copy of the name
403 of a string token. */
404
405char *
406copy_name (token)
407 struct stoken token;
408{
4ed3a9ea 409 memcpy (namecopy, token.ptr, token.length);
3d6b6a90
JG
410 namecopy[token.length] = 0;
411 return namecopy;
412}
413\f
414/* Reverse an expression from suffix form (in which it is constructed)
415 to prefix form (in which we can conveniently print or execute it). */
416
1ab3bf1b 417static void
3d6b6a90
JG
418prefixify_expression (expr)
419 register struct expression *expr;
420{
81028ab0
FF
421 register int len =
422 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
3d6b6a90
JG
423 register struct expression *temp;
424 register int inpos = expr->nelts, outpos = 0;
425
426 temp = (struct expression *) alloca (len);
427
428 /* Copy the original expression into temp. */
4ed3a9ea 429 memcpy (temp, expr, len);
3d6b6a90
JG
430
431 prefixify_subexp (temp, expr, inpos, outpos);
432}
433
434/* Return the number of exp_elements in the subexpression of EXPR
435 whose last exp_element is at index ENDPOS - 1 in EXPR. */
436
1ab3bf1b 437static int
3d6b6a90
JG
438length_of_subexp (expr, endpos)
439 register struct expression *expr;
440 register int endpos;
441{
442 register int oplen = 1;
443 register int args = 0;
444 register int i;
445
d1065385 446 if (endpos < 1)
3d6b6a90
JG
447 error ("?error in length_of_subexp");
448
449 i = (int) expr->elts[endpos - 1].opcode;
450
451 switch (i)
452 {
453 /* C++ */
454 case OP_SCOPE:
81028ab0
FF
455 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
456 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
3d6b6a90
JG
457 break;
458
459 case OP_LONG:
460 case OP_DOUBLE:
479fdd26 461 case OP_VAR_VALUE:
3d6b6a90
JG
462 oplen = 4;
463 break;
464
465 case OP_TYPE:
466 case OP_BOOL:
3d6b6a90
JG
467 case OP_LAST:
468 case OP_REGISTER:
469 case OP_INTERNALVAR:
470 oplen = 3;
471 break;
472
a91a6192
SS
473 case OP_F77_LITERAL_COMPLEX:
474 oplen = 1;
475 args = 2;
476 break;
477
478 case OP_F77_SUBSTR:
479 oplen = 1;
480 args = 2;
481 break;
482
3d6b6a90 483 case OP_FUNCALL:
a91a6192 484 case OP_F77_UNDETERMINED_ARGLIST:
3d6b6a90 485 oplen = 3;
d1065385 486 args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
3d6b6a90
JG
487 break;
488
489 case UNOP_MAX:
490 case UNOP_MIN:
491 oplen = 3;
3d6b6a90
JG
492 break;
493
494 case BINOP_VAL:
495 case UNOP_CAST:
496 case UNOP_MEMVAL:
497 oplen = 3;
498 args = 1;
499 break;
500
501 case UNOP_ABS:
502 case UNOP_CAP:
503 case UNOP_CHR:
504 case UNOP_FLOAT:
505 case UNOP_HIGH:
506 case UNOP_ODD:
507 case UNOP_ORD:
508 case UNOP_TRUNC:
509 oplen = 1;
510 args = 1;
511 break;
512
dcda44a0 513 case OP_LABELED:
2640f7e1
JG
514 case STRUCTOP_STRUCT:
515 case STRUCTOP_PTR:
516 args = 1;
d1065385 517 /* fall through */
3d6b6a90
JG
518 case OP_M2_STRING:
519 case OP_STRING:
81028ab0
FF
520 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
521 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
522 break;
523
524 case OP_BITSTRING:
525 oplen = longest_to_int (expr->elts[endpos - 2].longconst);
526 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
527 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
3d6b6a90
JG
528 break;
529
c4413e2c
FF
530 case OP_ARRAY:
531 oplen = 4;
532 args = longest_to_int (expr->elts[endpos - 2].longconst);
533 args -= longest_to_int (expr->elts[endpos - 3].longconst);
534 args += 1;
535 break;
536
3d6b6a90
JG
537 case TERNOP_COND:
538 args = 3;
539 break;
540
541 /* Modula-2 */
54bbbfb4 542 case MULTI_SUBSCRIPT:
a91a6192
SS
543 /* Fortran */
544 case MULTI_F77_SUBSCRIPT:
545 oplen = 3;
d1065385 546 args = 1 + longest_to_int (expr->elts[endpos- 2].longconst);
3d6b6a90
JG
547 break;
548
549 case BINOP_ASSIGN_MODIFY:
550 oplen = 3;
551 args = 2;
552 break;
553
554 /* C++ */
555 case OP_THIS:
556 oplen = 2;
557 break;
558
559 default:
560 args = 1 + (i < (int) BINOP_END);
561 }
562
563 while (args > 0)
564 {
565 oplen += length_of_subexp (expr, endpos - oplen);
566 args--;
567 }
568
569 return oplen;
570}
571
572/* Copy the subexpression ending just before index INEND in INEXPR
573 into OUTEXPR, starting at index OUTBEG.
574 In the process, convert it from suffix to prefix form. */
575
576static void
577prefixify_subexp (inexpr, outexpr, inend, outbeg)
578 register struct expression *inexpr;
579 struct expression *outexpr;
580 register int inend;
581 int outbeg;
582{
583 register int oplen = 1;
584 register int args = 0;
585 register int i;
586 int *arglens;
587 enum exp_opcode opcode;
588
589 /* Compute how long the last operation is (in OPLEN),
590 and also how many preceding subexpressions serve as
591 arguments for it (in ARGS). */
592
593 opcode = inexpr->elts[inend - 1].opcode;
594 switch (opcode)
595 {
596 /* C++ */
597 case OP_SCOPE:
81028ab0
FF
598 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
599 oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
3d6b6a90
JG
600 break;
601
602 case OP_LONG:
603 case OP_DOUBLE:
479fdd26 604 case OP_VAR_VALUE:
3d6b6a90
JG
605 oplen = 4;
606 break;
607
608 case OP_TYPE:
609 case OP_BOOL:
3d6b6a90
JG
610 case OP_LAST:
611 case OP_REGISTER:
612 case OP_INTERNALVAR:
613 oplen = 3;
614 break;
615
a91a6192
SS
616 case OP_F77_LITERAL_COMPLEX:
617 oplen = 1;
618 args = 2;
619 break;
620
621 case OP_F77_SUBSTR:
622 oplen = 1;
623 args = 2;
624 break;
625
3d6b6a90 626 case OP_FUNCALL:
a91a6192 627 case OP_F77_UNDETERMINED_ARGLIST:
3d6b6a90 628 oplen = 3;
d1065385 629 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
3d6b6a90
JG
630 break;
631
632 case UNOP_MIN:
633 case UNOP_MAX:
634 oplen = 3;
3d6b6a90
JG
635 break;
636
637 case UNOP_CAST:
638 case UNOP_MEMVAL:
639 oplen = 3;
640 args = 1;
641 break;
642
643 case UNOP_ABS:
644 case UNOP_CAP:
645 case UNOP_CHR:
646 case UNOP_FLOAT:
647 case UNOP_HIGH:
648 case UNOP_ODD:
649 case UNOP_ORD:
650 case UNOP_TRUNC:
651 oplen=1;
652 args=1;
653 break;
654
61c1724b 655 case STRUCTOP_STRUCT:
2640f7e1 656 case STRUCTOP_PTR:
dcda44a0 657 case OP_LABELED:
2640f7e1 658 args = 1;
d1065385 659 /* fall through */
3d6b6a90
JG
660 case OP_M2_STRING:
661 case OP_STRING:
81028ab0
FF
662 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
663 oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
664 break;
665
666 case OP_BITSTRING:
667 oplen = longest_to_int (inexpr->elts[inend - 2].longconst);
668 oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
669 oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
3d6b6a90
JG
670 break;
671
c4413e2c
FF
672 case OP_ARRAY:
673 oplen = 4;
674 args = longest_to_int (inexpr->elts[inend - 2].longconst);
675 args -= longest_to_int (inexpr->elts[inend - 3].longconst);
676 args += 1;
677 break;
678
3d6b6a90
JG
679 case TERNOP_COND:
680 args = 3;
681 break;
682
683 case BINOP_ASSIGN_MODIFY:
684 oplen = 3;
685 args = 2;
686 break;
687
688 /* Modula-2 */
54bbbfb4 689 case MULTI_SUBSCRIPT:
a91a6192
SS
690 /* Fortran */
691 case MULTI_F77_SUBSCRIPT:
692 oplen = 3;
d1065385 693 args = 1 + longest_to_int (inexpr->elts[inend - 2].longconst);
3d6b6a90
JG
694 break;
695
696 /* C++ */
697 case OP_THIS:
698 oplen = 2;
699 break;
700
701 default:
702 args = 1 + ((int) opcode < (int) BINOP_END);
703 }
704
705 /* Copy the final operator itself, from the end of the input
706 to the beginning of the output. */
707 inend -= oplen;
4ed3a9ea 708 memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
81028ab0 709 EXP_ELEM_TO_BYTES (oplen));
3d6b6a90
JG
710 outbeg += oplen;
711
712 /* Find the lengths of the arg subexpressions. */
713 arglens = (int *) alloca (args * sizeof (int));
714 for (i = args - 1; i >= 0; i--)
715 {
716 oplen = length_of_subexp (inexpr, inend);
717 arglens[i] = oplen;
718 inend -= oplen;
719 }
720
721 /* Now copy each subexpression, preserving the order of
722 the subexpressions, but prefixifying each one.
723 In this loop, inend starts at the beginning of
724 the expression this level is working on
725 and marches forward over the arguments.
726 outbeg does similarly in the output. */
727 for (i = 0; i < args; i++)
728 {
729 oplen = arglens[i];
730 inend += oplen;
731 prefixify_subexp (inexpr, outexpr, inend, outbeg);
732 outbeg += oplen;
733 }
734}
735\f
736/* This page contains the two entry points to this file. */
737
738/* Read an expression from the string *STRINGPTR points to,
739 parse it, and return a pointer to a struct expression that we malloc.
740 Use block BLOCK as the lexical context for variable names;
741 if BLOCK is zero, use the block of the selected stack frame.
742 Meanwhile, advance *STRINGPTR to point after the expression,
743 at the first nonwhite character that is not part of the expression
744 (possibly a null character).
745
746 If COMMA is nonzero, stop if a comma is reached. */
747
748struct expression *
749parse_exp_1 (stringptr, block, comma)
750 char **stringptr;
751 struct block *block;
752 int comma;
753{
754 struct cleanup *old_chain;
755
756 lexptr = *stringptr;
757
758 paren_depth = 0;
759 type_stack_depth = 0;
760
761 comma_terminates = comma;
762
763 if (lexptr == 0 || *lexptr == 0)
764 error_no_arg ("expression to compute");
765
766 old_chain = make_cleanup (free_funcalls, 0);
767 funcall_chain = 0;
768
769 expression_context_block = block ? block : get_selected_block ();
770
771 namecopy = (char *) alloca (strlen (lexptr) + 1);
772 expout_size = 10;
773 expout_ptr = 0;
774 expout = (struct expression *)
81028ab0 775 xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
3d6b6a90
JG
776 expout->language_defn = current_language;
777 make_cleanup (free_current_contents, &expout);
778
779 if (current_language->la_parser ())
780 current_language->la_error (NULL);
781
782 discard_cleanups (old_chain);
54bbbfb4
FF
783
784 /* Record the actual number of expression elements, and then
785 reallocate the expression memory so that we free up any
786 excess elements. */
787
3d6b6a90
JG
788 expout->nelts = expout_ptr;
789 expout = (struct expression *)
1ab3bf1b 790 xrealloc ((char *) expout,
81028ab0 791 sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
54bbbfb4
FF
792
793 /* Convert expression from postfix form as generated by yacc
794 parser, to a prefix form. */
795
199b2450 796 DUMP_EXPRESSION (expout, gdb_stdout, "before conversion to prefix form");
3d6b6a90 797 prefixify_expression (expout);
199b2450 798 DUMP_EXPRESSION (expout, gdb_stdout, "after conversion to prefix form");
54bbbfb4 799
3d6b6a90
JG
800 *stringptr = lexptr;
801 return expout;
802}
803
804/* Parse STRING as an expression, and complain if this fails
805 to use up all of the contents of STRING. */
806
807struct expression *
808parse_expression (string)
809 char *string;
810{
811 register struct expression *exp;
812 exp = parse_exp_1 (&string, 0, 0);
813 if (*string)
814 error ("Junk after end of expression.");
815 return exp;
816}
f843c95f
JK
817\f
818/* Stuff for maintaining a stack of types. Currently just used by C, but
819 probably useful for any language which declares its types "backwards". */
3d6b6a90
JG
820
821void
822push_type (tp)
823 enum type_pieces tp;
824{
825 if (type_stack_depth == type_stack_size)
826 {
827 type_stack_size *= 2;
828 type_stack = (union type_stack_elt *)
1ab3bf1b 829 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
3d6b6a90
JG
830 }
831 type_stack[type_stack_depth++].piece = tp;
832}
833
834void
835push_type_int (n)
836 int n;
837{
838 if (type_stack_depth == type_stack_size)
839 {
840 type_stack_size *= 2;
841 type_stack = (union type_stack_elt *)
1ab3bf1b 842 xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
3d6b6a90
JG
843 }
844 type_stack[type_stack_depth++].int_val = n;
845}
846
847enum type_pieces
848pop_type ()
849{
850 if (type_stack_depth)
851 return type_stack[--type_stack_depth].piece;
852 return tp_end;
853}
854
855int
856pop_type_int ()
857{
858 if (type_stack_depth)
859 return type_stack[--type_stack_depth].int_val;
860 /* "Can't happen". */
861 return 0;
862}
863
f843c95f
JK
864/* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
865 as modified by all the stuff on the stack. */
866struct type *
867follow_types (follow_type)
868 struct type *follow_type;
869{
870 int done = 0;
871 int array_size;
872 struct type *range_type;
873
874 while (!done)
875 switch (pop_type ())
876 {
877 case tp_end:
878 done = 1;
879 break;
880 case tp_pointer:
881 follow_type = lookup_pointer_type (follow_type);
882 break;
883 case tp_reference:
884 follow_type = lookup_reference_type (follow_type);
885 break;
886 case tp_array:
887 array_size = pop_type_int ();
888 if (array_size != -1)
889 {
890 range_type =
891 create_range_type ((struct type *) NULL,
892 builtin_type_int, 0,
893 array_size - 1);
894 follow_type =
895 create_array_type ((struct type *) NULL,
896 follow_type, range_type);
897 }
898 else
899 follow_type = lookup_pointer_type (follow_type);
900 break;
901 case tp_function:
902 follow_type = lookup_function_type (follow_type);
903 break;
904 }
905 return follow_type;
906}
907\f
3d6b6a90
JG
908void
909_initialize_parse ()
910{
911 type_stack_size = 80;
912 type_stack_depth = 0;
913 type_stack = (union type_stack_elt *)
914 xmalloc (type_stack_size * sizeof (*type_stack));
3fb93d86
JK
915
916 msym_text_symbol_type =
eedb3363 917 init_type (TYPE_CODE_FUNC, 1, 0, "<text variable, no debug info>", NULL);
3fb93d86
JK
918 TYPE_TARGET_TYPE (msym_text_symbol_type) = builtin_type_int;
919 msym_data_symbol_type =
920 init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
eedb3363 921 "<data variable, no debug info>", NULL);
3fb93d86 922 msym_unknown_symbol_type =
eedb3363
JK
923 init_type (TYPE_CODE_INT, 1, 0,
924 "<variable (not text or data), no debug info>",
3fb93d86 925 NULL);
3d6b6a90 926}
This page took 0.225367 seconds and 4 git commands to generate.