summaryrefslogtreecommitdiff
path: root/usr/src/lib/libdemangle/common/rust-v0.c
blob: 598d8457c90b4c8d3942ff9988b2f0dc1cec4772 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2019 Joyent, Inc.
 * Copyright 2021 Jason King
 */

/* BEGIN CSTYLED */

/*
 * This implements the 'symbol_name_mangling_v2' demangling for rust as
 * described in Rust RFC 2603 as opposed to the original (now called
 * legacy) mangling older versions of rust used (implemented in rust.c).
 *
 * The specification can be viewed at:
 *     https://github.com/rust-lang/rfcs/blob/master/text/2603-rust-symbol-name-mangling-v0.md
 */

/* END CSTYLED */

#include <errno.h>
#include <libcustr.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "rust.h"

/*
 * Help track amount of additional output added to rs_demangled across
 * a function call (to allow that portion to be output for debugging)
 */
#define	SAVE_LEN(_st, _len) _len = custr_len((_st)->rs_demangled)
#define	CSTR_END(_st, _len)					\
	((int)(custr_len((_st)->rs_demangled) - (_len))),	\
	custr_cstr((_st)->rs_demangled) + (_len)

typedef enum const_type_class {
	CTC_INVALID = -1,
	CTC_UNSIGNED,
	CTC_SIGNED,
	CTC_CHAR,
	CTC_BOOL,
} const_type_class_t;

/*
 * Sometimes, parsing something is optional.  In this case a failure to
 * parse is fine, however we still want to consider a fatal error as
 * failure.
 */
#define	OPTIONAL(_st, _f) ((_f) || !HAS_ERROR(_st))

static boolean_t rustv0_valid_sym(const strview_t *);
static const_type_class_t rustv0_classify_const_type(char);
static boolean_t rustv0_parse_hex_num(rust_state_t *restrict,
    strview_t *restrict, uint64_t *restrict);
static boolean_t rustv0_parse_base62(rust_state_t *restrict,
    strview_t *restrict, uint64_t *restrict);

static boolean_t rustv0_parse_undisambiguated_identifier(
    rust_state_t *restrict, strview_t *restrict, boolean_t);
static boolean_t rustv0_parse_disambiguator(rust_state_t *restrict,
    strview_t *restrict, uint64_t *restrict);

static boolean_t rustv0_parse_path(rust_state_t *restrict, strview_t *restrict,
    boolean_t);
static boolean_t rustv0_parse_impl_path(rust_state_t *restrict,
    strview_t *restrict, boolean_t);
static boolean_t rustv0_parse_nested_path(rust_state_t *restrict,
    strview_t *restrict, boolean_t);
static boolean_t rustv0_parse_basic_type(rust_state_t *restrict,
    strview_t *restrict);
static boolean_t rustv0_parse_backref(rust_state_t *restrict,
    strview_t *restrict,
    boolean_t (*)(rust_state_t *restrict, strview_t *restrict, boolean_t),
    boolean_t);
static boolean_t rustv0_parse_lifetime(rust_state_t *restrict,
    strview_t *restrict);
static boolean_t rustv0_parse_const(rust_state_t *restrict,
    strview_t *restrict, boolean_t);
static boolean_t rustv0_parse_fnsig(rust_state_t *restrict,
    strview_t *restrict);
static boolean_t rustv0_parse_dynbounds(rust_state_t *restrict,
    strview_t *restrict);
static boolean_t rustv0_parse_generic_arg(rust_state_t *restrict,
    strview_t *restrict, boolean_t);

boolean_t
rust_demangle_v0(rust_state_t *restrict st, strview_t *restrict sv)
{
	boolean_t save_skip;
	boolean_t ret;

	/* Make sure all the characters are valid */
	if (!rustv0_valid_sym(sv)) {
		st->rs_error = EINVAL;
		return (B_FALSE);
	}

	/*
	 * <symbol-name> = "_R" [<decimal-number>] <path>
	 *	[<instantiating-crate>]
	 *
	 * We've already parsed the prefix in rust_demangle(), as well
	 * as made sure there's no [<decimal-number>] present, so
	 * start with <path>.
	 */
	if (!rustv0_parse_path(st, sv, B_TRUE))
		return (B_FALSE);

	/* [<instantiating crate>] -- parse but don't save */
	SKIP_BEGIN(st, save_skip);
	ret = OPTIONAL(st, rustv0_parse_path(st, sv, B_FALSE));
	SKIP_END(st, save_skip);
	if (!ret)
		return (B_FALSE);

	/* If nothing's left, we know we're done */
	if (sv_remaining(sv) == 0)
		return (!HAS_ERROR(st));

	/*
	 * LLVM sometimes will suffix symbols starting with a '.'
	 * followed by extra data. For things that start with
	 * ".llvm.", we discard the rest of the string.  For
	 * other things that start with '.', we copy the
	 * results to the final string. This matches
	 * what the rust native demangler crate does, and
	 * we don't see a reason to deviate from their
	 * behavior.
	 */
	if (sv_consume_if(sv, ".llvm."))
		return (!HAS_ERROR(st));

	if (sv_peek(sv, 0) != '.') {
		DEMDEBUG("%s: Unexpected trailing data at the end of the "
		    "name: '%.*s'", __func__, SV_PRINT(sv));
		st->rs_error = EINVAL;
		return (B_FALSE);
	}

	return (rust_append_sv(st, sv_remaining(sv), sv));
}

/*
 * Parse an optional list terminated by 'E'. Each result of 'fn' is
 * separated by 'sep' in the output.
 */
