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