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