1 /* Convert types from GDB to GCC
3 Copyright (C) 2014-2018 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 "common/preprocessor.h"
24 #include "compile-internal.h"
25 #include "compile-cplus.h"
26 #include "gdb_assert.h"
29 #include "cp-support.h"
36 #include "compile-c.h" /* Included for c_get_range_decl_name
40 /* Default compile flags for C++. */
42 const char *compile_cplus_instance::m_default_cflags
= "-std=gnu++11";
44 /* Flag to enable internal debugging. */
46 static int debug_compile_cplus_types
= 0;
48 /* Flag to enable internal scope switching debugging. */
50 static int debug_compile_cplus_scopes
= 0;
52 /* Forward declarations. */
54 static gcc_type
compile_cplus_convert_func (compile_cplus_instance
*instance
,
56 bool strip_artificial
);
58 /* See description in compile-cplus.h. */
60 gdb::unique_xmalloc_ptr
<char>
61 compile_cplus_instance::decl_name (const char *natural
)
63 if (natural
== nullptr)
66 char *name
= cp_func_name (natural
);
68 return gdb::unique_xmalloc_ptr
<char> (name
);
70 return gdb::unique_xmalloc_ptr
<char> (xstrdup (natural
));
73 /* Get the access flag for the NUM'th field of TYPE. */
75 static enum gcc_cp_symbol_kind
76 get_field_access_flag (const struct type
*type
, int num
)
78 if (TYPE_FIELD_PROTECTED (type
, num
))
79 return GCC_CP_ACCESS_PROTECTED
;
80 else if (TYPE_FIELD_PRIVATE (type
, num
))
81 return GCC_CP_ACCESS_PRIVATE
;
83 /* GDB assumes everything else is public. */
84 return GCC_CP_ACCESS_PUBLIC
;
87 /* Get the access flag for the NUM'th method of TYPE's FNI'th
90 enum gcc_cp_symbol_kind
91 get_method_access_flag (const struct type
*type
, int fni
, int num
)
93 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_STRUCT
);
95 /* If this type was not declared a class, everything is public. */
96 if (!TYPE_DECLARED_CLASS (type
))
97 return GCC_CP_ACCESS_PUBLIC
;
99 /* Otherwise, read accessibility from the fn_field. */
100 const struct fn_field
*methods
= TYPE_FN_FIELDLIST1 (type
, fni
);
101 if (TYPE_FN_FIELD_PROTECTED (methods
, num
))
102 return GCC_CP_ACCESS_PROTECTED
;
103 else if (TYPE_FN_FIELD_PRIVATE (methods
, num
))
104 return GCC_CP_ACCESS_PRIVATE
;
106 return GCC_CP_ACCESS_PUBLIC
;
109 /* A useful debugging function to output the scope SCOPE to stdout. */
111 static void __attribute__ ((used
))
112 debug_print_scope (const compile_scope
&scope
)
114 for (const auto &comp
: scope
)
116 const char *symbol
= (comp
.bsymbol
.symbol
!= nullptr
117 ? SYMBOL_NATURAL_NAME (comp
.bsymbol
.symbol
)
120 printf_unfiltered ("\tname = %s, symbol = %s\n", comp
.name
.c_str (),
125 /* See description in compile-cplus.h. */
128 type_name_to_scope (const char *type_name
, const struct block
*block
)
132 if (type_name
== nullptr)
134 /* An anonymous type. We cannot really do much here. We simply cannot
135 look up anonymous types easily/at all. */
139 const char *p
= type_name
;
140 std::string lookup_name
;
144 /* Create a string token of the first component of TYPE_NAME. */
145 int len
= cp_find_first_component (p
);
146 std::string
s (p
, len
);
148 /* Advance past the last token. */
151 /* Look up the symbol and decide when to stop. */
152 if (!lookup_name
.empty ())
156 /* Look up the resulting name. */
157 struct block_symbol bsymbol
158 = lookup_symbol (lookup_name
.c_str (), block
, VAR_DOMAIN
, nullptr);
160 if (bsymbol
.symbol
!= nullptr)
162 scope_component comp
= {s
, bsymbol
};
164 scope
.push_back (comp
);
166 if (TYPE_CODE (SYMBOL_TYPE (bsymbol
.symbol
)) != TYPE_CODE_NAMESPACE
)
180 /* This shouldn't happen since we are not attempting to
181 loop over user input. This name is generated by GDB
183 internal_error (__FILE__
, __LINE__
,
184 _("malformed TYPE_NAME during parsing"));
192 /* Compare two scope_components for equality. These are equal if the names
193 of the two components' are the same. */
196 operator== (const scope_component
&lhs
, const scope_component
&rhs
)
198 return lhs
.name
== rhs
.name
;
201 /* Compare two scope_components for inequality. These are not equal if
202 the two components' names are not equal. */
205 operator!= (const scope_component
&lhs
, const scope_component
&rhs
)
207 return lhs
.name
!= rhs
.name
;
210 /* Compare two compile_scopes for equality. These are equal if they are both
211 contain the same number of components and each component is equal. */
214 operator== (const compile_scope
&lhs
, const compile_scope
&rhs
)
216 if (lhs
.size () != rhs
.size ())
219 for (int i
= 0; i
< lhs
.size (); ++i
)
221 if (lhs
[i
] != rhs
[i
])
228 /* Compare two compile_scopes for inequality. These are inequal if they
229 contain unequal number of elements or if any of the components are not
233 operator!= (const compile_scope
&lhs
, const compile_scope
&rhs
)
235 if (lhs
.size () != rhs
.size ())
238 for (int i
= 0; i
< lhs
.size (); ++i
)
240 if (lhs
[i
] != rhs
[i
])
247 /* See description in compile-cplus.h. */
250 compile_cplus_instance::enter_scope (compile_scope
&new_scope
)
252 bool must_push
= m_scopes
.empty () || m_scopes
.back () != new_scope
;
254 new_scope
.m_pushed
= must_push
;
256 /* Save the new scope. */
257 m_scopes
.push_back (new_scope
);
261 if (debug_compile_cplus_scopes
)
263 fprintf_unfiltered (gdb_stdlog
, "entering new scope %s\n",
264 host_address_to_string (&new_scope
));
267 /* Push the global namespace. */
268 plugin ().push_namespace ("");
270 /* Push all other namespaces. Note that we do not push the last
271 scope_component -- that's the actual type we are converting. */
273 (new_scope
.begin (), new_scope
.end () - 1,
274 [this] (const scope_component
&comp
)
276 gdb_assert (TYPE_CODE (SYMBOL_TYPE (comp
.bsymbol
.symbol
))
277 == TYPE_CODE_NAMESPACE
);
279 const char *ns
= (comp
.name
== CP_ANONYMOUS_NAMESPACE_STR
? nullptr
280 : comp
.name
.c_str ());
282 this->plugin ().push_namespace (ns
);
287 if (debug_compile_cplus_scopes
)
289 fprintf_unfiltered (gdb_stdlog
, "staying in current scope -- "
290 "scopes are identical\n");
295 /* See description in compile-cplus.h. */
298 compile_cplus_instance::leave_scope ()
300 /* Get the current scope and remove it from the internal list of
302 compile_scope current
= m_scopes
.back ();
304 m_scopes
.pop_back ();
306 if (current
.m_pushed
)
308 if (debug_compile_cplus_scopes
)
309 fprintf_unfiltered (gdb_stdlog
, "leaving scope %p\n", current
);
311 /* Pop namespaces. */
313 (current
.begin (),current
.end () - 1,
314 [this] (const scope_component
&comp
) {
315 gdb_assert (TYPE_CODE (SYMBOL_TYPE (comp
.bsymbol
.symbol
))
316 == TYPE_CODE_NAMESPACE
);
317 this->plugin ().pop_binding_level (comp
.name
.c_str ());
320 /* Pop global namespace. */
321 plugin ().pop_binding_level ("");
325 if (debug_compile_cplus_scopes
)
326 fprintf_unfiltered (gdb_stdlog
,
327 "identical scopes -- not leaving scope\n");
331 /* See description in compile-cplus.h. */
334 compile_cplus_instance::new_scope (const char *type_name
, struct type
*type
)
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
339 compile_scope scope
= type_name_to_scope (type_name
, block ());
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 ();
347 if (!types_equal (type
, SYMBOL_TYPE (comp
.bsymbol
.symbol
))
348 && (m_scopes
.empty ()
349 || (m_scopes
.back ().back ().bsymbol
.symbol
350 != comp
.bsymbol
.symbol
)))
352 /* The type is defined inside another class(es). Convert that
353 type instead of defining this type. */
354 convert_type (SYMBOL_TYPE (comp
.bsymbol
.symbol
));
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
360 get_cached_type (type
, scope
.m_nested_type
);
366 if (TYPE_NAME (type
) == nullptr)
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 ())
375 scope
= m_scopes
.back ();
376 scope
.m_pushed
= false;
379 scope
.push_back (scope_component ());
385 decl_name (TYPE_NAME (type
)).get (),
386 lookup_symbol (TYPE_NAME (type
), block (), VAR_DOMAIN
, nullptr)
388 scope
.push_back (comp
);
392 /* There must be at least one component in the compile_scope. */
393 gdb_assert (scope
.size () > 0);
397 /* See description in compile-cplus.h. */
400 compile_cplus_instance::convert_reference_base
401 (gcc_type base
, enum gcc_cp_ref_qualifiers rquals
)
403 return this->plugin ().build_reference_type (base
, rquals
);
406 /* Convert a reference type to its gcc representation. */
409 compile_cplus_convert_reference (compile_cplus_instance
*instance
,
412 gcc_type target
= instance
->convert_type (TYPE_TARGET_TYPE (type
));
414 enum gcc_cp_ref_qualifiers quals
= GCC_CP_REF_QUAL_NONE
;
415 switch (TYPE_CODE (type
))
418 quals
= GCC_CP_REF_QUAL_LVALUE
;
420 case TYPE_CODE_RVALUE_REF
:
421 quals
= GCC_CP_REF_QUAL_RVALUE
;
424 gdb_assert_not_reached ("unexpected type code for reference type");
427 return instance
->convert_reference_base (target
, quals
);
430 /* See description in compile-cplus.h. */
433 compile_cplus_instance::convert_pointer_base(gcc_type target
)
435 return plugin ().build_pointer_type (target
);
438 /* Convert a pointer type to its gcc representation. */
441 compile_cplus_convert_pointer (compile_cplus_instance
*instance
,
444 gcc_type target
= instance
->convert_type (TYPE_TARGET_TYPE (type
));
446 return instance
->convert_pointer_base (target
);
449 /* Convert an array type to its gcc representation. */
452 compile_cplus_convert_array (compile_cplus_instance
*instance
,
455 struct type
*range
= TYPE_INDEX_TYPE (type
);
456 gcc_type element_type
= instance
->convert_type (TYPE_TARGET_TYPE (type
));
458 if (TYPE_LOW_BOUND_KIND (range
) != PROP_CONST
)
460 const char *s
= _("array type with non-constant"
461 " lower bound is not supported");
463 return instance
->plugin ().error (s
);
466 if (TYPE_LOW_BOUND (range
) != 0)
468 const char *s
= _("cannot convert array type with "
469 "non-zero lower bound to C");
471 return instance
->plugin ().error (s
);
474 if (TYPE_HIGH_BOUND_KIND (range
) == PROP_LOCEXPR
475 || TYPE_HIGH_BOUND_KIND (range
) == PROP_LOCLIST
)
477 if (TYPE_VECTOR (type
))
479 const char *s
= _("variably-sized vector type is not supported");
481 return instance
->plugin ().error (s
);
484 std::string upper_bound
485 = c_get_range_decl_name (&TYPE_RANGE_DATA (range
)->high
);
486 return instance
->plugin ().build_vla_array_type (element_type
,
487 upper_bound
.c_str ());
491 LONGEST low_bound
, high_bound
, count
;
493 if (get_array_bounds (type
, &low_bound
, &high_bound
) == 0)
497 gdb_assert (low_bound
== 0); /* Ensured above. */
498 count
= high_bound
+ 1;
501 if (TYPE_VECTOR (type
))
502 return instance
->plugin ().build_vector_type (element_type
, count
);
504 return instance
->plugin ().build_array_type (element_type
, count
);
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
513 compile_cplus_convert_typedef (compile_cplus_instance
*instance
,
515 enum gcc_cp_symbol_kind nested_access
)
517 compile_scope scope
= instance
->new_scope (TYPE_NAME (type
), type
);
519 if (scope
.nested_type () != GCC_TYPE_NONE
)
520 return scope
.nested_type ();
522 gdb::unique_xmalloc_ptr
<char> name
523 = compile_cplus_instance::decl_name (TYPE_NAME (type
));
525 /* Make sure the scope for this type has been pushed. */
526 instance
->enter_scope (scope
);
528 /* Convert the typedef's real type. */
529 gcc_type typedef_type
= instance
->convert_type (check_typedef (type
));
531 instance
->plugin ().build_decl ("typedef", name
.get (),
532 GCC_CP_SYMBOL_TYPEDEF
| nested_access
,
533 typedef_type
, 0, 0, nullptr, 0);
535 /* Completed this scope. */
536 instance
->leave_scope ();
540 /* Convert types defined in TYPE. */
543 compile_cplus_convert_type_defns (compile_cplus_instance
*instance
,
547 enum gcc_cp_symbol_kind accessibility
;
549 /* Convert typedefs. */
550 for (i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (type
); ++i
)
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
;
557 accessibility
= GCC_CP_ACCESS_PUBLIC
;
558 instance
->convert_type (TYPE_TYPEDEF_FIELD_TYPE (type
, i
), accessibility
);
561 /* Convert nested types. */
562 for (i
= 0; i
< TYPE_NESTED_TYPES_COUNT (type
); ++i
)
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
;
569 accessibility
= GCC_CP_ACCESS_PUBLIC
;
570 instance
->convert_type (TYPE_NESTED_TYPES_FIELD_TYPE (type
, i
),
575 /* Convert data members defined in TYPE, which should be struct/class/union
576 with gcc_type COMP_TYPE. */
579 compile_cplus_convert_struct_or_union_members
580 (compile_cplus_instance
*instance
, struct type
*type
, gcc_type comp_type
)
582 for (int i
= TYPE_N_BASECLASSES (type
); i
< TYPE_NFIELDS (type
); ++i
)
584 const char *field_name
= TYPE_FIELD_NAME (type
, i
);
586 if (TYPE_FIELD_IGNORE (type
, i
)
587 || TYPE_FIELD_ARTIFICIAL (type
, i
))
590 /* GDB records unnamed/anonymous fields with empty string names. */
591 if (*field_name
== '\0')
592 field_name
= nullptr;
595 = instance
->convert_type (TYPE_FIELD_TYPE (type
, i
));
597 if (field_is_static (&TYPE_FIELD (type
, i
)))
601 switch (TYPE_FIELD_LOC_KIND (type
, i
))
603 case FIELD_LOC_KIND_PHYSADDR
:
605 physaddr
= TYPE_FIELD_STATIC_PHYSADDR (type
, i
);
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);
614 case FIELD_LOC_KIND_PHYSNAME
:
616 const char *physname
= TYPE_FIELD_STATIC_PHYSNAME (type
, i
);
617 struct block_symbol sym
618 = lookup_symbol (physname
, instance
->block (),
619 VAR_DOMAIN
, nullptr);
621 if (sym
.symbol
== nullptr)
623 /* We didn't actually find the symbol. There's little
624 we can do but ignore this member. */
627 const char *filename
= symbol_symtab (sym
.symbol
)->filename
;
628 unsigned int line
= SYMBOL_LINE (sym
.symbol
);
630 physaddr
= SYMBOL_VALUE_ADDRESS (sym
.symbol
);
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
);
639 gdb_assert_not_reached
640 ("unexpected static field location kind");
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
);
650 bitsize
= 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (type
, i
));
652 instance
->plugin ().build_field
653 (field_name
, field_type
, field_flags
, bitsize
,
654 TYPE_FIELD_BITPOS (type
, i
));
659 /* Convert a method type to its gcc representation. */
662 compile_cplus_convert_method (compile_cplus_instance
*instance
,
663 struct type
*parent_type
,
664 struct type
*method_type
)
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
= (enum gcc_cp_qualifiers
) 0;
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
;
679 /* Not yet implemented. */
680 gcc_cp_ref_qualifiers_flags rquals
= GCC_CP_REF_QUAL_NONE
;
682 return instance
->plugin ().build_method_type
683 (class_type
, func_type
, quals
, rquals
);
686 /* Convert a member or method pointer represented by TYPE. */
689 compile_cplus_convert_memberptr (compile_cplus_instance
*instance
,
692 struct type
*containing_class
= TYPE_SELF_TYPE (type
);
694 if (containing_class
== nullptr)
695 return GCC_TYPE_NONE
;
697 gcc_type class_type
= instance
->convert_type (containing_class
);
699 = instance
->convert_type (TYPE_TARGET_TYPE (type
));
701 return instance
->plugin ().build_pointer_to_member_type
702 (class_type
, member_type
);
705 /* Convert all methods defined in TYPE, which should be a class/struct/union
706 with gcc_type CLASS_TYPE. */
709 compile_cplus_convert_struct_or_union_methods (compile_cplus_instance
*instance
,
713 for (int i
= 0; i
< TYPE_NFN_FIELDS (type
); ++i
)
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
));
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
)
723 /* Skip artificial methods. */
724 if (TYPE_FN_FIELD_ARTIFICIAL (methods
, j
))
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);
733 if (sym
.symbol
== nullptr)
735 if (TYPE_FN_FIELD_VIRTUAL_P (methods
, j
))
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
));
742 instance
->plugin ().build_decl
743 ("pure virtual method", overloaded_name
.get (),
745 | get_method_access_flag (type
, i
, j
)
746 | GCC_CP_FLAG_VIRTUAL_FUNCTION
747 | GCC_CP_FLAG_PURE_VIRTUAL_FUNCTION
),
748 method_type
, nullptr, 0, nullptr, 0);
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.
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
));
766 const char *filename
= symbol_symtab (sym
.symbol
)->filename
;
767 unsigned int line
= SYMBOL_LINE (sym
.symbol
);
768 CORE_ADDR address
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
.symbol
));
771 if (TYPE_FN_FIELD_STATIC_P (methods
, j
))
773 kind
= "static method";
774 method_type
= compile_cplus_convert_func
775 (instance
, TYPE_FN_FIELD_TYPE (methods
, j
), true);
780 method_type
= (compile_cplus_convert_method
781 (instance
, type
, TYPE_FN_FIELD_TYPE (methods
, j
)));
784 if (TYPE_FN_FIELD_VIRTUAL_P (methods
, j
))
785 sym_kind
|= GCC_CP_FLAG_VIRTUAL_FUNCTION
;
787 instance
->plugin ().build_decl
788 (kind
, overloaded_name
.get (),
789 sym_kind
| get_method_access_flag (type
, i
, j
),
790 method_type
, nullptr, address
, filename
, line
);
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. */
800 compile_cplus_convert_struct_or_union (compile_cplus_instance
*instance
,
802 enum gcc_cp_symbol_kind nested_access
)
804 const char *filename
= nullptr;
805 unsigned short line
= 0;
807 /* Get the decl name of this type. */
808 gdb::unique_xmalloc_ptr
<char> name
809 = compile_cplus_instance::decl_name (TYPE_NAME (type
));
811 /* Create a new scope for TYPE. */
812 compile_scope scope
= instance
->new_scope (TYPE_NAME (type
), type
);
814 if (scope
.nested_type () != GCC_TYPE_NONE
)
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 ();
821 /* Push all scopes. */
822 instance
->enter_scope (scope
);
824 /* First we create the resulting type and enter it into our hash
825 table. This lets recursive types work. */
828 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
830 const char *what
= TYPE_DECLARED_CLASS (type
) ? "struct" : "class";
832 resuld
= instance
->plugin ().build_decl
833 (what
, name
.get (), (GCC_CP_SYMBOL_CLASS
| nested_access
834 | (TYPE_DECLARED_CLASS (type
)
835 ? GCC_CP_FLAG_CLASS_NOFLAG
836 : GCC_CP_FLAG_CLASS_IS_STRUCT
)),
837 0, nullptr, 0, filename
, line
);
841 gdb_assert (TYPE_CODE (type
) == 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
);
848 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
850 struct gcc_vbase_array bases
;
851 int num_baseclasses
= TYPE_N_BASECLASSES (type
);
853 memset (&bases
, 0, sizeof (bases
));
855 if (num_baseclasses
> 0)
857 bases
.elements
= XNEWVEC (gcc_type
, num_baseclasses
);
858 bases
.flags
= XNEWVEC (enum gcc_cp_symbol_kind
, num_baseclasses
);
859 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
);
873 result
= instance
->plugin ().start_class_type
874 (name
.get (), resuld
, &bases
, filename
, line
);
876 xfree (bases
.elements
);
880 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_UNION
);
881 result
= instance
->plugin ().start_class_type
882 (name
.get (), resuld
, nullptr, filename
, line
);
885 instance
->insert_type (type
, result
);
887 /* Add definitions. */
888 compile_cplus_convert_type_defns (instance
, type
);
891 compile_cplus_convert_struct_or_union_methods (instance
, type
, result
);
894 compile_cplus_convert_struct_or_union_members (instance
, type
, result
);
897 instance
->plugin ().finish_class_type (name
.get (), TYPE_LENGTH (type
));
899 /* Pop all scopes. */
900 instance
->leave_scope ();
904 /* Convert an enum type to its gcc representation. If this type
905 was defined in another type, NESTED_ACCESS should indicate the
906 accessibility of this type.*/
909 compile_cplus_convert_enum (compile_cplus_instance
*instance
, struct type
*type
,
910 enum gcc_cp_symbol_kind nested_access
)
912 int scoped_enum_p
= FALSE
;
914 /* Create a new scope for this type. */
915 compile_scope scope
= instance
->new_scope (TYPE_NAME (type
), type
);
917 if (scope
.nested_type () != GCC_TYPE_NONE
)
919 /* The type requested was actually defined inside another type,
920 such as a nested class definition. Return that type. */
921 return scope
.nested_type ();
924 gdb::unique_xmalloc_ptr
<char> name
925 = compile_cplus_instance::decl_name (TYPE_NAME (type
));
927 /* Push all scopes. */
928 instance
->enter_scope (scope
);
931 = instance
->plugin ().get_int_type (TYPE_UNSIGNED (type
),
932 TYPE_LENGTH (type
), nullptr);
934 = instance
->plugin ().start_enum_type (name
.get (), int_type
,
935 GCC_CP_SYMBOL_ENUM
| nested_access
937 ? GCC_CP_FLAG_ENUM_SCOPED
938 : GCC_CP_FLAG_ENUM_NOFLAG
),
940 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
942 gdb::unique_xmalloc_ptr
<char> fname
943 = compile_cplus_instance::decl_name (TYPE_FIELD_NAME (type
, i
));
945 if (TYPE_FIELD_LOC_KIND (type
, i
) != FIELD_LOC_KIND_ENUMVAL
949 instance
->plugin ().build_enum_constant (result
, fname
.get (),
950 TYPE_FIELD_ENUMVAL (type
, i
));
953 /* Finish enum definition and pop scopes. */
954 instance
->plugin ().finish_enum_type (result
);
955 instance
->leave_scope ();
959 /* Convert a function type to its gcc representation. This function does
960 not deal with function templates. */
963 compile_cplus_convert_func (compile_cplus_instance
*instance
,
964 struct type
*type
, bool strip_artificial
)
966 int is_varargs
= TYPE_VARARGS (type
);
967 struct type
*target_type
= TYPE_TARGET_TYPE (type
);
969 /* Functions with no debug info have no return type. Ideally we'd
970 want to fallback to the type of the cast just before the
971 function, like GDB's built-in expression parser, but we don't
972 have access to that type here. For now, fallback to int, like
973 GDB's parser used to do. */
974 if (target_type
== nullptr)
976 if (TYPE_OBJFILE_OWNED (type
))
977 target_type
= objfile_type (TYPE_OWNER (type
).objfile
)->builtin_int
;
979 target_type
= builtin_type (TYPE_OWNER (type
).gdbarch
)->builtin_int
;
980 warning (_("function has unknown return type; assuming int"));
983 /* This approach means we can't make self-referential function
984 types. Those are impossible in C, though. */
985 gcc_type return_type
= instance
->convert_type (target_type
);
987 struct gcc_type_array array
=
988 { TYPE_NFIELDS (type
), XNEWVEC (gcc_type
, TYPE_NFIELDS (type
)) };
990 for (int i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
992 if (strip_artificial
&& TYPE_FIELD_ARTIFICIAL (type
, i
))
999 array
.elements
[i
- artificials
]
1000 = instance
->convert_type (TYPE_FIELD_TYPE (type
, i
));
1004 /* We omit setting the argument types to `void' to be a little flexible
1005 with some minsyms like printf (compile-cplus.exp has examples). */
1006 gcc_type result
= instance
->plugin ().build_function_type
1007 (return_type
, &array
, is_varargs
);
1008 xfree (array
.elements
);
1012 /* Convert an integer type to its gcc representation. */
1015 compile_cplus_convert_int (compile_cplus_instance
*instance
, struct type
*type
)
1017 if (TYPE_NOSIGN (type
))
1019 gdb_assert (TYPE_LENGTH (type
) == 1);
1020 return instance
->plugin ().get_char_type ();
1023 return instance
->plugin ().get_int_type
1024 (TYPE_UNSIGNED (type
), TYPE_LENGTH (type
), TYPE_NAME (type
));
1027 /* Convert a floating-point type to its gcc representation. */
1030 compile_cplus_convert_float (compile_cplus_instance
*instance
,
1033 return instance
->plugin ().get_float_type
1034 (TYPE_LENGTH (type
), TYPE_NAME (type
));
1037 /* Convert the 'void' type to its gcc representation. */
1040 compile_cplus_convert_void (compile_cplus_instance
*instance
, struct type
*type
)
1042 return instance
->plugin ().get_void_type ();
1045 /* Convert a boolean type to its gcc representation. */
1048 compile_cplus_convert_bool (compile_cplus_instance
*instance
, struct type
*type
)
1050 return instance
->plugin ().get_bool_type ();
1053 /* See description in compile-cplus.h. */
1056 compile_cplus_instance::convert_qualified_base (gcc_type base
,
1057 gcc_cp_qualifiers_flags quals
)
1059 gcc_type result
= base
;
1061 if (quals
!= GCC_CP_REF_QUAL_NONE
)
1062 result
= plugin ().build_qualified_type (base
, quals
);
1067 /* See description in compile-cplus.h. */
1070 compile_cplus_convert_qualified (compile_cplus_instance
*instance
,
1073 struct type
*unqual
= make_unqualified_type (type
);
1074 gcc_cp_qualifiers_flags quals
= (enum gcc_cp_qualifiers
) 0;
1075 gcc_type unqual_converted
= instance
->convert_type (unqual
);
1077 if (TYPE_CONST (type
))
1078 quals
|= GCC_CP_QUALIFIER_CONST
;
1079 if (TYPE_VOLATILE (type
))
1080 quals
|= GCC_CP_QUALIFIER_VOLATILE
;
1081 if (TYPE_RESTRICT (type
))
1082 quals
|= GCC_CP_QUALIFIER_RESTRICT
;
1084 return instance
->convert_qualified_base (unqual_converted
, quals
);
1087 /* Convert a complex type to its gcc representation. */
1090 compile_cplus_convert_complex (compile_cplus_instance
*instance
,
1093 gcc_type base
= instance
->convert_type (TYPE_TARGET_TYPE (type
));
1095 return instance
->plugin ().build_complex_type (base
);
1098 /* Convert a namespace of TYPE. */
1101 compile_cplus_convert_namespace (compile_cplus_instance
*instance
,
1104 compile_scope scope
= instance
->new_scope (TYPE_NAME (type
), type
);
1105 gdb::unique_xmalloc_ptr
<char> name
1106 = compile_cplus_instance::decl_name (TYPE_NAME (type
));
1109 instance
->enter_scope (scope
);
1111 /* Convert this namespace. */
1112 instance
->plugin ().push_namespace (name
.get ());
1113 instance
->plugin ().pop_binding_level (name
.get ());
1116 instance
->leave_scope ();
1118 /* Namespaces are non-cacheable types. */
1119 return GCC_TYPE_NONE
;
1122 /* A helper function which knows how to convert most types from their
1123 gdb representation to the corresponding gcc form. This examines
1124 the TYPE and dispatches to the appropriate conversion function. It
1125 returns the gcc type.
1127 If the type was defined in another type, NESTED_ACCESS should indicate the
1128 accessibility of this type. */
1131 convert_type_cplus_basic (compile_cplus_instance
*instance
,
1133 enum gcc_cp_symbol_kind nested_access
)
1135 /* If we are converting a qualified type, first convert the
1136 unqualified type and then apply the qualifiers. */
1137 if ((TYPE_INSTANCE_FLAGS (type
) & (TYPE_INSTANCE_FLAG_CONST
1138 | TYPE_INSTANCE_FLAG_VOLATILE
1139 | TYPE_INSTANCE_FLAG_RESTRICT
)) != 0)
1140 return compile_cplus_convert_qualified (instance
, type
);
1142 switch (TYPE_CODE (type
))
1145 case TYPE_CODE_RVALUE_REF
:
1146 return compile_cplus_convert_reference (instance
, type
);
1149 return compile_cplus_convert_pointer (instance
, type
);
1151 case TYPE_CODE_ARRAY
:
1152 return compile_cplus_convert_array (instance
, type
);
1154 case TYPE_CODE_STRUCT
:
1155 case TYPE_CODE_UNION
:
1157 compile_cplus_convert_struct_or_union (instance
, type
, nested_access
);
1159 case TYPE_CODE_ENUM
:
1160 return compile_cplus_convert_enum (instance
, type
, nested_access
);
1162 case TYPE_CODE_FUNC
:
1163 return compile_cplus_convert_func (instance
, type
, false);
1165 case TYPE_CODE_METHOD
:
1167 compile_cplus_convert_method (instance
, TYPE_SELF_TYPE (type
), type
);
1169 case TYPE_CODE_MEMBERPTR
:
1170 case TYPE_CODE_METHODPTR
:
1171 return compile_cplus_convert_memberptr (instance
, type
);
1175 return compile_cplus_convert_int (instance
, type
);
1178 return compile_cplus_convert_float (instance
, type
);
1180 case TYPE_CODE_VOID
:
1181 return compile_cplus_convert_void (instance
, type
);
1183 case TYPE_CODE_BOOL
:
1184 return compile_cplus_convert_bool (instance
, type
);
1186 case TYPE_CODE_COMPLEX
:
1187 return compile_cplus_convert_complex (instance
, type
);
1189 case TYPE_CODE_NAMESPACE
:
1190 return compile_cplus_convert_namespace (instance
, type
);
1192 case TYPE_CODE_TYPEDEF
:
1193 return compile_cplus_convert_typedef (instance
, type
, nested_access
);
1199 std::string s
= string_printf (_("unhandled TYPE_CODE %d"),
1202 return instance
->plugin ().error (s
.c_str ());
1206 compile_cplus_instance::convert_type (struct type
*type
,
1207 enum gcc_cp_symbol_kind nested_access
)
1209 /* Check if TYPE has already been converted. */
1211 if (get_cached_type (type
, result
))
1214 /* It is the first time this type has been seen -- convert it
1215 and cache it, if appropriate.. */
1216 result
= convert_type_cplus_basic (this, type
, nested_access
);
1217 if (result
!= GCC_TYPE_NONE
)
1218 insert_type (type
, result
);
1223 compile_cplus_instance::gcc_cplus_enter_scope
1224 (void *datum
, struct gcc_cp_context
*gcc_context
)
1229 compile_cplus_instance::gcc_cplus_leave_scope
1230 (void *datum
, struct gcc_cp_context
*gcc_context
)
1236 /* Plug-in forwards. */
1238 /* C++ plug-in wrapper. */
1240 /* A result printer for plug-in calls that return a gcc_type or
1244 compile_cplus_debug_output_1 (gcc_type arg
)
1246 fprintf_unfiltered (gdb_stdlog
, "%lld", arg
);
1250 compile_cplus_debug_output_1 (const char *arg
)
1253 fputs_unfiltered ("NULL", gdb_stdlog
);
1255 fputs_unfiltered (arg
, gdb_stdlog
);
1259 compile_cplus_debug_output ()
1263 template <typename T
>
1265 compile_cplus_debug_output_1 (const T
*arg
)
1269 template <typename T
, typename
... Targs
>
1271 compile_cplus_debug_output (T arg
, Targs
... Args
)
1273 compile_cplus_debug_output_1 (arg
);
1274 fputc_unfiltered (' ', gdb_stdlog
);
1275 compile_cplus_debug_output (Args
...);
1278 #define FORWARD(OP,...) m_context->cp_ops->OP(m_context, ##__VA_ARGS__)
1279 #define OUTPUT_DEBUG_RESULT(R) \
1280 if (debug_compile_cplus_types) \
1282 fputs_unfiltered (": ", gdb_stdlog); \
1283 compile_cplus_debug_output (R); \
1284 fputc_unfiltered ('\n', gdb_stdlog); \
1287 #define GCC_METHOD0(R, N) \
1288 R gcc_cp_plugin::N () const \
1290 if (debug_compile_cplus_types) \
1291 compile_cplus_debug_output (STRINGIFY (N)); \
1292 auto result = FORWARD (N); \
1293 OUTPUT_DEBUG_RESULT (result); \
1296 #define GCC_METHOD1(R, N, A) \
1297 R gcc_cp_plugin::N (A a) const \
1299 if (debug_compile_cplus_types) \
1300 compile_cplus_debug_output (STRINGIFY (N), a); \
1301 auto result = FORWARD (N, a); \
1302 OUTPUT_DEBUG_RESULT (result); \
1305 #define GCC_METHOD2(R, N, A, B) \
1306 R gcc_cp_plugin::N (A a, B b) const \
1308 if (debug_compile_cplus_types) \
1309 compile_cplus_debug_output (STRINGIFY (N), a, b); \
1310 auto result = FORWARD (N, a, b); \
1311 OUTPUT_DEBUG_RESULT (result); \
1314 #define GCC_METHOD3(R, N, A, B, C) \
1315 R gcc_cp_plugin::N (A a, B b, C c) const \
1317 if (debug_compile_cplus_types) \
1318 compile_cplus_debug_output (STRINGIFY (N), a, b, c); \
1319 auto result = FORWARD (N, a, b, c); \
1320 OUTPUT_DEBUG_RESULT (result); \
1323 #define GCC_METHOD4(R, N, A, B, C, D) \
1324 R gcc_cp_plugin::N (A a, B b, C c, D d) const \
1326 if (debug_compile_cplus_types) \
1327 compile_cplus_debug_output (STRINGIFY (N), a, b, c, d); \
1328 auto result = FORWARD (N, a, b, c, d); \
1329 OUTPUT_DEBUG_RESULT (result); \
1332 #define GCC_METHOD5(R, N, A, B, C, D, E) \
1333 R gcc_cp_plugin::N (A a, B b, C c, D d, E e) const \
1335 if (debug_compile_cplus_types) \
1336 compile_cplus_debug_output (STRINGIFY (N), a, b, c, d, e); \
1337 auto result = FORWARD (N, a, b, c, d, e); \
1338 OUTPUT_DEBUG_RESULT (result); \
1341 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
1342 R gcc_cp_plugin::N (A a, B b, C c, D d, E e, F f, G g) const \
1344 if (debug_compile_cplus_types) \
1345 compile_cplus_debug_output (STRINGIFY (N), a, b, c, d, e, f, g); \
1346 auto result = FORWARD (N, a, b, c, d, e, f, g); \
1347 OUTPUT_DEBUG_RESULT (result); \
1351 #include "gcc-cp-fe.def"
1361 #undef OUTPUT_DEBUG_RESULT
1364 gcc_cp_plugin::build_decl (const char *debug_decltype
, const char *name
,
1365 enum gcc_cp_symbol_kind sym_kind
, gcc_type sym_type
,
1366 const char *substitution_name
, gcc_address address
,
1367 const char *filename
, unsigned int line_number
)
1369 if (debug_compile_cplus_types
)
1370 fprintf_unfiltered (gdb_stdlog
, "<%s> ", debug_decltype
);
1372 return build_decl (name
, sym_kind
, sym_type
, substitution_name
,
1373 address
, filename
, line_number
);
1377 gcc_cp_plugin::start_class_type (const char *debug_name
, gcc_decl typedecl
,
1378 const struct gcc_vbase_array
*base_classes
,
1379 const char *filename
, unsigned int line_number
)
1381 if (debug_compile_cplus_types
)
1382 fprintf_unfiltered (gdb_stdlog
, "<%s> ", debug_name
);
1384 return start_class_type (typedecl
, base_classes
, filename
, line_number
);
1388 gcc_cp_plugin::finish_class_type (const char *debug_name
,
1389 unsigned long size_in_bytes
)
1391 if (debug_compile_cplus_types
)
1392 fprintf_unfiltered (gdb_stdlog
, "<%s> ", debug_name
);
1394 return finish_class_type (size_in_bytes
);
1398 gcc_cp_plugin::pop_binding_level (const char *debug_name
)
1400 if (debug_compile_cplus_types
)
1401 fprintf_unfiltered (gdb_stdlog
, "<%s> ", debug_name
);
1403 return pop_binding_level ();
1407 _initialize_compile_cplus_types ()
1409 add_setshow_boolean_cmd ("compile-cplus-types", no_class
,
1410 &debug_compile_cplus_types
, _("\
1411 Set debugging of C++ compile type conversion."), _("\
1412 Show debugging of C++ compile type conversion."), _("\
1413 When enabled debugging messages are printed during C++ type conversion for\n\
1414 the compile commands."),
1420 add_setshow_boolean_cmd ("compile-cplus-scopes", no_class
,
1421 &debug_compile_cplus_scopes
, _("\
1422 Set debugging of C++ compile scopes."), _("\
1423 Show debugging of C++ compile scopes."), _("\
1424 When enabled debugging messages are printed about definition scopes during\n\
1425 C++ type conversion for the compile commands."),