Debian Codegraph @

libdrgn/language_c.c

raw
1 // Copyright (c) Meta Platforms, Inc. and affiliates.
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3
4 #include <assert.h>
5 #include <ctype.h>
6 #include <float.h>
7 #include <inttypes.h>
8 #include <limits.h>
9 #include <math.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "array.h"
14 #include "bitops.h"
15 #include "c_lexer.h"
16 #include "error.h"
17 #include "language.h" // IWYU pragma: associated
18 #include "lexer.h"
19 #include "minmax.h"
20 #include "object.h"
21 #include "program.h"
22 #include "string_builder.h"
23 #include "symbol.h"
24 #include "type.h"
25 #include "util.h"
26 #include "vector.h"
27
28 static struct drgn_error *
29 c_declare_variable(struct drgn_qualified_type qualified_type,
30 struct string_callback *name, size_t indent,
31 bool define_anonymous_type, struct string_builder *sb);
32
33 static struct drgn_error *
34 c_define_type(struct drgn_qualified_type qualified_type, size_t indent,
35 struct string_builder *sb);
36
37 static bool append_tabs(int n, struct string_builder *sb)
38 {
39 while (n-- > 0) {
40 if (!string_builder_appendc(sb, '\t'))
41 return false;
42 }
43 return true;
44 }
45
46 static struct drgn_error *c_variable_name(struct string_callback *name,
47 void *arg, struct string_builder *sb)
48 {
49 if (!string_builder_append(sb, arg))
50 return &drgn_enomem;
51 return NULL;
52 }
53
54 static struct drgn_error *c_append_qualifiers(enum drgn_qualifiers qualifiers,
55 struct string_builder *sb)
56 {
57 static const char *qualifier_names[] = {
58 "const", "volatile", "restrict", "_Atomic",
59 };
60 bool first = true;
61 unsigned int i;
62
63 static_assert((1 << array_size(qualifier_names)) - 1 ==
64 DRGN_ALL_QUALIFIERS, "missing C qualifier name");
65
66 for (i = 0; (1U << i) & DRGN_ALL_QUALIFIERS; i++) {
67 if (!(qualifiers & (1U << i)))
68 continue;
69 if (!first) {
70 if (!string_builder_appendc(sb, ' '))
71 return &drgn_enomem;
72 }
73 if (!string_builder_append(sb, qualifier_names[i]))
74 return &drgn_enomem;
75 first = false;
76 }
77 return NULL;
78 }
79
80 static struct drgn_error *
81 c_declare_basic(struct drgn_qualified_type qualified_type,
82 struct string_callback *name, size_t indent,
83 struct string_builder *sb)
84 {
85 struct drgn_error *err;
86
87 if (!append_tabs(indent, sb))
88 return &drgn_enomem;
89 if (qualified_type.qualifiers) {
90 err = c_append_qualifiers(qualified_type.qualifiers, sb);
91 if (err)
92 return err;
93 if (!string_builder_appendc(sb, ' '))
94 return &drgn_enomem;
95 }
96 if (!string_builder_append(sb,
97 drgn_type_kind(qualified_type.type) == DRGN_TYPE_VOID ?
98 "void" : drgn_type_name(qualified_type.type)))
99 return &drgn_enomem;
100 if (name) {
101 if (!string_builder_appendc(sb, ' '))
102 return &drgn_enomem;
103 err = string_callback_call(name, sb);
104 if (err)
105 return err;
106 }
107 return NULL;
108 }
109
110 static struct drgn_error *
111 c_append_tagged_name(struct drgn_qualified_type qualified_type, size_t indent,
112 bool need_keyword, struct string_builder *sb)
113 {
114 struct drgn_error *err;
115
116 const char *keyword;
117 switch (drgn_type_kind(qualified_type.type)) {
118 case DRGN_TYPE_STRUCT:
119 keyword = "struct";
120 break;
121 case DRGN_TYPE_UNION:
122 keyword = "union";
123 break;
124 case DRGN_TYPE_CLASS:
125 keyword = "class";
126 break;
127 case DRGN_TYPE_ENUM:
128 keyword = "enum";
129 break;
130 default:
131 UNREACHABLE();
132 }
133
134 const char *tag = drgn_type_tag(qualified_type.type);
135 if (!need_keyword
136 && (!tag
137 || drgn_type_language(qualified_type.type)
138 != &drgn_language_cpp))
139 need_keyword = true;
140
141 if (!append_tabs(indent, sb))
142 return &drgn_enomem;
143 if (qualified_type.qualifiers) {
144 err = c_append_qualifiers(qualified_type.qualifiers, sb);
145 if (err)
146 return err;
147 if (!string_builder_appendc(sb, ' '))
148 return &drgn_enomem;
149 }
150 if (need_keyword && !string_builder_append(sb, keyword))
151 return &drgn_enomem;
152
153 if (tag) {
154 if ((need_keyword && !string_builder_appendc(sb, ' ')) ||
155 !string_builder_append(sb, tag))
156 return &drgn_enomem;
157 }
158
159 return NULL;
160 }
161
162 static struct drgn_error *
163 c_declare_tagged(struct drgn_qualified_type qualified_type,
164 struct string_callback *name, size_t indent,
165 bool define_anonymous_type, struct string_builder *sb)
166 {
167 struct drgn_error *err;
168
169 bool anonymous = drgn_type_is_anonymous(qualified_type.type);
170 if (anonymous && define_anonymous_type)
171 err = c_define_type(qualified_type, indent, sb);
172 else
173 err = c_append_tagged_name(qualified_type, indent, false, sb);
174 if (err)
175 return err;
176 if (anonymous && !define_anonymous_type &&
177 !string_builder_append(sb, " <anonymous>"))
178 return &drgn_enomem;
179
180 if (name) {
181 if (!string_builder_appendc(sb, ' '))
182 return &drgn_enomem;
183 err = string_callback_call(name, sb);
184 if (err)
185 return err;
186 }
187 return NULL;
188 }
189
190 static struct drgn_error *c_pointer_name(struct string_callback *name,
191 void *arg, struct string_builder *sb)
192 {
193 struct drgn_error *err;
194 struct drgn_qualified_type *qualified_type = arg;
195 struct drgn_qualified_type referenced_type;
196 enum drgn_type_kind referenced_kind;
197 bool parenthesize;
198
199 referenced_type = drgn_type_type(qualified_type->type);
200 referenced_kind = drgn_type_kind(referenced_type.type);
201 parenthesize = (referenced_kind == DRGN_TYPE_ARRAY ||
202 referenced_kind == DRGN_TYPE_FUNCTION);
203 if (parenthesize && !string_builder_appendc(sb, '('))
204 return &drgn_enomem;
205
206 if (!string_builder_appendc(sb, '*'))
207 return &drgn_enomem;
208 if (qualified_type->qualifiers) {
209 if (!string_builder_appendc(sb, ' '))
210 return &drgn_enomem;
211 err = c_append_qualifiers(qualified_type->qualifiers, sb);
212 if (err)
213 return err;
214 if (name) {
215 if (!string_builder_appendc(sb, ' '))
216 return &drgn_enomem;
217 }
218 }
219
220 err = string_callback_call(name, sb);
221 if (err)
222 return err;
223
224 if (parenthesize && !string_builder_appendc(sb, ')'))
225 return &drgn_enomem;
226 return NULL;
227 }
228
229 static struct drgn_error *
230 c_declare_pointer(struct drgn_qualified_type qualified_type,
231 struct string_callback *name, size_t indent,
232 struct string_builder *sb)
233 {
234 struct string_callback pointer_name = {
235 .fn = c_pointer_name,
236 .str = name,
237 .arg = &qualified_type,
238 };
239 struct drgn_qualified_type referenced_type;
240
241 referenced_type = drgn_type_type(qualified_type.type);
242 return c_declare_variable(referenced_type, &pointer_name, indent, false,
243 sb);
244 }
245
246 static struct drgn_error *c_array_name(struct string_callback *name, void *arg,
247 struct string_builder *sb)
248 {
249 struct drgn_error *err;
250 struct drgn_qualified_type *qualified_type = arg;
251
252 err = string_callback_call(name, sb);
253 if (err)
254 return err;
255
256 if (drgn_type_is_complete(qualified_type->type)) {
257 uint64_t length = drgn_type_length(qualified_type->type);
258
259 if (!string_builder_appendf(sb, "[%" PRIu64 "]", length))
260 return &drgn_enomem;
261 } else {
262 if (!string_builder_append(sb, "[]"))
263 return &drgn_enomem;
264 }
265 return NULL;
266 }
267
268 static struct drgn_error *
269 c_declare_array(struct drgn_qualified_type qualified_type,
270 struct string_callback *name, size_t indent,
271 struct string_builder *sb)
272 {
273 struct string_callback array_name = {
274 .fn = c_array_name,
275 .str = name,
276 .arg = &qualified_type,
277 };
278 struct drgn_qualified_type element_type;
279
280 element_type = drgn_type_type(qualified_type.type);
281 return c_declare_variable(element_type, &array_name, indent, false, sb);
282 }
283
284 static struct drgn_error *
285 c_declare_function(struct drgn_qualified_type qualified_type,
286 struct string_callback *name, size_t indent,
287 struct string_builder *sb)
288 {
289 struct drgn_error *err;
290 struct drgn_type_parameter *parameters;
291 size_t num_parameters, i;
292 struct drgn_qualified_type return_type;
293
294 if (!name) {
295 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
296 "function must have name");
297 }
298
299 parameters = drgn_type_parameters(qualified_type.type);
300 num_parameters = drgn_type_num_parameters(qualified_type.type);
301
302 return_type = drgn_type_type(qualified_type.type);
303 err = c_declare_variable(return_type, name, indent, false, sb);
304 if (err)
305 return err;
306
307 if (!string_builder_appendc(sb, '('))
308 return &drgn_enomem;
309
310 for (i = 0; i < num_parameters; i++) {
311 const char *parameter_name = parameters[i].name;
312 struct drgn_qualified_type parameter_type;
313 struct string_callback name_cb = {
314 .fn = c_variable_name,
315 .arg = (void *)parameter_name,
316 };
317
318 err = drgn_parameter_type(&parameters[i], &parameter_type);
319 if (err)
320 return err;
321
322 if (i > 0) {
323 if (!string_builder_append(sb, ", "))
324 return &drgn_enomem;
325 }
326 err = c_declare_variable(parameter_type,
327 parameter_name && parameter_name[0] ?
328 &name_cb : NULL, 0, false, sb);
329 if (err)
330 return err;
331 }
332 if (num_parameters && drgn_type_is_variadic(qualified_type.type)) {
333 if (!string_builder_append(sb, ", ..."))
334 return &drgn_enomem;
335 } else if (!num_parameters &&
336 !drgn_type_is_variadic(qualified_type.type)) {
337 if (!string_builder_append(sb, "void"))
338 return &drgn_enomem;
339 }
340
341 if (!string_builder_appendc(sb, ')'))
342 return &drgn_enomem;
343 return NULL;
344 }
345
346 static struct drgn_error *
347 c_declare_variable(struct drgn_qualified_type qualified_type,
348 struct string_callback *name, size_t indent,
349 bool define_anonymous_type, struct string_builder *sb)
350 {
351 SWITCH_ENUM(drgn_type_kind(qualified_type.type)) {
352 case DRGN_TYPE_VOID:
353 case DRGN_TYPE_INT:
354 case DRGN_TYPE_BOOL:
355 case DRGN_TYPE_FLOAT:
356 case DRGN_TYPE_TYPEDEF:
357 return c_declare_basic(qualified_type, name, indent, sb);
358 case DRGN_TYPE_STRUCT:
359 case DRGN_TYPE_UNION:
360 case DRGN_TYPE_CLASS:
361 case DRGN_TYPE_ENUM:
362 return c_declare_tagged(qualified_type, name, indent,
363 define_anonymous_type, sb);
364 case DRGN_TYPE_POINTER:
365 return c_declare_pointer(qualified_type, name, indent, sb);
366 case DRGN_TYPE_ARRAY:
367 return c_declare_array(qualified_type, name, indent, sb);
368 case DRGN_TYPE_FUNCTION:
369 return c_declare_function(qualified_type, name, indent, sb);
370 default:
371 UNREACHABLE();
372 }
373 }
374
375 static struct drgn_error *
376 c_define_compound(struct drgn_qualified_type qualified_type, size_t indent,
377 struct string_builder *sb)
378 {
379 struct drgn_error *err;
380 struct drgn_type_member *members;
381 size_t num_members, i;
382
383 if (!drgn_type_is_complete(qualified_type.type)) {
384 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
385 "cannot get definition of incomplete compound type");
386 }
387
388 members = drgn_type_members(qualified_type.type);
389 num_members = drgn_type_num_members(qualified_type.type);
390
391 err = c_append_tagged_name(qualified_type, indent, true, sb);
392 if (err)
393 return err;
394 if (!string_builder_append(sb, " {\n"))
395 return &drgn_enomem;
396
397 for (i = 0; i < num_members; i++) {
398 struct drgn_qualified_type member_type;
399 uint64_t member_bit_field_size;
400 err = drgn_member_type(&members[i], &member_type,
401 &member_bit_field_size);
402 if (err)
403 return err;
404
405 const char *member_name = members[i].name;
406 struct string_callback name_cb = {
407 .fn = c_variable_name,
408 .arg = (void *)member_name,
409 };
410 err = c_declare_variable(member_type,
411 member_name && member_name[0] ?
412 &name_cb : NULL, indent + 1, true, sb);
413 if (err)
414 return err;
415 if (member_bit_field_size &&
416 !string_builder_appendf(sb, " : %" PRIu64,
417 member_bit_field_size))
418 return &drgn_enomem;
419 if (!string_builder_append(sb, ";\n"))
420 return &drgn_enomem;
421 }
422
423 if (!append_tabs(indent, sb) || !string_builder_appendc(sb, '}'))
424 return &drgn_enomem;
425 return NULL;
426 }
427
428 static struct drgn_error *
429 c_define_enum(struct drgn_qualified_type qualified_type, size_t indent,
430 struct string_builder *sb)
431 {
432 struct drgn_error *err;
433 const struct drgn_type_enumerator *enumerators;
434 size_t num_enumerators, i;
435 bool is_signed;
436
437 if (!drgn_type_is_complete(qualified_type.type)) {
438 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
439 "cannot get definition of incomplete enum type");
440 }
441
442 enumerators = drgn_type_enumerators(qualified_type.type);
443 num_enumerators = drgn_type_num_enumerators(qualified_type.type);
444
445 err = c_append_tagged_name(qualified_type, indent, true, sb);
446 if (err)
447 return err;
448 if (!string_builder_append(sb, " {\n"))
449 return &drgn_enomem;
450
451 is_signed = drgn_enum_type_is_signed(qualified_type.type);
452 for (i = 0; i < num_enumerators; i++) {
453 if (!append_tabs(indent + 1, sb) ||
454 !string_builder_append(sb, enumerators[i].name) ||
455 !string_builder_append(sb, " = "))
456 return &drgn_enomem;
457 if (is_signed) {
458 if (!string_builder_appendf(sb, "%" PRId64 ",\n",
459 enumerators[i].svalue))
460 return &drgn_enomem;
461 } else {
462 if (!string_builder_appendf(sb, "%" PRIu64 ",\n",
463 enumerators[i].uvalue))
464 return &drgn_enomem;
465 }
466 }
467
468 if (!append_tabs(indent, sb) || !string_builder_appendc(sb, '}'))
469 return &drgn_enomem;
470 return NULL;
471 }
472
473 static struct drgn_error *
474 c_define_typedef(struct drgn_qualified_type qualified_type, size_t indent,
475 struct string_builder *sb)
476 {
477 struct string_callback typedef_name = {
478 .fn = c_variable_name,
479 .arg = (char *)drgn_type_name(qualified_type.type),
480 };
481 struct drgn_qualified_type aliased_type;
482 struct drgn_error *err;
483
484 if (!append_tabs(indent, sb))
485 return &drgn_enomem;
486 if (qualified_type.qualifiers) {
487 err = c_append_qualifiers(qualified_type.qualifiers, sb);
488 if (err)
489 return err;
490 if (!string_builder_appendc(sb, ' '))
491 return &drgn_enomem;
492 }
493 if (!string_builder_append(sb, "typedef "))
494 return &drgn_enomem;
495
496 aliased_type = drgn_type_type(qualified_type.type);
497 return c_declare_variable(aliased_type, &typedef_name, 0, true, sb);
498 }
499
500 static struct drgn_error *
501 c_define_type(struct drgn_qualified_type qualified_type, size_t indent,
502 struct string_builder *sb)
503 {
504 SWITCH_ENUM(drgn_type_kind(qualified_type.type)) {
505 case DRGN_TYPE_VOID:
506 case DRGN_TYPE_INT:
507 case DRGN_TYPE_BOOL:
508 case DRGN_TYPE_FLOAT:
509 return c_declare_basic(qualified_type, NULL, indent, sb);
510 case DRGN_TYPE_STRUCT:
511 case DRGN_TYPE_UNION:
512 case DRGN_TYPE_CLASS:
513 return c_define_compound(qualified_type, indent, sb);
514 case DRGN_TYPE_ENUM:
515 return c_define_enum(qualified_type, indent, sb);
516 case DRGN_TYPE_TYPEDEF:
517 return c_define_typedef(qualified_type, indent, sb);
518 case DRGN_TYPE_POINTER:
519 return c_declare_pointer(qualified_type, NULL, indent, sb);
520 case DRGN_TYPE_ARRAY:
521 return c_declare_array(qualified_type, NULL, indent, sb);
522 case DRGN_TYPE_FUNCTION: {
523 struct string_callback name_cb = {
524 .fn = c_variable_name,
525 .arg = (void *)"",
526 };
527 return c_declare_function(qualified_type, &name_cb, indent, sb);
528 }
529 default:
530 UNREACHABLE();
531 }
532 }
533
534 static struct drgn_error *
535 c_format_type_name_impl(struct drgn_qualified_type qualified_type,
536 struct string_builder *sb)
537 {
538 if (drgn_type_kind(qualified_type.type) == DRGN_TYPE_FUNCTION) {
539 struct string_callback name_cb = {
540 .fn = c_variable_name,
541 .arg = (void *)"",
542 };
543
544 return c_declare_function(qualified_type, &name_cb, 0, sb);
545 } else {
546 return c_declare_variable(qualified_type, NULL, 0, false, sb);
547 }
548 }
549
550 static struct drgn_error *
551 c_format_type_name(struct drgn_qualified_type qualified_type, char **ret)
552 {
553 struct drgn_error *err;
554 STRING_BUILDER(sb);
555 err = c_format_type_name_impl(qualified_type, &sb);
556 if (err)
557 return err;
558 if (!string_builder_null_terminate(&sb))
559 return &drgn_enomem;
560 *ret = string_builder_steal(&sb);
561 return NULL;
562 }
563
564 static struct drgn_error *
565 c_format_type(struct drgn_qualified_type qualified_type, char **ret)
566 {
567 struct drgn_error *err;
568 STRING_BUILDER(sb);
569 if (drgn_type_is_complete(qualified_type.type))
570 err = c_define_type(qualified_type, 0, &sb);
571 else
572 err = c_format_type_name_impl(qualified_type, &sb);
573 if (err)
574 return err;
575 if (!string_builder_null_terminate(&sb))
576 return &drgn_enomem;
577 *ret = string_builder_steal(&sb);
578 return NULL;
579 }
580
581 static struct drgn_error *
582 c_format_variable_declaration(struct drgn_qualified_type qualified_type,
583 const char *name, char **ret)
584 {
585 struct drgn_error *err;
586 STRING_BUILDER(sb);
587 struct string_callback name_callback = {
588 .fn = c_variable_name,
589 .arg = (char *)name,
590 };
591 err = c_declare_variable(qualified_type, &name_callback, 0, true, &sb);
592 if (err)
593 return err;
594 if (!string_builder_null_terminate(&sb))
595 return &drgn_enomem;
596 *ret = string_builder_steal(&sb);
597 return NULL;
598 }
599
600 static struct drgn_error *
601 c_format_object_impl(const struct drgn_object *obj, size_t indent,
602 size_t one_line_columns, size_t multi_line_columns,
603 const struct drgn_format_object_options *options,
604 struct string_builder *sb);
605
606 static bool is_character_type(struct drgn_type *type)
607 {
608 switch (drgn_type_primitive(type)) {
609 case DRGN_C_TYPE_CHAR:
610 case DRGN_C_TYPE_SIGNED_CHAR:
611 case DRGN_C_TYPE_UNSIGNED_CHAR:
612 return true;
613 default:
614 return false;
615 }
616 }
617
618 static struct drgn_error *
619 c_format_character(unsigned char c, bool escape_single_quote,
620 bool escape_double_quote, struct string_builder *sb)
621 {
622 bool ret;
623
624 switch (c) {
625 case '\0':
626 ret = string_builder_append(sb, "\\0");
627 break;
628 case '\a':
629 ret = string_builder_append(sb, "\\a");
630 break;
631 case '\b':
632 ret = string_builder_append(sb, "\\b");
633 break;
634 case '\t':
635 ret = string_builder_append(sb, "\\t");
636 break;
637 case '\n':
638 ret = string_builder_append(sb, "\\n");
639 break;
640 case '\v':
641 ret = string_builder_append(sb, "\\v");
642 break;
643 case '\f':
644 ret = string_builder_append(sb, "\\f");
645 break;
646 case '\r':
647 ret = string_builder_append(sb, "\\r");
648 break;
649 case '"':
650 if (!escape_double_quote)
651 goto no_escape;
652 ret = string_builder_append(sb, "\\\"");
653 break;
654 case '\'':
655 if (!escape_single_quote)
656 goto no_escape;
657 ret = string_builder_append(sb, "\\'");
658 break;
659 case '\\':
660 ret = string_builder_append(sb, "\\\\");
661 break;
662 default:
663 if (c <= '\x1f' || c >= '\x7f') {
664 ret = string_builder_appendf(sb, "\\x%02x", c);
665 } else {
666 no_escape:
667 ret = string_builder_appendc(sb, c);
668 }
669 break;
670 }
671 return ret ? NULL : &drgn_enomem;
672 }
673
674 static struct drgn_error *
675 c_format_string(struct drgn_program *prog, uint64_t address, uint64_t length,
676 struct string_builder *sb)
677 {
678 struct drgn_error *err;
679
680 if (!string_builder_appendc(sb, '"'))
681 return &drgn_enomem;
682 while (length) {
683 unsigned char c;
684 err = drgn_program_read_memory(prog, &c, address++, 1, false);
685 if (err)
686 return err;
687
688 if (c == '\0') {
689 break;
690 } else {
691 err = c_format_character(c, false, true, sb);
692 if (err)
693 return err;
694 }
695 length--;
696 }
697 if (!string_builder_appendc(sb, '"'))
698 return &drgn_enomem;
699 return NULL;
700 }
701
702 static struct drgn_error *
703 c_format_int_object(const struct drgn_object *obj,
704 const struct drgn_format_object_options *options,
705 struct string_builder *sb)
706 {
707 struct drgn_error *err;
708
709 if ((options->flags & DRGN_FORMAT_OBJECT_CHAR)
710 && is_character_type(obj->type)) {
711 union drgn_value value;
712
713 if (!string_builder_appendc(sb, '\''))
714 return &drgn_enomem;
715 err = drgn_object_read_integer(obj, &value);
716 if (err)
717 return err;
718 err = c_format_character(value.uvalue, true, false, sb);
719 if (err)
720 return err;
721 if (!string_builder_appendc(sb, '\''))
722 return &drgn_enomem;
723 return NULL;
724 }
725
726 union drgn_value value_mem;
727 const union drgn_value *value;
728 err = drgn_object_read_value(obj, &value_mem, &value);
729 if (err)
730 return err;
731 switch (obj->encoding) {
732 case DRGN_OBJECT_ENCODING_SIGNED:
733 switch (options->integer_base) {
734 case 10:
735 if (!string_builder_appendf(sb, "%" PRId64,
736 value->svalue)) {
737 err = &drgn_enomem;
738 goto out;
739 }
740 break;
741 case 16:
742 if (!string_builder_appendf(sb, "%s0x%" PRIx64,
743 value->svalue < 0
744 ? "-" : "",
745 value->svalue < 0
746 // Casting before negating
747 // is necessary to handle
748 // INT64_MIN.
749 ? -(uint64_t)value->svalue
750 : (uint64_t)value->svalue)) {
751 err = &drgn_enomem;
752 goto out;
753 }
754 break;
755 case 8:
756 if (!string_builder_appendf(sb, "%s%#" PRIo64,
757 value->svalue < 0
758 ? "-" : "",
759 value->svalue < 0
760 ? -(uint64_t)value->svalue
761 : (uint64_t)value->svalue)) {
762 err = &drgn_enomem;
763 goto out;
764 }
765 break;
766 default:
767 UNREACHABLE();
768 }
769 break;
770 case DRGN_OBJECT_ENCODING_UNSIGNED: {
771 const char *format;
772 switch (options->integer_base) {
773 case 10:
774 format = "%" PRIu64;
775 break;
776 case 16:
777 format = "0x%" PRIx64;
778 break;
779 case 8:
780 format = "%#" PRIo64;
781 break;
782 default:
783 UNREACHABLE();
784 }
785 if (!string_builder_appendf(sb, format, value->uvalue)) {
786 err = &drgn_enomem;
787 goto out;
788 }
789 break;
790 }
791 case DRGN_OBJECT_ENCODING_SIGNED_BIG:
792 case DRGN_OBJECT_ENCODING_UNSIGNED_BIG: {
793 if (!string_builder_append(sb, "0x")) {
794 err = &drgn_enomem;
795 goto out;
796 }
797 const uint8_t *buf = (uint8_t *)value->bufp;
798 size_t bytes = drgn_object_size(obj);
799 if (obj->little_endian) {
800 size_t i = bytes - 1;
801 while (i > 0 && buf[i] == 0)
802 i--;
803 if (!string_builder_appendf(sb, "%" PRIx8, buf[i])) {
804 err = &drgn_enomem;
805 goto out;
806 }
807 while (i-- > 0) {
808 if (!string_builder_appendf(sb, "%02" PRIx8, buf[i])) {
809 err = &drgn_enomem;
810 goto out;
811 }
812 }
813 } else {
814 size_t i = 0;
815 while (i < bytes - 1 && buf[i] == 0)
816 i++;
817 if (!string_builder_appendf(sb, "%" PRIx8, buf[i])) {
818 err = &drgn_enomem;
819 goto out;
820 }
821 while (++i < bytes) {
822 if (!string_builder_appendf(sb, "%02" PRIx8, buf[i])) {
823 err = &drgn_enomem;
824 goto out;
825 }
826 }
827 }
828 break;
829 }
830 default:
831 UNREACHABLE();
832 }
833 err = NULL;
834 out:
835 drgn_object_deinit_value(obj, value);
836 return err;
837 }
838
839 static struct drgn_error *
840 c_format_float_object(const struct drgn_object *obj, struct string_builder *sb)
841 {
842 struct drgn_error *err;
843 double fvalue;
844
845 err = drgn_object_read_float(obj, &fvalue);
846 if (err)
847 return err;
848 if (rint(fvalue) == fvalue) {
849 if (!string_builder_appendf(sb, "%.1f", fvalue))
850 return &drgn_enomem;
851 } else {
852 if (!string_builder_appendf(sb, "%.*g", DBL_DECIMAL_DIG,
853 fvalue))
854 return &drgn_enomem;
855 }
856 return NULL;
857 }
858
859 static struct drgn_error drgn_line_wrap = {
860 .code = DRGN_ERROR_STOP,
861 .message = "needs line wrap",
862 };
863
864 struct initializer_iter {
865 struct drgn_error *(*next)(struct initializer_iter *,
866 struct drgn_object *);
867 void (*reset)(struct initializer_iter *);
868 struct drgn_error *(*append_designation)(struct initializer_iter *,
869 struct string_builder *);
870 };
871
872 static struct drgn_error *
873 c_format_initializer(struct drgn_program *prog, struct initializer_iter *iter,
874 size_t indent, size_t one_line_columns,
875 size_t multi_line_columns,
876 const struct drgn_format_object_options *options,
877 bool same_line, struct string_builder *sb)
878 {
879 struct drgn_error *err;
880 size_t brace, remaining_columns, start_columns;
881
882 DRGN_OBJECT(obj, prog);
883
884 /* First, try to fit everything on one line. */
885 brace = sb->len;
886 if (!string_builder_appendc(sb, '{'))
887 return &drgn_enomem;
888 if (__builtin_sub_overflow(one_line_columns, 1, &remaining_columns))
889 remaining_columns = 0;
890 for (;;) {
891 size_t initializer_start;
892
893 err = iter->next(iter, &obj);
894 if (err == &drgn_stop)
895 break;
896 else if (err)
897 return err;
898
899 if (!same_line) {
900 err = &drgn_line_wrap;
901 break;
902 } else if (sb->len == brace + 1) {
903 if (remaining_columns < 3) {
904 /*
905 * The preceding space and closing space and
906 * brace don't fit.
907 */
908 err = &drgn_line_wrap;
909 break;
910 }
911 if (!string_builder_appendc(sb, ' '))
912 return &drgn_enomem;
913 remaining_columns--;
914 } else {
915 if (remaining_columns < 4) {
916 /*
917 * The preceding comma and space and closing
918 * space and brace don't fit.
919 */
920 err = &drgn_line_wrap;
921 break;
922 }
923 if (!string_builder_append(sb, ", "))
924 return &drgn_enomem;
925 remaining_columns -= 2;
926 }
927
928 if (iter->append_designation) {
929 size_t designation_start = sb->len;
930
931 err = iter->append_designation(iter, sb);
932 if (err)
933 return err;
934 if (__builtin_sub_overflow(remaining_columns,
935 sb->len - designation_start,
936 &remaining_columns)) {
937 err = &drgn_line_wrap;
938 break;
939 }
940 }
941
942 initializer_start = sb->len;
943 err = c_format_object_impl(&obj, indent + 1,
944 remaining_columns - 2, 0, options,
945 sb);
946 if (err == &drgn_line_wrap)
947 break;
948 else if (err)
949 return err;
950
951 if (__builtin_sub_overflow(remaining_columns,
952 sb->len - initializer_start,
953 &remaining_columns)) {
954 err = &drgn_line_wrap;
955 break;
956 }
957 }
958 if (err != &drgn_line_wrap) {
959 /* All of the initializers fit. */
960 if (sb->len == brace + 1) {
961 /* There were no initializers. */
962 if (string_builder_appendc(sb, '}'))
963 return NULL;
964 else
965 return &drgn_enomem;
966 } else if (remaining_columns >= 2) {
967 if (string_builder_append(sb, " }"))
968 return NULL;
969 else
970 return &drgn_enomem;
971 }
972 /* The final space and closing brace didn't fit. */
973 }
974
975 /* It didn't fit on one line. Try multiple lines. */
976
977 if (multi_line_columns == 0) {
978 /* We were asked to stay on one line. */
979 return &drgn_line_wrap;
980 }
981
982 sb->len = brace + 1;
983 if (__builtin_sub_overflow(multi_line_columns, 8 * (indent + 1),
984 &start_columns))
985 start_columns = 0;
986 remaining_columns = 0;
987 iter->reset(iter);
988 for (;;) {
989 size_t newline, designation_start, line_columns;
990
991 err = iter->next(iter, &obj);
992 if (err == &drgn_stop)
993 break;
994 else if (err)
995 return err;
996
997 newline = sb->len;
998 if (!string_builder_appendc(sb, '\n') ||
999 !append_tabs(indent + 1, sb))
1000 return &drgn_enomem;
1001
1002 designation_start = sb->len;
1003 line_columns = start_columns;
1004 if (iter->append_designation) {
1005 err = iter->append_designation(iter, sb);
1006 if (err)
1007 return err;
1008 if (__builtin_sub_overflow(line_columns,
1009 sb->len - designation_start,
1010 &line_columns))
1011 line_columns = 0;
1012 }
1013
1014 if (line_columns > 1) {
1015 size_t initializer_start = sb->len;
1016
1017 err = c_format_object_impl(&obj, 0, line_columns - 1, 0,
1018 options, sb);
1019 if (!err) {
1020 size_t len = sb->len - designation_start;
1021
1022 if (same_line && len + 2 <= remaining_columns) {
1023 /*
1024 * It would've fit on the previous line.
1025 * Move it over.
1026 */
1027 sb->str[newline] = ' ';
1028 memmove(&sb->str[newline + 1],
1029 &sb->str[designation_start],
1030 len);
1031 sb->len = newline + 1 + len;
1032 if (!string_builder_appendc(sb, ','))
1033 return &drgn_enomem;
1034 remaining_columns -= len + 2;
1035 continue;
1036 }
1037 if (len < start_columns) {
1038 /* It fit on the new line. */
1039 if (!string_builder_appendc(sb, ','))
1040 return &drgn_enomem;
1041 remaining_columns =
1042 start_columns - len - 1;
1043 continue;
1044 }
1045 } else if (err != &drgn_line_wrap) {
1046 return err;
1047 }
1048 /* It didn't fit. */
1049 sb->len = initializer_start;
1050 }
1051
1052 err = c_format_object_impl(&obj, indent + 1, 0,
1053 multi_line_columns, options, sb);
1054 if (err)
1055 return err;
1056 if (!string_builder_appendc(sb, ','))
1057 return &drgn_enomem;
1058 remaining_columns = 0;
1059 }
1060
1061 if (!string_builder_appendc(sb, '\n') || !append_tabs(indent, sb) ||
1062 !string_builder_appendc(sb, '}'))
1063 return &drgn_enomem;
1064 return NULL;
1065 }
1066
1067 struct compound_initializer_state {
1068 struct drgn_type_member *member, *end;
1069 uint64_t bit_offset;
1070 };
1071
1072 DEFINE_VECTOR(compound_initializer_stack, struct compound_initializer_state);
1073
1074 struct compound_initializer_iter {
1075 struct initializer_iter iter;
1076 const struct drgn_object *obj;
1077 struct compound_initializer_stack stack;
1078 enum drgn_format_object_flags flags;
1079 };
1080
1081 static struct drgn_error *
1082 compound_initializer_iter_next(struct initializer_iter *iter_,
1083 struct drgn_object *ret)
1084 {
1085 struct drgn_error *err;
1086 struct compound_initializer_iter *iter =
1087 container_of(iter_, struct compound_initializer_iter, iter);
1088
1089 for (;;) {
1090 if (compound_initializer_stack_empty(&iter->stack))
1091 return &drgn_stop;
1092
1093 struct compound_initializer_state *top =
1094 compound_initializer_stack_last(&iter->stack);
1095 if (top->member == top->end) {
1096 compound_initializer_stack_pop(&iter->stack);
1097 continue;
1098 }
1099
1100 uint64_t bit_offset = top->bit_offset;
1101 struct drgn_type_member *member = top->member++;
1102 struct drgn_qualified_type member_type;
1103 uint64_t member_bit_field_size;
1104 err = drgn_member_type(member, &member_type,
1105 &member_bit_field_size);
1106 if (err)
1107 return err;
1108
1109 /*
1110 * If the member is named or we are not including names, return
1111 * it. Otherwise, if it has members, descend into it. If it
1112 * doesn't, then this isn't valid C, but let's return it
1113 * anyways.
1114 */
1115 if (member->name ||
1116 !(iter->flags & DRGN_FORMAT_OBJECT_MEMBER_NAMES) ||
1117 !drgn_type_has_members(member_type.type)) {
1118 err = drgn_object_fragment(ret, iter->obj, member_type,
1119 bit_offset + member->bit_offset,
1120 member_bit_field_size);
1121 if (err)
1122 return err;
1123
1124 /* If we're including names, we can skip zeroes. */
1125 if ((iter->flags & (DRGN_FORMAT_OBJECT_MEMBER_NAMES |
1126 DRGN_FORMAT_OBJECT_IMPLICIT_MEMBERS)) ==
1127 DRGN_FORMAT_OBJECT_MEMBER_NAMES) {
1128 bool zero;
1129
1130 err = drgn_object_is_zero(ret, &zero);
1131 if (err)
1132 return err;
1133 if (zero)
1134 continue;
1135 }
1136 break;
1137 }
1138
1139 struct compound_initializer_state *new =
1140 compound_initializer_stack_append_entry(&iter->stack);
1141 if (!new)
1142 return &drgn_enomem;
1143 new->member = drgn_type_members(member_type.type);
1144 new->end = new->member + drgn_type_num_members(member_type.type);
1145 new->bit_offset = bit_offset + member->bit_offset;
1146 }
1147 return NULL;
1148 }
1149
1150 static void compound_initializer_iter_reset(struct initializer_iter *iter_)
1151 {
1152 struct compound_initializer_iter *iter =
1153 container_of(iter_, struct compound_initializer_iter, iter);
1154 compound_initializer_stack_resize(&iter->stack, 1);
1155 compound_initializer_stack_first(&iter->stack)->member =
1156 drgn_type_members(drgn_underlying_type(iter->obj->type));
1157 }
1158
1159 static struct drgn_error *
1160 compound_initializer_append_designation(struct initializer_iter *iter_,
1161 struct string_builder *sb)
1162 {
1163 struct compound_initializer_iter *iter =
1164 container_of(iter_, struct compound_initializer_iter, iter);
1165 struct compound_initializer_state *top =
1166 compound_initializer_stack_last(&iter->stack);
1167 const char *name = top->member[-1].name;
1168
1169 if (name && !string_builder_appendf(sb, ".%s = ", name))
1170 return &drgn_enomem;
1171 return NULL;
1172 }
1173
1174 static struct drgn_error *
1175 c_format_compound_object(const struct drgn_object *obj,
1176 struct drgn_type *underlying_type, size_t indent,
1177 size_t one_line_columns, size_t multi_line_columns,
1178 const struct drgn_format_object_options *options,
1179 struct string_builder *sb)
1180 {
1181 struct drgn_error *err;
1182
1183 if (!drgn_type_is_complete(underlying_type)) {
1184 const char *keyword;
1185
1186 switch (drgn_type_kind(underlying_type)) {
1187 case DRGN_TYPE_STRUCT:
1188 keyword = "struct";
1189 break;
1190 case DRGN_TYPE_UNION:
1191 keyword = "union";
1192 break;
1193 case DRGN_TYPE_CLASS:
1194 keyword = "class";
1195 break;
1196 default:
1197 UNREACHABLE();
1198 }
1199 return drgn_error_format(DRGN_ERROR_TYPE,
1200 "cannot format incomplete %s object",
1201 keyword);
1202 }
1203
1204 struct compound_initializer_iter iter = {
1205 .iter = {
1206 .next = compound_initializer_iter_next,
1207 .reset = compound_initializer_iter_reset,
1208 .append_designation =
1209 options->flags & DRGN_FORMAT_OBJECT_MEMBER_NAMES ?
1210 compound_initializer_append_designation : NULL,
1211 },
1212 .obj = obj,
1213 .stack = VECTOR_INIT,
1214 .flags = options->flags,
1215 };
1216 struct compound_initializer_state *new =
1217 compound_initializer_stack_append_entry(&iter.stack);
1218 if (!new) {
1219 err = &drgn_enomem;
1220 goto out;
1221 }
1222 new->member = drgn_type_members(underlying_type);
1223 new->end = add_to_possibly_null_pointer(new->member,
1224 drgn_type_num_members(underlying_type));
1225 new->bit_offset = 0;
1226
1227 /*
1228 * If we don't want zero members, ignore any at the end. If we're
1229 * including member names, then we'll skip past zero members as we
1230 * iterate, so we don't need to do this.
1231 */
1232 if (!(options->flags & (DRGN_FORMAT_OBJECT_MEMBER_NAMES
1233 | DRGN_FORMAT_OBJECT_IMPLICIT_MEMBERS))
1234 && new->member < new->end) {
1235 DRGN_OBJECT(member, drgn_object_program(obj));
1236 do {
1237 struct drgn_qualified_type member_type;
1238 uint64_t member_bit_field_size;
1239 err = drgn_member_type(&new->end[-1], &member_type,
1240 &member_bit_field_size);
1241 if (err)
1242 goto out;
1243
1244 err = drgn_object_fragment(&member, obj, member_type,
1245 new->end[-1].bit_offset,
1246 member_bit_field_size);
1247 if (err)
1248 goto out;
1249
1250 bool zero;
1251 err = drgn_object_is_zero(&member, &zero);
1252 if (err)
1253 goto out;
1254 if (zero)
1255 new->end--;
1256 else
1257 break;
1258 } while (new->member < new->end);
1259 }
1260
1261 struct drgn_format_object_options initializer_options = *options;
1262 initializer_options.flags =
1263 drgn_member_format_object_flags(initializer_options.flags);
1264 err = c_format_initializer(drgn_object_program(obj), &iter.iter, indent,
1265 one_line_columns, multi_line_columns,
1266 &initializer_options,
1267 options->flags & DRGN_FORMAT_OBJECT_MEMBERS_SAME_LINE,
1268 sb);
1269 out:
1270 compound_initializer_stack_deinit(&iter.stack);
1271 return err;
1272 }
1273
1274 static struct drgn_error *
1275 c_format_enum_object(const struct drgn_object *obj,
1276 struct drgn_type *underlying_type,
1277 struct string_builder *sb)
1278 {
1279 struct drgn_error *err;
1280 struct drgn_type_enumerator *enumerators;
1281 size_t num_enumerators, i;
1282
1283 if (!drgn_type_is_complete(underlying_type)) {
1284 return drgn_error_create(DRGN_ERROR_TYPE,
1285 "cannot format incomplete enum object");
1286 }
1287
1288 enumerators = drgn_type_enumerators(underlying_type);
1289 num_enumerators = drgn_type_num_enumerators(underlying_type);
1290 if (drgn_enum_type_is_signed(underlying_type)) {
1291 int64_t svalue;
1292
1293 err = drgn_object_read_signed(obj, &svalue);
1294 if (err)
1295 return err;
1296 for (i = 0; i < num_enumerators; i++) {
1297 if (enumerators[i].svalue == svalue) {
1298 if (!string_builder_append(sb,
1299 enumerators[i].name))
1300 return &drgn_enomem;
1301 return NULL;
1302 }
1303 }
1304 if (!string_builder_appendf(sb, "%" PRId64, svalue))
1305 return &drgn_enomem;
1306 return NULL;
1307 } else {
1308 uint64_t uvalue;
1309
1310 err = drgn_object_read_unsigned(obj, &uvalue);
1311 if (err)
1312 return err;
1313 for (i = 0; i < num_enumerators; i++) {
1314 if (enumerators[i].uvalue == uvalue) {
1315 if (!string_builder_append(sb,
1316 enumerators[i].name))
1317 return &drgn_enomem;
1318 return NULL;
1319 }
1320 }
1321 if (!string_builder_appendf(sb, "%" PRIu64, uvalue))
1322 return &drgn_enomem;
1323 return NULL;
1324 }
1325 }
1326
1327 static struct drgn_error *
1328 c_format_pointer_object(const struct drgn_object *obj,
1329 struct drgn_type *underlying_type, size_t indent,
1330 size_t one_line_columns, size_t multi_line_columns,
1331 const struct drgn_format_object_options *options,
1332 struct string_builder *sb)
1333 {
1334 struct drgn_error *err;
1335 bool dereference = options->flags & DRGN_FORMAT_OBJECT_DEREFERENCE;
1336 bool c_string =
1337 ((options->flags & DRGN_FORMAT_OBJECT_STRING) &&
1338 is_character_type(drgn_type_type(underlying_type).type));
1339 uint64_t uvalue;
1340 _cleanup_symbol_ struct drgn_symbol *sym = NULL;
1341 size_t start, type_start, type_end, value_start, value_end;
1342
1343 start = sb->len;
1344 if (dereference && !c_string && !string_builder_appendc(sb, '*'))
1345 return &drgn_enomem;
1346 type_start = sb->len;
1347 if (options->flags & DRGN_FORMAT_OBJECT_TYPE_NAME) {
1348 if (!string_builder_appendc(sb, '('))
1349 return &drgn_enomem;
1350 err = c_format_type_name_impl(drgn_object_qualified_type(obj),
1351 sb);
1352 if (err)
1353 return err;
1354 if (!string_builder_appendc(sb, ')'))
1355 return &drgn_enomem;
1356
1357 }
1358 type_end = sb->len;
1359
1360 err = drgn_object_read_unsigned(obj, &uvalue);
1361 if (err)
1362 return err;
1363
1364 if ((options->flags & DRGN_FORMAT_OBJECT_SYMBOLIZE) &&
1365 (err = drgn_program_find_symbol_by_address_internal(drgn_object_program(obj),
1366 uvalue, &sym)))
1367 return err;
1368 if (sym && dereference && !c_string &&
1369 !string_builder_appendc(sb, '('))
1370 return &drgn_enomem;
1371 value_start = sb->len;
1372 if (sym &&
1373 !string_builder_appendf(sb, "%s+0x%" PRIx64 " = ", sym->name,
1374 uvalue - sym->address))
1375 return &drgn_enomem;
1376
1377 if (!string_builder_appendf(sb, "0x%" PRIx64, uvalue))
1378 return &drgn_enomem;
1379 if (!dereference && !c_string)
1380 return NULL;
1381 value_end = sb->len;
1382
1383 if ((sym && dereference && !c_string &&
1384 !string_builder_appendc(sb, ')')) ||
1385 !string_builder_append(sb, " = "))
1386 return &drgn_enomem;
1387
1388 if (c_string) {
1389 err = c_format_string(drgn_object_program(obj), uvalue,
1390 UINT64_MAX, sb);
1391 } else {
1392 DRGN_OBJECT(dereferenced, drgn_object_program(obj));
1393 err = drgn_object_dereference(&dereferenced, obj);
1394 if (err) {
1395 if (err->code == DRGN_ERROR_TYPE)
1396 goto no_dereference;
1397 return err;
1398 }
1399 if (__builtin_sub_overflow(one_line_columns, sb->len - start,
1400 &one_line_columns))
1401 one_line_columns = 0;
1402 struct drgn_format_object_options dereferenced_options =
1403 *options;
1404 dereferenced_options.flags =
1405 drgn_passthrough_format_object_flags(dereferenced_options.flags);
1406 err = c_format_object_impl(&dereferenced, indent,
1407 one_line_columns, multi_line_columns,
1408 &dereferenced_options, sb);
1409 }
1410 if (!err || (err->code != DRGN_ERROR_FAULT && err->code != DRGN_ERROR_OUT_OF_BOUNDS)) {
1411 /* We either succeeded or hit a fatal error. */
1412 return err;
1413 }
1414
1415 no_dereference:
1416 /*
1417 * We hit a non-fatal error. Delete the asterisk and symbol parentheses
1418 * and truncate everything after the address.
1419 */
1420 drgn_error_destroy(err);
1421 if (type_start != start) {
1422 memmove(&sb->str[start], &sb->str[type_start],
1423 type_end - type_start);
1424 }
1425 if (start + type_end - type_start != value_start) {
1426 memmove(&sb->str[start + type_end - type_start],
1427 &sb->str[value_start], value_end - value_start);
1428 }
1429 sb->len = start + type_end - type_start + value_end - value_start;
1430 return NULL;
1431 }
1432
1433 struct array_initializer_iter {
1434 struct initializer_iter iter;
1435 const struct drgn_object *obj;
1436 struct drgn_qualified_type element_type;
1437 uint64_t element_bit_size;
1438 uint64_t length, i;
1439 enum drgn_format_object_flags flags;
1440 };
1441
1442 static struct drgn_error *
1443 array_initializer_iter_next(struct initializer_iter *iter_,
1444 struct drgn_object *ret)
1445 {
1446 struct drgn_error *err;
1447 struct array_initializer_iter *iter =
1448 container_of(iter_, struct array_initializer_iter, iter);
1449
1450 for (;;) {
1451 bool zero;
1452
1453 if (iter->i >= iter->length)
1454 return &drgn_stop;
1455 err = drgn_object_fragment(ret, iter->obj, iter->element_type,
1456 iter->i * iter->element_bit_size, 0);
1457 if (err)
1458 return err;
1459 iter->i++;
1460
1461 /* If we're including indices, we can skip zeroes. */
1462 if ((iter->flags & (DRGN_FORMAT_OBJECT_ELEMENT_INDICES |
1463 DRGN_FORMAT_OBJECT_IMPLICIT_ELEMENTS)) !=
1464 DRGN_FORMAT_OBJECT_ELEMENT_INDICES)
1465 break;
1466
1467 err = drgn_object_is_zero(ret, &zero);
1468 if (err)
1469 return err;
1470 if (!zero)
1471 break;
1472 }
1473 return NULL;
1474 }
1475
1476 static void array_initializer_iter_reset(struct initializer_iter *iter_)
1477 {
1478 struct array_initializer_iter *iter =
1479 container_of(iter_, struct array_initializer_iter, iter);
1480
1481 iter->i = 0;
1482 }
1483
1484 static struct drgn_error *
1485 array_initializer_append_designation(struct initializer_iter *iter_,
1486 struct string_builder *sb)
1487 {
1488 struct array_initializer_iter *iter =
1489 container_of(iter_, struct array_initializer_iter, iter);
1490
1491 if (!string_builder_appendf(sb, "[%" PRIu64 "] = ", iter->i - 1))
1492 return &drgn_enomem;
1493 return NULL;
1494 }
1495
1496 static struct drgn_error *
1497 c_format_array_object(const struct drgn_object *obj,
1498 struct drgn_type *underlying_type, size_t indent,
1499 size_t one_line_columns, size_t multi_line_columns,
1500 const struct drgn_format_object_options *options,
1501 struct string_builder *sb)
1502 {
1503 struct drgn_error *err;
1504 struct array_initializer_iter iter = {
1505 .iter = {
1506 .next = array_initializer_iter_next,
1507 .reset = array_initializer_iter_reset,
1508 .append_designation =
1509 options->flags & DRGN_FORMAT_OBJECT_ELEMENT_INDICES
1510 ? array_initializer_append_designation : NULL,
1511 },
1512 .obj = obj,
1513 .element_type = drgn_type_type(underlying_type),
1514 .length = drgn_type_length(underlying_type),
1515 .flags = options->flags,
1516 };
1517
1518 if ((options->flags & DRGN_FORMAT_OBJECT_STRING) && iter.length &&
1519 is_character_type(iter.element_type.type)) {
1520 SWITCH_ENUM(obj->kind) {
1521 case DRGN_OBJECT_VALUE: {
1522 const unsigned char *buf;
1523 uint64_t size, i;
1524
1525 if (!string_builder_appendc(sb, '"'))
1526 return &drgn_enomem;
1527 buf = (const unsigned char *)drgn_object_buffer(obj);
1528 size = drgn_object_size(obj);
1529 for (i = 0; i < size; i++) {
1530 if (buf[i] == '\0')
1531 break;
1532 err = c_format_character(buf[i], false, true,
1533 sb);
1534 if (err)
1535 return err;
1536 }
1537 if (!string_builder_appendc(sb, '"'))
1538 return &drgn_enomem;
1539 return NULL;
1540 }
1541 case DRGN_OBJECT_REFERENCE:
1542 return c_format_string(drgn_object_program(obj),
1543 obj->address, iter.length, sb);
1544 case DRGN_OBJECT_ABSENT:
1545 default:
1546 UNREACHABLE();
1547 }
1548 }
1549
1550 err = drgn_type_bit_size(iter.element_type.type,
1551 &iter.element_bit_size);
1552 if (err)
1553 return err;
1554
1555 /*
1556 * If we don't want zero elements, ignore any at the end. If we're
1557 * including indices, then we'll skip past zeroes as we iterate, so we
1558 * don't need to do this.
1559 */
1560 if (!(options->flags & (DRGN_FORMAT_OBJECT_ELEMENT_INDICES
1561 | DRGN_FORMAT_OBJECT_IMPLICIT_ELEMENTS))
1562 && iter.length) {
1563 DRGN_OBJECT(element, drgn_object_program(obj));
1564 do {
1565 bool zero;
1566
1567 err = drgn_object_fragment(&element, obj,
1568 iter.element_type,
1569 (iter.length - 1)
1570 * iter.element_bit_size,
1571 0);
1572 if (err)
1573 return err;
1574
1575 err = drgn_object_is_zero(&element, &zero);
1576 if (err)
1577 return err;
1578 if (zero)
1579 iter.length--;
1580 else
1581 break;
1582 } while (iter.length);
1583 }
1584 struct drgn_format_object_options initializer_options = *options;
1585 initializer_options.flags =
1586 drgn_element_format_object_flags(initializer_options.flags);
1587 return c_format_initializer(drgn_object_program(obj), &iter.iter,
1588 indent, one_line_columns,
1589 multi_line_columns, &initializer_options,
1590 options->flags & DRGN_FORMAT_OBJECT_ELEMENTS_SAME_LINE,
1591 sb);
1592 }
1593
1594 static struct drgn_error *
1595 c_format_function_object(const struct drgn_object *obj,
1596 struct string_builder *sb)
1597 {
1598 assert(obj->kind == DRGN_OBJECT_REFERENCE);
1599 if (!string_builder_appendf(sb, "0x%" PRIx64, obj->address))
1600 return &drgn_enomem;
1601 return NULL;
1602 }
1603
1604 static const char *drgn_absence_reason_str(enum drgn_absence_reason reason)
1605 {
1606 SWITCH_ENUM (reason) {
1607 case DRGN_ABSENCE_REASON_OPTIMIZED_OUT:
1608 return "<optimized out>";
1609 case DRGN_ABSENCE_REASON_NOT_IMPLEMENTED:
1610 return "<not implemented>";
1611 case DRGN_ABSENCE_REASON_OTHER:
1612 default:
1613 return "<absent>";
1614 }
1615 }
1616
1617 // Note: this ignores options->columns in favor of {one,multi}_line_columns.
1618 static struct drgn_error *
1619 c_format_object_impl(const struct drgn_object *obj, size_t indent,
1620 size_t one_line_columns, size_t multi_line_columns,
1621 const struct drgn_format_object_options *options,
1622 struct string_builder *sb)
1623 {
1624 struct drgn_error *err;
1625 struct drgn_type *underlying_type = drgn_underlying_type(obj->type);
1626
1627 /*
1628 * Pointers are special because they can have an asterisk prefix if
1629 * we're dereferencing them.
1630 */
1631 if (drgn_type_kind(underlying_type) == DRGN_TYPE_POINTER &&
1632 obj->kind != DRGN_OBJECT_ABSENT) {
1633 return c_format_pointer_object(obj, underlying_type, indent,
1634 one_line_columns,
1635 multi_line_columns, options, sb);
1636 }
1637
1638 if (options->flags & DRGN_FORMAT_OBJECT_TYPE_NAME) {
1639 size_t old_len = sb->len;
1640
1641 if (!string_builder_appendc(sb, '('))
1642 return &drgn_enomem;
1643 err = c_format_type_name_impl(drgn_object_qualified_type(obj),
1644 sb);
1645 if (err)
1646 return err;
1647 if (!string_builder_appendc(sb, ')'))
1648 return &drgn_enomem;
1649
1650 if (__builtin_sub_overflow(one_line_columns, sb->len - old_len,
1651 &one_line_columns))
1652 one_line_columns = 0;
1653 }
1654
1655 if (obj->kind == DRGN_OBJECT_ABSENT) {
1656 if (!string_builder_append(sb,
1657 drgn_absence_reason_str(obj->absence_reason)))
1658 return &drgn_enomem;
1659 return NULL;
1660 }
1661
1662 SWITCH_ENUM(drgn_type_kind(underlying_type)) {
1663 case DRGN_TYPE_VOID:
1664 return drgn_error_create(DRGN_ERROR_TYPE,
1665 "cannot format void object");
1666 case DRGN_TYPE_INT:
1667 case DRGN_TYPE_BOOL:
1668 return c_format_int_object(obj, options, sb);
1669 case DRGN_TYPE_FLOAT:
1670 return c_format_float_object(obj, sb);
1671 case DRGN_TYPE_STRUCT:
1672 case DRGN_TYPE_UNION:
1673 case DRGN_TYPE_CLASS:
1674 return c_format_compound_object(obj, underlying_type, indent,
1675 one_line_columns,
1676 multi_line_columns, options,
1677 sb);
1678 case DRGN_TYPE_ENUM:
1679 return c_format_enum_object(obj, underlying_type, sb);
1680 case DRGN_TYPE_ARRAY:
1681 return c_format_array_object(obj, underlying_type, indent,
1682 one_line_columns,
1683 multi_line_columns, options, sb);
1684 case DRGN_TYPE_FUNCTION:
1685 return c_format_function_object(obj, sb);
1686 case DRGN_TYPE_TYPEDEF:
1687 case DRGN_TYPE_POINTER:
1688 default:
1689 UNREACHABLE();
1690 }
1691 }
1692
1693 static struct drgn_error *
1694 c_format_object(const struct drgn_object *obj,
1695 const struct drgn_format_object_options *options, char **ret)
1696 {
1697 struct drgn_error *err;
1698 STRING_BUILDER(sb);
1699 err = c_format_object_impl(obj, 0, options->columns,
1700 max(options->columns, (size_t)1), options,
1701 &sb);
1702 if (err)
1703 return err;
1704 if (!string_builder_null_terminate(&sb))
1705 return &drgn_enomem;
1706 *ret = string_builder_steal(&sb);
1707 return NULL;
1708 }
1709
1710 #include "c_keywords.inc"
1711
1712 struct drgn_error *drgn_c_family_lexer_func(struct drgn_lexer *lexer,
1713 struct drgn_token *token) {
1714 const char *p = lexer->p;
1715 bool cpp = ((struct drgn_c_family_lexer *)lexer)->cpp;
1716
1717 while (isspace(*p))
1718 p++;
1719
1720 token->value = p;
1721 switch (*p) {
1722 case '\0':
1723 token->kind = C_TOKEN_EOF;
1724 break;
1725 case '(':
1726 token->kind = C_TOKEN_LPAREN;
1727 p++;
1728 break;
1729 case ')':
1730 token->kind = C_TOKEN_RPAREN;
1731 p++;
1732 break;
1733 case '[':
1734 token->kind = C_TOKEN_LBRACKET;
1735 p++;
1736 break;
1737 case ']':
1738 token->kind = C_TOKEN_RBRACKET;
1739 p++;
1740 break;
1741 case '*':
1742 token->kind = C_TOKEN_ASTERISK;
1743 p++;
1744 break;
1745 case '.':
1746 token->kind = C_TOKEN_DOT;
1747 p++;
1748 break;
1749 case ':':
1750 token->kind = C_TOKEN_COLON;
1751 p++;
1752 break;
1753 case '<':
1754 // This is a hack for cpp_append_to_identifier(). We don't want
1755 // to deal with actually parsing template arguments, and we
1756 // don't care about "<" otherwise, so this scans a token from
1757 // the "<" to its matching ">".
1758 if (cpp) {
1759 token->kind = C_TOKEN_TEMPLATE_ARGUMENTS;
1760 p++;
1761 size_t less_thans = 1;
1762 bool in_single_quotes = false;
1763 do {
1764 switch (*p++) {
1765 case '<':
1766 if (!in_single_quotes)
1767 less_thans++;
1768 break;
1769 case '>':
1770 if (!in_single_quotes)
1771 less_thans--;
1772 break;
1773 case '\'':
1774 // Handling the edge-case of an escaped single-quote
1775 if (!(in_single_quotes && *(p - 2) == '\\'))
1776 in_single_quotes = !in_single_quotes;
1777 break;
1778 case '\0':
1779 return drgn_error_create(DRGN_ERROR_SYNTAX,
1780 "invalid template arguments");
1781 }
1782 } while (less_thans > 0);
1783 break;
1784 }
1785 fallthrough;
1786 default:
1787 if (isalpha(*p) || *p == '_') {
1788 do {
1789 p++;
1790 } while (isalnum(*p) || *p == '_');
1791 token->kind = identifier_token_kind(token->value,
1792 p - token->value,
1793 cpp);
1794 } else if ('0' <= *p && *p <= '9') {
1795 token->kind = C_TOKEN_NUMBER;
1796 if (*p++ == '0' && *p == 'x') {
1797 p++;
1798 while (('0' <= *p && *p <= '9') ||
1799 ('a' <= *p && *p <= 'f') ||
1800 ('A' <= *p && *p <= 'F')) {
1801 p++;
1802 }
1803 if (p - token->value <= 2) {
1804 return drgn_error_create(DRGN_ERROR_SYNTAX,
1805 "invalid number");
1806 }
1807 } else {
1808 while ('0' <= *p && *p <= '9')
1809 p++;
1810 }
1811 if (isalpha(*p) || *p == '_') {
1812 return drgn_error_create(DRGN_ERROR_SYNTAX,
1813 "invalid number");
1814 }
1815 } else {
1816 return drgn_error_format(DRGN_ERROR_SYNTAX,
1817 "invalid character \\x%02x", (unsigned char)*p);
1818 }
1819 break;
1820 }
1821
1822 token->len = p - token->value;
1823 lexer->p = p;
1824 return NULL;
1825 }
1826
1827 static struct drgn_error *c_token_to_u64(const struct drgn_token *token,
1828 uint64_t *ret)
1829 {
1830 uint64_t x = 0;
1831 size_t i;
1832
1833 assert(token->kind == C_TOKEN_NUMBER);
1834 if (token->len > 2 && token->value[0] == '0' &&
1835 token->value[1] == 'x') {
1836 for (i = 2; i < token->len; i++) {
1837 char c = token->value[i];
1838 int digit;
1839
1840 if ('0' <= c && c <= '9')
1841 digit = c - '0';
1842 else if ('a' <= c && c <= 'f')
1843 digit = c - 'a';
1844 else /* ('A' <= c && c <= 'F') */
1845 digit = c - 'A';
1846 if (x > UINT64_MAX / 16)
1847 goto overflow;
1848 x *= 16;
1849 if (x > UINT64_MAX - digit)
1850 goto overflow;
1851 x += digit;
1852 }
1853 } else if (token->value[0] == '0') {
1854 for (i = 1; i < token->len; i++) {
1855 int digit;
1856
1857 digit = token->value[i] - '0';
1858 if (x > UINT64_MAX / 8)
1859 goto overflow;
1860 x *= 8;
1861 if (x > UINT64_MAX - digit)
1862 goto overflow;
1863 x += digit;
1864 }
1865 } else {
1866 for (i = 0; i < token->len; i++) {
1867 int digit;
1868
1869 digit = token->value[i] - '0';
1870 if (x > UINT64_MAX / 10)
1871 goto overflow;
1872 x *= 10;
1873 if (x > UINT64_MAX - digit)
1874 goto overflow;
1875 x += digit;
1876 }
1877 }
1878 *ret = x;
1879 return NULL;
1880
1881 overflow:
1882 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
1883 "number is too large");
1884 }
1885
1886 enum c_type_specifier {
1887 SPECIFIER_ERROR,
1888 SPECIFIER_VOID,
1889 SPECIFIER_CHAR,
1890 SPECIFIER_SIGNED_CHAR,
1891 SPECIFIER_UNSIGNED_CHAR,
1892 SPECIFIER_SHORT,
1893 SPECIFIER_SHORT_INT,
1894 SPECIFIER_SIGNED_SHORT_INT,
1895 SPECIFIER_UNSIGNED_SHORT_INT,
1896 SPECIFIER_SIGNED_SHORT,
1897 SPECIFIER_UNSIGNED_SHORT,
1898 SPECIFIER_INT,
1899 SPECIFIER_SIGNED_INT,
1900 SPECIFIER_UNSIGNED_INT,
1901 SPECIFIER_LONG,
1902 SPECIFIER_LONG_INT,
1903 SPECIFIER_SIGNED_LONG,
1904 SPECIFIER_UNSIGNED_LONG,
1905 SPECIFIER_SIGNED_LONG_INT,
1906 SPECIFIER_UNSIGNED_LONG_INT,
1907 SPECIFIER_LONG_LONG,
1908 SPECIFIER_LONG_LONG_INT,
1909 SPECIFIER_SIGNED_LONG_LONG_INT,
1910 SPECIFIER_UNSIGNED_LONG_LONG_INT,
1911 SPECIFIER_SIGNED_LONG_LONG,
1912 SPECIFIER_UNSIGNED_LONG_LONG,
1913 SPECIFIER_SIGNED,
1914 SPECIFIER_UNSIGNED,
1915 SPECIFIER_BOOL,
1916 SPECIFIER_FLOAT,
1917 SPECIFIER_DOUBLE,
1918 SPECIFIER_LONG_DOUBLE,
1919 SPECIFIER_NONE,
1920 NUM_SPECIFIER_STATES,
1921 };
1922
1923 static const char *specifier_spelling[NUM_SPECIFIER_STATES] = {
1924 [SPECIFIER_VOID] = "void",
1925 [SPECIFIER_CHAR] = "char",
1926 [SPECIFIER_SIGNED_CHAR] = "signed char",
1927 [SPECIFIER_UNSIGNED_CHAR] = "unsigned char",
1928 [SPECIFIER_SHORT] = "short",
1929 [SPECIFIER_SHORT_INT] = "short int",
1930 [SPECIFIER_SIGNED_SHORT_INT] = "signed short int",
1931 [SPECIFIER_UNSIGNED_SHORT_INT] = "unsigned short int",
1932 [SPECIFIER_SIGNED_SHORT] = "signed short",
1933 [SPECIFIER_UNSIGNED_SHORT] = "unsigned short",
1934 [SPECIFIER_INT] = "int",
1935 [SPECIFIER_SIGNED_INT] = "signed int",
1936 [SPECIFIER_UNSIGNED_INT] = "unsigned int",
1937 [SPECIFIER_LONG] = "long",
1938 [SPECIFIER_LONG_INT] = "long int",
1939 [SPECIFIER_SIGNED_LONG] = "signed long",
1940 [SPECIFIER_UNSIGNED_LONG] = "unsigned long",
1941 [SPECIFIER_SIGNED_LONG_INT] = "signed long int",
1942 [SPECIFIER_UNSIGNED_LONG_INT] = "unsigned long int",
1943 [SPECIFIER_LONG_LONG] = "long long",
1944 [SPECIFIER_LONG_LONG_INT] = "long long int",
1945 [SPECIFIER_SIGNED_LONG_LONG_INT] = "signed long long int",
1946 [SPECIFIER_UNSIGNED_LONG_LONG_INT] = "unsigned long long int",
1947 [SPECIFIER_SIGNED_LONG_LONG] = "signed long long",
1948 [SPECIFIER_UNSIGNED_LONG_LONG] = "unsigned long long",
1949 [SPECIFIER_SIGNED] = "signed",
1950 [SPECIFIER_UNSIGNED] = "unsigned",
1951 [SPECIFIER_BOOL] = "_Bool",
1952 [SPECIFIER_FLOAT] = "float",
1953 [SPECIFIER_DOUBLE] = "double",
1954 [SPECIFIER_LONG_DOUBLE] = "long double",
1955 };
1956
1957 static const enum drgn_qualifiers qualifier_from_token[MAX_QUALIFIER_TOKEN + 1] = {
1958 [C_TOKEN_CONST] = DRGN_QUALIFIER_CONST,
1959 [C_TOKEN_RESTRICT] = DRGN_QUALIFIER_RESTRICT,
1960 [C_TOKEN_VOLATILE] = DRGN_QUALIFIER_VOLATILE,
1961 [C_TOKEN_ATOMIC] = DRGN_QUALIFIER_ATOMIC,
1962 };
1963
1964 static const enum c_type_specifier
1965 specifier_transition[NUM_SPECIFIER_STATES][MAX_SPECIFIER_TOKEN + 1] = {
1966 [SPECIFIER_NONE] = {
1967 [C_TOKEN_VOID] = SPECIFIER_VOID,
1968 [C_TOKEN_CHAR] = SPECIFIER_CHAR,
1969 [C_TOKEN_SHORT] = SPECIFIER_SHORT,
1970 [C_TOKEN_INT] = SPECIFIER_INT,
1971 [C_TOKEN_LONG] = SPECIFIER_LONG,
1972 [C_TOKEN_SIGNED] = SPECIFIER_SIGNED,
1973 [C_TOKEN_UNSIGNED] = SPECIFIER_UNSIGNED,
1974 [C_TOKEN_BOOL] = SPECIFIER_BOOL,
1975 [C_TOKEN_FLOAT] = SPECIFIER_FLOAT,
1976 [C_TOKEN_DOUBLE] = SPECIFIER_DOUBLE,
1977 },
1978 [SPECIFIER_VOID] = {},
1979 [SPECIFIER_CHAR] = {
1980 [C_TOKEN_SIGNED] = SPECIFIER_SIGNED_CHAR,
1981 [C_TOKEN_UNSIGNED] = SPECIFIER_UNSIGNED_CHAR,
1982 },
1983 [SPECIFIER_SIGNED_CHAR] = {},
1984 [SPECIFIER_UNSIGNED_CHAR] = {},
1985 [SPECIFIER_SHORT] = {
1986 [C_TOKEN_INT] = SPECIFIER_SHORT_INT,
1987 [C_TOKEN_SIGNED] = SPECIFIER_SIGNED_SHORT,
1988 [C_TOKEN_UNSIGNED] = SPECIFIER_UNSIGNED_SHORT,
1989 },
1990 [SPECIFIER_SHORT_INT] = {
1991 [C_TOKEN_SIGNED] = SPECIFIER_SIGNED_SHORT_INT,
1992 [C_TOKEN_UNSIGNED] = SPECIFIER_UNSIGNED_SHORT_INT,
1993 },
1994 [SPECIFIER_SIGNED_SHORT_INT] = {},
1995 [SPECIFIER_UNSIGNED_SHORT_INT] = {},
1996 [SPECIFIER_SIGNED_SHORT] = {
1997 [C_TOKEN_INT] = SPECIFIER_SIGNED_SHORT_INT,
1998 },
1999 [SPECIFIER_UNSIGNED_SHORT] = {
2000 [C_TOKEN_INT] = SPECIFIER_UNSIGNED_SHORT_INT,
2001 },
2002 [SPECIFIER_INT] = {
2003 [C_TOKEN_SHORT] = SPECIFIER_SHORT_INT,
2004 [C_TOKEN_LONG] = SPECIFIER_LONG_INT,
2005 [C_TOKEN_SIGNED] = SPECIFIER_SIGNED_INT,
2006 [C_TOKEN_UNSIGNED] = SPECIFIER_UNSIGNED_INT,
2007 },
2008 [SPECIFIER_SIGNED_INT] = {
2009 [C_TOKEN_SHORT] = SPECIFIER_SIGNED_SHORT_INT,
2010 [C_TOKEN_LONG] = SPECIFIER_SIGNED_LONG_INT,
2011 },
2012 [SPECIFIER_UNSIGNED_INT] = {
2013 [C_TOKEN_SHORT] = SPECIFIER_UNSIGNED_SHORT_INT,
2014 [C_TOKEN_LONG] = SPECIFIER_UNSIGNED_LONG_INT,
2015 },
2016 [SPECIFIER_LONG] = {
2017 [C_TOKEN_INT] = SPECIFIER_LONG_INT,
2018 [C_TOKEN_LONG] = SPECIFIER_LONG_LONG,
2019 [C_TOKEN_SIGNED] = SPECIFIER_SIGNED_LONG,
2020 [C_TOKEN_UNSIGNED] = SPECIFIER_UNSIGNED_LONG,
2021 [C_TOKEN_DOUBLE] = SPECIFIER_LONG_DOUBLE,
2022 },
2023 [SPECIFIER_LONG_INT] = {
2024 [C_TOKEN_LONG] = SPECIFIER_LONG_LONG_INT,
2025 [C_TOKEN_SIGNED] = SPECIFIER_SIGNED_LONG_INT,
2026 [C_TOKEN_UNSIGNED] = SPECIFIER_UNSIGNED_LONG_INT,
2027 },
2028 [SPECIFIER_SIGNED_LONG] = {
2029 [C_TOKEN_LONG] = SPECIFIER_SIGNED_LONG_LONG,
2030 [C_TOKEN_INT] = SPECIFIER_SIGNED_LONG_INT,
2031 },
2032 [SPECIFIER_UNSIGNED_LONG] = {
2033 [C_TOKEN_LONG] = SPECIFIER_UNSIGNED_LONG_LONG,
2034 [C_TOKEN_INT] = SPECIFIER_UNSIGNED_LONG_INT,
2035 },
2036 [SPECIFIER_SIGNED_LONG_INT] = {
2037 [C_TOKEN_LONG] = SPECIFIER_SIGNED_LONG_LONG_INT,
2038 },
2039 [SPECIFIER_UNSIGNED_LONG_INT] = {
2040 [C_TOKEN_LONG] = SPECIFIER_UNSIGNED_LONG_LONG_INT,
2041 },
2042 [SPECIFIER_LONG_LONG] = {
2043 [C_TOKEN_INT] = SPECIFIER_LONG_LONG_INT,
2044 [C_TOKEN_SIGNED] = SPECIFIER_SIGNED_LONG_LONG,
2045 [C_TOKEN_UNSIGNED] = SPECIFIER_UNSIGNED_LONG_LONG,
2046 },
2047 [SPECIFIER_LONG_LONG_INT] = {
2048 [C_TOKEN_SIGNED] = SPECIFIER_SIGNED_LONG_LONG_INT,
2049 [C_TOKEN_UNSIGNED] = SPECIFIER_UNSIGNED_LONG_LONG_INT,
2050 },
2051 [SPECIFIER_SIGNED_LONG_LONG_INT] = {},
2052 [SPECIFIER_UNSIGNED_LONG_LONG_INT] = {},
2053 [SPECIFIER_SIGNED_LONG_LONG] = {
2054 [C_TOKEN_INT] = SPECIFIER_SIGNED_LONG_LONG_INT,
2055 },
2056 [SPECIFIER_UNSIGNED_LONG_LONG] = {
2057 [C_TOKEN_INT] = SPECIFIER_UNSIGNED_LONG_LONG_INT,
2058 },
2059 [SPECIFIER_SIGNED] = {
2060 [C_TOKEN_CHAR] = SPECIFIER_SIGNED_CHAR,
2061 [C_TOKEN_SHORT] = SPECIFIER_SIGNED_SHORT,
2062 [C_TOKEN_INT] = SPECIFIER_SIGNED_INT,
2063 [C_TOKEN_LONG] = SPECIFIER_SIGNED_LONG,
2064 },
2065 [SPECIFIER_UNSIGNED] = {
2066 [C_TOKEN_CHAR] = SPECIFIER_UNSIGNED_CHAR,
2067 [C_TOKEN_SHORT] = SPECIFIER_UNSIGNED_SHORT,
2068 [C_TOKEN_INT] = SPECIFIER_UNSIGNED_INT,
2069 [C_TOKEN_LONG] = SPECIFIER_UNSIGNED_LONG,
2070 },
2071 [SPECIFIER_BOOL] = {},
2072 [SPECIFIER_FLOAT] = {},
2073 [SPECIFIER_DOUBLE] = {
2074 [C_TOKEN_LONG] = SPECIFIER_LONG_DOUBLE,
2075 },
2076 [SPECIFIER_LONG_DOUBLE] = {},
2077 };
2078
2079 static const enum drgn_primitive_type specifier_kind[NUM_SPECIFIER_STATES] = {
2080 [SPECIFIER_VOID] = DRGN_C_TYPE_VOID,
2081 [SPECIFIER_CHAR] = DRGN_C_TYPE_CHAR,
2082 [SPECIFIER_SIGNED_CHAR] = DRGN_C_TYPE_SIGNED_CHAR,
2083 [SPECIFIER_UNSIGNED_CHAR] = DRGN_C_TYPE_UNSIGNED_CHAR,
2084 [SPECIFIER_SHORT] = DRGN_C_TYPE_SHORT,
2085 [SPECIFIER_SHORT_INT] = DRGN_C_TYPE_SHORT,
2086 [SPECIFIER_SIGNED_SHORT_INT] = DRGN_C_TYPE_SHORT,
2087 [SPECIFIER_UNSIGNED_SHORT_INT] = DRGN_C_TYPE_UNSIGNED_SHORT,
2088 [SPECIFIER_SIGNED_SHORT] = DRGN_C_TYPE_SHORT,
2089 [SPECIFIER_UNSIGNED_SHORT] = DRGN_C_TYPE_UNSIGNED_SHORT,
2090 [SPECIFIER_INT] = DRGN_C_TYPE_INT,
2091 [SPECIFIER_SIGNED_INT] = DRGN_C_TYPE_INT,
2092 [SPECIFIER_UNSIGNED_INT] = DRGN_C_TYPE_UNSIGNED_INT,
2093 [SPECIFIER_LONG] = DRGN_C_TYPE_LONG,
2094 [SPECIFIER_LONG_INT] = DRGN_C_TYPE_LONG,
2095 [SPECIFIER_SIGNED_LONG] = DRGN_C_TYPE_LONG,
2096 [SPECIFIER_UNSIGNED_LONG] = DRGN_C_TYPE_UNSIGNED_LONG,
2097 [SPECIFIER_SIGNED_LONG_INT] = DRGN_C_TYPE_LONG,
2098 [SPECIFIER_UNSIGNED_LONG_INT] = DRGN_C_TYPE_UNSIGNED_LONG,
2099 [SPECIFIER_LONG_LONG] = DRGN_C_TYPE_LONG_LONG,
2100 [SPECIFIER_LONG_LONG_INT] = DRGN_C_TYPE_LONG_LONG,
2101 [SPECIFIER_SIGNED_LONG_LONG_INT] = DRGN_C_TYPE_LONG_LONG,
2102 [SPECIFIER_UNSIGNED_LONG_LONG_INT] = DRGN_C_TYPE_UNSIGNED_LONG_LONG,
2103 [SPECIFIER_SIGNED_LONG_LONG] = DRGN_C_TYPE_LONG_LONG,
2104 [SPECIFIER_UNSIGNED_LONG_LONG] = DRGN_C_TYPE_UNSIGNED_LONG_LONG,
2105 [SPECIFIER_SIGNED] = DRGN_C_TYPE_INT,
2106 [SPECIFIER_UNSIGNED] = DRGN_C_TYPE_UNSIGNED_INT,
2107 [SPECIFIER_BOOL] = DRGN_C_TYPE_BOOL,
2108 [SPECIFIER_FLOAT] = DRGN_C_TYPE_FLOAT,
2109 [SPECIFIER_DOUBLE] = DRGN_C_TYPE_DOUBLE,
2110 [SPECIFIER_LONG_DOUBLE] = DRGN_C_TYPE_LONG_DOUBLE,
2111 };
2112
2113 enum drgn_primitive_type c_parse_specifier_list(const char *s)
2114 {
2115 struct drgn_error *err;
2116
2117 DRGN_C_FAMILY_LEXER(c_family_lexer, s, false);
2118 struct drgn_lexer *lexer = &c_family_lexer.lexer;
2119
2120 enum c_type_specifier specifier = SPECIFIER_NONE;
2121 for (;;) {
2122 struct drgn_token token;
2123
2124 err = drgn_lexer_pop(lexer, &token);
2125 if (err) {
2126 drgn_error_destroy(err);
2127 return DRGN_NOT_PRIMITIVE_TYPE;
2128 }
2129
2130 if (MIN_SPECIFIER_TOKEN <= token.kind &&
2131 token.kind <= MAX_SPECIFIER_TOKEN)
2132 specifier = specifier_transition[specifier][token.kind];
2133 else if (token.kind == C_TOKEN_EOF)
2134 break;
2135 else
2136 specifier = SPECIFIER_ERROR;
2137 if (specifier == SPECIFIER_ERROR)
2138 return DRGN_NOT_PRIMITIVE_TYPE;
2139 }
2140 return specifier_kind[specifier];
2141 }
2142
2143
2144 // The DWARF index currently includes template arguments in indexed names. So,
2145 // to be able to find a type with template arguments, we have to look it up with
2146 // the template arguments included. This looks for a C_TOKEN_TEMPLATE_ARGUMENTS
2147 // token after the identifier and returns the length from the beginning of the
2148 // identifier to the end of the template arguments.
2149 //
2150 // Note that this requires that the user formats the template arguments exactly
2151 // as they appear in DWARF (which can vary between compilers). In the future, it
2152 // might be better to properly parse and either normalize the template arguments
2153 // or look them up as an AST somehow.
2154 static struct drgn_error *cpp_append_to_identifier(
2155 struct drgn_lexer *lexer, const char *identifier, size_t *len_ret)
2156 {
2157 struct drgn_error *err;
2158
2159 // Only for C++.
2160 if (!((struct drgn_c_family_lexer *)lexer)->cpp)
2161 return NULL;
2162
2163 struct drgn_token token;
2164
2165 do {
2166 err = drgn_lexer_pop(lexer, &token);
2167 } while (!err && (token.kind == C_TOKEN_IDENTIFIER ||
2168 token.kind == C_TOKEN_COLON));
2169
2170 if (err)
2171 return err;
2172 if (token.kind != C_TOKEN_TEMPLATE_ARGUMENTS) {
2173 err = drgn_lexer_push(lexer, &token);
2174 if (err)
2175 return err;
2176 }
2177
2178 *len_ret = token.value + token.len - identifier;
2179 return NULL;
2180 }
2181
2182 static struct drgn_error *
2183 c_parse_specifier_qualifier_list(struct drgn_program *prog,
2184 struct drgn_lexer *lexer, const char *filename,
2185 struct drgn_qualified_type *ret)
2186 {
2187 struct drgn_error *err;
2188 enum c_type_specifier specifier = SPECIFIER_NONE;
2189 enum drgn_qualifiers qualifiers = 0;
2190 const char *identifier = NULL;
2191 size_t identifier_len = 0;
2192 int tag_token = C_TOKEN_EOF;
2193
2194 for (;;) {
2195 struct drgn_token token;
2196
2197 err = drgn_lexer_pop(lexer, &token);
2198 if (err)
2199 return err;
2200
2201 /* type-qualifier */
2202 if (MIN_QUALIFIER_TOKEN <= token.kind &&
2203 token.kind <= MAX_QUALIFIER_TOKEN) {
2204 qualifiers |= qualifier_from_token[token.kind];
2205 /* type-specifier */
2206 } else if (MIN_SPECIFIER_TOKEN <= token.kind &&
2207 token.kind <= MAX_SPECIFIER_TOKEN) {
2208 enum c_type_specifier prev_specifier;
2209
2210 if (tag_token != C_TOKEN_EOF) {
2211 return drgn_error_format(DRGN_ERROR_SYNTAX,
2212 "cannot combine '%s' with '%s'",
2213 keyword_spelling[token.kind],
2214 keyword_spelling[tag_token]);
2215 }
2216 if (identifier) {
2217 return drgn_error_format(DRGN_ERROR_SYNTAX,
2218 "cannot combine '%s' with identifier",
2219 keyword_spelling[token.kind]);
2220 }
2221 prev_specifier = specifier;
2222 specifier = specifier_transition[specifier][token.kind];
2223 if (specifier == SPECIFIER_ERROR) {
2224 return drgn_error_format(DRGN_ERROR_SYNTAX,
2225 "cannot combine '%s' with '%s'",
2226 keyword_spelling[token.kind],
2227 specifier_spelling[prev_specifier]);
2228 }
2229 } else if ((token.kind == C_TOKEN_IDENTIFIER ||
2230 token.kind == C_TOKEN_COLON) &&
2231 specifier == SPECIFIER_NONE && !identifier) {
2232 identifier = token.value;
2233 identifier_len = token.len;
2234 err = cpp_append_to_identifier(lexer, identifier,
2235 &identifier_len);
2236 if (err)
2237 return err;
2238 } else if (token.kind == C_TOKEN_STRUCT ||
2239 token.kind == C_TOKEN_UNION ||
2240 token.kind == C_TOKEN_CLASS ||
2241 token.kind == C_TOKEN_ENUM) {
2242 if (identifier) {
2243 return drgn_error_format(DRGN_ERROR_SYNTAX,
2244 "cannot combine '%s' with identifier",
2245 keyword_spelling[token.kind]);
2246 }
2247 if (specifier != SPECIFIER_NONE) {
2248 return drgn_error_format(DRGN_ERROR_SYNTAX,
2249 "cannot combine '%s' with '%s'",
2250 keyword_spelling[token.kind],
2251 specifier_spelling[specifier]);
2252 }
2253 tag_token = token.kind;
2254 err = drgn_lexer_pop(lexer, &token);
2255 if (err)
2256 return err;
2257 if (!(token.kind == C_TOKEN_IDENTIFIER ||
2258 token.kind == C_TOKEN_COLON)) {
2259 return drgn_error_format(DRGN_ERROR_SYNTAX,
2260 "expected identifier after '%s'",
2261 keyword_spelling[tag_token]);
2262
2263 }
2264 identifier = token.value;
2265 identifier_len = token.len;
2266 err = cpp_append_to_identifier(lexer, identifier,
2267 &identifier_len);
2268 if (err)
2269 return err;
2270 } else {
2271 err = drgn_lexer_push(lexer, &token);
2272 if (err)
2273 return err;
2274 break;
2275 }
2276 }
2277
2278 if (specifier == SPECIFIER_NONE) {
2279 uint64_t kinds;
2280 if (tag_token == C_TOKEN_STRUCT) {
2281 kinds = 1 << DRGN_TYPE_STRUCT;
2282 } else if (tag_token == C_TOKEN_UNION) {
2283 kinds = 1 << DRGN_TYPE_UNION;
2284 } else if (tag_token == C_TOKEN_CLASS) {
2285 kinds = 1 << DRGN_TYPE_CLASS;
2286 } else if (tag_token == C_TOKEN_ENUM) {
2287 kinds = 1 << DRGN_TYPE_ENUM;
2288 } else if (identifier) {
2289 if (identifier_len == sizeof("size_t") - 1 &&
2290 memcmp(identifier, "size_t",
2291 sizeof("size_t") - 1) == 0) {
2292 err = drgn_program_find_primitive_type(prog,
2293 DRGN_C_TYPE_SIZE_T,
2294 &ret->type);
2295 if (err)
2296 return err;
2297 ret->qualifiers = 0;
2298 goto out;
2299 } else if (identifier_len == sizeof("ptrdiff_t") - 1 &&
2300 memcmp(identifier, "ptrdiff_t",
2301 sizeof("ptrdiff_t") - 1) == 0) {
2302 err = drgn_program_find_primitive_type(prog,
2303 DRGN_C_TYPE_PTRDIFF_T,
2304 &ret->type);
2305 if (err)
2306 return err;
2307 ret->qualifiers = 0;
2308 goto out;
2309 } else if (((struct drgn_c_family_lexer *)lexer)->cpp) {
2310 kinds = ((1 << DRGN_TYPE_STRUCT)
2311 | (1 << DRGN_TYPE_UNION)
2312 | (1 << DRGN_TYPE_CLASS)
2313 | (1 << DRGN_TYPE_ENUM)
2314 | (1 << DRGN_TYPE_TYPEDEF));
2315 } else {
2316 kinds = 1 << DRGN_TYPE_TYPEDEF;
2317 }
2318 } else {
2319 return drgn_error_create(DRGN_ERROR_SYNTAX,
2320 "expected type specifier");
2321 }
2322
2323 err = drgn_program_find_type_impl(prog, kinds, identifier,
2324 identifier_len, filename,
2325 ret);
2326 if (err)
2327 return err;
2328 } else {
2329 err = drgn_program_find_primitive_type(prog,
2330 specifier_kind[specifier],
2331 &ret->type);
2332 if (err)
2333 return err;
2334 ret->qualifiers = 0;
2335 }
2336 out:
2337 ret->qualifiers |= qualifiers;
2338 return NULL;
2339 }
2340
2341 struct c_declarator {
2342 /* C_TOKEN_ASTERISK or C_TOKEN_LBRACKET. */
2343 int kind;
2344 enum drgn_qualifiers qualifiers;
2345 /* Only for C_TOKEN_LBRACKET. */
2346 bool is_complete;
2347 uint64_t length;
2348 struct c_declarator *next;
2349 };
2350
2351 /* These functions don't free the declarator list on error. */
2352 static struct drgn_error *
2353 c_parse_abstract_declarator(struct drgn_program *prog,
2354 struct drgn_lexer *lexer,
2355 struct c_declarator **outer,
2356 struct c_declarator **inner);
2357
2358 static struct drgn_error *
2359 c_parse_optional_type_qualifier_list(struct drgn_lexer *lexer,
2360 enum drgn_qualifiers *qualifiers)
2361 {
2362 struct drgn_error *err;
2363 struct drgn_token token;
2364
2365 *qualifiers = 0;
2366 for (;;) {
2367 err = drgn_lexer_pop(lexer, &token);
2368 if (err)
2369 return err;
2370
2371 if (token.kind < MIN_QUALIFIER_TOKEN ||
2372 token.kind > MAX_QUALIFIER_TOKEN) {
2373 err = drgn_lexer_push(lexer, &token);
2374 if (err)
2375 return err;
2376 return NULL;
2377 }
2378 *qualifiers |= qualifier_from_token[token.kind];
2379 }
2380 }
2381
2382 static struct drgn_error *
2383 c_parse_pointer(struct drgn_program *prog, struct drgn_lexer *lexer,
2384 struct c_declarator **outer, struct c_declarator **inner)
2385 {
2386 struct drgn_error *err;
2387 struct drgn_token token;
2388
2389 err = drgn_lexer_pop(lexer, &token);
2390 if (err)
2391 return err;
2392 if (token.kind != C_TOKEN_ASTERISK)
2393 return drgn_error_create(DRGN_ERROR_SYNTAX, "expected '*'");
2394
2395 *inner = NULL;
2396 for (;;) {
2397 struct c_declarator *tmp;
2398
2399 tmp = malloc(sizeof(*tmp));
2400 if (!tmp)
2401 return &drgn_enomem;
2402
2403 tmp->kind = C_TOKEN_ASTERISK;
2404 tmp->next = *outer;
2405 *outer = tmp;
2406
2407 err = c_parse_optional_type_qualifier_list(lexer,
2408 &(*outer)->qualifiers);
2409 if (err)
2410 return err;
2411 if (!*inner)
2412 *inner = *outer;
2413
2414 err = drgn_lexer_pop(lexer, &token);
2415 if (err)
2416 return err;
2417 if (token.kind != C_TOKEN_ASTERISK)
2418 return drgn_lexer_push(lexer, &token);
2419 }
2420 }
2421
2422 static struct drgn_error *
2423 c_parse_direct_abstract_declarator(struct drgn_program *prog,
2424 struct drgn_lexer *lexer,
2425 struct c_declarator **outer,
2426 struct c_declarator **inner)
2427 {
2428 struct drgn_error *err;
2429 struct drgn_token token;
2430
2431 *inner = NULL;
2432
2433 err = drgn_lexer_pop(lexer, &token);
2434 if (err)
2435 return err;
2436 if (token.kind == C_TOKEN_LPAREN) {
2437 struct drgn_token token2;
2438
2439 err = drgn_lexer_peek(lexer, &token2);
2440 if (err)
2441 return err;
2442 if (token2.kind == C_TOKEN_ASTERISK ||
2443 token2.kind == C_TOKEN_LPAREN ||
2444 token2.kind == C_TOKEN_LBRACKET) {
2445 err = c_parse_abstract_declarator(prog, lexer, outer,
2446 inner);
2447 if (err)
2448 return err;
2449 err = drgn_lexer_pop(lexer, &token2);
2450 if (err)
2451 return err;
2452 if (token2.kind != C_TOKEN_RPAREN) {
2453 return drgn_error_create(DRGN_ERROR_SYNTAX,
2454 "expected ')'");
2455 }
2456 err = drgn_lexer_pop(lexer, &token);
2457 if (err)
2458 return err;
2459 }
2460 }
2461
2462 for (;;) {
2463 if (token.kind == C_TOKEN_LBRACKET) {
2464 struct c_declarator *tmp;
2465
2466 err = drgn_lexer_pop(lexer, &token);
2467 if (err)
2468 return err;
2469
2470 tmp = malloc(sizeof(*tmp));
2471 if (!tmp)
2472 return &drgn_enomem;
2473
2474 tmp->kind = C_TOKEN_LBRACKET;
2475 tmp->qualifiers = 0;
2476 if (token.kind == C_TOKEN_NUMBER) {
2477 tmp->is_complete = true;
2478 err = c_token_to_u64(&token, &tmp->length);
2479 if (err) {
2480 free(tmp);
2481 return err;
2482 }
2483 err = drgn_lexer_pop(lexer, &token);
2484 if (err) {
2485 free(tmp);
2486 return err;
2487 }
2488 } else {
2489 tmp->is_complete = false;
2490 }
2491
2492 if (*inner) {
2493 tmp->next = (*inner)->next;
2494 *inner = (*inner)->next = tmp;
2495 } else {
2496 tmp->next = *outer;
2497 *outer = *inner = tmp;
2498 }
2499 if (token.kind != C_TOKEN_RBRACKET) {
2500 return drgn_error_create(DRGN_ERROR_SYNTAX,
2501 "expected ']'");
2502 }
2503 } else if (token.kind == C_TOKEN_LPAREN) {
2504 return drgn_error_create(DRGN_ERROR_SYNTAX,
2505 "function pointer types are not implemented");
2506 } else {
2507 err = drgn_lexer_push(lexer, &token);
2508 if (err)
2509 return err;
2510
2511 if (!*inner) {
2512 return drgn_error_create(DRGN_ERROR_SYNTAX,
2513 "expected abstract declarator");
2514 }
2515 return NULL;
2516 }
2517
2518 err = drgn_lexer_pop(lexer, &token);
2519 if (err)
2520 return err;
2521 }
2522 }
2523
2524 static struct drgn_error *
2525 c_parse_abstract_declarator(struct drgn_program *prog,
2526 struct drgn_lexer *lexer,
2527 struct c_declarator **outer,
2528 struct c_declarator **inner)
2529 {
2530 struct drgn_error *err;
2531 struct drgn_token token;
2532
2533 err = drgn_lexer_peek(lexer, &token);
2534 if (err)
2535 return err;
2536 if (token.kind == C_TOKEN_ASTERISK) {
2537 err = c_parse_pointer(prog, lexer, outer, inner);
2538 if (err)
2539 return err;
2540
2541 err = drgn_lexer_peek(lexer, &token);
2542 if (err)
2543 return err;
2544 if (token.kind == C_TOKEN_LPAREN ||
2545 token.kind == C_TOKEN_LBRACKET) {
2546 struct c_declarator *tmp;
2547
2548 err = c_parse_direct_abstract_declarator(prog, lexer,
2549 outer, &tmp);
2550 if (err)
2551 return err;
2552 }
2553 return NULL;
2554 } else {
2555 return c_parse_direct_abstract_declarator(prog, lexer, outer,
2556 inner);
2557 }
2558 }
2559
2560 /* This always frees the declarator list regardless of success or failure. */
2561 static struct drgn_error *
2562 c_type_from_declarator(struct drgn_program *prog,
2563 struct c_declarator *declarator,
2564 struct drgn_qualified_type *ret)
2565 {
2566 struct drgn_error *err;
2567
2568 if (!declarator)
2569 return NULL;
2570
2571 err = c_type_from_declarator(prog, declarator->next, ret);
2572 if (err) {
2573 free(declarator);
2574 return err;
2575 }
2576
2577 if (declarator->kind == C_TOKEN_ASTERISK) {
2578 uint64_t address_size;
2579 err = drgn_program_address_size(prog, &address_size);
2580 if (!err) {
2581 err = drgn_pointer_type_create(prog, *ret, address_size,
2582 DRGN_PROGRAM_ENDIAN,
2583 drgn_type_language(ret->type),
2584 &ret->type);
2585 }
2586 } else if (declarator->is_complete) {
2587 err = drgn_array_type_create(prog, *ret, declarator->length,
2588 drgn_type_language(ret->type),
2589 &ret->type);
2590 } else {
2591 err = drgn_incomplete_array_type_create(prog, *ret,
2592 drgn_type_language(ret->type),
2593 &ret->type);
2594 }
2595
2596 if (!err)
2597 ret->qualifiers = declarator->qualifiers;
2598 free(declarator);
2599 return err;
2600 }
2601
2602 static struct drgn_error *c_family_find_type(const struct drgn_language *lang,
2603 struct drgn_program *prog,
2604 const char *name,
2605 const char *filename,
2606 struct drgn_qualified_type *ret)
2607 {
2608 struct drgn_error *err;
2609
2610 DRGN_C_FAMILY_LEXER(c_family_lexer, name, lang == &drgn_language_cpp);
2611 struct drgn_lexer *lexer = &c_family_lexer.lexer;
2612
2613 err = c_parse_specifier_qualifier_list(prog, lexer, filename, ret);
2614 if (err)
2615 return err;
2616
2617 struct drgn_token token;
2618 err = drgn_lexer_pop(lexer, &token);
2619 if (err)
2620 return err;
2621 if (token.kind != C_TOKEN_EOF) {
2622 struct c_declarator *outer = NULL, *inner;
2623
2624 err = drgn_lexer_push(lexer, &token);
2625 if (err)
2626 return err;
2627
2628 err = c_parse_abstract_declarator(prog, lexer, &outer, &inner);
2629 if (err) {
2630 while (outer) {
2631 struct c_declarator *next;
2632
2633 next = outer->next;
2634 free(outer);
2635 outer = next;
2636 }
2637 return err;
2638 }
2639
2640 err = c_type_from_declarator(prog, outer, ret);
2641 if (err)
2642 return err;
2643
2644 err = drgn_lexer_pop(lexer, &token);
2645 if (err)
2646 return err;
2647 if (token.kind != C_TOKEN_EOF) {
2648 return drgn_error_create(DRGN_ERROR_SYNTAX,
2649 "extra tokens after type name");
2650 }
2651 }
2652
2653 return NULL;
2654 }
2655
2656 static struct drgn_error *
2657 c_family_type_subobject(struct drgn_type *type, const char *designator,
2658 bool expect_member,
2659 struct drgn_qualified_type *type_ret,
2660 uint64_t *bit_offset_ret, uint64_t *bit_field_size_ret)
2661 {
2662 struct drgn_error *err;
2663 struct drgn_program *prog = drgn_type_program(type);
2664
2665 DRGN_C_FAMILY_LEXER(c_family_lexer, designator,
2666 prog->lang == &drgn_language_cpp);
2667 struct drgn_lexer *lexer = &c_family_lexer.lexer;
2668
2669 struct drgn_qualified_type qualified_type = { type };
2670 uint64_t bit_field_size = 0;
2671 enum {
2672 START_ANY = INT_MIN,
2673 START_MEMBER,
2674 };
2675 int state = expect_member ? START_MEMBER : START_ANY;
2676 uint64_t bit_offset = 0;
2677 for (;;) {
2678 struct drgn_token token;
2679 err = drgn_lexer_pop(lexer, &token);
2680 if (err)
2681 return err;
2682
2683 switch (state) {
2684 case START_ANY:
2685 case START_MEMBER:
2686 case C_TOKEN_DOT:
2687 if (token.kind == C_TOKEN_IDENTIFIER) {
2688 struct drgn_type_member *member;
2689 uint64_t member_bit_offset;
2690 err = drgn_type_find_member_len(qualified_type.type,
2691 token.value,
2692 token.len,
2693 &member,
2694 &member_bit_offset);
2695 if (err)
2696 return err;
2697 if (__builtin_add_overflow(bit_offset,
2698 member_bit_offset,
2699 &bit_offset)) {
2700 return drgn_error_create(DRGN_ERROR_OVERFLOW,
2701 "offset is too large");
2702 }
2703 err = drgn_member_type(member, &qualified_type,
2704 &bit_field_size);
2705 if (err)
2706 return err;
2707 } else if (state == C_TOKEN_DOT) {
2708 return drgn_error_create(DRGN_ERROR_SYNTAX,
2709 "expected identifier after '.'");
2710 } else if (state == START_MEMBER) {
2711 return drgn_error_create(DRGN_ERROR_SYNTAX,
2712 "expected identifier");
2713 } else if (token.kind != C_TOKEN_LBRACKET) {
2714 return drgn_error_create(DRGN_ERROR_SYNTAX,
2715 "expected identifier or '['");
2716 }
2717 break;
2718 case C_TOKEN_IDENTIFIER:
2719 case C_TOKEN_RBRACKET:
2720 switch (token.kind) {
2721 case C_TOKEN_EOF:
2722 if (type_ret)
2723 *type_ret = qualified_type;
2724 if (bit_offset_ret)
2725 *bit_offset_ret = bit_offset;
2726 if (bit_field_size_ret)
2727 *bit_field_size_ret = bit_field_size;
2728 return NULL;
2729 case C_TOKEN_DOT:
2730 case C_TOKEN_LBRACKET:
2731 break;
2732 default:
2733 if (state == C_TOKEN_IDENTIFIER) {
2734 return drgn_error_create(DRGN_ERROR_SYNTAX,
2735 "expected '.' or '[' after identifier");
2736 } else {
2737 return drgn_error_create(DRGN_ERROR_SYNTAX,
2738 "expected '.' or '[' after ']'");
2739 }
2740 }
2741 break;
2742 case C_TOKEN_LBRACKET:
2743 if (token.kind == C_TOKEN_NUMBER) {
2744 struct drgn_type *underlying_type;
2745 struct drgn_type *element_type;
2746 uint64_t index, bit_size, element_offset;
2747
2748 err = c_token_to_u64(&token, &index);
2749 if (err)
2750 return err;
2751
2752 underlying_type = drgn_underlying_type(qualified_type.type);
2753 if (drgn_type_kind(underlying_type) != DRGN_TYPE_ARRAY) {
2754 return drgn_type_error("'%s' is not an array",
2755 qualified_type.type);
2756 }
2757 element_type =
2758 drgn_type_type(underlying_type).type;
2759 err = drgn_type_bit_size(element_type,
2760 &bit_size);
2761 if (err)
2762 return err;
2763 if (__builtin_mul_overflow(index, bit_size,
2764 &element_offset) ||
2765 __builtin_add_overflow(bit_offset,
2766 element_offset,
2767 &bit_offset)) {
2768 return drgn_error_create(DRGN_ERROR_OVERFLOW,
2769 "offset is too large");
2770 }
2771 qualified_type = (struct drgn_qualified_type){ element_type };
2772 bit_field_size = 0;
2773 } else {
2774 return drgn_error_create(DRGN_ERROR_SYNTAX,
2775 "expected number after '['");
2776 }
2777 break;
2778 case C_TOKEN_NUMBER:
2779 if (token.kind != C_TOKEN_RBRACKET) {
2780 return drgn_error_create(DRGN_ERROR_SYNTAX,
2781 "expected ']' after number");
2782 }
2783 break;
2784 default:
2785 UNREACHABLE();
2786 }
2787 state = token.kind;
2788 }
2789 }
2790
2791 static struct drgn_error *c_integer_literal(struct drgn_object *res,
2792 uint64_t uvalue)
2793 {
2794 static const enum drgn_primitive_type types[] = {
2795 DRGN_C_TYPE_INT,
2796 DRGN_C_TYPE_LONG,
2797 DRGN_C_TYPE_LONG_LONG,
2798 DRGN_C_TYPE_UNSIGNED_LONG_LONG,
2799 };
2800 struct drgn_error *err;
2801
2802 unsigned int bits = fls(uvalue);
2803 struct drgn_qualified_type qualified_type;
2804 qualified_type.qualifiers = 0;
2805 array_for_each(type, types) {
2806 err = drgn_program_find_primitive_type(drgn_object_program(res),
2807 *type,
2808 &qualified_type.type);
2809 if (err)
2810 return err;
2811
2812 if (drgn_type_is_signed(qualified_type.type) &&
2813 bits < 8 * drgn_type_size(qualified_type.type)) {
2814 return drgn_object_set_signed(res,
2815 qualified_type,
2816 uvalue, 0);
2817 } else if (!drgn_type_is_signed(qualified_type.type) &&
2818 bits <= 8 * drgn_type_size(qualified_type.type)) {
2819 return drgn_object_set_unsigned(res, qualified_type,
2820 uvalue, 0);
2821 }
2822 }
2823 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
2824 "integer literal is too large");
2825 }
2826
2827 static struct drgn_error *c_bool_literal(struct drgn_object *res, bool bvalue)
2828 {
2829 struct drgn_error *err;
2830 struct drgn_qualified_type qualified_type;
2831
2832 err = drgn_program_find_primitive_type(drgn_object_program(res),
2833 DRGN_C_TYPE_INT,
2834 &qualified_type.type);
2835 if (err)
2836 return err;
2837 qualified_type.qualifiers = 0;
2838 return drgn_object_set_signed(res, qualified_type, bvalue, 0);
2839 }
2840
2841 static struct drgn_error *c_float_literal(struct drgn_object *res,
2842 double fvalue)
2843 {
2844 struct drgn_error *err;
2845 struct drgn_qualified_type qualified_type;
2846
2847 err = drgn_program_find_primitive_type(drgn_object_program(res),
2848 DRGN_C_TYPE_DOUBLE,
2849 &qualified_type.type);
2850 if (err)
2851 return err;
2852 qualified_type.qualifiers = 0;
2853 return drgn_object_set_float(res, qualified_type, fvalue);
2854 }
2855
2856 static const int c_integer_conversion_rank[] = {
2857 [DRGN_C_TYPE_BOOL] = 0,
2858 [DRGN_C_TYPE_CHAR] = 1,
2859 [DRGN_C_TYPE_SIGNED_CHAR] = 1,
2860 [DRGN_C_TYPE_UNSIGNED_CHAR] = 1,
2861 [DRGN_C_TYPE_SHORT] = 2,
2862 [DRGN_C_TYPE_UNSIGNED_SHORT] = 2,
2863 [DRGN_C_TYPE_INT] = 3,
2864 [DRGN_C_TYPE_UNSIGNED_INT] = 3,
2865 [DRGN_C_TYPE_LONG] = 4,
2866 [DRGN_C_TYPE_UNSIGNED_LONG] = 4,
2867 [DRGN_C_TYPE_LONG_LONG] = 5,
2868 [DRGN_C_TYPE_UNSIGNED_LONG_LONG] = 5,
2869 };
2870
2871 static bool c_can_represent_all_values(struct drgn_type *type1,
2872 uint64_t bit_field_size1,
2873 struct drgn_type *type2,
2874 uint64_t bit_field_size2)
2875 {
2876 uint64_t width1, width2;
2877 bool is_signed1, is_signed2;
2878
2879 if (drgn_type_kind(type1) == DRGN_TYPE_BOOL) {
2880 width1 = 1;
2881 is_signed1 = false;
2882 } else {
2883 width1 = (bit_field_size1 ? bit_field_size1 :
2884 8 * drgn_type_size(type1));
2885 is_signed1 = drgn_type_is_signed(type1);
2886 }
2887 if (drgn_type_kind(type2) == DRGN_TYPE_BOOL) {
2888 width2 = 1;
2889 is_signed2 = false;
2890 } else {
2891 width2 = (bit_field_size2 ? bit_field_size2 :
2892 8 * drgn_type_size(type2));
2893 is_signed2 = drgn_type_is_signed(type2);
2894 }
2895
2896 if (is_signed1 == is_signed2)
2897 return width1 >= width2;
2898 else if (is_signed1 && !is_signed2)
2899 return width1 > width2;
2900 else
2901 return false;
2902 }
2903
2904 static struct drgn_error *c_integer_promotions(struct drgn_program *prog,
2905 struct drgn_operand_type *type)
2906 {
2907 struct drgn_error *err;
2908 enum drgn_primitive_type primitive;
2909 struct drgn_type *int_type;
2910
2911 switch (drgn_type_kind(type->underlying_type)) {
2912 case DRGN_TYPE_ENUM:
2913 /* Convert the enum to its compatible type. */
2914 type->type = type->underlying_type =
2915 drgn_type_type(type->underlying_type).type;
2916 if (!type->type) {
2917 return drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT,
2918 "operand cannot have incomplete enum type");
2919 }
2920 break;
2921 case DRGN_TYPE_INT:
2922 case DRGN_TYPE_BOOL:
2923 break;
2924 default:
2925 return NULL;
2926 }
2927
2928 primitive = drgn_type_primitive(type->underlying_type);
2929 /*
2930 * Integer promotions are performed on types whose integer conversion
2931 * rank is less than or equal to the rank of int and unsigned int.
2932 *
2933 * If this isn't a standard integer type, then we don't know the rank,
2934 * so we may need to promote it. According to the C standard, "the rank
2935 * of a signed integer type shall be greater than the rank of any signed
2936 * integer type with less precision", and "the rank of any standard
2937 * integer type shall be greater than the rank of any extended integer
2938 * type with the same width". If an extended signed integer type has
2939 * less precision than int, or the same width as int, then all of its
2940 * values can be represented by int (and likewise for an extended
2941 * unsigned integer type and unsigned int). Therefore, an extended
2942 * integer type should be promoted iff all of its values can be
2943 * represented by int or unsigned int.
2944 *
2945 * Integer promotions are also performed on bit fields. The C standard
2946 * only requires that bit fields of type _Bool, int, or unsigned int are
2947 * supported, so it does not define how integer promotions should affect
2948 * a bit field which cannot be represented by int or unsigned int. Clang
2949 * promotes it to the full width, but GCC does not. We implement the GCC
2950 * behavior of preserving the width.
2951 */
2952 if (primitive >= array_size(c_integer_conversion_rank) ||
2953 type->bit_field_size) {
2954 err = drgn_program_find_primitive_type(prog, DRGN_C_TYPE_INT,
2955 &int_type);
2956 if (err)
2957 return err;
2958 if (c_can_represent_all_values(int_type, 0,
2959 type->underlying_type,
2960 type->bit_field_size)) {
2961 type->type = type->underlying_type = int_type;
2962 type->bit_field_size = 0;
2963 return NULL;
2964 }
2965
2966 err = drgn_program_find_primitive_type(prog,
2967 DRGN_C_TYPE_UNSIGNED_INT,
2968 &int_type);
2969 if (err)
2970 return err;
2971 if (c_can_represent_all_values(int_type, 0,
2972 type->underlying_type,
2973 type->bit_field_size)) {
2974 type->type = type->underlying_type = int_type;
2975 type->bit_field_size = 0;
2976 }
2977 return NULL;
2978 }
2979
2980 if (primitive == DRGN_C_TYPE_INT ||
2981 primitive == DRGN_C_TYPE_UNSIGNED_INT ||
2982 c_integer_conversion_rank[primitive] >
2983 c_integer_conversion_rank[DRGN_C_TYPE_INT])
2984 return NULL;
2985
2986 /*
2987 * If int can represent all values of the original type, then the result
2988 * is int. Otherwise, the result is unsigned int.
2989 */
2990 err = drgn_program_find_primitive_type(prog, DRGN_C_TYPE_INT,
2991 &int_type);
2992 if (err)
2993 return err;
2994 if (c_can_represent_all_values(int_type, 0, type->underlying_type, 0)) {
2995 type->type = int_type;
2996 } else {
2997 err = drgn_program_find_primitive_type(prog,
2998 DRGN_C_TYPE_UNSIGNED_INT,
2999 &type->type);
3000 if (err)
3001 return err;
3002 }
3003 type->underlying_type = type->type;
3004 return NULL;
3005 }
3006
3007 static struct drgn_error *
3008 c_corresponding_unsigned_type(struct drgn_program *prog,
3009 enum drgn_primitive_type type,
3010 struct drgn_type **ret)
3011 {
3012 switch (type) {
3013 /*
3014 * char, signed char, and short are promoted to int, so we don't need to
3015 * handle them here.
3016 */
3017 case DRGN_C_TYPE_INT:
3018 return drgn_program_find_primitive_type(prog,
3019 DRGN_C_TYPE_UNSIGNED_INT,
3020 ret);
3021 case DRGN_C_TYPE_LONG:
3022 return drgn_program_find_primitive_type(prog,
3023 DRGN_C_TYPE_UNSIGNED_LONG,
3024 ret);
3025 case DRGN_C_TYPE_LONG_LONG:
3026 return drgn_program_find_primitive_type(prog,
3027 DRGN_C_TYPE_UNSIGNED_LONG_LONG,
3028 ret);
3029 default:
3030 UNREACHABLE();
3031 }
3032 }
3033
3034 static struct drgn_error *c_common_real_type(struct drgn_program *prog,
3035 struct drgn_operand_type *type1,
3036 struct drgn_operand_type *type2,
3037 struct drgn_operand_type *ret)
3038 {
3039 struct drgn_error *err;
3040 enum drgn_primitive_type primitive1, primitive2;
3041 bool is_float1, is_float2;
3042 bool is_signed1, is_signed2;
3043 int rank_cmp;
3044
3045 ret->qualifiers = 0;
3046
3047 /*
3048 * Strictly, the rules are:
3049 *
3050 * If either operand is long double, then the result is long double.
3051 * Otherwise, if either operand is double, then the result is double.
3052 * Otherwise, if either operand is float, then the result is float.
3053 *
3054 * However, we also have to handle other floating types not in the
3055 * standard. Thus, the result is always the larger type, with ties
3056 * broken in the order unknown > long double > double > float.
3057 */
3058 is_float1 = drgn_type_kind(type1->underlying_type) == DRGN_TYPE_FLOAT;
3059 is_float2 = drgn_type_kind(type2->underlying_type) == DRGN_TYPE_FLOAT;
3060 if (is_float1 && is_float2) {
3061 uint64_t size1, size2;
3062
3063 size1 = drgn_type_size(type1->underlying_type);
3064 size2 = drgn_type_size(type2->underlying_type);
3065 if (size1 > size2)
3066 goto ret1;
3067 else if (size2 > size1)
3068 goto ret2;
3069 else if (drgn_type_primitive(type1->underlying_type) >
3070 drgn_type_primitive(type2->underlying_type))
3071 goto ret1;
3072 else
3073 goto ret2;
3074 } else if (is_float1) {
3075 goto ret1;
3076 } else if (is_float2) {
3077 goto ret2;
3078 }
3079
3080 /*
3081 * Otherwise, the integer promotions are performed before applying the
3082 * following rules.
3083 */
3084 err = c_integer_promotions(prog, type1);
3085 if (err)
3086 return err;
3087 err = c_integer_promotions(prog, type2);
3088 if (err)
3089 return err;
3090
3091 is_signed1 = drgn_type_is_signed(type1->underlying_type);
3092 is_signed2 = drgn_type_is_signed(type2->underlying_type);
3093
3094 /*
3095 * The C standard only requires that bit fields of type _Bool, int, or
3096 * unsigned int are supported, which are always promoted to int or
3097 * unsigned int, so it does not define how to find the common real type
3098 * when one or both of the operands are bit fields. GCC seems to use the
3099 * wider operand, or the unsigned operand if they have equal width. As
3100 * usual, we pick type2 if the two types are equivalent.
3101 */
3102 if (type1->bit_field_size || type2->bit_field_size) {
3103 uint64_t width1, width2;
3104
3105 width1 = (type1->bit_field_size ? type1->bit_field_size :
3106 8 * drgn_type_size(type1->type));
3107 width2 = (type2->bit_field_size ? type2->bit_field_size :
3108 8 * drgn_type_size(type2->type));
3109 if (width1 < width2 ||
3110 (width1 == width2 && (!is_signed2 || is_signed1)))
3111 goto ret2;
3112 else
3113 goto ret1;
3114 }
3115
3116 primitive1 = drgn_type_primitive(type1->underlying_type);
3117 primitive2 = drgn_type_primitive(type2->underlying_type);
3118
3119 if (primitive1 != DRGN_NOT_PRIMITIVE_TYPE &&
3120 primitive2 != DRGN_NOT_PRIMITIVE_TYPE) {
3121 /*
3122 * If both operands have the same type, then no further
3123 * conversions are needed.
3124 *
3125 * We can return either type1 or type2 here; it only makes a
3126 * difference for typedefs. Arbitrarily pick type2 because
3127 * that's what GCC seems to do (Clang always throws away the
3128 * typedef).
3129 */
3130 if (primitive1 == primitive2)
3131 goto ret2;
3132
3133 /* Ranks are small, so this won't overflow. */
3134 rank_cmp = (c_integer_conversion_rank[primitive1] -
3135 c_integer_conversion_rank[primitive2]);
3136 } else {
3137 /*
3138 * We don't know the rank of non-standard integer types.
3139 * However, we can usually compare their ranks, because
3140 * according to the C standard, "the rank of a signed integer
3141 * type shall be greater than the rank of any signed integer
3142 * type with less precision", "the rank of any unsigned integer
3143 * type shall equal the rank of the corresponding signed integer
3144 * type", and "the rank of any standard integer type shall be
3145 * greater than the rank of any extended integer type with the
3146 * same width". The only case where we can't is if both types
3147 * are non-standard and have the same size; we treat them as
3148 * having equal rank in this case.
3149 */
3150 uint64_t size1, size2;
3151
3152 size1 = drgn_type_size(type1->underlying_type);
3153 size2 = drgn_type_size(type2->underlying_type);
3154 if (size1 == size2 && primitive1 == DRGN_NOT_PRIMITIVE_TYPE &&
3155 primitive2 == DRGN_NOT_PRIMITIVE_TYPE)
3156 rank_cmp = 0;
3157 else if ((size1 == size2 && primitive2 != DRGN_NOT_PRIMITIVE_TYPE) ||
3158 size1 < size2)
3159 rank_cmp = -1;
3160 else
3161 rank_cmp = 1;
3162 }
3163
3164 /*
3165 * Otherwise, if both operands have signed integer types or both have
3166 * unsigned integer types, then the result is the type of the operand
3167 * with the greater rank.
3168 */
3169 if (is_signed1 == is_signed2) {
3170 if (rank_cmp > 0)
3171 goto ret1;
3172 else
3173 goto ret2;
3174 }
3175
3176 /*
3177 * Otherwise, if the operand that has unsigned integer type has rank
3178 * greater or equal to the rank of the type of the other operand, then
3179 * the result is the unsigned integer type.
3180 */
3181 if (!is_signed1 && rank_cmp >= 0)
3182 goto ret1;
3183 else if (!is_signed2 && rank_cmp <= 0)
3184 goto ret2;
3185
3186 /*
3187 * Otherwise, if the type of the operand with signed integer type can
3188 * represent all of the values of the type of the operand with unsigned
3189 * integer type, then the result is the signed integer type.
3190 */
3191 if (is_signed1 && c_can_represent_all_values(type1->underlying_type, 0,
3192 type2->underlying_type, 0))
3193 goto ret1;
3194 if (is_signed2 && c_can_represent_all_values(type2->underlying_type, 0,
3195 type1->underlying_type, 0))
3196 goto ret2;
3197
3198 /*
3199 * Otherwise, the result is the unsigned integer type corresponding to
3200 * the type of the operand with signed integer type.
3201 *
3202 * Note that this case is not reached for non-standard types: if the
3203 * types have different signs and the signed integer type has greater
3204 * rank, then it must have greater size and thus be able to represent
3205 * all values of the unsigned integer type.
3206 */
3207 err = c_corresponding_unsigned_type(prog,
3208 is_signed1 ? primitive1 : primitive2,
3209 &ret->type);
3210 if (err)
3211 return err;
3212 ret->underlying_type = ret->type;
3213 ret->bit_field_size = 0;
3214 return NULL;
3215
3216 ret1:
3217 *ret = *type1;
3218 return NULL;
3219 ret2:
3220 *ret = *type2;
3221 return NULL;
3222 }
3223
3224 static struct drgn_error *
3225 c_types_compatible_impl(struct drgn_qualified_type qualified_type1,
3226 struct drgn_qualified_type qualified_type2,
3227 bool *ret)
3228 {
3229 struct drgn_error *err;
3230
3231 // The types must have the same qualifiers.
3232 if (qualified_type1.qualifiers != qualified_type2.qualifiers) {
3233 *ret = false;
3234 return NULL;
3235 }
3236
3237 struct drgn_type *type1 = drgn_underlying_type(qualified_type1.type);
3238 struct drgn_type *type2 = drgn_underlying_type(qualified_type2.type);
3239
3240 // If the type descriptors are the same, then the types are definitely
3241 // compatible.
3242 if (type1 == type2)
3243 return NULL;
3244
3245 if (drgn_type_kind(type1) != drgn_type_kind(type2)) {
3246 // Enum types are compatible with their compatible integer type.
3247 // but not with different enum types with the same compatible
3248 // integer type.
3249 if (drgn_type_kind(type1) == DRGN_TYPE_ENUM) {
3250 qualified_type1.type = drgn_type_type(type1).type;
3251 if (qualified_type1.type) {
3252 return c_types_compatible_impl(qualified_type1,
3253 qualified_type2,
3254 ret);
3255 }
3256 } else if (drgn_type_kind(type2) == DRGN_TYPE_ENUM) {
3257 qualified_type2.type = drgn_type_type(type2).type;
3258 if (qualified_type2.type) {
3259 return c_types_compatible_impl(qualified_type1,
3260 qualified_type2,
3261 ret);
3262 }
3263 }
3264 *ret = false;
3265 return NULL;
3266 }
3267
3268 SWITCH_ENUM(drgn_type_kind(type1)) {
3269 case DRGN_TYPE_VOID:
3270 case DRGN_TYPE_INT:
3271 case DRGN_TYPE_BOOL:
3272 case DRGN_TYPE_FLOAT:
3273 // These types are deduplicated, so if they were compatible they
3274 // would have had the same type descriptor.
3275 *ret = false;
3276 return NULL;
3277 case DRGN_TYPE_STRUCT:
3278 case DRGN_TYPE_UNION:
3279 case DRGN_TYPE_CLASS: {
3280 // It's expensive to check all of the members, so we do a sloppy
3281 // check: if the tag and size are the same, then the types are
3282 // _probably_ compatible.
3283 if (drgn_type_is_complete(type1) && drgn_type_is_complete(type2)
3284 && drgn_type_size(type1) != drgn_type_size(type2)) {
3285 *ret = false;
3286 return NULL;
3287 }
3288 const char *tag1 = drgn_type_tag(type1);
3289 const char *tag2 = drgn_type_tag(type2);
3290 if ((!tag1 != !tag2) || (tag1 && strcmp(tag1, tag2) != 0))
3291 *ret = false;
3292 return NULL;
3293 }
3294 case DRGN_TYPE_ENUM: {
3295 // We do a similar sloppy check here: if the tag and compatible
3296 // type are the same, then the types are _probably_ compatible.
3297 if (drgn_type_is_complete(type1) && drgn_type_is_complete(type2)
3298 && drgn_underlying_type(drgn_type_type(type1).type)
3299 != drgn_underlying_type(drgn_type_type(type2).type)) {
3300 *ret = false;
3301 return NULL;
3302 }
3303 const char *tag1 = drgn_type_tag(type1);
3304 const char *tag2 = drgn_type_tag(type2);
3305 if ((!tag1 != !tag2) || (tag1 && strcmp(tag1, tag2) != 0))
3306 *ret = false;
3307 return NULL;
3308 }
3309 case DRGN_TYPE_POINTER:
3310 // The types are compatible iff their referenced types are
3311 // compatible.
3312 return c_types_compatible_impl(drgn_type_type(type1),
3313 drgn_type_type(type2), ret);
3314 case DRGN_TYPE_ARRAY:
3315 // The types are compatible iff their element types are
3316 // compatible and, if both types are complete, their lengths are
3317 // equal.
3318 if (drgn_type_is_complete(type1) && drgn_type_is_complete(type2)
3319 && drgn_type_length(type1) != drgn_type_length(type2)) {
3320 *ret = false;
3321 return NULL;
3322 }
3323 return c_types_compatible_impl(drgn_type_type(type1),
3324 drgn_type_type(type2), ret);
3325 case DRGN_TYPE_FUNCTION: {
3326 // The types are compatible iff their return types are
3327 // compatible, they have the same number of parameters, their
3328 // corresponding parameter types are compatible, and neither is
3329 // variadic or both are variadic.
3330 //
3331 // This is expensive, but there's no good shortcut like for
3332 // structs and enums.
3333 size_t num_parameters = drgn_type_num_parameters(type1);
3334 if (num_parameters != drgn_type_num_parameters(type2)
3335 || drgn_type_is_variadic(type1)
3336 != drgn_type_is_variadic(type2)) {
3337 *ret = false;
3338 return NULL;
3339 }
3340 err = c_types_compatible_impl(drgn_type_type(type1),
3341 drgn_type_type(type2),
3342 ret);
3343 if (err || !*ret)
3344 return err;
3345 struct drgn_type_parameter *parameters1 =
3346 drgn_type_parameters(type1);
3347 struct drgn_type_parameter *parameters2 =
3348 drgn_type_parameters(type2);
3349 for (size_t i = 0; i < num_parameters; i++) {
3350 struct drgn_qualified_type parameter_type1;
3351 err = drgn_parameter_type(&parameters1[i],
3352 &parameter_type1);
3353 if (err)
3354 return err;
3355 struct drgn_qualified_type parameter_type2;
3356 err = drgn_parameter_type(&parameters2[i],
3357 &parameter_type2);
3358 if (err)
3359 return err;
3360 err = c_types_compatible_impl(parameter_type1,
3361 parameter_type2, ret);
3362 if (err || !*ret)
3363 return err;
3364 }
3365 return NULL;
3366 }
3367 // This is already the underlying type, so it can't be a typedef.
3368 case DRGN_TYPE_TYPEDEF:
3369 default:
3370 UNREACHABLE();
3371 }
3372 }
3373
3374 static struct drgn_error *
3375 c_types_compatible(struct drgn_qualified_type qualified_type1,
3376 struct drgn_qualified_type qualified_type2,
3377 bool *ret)
3378 {
3379 *ret = true;
3380 return c_types_compatible_impl(qualified_type1, qualified_type2, ret);
3381 }
3382
3383 static struct drgn_error *c_operand_type(const struct drgn_object *obj,
3384 struct drgn_operand_type *type_ret,
3385 bool *is_pointer_ret,
3386 uint64_t *referenced_size_ret)
3387 {
3388 struct drgn_error *err;
3389
3390 *type_ret = drgn_object_operand_type(obj);
3391 switch (drgn_type_kind(type_ret->underlying_type)) {
3392 case DRGN_TYPE_ARRAY: {
3393 uint64_t address_size;
3394 err = drgn_program_address_size(drgn_object_program(obj),
3395 &address_size);
3396 if (err)
3397 return err;
3398 err = drgn_pointer_type_create(drgn_object_program(obj),
3399 drgn_type_type(type_ret->underlying_type),
3400 address_size,
3401 DRGN_PROGRAM_ENDIAN,
3402 drgn_type_language(type_ret->underlying_type),
3403 &type_ret->type);
3404 if (err)
3405 return err;
3406 type_ret->underlying_type = type_ret->type;
3407 break;
3408 }
3409 case DRGN_TYPE_FUNCTION: {
3410 struct drgn_qualified_type function_type =
3411 drgn_operand_type_qualified(type_ret);
3412 uint64_t address_size;
3413 err = drgn_program_address_size(drgn_object_program(obj),
3414 &address_size);
3415 if (err)
3416 return err;
3417 err = drgn_pointer_type_create(drgn_object_program(obj),
3418 function_type, address_size,
3419 DRGN_PROGRAM_ENDIAN,
3420 drgn_type_language(type_ret->underlying_type),
3421 &type_ret->type);
3422 if (err)
3423 return err;
3424 type_ret->underlying_type = type_ret->type;
3425 break;
3426 }
3427 default:
3428 err = drgn_type_with_byte_order(&type_ret->type,
3429 &type_ret->underlying_type,
3430 DRGN_PROGRAM_ENDIAN);
3431 if (err)
3432 return err;
3433 break;
3434 }
3435 type_ret->qualifiers = 0;
3436
3437 if (is_pointer_ret) {
3438 struct drgn_type *type = type_ret->underlying_type;
3439 *is_pointer_ret = drgn_type_kind(type) == DRGN_TYPE_POINTER;
3440 if (*is_pointer_ret && referenced_size_ret) {
3441 struct drgn_type *referenced_type =
3442 drgn_underlying_type(drgn_type_type(type).type);
3443 if (drgn_type_kind(referenced_type) == DRGN_TYPE_VOID) {
3444 *referenced_size_ret = 1;
3445 } else {
3446 err = drgn_type_sizeof(referenced_type,
3447 referenced_size_ret);
3448 if (err)
3449 return err;
3450 }
3451 }
3452 }
3453 return NULL;
3454 }
3455
3456 static struct drgn_error *c_op_cast(struct drgn_object *res,
3457 struct drgn_qualified_type qualified_type,
3458 const struct drgn_object *obj)
3459 {
3460 struct drgn_error *err;
3461
3462 struct drgn_object_type type;
3463 err = drgn_object_type(qualified_type, 0, &type);
3464 if (err)
3465 return err;
3466
3467 switch (drgn_type_kind(type.underlying_type)) {
3468 case DRGN_TYPE_VOID:
3469 drgn_object_set_absent_internal(res, &type,
3470 DRGN_ABSENCE_REASON_OTHER);
3471 return NULL;
3472 case DRGN_TYPE_BOOL: {
3473 bool truthy;
3474 err = drgn_object_bool(obj, &truthy);
3475 if (err)
3476 return err;
3477 return drgn_object_set_unsigned_internal(res, &type, truthy);
3478 }
3479 default:
3480 break;
3481 }
3482
3483 struct drgn_operand_type obj_type;
3484 err = c_operand_type(obj, &obj_type, NULL, NULL);
3485 if (err)
3486 return err;
3487 return drgn_op_cast(res, &type, obj, &obj_type);
3488 }
3489
3490 static struct drgn_error *
3491 c_op_implicit_convert(struct drgn_object *res,
3492 struct drgn_qualified_type qualified_type,
3493 uint64_t bit_field_size,
3494 const struct drgn_object *obj)
3495 {
3496 struct drgn_error *err;
3497
3498 struct drgn_object_type type;
3499 err = drgn_object_type(qualified_type, bit_field_size, &type);
3500 if (err)
3501 return err;
3502
3503 if (drgn_type_kind(type.underlying_type) == DRGN_TYPE_BOOL) {
3504 bool truthy;
3505 err = drgn_object_bool(obj, &truthy);
3506 if (err)
3507 return err;
3508 return drgn_object_set_unsigned_internal(res, &type, truthy);
3509 }
3510
3511 struct drgn_operand_type obj_type;
3512 err = c_operand_type(obj, &obj_type, NULL, NULL);
3513 if (err)
3514 return err;
3515
3516 SWITCH_ENUM(drgn_type_kind(type.underlying_type)) {
3517 case DRGN_TYPE_INT:
3518 case DRGN_TYPE_FLOAT:
3519 case DRGN_TYPE_ENUM:
3520 switch (drgn_type_kind(obj_type.underlying_type)) {
3521 case DRGN_TYPE_INT:
3522 case DRGN_TYPE_BOOL:
3523 case DRGN_TYPE_FLOAT:
3524 case DRGN_TYPE_ENUM:
3525 break;
3526 default:
3527 goto incompatible_type_error;
3528 }
3529 break;
3530 case DRGN_TYPE_STRUCT:
3531 case DRGN_TYPE_UNION:
3532 case DRGN_TYPE_CLASS: {
3533 struct drgn_qualified_type unqualified_type1 = {
3534 .type = type.underlying_type,
3535 };
3536 struct drgn_qualified_type unqualified_type2 = {
3537 .type = obj_type.underlying_type,
3538 };
3539 bool compatible;
3540 err = c_types_compatible(unqualified_type1, unqualified_type2,
3541 &compatible);
3542 if (err)
3543 return err;
3544 if (!compatible)
3545 goto incompatible_type_error;
3546 return drgn_object_fragment_internal(res, obj, &type, 0, 0);
3547 }
3548 case DRGN_TYPE_POINTER: {
3549 if (drgn_type_kind(obj_type.underlying_type)
3550 != DRGN_TYPE_POINTER)
3551 goto incompatible_type_error;
3552
3553 struct drgn_qualified_type referenced_type =
3554 drgn_type_type(type.underlying_type);
3555 referenced_type.type =
3556 drgn_underlying_type(referenced_type.type);
3557 struct drgn_qualified_type obj_referenced_type =
3558 drgn_type_type(obj_type.underlying_type);
3559 obj_referenced_type.type =
3560 drgn_underlying_type(obj_referenced_type.type);
3561
3562 // The type pointed to by the left must have all of the
3563 // qualifiers of the type pointed to by the right:
3564 // (lhs.qualifiers & rhs.qualifiers) == rhs.qualifiers.
3565 // We mask here and do the equality test below or in
3566 // c_types_compatible().
3567 referenced_type.qualifiers &= obj_referenced_type.qualifiers;
3568
3569 // The type pointed to by the left and the type pointed to by
3570 // the right must be compatible, or at least one must be void.
3571 if (drgn_type_kind(referenced_type.type) == DRGN_TYPE_VOID
3572 || drgn_type_kind(obj_referenced_type.type) == DRGN_TYPE_VOID) {
3573 if (referenced_type.qualifiers
3574 != obj_referenced_type.qualifiers)
3575 goto incompatible_type_error;
3576 } else {
3577 bool compatible;
3578 err = c_types_compatible(referenced_type, obj_referenced_type,
3579 &compatible);
3580 if (err)
3581 return err;
3582 if (!compatible)
3583 goto incompatible_type_error;
3584 }
3585 break;
3586 }
3587 case DRGN_TYPE_VOID:
3588 case DRGN_TYPE_ARRAY:
3589 case DRGN_TYPE_FUNCTION:
3590 return drgn_qualified_type_error("cannot convert to '%s'",
3591 qualified_type);
3592 // We handled bool earlier.
3593 case DRGN_TYPE_BOOL:
3594 // This is already the underlying type, so it can't be a typedef.
3595 case DRGN_TYPE_TYPEDEF:
3596 default:
3597 UNREACHABLE();
3598 }
3599
3600 return drgn_op_cast(res, &type, obj, &obj_type);
3601
3602 incompatible_type_error:
3603 return drgn_2_qualified_types_error("cannot convert '%s' to incompatible type '%s'",
3604 drgn_object_qualified_type(obj),
3605 qualified_type);
3606 }
3607
3608 /*
3609 * It's too expensive to check that two pointer types are compatible, so we just
3610 * check that they refer to the same kind of type with equal size.
3611 */
3612 static bool c_pointers_similar(const struct drgn_operand_type *lhs_type,
3613 const struct drgn_operand_type *rhs_type,
3614 uint64_t lhs_size, uint64_t rhs_size)
3615 {
3616 struct drgn_type *lhs_referenced_type, *rhs_referenced_type;
3617
3618 lhs_referenced_type = drgn_type_type(lhs_type->underlying_type).type;
3619 rhs_referenced_type = drgn_type_type(rhs_type->underlying_type).type;
3620 return (drgn_type_kind(lhs_referenced_type) ==
3621 drgn_type_kind(rhs_referenced_type) && lhs_size == rhs_size);
3622 }
3623
3624 static struct drgn_error *c_op_bool(const struct drgn_object *obj, bool *ret)
3625 {
3626 struct drgn_error *err;
3627 struct drgn_type *underlying_type;
3628
3629 underlying_type = drgn_underlying_type(obj->type);
3630 switch (drgn_type_kind(underlying_type)) {
3631 case DRGN_TYPE_ARRAY:
3632 case DRGN_TYPE_FUNCTION:
3633 SWITCH_ENUM(obj->kind) {
3634 case DRGN_OBJECT_VALUE:
3635 *ret = true;
3636 return NULL;
3637 case DRGN_OBJECT_REFERENCE:
3638 *ret = obj->address != 0;
3639 return NULL;
3640 case DRGN_OBJECT_ABSENT:
3641 return &drgn_error_object_absent;
3642 default:
3643 UNREACHABLE();
3644 }
3645 default:
3646 break;
3647 }
3648
3649 if (!drgn_type_is_scalar(underlying_type)) {
3650 return drgn_qualified_type_error("cannot convert '%s' to bool",
3651 drgn_object_qualified_type(obj));
3652 }
3653
3654 err = drgn_object_is_zero(obj, ret);
3655 if (err)
3656 return err;
3657 *ret = !*ret;
3658 return NULL;
3659 }
3660
3661 static struct drgn_error *c_op_cmp(const struct drgn_object *lhs,
3662 const struct drgn_object *rhs, int *ret)
3663 {
3664 struct drgn_error *err;
3665
3666 struct drgn_operand_type lhs_type, rhs_type;
3667 bool lhs_pointer, rhs_pointer;
3668 err = c_operand_type(lhs, &lhs_type, &lhs_pointer, NULL);
3669 if (err)
3670 return err;
3671 err = c_operand_type(rhs, &rhs_type, &rhs_pointer, NULL);
3672 if (err)
3673 return err;
3674
3675 if (lhs_pointer && rhs_pointer) {
3676 return drgn_op_cmp_pointers(lhs, rhs, ret);
3677 } else if (lhs_pointer || rhs_pointer) {
3678 goto type_error;
3679 } else {
3680 struct drgn_operand_type type;
3681 if (!drgn_type_is_arithmetic(lhs_type.underlying_type) ||
3682 !drgn_type_is_arithmetic(rhs_type.underlying_type))
3683 goto type_error;
3684 err = c_common_real_type(drgn_object_program(lhs), &lhs_type,
3685 &rhs_type, &type);
3686 if (err)
3687 return err;
3688
3689 return drgn_op_cmp_impl(lhs, rhs, &type, ret);
3690 }
3691
3692 type_error:
3693 return drgn_error_binary_op("comparison", &lhs_type, &rhs_type);
3694 }
3695
3696 static struct drgn_error *c_op_add(struct drgn_object *res,
3697 const struct drgn_object *lhs,
3698 const struct drgn_object *rhs)
3699 {
3700 struct drgn_error *err;
3701
3702 struct drgn_operand_type lhs_type, rhs_type;
3703 bool lhs_pointer, rhs_pointer;
3704 uint64_t lhs_size, rhs_size;
3705 err = c_operand_type(lhs, &lhs_type, &lhs_pointer, &lhs_size);
3706 if (err)
3707 return err;
3708 err = c_operand_type(rhs, &rhs_type, &rhs_pointer, &rhs_size);
3709 if (err)
3710 return err;
3711
3712 if (lhs_pointer) {
3713 if (!drgn_type_is_integer(rhs_type.underlying_type))
3714 goto type_error;
3715 return drgn_op_add_to_pointer(res, &lhs_type, lhs_size, false, lhs, rhs);
3716 } else if (rhs_pointer) {
3717 if (!drgn_type_is_integer(lhs_type.underlying_type))
3718 goto type_error;
3719 return drgn_op_add_to_pointer(res, &rhs_type, rhs_size, false, rhs, lhs);
3720 } else {
3721 struct drgn_operand_type type;
3722 if (!drgn_type_is_arithmetic(lhs_type.underlying_type) ||
3723 !drgn_type_is_arithmetic(rhs_type.underlying_type))
3724 goto type_error;
3725 err = c_common_real_type(drgn_object_program(lhs), &lhs_type,
3726 &rhs_type, &type);
3727 if (err)
3728 return err;
3729
3730 return drgn_op_add_impl(res, &type, lhs, rhs);
3731 }
3732
3733 type_error:
3734 return drgn_error_binary_op("binary +", &lhs_type, &rhs_type);
3735 }
3736
3737 static struct drgn_error *c_op_sub(struct drgn_object *res,
3738 const struct drgn_object *lhs,
3739 const struct drgn_object *rhs)
3740 {
3741 struct drgn_error *err;
3742
3743 struct drgn_operand_type lhs_type, rhs_type;
3744 bool lhs_pointer, rhs_pointer;
3745 uint64_t lhs_size, rhs_size;
3746 err = c_operand_type(lhs, &lhs_type, &lhs_pointer, &lhs_size);
3747 if (err)
3748 return err;
3749 err = c_operand_type(rhs, &rhs_type, &rhs_pointer, &rhs_size);
3750 if (err)
3751 return err;
3752
3753 if (lhs_pointer && rhs_pointer) {
3754 struct drgn_operand_type type = {};
3755 err = drgn_program_find_primitive_type(drgn_object_program(lhs),
3756 DRGN_C_TYPE_PTRDIFF_T,
3757 &type.type);
3758 if (err)
3759 return err;
3760 type.underlying_type = drgn_underlying_type(type.type);
3761 if (!c_pointers_similar(&lhs_type, &rhs_type, lhs_size,
3762 rhs_size))
3763 goto type_error;
3764 return drgn_op_sub_pointers(res, &type, lhs_size, lhs, rhs);
3765 } else if (lhs_pointer) {
3766 if (!drgn_type_is_integer(rhs_type.underlying_type))
3767 goto type_error;
3768 return drgn_op_add_to_pointer(res, &lhs_type, lhs_size, true,
3769 lhs, rhs);
3770 } else {
3771 struct drgn_operand_type type;
3772 if (!drgn_type_is_arithmetic(lhs_type.underlying_type) ||
3773 !drgn_type_is_arithmetic(rhs_type.underlying_type))
3774 goto type_error;
3775 err = c_common_real_type(drgn_object_program(lhs), &lhs_type,
3776 &rhs_type, &type);
3777 if (err)
3778 return err;
3779
3780 return drgn_op_sub_impl(res, &type, lhs, rhs);
3781 }
3782
3783 type_error:
3784 return drgn_error_binary_op("binary -", &lhs_type, &rhs_type);
3785 }
3786
3787 #define BINARY_OP(op_name, op, check) \
3788 static struct drgn_error *c_op_##op_name(struct drgn_object *res, \
3789 const struct drgn_object *lhs, \
3790 const struct drgn_object *rhs) \
3791 { \
3792 struct drgn_error *err; \
3793 \
3794 struct drgn_operand_type lhs_type, rhs_type, type; \
3795 err = c_operand_type(lhs, &lhs_type, NULL, NULL); \
3796 if (err) \
3797 return err; \
3798 err = c_operand_type(rhs, &rhs_type, NULL, NULL); \
3799 if (err) \
3800 return err; \
3801 if (!drgn_type_is_##check(lhs_type.underlying_type) || \
3802 !drgn_type_is_##check(rhs_type.underlying_type)) \
3803 return drgn_error_binary_op("binary "#op, &lhs_type, \
3804 &rhs_type); \
3805 \
3806 err = c_common_real_type(drgn_object_program(lhs), &lhs_type, \
3807 &rhs_type, &type); \
3808 if (err) \
3809 return err; \
3810 \
3811 return drgn_op_##op_name##_impl(res, &type, lhs, rhs); \
3812 }
3813 BINARY_OP(mul, *, arithmetic)
3814 BINARY_OP(div, /, arithmetic)
3815 BINARY_OP(mod, %, integer)
3816 BINARY_OP(and, &, integer)
3817 BINARY_OP(or, |, integer)
3818 BINARY_OP(xor, ^, integer)
3819 #undef BINARY_OP
3820
3821 #define SHIFT_OP(op_name, op) \
3822 static struct drgn_error *c_op_##op_name(struct drgn_object *res, \
3823 const struct drgn_object *lhs, \
3824 const struct drgn_object *rhs) \
3825 { \
3826 struct drgn_error *err; \
3827 \
3828 struct drgn_operand_type lhs_type, rhs_type; \
3829 err = c_operand_type(lhs, &lhs_type, NULL, NULL); \
3830 if (err) \
3831 return err; \
3832 err = c_operand_type(rhs, &rhs_type, NULL, NULL); \
3833 if (err) \
3834 return err; \
3835 if (!drgn_type_is_integer(lhs_type.underlying_type) || \
3836 !drgn_type_is_integer(rhs_type.underlying_type)) \
3837 return drgn_error_binary_op("binary " #op, &lhs_type, \
3838 &rhs_type); \
3839 \
3840 err = c_integer_promotions(drgn_object_program(lhs), &lhs_type); \
3841 if (err) \
3842 return err; \
3843 err = c_integer_promotions(drgn_object_program(lhs), &rhs_type); \
3844 if (err) \
3845 return err; \
3846 \
3847 return drgn_op_##op_name##_impl(res, lhs, &lhs_type, rhs, &rhs_type); \
3848 }
3849 SHIFT_OP(lshift, <<)
3850 SHIFT_OP(rshift, >>)
3851 #undef SHIFT_OP
3852
3853 #define UNARY_OP(op_name, op, check) \
3854 static struct drgn_error *c_op_##op_name(struct drgn_object *res, \
3855 const struct drgn_object *obj) \
3856 { \
3857 struct drgn_error *err; \
3858 \
3859 struct drgn_operand_type type; \
3860 err = c_operand_type(obj, &type, NULL, NULL); \
3861 if (err) \
3862 return err; \
3863 if (!drgn_type_is_##check(type.underlying_type)) \
3864 return drgn_error_unary_op("unary " #op, &type); \
3865 \
3866 err = c_integer_promotions(drgn_object_program(obj), &type); \
3867 if (err) \
3868 return err; \
3869 \
3870 return drgn_op_##op_name##_impl(res, &type, obj); \
3871 }
3872 UNARY_OP(pos, +, arithmetic)
3873 UNARY_OP(neg, -, arithmetic)
3874 UNARY_OP(not, ~, integer)
3875 #undef UNARY_OP
3876
3877 LIBDRGN_PUBLIC const struct drgn_language drgn_language_c = {
3878 .name = "C",
3879 .number = DRGN_LANGUAGE_C,
3880 .has_namespaces = false,
3881 .format_type_name = c_format_type_name,
3882 .format_type = c_format_type,
3883 .format_variable_declaration = c_format_variable_declaration,
3884 .format_object = c_format_object,
3885 .find_type = c_family_find_type,
3886 .type_subobject = c_family_type_subobject,
3887 .integer_literal = c_integer_literal,
3888 .bool_literal = c_bool_literal,
3889 .float_literal = c_float_literal,
3890 .op_cast = c_op_cast,
3891 .op_implicit_convert = c_op_implicit_convert,
3892 .op_bool = c_op_bool,
3893 .op_cmp = c_op_cmp,
3894 .op_add = c_op_add,
3895 .op_sub = c_op_sub,
3896 .op_mul = c_op_mul,
3897 .op_div = c_op_div,
3898 .op_mod = c_op_mod,
3899 .op_lshift = c_op_lshift,
3900 .op_rshift = c_op_rshift,
3901 .op_and = c_op_and,
3902 .op_or = c_op_or,
3903 .op_xor = c_op_xor,
3904 .op_pos = c_op_pos,
3905 .op_neg = c_op_neg,
3906 .op_not = c_op_not,
3907 };
3908
3909 LIBDRGN_PUBLIC const struct drgn_language drgn_language_cpp = {
3910 .name = "C++",
3911 .number = DRGN_LANGUAGE_CPP,
3912 .has_namespaces = true,
3913 .format_type_name = c_format_type_name,
3914 .format_type = c_format_type,
3915 .format_variable_declaration = c_format_variable_declaration,
3916 .format_object = c_format_object,
3917 .find_type = c_family_find_type,
3918 .type_subobject = c_family_type_subobject,
3919 .integer_literal = c_integer_literal,
3920 .bool_literal = c_bool_literal,
3921 .float_literal = c_float_literal,
3922 .op_cast = c_op_cast,
3923 .op_implicit_convert = c_op_implicit_convert,
3924 .op_bool = c_op_bool,
3925 .op_cmp = c_op_cmp,
3926 .op_add = c_op_add,
3927 .op_sub = c_op_sub,
3928 .op_mul = c_op_mul,
3929 .op_div = c_op_div,
3930 .op_mod = c_op_mod,
3931 .op_lshift = c_op_lshift,
3932 .op_rshift = c_op_rshift,
3933 .op_and = c_op_and,
3934 .op_or = c_op_or,
3935 .op_xor = c_op_xor,
3936 .op_pos = c_op_pos,
3937 .op_neg = c_op_neg,
3938 .op_not = c_op_not,
3939 };