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