static boolean_t
rustv0_parse_opt_list(rust_state_t *restrict st, strview_t *restrict sv,
    boolean_t (*fn)(rust_state_t *restrict, strview_t *restrict, boolean_t),
    const char *restrict sep, boolean_t bval, size_t *restrict countp)
{
	size_t count = 0;

	DEMDEBUG("%s: str = '%.*s'", __func__, SV_PRINT(sv));

	while (sv_remaining(sv) > 0) {
		if (sv_consume_if_c(sv, 'E')) {
			if (countp != NULL)
				*countp += count;
			return (B_TRUE);
		}

		if (count > 0 && !rust_append(st, sep))
			return (B_FALSE);

		if (!fn(st, sv, bval))
			return (B_FALSE);

		count++;
	}

	/*
	 * An optional list should terminate with an 'E'.  If we get here,
	 * we ran out of charaters and didn't terminate as we should.
	 */
	return (B_FALSE);
}

static boolean_t
rustv0_parse_uint_type(rust_state_t *restrict st, strview_t *sv)
{
	const char *str = NULL;
	strview_t save;
	char c;

	if (HAS_ERROR(st) || sv_remaining(sv) == 0)
		return (B_FALSE);

	sv_init_sv(&save, sv);

	switch (c = sv_consume_c(sv)) {
	case 'h':
		str = "u8";
		break;
	case 't':
		str = "u16";
		break;
	case 'm':
		str = "u32";
		break;
	case 'y':
		str = "u64";
		break;
	case 'o':
		str = "u128";
		break;
	case 'j':	/* usize */
		str = "usize";
		break;
	default:
		sv_init_sv(sv, &save);
		return (B_FALSE);
	}

	DEMDEBUG("%s: %c -> %s", __func__, c, str);
	return (rust_append(st, str));
}

static boolean_t
rustv0_parse_basic_type(rust_state_t *restrict st, strview_t *restrict sv)
{
	const char *str = NULL;
	strview_t save;
	char c;

	if (HAS_ERROR(st) || sv_remaining(sv) == 0)
		return (B_FALSE);

	if (rustv0_parse_uint_type(st, sv))
		return (B_TRUE);

	sv_init_sv(&save, sv);

	switch (c = sv_consume_c(sv)) {
	case 'a':
		str = "i8";
		break;
	case 'b':
		str = "bool";
		break;
	case 'c':
		str = "char";
		break;
	case 'd':
		str = "f64";
		break;
	case 'e':
		str = "str";
		break;
	case 'f':
		str = "f32";
		break;
	case 'i':
		str = "isize";
		break;
	case 'l':
		str = "i32";
		break;
	case 'n':
		str = "i128";
		break;
	case 'p':
		str = "_";
		break;
	case 's':
		str = "i16";
		break;
	case 'u':
		str = "()";
		break;
	case 'v':
		str = "...";
		break;
	case 'x':
		str = "i64";
		break;
	case 'z':
		str = "!";
		break;
	default:
		sv_init_sv(sv, &save);
		return (B_FALSE);
	}

	DEMDEBUG("%s: %c -> %s", __func__, c, str);
	return (rust_append(st, str));
}

static boolean_t
rustv0_parse_type(rust_state_t *restrict st, strview_t *restrict sv,
    boolean_t dummy __unused)
{
	strview_t save;
	size_t len, tuple_elem_count;
	boolean_t ret;
	char c;

	if (HAS_ERROR(st))
		return (B_FALSE);

	DEMDEBUG("%s: str='%.*s'", __func__, SV_PRINT(sv));

	if (sv_remaining(sv) == 0)
		return (B_FALSE);

	SAVE_LEN(st, len);
	sv_init_sv(&save, sv);

	switch (c = sv_consume_c(sv)) {
	case 'A':
		ret = rust_appendc(st, '[') &&
		    rustv0_parse_type(st, sv, B_FALSE) &&
		    rust_append(st, "; ") &&
		    rustv0_parse_const(st, sv, B_FALSE) &&
		    rust_appendc(st, ']');
		break;
	case 'S':
		ret = rust_appendc(st, '[') &&
		    rustv0_parse_type(st, sv, B_FALSE) &&
		    rust_appendc(st, ']');
		break;
	case 'T':
		tuple_elem_count = 0;
		ret = rust_appendc(st, '(') &&
		    rustv0_parse_opt_list(st, sv, rustv0_parse_type, ", ",
		    B_FALSE, &tuple_elem_count) &&
		    rust_append(st, (tuple_elem_count == 1) ? ",)" : ")");
		break;
	case 'R':
	case 'Q':
		/* `&mut T` or `&'... mut T` */
		if (!(ret = rust_appendc(st, '&')))
			break;

		/*
		 * lifetime is optional, but we need to add a trailing
		 * space if present (so we cannot use the OPTIONAL macro).
		 */
		if (rustv0_parse_lifetime(st, sv)) {
			if (!(ret = rust_appendc(st, ' ')))
				break;
		} else if (HAS_ERROR(st)) {
			break;
		}

		ret = rust_append(st, (c == 'Q') ? "mut " : "") &&
		    rustv0_parse_type(st, sv, B_FALSE);
		break;
	case 'P':
		ret = rust_append(st, "*const ") &&
		    rustv0_parse_type(st, sv, B_FALSE);
		break;
	case 'O':
		ret = rust_append(st, "*mut ") &&
		    rustv0_parse_type(st, sv, B_FALSE);
		break;
	case 'F':
		ret = rustv0_parse_fnsig(st, sv);
		break;
	case 'D':
		ret = rust_append(st, "dyn ") &&
		    rustv0_parse_dynbounds(st, sv);
		if (!ret)
			break;

		/*
		 * Rust RFC2603 shows the lifetime as required, however
		 * it appears this is optional.
		 */
		DEMDEBUG("%s: pre-lifetime: '%*s'", __func__, SV_PRINT(sv));

		/*
		 * We only want to print a non-zero (non "'_")
		 * lifetime.
		 */
		if (sv_consume_if(sv, "L_"))
			break;

		/*
		 * But if there is a lifetime we want to print,
		 * we want to prepend " + " before it.
		 */
		if (sv_peek(sv, 0) == 'L' &&
		    !(ret = rust_append(st, " + ")))
			break;

		ret = rustv0_parse_lifetime(st, sv);
		break;
	default:
		sv_init_sv(sv, &save);

		ret = rustv0_parse_backref(st, sv, rustv0_parse_type,
		    B_FALSE) ||
		    rustv0_parse_basic_type(st, sv);
		if (ret)
			break;

		ret = rustv0_parse_path(st, sv, B_FALSE);
		break;
	}

	DEMDEBUG("%s: type='%.*s' (%s)", __func__, CSTR_END(st, len),
	    ret ? "success" : "fail");

	return (ret);
}

