1 /* Convert types from GDB to GCC
3 Copyright (C) 2014-2021 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
22 #include "gdbsupport/preprocessor.h"
24 #include "compile-internal.h"
25 #include "compile-cplus.h"
26 #include "gdbsupport/gdb_assert.h"
29 #include "cp-support.h"
35 #include "compile-c.h"
38 /* Default compile flags for C++. */
40 const char *compile_cplus_instance::m_default_cflags
= "-std=gnu++11";
42 /* Flag to enable internal debugging. */
44 static bool debug_compile_cplus_types
= false;
46 /* Flag to enable internal scope switching debugging. */
48 static bool debug_compile_cplus_scopes
= false;
50 /* Forward declarations. */
52 static gcc_type
compile_cplus_convert_func (compile_cplus_instance
*instance
,
54 bool strip_artificial
);
56 /* See description in compile-cplus.h. */
58 gdb::unique_xmalloc_ptr
<char>
59 compile_cplus_instance::decl_name (const char *natural
)
61 if (natural
== nullptr)
64 gdb::unique_xmalloc_ptr
<char> name
= cp_func_name (natural
);
68 return make_unique_xstrdup (natural
);
71 /* Get the access flag for the NUM'th field of TYPE. */
73 static enum gcc_cp_symbol_kind
74 get_field_access_flag (const struct type
*type
, int num
)
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
;
81 /* GDB assumes everything else is public. */
82 return GCC_CP_ACCESS_PUBLIC
;
85 /* Get the access flag for the NUM'th method of TYPE's FNI'th
88 enum gcc_cp_symbol_kind
89 get_method_access_flag (const struct type
*type
, int fni
, int num
)
91 gdb_assert (type
->code () == TYPE_CODE_STRUCT
);
93 /* If this type was not declared a class, everything is public. */
94 if (!TYPE_DECLARED_CLASS (type
))
95 return GCC_CP_ACCESS_PUBLIC
;
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
;
104 return GCC_CP_ACCESS_PUBLIC
;
107 /* A useful debugging function to output the scope SCOPE to stdout. */
109 static void __attribute__ ((used
))
110 debug_print_scope (const compile_scope
&scope
)
112 for (const auto &comp
: scope
)
114 const char *symbol
= (comp
.bsymbol
.symbol
!= nullptr
115 ? comp
.bsymbol
.symbol
->natural_name ()
118 printf_unfiltered ("\tname = %s, symbol = %s\n", comp
.name
.c_str (),
123 /* See description in compile-cplus.h. */
126 type_name_to_scope (const char *type_name
, const struct block
*block
)
130 if (type_name
== nullptr)
132 /* An anonymous type. We cannot really do much here. We simply cannot
133 look up anonymous types easily/at all. */
137 const char *p
= type_name
;
138 std::string lookup_name
;
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
);
146 /* Advance past the last token. */
149 /* Look up the symbol and decide when to stop. */
150 if (!lookup_name
.empty ())
154 /* Look up the resulting name. */
155 struct block_symbol bsymbol
156 = lookup_symbol (lookup_name
.c_str (), block
, VAR_DOMAIN
, nullptr);
158 if (bsymbol
.symbol
!= nullptr)
160 scope_component comp
= {s
, bsymbol
};
162 scope
.push_back (comp
);
164 if (SYMBOL_TYPE (bsymbol
.symbol
)->code () != TYPE_CODE_NAMESPACE
)
178 /* This shouldn't happen since we are not attempting to
179 loop over user input. This name is generated by GDB
181 internal_error (__FILE__
, __LINE__
,
182 _("malformed TYPE_NAME during parsing"));
190 /* Compare two scope_components for equality. These are equal if the names
191 of the two components' are the same. */
194 operator== (const scope_component
&lhs
, const scope_component
&rhs
)
196 return lhs
.name
== rhs
.name
;
199 /* Compare two scope_components for inequality. These are not equal if
200 the two components' names are not equal. */
203 operator!= (const scope_component
&lhs
, const scope_component
&rhs
)
205 return lhs
.name
!= rhs
.name
;
208 /* Compare two compile_scopes for equality. These are equal if they are both
209 contain the same number of components and each component is equal. */
212 operator== (const compile_scope
&lhs
, const compile_scope
&rhs
)
214 if (lhs
.size () != rhs
.size ())
217 for (int i
= 0; i
< lhs
.size (); ++i
)
219 if (lhs
[i
] != rhs
[i
])
226 /* Compare two compile_scopes for inequality. These are inequal if they
227 contain unequal number of elements or if any of the components are not
231 operator!= (const compile_scope
&lhs
, const compile_scope
&rhs
)
233 if (lhs
.size () != rhs
.size ())
236 for (int i
= 0; i
< lhs
.size (); ++i
)
238 if (lhs
[i
] != rhs
[i
])
245 /* See description in compile-cplus.h. */
248 compile_cplus_instance::enter_scope (compile_scope
&&new_scope
)
250 bool must_push
= m_scopes
.empty () || m_scopes
.back () != new_scope
;
252 new_scope
.m_pushed
= must_push
;
254 /* Save the new scope. */
255 m_scopes
.push_back (std::move (new_scope
));
259 if (debug_compile_cplus_scopes
)
261 fprintf_unfiltered (gdb_stdlog
, "entering new scope %s\n",
262 host_address_to_string (&m_scopes
.back ()));
265 /* Push the global namespace. */
266 plugin ().push_namespace ("");
268 /* Push all other namespaces. Note that we do not push the last
269 scope_component -- that's the actual type we are converting. */
271 (m_scopes
.back ().begin (), m_scopes
.back ().end () - 1,
272 [this] (const scope_component
&comp
)
274 gdb_assert (SYMBOL_TYPE (comp
.bsymbol
.symbol
)->code ()
275 == TYPE_CODE_NAMESPACE
);
277 const char *ns
= (comp
.name
== CP_ANONYMOUS_NAMESPACE_STR
? nullptr
278 : comp
.name
.c_str ());
280 this->plugin ().push_namespace (ns
);
285 if (debug_compile_cplus_scopes
)
287 fprintf_unfiltered (gdb_stdlog
, "staying in current scope -- "
288 "scopes are identical\n");
293 /* See description in compile-cplus.h. */
296 compile_cplus_instance::leave_scope ()
298 /* Get the current scope and remove it from the internal list of
300 compile_scope current
= m_scopes
.back ();
302 m_scopes
.pop_back ();
304 if (current
.m_pushed
)
306 if (debug_compile_cplus_scopes
)
308 fprintf_unfiltered (gdb_stdlog
, "leaving scope %s\n",
309 host_address_to_string (¤t
));
312 /* Pop namespaces. */
314 (current
.begin (),current
.end () - 1,
315 [this] (const scope_component
&comp
) {
316 gdb_assert (SYMBOL_TYPE (comp
.bsymbol
.symbol
)->code ()
317 == TYPE_CODE_NAMESPACE
);
318 this->plugin ().pop_binding_level (comp
.name
.c_str ());
321 /* Pop global namespace. */
322 plugin ().pop_binding_level ("");
326 if (debug_compile_cplus_scopes
)
327 fprintf_unfiltered (gdb_stdlog
,
328 "identical scopes -- not leaving scope\n");
332 /* See description in compile-cplus.h. */
335 compile_cplus_instance::new_scope (const char *type_name
, struct type
*type
)
337 /* Break the type name into components. If TYPE was defined in some
338 superclass, we do not process TYPE but process the enclosing type
340 compile_scope scope
= type_name_to_scope (type_name
, block ());
344 /* Get the name of the last component, which should be the
345 unqualified name of the type to process. */
346 scope_component
&comp
= scope
.back ();
348 if (!types_equal (type
, SYMBOL_TYPE (comp
.bsymbol
.symbol
))
349 && (m_scopes
.empty ()
350 || (m_scopes
.back ().back ().bsymbol
.symbol
351 != comp
.bsymbol
.symbol
)))
353 /* The type is defined inside another class(es). Convert that
354 type instead of defining this type. */
355 convert_type (SYMBOL_TYPE (comp
.bsymbol
.symbol
));
357 /* If the original type (passed in to us) is defined in a nested
358 class, the previous call will give us that type's gcc_type.
359 Upper layers are expecting to get the original type's
361 get_cached_type (type
, &scope
.m_nested_type
);
367 if (type
->name () == nullptr)
371 /* We don't have a qualified name for this to look up, but
372 we need a scope. We have to assume, then, that it is the same
373 as the current scope, if any. */
374 if (!m_scopes
.empty ())
376 scope
= m_scopes
.back ();
377 scope
.m_pushed
= false;
380 scope
.push_back (scope_component ());
386 decl_name (type
->name ()).get (),
387 lookup_symbol (type
->name (), block (), VAR_DOMAIN
, nullptr)
389 scope
.push_back (comp
);
393 /* There must be at least one component in the compile_scope. */
394 gdb_assert (scope
.size () > 0);
398 /* See description in compile-cplus.h. */
401 compile_cplus_instance::convert_reference_base
402 (gcc_type base
, enum gcc_cp_ref_qualifiers rquals
)
404 return this->plugin ().build_reference_type (base
, rquals
);
407 /* Convert a reference type to its gcc representation. */
410 compile_cplus_convert_reference (compile_cplus_instance
*instance
,
413 gcc_type target
= instance
->convert_type (TYPE_TARGET_TYPE (type
));
415 enum gcc_cp_ref_qualifiers quals
= GCC_CP_REF_QUAL_NONE
;
416 switch (type
->code ())
419 quals
= GCC_CP_REF_QUAL_LVALUE
;
421 case TYPE_CODE_RVALUE_REF
:
422 quals
= GCC_CP_REF_QUAL_RVALUE
;
425 gdb_assert_not_reached ("unexpected type code for reference type");
428 return instance
->convert_reference_base (target
, quals
);
431 /* See description in compile-cplus.h. */
434 compile_cplus_instance::convert_pointer_base(gcc_type target
)
436 return plugin ().build_pointer_type (target
);
439 /* Convert a pointer type to its gcc representation. */
442 compile_cplus_convert_pointer (compile_cplus_instance
*instance
,
445 gcc_type target
= instance
->convert_type (TYPE_TARGET_TYPE (type
));
447 return instance
->convert_pointer_base (target
);
450 /* Convert an array type to its gcc representation. */
453 compile_cplus_convert_array (compile_cplus_instance
*instance
,
456 struct type
*range
= type
->index_type ();
457 gcc_type element_type
= instance
->convert_type (TYPE_TARGET_TYPE (type
));
459 if (range
->bounds ()->low
.kind () != PROP_CONST
)
461 const char *s
= _("array type with non-constant"
462 " lower bound is not supported");
464 return instance
->plugin ().error (s
);
467 if (range
->bounds ()->low
.const_val () != 0)
469 const char *s
= _("cannot convert array type with "
470 "non-zero lower bound to C");
472 return instance
->plugin ().error (s
);
475 if (range
->bounds ()->high
.kind () == PROP_LOCEXPR
476 || range
->bounds ()->high
.kind () == PROP_LOCLIST
)
478 if (type
->is_vector ())
480 const char *s
= _("variably-sized vector type is not supported");
482 return instance
->plugin ().error (s
);
485 std::string upper_bound
486 = c_get_range_decl_name (&range
->bounds ()->high
);
487 return instance
->plugin ().build_vla_array_type (element_type
,
488 upper_bound
.c_str ());
492 LONGEST low_bound
, high_bound
, count
;
494 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
498 gdb_assert (low_bound
== 0); /* Ensured above. */
499 count
= high_bound
+ 1;
502 if (type
->is_vector ())
503 return instance
->plugin ().build_vector_type (element_type
, count
);
505 return instance
->plugin ().build_array_type (element_type
, count
);
509 /* Convert a typedef of TYPE. If not GCC_CP_ACCESS_NONE, NESTED_ACCESS
510 will define the accessibility of the typedef definition in its
514 compile_cplus_convert_typedef (compile_cplus_instance
*instance
,
516 enum gcc_cp_symbol_kind nested_access
)
518 compile_scope scope
= instance
->new_scope (type
->name (), type
);
520 if (scope
.nested_type () != GCC_TYPE_NONE
)
521 return scope
.nested_type ();
523 gdb::unique_xmalloc_ptr
<char> name
524 = compile_cplus_instance::decl_name (type
->name ());
526 /* Make sure the scope for this type has been pushed. */
527 instance
->enter_scope (std::move (scope
));
529 /* Convert the typedef's real type. */
530 gcc_type typedef_type
= instance
->convert_type (check_typedef (type
));
532 instance
->plugin ().build_decl ("typedef", name
.get (),
533 GCC_CP_SYMBOL_TYPEDEF
| nested_access
,
534 typedef_type
, 0, 0, nullptr, 0);
536 /* Completed this scope. */
537 instance
->leave_scope ();
541 /* Convert types defined in TYPE. */
544 compile_cplus_convert_type_defns (compile_cplus_instance
*instance
,
548 enum gcc_cp_symbol_kind accessibility
;
550 /* Convert typedefs. */
551 for (i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (type
); ++i
)
553 if (TYPE_TYPEDEF_FIELD_PROTECTED (type
, i
))
554 accessibility
= GCC_CP_ACCESS_PROTECTED
;
555 else if (TYPE_TYPEDEF_FIELD_PRIVATE (type
, i
))
556 accessibility
= GCC_CP_ACCESS_PRIVATE
;
558 accessibility
= GCC_CP_ACCESS_PUBLIC
;
559 instance
->convert_type (TYPE_TYPEDEF_FIELD_TYPE (type
, i
), accessibility
);
562 /* Convert nested types. */
563 for (i
= 0; i
< TYPE_NESTED_TYPES_COUNT (type
); ++i
)
565 if (TYPE_NESTED_TYPES_FIELD_PROTECTED (type
, i
))
566 accessibility
= GCC_CP_ACCESS_PROTECTED
;
567 else if (TYPE_NESTED_TYPES_FIELD_PRIVATE (type
, i
))
568 accessibility
= GCC_CP_ACCESS_PRIVATE
;
570 accessibility
= GCC_CP_ACCESS_PUBLIC
;
571 instance
->convert_type (TYPE_NESTED_TYPES_FIELD_TYPE (type
, i
),
576 /* Convert data members defined in TYPE, which should be struct/class/union
577 with gcc_type COMP_TYPE. */
580 compile_cplus_convert_struct_or_union_members
581 (compile_cplus_instance
*instance
, struct type
*type
, gcc_type comp_type
)
583 for (int i
= TYPE_N_BASECLASSES (type
); i
< type
->num_fields (); ++i
)
585 const char *field_name
= TYPE_FIELD_NAME (type
, i
);
587 if (TYPE_FIELD_IGNORE (type
, i
)
588 || TYPE_FIELD_ARTIFICIAL (type
, i
))
591 /* GDB records unnamed/anonymous fields with empty string names. */
592 if (*field_name
== '\0')
593 field_name
= nullptr;
596 = instance
->convert_type (type
->field (i
).type ());
598 if (field_is_static (&type
->field (i
)))
602 switch (TYPE_FIELD_LOC_KIND (type
, i
))
604 case FIELD_LOC_KIND_PHYSADDR
:
606 physaddr
= TYPE_FIELD_STATIC_PHYSADDR (type
, i
);
608 instance
->plugin ().build_decl
609 ("field physaddr", field_name
,
610 (GCC_CP_SYMBOL_VARIABLE
| get_field_access_flag (type
, i
)),
611 field_type
, nullptr, physaddr
, nullptr, 0);
615 case FIELD_LOC_KIND_PHYSNAME
:
617 const char *physname
= TYPE_FIELD_STATIC_PHYSNAME (type
, i
);
618 struct block_symbol sym
619 = lookup_symbol (physname
, instance
->block (),
620 VAR_DOMAIN
, nullptr);
622 if (sym
.symbol
== nullptr)
624 /* We didn't actually find the symbol. There's little
625 we can do but ignore this member. */
628 const char *filename
= symbol_symtab (sym
.symbol
)->filename
;
629 unsigned int line
= SYMBOL_LINE (sym
.symbol
);
631 physaddr
= SYMBOL_VALUE_ADDRESS (sym
.symbol
);
632 instance
->plugin ().build_decl
633 ("field physname", field_name
,
634 (GCC_CP_SYMBOL_VARIABLE
| get_field_access_flag (type
, i
)),
635 field_type
, nullptr, physaddr
, filename
, line
);
640 gdb_assert_not_reached
641 ("unexpected static field location kind");
646 unsigned long bitsize
= TYPE_FIELD_BITSIZE (type
, i
);
647 enum gcc_cp_symbol_kind field_flags
= GCC_CP_SYMBOL_FIELD
648 | get_field_access_flag (type
, i
);
651 bitsize
= 8 * TYPE_LENGTH (type
->field (i
).type ());
653 instance
->plugin ().build_field
654 (field_name
, field_type
, field_flags
, bitsize
,
655 TYPE_FIELD_BITPOS (type
, i
));
660 /* Convert a method type to its gcc representation. */
663 compile_cplus_convert_method (compile_cplus_instance
*instance
,
664 struct type
*parent_type
,
665 struct type
*method_type
)
667 /* Get the actual function type of the method, the corresponding class's
668 type and corresponding qualifier flags. */
669 gcc_type func_type
= compile_cplus_convert_func (instance
, method_type
, true);
670 gcc_type class_type
= instance
->convert_type (parent_type
);
671 gcc_cp_qualifiers_flags quals
= 0;
673 if (TYPE_CONST (method_type
))
674 quals
|= GCC_CP_QUALIFIER_CONST
;
675 if (TYPE_VOLATILE (method_type
))
676 quals
|= GCC_CP_QUALIFIER_VOLATILE
;
677 if (TYPE_RESTRICT (method_type
))
678 quals
|= GCC_CP_QUALIFIER_RESTRICT
;
680 /* Not yet implemented. */
681 gcc_cp_ref_qualifiers_flags rquals
= GCC_CP_REF_QUAL_NONE
;
683 return instance
->plugin ().build_method_type
684 (class_type
, func_type
, quals
.raw (), rquals
.raw ());
687 /* Convert a member or method pointer represented by TYPE. */
690 compile_cplus_convert_memberptr (compile_cplus_instance
*instance
,
693 struct type
*containing_class
= TYPE_SELF_TYPE (type
);
695 if (containing_class
== nullptr)
696 return GCC_TYPE_NONE
;
698 gcc_type class_type
= instance
->convert_type (containing_class
);
700 = instance
->convert_type (TYPE_TARGET_TYPE (type
));
702 return instance
->plugin ().build_pointer_to_member_type
703 (class_type
, member_type
);
706 /* Convert all methods defined in TYPE, which should be a class/struct/union
707 with gcc_type CLASS_TYPE. */
710 compile_cplus_convert_struct_or_union_methods (compile_cplus_instance
*instance
,
714 for (int i
= 0; i
< TYPE_NFN_FIELDS (type
); ++i
)
716 struct fn_field
*methods
= TYPE_FN_FIELDLIST1 (type
, i
);
717 gdb::unique_xmalloc_ptr
<char> overloaded_name
718 = compile_cplus_instance::decl_name (TYPE_FN_FIELDLIST_NAME (type
, i
));
720 /* Loop through the fieldlist, adding decls to the compiler's
721 representation of the class. */
722 for (int j
= 0; j
< TYPE_FN_FIELDLIST_LENGTH (type
, i
); ++j
)
724 /* Skip artificial methods. */
725 if (TYPE_FN_FIELD_ARTIFICIAL (methods
, j
))
728 gcc_cp_symbol_kind_flags sym_kind
= GCC_CP_SYMBOL_FUNCTION
;
729 gcc_type method_type
;
730 struct block_symbol sym
731 = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (methods
, j
),
732 instance
->block (), VAR_DOMAIN
, nullptr);
734 if (sym
.symbol
== nullptr)
736 if (TYPE_FN_FIELD_VIRTUAL_P (methods
, j
))
738 /* This is beyond hacky, and is really only a workaround for
739 detecting pure virtual methods. */
740 method_type
= compile_cplus_convert_method
741 (instance
, type
, TYPE_FN_FIELD_TYPE (methods
, j
));
743 instance
->plugin ().build_decl
744 ("pure virtual method", overloaded_name
.get (),
746 | get_method_access_flag (type
, i
, j
)
747 | GCC_CP_FLAG_VIRTUAL_FUNCTION
748 | GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION
).raw (),
749 method_type
, nullptr, 0, nullptr, 0);
753 /* This can happen if we have a DW_AT_declaration DIE
754 for the method, but no "definition"-type DIE (with
755 DW_AT_specification referencing the decl DIE), i.e.,
756 the compiler has probably optimized the method away.
758 In this case, all we can hope to do is issue a warning
759 to the user letting him know. If the user has not actually
760 requested using this method, things should still work. */
761 warning (_("Method %s appears to be optimized out.\n"
762 "All references to this method will be undefined."),
763 TYPE_FN_FIELD_PHYSNAME (methods
, j
));
767 const char *filename
= symbol_symtab (sym
.symbol
)->filename
;
768 unsigned int line
= SYMBOL_LINE (sym
.symbol
);
769 CORE_ADDR address
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
.symbol
));
772 if (TYPE_FN_FIELD_STATIC_P (methods
, j
))
774 kind
= "static method";
775 method_type
= compile_cplus_convert_func
776 (instance
, TYPE_FN_FIELD_TYPE (methods
, j
), true);
781 method_type
= (compile_cplus_convert_method
782 (instance
, type
, TYPE_FN_FIELD_TYPE (methods
, j
)));
785 if (TYPE_FN_FIELD_VIRTUAL_P (methods
, j
))
786 sym_kind
|= GCC_CP_FLAG_VIRTUAL_FUNCTION
;
788 instance
->plugin ().build_decl
789 (kind
, overloaded_name
.get (),
790 (sym_kind
| get_method_access_flag (type
, i
, j
)).raw (),
791 method_type
, nullptr, address
, filename
, line
);
796 /* Convert a struct or union type to its gcc representation. If this type
797 was defined in another type, NESTED_ACCESS should indicate the
798 accessibility of this type. */
801 compile_cplus_convert_struct_or_union (compile_cplus_instance
*instance
,
803 enum gcc_cp_symbol_kind nested_access
)
805 const char *filename
= nullptr;
806 unsigned short line
= 0;
808 /* Get the decl name of this type. */
809 gdb::unique_xmalloc_ptr
<char> name
810 = compile_cplus_instance::decl_name (type
->name ());
812 /* Create a new scope for TYPE. */
813 compile_scope scope
= instance
->new_scope (type
->name (), type
);
815 if (scope
.nested_type () != GCC_TYPE_NONE
)
817 /* The type requested was actually defined inside another type,
818 such as a nested class definition. Return that type. */
819 return scope
.nested_type ();
822 /* Push all scopes. */
823 instance
->enter_scope (std::move (scope
));
825 /* First we create the resulting type and enter it into our hash
826 table. This lets recursive types work. */
829 if (type
->code () == TYPE_CODE_STRUCT
)
831 const char *what
= TYPE_DECLARED_CLASS (type
) ? "struct" : "class";
833 resuld
= instance
->plugin ().build_decl
834 (what
, name
.get (), (GCC_CP_SYMBOL_CLASS
| nested_access
835 | (TYPE_DECLARED_CLASS (type
)
836 ? GCC_CP_FLAG_CLASS_NOFLAG
837 : GCC_CP_FLAG_CLASS_IS_STRUCT
)),
838 0, nullptr, 0, filename
, line
);
842 gdb_assert (type
->code () == TYPE_CODE_UNION
);
843 resuld
= instance
->plugin ().build_decl
844 ("union", name
.get (), GCC_CP_SYMBOL_UNION
| nested_access
,
845 0, nullptr, 0, filename
, line
);
849 if (type
->code () == TYPE_CODE_STRUCT
)
851 int num_baseclasses
= TYPE_N_BASECLASSES (type
);
852 std::vector
<gcc_type
> elements (num_baseclasses
);
853 std::vector
<enum gcc_cp_symbol_kind
> flags (num_baseclasses
);
855 struct gcc_vbase_array bases
{};
856 bases
.elements
= elements
.data ();
857 bases
.flags
= flags
.data ();
858 bases
.n_elements
= num_baseclasses
;
860 for (int i
= 0; i
< num_baseclasses
; ++i
)
862 struct type
*base_type
= TYPE_BASECLASS (type
, i
);
864 bases
.flags
[i
] = (GCC_CP_SYMBOL_BASECLASS
865 | get_field_access_flag (type
, i
)
866 | (BASETYPE_VIA_VIRTUAL (type
, i
)
867 ? GCC_CP_FLAG_BASECLASS_VIRTUAL
868 : GCC_CP_FLAG_BASECLASS_NOFLAG
));
869 bases
.elements
[i
] = instance
->convert_type (base_type
);
872 result
= instance
->plugin ().start_class_type
873 (name
.get (), resuld
, &bases
, filename
, line
);
877 gdb_assert (type
->code () == TYPE_CODE_UNION
);
878 result
= instance
->plugin ().start_class_type
879 (name
.get (), resuld
, nullptr, filename
, line
);
882 instance
->insert_type (type
, result
);
884 /* Add definitions. */
885 compile_cplus_convert_type_defns (instance
, type
);
888 compile_cplus_convert_struct_or_union_methods (instance
, type
, result
);
891 compile_cplus_convert_struct_or_union_members (instance
, type
, result
);
894 instance
->plugin ().finish_class_type (name
.get (), TYPE_LENGTH (type
));
896 /* Pop all scopes. */
897 instance
->leave_scope ();
901 /* Convert an enum type to its gcc representation. If this type
902 was defined in another type, NESTED_ACCESS should indicate the
903 accessibility of this type.*/
906 compile_cplus_convert_enum (compile_cplus_instance
*instance
, struct type
*type
,
907 enum gcc_cp_symbol_kind nested_access
)
909 bool scoped_enum_p
= false;
911 /* Create a new scope for this type. */
912 compile_scope scope
= instance
->new_scope (type
->name (), type
);
914 if (scope
.nested_type () != GCC_TYPE_NONE
)
916 /* The type requested was actually defined inside another type,
917 such as a nested class definition. Return that type. */
918 return scope
.nested_type ();
921 gdb::unique_xmalloc_ptr
<char> name
922 = compile_cplus_instance::decl_name (type
->name ());
924 /* Push all scopes. */
925 instance
->enter_scope (std::move (scope
));
928 = instance
->plugin ().get_int_type (type
->is_unsigned (),
929 TYPE_LENGTH (type
), nullptr);
931 = instance
->plugin ().start_enum_type (name
.get (), int_type
,
932 GCC_CP_SYMBOL_ENUM
| nested_access
934 ? GCC_CP_FLAG_ENUM_SCOPED
935 : GCC_CP_FLAG_ENUM_NOFLAG
),
937 for (int i
= 0; i
< type
->num_fields (); ++i
)
939 gdb::unique_xmalloc_ptr
<char> fname
940 = compile_cplus_instance::decl_name (TYPE_FIELD_NAME (type
, i
));
942 if (TYPE_FIELD_LOC_KIND (type
, i
) != FIELD_LOC_KIND_ENUMVAL
946 instance
->plugin ().build_enum_constant (result
, fname
.get (),
947 TYPE_FIELD_ENUMVAL (type
, i
));
950 /* Finish enum definition and pop scopes. */
951 instance
->plugin ().finish_enum_type (result
);
952 instance
->leave_scope ();
956 /* Convert a function type to its gcc representation. This function does
957 not deal with function templates. */
960 compile_cplus_convert_func (compile_cplus_instance
*instance
,
961 struct type
*type
, bool strip_artificial
)
963 int is_varargs
= type
->has_varargs ();
964 struct type
*target_type
= TYPE_TARGET_TYPE (type
);
966 /* Functions with no debug info have no return type. Ideally we'd
967 want to fallback to the type of the cast just before the
968 function, like GDB's built-in expression parser, but we don't
969 have access to that type here. For now, fallback to int, like
970 GDB's parser used to do. */
971 if (target_type
== nullptr)
973 if (TYPE_OBJFILE_OWNED (type
))
974 target_type
= objfile_type (TYPE_OWNER (type
).objfile
)->builtin_int
;
976 target_type
= builtin_type (TYPE_OWNER (type
).gdbarch
)->builtin_int
;
977 warning (_("function has unknown return type; assuming int"));
980 /* This approach means we can't make self-referential function
981 types. Those are impossible in C, though. */
982 gcc_type return_type
= instance
->convert_type (target_type
);
984 std::vector
<gcc_type
> elements (type
->num_fields ());
985 struct gcc_type_array array
= { type
->num_fields (), elements
.data () };
987 for (int i
= 0; i
< type
->num_fields (); ++i
)
989 if (strip_artificial
&& TYPE_FIELD_ARTIFICIAL (type
, i
))
996 array
.elements
[i
- artificials
]
997 = instance
->convert_type (type
->field (i
).type ());
1001 /* We omit setting the argument types to `void' to be a little flexible
1002 with some minsyms like printf (compile-cplus.exp has examples). */
1003 gcc_type result
= instance
->plugin ().build_function_type
1004 (return_type
, &array
, is_varargs
);
1008 /* Convert an integer type to its gcc representation. */
1011 compile_cplus_convert_int (compile_cplus_instance
*instance
, struct type
*type
)
1013 if (type
->has_no_signedness ())
1015 gdb_assert (TYPE_LENGTH (type
) == 1);
1016 return instance
->plugin ().get_char_type ();
1019 return instance
->plugin ().get_int_type
1020 (type
->is_unsigned (), TYPE_LENGTH (type
), type
->name ());
1023 /* Convert a floating-point type to its gcc representation. */
1026 compile_cplus_convert_float (compile_cplus_instance
*instance
,
1029 return instance
->plugin ().get_float_type
1030 (TYPE_LENGTH (type
), type
->name ());
1033 /* Convert the 'void' type to its gcc representation. */
1036 compile_cplus_convert_void (compile_cplus_instance
*instance
, struct type
*type
)
1038 return instance
->plugin ().get_void_type ();
1041 /* Convert a boolean type to its gcc representation. */
1044 compile_cplus_convert_bool (compile_cplus_instance
*instance
, struct type
*type
)
1046 return instance
->plugin ().get_bool_type ();
1049 /* See description in compile-cplus.h. */
1052 compile_cplus_instance::convert_qualified_base (gcc_type base
,
1053 gcc_cp_qualifiers_flags quals
)
1055 gcc_type result
= base
;
1058 result
= plugin ().build_qualified_type (base
, quals
.raw ());
1063 /* See description in compile-cplus.h. */
1066 compile_cplus_convert_qualified (compile_cplus_instance
*instance
,
1069 struct type
*unqual
= make_unqualified_type (type
);
1070 gcc_cp_qualifiers_flags quals
= (enum gcc_cp_qualifiers
) 0;
1071 gcc_type unqual_converted
= instance
->convert_type (unqual
);
1073 if (TYPE_CONST (type
))
1074 quals
|= GCC_CP_QUALIFIER_CONST
;
1075 if (TYPE_VOLATILE (type
))
1076 quals
|= GCC_CP_QUALIFIER_VOLATILE
;
1077 if (TYPE_RESTRICT (type
))
1078 quals
|= GCC_CP_QUALIFIER_RESTRICT
;
1080 return instance
->convert_qualified_base (unqual_converted
, quals
);
1083 /* Convert a complex type to its gcc representation. */
1086 compile_cplus_convert_complex (compile_cplus_instance
*instance
,
1089 gcc_type base
= instance
->convert_type (TYPE_TARGET_TYPE (type
));
1091 return instance
->plugin ().build_complex_type (base
);
1094 /* Convert a namespace of TYPE. */
1097 compile_cplus_convert_namespace (compile_cplus_instance
*instance
,
1100 compile_scope scope
= instance
->new_scope (type
->name (), type
);
1101 gdb::unique_xmalloc_ptr
<char> name
1102 = compile_cplus_instance::decl_name (type
->name ());
1105 instance
->enter_scope (std::move (scope
));
1107 /* Convert this namespace. */
1108 instance
->plugin ().push_namespace (name
.get ());
1109 instance
->plugin ().pop_binding_level (name
.get ());
1112 instance
->leave_scope ();
1114 /* Namespaces are non-cacheable types. */
1115 return GCC_TYPE_NONE
;
1118 /* A helper function which knows how to convert most types from their
1119 gdb representation to the corresponding gcc form. This examines
1120 the TYPE and dispatches to the appropriate conversion function. It
1121 returns the gcc type.
1123 If the type was defined in another type, NESTED_ACCESS should indicate the
1124 accessibility of this type. */
1127 convert_type_cplus_basic (compile_cplus_instance
*instance
,
1129 enum gcc_cp_symbol_kind nested_access
)
1131 /* If we are converting a qualified type, first convert the
1132 unqualified type and then apply the qualifiers. */
1133 if ((type
->instance_flags () & (TYPE_INSTANCE_FLAG_CONST
1134 | TYPE_INSTANCE_FLAG_VOLATILE
1135 | TYPE_INSTANCE_FLAG_RESTRICT
)) != 0)
1136 return compile_cplus_convert_qualified (instance
, type
);
1138 switch (type
->code ())
1141 case TYPE_CODE_RVALUE_REF
:
1142 return compile_cplus_convert_reference (instance
, type
);
1145 return compile_cplus_convert_pointer (instance
, type
);
1147 case TYPE_CODE_ARRAY
:
1148 return compile_cplus_convert_array (instance
, type
);
1150 case TYPE_CODE_STRUCT
:
1151 case TYPE_CODE_UNION
:
1153 compile_cplus_convert_struct_or_union (instance
, type
, nested_access
);
1155 case TYPE_CODE_ENUM
:
1156 return compile_cplus_convert_enum (instance
, type
, nested_access
);
1158 case TYPE_CODE_FUNC
:
1159 return compile_cplus_convert_func (instance
, type
, false);
1161 case TYPE_CODE_METHOD
:
1163 compile_cplus_convert_method (instance
, TYPE_SELF_TYPE (type
), type
);
1165 case TYPE_CODE_MEMBERPTR
:
1166 case TYPE_CODE_METHODPTR
:
1167 return compile_cplus_convert_memberptr (instance
, type
);
1171 return compile_cplus_convert_int (instance
, type
);
1174 return compile_cplus_convert_float (instance
, type
);
1176 case TYPE_CODE_VOID
:
1177 return compile_cplus_convert_void (instance
, type
);
1179 case TYPE_CODE_BOOL
:
1180 return compile_cplus_convert_bool (instance
, type
);
1182 case TYPE_CODE_COMPLEX
:
1183 return compile_cplus_convert_complex (instance
, type
);
1185 case TYPE_CODE_NAMESPACE
:
1186 return compile_cplus_convert_namespace (instance
, type
);
1188 case TYPE_CODE_TYPEDEF
:
1189 return compile_cplus_convert_typedef (instance
, type
, nested_access
);
1195 std::string s
= string_printf (_("unhandled TYPE_CODE %d"),
1198 return instance
->plugin ().error (s
.c_str ());
1202 compile_cplus_instance::convert_type (struct type
*type
,
1203 enum gcc_cp_symbol_kind nested_access
)
1205 /* Check if TYPE has already been converted. */
1207 if (get_cached_type (type
, &result
))
1210 /* It is the first time this type has been seen -- convert it
1211 and cache it, if appropriate.. */
1212 result
= convert_type_cplus_basic (this, type
, nested_access
);
1213 if (result
!= GCC_TYPE_NONE
)
1214 insert_type (type
, result
);
1219 compile_cplus_instance::gcc_cplus_enter_scope
1220 (void *datum
, struct gcc_cp_context
*gcc_context
)
1225 compile_cplus_instance::gcc_cplus_leave_scope
1226 (void *datum
, struct gcc_cp_context
*gcc_context
)
1232 /* Plug-in forwards. */
1234 /* C++ plug-in wrapper. */
1236 /* A result printer for plug-in calls that return a gcc_type or
1240 compile_cplus_debug_output_1 (ULONGEST arg
)
1242 fprintf_unfiltered (gdb_stdlog
, "%s", pulongest (arg
));
1246 compile_cplus_debug_output_1 (const char *arg
)
1249 fputs_unfiltered ("NULL", gdb_stdlog
);
1251 fputs_unfiltered (arg
, gdb_stdlog
);
1255 compile_cplus_debug_output ()
1259 template <typename T
>
1261 compile_cplus_debug_output_1 (const T
*arg
)
1265 template <typename T
, typename
... Targs
>
1267 compile_cplus_debug_output (T arg
, Targs
... Args
)
1269 compile_cplus_debug_output_1 (arg
);
1270 fputc_unfiltered (' ', gdb_stdlog
);
1271 compile_cplus_debug_output (Args
...);
1274 #define FORWARD(OP,...) m_context->cp_ops->OP(m_context, ##__VA_ARGS__)
1275 #define OUTPUT_DEBUG_RESULT(R) \
1276 if (debug_compile_cplus_types) \
1278 fputs_unfiltered (": ", gdb_stdlog); \
1279 compile_cplus_debug_output (R); \
1280 fputc_unfiltered ('\n', gdb_stdlog); \
1283 #define GCC_METHOD0(R, N) \
1284 R gcc_cp_plugin::N () const \
1286 if (debug_compile_cplus_types) \
1287 compile_cplus_debug_output (STRINGIFY (N)); \
1288 auto result = FORWARD (N); \
1289 OUTPUT_DEBUG_RESULT (result); \
1292 #define GCC_METHOD1(R, N, A) \
1293 R gcc_cp_plugin::N (A a) const \
1295 if (debug_compile_cplus_types) \
1296 compile_cplus_debug_output (STRINGIFY (N), a); \
1297 auto result = FORWARD (N, a); \
1298 OUTPUT_DEBUG_RESULT (result); \
1301 #define GCC_METHOD2(R, N, A, B) \
1302 R gcc_cp_plugin::N (A a, B b) const \
1304 if (debug_compile_cplus_types) \
1305 compile_cplus_debug_output (STRINGIFY (N), a, b); \
1306 auto result = FORWARD (N, a, b); \
1307 OUTPUT_DEBUG_RESULT (result); \
1310 #define GCC_METHOD3(R, N, A, B, C) \
1311 R gcc_cp_plugin::N (A a, B b, C c) const \
1313 if (debug_compile_cplus_types) \
1314 compile_cplus_debug_output (STRINGIFY (N), a, b, c); \
1315 auto result = FORWARD (N, a, b, c); \
1316 OUTPUT_DEBUG_RESULT (result); \
1319 #define GCC_METHOD4(R, N, A, B, C, D) \
1320 R gcc_cp_plugin::N (A a, B b, C c, D d) const \
1322 if (debug_compile_cplus_types) \
1323 compile_cplus_debug_output (STRINGIFY (N), a, b, c, d); \
1324 auto result = FORWARD (N, a, b, c, d); \
1325 OUTPUT_DEBUG_RESULT (result); \
1328 #define GCC_METHOD5(R, N, A, B, C, D, E) \
1329 R gcc_cp_plugin::N (A a, B b, C c, D d, E e) const \
1331 if (debug_compile_cplus_types) \
1332 compile_cplus_debug_output (STRINGIFY (N), a, b, c, d, e); \
1333 auto result = FORWARD (N, a, b, c, d, e); \
1334 OUTPUT_DEBUG_RESULT (result); \
1337 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
1338 R gcc_cp_plugin::N (A a, B b, C c, D d, E e, F f, G g) const \
1340 if (debug_compile_cplus_types) \
1341 compile_cplus_debug_output (STRINGIFY (N), a, b, c, d, e, f, g); \
1342 auto result = FORWARD (N, a, b, c, d, e, f, g); \
1343 OUTPUT_DEBUG_RESULT (result); \
1347 #include "gcc-cp-fe.def"
1357 #undef OUTPUT_DEBUG_RESULT
1360 gcc_cp_plugin::build_decl (const char *debug_decltype
, const char *name
,
1361 enum gcc_cp_symbol_kind sym_kind
, gcc_type sym_type
,
1362 const char *substitution_name
, gcc_address address
,
1363 const char *filename
, unsigned int line_number
)
1365 if (debug_compile_cplus_types
)
1366 fprintf_unfiltered (gdb_stdlog
, "<%s> ", debug_decltype
);
1368 return build_decl (name
, sym_kind
, sym_type
, substitution_name
,
1369 address
, filename
, line_number
);
1373 gcc_cp_plugin::start_class_type (const char *debug_name
, gcc_decl typedecl
,
1374 const struct gcc_vbase_array
*base_classes
,
1375 const char *filename
, unsigned int line_number
)
1377 if (debug_compile_cplus_types
)
1378 fprintf_unfiltered (gdb_stdlog
, "<%s> ", debug_name
);
1380 return start_class_type (typedecl
, base_classes
, filename
, line_number
);
1384 gcc_cp_plugin::finish_class_type (const char *debug_name
,
1385 unsigned long size_in_bytes
)
1387 if (debug_compile_cplus_types
)
1388 fprintf_unfiltered (gdb_stdlog
, "<%s> ", debug_name
);
1390 return finish_class_type (size_in_bytes
);
1394 gcc_cp_plugin::pop_binding_level (const char *debug_name
)
1396 if (debug_compile_cplus_types
)
1397 fprintf_unfiltered (gdb_stdlog
, "<%s> ", debug_name
);
1399 return pop_binding_level ();
1402 void _initialize_compile_cplus_types ();
1404 _initialize_compile_cplus_types ()
1406 add_setshow_boolean_cmd ("compile-cplus-types", no_class
,
1407 &debug_compile_cplus_types
, _("\
1408 Set debugging of C++ compile type conversion."), _("\
1409 Show debugging of C++ compile type conversion."), _("\
1410 When enabled debugging messages are printed during C++ type conversion for\n\
1411 the compile commands."),
1417 add_setshow_boolean_cmd ("compile-cplus-scopes", no_class
,
1418 &debug_compile_cplus_scopes
, _("\
1419 Set debugging of C++ compile scopes."), _("\
1420 Show debugging of C++ compile scopes."), _("\
1421 When enabled debugging messages are printed about definition scopes during\n\
1422 C++ type conversion for the compile commands."),