2 SPDX-License-Identifier: GPL-2.0-only
4 Copyright (C) 2006 Mandriva Conectiva S.A.
5 Copyright (C) 2006 Arnaldo Carvalho de Melo <acme@mandriva.com>
6 Copyright (C) 2007 Red Hat Inc.
7 Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
13 #include "dwarves_emit.h"
16 void type_emissions__init(struct type_emissions
*emissions
, struct conf_fprintf
*conf_fprintf
)
18 INIT_LIST_HEAD(&emissions
->base_type_definitions
);
19 INIT_LIST_HEAD(&emissions
->definitions
);
20 INIT_LIST_HEAD(&emissions
->fwd_decls
);
21 emissions
->conf_fprintf
= conf_fprintf
;
24 static void type_emissions__add_definition(struct type_emissions
*emissions
,
27 type
->definition_emitted
= 1;
28 if (!list_empty(&type
->node
))
29 list_del(&type
->node
);
30 list_add_tail(&type
->node
, &emissions
->definitions
);
33 static void type_emissions__add_fwd_decl(struct type_emissions
*emissions
,
36 type
->fwd_decl_emitted
= 1;
37 if (list_empty(&type
->node
))
38 list_add_tail(&type
->node
, &emissions
->fwd_decls
);
41 struct type
*type_emissions__find_definition(const struct type_emissions
*emissions
,
42 uint16_t tag
, const char *name
)
49 list_for_each_entry(pos
, &emissions
->definitions
, node
)
50 if (type__tag(pos
)->tag
== tag
&&
51 type__name(pos
) != NULL
&&
52 strcmp(type__name(pos
), name
) == 0)
58 static bool type__can_have_shadow_definition(struct type
*type
)
60 struct tag
*tag
= type__tag(type
);
62 return tag__is_struct(tag
) || tag__is_union(tag
) || tag__is_enumeration(tag
);
65 // Find if 'struct foo' is defined with a pre-existing 'enum foo', 'union foo', etc
66 struct type
*type_emissions__find_shadow_definition(const struct type_emissions
*emissions
,
67 uint16_t tag
, const char *name
)
74 list_for_each_entry(pos
, &emissions
->definitions
, node
) {
75 if (type__tag(pos
)->tag
!= tag
&&
76 type__name(pos
) != NULL
&&
77 type__can_have_shadow_definition(pos
) &&
78 strcmp(type__name(pos
), name
) == 0)
85 static struct type
*type_emissions__find_fwd_decl(const struct type_emissions
*emissions
,
93 list_for_each_entry(pos
, &emissions
->fwd_decls
, node
) {
94 const char *curr_name
= type__name(pos
);
96 if (curr_name
&& strcmp(curr_name
, name
) == 0)
103 static int enumeration__emit_definitions(struct tag
*tag
,
104 struct type_emissions
*emissions
,
105 const struct conf_fprintf
*conf
,
108 struct type
*etype
= tag__type(tag
);
110 /* Have we already emitted this in this CU? */
111 if (etype
->definition_emitted
)
114 /* Ok, lets look at the previous CUs: */
115 if (type_emissions__find_definition(emissions
, DW_TAG_enumeration_type
, type__name(etype
)) != NULL
) {
117 * Yes, so lets mark it visited on this CU too,
118 * to speed up the lookup.
120 etype
->definition_emitted
= 1;
124 enumeration__fprintf(tag
, conf
, fp
);
127 // See comment on enumeration__fprintf(), it seems this happens with DWARF as well
128 // or BTF doesn't have type->declaration set because DWARF didn't have it set.
129 // But we consider type->nr_members == 0 as just a forward declaration, so don't
130 // mark it as defined because we may need it to __really__ printf it later.
131 if (etype
->nr_members
!= 0)
132 type_emissions__add_definition(emissions
, etype
);
136 static int tag__emit_definitions(struct tag
*tag
, struct cu
*cu
,
137 struct type_emissions
*emissions
, FILE *fp
);
139 static int typedef__emit_definitions(struct tag
*tdef
, struct cu
*cu
,
140 struct type_emissions
*emissions
, FILE *fp
)
142 struct type
*def
= tag__type(tdef
);
143 struct tag
*type
, *ptr_type
;
145 /* Have we already emitted this in this CU? */
146 if (def
->definition_emitted
)
149 /* Ok, lets look at the previous CUs: */
150 if (type_emissions__find_definition(emissions
, DW_TAG_typedef
, type__name(def
)) != NULL
) {
152 * Yes, so lets mark it visited on this CU too,
153 * to speed up the lookup.
155 def
->definition_emitted
= 1;
159 type
= cu__type(cu
, tdef
->type
);
160 if (type
== NULL
) // void
164 case DW_TAG_atomic_type
:
165 type
= cu__type(cu
, tdef
->type
);
167 tag__emit_definitions(type
, cu
, emissions
, fp
);
169 fprintf(stderr
, "%s: couldn't find the type pointed from _Atomic for '%s'\n", __func__
, type__name(def
));
171 case DW_TAG_array_type
:
172 tag__emit_definitions(type
, cu
, emissions
, fp
);
175 typedef__emit_definitions(type
, cu
, emissions
, fp
);
177 case DW_TAG_pointer_type
:
178 ptr_type
= cu__type(cu
, type
->type
);
179 /* void ** can make ptr_type be NULL */
180 if (ptr_type
== NULL
)
182 if (ptr_type
->tag
== DW_TAG_typedef
) {
183 typedef__emit_definitions(ptr_type
, cu
, emissions
, fp
);
185 } else if (ptr_type
->tag
!= DW_TAG_subroutine_type
)
189 case DW_TAG_subroutine_type
:
190 ftype__emit_definitions(tag__ftype(type
), cu
, emissions
, fp
);
192 case DW_TAG_enumeration_type
: {
193 struct type
*ctype
= tag__type(type
);
194 struct conf_fprintf conf
= {
198 if (type__name(ctype
) == NULL
) {
199 fputs("typedef ", fp
);
200 conf
.suffix
= type__name(def
);
201 enumeration__emit_definitions(type
, emissions
, &conf
, fp
);
204 enumeration__emit_definitions(type
, emissions
, &conf
, fp
);
207 case DW_TAG_structure_type
:
208 case DW_TAG_union_type
: {
209 struct type
*ctype
= tag__type(type
);
211 if (type__name(ctype
) == NULL
) {
212 type__emit_definitions(type__tag(ctype
), cu
, emissions
, fp
);
213 type__emit(type__tag(ctype
), cu
, "typedef", type__name(def
), fp
);
215 } else if (type__emit_definitions(type
, cu
, emissions
, fp
))
216 type__emit(type
, cu
, NULL
, NULL
, fp
);
221 * Recheck if the typedef was emitted, as there are cases, like
222 * wait_queue_t in the Linux kernel, that is against struct
223 * __wait_queue, that has a wait_queue_func_t member, a function
224 * typedef that has as one of its parameters a... wait_queue_t, that
225 * will thus be emitted before the function typedef, making a no go to
226 * redefine the typedef after struct __wait_queue.
229 if (!def
->definition_emitted
) {
230 typedef__fprintf(tdef
, cu
, NULL
, fp
);
234 type_emissions__add_definition(emissions
, def
);
238 static int type__emit_fwd_decl(struct type
*ctype
, struct type_emissions
*emissions
, FILE *fp
)
240 /* Have we already emitted this in this CU? */
241 if (ctype
->fwd_decl_emitted
)
244 const char *name
= type__name(ctype
);
248 /* Ok, lets look at the previous CUs: */
249 if (type_emissions__find_fwd_decl(emissions
, name
) != NULL
) {
251 * Yes, so lets mark it visited on this CU too,
252 * to speed up the lookup.
254 ctype
->fwd_decl_emitted
= 1;
258 fprintf(fp
, "%s %s;\n",
259 tag__is_union(&ctype
->namespace.tag
) ? "union" : "struct",
261 type_emissions__add_fwd_decl(emissions
, ctype
);
265 static struct base_type
*base_type_emissions__find_definition(const struct type_emissions
*emissions
, const char *name
)
267 struct base_type
*pos
;
272 list_for_each_entry(pos
, &emissions
->base_type_definitions
, node
)
273 if (strcmp(__base_type__name(pos
), name
) == 0)
279 static void base_type_emissions__add_definition(struct type_emissions
*emissions
, struct base_type
*type
)
281 type
->definition_emitted
= 1;
282 if (!list_empty(&type
->node
))
283 list_del(&type
->node
);
284 list_add_tail(&type
->node
, &emissions
->base_type_definitions
);
287 static const char *base_type__stdint2simple(const char *name
)
289 if (strcmp(name
, "int32_t") == 0)
291 if (strcmp(name
, "int16_t") == 0)
293 if (strcmp(name
, "int8_t") == 0)
295 if (strcmp(name
, "int64_t") == 0)
300 static int base_type__emit_definitions(struct base_type
*type
, struct type_emissions
*emissions
, FILE *fp
)
302 #define base_type__prefix "atomic_"
303 const size_t prefixlen
= sizeof(base_type__prefix
) - 1;
304 const char *name
= __base_type__name(type
);
306 // See if it was already emitted in this CU
307 if (type
->definition_emitted
)
310 // We're only emitting for "atomic_" prefixed base types
311 if (strncmp(name
, base_type__prefix
, prefixlen
) != 0)
314 // See if it was already emitted in another CU
315 if (base_type_emissions__find_definition(emissions
, name
)) {
316 type
->definition_emitted
= 1;
320 const char *non_atomic_name
= name
+ prefixlen
;
322 fputs("typedef _Atomic", fp
);
324 if (non_atomic_name
[0] == 's' &&
325 non_atomic_name
[1] != 'i' && non_atomic_name
[1] != 'h') // exclude atomic_size_t and atomic_short
326 fprintf(fp
, " signed %s", non_atomic_name
+ 1);
327 else if (non_atomic_name
[0] == 'l' && non_atomic_name
[1] == 'l')
328 fprintf(fp
, " long long");
329 else if (non_atomic_name
[0] == 'u') {
330 fprintf(fp
, " unsigned");
331 if (non_atomic_name
[1] == 'l') {
332 fprintf(fp
, " long");
333 if (non_atomic_name
[2] == 'l')
334 fprintf(fp
, " long");
336 fprintf(fp
, " %s", base_type__stdint2simple(non_atomic_name
+ 1));
337 } else if (non_atomic_name
[0] == 'b')
338 fprintf(fp
, " _Bool");
340 fprintf(fp
, " %s", base_type__stdint2simple(non_atomic_name
));
342 fprintf(fp
, " %s;\n", name
);
344 base_type_emissions__add_definition(emissions
, type
);
347 #undef base_type__prefix
350 static int tag__emit_definitions(struct tag
*tag
, struct cu
*cu
,
351 struct type_emissions
*emissions
, FILE *fp
)
353 struct tag
*type
= cu__type(cu
, tag
->type
);
360 case DW_TAG_base_type
:
361 if (emissions
->conf_fprintf
&& emissions
->conf_fprintf
->skip_emitting_atomic_typedefs
)
363 return base_type__emit_definitions(tag__base_type(type
), emissions
, fp
);
364 case DW_TAG_pointer_type
:
365 case DW_TAG_reference_type
:
368 case DW_TAG_array_type
:
369 case DW_TAG_const_type
:
370 case DW_TAG_volatile_type
:
371 case DW_TAG_atomic_type
:
372 type
= cu__type(cu
, type
->type
);
375 goto next_indirection
;
377 return typedef__emit_definitions(type
, cu
, emissions
, fp
);
378 case DW_TAG_enumeration_type
:
379 if (type__name(tag__type(type
)) != NULL
) {
380 struct conf_fprintf conf
= {
383 return enumeration__emit_definitions(type
, emissions
, &conf
, fp
);
386 case DW_TAG_structure_type
:
387 case DW_TAG_union_type
:
390 * Struct defined inline, no name, need to have its
391 * members types emitted.
393 if (type__name(tag__type(type
)) == NULL
)
394 type__emit_definitions(type
, cu
, emissions
, fp
);
396 return type__emit_fwd_decl(tag__type(type
), emissions
, fp
);
398 if (type__emit_definitions(type
, cu
, emissions
, fp
))
399 type__emit(type
, cu
, NULL
, NULL
, fp
);
401 case DW_TAG_subroutine_type
:
402 return ftype__emit_definitions(tag__ftype(type
), cu
,
409 int ftype__emit_definitions(struct ftype
*ftype
, struct cu
*cu
,
410 struct type_emissions
*emissions
, FILE *fp
)
412 struct parameter
*pos
;
413 /* First check the function return type */
414 int printed
= tag__emit_definitions(&ftype
->tag
, cu
, emissions
, fp
);
416 /* Then its parameters */
417 list_for_each_entry(pos
, &ftype
->parms
, tag
.node
)
418 if (tag__emit_definitions(&pos
->tag
, cu
, emissions
, fp
))
426 int type__emit_definitions(struct tag
*tag
, struct cu
*cu
,
427 struct type_emissions
*emissions
, FILE *fp
)
429 struct type
*ctype
= tag__type(tag
);
430 struct class_member
*pos
;
432 if (ctype
->definition_emitted
)
435 /* Ok, lets look at the previous CUs: */
436 if (type_emissions__find_definition(emissions
, tag
->tag
, type__name(ctype
)) != NULL
) {
437 ctype
->definition_emitted
= 1;
441 if (tag__is_typedef(tag
))
442 return typedef__emit_definitions(tag
, cu
, emissions
, fp
);
445 * vmlinux.h:120298:8: error: ‘irte’ defined as wrong kind of tag
447 * If we have a 'struct foo' and we then find a 'union foo', which happens
448 * twice in the Linux kernel, for instance, then we need to disambiguate by
449 * adding a suffix to the second type with the same name.
451 * That is the strategy used in:
453 * btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h
455 if (type__can_have_shadow_definition(ctype
)) {
456 if (type_emissions__find_shadow_definition(emissions
, tag
->tag
, type__name(ctype
))) {
457 ctype
->suffix_disambiguation
= 1;
459 char *disambiguated_name
;
461 if (asprintf(&disambiguated_name
, "%s__%u", type__name(ctype
), ctype
->suffix_disambiguation
) == -1) {
462 fprintf(stderr
, "emit: Not enough memory to allocate disambiguated type name for '%s'\n",
465 // Will be deleted in type__delete() on noticing ctype->suffix_disambiguation != 0
466 tag__namespace(tag
)->name
= disambiguated_name
;
468 // Now look again if it was emitted in a previous CU with the disambiguated name
469 if (type_emissions__find_definition(emissions
, tag
->tag
, type__name(ctype
)) != NULL
) {
470 ctype
->definition_emitted
= 1;
478 type_emissions__add_definition(emissions
, ctype
);
480 type__check_structs_at_unnatural_alignments(ctype
, cu
);
482 type__for_each_member(ctype
, pos
)
483 if (tag__emit_definitions(&pos
->tag
, cu
, emissions
, fp
))
489 void type__emit(struct tag
*tag
, struct cu
*cu
,
490 const char *prefix
, const char *suffix
, FILE *fp
)
492 struct type
*ctype
= tag__type(tag
);
494 if (type__name(ctype
) != NULL
||
495 suffix
!= NULL
|| prefix
!= NULL
) {
496 struct conf_fprintf conf
= {
501 tag__fprintf(tag
, cu
, &conf
, fp
);