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