More updated translations
[binutils-gdb.git] / gdb / gnu-v3-abi.c
blobb51c4d23fc7f89fd4a7a27a56ac81f3e41bd656a
1 /* Abstraction of GNU v3 abi.
2 Contributed by Jim Blandy <jimb@redhat.com>
4 Copyright (C) 2001-2024 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 "extract-store-integer.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"
36 #include "gdbsupport/unordered_map.h"
38 static struct cp_abi_ops gnu_v3_abi_ops;
40 /* A gdbarch key for std::type_info, in the event that it can't be
41 found in the debug info. */
43 static const registry<gdbarch>::key<struct type> std_type_info_gdbarch_data;
46 static int
47 gnuv3_is_vtable_name (const char *name)
49 return startswith (name, "_ZTV");
52 static int
53 gnuv3_is_operator_name (const char *name)
55 return startswith (name, CP_OPERATOR_STR);
59 /* To help us find the components of a vtable, we build ourselves a
60 GDB type object representing the vtable structure. Following the
61 V3 ABI, it goes something like this:
63 struct gdb_gnu_v3_abi_vtable {
65 / * An array of virtual call and virtual base offsets. The real
66 length of this array depends on the class hierarchy; we use
67 negative subscripts to access the elements. Yucky, but
68 better than the alternatives. * /
69 ptrdiff_t vcall_and_vbase_offsets[0];
71 / * The offset from a virtual pointer referring to this table
72 to the top of the complete object. * /
73 ptrdiff_t offset_to_top;
75 / * The type_info pointer for this class. This is really a
76 std::type_info *, but GDB doesn't really look at the
77 type_info object itself, so we don't bother to get the type
78 exactly right. * /
79 void *type_info;
81 / * Virtual table pointers in objects point here. * /
83 / * Virtual function pointers. Like the vcall/vbase array, the
84 real length of this table depends on the class hierarchy. * /
85 void (*virtual_functions[0]) ();
89 The catch, of course, is that the exact layout of this table
90 depends on the ABI --- word size, endianness, alignment, etc. So
91 the GDB type object is actually a per-architecture kind of thing.
93 vtable_type_gdbarch_data is a gdbarch per-architecture data pointer
94 which refers to the struct type * for this structure, laid out
95 appropriately for the architecture. */
96 static const registry<gdbarch>::key<struct type> vtable_type_gdbarch_data;
99 /* Human-readable names for the numbers of the fields above. */
100 enum {
101 vtable_field_vcall_and_vbase_offsets,
102 vtable_field_offset_to_top,
103 vtable_field_type_info,
104 vtable_field_virtual_functions
108 /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
109 described above, laid out appropriately for ARCH.
111 We use this function as the gdbarch per-architecture data
112 initialization function. */
113 static struct type *
114 get_gdb_vtable_type (struct gdbarch *arch)
116 struct type *t;
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 t = alloc.new_type (TYPE_CODE_STRUCT, 0, nullptr);
136 /* We assume no padding is necessary, since GDB doesn't know
137 anything about alignment at the moment. If this assumption bites
138 us, we should add a gdbarch method which, given a type, returns
139 the alignment that type requires, and then use that here. */
141 /* Build the field list. */
142 t->alloc_fields (4);
144 offset = 0;
146 /* ptrdiff_t vcall_and_vbase_offsets[0]; */
148 struct field &field0 = t->field (0);
149 field0.set_name ("vcall_and_vbase_offsets");
150 field0.set_type (lookup_array_range_type (ptrdiff_type, 0, -1));
151 field0.set_loc_bitpos (offset * TARGET_CHAR_BIT);
152 offset += field0.type ()->length ();
155 /* ptrdiff_t offset_to_top; */
157 struct field &field1 = t->field (1);
158 field1.set_name ("offset_to_top");
159 field1.set_type (ptrdiff_type);
160 field1.set_loc_bitpos (offset * TARGET_CHAR_BIT);
161 offset += field1.type ()->length ();
164 /* void *type_info; */
166 struct field &field2 = t->field (2);
167 field2.set_name ("type_info");
168 field2.set_type (void_ptr_type);
169 field2.set_loc_bitpos (offset * TARGET_CHAR_BIT);
170 offset += field2.type ()->length ();
173 /* void (*virtual_functions[0]) (); */
175 struct field &field3 = t->field (3);
176 field3.set_name ("virtual_functions");
177 field3.set_type (lookup_array_range_type (ptr_to_void_fn_type, 0, -1));
178 field3.set_loc_bitpos (offset * TARGET_CHAR_BIT);
179 offset += field3.type ()->length ();
182 t->set_length (offset);
184 t->set_name ("gdb_gnu_v3_abi_vtable");
185 INIT_CPLUS_SPECIFIC (t);
187 result = make_type_with_address_space (t, TYPE_INSTANCE_FLAG_CODE_SPACE);
188 vtable_type_gdbarch_data.set (arch, result);
189 return result;
193 /* Return the ptrdiff_t type used in the vtable type. */
194 static struct type *
195 vtable_ptrdiff_type (struct gdbarch *gdbarch)
197 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
199 /* The "offset_to_top" field has the appropriate (ptrdiff_t) type. */
200 return vtable_type->field (vtable_field_offset_to_top).type ();
203 /* Return the offset from the start of the imaginary `struct
204 gdb_gnu_v3_abi_vtable' object to the vtable's "address point"
205 (i.e., where objects' virtual table pointers point). */
206 static int
207 vtable_address_point_offset (struct gdbarch *gdbarch)
209 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
211 return (vtable_type->field (vtable_field_virtual_functions).loc_bitpos ()
212 / TARGET_CHAR_BIT);
216 /* Determine whether structure TYPE is a dynamic class. Cache the
217 result. */
219 static int
220 gnuv3_dynamic_class (struct type *type)
222 int fieldnum, fieldelem;
224 type = check_typedef (type);
225 gdb_assert (type->code () == TYPE_CODE_STRUCT
226 || type->code () == TYPE_CODE_UNION);
228 if (type->code () == TYPE_CODE_UNION)
229 return 0;
231 if (TYPE_CPLUS_DYNAMIC (type))
232 return TYPE_CPLUS_DYNAMIC (type) == 1;
234 ALLOCATE_CPLUS_STRUCT_TYPE (type);
236 for (fieldnum = 0; fieldnum < TYPE_N_BASECLASSES (type); fieldnum++)
237 if (BASETYPE_VIA_VIRTUAL (type, fieldnum)
238 || gnuv3_dynamic_class (type->field (fieldnum).type ()))
240 TYPE_CPLUS_DYNAMIC (type) = 1;
241 return 1;
244 for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
245 for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
246 fieldelem++)
248 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, fieldnum);
250 if (TYPE_FN_FIELD_VIRTUAL_P (f, fieldelem))
252 TYPE_CPLUS_DYNAMIC (type) = 1;
253 return 1;
257 TYPE_CPLUS_DYNAMIC (type) = -1;
258 return 0;
261 /* Find the vtable for a value of CONTAINER_TYPE located at
262 CONTAINER_ADDR. Return a value of the correct vtable type for this
263 architecture, or NULL if CONTAINER does not have a vtable. */
265 static struct value *
266 gnuv3_get_vtable (struct gdbarch *gdbarch,
267 struct type *container_type, CORE_ADDR container_addr)
269 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
270 struct type *vtable_pointer_type;
271 struct value *vtable_pointer;
272 CORE_ADDR vtable_address;
274 container_type = check_typedef (container_type);
275 gdb_assert (container_type->code () == TYPE_CODE_STRUCT);
277 /* If this type does not have a virtual table, don't read the first
278 field. */
279 if (!gnuv3_dynamic_class (container_type))
280 return NULL;
282 /* We do not consult the debug information to find the virtual table.
283 The ABI specifies that it is always at offset zero in any class,
284 and debug information may not represent it.
286 We avoid using value_contents on principle, because the object might
287 be large. */
289 /* Find the type "pointer to virtual table". */
290 vtable_pointer_type = lookup_pointer_type (vtable_type);
292 /* Load it from the start of the class. */
293 vtable_pointer = value_at (vtable_pointer_type, container_addr);
294 vtable_address = value_as_address (vtable_pointer);
296 /* Correct it to point at the start of the virtual table, rather
297 than the address point. */
298 return value_at_lazy (vtable_type,
299 vtable_address
300 - vtable_address_point_offset (gdbarch));
304 static struct type *
305 gnuv3_rtti_type (struct value *value,
306 int *full_p, LONGEST *top_p, int *using_enc_p)
308 struct gdbarch *gdbarch;
309 struct type *values_type = check_typedef (value->type ());
310 struct value *vtable;
311 struct minimal_symbol *vtable_symbol;
312 const char *vtable_symbol_name;
313 const char *class_name;
314 struct type *run_time_type;
315 LONGEST offset_to_top;
316 const char *atsign;
318 /* We only have RTTI for dynamic class objects. */
319 if (values_type->code () != TYPE_CODE_STRUCT
320 || !gnuv3_dynamic_class (values_type))
321 return NULL;
323 /* Determine architecture. */
324 gdbarch = values_type->arch ();
326 if (using_enc_p)
327 *using_enc_p = 0;
329 vtable = gnuv3_get_vtable (gdbarch, values_type,
330 value_as_address (value_addr (value)));
331 if (vtable == NULL)
332 return NULL;
334 /* Find the linker symbol for this vtable. */
335 vtable_symbol
336 = lookup_minimal_symbol_by_pc (vtable->address ()
337 + vtable->embedded_offset ()).minsym;
338 if (! vtable_symbol)
339 return NULL;
341 /* The symbol's demangled name should be something like "vtable for
342 CLASS", where CLASS is the name of the run-time type of VALUE.
343 If we didn't like this approach, we could instead look in the
344 type_info object itself to get the class name. But this way
345 should work just as well, and doesn't read target memory. */
346 vtable_symbol_name = vtable_symbol->demangled_name ();
347 if (vtable_symbol_name == NULL
348 || !startswith (vtable_symbol_name, "vtable for "))
350 warning (_("can't find linker symbol for virtual table for `%s' value"),
351 TYPE_SAFE_NAME (values_type));
352 if (vtable_symbol_name)
353 warning (_(" found `%s' instead"), vtable_symbol_name);
354 return NULL;
356 class_name = vtable_symbol_name + 11;
358 /* Strip off @plt and version suffixes. */
359 atsign = strchr (class_name, '@');
360 if (atsign != NULL)
362 char *copy;
364 copy = (char *) alloca (atsign - class_name + 1);
365 memcpy (copy, class_name, atsign - class_name);
366 copy[atsign - class_name] = '\0';
367 class_name = copy;
370 /* Try to look up the class name as a type name. */
371 /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */
372 run_time_type = cp_lookup_rtti_type (class_name, NULL);
373 if (run_time_type == NULL)
374 return NULL;
376 /* Get the offset from VALUE to the top of the complete object.
377 NOTE: this is the reverse of the meaning of *TOP_P. */
378 offset_to_top
379 = value_as_long (value_field (vtable, vtable_field_offset_to_top));
381 if (full_p)
382 *full_p = (- offset_to_top == value->embedded_offset ()
383 && (value->enclosing_type ()->length ()
384 >= run_time_type->length ()));
385 if (top_p)
386 *top_p = - offset_to_top;
387 return run_time_type;
390 /* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
391 function, of type FNTYPE. */
393 static struct value *
394 gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
395 struct type *fntype, int vtable_index)
397 struct value *vtable, *vfn;
399 /* Every class with virtual functions must have a vtable. */
400 vtable = gnuv3_get_vtable (gdbarch, container->type (),
401 value_as_address (value_addr (container)));
402 gdb_assert (vtable != NULL);
404 /* Fetch the appropriate function pointer from the vtable. */
405 vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
406 vtable_index);
408 /* If this architecture uses function descriptors directly in the vtable,
409 then the address of the vtable entry is actually a "function pointer"
410 (i.e. points to the descriptor). We don't need to scale the index
411 by the size of a function descriptor; GCC does that before outputting
412 debug information. */
413 if (gdbarch_vtable_function_descriptors (gdbarch))
414 vfn = value_addr (vfn);
416 /* Cast the function pointer to the appropriate type. */
417 vfn = value_cast (lookup_pointer_type (fntype), vfn);
419 return vfn;
422 /* GNU v3 implementation of value_virtual_fn_field. See cp-abi.h
423 for a description of the arguments. */
425 static struct value *
426 gnuv3_virtual_fn_field (struct value **value_p,
427 struct fn_field *f, int j,
428 struct type *vfn_base, int offset)
430 struct type *values_type = check_typedef ((*value_p)->type ());
431 struct gdbarch *gdbarch;
433 /* Some simple sanity checks. */
434 if (values_type->code () != TYPE_CODE_STRUCT)
435 error (_("Only classes can have virtual functions."));
437 /* Determine architecture. */
438 gdbarch = values_type->arch ();
440 /* Cast our value to the base class which defines this virtual
441 function. This takes care of any necessary `this'
442 adjustments. */
443 if (vfn_base != values_type)
444 *value_p = value_cast (vfn_base, *value_p);
446 return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
447 TYPE_FN_FIELD_VOFFSET (f, j));
450 /* Compute the offset of the baseclass which is
451 the INDEXth baseclass of class TYPE,
452 for value at VALADDR (in host) at ADDRESS (in target).
453 The result is the offset of the baseclass value relative
454 to (the address of)(ARG) + OFFSET.
456 -1 is returned on error. */
458 static int
459 gnuv3_baseclass_offset (struct type *type, int index,
460 const bfd_byte *valaddr, LONGEST embedded_offset,
461 CORE_ADDR address, const struct value *val)
463 struct gdbarch *gdbarch;
464 struct type *ptr_type;
465 struct value *vtable;
466 struct value *vbase_array;
467 long int cur_base_offset, base_offset;
469 /* Determine architecture. */
470 gdbarch = type->arch ();
471 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
473 /* If it isn't a virtual base, this is easy. The offset is in the
474 type definition. */
475 if (!BASETYPE_VIA_VIRTUAL (type, index))
476 return TYPE_BASECLASS_BITPOS (type, index) / 8;
478 /* If we have a DWARF expression for the offset, evaluate it. */
479 if (type->field (index).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
481 struct dwarf2_property_baton baton;
482 baton.property_type
483 = lookup_pointer_type (type->field (index).type ());
484 baton.locexpr = *type->field (index).loc_dwarf_block ();
486 struct dynamic_prop prop;
487 prop.set_locexpr (&baton);
489 struct property_addr_info addr_stack;
490 addr_stack.type = type;
491 /* Note that we don't set "valaddr" here. Doing so causes
492 regressions. FIXME. */
493 addr_stack.addr = address + embedded_offset;
494 addr_stack.next = nullptr;
496 CORE_ADDR result;
497 if (dwarf2_evaluate_property (&prop, nullptr, &addr_stack, &result,
498 {addr_stack.addr}))
499 return (int) (result - addr_stack.addr);
502 /* To access a virtual base, we need to use the vbase offset stored in
503 our vtable. Recent GCC versions provide this information. If it isn't
504 available, we could get what we needed from RTTI, or from drawing the
505 complete inheritance graph based on the debug info. Neither is
506 worthwhile. */
507 cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
508 if (cur_base_offset >= - vtable_address_point_offset (gdbarch))
509 error (_("Expected a negative vbase offset (old compiler?)"));
511 cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch);
512 if ((- cur_base_offset) % ptr_type->length () != 0)
513 error (_("Misaligned vbase offset."));
514 cur_base_offset = cur_base_offset / ((int) ptr_type->length ());
516 vtable = gnuv3_get_vtable (gdbarch, type, address + embedded_offset);
517 gdb_assert (vtable != NULL);
518 vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
519 base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset));
520 return base_offset;
523 /* Locate a virtual method in DOMAIN or its non-virtual base classes
524 which has virtual table index VOFFSET. The method has an associated
525 "this" adjustment of ADJUSTMENT bytes. */
527 static const char *
528 gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
529 LONGEST adjustment)
531 int i;
533 /* Search this class first. */
534 if (adjustment == 0)
536 int len;
538 len = TYPE_NFN_FIELDS (domain);
539 for (i = 0; i < len; i++)
541 int len2, j;
542 struct fn_field *f;
544 f = TYPE_FN_FIELDLIST1 (domain, i);
545 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
547 check_stub_method_group (domain, i);
548 for (j = 0; j < len2; j++)
549 if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset)
550 return TYPE_FN_FIELD_PHYSNAME (f, j);
554 /* Next search non-virtual bases. If it's in a virtual base,
555 we're out of luck. */
556 for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
558 int pos;
559 struct type *basetype;
561 if (BASETYPE_VIA_VIRTUAL (domain, i))
562 continue;
564 pos = TYPE_BASECLASS_BITPOS (domain, i) / 8;
565 basetype = domain->field (i).type ();
566 /* Recurse with a modified adjustment. We don't need to adjust
567 voffset. */
568 if (adjustment >= pos && adjustment < pos + basetype->length ())
569 return gnuv3_find_method_in (basetype, voffset, adjustment - pos);
572 return NULL;
575 /* Decode GNU v3 method pointer. */
577 static int
578 gnuv3_decode_method_ptr (struct gdbarch *gdbarch,
579 const gdb_byte *contents,
580 CORE_ADDR *value_p,
581 LONGEST *adjustment_p)
583 struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr;
584 struct type *offset_type = vtable_ptrdiff_type (gdbarch);
585 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
586 CORE_ADDR ptr_value;
587 LONGEST voffset, adjustment;
588 int vbit;
590 /* Extract the pointer to member. The first element is either a pointer
591 or a vtable offset. For pointers, we need to use extract_typed_address
592 to allow the back-end to convert the pointer to a GDB address -- but
593 vtable offsets we must handle as integers. At this point, we do not
594 yet know which case we have, so we extract the value under both
595 interpretations and choose the right one later on. */
596 ptr_value = extract_typed_address (contents, funcptr_type);
597 voffset = extract_signed_integer (contents,
598 funcptr_type->length (), byte_order);
599 contents += funcptr_type->length ();
600 adjustment = extract_signed_integer (contents,
601 offset_type->length (), byte_order);
603 if (!gdbarch_vbit_in_delta (gdbarch))
605 vbit = voffset & 1;
606 voffset = voffset ^ vbit;
608 else
610 vbit = adjustment & 1;
611 adjustment = adjustment >> 1;
614 *value_p = vbit? voffset : ptr_value;
615 *adjustment_p = adjustment;
616 return vbit;
619 /* GNU v3 implementation of cplus_print_method_ptr. */
621 static void
622 gnuv3_print_method_ptr (const gdb_byte *contents,
623 struct type *type,
624 struct ui_file *stream)
626 struct type *self_type = TYPE_SELF_TYPE (type);
627 struct gdbarch *gdbarch = self_type->arch ();
628 CORE_ADDR ptr_value;
629 LONGEST adjustment;
630 int vbit;
632 /* Extract the pointer to member. */
633 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
635 /* Check for NULL. */
636 if (ptr_value == 0 && vbit == 0)
638 gdb_printf (stream, "NULL");
639 return;
642 /* Search for a virtual method. */
643 if (vbit)
645 CORE_ADDR voffset;
646 const char *physname;
648 /* It's a virtual table offset, maybe in this class. Search
649 for a field with the correct vtable offset. First convert it
650 to an index, as used in TYPE_FN_FIELD_VOFFSET. */
651 voffset = ptr_value / vtable_ptrdiff_type (gdbarch)->length ();
653 physname = gnuv3_find_method_in (self_type, voffset, adjustment);
655 /* If we found a method, print that. We don't bother to disambiguate
656 possible paths to the method based on the adjustment. */
657 if (physname)
659 gdb::unique_xmalloc_ptr<char> demangled_name
660 = gdb_demangle (physname, DMGL_ANSI | DMGL_PARAMS);
662 gdb_printf (stream, "&virtual ");
663 if (demangled_name == NULL)
664 gdb_puts (physname, stream);
665 else
666 gdb_puts (demangled_name.get (), stream);
667 return;
670 else if (ptr_value != 0)
672 /* Found a non-virtual function: print out the type. */
673 gdb_puts ("(", stream);
674 c_print_type (type, "", stream, -1, 0, current_language->la_language,
675 &type_print_raw_options);
676 gdb_puts (") ", stream);
679 /* We didn't find it; print the raw data. */
680 if (vbit)
682 gdb_printf (stream, "&virtual table offset ");
683 print_longest (stream, 'd', 1, ptr_value);
685 else
687 struct value_print_options opts;
689 get_user_print_options (&opts);
690 print_address_demangle (&opts, gdbarch, ptr_value, stream, demangle);
693 if (adjustment)
695 gdb_printf (stream, ", this adjustment ");
696 print_longest (stream, 'd', 1, adjustment);
700 /* GNU v3 implementation of cplus_method_ptr_size. */
702 static int
703 gnuv3_method_ptr_size (struct type *type)
705 return 2 * builtin_type (type->arch ())->builtin_data_ptr->length ();
708 /* GNU v3 implementation of cplus_make_method_ptr. */
710 static void
711 gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
712 CORE_ADDR value, int is_virtual)
714 struct gdbarch *gdbarch = type->arch ();
715 int size = builtin_type (gdbarch)->builtin_data_ptr->length ();
716 enum bfd_endian byte_order = type_byte_order (type);
718 /* FIXME drow/2006-12-24: The adjustment of "this" is currently
719 always zero, since the method pointer is of the correct type.
720 But if the method pointer came from a base class, this is
721 incorrect - it should be the offset to the base. The best
722 fix might be to create the pointer to member pointing at the
723 base class and cast it to the derived class, but that requires
724 support for adjusting pointers to members when casting them -
725 not currently supported by GDB. */
727 if (!gdbarch_vbit_in_delta (gdbarch))
729 store_unsigned_integer (contents, size, byte_order, value | is_virtual);
730 store_unsigned_integer (contents + size, size, byte_order, 0);
732 else
734 store_unsigned_integer (contents, size, byte_order, value);
735 store_unsigned_integer (contents + size, size, byte_order, is_virtual);
739 /* GNU v3 implementation of cplus_method_ptr_to_value. */
741 static struct value *
742 gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
744 struct gdbarch *gdbarch;
745 const gdb_byte *contents = method_ptr->contents ().data ();
746 CORE_ADDR ptr_value;
747 struct type *self_type, *final_type, *method_type;
748 LONGEST adjustment;
749 int vbit;
751 self_type = TYPE_SELF_TYPE (check_typedef (method_ptr->type ()));
752 final_type = lookup_pointer_type (self_type);
754 method_type = check_typedef (method_ptr->type ())->target_type ();
756 /* Extract the pointer to member. */
757 gdbarch = self_type->arch ();
758 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
760 /* First convert THIS to match the containing type of the pointer to
761 member. This cast may adjust the value of THIS. */
762 *this_p = value_cast (final_type, *this_p);
764 /* Then apply whatever adjustment is necessary. This creates a somewhat
765 strange pointer: it claims to have type FINAL_TYPE, but in fact it
766 might not be a valid FINAL_TYPE. For instance, it might be a
767 base class of FINAL_TYPE. And if it's not the primary base class,
768 then printing it out as a FINAL_TYPE object would produce some pretty
769 garbage.
771 But we don't really know the type of the first argument in
772 METHOD_TYPE either, which is why this happens. We can't
773 dereference this later as a FINAL_TYPE, but once we arrive in the
774 called method we'll have debugging information for the type of
775 "this" - and that'll match the value we produce here.
777 You can provoke this case by casting a Base::* to a Derived::*, for
778 instance. */
779 *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p);
780 *this_p = value_ptradd (*this_p, adjustment);
781 *this_p = value_cast (final_type, *this_p);
783 if (vbit)
785 LONGEST voffset;
787 voffset = ptr_value / vtable_ptrdiff_type (gdbarch)->length ();
788 return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
789 method_type, voffset);
791 else
792 return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
795 struct vtable_value_hash_t
797 std::size_t operator() (value *val) const noexcept
798 { return val->address () + val->embedded_offset (); }
801 struct vtable_value_eq_t
803 bool operator() (value *lhs, value *rhs) const noexcept
805 return (lhs->address () + lhs->embedded_offset ()
806 == rhs->address () + rhs->embedded_offset ());
810 using vtable_hash_t
811 = gdb::unordered_map<value *, int, vtable_value_hash_t, vtable_value_eq_t>;
813 /* Comparison function used for sorting the vtable entries. */
815 static bool
816 compare_value_and_voffset (const std::pair<value *, int> &va,
817 const std::pair<value *, int> &vb)
819 CORE_ADDR addra = va.first->address () + va.first->embedded_offset ();
820 CORE_ADDR addrb = vb.first->address () + vb.first->embedded_offset ();
822 return addra < addrb;
825 /* A helper function used when printing vtables. This determines the
826 key (most derived) sub-object at each address and also computes the
827 maximum vtable offset seen for the corresponding vtable. Updates
828 OFFSET_HASH with a new value_and_voffset object, if needed. VALUE
829 is the object to examine. */
831 static void
832 compute_vtable_size (vtable_hash_t &offset_hash, struct value *value)
834 int i;
835 struct type *type = check_typedef (value->type ());
837 gdb_assert (type->code () == TYPE_CODE_STRUCT);
839 /* If the object is not dynamic, then we are done; as it cannot have
840 dynamic base types either. */
841 if (!gnuv3_dynamic_class (type))
842 return;
844 /* Update the hash and the vec, if needed. */
845 int &current_max_voffset = offset_hash.emplace (value, -1).first->second;
847 /* Update the value_and_voffset object with the highest vtable
848 offset from this class. */
849 for (i = 0; i < TYPE_NFN_FIELDS (type); ++i)
851 int j;
852 struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, i);
854 for (j = 0; j < TYPE_FN_FIELDLIST_LENGTH (type, i); ++j)
856 if (TYPE_FN_FIELD_VIRTUAL_P (fn, j))
858 int voffset = TYPE_FN_FIELD_VOFFSET (fn, j);
860 if (voffset > current_max_voffset)
861 current_max_voffset = voffset;
866 /* Recurse into base classes. */
867 for (i = 0; i < TYPE_N_BASECLASSES (type); ++i)
868 compute_vtable_size (offset_hash, value_field (value, i));
871 /* Helper for gnuv3_print_vtable that prints a single vtable. */
873 static void
874 print_one_vtable (struct gdbarch *gdbarch, struct value *value,
875 int max_voffset,
876 struct value_print_options *opts)
878 int i;
879 struct type *type = check_typedef (value->type ());
880 struct value *vtable;
881 CORE_ADDR vt_addr;
883 vtable = gnuv3_get_vtable (gdbarch, type,
884 value->address ()
885 + value->embedded_offset ());
886 vt_addr = value_field (vtable,
887 vtable_field_virtual_functions)->address ();
889 gdb_printf (_("vtable for '%s' @ %s (subobject @ %s):\n"),
890 TYPE_SAFE_NAME (type),
891 paddress (gdbarch, vt_addr),
892 paddress (gdbarch, (value->address ()
893 + value->embedded_offset ())));
895 for (i = 0; i <= max_voffset; ++i)
897 /* Initialize it just to avoid a GCC false warning. */
898 CORE_ADDR addr = 0;
899 int got_error = 0;
900 struct value *vfn;
902 gdb_printf ("[%d]: ", i);
904 vfn = value_subscript (value_field (vtable,
905 vtable_field_virtual_functions),
908 if (gdbarch_vtable_function_descriptors (gdbarch))
909 vfn = value_addr (vfn);
913 addr = value_as_address (vfn);
915 catch (const gdb_exception_error &ex)
917 fprintf_styled (gdb_stdout, metadata_style.style (),
918 _("<error: %s>"), ex.what ());
919 got_error = 1;
922 if (!got_error)
923 print_function_pointer_address (opts, gdbarch, addr, gdb_stdout);
924 gdb_printf ("\n");
928 /* Implementation of the print_vtable method. */
930 static void
931 gnuv3_print_vtable (struct value *value)
933 struct gdbarch *gdbarch;
934 struct type *type;
935 struct value *vtable;
936 struct value_print_options opts;
937 int count;
939 value = coerce_ref (value);
940 type = check_typedef (value->type ());
941 if (type->code () == TYPE_CODE_PTR)
943 value = value_ind (value);
944 type = check_typedef (value->type ());
947 get_user_print_options (&opts);
949 /* Respect 'set print object'. */
950 if (opts.objectprint)
952 value = value_full_object (value, NULL, 0, 0, 0);
953 type = check_typedef (value->type ());
956 gdbarch = type->arch ();
958 vtable = NULL;
959 if (type->code () == TYPE_CODE_STRUCT)
960 vtable = gnuv3_get_vtable (gdbarch, type,
961 value_as_address (value_addr (value)));
963 if (!vtable)
965 gdb_printf (_("This object does not have a virtual function table\n"));
966 return;
969 vtable_hash_t offset_hash;
970 compute_vtable_size (offset_hash, value);
972 std::vector<std::pair<struct value *, int>> result_vec (offset_hash.begin (),
973 offset_hash.end ());
974 std::sort (result_vec.begin (), result_vec.end (),
975 compare_value_and_voffset);
977 count = 0;
978 for (auto &item : result_vec)
980 if (item.second >= 0)
982 if (count > 0)
983 gdb_printf ("\n");
984 print_one_vtable (gdbarch, item.first, item.second, &opts);
985 ++count;
990 /* Return a GDB type representing `struct std::type_info', laid out
991 appropriately for ARCH.
993 We use this function as the gdbarch per-architecture data
994 initialization function. */
996 static struct type *
997 build_std_type_info_type (struct gdbarch *arch)
999 struct type *t;
1000 int offset;
1001 struct type *void_ptr_type
1002 = builtin_type (arch)->builtin_data_ptr;
1003 struct type *char_type
1004 = builtin_type (arch)->builtin_char;
1005 struct type *char_ptr_type
1006 = make_pointer_type (make_cv_type (1, 0, char_type, NULL), NULL);
1008 t = type_allocator (arch).new_type (TYPE_CODE_STRUCT, 0, nullptr);
1010 t->alloc_fields (2);
1012 offset = 0;
1014 /* The vtable. */
1016 struct field &field0 = t->field (0);
1017 field0.set_name ("_vptr.type_info");
1018 field0.set_type (void_ptr_type);
1019 field0.set_loc_bitpos (offset * TARGET_CHAR_BIT);
1020 offset += field0.type ()->length ();
1023 /* The name. */
1025 struct field &field1 = t->field (1);
1026 field1.set_name ("__name");
1027 field1.set_type (char_ptr_type);
1028 field1.set_loc_bitpos (offset * TARGET_CHAR_BIT);
1029 offset += field1.type ()->length ();
1032 t->set_length (offset);
1034 t->set_name ("gdb_gnu_v3_type_info");
1035 INIT_CPLUS_SPECIFIC (t);
1037 return t;
1040 /* Implement the 'get_typeid_type' method. */
1042 static struct type *
1043 gnuv3_get_typeid_type (struct gdbarch *gdbarch)
1045 struct symbol *typeinfo;
1046 struct type *typeinfo_type;
1048 typeinfo = lookup_symbol ("std::type_info", NULL, SEARCH_STRUCT_DOMAIN,
1049 NULL).symbol;
1050 if (typeinfo == NULL)
1052 typeinfo_type = std_type_info_gdbarch_data.get (gdbarch);
1053 if (typeinfo_type == nullptr)
1055 typeinfo_type = build_std_type_info_type (gdbarch);
1056 std_type_info_gdbarch_data.set (gdbarch, typeinfo_type);
1059 else
1060 typeinfo_type = typeinfo->type ();
1062 return typeinfo_type;
1065 /* Implement the 'get_typeid' method. */
1067 static struct value *
1068 gnuv3_get_typeid (struct value *value)
1070 struct type *typeinfo_type;
1071 struct type *type;
1072 struct gdbarch *gdbarch;
1073 struct value *result;
1074 std::string type_name;
1075 gdb::unique_xmalloc_ptr<char> canonical;
1077 /* We have to handle values a bit trickily here, to allow this code
1078 to work properly with non_lvalue values that are really just
1079 disguised types. */
1080 if (value->lval () == lval_memory)
1081 value = coerce_ref (value);
1083 type = check_typedef (value->type ());
1085 /* In the non_lvalue case, a reference might have slipped through
1086 here. */
1087 if (type->code () == TYPE_CODE_REF)
1088 type = check_typedef (type->target_type ());
1090 /* Ignore top-level cv-qualifiers. */
1091 type = make_cv_type (0, 0, type, NULL);
1092 gdbarch = type->arch ();
1094 type_name = type_to_string (type);
1095 if (type_name.empty ())
1096 error (_("cannot find typeinfo for unnamed type"));
1098 /* We need to canonicalize the type name here, because we do lookups
1099 using the demangled name, and so we must match the format it
1100 uses. E.g., GDB tends to use "const char *" as a type name, but
1101 the demangler uses "char const *". */
1102 canonical = cp_canonicalize_string (type_name.c_str ());
1103 const char *name = (canonical == nullptr
1104 ? type_name.c_str ()
1105 : canonical.get ());
1107 typeinfo_type = gnuv3_get_typeid_type (gdbarch);
1109 /* We check for lval_memory because in the "typeid (type-id)" case,
1110 the type is passed via a not_lval value object. */
1111 if (type->code () == TYPE_CODE_STRUCT
1112 && value->lval () == lval_memory
1113 && gnuv3_dynamic_class (type))
1115 struct value *vtable, *typeinfo_value;
1116 CORE_ADDR address = value->address () + value->embedded_offset ();
1118 vtable = gnuv3_get_vtable (gdbarch, type, address);
1119 if (vtable == NULL)
1120 error (_("cannot find typeinfo for object of type '%s'"),
1121 name);
1122 typeinfo_value = value_field (vtable, vtable_field_type_info);
1123 result = value_ind (value_cast (make_pointer_type (typeinfo_type, NULL),
1124 typeinfo_value));
1126 else
1128 std::string sym_name = std::string ("typeinfo for ") + name;
1129 bound_minimal_symbol minsym
1130 = lookup_minimal_symbol (current_program_space, sym_name.c_str ());
1132 if (minsym.minsym == NULL)
1133 error (_("could not find typeinfo symbol for '%s'"), name);
1135 result = value_at_lazy (typeinfo_type, minsym.value_address ());
1138 return result;
1141 /* Implement the 'get_typename_from_type_info' method. */
1143 static std::string
1144 gnuv3_get_typename_from_type_info (struct value *type_info_ptr)
1146 struct gdbarch *gdbarch = type_info_ptr->type ()->arch ();
1147 CORE_ADDR addr;
1148 const char *symname;
1149 const char *class_name;
1150 const char *atsign;
1152 addr = value_as_address (type_info_ptr);
1153 bound_minimal_symbol typeinfo_sym = lookup_minimal_symbol_by_pc (addr);
1154 if (typeinfo_sym.minsym == NULL)
1155 error (_("could not find minimal symbol for typeinfo address %s"),
1156 paddress (gdbarch, addr));
1158 #define TYPEINFO_PREFIX "typeinfo for "
1159 #define TYPEINFO_PREFIX_LEN (sizeof (TYPEINFO_PREFIX) - 1)
1160 symname = typeinfo_sym.minsym->demangled_name ();
1161 if (symname == NULL || strncmp (symname, TYPEINFO_PREFIX,
1162 TYPEINFO_PREFIX_LEN))
1163 error (_("typeinfo symbol '%s' has unexpected name"),
1164 typeinfo_sym.minsym->linkage_name ());
1165 class_name = symname + TYPEINFO_PREFIX_LEN;
1167 /* Strip off @plt and version suffixes. */
1168 atsign = strchr (class_name, '@');
1169 if (atsign != NULL)
1170 return std::string (class_name, atsign - class_name);
1171 return class_name;
1174 /* Implement the 'get_type_from_type_info' method. */
1176 static struct type *
1177 gnuv3_get_type_from_type_info (struct value *type_info_ptr)
1179 /* We have to parse the type name, since in general there is not a
1180 symbol for a type. This is somewhat bogus since there may be a
1181 mis-parse. Another approach might be to re-use the demangler's
1182 internal form to reconstruct the type somehow. */
1183 std::string type_name = gnuv3_get_typename_from_type_info (type_info_ptr);
1184 expression_up expr (parse_expression (type_name.c_str ()));
1185 struct value *type_val = expr->evaluate_type ();
1186 return type_val->type ();
1189 /* Determine if we are currently in a C++ thunk. If so, get the address
1190 of the routine we are thunking to and continue to there instead. */
1192 static CORE_ADDR
1193 gnuv3_skip_trampoline (const frame_info_ptr &frame, CORE_ADDR stop_pc)
1195 CORE_ADDR real_stop_pc, method_stop_pc, func_addr;
1196 struct gdbarch *gdbarch = get_frame_arch (frame);
1197 struct obj_section *section;
1198 const char *thunk_name, *fn_name;
1200 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
1201 if (real_stop_pc == 0)
1202 real_stop_pc = stop_pc;
1204 /* Find the linker symbol for this potential thunk. */
1205 bound_minimal_symbol thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
1206 section = find_pc_section (real_stop_pc);
1207 if (thunk_sym.minsym == NULL || section == NULL)
1208 return 0;
1210 /* The symbol's demangled name should be something like "virtual
1211 thunk to FUNCTION", where FUNCTION is the name of the function
1212 being thunked to. */
1213 thunk_name = thunk_sym.minsym->demangled_name ();
1214 if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL)
1215 return 0;
1217 fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to ");
1218 bound_minimal_symbol fn_sym
1219 = lookup_minimal_symbol (current_program_space, fn_name, section->objfile);
1220 if (fn_sym.minsym == NULL)
1221 return 0;
1223 method_stop_pc = fn_sym.value_address ();
1225 /* Some targets have minimal symbols pointing to function descriptors
1226 (powerpc 64 for example). Make sure to retrieve the address
1227 of the real function from the function descriptor before passing on
1228 the address to other layers of GDB. */
1229 func_addr = gdbarch_convert_from_func_ptr_addr
1230 (gdbarch, method_stop_pc, current_inferior ()->top_target ());
1231 if (func_addr != 0)
1232 method_stop_pc = func_addr;
1234 real_stop_pc = gdbarch_skip_trampoline_code
1235 (gdbarch, frame, method_stop_pc);
1236 if (real_stop_pc == 0)
1237 real_stop_pc = method_stop_pc;
1239 return real_stop_pc;
1242 /* A member function is in one these states. */
1244 enum definition_style
1246 DOES_NOT_EXIST_IN_SOURCE,
1247 DEFAULTED_INSIDE,
1248 DEFAULTED_OUTSIDE,
1249 DELETED,
1250 EXPLICIT,
1253 /* Return how the given field is defined. */
1255 static definition_style
1256 get_def_style (struct fn_field *fn, int fieldelem)
1258 if (TYPE_FN_FIELD_DELETED (fn, fieldelem))
1259 return DELETED;
1261 if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
1262 return DOES_NOT_EXIST_IN_SOURCE;
1264 switch (TYPE_FN_FIELD_DEFAULTED (fn, fieldelem))
1266 case DW_DEFAULTED_no:
1267 return EXPLICIT;
1268 case DW_DEFAULTED_in_class:
1269 return DEFAULTED_INSIDE;
1270 case DW_DEFAULTED_out_of_class:
1271 return DEFAULTED_OUTSIDE;
1272 default:
1273 break;
1276 return EXPLICIT;
1279 /* Helper functions to determine whether the given definition style
1280 denotes that the definition is user-provided or implicit.
1281 Being defaulted outside the class decl counts as an explicit
1282 user-definition, while being defaulted inside is implicit. */
1284 static bool
1285 is_user_provided_def (definition_style def)
1287 return def == EXPLICIT || def == DEFAULTED_OUTSIDE;
1290 static bool
1291 is_implicit_def (definition_style def)
1293 return def == DOES_NOT_EXIST_IN_SOURCE || def == DEFAULTED_INSIDE;
1296 /* Helper function to decide if METHOD_TYPE is a copy/move
1297 constructor type for CLASS_TYPE. EXPECTED is the expected
1298 type code for the "right-hand-side" argument.
1299 This function is supposed to be used by the IS_COPY_CONSTRUCTOR_TYPE
1300 and IS_MOVE_CONSTRUCTOR_TYPE functions below. Normally, you should
1301 not need to call this directly. */
1303 static bool
1304 is_copy_or_move_constructor_type (struct type *class_type,
1305 struct type *method_type,
1306 type_code expected)
1308 /* The method should take at least two arguments... */
1309 if (method_type->num_fields () < 2)
1310 return false;
1312 /* ...and the second argument should be the same as the class
1313 type, with the expected type code... */
1314 struct type *arg_type = method_type->field (1).type ();
1316 if (arg_type->code () != expected)
1317 return false;
1319 struct type *target = check_typedef (arg_type->target_type ());
1320 if (!(class_types_same_p (target, class_type)))
1321 return false;
1323 /* ...and if any of the remaining arguments don't have a default value
1324 then this is not a copy or move constructor, but just a
1325 constructor. */
1326 for (int i = 2; i < method_type->num_fields (); i++)
1328 arg_type = method_type->field (i).type ();
1329 /* FIXME aktemur/2019-10-31: As of this date, neither
1330 clang++-7.0.0 nor g++-8.2.0 produce a DW_AT_default_value
1331 attribute. GDB is also not set to read this attribute, yet.
1332 Hence, we immediately return false if there are more than
1333 2 parameters.
1334 GCC bug link:
1335 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42959
1337 return false;
1340 return true;
1343 /* Return true if METHOD_TYPE is a copy ctor type for CLASS_TYPE. */
1345 static bool
1346 is_copy_constructor_type (struct type *class_type,
1347 struct type *method_type)
1349 return is_copy_or_move_constructor_type (class_type, method_type,
1350 TYPE_CODE_REF);
1353 /* Return true if METHOD_TYPE is a move ctor type for CLASS_TYPE. */
1355 static bool
1356 is_move_constructor_type (struct type *class_type,
1357 struct type *method_type)
1359 return is_copy_or_move_constructor_type (class_type, method_type,
1360 TYPE_CODE_RVALUE_REF);
1363 /* Return pass-by-reference information for the given TYPE.
1365 The rule in the v3 ABI document comes from section 3.1.1. If the
1366 type has a non-trivial copy constructor or destructor, then the
1367 caller must make a copy (by calling the copy constructor if there
1368 is one or perform the copy itself otherwise), pass the address of
1369 the copy, and then destroy the temporary (if necessary).
1371 For return values with non-trivial copy/move constructors or
1372 destructors, space will be allocated in the caller, and a pointer
1373 will be passed as the first argument (preceding "this").
1375 We don't have a bulletproof mechanism for determining whether a
1376 constructor or destructor is trivial. For GCC and DWARF5 debug
1377 information, we can check the calling_convention attribute,
1378 the 'artificial' flag, the 'defaulted' attribute, and the
1379 'deleted' attribute. */
1381 static struct language_pass_by_ref_info
1382 gnuv3_pass_by_reference (struct type *type)
1384 int fieldnum, fieldelem;
1386 type = check_typedef (type);
1388 /* Start with the default values. */
1389 struct language_pass_by_ref_info info;
1391 bool has_cc_attr = false;
1392 bool is_pass_by_value = false;
1393 bool is_dynamic = false;
1394 definition_style cctor_def = DOES_NOT_EXIST_IN_SOURCE;
1395 definition_style dtor_def = DOES_NOT_EXIST_IN_SOURCE;
1396 definition_style mctor_def = DOES_NOT_EXIST_IN_SOURCE;
1398 /* We're only interested in things that can have methods. */
1399 if (type->code () != TYPE_CODE_STRUCT
1400 && type->code () != TYPE_CODE_UNION)
1401 return info;
1403 /* The compiler may have emitted the calling convention attribute.
1404 Note: GCC does not produce this attribute as of version 9.2.1.
1405 Bug link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92418 */
1406 if (TYPE_CPLUS_CALLING_CONVENTION (type) == DW_CC_pass_by_value)
1408 has_cc_attr = true;
1409 is_pass_by_value = true;
1410 /* Do not return immediately. We have to find out if this type
1411 is copy_constructible and destructible. */
1414 if (TYPE_CPLUS_CALLING_CONVENTION (type) == DW_CC_pass_by_reference)
1416 has_cc_attr = true;
1417 is_pass_by_value = false;
1420 /* A dynamic class has a non-trivial copy constructor.
1421 See c++98 section 12.8 Copying class objects [class.copy]. */
1422 if (gnuv3_dynamic_class (type))
1423 is_dynamic = true;
1425 for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
1426 for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
1427 fieldelem++)
1429 struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
1430 const char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
1431 struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
1433 if (name[0] == '~')
1435 /* We've found a destructor.
1436 There should be at most one dtor definition. */
1437 gdb_assert (dtor_def == DOES_NOT_EXIST_IN_SOURCE);
1438 dtor_def = get_def_style (fn, fieldelem);
1440 else if (is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem))
1441 || TYPE_FN_FIELD_CONSTRUCTOR (fn, fieldelem))
1443 /* FIXME drow/2007-09-23: We could do this using the name of
1444 the method and the name of the class instead of dealing
1445 with the mangled name. We don't have a convenient function
1446 to strip off both leading scope qualifiers and trailing
1447 template arguments yet. */
1448 if (is_copy_constructor_type (type, fieldtype))
1450 /* There may be more than one cctors. E.g.: one that
1451 take a const parameter and another that takes a
1452 non-const parameter. Such as:
1454 class K {
1455 K (const K &k)...
1456 K (K &k)...
1459 It is sufficient for the type to be non-trivial
1460 even only one of the cctors is explicit.
1461 Therefore, update the cctor_def value in the
1462 implicit -> explicit direction, not backwards. */
1464 if (is_implicit_def (cctor_def))
1465 cctor_def = get_def_style (fn, fieldelem);
1467 else if (is_move_constructor_type (type, fieldtype))
1469 /* Again, there may be multiple move ctors. Update the
1470 mctor_def value if we found an explicit def and the
1471 existing one is not explicit. Otherwise retain the
1472 existing value. */
1473 if (is_implicit_def (mctor_def))
1474 mctor_def = get_def_style (fn, fieldelem);
1479 bool cctor_implicitly_deleted
1480 = (mctor_def != DOES_NOT_EXIST_IN_SOURCE
1481 && cctor_def == DOES_NOT_EXIST_IN_SOURCE);
1483 bool cctor_explicitly_deleted = (cctor_def == DELETED);
1485 if (cctor_implicitly_deleted || cctor_explicitly_deleted)
1486 info.copy_constructible = false;
1488 if (dtor_def == DELETED)
1489 info.destructible = false;
1491 info.trivially_destructible = is_implicit_def (dtor_def);
1493 info.trivially_copy_constructible
1494 = (is_implicit_def (cctor_def)
1495 && !is_dynamic);
1497 info.trivially_copyable
1498 = (info.trivially_copy_constructible
1499 && info.trivially_destructible
1500 && !is_user_provided_def (mctor_def));
1502 /* Even if all the constructors and destructors were artificial, one
1503 of them may have invoked a non-artificial constructor or
1504 destructor in a base class. If any base class needs to be passed
1505 by reference, so does this class. Similarly for members, which
1506 are constructed whenever this class is. We do not need to worry
1507 about recursive loops here, since we are only looking at members
1508 of complete class type. Also ignore any static members. */
1509 for (fieldnum = 0; fieldnum < type->num_fields (); fieldnum++)
1510 if (!type->field (fieldnum).is_static ())
1512 struct type *field_type = type->field (fieldnum).type ();
1514 /* For arrays, make the decision based on the element type. */
1515 if (field_type->code () == TYPE_CODE_ARRAY)
1516 field_type = check_typedef (field_type->target_type ());
1518 struct language_pass_by_ref_info field_info
1519 = gnuv3_pass_by_reference (field_type);
1521 if (!field_info.copy_constructible)
1522 info.copy_constructible = false;
1523 if (!field_info.destructible)
1524 info.destructible = false;
1525 if (!field_info.trivially_copyable)
1526 info.trivially_copyable = false;
1527 if (!field_info.trivially_copy_constructible)
1528 info.trivially_copy_constructible = false;
1529 if (!field_info.trivially_destructible)
1530 info.trivially_destructible = false;
1533 /* Consistency check. */
1534 if (has_cc_attr && info.trivially_copyable != is_pass_by_value)
1536 /* DWARF CC attribute is not the same as the inferred value;
1537 use the DWARF attribute. */
1538 info.trivially_copyable = is_pass_by_value;
1541 return info;
1544 static void
1545 init_gnuv3_ops (void)
1547 gnu_v3_abi_ops.shortname = "gnu-v3";
1548 gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
1549 gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
1550 gnu_v3_abi_ops.is_destructor_name =
1551 (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor;
1552 gnu_v3_abi_ops.is_constructor_name =
1553 (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor;
1554 gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
1555 gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
1556 gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
1557 gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
1558 gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
1559 gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr;
1560 gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size;
1561 gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr;
1562 gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value;
1563 gnu_v3_abi_ops.print_vtable = gnuv3_print_vtable;
1564 gnu_v3_abi_ops.get_typeid = gnuv3_get_typeid;
1565 gnu_v3_abi_ops.get_typeid_type = gnuv3_get_typeid_type;
1566 gnu_v3_abi_ops.get_type_from_type_info = gnuv3_get_type_from_type_info;
1567 gnu_v3_abi_ops.get_typename_from_type_info
1568 = gnuv3_get_typename_from_type_info;
1569 gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
1570 gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
1573 void _initialize_gnu_v3_abi ();
1574 void
1575 _initialize_gnu_v3_abi ()
1577 init_gnuv3_ops ();
1579 register_cp_abi (&gnu_v3_abi_ops);
1580 set_cp_abi_as_auto_default (gnu_v3_abi_ops.shortname);