/*
 * <path> = "C" <identifier>		crate root
 *	| "M" <impl-path> <type>	<T>
 *	| "X" <impl-path> <type> <path>	<T as Trait> (trait impl)
 *	| "Y" <type> <path>		<T as Trait> (trait definition)
 *	| "N" <ns> <path> <identifier>	...::ident (nested path)
 *	| "I" <path> {<generic-arg>} "E" ...<T, U>
 *	| <backref>
 */
static boolean_t
rustv0_parse_path(rust_state_t *restrict st, strview_t *restrict sv,
    boolean_t in_value)
{
	strview_t save;
	uint64_t disamb = 0;
	size_t len;
	boolean_t ret = B_FALSE;
	boolean_t save_skip;
	boolean_t args_stay_save = st->rs_args_stay_open;
	boolean_t args_open_save = st->rs_args_is_open;

	if (HAS_ERROR(st))
		return (B_FALSE);

	DEMDEBUG("%s: str='%.*s'", __func__, SV_PRINT(sv));

	if (sv_remaining(sv) == 0)
		return (B_FALSE);

	SAVE_LEN(st, len);
	sv_init_sv(&save, sv);

	switch (sv_consume_c(sv)) {
	case 'C':
		if (!OPTIONAL(st, rustv0_parse_disambiguator(st, sv, &disamb)))
			goto done;

		if (!rustv0_parse_undisambiguated_identifier(st, sv, B_FALSE))
			goto done;

		if (st->rs_verbose &&
		    !rust_append_printf(st, "[%" PRIx64 "]", disamb))
			goto done;
		break;
	case 'M':
		SKIP_BEGIN(st, save_skip);
		if (!rustv0_parse_impl_path(st, sv, in_value)) {
			SKIP_END(st, save_skip);
			goto done;
		}
		SKIP_END(st, save_skip);

		if (!rust_appendc(st, '<') ||
		    !rustv0_parse_type(st, sv, B_FALSE) ||
		    !rust_appendc(st, '>'))
			goto done;
		break;
	case 'X':
		SKIP_BEGIN(st, save_skip);
		if (!rustv0_parse_impl_path(st, sv, in_value)) {
			SKIP_END(st, save_skip);
			goto done;
		}
		SKIP_END(st, save_skip);
		/*FALLTHRU*/
	case 'Y':
		if (!rust_appendc(st, '<') ||
		    !rustv0_parse_type(st, sv, B_FALSE) ||
		    !rust_append(st, " as ") ||
		    !rustv0_parse_path(st, sv, B_FALSE) ||
		    !rust_appendc(st, '>'))
			goto done;
		break;
	case 'N':
		if (!rustv0_parse_nested_path(st, sv, in_value))
			goto done;
		break;
	case 'I':
		st->rs_args_stay_open = B_FALSE;
		st->rs_args_is_open = B_FALSE;

		if (!rustv0_parse_path(st, sv, in_value))
			goto done;

		if (in_value && !rust_append(st, "::"))
			goto done;

		if (!rust_appendc(st, '<') ||
		    !rustv0_parse_opt_list(st, sv, rustv0_parse_generic_arg,
		    ", ", B_FALSE, NULL))
			goto done;

		st->rs_args_stay_open = args_stay_save;
		st->rs_args_is_open = args_open_save;

		/*
		 * If we were asked to not close our list, then don't and
		 * indicate that the list is open.
		 */
		if (st->rs_args_stay_open) {
			st->rs_args_stay_open = B_FALSE;
			st->rs_args_is_open = B_TRUE;
		} else if (!rust_appendc(st, '>')) {
			goto done;
		}
		break;
	default:
		/*
		 * Didn't recognize the letter, so it has to be a path. Restore
		 * sv to state prior to switch and continue.
		 */
		sv_init_sv(sv, &save);
		if (!rustv0_parse_backref(st, sv, rustv0_parse_path, in_value))
			goto done;
	}

	ret = B_TRUE;

done:
	DEMDEBUG("%s: path='%.*s' (%s)", __func__, CSTR_END(st, len),
	    ret ? "success" : "fail");

	return (ret);
}

