Commit | Line | Data |
---|---|---|
bd5635a1 | 1 | /* Evaluate expressions for GDB. |
e17960fb | 2 | Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc. |
bd5635a1 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
2ccb3837 | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
2ccb3837 JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
2ccb3837 | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
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. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
2ccb3837 JG |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 RP |
19 | |
20 | #include "defs.h" | |
bd5635a1 | 21 | #include "symtab.h" |
01be6913 | 22 | #include "gdbtypes.h" |
bd5635a1 RP |
23 | #include "value.h" |
24 | #include "expression.h" | |
25 | #include "target.h" | |
2ccb3837 | 26 | #include "frame.h" |
fb6e675f | 27 | #include "language.h" /* For CAST_IS_CONVERSION */ |
bd5635a1 | 28 | |
01be6913 PB |
29 | /* Values of NOSIDE argument to eval_subexp. */ |
30 | enum noside | |
31 | { EVAL_NORMAL, | |
32 | EVAL_SKIP, /* Only effect is to increment pos. */ | |
33 | EVAL_AVOID_SIDE_EFFECTS /* Don't modify any variables or | |
34 | call any functions. The value | |
35 | returned will have the correct | |
36 | type, and will have an | |
37 | approximately correct lvalue | |
38 | type (inaccuracy: anything that is | |
39 | listed as being in a register in | |
40 | the function in which it was | |
41 | declared will be lval_register). */ | |
42 | }; | |
43 | ||
44 | /* Prototypes for local functions. */ | |
45 | ||
46 | static value | |
47 | evaluate_subexp_for_sizeof PARAMS ((struct expression *, int *)); | |
48 | ||
49 | static value | |
50 | evaluate_subexp_with_coercion PARAMS ((struct expression *, int *, | |
51 | enum noside)); | |
52 | ||
53 | static value | |
54 | evaluate_subexp_for_address PARAMS ((struct expression *, int *, | |
55 | enum noside)); | |
56 | ||
57 | static value | |
58 | evaluate_subexp PARAMS ((struct type *, struct expression *, int *, | |
59 | enum noside)); | |
bd5635a1 RP |
60 | |
61 | \f | |
62 | /* Parse the string EXP as a C expression, evaluate it, | |
63 | and return the result as a number. */ | |
64 | ||
65 | CORE_ADDR | |
66 | parse_and_eval_address (exp) | |
67 | char *exp; | |
68 | { | |
2ccb3837 | 69 | struct expression *expr = parse_expression (exp); |
bd5635a1 | 70 | register CORE_ADDR addr; |
01be6913 PB |
71 | register struct cleanup *old_chain = |
72 | make_cleanup (free_current_contents, &expr); | |
bd5635a1 | 73 | |
2ccb3837 | 74 | addr = value_as_pointer (evaluate_expression (expr)); |
bd5635a1 RP |
75 | do_cleanups (old_chain); |
76 | return addr; | |
77 | } | |
78 | ||
79 | /* Like parse_and_eval_address but takes a pointer to a char * variable | |
80 | and advanced that variable across the characters parsed. */ | |
81 | ||
82 | CORE_ADDR | |
83 | parse_and_eval_address_1 (expptr) | |
84 | char **expptr; | |
85 | { | |
2ccb3837 | 86 | struct expression *expr = parse_exp_1 (expptr, (struct block *)0, 0); |
bd5635a1 | 87 | register CORE_ADDR addr; |
01be6913 PB |
88 | register struct cleanup *old_chain = |
89 | make_cleanup (free_current_contents, &expr); | |
bd5635a1 | 90 | |
2ccb3837 | 91 | addr = value_as_pointer (evaluate_expression (expr)); |
bd5635a1 RP |
92 | do_cleanups (old_chain); |
93 | return addr; | |
94 | } | |
95 | ||
96 | value | |
97 | parse_and_eval (exp) | |
98 | char *exp; | |
99 | { | |
2ccb3837 | 100 | struct expression *expr = parse_expression (exp); |
bd5635a1 RP |
101 | register value val; |
102 | register struct cleanup *old_chain | |
103 | = make_cleanup (free_current_contents, &expr); | |
104 | ||
105 | val = evaluate_expression (expr); | |
106 | do_cleanups (old_chain); | |
107 | return val; | |
108 | } | |
109 | ||
110 | /* Parse up to a comma (or to a closeparen) | |
111 | in the string EXPP as an expression, evaluate it, and return the value. | |
112 | EXPP is advanced to point to the comma. */ | |
113 | ||
114 | value | |
115 | parse_to_comma_and_eval (expp) | |
116 | char **expp; | |
117 | { | |
2ccb3837 | 118 | struct expression *expr = parse_exp_1 (expp, (struct block *) 0, 1); |
bd5635a1 RP |
119 | register value val; |
120 | register struct cleanup *old_chain | |
121 | = make_cleanup (free_current_contents, &expr); | |
122 | ||
123 | val = evaluate_expression (expr); | |
124 | do_cleanups (old_chain); | |
125 | return val; | |
126 | } | |
127 | \f | |
128 | /* Evaluate an expression in internal prefix form | |
0a5d35ed | 129 | such as is constructed by parse.y. |
bd5635a1 RP |
130 | |
131 | See expression.h for info on the format of an expression. */ | |
132 | ||
133 | static value evaluate_subexp (); | |
134 | static value evaluate_subexp_for_address (); | |
135 | static value evaluate_subexp_for_sizeof (); | |
136 | static value evaluate_subexp_with_coercion (); | |
137 | ||
bd5635a1 RP |
138 | value |
139 | evaluate_expression (exp) | |
140 | struct expression *exp; | |
141 | { | |
142 | int pc = 0; | |
143 | return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_NORMAL); | |
144 | } | |
145 | ||
146 | /* Evaluate an expression, avoiding all memory references | |
147 | and getting a value whose type alone is correct. */ | |
148 | ||
149 | value | |
150 | evaluate_type (exp) | |
151 | struct expression *exp; | |
152 | { | |
153 | int pc = 0; | |
154 | return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS); | |
155 | } | |
156 | ||
157 | static value | |
158 | evaluate_subexp (expect_type, exp, pos, noside) | |
159 | struct type *expect_type; | |
160 | register struct expression *exp; | |
161 | register int *pos; | |
162 | enum noside noside; | |
163 | { | |
164 | enum exp_opcode op; | |
1500864f | 165 | int tem, tem2, tem3; |
bd5635a1 RP |
166 | register int pc, pc2, oldpos; |
167 | register value arg1, arg2, arg3; | |
01be6913 | 168 | struct type *type; |
bd5635a1 RP |
169 | int nargs; |
170 | value *argvec; | |
171 | ||
172 | pc = (*pos)++; | |
173 | op = exp->elts[pc].opcode; | |
174 | ||
175 | switch (op) | |
176 | { | |
177 | case OP_SCOPE: | |
a8a69e63 | 178 | tem = longest_to_int (exp->elts[pc + 2].longconst); |
1500864f | 179 | (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1); |
01be6913 | 180 | arg1 = value_struct_elt_for_reference (exp->elts[pc + 1].type, |
8f86a4e4 | 181 | 0, |
01be6913 | 182 | exp->elts[pc + 1].type, |
a8a69e63 | 183 | &exp->elts[pc + 3].string, |
01be6913 | 184 | expect_type); |
5f00ca54 | 185 | if (arg1 == NULL) |
a8a69e63 | 186 | error ("There is no field named %s", &exp->elts[pc + 3].string); |
5f00ca54 | 187 | return arg1; |
bd5635a1 RP |
188 | |
189 | case OP_LONG: | |
190 | (*pos) += 3; | |
2ccb3837 | 191 | return value_from_longest (exp->elts[pc + 1].type, |
a8a69e63 | 192 | exp->elts[pc + 2].longconst); |
bd5635a1 RP |
193 | |
194 | case OP_DOUBLE: | |
195 | (*pos) += 3; | |
196 | return value_from_double (exp->elts[pc + 1].type, | |
197 | exp->elts[pc + 2].doubleconst); | |
198 | ||
199 | case OP_VAR_VALUE: | |
200 | (*pos) += 2; | |
201 | if (noside == EVAL_SKIP) | |
202 | goto nosideret; | |
203 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
204 | { | |
205 | struct symbol * sym = exp->elts[pc + 1].symbol; | |
206 | enum lval_type lv; | |
207 | ||
208 | switch (SYMBOL_CLASS (sym)) | |
209 | { | |
210 | case LOC_CONST: | |
211 | case LOC_LABEL: | |
212 | case LOC_CONST_BYTES: | |
213 | lv = not_lval; | |
214 | break; | |
215 | ||
216 | case LOC_REGISTER: | |
217 | case LOC_REGPARM: | |
218 | lv = lval_register; | |
219 | break; | |
220 | ||
221 | default: | |
222 | lv = lval_memory; | |
223 | break; | |
224 | } | |
225 | ||
226 | return value_zero (SYMBOL_TYPE (sym), lv); | |
227 | } | |
228 | else | |
229 | return value_of_variable (exp->elts[pc + 1].symbol); | |
230 | ||
231 | case OP_LAST: | |
232 | (*pos) += 2; | |
2ccb3837 JG |
233 | return |
234 | access_value_history (longest_to_int (exp->elts[pc + 1].longconst)); | |
bd5635a1 RP |
235 | |
236 | case OP_REGISTER: | |
237 | (*pos) += 2; | |
2ccb3837 | 238 | return value_of_register (longest_to_int (exp->elts[pc + 1].longconst)); |
bd5635a1 | 239 | |
e58de8a2 FF |
240 | case OP_BOOL: |
241 | (*pos) += 2; | |
242 | return value_from_longest (builtin_type_chill_bool, | |
243 | exp->elts[pc + 1].longconst); | |
244 | ||
bd5635a1 RP |
245 | case OP_INTERNALVAR: |
246 | (*pos) += 2; | |
247 | return value_of_internalvar (exp->elts[pc + 1].internalvar); | |
248 | ||
249 | case OP_STRING: | |
a8a69e63 | 250 | tem = longest_to_int (exp->elts[pc + 1].longconst); |
1500864f | 251 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); |
bd5635a1 RP |
252 | if (noside == EVAL_SKIP) |
253 | goto nosideret; | |
a8a69e63 | 254 | return value_string (&exp->elts[pc + 2].string, tem); |
bd5635a1 | 255 | |
1500864f JK |
256 | case OP_BITSTRING: |
257 | error ("support for OP_BITSTRING unimplemented"); | |
258 | break; | |
259 | ||
260 | case OP_ARRAY: | |
261 | (*pos) += 3; | |
262 | tem2 = longest_to_int (exp->elts[pc + 1].longconst); | |
263 | tem3 = longest_to_int (exp->elts[pc + 2].longconst); | |
264 | nargs = tem3 - tem2 + 1; | |
265 | argvec = (value *) alloca (sizeof (value) * nargs); | |
266 | for (tem = 0; tem < nargs; tem++) | |
267 | { | |
268 | /* Ensure that array expressions are coerced into pointer objects. */ | |
269 | argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside); | |
270 | } | |
271 | if (noside == EVAL_SKIP) | |
272 | goto nosideret; | |
273 | return (value_array (tem2, tem3, argvec)); | |
274 | break; | |
275 | ||
bd5635a1 RP |
276 | case TERNOP_COND: |
277 | /* Skip third and second args to evaluate the first one. */ | |
278 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
e58de8a2 | 279 | if (value_logical_not (arg1)) |
bd5635a1 RP |
280 | { |
281 | evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP); | |
282 | return evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
283 | } | |
284 | else | |
285 | { | |
286 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
287 | evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP); | |
288 | return arg2; | |
289 | } | |
290 | ||
291 | case OP_FUNCALL: | |
292 | (*pos) += 2; | |
293 | op = exp->elts[*pos].opcode; | |
294 | if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR) | |
295 | { | |
296 | int fnptr; | |
297 | ||
2ccb3837 | 298 | nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1; |
bd5635a1 RP |
299 | /* First, evaluate the structure into arg2 */ |
300 | pc2 = (*pos)++; | |
301 | ||
302 | if (noside == EVAL_SKIP) | |
303 | goto nosideret; | |
304 | ||
305 | if (op == STRUCTOP_MEMBER) | |
306 | { | |
307 | arg2 = evaluate_subexp_for_address (exp, pos, noside); | |
308 | } | |
309 | else | |
310 | { | |
311 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
312 | } | |
313 | ||
314 | /* If the function is a virtual function, then the | |
315 | aggregate value (providing the structure) plays | |
316 | its part by providing the vtable. Otherwise, | |
317 | it is just along for the ride: call the function | |
318 | directly. */ | |
319 | ||
320 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
321 | ||
2ccb3837 | 322 | fnptr = longest_to_int (value_as_long (arg1)); |
35fcebce PB |
323 | |
324 | if (METHOD_PTR_IS_VIRTUAL(fnptr)) | |
bd5635a1 | 325 | { |
35fcebce | 326 | int fnoffset = METHOD_PTR_TO_VOFFSET(fnptr); |
bd5635a1 | 327 | struct type *basetype; |
35fcebce PB |
328 | struct type *domain_type = |
329 | TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))); | |
bd5635a1 RP |
330 | int i, j; |
331 | basetype = TYPE_TARGET_TYPE (VALUE_TYPE (arg2)); | |
35fcebce PB |
332 | if (domain_type != basetype) |
333 | arg2 = value_cast(lookup_pointer_type (domain_type), arg2); | |
334 | basetype = TYPE_VPTR_BASETYPE (domain_type); | |
bd5635a1 RP |
335 | for (i = TYPE_NFN_FIELDS (basetype) - 1; i >= 0; i--) |
336 | { | |
337 | struct fn_field *f = TYPE_FN_FIELDLIST1 (basetype, i); | |
338 | /* If one is virtual, then all are virtual. */ | |
339 | if (TYPE_FN_FIELD_VIRTUAL_P (f, 0)) | |
340 | for (j = TYPE_FN_FIELDLIST_LENGTH (basetype, i) - 1; j >= 0; --j) | |
35fcebce | 341 | if (TYPE_FN_FIELD_VOFFSET (f, j) == fnoffset) |
bd5635a1 | 342 | { |
35fcebce PB |
343 | value temp = value_ind (arg2); |
344 | arg1 = value_virtual_fn_field (&temp, f, j, domain_type, 0); | |
345 | arg2 = value_addr (temp); | |
bd5635a1 RP |
346 | goto got_it; |
347 | } | |
348 | } | |
349 | if (i < 0) | |
35fcebce | 350 | error ("virtual function at index %d not found", fnoffset); |
bd5635a1 RP |
351 | } |
352 | else | |
353 | { | |
354 | VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))); | |
355 | } | |
356 | got_it: | |
357 | ||
358 | /* Now, say which argument to start evaluating from */ | |
359 | tem = 2; | |
360 | } | |
361 | else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR) | |
362 | { | |
363 | /* Hair for method invocations */ | |
364 | int tem2; | |
365 | ||
2ccb3837 | 366 | nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1; |
bd5635a1 RP |
367 | /* First, evaluate the structure into arg2 */ |
368 | pc2 = (*pos)++; | |
a8a69e63 | 369 | tem2 = longest_to_int (exp->elts[pc2 + 1].longconst); |
1500864f | 370 | *pos += 3 + BYTES_TO_EXP_ELEM (tem2 + 1); |
bd5635a1 RP |
371 | if (noside == EVAL_SKIP) |
372 | goto nosideret; | |
373 | ||
374 | if (op == STRUCTOP_STRUCT) | |
375 | { | |
376 | arg2 = evaluate_subexp_for_address (exp, pos, noside); | |
377 | } | |
378 | else | |
379 | { | |
380 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
381 | } | |
382 | /* Now, say which argument to start evaluating from */ | |
383 | tem = 2; | |
384 | } | |
385 | else | |
386 | { | |
2ccb3837 | 387 | nargs = longest_to_int (exp->elts[pc + 1].longconst); |
bd5635a1 RP |
388 | tem = 0; |
389 | } | |
1500864f JK |
390 | /* Allocate arg vector, including space for the function to be |
391 | called in argvec[0] and a terminating NULL */ | |
bd5635a1 RP |
392 | argvec = (value *) alloca (sizeof (value) * (nargs + 2)); |
393 | for (; tem <= nargs; tem++) | |
394 | /* Ensure that array expressions are coerced into pointer objects. */ | |
395 | argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside); | |
396 | ||
397 | /* signal end of arglist */ | |
398 | argvec[tem] = 0; | |
399 | ||
400 | if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR) | |
401 | { | |
402 | int static_memfuncp; | |
403 | value temp = arg2; | |
404 | ||
405 | argvec[1] = arg2; | |
406 | argvec[0] = | |
a8a69e63 | 407 | value_struct_elt (&temp, argvec+1, &exp->elts[pc2 + 2].string, |
bd5635a1 RP |
408 | &static_memfuncp, |
409 | op == STRUCTOP_STRUCT | |
410 | ? "structure" : "structure pointer"); | |
411 | if (VALUE_OFFSET (temp)) | |
412 | { | |
2ccb3837 | 413 | arg2 = value_from_longest (lookup_pointer_type (VALUE_TYPE (temp)), |
1500864f | 414 | VALUE_ADDRESS (temp)+VALUE_OFFSET (temp)); |
bd5635a1 RP |
415 | argvec[1] = arg2; |
416 | } | |
417 | if (static_memfuncp) | |
418 | { | |
419 | argvec[1] = argvec[0]; | |
420 | nargs--; | |
421 | argvec++; | |
422 | } | |
423 | } | |
424 | else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR) | |
425 | { | |
426 | argvec[1] = arg2; | |
427 | argvec[0] = arg1; | |
428 | } | |
429 | ||
430 | if (noside == EVAL_SKIP) | |
431 | goto nosideret; | |
432 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
433 | { | |
434 | /* If the return type doesn't look like a function type, call an | |
435 | error. This can happen if somebody tries to turn a variable into | |
436 | a function call. This is here because people often want to | |
437 | call, eg, strcmp, which gdb doesn't know is a function. If | |
438 | gdb isn't asked for it's opinion (ie. through "whatis"), | |
439 | it won't offer it. */ | |
440 | ||
441 | struct type *ftype = | |
442 | TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0])); | |
443 | ||
444 | if (ftype) | |
445 | return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0]))); | |
446 | else | |
447 | error ("Expression of type other than \"Function returning ...\" used as function"); | |
448 | } | |
e17960fb | 449 | return call_function_by_hand (argvec[0], nargs, argvec + 1); |
bd5635a1 RP |
450 | |
451 | case STRUCTOP_STRUCT: | |
a8a69e63 | 452 | tem = longest_to_int (exp->elts[pc + 1].longconst); |
1500864f | 453 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); |
bd5635a1 RP |
454 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
455 | if (noside == EVAL_SKIP) | |
456 | goto nosideret; | |
457 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
458 | return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1), | |
a8a69e63 | 459 | &exp->elts[pc + 2].string, |
35fcebce | 460 | 0), |
bd5635a1 RP |
461 | lval_memory); |
462 | else | |
463 | { | |
464 | value temp = arg1; | |
a8a69e63 | 465 | return value_struct_elt (&temp, (value *)0, &exp->elts[pc + 2].string, |
bd5635a1 RP |
466 | (int *) 0, "structure"); |
467 | } | |
468 | ||
469 | case STRUCTOP_PTR: | |
a8a69e63 | 470 | tem = longest_to_int (exp->elts[pc + 1].longconst); |
1500864f | 471 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); |
bd5635a1 RP |
472 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
473 | if (noside == EVAL_SKIP) | |
474 | goto nosideret; | |
475 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1500864f | 476 | return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1), |
a8a69e63 | 477 | &exp->elts[pc + 2].string, |
35fcebce | 478 | 0), |
bd5635a1 RP |
479 | lval_memory); |
480 | else | |
481 | { | |
482 | value temp = arg1; | |
a8a69e63 | 483 | return value_struct_elt (&temp, (value *)0, &exp->elts[pc + 2].string, |
bd5635a1 RP |
484 | (int *) 0, "structure pointer"); |
485 | } | |
486 | ||
487 | case STRUCTOP_MEMBER: | |
488 | arg1 = evaluate_subexp_for_address (exp, pos, noside); | |
01be6913 | 489 | goto handle_pointer_to_member; |
bd5635a1 RP |
490 | case STRUCTOP_MPTR: |
491 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
01be6913 | 492 | handle_pointer_to_member: |
bd5635a1 RP |
493 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
494 | if (noside == EVAL_SKIP) | |
495 | goto nosideret; | |
01be6913 PB |
496 | if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_PTR) |
497 | goto bad_pointer_to_member; | |
498 | type = TYPE_TARGET_TYPE (VALUE_TYPE (arg2)); | |
499 | if (TYPE_CODE (type) == TYPE_CODE_METHOD) | |
500 | error ("not implemented: pointer-to-method in pointer-to-member construct"); | |
501 | if (TYPE_CODE (type) != TYPE_CODE_MEMBER) | |
502 | goto bad_pointer_to_member; | |
bd5635a1 | 503 | /* Now, convert these values to an address. */ |
01be6913 PB |
504 | arg1 = value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type)), |
505 | arg1); | |
506 | arg3 = value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type)), | |
507 | value_as_long (arg1) + value_as_long (arg2)); | |
bd5635a1 | 508 | return value_ind (arg3); |
01be6913 PB |
509 | bad_pointer_to_member: |
510 | error("non-pointer-to-member value used in pointer-to-member construct"); | |
bd5635a1 | 511 | |
1500864f JK |
512 | case BINOP_CONCAT: |
513 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
514 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
515 | if (noside == EVAL_SKIP) | |
516 | goto nosideret; | |
517 | if (binop_user_defined_p (op, arg1, arg2)) | |
518 | return value_x_binop (arg1, arg2, op, OP_NULL); | |
519 | else | |
520 | return value_concat (arg1, arg2); | |
521 | ||
bd5635a1 RP |
522 | case BINOP_ASSIGN: |
523 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
524 | arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside); | |
525 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
526 | return arg1; | |
527 | if (binop_user_defined_p (op, arg1, arg2)) | |
2ccb3837 | 528 | return value_x_binop (arg1, arg2, op, OP_NULL); |
bd5635a1 RP |
529 | else |
530 | return value_assign (arg1, arg2); | |
531 | ||
532 | case BINOP_ASSIGN_MODIFY: | |
533 | (*pos) += 2; | |
534 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
535 | arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside); | |
536 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
537 | return arg1; | |
538 | op = exp->elts[pc + 1].opcode; | |
539 | if (binop_user_defined_p (op, arg1, arg2)) | |
540 | return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op); | |
541 | else if (op == BINOP_ADD) | |
542 | arg2 = value_add (arg1, arg2); | |
543 | else if (op == BINOP_SUB) | |
544 | arg2 = value_sub (arg1, arg2); | |
545 | else | |
546 | arg2 = value_binop (arg1, arg2, op); | |
547 | return value_assign (arg1, arg2); | |
548 | ||
549 | case BINOP_ADD: | |
550 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
551 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
552 | if (noside == EVAL_SKIP) | |
553 | goto nosideret; | |
554 | if (binop_user_defined_p (op, arg1, arg2)) | |
2ccb3837 | 555 | return value_x_binop (arg1, arg2, op, OP_NULL); |
bd5635a1 RP |
556 | else |
557 | return value_add (arg1, arg2); | |
558 | ||
559 | case BINOP_SUB: | |
560 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
561 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
562 | if (noside == EVAL_SKIP) | |
563 | goto nosideret; | |
564 | if (binop_user_defined_p (op, arg1, arg2)) | |
2ccb3837 | 565 | return value_x_binop (arg1, arg2, op, OP_NULL); |
bd5635a1 RP |
566 | else |
567 | return value_sub (arg1, arg2); | |
568 | ||
569 | case BINOP_MUL: | |
570 | case BINOP_DIV: | |
571 | case BINOP_REM: | |
76a0ffb4 | 572 | case BINOP_MOD: |
bd5635a1 RP |
573 | case BINOP_LSH: |
574 | case BINOP_RSH: | |
e58de8a2 FF |
575 | case BINOP_BITWISE_AND: |
576 | case BINOP_BITWISE_IOR: | |
577 | case BINOP_BITWISE_XOR: | |
bd5635a1 RP |
578 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
579 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
580 | if (noside == EVAL_SKIP) | |
581 | goto nosideret; | |
582 | if (binop_user_defined_p (op, arg1, arg2)) | |
2ccb3837 | 583 | return value_x_binop (arg1, arg2, op, OP_NULL); |
bd5635a1 RP |
584 | else |
585 | if (noside == EVAL_AVOID_SIDE_EFFECTS | |
76a0ffb4 | 586 | && (op == BINOP_DIV || op == BINOP_REM || op == BINOP_MOD)) |
bd5635a1 RP |
587 | return value_zero (VALUE_TYPE (arg1), not_lval); |
588 | else | |
589 | return value_binop (arg1, arg2, op); | |
590 | ||
591 | case BINOP_SUBSCRIPT: | |
592 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
593 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
594 | if (noside == EVAL_SKIP) | |
595 | goto nosideret; | |
596 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
35fcebce PB |
597 | { |
598 | /* If the user attempts to subscript something that has no target | |
599 | type (like a plain int variable for example), then report this | |
600 | as an error. */ | |
601 | ||
602 | type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1)); | |
603 | if (type) | |
604 | return value_zero (type, VALUE_LVAL (arg1)); | |
605 | else | |
606 | error ("cannot subscript something of type `%s'", | |
607 | TYPE_NAME (VALUE_TYPE (arg1))); | |
608 | } | |
bd5635a1 RP |
609 | |
610 | if (binop_user_defined_p (op, arg1, arg2)) | |
2ccb3837 | 611 | return value_x_binop (arg1, arg2, op, OP_NULL); |
bd5635a1 RP |
612 | else |
613 | return value_subscript (arg1, arg2); | |
614 | ||
54bbbfb4 FF |
615 | case MULTI_SUBSCRIPT: |
616 | (*pos) += 2; | |
617 | nargs = longest_to_int (exp->elts[pc + 1].longconst); | |
618 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
619 | while (nargs-- > 0) | |
620 | { | |
621 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
622 | /* FIXME: EVAL_SKIP handling may not be correct. */ | |
623 | if (noside == EVAL_SKIP) | |
624 | { | |
625 | if (nargs > 0) | |
626 | { | |
627 | continue; | |
628 | } | |
629 | else | |
630 | { | |
631 | goto nosideret; | |
632 | } | |
633 | } | |
634 | /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */ | |
635 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
636 | { | |
637 | /* If the user attempts to subscript something that has no target | |
638 | type (like a plain int variable for example), then report this | |
639 | as an error. */ | |
640 | ||
641 | type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1)); | |
642 | if (type != NULL) | |
643 | { | |
644 | arg1 = value_zero (type, VALUE_LVAL (arg1)); | |
645 | noside = EVAL_SKIP; | |
646 | continue; | |
647 | } | |
648 | else | |
649 | { | |
650 | error ("cannot subscript something of type `%s'", | |
651 | TYPE_NAME (VALUE_TYPE (arg1))); | |
652 | } | |
653 | } | |
654 | ||
655 | if (binop_user_defined_p (op, arg1, arg2)) | |
656 | { | |
657 | arg1 = value_x_binop (arg1, arg2, op, OP_NULL); | |
658 | } | |
659 | else | |
660 | { | |
661 | arg1 = value_subscript (arg1, arg2); | |
662 | } | |
663 | } | |
664 | return (arg1); | |
665 | ||
e58de8a2 | 666 | case BINOP_LOGICAL_AND: |
bd5635a1 RP |
667 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
668 | if (noside == EVAL_SKIP) | |
669 | { | |
670 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
671 | goto nosideret; | |
672 | } | |
673 | ||
674 | oldpos = *pos; | |
675 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS); | |
676 | *pos = oldpos; | |
677 | ||
678 | if (binop_user_defined_p (op, arg1, arg2)) | |
679 | { | |
680 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
2ccb3837 | 681 | return value_x_binop (arg1, arg2, op, OP_NULL); |
bd5635a1 RP |
682 | } |
683 | else | |
684 | { | |
e58de8a2 | 685 | tem = value_logical_not (arg1); |
bd5635a1 RP |
686 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, |
687 | (tem ? EVAL_SKIP : noside)); | |
2ccb3837 | 688 | return value_from_longest (builtin_type_int, |
e58de8a2 | 689 | (LONGEST) (!tem && !value_logical_not (arg2))); |
bd5635a1 RP |
690 | } |
691 | ||
e58de8a2 | 692 | case BINOP_LOGICAL_OR: |
bd5635a1 RP |
693 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
694 | if (noside == EVAL_SKIP) | |
695 | { | |
696 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
697 | goto nosideret; | |
698 | } | |
699 | ||
700 | oldpos = *pos; | |
701 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS); | |
702 | *pos = oldpos; | |
703 | ||
704 | if (binop_user_defined_p (op, arg1, arg2)) | |
705 | { | |
706 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
2ccb3837 | 707 | return value_x_binop (arg1, arg2, op, OP_NULL); |
bd5635a1 RP |
708 | } |
709 | else | |
710 | { | |
e58de8a2 | 711 | tem = value_logical_not (arg1); |
bd5635a1 RP |
712 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, |
713 | (!tem ? EVAL_SKIP : noside)); | |
2ccb3837 | 714 | return value_from_longest (builtin_type_int, |
e58de8a2 | 715 | (LONGEST) (!tem || !value_logical_not (arg2))); |
bd5635a1 RP |
716 | } |
717 | ||
718 | case BINOP_EQUAL: | |
719 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
720 | arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside); | |
721 | if (noside == EVAL_SKIP) | |
722 | goto nosideret; | |
723 | if (binop_user_defined_p (op, arg1, arg2)) | |
724 | { | |
2ccb3837 | 725 | return value_x_binop (arg1, arg2, op, OP_NULL); |
bd5635a1 RP |
726 | } |
727 | else | |
728 | { | |
729 | tem = value_equal (arg1, arg2); | |
2ccb3837 | 730 | return value_from_longest (builtin_type_int, (LONGEST) tem); |
bd5635a1 RP |
731 | } |
732 | ||
733 | case BINOP_NOTEQUAL: | |
734 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
735 | arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside); | |
736 | if (noside == EVAL_SKIP) | |
737 | goto nosideret; | |
738 | if (binop_user_defined_p (op, arg1, arg2)) | |
739 | { | |
2ccb3837 | 740 | return value_x_binop (arg1, arg2, op, OP_NULL); |
bd5635a1 RP |
741 | } |
742 | else | |
743 | { | |
744 | tem = value_equal (arg1, arg2); | |
2ccb3837 | 745 | return value_from_longest (builtin_type_int, (LONGEST) ! tem); |
bd5635a1 RP |
746 | } |
747 | ||
748 | case BINOP_LESS: | |
749 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
750 | arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside); | |
751 | if (noside == EVAL_SKIP) | |
752 | goto nosideret; | |
753 | if (binop_user_defined_p (op, arg1, arg2)) | |
754 | { | |
2ccb3837 | 755 | return value_x_binop (arg1, arg2, op, OP_NULL); |
bd5635a1 RP |
756 | } |
757 | else | |
758 | { | |
759 | tem = value_less (arg1, arg2); | |
2ccb3837 | 760 | return value_from_longest (builtin_type_int, (LONGEST) tem); |
bd5635a1 RP |
761 | } |
762 | ||
763 | case BINOP_GTR: | |
764 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
765 | arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside); | |
766 | if (noside == EVAL_SKIP) | |
767 | goto nosideret; | |
768 | if (binop_user_defined_p (op, arg1, arg2)) | |
769 | { | |
2ccb3837 | 770 | return value_x_binop (arg1, arg2, op, OP_NULL); |
bd5635a1 RP |
771 | } |
772 | else | |
773 | { | |
774 | tem = value_less (arg2, arg1); | |
2ccb3837 | 775 | return value_from_longest (builtin_type_int, (LONGEST) tem); |
bd5635a1 RP |
776 | } |
777 | ||
778 | case BINOP_GEQ: | |
779 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
780 | arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside); | |
781 | if (noside == EVAL_SKIP) | |
782 | goto nosideret; | |
783 | if (binop_user_defined_p (op, arg1, arg2)) | |
784 | { | |
2ccb3837 | 785 | return value_x_binop (arg1, arg2, op, OP_NULL); |
bd5635a1 RP |
786 | } |
787 | else | |
788 | { | |
8f86a4e4 JG |
789 | tem = value_less (arg2, arg1) || value_equal (arg1, arg2); |
790 | return value_from_longest (builtin_type_int, (LONGEST) tem); | |
bd5635a1 RP |
791 | } |
792 | ||
793 | case BINOP_LEQ: | |
794 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
795 | arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside); | |
796 | if (noside == EVAL_SKIP) | |
797 | goto nosideret; | |
798 | if (binop_user_defined_p (op, arg1, arg2)) | |
799 | { | |
2ccb3837 | 800 | return value_x_binop (arg1, arg2, op, OP_NULL); |
bd5635a1 RP |
801 | } |
802 | else | |
803 | { | |
8f86a4e4 JG |
804 | tem = value_less (arg1, arg2) || value_equal (arg1, arg2); |
805 | return value_from_longest (builtin_type_int, (LONGEST) tem); | |
bd5635a1 RP |
806 | } |
807 | ||
808 | case BINOP_REPEAT: | |
809 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
810 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
811 | if (noside == EVAL_SKIP) | |
812 | goto nosideret; | |
813 | if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT) | |
814 | error ("Non-integral right operand for \"@\" operator."); | |
815 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
816 | return allocate_repeat_value (VALUE_TYPE (arg1), | |
2ccb3837 | 817 | longest_to_int (value_as_long (arg2))); |
bd5635a1 | 818 | else |
2ccb3837 | 819 | return value_repeat (arg1, longest_to_int (value_as_long (arg2))); |
bd5635a1 RP |
820 | |
821 | case BINOP_COMMA: | |
822 | evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
823 | return evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
824 | ||
825 | case UNOP_NEG: | |
826 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
827 | if (noside == EVAL_SKIP) | |
828 | goto nosideret; | |
829 | if (unop_user_defined_p (op, arg1)) | |
830 | return value_x_unop (arg1, op); | |
831 | else | |
832 | return value_neg (arg1); | |
833 | ||
e58de8a2 | 834 | case UNOP_COMPLEMENT: |
5f00ca54 JK |
835 | /* C++: check for and handle destructor names. */ |
836 | op = exp->elts[*pos].opcode; | |
837 | ||
bd5635a1 RP |
838 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
839 | if (noside == EVAL_SKIP) | |
840 | goto nosideret; | |
e58de8a2 FF |
841 | if (unop_user_defined_p (UNOP_COMPLEMENT, arg1)) |
842 | return value_x_unop (arg1, UNOP_COMPLEMENT); | |
bd5635a1 | 843 | else |
e58de8a2 | 844 | return value_complement (arg1); |
bd5635a1 | 845 | |
e58de8a2 | 846 | case UNOP_LOGICAL_NOT: |
bd5635a1 RP |
847 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
848 | if (noside == EVAL_SKIP) | |
849 | goto nosideret; | |
850 | if (unop_user_defined_p (op, arg1)) | |
851 | return value_x_unop (arg1, op); | |
852 | else | |
2ccb3837 | 853 | return value_from_longest (builtin_type_int, |
e58de8a2 | 854 | (LONGEST) value_logical_not (arg1)); |
bd5635a1 RP |
855 | |
856 | case UNOP_IND: | |
857 | if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR) | |
858 | expect_type = TYPE_TARGET_TYPE (expect_type); | |
859 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
860 | if (noside == EVAL_SKIP) | |
861 | goto nosideret; | |
862 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
863 | { | |
864 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR | |
865 | || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF | |
866 | /* In C you can dereference an array to get the 1st elt. */ | |
867 | || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY | |
868 | ) | |
869 | return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)), | |
870 | lval_memory); | |
871 | else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT) | |
872 | /* GDB allows dereferencing an int. */ | |
873 | return value_zero (builtin_type_int, lval_memory); | |
874 | else | |
875 | error ("Attempt to take contents of a non-pointer value."); | |
876 | } | |
877 | return value_ind (arg1); | |
878 | ||
879 | case UNOP_ADDR: | |
880 | /* C++: check for and handle pointer to members. */ | |
881 | ||
882 | op = exp->elts[*pos].opcode; | |
883 | ||
884 | if (noside == EVAL_SKIP) | |
885 | { | |
886 | if (op == OP_SCOPE) | |
887 | { | |
a8a69e63 | 888 | int temm = longest_to_int (exp->elts[pc+3].longconst); |
1500864f | 889 | (*pos) += 3 + BYTES_TO_EXP_ELEM (temm + 1); |
bd5635a1 RP |
890 | } |
891 | else | |
892 | evaluate_subexp (expect_type, exp, pos, EVAL_SKIP); | |
893 | goto nosideret; | |
894 | } | |
895 | ||
01be6913 | 896 | return evaluate_subexp_for_address (exp, pos, noside); |
bd5635a1 RP |
897 | |
898 | case UNOP_SIZEOF: | |
899 | if (noside == EVAL_SKIP) | |
900 | { | |
901 | evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP); | |
902 | goto nosideret; | |
903 | } | |
904 | return evaluate_subexp_for_sizeof (exp, pos); | |
905 | ||
906 | case UNOP_CAST: | |
907 | (*pos) += 2; | |
908 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
909 | if (noside == EVAL_SKIP) | |
910 | goto nosideret; | |
911 | return value_cast (exp->elts[pc + 1].type, arg1); | |
912 | ||
913 | case UNOP_MEMVAL: | |
914 | (*pos) += 2; | |
915 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
916 | if (noside == EVAL_SKIP) | |
917 | goto nosideret; | |
918 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
919 | return value_zero (exp->elts[pc + 1].type, lval_memory); | |
920 | else | |
921 | return value_at_lazy (exp->elts[pc + 1].type, | |
2ccb3837 | 922 | value_as_pointer (arg1)); |
bd5635a1 RP |
923 | |
924 | case UNOP_PREINCREMENT: | |
925 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
926 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
927 | return arg1; | |
928 | else if (unop_user_defined_p (op, arg1)) | |
929 | { | |
930 | return value_x_unop (arg1, op); | |
931 | } | |
932 | else | |
933 | { | |
2ccb3837 | 934 | arg2 = value_add (arg1, value_from_longest (builtin_type_char, |
bd5635a1 RP |
935 | (LONGEST) 1)); |
936 | return value_assign (arg1, arg2); | |
937 | } | |
938 | ||
939 | case UNOP_PREDECREMENT: | |
940 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
941 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
942 | return arg1; | |
943 | else if (unop_user_defined_p (op, arg1)) | |
944 | { | |
945 | return value_x_unop (arg1, op); | |
946 | } | |
947 | else | |
948 | { | |
2ccb3837 | 949 | arg2 = value_sub (arg1, value_from_longest (builtin_type_char, |
bd5635a1 RP |
950 | (LONGEST) 1)); |
951 | return value_assign (arg1, arg2); | |
952 | } | |
953 | ||
954 | case UNOP_POSTINCREMENT: | |
955 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
956 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
957 | return arg1; | |
958 | else if (unop_user_defined_p (op, arg1)) | |
959 | { | |
960 | return value_x_unop (arg1, op); | |
961 | } | |
962 | else | |
963 | { | |
2ccb3837 | 964 | arg2 = value_add (arg1, value_from_longest (builtin_type_char, |
bd5635a1 RP |
965 | (LONGEST) 1)); |
966 | value_assign (arg1, arg2); | |
967 | return arg1; | |
968 | } | |
969 | ||
970 | case UNOP_POSTDECREMENT: | |
971 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
972 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
973 | return arg1; | |
974 | else if (unop_user_defined_p (op, arg1)) | |
975 | { | |
976 | return value_x_unop (arg1, op); | |
977 | } | |
978 | else | |
979 | { | |
2ccb3837 | 980 | arg2 = value_sub (arg1, value_from_longest (builtin_type_char, |
bd5635a1 RP |
981 | (LONGEST) 1)); |
982 | value_assign (arg1, arg2); | |
983 | return arg1; | |
984 | } | |
985 | ||
986 | case OP_THIS: | |
987 | (*pos) += 1; | |
988 | return value_of_this (1); | |
989 | ||
1500864f JK |
990 | case OP_TYPE: |
991 | error ("Attempt to use a type name as an expression"); | |
992 | ||
bd5635a1 | 993 | default: |
1500864f JK |
994 | /* Removing this case and compiling with gcc -Wall reveals that |
995 | a lot of cases are hitting this case. Some of these should | |
996 | probably be removed from expression.h (e.g. do we need a BINOP_SCOPE | |
997 | and an OP_SCOPE?); others are legitimate expressions which are | |
998 | (apparently) not fully implemented. | |
999 | ||
1000 | If there are any cases landing here which mean a user error, | |
1001 | then they should be separate cases, with more descriptive | |
1002 | error messages. */ | |
1003 | ||
1004 | error ("\ | |
1005 | GDB does not (yet) know how to evaluated that kind of expression"); | |
bd5635a1 RP |
1006 | } |
1007 | ||
1008 | nosideret: | |
2ccb3837 | 1009 | return value_from_longest (builtin_type_long, (LONGEST) 1); |
bd5635a1 RP |
1010 | } |
1011 | \f | |
1012 | /* Evaluate a subexpression of EXP, at index *POS, | |
1013 | and return the address of that subexpression. | |
1014 | Advance *POS over the subexpression. | |
1015 | If the subexpression isn't an lvalue, get an error. | |
1016 | NOSIDE may be EVAL_AVOID_SIDE_EFFECTS; | |
1017 | then only the type of the result need be correct. */ | |
1018 | ||
1019 | static value | |
1020 | evaluate_subexp_for_address (exp, pos, noside) | |
1021 | register struct expression *exp; | |
1022 | register int *pos; | |
1023 | enum noside noside; | |
1024 | { | |
1025 | enum exp_opcode op; | |
1026 | register int pc; | |
e17960fb | 1027 | struct symbol *var; |
bd5635a1 RP |
1028 | |
1029 | pc = (*pos); | |
1030 | op = exp->elts[pc].opcode; | |
1031 | ||
1032 | switch (op) | |
1033 | { | |
1034 | case UNOP_IND: | |
1035 | (*pos)++; | |
1036 | return evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1037 | ||
1038 | case UNOP_MEMVAL: | |
1039 | (*pos) += 3; | |
1040 | return value_cast (lookup_pointer_type (exp->elts[pc + 1].type), | |
1041 | evaluate_subexp (NULL_TYPE, exp, pos, noside)); | |
1042 | ||
1043 | case OP_VAR_VALUE: | |
e17960fb JG |
1044 | var = exp->elts[pc + 1].symbol; |
1045 | ||
1046 | /* C++: The "address" of a reference should yield the address | |
1047 | * of the object pointed to. Let value_addr() deal with it. */ | |
1048 | if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_REF) | |
1049 | goto default_case; | |
1050 | ||
bd5635a1 RP |
1051 | (*pos) += 3; |
1052 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1053 | { | |
1054 | struct type *type = | |
e17960fb JG |
1055 | lookup_pointer_type (SYMBOL_TYPE (var)); |
1056 | enum address_class sym_class = SYMBOL_CLASS (var); | |
bd5635a1 RP |
1057 | |
1058 | if (sym_class == LOC_CONST | |
1059 | || sym_class == LOC_CONST_BYTES | |
1060 | || sym_class == LOC_REGISTER | |
1061 | || sym_class == LOC_REGPARM) | |
1062 | error ("Attempt to take address of register or constant."); | |
1063 | ||
1064 | return | |
1065 | value_zero (type, not_lval); | |
1066 | } | |
1067 | else | |
e17960fb | 1068 | return locate_var_value (var, (FRAME) 0); |
bd5635a1 RP |
1069 | |
1070 | default: | |
e17960fb | 1071 | default_case: |
bd5635a1 RP |
1072 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
1073 | { | |
1074 | value x = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1075 | if (VALUE_LVAL (x) == lval_memory) | |
0a5d35ed | 1076 | return value_zero (lookup_pointer_type (VALUE_TYPE (x)), |
bd5635a1 RP |
1077 | not_lval); |
1078 | else | |
1079 | error ("Attempt to take address of non-lval"); | |
1080 | } | |
1081 | return value_addr (evaluate_subexp (NULL_TYPE, exp, pos, noside)); | |
1082 | } | |
1083 | } | |
1084 | ||
1085 | /* Evaluate like `evaluate_subexp' except coercing arrays to pointers. | |
fb6e675f FF |
1086 | When used in contexts where arrays will be coerced anyway, this is |
1087 | equivalent to `evaluate_subexp' but much faster because it avoids | |
1088 | actually fetching array contents. | |
1089 | ||
1090 | Note that we currently only do the coercion for C expressions, where | |
1091 | arrays are zero based and the coercion is correct. For other languages, | |
1092 | with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION | |
1093 | to decide if coercion is appropriate. | |
1094 | ||
1095 | */ | |
bd5635a1 RP |
1096 | |
1097 | static value | |
1098 | evaluate_subexp_with_coercion (exp, pos, noside) | |
1099 | register struct expression *exp; | |
1100 | register int *pos; | |
1101 | enum noside noside; | |
1102 | { | |
1103 | register enum exp_opcode op; | |
1104 | register int pc; | |
1105 | register value val; | |
e17960fb | 1106 | struct symbol *var; |
bd5635a1 RP |
1107 | |
1108 | pc = (*pos); | |
1109 | op = exp->elts[pc].opcode; | |
1110 | ||
1111 | switch (op) | |
1112 | { | |
1113 | case OP_VAR_VALUE: | |
e17960fb | 1114 | var = exp->elts[pc + 1].symbol; |
fb6e675f FF |
1115 | if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_ARRAY |
1116 | && CAST_IS_CONVERSION) | |
bd5635a1 RP |
1117 | { |
1118 | (*pos) += 3; | |
e17960fb JG |
1119 | val = locate_var_value (var, (FRAME) 0); |
1120 | return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (var))), | |
bd5635a1 RP |
1121 | val); |
1122 | } | |
1123 | default: | |
1124 | return evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1125 | } | |
1126 | } | |
1127 | ||
1128 | /* Evaluate a subexpression of EXP, at index *POS, | |
1129 | and return a value for the size of that subexpression. | |
1130 | Advance *POS over the subexpression. */ | |
1131 | ||
1132 | static value | |
1133 | evaluate_subexp_for_sizeof (exp, pos) | |
1134 | register struct expression *exp; | |
1135 | register int *pos; | |
1136 | { | |
1137 | enum exp_opcode op; | |
1138 | register int pc; | |
1139 | value val; | |
1140 | ||
1141 | pc = (*pos); | |
1142 | op = exp->elts[pc].opcode; | |
1143 | ||
1144 | switch (op) | |
1145 | { | |
1146 | /* This case is handled specially | |
1147 | so that we avoid creating a value for the result type. | |
1148 | If the result type is very big, it's desirable not to | |
1149 | create a value unnecessarily. */ | |
1150 | case UNOP_IND: | |
1151 | (*pos)++; | |
1152 | val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS); | |
2ccb3837 | 1153 | return value_from_longest (builtin_type_int, (LONGEST) |
bd5635a1 RP |
1154 | TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val)))); |
1155 | ||
1156 | case UNOP_MEMVAL: | |
1157 | (*pos) += 3; | |
2ccb3837 | 1158 | return value_from_longest (builtin_type_int, |
bd5635a1 RP |
1159 | (LONGEST) TYPE_LENGTH (exp->elts[pc + 1].type)); |
1160 | ||
1161 | case OP_VAR_VALUE: | |
1162 | (*pos) += 3; | |
2ccb3837 | 1163 | return value_from_longest (builtin_type_int, |
bd5635a1 RP |
1164 | (LONGEST) TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 1].symbol))); |
1165 | ||
1166 | default: | |
1167 | val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS); | |
2ccb3837 | 1168 | return value_from_longest (builtin_type_int, |
bd5635a1 RP |
1169 | (LONGEST) TYPE_LENGTH (VALUE_TYPE (val))); |
1170 | } | |
1171 | } | |
0a5d35ed SG |
1172 | |
1173 | /* Parse a type expression in the string [P..P+LENGTH). */ | |
1174 | ||
1175 | struct type * | |
1176 | parse_and_eval_type (p, length) | |
1177 | char *p; | |
1178 | int length; | |
1179 | { | |
1180 | char *tmp = (char *)alloca (length + 4); | |
1181 | struct expression *expr; | |
1182 | tmp[0] = '('; | |
35fcebce | 1183 | memcpy (tmp+1, p, length); |
0a5d35ed SG |
1184 | tmp[length+1] = ')'; |
1185 | tmp[length+2] = '0'; | |
1186 | tmp[length+3] = '\0'; | |
1187 | expr = parse_expression (tmp); | |
1188 | if (expr->elts[0].opcode != UNOP_CAST) | |
1189 | error ("Internal error in eval_type."); | |
1190 | return expr->elts[1].type; | |
1191 | } |