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