static boolean_t
rustv0_parse_impl_path(rust_state_t *restrict st, strview_t *restrict sv,
    boolean_t in_value)
{
	uint64_t val = 0;

	return (OPTIONAL(st, rustv0_parse_disambiguator(st, sv, &val)) &&
	    rustv0_parse_path(st, sv, in_value));
}

/*
 * A bit of a hack -- when printing a nested path, we need to know
 * if the identifier is there or not in order to correctly format
 * the output preceeding it (when present). This peeks ahead and
 * determines this.
 */
static boolean_t
rustv0_has_name(rust_state_t *restrict st, strview_t *restrict sv,
    boolean_t *has_namep)
{
	strview_t save;

	if (HAS_ERROR(st))
		return (B_FALSE);

	DEMDEBUG("%s: str='%.*s'", __func__, SV_PRINT(sv));

	if (sv_remaining(sv) == 0)
		return (B_FALSE);

	sv_init_sv(&save, sv);

	/* For checking the length, we don't care if it's punycode or not */
	(void) sv_consume_if_c(&save, 'u');

	if (sv_remaining(sv) == 0) {
		st->rs_error = EINVAL;
		return (B_FALSE);
	}

	if (sv_consume_if_c(&save, '0')) {
		*has_namep = B_FALSE;
		return (B_TRUE);
	}

	*has_namep = B_TRUE;
	return (B_TRUE);
}

static boolean_t
rustv0_parse_nested_path(rust_state_t *restrict st, strview_t *restrict sv,
    boolean_t in_value)
{
	uint64_t disambiguator = 0;
	size_t len = 0;
	char ns;
	boolean_t ret = B_FALSE;
	boolean_t has_name;

	if (HAS_ERROR(st))
		return (B_FALSE);

	DEMDEBUG("%s: str='%.*s'", __func__, SV_PRINT(sv));

	if (sv_remaining(sv) == 0)
		return (B_FALSE);

	SAVE_LEN(st, len);

	ns = sv_consume_c(sv);

	if (!rustv0_parse_path(st, sv, in_value))
		goto done;

	if (!OPTIONAL(st, rustv0_parse_disambiguator(st, sv, &disambiguator)))
		goto done;

	if (!rustv0_has_name(st, sv, &has_name))
		goto done;

	if (ISUPPER(ns)) {
		if (!rust_append(st, "::{"))
			goto done;

		switch (ns) {
		case 'C':
			if (!rust_append(st, "closure"))
				goto done;
			break;
		case 'S':
			if (!rust_append(st, "shim"))
				goto done;
			break;
		default:
			if (!rust_appendc(st, ns))
				goto done;
			break;
		}

		if (has_name && !rust_appendc(st, ':'))
			goto done;

		if (!rustv0_parse_undisambiguated_identifier(st, sv, B_FALSE))
			goto done;

		ret = rust_append_printf(st, "#%" PRIu64 "}", disambiguator);
	} else {
		if (has_name) {
			if (!(ret = rust_append(st, "::")))
				goto done;
		}
		ret = rustv0_parse_undisambiguated_identifier(st, sv, B_FALSE);
	}

done:
	DEMDEBUG("%s: nested path = '%.*s' (%s)", __func__, CSTR_END(st, len),
	    ret ? "success" : "fail");

	return (ret);
}

/*
 * <disambiguator> = "s" <base-64-number>
 *
 */
static boolean_t
rustv0_parse_disambiguator(rust_state_t *restrict st, strview_t *restrict sv,
    uint64_t *valp)
{
	if (HAS_ERROR(st) || sv_remaining(sv) < 2)
		return (B_FALSE);

	DEMDEBUG("%s: str='%.*s'", __func__, SV_PRINT(sv));

	*valp = 0;

	if (!sv_consume_if_c(sv, 's'))
		return (B_FALSE);

	if (!rustv0_parse_base62(st, sv, valp)) {
		st->rs_error = EINVAL;
		return (B_FALSE);
	}

	/*
	 * Rust RFC 2603 details this in Appendix A, but not the main
	 * portion of the RFC. If no disambiguator is present, the value
	 * is 0, if the decoded value is 0, the index is 1, ...
	 * rustv0_parse_base62() already adjusts _ -> 0, 0 -> 1, so we
	 * only need to add one here to complete the adjustment.
	 */
	*valp = *valp + 1;

	DEMDEBUG("%s: disambiguator=%" PRIu64, __func__, *valp);
	return (B_TRUE);
}

/* <undisambiguated-identifier> = ["u"] <decimal-number> ["_"] <bytes> */
static boolean_t
rustv0_parse_undisambiguated_identifier(rust_state_t *restrict st,
    strview_t *restrict sv, boolean_t repl_underscore)
{
	uint64_t len = 0;
	boolean_t puny = B_FALSE;

	if (HAS_ERROR(st))
		return (B_FALSE);

	DEMDEBUG("%s: str='%.*s'", __func__, SV_PRINT(sv));

	if (sv_remaining(sv) == 0)
		return (B_FALSE);

	if (sv_consume_if_c(sv, 'u'))
		puny = B_TRUE;

	if (!rust_parse_base10(st, sv, &len))
		return (B_FALSE);

	/* skip optional separator '_' */
	(void) sv_consume_if_c(sv, '_');

	if (sv_remaining(sv) < len) {
		DEMDEBUG("%s: ERROR: identifier length (%" PRIu64 ") "
		    "> remaining bytes (%zu)", __func__, len,
		    sv_remaining(sv));
		return (B_FALSE);
	}

	/* 0 length identifiers are acceptable */
	if (len == 0)
		return (B_TRUE);

	if (puny) {
		strview_t ident;

		sv_init_sv_range(&ident, sv, len);
		if (!rustv0_puny_decode(st, &ident, repl_underscore))
			return (B_FALSE);

		sv_consume_n(sv, len);
		return (B_TRUE);
	}

	/*
	 * rust identifiers do not contain '-'. However ABI identifiers
	 * are allowed to contain them (e.g. extern "foo-bar" fn ...).
	 * They are substituted with '_' in the mangled output. If we
	 * do not need to reverse this, we can just append 'len' bytes
	 * of sv.  Otherwise we need to go through and reverse this
	 * substitution.
	 */
	if (!repl_underscore)
		return (rust_append_sv(st, len, sv));

	/*
	 * We checked earlier that len < sv_remaining(sv); so this loop
	 * cannot overrun.
	 */
	for (size_t i = 0; i < len; i++) {
		char c = sv_consume_c(sv);

		if (c == '_')
			c = '-';

		if (!rust_appendc(st, c))
			return (B_FALSE);
	}

	return (B_TRUE);
}

