44b1b5c6f3d9282844cb2a95cd8fac0252cb53b6
[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 | typebase COLONCOLON '~' name
577 {
578 struct type *type = $1;
579 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
580 && TYPE_CODE (type) != TYPE_CODE_UNION)
581 error ("`%s' is not defined as an aggregate type.",
582 TYPE_NAME (type));
583
584 if (strcmp (type_name_no_tag (type), $4.ptr))
585 error ("invalid destructor `%s::~%s'",
586 type_name_no_tag (type), $4.ptr);
587
588 write_exp_elt_opcode (OP_SCOPE);
589 write_exp_elt_type (type);
590 write_exp_string ($4);
591 write_exp_elt_opcode (OP_SCOPE);
592 write_exp_elt_opcode (UNOP_LOGNOT);
593 }
594 | COLONCOLON name
595 {
596 char *name = copy_name ($2);
597 struct symbol *sym;
598 int i;
599
600 sym =
601 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
602 if (sym)
603 {
604 write_exp_elt_opcode (OP_VAR_VALUE);
605 write_exp_elt_sym (sym);
606 write_exp_elt_opcode (OP_VAR_VALUE);
607 break;
608 }
609 for (i = 0; i < misc_function_count; i++)
610 if (!strcmp (misc_function_vector[i].name, name))
611 break;
612
613 if (i < misc_function_count)
614 {
615 enum misc_function_type mft =
616 misc_function_vector[i].type;
617
618 write_exp_elt_opcode (OP_LONG);
619 write_exp_elt_type (builtin_type_int);
620 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
621 write_exp_elt_opcode (OP_LONG);
622 write_exp_elt_opcode (UNOP_MEMVAL);
623 if (mft == mf_data || mft == mf_bss)
624 write_exp_elt_type (builtin_type_int);
625 else if (mft == mf_text)
626 write_exp_elt_type (lookup_function_type (builtin_type_int));
627 else
628 write_exp_elt_type (builtin_type_char);
629 write_exp_elt_opcode (UNOP_MEMVAL);
630 }
631 else
632 if (symtab_list == 0
633 && partial_symtab_list == 0)
634 error ("No symbol table is loaded. Use the \"file\" command.");
635 else
636 error ("No symbol \"%s\" in current context.", name);
637 }
638 ;
639
640 variable: name_not_typename
641 { struct symbol *sym = $1.sym;
642
643 if (sym)
644 {
645 switch (sym->class)
646 {
647 case LOC_REGISTER:
648 case LOC_ARG:
649 case LOC_LOCAL:
650 case LOC_LOCAL_ARG:
651 if (innermost_block == 0 ||
652 contained_in (block_found,
653 innermost_block))
654 innermost_block = block_found;
655 }
656 write_exp_elt_opcode (OP_VAR_VALUE);
657 write_exp_elt_sym (sym);
658 write_exp_elt_opcode (OP_VAR_VALUE);
659 }
660 else if ($1.is_a_field_of_this)
661 {
662 /* C++: it hangs off of `this'. Must
663 not inadvertently convert from a method call
664 to data ref. */
665 if (innermost_block == 0 ||
666 contained_in (block_found, innermost_block))
667 innermost_block = block_found;
668 write_exp_elt_opcode (OP_THIS);
669 write_exp_elt_opcode (OP_THIS);
670 write_exp_elt_opcode (STRUCTOP_PTR);
671 write_exp_string ($1.stoken);
672 write_exp_elt_opcode (STRUCTOP_PTR);
673 }
674 else
675 {
676 register int i;
677 register char *arg = copy_name ($1.stoken);
678
679 /* FIXME, this search is linear! At least
680 optimize the strcmp with a 1-char cmp... */
681 for (i = 0; i < misc_function_count; i++)
682 if (!strcmp (misc_function_vector[i].name, arg))
683 break;
684
685 if (i < misc_function_count)
686 {
687 enum misc_function_type mft =
688 misc_function_vector[i].type;
689
690 write_exp_elt_opcode (OP_LONG);
691 write_exp_elt_type (builtin_type_int);
692 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
693 write_exp_elt_opcode (OP_LONG);
694 write_exp_elt_opcode (UNOP_MEMVAL);
695 if (mft == mf_data || mft == mf_bss)
696 write_exp_elt_type (builtin_type_int);
697 else if (mft == mf_text)
698 write_exp_elt_type (lookup_function_type (builtin_type_int));
699 else
700 write_exp_elt_type (builtin_type_char);
701 write_exp_elt_opcode (UNOP_MEMVAL);
702 }
703 else if (symtab_list == 0
704 && partial_symtab_list == 0)
705 error ("No symbol table is loaded. Use the \"file\" command.");
706 else
707 error ("No symbol \"%s\" in current context.",
708 copy_name ($1.stoken));
709 }
710 }
711 ;
712
713
714 ptype : typebase
715 | typebase abs_decl
716 {
717 /* This is where the interesting stuff happens. */
718 int done = 0;
719 int array_size;
720 struct type *follow_type = $1;
721
722 while (!done)
723 switch (pop_type ())
724 {
725 case tp_end:
726 done = 1;
727 break;
728 case tp_pointer:
729 follow_type = lookup_pointer_type (follow_type);
730 break;
731 case tp_reference:
732 follow_type = lookup_reference_type (follow_type);
733 break;
734 case tp_array:
735 array_size = (int) pop_type ();
736 if (array_size != -1)
737 follow_type = create_array_type (follow_type,
738 array_size);
739 else
740 follow_type = lookup_pointer_type (follow_type);
741 break;
742 case tp_function:
743 follow_type = lookup_function_type (follow_type);
744 break;
745 }
746 $$ = follow_type;
747 }
748 ;
749
750 abs_decl: '*'
751 { push_type (tp_pointer); $$ = 0; }
752 | '*' abs_decl
753 { push_type (tp_pointer); $$ = $2; }
754 | '&'
755 { push_type (tp_reference); $$ = 0; }
756 | '&' abs_decl
757 { push_type (tp_reference); $$ = $2; }
758 | direct_abs_decl
759 ;
760
761 direct_abs_decl: '(' abs_decl ')'
762 { $$ = $2; }
763 | direct_abs_decl array_mod
764 {
765 push_type ((enum type_pieces) $2);
766 push_type (tp_array);
767 }
768 | array_mod
769 {
770 push_type ((enum type_pieces) $1);
771 push_type (tp_array);
772 $$ = 0;
773 }
774 | direct_abs_decl func_mod
775 { push_type (tp_function); }
776 | func_mod
777 { push_type (tp_function); }
778 ;
779
780 array_mod: '[' ']'
781 { $$ = -1; }
782 | '[' INT ']'
783 { $$ = $2; }
784 ;
785
786 func_mod: '(' ')'
787 { $$ = 0; }
788 ;
789
790 type : ptype
791 | typebase COLONCOLON '*'
792 { $$ = lookup_member_type (builtin_type_int, $1); }
793 | type '(' typebase COLONCOLON '*' ')'
794 { $$ = lookup_member_type ($1, $3); }
795 | type '(' typebase COLONCOLON '*' ')' '(' ')'
796 { $$ = lookup_member_type
797 (lookup_function_type ($1), $3); }
798 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
799 { $$ = lookup_member_type
800 (lookup_function_type ($1), $3);
801 free ($8); }
802 ;
803
804 typebase
805 : TYPENAME
806 { $$ = $1.type; }
807 | INT_KEYWORD
808 { $$ = builtin_type_int; }
809 | LONG
810 { $$ = builtin_type_long; }
811 | SHORT
812 { $$ = builtin_type_short; }
813 | LONG INT_KEYWORD
814 { $$ = builtin_type_long; }
815 | UNSIGNED LONG INT_KEYWORD
816 { $$ = builtin_type_unsigned_long; }
817 | LONG LONG
818 { $$ = builtin_type_long_long; }
819 | LONG LONG INT_KEYWORD
820 { $$ = builtin_type_long_long; }
821 | UNSIGNED LONG LONG
822 { $$ = builtin_type_unsigned_long_long; }
823 | UNSIGNED LONG LONG INT_KEYWORD
824 { $$ = builtin_type_unsigned_long_long; }
825 | SHORT INT_KEYWORD
826 { $$ = builtin_type_short; }
827 | UNSIGNED SHORT INT_KEYWORD
828 { $$ = builtin_type_unsigned_short; }
829 | STRUCT name
830 { $$ = lookup_struct (copy_name ($2),
831 expression_context_block); }
832 | UNION name
833 { $$ = lookup_union (copy_name ($2),
834 expression_context_block); }
835 | ENUM name
836 { $$ = lookup_enum (copy_name ($2),
837 expression_context_block); }
838 | UNSIGNED typename
839 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
840 | UNSIGNED
841 { $$ = builtin_type_unsigned_int; }
842 | SIGNED typename
843 { $$ = $2.type; }
844 | SIGNED
845 { $$ = builtin_type_int; }
846 ;
847
848 typename: TYPENAME
849 | INT_KEYWORD
850 {
851 $$.stoken.ptr = "int";
852 $$.stoken.length = 3;
853 $$.type = builtin_type_int;
854 }
855 | LONG
856 {
857 $$.stoken.ptr = "long";
858 $$.stoken.length = 4;
859 $$.type = builtin_type_long;
860 }
861 | SHORT
862 {
863 $$.stoken.ptr = "short";
864 $$.stoken.length = 5;
865 $$.type = builtin_type_short;
866 }
867 ;
868
869 nonempty_typelist
870 : type
871 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
872 $$[0] = (struct type *)0;
873 $$[1] = $1;
874 }
875 | nonempty_typelist ',' type
876 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
877 $$ = (struct type **)xrealloc ($1, len);
878 $$[$<ivec>$[0]] = $3;
879 }
880 ;
881
882 name : NAME { $$ = $1.stoken; }
883 | BLOCKNAME { $$ = $1.stoken; }
884 | TYPENAME { $$ = $1.stoken; }
885 | NAME_OR_INT { $$ = $1.stoken; }
886 | NAME_OR_UINT { $$ = $1.stoken; }
887 ;
888
889 name_not_typename : NAME
890 | BLOCKNAME
891 /* These would be useful if name_not_typename was useful, but it is just
892 a fake for "variable", so these cause reduce/reduce conflicts because
893 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
894 =exp) or just an exp. If name_not_typename was ever used in an lvalue
895 context where only a name could occur, this might be useful.
896 | NAME_OR_INT
897 | NAME_OR_UINT
898 */
899 ;
900
901 %%
902 \f
903 /* Begin counting arguments for a function call,
904 saving the data about any containing call. */
905
906 static void
907 start_arglist ()
908 {
909 register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
910
911 new->next = funcall_chain;
912 new->arglist_len = arglist_len;
913 arglist_len = 0;
914 funcall_chain = new;
915 }
916
917 /* Return the number of arguments in a function call just terminated,
918 and restore the data for the containing function call. */
919
920 static int
921 end_arglist ()
922 {
923 register int val = arglist_len;
924 register struct funcall *call = funcall_chain;
925 funcall_chain = call->next;
926 arglist_len = call->arglist_len;
927 free (call);
928 return val;
929 }
930
931 /* Free everything in the funcall chain.
932 Used when there is an error inside parsing. */
933
934 static void
935 free_funcalls ()
936 {
937 register struct funcall *call, *next;
938
939 for (call = funcall_chain; call; call = next)
940 {
941 next = call->next;
942 free (call);
943 }
944 }
945 \f
946 /* This page contains the functions for adding data to the struct expression
947 being constructed. */
948
949 /* Add one element to the end of the expression. */
950
951 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
952 a register through here */
953
954 static void
955 write_exp_elt (expelt)
956 union exp_element expelt;
957 {
958 if (expout_ptr >= expout_size)
959 {
960 expout_size *= 2;
961 expout = (struct expression *) xrealloc (expout,
962 sizeof (struct expression)
963 + expout_size * sizeof (union exp_element));
964 }
965 expout->elts[expout_ptr++] = expelt;
966 }
967
968 static void
969 write_exp_elt_opcode (expelt)
970 enum exp_opcode expelt;
971 {
972 union exp_element tmp;
973
974 tmp.opcode = expelt;
975
976 write_exp_elt (tmp);
977 }
978
979 static void
980 write_exp_elt_sym (expelt)
981 struct symbol *expelt;
982 {
983 union exp_element tmp;
984
985 tmp.symbol = expelt;
986
987 write_exp_elt (tmp);
988 }
989
990 static void
991 write_exp_elt_longcst (expelt)
992 LONGEST expelt;
993 {
994 union exp_element tmp;
995
996 tmp.longconst = expelt;
997
998 write_exp_elt (tmp);
999 }
1000
1001 static void
1002 write_exp_elt_dblcst (expelt)
1003 double expelt;
1004 {
1005 union exp_element tmp;
1006
1007 tmp.doubleconst = expelt;
1008
1009 write_exp_elt (tmp);
1010 }
1011
1012 static void
1013 write_exp_elt_type (expelt)
1014 struct type *expelt;
1015 {
1016 union exp_element tmp;
1017
1018 tmp.type = expelt;
1019
1020 write_exp_elt (tmp);
1021 }
1022
1023 static void
1024 write_exp_elt_intern (expelt)
1025 struct internalvar *expelt;
1026 {
1027 union exp_element tmp;
1028
1029 tmp.internalvar = expelt;
1030
1031 write_exp_elt (tmp);
1032 }
1033
1034 /* Add a string constant to the end of the expression.
1035 Follow it by its length in bytes, as a separate exp_element. */
1036
1037 static void
1038 write_exp_string (str)
1039 struct stoken str;
1040 {
1041 register int len = str.length;
1042 register int lenelt
1043 = (len + sizeof (union exp_element)) / sizeof (union exp_element);
1044
1045 expout_ptr += lenelt;
1046
1047 if (expout_ptr >= expout_size)
1048 {
1049 expout_size = max (expout_size * 2, expout_ptr + 10);
1050 expout = (struct expression *)
1051 xrealloc (expout, (sizeof (struct expression)
1052 + (expout_size * sizeof (union exp_element))));
1053 }
1054 bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len);
1055 ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0;
1056 write_exp_elt_longcst ((LONGEST) len);
1057 }
1058 \f
1059 /* During parsing of a C expression, the pointer to the next character
1060 is in this variable. */
1061
1062 static char *lexptr;
1063
1064 /* Tokens that refer to names do so with explicit pointer and length,
1065 so they can share the storage that lexptr is parsing.
1066
1067 When it is necessary to pass a name to a function that expects
1068 a null-terminated string, the substring is copied out
1069 into a block of storage that namecopy points to.
1070
1071 namecopy is allocated once, guaranteed big enough, for each parsing. */
1072
1073 static char *namecopy;
1074
1075 /* Current depth in parentheses within the expression. */
1076
1077 static int paren_depth;
1078
1079 /* Nonzero means stop parsing on first comma (if not within parentheses). */
1080
1081 static int comma_terminates;
1082
1083 /* Take care of parsing a number (anything that starts with a digit).
1084 Set yylval and return the token type; update lexptr.
1085 LEN is the number of characters in it. */
1086
1087 /*** Needs some error checking for the float case ***/
1088
1089 static int
1090 parse_number (p, len, parsed_float, putithere)
1091 register char *p;
1092 register int len;
1093 int parsed_float;
1094 YYSTYPE *putithere;
1095 {
1096 register LONGEST n = 0;
1097 register int i;
1098 register int c;
1099 register int base = input_radix;
1100 int unsigned_p = 0;
1101
1102 extern double atof ();
1103
1104 if (parsed_float)
1105 {
1106 /* It's a float since it contains a point or an exponent. */
1107 putithere->dval = atof (p);
1108 return FLOAT;
1109 }
1110
1111 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1112 if (p[0] == '0')
1113 switch (p[1])
1114 {
1115 case 'x':
1116 case 'X':
1117 if (len >= 3)
1118 {
1119 p += 2;
1120 base = 16;
1121 len -= 2;
1122 }
1123 break;
1124
1125 case 't':
1126 case 'T':
1127 case 'd':
1128 case 'D':
1129 if (len >= 3)
1130 {
1131 p += 2;
1132 base = 10;
1133 len -= 2;
1134 }
1135 break;
1136
1137 default:
1138 base = 8;
1139 break;
1140 }
1141
1142 while (len-- > 0)
1143 {
1144 c = *p++;
1145 if (c >= 'A' && c <= 'Z')
1146 c += 'a' - 'A';
1147 if (c != 'l' && c != 'u')
1148 n *= base;
1149 if (c >= '0' && c <= '9')
1150 n += i = c - '0';
1151 else
1152 {
1153 if (base > 10 && c >= 'a' && c <= 'f')
1154 n += i = c - 'a' + 10;
1155 else if (len == 0 && c == 'l')
1156 ;
1157 else if (len == 0 && c == 'u')
1158 unsigned_p = 1;
1159 else
1160 return ERROR; /* Char not a digit */
1161 }
1162 if (i >= base)
1163 return ERROR; /* Invalid digit in this base */
1164 }
1165
1166 if (unsigned_p)
1167 {
1168 putithere->ulval = n;
1169 return UINT;
1170 }
1171 else
1172 {
1173 putithere->lval = n;
1174 return INT;
1175 }
1176 }
1177
1178 struct token
1179 {
1180 char *operator;
1181 int token;
1182 enum exp_opcode opcode;
1183 };
1184
1185 const static struct token tokentab3[] =
1186 {
1187 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1188 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1189 };
1190
1191 const static struct token tokentab2[] =
1192 {
1193 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1194 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1195 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1196 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1197 {"%=", ASSIGN_MODIFY, BINOP_REM},
1198 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1199 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1200 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1201 {"++", INCREMENT, BINOP_END},
1202 {"--", DECREMENT, BINOP_END},
1203 {"->", ARROW, BINOP_END},
1204 {"&&", AND, BINOP_END},
1205 {"||", OR, BINOP_END},
1206 {"::", COLONCOLON, BINOP_END},
1207 {"<<", LSH, BINOP_END},
1208 {">>", RSH, BINOP_END},
1209 {"==", EQUAL, BINOP_END},
1210 {"!=", NOTEQUAL, BINOP_END},
1211 {"<=", LEQ, BINOP_END},
1212 {">=", GEQ, BINOP_END}
1213 };
1214
1215 /* assign machine-independent names to certain registers
1216 * (unless overridden by the REGISTER_NAMES table)
1217 */
1218 struct std_regs {
1219 char *name;
1220 int regnum;
1221 } std_regs[] = {
1222 #ifdef PC_REGNUM
1223 { "pc", PC_REGNUM },
1224 #endif
1225 #ifdef FP_REGNUM
1226 { "fp", FP_REGNUM },
1227 #endif
1228 #ifdef SP_REGNUM
1229 { "sp", SP_REGNUM },
1230 #endif
1231 #ifdef PS_REGNUM
1232 { "ps", PS_REGNUM },
1233 #endif
1234 };
1235
1236 #define NUM_STD_REGS (sizeof std_regs / sizeof std_regs[0])
1237
1238 /* Read one token, getting characters through lexptr. */
1239
1240 static int
1241 yylex ()
1242 {
1243 register int c;
1244 register int namelen;
1245 register unsigned i;
1246 register char *tokstart;
1247
1248 retry:
1249
1250 tokstart = lexptr;
1251 /* See if it is a special token of length 3. */
1252 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1253 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1254 {
1255 lexptr += 3;
1256 yylval.opcode = tokentab3[i].opcode;
1257 return tokentab3[i].token;
1258 }
1259
1260 /* See if it is a special token of length 2. */
1261 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1262 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1263 {
1264 lexptr += 2;
1265 yylval.opcode = tokentab2[i].opcode;
1266 return tokentab2[i].token;
1267 }
1268
1269 switch (c = *tokstart)
1270 {
1271 case 0:
1272 return 0;
1273
1274 case ' ':
1275 case '\t':
1276 case '\n':
1277 lexptr++;
1278 goto retry;
1279
1280 case '\'':
1281 lexptr++;
1282 c = *lexptr++;
1283 if (c == '\\')
1284 c = parse_escape (&lexptr);
1285 yylval.lval = c;
1286 c = *lexptr++;
1287 if (c != '\'')
1288 error ("Invalid character constant.");
1289 return CHAR;
1290
1291 case '(':
1292 paren_depth++;
1293 lexptr++;
1294 return c;
1295
1296 case ')':
1297 if (paren_depth == 0)
1298 return 0;
1299 paren_depth--;
1300 lexptr++;
1301 return c;
1302
1303 case ',':
1304 if (comma_terminates && paren_depth == 0)
1305 return 0;
1306 lexptr++;
1307 return c;
1308
1309 case '.':
1310 /* Might be a floating point number. */
1311 if (lexptr[1] < '0' || lexptr[1] > '9')
1312 goto symbol; /* Nope, must be a symbol. */
1313 /* FALL THRU into number case. */
1314
1315 case '0':
1316 case '1':
1317 case '2':
1318 case '3':
1319 case '4':
1320 case '5':
1321 case '6':
1322 case '7':
1323 case '8':
1324 case '9':
1325 {
1326 /* It's a number. */
1327 int got_dot = 0, got_e = 0, toktype;
1328 register char *p = tokstart;
1329 int hex = input_radix > 10;
1330
1331 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1332 {
1333 p += 2;
1334 hex = 1;
1335 }
1336 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1337 {
1338 p += 2;
1339 hex = 0;
1340 }
1341
1342 for (;; ++p)
1343 {
1344 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1345 got_dot = got_e = 1;
1346 else if (!hex && !got_dot && *p == '.')
1347 got_dot = 1;
1348 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1349 && (*p == '-' || *p == '+'))
1350 /* This is the sign of the exponent, not the end of the
1351 number. */
1352 continue;
1353 /* We will take any letters or digits. parse_number will
1354 complain if past the radix, or if L or U are not final. */
1355 else if ((*p < '0' || *p > '9')
1356 && ((*p < 'a' || *p > 'z')
1357 && (*p < 'A' || *p > 'Z')))
1358 break;
1359 }
1360 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1361 if (toktype == ERROR)
1362 {
1363 char *err_copy = (char *) alloca (p - tokstart + 1);
1364
1365 bcopy (tokstart, err_copy, p - tokstart);
1366 err_copy[p - tokstart] = 0;
1367 error ("Invalid number \"%s\".", err_copy);
1368 }
1369 lexptr = p;
1370 return toktype;
1371 }
1372
1373 case '+':
1374 case '-':
1375 case '*':
1376 case '/':
1377 case '%':
1378 case '|':
1379 case '&':
1380 case '^':
1381 case '~':
1382 case '!':
1383 case '@':
1384 case '<':
1385 case '>':
1386 case '[':
1387 case ']':
1388 case '?':
1389 case ':':
1390 case '=':
1391 case '{':
1392 case '}':
1393 symbol:
1394 lexptr++;
1395 return c;
1396
1397 case '"':
1398 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1399 if (c == '\\')
1400 {
1401 c = tokstart[++namelen];
1402 if (c >= '0' && c <= '9')
1403 {
1404 c = tokstart[++namelen];
1405 if (c >= '0' && c <= '9')
1406 c = tokstart[++namelen];
1407 }
1408 }
1409 yylval.sval.ptr = tokstart + 1;
1410 yylval.sval.length = namelen - 1;
1411 lexptr += namelen + 1;
1412 return STRING;
1413 }
1414
1415 if (!(c == '_' || c == '$'
1416 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1417 /* We must have come across a bad character (e.g. ';'). */
1418 error ("Invalid character '%c' in expression.", c);
1419
1420 /* It's a name. See how long it is. */
1421 namelen = 0;
1422 for (c = tokstart[namelen];
1423 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1424 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1425 c = tokstart[++namelen])
1426 ;
1427
1428 /* The token "if" terminates the expression and is NOT
1429 removed from the input stream. */
1430 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1431 {
1432 return 0;
1433 }
1434
1435 lexptr += namelen;
1436
1437 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1438 and $$digits (equivalent to $<-digits> if you could type that).
1439 Make token type LAST, and put the number (the digits) in yylval. */
1440
1441 if (*tokstart == '$')
1442 {
1443 register int negate = 0;
1444 c = 1;
1445 /* Double dollar means negate the number and add -1 as well.
1446 Thus $$ alone means -1. */
1447 if (namelen >= 2 && tokstart[1] == '$')
1448 {
1449 negate = 1;
1450 c = 2;
1451 }
1452 if (c == namelen)
1453 {
1454 /* Just dollars (one or two) */
1455 yylval.lval = - negate;
1456 return LAST;
1457 }
1458 /* Is the rest of the token digits? */
1459 for (; c < namelen; c++)
1460 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1461 break;
1462 if (c == namelen)
1463 {
1464 yylval.lval = atoi (tokstart + 1 + negate);
1465 if (negate)
1466 yylval.lval = - yylval.lval;
1467 return LAST;
1468 }
1469 }
1470
1471 /* Handle tokens that refer to machine registers:
1472 $ followed by a register name. */
1473
1474 if (*tokstart == '$') {
1475 for (c = 0; c < NUM_REGS; c++)
1476 if (namelen - 1 == strlen (reg_names[c])
1477 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1478 {
1479 yylval.lval = c;
1480 return REGNAME;
1481 }
1482 for (c = 0; c < NUM_STD_REGS; c++)
1483 if (namelen - 1 == strlen (std_regs[c].name)
1484 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1485 {
1486 yylval.lval = std_regs[c].regnum;
1487 return REGNAME;
1488 }
1489 }
1490 /* Catch specific keywords. Should be done with a data structure. */
1491 switch (namelen)
1492 {
1493 case 8:
1494 if (!strncmp (tokstart, "unsigned", 8))
1495 return UNSIGNED;
1496 break;
1497 case 6:
1498 if (!strncmp (tokstart, "struct", 6))
1499 return STRUCT;
1500 if (!strncmp (tokstart, "signed", 6))
1501 return SIGNED;
1502 if (!strncmp (tokstart, "sizeof", 6))
1503 return SIZEOF;
1504 break;
1505 case 5:
1506 if (!strncmp (tokstart, "union", 5))
1507 return UNION;
1508 if (!strncmp (tokstart, "short", 5))
1509 return SHORT;
1510 break;
1511 case 4:
1512 if (!strncmp (tokstart, "enum", 4))
1513 return ENUM;
1514 if (!strncmp (tokstart, "long", 4))
1515 return LONG;
1516 if (!strncmp (tokstart, "this", 4))
1517 {
1518 static const char this_name[] =
1519 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1520
1521 if (lookup_symbol (this_name, expression_context_block,
1522 VAR_NAMESPACE, 0, NULL))
1523 return THIS;
1524 }
1525 break;
1526 case 3:
1527 if (!strncmp (tokstart, "int", 3))
1528 return INT_KEYWORD;
1529 break;
1530 default:
1531 break;
1532 }
1533
1534 yylval.sval.ptr = tokstart;
1535 yylval.sval.length = namelen;
1536
1537 /* Any other names starting in $ are debugger internal variables. */
1538
1539 if (*tokstart == '$')
1540 {
1541 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1542 return VARIABLE;
1543 }
1544
1545 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1546 functions or symtabs. If this is not so, then ...
1547 Use token-type TYPENAME for symbols that happen to be defined
1548 currently as names of types; NAME for other symbols.
1549 The caller is not constrained to care about the distinction. */
1550 {
1551 char *tmp = copy_name (yylval.sval);
1552 struct symbol *sym;
1553 int is_a_field_of_this = 0;
1554 int hextype;
1555
1556 sym = lookup_symbol (tmp, expression_context_block,
1557 VAR_NAMESPACE, &is_a_field_of_this, NULL);
1558 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1559 lookup_partial_symtab (tmp))
1560 {
1561 yylval.ssym.sym = sym;
1562 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1563 return BLOCKNAME;
1564 }
1565 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1566 {
1567 yylval.tsym.type = SYMBOL_TYPE (sym);
1568 return TYPENAME;
1569 }
1570 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1571 return TYPENAME;
1572
1573 /* Input names that aren't symbols but ARE valid hex numbers,
1574 when the input radix permits them, can be names or numbers
1575 depending on the parse. Note we support radixes > 16 here. */
1576 if (!sym &&
1577 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1578 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1579 {
1580 YYSTYPE newlval; /* Its value is ignored. */
1581 hextype = parse_number (tokstart, namelen, 0, &newlval);
1582 if (hextype == INT)
1583 {
1584 yylval.ssym.sym = sym;
1585 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1586 return NAME_OR_INT;
1587 }
1588 if (hextype == UINT)
1589 {
1590 yylval.ssym.sym = sym;
1591 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1592 return NAME_OR_UINT;
1593 }
1594 }
1595
1596 /* Any other kind of symbol */
1597 yylval.ssym.sym = sym;
1598 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1599 return NAME;
1600 }
1601 }
1602
1603 static void
1604 yyerror (msg)
1605 char *msg;
1606 {
1607 error ("Invalid syntax in expression.");
1608 }
1609
1610 /* Return a null-terminated temporary copy of the name
1611 of a string token. */
1612
1613 static char *
1614 copy_name (token)
1615 struct stoken token;
1616 {
1617 bcopy (token.ptr, namecopy, token.length);
1618 namecopy[token.length] = 0;
1619 return namecopy;
1620 }
1621 \f
1622 /* Reverse an expression from suffix form (in which it is constructed)
1623 to prefix form (in which we can conveniently print or execute it). */
1624
1625 static void prefixify_subexp ();
1626
1627 static void
1628 prefixify_expression (expr)
1629 register struct expression *expr;
1630 {
1631 register int len = sizeof (struct expression) +
1632 expr->nelts * sizeof (union exp_element);
1633 register struct expression *temp;
1634 register int inpos = expr->nelts, outpos = 0;
1635
1636 temp = (struct expression *) alloca (len);
1637
1638 /* Copy the original expression into temp. */
1639 bcopy (expr, temp, len);
1640
1641 prefixify_subexp (temp, expr, inpos, outpos);
1642 }
1643
1644 /* Return the number of exp_elements in the subexpression of EXPR
1645 whose last exp_element is at index ENDPOS - 1 in EXPR. */
1646
1647 static int
1648 length_of_subexp (expr, endpos)
1649 register struct expression *expr;
1650 register int endpos;
1651 {
1652 register int oplen = 1;
1653 register int args = 0;
1654 register int i;
1655
1656 if (endpos < 0)
1657 error ("?error in length_of_subexp");
1658
1659 i = (int) expr->elts[endpos - 1].opcode;
1660
1661 switch (i)
1662 {
1663 /* C++ */
1664 case OP_SCOPE:
1665 oplen = 4 + ((expr->elts[endpos - 2].longconst
1666 + sizeof (union exp_element))
1667 / sizeof (union exp_element));
1668 break;
1669
1670 case OP_LONG:
1671 case OP_DOUBLE:
1672 oplen = 4;
1673 break;
1674
1675 case OP_VAR_VALUE:
1676 case OP_LAST:
1677 case OP_REGISTER:
1678 case OP_INTERNALVAR:
1679 oplen = 3;
1680 break;
1681
1682 case OP_FUNCALL:
1683 oplen = 3;
1684 args = 1 + expr->elts[endpos - 2].longconst;
1685 break;
1686
1687 case UNOP_CAST:
1688 case UNOP_MEMVAL:
1689 oplen = 3;
1690 args = 1;
1691 break;
1692
1693 case STRUCTOP_STRUCT:
1694 case STRUCTOP_PTR:
1695 args = 1;
1696 case OP_STRING:
1697 oplen = 3 + ((expr->elts[endpos - 2].longconst
1698 + sizeof (union exp_element))
1699 / sizeof (union exp_element));
1700 break;
1701
1702 case TERNOP_COND:
1703 args = 3;
1704 break;
1705
1706 case BINOP_ASSIGN_MODIFY:
1707 oplen = 3;
1708 args = 2;
1709 break;
1710
1711 /* C++ */
1712 case OP_THIS:
1713 oplen = 2;
1714 break;
1715
1716 default:
1717 args = 1 + (i < (int) BINOP_END);
1718 }
1719
1720 while (args > 0)
1721 {
1722 oplen += length_of_subexp (expr, endpos - oplen);
1723 args--;
1724 }
1725
1726 return oplen;
1727 }
1728
1729 /* Copy the subexpression ending just before index INEND in INEXPR
1730 into OUTEXPR, starting at index OUTBEG.
1731 In the process, convert it from suffix to prefix form. */
1732
1733 static void
1734 prefixify_subexp (inexpr, outexpr, inend, outbeg)
1735 register struct expression *inexpr;
1736 struct expression *outexpr;
1737 register int inend;
1738 int outbeg;
1739 {
1740 register int oplen = 1;
1741 register int args = 0;
1742 register int i;
1743 int *arglens;
1744 enum exp_opcode opcode;
1745
1746 /* Compute how long the last operation is (in OPLEN),
1747 and also how many preceding subexpressions serve as
1748 arguments for it (in ARGS). */
1749
1750 opcode = inexpr->elts[inend - 1].opcode;
1751 switch (opcode)
1752 {
1753 /* C++ */
1754 case OP_SCOPE:
1755 oplen = 4 + ((inexpr->elts[inend - 2].longconst
1756 + sizeof (union exp_element))
1757 / sizeof (union exp_element));
1758 break;
1759
1760 case OP_LONG:
1761 case OP_DOUBLE:
1762 oplen = 4;
1763 break;
1764
1765 case OP_VAR_VALUE:
1766 case OP_LAST:
1767 case OP_REGISTER:
1768 case OP_INTERNALVAR:
1769 oplen = 3;
1770 break;
1771
1772 case OP_FUNCALL:
1773 oplen = 3;
1774 args = 1 + inexpr->elts[inend - 2].longconst;
1775 break;
1776
1777 case UNOP_CAST:
1778 case UNOP_MEMVAL:
1779 oplen = 3;
1780 args = 1;
1781 break;
1782
1783 case STRUCTOP_STRUCT:
1784 case STRUCTOP_PTR:
1785 args = 1;
1786 case OP_STRING:
1787 oplen = 3 + ((inexpr->elts[inend - 2].longconst
1788 + sizeof (union exp_element))
1789 / sizeof (union exp_element));
1790
1791 break;
1792
1793 case TERNOP_COND:
1794 args = 3;
1795 break;
1796
1797 case BINOP_ASSIGN_MODIFY:
1798 oplen = 3;
1799 args = 2;
1800 break;
1801
1802 /* C++ */
1803 case OP_THIS:
1804 oplen = 2;
1805 break;
1806
1807 default:
1808 args = 1 + ((int) opcode < (int) BINOP_END);
1809 }
1810
1811 /* Copy the final operator itself, from the end of the input
1812 to the beginning of the output. */
1813 inend -= oplen;
1814 bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg],
1815 oplen * sizeof (union exp_element));
1816 outbeg += oplen;
1817
1818 /* Find the lengths of the arg subexpressions. */
1819 arglens = (int *) alloca (args * sizeof (int));
1820 for (i = args - 1; i >= 0; i--)
1821 {
1822 oplen = length_of_subexp (inexpr, inend);
1823 arglens[i] = oplen;
1824 inend -= oplen;
1825 }
1826
1827 /* Now copy each subexpression, preserving the order of
1828 the subexpressions, but prefixifying each one.
1829 In this loop, inend starts at the beginning of
1830 the expression this level is working on
1831 and marches forward over the arguments.
1832 outbeg does similarly in the output. */
1833 for (i = 0; i < args; i++)
1834 {
1835 oplen = arglens[i];
1836 inend += oplen;
1837 prefixify_subexp (inexpr, outexpr, inend, outbeg);
1838 outbeg += oplen;
1839 }
1840 }
1841 \f
1842 /* This page contains the two entry points to this file. */
1843
1844 /* Read a C expression from the string *STRINGPTR points to,
1845 parse it, and return a pointer to a struct expression that we malloc.
1846 Use block BLOCK as the lexical context for variable names;
1847 if BLOCK is zero, use the block of the selected stack frame.
1848 Meanwhile, advance *STRINGPTR to point after the expression,
1849 at the first nonwhite character that is not part of the expression
1850 (possibly a null character).
1851
1852 If COMMA is nonzero, stop if a comma is reached. */
1853
1854 struct expression *
1855 parse_c_1 (stringptr, block, comma)
1856 char **stringptr;
1857 struct block *block;
1858 int comma;
1859 {
1860 struct cleanup *old_chain;
1861
1862 lexptr = *stringptr;
1863
1864 paren_depth = 0;
1865 type_stack_depth = 0;
1866
1867 comma_terminates = comma;
1868
1869 if (lexptr == 0 || *lexptr == 0)
1870 error_no_arg ("expression to compute");
1871
1872 old_chain = make_cleanup (free_funcalls, 0);
1873 funcall_chain = 0;
1874
1875 expression_context_block = block ? block : get_selected_block ();
1876
1877 namecopy = (char *) alloca (strlen (lexptr) + 1);
1878 expout_size = 10;
1879 expout_ptr = 0;
1880 expout = (struct expression *)
1881 xmalloc (sizeof (struct expression)
1882 + expout_size * sizeof (union exp_element));
1883 make_cleanup (free_current_contents, &expout);
1884 if (yyparse ())
1885 yyerror (NULL);
1886 discard_cleanups (old_chain);
1887 expout->nelts = expout_ptr;
1888 expout = (struct expression *)
1889 xrealloc (expout,
1890 sizeof (struct expression)
1891 + expout_ptr * sizeof (union exp_element));
1892 prefixify_expression (expout);
1893 *stringptr = lexptr;
1894 return expout;
1895 }
1896
1897 /* Parse STRING as an expression, and complain if this fails
1898 to use up all of the contents of STRING. */
1899
1900 struct expression *
1901 parse_c_expression (string)
1902 char *string;
1903 {
1904 register struct expression *exp;
1905 exp = parse_c_1 (&string, 0, 0);
1906 if (*string)
1907 error ("Junk after end of expression.");
1908 return exp;
1909 }
1910
1911 static void
1912 push_type (tp)
1913 enum type_pieces tp;
1914 {
1915 if (type_stack_depth == type_stack_size)
1916 {
1917 type_stack_size *= 2;
1918 type_stack = (enum type_pieces *)
1919 xrealloc (type_stack, type_stack_size * sizeof (enum type_pieces));
1920 }
1921 type_stack[type_stack_depth++] = tp;
1922 }
1923
1924 static enum type_pieces
1925 pop_type ()
1926 {
1927 if (type_stack_depth)
1928 return type_stack[--type_stack_depth];
1929 return tp_end;
1930 }
1931
1932 void
1933 _initialize_expread ()
1934 {
1935 type_stack_size = 80;
1936 type_stack_depth = 0;
1937 type_stack = (enum type_pieces *)
1938 xmalloc (type_stack_size * sizeof (enum type_pieces));
1939 }
This page took 0.065761 seconds and 3 git commands to generate.