gdb-2.5.1
[binutils-gdb.git] / gdb / expread.y
1 /* Parse C expressions for GDB.
2 Copyright (C) 1986 Free Software Foundation, Inc.
3
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
9
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
16
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
19 */
20 \f
21 /* Parse a C expression from text in a string,
22 and return the result as a struct expression pointer.
23 That structure contains arithmetic operations in reverse polish,
24 with constants represented by operations that are followed by special data.
25 See expression.h for the details of the format.
26 What is important here is that it can be built up sequentially
27 during the process of parsing; the lower levels of the tree always
28 come first in the result. */
29
30 %{
31 #include "defs.h"
32 #include "param.h"
33 #include "symtab.h"
34 #include "frame.h"
35 #include "expression.h"
36
37 #include <stdio.h>
38
39 static struct expression *expout;
40 static int expout_size;
41 static int expout_ptr;
42
43 static int yylex ();
44 static yyerror ();
45 static void write_exp_elt ();
46 static void write_exp_string ();
47 static void start_arglist ();
48 static int end_arglist ();
49 static void free_funcalls ();
50 static char *copy_name ();
51
52 /* If this is nonzero, this block is used as the lexical context
53 for symbol names. */
54
55 static struct block *expression_context_block;
56
57 /* Number of arguments seen so far in innermost function call. */
58 static int arglist_len;
59
60 /* Data structure for saving values of arglist_len
61 for function calls whose arguments contain other function calls. */
62
63 struct funcall
64 {
65 struct funcall *next;
66 int arglist_len;
67 };
68
69 struct funcall *funcall_chain;
70
71 /* This kind of datum is used to represent the name
72 of a symbol token. */
73
74 struct stoken
75 {
76 char *ptr;
77 int length;
78 };
79 %}
80
81 /* Although the yacc "value" of an expression is not used,
82 since the result is stored in the structure being created,
83 other node types do have values. */
84
85 %union
86 {
87 long lval;
88 double dval;
89 struct symbol *sym;
90 struct type *tval;
91 struct stoken sval;
92 int voidval;
93 struct block *bval;
94 enum exp_opcode opcode;
95 struct internalvar *ivar;
96
97 struct type **tvec;
98 int *ivec;
99 }
100
101 %type <voidval> exp exp1 start variable
102 %type <tval> type typebase
103 %type <tvec> nonempty_typelist
104 %type <bval> block
105
106 %token <lval> INT CHAR
107 %token <dval> FLOAT
108
109 /* Both NAME and TYPENAME tokens represent symbols in the input,
110 and both convey their data as strings.
111 But a TYPENAME is a string that happens to be defined as a typedef
112 or builtin type name (such as int or char)
113 and a NAME is any other symbol.
114
115 Contexts where this distinction is not important can use the
116 nonterminal "name", which matches either NAME or TYPENAME. */
117
118 %token <sval> NAME TYPENAME STRING
119 %type <sval> name
120
121 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
122
123 %token <lval> LAST REGNAME
124
125 %token <ivar> VARIABLE
126
127 %token <opcode> ASSIGN_MODIFY
128
129 /* C++ */
130 %token THIS
131
132 %left ','
133 %left ABOVE_COMMA
134 %right '=' ASSIGN_MODIFY
135 %left OR
136 %left AND
137 %left '|'
138 %left '^'
139 %left '&'
140 %left EQUAL NOTEQUAL
141 %left '<' '>' LEQ GEQ
142 %left LSH RSH
143 %left '+' '-'
144 %left '*' '/' '%'
145 %left '@'
146 %right UNARY INCREMENT DECREMENT
147 %right ARROW '.' '['
148 %left COLONCOLON
149 \f
150 %%
151
152 start : exp1
153 ;
154
155 /* Expressions, including the comma operator. */
156 exp1 : exp
157 | exp1 ',' exp
158 { write_exp_elt (BINOP_COMMA); }
159 ;
160
161 /* Expressions, not including the comma operator. */
162 exp : '*' exp %prec UNARY
163 { write_exp_elt (UNOP_IND); }
164
165 exp : '&' exp %prec UNARY
166 { write_exp_elt (UNOP_ADDR); }
167
168 exp : '-' exp %prec UNARY
169 { write_exp_elt (UNOP_NEG); }
170 ;
171
172 exp : '!' exp %prec UNARY
173 { write_exp_elt (UNOP_ZEROP); }
174 ;
175
176 exp : '~' exp %prec UNARY
177 { write_exp_elt (UNOP_LOGNOT); }
178 ;
179
180 exp : INCREMENT exp %prec UNARY
181 { write_exp_elt (UNOP_PREINCREMENT); }
182 ;
183
184 exp : DECREMENT exp %prec UNARY
185 { write_exp_elt (UNOP_PREDECREMENT); }
186 ;
187
188 exp : exp INCREMENT %prec UNARY
189 { write_exp_elt (UNOP_POSTINCREMENT); }
190 ;
191
192 exp : exp DECREMENT %prec UNARY
193 { write_exp_elt (UNOP_POSTDECREMENT); }
194 ;
195
196 exp : SIZEOF exp %prec UNARY
197 { write_exp_elt (UNOP_SIZEOF); }
198 ;
199
200 exp : exp ARROW name
201 { write_exp_elt (STRUCTOP_PTR);
202 write_exp_string ($3);
203 write_exp_elt (STRUCTOP_PTR); }
204 ;
205
206 exp : exp ARROW '*' exp
207 { write_exp_elt (STRUCTOP_MPTR); }
208 ;
209
210 exp : exp '.' name
211 { write_exp_elt (STRUCTOP_STRUCT);
212 write_exp_string ($3);
213 write_exp_elt (STRUCTOP_STRUCT); }
214 ;
215
216 exp : exp '.' '*' exp
217 { write_exp_elt (STRUCTOP_MEMBER); }
218 ;
219
220 exp : exp '[' exp1 ']'
221 { write_exp_elt (BINOP_SUBSCRIPT); }
222 ;
223
224 exp : exp '('
225 /* This is to save the value of arglist_len
226 being accumulated by an outer function call. */
227 { start_arglist (); }
228 arglist ')'
229 { write_exp_elt (OP_FUNCALL);
230 write_exp_elt (end_arglist ());
231 write_exp_elt (OP_FUNCALL); }
232 ;
233
234 arglist :
235 ;
236
237 arglist : exp
238 { arglist_len = 1; }
239 ;
240
241 arglist : arglist ',' exp %prec ABOVE_COMMA
242 { arglist_len++; }
243 ;
244
245 exp : '{' type '}' exp %prec UNARY
246 { write_exp_elt (UNOP_MEMVAL);
247 write_exp_elt ($2);
248 write_exp_elt (UNOP_MEMVAL); }
249 ;
250
251 exp : '(' type ')' exp %prec UNARY
252 { write_exp_elt (UNOP_CAST);
253 write_exp_elt ($2);
254 write_exp_elt (UNOP_CAST); }
255 ;
256
257 exp : '(' exp1 ')'
258 { }
259 ;
260
261 /* Binary operators in order of decreasing precedence. */
262
263 exp : exp '@' exp
264 { write_exp_elt (BINOP_REPEAT); }
265 ;
266
267 exp : exp '*' exp
268 { write_exp_elt (BINOP_MUL); }
269 ;
270
271 exp : exp '/' exp
272 { write_exp_elt (BINOP_DIV); }
273 ;
274
275 exp : exp '%' exp
276 { write_exp_elt (BINOP_REM); }
277 ;
278
279 exp : exp '+' exp
280 { write_exp_elt (BINOP_ADD); }
281 ;
282
283 exp : exp '-' exp
284 { write_exp_elt (BINOP_SUB); }
285 ;
286
287 exp : exp LSH exp
288 { write_exp_elt (BINOP_LSH); }
289 ;
290
291 exp : exp RSH exp
292 { write_exp_elt (BINOP_RSH); }
293 ;
294
295 exp : exp EQUAL exp
296 { write_exp_elt (BINOP_EQUAL); }
297 ;
298
299 exp : exp NOTEQUAL exp
300 { write_exp_elt (BINOP_NOTEQUAL); }
301 ;
302
303 exp : exp LEQ exp
304 { write_exp_elt (BINOP_LEQ); }
305 ;
306
307 exp : exp GEQ exp
308 { write_exp_elt (BINOP_GEQ); }
309 ;
310
311 exp : exp '<' exp
312 { write_exp_elt (BINOP_LESS); }
313 ;
314
315 exp : exp '>' exp
316 { write_exp_elt (BINOP_GTR); }
317 ;
318
319 exp : exp '&' exp
320 { write_exp_elt (BINOP_LOGAND); }
321 ;
322
323 exp : exp '^' exp
324 { write_exp_elt (BINOP_LOGXOR); }
325 ;
326
327 exp : exp '|' exp
328 { write_exp_elt (BINOP_LOGIOR); }
329 ;
330
331 exp : exp AND exp
332 { write_exp_elt (BINOP_AND); }
333 ;
334
335 exp : exp OR exp
336 { write_exp_elt (BINOP_OR); }
337 ;
338
339 exp : exp '?' exp ':' exp
340 { write_exp_elt (TERNOP_COND); }
341 ;
342
343 exp : exp '=' exp
344 { write_exp_elt (BINOP_ASSIGN); }
345 ;
346
347 exp : exp ASSIGN_MODIFY exp
348 { write_exp_elt (BINOP_ASSIGN_MODIFY);
349 write_exp_elt ($2);
350 write_exp_elt (BINOP_ASSIGN_MODIFY); }
351 ;
352
353 exp : INT
354 { write_exp_elt (OP_LONG);
355 write_exp_elt (builtin_type_long);
356 write_exp_elt ($1);
357 write_exp_elt (OP_LONG); }
358 ;
359
360 exp : CHAR
361 { write_exp_elt (OP_LONG);
362 write_exp_elt (builtin_type_char);
363 write_exp_elt ($1);
364 write_exp_elt (OP_LONG); }
365 ;
366
367 exp : FLOAT
368 { write_exp_elt (OP_DOUBLE);
369 write_exp_elt (builtin_type_double);
370 write_exp_elt ($1);
371 write_exp_elt (OP_DOUBLE); }
372 ;
373
374 exp : variable
375 ;
376
377 exp : LAST
378 { write_exp_elt (OP_LAST);
379 write_exp_elt ($1);
380 write_exp_elt (OP_LAST); }
381 ;
382
383 exp : REGNAME
384 { write_exp_elt (OP_REGISTER);
385 write_exp_elt ($1);
386 write_exp_elt (OP_REGISTER); }
387 ;
388
389 exp : VARIABLE
390 { write_exp_elt (OP_INTERNALVAR);
391 write_exp_elt ($1);
392 write_exp_elt (OP_INTERNALVAR); }
393 ;
394
395 exp : SIZEOF '(' type ')'
396 { write_exp_elt (OP_LONG);
397 write_exp_elt (builtin_type_int);
398 write_exp_elt ((long) TYPE_LENGTH ($3));
399 write_exp_elt (OP_LONG); }
400 ;
401
402 exp : STRING
403 { write_exp_elt (OP_STRING);
404 write_exp_string ($1);
405 write_exp_elt (OP_STRING); }
406 ;
407
408 /* C++. */
409 exp : THIS
410 { write_exp_elt (OP_THIS);
411 write_exp_elt (OP_THIS); }
412 ;
413
414 /* end of C++. */
415
416 block : name
417 {
418 struct symtab *tem = lookup_symtab (copy_name ($1));
419 struct symbol *sym;
420
421 if (tem)
422 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), 1);
423 else
424 {
425 sym = lookup_symbol (copy_name ($1),
426 expression_context_block,
427 VAR_NAMESPACE);
428 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
429 $$ = SYMBOL_BLOCK_VALUE (sym);
430 else
431 error ("No file or function \"%s\".",
432 copy_name ($1));
433 }
434 }
435 ;
436
437 block : block COLONCOLON name
438 {
439 struct symbol *tem
440 = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE);
441 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
442 error ("No function \"%s\" in specified context.",
443 copy_name ($1));
444 $$ = SYMBOL_BLOCK_VALUE (tem);
445 }
446 ;
447
448 variable: block COLONCOLON name
449 {
450 struct symbol *sym;
451 sym = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE);
452 if (sym == 0)
453 error ("No symbol \"%s\" in specified context.",
454 copy_name ($3));
455 write_exp_elt (OP_VAR_VALUE);
456 write_exp_elt (sym);
457 write_exp_elt (OP_VAR_VALUE);
458 }
459 ;
460
461 variable: typebase COLONCOLON name
462 {
463 struct type *type = $1;
464 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
465 && TYPE_CODE (type) != TYPE_CODE_UNION)
466 error ("`%s' is not defined as an aggregate type.",
467 TYPE_NAME (type));
468
469 write_exp_elt (OP_SCOPE);
470 write_exp_elt (type);
471 write_exp_string ($3);
472 write_exp_elt (OP_SCOPE);
473 }
474 | COLONCOLON name
475 {
476 char *name = copy_name ($2);
477 struct symbol *sym;
478 int i;
479
480 sym = lookup_symbol_2 (name, 0, VAR_NAMESPACE);
481 if (sym)
482 {
483 write_exp_elt (OP_VAR_VALUE);
484 write_exp_elt (sym);
485 write_exp_elt (OP_VAR_VALUE);
486 break;
487 }
488 for (i = 0; i < misc_function_count; i++)
489 if (!strcmp (misc_function_vector[i].name, name))
490 break;
491
492 if (i < misc_function_count)
493 {
494 write_exp_elt (OP_LONG);
495 write_exp_elt (builtin_type_int);
496 write_exp_elt (misc_function_vector[i].address);
497 write_exp_elt (OP_LONG);
498 write_exp_elt (UNOP_MEMVAL);
499 write_exp_elt (builtin_type_char);
500 write_exp_elt (UNOP_MEMVAL);
501 }
502 else
503 if (symtab_list == 0)
504 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
505 else
506 error ("No symbol \"%s\" in current context.", name);
507 }
508 ;
509
510 variable: NAME
511 { struct symbol *sym;
512 sym = lookup_symbol_1 (copy_name ($1),
513 expression_context_block,
514 VAR_NAMESPACE);
515 if (sym)
516 {
517 write_exp_elt (OP_VAR_VALUE);
518 write_exp_elt (sym);
519 write_exp_elt (OP_VAR_VALUE);
520 }
521 else
522 {
523 register char *arg = copy_name ($1);
524 register int i;
525 int v, val;
526 /* C++: see if it hangs off of `this'. Must
527 not inadvertently convert from a method call
528 to data ref. */
529 v = (int)value_of_this (0);
530 if (v)
531 {
532 val = check_field (v, arg);
533 if (val)
534 {
535 write_exp_elt (OP_THIS);
536 write_exp_elt (OP_THIS);
537 write_exp_elt (STRUCTOP_PTR);
538 write_exp_string ($1);
539 write_exp_elt (STRUCTOP_PTR);
540 break;
541 }
542 }
543 sym = lookup_symbol_2 (arg, 0, VAR_NAMESPACE);
544 if (sym)
545 {
546 write_exp_elt (OP_VAR_VALUE);
547 write_exp_elt (sym);
548 write_exp_elt (OP_VAR_VALUE);
549 break; /* YACC-dependent */
550 }
551 for (i = 0; i < misc_function_count; i++)
552 if (!strcmp (misc_function_vector[i].name, arg))
553 break;
554
555 if (i < misc_function_count)
556 {
557 write_exp_elt (OP_LONG);
558 write_exp_elt (builtin_type_int);
559 write_exp_elt (misc_function_vector[i].address);
560 write_exp_elt (OP_LONG);
561 write_exp_elt (UNOP_MEMVAL);
562 write_exp_elt (builtin_type_char);
563 write_exp_elt (UNOP_MEMVAL);
564 }
565 else
566 if (symtab_list == 0)
567 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
568 else
569 error ("No symbol \"%s\" in current context.",
570 copy_name ($1));
571 }
572 }
573 ;
574
575 type : typebase
576 | type '*'
577 { $$ = lookup_pointer_type ($1); }
578 | type '&'
579 { $$ = lookup_reference_type ($1); }
580 | typebase COLONCOLON '*'
581 { $$ = lookup_member_pointer_type (builtin_type_int, $1); }
582 | type '(' typebase COLONCOLON '*' ')'
583 { $$ = lookup_member_pointer_type ($1, $3); }
584 | type '(' typebase COLONCOLON '*' ')' '(' ')'
585 { $$ = lookup_member_pointer_type (lookup_function_type ($1, 0), $3); }
586 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
587 { $$ = lookup_member_pointer_type (lookup_function_type ($1, $8), $3);
588 free ($8); }
589 ;
590
591 typebase
592 : TYPENAME
593 { $$ = lookup_typename (copy_name ($1),
594 expression_context_block, 0); }
595 | STRUCT name
596 { $$ = lookup_struct (copy_name ($2),
597 expression_context_block); }
598 | UNION name
599 { $$ = lookup_union (copy_name ($2),
600 expression_context_block); }
601 | ENUM name
602 { $$ = lookup_enum (copy_name ($2),
603 expression_context_block); }
604 | UNSIGNED name
605 { $$ = lookup_unsigned_typename (copy_name ($2)); }
606 ;
607
608 nonempty_typelist
609 : type
610 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
611 $$[0] = (struct type *)0;
612 $$[1] = $1;
613 }
614 | nonempty_typelist ',' type
615 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
616 $$ = (struct type **)xrealloc ($1, len);
617 $$[$<ivec>$[0]] = $3;
618 }
619 ;
620
621 name : NAME
622 | TYPENAME
623 ;
624 %%
625 \f
626 /* Begin counting arguments for a function call,
627 saving the data about any containing call. */
628
629 static void
630 start_arglist ()
631 {
632 register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
633
634 new->next = funcall_chain;
635 new->arglist_len = arglist_len;
636 arglist_len = 0;
637 funcall_chain = new;
638 }
639
640 /* Return the number of arguments in a function call just terminated,
641 and restore the data for the containing function call. */
642
643 static int
644 end_arglist ()
645 {
646 register int val = arglist_len;
647 register struct funcall *call = funcall_chain;
648 funcall_chain = call->next;
649 arglist_len = call->arglist_len;
650 free (call);
651 return val;
652 }
653
654 /* Free everything in the funcall chain.
655 Used when there is an error inside parsing. */
656
657 static void
658 free_funcalls ()
659 {
660 register struct funcall *call, *next;
661
662 for (call = funcall_chain; call; call = next)
663 {
664 next = call->next;
665 free (call);
666 }
667 }
668 \f
669 /* This page contains the functions for adding data to the struct expression
670 being constructed. */
671
672 /* Add one element to the end of the expression. */
673
674 static void
675 write_exp_elt (expelt)
676 union exp_element expelt;
677 {
678 if (expout_ptr >= expout_size)
679 {
680 expout_size *= 2;
681 expout = (struct expression *) xrealloc (expout,
682 sizeof (struct expression)
683 + expout_size * sizeof (union exp_element));
684 }
685 expout->elts[expout_ptr++] = expelt;
686 }
687
688 /* Add a string constant to the end of the expression.
689 Follow it by its length in bytes, as a separate exp_element. */
690
691 static void
692 write_exp_string (str)
693 struct stoken str;
694 {
695 register int len = str.length;
696 register int lenelt
697 = (len + sizeof (union exp_element)) / sizeof (union exp_element);
698
699 expout_ptr += lenelt;
700
701 if (expout_ptr >= expout_size)
702 {
703 expout_size = max (expout_size * 2, expout_ptr + 10);
704 expout = (struct expression *) xrealloc (expout,
705 sizeof (struct expression)
706 + expout_size * sizeof (union exp_element));
707 }
708 bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len);
709 ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0;
710 write_exp_elt (len);
711 }
712 \f
713 /* During parsing of a C expression, the pointer to the next character
714 is in this variable. */
715
716 static char *lexptr;
717
718 /* Tokens that refer to names do so with explicit pointer and length,
719 so they can share the storage that lexptr is parsing.
720
721 When it is necessary to pass a name to a function that expects
722 a null-terminated string, the substring is copied out
723 into a block of storage that namecopy points to.
724
725 namecopy is allocated once, guaranteed big enough, for each parsing. */
726
727 static char *namecopy;
728
729 /* Current depth in parentheses within the expression. */
730
731 static int paren_depth;
732
733 /* Nonzero means stop parsing on first comma (if not within parentheses). */
734
735 static int comma_terminates;
736
737 /* Take care of parsing a number (anything that starts with a digit).
738 Set yylval and return the token type; update lexptr.
739 LEN is the number of characters in it. */
740
741 /*** Needs some error checking for the float case ***/
742
743 static int
744 parse_number (olen)
745 int olen;
746 {
747 register char *p = lexptr;
748 register long n = 0;
749 register int c;
750 register int base = 10;
751 register int len = olen;
752 char *err_copy;
753
754 extern double atof ();
755
756 for (c = 0; c < len; c++)
757 if (p[c] == '.')
758 {
759 /* It's a float since it contains a point. */
760 yylval.dval = atof (p);
761 lexptr += len;
762 return FLOAT;
763 }
764
765 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2)))
766 {
767 p += 2;
768 base = 16;
769 len -= 2;
770 }
771 else if (*p == '0')
772 base = 8;
773
774 while (len-- > 0)
775 {
776 c = *p++;
777 n *= base;
778 if (c >= '0' && c <= '9')
779 n += c - '0';
780 else
781 {
782 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
783 if (base == 16 && c >= 'a' && c <= 'f')
784 n += c - 'a' + 10;
785 else if (len == 0 && c == 'l')
786 ;
787 else
788 {
789 err_copy = (char *) alloca (olen + 1);
790 bcopy (lexptr, err_copy, olen);
791 err_copy[olen] = 0;
792 error ("Invalid number \"%s\".", err_copy);
793 }
794 }
795 }
796
797 lexptr = p;
798 yylval.lval = n;
799 return INT;
800 }
801
802 struct token
803 {
804 char *operator;
805 int token;
806 enum exp_opcode opcode;
807 };
808
809 static struct token tokentab3[] =
810 {
811 {">>=", ASSIGN_MODIFY, BINOP_RSH},
812 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
813 };
814
815 static struct token tokentab2[] =
816 {
817 {"+=", ASSIGN_MODIFY, BINOP_ADD},
818 {"-=", ASSIGN_MODIFY, BINOP_SUB},
819 {"*=", ASSIGN_MODIFY, BINOP_MUL},
820 {"/=", ASSIGN_MODIFY, BINOP_DIV},
821 {"%=", ASSIGN_MODIFY, BINOP_REM},
822 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
823 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
824 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
825 {"++", INCREMENT, BINOP_END},
826 {"--", DECREMENT, BINOP_END},
827 {"->", ARROW, BINOP_END},
828 {"&&", AND, BINOP_END},
829 {"||", OR, BINOP_END},
830 {"::", COLONCOLON, BINOP_END},
831 {"<<", LSH, BINOP_END},
832 {">>", RSH, BINOP_END},
833 {"==", EQUAL, BINOP_END},
834 {"!=", NOTEQUAL, BINOP_END},
835 {"<=", LEQ, BINOP_END},
836 {">=", GEQ, BINOP_END}
837 };
838
839 /* Read one token, getting characters through lexptr. */
840
841 static int
842 yylex ()
843 {
844 register int c;
845 register int namelen;
846 register int i;
847 register char *tokstart;
848
849 retry:
850
851 tokstart = lexptr;
852 /* See if it is a special token of length 3. */
853 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
854 if (!strncmp (tokstart, tokentab3[i].operator, 3))
855 {
856 lexptr += 3;
857 yylval.opcode = tokentab3[i].opcode;
858 return tokentab3[i].token;
859 }
860
861 /* See if it is a special token of length 2. */
862 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
863 if (!strncmp (tokstart, tokentab2[i].operator, 2))
864 {
865 lexptr += 2;
866 yylval.opcode = tokentab2[i].opcode;
867 return tokentab2[i].token;
868 }
869
870 switch (c = *tokstart)
871 {
872 case 0:
873 return 0;
874
875 case ' ':
876 case '\t':
877 case '\n':
878 lexptr++;
879 goto retry;
880
881 case '\'':
882 lexptr++;
883 c = *lexptr++;
884 if (c == '\\')
885 c = parse_escape (&lexptr);
886 yylval.lval = c;
887 c = *lexptr++;
888 if (c != '\'')
889 error ("Invalid character constant.");
890 return CHAR;
891
892 case '(':
893 paren_depth++;
894 lexptr++;
895 return c;
896
897 case ')':
898 if (paren_depth == 0)
899 return 0;
900 paren_depth--;
901 lexptr++;
902 return c;
903
904 case ',':
905 if (comma_terminates && paren_depth == 0)
906 return 0;
907 lexptr++;
908 return c;
909
910 case '+':
911 case '-':
912 case '*':
913 case '/':
914 case '%':
915 case '|':
916 case '&':
917 case '^':
918 case '~':
919 case '!':
920 case '@':
921 case '<':
922 case '>':
923 case '[':
924 case ']':
925 case '.':
926 case '?':
927 case ':':
928 case '=':
929 case '{':
930 case '}':
931 lexptr++;
932 return c;
933
934 case '"':
935 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
936 if (c == '\\')
937 {
938 c = tokstart[++namelen];
939 if (c >= '0' && c <= '9')
940 {
941 c = tokstart[++namelen];
942 if (c >= '0' && c <= '9')
943 c = tokstart[++namelen];
944 }
945 }
946 yylval.sval.ptr = tokstart + 1;
947 yylval.sval.length = namelen - 1;
948 lexptr += namelen + 1;
949 return STRING;
950 }
951 if (c >= '0' && c <= '9')
952 {
953 /* It's a number */
954 for (namelen = 0;
955 c = tokstart[namelen],
956 (c == '_' || c == '$' || c == '.' || (c >= '0' && c <= '9')
957 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
958 namelen++)
959 ;
960 return parse_number (namelen);
961 }
962
963 if (!(c == '_' || c == '$'
964 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
965 error ("Invalid token in expression.");
966
967 /* It is a name. See how long it is. */
968
969 for (namelen = 0;
970 c = tokstart[namelen],
971 (c == '_' || c == '$' || (c >= '0' && c <= '9')
972 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
973 namelen++)
974 ;
975
976 /* The token "if" terminates the expression and is NOT
977 removed from the input stream. */
978 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
979 {
980 return 0;
981 }
982
983 lexptr += namelen;
984
985 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
986 and $$digits (equivalent to $<-digits> if you could type that).
987 Make token type LAST, and put the number (the digits) in yylval. */
988
989 if (*tokstart == '$')
990 {
991 register int negate = 0;
992 c = 1;
993 /* Double dollar means negate the number and add -1 as well.
994 Thus $$ alone means -1. */
995 if (namelen >= 2 && tokstart[1] == '$')
996 {
997 negate = 1;
998 c = 2;
999 }
1000 if (c == namelen)
1001 {
1002 /* Just dollars (one or two) */
1003 yylval.lval = - negate;
1004 return LAST;
1005 }
1006 /* Is the rest of the token digits? */
1007 for (; c < namelen; c++)
1008 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1009 break;
1010 if (c == namelen)
1011 {
1012 yylval.lval = atoi (tokstart + 1 + negate);
1013 if (negate)
1014 yylval.lval = - yylval.lval;
1015 return LAST;
1016 }
1017 }
1018
1019 /* Handle tokens that refer to machine registers:
1020 $ followed by a register name. */
1021
1022 if (*tokstart == '$')
1023 for (c = 0; c < NUM_REGS; c++)
1024 if (namelen - 1 == strlen (reg_names[c])
1025 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1026 {
1027 yylval.lval = c;
1028 return REGNAME;
1029 }
1030
1031 if (namelen == 6 && !strncmp (tokstart, "struct", 6))
1032 {
1033 return STRUCT;
1034 }
1035 if (namelen == 5)
1036 {
1037 if (!strncmp (tokstart, "union", 5))
1038 {
1039 return UNION;
1040 }
1041 }
1042 if (namelen == 4)
1043 {
1044 if (!strncmp (tokstart, "enum", 4))
1045 {
1046 return ENUM;
1047 }
1048 if (!strncmp (tokstart, "this", 4))
1049 {
1050 return THIS;
1051 }
1052 }
1053 if (namelen == 6 && !strncmp (tokstart, "sizeof", 6))
1054 {
1055 return SIZEOF;
1056 }
1057 if (namelen == 8 && !strncmp (tokstart, "unsigned", 6))
1058 {
1059 return UNSIGNED;
1060 }
1061 yylval.sval.ptr = tokstart;
1062 yylval.sval.length = namelen;
1063
1064 /* Any other names starting in $ are debugger internal variables. */
1065
1066 if (*tokstart == '$')
1067 {
1068 yylval.ivar = (struct internalvar *) lookup_internalvar (copy_name (yylval.sval) + 1);
1069 return VARIABLE;
1070 }
1071
1072 /* Use token-type TYPENAME for symbols that happen to be defined
1073 currently as names of types; NAME for other symbols.
1074 The caller is not constrained to care about the distinction. */
1075 if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
1076 return TYPENAME;
1077 return NAME;
1078 }
1079
1080 static
1081 yyerror ()
1082 {
1083 error ("Invalid syntax in expression.");
1084 }
1085
1086 /* Return a null-terminated temporary copy of the name
1087 of a string token. */
1088
1089 static char *
1090 copy_name (token)
1091 struct stoken token;
1092 {
1093 bcopy (token.ptr, namecopy, token.length);
1094 namecopy[token.length] = 0;
1095 return namecopy;
1096 }
1097 \f
1098 /* Reverse an expression from suffix form (in which it is constructed)
1099 to prefix form (in which we can conveniently print or execute it). */
1100
1101 static void prefixify_subexp ();
1102
1103 static void
1104 prefixify_expression (expr)
1105 register struct expression *expr;
1106 {
1107 register int len = sizeof (struct expression) +
1108 expr->nelts * sizeof (union exp_element);
1109 register struct expression *temp;
1110 register int inpos = expr->nelts, outpos = 0;
1111
1112 temp = (struct expression *) alloca (len);
1113
1114 /* Copy the original expression into temp. */
1115 bcopy (expr, temp, len);
1116
1117 prefixify_subexp (temp, expr, inpos, outpos);
1118 }
1119
1120 /* Return the number of exp_elements in the subexpression of EXPR
1121 whose last exp_element is at index ENDPOS - 1 in EXPR. */
1122
1123 static int
1124 length_of_subexp (expr, endpos)
1125 register struct expression *expr;
1126 register int endpos;
1127 {
1128 register int oplen = 1;
1129 register int args = 0;
1130 register int i;
1131
1132 if (endpos < 0)
1133 error ("?error in length_of_subexp");
1134
1135 i = (int) expr->elts[endpos - 1].opcode;
1136
1137 switch (i)
1138 {
1139 /* C++ */
1140 case OP_SCOPE:
1141 oplen = 4 + ((expr->elts[endpos - 2].longconst
1142 + sizeof (union exp_element))
1143 / sizeof (union exp_element));
1144 break;
1145
1146 case OP_LONG:
1147 case OP_DOUBLE:
1148 oplen = 4;
1149 break;
1150
1151 case OP_VAR_VALUE:
1152 case OP_LAST:
1153 case OP_REGISTER:
1154 case OP_INTERNALVAR:
1155 oplen = 3;
1156 break;
1157
1158 case OP_FUNCALL:
1159 oplen = 3;
1160 args = 1 + expr->elts[endpos - 2].longconst;
1161 break;
1162
1163 case UNOP_CAST:
1164 case UNOP_MEMVAL:
1165 oplen = 3;
1166 args = 1;
1167 break;
1168
1169 case STRUCTOP_STRUCT:
1170 case STRUCTOP_PTR:
1171 args = 1;
1172 case OP_STRING:
1173 oplen = 3 + ((expr->elts[endpos - 2].longconst
1174 + sizeof (union exp_element))
1175 / sizeof (union exp_element));
1176 break;
1177
1178 case TERNOP_COND:
1179 args = 3;
1180 break;
1181
1182 case BINOP_ASSIGN_MODIFY:
1183 oplen = 3;
1184 args = 2;
1185 break;
1186
1187 /* C++ */
1188 case OP_THIS:
1189 oplen = 2;
1190 break;
1191
1192 default:
1193 args = 1 + (i < (int) BINOP_END);
1194 }
1195
1196 while (args > 0)
1197 {
1198 oplen += length_of_subexp (expr, endpos - oplen);
1199 args--;
1200 }
1201
1202 return oplen;
1203 }
1204
1205 /* Copy the subexpression ending just before index INEND in INEXPR
1206 into OUTEXPR, starting at index OUTBEG.
1207 In the process, convert it from suffix to prefix form. */
1208
1209 static void
1210 prefixify_subexp (inexpr, outexpr, inend, outbeg)
1211 register struct expression *inexpr;
1212 struct expression *outexpr;
1213 register int inend;
1214 int outbeg;
1215 {
1216 register int oplen = 1;
1217 register int args = 0;
1218 register int i;
1219 int *arglens;
1220 enum exp_opcode opcode;
1221
1222 /* Compute how long the last operation is (in OPLEN),
1223 and also how many preceding subexpressions serve as
1224 arguments for it (in ARGS). */
1225
1226 opcode = inexpr->elts[inend - 1].opcode;
1227 switch (opcode)
1228 {
1229 /* C++ */
1230 case OP_SCOPE:
1231 oplen = 4 + ((inexpr->elts[inend - 2].longconst
1232 + sizeof (union exp_element))
1233 / sizeof (union exp_element));
1234 break;
1235
1236 case OP_LONG:
1237 case OP_DOUBLE:
1238 oplen = 4;
1239 break;
1240
1241 case OP_VAR_VALUE:
1242 case OP_LAST:
1243 case OP_REGISTER:
1244 case OP_INTERNALVAR:
1245 oplen = 3;
1246 break;
1247
1248 case OP_FUNCALL:
1249 oplen = 3;
1250 args = 1 + inexpr->elts[inend - 2].longconst;
1251 break;
1252
1253 case UNOP_CAST:
1254 case UNOP_MEMVAL:
1255 oplen = 3;
1256 args = 1;
1257 break;
1258
1259 case STRUCTOP_STRUCT:
1260 case STRUCTOP_PTR:
1261 args = 1;
1262 case OP_STRING:
1263 oplen = 3 + ((inexpr->elts[inend - 2].longconst
1264 + sizeof (union exp_element))
1265 / sizeof (union exp_element));
1266
1267 break;
1268
1269 case TERNOP_COND:
1270 args = 3;
1271 break;
1272
1273 case BINOP_ASSIGN_MODIFY:
1274 oplen = 3;
1275 args = 2;
1276 break;
1277
1278 /* C++ */
1279 case OP_THIS:
1280 oplen = 2;
1281 break;
1282
1283 default:
1284 args = 1 + ((int) opcode < (int) BINOP_END);
1285 }
1286
1287 /* Copy the final operator itself, from the end of the input
1288 to the beginning of the output. */
1289 inend -= oplen;
1290 bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg],
1291 oplen * sizeof (union exp_element));
1292 outbeg += oplen;
1293
1294 /* Find the lengths of the arg subexpressions. */
1295 arglens = (int *) alloca (args * sizeof (int));
1296 for (i = args - 1; i >= 0; i--)
1297 {
1298 oplen = length_of_subexp (inexpr, inend);
1299 arglens[i] = oplen;
1300 inend -= oplen;
1301 }
1302
1303 /* Now copy each subexpression, preserving the order of
1304 the subexpressions, but prefixifying each one.
1305 In this loop, inend starts at the beginning of
1306 the expression this level is working on
1307 and marches forward over the arguments.
1308 outbeg does similarly in the output. */
1309 for (i = 0; i < args; i++)
1310 {
1311 oplen = arglens[i];
1312 inend += oplen;
1313 prefixify_subexp (inexpr, outexpr, inend, outbeg);
1314 outbeg += oplen;
1315 }
1316 }
1317 \f
1318 /* This page contains the two entry points to this file. */
1319
1320 /* Read a C expression from the string *STRINGPTR points to,
1321 parse it, and return a pointer to a struct expression that we malloc.
1322 Use block BLOCK as the lexical context for variable names;
1323 if BLOCK is zero, use the block of the selected stack frame.
1324 Meanwhile, advance *STRINGPTR to point after the expression,
1325 at the first nonwhite character that is not part of the expression
1326 (possibly a null character).
1327
1328 If COMMA is nonzero, stop if a comma is reached. */
1329
1330 struct expression *
1331 parse_c_1 (stringptr, block, comma)
1332 char **stringptr;
1333 struct block *block;
1334 {
1335 struct cleanup *old_chain;
1336
1337 lexptr = *stringptr;
1338
1339 comma_terminates = comma;
1340
1341 if (lexptr == 0 || *lexptr == 0)
1342 error_no_arg ("expression to compute");
1343
1344 old_chain = make_cleanup (free_funcalls, 0);
1345 funcall_chain = 0;
1346
1347 expression_context_block = block ? block : get_selected_block ();
1348
1349 namecopy = (char *) alloca (strlen (lexptr) + 1);
1350 expout_size = 10;
1351 expout_ptr = 0;
1352 expout = (struct expression *) xmalloc (sizeof (struct expression)
1353 + expout_size * sizeof (union exp_element));
1354 make_cleanup (free_current_contents, &expout);
1355 if (yyparse ())
1356 yyerror ();
1357 discard_cleanups (old_chain);
1358 expout->nelts = expout_ptr;
1359 expout = (struct expression *)
1360 xrealloc (expout,
1361 sizeof (struct expression)
1362 + expout_ptr * sizeof (union exp_element));
1363 prefixify_expression (expout);
1364 *stringptr = lexptr;
1365 return expout;
1366 }
1367
1368 /* Parse STRING as an expression, and complain if this fails
1369 to use up all of the contents of STRING. */
1370
1371 struct expression *
1372 parse_c_expression (string)
1373 char *string;
1374 {
1375 register struct expression *exp;
1376 exp = parse_c_1 (&string, 0, 0);
1377 if (*string)
1378 error ("Junk after end of expression.");
1379 return exp;
1380 }