/* <backref> = "B" <base-62-number> */
static boolean_t
rustv0_parse_backref(rust_state_t *restrict st, strview_t *restrict sv,
    boolean_t (*fn)(rust_state_t *restrict, strview_t *restrict, boolean_t b),
    boolean_t bval)
{
	strview_t backref;
	strview_t target;
	uint64_t idx = 0;
	size_t save_len;
	size_t len;

	if (HAS_ERROR(st))
		return (B_FALSE);

	sv_init_sv(&backref, sv);

	if (!sv_consume_if_c(sv, 'B'))
		return (B_FALSE);

	DEMDEBUG("%s: str='B%.*s'", __func__, SV_PRINT(sv));

	if (!rustv0_parse_base62(st, sv, &idx)) {
		st->rs_error = EINVAL;
		return (B_FALSE);
	}

	/*
	 * Determine how many bytes we've consumed (up to the start of
	 * the current backref token).
	 */
	VERIFY3P(backref.sv_first, >=, st->rs_orig.sv_first);
	len = (size_t)(uintptr_t)(backref.sv_first - st->rs_orig.sv_first);

	/*
	 * The backref can only refer to an index prior to the start of
	 * the current backref token -- that is must always refer back in
	 * the string, never to the current position or beyond.
	 */
	if (idx >= len) {
		DEMDEBUG("%s: ERROR: backref index (%" PRIu64 ") "
		    "is out of range [0, %zu)", __func__, idx, len);
		st->rs_error = ERANGE;
		return (B_FALSE);
	}

	/*
	 * Create a strview_t of the original string (sans prefix) by
	 * copying from st->rs_orig. The length of the target strview_t is
	 * capped to end immediately prior to this backref token. Since we
	 * enforce that backrefs must always refer to already processed
	 * portions of the string (i.e. must always refer backwards), and the
	 * length of the strview_t is set to end prior to the start of this
	 * backref token, we guarantee processing of a backref will always
	 * terminate before it can possibly encounter this backref token
	 * and cause a loop -- either the processing terminates normally or
	 * it reaches the end of the capped strview_t.
	 */
	sv_init_sv_range(&target, &st->rs_orig, len);

	/*
	 * Consume all the input in the target strview_t up to the index
	 */
	sv_consume_n(&target, idx);

	DEMDEBUG("%s: backref starting at %" PRIu64 " str='%.*s'%s", __func__,
	    idx, SV_PRINT(&target), st->rs_skip ? " (skipping)" : "");

	/*
	 * If we're skipping the output, there's no reason to bother reparsing
	 * the output -- we're not going to save it. We still setup everything
	 * so that the debug output is still emitted.
	 */
	if (st->rs_skip)
		return (B_TRUE);

	SAVE_LEN(st, save_len);
	if (!fn(st, &target, bval))
		return (B_FALSE);

	DEMDEBUG("%s: backref is '%.*s'", __func__, CSTR_END(st, save_len));
	return (B_TRUE);
}

static boolean_t
rustv0_append_lifetime(rust_state_t *restrict st, uint64_t lifetime)
{
	uint64_t bound_lt;

	if (HAS_ERROR(st))
		return (B_FALSE);

	if (!rust_appendc(st, '\''))
		return (B_FALSE);

	if (lifetime == 0)
		return (rust_appendc(st, '_'));

	if (sub_overflow(st->rs_lt_depth, lifetime, &bound_lt)) {
		DEMDEBUG("%s: ERROR: lifetime value %" PRIu64
		    " > current depth %" PRIu64, __func__, lifetime,
		    st->rs_lt_depth);
		st->rs_lt_depth = ERANGE;
		return (B_FALSE);
	}

	/*
	 * Use 'a, 'b, ...
	 */
	if (bound_lt < 26) {
		char c = (char)bound_lt + 'a';
		return (rust_append_printf(st, "%c", c));
	}

	/*
	 * Otherwise, use '_123, '_456, ...
	 */
	return (rust_append_printf(st, "_%" PRIu64, bound_lt));
}

static boolean_t
rustv0_parse_lifetime(rust_state_t *restrict st, strview_t *restrict sv)
{
	uint64_t lifetime;

	if (!sv_consume_if_c(sv, 'L'))
		return (B_FALSE);

	if (!rustv0_parse_base62(st, sv, &lifetime))
		return (B_FALSE);

	return (rustv0_append_lifetime(st, lifetime));
}

