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 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
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 static enum type_pieces *type_stack;
112 static int type_stack_depth, type_stack_size;
114 static void push_type ();
115 static enum type_pieces pop_type ();
117 /* Allow debugging of parsing. */
121 /* Although the yacc "value" of an expression is not used,
122 since the result is stored in the structure being created,
123 other node types do have values. */
128 unsigned LONGEST ulval;
134 struct symtoken ssym;
137 enum exp_opcode opcode;
138 struct internalvar *ivar;
144 %type <voidval> exp exp1 start variable
145 %type <tval> type typebase
146 %type <tvec> nonempty_typelist
149 /* Fancy type parsing. */
150 %type <voidval> func_mod direct_abs_decl abs_decl
152 %type <lval> array_mod
154 %token <lval> INT CHAR
158 /* Both NAME and TYPENAME tokens represent symbols in the input,
159 and both convey their data as strings.
160 But a TYPENAME is a string that happens to be defined as a typedef
161 or builtin type name (such as int or char)
162 and a NAME is any other symbol.
164 Contexts where this distinction is not important can use the
165 nonterminal "name", which matches either NAME or TYPENAME. */
168 %token <ssym> NAME BLOCKNAME
169 %token <tsym> TYPENAME
171 %type <ssym> name_not_typename
172 %type <tsym> typename
174 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
175 but which would parse as a valid number in the current input radix.
176 E.g. "c" when input_radix==16. Depending on the parse, it will be
177 turned into a name or into a number. NAME_OR_UINT ditto. */
179 %token <ssym> NAME_OR_INT NAME_OR_UINT
181 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
184 /* Special type cases, put in to allow the parser to distinguish different
186 %token SIGNED LONG SHORT INT_KEYWORD
188 %token <lval> LAST REGNAME
190 %token <ivar> VARIABLE
192 %token <opcode> ASSIGN_MODIFY
199 %right '=' ASSIGN_MODIFY
207 %left '<' '>' LEQ GEQ
212 %right UNARY INCREMENT DECREMENT
213 %right ARROW '.' '[' '('
221 /* Expressions, including the comma operator. */
224 { write_exp_elt_opcode (BINOP_COMMA); }
227 /* Expressions, not including the comma operator. */
228 exp : '*' exp %prec UNARY
229 { write_exp_elt_opcode (UNOP_IND); }
231 exp : '&' exp %prec UNARY
232 { write_exp_elt_opcode (UNOP_ADDR); }
234 exp : '-' exp %prec UNARY
235 { write_exp_elt_opcode (UNOP_NEG); }
238 exp : '!' exp %prec UNARY
239 { write_exp_elt_opcode (UNOP_ZEROP); }
242 exp : '~' exp %prec UNARY
243 { write_exp_elt_opcode (UNOP_LOGNOT); }
246 exp : INCREMENT exp %prec UNARY
247 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
250 exp : DECREMENT exp %prec UNARY
251 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
254 exp : exp INCREMENT %prec UNARY
255 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
258 exp : exp DECREMENT %prec UNARY
259 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
262 exp : SIZEOF exp %prec UNARY
263 { write_exp_elt_opcode (UNOP_SIZEOF); }
267 { write_exp_elt_opcode (STRUCTOP_PTR);
268 write_exp_string ($3);
269 write_exp_elt_opcode (STRUCTOP_PTR); }
272 exp : exp ARROW '*' exp
273 { write_exp_elt_opcode (STRUCTOP_MPTR); }
277 { write_exp_elt_opcode (STRUCTOP_STRUCT);
278 write_exp_string ($3);
279 write_exp_elt_opcode (STRUCTOP_STRUCT); }
282 exp : exp '.' '*' exp
283 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
286 exp : exp '[' exp1 ']'
287 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
291 /* This is to save the value of arglist_len
292 being accumulated by an outer function call. */
293 { start_arglist (); }
294 arglist ')' %prec ARROW
295 { write_exp_elt_opcode (OP_FUNCALL);
296 write_exp_elt_longcst ((LONGEST) end_arglist ());
297 write_exp_elt_opcode (OP_FUNCALL); }
307 arglist : arglist ',' exp %prec ABOVE_COMMA
311 exp : '{' type '}' exp %prec UNARY
312 { write_exp_elt_opcode (UNOP_MEMVAL);
313 write_exp_elt_type ($2);
314 write_exp_elt_opcode (UNOP_MEMVAL); }
317 exp : '(' type ')' exp %prec UNARY
318 { write_exp_elt_opcode (UNOP_CAST);
319 write_exp_elt_type ($2);
320 write_exp_elt_opcode (UNOP_CAST); }
327 /* Binary operators in order of decreasing precedence. */
330 { write_exp_elt_opcode (BINOP_REPEAT); }
334 { write_exp_elt_opcode (BINOP_MUL); }
338 { write_exp_elt_opcode (BINOP_DIV); }
342 { write_exp_elt_opcode (BINOP_REM); }
346 { write_exp_elt_opcode (BINOP_ADD); }
350 { write_exp_elt_opcode (BINOP_SUB); }
354 { write_exp_elt_opcode (BINOP_LSH); }
358 { write_exp_elt_opcode (BINOP_RSH); }
362 { write_exp_elt_opcode (BINOP_EQUAL); }
365 exp : exp NOTEQUAL exp
366 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
370 { write_exp_elt_opcode (BINOP_LEQ); }
374 { write_exp_elt_opcode (BINOP_GEQ); }
378 { write_exp_elt_opcode (BINOP_LESS); }
382 { write_exp_elt_opcode (BINOP_GTR); }
386 { write_exp_elt_opcode (BINOP_LOGAND); }
390 { write_exp_elt_opcode (BINOP_LOGXOR); }
394 { write_exp_elt_opcode (BINOP_LOGIOR); }
398 { write_exp_elt_opcode (BINOP_AND); }
402 { write_exp_elt_opcode (BINOP_OR); }
405 exp : exp '?' exp ':' exp %prec '?'
406 { write_exp_elt_opcode (TERNOP_COND); }
410 { write_exp_elt_opcode (BINOP_ASSIGN); }
413 exp : exp ASSIGN_MODIFY exp
414 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
415 write_exp_elt_opcode ($2);
416 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
420 { write_exp_elt_opcode (OP_LONG);
421 if ($1 == (int) $1 || $1 == (unsigned int) $1)
422 write_exp_elt_type (builtin_type_int);
424 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
425 write_exp_elt_longcst ((LONGEST) $1);
426 write_exp_elt_opcode (OP_LONG); }
431 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
432 write_exp_elt_opcode (OP_LONG);
433 if (val.lval == (int) val.lval ||
434 val.lval == (unsigned int) val.lval)
435 write_exp_elt_type (builtin_type_int);
437 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
438 write_exp_elt_longcst (val.lval);
439 write_exp_elt_opcode (OP_LONG); }
444 write_exp_elt_opcode (OP_LONG);
445 if ($1 == (unsigned int) $1)
446 write_exp_elt_type (builtin_type_unsigned_int);
448 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
449 write_exp_elt_longcst ((LONGEST) $1);
450 write_exp_elt_opcode (OP_LONG);
456 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
457 write_exp_elt_opcode (OP_LONG);
458 if (val.ulval == (unsigned int) val.ulval)
459 write_exp_elt_type (builtin_type_unsigned_int);
461 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
462 write_exp_elt_longcst ((LONGEST)val.ulval);
463 write_exp_elt_opcode (OP_LONG);
468 { write_exp_elt_opcode (OP_LONG);
469 write_exp_elt_type (builtin_type_char);
470 write_exp_elt_longcst ((LONGEST) $1);
471 write_exp_elt_opcode (OP_LONG); }
475 { write_exp_elt_opcode (OP_DOUBLE);
476 write_exp_elt_type (builtin_type_double);
477 write_exp_elt_dblcst ($1);
478 write_exp_elt_opcode (OP_DOUBLE); }
485 { write_exp_elt_opcode (OP_LAST);
486 write_exp_elt_longcst ((LONGEST) $1);
487 write_exp_elt_opcode (OP_LAST); }
491 { write_exp_elt_opcode (OP_REGISTER);
492 write_exp_elt_longcst ((LONGEST) $1);
493 write_exp_elt_opcode (OP_REGISTER); }
497 { write_exp_elt_opcode (OP_INTERNALVAR);
498 write_exp_elt_intern ($1);
499 write_exp_elt_opcode (OP_INTERNALVAR); }
502 exp : SIZEOF '(' type ')' %prec UNARY
503 { write_exp_elt_opcode (OP_LONG);
504 write_exp_elt_type (builtin_type_int);
505 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
506 write_exp_elt_opcode (OP_LONG); }
510 { write_exp_elt_opcode (OP_STRING);
511 write_exp_string ($1);
512 write_exp_elt_opcode (OP_STRING); }
517 { write_exp_elt_opcode (OP_THIS);
518 write_exp_elt_opcode (OP_THIS); }
526 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
530 lookup_symtab (copy_name ($1.stoken));
532 $$ = BLOCKVECTOR_BLOCK
533 (BLOCKVECTOR (tem), STATIC_BLOCK);
535 error ("No file or function \"%s\".",
536 copy_name ($1.stoken));
541 block : block COLONCOLON name
543 = lookup_symbol (copy_name ($3), $1,
544 VAR_NAMESPACE, 0, NULL);
545 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
546 error ("No function \"%s\" in specified context.",
548 $$ = SYMBOL_BLOCK_VALUE (tem); }
551 variable: block COLONCOLON name
552 { struct symbol *sym;
553 sym = lookup_symbol (copy_name ($3), $1,
554 VAR_NAMESPACE, 0, NULL);
556 error ("No symbol \"%s\" in specified context.",
558 write_exp_elt_opcode (OP_VAR_VALUE);
559 write_exp_elt_sym (sym);
560 write_exp_elt_opcode (OP_VAR_VALUE); }
563 variable: typebase COLONCOLON name
565 struct type *type = $1;
566 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
567 && TYPE_CODE (type) != TYPE_CODE_UNION)
568 error ("`%s' is not defined as an aggregate type.",
571 write_exp_elt_opcode (OP_SCOPE);
572 write_exp_elt_type (type);
573 write_exp_string ($3);
574 write_exp_elt_opcode (OP_SCOPE);
578 char *name = copy_name ($2);
583 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
586 write_exp_elt_opcode (OP_VAR_VALUE);
587 write_exp_elt_sym (sym);
588 write_exp_elt_opcode (OP_VAR_VALUE);
591 for (i = 0; i < misc_function_count; i++)
592 if (!strcmp (misc_function_vector[i].name, name))
595 if (i < misc_function_count)
597 enum misc_function_type mft =
598 misc_function_vector[i].type;
600 write_exp_elt_opcode (OP_LONG);
601 write_exp_elt_type (builtin_type_int);
602 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
603 write_exp_elt_opcode (OP_LONG);
604 write_exp_elt_opcode (UNOP_MEMVAL);
605 if (mft == mf_data || mft == mf_bss)
606 write_exp_elt_type (builtin_type_int);
607 else if (mft == mf_text)
608 write_exp_elt_type (lookup_function_type (builtin_type_int));
610 write_exp_elt_type (builtin_type_char);
611 write_exp_elt_opcode (UNOP_MEMVAL);
615 && partial_symtab_list == 0)
616 error ("No symbol table is loaded. Use the \"file\" command.");
618 error ("No symbol \"%s\" in current context.", name);
622 variable: name_not_typename
623 { struct symbol *sym = $1.sym;
633 if (innermost_block == 0 ||
634 contained_in (block_found,
636 innermost_block = block_found;
638 write_exp_elt_opcode (OP_VAR_VALUE);
639 write_exp_elt_sym (sym);
640 write_exp_elt_opcode (OP_VAR_VALUE);
642 else if ($1.is_a_field_of_this)
644 /* C++: it hangs off of `this'. Must
645 not inadvertently convert from a method call
647 if (innermost_block == 0 ||
648 contained_in (block_found, innermost_block))
649 innermost_block = block_found;
650 write_exp_elt_opcode (OP_THIS);
651 write_exp_elt_opcode (OP_THIS);
652 write_exp_elt_opcode (STRUCTOP_PTR);
653 write_exp_string ($1.stoken);
654 write_exp_elt_opcode (STRUCTOP_PTR);
659 register char *arg = copy_name ($1.stoken);
661 /* FIXME, this search is linear! At least
662 optimize the strcmp with a 1-char cmp... */
663 for (i = 0; i < misc_function_count; i++)
664 if (!strcmp (misc_function_vector[i].name, arg))
667 if (i < misc_function_count)
669 enum misc_function_type mft =
670 misc_function_vector[i].type;
672 write_exp_elt_opcode (OP_LONG);
673 write_exp_elt_type (builtin_type_int);
674 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
675 write_exp_elt_opcode (OP_LONG);
676 write_exp_elt_opcode (UNOP_MEMVAL);
677 if (mft == mf_data || mft == mf_bss)
678 write_exp_elt_type (builtin_type_int);
679 else if (mft == mf_text)
680 write_exp_elt_type (lookup_function_type (builtin_type_int));
682 write_exp_elt_type (builtin_type_char);
683 write_exp_elt_opcode (UNOP_MEMVAL);
685 else if (symtab_list == 0
686 && partial_symtab_list == 0)
687 error ("No symbol table is loaded. Use the \"file\" command.");
689 error ("No symbol \"%s\" in current context.",
690 copy_name ($1.stoken));
699 /* This is where the interesting stuff happens. */
702 struct type *follow_type = $1;
711 follow_type = lookup_pointer_type (follow_type);
714 follow_type = lookup_reference_type (follow_type);
717 array_size = (int) pop_type ();
718 if (array_size != -1)
719 follow_type = create_array_type (follow_type,
722 follow_type = lookup_pointer_type (follow_type);
725 follow_type = lookup_function_type (follow_type);
733 { push_type (tp_pointer); $$ = 0; }
735 { push_type (tp_pointer); $$ = $2; }
739 direct_abs_decl: '(' abs_decl ')'
741 | direct_abs_decl array_mod
743 push_type ((enum type_pieces) $2);
744 push_type (tp_array);
748 push_type ((enum type_pieces) $1);
749 push_type (tp_array);
752 | direct_abs_decl func_mod
753 { push_type (tp_function); }
755 { push_type (tp_function); }
769 | typebase COLONCOLON '*'
770 { $$ = lookup_member_type (builtin_type_int, $1); }
771 | type '(' typebase COLONCOLON '*' ')'
772 { $$ = lookup_member_type ($1, $3); }
773 | type '(' typebase COLONCOLON '*' ')' '(' ')'
774 { $$ = lookup_member_type
775 (lookup_function_type ($1), $3); }
776 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
777 { $$ = lookup_member_type
778 (lookup_function_type ($1), $3);
786 { $$ = builtin_type_int; }
788 { $$ = builtin_type_long; }
790 { $$ = builtin_type_short; }
792 { $$ = builtin_type_long; }
793 | UNSIGNED LONG INT_KEYWORD
794 { $$ = builtin_type_unsigned_long; }
796 { $$ = builtin_type_short; }
797 | UNSIGNED SHORT INT_KEYWORD
798 { $$ = builtin_type_unsigned_short; }
800 { $$ = lookup_struct (copy_name ($2),
801 expression_context_block); }
803 { $$ = lookup_union (copy_name ($2),
804 expression_context_block); }
806 { $$ = lookup_enum (copy_name ($2),
807 expression_context_block); }
809 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
811 { $$ = builtin_type_unsigned_int; }
815 { $$ = builtin_type_int; }
821 $$.stoken.ptr = "int";
822 $$.stoken.length = 3;
823 $$.type = builtin_type_int;
827 $$.stoken.ptr = "long";
828 $$.stoken.length = 4;
829 $$.type = builtin_type_long;
833 $$.stoken.ptr = "short";
834 $$.stoken.length = 5;
835 $$.type = builtin_type_short;
841 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
842 $$[0] = (struct type *)0;
845 | nonempty_typelist ',' type
846 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
847 $$ = (struct type **)xrealloc ($1, len);
848 $$[$<ivec>$[0]] = $3;
852 name : NAME { $$ = $1.stoken; }
853 | BLOCKNAME { $$ = $1.stoken; }
854 | TYPENAME { $$ = $1.stoken; }
855 | NAME_OR_INT { $$ = $1.stoken; }
856 | NAME_OR_UINT { $$ = $1.stoken; }
859 name_not_typename : NAME
861 /* These would be useful if name_not_typename was useful, but it is just
862 a fake for "variable", so these cause reduce/reduce conflicts because
863 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
864 =exp) or just an exp. If name_not_typename was ever used in an lvalue
865 context where only a name could occur, this might be useful.
873 /* Begin counting arguments for a function call,
874 saving the data about any containing call. */
879 register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
881 new->next = funcall_chain;
882 new->arglist_len = arglist_len;
887 /* Return the number of arguments in a function call just terminated,
888 and restore the data for the containing function call. */
893 register int val = arglist_len;
894 register struct funcall *call = funcall_chain;
895 funcall_chain = call->next;
896 arglist_len = call->arglist_len;
901 /* Free everything in the funcall chain.
902 Used when there is an error inside parsing. */
907 register struct funcall *call, *next;
909 for (call = funcall_chain; call; call = next)
916 /* This page contains the functions for adding data to the struct expression
917 being constructed. */
919 /* Add one element to the end of the expression. */
921 /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
922 a register through here */
925 write_exp_elt (expelt)
926 union exp_element expelt;
928 if (expout_ptr >= expout_size)
931 expout = (struct expression *) xrealloc (expout,
932 sizeof (struct expression)
933 + expout_size * sizeof (union exp_element));
935 expout->elts[expout_ptr++] = expelt;
939 write_exp_elt_opcode (expelt)
940 enum exp_opcode expelt;
942 union exp_element tmp;
950 write_exp_elt_sym (expelt)
951 struct symbol *expelt;
953 union exp_element tmp;
961 write_exp_elt_longcst (expelt)
964 union exp_element tmp;
966 tmp.longconst = expelt;
972 write_exp_elt_dblcst (expelt)
975 union exp_element tmp;
977 tmp.doubleconst = expelt;
983 write_exp_elt_type (expelt)
986 union exp_element tmp;
994 write_exp_elt_intern (expelt)
995 struct internalvar *expelt;
997 union exp_element tmp;
999 tmp.internalvar = expelt;
1001 write_exp_elt (tmp);
1004 /* Add a string constant to the end of the expression.
1005 Follow it by its length in bytes, as a separate exp_element. */
1008 write_exp_string (str)
1011 register int len = str.length;
1013 = (len + sizeof (union exp_element)) / sizeof (union exp_element);
1015 expout_ptr += lenelt;
1017 if (expout_ptr >= expout_size)
1019 expout_size = max (expout_size * 2, expout_ptr + 10);
1020 expout = (struct expression *)
1021 xrealloc (expout, (sizeof (struct expression)
1022 + (expout_size * sizeof (union exp_element))));
1024 bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len);
1025 ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0;
1026 write_exp_elt_longcst ((LONGEST) len);
1029 /* During parsing of a C expression, the pointer to the next character
1030 is in this variable. */
1032 static char *lexptr;
1034 /* Tokens that refer to names do so with explicit pointer and length,
1035 so they can share the storage that lexptr is parsing.
1037 When it is necessary to pass a name to a function that expects
1038 a null-terminated string, the substring is copied out
1039 into a block of storage that namecopy points to.
1041 namecopy is allocated once, guaranteed big enough, for each parsing. */
1043 static char *namecopy;
1045 /* Current depth in parentheses within the expression. */
1047 static int paren_depth;
1049 /* Nonzero means stop parsing on first comma (if not within parentheses). */
1051 static int comma_terminates;
1053 /* Take care of parsing a number (anything that starts with a digit).
1054 Set yylval and return the token type; update lexptr.
1055 LEN is the number of characters in it. */
1057 /*** Needs some error checking for the float case ***/
1060 parse_number (p, len, parsed_float, putithere)
1066 register LONGEST n = 0;
1069 register int base = input_radix;
1072 extern double atof ();
1076 /* It's a float since it contains a point or an exponent. */
1077 putithere->dval = atof (p);
1081 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1115 if (c >= 'A' && c <= 'Z')
1117 if (c != 'l' && c != 'u')
1119 if (c >= '0' && c <= '9')
1123 if (base > 10 && c >= 'a' && c <= 'f')
1124 n += i = c - 'a' + 10;
1125 else if (len == 0 && c == 'l')
1127 else if (len == 0 && c == 'u')
1130 return ERROR; /* Char not a digit */
1133 return ERROR; /* Invalid digit in this base */
1138 putithere->ulval = n;
1143 putithere->lval = n;
1152 enum exp_opcode opcode;
1155 static struct token tokentab3[] =
1157 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1158 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1161 static struct token tokentab2[] =
1163 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1164 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1165 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1166 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1167 {"%=", ASSIGN_MODIFY, BINOP_REM},
1168 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1169 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1170 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1171 {"++", INCREMENT, BINOP_END},
1172 {"--", DECREMENT, BINOP_END},
1173 {"->", ARROW, BINOP_END},
1174 {"&&", AND, BINOP_END},
1175 {"||", OR, BINOP_END},
1176 {"::", COLONCOLON, BINOP_END},
1177 {"<<", LSH, BINOP_END},
1178 {">>", RSH, BINOP_END},
1179 {"==", EQUAL, BINOP_END},
1180 {"!=", NOTEQUAL, BINOP_END},
1181 {"<=", LEQ, BINOP_END},
1182 {">=", GEQ, BINOP_END}
1185 /* assign machine-independent names to certain registers
1186 * (unless overridden by the REGISTER_NAMES table)
1193 { "pc", PC_REGNUM },
1196 { "fp", FP_REGNUM },
1199 { "sp", SP_REGNUM },
1202 { "ps", PS_REGNUM },
1206 #define NUM_STD_REGS (sizeof std_regs / sizeof std_regs[0])
1208 /* Read one token, getting characters through lexptr. */
1214 register int namelen;
1215 register unsigned i;
1216 register char *tokstart;
1221 /* See if it is a special token of length 3. */
1222 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1223 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1226 yylval.opcode = tokentab3[i].opcode;
1227 return tokentab3[i].token;
1230 /* See if it is a special token of length 2. */
1231 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1232 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1235 yylval.opcode = tokentab2[i].opcode;
1236 return tokentab2[i].token;
1239 switch (c = *tokstart)
1254 c = parse_escape (&lexptr);
1258 error ("Invalid character constant.");
1267 if (paren_depth == 0)
1274 if (comma_terminates && paren_depth == 0)
1280 /* Might be a floating point number. */
1281 if (lexptr[1] < '0' || lexptr[1] > '9')
1282 goto symbol; /* Nope, must be a symbol. */
1283 /* FALL THRU into number case. */
1296 /* It's a number. */
1297 int got_dot = 0, got_e = 0, toktype;
1298 register char *p = tokstart;
1299 int hex = input_radix > 10;
1301 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1306 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1314 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1315 got_dot = got_e = 1;
1316 else if (!hex && !got_dot && *p == '.')
1318 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1319 && (*p == '-' || *p == '+'))
1320 /* This is the sign of the exponent, not the end of the
1323 /* We will take any letters or digits. parse_number will
1324 complain if past the radix, or if L or U are not final. */
1325 else if ((*p < '0' || *p > '9')
1326 && ((*p < 'a' || *p > 'z')
1327 && (*p < 'A' || *p > 'Z')))
1330 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1331 if (toktype == ERROR)
1333 char *err_copy = (char *) alloca (p - tokstart + 1);
1335 bcopy (tokstart, err_copy, p - tokstart);
1336 err_copy[p - tokstart] = 0;
1337 error ("Invalid number \"%s\".", err_copy);
1368 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1371 c = tokstart[++namelen];
1372 if (c >= '0' && c <= '9')
1374 c = tokstart[++namelen];
1375 if (c >= '0' && c <= '9')
1376 c = tokstart[++namelen];
1379 yylval.sval.ptr = tokstart + 1;
1380 yylval.sval.length = namelen - 1;
1381 lexptr += namelen + 1;
1385 if (!(c == '_' || c == '$'
1386 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1387 /* We must have come across a bad character (e.g. ';'). */
1388 error ("Invalid character '%c' in expression.", c);
1390 /* It's a name. See how long it is. */
1392 for (c = tokstart[namelen];
1393 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1394 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1395 c = tokstart[++namelen])
1398 /* The token "if" terminates the expression and is NOT
1399 removed from the input stream. */
1400 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1407 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1408 and $$digits (equivalent to $<-digits> if you could type that).
1409 Make token type LAST, and put the number (the digits) in yylval. */
1411 if (*tokstart == '$')
1413 register int negate = 0;
1415 /* Double dollar means negate the number and add -1 as well.
1416 Thus $$ alone means -1. */
1417 if (namelen >= 2 && tokstart[1] == '$')
1424 /* Just dollars (one or two) */
1425 yylval.lval = - negate;
1428 /* Is the rest of the token digits? */
1429 for (; c < namelen; c++)
1430 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1434 yylval.lval = atoi (tokstart + 1 + negate);
1436 yylval.lval = - yylval.lval;
1441 /* Handle tokens that refer to machine registers:
1442 $ followed by a register name. */
1444 if (*tokstart == '$') {
1445 for (c = 0; c < NUM_REGS; c++)
1446 if (namelen - 1 == strlen (reg_names[c])
1447 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1452 for (c = 0; c < NUM_STD_REGS; c++)
1453 if (namelen - 1 == strlen (std_regs[c].name)
1454 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1456 yylval.lval = std_regs[c].regnum;
1460 /* Catch specific keywords. Should be done with a data structure. */
1464 if (!strncmp (tokstart, "unsigned", 8))
1468 if (!strncmp (tokstart, "struct", 6))
1470 if (!strncmp (tokstart, "signed", 6))
1472 if (!strncmp (tokstart, "sizeof", 6))
1476 if (!strncmp (tokstart, "union", 5))
1478 if (!strncmp (tokstart, "short", 5))
1482 if (!strncmp (tokstart, "enum", 4))
1484 if (!strncmp (tokstart, "long", 4))
1486 if (!strncmp (tokstart, "this", 4))
1488 static const char this_name[] =
1489 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1491 if (lookup_symbol (this_name, expression_context_block,
1492 VAR_NAMESPACE, 0, NULL))
1497 if (!strncmp (tokstart, "int", 3))
1504 yylval.sval.ptr = tokstart;
1505 yylval.sval.length = namelen;
1507 /* Any other names starting in $ are debugger internal variables. */
1509 if (*tokstart == '$')
1511 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1515 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1516 functions or symtabs. If this is not so, then ...
1517 Use token-type TYPENAME for symbols that happen to be defined
1518 currently as names of types; NAME for other symbols.
1519 The caller is not constrained to care about the distinction. */
1521 char *tmp = copy_name (yylval.sval);
1523 int is_a_field_of_this = 0;
1526 sym = lookup_symbol (tmp, expression_context_block,
1527 VAR_NAMESPACE, &is_a_field_of_this, NULL);
1528 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1529 lookup_partial_symtab (tmp))
1531 yylval.ssym.sym = sym;
1532 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1535 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1537 yylval.tsym.type = SYMBOL_TYPE (sym);
1540 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1543 /* Input names that aren't symbols but ARE valid hex numbers,
1544 when the input radix permits them, can be names or numbers
1545 depending on the parse. Note we support radixes > 16 here. */
1547 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1548 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1550 YYSTYPE newlval; /* Its value is ignored. */
1551 hextype = parse_number (tokstart, namelen, 0, &newlval);
1554 yylval.ssym.sym = sym;
1555 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1558 if (hextype == UINT)
1560 yylval.ssym.sym = sym;
1561 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1562 return NAME_OR_UINT;
1566 /* Any other kind of symbol */
1567 yylval.ssym.sym = sym;
1568 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1577 error ("Invalid syntax in expression.");
1580 /* Return a null-terminated temporary copy of the name
1581 of a string token. */
1585 struct stoken token;
1587 bcopy (token.ptr, namecopy, token.length);
1588 namecopy[token.length] = 0;
1592 /* Reverse an expression from suffix form (in which it is constructed)
1593 to prefix form (in which we can conveniently print or execute it). */
1595 static void prefixify_subexp ();
1598 prefixify_expression (expr)
1599 register struct expression *expr;
1601 register int len = sizeof (struct expression) +
1602 expr->nelts * sizeof (union exp_element);
1603 register struct expression *temp;
1604 register int inpos = expr->nelts, outpos = 0;
1606 temp = (struct expression *) alloca (len);
1608 /* Copy the original expression into temp. */
1609 bcopy (expr, temp, len);
1611 prefixify_subexp (temp, expr, inpos, outpos);
1614 /* Return the number of exp_elements in the subexpression of EXPR
1615 whose last exp_element is at index ENDPOS - 1 in EXPR. */
1618 length_of_subexp (expr, endpos)
1619 register struct expression *expr;
1620 register int endpos;
1622 register int oplen = 1;
1623 register int args = 0;
1627 error ("?error in length_of_subexp");
1629 i = (int) expr->elts[endpos - 1].opcode;
1635 oplen = 4 + ((expr->elts[endpos - 2].longconst
1636 + sizeof (union exp_element))
1637 / sizeof (union exp_element));
1648 case OP_INTERNALVAR:
1654 args = 1 + expr->elts[endpos - 2].longconst;
1663 case STRUCTOP_STRUCT:
1667 oplen = 3 + ((expr->elts[endpos - 2].longconst
1668 + sizeof (union exp_element))
1669 / sizeof (union exp_element));
1676 case BINOP_ASSIGN_MODIFY:
1687 args = 1 + (i < (int) BINOP_END);
1692 oplen += length_of_subexp (expr, endpos - oplen);
1699 /* Copy the subexpression ending just before index INEND in INEXPR
1700 into OUTEXPR, starting at index OUTBEG.
1701 In the process, convert it from suffix to prefix form. */
1704 prefixify_subexp (inexpr, outexpr, inend, outbeg)
1705 register struct expression *inexpr;
1706 struct expression *outexpr;
1710 register int oplen = 1;
1711 register int args = 0;
1714 enum exp_opcode opcode;
1716 /* Compute how long the last operation is (in OPLEN),
1717 and also how many preceding subexpressions serve as
1718 arguments for it (in ARGS). */
1720 opcode = inexpr->elts[inend - 1].opcode;
1725 oplen = 4 + ((inexpr->elts[inend - 2].longconst
1726 + sizeof (union exp_element))
1727 / sizeof (union exp_element));
1738 case OP_INTERNALVAR:
1744 args = 1 + inexpr->elts[inend - 2].longconst;
1753 case STRUCTOP_STRUCT:
1757 oplen = 3 + ((inexpr->elts[inend - 2].longconst
1758 + sizeof (union exp_element))
1759 / sizeof (union exp_element));
1767 case BINOP_ASSIGN_MODIFY:
1778 args = 1 + ((int) opcode < (int) BINOP_END);
1781 /* Copy the final operator itself, from the end of the input
1782 to the beginning of the output. */
1784 bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg],
1785 oplen * sizeof (union exp_element));
1788 /* Find the lengths of the arg subexpressions. */
1789 arglens = (int *) alloca (args * sizeof (int));
1790 for (i = args - 1; i >= 0; i--)
1792 oplen = length_of_subexp (inexpr, inend);
1797 /* Now copy each subexpression, preserving the order of
1798 the subexpressions, but prefixifying each one.
1799 In this loop, inend starts at the beginning of
1800 the expression this level is working on
1801 and marches forward over the arguments.
1802 outbeg does similarly in the output. */
1803 for (i = 0; i < args; i++)
1807 prefixify_subexp (inexpr, outexpr, inend, outbeg);
1812 /* This page contains the two entry points to this file. */
1814 /* Read a C expression from the string *STRINGPTR points to,
1815 parse it, and return a pointer to a struct expression that we malloc.
1816 Use block BLOCK as the lexical context for variable names;
1817 if BLOCK is zero, use the block of the selected stack frame.
1818 Meanwhile, advance *STRINGPTR to point after the expression,
1819 at the first nonwhite character that is not part of the expression
1820 (possibly a null character).
1822 If COMMA is nonzero, stop if a comma is reached. */
1825 parse_c_1 (stringptr, block, comma)
1827 struct block *block;
1830 struct cleanup *old_chain;
1832 lexptr = *stringptr;
1835 type_stack_depth = 0;
1837 comma_terminates = comma;
1839 if (lexptr == 0 || *lexptr == 0)
1840 error_no_arg ("expression to compute");
1842 old_chain = make_cleanup (free_funcalls, 0);
1845 expression_context_block = block ? block : get_selected_block ();
1847 namecopy = (char *) alloca (strlen (lexptr) + 1);
1850 expout = (struct expression *)
1851 xmalloc (sizeof (struct expression)
1852 + expout_size * sizeof (union exp_element));
1853 make_cleanup (free_current_contents, &expout);
1856 discard_cleanups (old_chain);
1857 expout->nelts = expout_ptr;
1858 expout = (struct expression *)
1860 sizeof (struct expression)
1861 + expout_ptr * sizeof (union exp_element));
1862 prefixify_expression (expout);
1863 *stringptr = lexptr;
1867 /* Parse STRING as an expression, and complain if this fails
1868 to use up all of the contents of STRING. */
1871 parse_c_expression (string)
1874 register struct expression *exp;
1875 exp = parse_c_1 (&string, 0, 0);
1877 error ("Junk after end of expression.");
1883 enum type_pieces tp;
1885 if (type_stack_depth == type_stack_size)
1887 type_stack_size *= 2;
1888 type_stack = (enum type_pieces *)
1889 xrealloc (type_stack, type_stack_size * sizeof (enum type_pieces));
1891 type_stack[type_stack_depth++] = tp;
1894 static enum type_pieces
1897 if (type_stack_depth)
1898 return type_stack[--type_stack_depth];
1903 _initialize_expread ()
1905 type_stack_size = 80;
1906 type_stack_depth = 0;
1907 type_stack = (enum type_pieces *)
1908 xmalloc (type_stack_size * sizeof (enum type_pieces));