pahole: Describe expected use of 'default' in the man page
[dwarves.git] / dwarves_emit.c
blob01b33b7ec41eb947fe1fb7c5e9c52fadd3057321
1 /*
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>
8 */
10 #include <string.h>
12 #include "list.h"
13 #include "dwarves_emit.h"
14 #include "dwarves.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,
25 struct type *type)
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,
34 struct type *type)
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)
44 struct type *pos;
46 if (name == NULL)
47 return NULL;
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)
53 return pos;
55 return NULL;
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)
69 struct type *pos;
71 if (name == NULL)
72 return NULL;
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)
79 return pos;
82 return NULL;
85 static struct type *type_emissions__find_fwd_decl(const struct type_emissions *emissions,
86 const char *name)
88 struct type *pos;
90 if (name == NULL)
91 return NULL;
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)
97 return pos;
100 return NULL;
103 static int enumeration__emit_definitions(struct tag *tag,
104 struct type_emissions *emissions,
105 const struct conf_fprintf *conf,
106 FILE *fp)
108 struct type *etype = tag__type(tag);
110 /* Have we already emitted this in this CU? */
111 if (etype->definition_emitted)
112 return 0;
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;
121 return 0;
124 enumeration__fprintf(tag, conf, fp);
125 fputs(";\n", 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);
133 return 1;
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)
147 return 0;
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;
156 return 0;
159 type = cu__type(cu, tdef->type);
160 if (type == NULL) // void
161 goto emit;
163 switch (type->tag) {
164 case DW_TAG_atomic_type:
165 type = cu__type(cu, tdef->type);
166 if (type)
167 tag__emit_definitions(type, cu, emissions, fp);
168 else
169 fprintf(stderr, "%s: couldn't find the type pointed from _Atomic for '%s'\n", __func__, type__name(def));
170 break;
171 case DW_TAG_array_type:
172 tag__emit_definitions(type, cu, emissions, fp);
173 break;
174 case DW_TAG_typedef:
175 typedef__emit_definitions(type, cu, emissions, fp);
176 break;
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)
181 break;
182 if (ptr_type->tag == DW_TAG_typedef) {
183 typedef__emit_definitions(ptr_type, cu, emissions, fp);
184 break;
185 } else if (ptr_type->tag != DW_TAG_subroutine_type)
186 break;
187 type = ptr_type;
188 /* Fall thru */
189 case DW_TAG_subroutine_type:
190 ftype__emit_definitions(tag__ftype(type), cu, emissions, fp);
191 break;
192 case DW_TAG_enumeration_type: {
193 struct type *ctype = tag__type(type);
194 struct conf_fprintf conf = {
195 .suffix = NULL,
198 if (type__name(ctype) == NULL) {
199 fputs("typedef ", fp);
200 conf.suffix = type__name(def);
201 enumeration__emit_definitions(type, emissions, &conf, fp);
202 goto out;
203 } else
204 enumeration__emit_definitions(type, emissions, &conf, fp);
206 break;
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);
214 goto out;
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.
228 emit:
229 if (!def->definition_emitted) {
230 typedef__fprintf(tdef, cu, NULL, fp);
231 fputs(";\n", fp);
233 out:
234 type_emissions__add_definition(emissions, def);
235 return 1;
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)
242 return 0;
244 const char *name = type__name(ctype);
245 if (name == NULL)
246 return 0;
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;
255 return 0;
258 fprintf(fp, "%s %s;\n",
259 tag__is_union(&ctype->namespace.tag) ? "union" : "struct",
260 type__name(ctype));
261 type_emissions__add_fwd_decl(emissions, ctype);
262 return 1;
265 static struct base_type *base_type_emissions__find_definition(const struct type_emissions *emissions, const char *name)
267 struct base_type *pos;
269 if (name == NULL)
270 return NULL;
272 list_for_each_entry(pos, &emissions->base_type_definitions, node)
273 if (strcmp(__base_type__name(pos), name) == 0)
274 return pos;
276 return NULL;
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)
290 return "int";
291 if (strcmp(name, "int16_t") == 0)
292 return "short";
293 if (strcmp(name, "int8_t") == 0)
294 return "char";
295 if (strcmp(name, "int64_t") == 0)
296 return "long";
297 return name;
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)
308 return 0;
310 // We're only emitting for "atomic_" prefixed base types
311 if (strncmp(name, base_type__prefix, prefixlen) != 0)
312 return 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;
317 return 0;
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");
335 } else
336 fprintf(fp, " %s", base_type__stdint2simple(non_atomic_name + 1));
337 } else if (non_atomic_name[0] == 'b')
338 fprintf(fp, " _Bool");
339 else
340 fprintf(fp, " %s", base_type__stdint2simple(non_atomic_name));
342 fprintf(fp, " %s;\n", name);
344 base_type_emissions__add_definition(emissions, type);
345 return 1;
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);
354 int pointer = 0;
356 if (type == NULL)
357 return 0;
358 next_indirection:
359 switch (type->tag) {
360 case DW_TAG_base_type:
361 if (emissions->conf_fprintf && emissions->conf_fprintf->skip_emitting_atomic_typedefs)
362 return 0;
363 return base_type__emit_definitions(tag__base_type(type), emissions, fp);
364 case DW_TAG_pointer_type:
365 case DW_TAG_reference_type:
366 pointer = 1;
367 /* Fall thru */
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);
373 if (type == NULL)
374 return 0;
375 goto next_indirection;
376 case DW_TAG_typedef:
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 = {
381 .suffix = NULL,
383 return enumeration__emit_definitions(type, emissions, &conf, fp);
385 break;
386 case DW_TAG_structure_type:
387 case DW_TAG_union_type:
388 if (pointer) {
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);
400 return 1;
401 case DW_TAG_subroutine_type:
402 return ftype__emit_definitions(tag__ftype(type), cu,
403 emissions, fp);
406 return 0;
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))
419 printed = 1;
421 if (printed)
422 fputc('\n', fp);
423 return printed;
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)
433 return 0;
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;
438 return 0;
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",
463 type__name(ctype));
464 } else {
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;
471 return 0;
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))
484 fputc('\n', fp);
486 return 1;
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 = {
497 .prefix = prefix,
498 .suffix = suffix,
499 .emit_stats = 1,
501 tag__fprintf(tag, cu, &conf, fp);
502 fputc('\n', fp);