static boolean_t
rustv0_parse_const_data(rust_state_t *restrict st,
    const_type_class_t type_class, strview_t *restrict sv)
{
	uint64_t val = 0;
	size_t save_len;
	boolean_t neg = B_FALSE;
	boolean_t ret = B_FALSE;

	VERIFY3S(type_class, !=, CTC_INVALID);

	if (HAS_ERROR(st))
		return (B_FALSE);

	DEMDEBUG("%s: str='%.*s'", __func__, SV_PRINT(sv));
	SAVE_LEN(st, save_len);

	if (sv_remaining(sv) == 0)
		return (B_FALSE);

	if (type_class == CTC_SIGNED && sv_consume_if_c(sv, 'n'))
		neg = B_TRUE;

	ret = OPTIONAL(st, rustv0_parse_hex_num(st, sv, &val)) &&
	    sv_consume_if_c(sv, '_');
	if (!ret)
		goto done;

	switch (type_class) {
	case CTC_SIGNED:
	case CTC_UNSIGNED:
		ret = rust_append_printf(st, "%s%" PRIu64, neg ? "-" : "", val);
		break;
	case CTC_BOOL:
		if (val > 1) {
			DEMDEBUG("%s: invalid bool val %" PRIu64, __func__,
			    val);
			ret = B_FALSE;
			break;
		}
		ret = rust_append_printf(st, "%s",
		    (val == 0) ? "false" : "true");
		break;
	case CTC_CHAR:
		if (val > UINT32_MAX) {
			DEMDEBUG("%s: char value %" PRIu64 " out of range",
			    __func__, val);
			ret = B_FALSE;
			break;
		}

		ret = rust_appendc(st, '\'') && rust_append_utf8_c(st, val) &&
		    rust_appendc(st, '\'');
		break;
	default:
		ret = B_FALSE;
	}

done:
	DEMDEBUG("%s: const='%.*s' (%s)", __func__, CSTR_END(st, save_len),
	    ret ? "success" : "fail");

	return (ret);
}

static boolean_t
rustv0_parse_const(rust_state_t *restrict st, strview_t *restrict sv,
    boolean_t dummy __unused)
{
	strview_t type;
	size_t start_len;
	const_type_class_t ctype_class;
	char ctype;
	boolean_t save_skip;
	boolean_t ret;

	if (HAS_ERROR(st))
		return (B_FALSE);

	DEMDEBUG("%s: str='%.*s'", __func__, SV_PRINT(sv));
	SAVE_LEN(st, start_len);

	if (sv_remaining(sv) == 0)
		return (B_FALSE);

	if (rustv0_parse_backref(st, sv, rustv0_parse_const, B_FALSE))
		return (B_TRUE);

	if (sv_consume_if_c(sv, 'p')) {
		ret = rust_appendc(st, '_');
		goto done;
	}

	ctype = sv_peek(sv, 0);
	ctype_class = rustv0_classify_const_type(ctype);
	if (ctype_class == CTC_INVALID) {
		DEMDEBUG("%s: const type isn't a valid const generic type",
		    __func__);
		return (B_FALSE);
	}

	/*
	 * This isn't spelled out clearly in Rust RFC 2603, but currently
	 * only unsigned int types are allowed at this point. However, we
	 * have a bit of a potential tricky situation. Unlike formatting
	 * the other tokens, if we want to display the type, we do so
	 * _after_ the value, even though the type appears first.
	 *
	 * This is bit of a hack, but we save off the input position from
	 * sv before the parse the type. We then parse it without saving
	 * the resulting value, then parse and output the constant. If
	 * we wish to then display the type, we can go back and parse
	 * the type again, this time saving the result.
	 */
	sv_init_sv(&type, sv);

	SKIP_BEGIN(st, save_skip);
	ret = rustv0_parse_type(st, sv, B_FALSE);
	SKIP_END(st, save_skip);

	if (!ret) {
		DEMDEBUG("%s: const type isn't valid", __func__);
		return (B_FALSE);
	}

	if (sv_consume_if_c(sv, 'p')) {
		ret = rust_appendc(st, '_');
	} else {
		ret = rustv0_parse_const_data(st, ctype_class, sv);
	}
	if (!ret)
		goto done;

	if (st->rs_show_const_type) {
		ret = rust_append(st, ": ") &&
		    rustv0_parse_uint_type(st, &type);
	}

done:
	DEMDEBUG("%s: const='%.*s' (%s)", __func__, CSTR_END(st, start_len),
	    ret ? "success" : "fail");
	return (ret);
}

static boolean_t
rustv0_parse_abi(rust_state_t *restrict st, strview_t *restrict sv)
{
	DEMDEBUG("%s: str = '%.*s'", __func__, SV_PRINT(sv));

	if (sv_consume_if_c(sv, 'C'))
		return (rust_appendc(st, 'C'));

	return (rustv0_parse_undisambiguated_identifier(st, sv, B_TRUE));
}

static boolean_t
rustv0_parse_binder(rust_state_t *restrict st, strview_t *restrict sv)
{
	uint64_t n, i;

	if (!sv_consume_if_c(sv, 'G'))
		return (B_FALSE);

	if (!rustv0_parse_base62(st, sv, &n))
		return (B_FALSE);
	n += 1;

	if (!rust_append(st, "for<"))
		return (B_FALSE);

	for (i = 0; i < n; i++) {
		if (i > 0 && !rust_append(st, ", "))
			return (B_FALSE);

		st->rs_lt_depth++;
		if (!rustv0_append_lifetime(st, 1))
			return (B_FALSE);
	}

	if (!rust_append(st, "> "))
		return (B_FALSE);

	return (B_TRUE);
}

