* symtab.h (GLOBAL_BLOCK, STATIC_BLOCK, FIRST_LOCAL_BLOCK): New
[deliverable/binutils-gdb.git] / gdb / expread.y
1 /* Parse C expressions for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
10
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 \f
20 /* Parse a C expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result. */
28
29 %{
30 #include <stdio.h>
31 #include "defs.h"
32 #include "param.h"
33 #include "symtab.h"
34 #include "frame.h"
35 #include "expression.h"
36 #include "value.h"
37 #include "command.h"
38
39 static struct expression *expout;
40 static int expout_size;
41 static int expout_ptr;
42
43 static int yylex ();
44 static void yyerror ();
45 static void write_exp_elt ();
46 static void write_exp_elt_opcode ();
47 static void write_exp_elt_sym ();
48 static void write_exp_elt_longcst ();
49 static void write_exp_elt_dblcst ();
50 static void write_exp_elt_type ();
51 static void write_exp_elt_intern ();
52 static void write_exp_string ();
53 static void start_arglist ();
54 static int end_arglist ();
55 static void free_funcalls ();
56 static char *copy_name ();
57 static int parse_number ();
58
59 /* If this is nonzero, this block is used as the lexical context
60 for symbol names. */
61
62 static struct block *expression_context_block;
63
64 /* The innermost context required by the stack and register variables
65 we've encountered so far. */
66 struct block *innermost_block;
67
68 /* The block in which the most recently discovered symbol was found. */
69 struct block *block_found;
70
71 /* Number of arguments seen so far in innermost function call. */
72 static int arglist_len;
73
74 /* Data structure for saving values of arglist_len
75 for function calls whose arguments contain other function calls. */
76
77 struct funcall
78 {
79 struct funcall *next;
80 int arglist_len;
81 };
82
83 struct funcall *funcall_chain;
84
85 /* This kind of datum is used to represent the name
86 of a symbol token. */
87
88 struct stoken
89 {
90 char *ptr;
91 int length;
92 };
93
94 struct ttype
95 {
96 struct stoken stoken;
97 struct type *type;
98 };
99
100 struct symtoken
101 {
102 struct stoken stoken;
103 struct symbol *sym;
104 int is_a_field_of_this;
105 };
106
107 /* For parsing of complicated types.
108 An array should be preceded in the list by the size of the array. */
109 enum type_pieces
110 {tp_end = -1, tp_pointer, tp_reference, tp_array, tp_function};
111 static enum type_pieces *type_stack;
112 static int type_stack_depth, type_stack_size;
113
114 static void push_type ();
115 static enum type_pieces pop_type ();
116
117 /* Allow debugging of parsing. */
118 #define YYDEBUG 1
119 %}
120
121 /* Although the yacc "value" of an expression is not used,
122 since the result is stored in the structure being created,
123 other node types do have values. */
124
125 %union
126 {
127 LONGEST lval;
128 unsigned LONGEST ulval;
129 double dval;
130 struct symbol *sym;
131 struct type *tval;
132 struct stoken sval;
133 struct ttype tsym;
134 struct symtoken ssym;
135 int voidval;
136 struct block *bval;
137 enum exp_opcode opcode;
138 struct internalvar *ivar;
139
140 struct type **tvec;
141 int *ivec;
142 }
143
144 %type <voidval> exp exp1 start variable
145 %type <tval> type typebase
146 %type <tvec> nonempty_typelist
147 %type <bval> block
148
149 /* Fancy type parsing. */
150 %type <voidval> func_mod direct_abs_decl abs_decl
151 %type <tval> ptype
152 %type <lval> array_mod
153
154 %token <lval> INT CHAR
155 %token <ulval> UINT
156 %token <dval> FLOAT
157
158 /* Both NAME and TYPENAME tokens represent symbols in the input,
159 and both convey their data as strings.
160 But a TYPENAME is a string that happens to be defined as a typedef
161 or builtin type name (such as int or char)
162 and a NAME is any other symbol.
163
164 Contexts where this distinction is not important can use the
165 nonterminal "name", which matches either NAME or TYPENAME. */
166
167 %token <sval> STRING
168 %token <ssym> NAME BLOCKNAME
169 %token <tsym> TYPENAME
170 %type <sval> name
171 %type <ssym> name_not_typename
172 %type <tsym> typename
173
174 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
175 but which would parse as a valid number in the current input radix.
176 E.g. "c" when input_radix==16. Depending on the parse, it will be
177 turned into a name or into a number. NAME_OR_UINT ditto. */
178
179 %token <ssym> NAME_OR_INT NAME_OR_UINT
180
181 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
182 %token ERROR
183
184 /* Special type cases, put in to allow the parser to distinguish different
185 legal basetypes. */
186 %token SIGNED LONG SHORT INT_KEYWORD
187
188 %token <lval> LAST REGNAME
189
190 %token <ivar> VARIABLE
191
192 %token <opcode> ASSIGN_MODIFY
193
194 /* C++ */
195 %token THIS
196
197 %left ','
198 %left ABOVE_COMMA
199 %right '=' ASSIGN_MODIFY
200 %right '?'
201 %left OR
202 %left AND
203 %left '|'
204 %left '^'
205 %left '&'
206 %left EQUAL NOTEQUAL
207 %left '<' '>' LEQ GEQ
208 %left LSH RSH
209 %left '@'
210 %left '+' '-'
211 %left '*' '/' '%'
212 %right UNARY INCREMENT DECREMENT
213 %right ARROW '.' '[' '('
214 %left COLONCOLON
215 \f
216 %%
217
218 start : exp1
219 ;
220
221 /* Expressions, including the comma operator. */
222 exp1 : exp
223 | exp1 ',' exp
224 { write_exp_elt_opcode (BINOP_COMMA); }
225 ;
226
227 /* Expressions, not including the comma operator. */
228 exp : '*' exp %prec UNARY
229 { write_exp_elt_opcode (UNOP_IND); }
230
231 exp : '&' exp %prec UNARY
232 { write_exp_elt_opcode (UNOP_ADDR); }
233
234 exp : '-' exp %prec UNARY
235 { write_exp_elt_opcode (UNOP_NEG); }
236 ;
237
238 exp : '!' exp %prec UNARY
239 { write_exp_elt_opcode (UNOP_ZEROP); }
240 ;
241
242 exp : '~' exp %prec UNARY
243 { write_exp_elt_opcode (UNOP_LOGNOT); }
244 ;
245
246 exp : INCREMENT exp %prec UNARY
247 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
248 ;
249
250 exp : DECREMENT exp %prec UNARY
251 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
252 ;
253
254 exp : exp INCREMENT %prec UNARY
255 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
256 ;
257
258 exp : exp DECREMENT %prec UNARY
259 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
260 ;
261
262 exp : SIZEOF exp %prec UNARY
263 { write_exp_elt_opcode (UNOP_SIZEOF); }
264 ;
265
266 exp : exp ARROW name
267 { write_exp_elt_opcode (STRUCTOP_PTR);
268 write_exp_string ($3);
269 write_exp_elt_opcode (STRUCTOP_PTR); }
270 ;
271
272 exp : exp ARROW '*' exp
273 { write_exp_elt_opcode (STRUCTOP_MPTR); }
274 ;
275
276 exp : exp '.' name
277 { write_exp_elt_opcode (STRUCTOP_STRUCT);
278 write_exp_string ($3);
279 write_exp_elt_opcode (STRUCTOP_STRUCT); }
280 ;
281
282 exp : exp '.' '*' exp
283 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
284 ;
285
286 exp : exp '[' exp1 ']'
287 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
288 ;
289
290 exp : exp '('
291 /* This is to save the value of arglist_len
292 being accumulated by an outer function call. */
293 { start_arglist (); }
294 arglist ')' %prec ARROW
295 { write_exp_elt_opcode (OP_FUNCALL);
296 write_exp_elt_longcst ((LONGEST) end_arglist ());
297 write_exp_elt_opcode (OP_FUNCALL); }
298 ;
299
300 arglist :
301 ;
302
303 arglist : exp
304 { arglist_len = 1; }
305 ;
306
307 arglist : arglist ',' exp %prec ABOVE_COMMA
308 { arglist_len++; }
309 ;
310
311 exp : '{' type '}' exp %prec UNARY
312 { write_exp_elt_opcode (UNOP_MEMVAL);
313 write_exp_elt_type ($2);
314 write_exp_elt_opcode (UNOP_MEMVAL); }
315 ;
316
317 exp : '(' type ')' exp %prec UNARY
318 { write_exp_elt_opcode (UNOP_CAST);
319 write_exp_elt_type ($2);
320 write_exp_elt_opcode (UNOP_CAST); }
321 ;
322
323 exp : '(' exp1 ')'
324 { }
325 ;
326
327 /* Binary operators in order of decreasing precedence. */
328
329 exp : exp '@' exp
330 { write_exp_elt_opcode (BINOP_REPEAT); }
331 ;
332
333 exp : exp '*' exp
334 { write_exp_elt_opcode (BINOP_MUL); }
335 ;
336
337 exp : exp '/' exp
338 { write_exp_elt_opcode (BINOP_DIV); }
339 ;
340
341 exp : exp '%' exp
342 { write_exp_elt_opcode (BINOP_REM); }
343 ;
344
345 exp : exp '+' exp
346 { write_exp_elt_opcode (BINOP_ADD); }
347 ;
348
349 exp : exp '-' exp
350 { write_exp_elt_opcode (BINOP_SUB); }
351 ;
352
353 exp : exp LSH exp
354 { write_exp_elt_opcode (BINOP_LSH); }
355 ;
356
357 exp : exp RSH exp
358 { write_exp_elt_opcode (BINOP_RSH); }
359 ;
360
361 exp : exp EQUAL exp
362 { write_exp_elt_opcode (BINOP_EQUAL); }
363 ;
364
365 exp : exp NOTEQUAL exp
366 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
367 ;
368
369 exp : exp LEQ exp
370 { write_exp_elt_opcode (BINOP_LEQ); }
371 ;
372
373 exp : exp GEQ exp
374 { write_exp_elt_opcode (BINOP_GEQ); }
375 ;
376
377 exp : exp '<' exp
378 { write_exp_elt_opcode (BINOP_LESS); }
379 ;
380
381 exp : exp '>' exp
382 { write_exp_elt_opcode (BINOP_GTR); }
383 ;
384
385 exp : exp '&' exp
386 { write_exp_elt_opcode (BINOP_LOGAND); }
387 ;
388
389 exp : exp '^' exp
390 { write_exp_elt_opcode (BINOP_LOGXOR); }
391 ;
392
393 exp : exp '|' exp
394 { write_exp_elt_opcode (BINOP_LOGIOR); }
395 ;
396
397 exp : exp AND exp
398 { write_exp_elt_opcode (BINOP_AND); }
399 ;
400
401 exp : exp OR exp
402 { write_exp_elt_opcode (BINOP_OR); }
403 ;
404
405 exp : exp '?' exp ':' exp %prec '?'
406 { write_exp_elt_opcode (TERNOP_COND); }
407 ;
408
409 exp : exp '=' exp
410 { write_exp_elt_opcode (BINOP_ASSIGN); }
411 ;
412
413 exp : exp ASSIGN_MODIFY exp
414 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
415 write_exp_elt_opcode ($2);
416 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
417 ;
418
419 exp : INT
420 { write_exp_elt_opcode (OP_LONG);
421 if ($1 == (int) $1 || $1 == (unsigned int) $1)
422 write_exp_elt_type (builtin_type_int);
423 else
424 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
425 write_exp_elt_longcst ((LONGEST) $1);
426 write_exp_elt_opcode (OP_LONG); }
427 ;
428
429 exp : NAME_OR_INT
430 { YYSTYPE val;
431 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
432 write_exp_elt_opcode (OP_LONG);
433 if (val.lval == (int) val.lval ||
434 val.lval == (unsigned int) val.lval)
435 write_exp_elt_type (builtin_type_int);
436 else
437 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
438 write_exp_elt_longcst (val.lval);
439 write_exp_elt_opcode (OP_LONG); }
440 ;
441
442 exp : UINT
443 {
444 write_exp_elt_opcode (OP_LONG);
445 if ($1 == (unsigned int) $1)
446 write_exp_elt_type (builtin_type_unsigned_int);
447 else
448 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
449 write_exp_elt_longcst ((LONGEST) $1);
450 write_exp_elt_opcode (OP_LONG);
451 }
452 ;
453
454 exp : NAME_OR_UINT
455 { YYSTYPE val;
456 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
457 write_exp_elt_opcode (OP_LONG);
458 if (val.ulval == (unsigned int) val.ulval)
459 write_exp_elt_type (builtin_type_unsigned_int);
460 else
461 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
462 write_exp_elt_longcst ((LONGEST)val.ulval);
463 write_exp_elt_opcode (OP_LONG);
464 }
465 ;
466
467 exp : CHAR
468 { write_exp_elt_opcode (OP_LONG);
469 write_exp_elt_type (builtin_type_char);
470 write_exp_elt_longcst ((LONGEST) $1);
471 write_exp_elt_opcode (OP_LONG); }
472 ;
473
474 exp : FLOAT
475 { write_exp_elt_opcode (OP_DOUBLE);
476 write_exp_elt_type (builtin_type_double);
477 write_exp_elt_dblcst ($1);
478 write_exp_elt_opcode (OP_DOUBLE); }
479 ;
480
481 exp : variable
482 ;
483
484 exp : LAST
485 { write_exp_elt_opcode (OP_LAST);
486 write_exp_elt_longcst ((LONGEST) $1);
487 write_exp_elt_opcode (OP_LAST); }
488 ;
489
490 exp : REGNAME
491 { write_exp_elt_opcode (OP_REGISTER);
492 write_exp_elt_longcst ((LONGEST) $1);
493 write_exp_elt_opcode (OP_REGISTER); }
494 ;
495
496 exp : VARIABLE
497 { write_exp_elt_opcode (OP_INTERNALVAR);
498 write_exp_elt_intern ($1);
499 write_exp_elt_opcode (OP_INTERNALVAR); }
500 ;
501
502 exp : SIZEOF '(' type ')' %prec UNARY
503 { write_exp_elt_opcode (OP_LONG);
504 write_exp_elt_type (builtin_type_int);
505 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
506 write_exp_elt_opcode (OP_LONG); }
507 ;
508
509 exp : STRING
510 { write_exp_elt_opcode (OP_STRING);
511 write_exp_string ($1);
512 write_exp_elt_opcode (OP_STRING); }
513 ;
514
515 /* C++. */
516 exp : THIS
517 { write_exp_elt_opcode (OP_THIS);
518 write_exp_elt_opcode (OP_THIS); }
519 ;
520
521 /* end of C++. */
522
523 block : BLOCKNAME
524 {
525 if ($1.sym != 0)
526 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
527 else
528 {
529 struct symtab *tem =
530 lookup_symtab (copy_name ($1.stoken));
531 if (tem)
532 $$ = BLOCKVECTOR_BLOCK
533 (BLOCKVECTOR (tem), STATIC_BLOCK);
534 else
535 error ("No file or function \"%s\".",
536 copy_name ($1.stoken));
537 }
538 }
539 ;
540
541 block : block COLONCOLON name
542 { struct symbol *tem
543 = lookup_symbol (copy_name ($3), $1,
544 VAR_NAMESPACE, 0, NULL);
545 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
546 error ("No function \"%s\" in specified context.",
547 copy_name ($3));
548 $$ = SYMBOL_BLOCK_VALUE (tem); }
549 ;
550
551 variable: block COLONCOLON name
552 { struct symbol *sym;
553 sym = lookup_symbol (copy_name ($3), $1,
554 VAR_NAMESPACE, 0, NULL);
555 if (sym == 0)
556 error ("No symbol \"%s\" in specified context.",
557 copy_name ($3));
558 write_exp_elt_opcode (OP_VAR_VALUE);
559 write_exp_elt_sym (sym);
560 write_exp_elt_opcode (OP_VAR_VALUE); }
561 ;
562
563 variable: typebase COLONCOLON name
564 {
565 struct type *type = $1;
566 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
567 && TYPE_CODE (type) != TYPE_CODE_UNION)
568 error ("`%s' is not defined as an aggregate type.",
569 TYPE_NAME (type));
570
571 write_exp_elt_opcode (OP_SCOPE);
572 write_exp_elt_type (type);
573 write_exp_string ($3);
574 write_exp_elt_opcode (OP_SCOPE);
575 }
576 | COLONCOLON name
577 {
578 char *name = copy_name ($2);
579 struct symbol *sym;
580 int i;
581
582 sym =
583 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
584 if (sym)
585 {
586 write_exp_elt_opcode (OP_VAR_VALUE);
587 write_exp_elt_sym (sym);
588 write_exp_elt_opcode (OP_VAR_VALUE);
589 break;
590 }
591 for (i = 0; i < misc_function_count; i++)
592 if (!strcmp (misc_function_vector[i].name, name))
593 break;
594
595 if (i < misc_function_count)
596 {
597 enum misc_function_type mft =
598 misc_function_vector[i].type;
599
600 write_exp_elt_opcode (OP_LONG);
601 write_exp_elt_type (builtin_type_int);
602 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
603 write_exp_elt_opcode (OP_LONG);
604 write_exp_elt_opcode (UNOP_MEMVAL);
605 if (mft == mf_data || mft == mf_bss)
606 write_exp_elt_type (builtin_type_int);
607 else if (mft == mf_text)
608 write_exp_elt_type (lookup_function_type (builtin_type_int));
609 else
610 write_exp_elt_type (builtin_type_char);
611 write_exp_elt_opcode (UNOP_MEMVAL);
612 }
613 else
614 if (symtab_list == 0
615 && partial_symtab_list == 0)
616 error ("No symbol table is loaded. Use the \"file\" command.");
617 else
618 error ("No symbol \"%s\" in current context.", name);
619 }
620 ;
621
622 variable: name_not_typename
623 { struct symbol *sym = $1.sym;
624
625 if (sym)
626 {
627 switch (sym->class)
628 {
629 case LOC_REGISTER:
630 case LOC_ARG:
631 case LOC_LOCAL:
632 case LOC_LOCAL_ARG:
633 if (innermost_block == 0 ||
634 contained_in (block_found,
635 innermost_block))
636 innermost_block = block_found;
637 }
638 write_exp_elt_opcode (OP_VAR_VALUE);
639 write_exp_elt_sym (sym);
640 write_exp_elt_opcode (OP_VAR_VALUE);
641 }
642 else if ($1.is_a_field_of_this)
643 {
644 /* C++: it hangs off of `this'. Must
645 not inadvertently convert from a method call
646 to data ref. */
647 if (innermost_block == 0 ||
648 contained_in (block_found, innermost_block))
649 innermost_block = block_found;
650 write_exp_elt_opcode (OP_THIS);
651 write_exp_elt_opcode (OP_THIS);
652 write_exp_elt_opcode (STRUCTOP_PTR);
653 write_exp_string ($1.stoken);
654 write_exp_elt_opcode (STRUCTOP_PTR);
655 }
656 else
657 {
658 register int i;
659 register char *arg = copy_name ($1.stoken);
660
661 /* FIXME, this search is linear! At least
662 optimize the strcmp with a 1-char cmp... */
663 for (i = 0; i < misc_function_count; i++)
664 if (!strcmp (misc_function_vector[i].name, arg))
665 break;
666
667 if (i < misc_function_count)
668 {
669 enum misc_function_type mft =
670 misc_function_vector[i].type;
671
672 write_exp_elt_opcode (OP_LONG);
673 write_exp_elt_type (builtin_type_int);
674 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
675 write_exp_elt_opcode (OP_LONG);
676 write_exp_elt_opcode (UNOP_MEMVAL);
677 if (mft == mf_data || mft == mf_bss)
678 write_exp_elt_type (builtin_type_int);
679 else if (mft == mf_text)
680 write_exp_elt_type (lookup_function_type (builtin_type_int));
681 else
682 write_exp_elt_type (builtin_type_char);
683 write_exp_elt_opcode (UNOP_MEMVAL);
684 }
685 else if (symtab_list == 0
686 && partial_symtab_list == 0)
687 error ("No symbol table is loaded. Use the \"file\" command.");
688 else
689 error ("No symbol \"%s\" in current context.",
690 copy_name ($1.stoken));
691 }
692 }
693 ;
694
695
696 ptype : typebase
697 | typebase abs_decl
698 {
699 /* This is where the interesting stuff happens. */
700 int done = 0;
701 int array_size;
702 struct type *follow_type = $1;
703
704 while (!done)
705 switch (pop_type ())
706 {
707 case tp_end:
708 done = 1;
709 break;
710 case tp_pointer:
711 follow_type = lookup_pointer_type (follow_type);
712 break;
713 case tp_reference:
714 follow_type = lookup_reference_type (follow_type);
715 break;
716 case tp_array:
717 array_size = (int) pop_type ();
718 if (array_size != -1)
719 follow_type = create_array_type (follow_type,
720 array_size);
721 else
722 follow_type = lookup_pointer_type (follow_type);
723 break;
724 case tp_function:
725 follow_type = lookup_function_type (follow_type);
726 break;
727 }
728 $$ = follow_type;
729 }
730 ;
731
732 abs_decl: '*'
733 { push_type (tp_pointer); $$ = 0; }
734 | '*' abs_decl
735 { push_type (tp_pointer); $$ = $2; }
736 | direct_abs_decl
737 ;
738
739 direct_abs_decl: '(' abs_decl ')'
740 { $$ = $2; }
741 | direct_abs_decl array_mod
742 {
743 push_type ((enum type_pieces) $2);
744 push_type (tp_array);
745 }
746 | array_mod
747 {
748 push_type ((enum type_pieces) $1);
749 push_type (tp_array);
750 $$ = 0;
751 }
752 | direct_abs_decl func_mod
753 { push_type (tp_function); }
754 | func_mod
755 { push_type (tp_function); }
756 ;
757
758 array_mod: '[' ']'
759 { $$ = -1; }
760 | '[' INT ']'
761 { $$ = $2; }
762 ;
763
764 func_mod: '(' ')'
765 { $$ = 0; }
766 ;
767
768 type : ptype
769 | typebase COLONCOLON '*'
770 { $$ = lookup_member_type (builtin_type_int, $1); }
771 | type '(' typebase COLONCOLON '*' ')'
772 { $$ = lookup_member_type ($1, $3); }
773 | type '(' typebase COLONCOLON '*' ')' '(' ')'
774 { $$ = lookup_member_type
775 (lookup_function_type ($1), $3); }
776 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
777 { $$ = lookup_member_type
778 (lookup_function_type ($1), $3);
779 free ($8); }
780 ;
781
782 typebase
783 : TYPENAME
784 { $$ = $1.type; }
785 | INT_KEYWORD
786 { $$ = builtin_type_int; }
787 | LONG
788 { $$ = builtin_type_long; }
789 | SHORT
790 { $$ = builtin_type_short; }
791 | LONG INT_KEYWORD
792 { $$ = builtin_type_long; }
793 | UNSIGNED LONG INT_KEYWORD
794 { $$ = builtin_type_unsigned_long; }
795 | SHORT INT_KEYWORD
796 { $$ = builtin_type_short; }
797 | UNSIGNED SHORT INT_KEYWORD
798 { $$ = builtin_type_unsigned_short; }
799 | STRUCT name
800 { $$ = lookup_struct (copy_name ($2),
801 expression_context_block); }
802 | UNION name
803 { $$ = lookup_union (copy_name ($2),
804 expression_context_block); }
805 | ENUM name
806 { $$ = lookup_enum (copy_name ($2),
807 expression_context_block); }
808 | UNSIGNED typename
809 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
810 | UNSIGNED
811 { $$ = builtin_type_unsigned_int; }
812 | SIGNED typename
813 { $$ = $2.type; }
814 | SIGNED
815 { $$ = builtin_type_int; }
816 ;
817
818 typename: TYPENAME
819 | INT_KEYWORD
820 {
821 $$.stoken.ptr = "int";
822 $$.stoken.length = 3;
823 $$.type = builtin_type_int;
824 }
825 | LONG
826 {
827 $$.stoken.ptr = "long";
828 $$.stoken.length = 4;
829 $$.type = builtin_type_long;
830 }
831 | SHORT
832 {
833 $$.stoken.ptr = "short";
834 $$.stoken.length = 5;
835 $$.type = builtin_type_short;
836 }
837 ;
838
839 nonempty_typelist
840 : type
841 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
842 $$[0] = (struct type *)0;
843 $$[1] = $1;
844 }
845 | nonempty_typelist ',' type
846 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
847 $$ = (struct type **)xrealloc ($1, len);
848 $$[$<ivec>$[0]] = $3;
849 }
850 ;
851
852 name : NAME { $$ = $1.stoken; }
853 | BLOCKNAME { $$ = $1.stoken; }
854 | TYPENAME { $$ = $1.stoken; }
855 | NAME_OR_INT { $$ = $1.stoken; }
856 | NAME_OR_UINT { $$ = $1.stoken; }
857 ;
858
859 name_not_typename : NAME
860 | BLOCKNAME
861 /* These would be useful if name_not_typename was useful, but it is just
862 a fake for "variable", so these cause reduce/reduce conflicts because
863 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
864 =exp) or just an exp. If name_not_typename was ever used in an lvalue
865 context where only a name could occur, this might be useful.
866 | NAME_OR_INT
867 | NAME_OR_UINT
868 */
869 ;
870
871 %%
872 \f
873 /* Begin counting arguments for a function call,
874 saving the data about any containing call. */
875
876 static void
877 start_arglist ()
878 {
879 register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
880
881 new->next = funcall_chain;
882 new->arglist_len = arglist_len;
883 arglist_len = 0;
884 funcall_chain = new;
885 }
886
887 /* Return the number of arguments in a function call just terminated,
888 and restore the data for the containing function call. */
889
890 static int
891 end_arglist ()
892 {
893 register int val = arglist_len;
894 register struct funcall *call = funcall_chain;
895 funcall_chain = call->next;
896 arglist_len = call->arglist_len;
897 free (call);
898 return val;
899 }
900
901 /* Free everything in the funcall chain.
902 Used when there is an error inside parsing. */
903
904 static void
905 free_funcalls ()
906 {
907 register struct funcall *call, *next;
908
909 for (call = funcall_chain; call; call = next)
910 {
911 next = call->next;
912 free (call);
913 }
914 }
915 \f
916 /* This page contains the functions for adding data to the struct expression
917 being constructed. */
918
919 /* Add one element to the end of the expression. */
920
921 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
922 a register through here */
923
924 static void
925 write_exp_elt (expelt)
926 union exp_element expelt;
927 {
928 if (expout_ptr >= expout_size)
929 {
930 expout_size *= 2;
931 expout = (struct expression *) xrealloc (expout,
932 sizeof (struct expression)
933 + expout_size * sizeof (union exp_element));
934 }
935 expout->elts[expout_ptr++] = expelt;
936 }
937
938 static void
939 write_exp_elt_opcode (expelt)
940 enum exp_opcode expelt;
941 {
942 union exp_element tmp;
943
944 tmp.opcode = expelt;
945
946 write_exp_elt (tmp);
947 }
948
949 static void
950 write_exp_elt_sym (expelt)
951 struct symbol *expelt;
952 {
953 union exp_element tmp;
954
955 tmp.symbol = expelt;
956
957 write_exp_elt (tmp);
958 }
959
960 static void
961 write_exp_elt_longcst (expelt)
962 LONGEST expelt;
963 {
964 union exp_element tmp;
965
966 tmp.longconst = expelt;
967
968 write_exp_elt (tmp);
969 }
970
971 static void
972 write_exp_elt_dblcst (expelt)
973 double expelt;
974 {
975 union exp_element tmp;
976
977 tmp.doubleconst = expelt;
978
979 write_exp_elt (tmp);
980 }
981
982 static void
983 write_exp_elt_type (expelt)
984 struct type *expelt;
985 {
986 union exp_element tmp;
987
988 tmp.type = expelt;
989
990 write_exp_elt (tmp);
991 }
992
993 static void
994 write_exp_elt_intern (expelt)
995 struct internalvar *expelt;
996 {
997 union exp_element tmp;
998
999 tmp.internalvar = expelt;
1000
1001 write_exp_elt (tmp);
1002 }
1003
1004 /* Add a string constant to the end of the expression.
1005 Follow it by its length in bytes, as a separate exp_element. */
1006
1007 static void
1008 write_exp_string (str)
1009 struct stoken str;
1010 {
1011 register int len = str.length;
1012 register int lenelt
1013 = (len + sizeof (union exp_element)) / sizeof (union exp_element);
1014
1015 expout_ptr += lenelt;
1016
1017 if (expout_ptr >= expout_size)
1018 {
1019 expout_size = max (expout_size * 2, expout_ptr + 10);
1020 expout = (struct expression *)
1021 xrealloc (expout, (sizeof (struct expression)
1022 + (expout_size * sizeof (union exp_element))));
1023 }
1024 bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len);
1025 ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0;
1026 write_exp_elt_longcst ((LONGEST) len);
1027 }
1028 \f
1029 /* During parsing of a C expression, the pointer to the next character
1030 is in this variable. */
1031
1032 static char *lexptr;
1033
1034 /* Tokens that refer to names do so with explicit pointer and length,
1035 so they can share the storage that lexptr is parsing.
1036
1037 When it is necessary to pass a name to a function that expects
1038 a null-terminated string, the substring is copied out
1039 into a block of storage that namecopy points to.
1040
1041 namecopy is allocated once, guaranteed big enough, for each parsing. */
1042
1043 static char *namecopy;
1044
1045 /* Current depth in parentheses within the expression. */
1046
1047 static int paren_depth;
1048
1049 /* Nonzero means stop parsing on first comma (if not within parentheses). */
1050
1051 static int comma_terminates;
1052
1053 /* Take care of parsing a number (anything that starts with a digit).
1054 Set yylval and return the token type; update lexptr.
1055 LEN is the number of characters in it. */
1056
1057 /*** Needs some error checking for the float case ***/
1058
1059 static int
1060 parse_number (p, len, parsed_float, putithere)
1061 register char *p;
1062 register int len;
1063 int parsed_float;
1064 YYSTYPE *putithere;
1065 {
1066 register LONGEST n = 0;
1067 register int i;
1068 register int c;
1069 register int base = input_radix;
1070 int unsigned_p = 0;
1071
1072 extern double atof ();
1073
1074 if (parsed_float)
1075 {
1076 /* It's a float since it contains a point or an exponent. */
1077 putithere->dval = atof (p);
1078 return FLOAT;
1079 }
1080
1081 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1082 if (p[0] == '0')
1083 switch (p[1])
1084 {
1085 case 'x':
1086 case 'X':
1087 if (len >= 3)
1088 {
1089 p += 2;
1090 base = 16;
1091 len -= 2;
1092 }
1093 break;
1094
1095 case 't':
1096 case 'T':
1097 case 'd':
1098 case 'D':
1099 if (len >= 3)
1100 {
1101 p += 2;
1102 base = 10;
1103 len -= 2;
1104 }
1105 break;
1106
1107 default:
1108 base = 8;
1109 break;
1110 }
1111
1112 while (len-- > 0)
1113 {
1114 c = *p++;
1115 if (c >= 'A' && c <= 'Z')
1116 c += 'a' - 'A';
1117 if (c != 'l' && c != 'u')
1118 n *= base;
1119 if (c >= '0' && c <= '9')
1120 n += i = c - '0';
1121 else
1122 {
1123 if (base > 10 && c >= 'a' && c <= 'f')
1124 n += i = c - 'a' + 10;
1125 else if (len == 0 && c == 'l')
1126 ;
1127 else if (len == 0 && c == 'u')
1128 unsigned_p = 1;
1129 else
1130 return ERROR; /* Char not a digit */
1131 }
1132 if (i >= base)
1133 return ERROR; /* Invalid digit in this base */
1134 }
1135
1136 if (unsigned_p)
1137 {
1138 putithere->ulval = n;
1139 return UINT;
1140 }
1141 else
1142 {
1143 putithere->lval = n;
1144 return INT;
1145 }
1146 }
1147
1148 struct token
1149 {
1150 char *operator;
1151 int token;
1152 enum exp_opcode opcode;
1153 };
1154
1155 static struct token tokentab3[] =
1156 {
1157 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1158 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1159 };
1160
1161 static struct token tokentab2[] =
1162 {
1163 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1164 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1165 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1166 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1167 {"%=", ASSIGN_MODIFY, BINOP_REM},
1168 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1169 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1170 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1171 {"++", INCREMENT, BINOP_END},
1172 {"--", DECREMENT, BINOP_END},
1173 {"->", ARROW, BINOP_END},
1174 {"&&", AND, BINOP_END},
1175 {"||", OR, BINOP_END},
1176 {"::", COLONCOLON, BINOP_END},
1177 {"<<", LSH, BINOP_END},
1178 {">>", RSH, BINOP_END},
1179 {"==", EQUAL, BINOP_END},
1180 {"!=", NOTEQUAL, BINOP_END},
1181 {"<=", LEQ, BINOP_END},
1182 {">=", GEQ, BINOP_END}
1183 };
1184
1185 /* assign machine-independent names to certain registers
1186 * (unless overridden by the REGISTER_NAMES table)
1187 */
1188 struct std_regs {
1189 char *name;
1190 int regnum;
1191 } std_regs[] = {
1192 #ifdef PC_REGNUM
1193 { "pc", PC_REGNUM },
1194 #endif
1195 #ifdef FP_REGNUM
1196 { "fp", FP_REGNUM },
1197 #endif
1198 #ifdef SP_REGNUM
1199 { "sp", SP_REGNUM },
1200 #endif
1201 #ifdef PS_REGNUM
1202 { "ps", PS_REGNUM },
1203 #endif
1204 };
1205
1206 #define NUM_STD_REGS (sizeof std_regs / sizeof std_regs[0])
1207
1208 /* Read one token, getting characters through lexptr. */
1209
1210 static int
1211 yylex ()
1212 {
1213 register int c;
1214 register int namelen;
1215 register unsigned i;
1216 register char *tokstart;
1217
1218 retry:
1219
1220 tokstart = lexptr;
1221 /* See if it is a special token of length 3. */
1222 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1223 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1224 {
1225 lexptr += 3;
1226 yylval.opcode = tokentab3[i].opcode;
1227 return tokentab3[i].token;
1228 }
1229
1230 /* See if it is a special token of length 2. */
1231 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1232 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1233 {
1234 lexptr += 2;
1235 yylval.opcode = tokentab2[i].opcode;
1236 return tokentab2[i].token;
1237 }
1238
1239 switch (c = *tokstart)
1240 {
1241 case 0:
1242 return 0;
1243
1244 case ' ':
1245 case '\t':
1246 case '\n':
1247 lexptr++;
1248 goto retry;
1249
1250 case '\'':
1251 lexptr++;
1252 c = *lexptr++;
1253 if (c == '\\')
1254 c = parse_escape (&lexptr);
1255 yylval.lval = c;
1256 c = *lexptr++;
1257 if (c != '\'')
1258 error ("Invalid character constant.");
1259 return CHAR;
1260
1261 case '(':
1262 paren_depth++;
1263 lexptr++;
1264 return c;
1265
1266 case ')':
1267 if (paren_depth == 0)
1268 return 0;
1269 paren_depth--;
1270 lexptr++;
1271 return c;
1272
1273 case ',':
1274 if (comma_terminates && paren_depth == 0)
1275 return 0;
1276 lexptr++;
1277 return c;
1278
1279 case '.':
1280 /* Might be a floating point number. */
1281 if (lexptr[1] < '0' || lexptr[1] > '9')
1282 goto symbol; /* Nope, must be a symbol. */
1283 /* FALL THRU into number case. */
1284
1285 case '0':
1286 case '1':
1287 case '2':
1288 case '3':
1289 case '4':
1290 case '5':
1291 case '6':
1292 case '7':
1293 case '8':
1294 case '9':
1295 {
1296 /* It's a number. */
1297 int got_dot = 0, got_e = 0, toktype;
1298 register char *p = tokstart;
1299 int hex = input_radix > 10;
1300
1301 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1302 {
1303 p += 2;
1304 hex = 1;
1305 }
1306 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1307 {
1308 p += 2;
1309 hex = 0;
1310 }
1311
1312 for (;; ++p)
1313 {
1314 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1315 got_dot = got_e = 1;
1316 else if (!hex && !got_dot && *p == '.')
1317 got_dot = 1;
1318 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1319 && (*p == '-' || *p == '+'))
1320 /* This is the sign of the exponent, not the end of the
1321 number. */
1322 continue;
1323 /* We will take any letters or digits. parse_number will
1324 complain if past the radix, or if L or U are not final. */
1325 else if ((*p < '0' || *p > '9')
1326 && ((*p < 'a' || *p > 'z')
1327 && (*p < 'A' || *p > 'Z')))
1328 break;
1329 }
1330 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1331 if (toktype == ERROR)
1332 {
1333 char *err_copy = (char *) alloca (p - tokstart + 1);
1334
1335 bcopy (tokstart, err_copy, p - tokstart);
1336 err_copy[p - tokstart] = 0;
1337 error ("Invalid number \"%s\".", err_copy);
1338 }
1339 lexptr = p;
1340 return toktype;
1341 }
1342
1343 case '+':
1344 case '-':
1345 case '*':
1346 case '/':
1347 case '%':
1348 case '|':
1349 case '&':
1350 case '^':
1351 case '~':
1352 case '!':
1353 case '@':
1354 case '<':
1355 case '>':
1356 case '[':
1357 case ']':
1358 case '?':
1359 case ':':
1360 case '=':
1361 case '{':
1362 case '}':
1363 symbol:
1364 lexptr++;
1365 return c;
1366
1367 case '"':
1368 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1369 if (c == '\\')
1370 {
1371 c = tokstart[++namelen];
1372 if (c >= '0' && c <= '9')
1373 {
1374 c = tokstart[++namelen];
1375 if (c >= '0' && c <= '9')
1376 c = tokstart[++namelen];
1377 }
1378 }
1379 yylval.sval.ptr = tokstart + 1;
1380 yylval.sval.length = namelen - 1;
1381 lexptr += namelen + 1;
1382 return STRING;
1383 }
1384
1385 if (!(c == '_' || c == '$'
1386 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1387 /* We must have come across a bad character (e.g. ';'). */
1388 error ("Invalid character '%c' in expression.", c);
1389
1390 /* It's a name. See how long it is. */
1391 namelen = 0;
1392 for (c = tokstart[namelen];
1393 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1394 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1395 c = tokstart[++namelen])
1396 ;
1397
1398 /* The token "if" terminates the expression and is NOT
1399 removed from the input stream. */
1400 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1401 {
1402 return 0;
1403 }
1404
1405 lexptr += namelen;
1406
1407 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1408 and $$digits (equivalent to $<-digits> if you could type that).
1409 Make token type LAST, and put the number (the digits) in yylval. */
1410
1411 if (*tokstart == '$')
1412 {
1413 register int negate = 0;
1414 c = 1;
1415 /* Double dollar means negate the number and add -1 as well.
1416 Thus $$ alone means -1. */
1417 if (namelen >= 2 && tokstart[1] == '$')
1418 {
1419 negate = 1;
1420 c = 2;
1421 }
1422 if (c == namelen)
1423 {
1424 /* Just dollars (one or two) */
1425 yylval.lval = - negate;
1426 return LAST;
1427 }
1428 /* Is the rest of the token digits? */
1429 for (; c < namelen; c++)
1430 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1431 break;
1432 if (c == namelen)
1433 {
1434 yylval.lval = atoi (tokstart + 1 + negate);
1435 if (negate)
1436 yylval.lval = - yylval.lval;
1437 return LAST;
1438 }
1439 }
1440
1441 /* Handle tokens that refer to machine registers:
1442 $ followed by a register name. */
1443
1444 if (*tokstart == '$') {
1445 for (c = 0; c < NUM_REGS; c++)
1446 if (namelen - 1 == strlen (reg_names[c])
1447 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1448 {
1449 yylval.lval = c;
1450 return REGNAME;
1451 }
1452 for (c = 0; c < NUM_STD_REGS; c++)
1453 if (namelen - 1 == strlen (std_regs[c].name)
1454 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1455 {
1456 yylval.lval = std_regs[c].regnum;
1457 return REGNAME;
1458 }
1459 }
1460 /* Catch specific keywords. Should be done with a data structure. */
1461 switch (namelen)
1462 {
1463 case 8:
1464 if (!strncmp (tokstart, "unsigned", 8))
1465 return UNSIGNED;
1466 break;
1467 case 6:
1468 if (!strncmp (tokstart, "struct", 6))
1469 return STRUCT;
1470 if (!strncmp (tokstart, "signed", 6))
1471 return SIGNED;
1472 if (!strncmp (tokstart, "sizeof", 6))
1473 return SIZEOF;
1474 break;
1475 case 5:
1476 if (!strncmp (tokstart, "union", 5))
1477 return UNION;
1478 if (!strncmp (tokstart, "short", 5))
1479 return SHORT;
1480 break;
1481 case 4:
1482 if (!strncmp (tokstart, "enum", 4))
1483 return ENUM;
1484 if (!strncmp (tokstart, "long", 4))
1485 return LONG;
1486 if (!strncmp (tokstart, "this", 4))
1487 {
1488 static const char this_name[] =
1489 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1490
1491 if (lookup_symbol (this_name, expression_context_block,
1492 VAR_NAMESPACE, 0, NULL))
1493 return THIS;
1494 }
1495 break;
1496 case 3:
1497 if (!strncmp (tokstart, "int", 3))
1498 return INT_KEYWORD;
1499 break;
1500 default:
1501 break;
1502 }
1503
1504 yylval.sval.ptr = tokstart;
1505 yylval.sval.length = namelen;
1506
1507 /* Any other names starting in $ are debugger internal variables. */
1508
1509 if (*tokstart == '$')
1510 {
1511 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1512 return VARIABLE;
1513 }
1514
1515 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1516 functions or symtabs. If this is not so, then ...
1517 Use token-type TYPENAME for symbols that happen to be defined
1518 currently as names of types; NAME for other symbols.
1519 The caller is not constrained to care about the distinction. */
1520 {
1521 char *tmp = copy_name (yylval.sval);
1522 struct symbol *sym;
1523 int is_a_field_of_this = 0;
1524 int hextype;
1525
1526 sym = lookup_symbol (tmp, expression_context_block,
1527 VAR_NAMESPACE, &is_a_field_of_this, NULL);
1528 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1529 lookup_partial_symtab (tmp))
1530 {
1531 yylval.ssym.sym = sym;
1532 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1533 return BLOCKNAME;
1534 }
1535 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1536 {
1537 yylval.tsym.type = SYMBOL_TYPE (sym);
1538 return TYPENAME;
1539 }
1540 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1541 return TYPENAME;
1542
1543 /* Input names that aren't symbols but ARE valid hex numbers,
1544 when the input radix permits them, can be names or numbers
1545 depending on the parse. Note we support radixes > 16 here. */
1546 if (!sym &&
1547 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1548 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1549 {
1550 YYSTYPE newlval; /* Its value is ignored. */
1551 hextype = parse_number (tokstart, namelen, 0, &newlval);
1552 if (hextype == INT)
1553 {
1554 yylval.ssym.sym = sym;
1555 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1556 return NAME_OR_INT;
1557 }
1558 if (hextype == UINT)
1559 {
1560 yylval.ssym.sym = sym;
1561 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1562 return NAME_OR_UINT;
1563 }
1564 }
1565
1566 /* Any other kind of symbol */
1567 yylval.ssym.sym = sym;
1568 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1569 return NAME;
1570 }
1571 }
1572
1573 static void
1574 yyerror (msg)
1575 char *msg;
1576 {
1577 error ("Invalid syntax in expression.");
1578 }
1579
1580 /* Return a null-terminated temporary copy of the name
1581 of a string token. */
1582
1583 static char *
1584 copy_name (token)
1585 struct stoken token;
1586 {
1587 bcopy (token.ptr, namecopy, token.length);
1588 namecopy[token.length] = 0;
1589 return namecopy;
1590 }
1591 \f
1592 /* Reverse an expression from suffix form (in which it is constructed)
1593 to prefix form (in which we can conveniently print or execute it). */
1594
1595 static void prefixify_subexp ();
1596
1597 static void
1598 prefixify_expression (expr)
1599 register struct expression *expr;
1600 {
1601 register int len = sizeof (struct expression) +
1602 expr->nelts * sizeof (union exp_element);
1603 register struct expression *temp;
1604 register int inpos = expr->nelts, outpos = 0;
1605
1606 temp = (struct expression *) alloca (len);
1607
1608 /* Copy the original expression into temp. */
1609 bcopy (expr, temp, len);
1610
1611 prefixify_subexp (temp, expr, inpos, outpos);
1612 }
1613
1614 /* Return the number of exp_elements in the subexpression of EXPR
1615 whose last exp_element is at index ENDPOS - 1 in EXPR. */
1616
1617 static int
1618 length_of_subexp (expr, endpos)
1619 register struct expression *expr;
1620 register int endpos;
1621 {
1622 register int oplen = 1;
1623 register int args = 0;
1624 register int i;
1625
1626 if (endpos < 0)
1627 error ("?error in length_of_subexp");
1628
1629 i = (int) expr->elts[endpos - 1].opcode;
1630
1631 switch (i)
1632 {
1633 /* C++ */
1634 case OP_SCOPE:
1635 oplen = 4 + ((expr->elts[endpos - 2].longconst
1636 + sizeof (union exp_element))
1637 / sizeof (union exp_element));
1638 break;
1639
1640 case OP_LONG:
1641 case OP_DOUBLE:
1642 oplen = 4;
1643 break;
1644
1645 case OP_VAR_VALUE:
1646 case OP_LAST:
1647 case OP_REGISTER:
1648 case OP_INTERNALVAR:
1649 oplen = 3;
1650 break;
1651
1652 case OP_FUNCALL:
1653 oplen = 3;
1654 args = 1 + expr->elts[endpos - 2].longconst;
1655 break;
1656
1657 case UNOP_CAST:
1658 case UNOP_MEMVAL:
1659 oplen = 3;
1660 args = 1;
1661 break;
1662
1663 case STRUCTOP_STRUCT:
1664 case STRUCTOP_PTR:
1665 args = 1;
1666 case OP_STRING:
1667 oplen = 3 + ((expr->elts[endpos - 2].longconst
1668 + sizeof (union exp_element))
1669 / sizeof (union exp_element));
1670 break;
1671
1672 case TERNOP_COND:
1673 args = 3;
1674 break;
1675
1676 case BINOP_ASSIGN_MODIFY:
1677 oplen = 3;
1678 args = 2;
1679 break;
1680
1681 /* C++ */
1682 case OP_THIS:
1683 oplen = 2;
1684 break;
1685
1686 default:
1687 args = 1 + (i < (int) BINOP_END);
1688 }
1689
1690 while (args > 0)
1691 {
1692 oplen += length_of_subexp (expr, endpos - oplen);
1693 args--;
1694 }
1695
1696 return oplen;
1697 }
1698
1699 /* Copy the subexpression ending just before index INEND in INEXPR
1700 into OUTEXPR, starting at index OUTBEG.
1701 In the process, convert it from suffix to prefix form. */
1702
1703 static void
1704 prefixify_subexp (inexpr, outexpr, inend, outbeg)
1705 register struct expression *inexpr;
1706 struct expression *outexpr;
1707 register int inend;
1708 int outbeg;
1709 {
1710 register int oplen = 1;
1711 register int args = 0;
1712 register int i;
1713 int *arglens;
1714 enum exp_opcode opcode;
1715
1716 /* Compute how long the last operation is (in OPLEN),
1717 and also how many preceding subexpressions serve as
1718 arguments for it (in ARGS). */
1719
1720 opcode = inexpr->elts[inend - 1].opcode;
1721 switch (opcode)
1722 {
1723 /* C++ */
1724 case OP_SCOPE:
1725 oplen = 4 + ((inexpr->elts[inend - 2].longconst
1726 + sizeof (union exp_element))
1727 / sizeof (union exp_element));
1728 break;
1729
1730 case OP_LONG:
1731 case OP_DOUBLE:
1732 oplen = 4;
1733 break;
1734
1735 case OP_VAR_VALUE:
1736 case OP_LAST:
1737 case OP_REGISTER:
1738 case OP_INTERNALVAR:
1739 oplen = 3;
1740 break;
1741
1742 case OP_FUNCALL:
1743 oplen = 3;
1744 args = 1 + inexpr->elts[inend - 2].longconst;
1745 break;
1746
1747 case UNOP_CAST:
1748 case UNOP_MEMVAL:
1749 oplen = 3;
1750 args = 1;
1751 break;
1752
1753 case STRUCTOP_STRUCT:
1754 case STRUCTOP_PTR:
1755 args = 1;
1756 case OP_STRING:
1757 oplen = 3 + ((inexpr->elts[inend - 2].longconst
1758 + sizeof (union exp_element))
1759 / sizeof (union exp_element));
1760
1761 break;
1762
1763 case TERNOP_COND:
1764 args = 3;
1765 break;
1766
1767 case BINOP_ASSIGN_MODIFY:
1768 oplen = 3;
1769 args = 2;
1770 break;
1771
1772 /* C++ */
1773 case OP_THIS:
1774 oplen = 2;
1775 break;
1776
1777 default:
1778 args = 1 + ((int) opcode < (int) BINOP_END);
1779 }
1780
1781 /* Copy the final operator itself, from the end of the input
1782 to the beginning of the output. */
1783 inend -= oplen;
1784 bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg],
1785 oplen * sizeof (union exp_element));
1786 outbeg += oplen;
1787
1788 /* Find the lengths of the arg subexpressions. */
1789 arglens = (int *) alloca (args * sizeof (int));
1790 for (i = args - 1; i >= 0; i--)
1791 {
1792 oplen = length_of_subexp (inexpr, inend);
1793 arglens[i] = oplen;
1794 inend -= oplen;
1795 }
1796
1797 /* Now copy each subexpression, preserving the order of
1798 the subexpressions, but prefixifying each one.
1799 In this loop, inend starts at the beginning of
1800 the expression this level is working on
1801 and marches forward over the arguments.
1802 outbeg does similarly in the output. */
1803 for (i = 0; i < args; i++)
1804 {
1805 oplen = arglens[i];
1806 inend += oplen;
1807 prefixify_subexp (inexpr, outexpr, inend, outbeg);
1808 outbeg += oplen;
1809 }
1810 }
1811 \f
1812 /* This page contains the two entry points to this file. */
1813
1814 /* Read a C expression from the string *STRINGPTR points to,
1815 parse it, and return a pointer to a struct expression that we malloc.
1816 Use block BLOCK as the lexical context for variable names;
1817 if BLOCK is zero, use the block of the selected stack frame.
1818 Meanwhile, advance *STRINGPTR to point after the expression,
1819 at the first nonwhite character that is not part of the expression
1820 (possibly a null character).
1821
1822 If COMMA is nonzero, stop if a comma is reached. */
1823
1824 struct expression *
1825 parse_c_1 (stringptr, block, comma)
1826 char **stringptr;
1827 struct block *block;
1828 int comma;
1829 {
1830 struct cleanup *old_chain;
1831
1832 lexptr = *stringptr;
1833
1834 paren_depth = 0;
1835 type_stack_depth = 0;
1836
1837 comma_terminates = comma;
1838
1839 if (lexptr == 0 || *lexptr == 0)
1840 error_no_arg ("expression to compute");
1841
1842 old_chain = make_cleanup (free_funcalls, 0);
1843 funcall_chain = 0;
1844
1845 expression_context_block = block ? block : get_selected_block ();
1846
1847 namecopy = (char *) alloca (strlen (lexptr) + 1);
1848 expout_size = 10;
1849 expout_ptr = 0;
1850 expout = (struct expression *)
1851 xmalloc (sizeof (struct expression)
1852 + expout_size * sizeof (union exp_element));
1853 make_cleanup (free_current_contents, &expout);
1854 if (yyparse ())
1855 yyerror (NULL);
1856 discard_cleanups (old_chain);
1857 expout->nelts = expout_ptr;
1858 expout = (struct expression *)
1859 xrealloc (expout,
1860 sizeof (struct expression)
1861 + expout_ptr * sizeof (union exp_element));
1862 prefixify_expression (expout);
1863 *stringptr = lexptr;
1864 return expout;
1865 }
1866
1867 /* Parse STRING as an expression, and complain if this fails
1868 to use up all of the contents of STRING. */
1869
1870 struct expression *
1871 parse_c_expression (string)
1872 char *string;
1873 {
1874 register struct expression *exp;
1875 exp = parse_c_1 (&string, 0, 0);
1876 if (*string)
1877 error ("Junk after end of expression.");
1878 return exp;
1879 }
1880
1881 static void
1882 push_type (tp)
1883 enum type_pieces tp;
1884 {
1885 if (type_stack_depth == type_stack_size)
1886 {
1887 type_stack_size *= 2;
1888 type_stack = (enum type_pieces *)
1889 xrealloc (type_stack, type_stack_size * sizeof (enum type_pieces));
1890 }
1891 type_stack[type_stack_depth++] = tp;
1892 }
1893
1894 static enum type_pieces
1895 pop_type ()
1896 {
1897 if (type_stack_depth)
1898 return type_stack[--type_stack_depth];
1899 return tp_end;
1900 }
1901
1902 void
1903 _initialize_expread ()
1904 {
1905 type_stack_size = 80;
1906 type_stack_depth = 0;
1907 type_stack = (enum type_pieces *)
1908 xmalloc (type_stack_size * sizeof (enum type_pieces));
1909 }
This page took 0.06882 seconds and 5 git commands to generate.