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