]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/compile/compile-cplus-types.c
Add dynamic_prop::is_constant
[thirdparty/binutils-gdb.git] / gdb / compile / compile-cplus-types.c
1 /* Convert types from GDB to GCC
2
3 Copyright (C) 2014-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 #include "defs.h"
22 #include "gdbsupport/preprocessor.h"
23 #include "gdbtypes.h"
24 #include "compile-internal.h"
25 #include "compile-cplus.h"
26 #include "gdbsupport/gdb_assert.h"
27 #include "symtab.h"
28 #include "source.h"
29 #include "cp-support.h"
30 #include "cp-abi.h"
31 #include "objfiles.h"
32 #include "block.h"
33 #include "gdbcmd.h"
34 #include "c-lang.h"
35 #include "compile-c.h"
36 #include <algorithm>
37
38 /* Default compile flags for C++. */
39
40 const char *compile_cplus_instance::m_default_cflags = "-std=gnu++11";
41
42 /* Flag to enable internal debugging. */
43
44 static bool debug_compile_cplus_types = false;
45
46 /* Flag to enable internal scope switching debugging. */
47
48 static bool debug_compile_cplus_scopes = false;
49
50 /* Forward declarations. */
51
52 static gcc_type compile_cplus_convert_func (compile_cplus_instance *instance,
53 struct type *type,
54 bool strip_artificial);
55
56 /* See description in compile-cplus.h. */
57
58 gdb::unique_xmalloc_ptr<char>
59 compile_cplus_instance::decl_name (const char *natural)
60 {
61 if (natural == nullptr)
62 return nullptr;
63
64 gdb::unique_xmalloc_ptr<char> name = cp_func_name (natural);
65 if (name != nullptr)
66 return name;
67
68 return make_unique_xstrdup (natural);
69 }
70
71 /* Get the access flag for the NUM'th field of TYPE. */
72
73 static enum gcc_cp_symbol_kind
74 get_field_access_flag (const struct type *type, int num)
75 {
76 if (TYPE_FIELD_PROTECTED (type, num))
77 return GCC_CP_ACCESS_PROTECTED;
78 else if (TYPE_FIELD_PRIVATE (type, num))
79 return GCC_CP_ACCESS_PRIVATE;
80
81 /* GDB assumes everything else is public. */
82 return GCC_CP_ACCESS_PUBLIC;
83 }
84
85 /* Get the access flag for the NUM'th method of TYPE's FNI'th
86 fieldlist. */
87
88 enum gcc_cp_symbol_kind
89 get_method_access_flag (const struct type *type, int fni, int num)
90 {
91 gdb_assert (type->code () == TYPE_CODE_STRUCT);
92
93 /* If this type was not declared a class, everything is public. */
94 if (!type->is_declared_class ())
95 return GCC_CP_ACCESS_PUBLIC;
96
97 /* Otherwise, read accessibility from the fn_field. */
98 const struct fn_field *methods = TYPE_FN_FIELDLIST1 (type, fni);
99 if (TYPE_FN_FIELD_PROTECTED (methods, num))
100 return GCC_CP_ACCESS_PROTECTED;
101 else if (TYPE_FN_FIELD_PRIVATE (methods, num))
102 return GCC_CP_ACCESS_PRIVATE;
103 else
104 return GCC_CP_ACCESS_PUBLIC;
105 }
106
107 /* A useful debugging function to output the scope SCOPE to stdout. */
108
109 static void __attribute__ ((used))
110 debug_print_scope (const compile_scope &scope)
111 {
112 for (const auto &comp: scope)
113 {
114 const char *symbol = (comp.bsymbol.symbol != nullptr
115 ? comp.bsymbol.symbol->natural_name ()
116 : "<none>");
117
118 printf_unfiltered ("\tname = %s, symbol = %s\n", comp.name.c_str (),
119 symbol);
120 }
121 }
122
123 /* See description in compile-cplus.h. */
124
125 compile_scope
126 type_name_to_scope (const char *type_name, const struct block *block)
127 {
128 compile_scope scope;
129
130 if (type_name == nullptr)
131 {
132 /* An anonymous type. We cannot really do much here. We simply cannot
133 look up anonymous types easily/at all. */
134 return scope;
135 }
136
137 const char *p = type_name;
138 std::string lookup_name;
139
140 while (*p != '\0')
141 {
142 /* Create a string token of the first component of TYPE_NAME. */
143 int len = cp_find_first_component (p);
144 std::string s (p, len);
145
146 /* Advance past the last token. */
147 p += len;
148
149 /* Look up the symbol and decide when to stop. */
150 if (!lookup_name.empty ())
151 lookup_name += "::";
152 lookup_name += s;
153
154 /* Look up the resulting name. */
155 struct block_symbol bsymbol
156 = lookup_symbol (lookup_name.c_str (), block, VAR_DOMAIN, nullptr);
157
158 if (bsymbol.symbol != nullptr)
159 {
160 scope_component comp = {s, bsymbol};
161
162 scope.push_back (comp);
163
164 if (bsymbol.symbol->type ()->code () != TYPE_CODE_NAMESPACE)
165 {
166 /* We're done. */
167 break;
168 }
169 }
170
171 if (*p == ':')
172 {
173 ++p;
174 if (*p == ':')
175 ++p;
176 else
177 {
178 /* This shouldn't happen since we are not attempting to
179 loop over user input. This name is generated by GDB
180 from debug info. */
181 internal_error (_("malformed TYPE_NAME during parsing"));
182 }
183 }
184 }
185
186 return scope;
187 }
188
189 /* Compare two scope_components for equality. These are equal if the names
190 of the two components' are the same. */
191
192 bool
193 operator== (const scope_component &lhs, const scope_component &rhs)
194 {
195 return lhs.name == rhs.name;
196 }
197
198 /* Compare two scope_components for inequality. These are not equal if
199 the two components' names are not equal. */
200
201 bool
202 operator!= (const scope_component &lhs, const scope_component &rhs)
203 {
204 return lhs.name != rhs.name;
205 }
206
207 /* Compare two compile_scopes for equality. These are equal if they are both
208 contain the same number of components and each component is equal. */
209
210 bool
211 operator== (const compile_scope &lhs, const compile_scope &rhs)
212 {
213 if (lhs.size () != rhs.size ())
214 return false;
215
216 for (int i = 0; i < lhs.size (); ++i)
217 {
218 if (lhs[i] != rhs[i])
219 return false;
220 }
221
222 return true;
223 }
224
225 /* Compare two compile_scopes for inequality. These are inequal if they
226 contain unequal number of elements or if any of the components are not
227 the same. */
228
229 bool
230 operator!= (const compile_scope &lhs, const compile_scope &rhs)
231 {
232 if (lhs.size () != rhs.size ())
233 return true;
234
235 for (int i = 0; i < lhs.size (); ++i)
236 {
237 if (lhs[i] != rhs[i])
238 return true;
239 }
240
241 return false;
242 }
243
244 /* See description in compile-cplus.h. */
245
246 void
247 compile_cplus_instance::enter_scope (compile_scope &&new_scope)
248 {
249 bool must_push = m_scopes.empty () || m_scopes.back () != new_scope;
250
251 new_scope.m_pushed = must_push;
252
253 /* Save the new scope. */
254 m_scopes.push_back (std::move (new_scope));
255
256 if (must_push)
257 {
258 if (debug_compile_cplus_scopes)
259 {
260 gdb_printf (gdb_stdlog, "entering new scope %s\n",
261 host_address_to_string (&m_scopes.back ()));
262 }
263
264 /* Push the global namespace. */
265 plugin ().push_namespace ("");
266
267 /* Push all other namespaces. Note that we do not push the last
268 scope_component -- that's the actual type we are converting. */
269 std::for_each
270 (m_scopes.back ().begin (), m_scopes.back ().end () - 1,
271 [this] (const scope_component &comp)
272 {
273 gdb_assert (comp.bsymbol.symbol->type ()->code ()
274 == TYPE_CODE_NAMESPACE);
275
276 const char *ns = (comp.name == CP_ANONYMOUS_NAMESPACE_STR ? nullptr
277 : comp.name.c_str ());
278
279 this->plugin ().push_namespace (ns);
280 });
281 }
282 else
283 {
284 if (debug_compile_cplus_scopes)
285 {
286 gdb_printf (gdb_stdlog, "staying in current scope -- "
287 "scopes are identical\n");
288 }
289 }
290 }
291
292 /* See description in compile-cplus.h. */
293
294 void
295 compile_cplus_instance::leave_scope ()
296 {
297 /* Get the current scope and remove it from the internal list of
298 scopes. */
299 compile_scope current = m_scopes.back ();
300
301 m_scopes.pop_back ();
302
303 if (current.m_pushed)
304 {
305 if (debug_compile_cplus_scopes)
306 {
307 gdb_printf (gdb_stdlog, "leaving scope %s\n",
308 host_address_to_string (&current));
309 }
310
311 /* Pop namespaces. */
312 std::for_each
313 (current.begin (),current.end () - 1,
314 [this] (const scope_component &comp) {
315 gdb_assert (comp.bsymbol.symbol->type ()->code ()
316 == TYPE_CODE_NAMESPACE);
317 this->plugin ().pop_binding_level (comp.name.c_str ());
318 });
319
320 /* Pop global namespace. */
321 plugin ().pop_binding_level ("");
322 }
323 else
324 {
325 if (debug_compile_cplus_scopes)
326 gdb_printf (gdb_stdlog,
327 "identical scopes -- not leaving scope\n");
328 }
329 }
330
331 /* See description in compile-cplus.h. */
332
333 compile_scope
334 compile_cplus_instance::new_scope (const char *type_name, struct type *type)
335 {
336 /* Break the type name into components. If TYPE was defined in some
337 superclass, we do not process TYPE but process the enclosing type
338 instead. */
339 compile_scope scope = type_name_to_scope (type_name, block ());
340
341 if (!scope.empty ())
342 {
343 /* Get the name of the last component, which should be the
344 unqualified name of the type to process. */
345 scope_component &comp = scope.back ();
346
347 if (!types_equal (type, comp.bsymbol.symbol->type ())
348 && (m_scopes.empty ()
349 || (m_scopes.back ().back ().bsymbol.symbol
350 != comp.bsymbol.symbol)))
351 {
352 /* The type is defined inside another class(es). Convert that
353 type instead of defining this type. */
354 convert_type (comp.bsymbol.symbol->type ());
355
356 /* If the original type (passed in to us) is defined in a nested
357 class, the previous call will give us that type's gcc_type.
358 Upper layers are expecting to get the original type's
359 gcc_type! */
360 get_cached_type (type, &scope.m_nested_type);
361 return scope;
362 }
363 }
364 else
365 {
366 if (type->name () == nullptr)
367 {
368 /* Anonymous type */
369
370 /* We don't have a qualified name for this to look up, but
371 we need a scope. We have to assume, then, that it is the same
372 as the current scope, if any. */
373 if (!m_scopes.empty ())
374 {
375 scope = m_scopes.back ();
376 scope.m_pushed = false;
377 }
378 else
379 scope.push_back (scope_component ());
380 }
381 else
382 {
383 scope_component comp
384 = {
385 decl_name (type->name ()).get (),
386 lookup_symbol (type->name (), block (), VAR_DOMAIN, nullptr)
387 };
388 scope.push_back (comp);
389 }
390 }
391
392 /* There must be at least one component in the compile_scope. */
393 gdb_assert (scope.size () > 0);
394 return scope;
395 }
396
397 /* See description in compile-cplus.h. */
398
399 gcc_type
400 compile_cplus_instance::convert_reference_base
401 (gcc_type base, enum gcc_cp_ref_qualifiers rquals)
402 {
403 return this->plugin ().build_reference_type (base, rquals);
404 }
405
406 /* Convert a reference type to its gcc representation. */
407
408 static gcc_type
409 compile_cplus_convert_reference (compile_cplus_instance *instance,
410 struct type *type)
411 {
412 gcc_type target = instance->convert_type (type->target_type ());
413
414 enum gcc_cp_ref_qualifiers quals = GCC_CP_REF_QUAL_NONE;
415 switch (type->code ())
416 {
417 case TYPE_CODE_REF:
418 quals = GCC_CP_REF_QUAL_LVALUE;
419 break;
420 case TYPE_CODE_RVALUE_REF:
421 quals = GCC_CP_REF_QUAL_RVALUE;
422 break;
423 default:
424 gdb_assert_not_reached ("unexpected type code for reference type");
425 }
426
427 return instance->convert_reference_base (target, quals);
428 }
429
430 /* See description in compile-cplus.h. */
431
432 gcc_type
433 compile_cplus_instance::convert_pointer_base(gcc_type target)
434 {
435 return plugin ().build_pointer_type (target);
436 }
437
438 /* Convert a pointer type to its gcc representation. */
439
440 static gcc_type
441 compile_cplus_convert_pointer (compile_cplus_instance *instance,
442 struct type *type)
443 {
444 gcc_type target = instance->convert_type (type->target_type ());
445
446 return instance->convert_pointer_base (target);
447 }
448
449 /* Convert an array type to its gcc representation. */
450
451 static gcc_type
452 compile_cplus_convert_array (compile_cplus_instance *instance,
453 struct type *type)
454 {
455 struct type *range = type->index_type ();
456 gcc_type element_type = instance->convert_type (type->target_type ());
457
458 if (!range->bounds ()->low.is_constant ())
459 {
460 const char *s = _("array type with non-constant"
461 " lower bound is not supported");
462
463 return instance->plugin ().error (s);
464 }
465
466 if (range->bounds ()->low.const_val () != 0)
467 {
468 const char *s = _("cannot convert array type with "
469 "non-zero lower bound to C");
470
471 return instance->plugin ().error (s);
472 }
473
474 if (range->bounds ()->high.kind () == PROP_LOCEXPR
475 || range->bounds ()->high.kind () == PROP_LOCLIST)
476 {
477 if (type->is_vector ())
478 {
479 const char *s = _("variably-sized vector type is not supported");
480
481 return instance->plugin ().error (s);
482 }
483
484 std::string upper_bound
485 = c_get_range_decl_name (&range->bounds ()->high);
486 return instance->plugin ().build_vla_array_type (element_type,
487 upper_bound.c_str ());
488 }
489 else
490 {
491 LONGEST low_bound, high_bound, count;
492
493 if (!get_array_bounds (type, &low_bound, &high_bound))
494 count = -1;
495 else
496 {
497 gdb_assert (low_bound == 0); /* Ensured above. */
498 count = high_bound + 1;
499 }
500
501 if (type->is_vector ())
502 return instance->plugin ().build_vector_type (element_type, count);
503
504 return instance->plugin ().build_array_type (element_type, count);
505 }
506 }
507
508 /* Convert a typedef of TYPE. If not GCC_CP_ACCESS_NONE, NESTED_ACCESS
509 will define the accessibility of the typedef definition in its
510 containing class. */
511
512 static gcc_type
513 compile_cplus_convert_typedef (compile_cplus_instance *instance,
514 struct type *type,
515 enum gcc_cp_symbol_kind nested_access)
516 {
517 compile_scope scope = instance->new_scope (type->name (), type);
518
519 if (scope.nested_type () != GCC_TYPE_NONE)
520 return scope.nested_type ();
521
522 gdb::unique_xmalloc_ptr<char> name
523 = compile_cplus_instance::decl_name (type->name ());
524
525 /* Make sure the scope for this type has been pushed. */
526 instance->enter_scope (std::move (scope));
527
528 /* Convert the typedef's real type. */
529 gcc_type typedef_type = instance->convert_type (check_typedef (type));
530
531 instance->plugin ().build_decl ("typedef", name.get (),
532 GCC_CP_SYMBOL_TYPEDEF | nested_access,
533 typedef_type, 0, 0, nullptr, 0);
534
535 /* Completed this scope. */
536 instance->leave_scope ();
537 return typedef_type;
538 }
539
540 /* Convert types defined in TYPE. */
541
542 static void
543 compile_cplus_convert_type_defns (compile_cplus_instance *instance,
544 struct type *type)
545 {
546 int i;
547 enum gcc_cp_symbol_kind accessibility;
548
549 /* Convert typedefs. */
550 for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); ++i)
551 {
552 if (TYPE_TYPEDEF_FIELD_PROTECTED (type, i))
553 accessibility = GCC_CP_ACCESS_PROTECTED;
554 else if (TYPE_TYPEDEF_FIELD_PRIVATE (type, i))
555 accessibility = GCC_CP_ACCESS_PRIVATE;
556 else
557 accessibility = GCC_CP_ACCESS_PUBLIC;
558 instance->convert_type (TYPE_TYPEDEF_FIELD_TYPE (type, i), accessibility);
559 }
560
561 /* Convert nested types. */
562 for (i = 0; i < TYPE_NESTED_TYPES_COUNT (type); ++i)
563 {
564 if (TYPE_NESTED_TYPES_FIELD_PROTECTED (type, i))
565 accessibility = GCC_CP_ACCESS_PROTECTED;
566 else if (TYPE_NESTED_TYPES_FIELD_PRIVATE (type, i))
567 accessibility = GCC_CP_ACCESS_PRIVATE;
568 else
569 accessibility = GCC_CP_ACCESS_PUBLIC;
570 instance->convert_type (TYPE_NESTED_TYPES_FIELD_TYPE (type, i),
571 accessibility);
572 }
573 }
574
575 /* Convert data members defined in TYPE, which should be struct/class/union
576 with gcc_type COMP_TYPE. */
577
578 static void
579 compile_cplus_convert_struct_or_union_members
580 (compile_cplus_instance *instance, struct type *type, gcc_type comp_type)
581 {
582 for (int i = TYPE_N_BASECLASSES (type); i < type->num_fields (); ++i)
583 {
584 const char *field_name = type->field (i).name ();
585
586 if (TYPE_FIELD_IGNORE (type, i)
587 || TYPE_FIELD_ARTIFICIAL (type, i))
588 continue;
589
590 /* GDB records unnamed/anonymous fields with empty string names. */
591 if (*field_name == '\0')
592 field_name = nullptr;
593
594 gcc_type field_type
595 = instance->convert_type (type->field (i).type ());
596
597 if (type->field (i).is_static ())
598 {
599 CORE_ADDR physaddr;
600
601 switch (type->field (i).loc_kind ())
602 {
603 case FIELD_LOC_KIND_PHYSADDR:
604 {
605 physaddr = type->field (i).loc_physaddr ();
606
607 instance->plugin ().build_decl
608 ("field physaddr", field_name,
609 (GCC_CP_SYMBOL_VARIABLE | get_field_access_flag (type, i)),
610 field_type, nullptr, physaddr, nullptr, 0);
611 }
612 break;
613
614 case FIELD_LOC_KIND_PHYSNAME:
615 {
616 const char *physname = type->field (i).loc_physname ();
617 struct block_symbol sym
618 = lookup_symbol (physname, instance->block (),
619 VAR_DOMAIN, nullptr);
620
621 if (sym.symbol == nullptr)
622 {
623 /* We didn't actually find the symbol. There's little
624 we can do but ignore this member. */
625 continue;
626 }
627 const char *filename = sym.symbol->symtab ()->filename;
628 unsigned int line = sym.symbol->line ();
629
630 physaddr = sym.symbol->value_address ();
631 instance->plugin ().build_decl
632 ("field physname", field_name,
633 (GCC_CP_SYMBOL_VARIABLE| get_field_access_flag (type, i)),
634 field_type, nullptr, physaddr, filename, line);
635 }
636 break;
637
638 default:
639 gdb_assert_not_reached
640 ("unexpected static field location kind");
641 }
642 }
643 else
644 {
645 unsigned long bitsize = TYPE_FIELD_BITSIZE (type, i);
646 enum gcc_cp_symbol_kind field_flags = GCC_CP_SYMBOL_FIELD
647 | get_field_access_flag (type, i);
648
649 if (bitsize == 0)
650 bitsize = 8 * type->field (i).type ()->length ();
651
652 instance->plugin ().build_field
653 (field_name, field_type, field_flags, bitsize,
654 type->field (i).loc_bitpos ());
655 }
656 }
657 }
658
659 /* Convert a method type to its gcc representation. */
660
661 static gcc_type
662 compile_cplus_convert_method (compile_cplus_instance *instance,
663 struct type *parent_type,
664 struct type *method_type)
665 {
666 /* Get the actual function type of the method, the corresponding class's
667 type and corresponding qualifier flags. */
668 gcc_type func_type = compile_cplus_convert_func (instance, method_type, true);
669 gcc_type class_type = instance->convert_type (parent_type);
670 gcc_cp_qualifiers_flags quals = 0;
671
672 if (TYPE_CONST (method_type))
673 quals |= GCC_CP_QUALIFIER_CONST;
674 if (TYPE_VOLATILE (method_type))
675 quals |= GCC_CP_QUALIFIER_VOLATILE;
676 if (TYPE_RESTRICT (method_type))
677 quals |= GCC_CP_QUALIFIER_RESTRICT;
678
679 /* Not yet implemented. */
680 gcc_cp_ref_qualifiers_flags rquals = GCC_CP_REF_QUAL_NONE;
681
682 return instance->plugin ().build_method_type
683 (class_type, func_type, quals.raw (), rquals.raw ());
684 }
685
686 /* Convert a member or method pointer represented by TYPE. */
687
688 static gcc_type
689 compile_cplus_convert_memberptr (compile_cplus_instance *instance,
690 struct type *type)
691 {
692 struct type *containing_class = TYPE_SELF_TYPE (type);
693
694 if (containing_class == nullptr)
695 return GCC_TYPE_NONE;
696
697 gcc_type class_type = instance->convert_type (containing_class);
698 gcc_type member_type
699 = instance->convert_type (type->target_type ());
700
701 return instance->plugin ().build_pointer_to_member_type
702 (class_type, member_type);
703 }
704
705 /* Convert all methods defined in TYPE, which should be a class/struct/union
706 with gcc_type CLASS_TYPE. */
707
708 static void
709 compile_cplus_convert_struct_or_union_methods (compile_cplus_instance *instance,
710 struct type *type,
711 gcc_type class_type)
712 {
713 for (int i = 0; i < TYPE_NFN_FIELDS (type); ++i)
714 {
715 struct fn_field *methods = TYPE_FN_FIELDLIST1 (type, i);
716 gdb::unique_xmalloc_ptr<char> overloaded_name
717 = compile_cplus_instance::decl_name (TYPE_FN_FIELDLIST_NAME (type, i));
718
719 /* Loop through the fieldlist, adding decls to the compiler's
720 representation of the class. */
721 for (int j = 0; j < TYPE_FN_FIELDLIST_LENGTH (type, i); ++j)
722 {
723 /* Skip artificial methods. */
724 if (TYPE_FN_FIELD_ARTIFICIAL (methods, j))
725 continue;
726
727 gcc_cp_symbol_kind_flags sym_kind = GCC_CP_SYMBOL_FUNCTION;
728 gcc_type method_type;
729 struct block_symbol sym
730 = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (methods, j),
731 instance->block (), VAR_DOMAIN, nullptr);
732
733 if (sym.symbol == nullptr)
734 {
735 if (TYPE_FN_FIELD_VIRTUAL_P (methods, j))
736 {
737 /* This is beyond hacky, and is really only a workaround for
738 detecting pure virtual methods. */
739 method_type = compile_cplus_convert_method
740 (instance, type, TYPE_FN_FIELD_TYPE (methods, j));
741
742 instance->plugin ().build_decl
743 ("pure virtual method", overloaded_name.get (),
744 (sym_kind
745 | get_method_access_flag (type, i, j)
746 | GCC_CP_FLAG_VIRTUAL_FUNCTION
747 | GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION).raw (),
748 method_type, nullptr, 0, nullptr, 0);
749 continue;
750 }
751
752 /* This can happen if we have a DW_AT_declaration DIE
753 for the method, but no "definition"-type DIE (with
754 DW_AT_specification referencing the decl DIE), i.e.,
755 the compiler has probably optimized the method away.
756
757 In this case, all we can hope to do is issue a warning
758 to the user letting him know. If the user has not actually
759 requested using this method, things should still work. */
760 warning (_("Method %s appears to be optimized out.\n"
761 "All references to this method will be undefined."),
762 TYPE_FN_FIELD_PHYSNAME (methods, j));
763 continue;
764 }
765
766 const char *filename = sym.symbol->symtab ()->filename;
767 unsigned int line = sym.symbol->line ();
768 CORE_ADDR address = sym.symbol->value_block()->start ();
769 const char *kind;
770
771 if (TYPE_FN_FIELD_STATIC_P (methods, j))
772 {
773 kind = "static method";
774 method_type = compile_cplus_convert_func
775 (instance, TYPE_FN_FIELD_TYPE (methods, j), true);
776 }
777 else
778 {
779 kind = "method";
780 method_type = (compile_cplus_convert_method
781 (instance, type, TYPE_FN_FIELD_TYPE (methods, j)));
782 }
783
784 if (TYPE_FN_FIELD_VIRTUAL_P (methods, j))
785 sym_kind |= GCC_CP_FLAG_VIRTUAL_FUNCTION;
786
787 instance->plugin ().build_decl
788 (kind, overloaded_name.get (),
789 (sym_kind | get_method_access_flag (type, i, j)).raw (),
790 method_type, nullptr, address, filename, line);
791 }
792 }
793 }
794
795 /* Convert a struct or union type to its gcc representation. If this type
796 was defined in another type, NESTED_ACCESS should indicate the
797 accessibility of this type. */
798
799 static gcc_type
800 compile_cplus_convert_struct_or_union (compile_cplus_instance *instance,
801 struct type *type,
802 enum gcc_cp_symbol_kind nested_access)
803 {
804 const char *filename = nullptr;
805 unsigned int line = 0;
806
807 /* Get the decl name of this type. */
808 gdb::unique_xmalloc_ptr<char> name
809 = compile_cplus_instance::decl_name (type->name ());
810
811 /* Create a new scope for TYPE. */
812 compile_scope scope = instance->new_scope (type->name (), type);
813
814 if (scope.nested_type () != GCC_TYPE_NONE)
815 {
816 /* The type requested was actually defined inside another type,
817 such as a nested class definition. Return that type. */
818 return scope.nested_type ();
819 }
820
821 /* Push all scopes. */
822 instance->enter_scope (std::move (scope));
823
824 /* First we create the resulting type and enter it into our hash
825 table. This lets recursive types work. */
826
827 gcc_decl resuld;
828 if (type->code () == TYPE_CODE_STRUCT)
829 {
830 const char *what = type->is_declared_class () ? "class" : "struct";
831
832 resuld = instance->plugin ().build_decl
833 (what, name.get (), (GCC_CP_SYMBOL_CLASS | nested_access
834 | (type->is_declared_class ()
835 ? GCC_CP_FLAG_CLASS_NOFLAG
836 : GCC_CP_FLAG_CLASS_IS_STRUCT)),
837 0, nullptr, 0, filename, line);
838 }
839 else
840 {
841 gdb_assert (type->code () == TYPE_CODE_UNION);
842 resuld = instance->plugin ().build_decl
843 ("union", name.get (), GCC_CP_SYMBOL_UNION | nested_access,
844 0, nullptr, 0, filename, line);
845 }
846
847 gcc_type result;
848 if (type->code () == TYPE_CODE_STRUCT)
849 {
850 int num_baseclasses = TYPE_N_BASECLASSES (type);
851 std::vector<gcc_type> elements (num_baseclasses);
852 std::vector<enum gcc_cp_symbol_kind> flags (num_baseclasses);
853
854 struct gcc_vbase_array bases {};
855 bases.elements = elements.data ();
856 bases.flags = flags.data ();
857 bases.n_elements = num_baseclasses;
858
859 for (int i = 0; i < num_baseclasses; ++i)
860 {
861 struct type *base_type = TYPE_BASECLASS (type, i);
862
863 bases.flags[i] = (GCC_CP_SYMBOL_BASECLASS
864 | get_field_access_flag (type, i)
865 | (BASETYPE_VIA_VIRTUAL (type, i)
866 ? GCC_CP_FLAG_BASECLASS_VIRTUAL
867 : GCC_CP_FLAG_BASECLASS_NOFLAG));
868 bases.elements[i] = instance->convert_type (base_type);
869 }
870
871 result = instance->plugin ().start_class_type
872 (name.get (), resuld, &bases, filename, line);
873 }
874 else
875 {
876 gdb_assert (type->code () == TYPE_CODE_UNION);
877 result = instance->plugin ().start_class_type
878 (name.get (), resuld, nullptr, filename, line);
879 }
880
881 instance->insert_type (type, result);
882
883 /* Add definitions. */
884 compile_cplus_convert_type_defns (instance, type);
885
886 /* Add methods. */
887 compile_cplus_convert_struct_or_union_methods (instance, type, result);
888
889 /* Add members. */
890 compile_cplus_convert_struct_or_union_members (instance, type, result);
891
892 /* All finished. */
893 instance->plugin ().finish_class_type (name.get (), type->length ());
894
895 /* Pop all scopes. */
896 instance->leave_scope ();
897 return result;
898 }
899
900 /* Convert an enum type to its gcc representation. If this type
901 was defined in another type, NESTED_ACCESS should indicate the
902 accessibility of this type.*/
903
904 static gcc_type
905 compile_cplus_convert_enum (compile_cplus_instance *instance, struct type *type,
906 enum gcc_cp_symbol_kind nested_access)
907 {
908 bool scoped_enum_p = false;
909
910 /* Create a new scope for this type. */
911 compile_scope scope = instance->new_scope (type->name (), type);
912
913 if (scope.nested_type () != GCC_TYPE_NONE)
914 {
915 /* The type requested was actually defined inside another type,
916 such as a nested class definition. Return that type. */
917 return scope.nested_type ();
918 }
919
920 gdb::unique_xmalloc_ptr<char> name
921 = compile_cplus_instance::decl_name (type->name ());
922
923 /* Push all scopes. */
924 instance->enter_scope (std::move (scope));
925
926 gcc_type int_type
927 = instance->plugin ().get_int_type (type->is_unsigned (),
928 type->length (), nullptr);
929 gcc_type result
930 = instance->plugin ().start_enum_type (name.get (), int_type,
931 GCC_CP_SYMBOL_ENUM | nested_access
932 | (scoped_enum_p
933 ? GCC_CP_FLAG_ENUM_SCOPED
934 : GCC_CP_FLAG_ENUM_NOFLAG),
935 nullptr, 0);
936 for (int i = 0; i < type->num_fields (); ++i)
937 {
938 gdb::unique_xmalloc_ptr<char> fname
939 = compile_cplus_instance::decl_name (type->field (i).name ());
940
941 if (type->field (i).loc_kind () != FIELD_LOC_KIND_ENUMVAL
942 || fname == nullptr)
943 continue;
944
945 instance->plugin ().build_enum_constant (result, fname.get (),
946 type->field (i).loc_enumval ());
947 }
948
949 /* Finish enum definition and pop scopes. */
950 instance->plugin ().finish_enum_type (result);
951 instance->leave_scope ();
952 return result;
953 }
954
955 /* Convert a function type to its gcc representation. This function does
956 not deal with function templates. */
957
958 static gcc_type
959 compile_cplus_convert_func (compile_cplus_instance *instance,
960 struct type *type, bool strip_artificial)
961 {
962 int is_varargs = type->has_varargs ();
963 struct type *target_type = type->target_type ();
964
965 /* Functions with no debug info have no return type. Ideally we'd
966 want to fallback to the type of the cast just before the
967 function, like GDB's built-in expression parser, but we don't
968 have access to that type here. For now, fallback to int, like
969 GDB's parser used to do. */
970 if (target_type == nullptr)
971 {
972 target_type = builtin_type (type->arch ())->builtin_int;
973 warning (_("function has unknown return type; assuming int"));
974 }
975
976 /* This approach means we can't make self-referential function
977 types. Those are impossible in C, though. */
978 gcc_type return_type = instance->convert_type (target_type);
979
980 std::vector<gcc_type> elements (type->num_fields ());
981 struct gcc_type_array array = { (int) type->num_fields (), elements.data () };
982 int artificials = 0;
983 for (int i = 0; i < type->num_fields (); ++i)
984 {
985 if (strip_artificial && TYPE_FIELD_ARTIFICIAL (type, i))
986 {
987 --array.n_elements;
988 ++artificials;
989 }
990 else
991 {
992 array.elements[i - artificials]
993 = instance->convert_type (type->field (i).type ());
994 }
995 }
996
997 /* We omit setting the argument types to `void' to be a little flexible
998 with some minsyms like printf (compile-cplus.exp has examples). */
999 gcc_type result = instance->plugin ().build_function_type
1000 (return_type, &array, is_varargs);
1001 return result;
1002 }
1003
1004 /* Convert an integer type to its gcc representation. */
1005
1006 static gcc_type
1007 compile_cplus_convert_int (compile_cplus_instance *instance, struct type *type)
1008 {
1009 if (type->has_no_signedness ())
1010 {
1011 gdb_assert (type->length () == 1);
1012 return instance->plugin ().get_char_type ();
1013 }
1014
1015 return instance->plugin ().get_int_type
1016 (type->is_unsigned (), type->length (), type->name ());
1017 }
1018
1019 /* Convert a floating-point type to its gcc representation. */
1020
1021 static gcc_type
1022 compile_cplus_convert_float (compile_cplus_instance *instance,
1023 struct type *type)
1024 {
1025 return instance->plugin ().get_float_type
1026 (type->length (), type->name ());
1027 }
1028
1029 /* Convert the 'void' type to its gcc representation. */
1030
1031 static gcc_type
1032 compile_cplus_convert_void (compile_cplus_instance *instance, struct type *type)
1033 {
1034 return instance->plugin ().get_void_type ();
1035 }
1036
1037 /* Convert a boolean type to its gcc representation. */
1038
1039 static gcc_type
1040 compile_cplus_convert_bool (compile_cplus_instance *instance, struct type *type)
1041 {
1042 return instance->plugin ().get_bool_type ();
1043 }
1044
1045 /* See description in compile-cplus.h. */
1046
1047 gcc_type
1048 compile_cplus_instance::convert_qualified_base (gcc_type base,
1049 gcc_cp_qualifiers_flags quals)
1050 {
1051 gcc_type result = base;
1052
1053 if (quals != 0)
1054 result = plugin ().build_qualified_type (base, quals.raw ());
1055
1056 return result;
1057 }
1058
1059 /* See description in compile-cplus.h. */
1060
1061 static gcc_type
1062 compile_cplus_convert_qualified (compile_cplus_instance *instance,
1063 struct type *type)
1064 {
1065 struct type *unqual = make_unqualified_type (type);
1066 gcc_cp_qualifiers_flags quals = (enum gcc_cp_qualifiers) 0;
1067 gcc_type unqual_converted = instance->convert_type (unqual);
1068
1069 if (TYPE_CONST (type))
1070 quals |= GCC_CP_QUALIFIER_CONST;
1071 if (TYPE_VOLATILE (type))
1072 quals |= GCC_CP_QUALIFIER_VOLATILE;
1073 if (TYPE_RESTRICT (type))
1074 quals |= GCC_CP_QUALIFIER_RESTRICT;
1075
1076 return instance->convert_qualified_base (unqual_converted, quals);
1077 }
1078
1079 /* Convert a complex type to its gcc representation. */
1080
1081 static gcc_type
1082 compile_cplus_convert_complex (compile_cplus_instance *instance,
1083 struct type *type)
1084 {
1085 gcc_type base = instance->convert_type (type->target_type ());
1086
1087 return instance->plugin ().build_complex_type (base);
1088 }
1089
1090 /* Convert a namespace of TYPE. */
1091
1092 static gcc_type
1093 compile_cplus_convert_namespace (compile_cplus_instance *instance,
1094 struct type *type)
1095 {
1096 compile_scope scope = instance->new_scope (type->name (), type);
1097 gdb::unique_xmalloc_ptr<char> name
1098 = compile_cplus_instance::decl_name (type->name ());
1099
1100 /* Push scope. */
1101 instance->enter_scope (std::move (scope));
1102
1103 /* Convert this namespace. */
1104 instance->plugin ().push_namespace (name.get ());
1105 instance->plugin ().pop_binding_level (name.get ());
1106
1107 /* Pop scope. */
1108 instance->leave_scope ();
1109
1110 /* Namespaces are non-cacheable types. */
1111 return GCC_TYPE_NONE;
1112 }
1113
1114 /* A helper function which knows how to convert most types from their
1115 gdb representation to the corresponding gcc form. This examines
1116 the TYPE and dispatches to the appropriate conversion function. It
1117 returns the gcc type.
1118
1119 If the type was defined in another type, NESTED_ACCESS should indicate the
1120 accessibility of this type. */
1121
1122 static gcc_type
1123 convert_type_cplus_basic (compile_cplus_instance *instance,
1124 struct type *type,
1125 enum gcc_cp_symbol_kind nested_access)
1126 {
1127 /* If we are converting a qualified type, first convert the
1128 unqualified type and then apply the qualifiers. */
1129 if ((type->instance_flags () & (TYPE_INSTANCE_FLAG_CONST
1130 | TYPE_INSTANCE_FLAG_VOLATILE
1131 | TYPE_INSTANCE_FLAG_RESTRICT)) != 0)
1132 return compile_cplus_convert_qualified (instance, type);
1133
1134 switch (type->code ())
1135 {
1136 case TYPE_CODE_REF:
1137 case TYPE_CODE_RVALUE_REF:
1138 return compile_cplus_convert_reference (instance, type);
1139
1140 case TYPE_CODE_PTR:
1141 return compile_cplus_convert_pointer (instance, type);
1142
1143 case TYPE_CODE_ARRAY:
1144 return compile_cplus_convert_array (instance, type);
1145
1146 case TYPE_CODE_STRUCT:
1147 case TYPE_CODE_UNION:
1148 return
1149 compile_cplus_convert_struct_or_union (instance, type, nested_access);
1150
1151 case TYPE_CODE_ENUM:
1152 return compile_cplus_convert_enum (instance, type, nested_access);
1153
1154 case TYPE_CODE_FUNC:
1155 return compile_cplus_convert_func (instance, type, false);
1156
1157 case TYPE_CODE_METHOD:
1158 return
1159 compile_cplus_convert_method (instance, TYPE_SELF_TYPE (type), type);
1160
1161 case TYPE_CODE_MEMBERPTR:
1162 case TYPE_CODE_METHODPTR:
1163 return compile_cplus_convert_memberptr (instance, type);
1164 break;
1165
1166 case TYPE_CODE_INT:
1167 return compile_cplus_convert_int (instance, type);
1168
1169 case TYPE_CODE_FLT:
1170 return compile_cplus_convert_float (instance, type);
1171
1172 case TYPE_CODE_VOID:
1173 return compile_cplus_convert_void (instance, type);
1174
1175 case TYPE_CODE_BOOL:
1176 return compile_cplus_convert_bool (instance, type);
1177
1178 case TYPE_CODE_COMPLEX:
1179 return compile_cplus_convert_complex (instance, type);
1180
1181 case TYPE_CODE_NAMESPACE:
1182 return compile_cplus_convert_namespace (instance, type);
1183
1184 case TYPE_CODE_TYPEDEF:
1185 return compile_cplus_convert_typedef (instance, type, nested_access);
1186
1187 default:
1188 break;
1189 }
1190
1191 std::string s = string_printf (_("unhandled TYPE_CODE %d"),
1192 type->code ());
1193
1194 return instance->plugin ().error (s.c_str ());
1195 }
1196
1197 gcc_type
1198 compile_cplus_instance::convert_type (struct type *type,
1199 enum gcc_cp_symbol_kind nested_access)
1200 {
1201 /* Check if TYPE has already been converted. */
1202 gcc_type result;
1203 if (get_cached_type (type, &result))
1204 return result;
1205
1206 /* It is the first time this type has been seen -- convert it
1207 and cache it, if appropriate.. */
1208 result = convert_type_cplus_basic (this, type, nested_access);
1209 if (result != GCC_TYPE_NONE)
1210 insert_type (type, result);
1211 return result;
1212 }
1213
1214 void
1215 compile_cplus_instance::gcc_cplus_enter_scope
1216 (void *datum, struct gcc_cp_context *gcc_context)
1217 {
1218 }
1219
1220 void
1221 compile_cplus_instance::gcc_cplus_leave_scope
1222 (void *datum, struct gcc_cp_context *gcc_context)
1223 {
1224 }
1225
1226 \f
1227
1228 /* Plug-in forwards. */
1229
1230 /* C++ plug-in wrapper. */
1231
1232 /* A result printer for plug-in calls that return a gcc_type or
1233 gcc_decl. */
1234
1235 static void
1236 compile_cplus_debug_output_1 (ULONGEST arg)
1237 {
1238 gdb_printf (gdb_stdlog, "%s", pulongest (arg));
1239 }
1240
1241 static void
1242 compile_cplus_debug_output_1 (const char *arg)
1243 {
1244 if (arg == nullptr)
1245 gdb_puts ("NULL", gdb_stdlog);
1246 else
1247 gdb_puts (arg, gdb_stdlog);
1248 }
1249
1250 static void
1251 compile_cplus_debug_output ()
1252 {
1253 }
1254
1255 template <typename T>
1256 static void
1257 compile_cplus_debug_output_1 (const T *arg)
1258 {
1259 }
1260
1261 template <typename T, typename... Targs>
1262 static void
1263 compile_cplus_debug_output (T arg, Targs... Args)
1264 {
1265 compile_cplus_debug_output_1 (arg);
1266 gdb_putc (' ', gdb_stdlog);
1267 compile_cplus_debug_output (Args...);
1268 }
1269
1270 #define FORWARD(OP,...) m_context->cp_ops->OP(m_context, ##__VA_ARGS__)
1271 #define OUTPUT_DEBUG_RESULT(R) \
1272 if (debug_compile_cplus_types) \
1273 { \
1274 gdb_puts (": ", gdb_stdlog); \
1275 compile_cplus_debug_output (R); \
1276 gdb_putc ('\n', gdb_stdlog); \
1277 } \
1278
1279 #define GCC_METHOD0(R, N) \
1280 R gcc_cp_plugin::N () const \
1281 { \
1282 if (debug_compile_cplus_types) \
1283 compile_cplus_debug_output (STRINGIFY (N)); \
1284 auto result = FORWARD (N); \
1285 OUTPUT_DEBUG_RESULT (result); \
1286 return result; \
1287 }
1288 #define GCC_METHOD1(R, N, A) \
1289 R gcc_cp_plugin::N (A a) const \
1290 { \
1291 if (debug_compile_cplus_types) \
1292 compile_cplus_debug_output (STRINGIFY (N), a); \
1293 auto result = FORWARD (N, a); \
1294 OUTPUT_DEBUG_RESULT (result); \
1295 return result; \
1296 }
1297 #define GCC_METHOD2(R, N, A, B) \
1298 R gcc_cp_plugin::N (A a, B b) const \
1299 { \
1300 if (debug_compile_cplus_types) \
1301 compile_cplus_debug_output (STRINGIFY (N), a, b); \
1302 auto result = FORWARD (N, a, b); \
1303 OUTPUT_DEBUG_RESULT (result); \
1304 return result; \
1305 }
1306 #define GCC_METHOD3(R, N, A, B, C) \
1307 R gcc_cp_plugin::N (A a, B b, C c) const \
1308 { \
1309 if (debug_compile_cplus_types) \
1310 compile_cplus_debug_output (STRINGIFY (N), a, b, c); \
1311 auto result = FORWARD (N, a, b, c); \
1312 OUTPUT_DEBUG_RESULT (result); \
1313 return result; \
1314 }
1315 #define GCC_METHOD4(R, N, A, B, C, D) \
1316 R gcc_cp_plugin::N (A a, B b, C c, D d) const \
1317 { \
1318 if (debug_compile_cplus_types) \
1319 compile_cplus_debug_output (STRINGIFY (N), a, b, c, d); \
1320 auto result = FORWARD (N, a, b, c, d); \
1321 OUTPUT_DEBUG_RESULT (result); \
1322 return result; \
1323 }
1324 #define GCC_METHOD5(R, N, A, B, C, D, E) \
1325 R gcc_cp_plugin::N (A a, B b, C c, D d, E e) const \
1326 { \
1327 if (debug_compile_cplus_types) \
1328 compile_cplus_debug_output (STRINGIFY (N), a, b, c, d, e); \
1329 auto result = FORWARD (N, a, b, c, d, e); \
1330 OUTPUT_DEBUG_RESULT (result); \
1331 return result; \
1332 }
1333 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
1334 R gcc_cp_plugin::N (A a, B b, C c, D d, E e, F f, G g) const \
1335 { \
1336 if (debug_compile_cplus_types) \
1337 compile_cplus_debug_output (STRINGIFY (N), a, b, c, d, e, f, g); \
1338 auto result = FORWARD (N, a, b, c, d, e, f, g); \
1339 OUTPUT_DEBUG_RESULT (result); \
1340 return result; \
1341 }
1342
1343 #include "gcc-cp-fe.def"
1344
1345 #undef GCC_METHOD0
1346 #undef GCC_METHOD1
1347 #undef GCC_METHOD2
1348 #undef GCC_METHOD3
1349 #undef GCC_METHOD4
1350 #undef GCC_METHOD5
1351 #undef GCC_METHOD7
1352 #undef FORWARD
1353 #undef OUTPUT_DEBUG_RESULT
1354
1355 gcc_expr
1356 gcc_cp_plugin::build_decl (const char *debug_decltype, const char *name,
1357 enum gcc_cp_symbol_kind sym_kind, gcc_type sym_type,
1358 const char *substitution_name, gcc_address address,
1359 const char *filename, unsigned int line_number)
1360 {
1361 if (debug_compile_cplus_types)
1362 gdb_printf (gdb_stdlog, "<%s> ", debug_decltype);
1363
1364 return build_decl (name, sym_kind, sym_type, substitution_name,
1365 address, filename, line_number);
1366 }
1367
1368 gcc_type
1369 gcc_cp_plugin::start_class_type (const char *debug_name, gcc_decl typedecl,
1370 const struct gcc_vbase_array *base_classes,
1371 const char *filename, unsigned int line_number)
1372 {
1373 if (debug_compile_cplus_types)
1374 gdb_printf (gdb_stdlog, "<%s> ", debug_name);
1375
1376 return start_class_type (typedecl, base_classes, filename, line_number);
1377 }
1378
1379 int
1380 gcc_cp_plugin::finish_class_type (const char *debug_name,
1381 unsigned long size_in_bytes)
1382 {
1383 if (debug_compile_cplus_types)
1384 gdb_printf (gdb_stdlog, "<%s> ", debug_name);
1385
1386 return finish_class_type (size_in_bytes);
1387 }
1388
1389 int
1390 gcc_cp_plugin::pop_binding_level (const char *debug_name)
1391 {
1392 if (debug_compile_cplus_types)
1393 gdb_printf (gdb_stdlog, "<%s> ", debug_name);
1394
1395 return pop_binding_level ();
1396 }
1397
1398 void _initialize_compile_cplus_types ();
1399 void
1400 _initialize_compile_cplus_types ()
1401 {
1402 add_setshow_boolean_cmd ("compile-cplus-types", no_class,
1403 &debug_compile_cplus_types, _("\
1404 Set debugging of C++ compile type conversion."), _("\
1405 Show debugging of C++ compile type conversion."), _("\
1406 When enabled debugging messages are printed during C++ type conversion for\n\
1407 the compile commands."),
1408 nullptr,
1409 nullptr,
1410 &setdebuglist,
1411 &showdebuglist);
1412
1413 add_setshow_boolean_cmd ("compile-cplus-scopes", no_class,
1414 &debug_compile_cplus_scopes, _("\
1415 Set debugging of C++ compile scopes."), _("\
1416 Show debugging of C++ compile scopes."), _("\
1417 When enabled debugging messages are printed about definition scopes during\n\
1418 C++ type conversion for the compile commands."),
1419 nullptr,
1420 nullptr,
1421 &setdebuglist,
1422 &showdebuglist);
1423 }