* core-aout.c (fetch_core_registers): Cast core_reg_size to int
[deliverable/binutils-gdb.git] / gdb / eval.c
CommitLineData
bd5635a1 1/* Evaluate expressions for GDB.
477b2425 2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995
2d67c7e9 3 Free Software Foundation, Inc.
bd5635a1
RP
4
5This file is part of GDB.
6
2ccb3837 7This program is free software; you can redistribute it and/or modify
bd5635a1 8it under the terms of the GNU General Public License as published by
2ccb3837
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
bd5635a1 11
2ccb3837 12This program is distributed in the hope that it will be useful,
bd5635a1
RP
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
2ccb3837 18along with this program; if not, write to the Free Software
0694bce6 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
bd5635a1
RP
20
21#include "defs.h"
2b576293 22#include "gdb_string.h"
bd5635a1 23#include "symtab.h"
01be6913 24#include "gdbtypes.h"
bd5635a1
RP
25#include "value.h"
26#include "expression.h"
27#include "target.h"
2ccb3837 28#include "frame.h"
40620258 29#include "demangle.h"
fb6e675f 30#include "language.h" /* For CAST_IS_CONVERSION */
477b2425 31#include "f-lang.h" /* for array bound stuff */
cd10c7e3 32/* start-sanitize-gm */
bfe8f516
FF
33#ifdef GENERAL_MAGIC
34#include "gmagic.h"
35#endif /* GENERAL_MAGIC */
cd10c7e3 36/* end-sanitize-gm */
bd5635a1 37
01be6913
PB
38/* Prototypes for local functions. */
39
2d67c7e9
PB
40static value_ptr evaluate_subexp_for_sizeof PARAMS ((struct expression *,
41 int *));
01be6913 42
2d67c7e9
PB
43static value_ptr evaluate_subexp_for_address PARAMS ((struct expression *,
44 int *, enum noside));
01be6913 45
7398958c
PB
46#ifdef __GNUC__
47inline
48#endif
49static value_ptr
50evaluate_subexp (expect_type, exp, pos, noside)
51 struct type *expect_type;
52 register struct expression *exp;
53 register int *pos;
54 enum noside noside;
55{
56 return (*exp->language_defn->evaluate_exp) (expect_type, exp, pos, noside);
57}
bd5635a1
RP
58\f
59/* Parse the string EXP as a C expression, evaluate it,
60 and return the result as a number. */
61
62CORE_ADDR
63parse_and_eval_address (exp)
64 char *exp;
65{
2ccb3837 66 struct expression *expr = parse_expression (exp);
bd5635a1 67 register CORE_ADDR addr;
01be6913
PB
68 register struct cleanup *old_chain =
69 make_cleanup (free_current_contents, &expr);
bd5635a1 70
2ccb3837 71 addr = value_as_pointer (evaluate_expression (expr));
bd5635a1
RP
72 do_cleanups (old_chain);
73 return addr;
74}
75
76/* Like parse_and_eval_address but takes a pointer to a char * variable
77 and advanced that variable across the characters parsed. */
78
79CORE_ADDR
80parse_and_eval_address_1 (expptr)
81 char **expptr;
82{
2ccb3837 83 struct expression *expr = parse_exp_1 (expptr, (struct block *)0, 0);
bd5635a1 84 register CORE_ADDR addr;
01be6913
PB
85 register struct cleanup *old_chain =
86 make_cleanup (free_current_contents, &expr);
bd5635a1 87
2ccb3837 88 addr = value_as_pointer (evaluate_expression (expr));
bd5635a1
RP
89 do_cleanups (old_chain);
90 return addr;
91}
92
2d67c7e9 93value_ptr
bd5635a1
RP
94parse_and_eval (exp)
95 char *exp;
96{
2ccb3837 97 struct expression *expr = parse_expression (exp);
2d67c7e9 98 register value_ptr val;
bd5635a1
RP
99 register struct cleanup *old_chain
100 = make_cleanup (free_current_contents, &expr);
101
102 val = evaluate_expression (expr);
103 do_cleanups (old_chain);
104 return val;
105}
106
107/* Parse up to a comma (or to a closeparen)
108 in the string EXPP as an expression, evaluate it, and return the value.
109 EXPP is advanced to point to the comma. */
110
2d67c7e9 111value_ptr
bd5635a1
RP
112parse_to_comma_and_eval (expp)
113 char **expp;
114{
2ccb3837 115 struct expression *expr = parse_exp_1 (expp, (struct block *) 0, 1);
2d67c7e9 116 register value_ptr val;
bd5635a1
RP
117 register struct cleanup *old_chain
118 = make_cleanup (free_current_contents, &expr);
119
120 val = evaluate_expression (expr);
121 do_cleanups (old_chain);
122 return val;
123}
124\f
125/* Evaluate an expression in internal prefix form
0a5d35ed 126 such as is constructed by parse.y.
bd5635a1
RP
127
128 See expression.h for info on the format of an expression. */
129
2d67c7e9 130value_ptr
bd5635a1
RP
131evaluate_expression (exp)
132 struct expression *exp;
133{
134 int pc = 0;
135 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_NORMAL);
136}
137
138/* Evaluate an expression, avoiding all memory references
139 and getting a value whose type alone is correct. */
140
2d67c7e9 141value_ptr
bd5635a1
RP
142evaluate_type (exp)
143 struct expression *exp;
144{
145 int pc = 0;
146 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
147}
148
0694bce6
SC
149/* If the next expression is an OP_LABELED, skips past it,
150 returning the label. Otherwise, does nothing and returns NULL. */
dcda44a0 151
0694bce6
SC
152static char*
153get_label (exp, pos)
dcda44a0 154 register struct expression *exp;
0694bce6 155 int *pos;
dcda44a0 156{
dcda44a0
PB
157 if (exp->elts[*pos].opcode == OP_LABELED)
158 {
159 int pc = (*pos)++;
160 char *name = &exp->elts[pc + 2].string;
161 int tem = longest_to_int (exp->elts[pc + 1].longconst);
162 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
0694bce6 163 return name;
dcda44a0
PB
164 }
165 else
0694bce6
SC
166 return NULL;
167}
168
169/* This function evaluates tupes (in Chill) or brace-initializers
170 (in C/C++) for structure types. */
171
172static value_ptr
173evaluate_struct_tuple (struct_val, exp, pos, noside, nargs)
174 value_ptr struct_val;
175 register struct expression *exp;
176 register int *pos;
177 enum noside noside;
178 int nargs;
179{
bcbf388e 180 struct type *struct_type = check_typedef (VALUE_TYPE (struct_val));
0694bce6
SC
181 struct type *substruct_type = struct_type;
182 struct type *field_type;
183 int fieldno = -1;
184 int variantno = -1;
185 int subfieldno = -1;
bcbf388e 186 while (--nargs >= 0)
dcda44a0 187 {
0694bce6
SC
188 int pc = *pos;
189 value_ptr val = NULL;
190 int nlabels = 0;
191 int bitpos, bitsize;
192 char *addr;
193
194 /* Skip past the labels, and count them. */
195 while (get_label (exp, pos) != NULL)
196 nlabels++;
197
198 do
199 {
200 char *label = get_label (exp, &pc);
201 if (label)
202 {
203 for (fieldno = 0; fieldno < TYPE_NFIELDS (struct_type);
204 fieldno++)
205 {
206 char *field_name = TYPE_FIELD_NAME (struct_type, fieldno);
207 if (field_name != NULL && STREQ (field_name, label))
208 {
209 variantno = -1;
210 subfieldno = fieldno;
211 substruct_type = struct_type;
212 goto found;
213 }
214 }
215 for (fieldno = 0; fieldno < TYPE_NFIELDS (struct_type);
216 fieldno++)
217 {
218 char *field_name = TYPE_FIELD_NAME (struct_type, fieldno);
219 field_type = TYPE_FIELD_TYPE (struct_type, fieldno);
220 if ((field_name == 0 || *field_name == '\0')
221 && TYPE_CODE (field_type) == TYPE_CODE_UNION)
222 {
223 variantno = 0;
224 for (; variantno < TYPE_NFIELDS (field_type);
225 variantno++)
226 {
227 substruct_type
228 = TYPE_FIELD_TYPE (field_type, variantno);
229 if (TYPE_CODE (substruct_type) == TYPE_CODE_STRUCT)
230 {
231 for (subfieldno = 0;
232 subfieldno < TYPE_NFIELDS (substruct_type);
233 subfieldno++)
234 {
235 if (STREQ (TYPE_FIELD_NAME (substruct_type,
236 subfieldno),
237 label))
238 {
239 goto found;
240 }
241 }
242 }
243 }
244 }
245 }
246 error ("there is no field named %s", label);
247 found:
248 ;
249 }
250 else
251 {
252 /* Unlabelled tuple element - go to next field. */
253 if (variantno >= 0)
254 {
255 subfieldno++;
256 if (subfieldno >= TYPE_NFIELDS (substruct_type))
257 {
258 variantno = -1;
259 substruct_type = struct_type;
260 }
261 }
262 if (variantno < 0)
263 {
264 fieldno++;
265 subfieldno = fieldno;
266 if (fieldno >= TYPE_NFIELDS (struct_type))
267 error ("too many initializers");
268 field_type = TYPE_FIELD_TYPE (struct_type, fieldno);
269 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
270 && TYPE_FIELD_NAME (struct_type, fieldno)[0] == '0')
271 error ("don't know which variant you want to set");
272 }
273 }
dcda44a0 274
0694bce6
SC
275 /* Here, struct_type is the type of the inner struct,
276 while substruct_type is the type of the inner struct.
277 These are the same for normal structures, but a variant struct
278 contains anonymous union fields that contain substruct fields.
279 The value fieldno is the index of the top-level (normal or
280 anonymous union) field in struct_field, while the value
281 subfieldno is the index of the actual real (named inner) field
282 in substruct_type. */
283
284 field_type = TYPE_FIELD_TYPE (substruct_type, subfieldno);
285 if (val == 0)
1c486a2b 286 val = evaluate_subexp (field_type, exp, pos, noside);
0694bce6
SC
287
288 /* Now actually set the field in struct_val. */
289
290 /* Assign val to field fieldno. */
291 if (VALUE_TYPE (val) != field_type)
292 val = value_cast (field_type, val);
293
294 bitsize = TYPE_FIELD_BITSIZE (substruct_type, subfieldno);
295 bitpos = TYPE_FIELD_BITPOS (struct_type, fieldno);
296 if (variantno >= 0)
297 bitpos += TYPE_FIELD_BITPOS (substruct_type, subfieldno);
298 addr = VALUE_CONTENTS (struct_val) + bitpos / 8;
299 if (bitsize)
300 modify_field (addr, value_as_long (val),
301 bitpos % 8, bitsize);
302 else
303 memcpy (addr, VALUE_CONTENTS (val),
304 TYPE_LENGTH (VALUE_TYPE (val)));
305 } while (--nlabels > 0);
306 }
307 return struct_val;
dcda44a0
PB
308}
309
bcbf388e
PB
310/* Recursive helper function for setting elements of array tuples for Chill.
311 The target is ARRAY (which has bounds LOW_BOUND to HIGH_BOUND);
312 the element value is ELEMENT;
313 EXP, POS and NOSIDE are as usual.
314 Evaluates index expresions and sets the specified element(s) of
315 ARRAY to ELEMENT.
316 Returns last index value. */
317
318static LONGEST
319init_array_element (array, element, exp, pos, noside, low_bound, high_bound)
320 value_ptr array, element;
321 register struct expression *exp;
322 register int *pos;
323 enum noside noside;
324{
325 LONGEST index;
326 int element_size = TYPE_LENGTH (VALUE_TYPE (element));
327 if (exp->elts[*pos].opcode == BINOP_COMMA)
328 {
329 (*pos)++;
330 init_array_element (array, element, exp, pos, noside,
331 low_bound, high_bound);
332 return init_array_element (array, element,
333 exp, pos, noside, low_bound, high_bound);
334 }
335 else if (exp->elts[*pos].opcode == BINOP_RANGE)
336 {
337 LONGEST low, high;
bcbf388e
PB
338 (*pos)++;
339 low = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
340 high = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
341 if (low < low_bound || high > high_bound)
342 error ("tuple range index out of range");
343 for (index = low ; index <= high; index++)
344 {
345 memcpy (VALUE_CONTENTS_RAW (array)
346 + (index - low_bound) * element_size,
347 VALUE_CONTENTS (element), element_size);
348 }
349 }
350 else
351 {
352 index = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
353 if (index < low_bound || index > high_bound)
354 error ("tuple index out of range");
355 memcpy (VALUE_CONTENTS_RAW (array) + (index - low_bound) * element_size,
356 VALUE_CONTENTS (element), element_size);
357 }
358 return index;
359}
360
7398958c
PB
361value_ptr
362evaluate_subexp_standard (expect_type, exp, pos, noside)
bd5635a1
RP
363 struct type *expect_type;
364 register struct expression *exp;
365 register int *pos;
366 enum noside noside;
367{
368 enum exp_opcode op;
1500864f 369 int tem, tem2, tem3;
40620258 370 register int pc, pc2 = 0, oldpos;
2d67c7e9 371 register value_ptr arg1 = NULL, arg2 = NULL, arg3;
01be6913 372 struct type *type;
bd5635a1 373 int nargs;
2d67c7e9 374 value_ptr *argvec;
2d67c7e9
PB
375 int upper, lower, retcode;
376 int code;
bd5635a1 377
764adcb4
JK
378 /* This expect_type crap should not be used for C. C expressions do
379 not have any notion of expected types, never has and (goddess
380 willing) never will. The C++ code uses it for some twisted
381 purpose (I haven't investigated but I suspect it just the usual
382 combination of Stroustrup figuring out some crazy language
383 feature and Tiemann figuring out some crazier way to try to
384 implement it). CHILL has the tuple stuff; I don't know enough
385 about CHILL to know whether expected types is the way to do it.
386 FORTRAN I don't know. */
dcda44a0
PB
387 if (exp->language_defn->la_language != language_cplus
388 && exp->language_defn->la_language != language_chill)
22b1c54a
JK
389 expect_type = NULL_TYPE;
390
bd5635a1
RP
391 pc = (*pos)++;
392 op = exp->elts[pc].opcode;
393
394 switch (op)
395 {
396 case OP_SCOPE:
a8a69e63 397 tem = longest_to_int (exp->elts[pc + 2].longconst);
1500864f 398 (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1);
01be6913 399 arg1 = value_struct_elt_for_reference (exp->elts[pc + 1].type,
8f86a4e4 400 0,
01be6913 401 exp->elts[pc + 1].type,
a8a69e63 402 &exp->elts[pc + 3].string,
01be6913 403 expect_type);
5f00ca54 404 if (arg1 == NULL)
a8a69e63 405 error ("There is no field named %s", &exp->elts[pc + 3].string);
5f00ca54 406 return arg1;
bd5635a1
RP
407
408 case OP_LONG:
409 (*pos) += 3;
2ccb3837 410 return value_from_longest (exp->elts[pc + 1].type,
a8a69e63 411 exp->elts[pc + 2].longconst);
bd5635a1
RP
412
413 case OP_DOUBLE:
414 (*pos) += 3;
415 return value_from_double (exp->elts[pc + 1].type,
416 exp->elts[pc + 2].doubleconst);
417
418 case OP_VAR_VALUE:
479fdd26 419 (*pos) += 3;
bd5635a1
RP
420 if (noside == EVAL_SKIP)
421 goto nosideret;
422 if (noside == EVAL_AVOID_SIDE_EFFECTS)
423 {
40620258 424 struct symbol * sym = exp->elts[pc + 2].symbol;
bd5635a1
RP
425 enum lval_type lv;
426
427 switch (SYMBOL_CLASS (sym))
428 {
429 case LOC_CONST:
430 case LOC_LABEL:
431 case LOC_CONST_BYTES:
432 lv = not_lval;
433 break;
434
435 case LOC_REGISTER:
436 case LOC_REGPARM:
437 lv = lval_register;
438 break;
439
440 default:
441 lv = lval_memory;
442 break;
443 }
444
445 return value_zero (SYMBOL_TYPE (sym), lv);
446 }
447 else
479fdd26
JK
448 return value_of_variable (exp->elts[pc + 2].symbol,
449 exp->elts[pc + 1].block);
bd5635a1
RP
450
451 case OP_LAST:
452 (*pos) += 2;
2ccb3837
JG
453 return
454 access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
bd5635a1
RP
455
456 case OP_REGISTER:
457 (*pos) += 2;
2ccb3837 458 return value_of_register (longest_to_int (exp->elts[pc + 1].longconst));
bd5635a1 459
e58de8a2
FF
460 case OP_BOOL:
461 (*pos) += 2;
b52cac6b 462 return value_from_longest (LA_BOOL_TYPE,
2d67c7e9 463 exp->elts[pc + 1].longconst);
e58de8a2 464
bd5635a1
RP
465 case OP_INTERNALVAR:
466 (*pos) += 2;
467 return value_of_internalvar (exp->elts[pc + 1].internalvar);
468
469 case OP_STRING:
a8a69e63 470 tem = longest_to_int (exp->elts[pc + 1].longconst);
1500864f 471 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
bd5635a1
RP
472 if (noside == EVAL_SKIP)
473 goto nosideret;
a8a69e63 474 return value_string (&exp->elts[pc + 2].string, tem);
bd5635a1 475
1500864f 476 case OP_BITSTRING:
6d34c236
PB
477 tem = longest_to_int (exp->elts[pc + 1].longconst);
478 (*pos)
479 += 3 + BYTES_TO_EXP_ELEM ((tem + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT);
480 if (noside == EVAL_SKIP)
481 goto nosideret;
482 return value_bitstring (&exp->elts[pc + 2].string, tem);
1500864f
JK
483 break;
484
485 case OP_ARRAY:
486 (*pos) += 3;
487 tem2 = longest_to_int (exp->elts[pc + 1].longconst);
488 tem3 = longest_to_int (exp->elts[pc + 2].longconst);
489 nargs = tem3 - tem2 + 1;
bcbf388e 490 type = expect_type ? check_typedef (expect_type) : NULL_TYPE;
2d67c7e9
PB
491
492 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
bcbf388e 493 && TYPE_CODE (type) == TYPE_CODE_STRUCT)
2d67c7e9
PB
494 {
495 value_ptr rec = allocate_value (expect_type);
bcbf388e 496 memset (VALUE_CONTENTS_RAW (rec), '\0', TYPE_LENGTH (type));
0694bce6 497 return evaluate_struct_tuple (rec, exp, pos, noside, nargs);
2d67c7e9
PB
498 }
499
500 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
bcbf388e 501 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
2d67c7e9 502 {
bcbf388e
PB
503 struct type *range_type = TYPE_FIELD_TYPE (type, 0);
504 struct type *element_type = TYPE_TARGET_TYPE (type);
f91a9e05 505 value_ptr array = allocate_value (expect_type);
bcbf388e
PB
506 int element_size = TYPE_LENGTH (check_typedef (element_type));
507 LONGEST low_bound, high_bound, index;
508 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
509 {
510 low_bound = 0;
511 high_bound = (TYPE_LENGTH (type) / element_size) - 1;
512 }
513 index = low_bound;
514 memset (VALUE_CONTENTS_RAW (array), 0, TYPE_LENGTH (expect_type));
515 for (tem = nargs; --nargs >= 0; )
2d67c7e9 516 {
bcbf388e
PB
517 value_ptr element;
518 int index_pc = 0;
519 if (exp->elts[*pos].opcode == BINOP_RANGE)
520 {
521 index_pc = ++(*pos);
522 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
523 }
524 element = evaluate_subexp (element_type, exp, pos, noside);
f91a9e05
PB
525 if (VALUE_TYPE (element) != element_type)
526 element = value_cast (element_type, element);
bcbf388e
PB
527 if (index_pc)
528 {
529 int continue_pc = *pos;
530 *pos = index_pc;
531 index = init_array_element (array, element, exp, pos, noside,
532 low_bound, high_bound);
533 *pos = continue_pc;
534 }
535 else
536 {
537 memcpy (VALUE_CONTENTS_RAW (array)
538 + (index - low_bound) * element_size,
539 VALUE_CONTENTS (element),
540 element_size);
541 }
542 index++;
2d67c7e9 543 }
f91a9e05 544 return array;
2d67c7e9
PB
545 }
546
dcda44a0 547 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
bcbf388e 548 && TYPE_CODE (type) == TYPE_CODE_SET)
dcda44a0
PB
549 {
550 value_ptr set = allocate_value (expect_type);
dcda44a0 551 char *valaddr = VALUE_CONTENTS_RAW (set);
bcbf388e
PB
552 struct type *element_type = TYPE_INDEX_TYPE (type);
553 LONGEST low_bound, high_bound;
554 if (get_discrete_bounds (element_type, &low_bound, &high_bound) < 0)
555 error ("(power)set type with unknown size");
556 memset (valaddr, '\0', TYPE_LENGTH (type));
dcda44a0
PB
557 for (tem = 0; tem < nargs; tem++)
558 {
bcbf388e
PB
559 LONGEST range_low, range_high;
560 value_ptr elem_val;
561 if (exp->elts[*pos].opcode == BINOP_RANGE)
562 {
563 (*pos)++;
564 elem_val = evaluate_subexp (element_type, exp, pos, noside);
565 range_low = value_as_long (elem_val);
566 elem_val = evaluate_subexp (element_type, exp, pos, noside);
567 range_high = value_as_long (elem_val);
568 }
569 else
570 {
571 elem_val = evaluate_subexp (element_type, exp, pos, noside);
572 range_low = range_high = value_as_long (elem_val);
573 }
574 if (range_low > range_high)
575 {
576 warning ("empty POWERSET tuple range");
577 continue;
578 }
579 if (range_low < low_bound || range_high > high_bound)
dcda44a0 580 error ("POWERSET tuple element out of range");
bcbf388e
PB
581 range_low -= low_bound;
582 range_high -= low_bound;
583 for ( ; range_low <= range_high; range_low++)
584 {
585 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
586 if (BITS_BIG_ENDIAN)
587 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
588 valaddr [(unsigned) range_low / TARGET_CHAR_BIT]
589 |= 1 << bit_index;
590 }
dcda44a0
PB
591 }
592 return set;
593 }
594
2d67c7e9 595 argvec = (value_ptr *) alloca (sizeof (value_ptr) * nargs);
1500864f
JK
596 for (tem = 0; tem < nargs; tem++)
597 {
598 /* Ensure that array expressions are coerced into pointer objects. */
599 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
600 }
601 if (noside == EVAL_SKIP)
602 goto nosideret;
2d67c7e9 603 return value_array (tem2, tem3, argvec);
1500864f 604
f91a9e05
PB
605 case TERNOP_SLICE:
606 {
607 value_ptr array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
608 int lowbound
609 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
610 int upper
611 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
bcbf388e
PB
612 if (noside == EVAL_SKIP)
613 goto nosideret;
f91a9e05
PB
614 return value_slice (array, lowbound, upper - lowbound + 1);
615 }
616
617 case TERNOP_SLICE_COUNT:
618 {
619 value_ptr array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
620 int lowbound
621 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
622 int length
623 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
624 return value_slice (array, lowbound, length);
625 }
626
bd5635a1
RP
627 case TERNOP_COND:
628 /* Skip third and second args to evaluate the first one. */
629 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
e58de8a2 630 if (value_logical_not (arg1))
bd5635a1
RP
631 {
632 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
633 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
634 }
635 else
636 {
637 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
638 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
639 return arg2;
640 }
641
642 case OP_FUNCALL:
643 (*pos) += 2;
644 op = exp->elts[*pos].opcode;
1c486a2b
PB
645 nargs = longest_to_int (exp->elts[pc + 1].longconst);
646 /* Allocate arg vector, including space for the function to be
647 called in argvec[0] and a terminating NULL */
648 argvec = (value_ptr *) alloca (sizeof (value_ptr) * (nargs + 3));
bd5635a1
RP
649 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
650 {
2d67c7e9 651 LONGEST fnptr;
bd5635a1 652
1c486a2b 653 nargs++;
bd5635a1
RP
654 /* First, evaluate the structure into arg2 */
655 pc2 = (*pos)++;
656
657 if (noside == EVAL_SKIP)
658 goto nosideret;
659
660 if (op == STRUCTOP_MEMBER)
661 {
662 arg2 = evaluate_subexp_for_address (exp, pos, noside);
663 }
664 else
665 {
666 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
667 }
668
669 /* If the function is a virtual function, then the
670 aggregate value (providing the structure) plays
671 its part by providing the vtable. Otherwise,
672 it is just along for the ride: call the function
673 directly. */
674
675 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
676
2d67c7e9 677 fnptr = value_as_long (arg1);
35fcebce
PB
678
679 if (METHOD_PTR_IS_VIRTUAL(fnptr))
bd5635a1 680 {
35fcebce 681 int fnoffset = METHOD_PTR_TO_VOFFSET(fnptr);
bd5635a1 682 struct type *basetype;
35fcebce
PB
683 struct type *domain_type =
684 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
bd5635a1
RP
685 int i, j;
686 basetype = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
35fcebce
PB
687 if (domain_type != basetype)
688 arg2 = value_cast(lookup_pointer_type (domain_type), arg2);
689 basetype = TYPE_VPTR_BASETYPE (domain_type);
bd5635a1
RP
690 for (i = TYPE_NFN_FIELDS (basetype) - 1; i >= 0; i--)
691 {
692 struct fn_field *f = TYPE_FN_FIELDLIST1 (basetype, i);
693 /* If one is virtual, then all are virtual. */
694 if (TYPE_FN_FIELD_VIRTUAL_P (f, 0))
695 for (j = TYPE_FN_FIELDLIST_LENGTH (basetype, i) - 1; j >= 0; --j)
b52cac6b 696 if ((int) TYPE_FN_FIELD_VOFFSET (f, j) == fnoffset)
bd5635a1 697 {
2d67c7e9 698 value_ptr temp = value_ind (arg2);
35fcebce
PB
699 arg1 = value_virtual_fn_field (&temp, f, j, domain_type, 0);
700 arg2 = value_addr (temp);
bd5635a1
RP
701 goto got_it;
702 }
703 }
704 if (i < 0)
35fcebce 705 error ("virtual function at index %d not found", fnoffset);
bd5635a1
RP
706 }
707 else
708 {
709 VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
710 }
711 got_it:
712
713 /* Now, say which argument to start evaluating from */
714 tem = 2;
715 }
716 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
717 {
718 /* Hair for method invocations */
719 int tem2;
720
1c486a2b 721 nargs++;
bd5635a1
RP
722 /* First, evaluate the structure into arg2 */
723 pc2 = (*pos)++;
a8a69e63 724 tem2 = longest_to_int (exp->elts[pc2 + 1].longconst);
1500864f 725 *pos += 3 + BYTES_TO_EXP_ELEM (tem2 + 1);
bd5635a1
RP
726 if (noside == EVAL_SKIP)
727 goto nosideret;
728
729 if (op == STRUCTOP_STRUCT)
730 {
479fdd26
JK
731 /* If v is a variable in a register, and the user types
732 v.method (), this will produce an error, because v has
733 no address.
734
735 A possible way around this would be to allocate a
736 copy of the variable on the stack, copy in the
737 contents, call the function, and copy out the
738 contents. I.e. convert this from call by reference
739 to call by copy-return (or whatever it's called).
740 However, this does not work because it is not the
741 same: the method being called could stash a copy of
742 the address, and then future uses through that address
743 (after the method returns) would be expected to
744 use the variable itself, not some copy of it. */
bd5635a1
RP
745 arg2 = evaluate_subexp_for_address (exp, pos, noside);
746 }
747 else
748 {
749 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
750 }
751 /* Now, say which argument to start evaluating from */
752 tem = 2;
753 }
754 else
755 {
1c486a2b
PB
756 argvec[0] = evaluate_subexp_with_coercion (exp, pos, noside);
757 tem = 1;
758 type = VALUE_TYPE (argvec[0]);
759 if (type && TYPE_CODE (type) == TYPE_CODE_PTR)
760 type = TYPE_TARGET_TYPE (type);
761 if (type && TYPE_CODE (type) == TYPE_CODE_FUNC)
762 {
763 for (; tem <= nargs && tem <= TYPE_NFIELDS (type); tem++)
764 {
765 argvec[tem] = evaluate_subexp (TYPE_FIELD_TYPE (type, tem-1),
766 exp, pos, noside);
767 }
768 }
bd5635a1 769 }
1c486a2b 770
bd5635a1 771 for (; tem <= nargs; tem++)
1c486a2b
PB
772 {
773 /* Ensure that array expressions are coerced into pointer objects. */
774
775 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
776 }
bd5635a1
RP
777
778 /* signal end of arglist */
779 argvec[tem] = 0;
780
781 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
782 {
783 int static_memfuncp;
2d67c7e9
PB
784 value_ptr temp = arg2;
785 char tstr[64];
bd5635a1
RP
786
787 argvec[1] = arg2;
40620258
KH
788 argvec[0] = 0;
789 strcpy(tstr, &exp->elts[pc2+2].string);
40620258 790 if (!argvec[0])
bd5635a1 791 {
40620258
KH
792 temp = arg2;
793 argvec[0] =
794 value_struct_elt (&temp, argvec+1, tstr,
795 &static_memfuncp,
796 op == STRUCTOP_STRUCT
797 ? "structure" : "structure pointer");
bd5635a1 798 }
40620258
KH
799 arg2 = value_from_longest (lookup_pointer_type(VALUE_TYPE (temp)),
800 VALUE_ADDRESS (temp)+VALUE_OFFSET (temp));
801 argvec[1] = arg2;
802
bd5635a1
RP
803 if (static_memfuncp)
804 {
805 argvec[1] = argvec[0];
806 nargs--;
807 argvec++;
808 }
809 }
810 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
811 {
812 argvec[1] = arg2;
813 argvec[0] = arg1;
814 }
815
ead95f8a
PB
816 do_call_it:
817
bd5635a1
RP
818 if (noside == EVAL_SKIP)
819 goto nosideret;
820 if (noside == EVAL_AVOID_SIDE_EFFECTS)
821 {
822 /* If the return type doesn't look like a function type, call an
823 error. This can happen if somebody tries to turn a variable into
824 a function call. This is here because people often want to
825 call, eg, strcmp, which gdb doesn't know is a function. If
826 gdb isn't asked for it's opinion (ie. through "whatis"),
827 it won't offer it. */
828
829 struct type *ftype =
830 TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0]));
831
832 if (ftype)
833 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0])));
834 else
835 error ("Expression of type other than \"Function returning ...\" used as function");
836 }
e17960fb 837 return call_function_by_hand (argvec[0], nargs, argvec + 1);
bd5635a1 838
2d67c7e9
PB
839 case OP_F77_UNDETERMINED_ARGLIST:
840
2d67c7e9
PB
841 /* Remember that in F77, functions, substring ops and
842 array subscript operations cannot be disambiguated
843 at parse time. We have made all array subscript operations,
844 substring operations as well as function calls come here
845 and we now have to discover what the heck this thing actually was.
7398958c 846 If it is a function, we process just as if we got an OP_FUNCALL. */
2d67c7e9 847
ead95f8a
PB
848 nargs = longest_to_int (exp->elts[pc+1].longconst);
849 (*pos) += 2;
2d67c7e9
PB
850
851 /* First determine the type code we are dealing with. */
ead95f8a 852 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
bcbf388e
PB
853 type = check_typedef (VALUE_TYPE (arg1));
854 code = TYPE_CODE (type);
2d67c7e9
PB
855
856 switch (code)
857 {
ead95f8a
PB
858 case TYPE_CODE_ARRAY:
859 goto multi_f77_subscript;
860
2d67c7e9 861 case TYPE_CODE_STRING:
ead95f8a 862 goto op_f77_substr;
2d67c7e9
PB
863
864 case TYPE_CODE_PTR:
865 case TYPE_CODE_FUNC:
ead95f8a
PB
866 /* It's a function call. */
867 /* Allocate arg vector, including space for the function to be
868 called in argvec[0] and a terminating NULL */
869 argvec = (value_ptr *) alloca (sizeof (value_ptr) * (nargs + 2));
870 argvec[0] = arg1;
871 tem = 1;
872 for (; tem <= nargs; tem++)
873 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
874 argvec[tem] = 0; /* signal end of arglist */
875 goto do_call_it;
2d67c7e9
PB
876
877 default:
878 error ("Cannot perform substring on this type");
879 }
880
ead95f8a 881 op_f77_substr:
2d67c7e9
PB
882 /* We have a substring operation on our hands here,
883 let us get the string we will be dealing with */
884
2d67c7e9
PB
885 /* Now evaluate the 'from' and 'to' */
886
887 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
888
ead95f8a
PB
889 if (nargs < 2)
890 return value_subscript (arg1, arg2);
891
2d67c7e9
PB
892 arg3 = evaluate_subexp_with_coercion (exp, pos, noside);
893
2d67c7e9
PB
894 if (noside == EVAL_SKIP)
895 goto nosideret;
896
bcbf388e 897 tem2 = value_as_long (arg2);
a56c9325 898 tem3 = value_as_long (arg3);
bcbf388e 899
ead95f8a 900 return value_slice (arg1, tem2, tem3 - tem2 + 1);
2d67c7e9 901
ead95f8a 902 case OP_COMPLEX:
2d67c7e9
PB
903 /* We have a complex number, There should be 2 floating
904 point numbers that compose it */
905 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
906 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
907
ead95f8a 908 return value_literal_complex (arg1, arg2, builtin_type_f_complex_s16);
2d67c7e9 909
bd5635a1 910 case STRUCTOP_STRUCT:
a8a69e63 911 tem = longest_to_int (exp->elts[pc + 1].longconst);
1500864f 912 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
bd5635a1
RP
913 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
914 if (noside == EVAL_SKIP)
915 goto nosideret;
916 if (noside == EVAL_AVOID_SIDE_EFFECTS)
917 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
a8a69e63 918 &exp->elts[pc + 2].string,
35fcebce 919 0),
bd5635a1
RP
920 lval_memory);
921 else
922 {
2d67c7e9
PB
923 value_ptr temp = arg1;
924 return value_struct_elt (&temp, NULL, &exp->elts[pc + 2].string,
925 NULL, "structure");
bd5635a1
RP
926 }
927
928 case STRUCTOP_PTR:
a8a69e63 929 tem = longest_to_int (exp->elts[pc + 1].longconst);
1500864f 930 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
bd5635a1
RP
931 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
932 if (noside == EVAL_SKIP)
933 goto nosideret;
934 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1500864f 935 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
a8a69e63 936 &exp->elts[pc + 2].string,
35fcebce 937 0),
bd5635a1
RP
938 lval_memory);
939 else
940 {
2d67c7e9
PB
941 value_ptr temp = arg1;
942 return value_struct_elt (&temp, NULL, &exp->elts[pc + 2].string,
943 NULL, "structure pointer");
bd5635a1
RP
944 }
945
cd10c7e3 946/* start-sanitize-gm */
bfe8f516 947#ifdef GENERAL_MAGIC
cd10c7e3
SG
948 case STRUCTOP_FIELD:
949 tem = longest_to_int (exp->elts[pc + 1].longconst);
950 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
951 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
952 if (noside == EVAL_SKIP)
953 goto nosideret;
954 {
955 CORE_ADDR object = value_as_long (arg1);
956 struct type *type = type_of_object (object);
957
958 if (noside == EVAL_AVOID_SIDE_EFFECTS)
959 return value_zero (lookup_struct_elt_type (type,
960 &exp->elts[pc + 2].string,
961 0),
962 lval_memory);
963 else
964 {
965 value_ptr temp = value_from_longest (builtin_type_unsigned_long,
966 baseptr_of_object (value_as_long(arg1)));
967
968 VALUE_TYPE (temp) = type;
969 return value_struct_elt (&temp, NULL, &exp->elts[pc + 2].string,
970 NULL, "structure pointer");
971 }
972 }
bfe8f516 973#endif /* GENERAL_MAGIC */
cd10c7e3
SG
974/* end-sanitize-gm */
975
bd5635a1
RP
976 case STRUCTOP_MEMBER:
977 arg1 = evaluate_subexp_for_address (exp, pos, noside);
01be6913 978 goto handle_pointer_to_member;
bd5635a1
RP
979 case STRUCTOP_MPTR:
980 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
01be6913 981 handle_pointer_to_member:
bd5635a1
RP
982 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
983 if (noside == EVAL_SKIP)
984 goto nosideret;
bcbf388e
PB
985 type = check_typedef (VALUE_TYPE (arg2));
986 if (TYPE_CODE (type) != TYPE_CODE_PTR)
01be6913 987 goto bad_pointer_to_member;
bcbf388e 988 type = check_typedef (TYPE_TARGET_TYPE (type));
01be6913
PB
989 if (TYPE_CODE (type) == TYPE_CODE_METHOD)
990 error ("not implemented: pointer-to-method in pointer-to-member construct");
991 if (TYPE_CODE (type) != TYPE_CODE_MEMBER)
992 goto bad_pointer_to_member;
bd5635a1 993 /* Now, convert these values to an address. */
01be6913
PB
994 arg1 = value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type)),
995 arg1);
996 arg3 = value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
997 value_as_long (arg1) + value_as_long (arg2));
bd5635a1 998 return value_ind (arg3);
01be6913
PB
999 bad_pointer_to_member:
1000 error("non-pointer-to-member value used in pointer-to-member construct");
bd5635a1 1001
1500864f
JK
1002 case BINOP_CONCAT:
1003 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1004 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1005 if (noside == EVAL_SKIP)
1006 goto nosideret;
1007 if (binop_user_defined_p (op, arg1, arg2))
1008 return value_x_binop (arg1, arg2, op, OP_NULL);
1009 else
1010 return value_concat (arg1, arg2);
1011
bd5635a1
RP
1012 case BINOP_ASSIGN:
1013 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1014 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1015 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1016 return arg1;
1017 if (binop_user_defined_p (op, arg1, arg2))
2ccb3837 1018 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
1019 else
1020 return value_assign (arg1, arg2);
1021
1022 case BINOP_ASSIGN_MODIFY:
1023 (*pos) += 2;
1024 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1025 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1026 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1027 return arg1;
1028 op = exp->elts[pc + 1].opcode;
1029 if (binop_user_defined_p (op, arg1, arg2))
1030 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op);
1031 else if (op == BINOP_ADD)
1032 arg2 = value_add (arg1, arg2);
1033 else if (op == BINOP_SUB)
1034 arg2 = value_sub (arg1, arg2);
1035 else
1036 arg2 = value_binop (arg1, arg2, op);
1037 return value_assign (arg1, arg2);
1038
1039 case BINOP_ADD:
1040 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1041 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1042 if (noside == EVAL_SKIP)
1043 goto nosideret;
1044 if (binop_user_defined_p (op, arg1, arg2))
2ccb3837 1045 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
1046 else
1047 return value_add (arg1, arg2);
1048
1049 case BINOP_SUB:
1050 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1051 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1052 if (noside == EVAL_SKIP)
1053 goto nosideret;
1054 if (binop_user_defined_p (op, arg1, arg2))
2ccb3837 1055 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
1056 else
1057 return value_sub (arg1, arg2);
1058
1059 case BINOP_MUL:
1060 case BINOP_DIV:
1061 case BINOP_REM:
76a0ffb4 1062 case BINOP_MOD:
bd5635a1
RP
1063 case BINOP_LSH:
1064 case BINOP_RSH:
e58de8a2
FF
1065 case BINOP_BITWISE_AND:
1066 case BINOP_BITWISE_IOR:
1067 case BINOP_BITWISE_XOR:
bd5635a1
RP
1068 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1069 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1070 if (noside == EVAL_SKIP)
1071 goto nosideret;
1072 if (binop_user_defined_p (op, arg1, arg2))
2ccb3837 1073 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
1074 else
1075 if (noside == EVAL_AVOID_SIDE_EFFECTS
76a0ffb4 1076 && (op == BINOP_DIV || op == BINOP_REM || op == BINOP_MOD))
bd5635a1
RP
1077 return value_zero (VALUE_TYPE (arg1), not_lval);
1078 else
1079 return value_binop (arg1, arg2, op);
1080
badefd28
PB
1081 case BINOP_RANGE:
1082 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1083 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1084 if (noside == EVAL_SKIP)
1085 goto nosideret;
1086 error ("':' operator used in invalid context");
1087
bd5635a1
RP
1088 case BINOP_SUBSCRIPT:
1089 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1090 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1091 if (noside == EVAL_SKIP)
1092 goto nosideret;
1093 if (noside == EVAL_AVOID_SIDE_EFFECTS)
35fcebce
PB
1094 {
1095 /* If the user attempts to subscript something that has no target
1096 type (like a plain int variable for example), then report this
1097 as an error. */
1098
bcbf388e 1099 type = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (arg1)));
35fcebce
PB
1100 if (type)
1101 return value_zero (type, VALUE_LVAL (arg1));
1102 else
1103 error ("cannot subscript something of type `%s'",
1104 TYPE_NAME (VALUE_TYPE (arg1)));
1105 }
bd5635a1
RP
1106
1107 if (binop_user_defined_p (op, arg1, arg2))
2ccb3837 1108 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
1109 else
1110 return value_subscript (arg1, arg2);
2d67c7e9
PB
1111
1112 case BINOP_IN:
1113 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1114 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1115 if (noside == EVAL_SKIP)
1116 goto nosideret;
1117 return value_in (arg1, arg2);
bd5635a1 1118
54bbbfb4
FF
1119 case MULTI_SUBSCRIPT:
1120 (*pos) += 2;
1121 nargs = longest_to_int (exp->elts[pc + 1].longconst);
1122 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1123 while (nargs-- > 0)
1124 {
1125 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1126 /* FIXME: EVAL_SKIP handling may not be correct. */
1127 if (noside == EVAL_SKIP)
1128 {
1129 if (nargs > 0)
1130 {
1131 continue;
1132 }
1133 else
1134 {
1135 goto nosideret;
1136 }
1137 }
1138 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
1139 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1140 {
1141 /* If the user attempts to subscript something that has no target
1142 type (like a plain int variable for example), then report this
1143 as an error. */
1144
bcbf388e 1145 type = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (arg1)));
54bbbfb4
FF
1146 if (type != NULL)
1147 {
1148 arg1 = value_zero (type, VALUE_LVAL (arg1));
1149 noside = EVAL_SKIP;
1150 continue;
1151 }
1152 else
1153 {
1154 error ("cannot subscript something of type `%s'",
1155 TYPE_NAME (VALUE_TYPE (arg1)));
1156 }
1157 }
1158
7398958c 1159 if (binop_user_defined_p (op, arg1, arg2))
54bbbfb4
FF
1160 {
1161 arg1 = value_x_binop (arg1, arg2, op, OP_NULL);
1162 }
1163 else
1164 {
1165 arg1 = value_subscript (arg1, arg2);
1166 }
1167 }
1168 return (arg1);
1169
ead95f8a 1170 multi_f77_subscript:
2d67c7e9
PB
1171 {
1172 int subscript_array[MAX_FORTRAN_DIMS+1]; /* 1-based array of
1173 subscripts, max == 7 */
1174 int array_size_array[MAX_FORTRAN_DIMS+1];
1175 int ndimensions=1,i;
1176 struct type *tmp_type;
1177 int offset_item; /* The array offset where the item lives */
2d67c7e9 1178
2d67c7e9
PB
1179 if (nargs > MAX_FORTRAN_DIMS)
1180 error ("Too many subscripts for F77 (%d Max)", MAX_FORTRAN_DIMS);
bcbf388e
PB
1181
1182 tmp_type = check_typedef (VALUE_TYPE (arg1));
1183 ndimensions = calc_f77_array_dims (type);
2d67c7e9
PB
1184
1185 if (nargs != ndimensions)
1186 error ("Wrong number of subscripts");
1187
1188 /* Now that we know we have a legal array subscript expression
1189 let us actually find out where this element exists in the array. */
1190
2d67c7e9
PB
1191 offset_item = 0;
1192 for (i = 1; i <= nargs; i++)
1193 {
1194 /* Evaluate each subscript, It must be a legal integer in F77 */
1195 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1196
2d67c7e9
PB
1197 /* Fill in the subscript and array size arrays */
1198
badefd28 1199 subscript_array[i] = value_as_long (arg2);
2d67c7e9
PB
1200
1201 retcode = f77_get_dynamic_upperbound (tmp_type, &upper);
1202 if (retcode == BOUND_FETCH_ERROR)
1203 error ("Cannot obtain dynamic upper bound");
1204
1205 retcode = f77_get_dynamic_lowerbound (tmp_type, &lower);
1206 if (retcode == BOUND_FETCH_ERROR)
1207 error("Cannot obtain dynamic lower bound");
1208
1209 array_size_array[i] = upper - lower + 1;
1210
1211 /* Zero-normalize subscripts so that offsetting will work. */
1212
1213 subscript_array[i] -= lower;
1214
1215 /* If we are at the bottom of a multidimensional
1216 array type then keep a ptr to the last ARRAY
1217 type around for use when calling value_subscript()
1218 below. This is done because we pretend to value_subscript
1219 that we actually have a one-dimensional array
1220 of base element type that we apply a simple
1221 offset to. */
1222
1223 if (i < nargs)
bcbf388e 1224 tmp_type = check_typedef (TYPE_TARGET_TYPE (tmp_type));
2d67c7e9
PB
1225 }
1226
1227 /* Now let us calculate the offset for this item */
1228
1229 offset_item = subscript_array[ndimensions];
1230
1231 for (i = ndimensions - 1; i >= 1; i--)
1232 offset_item =
1233 array_size_array[i] * offset_item + subscript_array[i];
1234
1235 /* Construct a value node with the value of the offset */
1236
1237 arg2 = value_from_longest (builtin_type_f_integer, offset_item);
1238
1239 /* Let us now play a dirty trick: we will take arg1
1240 which is a value node pointing to the topmost level
1241 of the multidimensional array-set and pretend
1242 that it is actually a array of the final element
1243 type, this will ensure that value_subscript()
1244 returns the correct type value */
1245
1246 VALUE_TYPE (arg1) = tmp_type;
7398958c 1247 return value_ind (value_add (value_coerce_array (arg1), arg2));
2d67c7e9
PB
1248 }
1249
e58de8a2 1250 case BINOP_LOGICAL_AND:
bd5635a1
RP
1251 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1252 if (noside == EVAL_SKIP)
1253 {
1254 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1255 goto nosideret;
1256 }
1257
1258 oldpos = *pos;
1259 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1260 *pos = oldpos;
1261
1262 if (binop_user_defined_p (op, arg1, arg2))
1263 {
1264 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2ccb3837 1265 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
1266 }
1267 else
1268 {
e58de8a2 1269 tem = value_logical_not (arg1);
bd5635a1
RP
1270 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
1271 (tem ? EVAL_SKIP : noside));
a366d882 1272 return value_from_longest (LA_BOOL_TYPE,
e58de8a2 1273 (LONGEST) (!tem && !value_logical_not (arg2)));
bd5635a1
RP
1274 }
1275
e58de8a2 1276 case BINOP_LOGICAL_OR:
bd5635a1
RP
1277 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1278 if (noside == EVAL_SKIP)
1279 {
1280 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1281 goto nosideret;
1282 }
1283
1284 oldpos = *pos;
1285 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1286 *pos = oldpos;
1287
1288 if (binop_user_defined_p (op, arg1, arg2))
1289 {
1290 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2ccb3837 1291 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
1292 }
1293 else
1294 {
e58de8a2 1295 tem = value_logical_not (arg1);
bd5635a1
RP
1296 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
1297 (!tem ? EVAL_SKIP : noside));
a366d882 1298 return value_from_longest (LA_BOOL_TYPE,
e58de8a2 1299 (LONGEST) (!tem || !value_logical_not (arg2)));
bd5635a1
RP
1300 }
1301
1302 case BINOP_EQUAL:
1303 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1304 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1305 if (noside == EVAL_SKIP)
1306 goto nosideret;
1307 if (binop_user_defined_p (op, arg1, arg2))
1308 {
2ccb3837 1309 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
1310 }
1311 else
1312 {
1313 tem = value_equal (arg1, arg2);
a366d882 1314 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
bd5635a1
RP
1315 }
1316
1317 case BINOP_NOTEQUAL:
1318 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1319 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1320 if (noside == EVAL_SKIP)
1321 goto nosideret;
1322 if (binop_user_defined_p (op, arg1, arg2))
1323 {
2ccb3837 1324 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
1325 }
1326 else
1327 {
1328 tem = value_equal (arg1, arg2);
a366d882 1329 return value_from_longest (LA_BOOL_TYPE, (LONGEST) ! tem);
bd5635a1
RP
1330 }
1331
1332 case BINOP_LESS:
1333 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1334 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1335 if (noside == EVAL_SKIP)
1336 goto nosideret;
1337 if (binop_user_defined_p (op, arg1, arg2))
1338 {
2ccb3837 1339 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
1340 }
1341 else
1342 {
1343 tem = value_less (arg1, arg2);
a366d882 1344 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
bd5635a1
RP
1345 }
1346
1347 case BINOP_GTR:
1348 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1349 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1350 if (noside == EVAL_SKIP)
1351 goto nosideret;
1352 if (binop_user_defined_p (op, arg1, arg2))
1353 {
2ccb3837 1354 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
1355 }
1356 else
1357 {
1358 tem = value_less (arg2, arg1);
a366d882 1359 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
bd5635a1
RP
1360 }
1361
1362 case BINOP_GEQ:
1363 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1364 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1365 if (noside == EVAL_SKIP)
1366 goto nosideret;
1367 if (binop_user_defined_p (op, arg1, arg2))
1368 {
2ccb3837 1369 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
1370 }
1371 else
1372 {
8f86a4e4 1373 tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
a366d882 1374 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
bd5635a1
RP
1375 }
1376
1377 case BINOP_LEQ:
1378 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1379 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
1380 if (noside == EVAL_SKIP)
1381 goto nosideret;
1382 if (binop_user_defined_p (op, arg1, arg2))
1383 {
2ccb3837 1384 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
1385 }
1386 else
1387 {
8f86a4e4 1388 tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
a366d882 1389 return value_from_longest (LA_BOOL_TYPE, (LONGEST) tem);
bd5635a1
RP
1390 }
1391
1392 case BINOP_REPEAT:
1393 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1394 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1395 if (noside == EVAL_SKIP)
1396 goto nosideret;
1397 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT)
1398 error ("Non-integral right operand for \"@\" operator.");
1399 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2b576293 1400 {
2b576293
C
1401 return allocate_repeat_value (VALUE_TYPE (arg1),
1402 longest_to_int (value_as_long (arg2)));
1403 }
bd5635a1 1404 else
2ccb3837 1405 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
bd5635a1
RP
1406
1407 case BINOP_COMMA:
1408 evaluate_subexp (NULL_TYPE, exp, pos, noside);
1409 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1410
1411 case UNOP_NEG:
1412 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1413 if (noside == EVAL_SKIP)
1414 goto nosideret;
1415 if (unop_user_defined_p (op, arg1))
1416 return value_x_unop (arg1, op);
1417 else
1418 return value_neg (arg1);
1419
e58de8a2 1420 case UNOP_COMPLEMENT:
5f00ca54
JK
1421 /* C++: check for and handle destructor names. */
1422 op = exp->elts[*pos].opcode;
1423
bd5635a1
RP
1424 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1425 if (noside == EVAL_SKIP)
1426 goto nosideret;
e58de8a2
FF
1427 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
1428 return value_x_unop (arg1, UNOP_COMPLEMENT);
bd5635a1 1429 else
e58de8a2 1430 return value_complement (arg1);
bd5635a1 1431
e58de8a2 1432 case UNOP_LOGICAL_NOT:
bd5635a1
RP
1433 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1434 if (noside == EVAL_SKIP)
1435 goto nosideret;
1436 if (unop_user_defined_p (op, arg1))
1437 return value_x_unop (arg1, op);
1438 else
2ccb3837 1439 return value_from_longest (builtin_type_int,
e58de8a2 1440 (LONGEST) value_logical_not (arg1));
bd5635a1
RP
1441
1442 case UNOP_IND:
1443 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
bcbf388e 1444 expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
bd5635a1
RP
1445 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1446 if (noside == EVAL_SKIP)
1447 goto nosideret;
1448 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1449 {
bcbf388e
PB
1450 type = check_typedef (VALUE_TYPE (arg1));
1451 if (TYPE_CODE (type) == TYPE_CODE_PTR
1452 || TYPE_CODE (type) == TYPE_CODE_REF
bd5635a1 1453 /* In C you can dereference an array to get the 1st elt. */
bcbf388e 1454 || TYPE_CODE (type) == TYPE_CODE_ARRAY
bd5635a1 1455 )
bcbf388e 1456 return value_zero (TYPE_TARGET_TYPE (type),
bd5635a1 1457 lval_memory);
bcbf388e 1458 else if (TYPE_CODE (type) == TYPE_CODE_INT)
bd5635a1
RP
1459 /* GDB allows dereferencing an int. */
1460 return value_zero (builtin_type_int, lval_memory);
1461 else
1462 error ("Attempt to take contents of a non-pointer value.");
1463 }
1464 return value_ind (arg1);
1465
1466 case UNOP_ADDR:
1467 /* C++: check for and handle pointer to members. */
1468
1469 op = exp->elts[*pos].opcode;
1470
1471 if (noside == EVAL_SKIP)
1472 {
1473 if (op == OP_SCOPE)
1474 {
a8a69e63 1475 int temm = longest_to_int (exp->elts[pc+3].longconst);
1500864f 1476 (*pos) += 3 + BYTES_TO_EXP_ELEM (temm + 1);
bd5635a1
RP
1477 }
1478 else
1479 evaluate_subexp (expect_type, exp, pos, EVAL_SKIP);
1480 goto nosideret;
1481 }
1482
01be6913 1483 return evaluate_subexp_for_address (exp, pos, noside);
bd5635a1
RP
1484
1485 case UNOP_SIZEOF:
1486 if (noside == EVAL_SKIP)
1487 {
1488 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1489 goto nosideret;
1490 }
1491 return evaluate_subexp_for_sizeof (exp, pos);
1492
1493 case UNOP_CAST:
1494 (*pos) += 2;
2d67c7e9
PB
1495 type = exp->elts[pc + 1].type;
1496 arg1 = evaluate_subexp (type, exp, pos, noside);
bd5635a1
RP
1497 if (noside == EVAL_SKIP)
1498 goto nosideret;
2d67c7e9
PB
1499 if (type != VALUE_TYPE (arg1))
1500 arg1 = value_cast (type, arg1);
1501 return arg1;
bd5635a1
RP
1502
1503 case UNOP_MEMVAL:
1504 (*pos) += 2;
1505 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1506 if (noside == EVAL_SKIP)
1507 goto nosideret;
1508 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1509 return value_zero (exp->elts[pc + 1].type, lval_memory);
1510 else
1511 return value_at_lazy (exp->elts[pc + 1].type,
2ccb3837 1512 value_as_pointer (arg1));
bd5635a1
RP
1513
1514 case UNOP_PREINCREMENT:
1515 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1516 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1517 return arg1;
1518 else if (unop_user_defined_p (op, arg1))
1519 {
1520 return value_x_unop (arg1, op);
1521 }
1522 else
1523 {
2ccb3837 1524 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
bd5635a1
RP
1525 (LONGEST) 1));
1526 return value_assign (arg1, arg2);
1527 }
1528
1529 case UNOP_PREDECREMENT:
1530 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1531 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1532 return arg1;
1533 else if (unop_user_defined_p (op, arg1))
1534 {
1535 return value_x_unop (arg1, op);
1536 }
1537 else
1538 {
2ccb3837 1539 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
bd5635a1
RP
1540 (LONGEST) 1));
1541 return value_assign (arg1, arg2);
1542 }
1543
1544 case UNOP_POSTINCREMENT:
1545 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1546 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1547 return arg1;
1548 else if (unop_user_defined_p (op, arg1))
1549 {
1550 return value_x_unop (arg1, op);
1551 }
1552 else
1553 {
2ccb3837 1554 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
bd5635a1
RP
1555 (LONGEST) 1));
1556 value_assign (arg1, arg2);
1557 return arg1;
1558 }
1559
1560 case UNOP_POSTDECREMENT:
1561 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1562 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1563 return arg1;
1564 else if (unop_user_defined_p (op, arg1))
1565 {
1566 return value_x_unop (arg1, op);
1567 }
1568 else
1569 {
2ccb3837 1570 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
bd5635a1
RP
1571 (LONGEST) 1));
1572 value_assign (arg1, arg2);
1573 return arg1;
1574 }
1575
1576 case OP_THIS:
1577 (*pos) += 1;
1578 return value_of_this (1);
1579
1500864f
JK
1580 case OP_TYPE:
1581 error ("Attempt to use a type name as an expression");
1582
bd5635a1 1583 default:
1500864f
JK
1584 /* Removing this case and compiling with gcc -Wall reveals that
1585 a lot of cases are hitting this case. Some of these should
1586 probably be removed from expression.h (e.g. do we need a BINOP_SCOPE
1587 and an OP_SCOPE?); others are legitimate expressions which are
1588 (apparently) not fully implemented.
1589
1590 If there are any cases landing here which mean a user error,
1591 then they should be separate cases, with more descriptive
1592 error messages. */
1593
1594 error ("\
2d67c7e9 1595GDB does not (yet) know how to evaluate that kind of expression");
bd5635a1
RP
1596 }
1597
1598 nosideret:
2ccb3837 1599 return value_from_longest (builtin_type_long, (LONGEST) 1);
bd5635a1
RP
1600}
1601\f
1602/* Evaluate a subexpression of EXP, at index *POS,
1603 and return the address of that subexpression.
1604 Advance *POS over the subexpression.
1605 If the subexpression isn't an lvalue, get an error.
1606 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
1607 then only the type of the result need be correct. */
1608
2d67c7e9 1609static value_ptr
bd5635a1
RP
1610evaluate_subexp_for_address (exp, pos, noside)
1611 register struct expression *exp;
1612 register int *pos;
1613 enum noside noside;
1614{
1615 enum exp_opcode op;
1616 register int pc;
e17960fb 1617 struct symbol *var;
bd5635a1
RP
1618
1619 pc = (*pos);
1620 op = exp->elts[pc].opcode;
1621
1622 switch (op)
1623 {
1624 case UNOP_IND:
1625 (*pos)++;
1626 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1627
1628 case UNOP_MEMVAL:
1629 (*pos) += 3;
1630 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
1631 evaluate_subexp (NULL_TYPE, exp, pos, noside));
1632
1633 case OP_VAR_VALUE:
479fdd26 1634 var = exp->elts[pc + 2].symbol;
e17960fb
JG
1635
1636 /* C++: The "address" of a reference should yield the address
1637 * of the object pointed to. Let value_addr() deal with it. */
1638 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_REF)
1639 goto default_case;
1640
479fdd26 1641 (*pos) += 4;
bd5635a1
RP
1642 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1643 {
1644 struct type *type =
e17960fb
JG
1645 lookup_pointer_type (SYMBOL_TYPE (var));
1646 enum address_class sym_class = SYMBOL_CLASS (var);
bd5635a1
RP
1647
1648 if (sym_class == LOC_CONST
1649 || sym_class == LOC_CONST_BYTES
1650 || sym_class == LOC_REGISTER
1651 || sym_class == LOC_REGPARM)
1652 error ("Attempt to take address of register or constant.");
1653
1654 return
1655 value_zero (type, not_lval);
1656 }
1657 else
479fdd26
JK
1658 return
1659 locate_var_value
1660 (var,
1661 block_innermost_frame (exp->elts[pc + 1].block));
bd5635a1
RP
1662
1663 default:
e17960fb 1664 default_case:
bd5635a1
RP
1665 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1666 {
2d67c7e9 1667 value_ptr x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
bd5635a1 1668 if (VALUE_LVAL (x) == lval_memory)
0a5d35ed 1669 return value_zero (lookup_pointer_type (VALUE_TYPE (x)),
bd5635a1
RP
1670 not_lval);
1671 else
1672 error ("Attempt to take address of non-lval");
1673 }
1674 return value_addr (evaluate_subexp (NULL_TYPE, exp, pos, noside));
1675 }
1676}
1677
1678/* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
fb6e675f
FF
1679 When used in contexts where arrays will be coerced anyway, this is
1680 equivalent to `evaluate_subexp' but much faster because it avoids
479fdd26
JK
1681 actually fetching array contents (perhaps obsolete now that we have
1682 VALUE_LAZY).
fb6e675f
FF
1683
1684 Note that we currently only do the coercion for C expressions, where
1685 arrays are zero based and the coercion is correct. For other languages,
1686 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
1687 to decide if coercion is appropriate.
1688
479fdd26 1689 */
bd5635a1 1690
7398958c 1691value_ptr
bd5635a1
RP
1692evaluate_subexp_with_coercion (exp, pos, noside)
1693 register struct expression *exp;
1694 register int *pos;
1695 enum noside noside;
1696{
1697 register enum exp_opcode op;
1698 register int pc;
2d67c7e9 1699 register value_ptr val;
e17960fb 1700 struct symbol *var;
bd5635a1
RP
1701
1702 pc = (*pos);
1703 op = exp->elts[pc].opcode;
1704
1705 switch (op)
1706 {
1707 case OP_VAR_VALUE:
479fdd26 1708 var = exp->elts[pc + 2].symbol;
bcbf388e 1709 if (TYPE_CODE (check_typedef (SYMBOL_TYPE (var))) == TYPE_CODE_ARRAY
fb6e675f 1710 && CAST_IS_CONVERSION)
bd5635a1 1711 {
479fdd26
JK
1712 (*pos) += 4;
1713 val =
1714 locate_var_value
1715 (var, block_innermost_frame (exp->elts[pc + 1].block));
e17960fb 1716 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (var))),
bd5635a1
RP
1717 val);
1718 }
479fdd26
JK
1719 /* FALLTHROUGH */
1720
1721 default:
1722 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
bd5635a1
RP
1723 }
1724}
1725
1726/* Evaluate a subexpression of EXP, at index *POS,
1727 and return a value for the size of that subexpression.
1728 Advance *POS over the subexpression. */
1729
2d67c7e9 1730static value_ptr
bd5635a1
RP
1731evaluate_subexp_for_sizeof (exp, pos)
1732 register struct expression *exp;
1733 register int *pos;
1734{
1735 enum exp_opcode op;
1736 register int pc;
bcbf388e 1737 struct type *type;
2d67c7e9 1738 value_ptr val;
bd5635a1
RP
1739
1740 pc = (*pos);
1741 op = exp->elts[pc].opcode;
1742
1743 switch (op)
1744 {
1745 /* This case is handled specially
1746 so that we avoid creating a value for the result type.
1747 If the result type is very big, it's desirable not to
1748 create a value unnecessarily. */
1749 case UNOP_IND:
1750 (*pos)++;
1751 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
bcbf388e
PB
1752 type = check_typedef (VALUE_TYPE (val));
1753 type = check_typedef (TYPE_TARGET_TYPE (type));
2ccb3837 1754 return value_from_longest (builtin_type_int, (LONGEST)
bcbf388e 1755 TYPE_LENGTH (type));
bd5635a1
RP
1756
1757 case UNOP_MEMVAL:
1758 (*pos) += 3;
bcbf388e
PB
1759 type = check_typedef (exp->elts[pc + 1].type);
1760 return value_from_longest (builtin_type_int,
1761 (LONGEST) TYPE_LENGTH (type));
bd5635a1
RP
1762
1763 case OP_VAR_VALUE:
479fdd26 1764 (*pos) += 4;
bcbf388e 1765 type = check_typedef (SYMBOL_TYPE (exp->elts[pc + 2].symbol));
479fdd26 1766 return
bcbf388e 1767 value_from_longest (builtin_type_int, (LONGEST) TYPE_LENGTH (type));
bd5635a1
RP
1768
1769 default:
1770 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2ccb3837 1771 return value_from_longest (builtin_type_int,
bd5635a1
RP
1772 (LONGEST) TYPE_LENGTH (VALUE_TYPE (val)));
1773 }
1774}
0a5d35ed
SG
1775
1776/* Parse a type expression in the string [P..P+LENGTH). */
1777
1778struct type *
1779parse_and_eval_type (p, length)
1780 char *p;
1781 int length;
1782{
1783 char *tmp = (char *)alloca (length + 4);
1784 struct expression *expr;
1785 tmp[0] = '(';
35fcebce 1786 memcpy (tmp+1, p, length);
0a5d35ed
SG
1787 tmp[length+1] = ')';
1788 tmp[length+2] = '0';
1789 tmp[length+3] = '\0';
1790 expr = parse_expression (tmp);
1791 if (expr->elts[0].opcode != UNOP_CAST)
1792 error ("Internal error in eval_type.");
1793 return expr->elts[1].type;
1794}
2d67c7e9
PB
1795
1796int
1797calc_f77_array_dims (array_type)
1798 struct type *array_type;
1799{
1800 int ndimen = 1;
1801 struct type *tmp_type;
1802
1803 if ((TYPE_CODE(array_type) != TYPE_CODE_ARRAY))
1804 error ("Can't get dimensions for a non-array type");
1805
1806 tmp_type = array_type;
1807
477b2425 1808 while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
2d67c7e9
PB
1809 {
1810 if (TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY)
1811 ++ndimen;
1812 }
1813 return ndimen;
1814}
This page took 0.323711 seconds and 4 git commands to generate.