1 /* Parse C expressions for GDB.
2 Copyright (C) 1986 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
21 /* Parse a C expression from text in a string,
22 and return the result as a struct expression pointer.
23 That structure contains arithmetic operations in reverse polish,
24 with constants represented by operations that are followed by special data.
25 See expression.h for the details of the format.
26 What is important here is that it can be built up sequentially
27 during the process of parsing; the lower levels of the tree always
28 come first in the result. */
35 #include "expression.h"
39 static struct expression *expout;
40 static int expout_size;
41 static int expout_ptr;
45 static void write_exp_elt ();
46 static void write_exp_string ();
47 static void start_arglist ();
48 static int end_arglist ();
49 static void free_funcalls ();
50 static char *copy_name ();
52 /* If this is nonzero, this block is used as the lexical context
55 static struct block *expression_context_block;
57 /* Number of arguments seen so far in innermost function call. */
58 static int arglist_len;
60 /* Data structure for saving values of arglist_len
61 for function calls whose arguments contain other function calls. */
69 struct funcall *funcall_chain;
71 /* This kind of datum is used to represent the name
81 /* Although the yacc "value" of an expression is not used,
82 since the result is stored in the structure being created,
83 other node types do have values. */
94 enum exp_opcode opcode;
95 struct internalvar *ivar;
98 %type <voidval> exp exp1 start variable
99 %type <tval> type typebase
102 %token <lval> INT CHAR
105 /* Both NAME and TYPENAME tokens represent symbols in the input,
106 and both convey their data as strings.
107 But a TYPENAME is a string that happens to be defined as a typedef
108 or builtin type name (such as int or char)
109 and a NAME is any other symbol.
111 Contexts where this distinction is not important can use the
112 nonterminal "name", which matches either NAME or TYPENAME. */
114 %token <sval> NAME TYPENAME STRING
117 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
119 %token <lval> LAST REGNAME
121 %token <ivar> VARIABLE
123 %token <opcode> ASSIGN_MODIFY
127 %right '=' ASSIGN_MODIFY
134 %left '<' '>' LEQ GEQ
139 %right UNARY INCREMENT DECREMENT
148 /* Expressions, including the comma operator. */
151 { write_exp_elt (BINOP_COMMA); }
154 /* Expressions, not including the comma operator. */
155 exp : '*' exp %prec UNARY
156 { write_exp_elt (UNOP_IND); }
158 exp : '&' exp %prec UNARY
159 { write_exp_elt (UNOP_ADDR); }
161 exp : '-' exp %prec UNARY
162 { write_exp_elt (UNOP_NEG); }
165 exp : '!' exp %prec UNARY
166 { write_exp_elt (UNOP_ZEROP); }
169 exp : '~' exp %prec UNARY
170 { write_exp_elt (UNOP_LOGNOT); }
173 exp : INCREMENT exp %prec UNARY
174 { write_exp_elt (UNOP_PREINCREMENT); }
177 exp : DECREMENT exp %prec UNARY
178 { write_exp_elt (UNOP_PREDECREMENT); }
181 exp : exp INCREMENT %prec UNARY
182 { write_exp_elt (UNOP_POSTINCREMENT); }
185 exp : exp DECREMENT %prec UNARY
186 { write_exp_elt (UNOP_POSTDECREMENT); }
189 exp : SIZEOF exp %prec UNARY
190 { write_exp_elt (UNOP_SIZEOF); }
194 { write_exp_elt (STRUCTOP_PTR);
195 write_exp_string ($3);
196 write_exp_elt (STRUCTOP_PTR); }
200 { write_exp_elt (STRUCTOP_STRUCT);
201 write_exp_string ($3);
202 write_exp_elt (STRUCTOP_STRUCT); }
205 exp : exp '[' exp1 ']'
206 { write_exp_elt (BINOP_SUBSCRIPT); }
210 /* This is to save the value of arglist_len
211 being accumulated by an outer function call. */
212 { start_arglist (); }
214 { write_exp_elt (OP_FUNCALL);
215 write_exp_elt (end_arglist ());
216 write_exp_elt (OP_FUNCALL); }
226 arglist : arglist ',' exp %prec ABOVE_COMMA
230 exp : '{' type '}' exp %prec UNARY
231 { write_exp_elt (UNOP_MEMVAL);
233 write_exp_elt (UNOP_MEMVAL); }
236 exp : '(' type ')' exp %prec UNARY
237 { write_exp_elt (UNOP_CAST);
239 write_exp_elt (UNOP_CAST); }
246 /* Binary operators in order of decreasing precedence. */
249 { write_exp_elt (BINOP_REPEAT); }
253 { write_exp_elt (BINOP_MUL); }
257 { write_exp_elt (BINOP_DIV); }
261 { write_exp_elt (BINOP_REM); }
265 { write_exp_elt (BINOP_ADD); }
269 { write_exp_elt (BINOP_SUB); }
273 { write_exp_elt (BINOP_LSH); }
277 { write_exp_elt (BINOP_RSH); }
281 { write_exp_elt (BINOP_EQUAL); }
284 exp : exp NOTEQUAL exp
285 { write_exp_elt (BINOP_NOTEQUAL); }
289 { write_exp_elt (BINOP_LEQ); }
293 { write_exp_elt (BINOP_GEQ); }
297 { write_exp_elt (BINOP_LESS); }
301 { write_exp_elt (BINOP_GTR); }
305 { write_exp_elt (BINOP_LOGAND); }
309 { write_exp_elt (BINOP_LOGXOR); }
313 { write_exp_elt (BINOP_LOGIOR); }
317 { write_exp_elt (BINOP_AND); }
321 { write_exp_elt (BINOP_OR); }
324 exp : exp '?' exp ':' exp
325 { write_exp_elt (TERNOP_COND); }
329 { write_exp_elt (BINOP_ASSIGN); }
332 exp : exp ASSIGN_MODIFY exp
333 { write_exp_elt (BINOP_ASSIGN_MODIFY);
335 write_exp_elt (BINOP_ASSIGN_MODIFY); }
339 { write_exp_elt (OP_LONG);
340 write_exp_elt (builtin_type_long);
342 write_exp_elt (OP_LONG); }
346 { write_exp_elt (OP_LONG);
347 write_exp_elt (builtin_type_char);
349 write_exp_elt (OP_LONG); }
353 { write_exp_elt (OP_DOUBLE);
354 write_exp_elt (builtin_type_double);
356 write_exp_elt (OP_DOUBLE); }
363 { write_exp_elt (OP_LAST);
365 write_exp_elt (OP_LAST); }
369 { write_exp_elt (OP_REGISTER);
371 write_exp_elt (OP_REGISTER); }
375 { write_exp_elt (OP_INTERNALVAR);
377 write_exp_elt (OP_INTERNALVAR); }
380 exp : SIZEOF '(' type ')'
381 { write_exp_elt (OP_LONG);
382 write_exp_elt (builtin_type_int);
383 write_exp_elt ((long) TYPE_LENGTH ($3));
384 write_exp_elt (OP_LONG); }
388 { write_exp_elt (OP_STRING);
389 write_exp_string ($1);
390 write_exp_elt (OP_STRING); }
394 { struct symtab *tem = lookup_symtab (copy_name ($1));
398 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), 1);
401 sym = lookup_symbol (copy_name ($1),
402 expression_context_block,
404 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
405 $$ = SYMBOL_BLOCK_VALUE (sym);
407 error ("No file or function \"%s\".",
412 block : block COLONCOLON name
414 = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE);
415 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
416 error ("No function \"%s\" in specified context.",
418 $$ = SYMBOL_BLOCK_VALUE (tem); }
421 variable: block COLONCOLON name
422 { struct symbol *sym;
423 sym = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE);
425 error ("No symbol \"%s\" in specified context.",
427 write_exp_elt (OP_VAR_VALUE);
429 write_exp_elt (OP_VAR_VALUE); }
433 { struct symbol *sym;
434 sym = lookup_symbol (copy_name ($1),
435 expression_context_block,
439 write_exp_elt (OP_VAR_VALUE);
441 write_exp_elt (OP_VAR_VALUE);
445 register char *arg = copy_name ($1);
447 for (i = 0; i < misc_function_count; i++)
448 if (!strcmp (misc_function_vector[i].name, arg))
451 if (i < misc_function_count)
453 write_exp_elt (OP_LONG);
454 write_exp_elt (builtin_type_int);
455 write_exp_elt (misc_function_vector[i].address);
456 write_exp_elt (OP_LONG);
457 write_exp_elt (UNOP_MEMVAL);
458 write_exp_elt (builtin_type_char);
459 write_exp_elt (UNOP_MEMVAL);
462 if (symtab_list == 0)
463 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
465 error ("No symbol \"%s\" in current context.",
473 { $$ = lookup_pointer_type ($1); }
478 { $$ = lookup_typename (copy_name ($1),
479 expression_context_block, 0); }
481 { $$ = lookup_struct (copy_name ($2),
482 expression_context_block); }
484 { $$ = lookup_union (copy_name ($2),
485 expression_context_block); }
487 { $$ = lookup_enum (copy_name ($2),
488 expression_context_block); }
490 { $$ = lookup_unsigned_typename (copy_name ($2)); }
498 /* Begin counting arguments for a function call,
499 saving the data about any containing call. */
504 register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
506 new->next = funcall_chain;
507 new->arglist_len = arglist_len;
512 /* Return the number of arguments in a function call just terminated,
513 and restore the data for the containing function call. */
518 register int val = arglist_len;
519 register struct funcall *call = funcall_chain;
520 funcall_chain = call->next;
521 arglist_len = call->arglist_len;
526 /* Free everything in the funcall chain.
527 Used when there is an error inside parsing. */
532 register struct funcall *call, *next;
534 for (call = funcall_chain; call; call = next)
541 /* This page contains the functions for adding data to the struct expression
542 being constructed. */
544 /* Add one element to the end of the expression. */
547 write_exp_elt (expelt)
548 union exp_element expelt;
550 if (expout_ptr >= expout_size)
553 expout = (struct expression *) xrealloc (expout,
554 sizeof (struct expression)
555 + expout_size * sizeof (union exp_element));
557 expout->elts[expout_ptr++] = expelt;
560 /* Add a string constant to the end of the expression.
561 Follow it by its length in bytes, as a separate exp_element. */
564 write_exp_string (str)
567 register int len = str.length;
569 = (len + sizeof (union exp_element)) / sizeof (union exp_element);
571 expout_ptr += lenelt;
573 if (expout_ptr >= expout_size)
575 expout_size = max (expout_size * 2, expout_ptr + 10);
576 expout = (struct expression *) xrealloc (expout,
577 sizeof (struct expression)
578 + expout_size * sizeof (union exp_element));
580 bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len);
581 ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0;
585 /* During parsing of a C expression, the pointer to the next character
586 is in this variable. */
590 /* Tokens that refer to names do so with explicit pointer and length,
591 so they can share the storage that lexptr is parsing.
593 When it is necessary to pass a name to a function that expects
594 a null-terminated string, the substring is copied out
595 into a block of storage that namecopy points to.
597 namecopy is allocated once, guaranteed big enough, for each parsing. */
599 static char *namecopy;
601 /* Current depth in parentheses within the expression. */
603 static int paren_depth;
605 /* Nonzero means stop parsing on first comma (if not within parentheses). */
607 static int comma_terminates;
609 /* Take care of parsing a number (anything that starts with a digit).
610 Set yylval and return the token type; update lexptr.
611 LEN is the number of characters in it. */
613 /*** Needs some error checking for the float case ***/
619 register char *p = lexptr;
622 register int base = 10;
623 register int len = olen;
626 extern double atof ();
628 for (c = 0; c < len; c++)
631 /* It's a float since it contains a point. */
632 yylval.dval = atof (p);
637 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2)))
650 if (c >= '0' && c <= '9')
654 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
655 if (base == 16 && c >= 'a' && c <= 'f')
657 else if (len == 0 && c == 'l')
661 err_copy = (char *) alloca (olen + 1);
662 bcopy (lexptr, err_copy, olen);
664 error ("Invalid number \"%s\".", err_copy);
678 enum exp_opcode opcode;
681 static struct token tokentab3[] =
683 {">>=", ASSIGN_MODIFY, BINOP_RSH},
684 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
687 static struct token tokentab2[] =
689 {"+=", ASSIGN_MODIFY, BINOP_ADD},
690 {"-=", ASSIGN_MODIFY, BINOP_SUB},
691 {"*=", ASSIGN_MODIFY, BINOP_MUL},
692 {"/=", ASSIGN_MODIFY, BINOP_DIV},
693 {"%=", ASSIGN_MODIFY, BINOP_REM},
694 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
695 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
696 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
697 {"++", INCREMENT, BINOP_END},
698 {"--", DECREMENT, BINOP_END},
699 {"->", ARROW, BINOP_END},
700 {"&&", AND, BINOP_END},
701 {"||", OR, BINOP_END},
702 {"::", COLONCOLON, BINOP_END},
703 {"<<", LSH, BINOP_END},
704 {">>", RSH, BINOP_END},
705 {"==", EQUAL, BINOP_END},
706 {"!=", NOTEQUAL, BINOP_END},
707 {"<=", LEQ, BINOP_END},
708 {">=", GEQ, BINOP_END}
711 /* Read one token, getting characters through lexptr. */
717 register int namelen;
719 register char *tokstart;
724 /* See if it is a special token of length 3. */
725 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
726 if (!strncmp (tokstart, tokentab3[i].operator, 3))
729 yylval.opcode = tokentab3[i].opcode;
730 return tokentab3[i].token;
733 /* See if it is a special token of length 2. */
734 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
735 if (!strncmp (tokstart, tokentab2[i].operator, 2))
738 yylval.opcode = tokentab2[i].opcode;
739 return tokentab2[i].token;
742 switch (c = *tokstart)
757 c = parse_escape (&lexptr);
761 error ("Invalid character constant.");
770 if (paren_depth == 0)
777 if (comma_terminates && paren_depth == 0)
807 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
810 c = tokstart[++namelen];
811 if (c >= '0' && c <= '9')
813 c = tokstart[++namelen];
814 if (c >= '0' && c <= '9')
815 c = tokstart[++namelen];
818 yylval.sval.ptr = tokstart + 1;
819 yylval.sval.length = namelen - 1;
820 lexptr += namelen + 1;
823 if (c >= '0' && c <= '9')
827 c = tokstart[namelen],
828 (c == '_' || c == '$' || c == '.' || (c >= '0' && c <= '9')
829 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
832 return parse_number (namelen);
835 if (!(c == '_' || c == '$'
836 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
837 error ("Invalid token in expression.");
839 /* It is a name. See how long it is. */
842 c = tokstart[namelen],
843 (c == '_' || c == '$' || (c >= '0' && c <= '9')
844 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
848 /* The token "if" terminates the expression and is NOT
849 removed from the input stream. */
850 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
857 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
858 and $$digits (equivalent to $<-digits> if you could type that).
859 Make token type LAST, and put the number (the digits) in yylval. */
861 if (*tokstart == '$')
863 register int negate = 0;
865 /* Double dollar means negate the number and add -1 as well.
866 Thus $$ alone means -1. */
867 if (namelen >= 2 && tokstart[1] == '$')
874 /* Just dollars (one or two) */
875 yylval.lval = - negate;
878 /* Is the rest of the token digits? */
879 for (; c < namelen; c++)
880 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
884 yylval.lval = atoi (tokstart + 1 + negate);
886 yylval.lval = - yylval.lval;
891 /* Handle tokens that refer to machine registers:
892 $ followed by a register name. */
894 if (*tokstart == '$')
895 for (c = 0; c < NUM_REGS; c++)
896 if (namelen - 1 == strlen (reg_names[c])
897 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
903 if (namelen == 6 && !strncmp (tokstart, "struct", 6))
907 if (namelen == 5 && !strncmp (tokstart, "union", 5))
911 if (namelen == 4 && !strncmp (tokstart, "enum", 4))
915 if (namelen == 6 && !strncmp (tokstart, "sizeof", 6))
919 if (namelen == 8 && !strncmp (tokstart, "unsigned", 6))
923 yylval.sval.ptr = tokstart;
924 yylval.sval.length = namelen;
926 /* Any other names starting in $ are debugger internal variables. */
928 if (*tokstart == '$')
930 yylval.ivar = (struct internalvar *) lookup_internalvar (copy_name (yylval.sval) + 1);
934 /* Use token-type TYPENAME for symbols that happen to be defined
935 currently as names of types; NAME for other symbols.
936 The caller is not constrained to care about the distinction. */
937 if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
945 error ("Invalid syntax in expression.");
948 /* Return a null-terminated temporary copy of the name
949 of a string token. */
955 bcopy (token.ptr, namecopy, token.length);
956 namecopy[token.length] = 0;
960 /* Reverse an expression from suffix form (in which it is constructed)
961 to prefix form (in which we can conveniently print or execute it). */
963 static void prefixify_subexp ();
966 prefixify_expression (expr)
967 register struct expression *expr;
969 register int len = sizeof (struct expression) +
970 expr->nelts * sizeof (union exp_element);
971 register struct expression *temp;
972 register int inpos = expr->nelts, outpos = 0;
974 temp = (struct expression *) alloca (len);
976 /* Copy the original expression into temp. */
977 bcopy (expr, temp, len);
979 prefixify_subexp (temp, expr, inpos, outpos);
982 /* Return the number of exp_elements in the subexpression of EXPR
983 whose last exp_element is at index ENDPOS - 1 in EXPR. */
986 length_of_subexp (expr, endpos)
987 register struct expression *expr;
990 register int oplen = 1;
991 register int args = 0;
994 i = (int) expr->elts[endpos - 1].opcode;
1006 case OP_INTERNALVAR:
1012 args = 1 + expr->elts[endpos - 2].longconst;
1021 case STRUCTOP_STRUCT:
1025 oplen = 3 + ((expr->elts[endpos - 2].longconst
1026 + sizeof (union exp_element))
1027 / sizeof (union exp_element));
1035 case BINOP_ASSIGN_MODIFY:
1041 args = 1 + (i < (int) BINOP_END);
1046 oplen += length_of_subexp (expr, endpos - oplen);
1053 /* Copy the subexpression ending just before index INEND in INEXPR
1054 into OUTEXPR, starting at index OUTBEG.
1055 In the process, convert it from suffix to prefix form. */
1058 prefixify_subexp (inexpr, outexpr, inend, outbeg)
1059 register struct expression *inexpr;
1060 struct expression *outexpr;
1064 register int oplen = 1;
1065 register int args = 0;
1068 enum exp_opcode opcode;
1070 /* Compute how long the last operation is (in OPLEN),
1071 and also how many preceding subexpressions serve as
1072 arguments for it (in ARGS). */
1074 opcode = inexpr->elts[inend - 1].opcode;
1085 case OP_INTERNALVAR:
1091 args = 1 + inexpr->elts[inend - 2].longconst;
1100 case STRUCTOP_STRUCT:
1104 oplen = 3 + ((inexpr->elts[inend - 2].longconst
1105 + sizeof (union exp_element))
1106 / sizeof (union exp_element));
1114 case BINOP_ASSIGN_MODIFY:
1120 args = 1 + ((int) opcode < (int) BINOP_END);
1123 /* Copy the final operator itself, from the end of the input
1124 to the beginning of the output. */
1126 bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg],
1127 oplen * sizeof (union exp_element));
1130 /* Find the lengths of the arg subexpressions. */
1131 arglens = (int *) alloca (args * sizeof (int));
1132 for (i = args - 1; i >= 0; i--)
1134 oplen = length_of_subexp (inexpr, inend);
1139 /* Now copy each subexpression, preserving the order of
1140 the subexpressions, but prefixifying each one.
1141 In this loop, inend starts at the beginning of
1142 the expression this level is working on
1143 and marches forward over the arguments.
1144 outbeg does similarly in the output. */
1145 for (i = 0; i < args; i++)
1149 prefixify_subexp (inexpr, outexpr, inend, outbeg);
1154 /* This page contains the two entry points to this file. */
1156 /* Read a C expression from the string *STRINGPTR points to,
1157 parse it, and return a pointer to a struct expression that we malloc.
1158 Use block BLOCK as the lexical context for variable names;
1159 if BLOCK is zero, use the block of the selected stack frame.
1160 Meanwhile, advance *STRINGPTR to point after the expression,
1161 at the first nonwhite character that is not part of the expression
1162 (possibly a null character).
1164 If COMMA is nonzero, stop if a comma is reached. */
1167 parse_c_1 (stringptr, block, comma)
1169 struct block *block;
1171 struct cleanup *old_chain;
1173 lexptr = *stringptr;
1175 comma_terminates = comma;
1177 if (lexptr == 0 || *lexptr == 0)
1178 error_no_arg ("expression to compute");
1180 old_chain = make_cleanup (free_funcalls, 0);
1183 expression_context_block = block ? block : get_selected_block ();
1185 namecopy = (char *) alloca (strlen (lexptr) + 1);
1188 expout = (struct expression *) xmalloc (sizeof (struct expression)
1189 + expout_size * sizeof (union exp_element));
1190 make_cleanup (free_current_contents, &expout);
1193 discard_cleanups (old_chain);
1194 expout->nelts = expout_ptr;
1195 expout = (struct expression *)
1197 sizeof (struct expression)
1198 + expout_ptr * sizeof (union exp_element));
1199 prefixify_expression (expout);
1200 *stringptr = lexptr;
1204 /* Parse STRING as an expression, and complain if this fails
1205 to use up all of the contents of STRING. */
1208 parse_c_expression (string)
1211 register struct expression *exp;
1212 exp = parse_c_1 (&string, 0, 0);
1214 error ("Junk after end of expression.");