/*
 * <fn-sig> := [<binder>] ["U"] ["K" <abi>] {type} "E" <type>
 *
 * Note that while the Rust RFC states the binder is manditory, based on
 * actual examples, and comparing with the rust-based demangler, it is in
 * fact optional.
 */
static boolean_t
rustv0_parse_fnsig(rust_state_t *restrict st, strview_t *restrict sv)
{
	uint64_t save_lt = st->rs_lt_depth;

	DEMDEBUG("%s: str = '%.*s'", __func__, SV_PRINT(sv));

	if (!OPTIONAL(st, rustv0_parse_binder(st, sv)))
		return (B_FALSE);

	if (sv_consume_if_c(sv, 'U') && !rust_append(st, "unsafe "))
		return (B_FALSE);

	if (sv_consume_if_c(sv, 'K') &&
	    (!rust_append(st, "extern \"") || !rustv0_parse_abi(st, sv) ||
	    !rust_append(st, "\" ")))
		return (B_FALSE);

	if (!rust_append(st, "fn("))
		return (B_FALSE);

	if (!rustv0_parse_opt_list(st, sv, rustv0_parse_type, ", ", B_FALSE,
	    NULL)) {
		return (B_FALSE);
	}

	if (!rust_appendc(st, ')'))
		return (B_FALSE);

	/* If the return type is (), don't print it */
	if (!sv_consume_if_c(sv, 'u')) {
		if (!rust_append(st, " -> "))
			return (B_FALSE);

		if (!rustv0_parse_type(st, sv, B_FALSE))
			return (B_FALSE);
	}

	st->rs_lt_depth = save_lt;

	return (B_TRUE);
}

/*
 * <dyn-trait-assoc-binding> = "p" <undisambiguated-identifier> <type>
 */
static boolean_t
rustv0_parse_dyn_trait_assoc_binding(rust_state_t *restrict st,
    strview_t *restrict sv, boolean_t open)
{
	size_t save_len;

	if (HAS_ERROR(st))
		return (B_FALSE);

	if (sv_remaining(sv) == 0)
		return (B_FALSE);

	if (!sv_consume_if_c(sv, 'p'))
		return (B_FALSE);

	DEMDEBUG("%s: str='%.*s'", __func__, SV_PRINT(sv));
	SAVE_LEN(st, save_len);

	if (!rust_append(st, open ? ", " : "<"))
		return (B_FALSE);

	if (!rustv0_parse_undisambiguated_identifier(st, sv, B_FALSE)) {
		st->rs_error = EINVAL;
		return (B_FALSE);
	}

	if (!rust_append(st, " = "))
		return (B_FALSE);

	if (!rustv0_parse_type(st, sv, B_FALSE)) {
		st->rs_error = EINVAL;
		return (B_FALSE);
	}

	DEMDEBUG("%s: binding='%.*s'", __func__, CSTR_END(st, save_len));

	return (B_TRUE);
}

static boolean_t
rustv0_parse_dyn_trait(rust_state_t *restrict st, strview_t *restrict sv,
    boolean_t dummy __unused)
{
	boolean_t stay_save = st->rs_args_stay_open;
	boolean_t open_save = st->rs_args_is_open;
	boolean_t open = B_FALSE;

	if (HAS_ERROR(st))
		return (B_FALSE);

	DEMDEBUG("%s: str='%.*s'", __func__, SV_PRINT(sv));

	/*
	 * This is a bit subtle, but when formatting a trait in trait,
	 * we want something like this:
	 *
	 *	dyn Trait<T, U, Assoc=X>
	 *
	 * instead of
	 *
	 *	dyn Trait<T, U, <Assoc=X>>
	 *
	 * So when parsing the path, if we encounter generic arguments, we want
	 * the arg list to remain open at the end of processing the path so
	 * we can append the bindings to it. We set rs_args_stay_open to B_TRUE
	 * to indidcate to rustv0_parse_path() that a generic argument list
	 * should not be closed (i.e. don't append a '>' at the end of the
	 * list). If rustv0_parse_path() encounters a list of generic arguments,
	 * it will also set rs->args_is_open to indiciate it opened the list.
	 * We save this in 'open' so that when we process the associated
	 * bindings, we know if we need to open the list on the first binding
	 * or not -- we don't want 'dyn Trait<>' if there are no bindings,
	 * just 'dyn Trait'.
	 */
	st->rs_args_stay_open = B_TRUE;
	st->rs_args_is_open = B_FALSE;

	if (!rustv0_parse_path(st, sv, B_FALSE)) {
		st->rs_args_stay_open = stay_save;
		st->rs_args_is_open = open_save;
		return (B_FALSE);
	}

	open = st->rs_args_is_open;

	st->rs_args_stay_open = stay_save;
	st->rs_args_is_open = open_save;

	while (rustv0_parse_dyn_trait_assoc_binding(st, sv, open)) {
		open = B_TRUE;
	}

	if (HAS_ERROR(st))
		return (B_FALSE);

	if (open && !rust_appendc(st, '>'))
		return (B_FALSE);

	return (!HAS_ERROR(st));
}

