1 /* Parse C expressions for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
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. */
35 #include "expression.h"
39 static struct expression *expout;
40 static int expout_size;
41 static int expout_ptr;
44 static void yyerror ();
45 static void write_exp_elt ();
46 static void write_exp_elt_opcode ();
47 static void write_exp_elt_sym ();
48 static void write_exp_elt_longcst ();
49 static void write_exp_elt_dblcst ();
50 static void write_exp_elt_type ();
51 static void write_exp_elt_intern ();
52 static void write_exp_string ();
53 static void start_arglist ();
54 static int end_arglist ();
55 static void free_funcalls ();
56 static char *copy_name ();
57 static int parse_number ();
59 /* If this is nonzero, this block is used as the lexical context
62 static struct block *expression_context_block;
64 /* The innermost context required by the stack and register variables
65 we've encountered so far. */
66 struct block *innermost_block;
68 /* The block in which the most recently discovered symbol was found. */
69 struct block *block_found;
71 /* Number of arguments seen so far in innermost function call. */
72 static int arglist_len;
74 /* Data structure for saving values of arglist_len
75 for function calls whose arguments contain other function calls. */
83 struct funcall *funcall_chain;
85 /* This kind of datum is used to represent the name
102 struct stoken stoken;
104 int is_a_field_of_this;
107 /* For parsing of complicated types.
108 An array should be preceded in the list by the size of the array. */
110 {tp_end = -1, tp_pointer, tp_reference, tp_array, tp_function};
111 /* The stack can contain either an enum type_pieces or an int. */
112 union type_stack_elt {
113 enum type_pieces piece;
116 static union type_stack_elt *type_stack;
117 static int type_stack_depth, type_stack_size;
119 static void push_type ();
120 static void push_type_int ();
121 static enum type_pieces pop_type ();
122 static int pop_type_int ();
124 /* Allow debugging of parsing. */
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. */
135 unsigned LONGEST ulval;
141 struct symtoken ssym;
144 enum exp_opcode opcode;
145 struct internalvar *ivar;
151 %type <voidval> exp exp1 start variable
152 %type <tval> type typebase
153 %type <tvec> nonempty_typelist
156 /* Fancy type parsing. */
157 %type <voidval> func_mod direct_abs_decl abs_decl
159 %type <lval> array_mod
161 %token <lval> INT CHAR
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.
171 Contexts where this distinction is not important can use the
172 nonterminal "name", which matches either NAME or TYPENAME. */
175 %token <ssym> NAME BLOCKNAME
176 %token <tsym> TYPENAME
178 %type <ssym> name_not_typename
179 %type <tsym> typename
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. */
186 %token <ssym> NAME_OR_INT NAME_OR_UINT
188 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
191 /* Special type cases, put in to allow the parser to distinguish different
193 %token SIGNED LONG SHORT INT_KEYWORD
195 %token <lval> LAST REGNAME
197 %token <ivar> VARIABLE
199 %token <opcode> ASSIGN_MODIFY
206 %right '=' ASSIGN_MODIFY
214 %left '<' '>' LEQ GEQ
219 %right UNARY INCREMENT DECREMENT
220 %right ARROW '.' '[' '('
228 /* Expressions, including the comma operator. */
231 { write_exp_elt_opcode (BINOP_COMMA); }
234 /* Expressions, not including the comma operator. */
235 exp : '*' exp %prec UNARY
236 { write_exp_elt_opcode (UNOP_IND); }
238 exp : '&' exp %prec UNARY
239 { write_exp_elt_opcode (UNOP_ADDR); }
241 exp : '-' exp %prec UNARY
242 { write_exp_elt_opcode (UNOP_NEG); }
245 exp : '!' exp %prec UNARY
246 { write_exp_elt_opcode (UNOP_ZEROP); }
249 exp : '~' exp %prec UNARY
250 { write_exp_elt_opcode (UNOP_LOGNOT); }
253 exp : INCREMENT exp %prec UNARY
254 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
257 exp : DECREMENT exp %prec UNARY
258 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
261 exp : exp INCREMENT %prec UNARY
262 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
265 exp : exp DECREMENT %prec UNARY
266 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
269 exp : SIZEOF exp %prec UNARY
270 { write_exp_elt_opcode (UNOP_SIZEOF); }
274 { write_exp_elt_opcode (STRUCTOP_PTR);
275 write_exp_string ($3);
276 write_exp_elt_opcode (STRUCTOP_PTR); }
279 exp : exp ARROW '*' exp
280 { write_exp_elt_opcode (STRUCTOP_MPTR); }
284 { write_exp_elt_opcode (STRUCTOP_STRUCT);
285 write_exp_string ($3);
286 write_exp_elt_opcode (STRUCTOP_STRUCT); }
289 exp : exp '.' '*' exp
290 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
293 exp : exp '[' exp1 ']'
294 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
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); }
314 arglist : arglist ',' exp %prec ABOVE_COMMA
318 exp : '{' type '}' exp %prec UNARY
319 { write_exp_elt_opcode (UNOP_MEMVAL);
320 write_exp_elt_type ($2);
321 write_exp_elt_opcode (UNOP_MEMVAL); }
324 exp : '(' type ')' exp %prec UNARY
325 { write_exp_elt_opcode (UNOP_CAST);
326 write_exp_elt_type ($2);
327 write_exp_elt_opcode (UNOP_CAST); }
334 /* Binary operators in order of decreasing precedence. */
337 { write_exp_elt_opcode (BINOP_REPEAT); }
341 { write_exp_elt_opcode (BINOP_MUL); }
345 { write_exp_elt_opcode (BINOP_DIV); }
349 { write_exp_elt_opcode (BINOP_REM); }
353 { write_exp_elt_opcode (BINOP_ADD); }
357 { write_exp_elt_opcode (BINOP_SUB); }
361 { write_exp_elt_opcode (BINOP_LSH); }
365 { write_exp_elt_opcode (BINOP_RSH); }
369 { write_exp_elt_opcode (BINOP_EQUAL); }
372 exp : exp NOTEQUAL exp
373 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
377 { write_exp_elt_opcode (BINOP_LEQ); }
381 { write_exp_elt_opcode (BINOP_GEQ); }
385 { write_exp_elt_opcode (BINOP_LESS); }
389 { write_exp_elt_opcode (BINOP_GTR); }
393 { write_exp_elt_opcode (BINOP_LOGAND); }
397 { write_exp_elt_opcode (BINOP_LOGXOR); }
401 { write_exp_elt_opcode (BINOP_LOGIOR); }
405 { write_exp_elt_opcode (BINOP_AND); }
409 { write_exp_elt_opcode (BINOP_OR); }
412 exp : exp '?' exp ':' exp %prec '?'
413 { write_exp_elt_opcode (TERNOP_COND); }
417 { write_exp_elt_opcode (BINOP_ASSIGN); }
420 exp : 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); }
427 { write_exp_elt_opcode (OP_LONG);
428 if ($1 == (int) $1 || $1 == (unsigned int) $1)
429 write_exp_elt_type (builtin_type_int);
431 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
432 write_exp_elt_longcst ((LONGEST) $1);
433 write_exp_elt_opcode (OP_LONG); }
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);
444 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
445 write_exp_elt_longcst (val.lval);
446 write_exp_elt_opcode (OP_LONG); }
451 write_exp_elt_opcode (OP_LONG);
452 if ($1 == (unsigned int) $1)
453 write_exp_elt_type (builtin_type_unsigned_int);
455 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
456 write_exp_elt_longcst ((LONGEST) $1);
457 write_exp_elt_opcode (OP_LONG);
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);
468 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
469 write_exp_elt_longcst ((LONGEST)val.ulval);
470 write_exp_elt_opcode (OP_LONG);
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); }
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); }
492 { write_exp_elt_opcode (OP_LAST);
493 write_exp_elt_longcst ((LONGEST) $1);
494 write_exp_elt_opcode (OP_LAST); }
498 { write_exp_elt_opcode (OP_REGISTER);
499 write_exp_elt_longcst ((LONGEST) $1);
500 write_exp_elt_opcode (OP_REGISTER); }
504 { write_exp_elt_opcode (OP_INTERNALVAR);
505 write_exp_elt_intern ($1);
506 write_exp_elt_opcode (OP_INTERNALVAR); }
509 exp : 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); }
517 { write_exp_elt_opcode (OP_STRING);
518 write_exp_string ($1);
519 write_exp_elt_opcode (OP_STRING); }
524 { write_exp_elt_opcode (OP_THIS);
525 write_exp_elt_opcode (OP_THIS); }
533 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
537 lookup_symtab (copy_name ($1.stoken));
539 $$ = BLOCKVECTOR_BLOCK
540 (BLOCKVECTOR (tem), STATIC_BLOCK);
542 error ("No file or function \"%s\".",
543 copy_name ($1.stoken));
548 block : block COLONCOLON name
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.",
555 $$ = SYMBOL_BLOCK_VALUE (tem); }
558 variable: block COLONCOLON name
559 { struct symbol *sym;
560 sym = lookup_symbol (copy_name ($3), $1,
561 VAR_NAMESPACE, 0, NULL);
563 error ("No symbol \"%s\" in specified context.",
565 write_exp_elt_opcode (OP_VAR_VALUE);
566 write_exp_elt_sym (sym);
567 write_exp_elt_opcode (OP_VAR_VALUE); }
570 variable: typebase COLONCOLON name
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.",
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);
583 | typebase COLONCOLON '~' name
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.",
591 if (strcmp (type_name_no_tag (type), $4.ptr))
592 error ("invalid destructor `%s::~%s'",
593 type_name_no_tag (type), $4.ptr);
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);
603 char *name = copy_name ($2);
608 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
611 write_exp_elt_opcode (OP_VAR_VALUE);
612 write_exp_elt_sym (sym);
613 write_exp_elt_opcode (OP_VAR_VALUE);
616 for (i = 0; i < misc_function_count; i++)
617 if (!strcmp (misc_function_vector[i].name, name))
620 if (i < misc_function_count)
622 enum misc_function_type mft =
623 misc_function_vector[i].type;
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));
635 write_exp_elt_type (builtin_type_char);
636 write_exp_elt_opcode (UNOP_MEMVAL);
640 && partial_symtab_list == 0)
641 error ("No symbol table is loaded. Use the \"file\" command.");
643 error ("No symbol \"%s\" in current context.", name);
647 variable: name_not_typename
648 { struct symbol *sym = $1.sym;
660 if (innermost_block == 0 ||
661 contained_in (block_found,
663 innermost_block = block_found;
670 case LOC_CONST_BYTES:
672 /* In this case the expression can
673 be evaluated regardless of what
674 frame we are in, so there is no
675 need to check for the
676 innermost_block. These cases are
677 listed so that gcc -Wall will
678 report types that may not have
683 write_exp_elt_opcode (OP_VAR_VALUE);
684 write_exp_elt_sym (sym);
685 write_exp_elt_opcode (OP_VAR_VALUE);
687 else if ($1.is_a_field_of_this)
689 /* C++: it hangs off of `this'. Must
690 not inadvertently convert from a method call
692 if (innermost_block == 0 ||
693 contained_in (block_found, innermost_block))
694 innermost_block = block_found;
695 write_exp_elt_opcode (OP_THIS);
696 write_exp_elt_opcode (OP_THIS);
697 write_exp_elt_opcode (STRUCTOP_PTR);
698 write_exp_string ($1.stoken);
699 write_exp_elt_opcode (STRUCTOP_PTR);
704 register char *arg = copy_name ($1.stoken);
706 /* FIXME, this search is linear! At least
707 optimize the strcmp with a 1-char cmp... */
708 for (i = 0; i < misc_function_count; i++)
709 if (!strcmp (misc_function_vector[i].name, arg))
712 if (i < misc_function_count)
714 enum misc_function_type mft =
715 misc_function_vector[i].type;
717 write_exp_elt_opcode (OP_LONG);
718 write_exp_elt_type (builtin_type_int);
719 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
720 write_exp_elt_opcode (OP_LONG);
721 write_exp_elt_opcode (UNOP_MEMVAL);
722 if (mft == mf_data || mft == mf_bss)
723 write_exp_elt_type (builtin_type_int);
724 else if (mft == mf_text)
725 write_exp_elt_type (lookup_function_type (builtin_type_int));
727 write_exp_elt_type (builtin_type_char);
728 write_exp_elt_opcode (UNOP_MEMVAL);
730 else if (symtab_list == 0
731 && partial_symtab_list == 0)
732 error ("No symbol table is loaded. Use the \"file\" command.");
734 error ("No symbol \"%s\" in current context.",
735 copy_name ($1.stoken));
744 /* This is where the interesting stuff happens. */
747 struct type *follow_type = $1;
756 follow_type = lookup_pointer_type (follow_type);
759 follow_type = lookup_reference_type (follow_type);
762 array_size = pop_type_int ();
763 if (array_size != -1)
764 follow_type = create_array_type (follow_type,
767 follow_type = lookup_pointer_type (follow_type);
770 follow_type = lookup_function_type (follow_type);
778 { push_type (tp_pointer); $$ = 0; }
780 { push_type (tp_pointer); $$ = $2; }
782 { push_type (tp_reference); $$ = 0; }
784 { push_type (tp_reference); $$ = $2; }
788 direct_abs_decl: '(' abs_decl ')'
790 | direct_abs_decl array_mod
793 push_type (tp_array);
798 push_type (tp_array);
801 | direct_abs_decl func_mod
802 { push_type (tp_function); }
804 { push_type (tp_function); }
818 | typebase COLONCOLON '*'
819 { $$ = lookup_member_type (builtin_type_int, $1); }
820 | type '(' typebase COLONCOLON '*' ')'
821 { $$ = lookup_member_type ($1, $3); }
822 | type '(' typebase COLONCOLON '*' ')' '(' ')'
823 { $$ = lookup_member_type
824 (lookup_function_type ($1), $3); }
825 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
826 { $$ = lookup_member_type
827 (lookup_function_type ($1), $3);
835 { $$ = builtin_type_int; }
837 { $$ = builtin_type_long; }
839 { $$ = builtin_type_short; }
841 { $$ = builtin_type_long; }
842 | UNSIGNED LONG INT_KEYWORD
843 { $$ = builtin_type_unsigned_long; }
845 { $$ = builtin_type_long_long; }
846 | LONG LONG INT_KEYWORD
847 { $$ = builtin_type_long_long; }
849 { $$ = builtin_type_unsigned_long_long; }
850 | UNSIGNED LONG LONG INT_KEYWORD
851 { $$ = builtin_type_unsigned_long_long; }
853 { $$ = builtin_type_short; }
854 | UNSIGNED SHORT INT_KEYWORD
855 { $$ = builtin_type_unsigned_short; }
857 { $$ = lookup_struct (copy_name ($2),
858 expression_context_block); }
860 { $$ = lookup_union (copy_name ($2),
861 expression_context_block); }
863 { $$ = lookup_enum (copy_name ($2),
864 expression_context_block); }
866 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
868 { $$ = builtin_type_unsigned_int; }
872 { $$ = builtin_type_int; }
878 $$.stoken.ptr = "int";
879 $$.stoken.length = 3;
880 $$.type = builtin_type_int;
884 $$.stoken.ptr = "long";
885 $$.stoken.length = 4;
886 $$.type = builtin_type_long;
890 $$.stoken.ptr = "short";
891 $$.stoken.length = 5;
892 $$.type = builtin_type_short;
898 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
899 $$[0] = (struct type *)0;
902 | nonempty_typelist ',' type
903 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
904 $$ = (struct type **)xrealloc ($1, len);
905 $$[$<ivec>$[0]] = $3;
909 name : NAME { $$ = $1.stoken; }
910 | BLOCKNAME { $$ = $1.stoken; }
911 | TYPENAME { $$ = $1.stoken; }
912 | NAME_OR_INT { $$ = $1.stoken; }
913 | NAME_OR_UINT { $$ = $1.stoken; }
916 name_not_typename : NAME
918 /* These would be useful if name_not_typename was useful, but it is just
919 a fake for "variable", so these cause reduce/reduce conflicts because
920 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
921 =exp) or just an exp. If name_not_typename was ever used in an lvalue
922 context where only a name could occur, this might be useful.
930 /* Begin counting arguments for a function call,
931 saving the data about any containing call. */
936 register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
938 new->next = funcall_chain;
939 new->arglist_len = arglist_len;
944 /* Return the number of arguments in a function call just terminated,
945 and restore the data for the containing function call. */
950 register int val = arglist_len;
951 register struct funcall *call = funcall_chain;
952 funcall_chain = call->next;
953 arglist_len = call->arglist_len;
958 /* Free everything in the funcall chain.
959 Used when there is an error inside parsing. */
964 register struct funcall *call, *next;
966 for (call = funcall_chain; call; call = next)
973 /* This page contains the functions for adding data to the struct expression
974 being constructed. */
976 /* Add one element to the end of the expression. */
978 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
979 a register through here */
982 write_exp_elt (expelt)
983 union exp_element expelt;
985 if (expout_ptr >= expout_size)
988 expout = (struct expression *) xrealloc (expout,
989 sizeof (struct expression)
990 + expout_size * sizeof (union exp_element));
992 expout->elts[expout_ptr++] = expelt;
996 write_exp_elt_opcode (expelt)
997 enum exp_opcode expelt;
999 union exp_element tmp;
1001 tmp.opcode = expelt;
1003 write_exp_elt (tmp);
1007 write_exp_elt_sym (expelt)
1008 struct symbol *expelt;
1010 union exp_element tmp;
1012 tmp.symbol = expelt;
1014 write_exp_elt (tmp);
1018 write_exp_elt_longcst (expelt)
1021 union exp_element tmp;
1023 tmp.longconst = expelt;
1025 write_exp_elt (tmp);
1029 write_exp_elt_dblcst (expelt)
1032 union exp_element tmp;
1034 tmp.doubleconst = expelt;
1036 write_exp_elt (tmp);
1040 write_exp_elt_type (expelt)
1041 struct type *expelt;
1043 union exp_element tmp;
1047 write_exp_elt (tmp);
1051 write_exp_elt_intern (expelt)
1052 struct internalvar *expelt;
1054 union exp_element tmp;
1056 tmp.internalvar = expelt;
1058 write_exp_elt (tmp);
1061 /* Add a string constant to the end of the expression.
1062 Follow it by its length in bytes, as a separate exp_element. */
1065 write_exp_string (str)
1068 register int len = str.length;
1070 = (len + sizeof (union exp_element)) / sizeof (union exp_element);
1072 expout_ptr += lenelt;
1074 if (expout_ptr >= expout_size)
1076 expout_size = max (expout_size * 2, expout_ptr + 10);
1077 expout = (struct expression *)
1078 xrealloc (expout, (sizeof (struct expression)
1079 + (expout_size * sizeof (union exp_element))));
1081 bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len);
1082 ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0;
1083 write_exp_elt_longcst ((LONGEST) len);
1086 /* During parsing of a C expression, the pointer to the next character
1087 is in this variable. */
1089 static char *lexptr;
1091 /* Tokens that refer to names do so with explicit pointer and length,
1092 so they can share the storage that lexptr is parsing.
1094 When it is necessary to pass a name to a function that expects
1095 a null-terminated string, the substring is copied out
1096 into a block of storage that namecopy points to.
1098 namecopy is allocated once, guaranteed big enough, for each parsing. */
1100 static char *namecopy;
1102 /* Current depth in parentheses within the expression. */
1104 static int paren_depth;
1106 /* Nonzero means stop parsing on first comma (if not within parentheses). */
1108 static int comma_terminates;
1110 /* Take care of parsing a number (anything that starts with a digit).
1111 Set yylval and return the token type; update lexptr.
1112 LEN is the number of characters in it. */
1114 /*** Needs some error checking for the float case ***/
1117 parse_number (p, len, parsed_float, putithere)
1123 register LONGEST n = 0;
1126 register int base = input_radix;
1129 extern double atof ();
1133 /* It's a float since it contains a point or an exponent. */
1134 putithere->dval = atof (p);
1138 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1172 if (c >= 'A' && c <= 'Z')
1174 if (c != 'l' && c != 'u')
1176 if (c >= '0' && c <= '9')
1180 if (base > 10 && c >= 'a' && c <= 'f')
1181 n += i = c - 'a' + 10;
1182 else if (len == 0 && c == 'l')
1184 else if (len == 0 && c == 'u')
1187 return ERROR; /* Char not a digit */
1190 return ERROR; /* Invalid digit in this base */
1195 putithere->ulval = n;
1200 putithere->lval = n;
1209 enum exp_opcode opcode;
1212 const static struct token tokentab3[] =
1214 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1215 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1218 const static struct token tokentab2[] =
1220 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1221 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1222 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1223 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1224 {"%=", ASSIGN_MODIFY, BINOP_REM},
1225 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1226 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1227 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1228 {"++", INCREMENT, BINOP_END},
1229 {"--", DECREMENT, BINOP_END},
1230 {"->", ARROW, BINOP_END},
1231 {"&&", AND, BINOP_END},
1232 {"||", OR, BINOP_END},
1233 {"::", COLONCOLON, BINOP_END},
1234 {"<<", LSH, BINOP_END},
1235 {">>", RSH, BINOP_END},
1236 {"==", EQUAL, BINOP_END},
1237 {"!=", NOTEQUAL, BINOP_END},
1238 {"<=", LEQ, BINOP_END},
1239 {">=", GEQ, BINOP_END}
1242 /* assign machine-independent names to certain registers
1243 * (unless overridden by the REGISTER_NAMES table)
1250 { "pc", PC_REGNUM },
1253 { "fp", FP_REGNUM },
1256 { "sp", SP_REGNUM },
1259 { "ps", PS_REGNUM },
1263 #define NUM_STD_REGS (sizeof std_regs / sizeof std_regs[0])
1265 /* Read one token, getting characters through lexptr. */
1271 register int namelen;
1272 register unsigned i;
1273 register char *tokstart;
1278 /* See if it is a special token of length 3. */
1279 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1280 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1283 yylval.opcode = tokentab3[i].opcode;
1284 return tokentab3[i].token;
1287 /* See if it is a special token of length 2. */
1288 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1289 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1292 yylval.opcode = tokentab2[i].opcode;
1293 return tokentab2[i].token;
1296 switch (c = *tokstart)
1311 c = parse_escape (&lexptr);
1315 error ("Invalid character constant.");
1324 if (paren_depth == 0)
1331 if (comma_terminates && paren_depth == 0)
1337 /* Might be a floating point number. */
1338 if (lexptr[1] < '0' || lexptr[1] > '9')
1339 goto symbol; /* Nope, must be a symbol. */
1340 /* FALL THRU into number case. */
1353 /* It's a number. */
1354 int got_dot = 0, got_e = 0, toktype;
1355 register char *p = tokstart;
1356 int hex = input_radix > 10;
1358 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1363 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1371 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1372 got_dot = got_e = 1;
1373 else if (!hex && !got_dot && *p == '.')
1375 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1376 && (*p == '-' || *p == '+'))
1377 /* This is the sign of the exponent, not the end of the
1380 /* We will take any letters or digits. parse_number will
1381 complain if past the radix, or if L or U are not final. */
1382 else if ((*p < '0' || *p > '9')
1383 && ((*p < 'a' || *p > 'z')
1384 && (*p < 'A' || *p > 'Z')))
1387 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1388 if (toktype == ERROR)
1390 char *err_copy = (char *) alloca (p - tokstart + 1);
1392 bcopy (tokstart, err_copy, p - tokstart);
1393 err_copy[p - tokstart] = 0;
1394 error ("Invalid number \"%s\".", err_copy);
1425 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1428 c = tokstart[++namelen];
1429 if (c >= '0' && c <= '9')
1431 c = tokstart[++namelen];
1432 if (c >= '0' && c <= '9')
1433 c = tokstart[++namelen];
1436 yylval.sval.ptr = tokstart + 1;
1437 yylval.sval.length = namelen - 1;
1438 lexptr += namelen + 1;
1442 if (!(c == '_' || c == '$'
1443 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1444 /* We must have come across a bad character (e.g. ';'). */
1445 error ("Invalid character '%c' in expression.", c);
1447 /* It's a name. See how long it is. */
1449 for (c = tokstart[namelen];
1450 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1451 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1452 c = tokstart[++namelen])
1455 /* The token "if" terminates the expression and is NOT
1456 removed from the input stream. */
1457 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1464 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1465 and $$digits (equivalent to $<-digits> if you could type that).
1466 Make token type LAST, and put the number (the digits) in yylval. */
1468 if (*tokstart == '$')
1470 register int negate = 0;
1472 /* Double dollar means negate the number and add -1 as well.
1473 Thus $$ alone means -1. */
1474 if (namelen >= 2 && tokstart[1] == '$')
1481 /* Just dollars (one or two) */
1482 yylval.lval = - negate;
1485 /* Is the rest of the token digits? */
1486 for (; c < namelen; c++)
1487 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1491 yylval.lval = atoi (tokstart + 1 + negate);
1493 yylval.lval = - yylval.lval;
1498 /* Handle tokens that refer to machine registers:
1499 $ followed by a register name. */
1501 if (*tokstart == '$') {
1502 for (c = 0; c < NUM_REGS; c++)
1503 if (namelen - 1 == strlen (reg_names[c])
1504 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1509 for (c = 0; c < NUM_STD_REGS; c++)
1510 if (namelen - 1 == strlen (std_regs[c].name)
1511 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1513 yylval.lval = std_regs[c].regnum;
1517 /* Catch specific keywords. Should be done with a data structure. */
1521 if (!strncmp (tokstart, "unsigned", 8))
1525 if (!strncmp (tokstart, "struct", 6))
1527 if (!strncmp (tokstart, "signed", 6))
1529 if (!strncmp (tokstart, "sizeof", 6))
1533 if (!strncmp (tokstart, "union", 5))
1535 if (!strncmp (tokstart, "short", 5))
1539 if (!strncmp (tokstart, "enum", 4))
1541 if (!strncmp (tokstart, "long", 4))
1543 if (!strncmp (tokstart, "this", 4))
1545 static const char this_name[] =
1546 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1548 if (lookup_symbol (this_name, expression_context_block,
1549 VAR_NAMESPACE, 0, NULL))
1554 if (!strncmp (tokstart, "int", 3))
1561 yylval.sval.ptr = tokstart;
1562 yylval.sval.length = namelen;
1564 /* Any other names starting in $ are debugger internal variables. */
1566 if (*tokstart == '$')
1568 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1572 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1573 functions or symtabs. If this is not so, then ...
1574 Use token-type TYPENAME for symbols that happen to be defined
1575 currently as names of types; NAME for other symbols.
1576 The caller is not constrained to care about the distinction. */
1578 char *tmp = copy_name (yylval.sval);
1580 int is_a_field_of_this = 0;
1583 sym = lookup_symbol (tmp, expression_context_block,
1584 VAR_NAMESPACE, &is_a_field_of_this, NULL);
1585 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1586 lookup_partial_symtab (tmp))
1588 yylval.ssym.sym = sym;
1589 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1592 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1594 yylval.tsym.type = SYMBOL_TYPE (sym);
1597 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1600 /* Input names that aren't symbols but ARE valid hex numbers,
1601 when the input radix permits them, can be names or numbers
1602 depending on the parse. Note we support radixes > 16 here. */
1604 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1605 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1607 YYSTYPE newlval; /* Its value is ignored. */
1608 hextype = parse_number (tokstart, namelen, 0, &newlval);
1611 yylval.ssym.sym = sym;
1612 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1615 if (hextype == UINT)
1617 yylval.ssym.sym = sym;
1618 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1619 return NAME_OR_UINT;
1623 /* Any other kind of symbol */
1624 yylval.ssym.sym = sym;
1625 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1635 error ("Invalid syntax in expression.");
1638 /* Return a null-terminated temporary copy of the name
1639 of a string token. */
1643 struct stoken token;
1645 bcopy (token.ptr, namecopy, token.length);
1646 namecopy[token.length] = 0;
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). */
1653 static void prefixify_subexp ();
1656 prefixify_expression (expr)
1657 register struct expression *expr;
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;
1664 temp = (struct expression *) alloca (len);
1666 /* Copy the original expression into temp. */
1667 bcopy (expr, temp, len);
1669 prefixify_subexp (temp, expr, inpos, outpos);
1672 /* Return the number of exp_elements in the subexpression of EXPR
1673 whose last exp_element is at index ENDPOS - 1 in EXPR. */
1676 length_of_subexp (expr, endpos)
1677 register struct expression *expr;
1678 register int endpos;
1680 register int oplen = 1;
1681 register int args = 0;
1685 error ("?error in length_of_subexp");
1687 i = (int) expr->elts[endpos - 1].opcode;
1693 oplen = 4 + ((expr->elts[endpos - 2].longconst
1694 + sizeof (union exp_element))
1695 / sizeof (union exp_element));
1706 case OP_INTERNALVAR:
1712 args = 1 + expr->elts[endpos - 2].longconst;
1721 case STRUCTOP_STRUCT:
1725 oplen = 3 + ((expr->elts[endpos - 2].longconst
1726 + sizeof (union exp_element))
1727 / sizeof (union exp_element));
1734 case BINOP_ASSIGN_MODIFY:
1745 args = 1 + (i < (int) BINOP_END);
1750 oplen += length_of_subexp (expr, endpos - oplen);
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. */
1762 prefixify_subexp (inexpr, outexpr, inend, outbeg)
1763 register struct expression *inexpr;
1764 struct expression *outexpr;
1768 register int oplen = 1;
1769 register int args = 0;
1772 enum exp_opcode opcode;
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). */
1778 opcode = inexpr->elts[inend - 1].opcode;
1783 oplen = 4 + ((inexpr->elts[inend - 2].longconst
1784 + sizeof (union exp_element))
1785 / sizeof (union exp_element));
1796 case OP_INTERNALVAR:
1802 args = 1 + inexpr->elts[inend - 2].longconst;
1811 case STRUCTOP_STRUCT:
1815 oplen = 3 + ((inexpr->elts[inend - 2].longconst
1816 + sizeof (union exp_element))
1817 / sizeof (union exp_element));
1825 case BINOP_ASSIGN_MODIFY:
1836 args = 1 + ((int) opcode < (int) BINOP_END);
1839 /* Copy the final operator itself, from the end of the input
1840 to the beginning of the output. */
1842 bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg],
1843 oplen * sizeof (union exp_element));
1846 /* Find the lengths of the arg subexpressions. */
1847 arglens = (int *) alloca (args * sizeof (int));
1848 for (i = args - 1; i >= 0; i--)
1850 oplen = length_of_subexp (inexpr, inend);
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++)
1865 prefixify_subexp (inexpr, outexpr, inend, outbeg);
1870 /* This page contains the two entry points to this file. */
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).
1880 If COMMA is nonzero, stop if a comma is reached. */
1883 parse_c_1 (stringptr, block, comma)
1885 struct block *block;
1888 struct cleanup *old_chain;
1890 lexptr = *stringptr;
1893 type_stack_depth = 0;
1895 comma_terminates = comma;
1897 if (lexptr == 0 || *lexptr == 0)
1898 error_no_arg ("expression to compute");
1900 old_chain = make_cleanup (free_funcalls, 0);
1903 expression_context_block = block ? block : get_selected_block ();
1905 namecopy = (char *) alloca (strlen (lexptr) + 1);
1908 expout = (struct expression *)
1909 xmalloc (sizeof (struct expression)
1910 + expout_size * sizeof (union exp_element));
1911 make_cleanup (free_current_contents, &expout);
1914 discard_cleanups (old_chain);
1915 expout->nelts = expout_ptr;
1916 expout = (struct expression *)
1918 sizeof (struct expression)
1919 + expout_ptr * sizeof (union exp_element));
1920 prefixify_expression (expout);
1921 *stringptr = lexptr;
1925 /* Parse STRING as an expression, and complain if this fails
1926 to use up all of the contents of STRING. */
1929 parse_c_expression (string)
1932 register struct expression *exp;
1933 exp = parse_c_1 (&string, 0, 0);
1935 error ("Junk after end of expression.");
1941 enum type_pieces tp;
1943 if (type_stack_depth == type_stack_size)
1945 type_stack_size *= 2;
1946 type_stack = (union type_stack_elt *)
1947 xrealloc (type_stack, type_stack_size * sizeof (*type_stack));
1949 type_stack[type_stack_depth++].piece = tp;
1956 if (type_stack_depth == type_stack_size)
1958 type_stack_size *= 2;
1959 type_stack = (union type_stack_elt *)
1960 xrealloc (type_stack, type_stack_size * sizeof (*type_stack));
1962 type_stack[type_stack_depth++].int_val = n;
1965 static enum type_pieces
1968 if (type_stack_depth)
1969 return type_stack[--type_stack_depth].piece;
1976 if (type_stack_depth)
1977 return type_stack[--type_stack_depth].int_val;
1978 /* "Can't happen". */
1983 _initialize_expread ()
1985 type_stack_size = 80;
1986 type_stack_depth = 0;
1987 type_stack = (union type_stack_elt *)
1988 xmalloc (type_stack_size * sizeof (*type_stack));