Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Evaluate expressions for GDB. |
1bac305b | 2 | |
6aba47ca DJ |
3 | Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, |
4 | 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2007 | |
0d5de010 | 5 | Free Software Foundation, Inc. |
c906108c | 6 | |
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
c906108c | 13 | |
c5aa993b JM |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
c906108c | 18 | |
c5aa993b JM |
19 | You should have received a copy of the GNU General Public License |
20 | along with this program; if not, write to the Free Software | |
197e01b6 EZ |
21 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
22 | Boston, MA 02110-1301, USA. */ | |
c906108c SS |
23 | |
24 | #include "defs.h" | |
25 | #include "gdb_string.h" | |
26 | #include "symtab.h" | |
27 | #include "gdbtypes.h" | |
28 | #include "value.h" | |
29 | #include "expression.h" | |
30 | #include "target.h" | |
31 | #include "frame.h" | |
c5aa993b JM |
32 | #include "language.h" /* For CAST_IS_CONVERSION */ |
33 | #include "f-lang.h" /* for array bound stuff */ | |
015a42b4 | 34 | #include "cp-abi.h" |
04714b91 | 35 | #include "infcall.h" |
a9fa03de AF |
36 | #include "objc-lang.h" |
37 | #include "block.h" | |
5f9769d1 | 38 | #include "parser-defs.h" |
d3cbe7ef | 39 | #include "cp-support.h" |
5e572bb4 DJ |
40 | #include "ui-out.h" |
41 | #include "exceptions.h" | |
123dc839 | 42 | #include "regcache.h" |
c906108c | 43 | |
0d5de010 DJ |
44 | #include "gdb_assert.h" |
45 | ||
c5aa993b | 46 | /* This is defined in valops.c */ |
c906108c SS |
47 | extern int overload_resolution; |
48 | ||
070ad9f0 DB |
49 | /* JYG: lookup rtti type of STRUCTOP_PTR when this is set to continue |
50 | on with successful lookup for member/method of the rtti type. */ | |
51 | extern int objectprint; | |
c906108c SS |
52 | |
53 | /* Prototypes for local functions. */ | |
54 | ||
61051030 | 55 | static struct value *evaluate_subexp_for_sizeof (struct expression *, int *); |
c906108c | 56 | |
61051030 AC |
57 | static struct value *evaluate_subexp_for_address (struct expression *, |
58 | int *, enum noside); | |
c906108c | 59 | |
61051030 AC |
60 | static struct value *evaluate_subexp (struct type *, struct expression *, |
61 | int *, enum noside); | |
c906108c | 62 | |
a14ed312 | 63 | static char *get_label (struct expression *, int *); |
c906108c | 64 | |
61051030 AC |
65 | static struct value *evaluate_struct_tuple (struct value *, |
66 | struct expression *, int *, | |
67 | enum noside, int); | |
c906108c | 68 | |
61051030 AC |
69 | static LONGEST init_array_element (struct value *, struct value *, |
70 | struct expression *, int *, enum noside, | |
71 | LONGEST, LONGEST); | |
c906108c | 72 | |
61051030 | 73 | static struct value * |
aa1ee363 AC |
74 | evaluate_subexp (struct type *expect_type, struct expression *exp, |
75 | int *pos, enum noside noside) | |
c906108c | 76 | { |
5f9769d1 PH |
77 | return (*exp->language_defn->la_exp_desc->evaluate_exp) |
78 | (expect_type, exp, pos, noside); | |
c906108c SS |
79 | } |
80 | \f | |
81 | /* Parse the string EXP as a C expression, evaluate it, | |
82 | and return the result as a number. */ | |
83 | ||
84 | CORE_ADDR | |
fba45db2 | 85 | parse_and_eval_address (char *exp) |
c906108c SS |
86 | { |
87 | struct expression *expr = parse_expression (exp); | |
52f0bd74 AC |
88 | CORE_ADDR addr; |
89 | struct cleanup *old_chain = | |
62995fc4 | 90 | make_cleanup (free_current_contents, &expr); |
c906108c | 91 | |
1aa20aa8 | 92 | addr = value_as_address (evaluate_expression (expr)); |
c906108c SS |
93 | do_cleanups (old_chain); |
94 | return addr; | |
95 | } | |
96 | ||
97 | /* Like parse_and_eval_address but takes a pointer to a char * variable | |
98 | and advanced that variable across the characters parsed. */ | |
99 | ||
100 | CORE_ADDR | |
fba45db2 | 101 | parse_and_eval_address_1 (char **expptr) |
c906108c | 102 | { |
c5aa993b | 103 | struct expression *expr = parse_exp_1 (expptr, (struct block *) 0, 0); |
52f0bd74 AC |
104 | CORE_ADDR addr; |
105 | struct cleanup *old_chain = | |
62995fc4 | 106 | make_cleanup (free_current_contents, &expr); |
c906108c | 107 | |
1aa20aa8 | 108 | addr = value_as_address (evaluate_expression (expr)); |
c906108c SS |
109 | do_cleanups (old_chain); |
110 | return addr; | |
111 | } | |
112 | ||
bb518678 DT |
113 | /* Like parse_and_eval_address, but treats the value of the expression |
114 | as an integer, not an address, returns a LONGEST, not a CORE_ADDR */ | |
115 | LONGEST | |
116 | parse_and_eval_long (char *exp) | |
117 | { | |
118 | struct expression *expr = parse_expression (exp); | |
52f0bd74 AC |
119 | LONGEST retval; |
120 | struct cleanup *old_chain = | |
bb518678 DT |
121 | make_cleanup (free_current_contents, &expr); |
122 | ||
123 | retval = value_as_long (evaluate_expression (expr)); | |
124 | do_cleanups (old_chain); | |
125 | return (retval); | |
126 | } | |
127 | ||
61051030 | 128 | struct value * |
fba45db2 | 129 | parse_and_eval (char *exp) |
c906108c SS |
130 | { |
131 | struct expression *expr = parse_expression (exp); | |
61051030 | 132 | struct value *val; |
52f0bd74 | 133 | struct cleanup *old_chain = |
62995fc4 | 134 | make_cleanup (free_current_contents, &expr); |
c906108c SS |
135 | |
136 | val = evaluate_expression (expr); | |
137 | do_cleanups (old_chain); | |
138 | return val; | |
139 | } | |
140 | ||
141 | /* Parse up to a comma (or to a closeparen) | |
142 | in the string EXPP as an expression, evaluate it, and return the value. | |
143 | EXPP is advanced to point to the comma. */ | |
144 | ||
61051030 | 145 | struct value * |
fba45db2 | 146 | parse_to_comma_and_eval (char **expp) |
c906108c SS |
147 | { |
148 | struct expression *expr = parse_exp_1 (expp, (struct block *) 0, 1); | |
61051030 | 149 | struct value *val; |
52f0bd74 | 150 | struct cleanup *old_chain = |
62995fc4 | 151 | make_cleanup (free_current_contents, &expr); |
c906108c SS |
152 | |
153 | val = evaluate_expression (expr); | |
154 | do_cleanups (old_chain); | |
155 | return val; | |
156 | } | |
157 | \f | |
158 | /* Evaluate an expression in internal prefix form | |
159 | such as is constructed by parse.y. | |
160 | ||
161 | See expression.h for info on the format of an expression. */ | |
162 | ||
61051030 | 163 | struct value * |
fba45db2 | 164 | evaluate_expression (struct expression *exp) |
c906108c SS |
165 | { |
166 | int pc = 0; | |
167 | return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_NORMAL); | |
168 | } | |
169 | ||
170 | /* Evaluate an expression, avoiding all memory references | |
171 | and getting a value whose type alone is correct. */ | |
172 | ||
61051030 | 173 | struct value * |
fba45db2 | 174 | evaluate_type (struct expression *exp) |
c906108c SS |
175 | { |
176 | int pc = 0; | |
177 | return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS); | |
178 | } | |
179 | ||
180 | /* If the next expression is an OP_LABELED, skips past it, | |
181 | returning the label. Otherwise, does nothing and returns NULL. */ | |
182 | ||
c5aa993b | 183 | static char * |
aa1ee363 | 184 | get_label (struct expression *exp, int *pos) |
c906108c SS |
185 | { |
186 | if (exp->elts[*pos].opcode == OP_LABELED) | |
187 | { | |
188 | int pc = (*pos)++; | |
189 | char *name = &exp->elts[pc + 2].string; | |
190 | int tem = longest_to_int (exp->elts[pc + 1].longconst); | |
191 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
192 | return name; | |
193 | } | |
194 | else | |
195 | return NULL; | |
196 | } | |
197 | ||
1b831c93 | 198 | /* This function evaluates tuples (in (the deleted) Chill) or |
db034ac5 | 199 | brace-initializers (in C/C++) for structure types. */ |
c906108c | 200 | |
61051030 AC |
201 | static struct value * |
202 | evaluate_struct_tuple (struct value *struct_val, | |
aa1ee363 AC |
203 | struct expression *exp, |
204 | int *pos, enum noside noside, int nargs) | |
c906108c | 205 | { |
df407dfe | 206 | struct type *struct_type = check_typedef (value_type (struct_val)); |
c906108c SS |
207 | struct type *substruct_type = struct_type; |
208 | struct type *field_type; | |
209 | int fieldno = -1; | |
210 | int variantno = -1; | |
211 | int subfieldno = -1; | |
c5aa993b | 212 | while (--nargs >= 0) |
c906108c SS |
213 | { |
214 | int pc = *pos; | |
61051030 | 215 | struct value *val = NULL; |
c906108c SS |
216 | int nlabels = 0; |
217 | int bitpos, bitsize; | |
0fd88904 | 218 | bfd_byte *addr; |
c5aa993b | 219 | |
c906108c SS |
220 | /* Skip past the labels, and count them. */ |
221 | while (get_label (exp, pos) != NULL) | |
222 | nlabels++; | |
223 | ||
224 | do | |
225 | { | |
226 | char *label = get_label (exp, &pc); | |
227 | if (label) | |
228 | { | |
229 | for (fieldno = 0; fieldno < TYPE_NFIELDS (struct_type); | |
230 | fieldno++) | |
231 | { | |
232 | char *field_name = TYPE_FIELD_NAME (struct_type, fieldno); | |
edf8c5a3 | 233 | if (field_name != NULL && strcmp (field_name, label) == 0) |
c906108c SS |
234 | { |
235 | variantno = -1; | |
236 | subfieldno = fieldno; | |
237 | substruct_type = struct_type; | |
238 | goto found; | |
239 | } | |
240 | } | |
241 | for (fieldno = 0; fieldno < TYPE_NFIELDS (struct_type); | |
242 | fieldno++) | |
243 | { | |
244 | char *field_name = TYPE_FIELD_NAME (struct_type, fieldno); | |
245 | field_type = TYPE_FIELD_TYPE (struct_type, fieldno); | |
246 | if ((field_name == 0 || *field_name == '\0') | |
247 | && TYPE_CODE (field_type) == TYPE_CODE_UNION) | |
248 | { | |
249 | variantno = 0; | |
250 | for (; variantno < TYPE_NFIELDS (field_type); | |
251 | variantno++) | |
252 | { | |
253 | substruct_type | |
254 | = TYPE_FIELD_TYPE (field_type, variantno); | |
255 | if (TYPE_CODE (substruct_type) == TYPE_CODE_STRUCT) | |
c5aa993b | 256 | { |
c906108c | 257 | for (subfieldno = 0; |
c5aa993b | 258 | subfieldno < TYPE_NFIELDS (substruct_type); |
c906108c SS |
259 | subfieldno++) |
260 | { | |
edf8c5a3 | 261 | if (strcmp(TYPE_FIELD_NAME (substruct_type, |
c906108c | 262 | subfieldno), |
edf8c5a3 | 263 | label) == 0) |
c906108c SS |
264 | { |
265 | goto found; | |
266 | } | |
267 | } | |
268 | } | |
269 | } | |
270 | } | |
271 | } | |
8a3fe4f8 | 272 | error (_("there is no field named %s"), label); |
c906108c SS |
273 | found: |
274 | ; | |
275 | } | |
276 | else | |
277 | { | |
278 | /* Unlabelled tuple element - go to next field. */ | |
279 | if (variantno >= 0) | |
280 | { | |
281 | subfieldno++; | |
282 | if (subfieldno >= TYPE_NFIELDS (substruct_type)) | |
283 | { | |
284 | variantno = -1; | |
285 | substruct_type = struct_type; | |
286 | } | |
287 | } | |
288 | if (variantno < 0) | |
289 | { | |
290 | fieldno++; | |
16963cb6 DJ |
291 | /* Skip static fields. */ |
292 | while (fieldno < TYPE_NFIELDS (struct_type) | |
293 | && TYPE_FIELD_STATIC_KIND (struct_type, fieldno)) | |
294 | fieldno++; | |
c906108c SS |
295 | subfieldno = fieldno; |
296 | if (fieldno >= TYPE_NFIELDS (struct_type)) | |
8a3fe4f8 | 297 | error (_("too many initializers")); |
c906108c SS |
298 | field_type = TYPE_FIELD_TYPE (struct_type, fieldno); |
299 | if (TYPE_CODE (field_type) == TYPE_CODE_UNION | |
300 | && TYPE_FIELD_NAME (struct_type, fieldno)[0] == '0') | |
8a3fe4f8 | 301 | error (_("don't know which variant you want to set")); |
c906108c SS |
302 | } |
303 | } | |
304 | ||
305 | /* Here, struct_type is the type of the inner struct, | |
306 | while substruct_type is the type of the inner struct. | |
307 | These are the same for normal structures, but a variant struct | |
308 | contains anonymous union fields that contain substruct fields. | |
309 | The value fieldno is the index of the top-level (normal or | |
310 | anonymous union) field in struct_field, while the value | |
311 | subfieldno is the index of the actual real (named inner) field | |
312 | in substruct_type. */ | |
313 | ||
314 | field_type = TYPE_FIELD_TYPE (substruct_type, subfieldno); | |
315 | if (val == 0) | |
316 | val = evaluate_subexp (field_type, exp, pos, noside); | |
317 | ||
318 | /* Now actually set the field in struct_val. */ | |
319 | ||
320 | /* Assign val to field fieldno. */ | |
df407dfe | 321 | if (value_type (val) != field_type) |
c906108c SS |
322 | val = value_cast (field_type, val); |
323 | ||
324 | bitsize = TYPE_FIELD_BITSIZE (substruct_type, subfieldno); | |
325 | bitpos = TYPE_FIELD_BITPOS (struct_type, fieldno); | |
326 | if (variantno >= 0) | |
327 | bitpos += TYPE_FIELD_BITPOS (substruct_type, subfieldno); | |
0fd88904 | 328 | addr = value_contents_writeable (struct_val) + bitpos / 8; |
c906108c SS |
329 | if (bitsize) |
330 | modify_field (addr, value_as_long (val), | |
331 | bitpos % 8, bitsize); | |
332 | else | |
0fd88904 | 333 | memcpy (addr, value_contents (val), |
df407dfe | 334 | TYPE_LENGTH (value_type (val))); |
c5aa993b JM |
335 | } |
336 | while (--nlabels > 0); | |
c906108c SS |
337 | } |
338 | return struct_val; | |
339 | } | |
340 | ||
db034ac5 | 341 | /* Recursive helper function for setting elements of array tuples for |
1b831c93 AC |
342 | (the deleted) Chill. The target is ARRAY (which has bounds |
343 | LOW_BOUND to HIGH_BOUND); the element value is ELEMENT; EXP, POS | |
344 | and NOSIDE are as usual. Evaluates index expresions and sets the | |
345 | specified element(s) of ARRAY to ELEMENT. Returns last index | |
346 | value. */ | |
c906108c SS |
347 | |
348 | static LONGEST | |
61051030 | 349 | init_array_element (struct value *array, struct value *element, |
aa1ee363 | 350 | struct expression *exp, int *pos, |
fba45db2 | 351 | enum noside noside, LONGEST low_bound, LONGEST high_bound) |
c906108c SS |
352 | { |
353 | LONGEST index; | |
df407dfe | 354 | int element_size = TYPE_LENGTH (value_type (element)); |
c906108c SS |
355 | if (exp->elts[*pos].opcode == BINOP_COMMA) |
356 | { | |
357 | (*pos)++; | |
358 | init_array_element (array, element, exp, pos, noside, | |
359 | low_bound, high_bound); | |
360 | return init_array_element (array, element, | |
361 | exp, pos, noside, low_bound, high_bound); | |
362 | } | |
363 | else if (exp->elts[*pos].opcode == BINOP_RANGE) | |
364 | { | |
365 | LONGEST low, high; | |
366 | (*pos)++; | |
367 | low = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside)); | |
368 | high = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside)); | |
369 | if (low < low_bound || high > high_bound) | |
8a3fe4f8 | 370 | error (_("tuple range index out of range")); |
c5aa993b | 371 | for (index = low; index <= high; index++) |
c906108c | 372 | { |
990a07ab | 373 | memcpy (value_contents_raw (array) |
c906108c | 374 | + (index - low_bound) * element_size, |
0fd88904 | 375 | value_contents (element), element_size); |
c906108c SS |
376 | } |
377 | } | |
378 | else | |
379 | { | |
380 | index = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside)); | |
381 | if (index < low_bound || index > high_bound) | |
8a3fe4f8 | 382 | error (_("tuple index out of range")); |
990a07ab | 383 | memcpy (value_contents_raw (array) + (index - low_bound) * element_size, |
0fd88904 | 384 | value_contents (element), element_size); |
c906108c SS |
385 | } |
386 | return index; | |
387 | } | |
388 | ||
0b4e1325 WZ |
389 | struct value * |
390 | value_f90_subarray (struct value *array, | |
391 | struct expression *exp, int *pos, enum noside noside) | |
392 | { | |
393 | int pc = (*pos) + 1; | |
394 | LONGEST low_bound, high_bound; | |
395 | struct type *range = check_typedef (TYPE_INDEX_TYPE (value_type (array))); | |
396 | enum f90_range_type range_type = longest_to_int (exp->elts[pc].longconst); | |
397 | ||
398 | *pos += 3; | |
399 | ||
400 | if (range_type == LOW_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT) | |
401 | low_bound = TYPE_LOW_BOUND (range); | |
402 | else | |
403 | low_bound = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside)); | |
404 | ||
405 | if (range_type == HIGH_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT) | |
406 | high_bound = TYPE_HIGH_BOUND (range); | |
407 | else | |
408 | high_bound = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside)); | |
409 | ||
410 | return value_slice (array, low_bound, high_bound - low_bound + 1); | |
411 | } | |
412 | ||
61051030 | 413 | struct value * |
fba45db2 | 414 | evaluate_subexp_standard (struct type *expect_type, |
aa1ee363 | 415 | struct expression *exp, int *pos, |
fba45db2 | 416 | enum noside noside) |
c906108c SS |
417 | { |
418 | enum exp_opcode op; | |
419 | int tem, tem2, tem3; | |
52f0bd74 | 420 | int pc, pc2 = 0, oldpos; |
61051030 AC |
421 | struct value *arg1 = NULL; |
422 | struct value *arg2 = NULL; | |
423 | struct value *arg3; | |
c906108c SS |
424 | struct type *type; |
425 | int nargs; | |
61051030 | 426 | struct value **argvec; |
c5aa993b | 427 | int upper, lower, retcode; |
c906108c SS |
428 | int code; |
429 | int ix; | |
430 | long mem_offset; | |
c5aa993b | 431 | struct type **arg_types; |
c906108c SS |
432 | int save_pos1; |
433 | ||
c906108c SS |
434 | pc = (*pos)++; |
435 | op = exp->elts[pc].opcode; | |
436 | ||
437 | switch (op) | |
438 | { | |
439 | case OP_SCOPE: | |
440 | tem = longest_to_int (exp->elts[pc + 2].longconst); | |
441 | (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1); | |
0d5de010 DJ |
442 | if (noside == EVAL_SKIP) |
443 | goto nosideret; | |
79c2c32d DC |
444 | arg1 = value_aggregate_elt (exp->elts[pc + 1].type, |
445 | &exp->elts[pc + 3].string, | |
0d5de010 | 446 | 0, noside); |
c906108c | 447 | if (arg1 == NULL) |
8a3fe4f8 | 448 | error (_("There is no field named %s"), &exp->elts[pc + 3].string); |
c906108c SS |
449 | return arg1; |
450 | ||
451 | case OP_LONG: | |
452 | (*pos) += 3; | |
453 | return value_from_longest (exp->elts[pc + 1].type, | |
454 | exp->elts[pc + 2].longconst); | |
455 | ||
456 | case OP_DOUBLE: | |
457 | (*pos) += 3; | |
458 | return value_from_double (exp->elts[pc + 1].type, | |
459 | exp->elts[pc + 2].doubleconst); | |
460 | ||
461 | case OP_VAR_VALUE: | |
462 | (*pos) += 3; | |
463 | if (noside == EVAL_SKIP) | |
464 | goto nosideret; | |
c906108c | 465 | |
070ad9f0 DB |
466 | /* JYG: We used to just return value_zero of the symbol type |
467 | if we're asked to avoid side effects. Otherwise we return | |
468 | value_of_variable (...). However I'm not sure if | |
469 | value_of_variable () has any side effect. | |
470 | We need a full value object returned here for whatis_exp () | |
471 | to call evaluate_type () and then pass the full value to | |
472 | value_rtti_target_type () if we are dealing with a pointer | |
473 | or reference to a base class and print object is on. */ | |
c906108c | 474 | |
5e572bb4 DJ |
475 | { |
476 | volatile struct gdb_exception except; | |
477 | struct value *ret = NULL; | |
478 | ||
479 | TRY_CATCH (except, RETURN_MASK_ERROR) | |
480 | { | |
481 | ret = value_of_variable (exp->elts[pc + 2].symbol, | |
482 | exp->elts[pc + 1].block); | |
483 | } | |
484 | ||
485 | if (except.reason < 0) | |
486 | { | |
487 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
488 | ret = value_zero (SYMBOL_TYPE (exp->elts[pc + 2].symbol), not_lval); | |
489 | else | |
490 | throw_exception (except); | |
491 | } | |
492 | ||
493 | return ret; | |
494 | } | |
c906108c SS |
495 | |
496 | case OP_LAST: | |
497 | (*pos) += 2; | |
498 | return | |
499 | access_value_history (longest_to_int (exp->elts[pc + 1].longconst)); | |
500 | ||
501 | case OP_REGISTER: | |
502 | { | |
67f3407f DJ |
503 | const char *name = &exp->elts[pc + 2].string; |
504 | int regno; | |
123dc839 | 505 | struct value *val; |
67f3407f DJ |
506 | |
507 | (*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1); | |
508 | regno = frame_map_name_to_regnum (deprecated_safe_get_selected_frame (), | |
509 | name, strlen (name)); | |
510 | if (regno == -1) | |
511 | error (_("Register $%s not available."), name); | |
123dc839 DJ |
512 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
513 | val = value_zero (register_type (current_gdbarch, regno), not_lval); | |
514 | else | |
515 | val = value_of_register (regno, get_selected_frame (NULL)); | |
c906108c | 516 | if (val == NULL) |
67f3407f | 517 | error (_("Value of register %s not available."), name); |
c906108c SS |
518 | else |
519 | return val; | |
520 | } | |
521 | case OP_BOOL: | |
522 | (*pos) += 2; | |
523 | return value_from_longest (LA_BOOL_TYPE, | |
c5aa993b | 524 | exp->elts[pc + 1].longconst); |
c906108c SS |
525 | |
526 | case OP_INTERNALVAR: | |
527 | (*pos) += 2; | |
528 | return value_of_internalvar (exp->elts[pc + 1].internalvar); | |
529 | ||
530 | case OP_STRING: | |
531 | tem = longest_to_int (exp->elts[pc + 1].longconst); | |
532 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
533 | if (noside == EVAL_SKIP) | |
534 | goto nosideret; | |
535 | return value_string (&exp->elts[pc + 2].string, tem); | |
536 | ||
a9fa03de AF |
537 | case OP_OBJC_NSSTRING: /* Objective C Foundation Class NSString constant. */ |
538 | tem = longest_to_int (exp->elts[pc + 1].longconst); | |
539 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
540 | if (noside == EVAL_SKIP) | |
541 | { | |
542 | goto nosideret; | |
543 | } | |
544 | return (struct value *) value_nsstring (&exp->elts[pc + 2].string, tem + 1); | |
545 | ||
c906108c SS |
546 | case OP_BITSTRING: |
547 | tem = longest_to_int (exp->elts[pc + 1].longconst); | |
548 | (*pos) | |
549 | += 3 + BYTES_TO_EXP_ELEM ((tem + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT); | |
550 | if (noside == EVAL_SKIP) | |
551 | goto nosideret; | |
552 | return value_bitstring (&exp->elts[pc + 2].string, tem); | |
553 | break; | |
554 | ||
555 | case OP_ARRAY: | |
556 | (*pos) += 3; | |
557 | tem2 = longest_to_int (exp->elts[pc + 1].longconst); | |
558 | tem3 = longest_to_int (exp->elts[pc + 2].longconst); | |
559 | nargs = tem3 - tem2 + 1; | |
560 | type = expect_type ? check_typedef (expect_type) : NULL_TYPE; | |
561 | ||
562 | if (expect_type != NULL_TYPE && noside != EVAL_SKIP | |
563 | && TYPE_CODE (type) == TYPE_CODE_STRUCT) | |
564 | { | |
61051030 | 565 | struct value *rec = allocate_value (expect_type); |
990a07ab | 566 | memset (value_contents_raw (rec), '\0', TYPE_LENGTH (type)); |
c906108c SS |
567 | return evaluate_struct_tuple (rec, exp, pos, noside, nargs); |
568 | } | |
569 | ||
570 | if (expect_type != NULL_TYPE && noside != EVAL_SKIP | |
571 | && TYPE_CODE (type) == TYPE_CODE_ARRAY) | |
572 | { | |
573 | struct type *range_type = TYPE_FIELD_TYPE (type, 0); | |
574 | struct type *element_type = TYPE_TARGET_TYPE (type); | |
61051030 | 575 | struct value *array = allocate_value (expect_type); |
c906108c SS |
576 | int element_size = TYPE_LENGTH (check_typedef (element_type)); |
577 | LONGEST low_bound, high_bound, index; | |
578 | if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0) | |
579 | { | |
580 | low_bound = 0; | |
581 | high_bound = (TYPE_LENGTH (type) / element_size) - 1; | |
582 | } | |
583 | index = low_bound; | |
990a07ab | 584 | memset (value_contents_raw (array), 0, TYPE_LENGTH (expect_type)); |
c5aa993b | 585 | for (tem = nargs; --nargs >= 0;) |
c906108c | 586 | { |
61051030 | 587 | struct value *element; |
c906108c SS |
588 | int index_pc = 0; |
589 | if (exp->elts[*pos].opcode == BINOP_RANGE) | |
590 | { | |
591 | index_pc = ++(*pos); | |
592 | evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP); | |
593 | } | |
594 | element = evaluate_subexp (element_type, exp, pos, noside); | |
df407dfe | 595 | if (value_type (element) != element_type) |
c906108c SS |
596 | element = value_cast (element_type, element); |
597 | if (index_pc) | |
598 | { | |
599 | int continue_pc = *pos; | |
600 | *pos = index_pc; | |
601 | index = init_array_element (array, element, exp, pos, noside, | |
602 | low_bound, high_bound); | |
603 | *pos = continue_pc; | |
604 | } | |
605 | else | |
606 | { | |
607 | if (index > high_bound) | |
608 | /* to avoid memory corruption */ | |
8a3fe4f8 | 609 | error (_("Too many array elements")); |
990a07ab | 610 | memcpy (value_contents_raw (array) |
c906108c | 611 | + (index - low_bound) * element_size, |
0fd88904 | 612 | value_contents (element), |
c906108c SS |
613 | element_size); |
614 | } | |
615 | index++; | |
616 | } | |
617 | return array; | |
618 | } | |
619 | ||
620 | if (expect_type != NULL_TYPE && noside != EVAL_SKIP | |
621 | && TYPE_CODE (type) == TYPE_CODE_SET) | |
622 | { | |
61051030 | 623 | struct value *set = allocate_value (expect_type); |
47b667de | 624 | gdb_byte *valaddr = value_contents_raw (set); |
c906108c SS |
625 | struct type *element_type = TYPE_INDEX_TYPE (type); |
626 | struct type *check_type = element_type; | |
627 | LONGEST low_bound, high_bound; | |
628 | ||
629 | /* get targettype of elementtype */ | |
630 | while (TYPE_CODE (check_type) == TYPE_CODE_RANGE || | |
631 | TYPE_CODE (check_type) == TYPE_CODE_TYPEDEF) | |
632 | check_type = TYPE_TARGET_TYPE (check_type); | |
633 | ||
634 | if (get_discrete_bounds (element_type, &low_bound, &high_bound) < 0) | |
8a3fe4f8 | 635 | error (_("(power)set type with unknown size")); |
c906108c SS |
636 | memset (valaddr, '\0', TYPE_LENGTH (type)); |
637 | for (tem = 0; tem < nargs; tem++) | |
638 | { | |
639 | LONGEST range_low, range_high; | |
640 | struct type *range_low_type, *range_high_type; | |
61051030 | 641 | struct value *elem_val; |
c906108c SS |
642 | if (exp->elts[*pos].opcode == BINOP_RANGE) |
643 | { | |
644 | (*pos)++; | |
645 | elem_val = evaluate_subexp (element_type, exp, pos, noside); | |
df407dfe | 646 | range_low_type = value_type (elem_val); |
c906108c SS |
647 | range_low = value_as_long (elem_val); |
648 | elem_val = evaluate_subexp (element_type, exp, pos, noside); | |
df407dfe | 649 | range_high_type = value_type (elem_val); |
c906108c SS |
650 | range_high = value_as_long (elem_val); |
651 | } | |
652 | else | |
653 | { | |
654 | elem_val = evaluate_subexp (element_type, exp, pos, noside); | |
df407dfe | 655 | range_low_type = range_high_type = value_type (elem_val); |
c906108c SS |
656 | range_low = range_high = value_as_long (elem_val); |
657 | } | |
658 | /* check types of elements to avoid mixture of elements from | |
c5aa993b JM |
659 | different types. Also check if type of element is "compatible" |
660 | with element type of powerset */ | |
c906108c SS |
661 | if (TYPE_CODE (range_low_type) == TYPE_CODE_RANGE) |
662 | range_low_type = TYPE_TARGET_TYPE (range_low_type); | |
663 | if (TYPE_CODE (range_high_type) == TYPE_CODE_RANGE) | |
664 | range_high_type = TYPE_TARGET_TYPE (range_high_type); | |
665 | if ((TYPE_CODE (range_low_type) != TYPE_CODE (range_high_type)) || | |
666 | (TYPE_CODE (range_low_type) == TYPE_CODE_ENUM && | |
667 | (range_low_type != range_high_type))) | |
668 | /* different element modes */ | |
8a3fe4f8 | 669 | error (_("POWERSET tuple elements of different mode")); |
c906108c SS |
670 | if ((TYPE_CODE (check_type) != TYPE_CODE (range_low_type)) || |
671 | (TYPE_CODE (check_type) == TYPE_CODE_ENUM && | |
672 | range_low_type != check_type)) | |
8a3fe4f8 | 673 | error (_("incompatible POWERSET tuple elements")); |
c906108c SS |
674 | if (range_low > range_high) |
675 | { | |
8a3fe4f8 | 676 | warning (_("empty POWERSET tuple range")); |
c906108c SS |
677 | continue; |
678 | } | |
679 | if (range_low < low_bound || range_high > high_bound) | |
8a3fe4f8 | 680 | error (_("POWERSET tuple element out of range")); |
c906108c SS |
681 | range_low -= low_bound; |
682 | range_high -= low_bound; | |
c5aa993b | 683 | for (; range_low <= range_high; range_low++) |
c906108c SS |
684 | { |
685 | int bit_index = (unsigned) range_low % TARGET_CHAR_BIT; | |
686 | if (BITS_BIG_ENDIAN) | |
687 | bit_index = TARGET_CHAR_BIT - 1 - bit_index; | |
c5aa993b | 688 | valaddr[(unsigned) range_low / TARGET_CHAR_BIT] |
c906108c SS |
689 | |= 1 << bit_index; |
690 | } | |
691 | } | |
692 | return set; | |
693 | } | |
694 | ||
f976f6d4 | 695 | argvec = (struct value **) alloca (sizeof (struct value *) * nargs); |
c906108c SS |
696 | for (tem = 0; tem < nargs; tem++) |
697 | { | |
698 | /* Ensure that array expressions are coerced into pointer objects. */ | |
699 | argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside); | |
700 | } | |
701 | if (noside == EVAL_SKIP) | |
702 | goto nosideret; | |
703 | return value_array (tem2, tem3, argvec); | |
704 | ||
705 | case TERNOP_SLICE: | |
706 | { | |
61051030 | 707 | struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
c906108c | 708 | int lowbound |
c5aa993b | 709 | = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside)); |
c906108c | 710 | int upper |
c5aa993b | 711 | = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside)); |
c906108c SS |
712 | if (noside == EVAL_SKIP) |
713 | goto nosideret; | |
714 | return value_slice (array, lowbound, upper - lowbound + 1); | |
715 | } | |
716 | ||
717 | case TERNOP_SLICE_COUNT: | |
718 | { | |
61051030 | 719 | struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
c906108c | 720 | int lowbound |
c5aa993b | 721 | = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside)); |
c906108c | 722 | int length |
c5aa993b | 723 | = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside)); |
c906108c SS |
724 | return value_slice (array, lowbound, length); |
725 | } | |
726 | ||
727 | case TERNOP_COND: | |
728 | /* Skip third and second args to evaluate the first one. */ | |
729 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
730 | if (value_logical_not (arg1)) | |
731 | { | |
732 | evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP); | |
733 | return evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
734 | } | |
735 | else | |
736 | { | |
737 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
738 | evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP); | |
739 | return arg2; | |
740 | } | |
741 | ||
a9fa03de AF |
742 | case OP_OBJC_SELECTOR: |
743 | { /* Objective C @selector operator. */ | |
744 | char *sel = &exp->elts[pc + 2].string; | |
745 | int len = longest_to_int (exp->elts[pc + 1].longconst); | |
746 | ||
747 | (*pos) += 3 + BYTES_TO_EXP_ELEM (len + 1); | |
748 | if (noside == EVAL_SKIP) | |
749 | goto nosideret; | |
750 | ||
751 | if (sel[len] != 0) | |
752 | sel[len] = 0; /* Make sure it's terminated. */ | |
753 | return value_from_longest (lookup_pointer_type (builtin_type_void), | |
754 | lookup_child_selector (sel)); | |
755 | } | |
756 | ||
757 | case OP_OBJC_MSGCALL: | |
758 | { /* Objective C message (method) call. */ | |
759 | ||
c253954e JB |
760 | static CORE_ADDR responds_selector = 0; |
761 | static CORE_ADDR method_selector = 0; | |
a9fa03de | 762 | |
c253954e | 763 | CORE_ADDR selector = 0; |
a9fa03de AF |
764 | |
765 | int using_gcc = 0; | |
766 | int struct_return = 0; | |
767 | int sub_no_side = 0; | |
768 | ||
769 | static struct value *msg_send = NULL; | |
770 | static struct value *msg_send_stret = NULL; | |
771 | static int gnu_runtime = 0; | |
772 | ||
773 | struct value *target = NULL; | |
774 | struct value *method = NULL; | |
775 | struct value *called_method = NULL; | |
776 | ||
777 | struct type *selector_type = NULL; | |
778 | ||
779 | struct value *ret = NULL; | |
780 | CORE_ADDR addr = 0; | |
781 | ||
782 | selector = exp->elts[pc + 1].longconst; | |
783 | nargs = exp->elts[pc + 2].longconst; | |
784 | argvec = (struct value **) alloca (sizeof (struct value *) | |
785 | * (nargs + 5)); | |
786 | ||
787 | (*pos) += 3; | |
788 | ||
789 | selector_type = lookup_pointer_type (builtin_type_void); | |
790 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
791 | sub_no_side = EVAL_NORMAL; | |
792 | else | |
793 | sub_no_side = noside; | |
794 | ||
795 | target = evaluate_subexp (selector_type, exp, pos, sub_no_side); | |
796 | ||
797 | if (value_as_long (target) == 0) | |
798 | return value_from_longest (builtin_type_long, 0); | |
799 | ||
800 | if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0)) | |
801 | gnu_runtime = 1; | |
802 | ||
803 | /* Find the method dispatch (Apple runtime) or method lookup | |
804 | (GNU runtime) function for Objective-C. These will be used | |
805 | to lookup the symbol information for the method. If we | |
806 | can't find any symbol information, then we'll use these to | |
807 | call the method, otherwise we can call the method | |
808 | directly. The msg_send_stret function is used in the special | |
809 | case of a method that returns a structure (Apple runtime | |
810 | only). */ | |
811 | if (gnu_runtime) | |
812 | { | |
c253954e JB |
813 | struct type *type; |
814 | type = lookup_pointer_type (builtin_type_void); | |
815 | type = lookup_function_type (type); | |
816 | type = lookup_pointer_type (type); | |
817 | type = lookup_function_type (type); | |
818 | type = lookup_pointer_type (type); | |
819 | ||
a9fa03de AF |
820 | msg_send = find_function_in_inferior ("objc_msg_lookup"); |
821 | msg_send_stret = find_function_in_inferior ("objc_msg_lookup"); | |
c253954e JB |
822 | |
823 | msg_send = value_from_pointer (type, value_as_address (msg_send)); | |
824 | msg_send_stret = value_from_pointer (type, | |
825 | value_as_address (msg_send_stret)); | |
a9fa03de AF |
826 | } |
827 | else | |
828 | { | |
829 | msg_send = find_function_in_inferior ("objc_msgSend"); | |
830 | /* Special dispatcher for methods returning structs */ | |
831 | msg_send_stret = find_function_in_inferior ("objc_msgSend_stret"); | |
832 | } | |
833 | ||
834 | /* Verify the target object responds to this method. The | |
835 | standard top-level 'Object' class uses a different name for | |
836 | the verification method than the non-standard, but more | |
837 | often used, 'NSObject' class. Make sure we check for both. */ | |
838 | ||
839 | responds_selector = lookup_child_selector ("respondsToSelector:"); | |
840 | if (responds_selector == 0) | |
841 | responds_selector = lookup_child_selector ("respondsTo:"); | |
842 | ||
843 | if (responds_selector == 0) | |
8a3fe4f8 | 844 | error (_("no 'respondsTo:' or 'respondsToSelector:' method")); |
a9fa03de AF |
845 | |
846 | method_selector = lookup_child_selector ("methodForSelector:"); | |
847 | if (method_selector == 0) | |
848 | method_selector = lookup_child_selector ("methodFor:"); | |
849 | ||
850 | if (method_selector == 0) | |
8a3fe4f8 | 851 | error (_("no 'methodFor:' or 'methodForSelector:' method")); |
a9fa03de AF |
852 | |
853 | /* Call the verification method, to make sure that the target | |
854 | class implements the desired method. */ | |
855 | ||
856 | argvec[0] = msg_send; | |
857 | argvec[1] = target; | |
858 | argvec[2] = value_from_longest (builtin_type_long, responds_selector); | |
859 | argvec[3] = value_from_longest (builtin_type_long, selector); | |
860 | argvec[4] = 0; | |
861 | ||
862 | ret = call_function_by_hand (argvec[0], 3, argvec + 1); | |
863 | if (gnu_runtime) | |
864 | { | |
865 | /* Function objc_msg_lookup returns a pointer. */ | |
866 | argvec[0] = ret; | |
867 | ret = call_function_by_hand (argvec[0], 3, argvec + 1); | |
868 | } | |
869 | if (value_as_long (ret) == 0) | |
8a3fe4f8 | 870 | error (_("Target does not respond to this message selector.")); |
a9fa03de AF |
871 | |
872 | /* Call "methodForSelector:" method, to get the address of a | |
873 | function method that implements this selector for this | |
874 | class. If we can find a symbol at that address, then we | |
875 | know the return type, parameter types etc. (that's a good | |
876 | thing). */ | |
877 | ||
878 | argvec[0] = msg_send; | |
879 | argvec[1] = target; | |
880 | argvec[2] = value_from_longest (builtin_type_long, method_selector); | |
881 | argvec[3] = value_from_longest (builtin_type_long, selector); | |
882 | argvec[4] = 0; | |
883 | ||
884 | ret = call_function_by_hand (argvec[0], 3, argvec + 1); | |
885 | if (gnu_runtime) | |
886 | { | |
887 | argvec[0] = ret; | |
888 | ret = call_function_by_hand (argvec[0], 3, argvec + 1); | |
889 | } | |
890 | ||
891 | /* ret should now be the selector. */ | |
892 | ||
893 | addr = value_as_long (ret); | |
894 | if (addr) | |
895 | { | |
896 | struct symbol *sym = NULL; | |
897 | /* Is it a high_level symbol? */ | |
898 | ||
899 | sym = find_pc_function (addr); | |
900 | if (sym != NULL) | |
901 | method = value_of_variable (sym, 0); | |
902 | } | |
903 | ||
904 | /* If we found a method with symbol information, check to see | |
905 | if it returns a struct. Otherwise assume it doesn't. */ | |
906 | ||
907 | if (method) | |
908 | { | |
909 | struct block *b; | |
910 | CORE_ADDR funaddr; | |
911 | struct type *value_type; | |
912 | ||
913 | funaddr = find_function_addr (method, &value_type); | |
914 | ||
915 | b = block_for_pc (funaddr); | |
916 | ||
917 | /* If compiled without -g, assume GCC 2. */ | |
918 | using_gcc = (b == NULL ? 2 : BLOCK_GCC_COMPILED (b)); | |
919 | ||
920 | CHECK_TYPEDEF (value_type); | |
921 | ||
922 | if ((value_type == NULL) | |
923 | || (TYPE_CODE(value_type) == TYPE_CODE_ERROR)) | |
924 | { | |
925 | if (expect_type != NULL) | |
926 | value_type = expect_type; | |
927 | } | |
928 | ||
48436ce6 | 929 | struct_return = using_struct_return (value_type, using_gcc); |
a9fa03de AF |
930 | } |
931 | else if (expect_type != NULL) | |
932 | { | |
48436ce6 | 933 | struct_return = using_struct_return (check_typedef (expect_type), using_gcc); |
a9fa03de AF |
934 | } |
935 | ||
936 | /* Found a function symbol. Now we will substitute its | |
937 | value in place of the message dispatcher (obj_msgSend), | |
938 | so that we call the method directly instead of thru | |
939 | the dispatcher. The main reason for doing this is that | |
940 | we can now evaluate the return value and parameter values | |
941 | according to their known data types, in case we need to | |
942 | do things like promotion, dereferencing, special handling | |
943 | of structs and doubles, etc. | |
944 | ||
945 | We want to use the type signature of 'method', but still | |
946 | jump to objc_msgSend() or objc_msgSend_stret() to better | |
947 | mimic the behavior of the runtime. */ | |
948 | ||
949 | if (method) | |
950 | { | |
df407dfe | 951 | if (TYPE_CODE (value_type (method)) != TYPE_CODE_FUNC) |
8a3fe4f8 | 952 | error (_("method address has symbol information with non-function type; skipping")); |
a9fa03de AF |
953 | if (struct_return) |
954 | VALUE_ADDRESS (method) = value_as_address (msg_send_stret); | |
955 | else | |
956 | VALUE_ADDRESS (method) = value_as_address (msg_send); | |
957 | called_method = method; | |
958 | } | |
959 | else | |
960 | { | |
961 | if (struct_return) | |
962 | called_method = msg_send_stret; | |
963 | else | |
964 | called_method = msg_send; | |
965 | } | |
966 | ||
967 | if (noside == EVAL_SKIP) | |
968 | goto nosideret; | |
969 | ||
970 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
971 | { | |
972 | /* If the return type doesn't look like a function type, | |
973 | call an error. This can happen if somebody tries to | |
974 | turn a variable into a function call. This is here | |
975 | because people often want to call, eg, strcmp, which | |
976 | gdb doesn't know is a function. If gdb isn't asked for | |
977 | it's opinion (ie. through "whatis"), it won't offer | |
978 | it. */ | |
979 | ||
df407dfe | 980 | struct type *type = value_type (called_method); |
a9fa03de AF |
981 | if (type && TYPE_CODE (type) == TYPE_CODE_PTR) |
982 | type = TYPE_TARGET_TYPE (type); | |
983 | type = TYPE_TARGET_TYPE (type); | |
984 | ||
985 | if (type) | |
986 | { | |
987 | if ((TYPE_CODE (type) == TYPE_CODE_ERROR) && expect_type) | |
988 | return allocate_value (expect_type); | |
989 | else | |
990 | return allocate_value (type); | |
991 | } | |
992 | else | |
8a3fe4f8 | 993 | error (_("Expression of type other than \"method returning ...\" used as a method")); |
a9fa03de AF |
994 | } |
995 | ||
996 | /* Now depending on whether we found a symbol for the method, | |
997 | we will either call the runtime dispatcher or the method | |
998 | directly. */ | |
999 | ||
1000 | argvec[0] = called_method; | |
1001 | argvec[1] = target; | |
1002 | argvec[2] = value_from_longest (builtin_type_long, selector); | |
1003 | /* User-supplied arguments. */ | |
1004 | for (tem = 0; tem < nargs; tem++) | |
1005 | argvec[tem + 3] = evaluate_subexp_with_coercion (exp, pos, noside); | |
1006 | argvec[tem + 3] = 0; | |
1007 | ||
1008 | if (gnu_runtime && (method != NULL)) | |
1009 | { | |
a9fa03de | 1010 | /* Function objc_msg_lookup returns a pointer. */ |
04624583 AC |
1011 | deprecated_set_value_type (argvec[0], |
1012 | lookup_function_type (lookup_pointer_type (value_type (argvec[0])))); | |
c253954e | 1013 | argvec[0] = call_function_by_hand (argvec[0], nargs + 2, argvec + 1); |
a9fa03de | 1014 | } |
a9fa03de | 1015 | |
c253954e | 1016 | ret = call_function_by_hand (argvec[0], nargs + 2, argvec + 1); |
a9fa03de AF |
1017 | return ret; |
1018 | } | |
1019 | break; | |
1020 | ||
c906108c SS |
1021 | case OP_FUNCALL: |
1022 | (*pos) += 2; | |
1023 | op = exp->elts[*pos].opcode; | |
1024 | nargs = longest_to_int (exp->elts[pc + 1].longconst); | |
1025 | /* Allocate arg vector, including space for the function to be | |
c5aa993b | 1026 | called in argvec[0] and a terminating NULL */ |
f976f6d4 | 1027 | argvec = (struct value **) alloca (sizeof (struct value *) * (nargs + 3)); |
c906108c SS |
1028 | if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR) |
1029 | { | |
c906108c SS |
1030 | nargs++; |
1031 | /* First, evaluate the structure into arg2 */ | |
1032 | pc2 = (*pos)++; | |
1033 | ||
1034 | if (noside == EVAL_SKIP) | |
1035 | goto nosideret; | |
1036 | ||
1037 | if (op == STRUCTOP_MEMBER) | |
1038 | { | |
1039 | arg2 = evaluate_subexp_for_address (exp, pos, noside); | |
1040 | } | |
1041 | else | |
1042 | { | |
1043 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1044 | } | |
1045 | ||
1046 | /* If the function is a virtual function, then the | |
1047 | aggregate value (providing the structure) plays | |
1048 | its part by providing the vtable. Otherwise, | |
1049 | it is just along for the ride: call the function | |
1050 | directly. */ | |
1051 | ||
1052 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1053 | ||
0d5de010 DJ |
1054 | if (TYPE_CODE (check_typedef (value_type (arg1))) |
1055 | != TYPE_CODE_METHODPTR) | |
1056 | error (_("Non-pointer-to-member value used in pointer-to-member " | |
1057 | "construct")); | |
c906108c | 1058 | |
0d5de010 | 1059 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
c906108c | 1060 | { |
0d5de010 DJ |
1061 | struct type *method_type = check_typedef (value_type (arg1)); |
1062 | arg1 = value_zero (method_type, not_lval); | |
c906108c SS |
1063 | } |
1064 | else | |
0d5de010 | 1065 | arg1 = cplus_method_ptr_to_value (&arg2, arg1); |
c906108c SS |
1066 | |
1067 | /* Now, say which argument to start evaluating from */ | |
1068 | tem = 2; | |
1069 | } | |
1070 | else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR) | |
1071 | { | |
1072 | /* Hair for method invocations */ | |
1073 | int tem2; | |
1074 | ||
1075 | nargs++; | |
1076 | /* First, evaluate the structure into arg2 */ | |
1077 | pc2 = (*pos)++; | |
1078 | tem2 = longest_to_int (exp->elts[pc2 + 1].longconst); | |
1079 | *pos += 3 + BYTES_TO_EXP_ELEM (tem2 + 1); | |
1080 | if (noside == EVAL_SKIP) | |
1081 | goto nosideret; | |
1082 | ||
1083 | if (op == STRUCTOP_STRUCT) | |
1084 | { | |
1085 | /* If v is a variable in a register, and the user types | |
c5aa993b JM |
1086 | v.method (), this will produce an error, because v has |
1087 | no address. | |
1088 | ||
1089 | A possible way around this would be to allocate a | |
1090 | copy of the variable on the stack, copy in the | |
1091 | contents, call the function, and copy out the | |
1092 | contents. I.e. convert this from call by reference | |
1093 | to call by copy-return (or whatever it's called). | |
1094 | However, this does not work because it is not the | |
1095 | same: the method being called could stash a copy of | |
1096 | the address, and then future uses through that address | |
1097 | (after the method returns) would be expected to | |
1098 | use the variable itself, not some copy of it. */ | |
c906108c SS |
1099 | arg2 = evaluate_subexp_for_address (exp, pos, noside); |
1100 | } | |
1101 | else | |
1102 | { | |
1103 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1104 | } | |
1105 | /* Now, say which argument to start evaluating from */ | |
1106 | tem = 2; | |
1107 | } | |
1108 | else | |
1109 | { | |
1110 | /* Non-method function call */ | |
1111 | save_pos1 = *pos; | |
1112 | argvec[0] = evaluate_subexp_with_coercion (exp, pos, noside); | |
1113 | tem = 1; | |
df407dfe | 1114 | type = value_type (argvec[0]); |
c906108c SS |
1115 | if (type && TYPE_CODE (type) == TYPE_CODE_PTR) |
1116 | type = TYPE_TARGET_TYPE (type); | |
1117 | if (type && TYPE_CODE (type) == TYPE_CODE_FUNC) | |
1118 | { | |
1119 | for (; tem <= nargs && tem <= TYPE_NFIELDS (type); tem++) | |
1120 | { | |
c5aa993b JM |
1121 | /* pai: FIXME This seems to be coercing arguments before |
1122 | * overload resolution has been done! */ | |
1123 | argvec[tem] = evaluate_subexp (TYPE_FIELD_TYPE (type, tem - 1), | |
c906108c SS |
1124 | exp, pos, noside); |
1125 | } | |
1126 | } | |
1127 | } | |
1128 | ||
1129 | /* Evaluate arguments */ | |
1130 | for (; tem <= nargs; tem++) | |
1131 | { | |
1132 | /* Ensure that array expressions are coerced into pointer objects. */ | |
1133 | argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside); | |
1134 | } | |
1135 | ||
1136 | /* signal end of arglist */ | |
1137 | argvec[tem] = 0; | |
1138 | ||
1139 | if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR) | |
1140 | { | |
1141 | int static_memfuncp; | |
c906108c | 1142 | char tstr[256]; |
c5aa993b JM |
1143 | |
1144 | /* Method invocation : stuff "this" as first parameter */ | |
9b013045 | 1145 | argvec[1] = arg2; |
c5aa993b JM |
1146 | /* Name of method from expression */ |
1147 | strcpy (tstr, &exp->elts[pc2 + 2].string); | |
1148 | ||
1149 | if (overload_resolution && (exp->language_defn->la_language == language_cplus)) | |
1150 | { | |
1151 | /* Language is C++, do some overload resolution before evaluation */ | |
61051030 | 1152 | struct value *valp = NULL; |
c5aa993b JM |
1153 | |
1154 | /* Prepare list of argument types for overload resolution */ | |
c2636352 | 1155 | arg_types = (struct type **) alloca (nargs * (sizeof (struct type *))); |
c5aa993b | 1156 | for (ix = 1; ix <= nargs; ix++) |
df407dfe | 1157 | arg_types[ix - 1] = value_type (argvec[ix]); |
c5aa993b JM |
1158 | |
1159 | (void) find_overload_match (arg_types, nargs, tstr, | |
1160 | 1 /* method */ , 0 /* strict match */ , | |
7f8c9282 | 1161 | &arg2 /* the object */ , NULL, |
c5aa993b JM |
1162 | &valp, NULL, &static_memfuncp); |
1163 | ||
1164 | ||
1165 | argvec[1] = arg2; /* the ``this'' pointer */ | |
1166 | argvec[0] = valp; /* use the method found after overload resolution */ | |
1167 | } | |
1168 | else | |
1169 | /* Non-C++ case -- or no overload resolution */ | |
1170 | { | |
9b013045 | 1171 | struct value *temp = arg2; |
c5aa993b JM |
1172 | argvec[0] = value_struct_elt (&temp, argvec + 1, tstr, |
1173 | &static_memfuncp, | |
1174 | op == STRUCTOP_STRUCT | |
1175 | ? "structure" : "structure pointer"); | |
9b013045 PS |
1176 | /* value_struct_elt updates temp with the correct value |
1177 | of the ``this'' pointer if necessary, so modify argvec[1] to | |
1178 | reflect any ``this'' changes. */ | |
df407dfe AC |
1179 | arg2 = value_from_longest (lookup_pointer_type(value_type (temp)), |
1180 | VALUE_ADDRESS (temp) + value_offset (temp) | |
13c3b5f5 | 1181 | + value_embedded_offset (temp)); |
c5aa993b JM |
1182 | argvec[1] = arg2; /* the ``this'' pointer */ |
1183 | } | |
c906108c SS |
1184 | |
1185 | if (static_memfuncp) | |
1186 | { | |
1187 | argvec[1] = argvec[0]; | |
1188 | nargs--; | |
1189 | argvec++; | |
1190 | } | |
1191 | } | |
1192 | else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR) | |
1193 | { | |
1194 | argvec[1] = arg2; | |
1195 | argvec[0] = arg1; | |
1196 | } | |
917317f4 | 1197 | else if (op == OP_VAR_VALUE) |
c5aa993b | 1198 | { |
c906108c | 1199 | /* Non-member function being called */ |
917317f4 JM |
1200 | /* fn: This can only be done for C++ functions. A C-style function |
1201 | in a C++ program, for instance, does not have the fields that | |
1202 | are expected here */ | |
c906108c | 1203 | |
c5aa993b JM |
1204 | if (overload_resolution && (exp->language_defn->la_language == language_cplus)) |
1205 | { | |
1206 | /* Language is C++, do some overload resolution before evaluation */ | |
1207 | struct symbol *symp; | |
1208 | ||
1209 | /* Prepare list of argument types for overload resolution */ | |
c2636352 | 1210 | arg_types = (struct type **) alloca (nargs * (sizeof (struct type *))); |
c5aa993b | 1211 | for (ix = 1; ix <= nargs; ix++) |
df407dfe | 1212 | arg_types[ix - 1] = value_type (argvec[ix]); |
c5aa993b JM |
1213 | |
1214 | (void) find_overload_match (arg_types, nargs, NULL /* no need for name */ , | |
1215 | 0 /* not method */ , 0 /* strict match */ , | |
917317f4 | 1216 | NULL, exp->elts[save_pos1+2].symbol /* the function */ , |
c5aa993b JM |
1217 | NULL, &symp, NULL); |
1218 | ||
1219 | /* Now fix the expression being evaluated */ | |
917317f4 | 1220 | exp->elts[save_pos1+2].symbol = symp; |
c5aa993b JM |
1221 | argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1, noside); |
1222 | } | |
1223 | else | |
1224 | { | |
1225 | /* Not C++, or no overload resolution allowed */ | |
1226 | /* nothing to be done; argvec already correctly set up */ | |
1227 | } | |
1228 | } | |
917317f4 JM |
1229 | else |
1230 | { | |
1231 | /* It is probably a C-style function */ | |
1232 | /* nothing to be done; argvec already correctly set up */ | |
1233 | } | |
c906108c SS |
1234 | |
1235 | do_call_it: | |
1236 | ||
1237 | if (noside == EVAL_SKIP) | |
1238 | goto nosideret; | |
0478d61c | 1239 | if (argvec[0] == NULL) |
8a3fe4f8 | 1240 | error (_("Cannot evaluate function -- may be inlined")); |
c906108c SS |
1241 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
1242 | { | |
1243 | /* If the return type doesn't look like a function type, call an | |
1244 | error. This can happen if somebody tries to turn a variable into | |
1245 | a function call. This is here because people often want to | |
1246 | call, eg, strcmp, which gdb doesn't know is a function. If | |
1247 | gdb isn't asked for it's opinion (ie. through "whatis"), | |
1248 | it won't offer it. */ | |
1249 | ||
1250 | struct type *ftype = | |
df407dfe | 1251 | TYPE_TARGET_TYPE (value_type (argvec[0])); |
c906108c SS |
1252 | |
1253 | if (ftype) | |
df407dfe | 1254 | return allocate_value (TYPE_TARGET_TYPE (value_type (argvec[0]))); |
c906108c | 1255 | else |
8a3fe4f8 | 1256 | error (_("Expression of type other than \"Function returning ...\" used as function")); |
c906108c | 1257 | } |
c906108c SS |
1258 | return call_function_by_hand (argvec[0], nargs, argvec + 1); |
1259 | /* pai: FIXME save value from call_function_by_hand, then adjust pc by adjust_fn_pc if +ve */ | |
1260 | ||
c5aa993b | 1261 | case OP_F77_UNDETERMINED_ARGLIST: |
c906108c SS |
1262 | |
1263 | /* Remember that in F77, functions, substring ops and | |
1264 | array subscript operations cannot be disambiguated | |
1265 | at parse time. We have made all array subscript operations, | |
1266 | substring operations as well as function calls come here | |
1267 | and we now have to discover what the heck this thing actually was. | |
c5aa993b | 1268 | If it is a function, we process just as if we got an OP_FUNCALL. */ |
c906108c | 1269 | |
c5aa993b | 1270 | nargs = longest_to_int (exp->elts[pc + 1].longconst); |
c906108c SS |
1271 | (*pos) += 2; |
1272 | ||
c5aa993b | 1273 | /* First determine the type code we are dealing with. */ |
c906108c | 1274 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
df407dfe | 1275 | type = check_typedef (value_type (arg1)); |
c906108c SS |
1276 | code = TYPE_CODE (type); |
1277 | ||
df0ca547 WZ |
1278 | if (code == TYPE_CODE_PTR) |
1279 | { | |
1280 | /* Fortran always passes variable to subroutines as pointer. | |
1281 | So we need to look into its target type to see if it is | |
1282 | array, string or function. If it is, we need to switch | |
1283 | to the target value the original one points to. */ | |
1284 | struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type)); | |
1285 | ||
1286 | if (TYPE_CODE (target_type) == TYPE_CODE_ARRAY | |
1287 | || TYPE_CODE (target_type) == TYPE_CODE_STRING | |
1288 | || TYPE_CODE (target_type) == TYPE_CODE_FUNC) | |
1289 | { | |
1290 | arg1 = value_ind (arg1); | |
1291 | type = check_typedef (value_type (arg1)); | |
1292 | code = TYPE_CODE (type); | |
1293 | } | |
1294 | } | |
1295 | ||
c5aa993b | 1296 | switch (code) |
c906108c SS |
1297 | { |
1298 | case TYPE_CODE_ARRAY: | |
0b4e1325 WZ |
1299 | if (exp->elts[*pos].opcode == OP_F90_RANGE) |
1300 | return value_f90_subarray (arg1, exp, pos, noside); | |
1301 | else | |
1302 | goto multi_f77_subscript; | |
c906108c SS |
1303 | |
1304 | case TYPE_CODE_STRING: | |
0b4e1325 WZ |
1305 | if (exp->elts[*pos].opcode == OP_F90_RANGE) |
1306 | return value_f90_subarray (arg1, exp, pos, noside); | |
1307 | else | |
1308 | { | |
1309 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
1310 | return value_subscript (arg1, arg2); | |
1311 | } | |
c906108c SS |
1312 | |
1313 | case TYPE_CODE_PTR: | |
1314 | case TYPE_CODE_FUNC: | |
1315 | /* It's a function call. */ | |
1316 | /* Allocate arg vector, including space for the function to be | |
1317 | called in argvec[0] and a terminating NULL */ | |
f976f6d4 | 1318 | argvec = (struct value **) alloca (sizeof (struct value *) * (nargs + 2)); |
c906108c SS |
1319 | argvec[0] = arg1; |
1320 | tem = 1; | |
1321 | for (; tem <= nargs; tem++) | |
1322 | argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside); | |
c5aa993b | 1323 | argvec[tem] = 0; /* signal end of arglist */ |
c906108c SS |
1324 | goto do_call_it; |
1325 | ||
1326 | default: | |
8a3fe4f8 | 1327 | error (_("Cannot perform substring on this type")); |
c906108c SS |
1328 | } |
1329 | ||
c906108c SS |
1330 | case OP_COMPLEX: |
1331 | /* We have a complex number, There should be 2 floating | |
c5aa993b | 1332 | point numbers that compose it */ |
c906108c | 1333 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
c5aa993b | 1334 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
c906108c SS |
1335 | |
1336 | return value_literal_complex (arg1, arg2, builtin_type_f_complex_s16); | |
1337 | ||
1338 | case STRUCTOP_STRUCT: | |
1339 | tem = longest_to_int (exp->elts[pc + 1].longconst); | |
1340 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
1341 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1342 | if (noside == EVAL_SKIP) | |
1343 | goto nosideret; | |
1344 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
df407dfe | 1345 | return value_zero (lookup_struct_elt_type (value_type (arg1), |
c906108c SS |
1346 | &exp->elts[pc + 2].string, |
1347 | 0), | |
1348 | lval_memory); | |
1349 | else | |
1350 | { | |
61051030 | 1351 | struct value *temp = arg1; |
c906108c SS |
1352 | return value_struct_elt (&temp, NULL, &exp->elts[pc + 2].string, |
1353 | NULL, "structure"); | |
1354 | } | |
1355 | ||
1356 | case STRUCTOP_PTR: | |
1357 | tem = longest_to_int (exp->elts[pc + 1].longconst); | |
1358 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
1359 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1360 | if (noside == EVAL_SKIP) | |
1361 | goto nosideret; | |
070ad9f0 DB |
1362 | |
1363 | /* JYG: if print object is on we need to replace the base type | |
1364 | with rtti type in order to continue on with successful | |
1365 | lookup of member / method only available in the rtti type. */ | |
1366 | { | |
df407dfe | 1367 | struct type *type = value_type (arg1); |
070ad9f0 DB |
1368 | struct type *real_type; |
1369 | int full, top, using_enc; | |
1370 | ||
1371 | if (objectprint && TYPE_TARGET_TYPE(type) && | |
1372 | (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) | |
1373 | { | |
1374 | real_type = value_rtti_target_type (arg1, &full, &top, &using_enc); | |
1375 | if (real_type) | |
1376 | { | |
1377 | if (TYPE_CODE (type) == TYPE_CODE_PTR) | |
1378 | real_type = lookup_pointer_type (real_type); | |
1379 | else | |
1380 | real_type = lookup_reference_type (real_type); | |
1381 | ||
1382 | arg1 = value_cast (real_type, arg1); | |
1383 | } | |
1384 | } | |
1385 | } | |
1386 | ||
c906108c | 1387 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
df407dfe | 1388 | return value_zero (lookup_struct_elt_type (value_type (arg1), |
c906108c SS |
1389 | &exp->elts[pc + 2].string, |
1390 | 0), | |
1391 | lval_memory); | |
1392 | else | |
1393 | { | |
61051030 | 1394 | struct value *temp = arg1; |
c906108c SS |
1395 | return value_struct_elt (&temp, NULL, &exp->elts[pc + 2].string, |
1396 | NULL, "structure pointer"); | |
1397 | } | |
1398 | ||
1399 | case STRUCTOP_MEMBER: | |
0d5de010 DJ |
1400 | case STRUCTOP_MPTR: |
1401 | if (op == STRUCTOP_MEMBER) | |
1402 | arg1 = evaluate_subexp_for_address (exp, pos, noside); | |
1403 | else | |
1404 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1405 | ||
c906108c SS |
1406 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
1407 | ||
0d5de010 DJ |
1408 | if (noside == EVAL_SKIP) |
1409 | goto nosideret; | |
c5aa993b | 1410 | |
0d5de010 DJ |
1411 | type = check_typedef (value_type (arg2)); |
1412 | switch (TYPE_CODE (type)) | |
1413 | { | |
1414 | case TYPE_CODE_METHODPTR: | |
0d5de010 DJ |
1415 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
1416 | return value_zero (TYPE_TARGET_TYPE (type), not_lval); | |
1417 | else | |
1418 | { | |
1419 | arg2 = cplus_method_ptr_to_value (&arg1, arg2); | |
1420 | gdb_assert (TYPE_CODE (value_type (arg2)) == TYPE_CODE_PTR); | |
1421 | return value_ind (arg2); | |
1422 | } | |
c906108c | 1423 | |
0d5de010 DJ |
1424 | case TYPE_CODE_MEMBERPTR: |
1425 | /* Now, convert these values to an address. */ | |
1426 | arg1 = value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type)), | |
1427 | arg1); | |
c906108c | 1428 | |
0d5de010 | 1429 | mem_offset = value_as_long (arg2); |
c906108c | 1430 | |
0d5de010 DJ |
1431 | arg3 = value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)), |
1432 | value_as_long (arg1) + mem_offset); | |
1433 | return value_ind (arg3); | |
1434 | ||
1435 | default: | |
1436 | error (_("non-pointer-to-member value used in pointer-to-member construct")); | |
c5aa993b | 1437 | } |
c906108c SS |
1438 | |
1439 | case BINOP_CONCAT: | |
1440 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
1441 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
1442 | if (noside == EVAL_SKIP) | |
1443 | goto nosideret; | |
1444 | if (binop_user_defined_p (op, arg1, arg2)) | |
1445 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1446 | else | |
1447 | return value_concat (arg1, arg2); | |
1448 | ||
1449 | case BINOP_ASSIGN: | |
1450 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
df407dfe | 1451 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
c906108c | 1452 | |
c906108c SS |
1453 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) |
1454 | return arg1; | |
1455 | if (binop_user_defined_p (op, arg1, arg2)) | |
1456 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1457 | else | |
1458 | return value_assign (arg1, arg2); | |
1459 | ||
1460 | case BINOP_ASSIGN_MODIFY: | |
1461 | (*pos) += 2; | |
1462 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
df407dfe | 1463 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
c906108c SS |
1464 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) |
1465 | return arg1; | |
1466 | op = exp->elts[pc + 1].opcode; | |
1467 | if (binop_user_defined_p (op, arg1, arg2)) | |
1468 | return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside); | |
1469 | else if (op == BINOP_ADD) | |
1470 | arg2 = value_add (arg1, arg2); | |
1471 | else if (op == BINOP_SUB) | |
1472 | arg2 = value_sub (arg1, arg2); | |
1473 | else | |
1474 | arg2 = value_binop (arg1, arg2, op); | |
1475 | return value_assign (arg1, arg2); | |
1476 | ||
1477 | case BINOP_ADD: | |
1478 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
1479 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
1480 | if (noside == EVAL_SKIP) | |
1481 | goto nosideret; | |
1482 | if (binop_user_defined_p (op, arg1, arg2)) | |
1483 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1484 | else | |
1485 | return value_add (arg1, arg2); | |
1486 | ||
1487 | case BINOP_SUB: | |
1488 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
1489 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
1490 | if (noside == EVAL_SKIP) | |
1491 | goto nosideret; | |
1492 | if (binop_user_defined_p (op, arg1, arg2)) | |
1493 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1494 | else | |
1495 | return value_sub (arg1, arg2); | |
1496 | ||
bd49c137 | 1497 | case BINOP_EXP: |
c906108c SS |
1498 | case BINOP_MUL: |
1499 | case BINOP_DIV: | |
1500 | case BINOP_REM: | |
1501 | case BINOP_MOD: | |
1502 | case BINOP_LSH: | |
1503 | case BINOP_RSH: | |
1504 | case BINOP_BITWISE_AND: | |
1505 | case BINOP_BITWISE_IOR: | |
1506 | case BINOP_BITWISE_XOR: | |
1507 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1508 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1509 | if (noside == EVAL_SKIP) | |
1510 | goto nosideret; | |
1511 | if (binop_user_defined_p (op, arg1, arg2)) | |
1512 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
c5aa993b JM |
1513 | else if (noside == EVAL_AVOID_SIDE_EFFECTS |
1514 | && (op == BINOP_DIV || op == BINOP_REM || op == BINOP_MOD)) | |
df407dfe | 1515 | return value_zero (value_type (arg1), not_lval); |
c906108c SS |
1516 | else |
1517 | return value_binop (arg1, arg2, op); | |
1518 | ||
1519 | case BINOP_RANGE: | |
1520 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1521 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1522 | if (noside == EVAL_SKIP) | |
1523 | goto nosideret; | |
8a3fe4f8 | 1524 | error (_("':' operator used in invalid context")); |
c906108c SS |
1525 | |
1526 | case BINOP_SUBSCRIPT: | |
1527 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
1528 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
1529 | if (noside == EVAL_SKIP) | |
1530 | goto nosideret; | |
1531 | if (binop_user_defined_p (op, arg1, arg2)) | |
1532 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1533 | else | |
c5aa993b | 1534 | { |
c906108c SS |
1535 | /* If the user attempts to subscript something that is not an |
1536 | array or pointer type (like a plain int variable for example), | |
1537 | then report this as an error. */ | |
1538 | ||
994b9211 | 1539 | arg1 = coerce_ref (arg1); |
df407dfe | 1540 | type = check_typedef (value_type (arg1)); |
c906108c SS |
1541 | if (TYPE_CODE (type) != TYPE_CODE_ARRAY |
1542 | && TYPE_CODE (type) != TYPE_CODE_PTR) | |
1543 | { | |
1544 | if (TYPE_NAME (type)) | |
8a3fe4f8 | 1545 | error (_("cannot subscript something of type `%s'"), |
c906108c SS |
1546 | TYPE_NAME (type)); |
1547 | else | |
8a3fe4f8 | 1548 | error (_("cannot subscript requested type")); |
c906108c SS |
1549 | } |
1550 | ||
1551 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1552 | return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1)); | |
1553 | else | |
1554 | return value_subscript (arg1, arg2); | |
c5aa993b | 1555 | } |
c906108c SS |
1556 | |
1557 | case BINOP_IN: | |
1558 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
1559 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
1560 | if (noside == EVAL_SKIP) | |
1561 | goto nosideret; | |
1562 | return value_in (arg1, arg2); | |
c5aa993b | 1563 | |
c906108c SS |
1564 | case MULTI_SUBSCRIPT: |
1565 | (*pos) += 2; | |
1566 | nargs = longest_to_int (exp->elts[pc + 1].longconst); | |
1567 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
1568 | while (nargs-- > 0) | |
1569 | { | |
1570 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
1571 | /* FIXME: EVAL_SKIP handling may not be correct. */ | |
1572 | if (noside == EVAL_SKIP) | |
1573 | { | |
1574 | if (nargs > 0) | |
1575 | { | |
1576 | continue; | |
1577 | } | |
1578 | else | |
1579 | { | |
1580 | goto nosideret; | |
1581 | } | |
1582 | } | |
1583 | /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */ | |
1584 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1585 | { | |
1586 | /* If the user attempts to subscript something that has no target | |
c5aa993b JM |
1587 | type (like a plain int variable for example), then report this |
1588 | as an error. */ | |
1589 | ||
df407dfe | 1590 | type = TYPE_TARGET_TYPE (check_typedef (value_type (arg1))); |
c906108c SS |
1591 | if (type != NULL) |
1592 | { | |
1593 | arg1 = value_zero (type, VALUE_LVAL (arg1)); | |
1594 | noside = EVAL_SKIP; | |
1595 | continue; | |
1596 | } | |
1597 | else | |
1598 | { | |
8a3fe4f8 | 1599 | error (_("cannot subscript something of type `%s'"), |
df407dfe | 1600 | TYPE_NAME (value_type (arg1))); |
c906108c SS |
1601 | } |
1602 | } | |
c5aa993b | 1603 | |
c906108c SS |
1604 | if (binop_user_defined_p (op, arg1, arg2)) |
1605 | { | |
1606 | arg1 = value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1607 | } | |
1608 | else | |
1609 | { | |
1610 | arg1 = value_subscript (arg1, arg2); | |
1611 | } | |
1612 | } | |
1613 | return (arg1); | |
1614 | ||
1615 | multi_f77_subscript: | |
c5aa993b | 1616 | { |
7ca2d3a3 DL |
1617 | int subscript_array[MAX_FORTRAN_DIMS]; |
1618 | int array_size_array[MAX_FORTRAN_DIMS]; | |
c5aa993b JM |
1619 | int ndimensions = 1, i; |
1620 | struct type *tmp_type; | |
1621 | int offset_item; /* The array offset where the item lives */ | |
c906108c SS |
1622 | |
1623 | if (nargs > MAX_FORTRAN_DIMS) | |
8a3fe4f8 | 1624 | error (_("Too many subscripts for F77 (%d Max)"), MAX_FORTRAN_DIMS); |
c906108c | 1625 | |
df407dfe | 1626 | tmp_type = check_typedef (value_type (arg1)); |
c906108c SS |
1627 | ndimensions = calc_f77_array_dims (type); |
1628 | ||
1629 | if (nargs != ndimensions) | |
8a3fe4f8 | 1630 | error (_("Wrong number of subscripts")); |
c906108c SS |
1631 | |
1632 | /* Now that we know we have a legal array subscript expression | |
c5aa993b | 1633 | let us actually find out where this element exists in the array. */ |
c906108c | 1634 | |
c5aa993b | 1635 | offset_item = 0; |
7ca2d3a3 DL |
1636 | /* Take array indices left to right */ |
1637 | for (i = 0; i < nargs; i++) | |
c906108c | 1638 | { |
c5aa993b | 1639 | /* Evaluate each subscript, It must be a legal integer in F77 */ |
c906108c SS |
1640 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); |
1641 | ||
c5aa993b | 1642 | /* Fill in the subscript and array size arrays */ |
c906108c SS |
1643 | |
1644 | subscript_array[i] = value_as_long (arg2); | |
7ca2d3a3 | 1645 | } |
c5aa993b | 1646 | |
7ca2d3a3 DL |
1647 | /* Internal type of array is arranged right to left */ |
1648 | for (i = 0; i < nargs; i++) | |
1649 | { | |
c906108c SS |
1650 | retcode = f77_get_dynamic_upperbound (tmp_type, &upper); |
1651 | if (retcode == BOUND_FETCH_ERROR) | |
8a3fe4f8 | 1652 | error (_("Cannot obtain dynamic upper bound")); |
c906108c | 1653 | |
c5aa993b | 1654 | retcode = f77_get_dynamic_lowerbound (tmp_type, &lower); |
c906108c | 1655 | if (retcode == BOUND_FETCH_ERROR) |
8a3fe4f8 | 1656 | error (_("Cannot obtain dynamic lower bound")); |
c906108c | 1657 | |
7ca2d3a3 | 1658 | array_size_array[nargs - i - 1] = upper - lower + 1; |
c5aa993b JM |
1659 | |
1660 | /* Zero-normalize subscripts so that offsetting will work. */ | |
1661 | ||
7ca2d3a3 | 1662 | subscript_array[nargs - i - 1] -= lower; |
c906108c SS |
1663 | |
1664 | /* If we are at the bottom of a multidimensional | |
1665 | array type then keep a ptr to the last ARRAY | |
1666 | type around for use when calling value_subscript() | |
1667 | below. This is done because we pretend to value_subscript | |
1668 | that we actually have a one-dimensional array | |
1669 | of base element type that we apply a simple | |
c5aa993b | 1670 | offset to. */ |
c906108c | 1671 | |
7ca2d3a3 | 1672 | if (i < nargs - 1) |
c5aa993b | 1673 | tmp_type = check_typedef (TYPE_TARGET_TYPE (tmp_type)); |
c906108c SS |
1674 | } |
1675 | ||
1676 | /* Now let us calculate the offset for this item */ | |
1677 | ||
7ca2d3a3 | 1678 | offset_item = subscript_array[ndimensions - 1]; |
c5aa993b | 1679 | |
7ca2d3a3 | 1680 | for (i = ndimensions - 1; i > 0; --i) |
c5aa993b | 1681 | offset_item = |
7ca2d3a3 | 1682 | array_size_array[i - 1] * offset_item + subscript_array[i - 1]; |
c906108c | 1683 | |
962d6d93 DL |
1684 | /* Construct a value node with the value of the offset */ |
1685 | ||
1686 | arg2 = value_from_longest (builtin_type_f_integer, offset_item); | |
1687 | ||
c906108c SS |
1688 | /* Let us now play a dirty trick: we will take arg1 |
1689 | which is a value node pointing to the topmost level | |
1690 | of the multidimensional array-set and pretend | |
1691 | that it is actually a array of the final element | |
1692 | type, this will ensure that value_subscript() | |
1693 | returns the correct type value */ | |
1694 | ||
04624583 | 1695 | deprecated_set_value_type (arg1, tmp_type); |
962d6d93 | 1696 | return value_ind (value_add (value_coerce_array (arg1), arg2)); |
c906108c SS |
1697 | } |
1698 | ||
1699 | case BINOP_LOGICAL_AND: | |
1700 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1701 | if (noside == EVAL_SKIP) | |
1702 | { | |
1703 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1704 | goto nosideret; | |
1705 | } | |
c5aa993b | 1706 | |
c906108c SS |
1707 | oldpos = *pos; |
1708 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS); | |
1709 | *pos = oldpos; | |
c5aa993b JM |
1710 | |
1711 | if (binop_user_defined_p (op, arg1, arg2)) | |
c906108c SS |
1712 | { |
1713 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1714 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1715 | } | |
1716 | else | |
1717 | { | |
1718 | tem = value_logical_not (arg1); | |
1719 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, | |
1720 | (tem ? EVAL_SKIP : noside)); | |
1721 | return value_from_longest (LA_BOOL_TYPE, | |
c5aa993b | 1722 | (LONGEST) (!tem && !value_logical_not (arg2))); |
c906108c SS |
1723 | } |
1724 | ||
1725 | case BINOP_LOGICAL_OR: | |
1726 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1727 | if (noside == EVAL_SKIP) | |
1728 | { | |
1729 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1730 | goto nosideret; | |
1731 | } | |
c5aa993b | 1732 | |
c906108c SS |
1733 | oldpos = *pos; |
1734 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS); | |
1735 | *pos = oldpos; | |
c5aa993b JM |
1736 | |
1737 | if (binop_user_defined_p (op, arg1, arg2)) | |
c906108c SS |
1738 | { |
1739 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1740 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1741 | } | |
1742 | else | |
1743 | { | |
1744 | tem = value_logical_not (arg1); | |
1745 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, | |
1746 | (!tem ? EVAL_SKIP : noside)); | |
1747 | return value_from_longest (LA_BOOL_TYPE, | |
c5aa993b | 1748 | (LONGEST) (!tem || !value_logical_not (arg2))); |
c906108c SS |
1749 | } |
1750 | ||
1751 | case BINOP_EQUAL: | |
1752 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
df407dfe | 1753 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
c906108c SS |
1754 | if (noside == EVAL_SKIP) |
1755 | goto nosideret; | |
1756 | if (binop_user_defined_p (op, arg1, arg2)) | |
1757 | { | |
1758 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1759 | } | |
1760 | else | |
1761 | { | |
1762 | tem = value_equal (arg1, arg2); | |
1763 | return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem); | |
1764 | } | |
1765 | ||
1766 | case BINOP_NOTEQUAL: | |
1767 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
df407dfe | 1768 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
c906108c SS |
1769 | if (noside == EVAL_SKIP) |
1770 | goto nosideret; | |
1771 | if (binop_user_defined_p (op, arg1, arg2)) | |
1772 | { | |
1773 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1774 | } | |
1775 | else | |
1776 | { | |
1777 | tem = value_equal (arg1, arg2); | |
1778 | return value_from_longest (LA_BOOL_TYPE, (LONGEST) ! tem); | |
1779 | } | |
1780 | ||
1781 | case BINOP_LESS: | |
1782 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
df407dfe | 1783 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
c906108c SS |
1784 | if (noside == EVAL_SKIP) |
1785 | goto nosideret; | |
1786 | if (binop_user_defined_p (op, arg1, arg2)) | |
1787 | { | |
1788 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1789 | } | |
1790 | else | |
1791 | { | |
1792 | tem = value_less (arg1, arg2); | |
1793 | return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem); | |
1794 | } | |
1795 | ||
1796 | case BINOP_GTR: | |
1797 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
df407dfe | 1798 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
c906108c SS |
1799 | if (noside == EVAL_SKIP) |
1800 | goto nosideret; | |
1801 | if (binop_user_defined_p (op, arg1, arg2)) | |
1802 | { | |
1803 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1804 | } | |
1805 | else | |
1806 | { | |
1807 | tem = value_less (arg2, arg1); | |
1808 | return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem); | |
1809 | } | |
1810 | ||
1811 | case BINOP_GEQ: | |
1812 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
df407dfe | 1813 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
c906108c SS |
1814 | if (noside == EVAL_SKIP) |
1815 | goto nosideret; | |
1816 | if (binop_user_defined_p (op, arg1, arg2)) | |
1817 | { | |
1818 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1819 | } | |
1820 | else | |
1821 | { | |
1822 | tem = value_less (arg2, arg1) || value_equal (arg1, arg2); | |
1823 | return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem); | |
1824 | } | |
1825 | ||
1826 | case BINOP_LEQ: | |
1827 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
df407dfe | 1828 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
c906108c SS |
1829 | if (noside == EVAL_SKIP) |
1830 | goto nosideret; | |
1831 | if (binop_user_defined_p (op, arg1, arg2)) | |
1832 | { | |
1833 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1834 | } | |
c5aa993b | 1835 | else |
c906108c SS |
1836 | { |
1837 | tem = value_less (arg1, arg2) || value_equal (arg1, arg2); | |
1838 | return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem); | |
1839 | } | |
1840 | ||
1841 | case BINOP_REPEAT: | |
1842 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1843 | arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1844 | if (noside == EVAL_SKIP) | |
1845 | goto nosideret; | |
df407dfe | 1846 | type = check_typedef (value_type (arg2)); |
c906108c | 1847 | if (TYPE_CODE (type) != TYPE_CODE_INT) |
8a3fe4f8 | 1848 | error (_("Non-integral right operand for \"@\" operator.")); |
c906108c SS |
1849 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
1850 | { | |
df407dfe | 1851 | return allocate_repeat_value (value_type (arg1), |
c5aa993b | 1852 | longest_to_int (value_as_long (arg2))); |
c906108c SS |
1853 | } |
1854 | else | |
1855 | return value_repeat (arg1, longest_to_int (value_as_long (arg2))); | |
1856 | ||
1857 | case BINOP_COMMA: | |
1858 | evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1859 | return evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1860 | ||
36e9969c NS |
1861 | case UNOP_PLUS: |
1862 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1863 | if (noside == EVAL_SKIP) | |
1864 | goto nosideret; | |
1865 | if (unop_user_defined_p (op, arg1)) | |
1866 | return value_x_unop (arg1, op, noside); | |
1867 | else | |
1868 | return value_pos (arg1); | |
1869 | ||
c906108c SS |
1870 | case UNOP_NEG: |
1871 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1872 | if (noside == EVAL_SKIP) | |
1873 | goto nosideret; | |
1874 | if (unop_user_defined_p (op, arg1)) | |
1875 | return value_x_unop (arg1, op, noside); | |
1876 | else | |
1877 | return value_neg (arg1); | |
1878 | ||
1879 | case UNOP_COMPLEMENT: | |
1880 | /* C++: check for and handle destructor names. */ | |
1881 | op = exp->elts[*pos].opcode; | |
1882 | ||
1883 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1884 | if (noside == EVAL_SKIP) | |
1885 | goto nosideret; | |
1886 | if (unop_user_defined_p (UNOP_COMPLEMENT, arg1)) | |
1887 | return value_x_unop (arg1, UNOP_COMPLEMENT, noside); | |
1888 | else | |
1889 | return value_complement (arg1); | |
1890 | ||
1891 | case UNOP_LOGICAL_NOT: | |
1892 | arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
1893 | if (noside == EVAL_SKIP) | |
1894 | goto nosideret; | |
1895 | if (unop_user_defined_p (op, arg1)) | |
1896 | return value_x_unop (arg1, op, noside); | |
1897 | else | |
1898 | return value_from_longest (LA_BOOL_TYPE, | |
1899 | (LONGEST) value_logical_not (arg1)); | |
1900 | ||
1901 | case UNOP_IND: | |
1902 | if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR) | |
c5aa993b | 1903 | expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type)); |
c906108c | 1904 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); |
0d5de010 DJ |
1905 | type = check_typedef (value_type (arg1)); |
1906 | if (TYPE_CODE (type) == TYPE_CODE_METHODPTR | |
1907 | || TYPE_CODE (type) == TYPE_CODE_MEMBERPTR) | |
8a3fe4f8 | 1908 | error (_("Attempt to dereference pointer to member without an object")); |
c906108c SS |
1909 | if (noside == EVAL_SKIP) |
1910 | goto nosideret; | |
1911 | if (unop_user_defined_p (op, arg1)) | |
1912 | return value_x_unop (arg1, op, noside); | |
1913 | else if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1914 | { | |
df407dfe | 1915 | type = check_typedef (value_type (arg1)); |
c906108c SS |
1916 | if (TYPE_CODE (type) == TYPE_CODE_PTR |
1917 | || TYPE_CODE (type) == TYPE_CODE_REF | |
c5aa993b | 1918 | /* In C you can dereference an array to get the 1st elt. */ |
c906108c | 1919 | || TYPE_CODE (type) == TYPE_CODE_ARRAY |
c5aa993b | 1920 | ) |
c906108c SS |
1921 | return value_zero (TYPE_TARGET_TYPE (type), |
1922 | lval_memory); | |
1923 | else if (TYPE_CODE (type) == TYPE_CODE_INT) | |
1924 | /* GDB allows dereferencing an int. */ | |
1925 | return value_zero (builtin_type_int, lval_memory); | |
1926 | else | |
8a3fe4f8 | 1927 | error (_("Attempt to take contents of a non-pointer value.")); |
c906108c SS |
1928 | } |
1929 | return value_ind (arg1); | |
1930 | ||
1931 | case UNOP_ADDR: | |
1932 | /* C++: check for and handle pointer to members. */ | |
c5aa993b | 1933 | |
c906108c SS |
1934 | op = exp->elts[*pos].opcode; |
1935 | ||
1936 | if (noside == EVAL_SKIP) | |
1937 | { | |
0d5de010 | 1938 | evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP); |
c906108c SS |
1939 | goto nosideret; |
1940 | } | |
c5aa993b JM |
1941 | else |
1942 | { | |
61051030 | 1943 | struct value *retvalp = evaluate_subexp_for_address (exp, pos, noside); |
c5aa993b JM |
1944 | return retvalp; |
1945 | } | |
1946 | ||
c906108c SS |
1947 | case UNOP_SIZEOF: |
1948 | if (noside == EVAL_SKIP) | |
1949 | { | |
1950 | evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP); | |
1951 | goto nosideret; | |
1952 | } | |
1953 | return evaluate_subexp_for_sizeof (exp, pos); | |
1954 | ||
1955 | case UNOP_CAST: | |
1956 | (*pos) += 2; | |
1957 | type = exp->elts[pc + 1].type; | |
1958 | arg1 = evaluate_subexp (type, exp, pos, noside); | |
1959 | if (noside == EVAL_SKIP) | |
1960 | goto nosideret; | |
df407dfe | 1961 | if (type != value_type (arg1)) |
c906108c SS |
1962 | arg1 = value_cast (type, arg1); |
1963 | return arg1; | |
1964 | ||
1965 | case UNOP_MEMVAL: | |
1966 | (*pos) += 2; | |
1967 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
1968 | if (noside == EVAL_SKIP) | |
1969 | goto nosideret; | |
1970 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1971 | return value_zero (exp->elts[pc + 1].type, lval_memory); | |
1972 | else | |
1973 | return value_at_lazy (exp->elts[pc + 1].type, | |
00a4c844 | 1974 | value_as_address (arg1)); |
c906108c | 1975 | |
9e35dae4 DJ |
1976 | case UNOP_MEMVAL_TLS: |
1977 | (*pos) += 3; | |
1978 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
1979 | if (noside == EVAL_SKIP) | |
1980 | goto nosideret; | |
1981 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1982 | return value_zero (exp->elts[pc + 2].type, lval_memory); | |
1983 | else | |
1984 | { | |
1985 | CORE_ADDR tls_addr; | |
1986 | tls_addr = target_translate_tls_address (exp->elts[pc + 1].objfile, | |
1987 | value_as_address (arg1)); | |
1988 | return value_at_lazy (exp->elts[pc + 2].type, tls_addr); | |
1989 | } | |
1990 | ||
c906108c SS |
1991 | case UNOP_PREINCREMENT: |
1992 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
1993 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
1994 | return arg1; | |
1995 | else if (unop_user_defined_p (op, arg1)) | |
1996 | { | |
1997 | return value_x_unop (arg1, op, noside); | |
1998 | } | |
1999 | else | |
2000 | { | |
c5aa993b JM |
2001 | arg2 = value_add (arg1, value_from_longest (builtin_type_char, |
2002 | (LONGEST) 1)); | |
c906108c SS |
2003 | return value_assign (arg1, arg2); |
2004 | } | |
2005 | ||
2006 | case UNOP_PREDECREMENT: | |
2007 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
2008 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
2009 | return arg1; | |
2010 | else if (unop_user_defined_p (op, arg1)) | |
2011 | { | |
2012 | return value_x_unop (arg1, op, noside); | |
2013 | } | |
2014 | else | |
2015 | { | |
c5aa993b JM |
2016 | arg2 = value_sub (arg1, value_from_longest (builtin_type_char, |
2017 | (LONGEST) 1)); | |
c906108c SS |
2018 | return value_assign (arg1, arg2); |
2019 | } | |
2020 | ||
2021 | case UNOP_POSTINCREMENT: | |
2022 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
2023 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
2024 | return arg1; | |
2025 | else if (unop_user_defined_p (op, arg1)) | |
2026 | { | |
2027 | return value_x_unop (arg1, op, noside); | |
2028 | } | |
2029 | else | |
2030 | { | |
c5aa993b JM |
2031 | arg2 = value_add (arg1, value_from_longest (builtin_type_char, |
2032 | (LONGEST) 1)); | |
c906108c SS |
2033 | value_assign (arg1, arg2); |
2034 | return arg1; | |
2035 | } | |
2036 | ||
2037 | case UNOP_POSTDECREMENT: | |
2038 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
2039 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
2040 | return arg1; | |
2041 | else if (unop_user_defined_p (op, arg1)) | |
2042 | { | |
2043 | return value_x_unop (arg1, op, noside); | |
2044 | } | |
2045 | else | |
2046 | { | |
c5aa993b JM |
2047 | arg2 = value_sub (arg1, value_from_longest (builtin_type_char, |
2048 | (LONGEST) 1)); | |
c906108c SS |
2049 | value_assign (arg1, arg2); |
2050 | return arg1; | |
2051 | } | |
c5aa993b | 2052 | |
c906108c SS |
2053 | case OP_THIS: |
2054 | (*pos) += 1; | |
2055 | return value_of_this (1); | |
2056 | ||
a9fa03de AF |
2057 | case OP_OBJC_SELF: |
2058 | (*pos) += 1; | |
2059 | return value_of_local ("self", 1); | |
2060 | ||
c906108c | 2061 | case OP_TYPE: |
d843c49c FF |
2062 | /* The value is not supposed to be used. This is here to make it |
2063 | easier to accommodate expressions that contain types. */ | |
2064 | (*pos) += 2; | |
2065 | if (noside == EVAL_SKIP) | |
2066 | goto nosideret; | |
2067 | else if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
2068 | return allocate_value (exp->elts[pc + 1].type); | |
2069 | else | |
2070 | error (_("Attempt to use a type name as an expression")); | |
c906108c SS |
2071 | |
2072 | default: | |
2073 | /* Removing this case and compiling with gcc -Wall reveals that | |
c5aa993b | 2074 | a lot of cases are hitting this case. Some of these should |
2df3850c JM |
2075 | probably be removed from expression.h; others are legitimate |
2076 | expressions which are (apparently) not fully implemented. | |
c906108c | 2077 | |
c5aa993b JM |
2078 | If there are any cases landing here which mean a user error, |
2079 | then they should be separate cases, with more descriptive | |
2080 | error messages. */ | |
c906108c | 2081 | |
8a3fe4f8 AC |
2082 | error (_("\ |
2083 | GDB does not (yet) know how to evaluate that kind of expression")); | |
c906108c SS |
2084 | } |
2085 | ||
c5aa993b | 2086 | nosideret: |
c906108c SS |
2087 | return value_from_longest (builtin_type_long, (LONGEST) 1); |
2088 | } | |
2089 | \f | |
2090 | /* Evaluate a subexpression of EXP, at index *POS, | |
2091 | and return the address of that subexpression. | |
2092 | Advance *POS over the subexpression. | |
2093 | If the subexpression isn't an lvalue, get an error. | |
2094 | NOSIDE may be EVAL_AVOID_SIDE_EFFECTS; | |
2095 | then only the type of the result need be correct. */ | |
2096 | ||
61051030 | 2097 | static struct value * |
aa1ee363 | 2098 | evaluate_subexp_for_address (struct expression *exp, int *pos, |
fba45db2 | 2099 | enum noside noside) |
c906108c SS |
2100 | { |
2101 | enum exp_opcode op; | |
52f0bd74 | 2102 | int pc; |
c906108c | 2103 | struct symbol *var; |
ab5c9f60 | 2104 | struct value *x; |
0d5de010 | 2105 | int tem; |
c906108c SS |
2106 | |
2107 | pc = (*pos); | |
2108 | op = exp->elts[pc].opcode; | |
2109 | ||
2110 | switch (op) | |
2111 | { | |
2112 | case UNOP_IND: | |
2113 | (*pos)++; | |
ab5c9f60 DJ |
2114 | x = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
2115 | ||
2116 | /* We can't optimize out "&*" if there's a user-defined operator*. */ | |
2117 | if (unop_user_defined_p (op, x)) | |
2118 | { | |
2119 | x = value_x_unop (x, op, noside); | |
0d5de010 | 2120 | goto default_case_after_eval; |
ab5c9f60 DJ |
2121 | } |
2122 | ||
2123 | return x; | |
c906108c SS |
2124 | |
2125 | case UNOP_MEMVAL: | |
2126 | (*pos) += 3; | |
2127 | return value_cast (lookup_pointer_type (exp->elts[pc + 1].type), | |
2128 | evaluate_subexp (NULL_TYPE, exp, pos, noside)); | |
2129 | ||
2130 | case OP_VAR_VALUE: | |
2131 | var = exp->elts[pc + 2].symbol; | |
2132 | ||
2133 | /* C++: The "address" of a reference should yield the address | |
2134 | * of the object pointed to. Let value_addr() deal with it. */ | |
2135 | if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_REF) | |
c5aa993b | 2136 | goto default_case; |
c906108c SS |
2137 | |
2138 | (*pos) += 4; | |
2139 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
2140 | { | |
2141 | struct type *type = | |
c5aa993b | 2142 | lookup_pointer_type (SYMBOL_TYPE (var)); |
c906108c SS |
2143 | enum address_class sym_class = SYMBOL_CLASS (var); |
2144 | ||
2145 | if (sym_class == LOC_CONST | |
2146 | || sym_class == LOC_CONST_BYTES | |
2147 | || sym_class == LOC_REGISTER | |
2148 | || sym_class == LOC_REGPARM) | |
8a3fe4f8 | 2149 | error (_("Attempt to take address of register or constant.")); |
c906108c | 2150 | |
c5aa993b JM |
2151 | return |
2152 | value_zero (type, not_lval); | |
c906108c SS |
2153 | } |
2154 | else | |
2155 | return | |
2156 | locate_var_value | |
c5aa993b JM |
2157 | (var, |
2158 | block_innermost_frame (exp->elts[pc + 1].block)); | |
c906108c | 2159 | |
0d5de010 DJ |
2160 | case OP_SCOPE: |
2161 | tem = longest_to_int (exp->elts[pc + 2].longconst); | |
2162 | (*pos) += 5 + BYTES_TO_EXP_ELEM (tem + 1); | |
2163 | x = value_aggregate_elt (exp->elts[pc + 1].type, | |
2164 | &exp->elts[pc + 3].string, | |
2165 | 1, noside); | |
2166 | if (x == NULL) | |
2167 | error (_("There is no field named %s"), &exp->elts[pc + 3].string); | |
2168 | return x; | |
2169 | ||
c906108c SS |
2170 | default: |
2171 | default_case: | |
ab5c9f60 | 2172 | x = evaluate_subexp (NULL_TYPE, exp, pos, noside); |
0d5de010 | 2173 | default_case_after_eval: |
c906108c SS |
2174 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
2175 | { | |
0d5de010 DJ |
2176 | struct type *type = check_typedef (value_type (x)); |
2177 | ||
c906108c | 2178 | if (VALUE_LVAL (x) == lval_memory) |
df407dfe | 2179 | return value_zero (lookup_pointer_type (value_type (x)), |
c906108c | 2180 | not_lval); |
0d5de010 DJ |
2181 | else if (TYPE_CODE (type) == TYPE_CODE_REF) |
2182 | return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type)), | |
2183 | not_lval); | |
c906108c | 2184 | else |
8a3fe4f8 | 2185 | error (_("Attempt to take address of non-lval")); |
c906108c | 2186 | } |
ab5c9f60 | 2187 | return value_addr (x); |
c906108c SS |
2188 | } |
2189 | } | |
2190 | ||
2191 | /* Evaluate like `evaluate_subexp' except coercing arrays to pointers. | |
2192 | When used in contexts where arrays will be coerced anyway, this is | |
2193 | equivalent to `evaluate_subexp' but much faster because it avoids | |
2194 | actually fetching array contents (perhaps obsolete now that we have | |
d69fe07e | 2195 | value_lazy()). |
c906108c SS |
2196 | |
2197 | Note that we currently only do the coercion for C expressions, where | |
2198 | arrays are zero based and the coercion is correct. For other languages, | |
2199 | with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION | |
2200 | to decide if coercion is appropriate. | |
2201 | ||
c5aa993b | 2202 | */ |
c906108c | 2203 | |
61051030 | 2204 | struct value * |
aa1ee363 AC |
2205 | evaluate_subexp_with_coercion (struct expression *exp, |
2206 | int *pos, enum noside noside) | |
c906108c | 2207 | { |
52f0bd74 AC |
2208 | enum exp_opcode op; |
2209 | int pc; | |
61051030 | 2210 | struct value *val; |
c906108c SS |
2211 | struct symbol *var; |
2212 | ||
2213 | pc = (*pos); | |
2214 | op = exp->elts[pc].opcode; | |
2215 | ||
2216 | switch (op) | |
2217 | { | |
2218 | case OP_VAR_VALUE: | |
2219 | var = exp->elts[pc + 2].symbol; | |
2220 | if (TYPE_CODE (check_typedef (SYMBOL_TYPE (var))) == TYPE_CODE_ARRAY | |
2221 | && CAST_IS_CONVERSION) | |
2222 | { | |
2223 | (*pos) += 4; | |
2224 | val = | |
2225 | locate_var_value | |
c5aa993b | 2226 | (var, block_innermost_frame (exp->elts[pc + 1].block)); |
751a959b | 2227 | return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (check_typedef (SYMBOL_TYPE (var)))), |
c906108c SS |
2228 | val); |
2229 | } | |
2230 | /* FALLTHROUGH */ | |
2231 | ||
2232 | default: | |
2233 | return evaluate_subexp (NULL_TYPE, exp, pos, noside); | |
2234 | } | |
2235 | } | |
2236 | ||
2237 | /* Evaluate a subexpression of EXP, at index *POS, | |
2238 | and return a value for the size of that subexpression. | |
2239 | Advance *POS over the subexpression. */ | |
2240 | ||
61051030 | 2241 | static struct value * |
aa1ee363 | 2242 | evaluate_subexp_for_sizeof (struct expression *exp, int *pos) |
c906108c SS |
2243 | { |
2244 | enum exp_opcode op; | |
52f0bd74 | 2245 | int pc; |
c906108c | 2246 | struct type *type; |
61051030 | 2247 | struct value *val; |
c906108c SS |
2248 | |
2249 | pc = (*pos); | |
2250 | op = exp->elts[pc].opcode; | |
2251 | ||
2252 | switch (op) | |
2253 | { | |
2254 | /* This case is handled specially | |
c5aa993b JM |
2255 | so that we avoid creating a value for the result type. |
2256 | If the result type is very big, it's desirable not to | |
2257 | create a value unnecessarily. */ | |
c906108c SS |
2258 | case UNOP_IND: |
2259 | (*pos)++; | |
2260 | val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS); | |
df407dfe | 2261 | type = check_typedef (value_type (val)); |
c906108c SS |
2262 | if (TYPE_CODE (type) != TYPE_CODE_PTR |
2263 | && TYPE_CODE (type) != TYPE_CODE_REF | |
2264 | && TYPE_CODE (type) != TYPE_CODE_ARRAY) | |
8a3fe4f8 | 2265 | error (_("Attempt to take contents of a non-pointer value.")); |
c906108c SS |
2266 | type = check_typedef (TYPE_TARGET_TYPE (type)); |
2267 | return value_from_longest (builtin_type_int, (LONGEST) | |
c5aa993b | 2268 | TYPE_LENGTH (type)); |
c906108c SS |
2269 | |
2270 | case UNOP_MEMVAL: | |
2271 | (*pos) += 3; | |
2272 | type = check_typedef (exp->elts[pc + 1].type); | |
2273 | return value_from_longest (builtin_type_int, | |
2274 | (LONGEST) TYPE_LENGTH (type)); | |
2275 | ||
2276 | case OP_VAR_VALUE: | |
2277 | (*pos) += 4; | |
2278 | type = check_typedef (SYMBOL_TYPE (exp->elts[pc + 2].symbol)); | |
2279 | return | |
2280 | value_from_longest (builtin_type_int, (LONGEST) TYPE_LENGTH (type)); | |
2281 | ||
2282 | default: | |
2283 | val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS); | |
2284 | return value_from_longest (builtin_type_int, | |
df407dfe | 2285 | (LONGEST) TYPE_LENGTH (value_type (val))); |
c906108c SS |
2286 | } |
2287 | } | |
2288 | ||
2289 | /* Parse a type expression in the string [P..P+LENGTH). */ | |
2290 | ||
2291 | struct type * | |
fba45db2 | 2292 | parse_and_eval_type (char *p, int length) |
c906108c | 2293 | { |
c5aa993b JM |
2294 | char *tmp = (char *) alloca (length + 4); |
2295 | struct expression *expr; | |
2296 | tmp[0] = '('; | |
2297 | memcpy (tmp + 1, p, length); | |
2298 | tmp[length + 1] = ')'; | |
2299 | tmp[length + 2] = '0'; | |
2300 | tmp[length + 3] = '\0'; | |
2301 | expr = parse_expression (tmp); | |
2302 | if (expr->elts[0].opcode != UNOP_CAST) | |
8a3fe4f8 | 2303 | error (_("Internal error in eval_type.")); |
c5aa993b | 2304 | return expr->elts[1].type; |
c906108c SS |
2305 | } |
2306 | ||
2307 | int | |
fba45db2 | 2308 | calc_f77_array_dims (struct type *array_type) |
c906108c SS |
2309 | { |
2310 | int ndimen = 1; | |
2311 | struct type *tmp_type; | |
2312 | ||
c5aa993b | 2313 | if ((TYPE_CODE (array_type) != TYPE_CODE_ARRAY)) |
8a3fe4f8 | 2314 | error (_("Can't get dimensions for a non-array type")); |
c5aa993b JM |
2315 | |
2316 | tmp_type = array_type; | |
c906108c SS |
2317 | |
2318 | while ((tmp_type = TYPE_TARGET_TYPE (tmp_type))) | |
2319 | { | |
2320 | if (TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY) | |
2321 | ++ndimen; | |
2322 | } | |
c5aa993b | 2323 | return ndimen; |
c906108c | 2324 | } |