Updated Swedish translation for the opcodes directory
[binutils-gdb.git] / gdb / gnu-v3-abi.c
blob2c7c83e67b1c25577158674fb138015e7a93eecf
1 /* Abstraction of GNU v3 abi.
2 Contributed by Jim Blandy <jimb@redhat.com>
4 Copyright (C) 2001-2023 Free Software Foundation, Inc.
6 This file is part of GDB.
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "defs.h"
22 #include "language.h"
23 #include "value.h"
24 #include "cp-abi.h"
25 #include "cp-support.h"
26 #include "demangle.h"
27 #include "dwarf2.h"
28 #include "objfiles.h"
29 #include "valprint.h"
30 #include "c-lang.h"
31 #include "typeprint.h"
32 #include <algorithm>
33 #include "cli/cli-style.h"
34 #include "dwarf2/loc.h"
35 #include "inferior.h"
37 static struct cp_abi_ops gnu_v3_abi_ops;
39 /* A gdbarch key for std::type_info, in the event that it can't be
40 found in the debug info. */
42 static const registry<gdbarch>::key<struct type> std_type_info_gdbarch_data;
45 static int
46 gnuv3_is_vtable_name (const char *name)
48 return startswith (name, "_ZTV");
51 static int
52 gnuv3_is_operator_name (const char *name)
54 return startswith (name, CP_OPERATOR_STR);
58 /* To help us find the components of a vtable, we build ourselves a
59 GDB type object representing the vtable structure. Following the
60 V3 ABI, it goes something like this:
62 struct gdb_gnu_v3_abi_vtable {
64 / * An array of virtual call and virtual base offsets. The real
65 length of this array depends on the class hierarchy; we use
66 negative subscripts to access the elements. Yucky, but
67 better than the alternatives. * /
68 ptrdiff_t vcall_and_vbase_offsets[0];
70 / * The offset from a virtual pointer referring to this table
71 to the top of the complete object. * /
72 ptrdiff_t offset_to_top;
74 / * The type_info pointer for this class. This is really a
75 std::type_info *, but GDB doesn't really look at the
76 type_info object itself, so we don't bother to get the type
77 exactly right. * /
78 void *type_info;
80 / * Virtual table pointers in objects point here. * /
82 / * Virtual function pointers. Like the vcall/vbase array, the
83 real length of this table depends on the class hierarchy. * /
84 void (*virtual_functions[0]) ();
88 The catch, of course, is that the exact layout of this table
89 depends on the ABI --- word size, endianness, alignment, etc. So
90 the GDB type object is actually a per-architecture kind of thing.
92 vtable_type_gdbarch_data is a gdbarch per-architecture data pointer
93 which refers to the struct type * for this structure, laid out
94 appropriately for the architecture. */
95 static const registry<gdbarch>::key<struct type> vtable_type_gdbarch_data;
98 /* Human-readable names for the numbers of the fields above. */
99 enum {
100 vtable_field_vcall_and_vbase_offsets,
101 vtable_field_offset_to_top,
102 vtable_field_type_info,
103 vtable_field_virtual_functions
107 /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
108 described above, laid out appropriately for ARCH.
110 We use this function as the gdbarch per-architecture data
111 initialization function. */
112 static struct type *
113 get_gdb_vtable_type (struct gdbarch *arch)
115 struct type *t;
116 struct field *field_list, *field;
117 int offset;
119 struct type *result = vtable_type_gdbarch_data.get (arch);
120 if (result != nullptr)
121 return result;
123 struct type *void_ptr_type
124 = builtin_type (arch)->builtin_data_ptr;
125 struct type *ptr_to_void_fn_type
126 = builtin_type (arch)->builtin_func_ptr;
128 type_allocator alloc (arch);
130 /* ARCH can't give us the true ptrdiff_t type, so we guess. */
131 struct type *ptrdiff_type
132 = init_integer_type (alloc, gdbarch_ptr_bit (arch), 0, "ptrdiff_t");
134 /* We assume no padding is necessary, since GDB doesn't know
135 anything about alignment at the moment. If this assumption bites
136 us, we should add a gdbarch method which, given a type, returns
137 the alignment that type requires, and then use that here. */
139 /* Build the field list. */
140 field_list = XCNEWVEC (struct field, 4);
141 field = &field_list[0];
142 offset = 0;
144 /* ptrdiff_t vcall_and_vbase_offsets[0]; */
145 field->set_name ("vcall_and_vbase_offsets");
146 field->set_type (lookup_array_range_type (ptrdiff_type, 0, -1));
147 field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
148 offset += field->type ()->length ();
149 field++;
151 /* ptrdiff_t offset_to_top; */
152 field->set_name ("offset_to_top");
153 field->set_type (ptrdiff_type);
154 field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
155 offset += field->type ()->length ();
156 field++;
158 /* void *type_info; */
159 field->set_name ("type_info");
160 field->set_type (void_ptr_type);
161 field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
162 offset += field->type ()->length ();
163 field++;
165 /* void (*virtual_functions[0]) (); */
166 field->set_name ("virtual_functions");
167 field->set_type (lookup_array_range_type (ptr_to_void_fn_type, 0, -1));
168 field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
169 offset += field->type ()->length ();
170 field++;
172 /* We assumed in the allocation above that there were four fields. */
173 gdb_assert (field == (field_list + 4));
175 t = alloc.new_type (TYPE_CODE_STRUCT, offset * TARGET_CHAR_BIT, NULL);
176 t->set_num_fields (field - field_list);
177 t->set_fields (field_list);
178 t->set_name ("gdb_gnu_v3_abi_vtable");
179 INIT_CPLUS_SPECIFIC (t);
181 result = make_type_with_address_space (t, TYPE_INSTANCE_FLAG_CODE_SPACE);
182 vtable_type_gdbarch_data.set (arch, result);
183 return result;
187 /* Return the ptrdiff_t type used in the vtable type. */
188 static struct type *
189 vtable_ptrdiff_type (struct gdbarch *gdbarch)
191 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
193 /* The "offset_to_top" field has the appropriate (ptrdiff_t) type. */
194 return vtable_type->field (vtable_field_offset_to_top).type ();
197 /* Return the offset from the start of the imaginary `struct
198 gdb_gnu_v3_abi_vtable' object to the vtable's "address point"
199 (i.e., where objects' virtual table pointers point). */
200 static int
201 vtable_address_point_offset (struct gdbarch *gdbarch)
203 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
205 return (vtable_type->field (vtable_field_virtual_functions).loc_bitpos ()
206 / TARGET_CHAR_BIT);
210 /* Determine whether structure TYPE is a dynamic class. Cache the
211 result. */
213 static int
214 gnuv3_dynamic_class (struct type *type)
216 int fieldnum, fieldelem;
218 type = check_typedef (type);
219 gdb_assert (type->code () == TYPE_CODE_STRUCT
220 || type->code () == TYPE_CODE_UNION);
222 if (type->code () == TYPE_CODE_UNION)
223 return 0;
225 if (TYPE_CPLUS_DYNAMIC (type))
226 return TYPE_CPLUS_DYNAMIC (type) == 1;
228 ALLOCATE_CPLUS_STRUCT_TYPE (type);
230 for (fieldnum = 0; fieldnum < TYPE_N_BASECLASSES (type); fieldnum++)
231 if (BASETYPE_VIA_VIRTUAL (type, fieldnum)
232 || gnuv3_dynamic_class (type->field (fieldnum).type ()))
234 TYPE_CPLUS_DYNAMIC (type) = 1;
235 return 1;
238 for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
239 for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
240 fieldelem++)
242 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, fieldnum);
244 if (TYPE_FN_FIELD_VIRTUAL_P (f, fieldelem))
246 TYPE_CPLUS_DYNAMIC (type) = 1;
247 return 1;
251 TYPE_CPLUS_DYNAMIC (type) = -1;
252 return 0;
255 /* Find the vtable for a value of CONTAINER_TYPE located at
256 CONTAINER_ADDR. Return a value of the correct vtable type for this
257 architecture, or NULL if CONTAINER does not have a vtable. */
259 static struct value *
260 gnuv3_get_vtable (struct gdbarch *gdbarch,
261 struct type *container_type, CORE_ADDR container_addr)
263 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
264 struct type *vtable_pointer_type;
265 struct value *vtable_pointer;
266 CORE_ADDR vtable_address;
268 container_type = check_typedef (container_type);
269 gdb_assert (container_type->code () == TYPE_CODE_STRUCT);
271 /* If this type does not have a virtual table, don't read the first
272 field. */
273 if (!gnuv3_dynamic_class (container_type))
274 return NULL;
276 /* We do not consult the debug information to find the virtual table.
277 The ABI specifies that it is always at offset zero in any class,
278 and debug information may not represent it.
280 We avoid using value_contents on principle, because the object might
281 be large. */
283 /* Find the type "pointer to virtual table". */
284 vtable_pointer_type = lookup_pointer_type (vtable_type);
286 /* Load it from the start of the class. */
287 vtable_pointer = value_at (vtable_pointer_type, container_addr);
288 vtable_address = value_as_address (vtable_pointer);
290 /* Correct it to point at the start of the virtual table, rather
291 than the address point. */
292 return value_at_lazy (vtable_type,
293 vtable_address
294 - vtable_address_point_offset (gdbarch));
298 static struct type *
299 gnuv3_rtti_type (struct value *value,
300 int *full_p, LONGEST *top_p, int *using_enc_p)
302 struct gdbarch *gdbarch;
303 struct type *values_type = check_typedef (value->type ());
304 struct value *vtable;
305 struct minimal_symbol *vtable_symbol;
306 const char *vtable_symbol_name;
307 const char *class_name;
308 struct type *run_time_type;
309 LONGEST offset_to_top;
310 const char *atsign;
312 /* We only have RTTI for dynamic class objects. */
313 if (values_type->code () != TYPE_CODE_STRUCT
314 || !gnuv3_dynamic_class (values_type))
315 return NULL;
317 /* Determine architecture. */
318 gdbarch = values_type->arch ();
320 if (using_enc_p)
321 *using_enc_p = 0;
323 vtable = gnuv3_get_vtable (gdbarch, values_type,
324 value_as_address (value_addr (value)));
325 if (vtable == NULL)
326 return NULL;
328 /* Find the linker symbol for this vtable. */
329 vtable_symbol
330 = lookup_minimal_symbol_by_pc (vtable->address ()
331 + vtable->embedded_offset ()).minsym;
332 if (! vtable_symbol)
333 return NULL;
335 /* The symbol's demangled name should be something like "vtable for
336 CLASS", where CLASS is the name of the run-time type of VALUE.
337 If we didn't like this approach, we could instead look in the
338 type_info object itself to get the class name. But this way
339 should work just as well, and doesn't read target memory. */
340 vtable_symbol_name = vtable_symbol->demangled_name ();
341 if (vtable_symbol_name == NULL
342 || !startswith (vtable_symbol_name, "vtable for "))
344 warning (_("can't find linker symbol for virtual table for `%s' value"),
345 TYPE_SAFE_NAME (values_type));
346 if (vtable_symbol_name)
347 warning (_(" found `%s' instead"), vtable_symbol_name);
348 return NULL;
350 class_name = vtable_symbol_name + 11;
352 /* Strip off @plt and version suffixes. */
353 atsign = strchr (class_name, '@');
354 if (atsign != NULL)
356 char *copy;
358 copy = (char *) alloca (atsign - class_name + 1);
359 memcpy (copy, class_name, atsign - class_name);
360 copy[atsign - class_name] = '\0';
361 class_name = copy;
364 /* Try to look up the class name as a type name. */
365 /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */
366 run_time_type = cp_lookup_rtti_type (class_name, NULL);
367 if (run_time_type == NULL)
368 return NULL;
370 /* Get the offset from VALUE to the top of the complete object.
371 NOTE: this is the reverse of the meaning of *TOP_P. */
372 offset_to_top
373 = value_as_long (value_field (vtable, vtable_field_offset_to_top));
375 if (full_p)
376 *full_p = (- offset_to_top == value->embedded_offset ()
377 && (value->enclosing_type ()->length ()
378 >= run_time_type->length ()));
379 if (top_p)
380 *top_p = - offset_to_top;
381 return run_time_type;
384 /* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
385 function, of type FNTYPE. */
387 static struct value *
388 gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
389 struct type *fntype, int vtable_index)
391 struct value *vtable, *vfn;
393 /* Every class with virtual functions must have a vtable. */
394 vtable = gnuv3_get_vtable (gdbarch, container->type (),
395 value_as_address (value_addr (container)));
396 gdb_assert (vtable != NULL);
398 /* Fetch the appropriate function pointer from the vtable. */
399 vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
400 vtable_index);
402 /* If this architecture uses function descriptors directly in the vtable,
403 then the address of the vtable entry is actually a "function pointer"
404 (i.e. points to the descriptor). We don't need to scale the index
405 by the size of a function descriptor; GCC does that before outputting
406 debug information. */
407 if (gdbarch_vtable_function_descriptors (gdbarch))
408 vfn = value_addr (vfn);
410 /* Cast the function pointer to the appropriate type. */
411 vfn = value_cast (lookup_pointer_type (fntype), vfn);
413 return vfn;
416 /* GNU v3 implementation of value_virtual_fn_field. See cp-abi.h
417 for a description of the arguments. */
419 static struct value *
420 gnuv3_virtual_fn_field (struct value **value_p,
421 struct fn_field *f, int j,
422 struct type *vfn_base, int offset)
424 struct type *values_type = check_typedef ((*value_p)->type ());
425 struct gdbarch *gdbarch;
427 /* Some simple sanity checks. */
428 if (values_type->code () != TYPE_CODE_STRUCT)
429 error (_("Only classes can have virtual functions."));
431 /* Determine architecture. */
432 gdbarch = values_type->arch ();
434 /* Cast our value to the base class which defines this virtual
435 function. This takes care of any necessary `this'
436 adjustments. */
437 if (vfn_base != values_type)
438 *value_p = value_cast (vfn_base, *value_p);
440 return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
441 TYPE_FN_FIELD_VOFFSET (f, j));
444 /* Compute the offset of the baseclass which is
445 the INDEXth baseclass of class TYPE,
446 for value at VALADDR (in host) at ADDRESS (in target).
447 The result is the offset of the baseclass value relative
448 to (the address of)(ARG) + OFFSET.
450 -1 is returned on error. */
452 static int
453 gnuv3_baseclass_offset (struct type *type, int index,
454 const bfd_byte *valaddr, LONGEST embedded_offset,
455 CORE_ADDR address, const struct value *val)
457 struct gdbarch *gdbarch;
458 struct type *ptr_type;
459 struct value *vtable;
460 struct value *vbase_array;
461 long int cur_base_offset, base_offset;
463 /* Determine architecture. */
464 gdbarch = type->arch ();
465 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
467 /* If it isn't a virtual base, this is easy. The offset is in the
468 type definition. */
469 if (!BASETYPE_VIA_VIRTUAL (type, index))
470 return TYPE_BASECLASS_BITPOS (type, index) / 8;
472 /* If we have a DWARF expression for the offset, evaluate it. */
473 if (type->field (index).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
475 struct dwarf2_property_baton baton;
476 baton.property_type
477 = lookup_pointer_type (type->field (index).type ());
478 baton.locexpr = *type->field (index).loc_dwarf_block ();
480 struct dynamic_prop prop;
481 prop.set_locexpr (&baton);
483 struct property_addr_info addr_stack;
484 addr_stack.type = type;
485 /* Note that we don't set "valaddr" here. Doing so causes
486 regressions. FIXME. */
487 addr_stack.addr = address + embedded_offset;
488 addr_stack.next = nullptr;
490 CORE_ADDR result;
491 if (dwarf2_evaluate_property (&prop, nullptr, &addr_stack, &result,
492 {addr_stack.addr}))
493 return (int) (result - addr_stack.addr);
496 /* To access a virtual base, we need to use the vbase offset stored in
497 our vtable. Recent GCC versions provide this information. If it isn't
498 available, we could get what we needed from RTTI, or from drawing the
499 complete inheritance graph based on the debug info. Neither is
500 worthwhile. */
501 cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
502 if (cur_base_offset >= - vtable_address_point_offset (gdbarch))
503 error (_("Expected a negative vbase offset (old compiler?)"));
505 cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch);
506 if ((- cur_base_offset) % ptr_type->length () != 0)
507 error (_("Misaligned vbase offset."));
508 cur_base_offset = cur_base_offset / ((int) ptr_type->length ());
510 vtable = gnuv3_get_vtable (gdbarch, type, address + embedded_offset);
511 gdb_assert (vtable != NULL);
512 vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
513 base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset));
514 return base_offset;
517 /* Locate a virtual method in DOMAIN or its non-virtual base classes
518 which has virtual table index VOFFSET. The method has an associated
519 "this" adjustment of ADJUSTMENT bytes. */
521 static const char *
522 gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
523 LONGEST adjustment)
525 int i;
527 /* Search this class first. */
528 if (adjustment == 0)
530 int len;
532 len = TYPE_NFN_FIELDS (domain);
533 for (i = 0; i < len; i++)
535 int len2, j;
536 struct fn_field *f;
538 f = TYPE_FN_FIELDLIST1 (domain, i);
539 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
541 check_stub_method_group (domain, i);
542 for (j = 0; j < len2; j++)
543 if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset)
544 return TYPE_FN_FIELD_PHYSNAME (f, j);
548 /* Next search non-virtual bases. If it's in a virtual base,
549 we're out of luck. */
550 for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
552 int pos;
553 struct type *basetype;
555 if (BASETYPE_VIA_VIRTUAL (domain, i))
556 continue;
558 pos = TYPE_BASECLASS_BITPOS (domain, i) / 8;
559 basetype = domain->field (i).type ();
560 /* Recurse with a modified adjustment. We don't need to adjust
561 voffset. */
562 if (adjustment >= pos && adjustment < pos + basetype->length ())
563 return gnuv3_find_method_in (basetype, voffset, adjustment - pos);
566 return NULL;
569 /* Decode GNU v3 method pointer. */
571 static int
572 gnuv3_decode_method_ptr (struct gdbarch *gdbarch,
573 const gdb_byte *contents,
574 CORE_ADDR *value_p,
575 LONGEST *adjustment_p)
577 struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr;
578 struct type *offset_type = vtable_ptrdiff_type (gdbarch);
579 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
580 CORE_ADDR ptr_value;
581 LONGEST voffset, adjustment;
582 int vbit;
584 /* Extract the pointer to member. The first element is either a pointer
585 or a vtable offset. For pointers, we need to use extract_typed_address
586 to allow the back-end to convert the pointer to a GDB address -- but
587 vtable offsets we must handle as integers. At this point, we do not
588 yet know which case we have, so we extract the value under both
589 interpretations and choose the right one later on. */
590 ptr_value = extract_typed_address (contents, funcptr_type);
591 voffset = extract_signed_integer (contents,
592 funcptr_type->length (), byte_order);
593 contents += funcptr_type->length ();
594 adjustment = extract_signed_integer (contents,
595 offset_type->length (), byte_order);
597 if (!gdbarch_vbit_in_delta (gdbarch))
599 vbit = voffset & 1;
600 voffset = voffset ^ vbit;
602 else
604 vbit = adjustment & 1;
605 adjustment = adjustment >> 1;
608 *value_p = vbit? voffset : ptr_value;
609 *adjustment_p = adjustment;
610 return vbit;
613 /* GNU v3 implementation of cplus_print_method_ptr. */
615 static void
616 gnuv3_print_method_ptr (const gdb_byte *contents,
617 struct type *type,
618 struct ui_file *stream)
620 struct type *self_type = TYPE_SELF_TYPE (type);
621 struct gdbarch *gdbarch = self_type->arch ();
622 CORE_ADDR ptr_value;
623 LONGEST adjustment;
624 int vbit;
626 /* Extract the pointer to member. */
627 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
629 /* Check for NULL. */
630 if (ptr_value == 0 && vbit == 0)
632 gdb_printf (stream, "NULL");
633 return;
636 /* Search for a virtual method. */
637 if (vbit)
639 CORE_ADDR voffset;
640 const char *physname;
642 /* It's a virtual table offset, maybe in this class. Search
643 for a field with the correct vtable offset. First convert it
644 to an index, as used in TYPE_FN_FIELD_VOFFSET. */
645 voffset = ptr_value / vtable_ptrdiff_type (gdbarch)->length ();
647 physname = gnuv3_find_method_in (self_type, voffset, adjustment);
649 /* If we found a method, print that. We don't bother to disambiguate
650 possible paths to the method based on the adjustment. */
651 if (physname)
653 gdb::unique_xmalloc_ptr<char> demangled_name
654 = gdb_demangle (physname, DMGL_ANSI | DMGL_PARAMS);
656 gdb_printf (stream, "&virtual ");
657 if (demangled_name == NULL)
658 gdb_puts (physname, stream);
659 else
660 gdb_puts (demangled_name.get (), stream);
661 return;
664 else if (ptr_value != 0)
666 /* Found a non-virtual function: print out the type. */
667 gdb_puts ("(", stream);
668 c_print_type (type, "", stream, -1, 0, current_language->la_language,
669 &type_print_raw_options);
670 gdb_puts (") ", stream);
673 /* We didn't find it; print the raw data. */
674 if (vbit)
676 gdb_printf (stream, "&virtual table offset ");
677 print_longest (stream, 'd', 1, ptr_value);
679 else
681 struct value_print_options opts;
683 get_user_print_options (&opts);
684 print_address_demangle (&opts, gdbarch, ptr_value, stream, demangle);
687 if (adjustment)
689 gdb_printf (stream, ", this adjustment ");
690 print_longest (stream, 'd', 1, adjustment);
694 /* GNU v3 implementation of cplus_method_ptr_size. */
696 static int
697 gnuv3_method_ptr_size (struct type *type)
699 return 2 * builtin_type (type->arch ())->builtin_data_ptr->length ();
702 /* GNU v3 implementation of cplus_make_method_ptr. */
704 static void
705 gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
706 CORE_ADDR value, int is_virtual)
708 struct gdbarch *gdbarch = type->arch ();
709 int size = builtin_type (gdbarch)->builtin_data_ptr->length ();
710 enum bfd_endian byte_order = type_byte_order (type);
712 /* FIXME drow/2006-12-24: The adjustment of "this" is currently
713 always zero, since the method pointer is of the correct type.
714 But if the method pointer came from a base class, this is
715 incorrect - it should be the offset to the base. The best
716 fix might be to create the pointer to member pointing at the
717 base class and cast it to the derived class, but that requires
718 support for adjusting pointers to members when casting them -
719 not currently supported by GDB. */
721 if (!gdbarch_vbit_in_delta (gdbarch))
723 store_unsigned_integer (contents, size, byte_order, value | is_virtual);
724 store_unsigned_integer (contents + size, size, byte_order, 0);
726 else
728 store_unsigned_integer (contents, size, byte_order, value);
729 store_unsigned_integer (contents + size, size, byte_order, is_virtual);
733 /* GNU v3 implementation of cplus_method_ptr_to_value. */
735 static struct value *
736 gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
738 struct gdbarch *gdbarch;
739 const gdb_byte *contents = method_ptr->contents ().data ();
740 CORE_ADDR ptr_value;
741 struct type *self_type, *final_type, *method_type;
742 LONGEST adjustment;
743 int vbit;
745 self_type = TYPE_SELF_TYPE (check_typedef (method_ptr->type ()));
746 final_type = lookup_pointer_type (self_type);
748 method_type = check_typedef (method_ptr->type ())->target_type ();
750 /* Extract the pointer to member. */
751 gdbarch = self_type->arch ();
752 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
754 /* First convert THIS to match the containing type of the pointer to
755 member. This cast may adjust the value of THIS. */
756 *this_p = value_cast (final_type, *this_p);
758 /* Then apply whatever adjustment is necessary. This creates a somewhat
759 strange pointer: it claims to have type FINAL_TYPE, but in fact it
760 might not be a valid FINAL_TYPE. For instance, it might be a
761 base class of FINAL_TYPE. And if it's not the primary base class,
762 then printing it out as a FINAL_TYPE object would produce some pretty
763 garbage.
765 But we don't really know the type of the first argument in
766 METHOD_TYPE either, which is why this happens. We can't
767 dereference this later as a FINAL_TYPE, but once we arrive in the
768 called method we'll have debugging information for the type of
769 "this" - and that'll match the value we produce here.
771 You can provoke this case by casting a Base::* to a Derived::*, for
772 instance. */
773 *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p);
774 *this_p = value_ptradd (*this_p, adjustment);
775 *this_p = value_cast (final_type, *this_p);
777 if (vbit)
779 LONGEST voffset;
781 voffset = ptr_value / vtable_ptrdiff_type (gdbarch)->length ();
782 return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
783 method_type, voffset);
785 else
786 return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
789 /* Objects of this type are stored in a hash table and a vector when
790 printing the vtables for a class. */
792 struct value_and_voffset
794 /* The value representing the object. */
795 struct value *value;
797 /* The maximum vtable offset we've found for any object at this
798 offset in the outermost object. */
799 int max_voffset;
802 /* Hash function for value_and_voffset. */
804 static hashval_t
805 hash_value_and_voffset (const void *p)
807 const struct value_and_voffset *o = (const struct value_and_voffset *) p;
809 return o->value->address () + o->value->embedded_offset ();
812 /* Equality function for value_and_voffset. */
814 static int
815 eq_value_and_voffset (const void *a, const void *b)
817 const struct value_and_voffset *ova = (const struct value_and_voffset *) a;
818 const struct value_and_voffset *ovb = (const struct value_and_voffset *) b;
820 return (ova->value->address () + ova->value->embedded_offset ()
821 == ovb->value->address () + ovb->value->embedded_offset ());
824 /* Comparison function for value_and_voffset. */
826 static bool
827 compare_value_and_voffset (const struct value_and_voffset *va,
828 const struct value_and_voffset *vb)
830 CORE_ADDR addra = (va->value->address ()
831 + va->value->embedded_offset ());
832 CORE_ADDR addrb = (vb->value->address ()
833 + vb->value->embedded_offset ());
835 return addra < addrb;
838 /* A helper function used when printing vtables. This determines the
839 key (most derived) sub-object at each address and also computes the
840 maximum vtable offset seen for the corresponding vtable. Updates
841 OFFSET_HASH and OFFSET_VEC with a new value_and_voffset object, if
842 needed. VALUE is the object to examine. */
844 static void
845 compute_vtable_size (htab_t offset_hash,
846 std::vector<value_and_voffset *> *offset_vec,
847 struct value *value)
849 int i;
850 struct type *type = check_typedef (value->type ());
851 void **slot;
852 struct value_and_voffset search_vo, *current_vo;
854 gdb_assert (type->code () == TYPE_CODE_STRUCT);
856 /* If the object is not dynamic, then we are done; as it cannot have
857 dynamic base types either. */
858 if (!gnuv3_dynamic_class (type))
859 return;
861 /* Update the hash and the vec, if needed. */
862 search_vo.value = value;
863 slot = htab_find_slot (offset_hash, &search_vo, INSERT);
864 if (*slot)
865 current_vo = (struct value_and_voffset *) *slot;
866 else
868 current_vo = XNEW (struct value_and_voffset);
869 current_vo->value = value;
870 current_vo->max_voffset = -1;
871 *slot = current_vo;
872 offset_vec->push_back (current_vo);
875 /* Update the value_and_voffset object with the highest vtable
876 offset from this class. */
877 for (i = 0; i < TYPE_NFN_FIELDS (type); ++i)
879 int j;
880 struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, i);
882 for (j = 0; j < TYPE_FN_FIELDLIST_LENGTH (type, i); ++j)
884 if (TYPE_FN_FIELD_VIRTUAL_P (fn, j))
886 int voffset = TYPE_FN_FIELD_VOFFSET (fn, j);
888 if (voffset > current_vo->max_voffset)
889 current_vo->max_voffset = voffset;
894 /* Recurse into base classes. */
895 for (i = 0; i < TYPE_N_BASECLASSES (type); ++i)
896 compute_vtable_size (offset_hash, offset_vec, value_field (value, i));
899 /* Helper for gnuv3_print_vtable that prints a single vtable. */
901 static void
902 print_one_vtable (struct gdbarch *gdbarch, struct value *value,
903 int max_voffset,
904 struct value_print_options *opts)
906 int i;
907 struct type *type = check_typedef (value->type ());
908 struct value *vtable;
909 CORE_ADDR vt_addr;
911 vtable = gnuv3_get_vtable (gdbarch, type,
912 value->address ()
913 + value->embedded_offset ());
914 vt_addr = value_field (vtable,
915 vtable_field_virtual_functions)->address ();
917 gdb_printf (_("vtable for '%s' @ %s (subobject @ %s):\n"),
918 TYPE_SAFE_NAME (type),
919 paddress (gdbarch, vt_addr),
920 paddress (gdbarch, (value->address ()
921 + value->embedded_offset ())));
923 for (i = 0; i <= max_voffset; ++i)
925 /* Initialize it just to avoid a GCC false warning. */
926 CORE_ADDR addr = 0;
927 int got_error = 0;
928 struct value *vfn;
930 gdb_printf ("[%d]: ", i);
932 vfn = value_subscript (value_field (vtable,
933 vtable_field_virtual_functions),
936 if (gdbarch_vtable_function_descriptors (gdbarch))
937 vfn = value_addr (vfn);
941 addr = value_as_address (vfn);
943 catch (const gdb_exception_error &ex)
945 fprintf_styled (gdb_stdout, metadata_style.style (),
946 _("<error: %s>"), ex.what ());
947 got_error = 1;
950 if (!got_error)
951 print_function_pointer_address (opts, gdbarch, addr, gdb_stdout);
952 gdb_printf ("\n");
956 /* Implementation of the print_vtable method. */
958 static void
959 gnuv3_print_vtable (struct value *value)
961 struct gdbarch *gdbarch;
962 struct type *type;
963 struct value *vtable;
964 struct value_print_options opts;
965 int count;
967 value = coerce_ref (value);
968 type = check_typedef (value->type ());
969 if (type->code () == TYPE_CODE_PTR)
971 value = value_ind (value);
972 type = check_typedef (value->type ());
975 get_user_print_options (&opts);
977 /* Respect 'set print object'. */
978 if (opts.objectprint)
980 value = value_full_object (value, NULL, 0, 0, 0);
981 type = check_typedef (value->type ());
984 gdbarch = type->arch ();
986 vtable = NULL;
987 if (type->code () == TYPE_CODE_STRUCT)
988 vtable = gnuv3_get_vtable (gdbarch, type,
989 value_as_address (value_addr (value)));
991 if (!vtable)
993 gdb_printf (_("This object does not have a virtual function table\n"));
994 return;
997 htab_up offset_hash (htab_create_alloc (1, hash_value_and_voffset,
998 eq_value_and_voffset,
999 xfree, xcalloc, xfree));
1000 std::vector<value_and_voffset *> result_vec;
1002 compute_vtable_size (offset_hash.get (), &result_vec, value);
1003 std::sort (result_vec.begin (), result_vec.end (),
1004 compare_value_and_voffset);
1006 count = 0;
1007 for (value_and_voffset *iter : result_vec)
1009 if (iter->max_voffset >= 0)
1011 if (count > 0)
1012 gdb_printf ("\n");
1013 print_one_vtable (gdbarch, iter->value, iter->max_voffset, &opts);
1014 ++count;
1019 /* Return a GDB type representing `struct std::type_info', laid out
1020 appropriately for ARCH.
1022 We use this function as the gdbarch per-architecture data
1023 initialization function. */
1025 static struct type *
1026 build_std_type_info_type (struct gdbarch *arch)
1028 struct type *t;
1029 struct field *field_list, *field;
1030 int offset;
1031 struct type *void_ptr_type
1032 = builtin_type (arch)->builtin_data_ptr;
1033 struct type *char_type
1034 = builtin_type (arch)->builtin_char;
1035 struct type *char_ptr_type
1036 = make_pointer_type (make_cv_type (1, 0, char_type, NULL), NULL);
1038 field_list = XCNEWVEC (struct field, 2);
1039 field = &field_list[0];
1040 offset = 0;
1042 /* The vtable. */
1043 field->set_name ("_vptr.type_info");
1044 field->set_type (void_ptr_type);
1045 field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
1046 offset += field->type ()->length ();
1047 field++;
1049 /* The name. */
1050 field->set_name ("__name");
1051 field->set_type (char_ptr_type);
1052 field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
1053 offset += field->type ()->length ();
1054 field++;
1056 gdb_assert (field == (field_list + 2));
1058 t = type_allocator (arch).new_type (TYPE_CODE_STRUCT,
1059 offset * TARGET_CHAR_BIT, nullptr);
1060 t->set_num_fields (field - field_list);
1061 t->set_fields (field_list);
1062 t->set_name ("gdb_gnu_v3_type_info");
1063 INIT_CPLUS_SPECIFIC (t);
1065 return t;
1068 /* Implement the 'get_typeid_type' method. */
1070 static struct type *
1071 gnuv3_get_typeid_type (struct gdbarch *gdbarch)
1073 struct symbol *typeinfo;
1074 struct type *typeinfo_type;
1076 typeinfo = lookup_symbol ("std::type_info", NULL, STRUCT_DOMAIN,
1077 NULL).symbol;
1078 if (typeinfo == NULL)
1080 typeinfo_type = std_type_info_gdbarch_data.get (gdbarch);
1081 if (typeinfo_type == nullptr)
1083 typeinfo_type = build_std_type_info_type (gdbarch);
1084 std_type_info_gdbarch_data.set (gdbarch, typeinfo_type);
1087 else
1088 typeinfo_type = typeinfo->type ();
1090 return typeinfo_type;
1093 /* Implement the 'get_typeid' method. */
1095 static struct value *
1096 gnuv3_get_typeid (struct value *value)
1098 struct type *typeinfo_type;
1099 struct type *type;
1100 struct gdbarch *gdbarch;
1101 struct value *result;
1102 std::string type_name;
1103 gdb::unique_xmalloc_ptr<char> canonical;
1105 /* We have to handle values a bit trickily here, to allow this code
1106 to work properly with non_lvalue values that are really just
1107 disguised types. */
1108 if (value->lval () == lval_memory)
1109 value = coerce_ref (value);
1111 type = check_typedef (value->type ());
1113 /* In the non_lvalue case, a reference might have slipped through
1114 here. */
1115 if (type->code () == TYPE_CODE_REF)
1116 type = check_typedef (type->target_type ());
1118 /* Ignore top-level cv-qualifiers. */
1119 type = make_cv_type (0, 0, type, NULL);
1120 gdbarch = type->arch ();
1122 type_name = type_to_string (type);
1123 if (type_name.empty ())
1124 error (_("cannot find typeinfo for unnamed type"));
1126 /* We need to canonicalize the type name here, because we do lookups
1127 using the demangled name, and so we must match the format it
1128 uses. E.g., GDB tends to use "const char *" as a type name, but
1129 the demangler uses "char const *". */
1130 canonical = cp_canonicalize_string (type_name.c_str ());
1131 const char *name = (canonical == nullptr
1132 ? type_name.c_str ()
1133 : canonical.get ());
1135 typeinfo_type = gnuv3_get_typeid_type (gdbarch);
1137 /* We check for lval_memory because in the "typeid (type-id)" case,
1138 the type is passed via a not_lval value object. */
1139 if (type->code () == TYPE_CODE_STRUCT
1140 && value->lval () == lval_memory
1141 && gnuv3_dynamic_class (type))
1143 struct value *vtable, *typeinfo_value;
1144 CORE_ADDR address = value->address () + value->embedded_offset ();
1146 vtable = gnuv3_get_vtable (gdbarch, type, address);
1147 if (vtable == NULL)
1148 error (_("cannot find typeinfo for object of type '%s'"),
1149 name);
1150 typeinfo_value = value_field (vtable, vtable_field_type_info);
1151 result = value_ind (value_cast (make_pointer_type (typeinfo_type, NULL),
1152 typeinfo_value));
1154 else
1156 std::string sym_name = std::string ("typeinfo for ") + name;
1157 bound_minimal_symbol minsym
1158 = lookup_minimal_symbol (sym_name.c_str (), NULL, NULL);
1160 if (minsym.minsym == NULL)
1161 error (_("could not find typeinfo symbol for '%s'"), name);
1163 result = value_at_lazy (typeinfo_type, minsym.value_address ());
1166 return result;
1169 /* Implement the 'get_typename_from_type_info' method. */
1171 static std::string
1172 gnuv3_get_typename_from_type_info (struct value *type_info_ptr)
1174 struct gdbarch *gdbarch = type_info_ptr->type ()->arch ();
1175 struct bound_minimal_symbol typeinfo_sym;
1176 CORE_ADDR addr;
1177 const char *symname;
1178 const char *class_name;
1179 const char *atsign;
1181 addr = value_as_address (type_info_ptr);
1182 typeinfo_sym = lookup_minimal_symbol_by_pc (addr);
1183 if (typeinfo_sym.minsym == NULL)
1184 error (_("could not find minimal symbol for typeinfo address %s"),
1185 paddress (gdbarch, addr));
1187 #define TYPEINFO_PREFIX "typeinfo for "
1188 #define TYPEINFO_PREFIX_LEN (sizeof (TYPEINFO_PREFIX) - 1)
1189 symname = typeinfo_sym.minsym->demangled_name ();
1190 if (symname == NULL || strncmp (symname, TYPEINFO_PREFIX,
1191 TYPEINFO_PREFIX_LEN))
1192 error (_("typeinfo symbol '%s' has unexpected name"),
1193 typeinfo_sym.minsym->linkage_name ());
1194 class_name = symname + TYPEINFO_PREFIX_LEN;
1196 /* Strip off @plt and version suffixes. */
1197 atsign = strchr (class_name, '@');
1198 if (atsign != NULL)
1199 return std::string (class_name, atsign - class_name);
1200 return class_name;
1203 /* Implement the 'get_type_from_type_info' method. */
1205 static struct type *
1206 gnuv3_get_type_from_type_info (struct value *type_info_ptr)
1208 /* We have to parse the type name, since in general there is not a
1209 symbol for a type. This is somewhat bogus since there may be a
1210 mis-parse. Another approach might be to re-use the demangler's
1211 internal form to reconstruct the type somehow. */
1212 std::string type_name = gnuv3_get_typename_from_type_info (type_info_ptr);
1213 expression_up expr (parse_expression (type_name.c_str ()));
1214 struct value *type_val = expr->evaluate_type ();
1215 return type_val->type ();
1218 /* Determine if we are currently in a C++ thunk. If so, get the address
1219 of the routine we are thunking to and continue to there instead. */
1221 static CORE_ADDR
1222 gnuv3_skip_trampoline (frame_info_ptr frame, CORE_ADDR stop_pc)
1224 CORE_ADDR real_stop_pc, method_stop_pc, func_addr;
1225 struct gdbarch *gdbarch = get_frame_arch (frame);
1226 struct bound_minimal_symbol thunk_sym, fn_sym;
1227 struct obj_section *section;
1228 const char *thunk_name, *fn_name;
1230 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
1231 if (real_stop_pc == 0)
1232 real_stop_pc = stop_pc;
1234 /* Find the linker symbol for this potential thunk. */
1235 thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
1236 section = find_pc_section (real_stop_pc);
1237 if (thunk_sym.minsym == NULL || section == NULL)
1238 return 0;
1240 /* The symbol's demangled name should be something like "virtual
1241 thunk to FUNCTION", where FUNCTION is the name of the function
1242 being thunked to. */
1243 thunk_name = thunk_sym.minsym->demangled_name ();
1244 if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL)
1245 return 0;
1247 fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to ");
1248 fn_sym = lookup_minimal_symbol (fn_name, NULL, section->objfile);
1249 if (fn_sym.minsym == NULL)
1250 return 0;
1252 method_stop_pc = fn_sym.value_address ();
1254 /* Some targets have minimal symbols pointing to function descriptors
1255 (powerpc 64 for example). Make sure to retrieve the address
1256 of the real function from the function descriptor before passing on
1257 the address to other layers of GDB. */
1258 func_addr = gdbarch_convert_from_func_ptr_addr
1259 (gdbarch, method_stop_pc, current_inferior ()->top_target ());
1260 if (func_addr != 0)
1261 method_stop_pc = func_addr;
1263 real_stop_pc = gdbarch_skip_trampoline_code
1264 (gdbarch, frame, method_stop_pc);
1265 if (real_stop_pc == 0)
1266 real_stop_pc = method_stop_pc;
1268 return real_stop_pc;
1271 /* A member function is in one these states. */
1273 enum definition_style
1275 DOES_NOT_EXIST_IN_SOURCE,
1276 DEFAULTED_INSIDE,
1277 DEFAULTED_OUTSIDE,
1278 DELETED,
1279 EXPLICIT,
1282 /* Return how the given field is defined. */
1284 static definition_style
1285 get_def_style (struct fn_field *fn, int fieldelem)
1287 if (TYPE_FN_FIELD_DELETED (fn, fieldelem))
1288 return DELETED;
1290 if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
1291 return DOES_NOT_EXIST_IN_SOURCE;
1293 switch (TYPE_FN_FIELD_DEFAULTED (fn, fieldelem))
1295 case DW_DEFAULTED_no:
1296 return EXPLICIT;
1297 case DW_DEFAULTED_in_class:
1298 return DEFAULTED_INSIDE;
1299 case DW_DEFAULTED_out_of_class:
1300 return DEFAULTED_OUTSIDE;
1301 default:
1302 break;
1305 return EXPLICIT;
1308 /* Helper functions to determine whether the given definition style
1309 denotes that the definition is user-provided or implicit.
1310 Being defaulted outside the class decl counts as an explicit
1311 user-definition, while being defaulted inside is implicit. */
1313 static bool
1314 is_user_provided_def (definition_style def)
1316 return def == EXPLICIT || def == DEFAULTED_OUTSIDE;
1319 static bool
1320 is_implicit_def (definition_style def)
1322 return def == DOES_NOT_EXIST_IN_SOURCE || def == DEFAULTED_INSIDE;
1325 /* Helper function to decide if METHOD_TYPE is a copy/move
1326 constructor type for CLASS_TYPE. EXPECTED is the expected
1327 type code for the "right-hand-side" argument.
1328 This function is supposed to be used by the IS_COPY_CONSTRUCTOR_TYPE
1329 and IS_MOVE_CONSTRUCTOR_TYPE functions below. Normally, you should
1330 not need to call this directly. */
1332 static bool
1333 is_copy_or_move_constructor_type (struct type *class_type,
1334 struct type *method_type,
1335 type_code expected)
1337 /* The method should take at least two arguments... */
1338 if (method_type->num_fields () < 2)
1339 return false;
1341 /* ...and the second argument should be the same as the class
1342 type, with the expected type code... */
1343 struct type *arg_type = method_type->field (1).type ();
1345 if (arg_type->code () != expected)
1346 return false;
1348 struct type *target = check_typedef (arg_type->target_type ());
1349 if (!(class_types_same_p (target, class_type)))
1350 return false;
1352 /* ...and if any of the remaining arguments don't have a default value
1353 then this is not a copy or move constructor, but just a
1354 constructor. */
1355 for (int i = 2; i < method_type->num_fields (); i++)
1357 arg_type = method_type->field (i).type ();
1358 /* FIXME aktemur/2019-10-31: As of this date, neither
1359 clang++-7.0.0 nor g++-8.2.0 produce a DW_AT_default_value
1360 attribute. GDB is also not set to read this attribute, yet.
1361 Hence, we immediately return false if there are more than
1362 2 parameters.
1363 GCC bug link:
1364 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42959
1366 return false;
1369 return true;
1372 /* Return true if METHOD_TYPE is a copy ctor type for CLASS_TYPE. */
1374 static bool
1375 is_copy_constructor_type (struct type *class_type,
1376 struct type *method_type)
1378 return is_copy_or_move_constructor_type (class_type, method_type,
1379 TYPE_CODE_REF);
1382 /* Return true if METHOD_TYPE is a move ctor type for CLASS_TYPE. */
1384 static bool
1385 is_move_constructor_type (struct type *class_type,
1386 struct type *method_type)
1388 return is_copy_or_move_constructor_type (class_type, method_type,
1389 TYPE_CODE_RVALUE_REF);
1392 /* Return pass-by-reference information for the given TYPE.
1394 The rule in the v3 ABI document comes from section 3.1.1. If the
1395 type has a non-trivial copy constructor or destructor, then the
1396 caller must make a copy (by calling the copy constructor if there
1397 is one or perform the copy itself otherwise), pass the address of
1398 the copy, and then destroy the temporary (if necessary).
1400 For return values with non-trivial copy/move constructors or
1401 destructors, space will be allocated in the caller, and a pointer
1402 will be passed as the first argument (preceding "this").
1404 We don't have a bulletproof mechanism for determining whether a
1405 constructor or destructor is trivial. For GCC and DWARF5 debug
1406 information, we can check the calling_convention attribute,
1407 the 'artificial' flag, the 'defaulted' attribute, and the
1408 'deleted' attribute. */
1410 static struct language_pass_by_ref_info
1411 gnuv3_pass_by_reference (struct type *type)
1413 int fieldnum, fieldelem;
1415 type = check_typedef (type);
1417 /* Start with the default values. */
1418 struct language_pass_by_ref_info info;
1420 bool has_cc_attr = false;
1421 bool is_pass_by_value = false;
1422 bool is_dynamic = false;
1423 definition_style cctor_def = DOES_NOT_EXIST_IN_SOURCE;
1424 definition_style dtor_def = DOES_NOT_EXIST_IN_SOURCE;
1425 definition_style mctor_def = DOES_NOT_EXIST_IN_SOURCE;
1427 /* We're only interested in things that can have methods. */
1428 if (type->code () != TYPE_CODE_STRUCT
1429 && type->code () != TYPE_CODE_UNION)
1430 return info;
1432 /* The compiler may have emitted the calling convention attribute.
1433 Note: GCC does not produce this attribute as of version 9.2.1.
1434 Bug link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92418 */
1435 if (TYPE_CPLUS_CALLING_CONVENTION (type) == DW_CC_pass_by_value)
1437 has_cc_attr = true;
1438 is_pass_by_value = true;
1439 /* Do not return immediately. We have to find out if this type
1440 is copy_constructible and destructible. */
1443 if (TYPE_CPLUS_CALLING_CONVENTION (type) == DW_CC_pass_by_reference)
1445 has_cc_attr = true;
1446 is_pass_by_value = false;
1449 /* A dynamic class has a non-trivial copy constructor.
1450 See c++98 section 12.8 Copying class objects [class.copy]. */
1451 if (gnuv3_dynamic_class (type))
1452 is_dynamic = true;
1454 for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
1455 for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
1456 fieldelem++)
1458 struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
1459 const char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
1460 struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
1462 if (name[0] == '~')
1464 /* We've found a destructor.
1465 There should be at most one dtor definition. */
1466 gdb_assert (dtor_def == DOES_NOT_EXIST_IN_SOURCE);
1467 dtor_def = get_def_style (fn, fieldelem);
1469 else if (is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem))
1470 || TYPE_FN_FIELD_CONSTRUCTOR (fn, fieldelem))
1472 /* FIXME drow/2007-09-23: We could do this using the name of
1473 the method and the name of the class instead of dealing
1474 with the mangled name. We don't have a convenient function
1475 to strip off both leading scope qualifiers and trailing
1476 template arguments yet. */
1477 if (is_copy_constructor_type (type, fieldtype))
1479 /* There may be more than one cctors. E.g.: one that
1480 take a const parameter and another that takes a
1481 non-const parameter. Such as:
1483 class K {
1484 K (const K &k)...
1485 K (K &k)...
1488 It is sufficient for the type to be non-trivial
1489 even only one of the cctors is explicit.
1490 Therefore, update the cctor_def value in the
1491 implicit -> explicit direction, not backwards. */
1493 if (is_implicit_def (cctor_def))
1494 cctor_def = get_def_style (fn, fieldelem);
1496 else if (is_move_constructor_type (type, fieldtype))
1498 /* Again, there may be multiple move ctors. Update the
1499 mctor_def value if we found an explicit def and the
1500 existing one is not explicit. Otherwise retain the
1501 existing value. */
1502 if (is_implicit_def (mctor_def))
1503 mctor_def = get_def_style (fn, fieldelem);
1508 bool cctor_implicitly_deleted
1509 = (mctor_def != DOES_NOT_EXIST_IN_SOURCE
1510 && cctor_def == DOES_NOT_EXIST_IN_SOURCE);
1512 bool cctor_explicitly_deleted = (cctor_def == DELETED);
1514 if (cctor_implicitly_deleted || cctor_explicitly_deleted)
1515 info.copy_constructible = false;
1517 if (dtor_def == DELETED)
1518 info.destructible = false;
1520 info.trivially_destructible = is_implicit_def (dtor_def);
1522 info.trivially_copy_constructible
1523 = (is_implicit_def (cctor_def)
1524 && !is_dynamic);
1526 info.trivially_copyable
1527 = (info.trivially_copy_constructible
1528 && info.trivially_destructible
1529 && !is_user_provided_def (mctor_def));
1531 /* Even if all the constructors and destructors were artificial, one
1532 of them may have invoked a non-artificial constructor or
1533 destructor in a base class. If any base class needs to be passed
1534 by reference, so does this class. Similarly for members, which
1535 are constructed whenever this class is. We do not need to worry
1536 about recursive loops here, since we are only looking at members
1537 of complete class type. Also ignore any static members. */
1538 for (fieldnum = 0; fieldnum < type->num_fields (); fieldnum++)
1539 if (!type->field (fieldnum).is_static ())
1541 struct type *field_type = type->field (fieldnum).type ();
1543 /* For arrays, make the decision based on the element type. */
1544 if (field_type->code () == TYPE_CODE_ARRAY)
1545 field_type = check_typedef (field_type->target_type ());
1547 struct language_pass_by_ref_info field_info
1548 = gnuv3_pass_by_reference (field_type);
1550 if (!field_info.copy_constructible)
1551 info.copy_constructible = false;
1552 if (!field_info.destructible)
1553 info.destructible = false;
1554 if (!field_info.trivially_copyable)
1555 info.trivially_copyable = false;
1556 if (!field_info.trivially_copy_constructible)
1557 info.trivially_copy_constructible = false;
1558 if (!field_info.trivially_destructible)
1559 info.trivially_destructible = false;
1562 /* Consistency check. */
1563 if (has_cc_attr && info.trivially_copyable != is_pass_by_value)
1565 /* DWARF CC attribute is not the same as the inferred value;
1566 use the DWARF attribute. */
1567 info.trivially_copyable = is_pass_by_value;
1570 return info;
1573 static void
1574 init_gnuv3_ops (void)
1576 gnu_v3_abi_ops.shortname = "gnu-v3";
1577 gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
1578 gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
1579 gnu_v3_abi_ops.is_destructor_name =
1580 (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor;
1581 gnu_v3_abi_ops.is_constructor_name =
1582 (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor;
1583 gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
1584 gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
1585 gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
1586 gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
1587 gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
1588 gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr;
1589 gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size;
1590 gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr;
1591 gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value;
1592 gnu_v3_abi_ops.print_vtable = gnuv3_print_vtable;
1593 gnu_v3_abi_ops.get_typeid = gnuv3_get_typeid;
1594 gnu_v3_abi_ops.get_typeid_type = gnuv3_get_typeid_type;
1595 gnu_v3_abi_ops.get_type_from_type_info = gnuv3_get_type_from_type_info;
1596 gnu_v3_abi_ops.get_typename_from_type_info
1597 = gnuv3_get_typename_from_type_info;
1598 gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
1599 gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
1602 void _initialize_gnu_v3_abi ();
1603 void
1604 _initialize_gnu_v3_abi ()
1606 init_gnuv3_ops ();
1608 register_cp_abi (&gnu_v3_abi_ops);
1609 set_cp_abi_as_auto_default (gnu_v3_abi_ops.shortname);