(Changes from Daniel Berlin, with revisions by Jim Blandy.)
[external/binutils.git] / gdb / value.h
1 /* Definitions for values of C expressions, for GDB.
2    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3    1996, 1997, 1998, 1999, 2000, 2001
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #if !defined (VALUE_H)
24 #define VALUE_H 1
25
26 /*
27  * The structure which defines the type of a value.  It should never
28  * be possible for a program lval value to survive over a call to the inferior
29  * (ie to be put into the history list or an internal variable).
30  */
31
32 struct value
33   {
34     /* Type of value; either not an lval, or one of the various
35        different possible kinds of lval.  */
36     enum lval_type lval;
37     /* Is it modifiable?  Only relevant if lval != not_lval.  */
38     int modifiable;
39     /* Location of value (if lval).  */
40     union
41       {
42         /* If lval == lval_memory, this is the address in the inferior.
43            If lval == lval_register, this is the byte offset into the
44            registers structure.  */
45         CORE_ADDR address;
46         /* Pointer to internal variable.  */
47         struct internalvar *internalvar;
48         /* Number of register.  Only used with
49            lval_reg_frame_relative.  */
50         int regnum;
51       }
52     location;
53     /* Describes offset of a value within lval of a structure in bytes.
54        If lval == lval_memory, this is an offset to the address.
55        If lval == lval_register, this is a further offset from
56           location.address within the registers structure.  
57        Note also the member embedded_offset below.  */
58     int offset;
59     /* Only used for bitfields; number of bits contained in them.  */
60     int bitsize;
61     /* Only used for bitfields; position of start of field.
62        For BITS_BIG_ENDIAN=0 targets, it is the position of the LSB.
63        For BITS_BIG_ENDIAN=1 targets, it is the position of the MSB. */
64     int bitpos;
65     /* Frame value is relative to.  In practice, this address is only
66        used if the value is stored in several registers in other than
67        the current frame, and these registers have not all been saved
68        at the same place in memory.  This will be described in the
69        lval enum above as "lval_reg_frame_relative".  */
70     CORE_ADDR frame_addr;
71     /* Type of the value.  */
72     struct type *type;
73     /* Type of the enclosing object if this is an embedded subobject.
74        The member embedded_offset gives the real position of the subobject
75        if type is not the same as enclosing_type.
76
77        If the type field is a pointer type, then enclosing_type is 
78        a pointer type pointing to the real (enclosing) type of the target
79        object. */
80     struct type *enclosing_type;
81     /* Values are stored in a chain, so that they can be deleted
82        easily over calls to the inferior.  Values assigned to internal
83        variables or put into the value history are taken off this
84        list.  */
85     struct value *next;
86
87     /* ??? When is this used?  */
88     union
89       {
90         CORE_ADDR memaddr;
91         char *myaddr;
92       }
93     substring_addr;
94
95     /* Register number if the value is from a register.  Is not kept
96        if you take a field of a structure that is stored in a
97        register.  Shouldn't it be?  */
98     short regno;
99     /* If zero, contents of this value are in the contents field.
100        If nonzero, contents are in inferior memory at address
101        in the location.address field plus the offset field
102        (and the lval field should be lval_memory).
103
104        WARNING: This field is used by the code which handles
105        watchpoints (see breakpoint.c) to decide whether a particular
106        value can be watched by hardware watchpoints.  If the lazy flag
107        is set for some member of a value chain, it is assumed that
108        this member of the chain doesn't need to be watched as part of
109        watching the value itself.  This is how GDB avoids watching the
110        entire struct or array when the user wants to watch a single
111        struct member or array element.  If you ever change the way
112        lazy flag is set and reset, be sure to consider this use as
113        well!  */
114     char lazy;
115     /* If nonzero, this is the value of a variable which does not
116        actually exist in the program.  */
117     char optimized_out;
118     /* If this value represents an object that is embedded inside a
119        larger object (e.g., a base subobject in C++), this gives the
120        offset (in bytes) from the start of the contents buffer where
121        the embedded object begins. This is required because some C++
122        runtime implementations lay out objects (especially virtual bases
123        with possibly negative offsets to ancestors).
124        Note: This may be positive or negative! Also note that this offset
125        is not used when retrieving contents from target memory; the entire
126        enclosing object has to be retrieved always, and the offset for
127        that is given by the member offset above. */
128     int embedded_offset;
129     /* If this value represents a pointer to an object that is embedded
130        in another object, this gives the embedded_offset of the object
131        that is pointed to. */
132     int pointed_to_offset;
133     /* The BFD section associated with this value.  */
134     asection *bfd_section;
135     /* Actual contents of the value.  For use of this value; setting
136        it uses the stuff above.  Not valid if lazy is nonzero.
137        Target byte-order.  We force it to be aligned properly for any
138        possible value.  Note that a value therefore extends beyond
139        what is declared here.  */
140     union
141       {
142         long contents[1];
143         double force_double_align;
144         LONGEST force_longlong_align;
145         char *literal_data;
146       }
147     aligner;
148     /* Do not add any new members here -- contents above will trash them */
149   };
150
151 typedef struct value *value_ptr;
152
153 #define VALUE_TYPE(val) (val)->type
154 #define VALUE_ENCLOSING_TYPE(val) (val)->enclosing_type
155 #define VALUE_LAZY(val) (val)->lazy
156 /* VALUE_CONTENTS and VALUE_CONTENTS_RAW both return the address of
157    the gdb buffer used to hold a copy of the contents of the lval.  
158    VALUE_CONTENTS is used when the contents of the buffer are needed --
159    it uses value_fetch_lazy() to load the buffer from the process being 
160    debugged if it hasn't already been loaded.  VALUE_CONTENTS_RAW is 
161    used when data is being stored into the buffer, or when it is 
162    certain that the contents of the buffer are valid.
163    Note: The contents pointer is adjusted by the offset required to
164    get to the real subobject, if the value happens to represent
165    something embedded in a larger run-time object. */
166
167 #define VALUE_CONTENTS_RAW(val) ((char *) (val)->aligner.contents + (val)->embedded_offset)
168 #define VALUE_CONTENTS(val) ((void)(VALUE_LAZY(val) && value_fetch_lazy(val)),\
169                              VALUE_CONTENTS_RAW(val))
170
171 /* The ALL variants of the above two macros do not adjust the returned
172    pointer by the embedded_offset value. */
173
174 #define VALUE_CONTENTS_ALL_RAW(val) ((char *) (val)->aligner.contents)
175 #define VALUE_CONTENTS_ALL(val) ((void) (VALUE_LAZY(val) && value_fetch_lazy(val)),\
176                                  VALUE_CONTENTS_ALL_RAW(val))
177
178
179 extern int value_fetch_lazy (value_ptr val);
180
181 #define VALUE_LVAL(val) (val)->lval
182 #define VALUE_ADDRESS(val) (val)->location.address
183 #define VALUE_INTERNALVAR(val) (val)->location.internalvar
184 #define VALUE_FRAME_REGNUM(val) ((val)->location.regnum)
185 #define VALUE_FRAME(val) ((val)->frame_addr)
186 #define VALUE_OFFSET(val) (val)->offset
187 #define VALUE_BITSIZE(val) (val)->bitsize
188 #define VALUE_BITPOS(val) (val)->bitpos
189 #define VALUE_NEXT(val) (val)->next
190 #define VALUE_REGNO(val) (val)->regno
191 #define VALUE_OPTIMIZED_OUT(val) ((val)->optimized_out)
192 #define VALUE_EMBEDDED_OFFSET(val) ((val)->embedded_offset)
193 #define VALUE_POINTED_TO_OFFSET(val) ((val)->pointed_to_offset)
194 #define VALUE_BFD_SECTION(val) ((val)->bfd_section)
195
196 /* Convert a REF to the object referenced. */
197
198 #define COERCE_REF(arg)    \
199 do { struct type *value_type_arg_tmp = check_typedef (VALUE_TYPE (arg));\
200      if (TYPE_CODE (value_type_arg_tmp) == TYPE_CODE_REF)               \
201          arg = value_at_lazy (TYPE_TARGET_TYPE (value_type_arg_tmp),    \
202                               unpack_pointer (VALUE_TYPE (arg),         \
203                                               VALUE_CONTENTS (arg)),    \
204                               VALUE_BFD_SECTION (arg));                 \
205     } while (0)
206
207 /* If ARG is an array, convert it to a pointer.
208    If ARG is an enum, convert it to an integer.
209    If ARG is a function, convert it to a function pointer.
210
211    References are dereferenced.  */
212
213 #define COERCE_ARRAY(arg)    \
214 do { COERCE_REF(arg);                                                   \
215   if (current_language->c_style_arrays                                  \
216       && TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_ARRAY)               \
217     arg = value_coerce_array (arg);                                     \
218   if (TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_FUNC)                   \
219     arg = value_coerce_function (arg);                                  \
220 } while (0)
221
222 #define COERCE_NUMBER(arg)  \
223   do { COERCE_ARRAY(arg);  COERCE_ENUM(arg); } while (0)
224
225 #define COERCE_VARYING_ARRAY(arg, real_arg_type)        \
226 { if (chill_varying_type (real_arg_type))  \
227     arg = varying_to_slice (arg), real_arg_type = VALUE_TYPE (arg); }
228
229 /* If ARG is an enum, convert it to an integer.  */
230
231 #define COERCE_ENUM(arg)   { \
232   if (TYPE_CODE (check_typedef (VALUE_TYPE (arg))) == TYPE_CODE_ENUM)   \
233     arg = value_cast (builtin_type_unsigned_int, arg);                  \
234 }
235
236 /* Internal variables (variables for convenience of use of debugger)
237    are recorded as a chain of these structures.  */
238
239 struct internalvar
240   {
241     struct internalvar *next;
242     char *name;
243     value_ptr value;
244   };
245
246 /* Pointer to member function.  Depends on compiler implementation. */
247
248 #define METHOD_PTR_IS_VIRTUAL(ADDR)  ((ADDR) & 0x80000000)
249 #define METHOD_PTR_FROM_VOFFSET(OFFSET) (0x80000000 + (OFFSET))
250 #define METHOD_PTR_TO_VOFFSET(ADDR) (~0x80000000 & (ADDR))
251 \f
252
253 #include "symtab.h"
254 #include "gdbtypes.h"
255 #include "expression.h"
256
257 struct frame_info;
258 struct fn_field;
259
260 extern void print_address_demangle (CORE_ADDR, struct ui_file *, int);
261
262 extern LONGEST value_as_long (value_ptr val);
263
264 extern DOUBLEST value_as_double (value_ptr val);
265
266 extern CORE_ADDR value_as_pointer (value_ptr val);
267
268 extern LONGEST unpack_long (struct type *type, char *valaddr);
269
270 extern DOUBLEST unpack_double (struct type *type, char *valaddr, int *invp);
271
272 extern CORE_ADDR unpack_pointer (struct type *type, char *valaddr);
273
274 extern LONGEST unpack_field_as_long (struct type *type, char *valaddr,
275                                      int fieldno);
276
277 extern value_ptr value_from_longest (struct type *type, LONGEST num);
278
279 extern value_ptr value_from_pointer (struct type *type, CORE_ADDR addr);
280
281 extern value_ptr value_from_double (struct type *type, DOUBLEST num);
282
283 extern value_ptr value_from_string (char *string);
284
285 extern value_ptr value_at (struct type *type, CORE_ADDR addr,
286                            asection * sect);
287
288 extern value_ptr value_at_lazy (struct type *type, CORE_ADDR addr,
289                                 asection * sect);
290
291 extern value_ptr value_from_register (struct type *type, int regnum,
292                                       struct frame_info *frame);
293
294 extern value_ptr value_of_variable (struct symbol *var, struct block *b);
295
296 extern value_ptr value_of_register (int regnum);
297
298 extern int symbol_read_needs_frame (struct symbol *);
299
300 extern value_ptr read_var_value (struct symbol *var,
301                                  struct frame_info *frame);
302
303 extern value_ptr locate_var_value (struct symbol *var,
304                                    struct frame_info *frame);
305
306 extern value_ptr allocate_value (struct type *type);
307
308 extern value_ptr allocate_repeat_value (struct type *type, int count);
309
310 extern value_ptr value_mark (void);
311
312 extern void value_free_to_mark (value_ptr mark);
313
314 extern value_ptr value_string (char *ptr, int len);
315 extern value_ptr value_bitstring (char *ptr, int len);
316
317 extern value_ptr value_array (int lowbound, int highbound,
318                               value_ptr * elemvec);
319
320 extern value_ptr value_concat (value_ptr arg1, value_ptr arg2);
321
322 extern value_ptr value_binop (value_ptr arg1, value_ptr arg2,
323                               enum exp_opcode op);
324
325 extern value_ptr value_add (value_ptr arg1, value_ptr arg2);
326
327 extern value_ptr value_sub (value_ptr arg1, value_ptr arg2);
328
329 extern value_ptr value_coerce_array (value_ptr arg1);
330
331 extern value_ptr value_coerce_function (value_ptr arg1);
332
333 extern value_ptr value_ind (value_ptr arg1);
334
335 extern value_ptr value_addr (value_ptr arg1);
336
337 extern value_ptr value_assign (value_ptr toval, value_ptr fromval);
338
339 extern value_ptr value_neg (value_ptr arg1);
340
341 extern value_ptr value_complement (value_ptr arg1);
342
343 extern value_ptr value_struct_elt (value_ptr * argp, value_ptr * args,
344                                    char *name,
345                                    int *static_memfuncp, char *err);
346
347 extern value_ptr value_struct_elt_for_reference (struct type *domain,
348                                                  int offset,
349                                                  struct type *curtype,
350                                                  char *name,
351                                                  struct type *intype);
352
353 extern value_ptr value_static_field (struct type *type, int fieldno);
354
355 extern struct fn_field *value_find_oload_method_list (value_ptr *, char *,
356                                                       int, int *, int *,
357                                                       struct type **, int *);
358
359 extern int find_overload_match (struct type **arg_types, int nargs,
360                                 char *name, int method, int lax,
361                                 value_ptr obj, struct symbol *fsym,
362                                 value_ptr * valp, struct symbol **symp,
363                                 int *staticp);
364
365 extern value_ptr value_field (value_ptr arg1, int fieldno);
366
367 extern value_ptr value_primitive_field (value_ptr arg1, int offset,
368                                         int fieldno, struct type *arg_type);
369
370
371 extern struct type *value_rtti_target_type (value_ptr, int *, int *, int *);
372
373 extern value_ptr value_full_object (value_ptr, struct type *, int, int, int);
374
375 extern value_ptr value_cast (struct type *type, value_ptr arg2);
376
377 extern value_ptr value_zero (struct type *type, enum lval_type lv);
378
379 extern value_ptr value_repeat (value_ptr arg1, int count);
380
381 extern value_ptr value_subscript (value_ptr array, value_ptr idx);
382
383 extern value_ptr value_from_vtable_info (value_ptr arg, struct type *type);
384
385 extern value_ptr value_being_returned (struct type *valtype,
386                                        char *retbuf, int struct_return);
387
388 extern value_ptr value_in (value_ptr element, value_ptr set);
389
390 extern int value_bit_index (struct type *type, char *addr, int index);
391
392 extern int using_struct_return (value_ptr function, CORE_ADDR funcaddr,
393                                 struct type *value_type, int gcc_p);
394
395 extern void set_return_value (value_ptr val);
396
397 extern value_ptr evaluate_expression (struct expression *exp);
398
399 extern value_ptr evaluate_type (struct expression *exp);
400
401 extern value_ptr evaluate_subexp_with_coercion (struct expression *,
402                                                 int *, enum noside);
403
404 extern value_ptr parse_and_eval (char *exp);
405
406 extern value_ptr parse_to_comma_and_eval (char **expp);
407
408 extern struct type *parse_and_eval_type (char *p, int length);
409
410 extern CORE_ADDR parse_and_eval_address (char *exp);
411
412 extern CORE_ADDR parse_and_eval_address_1 (char **expptr);
413
414 extern LONGEST parse_and_eval_long (char *exp);
415
416 extern value_ptr access_value_history (int num);
417
418 extern value_ptr value_of_internalvar (struct internalvar *var);
419
420 extern void set_internalvar (struct internalvar *var, value_ptr val);
421
422 extern void set_internalvar_component (struct internalvar *var,
423                                        int offset,
424                                        int bitpos, int bitsize,
425                                        value_ptr newvalue);
426
427 extern struct internalvar *lookup_internalvar (char *name);
428
429 extern int value_equal (value_ptr arg1, value_ptr arg2);
430
431 extern int value_less (value_ptr arg1, value_ptr arg2);
432
433 extern int value_logical_not (value_ptr arg1);
434
435 /* C++ */
436
437 extern value_ptr value_of_this (int complain);
438
439 extern value_ptr value_x_binop (value_ptr arg1, value_ptr arg2,
440                                 enum exp_opcode op,
441                                 enum exp_opcode otherop, enum noside noside);
442
443 extern value_ptr value_x_unop (value_ptr arg1, enum exp_opcode op,
444                                enum noside noside);
445
446 extern value_ptr value_fn_field (value_ptr * arg1p, struct fn_field *f,
447                                  int j, struct type *type, int offset);
448
449 extern int binop_user_defined_p (enum exp_opcode op,
450                                  value_ptr arg1, value_ptr arg2);
451
452 extern int unop_user_defined_p (enum exp_opcode op, value_ptr arg1);
453
454 extern int destructor_name_p (const char *name, const struct type *type);
455
456 #define value_free(val) xfree (val)
457
458 extern void free_all_values (void);
459
460 extern void release_value (value_ptr val);
461
462 extern int record_latest_value (value_ptr val);
463
464 extern void
465 modify_field (char *addr, LONGEST fieldval, int bitpos, int bitsize);
466
467 extern void type_print (struct type * type, char *varstring,
468                         struct ui_file * stream, int show);
469
470 extern char *baseclass_addr (struct type *type, int index,
471                              char *valaddr, value_ptr * valuep, int *errp);
472
473 extern void print_longest (struct ui_file * stream, int format,
474                            int use_local, LONGEST val);
475
476 extern void print_floating (char *valaddr, struct type * type,
477                             struct ui_file * stream);
478
479 extern int value_print (value_ptr val, struct ui_file *stream, int format,
480                         enum val_prettyprint pretty);
481
482 extern void value_print_array_elements (value_ptr val,
483                                         struct ui_file *stream,
484                                         int format,
485                                         enum val_prettyprint pretty);
486
487 extern value_ptr value_release_to_mark (value_ptr mark);
488
489 extern int val_print (struct type * type, char *valaddr,
490                       int embedded_offset, CORE_ADDR address,
491                       struct ui_file * stream, int format,
492                       int deref_ref, int recurse,
493                       enum val_prettyprint pretty);
494
495 extern int val_print_string (CORE_ADDR addr, int len, int width, struct ui_file *stream);
496
497 extern void print_variable_value (struct symbol * var,
498                                   struct frame_info * frame,
499                                   struct ui_file *stream);
500
501 extern int check_field (value_ptr, const char *);
502
503 extern void typedef_print (struct type * type, struct symbol * news,
504                              struct ui_file * stream);
505
506 extern char *internalvar_name (struct internalvar *var);
507
508 extern void clear_value_history (void);
509
510 extern void clear_internalvars (void);
511
512 /* From values.c */
513
514 extern value_ptr value_copy (value_ptr);
515
516 extern int baseclass_offset (struct type *, int, char *, CORE_ADDR);
517
518 /* From valops.c */
519
520 extern value_ptr varying_to_slice (value_ptr);
521
522 extern value_ptr value_slice (value_ptr, int, int);
523
524 extern value_ptr call_function_by_hand (value_ptr, int, value_ptr *);
525
526 extern int default_coerce_float_to_double (struct type *, struct type *);
527
528 extern int standard_coerce_float_to_double (struct type *, struct type *);
529
530 extern value_ptr value_literal_complex (value_ptr, value_ptr, struct type *);
531
532 extern void find_rt_vbase_offset (struct type *, struct type *, char *, int,
533                                   int *, int *);
534
535 extern value_ptr find_function_in_inferior (char *);
536
537 extern value_ptr value_allocate_space_in_inferior (int);
538
539 extern CORE_ADDR default_push_arguments (int nargs, value_ptr * args,
540                                          CORE_ADDR sp,
541                                          int struct_return,
542                                          CORE_ADDR struct_addr);
543
544 #endif /* !defined (VALUE_H) */