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;
101 %type <voidval> exp exp1 start variable
102 %type <tval> type typebase
103 %type <tvec> nonempty_typelist
106 %token <lval> INT CHAR
109 /* Both NAME and TYPENAME tokens represent symbols in the input,
110 and both convey their data as strings.
111 But a TYPENAME is a string that happens to be defined as a typedef
112 or builtin type name (such as int or char)
113 and a NAME is any other symbol.
115 Contexts where this distinction is not important can use the
116 nonterminal "name", which matches either NAME or TYPENAME. */
118 %token <sval> NAME TYPENAME STRING
121 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
123 %token <lval> LAST REGNAME
125 %token <ivar> VARIABLE
127 %token <opcode> ASSIGN_MODIFY
134 %right '=' ASSIGN_MODIFY
141 %left '<' '>' LEQ GEQ
146 %right UNARY INCREMENT DECREMENT
155 /* Expressions, including the comma operator. */
158 { write_exp_elt (BINOP_COMMA); }
161 /* Expressions, not including the comma operator. */
162 exp : '*' exp %prec UNARY
163 { write_exp_elt (UNOP_IND); }
165 exp : '&' exp %prec UNARY
166 { write_exp_elt (UNOP_ADDR); }
168 exp : '-' exp %prec UNARY
169 { write_exp_elt (UNOP_NEG); }
172 exp : '!' exp %prec UNARY
173 { write_exp_elt (UNOP_ZEROP); }
176 exp : '~' exp %prec UNARY
177 { write_exp_elt (UNOP_LOGNOT); }
180 exp : INCREMENT exp %prec UNARY
181 { write_exp_elt (UNOP_PREINCREMENT); }
184 exp : DECREMENT exp %prec UNARY
185 { write_exp_elt (UNOP_PREDECREMENT); }
188 exp : exp INCREMENT %prec UNARY
189 { write_exp_elt (UNOP_POSTINCREMENT); }
192 exp : exp DECREMENT %prec UNARY
193 { write_exp_elt (UNOP_POSTDECREMENT); }
196 exp : SIZEOF exp %prec UNARY
197 { write_exp_elt (UNOP_SIZEOF); }
201 { write_exp_elt (STRUCTOP_PTR);
202 write_exp_string ($3);
203 write_exp_elt (STRUCTOP_PTR); }
206 exp : exp ARROW '*' exp
207 { write_exp_elt (STRUCTOP_MPTR); }
211 { write_exp_elt (STRUCTOP_STRUCT);
212 write_exp_string ($3);
213 write_exp_elt (STRUCTOP_STRUCT); }
216 exp : exp '.' '*' exp
217 { write_exp_elt (STRUCTOP_MEMBER); }
220 exp : exp '[' exp1 ']'
221 { write_exp_elt (BINOP_SUBSCRIPT); }
225 /* This is to save the value of arglist_len
226 being accumulated by an outer function call. */
227 { start_arglist (); }
229 { write_exp_elt (OP_FUNCALL);
230 write_exp_elt (end_arglist ());
231 write_exp_elt (OP_FUNCALL); }
241 arglist : arglist ',' exp %prec ABOVE_COMMA
245 exp : '{' type '}' exp %prec UNARY
246 { write_exp_elt (UNOP_MEMVAL);
248 write_exp_elt (UNOP_MEMVAL); }
251 exp : '(' type ')' exp %prec UNARY
252 { write_exp_elt (UNOP_CAST);
254 write_exp_elt (UNOP_CAST); }
261 /* Binary operators in order of decreasing precedence. */
264 { write_exp_elt (BINOP_REPEAT); }
268 { write_exp_elt (BINOP_MUL); }
272 { write_exp_elt (BINOP_DIV); }
276 { write_exp_elt (BINOP_REM); }
280 { write_exp_elt (BINOP_ADD); }
284 { write_exp_elt (BINOP_SUB); }
288 { write_exp_elt (BINOP_LSH); }
292 { write_exp_elt (BINOP_RSH); }
296 { write_exp_elt (BINOP_EQUAL); }
299 exp : exp NOTEQUAL exp
300 { write_exp_elt (BINOP_NOTEQUAL); }
304 { write_exp_elt (BINOP_LEQ); }
308 { write_exp_elt (BINOP_GEQ); }
312 { write_exp_elt (BINOP_LESS); }
316 { write_exp_elt (BINOP_GTR); }
320 { write_exp_elt (BINOP_LOGAND); }
324 { write_exp_elt (BINOP_LOGXOR); }
328 { write_exp_elt (BINOP_LOGIOR); }
332 { write_exp_elt (BINOP_AND); }
336 { write_exp_elt (BINOP_OR); }
339 exp : exp '?' exp ':' exp
340 { write_exp_elt (TERNOP_COND); }
344 { write_exp_elt (BINOP_ASSIGN); }
347 exp : exp ASSIGN_MODIFY exp
348 { write_exp_elt (BINOP_ASSIGN_MODIFY);
350 write_exp_elt (BINOP_ASSIGN_MODIFY); }
354 { write_exp_elt (OP_LONG);
355 write_exp_elt (builtin_type_long);
357 write_exp_elt (OP_LONG); }
361 { write_exp_elt (OP_LONG);
362 write_exp_elt (builtin_type_char);
364 write_exp_elt (OP_LONG); }
368 { write_exp_elt (OP_DOUBLE);
369 write_exp_elt (builtin_type_double);
371 write_exp_elt (OP_DOUBLE); }
378 { write_exp_elt (OP_LAST);
380 write_exp_elt (OP_LAST); }
384 { write_exp_elt (OP_REGISTER);
386 write_exp_elt (OP_REGISTER); }
390 { write_exp_elt (OP_INTERNALVAR);
392 write_exp_elt (OP_INTERNALVAR); }
395 exp : SIZEOF '(' type ')'
396 { write_exp_elt (OP_LONG);
397 write_exp_elt (builtin_type_int);
398 write_exp_elt ((long) TYPE_LENGTH ($3));
399 write_exp_elt (OP_LONG); }
403 { write_exp_elt (OP_STRING);
404 write_exp_string ($1);
405 write_exp_elt (OP_STRING); }
410 { write_exp_elt (OP_THIS);
411 write_exp_elt (OP_THIS); }
418 struct symtab *tem = lookup_symtab (copy_name ($1));
422 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), 1);
425 sym = lookup_symbol (copy_name ($1),
426 expression_context_block,
428 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
429 $$ = SYMBOL_BLOCK_VALUE (sym);
431 error ("No file or function \"%s\".",
437 block : block COLONCOLON name
440 = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE);
441 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
442 error ("No function \"%s\" in specified context.",
444 $$ = SYMBOL_BLOCK_VALUE (tem);
448 variable: block COLONCOLON name
451 sym = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE);
453 error ("No symbol \"%s\" in specified context.",
455 write_exp_elt (OP_VAR_VALUE);
457 write_exp_elt (OP_VAR_VALUE);
461 variable: typebase COLONCOLON name
463 struct type *type = $1;
464 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
465 && TYPE_CODE (type) != TYPE_CODE_UNION)
466 error ("`%s' is not defined as an aggregate type.",
469 write_exp_elt (OP_SCOPE);
470 write_exp_elt (type);
471 write_exp_string ($3);
472 write_exp_elt (OP_SCOPE);
476 char *name = copy_name ($2);
480 sym = lookup_symbol_2 (name, 0, VAR_NAMESPACE);
483 write_exp_elt (OP_VAR_VALUE);
485 write_exp_elt (OP_VAR_VALUE);
488 for (i = 0; i < misc_function_count; i++)
489 if (!strcmp (misc_function_vector[i].name, name))
492 if (i < misc_function_count)
494 write_exp_elt (OP_LONG);
495 write_exp_elt (builtin_type_int);
496 write_exp_elt (misc_function_vector[i].address);
497 write_exp_elt (OP_LONG);
498 write_exp_elt (UNOP_MEMVAL);
499 write_exp_elt (builtin_type_char);
500 write_exp_elt (UNOP_MEMVAL);
503 if (symtab_list == 0)
504 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
506 error ("No symbol \"%s\" in current context.", name);
511 { struct symbol *sym;
512 sym = lookup_symbol_1 (copy_name ($1),
513 expression_context_block,
517 write_exp_elt (OP_VAR_VALUE);
519 write_exp_elt (OP_VAR_VALUE);
523 register char *arg = copy_name ($1);
526 /* C++: see if it hangs off of `this'. Must
527 not inadvertently convert from a method call
529 v = (int)value_of_this (0);
532 val = check_field (v, arg);
535 write_exp_elt (OP_THIS);
536 write_exp_elt (OP_THIS);
537 write_exp_elt (STRUCTOP_PTR);
538 write_exp_string ($1);
539 write_exp_elt (STRUCTOP_PTR);
543 sym = lookup_symbol_2 (arg, 0, VAR_NAMESPACE);
546 write_exp_elt (OP_VAR_VALUE);
548 write_exp_elt (OP_VAR_VALUE);
549 break; /* YACC-dependent */
551 for (i = 0; i < misc_function_count; i++)
552 if (!strcmp (misc_function_vector[i].name, arg))
555 if (i < misc_function_count)
557 write_exp_elt (OP_LONG);
558 write_exp_elt (builtin_type_int);
559 write_exp_elt (misc_function_vector[i].address);
560 write_exp_elt (OP_LONG);
561 write_exp_elt (UNOP_MEMVAL);
562 write_exp_elt (builtin_type_char);
563 write_exp_elt (UNOP_MEMVAL);
566 if (symtab_list == 0)
567 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
569 error ("No symbol \"%s\" in current context.",
577 { $$ = lookup_pointer_type ($1); }
579 { $$ = lookup_reference_type ($1); }
580 | typebase COLONCOLON '*'
581 { $$ = lookup_member_type (builtin_type_int, $1); }
582 | type '(' typebase COLONCOLON '*' ')'
583 { $$ = lookup_member_type ($1, $3); }
584 | type '(' typebase COLONCOLON '*' ')' '(' ')'
585 { $$ = lookup_member_type (lookup_function_type ($1, 0), $3); }
586 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
587 { $$ = lookup_member_type (lookup_function_type ($1, $8), $3);
593 { $$ = lookup_typename (copy_name ($1),
594 expression_context_block, 0); }
596 { $$ = lookup_struct (copy_name ($2),
597 expression_context_block); }
599 { $$ = lookup_union (copy_name ($2),
600 expression_context_block); }
602 { $$ = lookup_enum (copy_name ($2),
603 expression_context_block); }
605 { $$ = lookup_unsigned_typename (copy_name ($2)); }
610 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
611 $$[0] = (struct type *)0;
614 | nonempty_typelist ',' type
615 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
616 $$ = (struct type **)xrealloc ($1, len);
617 $$[$<ivec>$[0]] = $3;
626 /* Begin counting arguments for a function call,
627 saving the data about any containing call. */
632 register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
634 new->next = funcall_chain;
635 new->arglist_len = arglist_len;
640 /* Return the number of arguments in a function call just terminated,
641 and restore the data for the containing function call. */
646 register int val = arglist_len;
647 register struct funcall *call = funcall_chain;
648 funcall_chain = call->next;
649 arglist_len = call->arglist_len;
654 /* Free everything in the funcall chain.
655 Used when there is an error inside parsing. */
660 register struct funcall *call, *next;
662 for (call = funcall_chain; call; call = next)
669 /* This page contains the functions for adding data to the struct expression
670 being constructed. */
672 /* Add one element to the end of the expression. */
675 write_exp_elt (expelt)
676 union exp_element expelt;
678 if (expout_ptr >= expout_size)
681 expout = (struct expression *) xrealloc (expout,
682 sizeof (struct expression)
683 + expout_size * sizeof (union exp_element));
685 expout->elts[expout_ptr++] = expelt;
688 /* Add a string constant to the end of the expression.
689 Follow it by its length in bytes, as a separate exp_element. */
692 write_exp_string (str)
695 register int len = str.length;
697 = (len + sizeof (union exp_element)) / sizeof (union exp_element);
699 expout_ptr += lenelt;
701 if (expout_ptr >= expout_size)
703 expout_size = max (expout_size * 2, expout_ptr + 10);
704 expout = (struct expression *) xrealloc (expout,
705 sizeof (struct expression)
706 + expout_size * sizeof (union exp_element));
708 bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len);
709 ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0;
713 /* During parsing of a C expression, the pointer to the next character
714 is in this variable. */
718 /* Tokens that refer to names do so with explicit pointer and length,
719 so they can share the storage that lexptr is parsing.
721 When it is necessary to pass a name to a function that expects
722 a null-terminated string, the substring is copied out
723 into a block of storage that namecopy points to.
725 namecopy is allocated once, guaranteed big enough, for each parsing. */
727 static char *namecopy;
729 /* Current depth in parentheses within the expression. */
731 static int paren_depth;
733 /* Nonzero means stop parsing on first comma (if not within parentheses). */
735 static int comma_terminates;
737 /* Take care of parsing a number (anything that starts with a digit).
738 Set yylval and return the token type; update lexptr.
739 LEN is the number of characters in it. */
741 /*** Needs some error checking for the float case ***/
747 register char *p = lexptr;
750 register int base = 10;
751 register int len = olen;
754 extern double atof ();
756 for (c = 0; c < len; c++)
759 /* It's a float since it contains a point. */
760 yylval.dval = atof (p);
765 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2)))
778 if (c >= '0' && c <= '9')
782 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
783 if (base == 16 && c >= 'a' && c <= 'f')
785 else if (len == 0 && c == 'l')
789 err_copy = (char *) alloca (olen + 1);
790 bcopy (lexptr, err_copy, olen);
792 error ("Invalid number \"%s\".", err_copy);
806 enum exp_opcode opcode;
809 static struct token tokentab3[] =
811 {">>=", ASSIGN_MODIFY, BINOP_RSH},
812 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
815 static struct token tokentab2[] =
817 {"+=", ASSIGN_MODIFY, BINOP_ADD},
818 {"-=", ASSIGN_MODIFY, BINOP_SUB},
819 {"*=", ASSIGN_MODIFY, BINOP_MUL},
820 {"/=", ASSIGN_MODIFY, BINOP_DIV},
821 {"%=", ASSIGN_MODIFY, BINOP_REM},
822 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
823 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
824 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
825 {"++", INCREMENT, BINOP_END},
826 {"--", DECREMENT, BINOP_END},
827 {"->", ARROW, BINOP_END},
828 {"&&", AND, BINOP_END},
829 {"||", OR, BINOP_END},
830 {"::", COLONCOLON, BINOP_END},
831 {"<<", LSH, BINOP_END},
832 {">>", RSH, BINOP_END},
833 {"==", EQUAL, BINOP_END},
834 {"!=", NOTEQUAL, BINOP_END},
835 {"<=", LEQ, BINOP_END},
836 {">=", GEQ, BINOP_END}
839 /* Read one token, getting characters through lexptr. */
845 register int namelen;
847 register char *tokstart;
852 /* See if it is a special token of length 3. */
853 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
854 if (!strncmp (tokstart, tokentab3[i].operator, 3))
857 yylval.opcode = tokentab3[i].opcode;
858 return tokentab3[i].token;
861 /* See if it is a special token of length 2. */
862 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
863 if (!strncmp (tokstart, tokentab2[i].operator, 2))
866 yylval.opcode = tokentab2[i].opcode;
867 return tokentab2[i].token;
870 switch (c = *tokstart)
885 c = parse_escape (&lexptr);
889 error ("Invalid character constant.");
898 if (paren_depth == 0)
905 if (comma_terminates && paren_depth == 0)
935 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
938 c = tokstart[++namelen];
939 if (c >= '0' && c <= '9')
941 c = tokstart[++namelen];
942 if (c >= '0' && c <= '9')
943 c = tokstart[++namelen];
946 yylval.sval.ptr = tokstart + 1;
947 yylval.sval.length = namelen - 1;
948 lexptr += namelen + 1;
951 if (c >= '0' && c <= '9')
955 c = tokstart[namelen],
956 (c == '_' || c == '$' || c == '.' || (c >= '0' && c <= '9')
957 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
960 return parse_number (namelen);
963 if (!(c == '_' || c == '$'
964 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
965 error ("Invalid token in expression.");
967 /* It is a name. See how long it is. */
970 c = tokstart[namelen],
971 (c == '_' || c == '$' || (c >= '0' && c <= '9')
972 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
976 /* The token "if" terminates the expression and is NOT
977 removed from the input stream. */
978 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
985 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
986 and $$digits (equivalent to $<-digits> if you could type that).
987 Make token type LAST, and put the number (the digits) in yylval. */
989 if (*tokstart == '$')
991 register int negate = 0;
993 /* Double dollar means negate the number and add -1 as well.
994 Thus $$ alone means -1. */
995 if (namelen >= 2 && tokstart[1] == '$')
1002 /* Just dollars (one or two) */
1003 yylval.lval = - negate;
1006 /* Is the rest of the token digits? */
1007 for (; c < namelen; c++)
1008 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1012 yylval.lval = atoi (tokstart + 1 + negate);
1014 yylval.lval = - yylval.lval;
1019 /* Handle tokens that refer to machine registers:
1020 $ followed by a register name. */
1022 if (*tokstart == '$')
1023 for (c = 0; c < NUM_REGS; c++)
1024 if (namelen - 1 == strlen (reg_names[c])
1025 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1031 if (namelen == 6 && !strncmp (tokstart, "struct", 6))
1037 if (!strncmp (tokstart, "union", 5))
1044 if (!strncmp (tokstart, "enum", 4))
1048 if (!strncmp (tokstart, "this", 4))
1053 if (namelen == 6 && !strncmp (tokstart, "sizeof", 6))
1057 if (namelen == 8 && !strncmp (tokstart, "unsigned", 6))
1061 yylval.sval.ptr = tokstart;
1062 yylval.sval.length = namelen;
1064 /* Any other names starting in $ are debugger internal variables. */
1066 if (*tokstart == '$')
1068 yylval.ivar = (struct internalvar *) lookup_internalvar (copy_name (yylval.sval) + 1);
1072 /* Use token-type TYPENAME for symbols that happen to be defined
1073 currently as names of types; NAME for other symbols.
1074 The caller is not constrained to care about the distinction. */
1075 if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
1083 error ("Invalid syntax in expression.");
1086 /* Return a null-terminated temporary copy of the name
1087 of a string token. */
1091 struct stoken token;
1093 bcopy (token.ptr, namecopy, token.length);
1094 namecopy[token.length] = 0;
1098 /* Reverse an expression from suffix form (in which it is constructed)
1099 to prefix form (in which we can conveniently print or execute it). */
1101 static void prefixify_subexp ();
1104 prefixify_expression (expr)
1105 register struct expression *expr;
1107 register int len = sizeof (struct expression) +
1108 expr->nelts * sizeof (union exp_element);
1109 register struct expression *temp;
1110 register int inpos = expr->nelts, outpos = 0;
1112 temp = (struct expression *) alloca (len);
1114 /* Copy the original expression into temp. */
1115 bcopy (expr, temp, len);
1117 prefixify_subexp (temp, expr, inpos, outpos);
1120 /* Return the number of exp_elements in the subexpression of EXPR
1121 whose last exp_element is at index ENDPOS - 1 in EXPR. */
1124 length_of_subexp (expr, endpos)
1125 register struct expression *expr;
1126 register int endpos;
1128 register int oplen = 1;
1129 register int args = 0;
1133 error ("?error in length_of_subexp");
1135 i = (int) expr->elts[endpos - 1].opcode;
1141 oplen = 4 + ((expr->elts[endpos - 2].longconst
1142 + sizeof (union exp_element))
1143 / sizeof (union exp_element));
1154 case OP_INTERNALVAR:
1160 args = 1 + expr->elts[endpos - 2].longconst;
1169 case STRUCTOP_STRUCT:
1173 oplen = 3 + ((expr->elts[endpos - 2].longconst
1174 + sizeof (union exp_element))
1175 / sizeof (union exp_element));
1182 case BINOP_ASSIGN_MODIFY:
1193 args = 1 + (i < (int) BINOP_END);
1198 oplen += length_of_subexp (expr, endpos - oplen);
1205 /* Copy the subexpression ending just before index INEND in INEXPR
1206 into OUTEXPR, starting at index OUTBEG.
1207 In the process, convert it from suffix to prefix form. */
1210 prefixify_subexp (inexpr, outexpr, inend, outbeg)
1211 register struct expression *inexpr;
1212 struct expression *outexpr;
1216 register int oplen = 1;
1217 register int args = 0;
1220 enum exp_opcode opcode;
1222 /* Compute how long the last operation is (in OPLEN),
1223 and also how many preceding subexpressions serve as
1224 arguments for it (in ARGS). */
1226 opcode = inexpr->elts[inend - 1].opcode;
1231 oplen = 4 + ((inexpr->elts[inend - 2].longconst
1232 + sizeof (union exp_element))
1233 / sizeof (union exp_element));
1244 case OP_INTERNALVAR:
1250 args = 1 + inexpr->elts[inend - 2].longconst;
1259 case STRUCTOP_STRUCT:
1263 oplen = 3 + ((inexpr->elts[inend - 2].longconst
1264 + sizeof (union exp_element))
1265 / sizeof (union exp_element));
1273 case BINOP_ASSIGN_MODIFY:
1284 args = 1 + ((int) opcode < (int) BINOP_END);
1287 /* Copy the final operator itself, from the end of the input
1288 to the beginning of the output. */
1290 bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg],
1291 oplen * sizeof (union exp_element));
1294 /* Find the lengths of the arg subexpressions. */
1295 arglens = (int *) alloca (args * sizeof (int));
1296 for (i = args - 1; i >= 0; i--)
1298 oplen = length_of_subexp (inexpr, inend);
1303 /* Now copy each subexpression, preserving the order of
1304 the subexpressions, but prefixifying each one.
1305 In this loop, inend starts at the beginning of
1306 the expression this level is working on
1307 and marches forward over the arguments.
1308 outbeg does similarly in the output. */
1309 for (i = 0; i < args; i++)
1313 prefixify_subexp (inexpr, outexpr, inend, outbeg);
1318 /* This page contains the two entry points to this file. */
1320 /* Read a C expression from the string *STRINGPTR points to,
1321 parse it, and return a pointer to a struct expression that we malloc.
1322 Use block BLOCK as the lexical context for variable names;
1323 if BLOCK is zero, use the block of the selected stack frame.
1324 Meanwhile, advance *STRINGPTR to point after the expression,
1325 at the first nonwhite character that is not part of the expression
1326 (possibly a null character).
1328 If COMMA is nonzero, stop if a comma is reached. */
1331 parse_c_1 (stringptr, block, comma)
1333 struct block *block;
1335 struct cleanup *old_chain;
1337 lexptr = *stringptr;
1339 comma_terminates = comma;
1341 if (lexptr == 0 || *lexptr == 0)
1342 error_no_arg ("expression to compute");
1344 old_chain = make_cleanup (free_funcalls, 0);
1347 expression_context_block = block ? block : get_selected_block ();
1349 namecopy = (char *) alloca (strlen (lexptr) + 1);
1352 expout = (struct expression *) xmalloc (sizeof (struct expression)
1353 + expout_size * sizeof (union exp_element));
1354 make_cleanup (free_current_contents, &expout);
1357 discard_cleanups (old_chain);
1358 expout->nelts = expout_ptr;
1359 expout = (struct expression *)
1361 sizeof (struct expression)
1362 + expout_ptr * sizeof (union exp_element));
1363 prefixify_expression (expout);
1364 *stringptr = lexptr;
1368 /* Parse STRING as an expression, and complain if this fails
1369 to use up all of the contents of STRING. */
1372 parse_c_expression (string)
1375 register struct expression *exp;
1376 exp = parse_c_1 (&string, 0, 0);
1378 error ("Junk after end of expression.");