-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathv3.c
More file actions
4748 lines (4467 loc) · 143 KB
/
v3.c
File metadata and controls
4748 lines (4467 loc) · 143 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: 2025 Siddharth Mishra <admin@brightprogrammer.in>
// SPDX-FileCopyrightText: 2025-2026 RizinOrg <info@rizin.re>
// SPDX-FileCopyrightText: 2025-2026 Billow <billow.fun@gmail.com>
// SPDX-License-Identifier: LGPL-3.0-only
/**
* Documentation for used grammar can be found at either of
* - https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling
*/
// Disable unused-value warning for the macros in this file
// The CALL_RULE* macros use comma expressions where intermediate results are intentionally discarded
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic ignored "-Wunused-value"
#endif
#include "v3.h"
#include "v3_pp.h"
#include "../demangle.h"
#include "demangler_util.h"
#include "macros.h"
#include "parser_combinator.h"
#include "types.h"
#include "../vec.h"
#include <ctype.h>
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
void pp_cv_qualifiers(CvQualifiers qualifiers, DemString *out, PPContext *ctx) {
if (ctx && !(ctx->opts & DEM_OPT_ANSI)) {
return;
}
if (qualifiers.is_const) {
dem_string_append(out, " const");
}
if (qualifiers.is_volatile) {
dem_string_append(out, " volatile");
}
if (qualifiers.is_restrict) {
dem_string_append(out, " restrict");
}
}
void pp_ref_qualifiers(RefQualifiers qualifiers, DemString *out, PPContext *ctx) {
if (qualifiers.is_l_value) {
dem_string_append(out, " &");
}
if (qualifiers.is_r_value) {
dem_string_append(out, " &&");
}
}
void pp_array_type_dimension(DemNode *node, DemString *out, PDemNode *pbase_ty, PPContext *ctx) {
if (!node || !(node->tag == CP_DEM_TYPE_KIND_ARRAY_TYPE || node->tag == CP_DEM_TYPE_KIND_VECTOR_TYPE)) {
return;
}
size_t count = 0;
PDemNode base_ty = node;
do {
ArrayTy array_ty = base_ty->array_ty;
dem_string_appends(out, "[");
ast_pp(array_ty.dimension, out, ctx);
dem_string_appends(out, "]");
base_ty = array_ty.inner_ty;
count++;
} while (base_ty && (base_ty->tag == CP_DEM_TYPE_KIND_ARRAY_TYPE || base_ty->tag == CP_DEM_TYPE_KIND_VECTOR_TYPE));
if (count == 0) {
dem_string_append(out, "[]");
}
if (pbase_ty) {
*pbase_ty = base_ty;
}
}
static void pp_array_type(PDemNode node, DemString *out, PPContext *ctx) {
DemString dim_string = { 0 };
dem_string_init(&dim_string);
PDemNode base_node = NULL;
pp_array_type_dimension(node, &dim_string, &base_node, ctx);
ast_pp(base_node, out, ctx);
if (dem_string_non_empty(&dim_string)) {
dem_string_append(out, " ");
dem_string_concat(out, &dim_string);
}
dem_string_deinit(&dim_string);
}
static bool pp_init_list_as_string(PDemNode node, DemString *out, PPContext *ctx, PDemNode elems) {
PDemNode base_node = node->array_ty.inner_ty;
if ((base_node->tag == CP_DEM_TYPE_KIND_PRIMITIVE_TY &&
strncmp(base_node->primitive_ty.name.buf, "char", base_node->primitive_ty.name.len) == 0) ||
(base_node->tag == CP_DEM_TYPE_KIND_BUILTIN_TYPE && AST_(base_node, 0) &&
strncmp(AST_(base_node, 0)->primitive_ty.name.buf, "char", AST_(base_node, 0)->primitive_ty.name.len) == 0)) {
dem_string_append(out, "\"");
// Reconstruct string content from IntegerLiteral children
if (elems && elems->children) {
size_t count = VecPDemNode_len(elems->children);
for (size_t i = 0; i < count; i++) {
PDemNode *child_ptr = VecPDemNode_at(elems->children, i);
if (!child_ptr || !*child_ptr) {
continue;
}
PDemNode child = *child_ptr;
if (child->tag != CP_DEM_TYPE_KIND_INTEGER_LITERAL) {
continue;
}
// Parse the integer value from the mangled representation
DemStringView val = child->integer_literal_expr.value;
ut64 num = 0;
const char *vp = val.buf;
const char *ve = val.buf + val.len;
if (vp < ve && *vp == 'n') {
vp++; // skip negative sign (shouldn't happen for chars, but handle it)
}
while (vp < ve && *vp >= '0' && *vp <= '9') {
num = num * 10 + (ut64)(*vp - '0');
vp++;
}
if (num > 0 && num < 128) {
char ch = (char)num;
dem_string_append_n(out, &ch, 1);
}
}
}
dem_string_append(out, "\"");
return true;
}
pp_array_type(node, out, ctx);
return false;
}
// Check if a node is an ObjC "id<Proto>" type:
// vendor-ext-qualified with "objcproto" prefix and inner type "objc_object".
// In ObjC, id is already a pointer (objc_object*), so pointer decorators should be suppressed.
static bool is_objc_id_protocol_type(PDemNode node) {
if (!node || node->tag != CP_DEM_TYPE_KIND_VENDOR_EXT_QUALIFIED_TYPE) {
return false;
}
const char *vext = node->vendor_ext_qualified_ty.vendor_ext.buf;
size_t vext_len = node->vendor_ext_qualified_ty.vendor_ext.len;
if (!vext || vext_len <= 9 || memcmp(vext, "objcproto", 9) != 0) {
return false;
}
// The inner type may be wrapped in a QUALIFIED_TYPE from rule_qualified_type.
// Unwrap it to find the actual type name.
DemNode *inner = node->vendor_ext_qualified_ty.inner_type;
if (inner && inner->tag == CP_DEM_TYPE_KIND_QUALIFIED_TYPE) {
inner = inner->qualified_ty.inner_type;
}
return (inner &&
inner->tag == CP_DEM_TYPE_KIND_PRIMITIVE_TY &&
inner->primitive_ty.name.len == 11 &&
inner->primitive_ty.name.buf &&
memcmp(inner->primitive_ty.name.buf, "objc_object", 11) == 0);
}
static void pp_type_quals(PDemNode node, DemString *out, CpDemTypeKind target_tag, PDemNode *pbase_ty, PPContext *ctx) {
if (!node || !out) {
return;
}
if (node->tag == target_tag) {
// Reached the target type - stop recursion
if (pbase_ty) {
*pbase_ty = node;
}
return;
}
if (node->tag == CP_DEM_TYPE_KIND_TYPE && node->subtag != SUB_TAG_INVALID) {
// Recurse first to get inner decorators
if (AST(0)) {
pp_type_quals(AST(0), out, target_tag, pbase_ty, ctx);
}
// Then add our decorator
switch (node->subtag) {
case POINTER_TYPE:
// Suppress pointer decorator for ObjC id<Proto> types:
// id is already a pointer (objc_object*), so P+objcproto+objc_object = id<Proto>
if (!is_objc_id_protocol_type(AST(0))) {
dem_string_append(out, "*");
}
break;
case REFERENCE_TYPE:
dem_string_append(out, "&");
break;
case RVALUE_REFERENCE_TYPE:
dem_string_append(out, "&&");
break;
default:
DEM_UNREACHABLE;
break;
}
return;
}
if (node->tag == CP_DEM_TYPE_KIND_QUALIFIED_TYPE) {
// Check if this qualified type wraps an array - if so, stop here
// and return the qualified_type node as the base (qualifiers apply to array elements)
if (node->qualified_ty.inner_type &&
node->qualified_ty.inner_type->tag == CP_DEM_TYPE_KIND_ARRAY_TYPE) {
if (pbase_ty) {
*pbase_ty = node;
}
return;
}
if (node->qualified_ty.inner_type) {
pp_type_quals(node->qualified_ty.inner_type, out, target_tag, pbase_ty, ctx);
}
pp_cv_qualifiers(node->qualified_ty.qualifiers, out, ctx);
return;
}
if (pbase_ty) {
*pbase_ty = node;
}
}
// Helper to reorder qualifiers for array/function references
// Transforms " const&" to "const &" for arrays, or "*const&" to "* const&" for function pointers
static void reorder_qualifiers_for_array_fn_ref(DemString *quals) {
if (!quals || !quals->buf || quals->len < 2) {
return;
}
// Look for pattern: [*][ cv-qualifiers][&|&&]
// We need to move cv-qualifiers before & but after *, with proper spacing
char *ref_pos = NULL;
bool is_rvalue_ref = false;
// Find reference marker from the end
if (quals->len >= 2 && quals->buf[quals->len - 2] == '&' && quals->buf[quals->len - 1] == '&') {
ref_pos = &quals->buf[quals->len - 2];
is_rvalue_ref = true;
} else if (quals->len >= 1 && quals->buf[quals->len - 1] == '&') {
ref_pos = &quals->buf[quals->len - 1];
}
if (!ref_pos) {
return; // No reference found
}
// Find where cv-qualifiers start (after last * or from beginning)
char *cv_start = NULL;
char *ptr = quals->buf;
char *last_star = NULL;
while (ptr < ref_pos) {
if (*ptr == '*') {
last_star = ptr;
}
ptr++;
}
// Determine start of prefix and cv-qualifiers
char *prefix_end = last_star ? (last_star + 1) : quals->buf;
cv_start = prefix_end;
while (cv_start < ref_pos && *cv_start == ' ') {
cv_start++;
}
if (cv_start >= ref_pos) {
return; // No cv-qualifiers before reference
}
// Extract parts
size_t prefix_len = prefix_end - quals->buf;
size_t cv_len = ref_pos - cv_start;
size_t ref_len = is_rvalue_ref ? 2 : 1;
// Build reordered string: prefix + " " + cv-quals + ref (no space before ref for function pointers)
// For arrays: "const &", for function pointers: "* const&"
DemString reordered = { 0 };
dem_string_init(&reordered);
if (prefix_len > 0) {
dem_string_append_n(&reordered, quals->buf, prefix_len);
// Add space after * for function pointers
if (last_star && cv_start < ref_pos) {
dem_string_append(&reordered, " ");
}
}
dem_string_append_n(&reordered, cv_start, cv_len);
dem_string_append_n(&reordered, ref_pos, ref_len);
// Replace original
dem_string_deinit(quals);
*quals = reordered;
}
static void pp_type_with_quals(PDemNode node, DemString *out, PPContext *ctx) {
DemString qualifiers_string = { 0 };
dem_string_init(&qualifiers_string);
PDemNode base_node = NULL;
pp_type_quals(node, &qualifiers_string, CP_DEM_TYPE_KIND_UNKNOWN, &base_node, ctx);
if (base_node && base_node->tag == CP_DEM_TYPE_KIND_ARRAY_TYPE) {
DemString array_dem_string = { 0 };
dem_string_init(&array_dem_string);
PDemNode array_inner_base = NULL;
pp_array_type_dimension(base_node, &array_dem_string, &array_inner_base, ctx);
ast_pp(array_inner_base, out, ctx);
if (dem_string_non_empty(&qualifiers_string)) {
reorder_qualifiers_for_array_fn_ref(&qualifiers_string);
dem_string_append(out, " (");
dem_string_concat(out, &qualifiers_string);
dem_string_append(out, ")");
}
if (dem_string_non_empty(&array_dem_string)) {
dem_string_append(out, " ");
dem_string_concat(out, &array_dem_string);
}
dem_string_deinit(&array_dem_string);
} else if (base_node && base_node->tag == CP_DEM_TYPE_KIND_QUALIFIED_TYPE &&
base_node->qualified_ty.inner_type &&
base_node->qualified_ty.inner_type->tag == CP_DEM_TYPE_KIND_ARRAY_TYPE) {
// Handle qualified array: print element type with qualifiers, then ref, then array dimension
DemString array_dem_string = { 0 };
dem_string_init(&array_dem_string);
PDemNode array_inner_base = NULL;
pp_array_type_dimension(base_node->qualified_ty.inner_type, &array_dem_string, &array_inner_base, ctx);
ast_pp(array_inner_base, out, ctx);
pp_cv_qualifiers(base_node->qualified_ty.qualifiers, out, ctx);
if (dem_string_non_empty(&qualifiers_string)) {
reorder_qualifiers_for_array_fn_ref(&qualifiers_string);
dem_string_append(out, " (");
dem_string_concat(out, &qualifiers_string);
dem_string_append(out, ")");
}
if (dem_string_non_empty(&array_dem_string)) {
dem_string_append(out, " ");
dem_string_concat(out, &array_dem_string);
}
dem_string_deinit(&array_dem_string);
} else {
size_t saved_len = dem_string_length(out);
ast_pp(base_node, out, ctx);
size_t out_len = dem_string_length(out);
// If the base type produced no output (e.g., due to a circular forward template
// reference cycle being detected), skip qualifiers too - the entire type wrapper
// including its pointer/reference qualifier is implicit/deduced.
if (out_len == saved_len && base_node &&
base_node->tag == CP_DEM_TYPE_KIND_FWD_TEMPLATE_REF) {
// Skip qualifiers - cycle was detected inside
} else if (dem_string_non_empty(&qualifiers_string)) {
// Reference collapsing (C++ rules):
// When a reference qualifier is about to be appended and the base type
// already ends with a reference (e.g., from a pack expansion element),
// apply collapsing: & + & = &, & + && = &, && + & = &, && + && = &&.
size_t q_len = qualifiers_string.len;
bool out_ends_with_ref = (out_len >= 1 && out->buf[out_len - 1] == '&');
bool quals_is_ref = (q_len >= 1 && qualifiers_string.buf[0] == '&');
if (out_ends_with_ref && quals_is_ref) {
// Determine inner and outer ref types
bool inner_is_rvalue = (out_len >= 2 && out->buf[out_len - 2] == '&');
bool outer_is_rvalue = (q_len >= 2 && qualifiers_string.buf[1] == '&');
if (inner_is_rvalue && outer_is_rvalue) {
// && + && = &&: already have &&, don't append
} else if (inner_is_rvalue) {
// && + & = &: trim trailing & from out
out->len--;
out->buf[out->len] = '\0';
} else {
// & + & = & or & + && = &: already have &, don't append
}
} else {
dem_string_concat(out, &qualifiers_string);
}
}
}
dem_string_deinit(&qualifiers_string);
}
bool pp_parameter_pack(PDemNode node, DemString *out, PPContext *pp_ctx) {
if (!(node->tag == CP_DEM_TYPE_KIND_PARAMETER_PACK && node->child_ref && node->child_ref->tag == CP_DEM_TYPE_KIND_MANY)) {
return false;
}
const DemNode *many_node = node->child_ref;
if (pp_ctx->current_pack_index == UT32_MAX) {
pp_ctx->current_pack_index = 0;
pp_ctx->current_pack_max = VecPDemNode_len(many_node->children);
}
if (pp_ctx->current_pack_index < VecPDemNode_len(many_node->children)) {
PDemNode *child = VecPDemNode_at(many_node->children, pp_ctx->current_pack_index);
if (child && *child) {
ast_pp(*child, out, pp_ctx);
} else {
dem_string_append(out, "<null pack element>");
}
}
return true;
}
// Print all elements of a parameter_pack comma-separated (for fold expressions)
static void pp_pack_all_elements(PDemNode node, DemString *out, PPContext *pp_ctx) {
if (node->tag == CP_DEM_TYPE_KIND_PARAMETER_PACK && node->child_ref && node->child_ref->tag == CP_DEM_TYPE_KIND_MANY) {
const DemNode *many_node = node->child_ref;
size_t count = VecPDemNode_len(many_node->children);
for (size_t i = 0; i < count; i++) {
if (i > 0) {
dem_string_append(out, ", ");
}
PDemNode *child = VecPDemNode_at(many_node->children, i);
if (child && *child) {
ast_pp(*child, out, pp_ctx);
}
}
} else {
ast_pp(node, out, pp_ctx);
}
}
// Resolve a PARAMETER_PACK node to its current element during pack expansion.
// Returns the resolved element node, or NULL if not a pack or pack is empty.
// When current_pack_index == UT32_MAX (initial iteration), peeks at element 0
// to determine the type without advancing the iteration state.
// Also follows FWD_TEMPLATE_REF indirection to find the underlying pack.
static PDemNode resolve_pack_element(PDemNode node, PPContext *pp_ctx) {
if (!node) {
return NULL;
}
// Follow FWD_TEMPLATE_REF to the resolved node
if (node->tag == CP_DEM_TYPE_KIND_FWD_TEMPLATE_REF) {
if (node->fwd_template_ref && node->fwd_template_ref->ref) {
node = node->fwd_template_ref->ref;
} else {
return NULL;
}
}
if (node->tag != CP_DEM_TYPE_KIND_PARAMETER_PACK) {
return NULL;
}
if (!node->child_ref || node->child_ref->tag != CP_DEM_TYPE_KIND_MANY) {
return NULL;
}
const DemNode *many_node = node->child_ref;
size_t count = VecPDemNode_len(many_node->children);
if (count == 0) {
return NULL;
}
ut32 idx = pp_ctx->current_pack_index;
if (idx == UT32_MAX) {
// Initial iteration: peek at first element
idx = 0;
}
if (idx < count) {
PDemNode *child = VecPDemNode_at(many_node->children, idx);
if (child && *child) {
return *child;
}
}
return NULL;
}
bool pp_pack_expansion(PDemNode node, DemString *out, PPContext *pp_ctx) {
ut32 saved_pack_index = pp_ctx->current_pack_index;
ut32 saved_pack_max = pp_ctx->current_pack_max;
pp_ctx->current_pack_index = UT32_MAX;
pp_ctx->current_pack_max = UT32_MAX;
size_t saved_pos = dem_string_length(out);
ast_pp(node->child, out, pp_ctx);
if (pp_ctx->current_pack_index == UT32_MAX) {
dem_string_append(out, "...");
goto beach;
}
if (pp_ctx->current_pack_max == 0 && out->len != saved_pos) {
// Empty pack expansion - remove previously appended content
out->len = saved_pos;
out->buf[out->len] = '\0';
goto beach;
}
for (size_t i = 1; i < pp_ctx->current_pack_max; i++) {
dem_string_append(out, ", ");
pp_ctx->current_pack_index = i;
ast_pp(node->child, out, pp_ctx);
}
beach:
pp_ctx->current_pack_index = saved_pack_index;
pp_ctx->current_pack_max = saved_pack_max;
return true;
}
// Helper function to extract the base class name from a ctor/dtor name
// Recursively unwraps name_with_template_args and nested_name to get the final primitive name
static bool node_base_name(PDemNode node, DemStringView *out) {
if (!node) {
return false;
}
switch (node->tag) {
case CP_DEM_TYPE_KIND_ABI_TAG_TY:
// Unwrap abi_tag_ty to get the inner type
if (node->abi_tag_ty.ty) {
return node_base_name(node->abi_tag_ty.ty, out);
}
break;
case CP_DEM_TYPE_KIND_NAME_WITH_TEMPLATE_ARGS:
// Unwrap template args to get the base name
if (node->name_with_template_args.name) {
return node_base_name(node->name_with_template_args.name, out);
}
break;
case CP_DEM_TYPE_KIND_NESTED_NAME:
// Get the final name component (not the qualifier)
if (node->nested_name.name) {
return node_base_name(node->nested_name.name, out);
}
break;
case CP_DEM_TYPE_KIND_EXPANDED_SPECIAL_SUBSTITUTION:
switch (node->subtag) {
case SPECIAL_SUBSTITUTION_ALLOCATOR:
return sv_form_cstr(out, "allocator");
case SPECIAL_SUBSTITUTION_BASIC_STRING:
return sv_form_cstr(out, "basic_string");
case SPECIAL_SUBSTITUTION_STRING:
return sv_form_cstr(out, "basic_string");
case SPECIAL_SUBSTITUTION_IOSTREAM:
return sv_form_cstr(out, "basic_iostream");
case SPECIAL_SUBSTITUTION_ISTREAM:
return sv_form_cstr(out, "basic_istream");
case SPECIAL_SUBSTITUTION_OSTREAM:
return sv_form_cstr(out, "basic_ostream");
default:
return sv_form_cstr(out, "<unknown special substitution>");
}
return false;
case CP_DEM_TYPE_KIND_SPECIAL_SUBSTITUTION:
switch (node->subtag) {
case SPECIAL_SUBSTITUTION_ALLOCATOR:
return sv_form_cstr(out, "allocator");
case SPECIAL_SUBSTITUTION_BASIC_STRING:
return sv_form_cstr(out, "basic_string");
case SPECIAL_SUBSTITUTION_STRING:
return sv_form_cstr(out, "string");
case SPECIAL_SUBSTITUTION_IOSTREAM:
return sv_form_cstr(out, "iostream");
case SPECIAL_SUBSTITUTION_ISTREAM:
return sv_form_cstr(out, "istream");
case SPECIAL_SUBSTITUTION_OSTREAM:
return sv_form_cstr(out, "ostream");
default:
return sv_form_cstr(out, "<unknown special substitution>");
}
return false;
case CP_DEM_TYPE_KIND_PRIMITIVE_TY:
return sv_form_cstr(out, node->primitive_ty.name.buf);
default:
return node_base_name(node, out);
}
return false;
}
static bool pp_base_name(PDemNode node, DemString *out) {
DemStringView base_name = { 0 };
if (!node_base_name(node, &base_name)) {
return false;
}
return dem_string_append_sv(out, base_name);
}
// Helper to check if a type node ultimately wraps a function type
// This walks through type wrappers (pointer, reference, qualified) to find the innermost type
struct PPFnContext_t;
typedef void (*AST_PP_FN)(struct PPFnContext_t *, DemString *);
typedef struct PPFnContext_t {
DemNode *fn;
DemNode *mod;
AST_PP_FN pp_mod;
DemNode *quals;
AST_PP_FN pp_quals;
PPContext *pp_ctx;
} PPFnContext;
static bool extract_function_type(DemNode *node, DemNode **out_func_node) {
if (!node) {
return false;
}
if (node->tag == CP_DEM_TYPE_KIND_FUNCTION_TYPE) {
if (out_func_node) {
*out_func_node = node;
}
return true;
}
if (node->tag == CP_DEM_TYPE_KIND_TYPE && AST(0)) {
return extract_function_type(AST(0), out_func_node);
}
if (node->tag == CP_DEM_TYPE_KIND_QUALIFIED_TYPE && node->qualified_ty.inner_type) {
return extract_function_type(node->qualified_ty.inner_type, out_func_node);
}
return false;
}
/// Check if a type is pointer/reference to an array type.
/// Returns true if the outermost non-qualified wrapper is a pointer/reference to an array.
static bool is_pointer_to_array_type(DemNode *node) {
if (!node) {
return false;
}
if (node->tag == CP_DEM_TYPE_KIND_TYPE && node->subtag != SUB_TAG_INVALID && AST(0)) {
DemNode *inner = AST(0);
if (inner->tag == CP_DEM_TYPE_KIND_ARRAY_TYPE) {
return true;
}
if (inner->tag == CP_DEM_TYPE_KIND_QUALIFIED_TYPE &&
inner->qualified_ty.inner_type &&
inner->qualified_ty.inner_type->tag == CP_DEM_TYPE_KIND_ARRAY_TYPE) {
return true;
}
// Nested pointer/reference to array
return is_pointer_to_array_type(inner);
}
return false;
}
static void pp_function_ty_mod_return_fn(PPFnContext *, DemString *);
static void pp_function_ty_quals(PPFnContext *ctx, DemString *out);
static void pp_function_ty_with_context(PPFnContext *ctx, DemString *out) {
if (!ctx || !ctx->fn || !out) {
return;
}
DemNode *node = ctx->fn;
FunctionTy *ft = &node->fn_ty;
// Print return type
DemNode *ret_fn_ty = NULL;
if (ft->ret && extract_function_type(ft->ret, &ret_fn_ty)) {
DemNode *saved_ret = ft->ret;
ft->ret = NULL;
PPFnContext inner_ctx = {
.fn = ret_fn_ty,
.quals = saved_ret,
.pp_quals = pp_function_ty_quals,
.mod = node,
.pp_mod = pp_function_ty_mod_return_fn,
.pp_ctx = ctx->pp_ctx,
};
pp_function_ty_with_context(&inner_ctx, out);
ft->ret = saved_ret;
return;
}
if (ft->ret) {
ast_pp(ft->ret, out, ctx->pp_ctx);
dem_string_append(out, " ");
}
bool has_mod = ctx && ctx->mod && ctx->pp_mod;
bool has_quals = ctx && ctx->quals && ctx->pp_quals;
bool has_mod_or_quals = has_mod || has_quals;
if (has_mod_or_quals) {
dem_string_append(out, "(");
}
if (ft->name) {
ast_pp(ft->name, out, ctx->pp_ctx);
if (has_mod_or_quals) {
dem_string_append(out, " ");
}
}
if (has_quals) {
ctx->pp_quals(ctx, out);
}
if (has_mod) {
ctx->pp_mod(ctx, out);
}
if (has_mod_or_quals) {
dem_string_append(out, ")");
}
// Print parameters
dem_string_append(out, "(");
if (ft->params && (ctx->pp_ctx->opts & DEM_OPT_PARAMS)) {
// C++23 deducing this: prefix first parameter with "this "
if (ft->has_explicit_object_param) {
dem_string_append(out, "this ");
}
ast_pp(ft->params, out, ctx->pp_ctx);
}
dem_string_append(out, ")");
// Print cv and ref qualifiers
pp_cv_qualifiers(ft->cv_qualifiers, out, ctx->pp_ctx);
pp_ref_qualifiers(ft->ref_qualifiers, out, ctx->pp_ctx);
// Print exception spec
if (ft->exception_spec) {
dem_string_append(out, " ");
ast_pp(ft->exception_spec, out, ctx->pp_ctx);
}
// Print enable_if attribute: [enable_if:cond1, cond2]
if (ft->enable_if_attrs) {
dem_string_append(out, " [enable_if:");
ast_pp(ft->enable_if_attrs, out, ctx->pp_ctx);
dem_string_append(out, "]");
}
// Print requires clause
if (ft->requires_node) {
dem_string_append(out, " requires ");
ast_pp(ft->requires_node, out, ctx->pp_ctx);
}
}
static void pp_function_ty(PDemNode node, DemString *out, PPContext *pp_ctx) {
PPFnContext ctx = { 0 };
ctx.fn = node;
ctx.pp_ctx = pp_ctx;
pp_function_ty_with_context(&ctx, out);
}
static void pp_function_ty_mod_return_fn(PPFnContext *ctx, DemString *out) {
PPFnContext inner_ctx = {
.fn = ctx->mod,
.pp_ctx = ctx->pp_ctx
};
pp_function_ty_with_context(&inner_ctx, out);
}
static void pp_function_ty_quals(PPFnContext *ctx, DemString *out) {
if (!ctx || !ctx->quals) {
return;
}
DemString quals_str = { 0 };
dem_string_init(&quals_str);
pp_type_quals(ctx->quals, &quals_str, CP_DEM_TYPE_KIND_FUNCTION_TYPE, NULL, ctx->pp_ctx);
reorder_qualifiers_for_array_fn_ref(&quals_str);
dem_string_concat(out, &quals_str);
dem_string_deinit(&quals_str);
}
static void pp_function_ty_mod_pointer_to_member_type(PPFnContext *ctx, DemString *out) {
if (ctx && ctx->mod) {
ast_pp(ctx->mod, out, ctx->pp_ctx);
}
dem_string_append(out, "::*");
}
void pp_expanded_special_substitution(DemNode *node, DemString *out, PPContext *ctx) {
dem_string_append(out, "std::");
pp_base_name(node, out);
if (node->subtag >= SPECIAL_SUBSTITUTION_STRING) {
dem_string_append(out, "<char, std::char_traits<char>");
if (node->subtag == SPECIAL_SUBSTITUTION_STRING) {
dem_string_append(out, ", std::allocator<char>");
}
dem_string_append(out, ">");
}
}
void pp_special_substitution(DemNode *node, DemString *out) {
dem_string_append(out, "std::");
pp_base_name(node, out);
}
// Helper functions for printing parentheses with depth tracking
static inline void print_open(DemString *out, PPContext *ctx) {
dem_string_append(out, "(");
ctx->paren_depth++;
}
static inline void print_close(DemString *out, PPContext *ctx) {
dem_string_append(out, ")");
ctx->paren_depth--;
}
static inline void pp_as_operand_ex(DemNode *node, DemString *out, Prec prec, bool strictly_worse, PPContext *ctx) {
if (!node || !out) {
return;
}
bool need_parens = (size_t)node->prec >= (size_t)prec + (size_t)strictly_worse;
if (need_parens) {
print_open(out, ctx);
}
ast_pp(node, out, ctx);
if (need_parens) {
print_close(out, ctx);
}
}
static void pp_template_param_decl_inner(DemNode *node, DemString *out, PPContext *ctx, bool is_pack) {
if (!node || !out) {
return;
}
switch (node->tag) {
case CP_DEM_TYPE_KIND_TYPE_TEMPLATE_PARAM_DECL:
dem_string_append(out, "typename ");
if (is_pack) {
dem_string_append(out, "...");
}
ast_pp(node->child, out, ctx);
break;
case CP_DEM_TYPE_KIND_CONSTRAINED_TYPE_TEMPLATE_PARAM_DECL:
if (node->constrained_type_template_param_decl.constraint) {
ast_pp(node->constrained_type_template_param_decl.constraint, out, ctx);
dem_string_append(out, " ");
}
if (is_pack) {
dem_string_append(out, "...");
}
if (node->constrained_type_template_param_decl.name) {
ast_pp(node->constrained_type_template_param_decl.name, out, ctx);
}
break;
case CP_DEM_TYPE_KIND_NON_TYPE_TEMPLATE_PARAM_DECL:
if (node->non_type_template_param_decl.ty &&
is_pointer_to_array_type(node->non_type_template_param_decl.ty)) {
// Pointer/reference to array with a name: need C declarator syntax
// e.g. int (*$N) [3] instead of int (*) [3] $N
PDemNode ty = node->non_type_template_param_decl.ty;
DemString qualifiers_string = { 0 };
dem_string_init(&qualifiers_string);
PDemNode base_node = NULL;
pp_type_quals(ty, &qualifiers_string, CP_DEM_TYPE_KIND_UNKNOWN, &base_node, ctx);
DemString array_dem_string = { 0 };
dem_string_init(&array_dem_string);
PDemNode array_inner_base = NULL;
PDemNode array_node = base_node;
if (base_node && base_node->tag == CP_DEM_TYPE_KIND_QUALIFIED_TYPE &&
base_node->qualified_ty.inner_type &&
base_node->qualified_ty.inner_type->tag == CP_DEM_TYPE_KIND_ARRAY_TYPE) {
array_node = base_node->qualified_ty.inner_type;
}
pp_array_type_dimension(array_node, &array_dem_string, &array_inner_base, ctx);
ast_pp(array_inner_base, out, ctx);
if (base_node && base_node->tag == CP_DEM_TYPE_KIND_QUALIFIED_TYPE) {
pp_cv_qualifiers(base_node->qualified_ty.qualifiers, out, ctx);
}
if (dem_string_non_empty(&qualifiers_string) ||
is_pack ||
node->non_type_template_param_decl.name) {
dem_string_append(out, " (");
if (dem_string_non_empty(&qualifiers_string)) {
reorder_qualifiers_for_array_fn_ref(&qualifiers_string);
dem_string_concat(out, &qualifiers_string);
}
if (is_pack) {
dem_string_append(out, "...");
}
if (node->non_type_template_param_decl.name) {
ast_pp(node->non_type_template_param_decl.name, out, ctx);
}
dem_string_append(out, ")");
}
if (dem_string_non_empty(&array_dem_string)) {
dem_string_append(out, " ");
dem_string_concat(out, &array_dem_string);
}
dem_string_deinit(&array_dem_string);
dem_string_deinit(&qualifiers_string);
} else {
if (node->non_type_template_param_decl.ty) {
ast_pp(node->non_type_template_param_decl.ty, out, ctx);
dem_string_append(out, " ");
}
if (is_pack) {
dem_string_append(out, "...");
}
if (node->non_type_template_param_decl.name) {
ast_pp(node->non_type_template_param_decl.name, out, ctx);
}
}
break;
case CP_DEM_TYPE_KIND_TEMPLATE_PARAM_DECL:
dem_string_append(out, "template<");
if (node->template_param_decl.params) {
ast_pp(node->template_param_decl.params, out, ctx);
}
dem_string_append(out, "> typename ");
if (is_pack) {
dem_string_append(out, "...");
}
if (node->template_param_decl.name) {
ast_pp(node->template_param_decl.name, out, ctx);
}
break;
case CP_DEM_TYPE_KIND_TEMPLATE_PARAM_PACK_DECL:
pp_template_param_decl_inner(node->child, out, ctx, true);
break;
default:
break;
}
}
static void pp_template_param_decl(DemNode *node, DemString *out, PPContext *ctx) {
if (!node || !out) {
return;
}
// In template args context, param decls define parameter kinds
// but produce no output (only actual args are shown).
if (ctx->inside_template) {
return;
}
pp_template_param_decl_inner(node, out, ctx, false);
}
void ast_pp(DemNode *node, DemString *out, PPContext *ctx) {
if (!node || !out || !ctx) {
return;
}
// Guard against infinite recursion (e.g. from circular template refs)
if (ctx->recursion_depth > 256) {
return;
}
ctx->recursion_depth++;
switch (node->tag) {
case CP_DEM_TYPE_KIND_PRIMITIVE_TY:
// Primitive type nodes contain literal strings
if (node->primitive_ty.name.buf) {
dem_string_append(out, node->primitive_ty.name.buf);
}
break;
case CP_DEM_TYPE_KIND_SPECIAL_SUBSTITUTION:
pp_special_substitution(node, out);
break;
case CP_DEM_TYPE_KIND_EXPANDED_SPECIAL_SUBSTITUTION:
pp_expanded_special_substitution(node, out, ctx);
break;
case CP_DEM_TYPE_KIND_ABI_TAG_TY:
ast_pp(node->abi_tag_ty.ty, out, ctx);
dem_string_append(out, "[abi:");
dem_string_append_n(out,
node->abi_tag_ty.tag.buf,
node->abi_tag_ty.tag.len);
dem_string_append(out, "]");
break;
case CP_DEM_TYPE_KIND_NOEXCEPT_SPEC:
dem_string_append(out, "noexcept");
print_open(out, ctx);
ast_pp(node->child, out, ctx);
print_close(out, ctx);
break;
case CP_DEM_TYPE_KIND_DYNAMIC_EXCEPTION_SPEC:
dem_string_append(out, "throw");
print_open(out, ctx);
ast_pp(node->child, out, ctx);
print_close(out, ctx);
break;
case CP_DEM_TYPE_KIND_FUNCTION_TYPE: {
pp_function_ty(node, out, ctx);
break;
}
case CP_DEM_TYPE_KIND_MODULE_NAME:
if (node->module_name_ty.pare) {
ast_pp(node->module_name_ty.pare, out, ctx);
}
if (node->module_name_ty.pare || node->module_name_ty.IsPartition) {
dem_string_append(out, node->module_name_ty.IsPartition ? ":" : ".");
}
if (node->module_name_ty.name) {
ast_pp(node->module_name_ty.name, out, ctx);
}
break;
case CP_DEM_TYPE_KIND_TEMPLATE_ARGS:
dem_string_append(out, "<");
// Set inside_template flag when printing template arguments
bool old_inside_template = ctx->inside_template;
ctx->inside_template = true;
ast_pp(node->child, out, ctx);
ctx->inside_template = old_inside_template;
dem_string_append(out, ">");
break;
case CP_DEM_TYPE_KIND_TEMPLATE_PARAM_DECL:
case CP_DEM_TYPE_KIND_TEMPLATE_PARAM_PACK_DECL:
case CP_DEM_TYPE_KIND_TYPE_TEMPLATE_PARAM_DECL:
case CP_DEM_TYPE_KIND_CONSTRAINED_TYPE_TEMPLATE_PARAM_DECL:
case CP_DEM_TYPE_KIND_NON_TYPE_TEMPLATE_PARAM_DECL:
pp_template_param_decl(node, out, ctx);
break;
case CP_DEM_TYPE_KIND_SYNTHETIC_TEMPLATE_PARAM_NAME: {
const char *prefix;