static boolean_t
rustv0_parse_dynbounds(rust_state_t *restrict st, strview_t *restrict sv)
{
	uint64_t save_lt = st->rs_lt_depth;

	if (HAS_ERROR(st))
		return (B_FALSE);

	DEMDEBUG("%s: str='%.*s'", __func__, SV_PRINT(sv));

	/*
	 * This is another case where Rust RFC2603 seems to disagree with
	 * the implementation. The RFC implies this is mandatory, while
	 * the implementations treat it as optional.
	 */
	if (!OPTIONAL(st, rustv0_parse_binder(st, sv)))
		return (B_FALSE);

	if (!rustv0_parse_opt_list(st, sv, rustv0_parse_dyn_trait, " + ",
	    B_FALSE, NULL))
		return (B_FALSE);

	st->rs_lt_depth = save_lt;

	return (B_TRUE);
}

static boolean_t
rustv0_parse_generic_arg(rust_state_t *restrict st, strview_t *restrict sv,
    boolean_t dummy __unused)
{
	DEMDEBUG("%s: str='%.*s'", __func__, SV_PRINT(sv));

	if (sv_consume_if_c(sv, 'K'))
		return (rustv0_parse_const(st, sv, B_FALSE));

	if (rustv0_parse_lifetime(st, sv))
		return (B_TRUE);

	return (rustv0_parse_type(st, sv, B_FALSE));
}

/*
 * Parse a hex value into *valp. Note that rust only uses lower case
 * hex values.
 */
static boolean_t
rustv0_parse_hex_num(rust_state_t *restrict st, strview_t *restrict sv,
    uint64_t *restrict valp)
{
	uint64_t val = 0;
	size_t ndigits = 0;

	if (HAS_ERROR(st))
		return (B_FALSE);

	DEMDEBUG("%s: str='%.*s'", __func__, SV_PRINT(sv));

	if (sv_remaining(sv) == 0)
		return (B_FALSE);

	/*
	 * Unfortunately, Rust RFC 2603 also doesn't not explicty define
	 * {hex-digits}. We follow what decimal digits does, and treat a
	 * leading 0 as a terminator.
	 */
	while (sv_remaining(sv) > 0) {
		char c = sv_peek(sv, 0);

		if (ISDIGIT(c)) {
			val *= 16;
			val += c - '0';
		} else if (c >= 'a' && c <= 'f') {
			val *= 16;
			val += c - 'a' + 10;
		} else {
			break;
		}

		sv_consume_n(sv, 1);

		if (++ndigits == 1 && val == 0)
			break;
	}

	if (ndigits > 0)
		*valp = val;

	return ((ndigits > 0) ? B_TRUE : B_FALSE);
}

/*
 * Parse a base62 number into *valp.  The number is explicitly terminated
 * by a '_'.  The values are also offset by 0 -- that is '_' == 0,
 * '0_' == 1, ...
 */
static boolean_t
rustv0_parse_base62(rust_state_t *restrict st, strview_t *restrict sv,
    uint64_t *restrict valp)
{
	uint64_t val = 0;
	char c;

	if (HAS_ERROR(st))
		return (B_FALSE);

	DEMDEBUG("%s: str='%.*s'", __func__, SV_PRINT(sv));

	if (sv_remaining(sv) == 0)
		return (B_FALSE);

	/* A terminating '_' without any digits is 0 */
	if (sv_consume_if_c(sv, '_')) {
		*valp = 0;
		return (B_TRUE);
	}

	/* Need at least one valid digit if > 0 */
	if (!ISALNUM(sv_peek(sv, 0)))
		return (B_FALSE);

	while (sv_remaining(sv) > 0) {
		c = sv_consume_c(sv);

		if (c == '_') {
			/*
			 * Because a lone '_' was already handled earlier,
			 * we know we've had at least one other digit and
			 * can increment the value and return.
			 */
			*valp = val + 1;
			return (B_TRUE);
		} else if (ISDIGIT(c)) {
			val *= 62;
			val += c - '0';
		} else if (ISLOWER(c)) {
			val *= 62;
			val += c - 'a' + 10;
		} else if (ISUPPER(c)) {
			val *= 62;
			val += c - 'A' + 36;
		} else {
			return (B_FALSE);
		}
	}

	/* We reached the end of the string without a terminating _ */
	return (B_FALSE);
}

static const_type_class_t
rustv0_classify_const_type(char type)
{
	switch (type) {
	case 'h': case 't': case 'm': case 'y': case 'o': case 'j':
		return (CTC_UNSIGNED);
	case 'a': case 'i': case 'l': case 'n': case 's': case 'x':
		return (CTC_SIGNED);
	case 'b':
		return (CTC_BOOL);
	case 'c':
		return (CTC_CHAR);
	default:
		return (CTC_INVALID);
	}
}

/*
 * Make sure the name is a plausible mangled rust symbol.
 * Non-ASCII are never allowed.  Rust itself uses [_0-9A-Za-z], however
 * some things will add a suffix starting with a '.' (e.g. LLVM thin LTO).
 * As such we proceed in two phases. We first only allow [_0-9A-Z-az] until
 * we encounter a '.'. At that point, any ASCII character is allowed.
 */
static boolean_t
rustv0_valid_sym(const strview_t *sv)
{
	size_t i;
	boolean_t check_rust = B_TRUE;

	for (i = 0; i < sv->sv_rem; i++) {
		char c = sv->sv_first[i];

		if (ISALNUM(c) || c == '_')
			continue;

		if (c == '.') {
			check_rust = B_FALSE;
			continue;
		}

		if (check_rust || (c & 0x80) != 0) {
			DEMDEBUG("%s: ERROR found invalid character '%c' "
			    "in '%.*s' at index %zu",
			    __func__, c, SV_PRINT(sv), i);
			return (B_FALSE);
		}
	}
	return (B_TRUE);
}