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