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