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
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
|
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include <google/protobuf/stubs/hash.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor_database.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/text_format.h>
#include <google/protobuf/unknown_field_set.h>
#include <google/protobuf/wire_format.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/substitute.h>
#include <google/protobuf/stubs/map-util.h>
#include <google/protobuf/stubs/stl_util-inl.h>
#undef PACKAGE // autoheader #defines this. :(
namespace google {
namespace protobuf {
const FieldDescriptor::CppType
FieldDescriptor::kTypeToCppTypeMap[MAX_TYPE + 1] = {
static_cast<CppType>(0), // 0 is reserved for errors
CPPTYPE_DOUBLE, // TYPE_DOUBLE
CPPTYPE_FLOAT, // TYPE_FLOAT
CPPTYPE_INT64, // TYPE_INT64
CPPTYPE_UINT64, // TYPE_UINT64
CPPTYPE_INT32, // TYPE_INT32
CPPTYPE_UINT64, // TYPE_FIXED64
CPPTYPE_UINT32, // TYPE_FIXED32
CPPTYPE_BOOL, // TYPE_BOOL
CPPTYPE_STRING, // TYPE_STRING
CPPTYPE_MESSAGE, // TYPE_GROUP
CPPTYPE_MESSAGE, // TYPE_MESSAGE
CPPTYPE_STRING, // TYPE_BYTES
CPPTYPE_UINT32, // TYPE_UINT32
CPPTYPE_ENUM, // TYPE_ENUM
CPPTYPE_INT32, // TYPE_SFIXED32
CPPTYPE_INT64, // TYPE_SFIXED64
CPPTYPE_INT32, // TYPE_SINT32
CPPTYPE_INT64, // TYPE_SINT64
};
const char * const FieldDescriptor::kTypeToName[MAX_TYPE + 1] = {
"ERROR", // 0 is reserved for errors
"double", // TYPE_DOUBLE
"float", // TYPE_FLOAT
"int64", // TYPE_INT64
"uint64", // TYPE_UINT64
"int32", // TYPE_INT32
"fixed64", // TYPE_FIXED64
"fixed32", // TYPE_FIXED32
"bool", // TYPE_BOOL
"string", // TYPE_STRING
"group", // TYPE_GROUP
"message", // TYPE_MESSAGE
"bytes", // TYPE_BYTES
"uint32", // TYPE_UINT32
"enum", // TYPE_ENUM
"sfixed32", // TYPE_SFIXED32
"sfixed64", // TYPE_SFIXED64
"sint32", // TYPE_SINT32
"sint64", // TYPE_SINT64
};
const char * const FieldDescriptor::kLabelToName[MAX_LABEL + 1] = {
"ERROR", // 0 is reserved for errors
"optional", // LABEL_OPTIONAL
"required", // LABEL_REQUIRED
"repeated", // LABEL_REPEATED
};
#ifndef _MSC_VER // MSVC doesn't need these and won't even accept them.
const int FieldDescriptor::kMaxNumber;
const int FieldDescriptor::kFirstReservedNumber;
const int FieldDescriptor::kLastReservedNumber;
#endif
namespace {
const string kEmptyString;
string ToCamelCase(const string& input) {
bool capitalize_next = false;
string result;
result.reserve(input.size());
for (int i = 0; i < input.size(); i++) {
if (input[i] == '_') {
capitalize_next = true;
} else if (capitalize_next) {
// Note: I distrust ctype.h due to locales.
if ('a' <= input[i] && input[i] <= 'z') {
result.push_back(input[i] - 'a' + 'A');
} else {
result.push_back(input[i]);
}
capitalize_next = false;
} else {
result.push_back(input[i]);
}
}
// Lower-case the first letter.
if (!result.empty() && 'A' <= result[0] && result[0] <= 'Z') {
result[0] = result[0] - 'A' + 'a';
}
return result;
}
// A DescriptorPool contains a bunch of hash_maps to implement the
// various Find*By*() methods. Since hashtable lookups are O(1), it's
// most efficient to construct a fixed set of large hash_maps used by
// all objects in the pool rather than construct one or more small
// hash_maps for each object.
//
// The keys to these hash_maps are (parent, name) or (parent, number)
// pairs. Unfortunately STL doesn't provide hash functions for pair<>,
// so we must invent our own.
//
// TODO(kenton): Use StringPiece rather than const char* in keys? It would
// be a lot cleaner but we'd just have to convert it back to const char*
// for the open source release.
typedef pair<const void*, const char*> PointerStringPair;
struct PointerStringPairEqual {
inline bool operator()(const PointerStringPair& a,
const PointerStringPair& b) const {
return a.first == b.first && strcmp(a.second, b.second) == 0;
}
};
template<typename PairType>
struct PointerIntegerPairHash {
size_t operator()(const PairType& p) const {
// FIXME(kenton): What is the best way to compute this hash? I have
// no idea! This seems a bit better than an XOR.
return reinterpret_cast<intptr_t>(p.first) * ((1 << 16) - 1) + p.second;
}
// Used only by MSVC and platforms where hash_map is not available.
static const size_t bucket_size = 4;
static const size_t min_buckets = 8;
inline bool operator()(const PairType& a, const PairType& b) const {
return a.first < b.first ||
(a.first == b.first && a.second < b.second);
}
};
typedef pair<const Descriptor*, int> DescriptorIntPair;
typedef pair<const EnumDescriptor*, int> EnumIntPair;
struct PointerStringPairHash {
size_t operator()(const PointerStringPair& p) const {
// FIXME(kenton): What is the best way to compute this hash? I have
// no idea! This seems a bit better than an XOR.
hash<const char*> cstring_hash;
return reinterpret_cast<intptr_t>(p.first) * ((1 << 16) - 1) +
cstring_hash(p.second);
}
// Used only by MSVC and platforms where hash_map is not available.
static const size_t bucket_size = 4;
static const size_t min_buckets = 8;
inline bool operator()(const PointerStringPair& a,
const PointerStringPair& b) const {
if (a.first < b.first) return true;
if (a.first > b.first) return false;
return strcmp(a.second, b.second) < 0;
}
};
struct Symbol {
enum Type {
NULL_SYMBOL, MESSAGE, FIELD, ENUM, ENUM_VALUE, SERVICE, METHOD, PACKAGE
};
Type type;
union {
const Descriptor* descriptor;
const FieldDescriptor* field_descriptor;
const EnumDescriptor* enum_descriptor;
const EnumValueDescriptor* enum_value_descriptor;
const ServiceDescriptor* service_descriptor;
const MethodDescriptor* method_descriptor;
const FileDescriptor* package_file_descriptor;
};
inline Symbol() : type(NULL_SYMBOL) { descriptor = NULL; }
inline bool IsNull() const { return type == NULL_SYMBOL; }
inline bool IsType() const {
return type == MESSAGE || type == ENUM;
}
inline bool IsAggregate() const {
return type == MESSAGE || type == PACKAGE
|| type == ENUM || type == SERVICE;
}
#define CONSTRUCTOR(TYPE, TYPE_CONSTANT, FIELD) \
inline explicit Symbol(const TYPE* value) { \
type = TYPE_CONSTANT; \
this->FIELD = value; \
}
CONSTRUCTOR(Descriptor , MESSAGE , descriptor )
CONSTRUCTOR(FieldDescriptor , FIELD , field_descriptor )
CONSTRUCTOR(EnumDescriptor , ENUM , enum_descriptor )
CONSTRUCTOR(EnumValueDescriptor, ENUM_VALUE, enum_value_descriptor )
CONSTRUCTOR(ServiceDescriptor , SERVICE , service_descriptor )
CONSTRUCTOR(MethodDescriptor , METHOD , method_descriptor )
CONSTRUCTOR(FileDescriptor , PACKAGE , package_file_descriptor)
#undef CONSTRUCTOR
const FileDescriptor* GetFile() const {
switch (type) {
case NULL_SYMBOL: return NULL;
case MESSAGE : return descriptor ->file();
case FIELD : return field_descriptor ->file();
case ENUM : return enum_descriptor ->file();
case ENUM_VALUE : return enum_value_descriptor->type()->file();
case SERVICE : return service_descriptor ->file();
case METHOD : return method_descriptor ->service()->file();
case PACKAGE : return package_file_descriptor;
}
return NULL;
}
};
const Symbol kNullSymbol;
typedef hash_map<const char*, Symbol,
hash<const char*>, streq>
SymbolsByNameMap;
typedef hash_map<PointerStringPair, Symbol,
PointerStringPairHash, PointerStringPairEqual>
SymbolsByParentMap;
typedef hash_map<const char*, const FileDescriptor*,
hash<const char*>, streq>
FilesByNameMap;
typedef hash_map<PointerStringPair, const FieldDescriptor*,
PointerStringPairHash, PointerStringPairEqual>
FieldsByNameMap;
typedef hash_map<DescriptorIntPair, const FieldDescriptor*,
PointerIntegerPairHash<DescriptorIntPair> >
FieldsByNumberMap;
typedef hash_map<EnumIntPair, const EnumValueDescriptor*,
PointerIntegerPairHash<EnumIntPair> >
EnumValuesByNumberMap;
// This is a map rather than a hash_map, since we use it to iterate
// through all the extensions that extend a given Descriptor, and an
// ordered data structure that implements lower_bound is convenient
// for that.
typedef map<DescriptorIntPair, const FieldDescriptor*>
ExtensionsGroupedByDescriptorMap;
} // anonymous namespace
// ===================================================================
// DescriptorPool::Tables
class DescriptorPool::Tables {
public:
Tables();
~Tables();
// Checkpoint the state of the tables. Future calls to Rollback() will
// return the Tables to this state. This is used when building files, since
// some kinds of validation errors cannot be detected until the file's
// descriptors have already been added to the tables. BuildFile() calls
// Checkpoint() before it starts building and Rollback() if it encounters
// an error.
void Checkpoint();
// Roll back the Tables to the state of the last Checkpoint(), removing
// everything that was added after that point.
void Rollback();
// The stack of files which are currently being built. Used to detect
// cyclic dependencies when loading files from a DescriptorDatabase. Not
// used when fallback_database_ == NULL.
vector<string> pending_files_;
// A set of files which we have tried to load from the fallback database
// and encountered errors. We will not attempt to load them again.
// Not used when fallback_database_ == NULL.
hash_set<string> known_bad_files_;
// The set of descriptors for which we've already loaded the full
// set of extensions numbers from fallback_database_.
hash_set<const Descriptor*> extensions_loaded_from_db_;
// -----------------------------------------------------------------
// Finding items.
// Find symbols. This returns a null Symbol (symbol.IsNull() is true)
// if not found.
inline Symbol FindSymbol(const string& key) const;
// This implements the body of DescriptorPool::Find*ByName(). It should
// really be a private method of DescriptorPool, but that would require
// declaring Symbol in descriptor.h, which would drag all kinds of other
// stuff into the header. Yay C++.
Symbol FindByNameHelper(
const DescriptorPool* pool, const string& name) const;
// These return NULL if not found.
inline const FileDescriptor* FindFile(const string& key) const;
inline const FieldDescriptor* FindExtension(const Descriptor* extendee,
int number);
inline void FindAllExtensions(const Descriptor* extendee,
vector<const FieldDescriptor*>* out) const;
// -----------------------------------------------------------------
// Adding items.
// These add items to the corresponding tables. They return false if
// the key already exists in the table. For AddSymbol(), the string passed
// in must be one that was constructed using AllocateString(), as it will
// be used as a key in the symbols_by_name_ map without copying.
bool AddSymbol(const string& full_name, Symbol symbol);
bool AddFile(const FileDescriptor* file);
bool AddExtension(const FieldDescriptor* field);
// -----------------------------------------------------------------
// Allocating memory.
// Allocate an object which will be reclaimed when the pool is
// destroyed. Note that the object's destructor will never be called,
// so its fields must be plain old data (primitive data types and
// pointers). All of the descriptor types are such objects.
template<typename Type> Type* Allocate();
// Allocate an array of objects which will be reclaimed when the
// pool in destroyed. Again, destructors are never called.
template<typename Type> Type* AllocateArray(int count);
// Allocate a string which will be destroyed when the pool is destroyed.
// The string is initialized to the given value for convenience.
string* AllocateString(const string& value);
// Allocate a protocol message object. Some older versions of GCC have
// trouble understanding explicit template instantiations in some cases, so
// in those cases we have to pass a dummy pointer of the right type as the
// parameter instead of specifying the type explicitly.
template<typename Type> Type* AllocateMessage(Type* dummy = NULL);
// Allocate a FileDescriptorTables object.
FileDescriptorTables* AllocateFileTables();
private:
vector<string*> strings_; // All strings in the pool.
vector<Message*> messages_; // All messages in the pool.
vector<FileDescriptorTables*> file_tables_; // All file tables in the pool.
vector<void*> allocations_; // All other memory allocated in the pool.
SymbolsByNameMap symbols_by_name_;
FilesByNameMap files_by_name_;
ExtensionsGroupedByDescriptorMap extensions_;
int strings_before_checkpoint_;
int messages_before_checkpoint_;
int file_tables_before_checkpoint_;
int allocations_before_checkpoint_;
vector<const char* > symbols_after_checkpoint_;
vector<const char* > files_after_checkpoint_;
vector<DescriptorIntPair> extensions_after_checkpoint_;
// Allocate some bytes which will be reclaimed when the pool is
// destroyed.
void* AllocateBytes(int size);
};
// Contains tables specific to a particular file. These tables are not
// modified once the file has been constructed, so they need not be
// protected by a mutex. This makes operations that depend only on the
// contents of a single file -- e.g. Descriptor::FindFieldByName() --
// lock-free.
//
// For historical reasons, the definitions of the methods of
// FileDescriptorTables and DescriptorPool::Tables are interleaved below.
// These used to be a single class.
class FileDescriptorTables {
public:
FileDescriptorTables();
~FileDescriptorTables();
// Empty table, used with placeholder files.
static const FileDescriptorTables kEmpty;
// -----------------------------------------------------------------
// Finding items.
// Find symbols. These return a null Symbol (symbol.IsNull() is true)
// if not found.
inline Symbol FindNestedSymbol(const void* parent,
const string& name) const;
inline Symbol FindNestedSymbolOfType(const void* parent,
const string& name,
const Symbol::Type type) const;
// These return NULL if not found.
inline const FieldDescriptor* FindFieldByNumber(
const Descriptor* parent, int number) const;
inline const FieldDescriptor* FindFieldByLowercaseName(
const void* parent, const string& lowercase_name) const;
inline const FieldDescriptor* FindFieldByCamelcaseName(
const void* parent, const string& camelcase_name) const;
inline const EnumValueDescriptor* FindEnumValueByNumber(
const EnumDescriptor* parent, int number) const;
// -----------------------------------------------------------------
// Adding items.
// These add items to the corresponding tables. They return false if
// the key already exists in the table. For AddAliasUnderParent(), the
// string passed in must be one that was constructed using AllocateString(),
// as it will be used as a key in the symbols_by_parent_ map without copying.
bool AddAliasUnderParent(const void* parent, const string& name,
Symbol symbol);
bool AddFieldByNumber(const FieldDescriptor* field);
bool AddEnumValueByNumber(const EnumValueDescriptor* value);
// Adds the field to the lowercase_name and camelcase_name maps. Never
// fails because we allow duplicates; the first field by the name wins.
void AddFieldByStylizedNames(const FieldDescriptor* field);
private:
SymbolsByParentMap symbols_by_parent_;
FieldsByNameMap fields_by_lowercase_name_;
FieldsByNameMap fields_by_camelcase_name_;
FieldsByNumberMap fields_by_number_; // Not including extensions.
EnumValuesByNumberMap enum_values_by_number_;
};
DescriptorPool::Tables::Tables()
: strings_before_checkpoint_(0),
messages_before_checkpoint_(0),
allocations_before_checkpoint_(0) {}
DescriptorPool::Tables::~Tables() {
// Note that the deletion order is important, since the destructors of some
// messages may refer to objects in allocations_.
STLDeleteElements(&messages_);
for (int i = 0; i < allocations_.size(); i++) {
operator delete(allocations_[i]);
}
STLDeleteElements(&strings_);
STLDeleteElements(&file_tables_);
}
FileDescriptorTables::FileDescriptorTables() {}
FileDescriptorTables::~FileDescriptorTables() {}
const FileDescriptorTables FileDescriptorTables::kEmpty;
void DescriptorPool::Tables::Checkpoint() {
strings_before_checkpoint_ = strings_.size();
messages_before_checkpoint_ = messages_.size();
file_tables_before_checkpoint_ = file_tables_.size();
allocations_before_checkpoint_ = allocations_.size();
symbols_after_checkpoint_.clear();
files_after_checkpoint_.clear();
extensions_after_checkpoint_.clear();
}
void DescriptorPool::Tables::Rollback() {
for (int i = 0; i < symbols_after_checkpoint_.size(); i++) {
symbols_by_name_.erase(symbols_after_checkpoint_[i]);
}
for (int i = 0; i < files_after_checkpoint_.size(); i++) {
files_by_name_.erase(files_after_checkpoint_[i]);
}
for (int i = 0; i < extensions_after_checkpoint_.size(); i++) {
extensions_.erase(extensions_after_checkpoint_[i]);
}
symbols_after_checkpoint_.clear();
files_after_checkpoint_.clear();
extensions_after_checkpoint_.clear();
STLDeleteContainerPointers(
strings_.begin() + strings_before_checkpoint_, strings_.end());
STLDeleteContainerPointers(
messages_.begin() + messages_before_checkpoint_, messages_.end());
STLDeleteContainerPointers(
file_tables_.begin() + file_tables_before_checkpoint_, file_tables_.end());
for (int i = allocations_before_checkpoint_; i < allocations_.size(); i++) {
operator delete(allocations_[i]);
}
strings_.resize(strings_before_checkpoint_);
messages_.resize(messages_before_checkpoint_);
file_tables_.resize(file_tables_before_checkpoint_);
allocations_.resize(allocations_before_checkpoint_);
}
// -------------------------------------------------------------------
inline Symbol DescriptorPool::Tables::FindSymbol(const string& key) const {
const Symbol* result = FindOrNull(symbols_by_name_, key.c_str());
if (result == NULL) {
return kNullSymbol;
} else {
return *result;
}
}
inline Symbol FileDescriptorTables::FindNestedSymbol(
const void* parent, const string& name) const {
const Symbol* result =
FindOrNull(symbols_by_parent_, PointerStringPair(parent, name.c_str()));
if (result == NULL) {
return kNullSymbol;
} else {
return *result;
}
}
inline Symbol FileDescriptorTables::FindNestedSymbolOfType(
const void* parent, const string& name, const Symbol::Type type) const {
Symbol result = FindNestedSymbol(parent, name);
if (result.type != type) return kNullSymbol;
return result;
}
Symbol DescriptorPool::Tables::FindByNameHelper(
const DescriptorPool* pool, const string& name) const {
MutexLockMaybe lock(pool->mutex_);
Symbol result = FindSymbol(name);
if (result.IsNull() && pool->underlay_ != NULL) {
// Symbol not found; check the underlay.
result =
pool->underlay_->tables_->FindByNameHelper(pool->underlay_, name);
}
if (result.IsNull()) {
// Symbol still not found, so check fallback database.
if (pool->TryFindSymbolInFallbackDatabase(name)) {
result = FindSymbol(name);
}
}
return result;
}
inline const FileDescriptor* DescriptorPool::Tables::FindFile(
const string& key) const {
return FindPtrOrNull(files_by_name_, key.c_str());
}
inline const FieldDescriptor* FileDescriptorTables::FindFieldByNumber(
const Descriptor* parent, int number) const {
return FindPtrOrNull(fields_by_number_, make_pair(parent, number));
}
inline const FieldDescriptor* FileDescriptorTables::FindFieldByLowercaseName(
const void* parent, const string& lowercase_name) const {
return FindPtrOrNull(fields_by_lowercase_name_,
PointerStringPair(parent, lowercase_name.c_str()));
}
inline const FieldDescriptor* FileDescriptorTables::FindFieldByCamelcaseName(
const void* parent, const string& camelcase_name) const {
return FindPtrOrNull(fields_by_camelcase_name_,
PointerStringPair(parent, camelcase_name.c_str()));
}
inline const EnumValueDescriptor* FileDescriptorTables::FindEnumValueByNumber(
const EnumDescriptor* parent, int number) const {
return FindPtrOrNull(enum_values_by_number_, make_pair(parent, number));
}
inline const FieldDescriptor* DescriptorPool::Tables::FindExtension(
const Descriptor* extendee, int number) {
return FindPtrOrNull(extensions_, make_pair(extendee, number));
}
inline void DescriptorPool::Tables::FindAllExtensions(
const Descriptor* extendee, vector<const FieldDescriptor*>* out) const {
ExtensionsGroupedByDescriptorMap::const_iterator it =
extensions_.lower_bound(make_pair(extendee, 0));
for (; it != extensions_.end() && it->first.first == extendee; ++it) {
out->push_back(it->second);
}
}
// -------------------------------------------------------------------
bool DescriptorPool::Tables::AddSymbol(
const string& full_name, Symbol symbol) {
if (InsertIfNotPresent(&symbols_by_name_, full_name.c_str(), symbol)) {
symbols_after_checkpoint_.push_back(full_name.c_str());
return true;
} else {
return false;
}
}
bool FileDescriptorTables::AddAliasUnderParent(
const void* parent, const string& name, Symbol symbol) {
PointerStringPair by_parent_key(parent, name.c_str());
return InsertIfNotPresent(&symbols_by_parent_, by_parent_key, symbol);
}
bool DescriptorPool::Tables::AddFile(const FileDescriptor* file) {
if (InsertIfNotPresent(&files_by_name_, file->name().c_str(), file)) {
files_after_checkpoint_.push_back(file->name().c_str());
return true;
} else {
return false;
}
}
void FileDescriptorTables::AddFieldByStylizedNames(
const FieldDescriptor* field) {
const void* parent;
if (field->is_extension()) {
if (field->extension_scope() == NULL) {
parent = field->file();
} else {
parent = field->extension_scope();
}
} else {
parent = field->containing_type();
}
PointerStringPair lowercase_key(parent, field->lowercase_name().c_str());
InsertIfNotPresent(&fields_by_lowercase_name_, lowercase_key, field);
PointerStringPair camelcase_key(parent, field->camelcase_name().c_str());
InsertIfNotPresent(&fields_by_camelcase_name_, camelcase_key, field);
}
bool FileDescriptorTables::AddFieldByNumber(const FieldDescriptor* field) {
DescriptorIntPair key(field->containing_type(), field->number());
return InsertIfNotPresent(&fields_by_number_, key, field);
}
bool FileDescriptorTables::AddEnumValueByNumber(
const EnumValueDescriptor* value) {
EnumIntPair key(value->type(), value->number());
return InsertIfNotPresent(&enum_values_by_number_, key, value);
}
bool DescriptorPool::Tables::AddExtension(const FieldDescriptor* field) {
DescriptorIntPair key(field->containing_type(), field->number());
if (InsertIfNotPresent(&extensions_, key, field)) {
extensions_after_checkpoint_.push_back(key);
return true;
} else {
return false;
}
}
// -------------------------------------------------------------------
template<typename Type>
Type* DescriptorPool::Tables::Allocate() {
return reinterpret_cast<Type*>(AllocateBytes(sizeof(Type)));
}
template<typename Type>
Type* DescriptorPool::Tables::AllocateArray(int count) {
return reinterpret_cast<Type*>(AllocateBytes(sizeof(Type) * count));
}
string* DescriptorPool::Tables::AllocateString(const string& value) {
string* result = new string(value);
strings_.push_back(result);
return result;
}
template<typename Type>
Type* DescriptorPool::Tables::AllocateMessage(Type* dummy) {
Type* result = new Type;
messages_.push_back(result);
return result;
}
FileDescriptorTables* DescriptorPool::Tables::AllocateFileTables() {
FileDescriptorTables* result = new FileDescriptorTables;
file_tables_.push_back(result);
return result;
}
void* DescriptorPool::Tables::AllocateBytes(int size) {
// TODO(kenton): Would it be worthwhile to implement this in some more
// sophisticated way? Probably not for the open source release, but for
// internal use we could easily plug in one of our existing memory pool
// allocators...
if (size == 0) return NULL;
void* result = operator new(size);
allocations_.push_back(result);
return result;
}
// ===================================================================
// DescriptorPool
DescriptorPool::ErrorCollector::~ErrorCollector() {}
DescriptorPool::DescriptorPool()
: mutex_(NULL),
fallback_database_(NULL),
default_error_collector_(NULL),
underlay_(NULL),
tables_(new Tables),
enforce_dependencies_(true),
allow_unknown_(false) {}
DescriptorPool::DescriptorPool(DescriptorDatabase* fallback_database,
ErrorCollector* error_collector)
: mutex_(new Mutex),
fallback_database_(fallback_database),
default_error_collector_(error_collector),
underlay_(NULL),
tables_(new Tables),
enforce_dependencies_(true),
allow_unknown_(false) {
}
DescriptorPool::DescriptorPool(const DescriptorPool* underlay)
: mutex_(NULL),
fallback_database_(NULL),
default_error_collector_(NULL),
underlay_(underlay),
tables_(new Tables),
enforce_dependencies_(true),
allow_unknown_(false) {}
DescriptorPool::~DescriptorPool() {
if (mutex_ != NULL) delete mutex_;
}
// DescriptorPool::BuildFile() defined later.
// DescriptorPool::BuildFileCollectingErrors() defined later.
void DescriptorPool::InternalDontEnforceDependencies() {
enforce_dependencies_ = false;
}
bool DescriptorPool::InternalIsFileLoaded(const string& filename) const {
MutexLockMaybe lock(mutex_);
return tables_->FindFile(filename) != NULL;
}
// generated_pool ====================================================
namespace {
EncodedDescriptorDatabase* generated_database_ = NULL;
DescriptorPool* generated_pool_ = NULL;
void InitGeneratedPool() {
GOOGLE_CHECK(generated_pool_ == NULL);
generated_database_ = new EncodedDescriptorDatabase;
generated_pool_ = new DescriptorPool(generated_database_);
}
// Force InitGeneratedPool to be called at static init time, before any threads
// can be created.
struct Initializer {
Initializer() {
if (generated_pool_ == NULL) InitGeneratedPool();
}
} initializer;
} // anonymous namespace
const DescriptorPool* DescriptorPool::generated_pool() {
if (generated_pool_ == NULL) InitGeneratedPool();
return generated_pool_;
}
DescriptorPool* DescriptorPool::internal_generated_pool() {
if (generated_pool_ == NULL) InitGeneratedPool();
return generated_pool_;
}
void DescriptorPool::InternalAddGeneratedFile(
const void* encoded_file_descriptor, int size) {
// So, this function is called in the process of initializing the
// descriptors for generated proto classes. Each generated .pb.cc file
// has an internal procedure called AddDescriptors() which is called at
// process startup, and that function calls this one in order to register
// the raw bytes of the FileDescriptorProto representing the file.
//
// We do not actually construct the descriptor objects right away. We just
// hang on to the bytes until they are actually needed. We actually construct
// the descriptor the first time one of the following things happens:
// * Someone calls a method like descriptor(), GetDescriptor(), or
// GetReflection() on the generated types, which requires returning the
// descriptor or an object based on it.
// * Someone looks up the descriptor in DescriptorPool::generated_pool().
//
// Once one of these happens, the DescriptorPool actually parses the
// FileDescriptorProto and generates a FileDescriptor (and all its children)
// based on it.
//
// Note that FileDescriptorProto is itself a generated protocol message.
// Therefore, when we parse one, we have to be very careful to avoid using
// any descriptor-based operations, since this might cause infinite recursion
// or deadlock.
if (generated_pool_ == NULL) InitGeneratedPool();
GOOGLE_CHECK(generated_database_->Add(encoded_file_descriptor, size));
}
// Find*By* methods ==================================================
// TODO(kenton): There's a lot of repeated code here, but I'm not sure if
// there's any good way to factor it out. Think about this some time when
// there's nothing more important to do (read: never).
const FileDescriptor* DescriptorPool::FindFileByName(const string& name) const {
MutexLockMaybe lock(mutex_);
const FileDescriptor* result = tables_->FindFile(name);
if (result != NULL) return result;
if (underlay_ != NULL) {
const FileDescriptor* result = underlay_->FindFileByName(name);
if (result != NULL) return result;
}
if (TryFindFileInFallbackDatabase(name)) {
const FileDescriptor* result = tables_->FindFile(name);
if (result != NULL) return result;
}
return NULL;
}
const FileDescriptor* DescriptorPool::FindFileContainingSymbol(
const string& symbol_name) const {
MutexLockMaybe lock(mutex_);
Symbol result = tables_->FindSymbol(symbol_name);
if (!result.IsNull()) return result.GetFile();
if (underlay_ != NULL) {
const FileDescriptor* result =
underlay_->FindFileContainingSymbol(symbol_name);
if (result != NULL) return result;
}
if (TryFindSymbolInFallbackDatabase(symbol_name)) {
Symbol result = tables_->FindSymbol(symbol_name);
if (!result.IsNull()) return result.GetFile();
}
return NULL;
}
const Descriptor* DescriptorPool::FindMessageTypeByName(
const string& name) const {
Symbol result = tables_->FindByNameHelper(this, name);
return (result.type == Symbol::MESSAGE) ? result.descriptor : NULL;
}
const FieldDescriptor* DescriptorPool::FindFieldByName(
const string& name) const {
Symbol result = tables_->FindByNameHelper(this, name);
if (result.type == Symbol::FIELD &&
!result.field_descriptor->is_extension()) {
return result.field_descriptor;
} else {
return NULL;
}
}
const FieldDescriptor* DescriptorPool::FindExtensionByName(
const string& name) const {
Symbol result = tables_->FindByNameHelper(this, name);
if (result.type == Symbol::FIELD &&
result.field_descriptor->is_extension()) {
return result.field_descriptor;
} else {
return NULL;
}
}
const EnumDescriptor* DescriptorPool::FindEnumTypeByName(
const string& name) const {
Symbol result = tables_->FindByNameHelper(this, name);
return (result.type == Symbol::ENUM) ? result.enum_descriptor : NULL;
}
const EnumValueDescriptor* DescriptorPool::FindEnumValueByName(
const string& name) const {
Symbol result = tables_->FindByNameHelper(this, name);
return (result.type == Symbol::ENUM_VALUE) ?
result.enum_value_descriptor : NULL;
}
const ServiceDescriptor* DescriptorPool::FindServiceByName(
const string& name) const {
Symbol result = tables_->FindByNameHelper(this, name);
return (result.type == Symbol::SERVICE) ? result.service_descriptor : NULL;
}
const MethodDescriptor* DescriptorPool::FindMethodByName(
const string& name) const {
Symbol result = tables_->FindByNameHelper(this, name);
return (result.type == Symbol::METHOD) ? result.method_descriptor : NULL;
}
const FieldDescriptor* DescriptorPool::FindExtensionByNumber(
const Descriptor* extendee, int number) const {
MutexLockMaybe lock(mutex_);
const FieldDescriptor* result = tables_->FindExtension(extendee, number);
if (result != NULL) {
return result;
}
if (underlay_ != NULL) {
const FieldDescriptor* result =
underlay_->FindExtensionByNumber(extendee, number);
if (result != NULL) return result;
}
if (TryFindExtensionInFallbackDatabase(extendee, number)) {
const FieldDescriptor* result = tables_->FindExtension(extendee, number);
if (result != NULL) {
return result;
}
}
return NULL;
}
void DescriptorPool::FindAllExtensions(
const Descriptor* extendee, vector<const FieldDescriptor*>* out) const {
MutexLockMaybe lock(mutex_);
// Initialize tables_->extensions_ from the fallback database first
// (but do this only once per descriptor).
if (fallback_database_ != NULL &&
tables_->extensions_loaded_from_db_.count(extendee) == 0) {
vector<int> numbers;
if (fallback_database_->FindAllExtensionNumbers(extendee->full_name(),
&numbers)) {
for (int i = 0; i < numbers.size(); ++i) {
int number = numbers[i];
if (tables_->FindExtension(extendee, number) == NULL) {
TryFindExtensionInFallbackDatabase(extendee, number);
}
}
tables_->extensions_loaded_from_db_.insert(extendee);
}
}
tables_->FindAllExtensions(extendee, out);
if (underlay_ != NULL) {
underlay_->FindAllExtensions(extendee, out);
}
}
// -------------------------------------------------------------------
const FieldDescriptor*
Descriptor::FindFieldByNumber(int key) const {
const FieldDescriptor* result =
file()->tables_->FindFieldByNumber(this, key);
if (result == NULL || result->is_extension()) {
return NULL;
} else {
return result;
}
}
const FieldDescriptor*
Descriptor::FindFieldByLowercaseName(const string& key) const {
const FieldDescriptor* result =
file()->tables_->FindFieldByLowercaseName(this, key);
if (result == NULL || result->is_extension()) {
return NULL;
} else {
return result;
}
}
const FieldDescriptor*
Descriptor::FindFieldByCamelcaseName(const string& key) const {
const FieldDescriptor* result =
file()->tables_->FindFieldByCamelcaseName(this, key);
if (result == NULL || result->is_extension()) {
return NULL;
} else {
return result;
}
}
const FieldDescriptor*
Descriptor::FindFieldByName(const string& key) const {
Symbol result =
file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD);
if (!result.IsNull() && !result.field_descriptor->is_extension()) {
return result.field_descriptor;
} else {
return NULL;
}
}
const FieldDescriptor*
Descriptor::FindExtensionByName(const string& key) const {
Symbol result =
file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD);
if (!result.IsNull() && result.field_descriptor->is_extension()) {
return result.field_descriptor;
} else {
return NULL;
}
}
const FieldDescriptor*
Descriptor::FindExtensionByLowercaseName(const string& key) const {
const FieldDescriptor* result =
file()->tables_->FindFieldByLowercaseName(this, key);
if (result == NULL || !result->is_extension()) {
return NULL;
} else {
return result;
}
}
const FieldDescriptor*
Descriptor::FindExtensionByCamelcaseName(const string& key) const {
const FieldDescriptor* result =
file()->tables_->FindFieldByCamelcaseName(this, key);
if (result == NULL || !result->is_extension()) {
return NULL;
} else {
return result;
}
}
const Descriptor*
Descriptor::FindNestedTypeByName(const string& key) const {
Symbol result =
file()->tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE);
if (!result.IsNull()) {
return result.descriptor;
} else {
return NULL;
}
}
const EnumDescriptor*
Descriptor::FindEnumTypeByName(const string& key) const {
Symbol result =
file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM);
if (!result.IsNull()) {
return result.enum_descriptor;
} else {
return NULL;
}
}
const EnumValueDescriptor*
Descriptor::FindEnumValueByName(const string& key) const {
Symbol result =
file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE);
if (!result.IsNull()) {
return result.enum_value_descriptor;
} else {
return NULL;
}
}
const EnumValueDescriptor*
EnumDescriptor::FindValueByName(const string& key) const {
Symbol result =
file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE);
if (!result.IsNull()) {
return result.enum_value_descriptor;
} else {
return NULL;
}
}
const EnumValueDescriptor*
EnumDescriptor::FindValueByNumber(int key) const {
return file()->tables_->FindEnumValueByNumber(this, key);
}
const MethodDescriptor*
ServiceDescriptor::FindMethodByName(const string& key) const {
Symbol result =
file()->tables_->FindNestedSymbolOfType(this, key, Symbol::METHOD);
if (!result.IsNull()) {
return result.method_descriptor;
} else {
return NULL;
}
}
const Descriptor*
FileDescriptor::FindMessageTypeByName(const string& key) const {
Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE);
if (!result.IsNull()) {
return result.descriptor;
} else {
return NULL;
}
}
const EnumDescriptor*
FileDescriptor::FindEnumTypeByName(const string& key) const {
Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM);
if (!result.IsNull()) {
return result.enum_descriptor;
} else {
return NULL;
}
}
const EnumValueDescriptor*
FileDescriptor::FindEnumValueByName(const string& key) const {
Symbol result =
tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE);
if (!result.IsNull()) {
return result.enum_value_descriptor;
} else {
return NULL;
}
}
const ServiceDescriptor*
FileDescriptor::FindServiceByName(const string& key) const {
Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::SERVICE);
if (!result.IsNull()) {
return result.service_descriptor;
} else {
return NULL;
}
}
const FieldDescriptor*
FileDescriptor::FindExtensionByName(const string& key) const {
Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD);
if (!result.IsNull() && result.field_descriptor->is_extension()) {
return result.field_descriptor;
} else {
return NULL;
}
}
const FieldDescriptor*
FileDescriptor::FindExtensionByLowercaseName(const string& key) const {
const FieldDescriptor* result = tables_->FindFieldByLowercaseName(this, key);
if (result == NULL || !result->is_extension()) {
return NULL;
} else {
return result;
}
}
const FieldDescriptor*
FileDescriptor::FindExtensionByCamelcaseName(const string& key) const {
const FieldDescriptor* result = tables_->FindFieldByCamelcaseName(this, key);
if (result == NULL || !result->is_extension()) {
return NULL;
} else {
return result;
}
}
bool Descriptor::IsExtensionNumber(int number) const {
// Linear search should be fine because we don't expect a message to have
// more than a couple extension ranges.
for (int i = 0; i < extension_range_count(); i++) {
if (number >= extension_range(i)->start &&
number < extension_range(i)->end) {
return true;
}
}
return false;
}
// -------------------------------------------------------------------
bool DescriptorPool::TryFindFileInFallbackDatabase(const string& name) const {
if (fallback_database_ == NULL) return false;
if (tables_->known_bad_files_.count(name) > 0) return false;
FileDescriptorProto file_proto;
if (!fallback_database_->FindFileByName(name, &file_proto) ||
BuildFileFromDatabase(file_proto) == NULL) {
tables_->known_bad_files_.insert(name);
return false;
}
return true;
}
bool DescriptorPool::TryFindSymbolInFallbackDatabase(const string& name) const {
if (fallback_database_ == NULL) return false;
FileDescriptorProto file_proto;
if (!fallback_database_->FindFileContainingSymbol(name, &file_proto)) {
return false;
}
if (tables_->FindFile(file_proto.name()) != NULL) {
// We've already loaded this file, and it apparently doesn't contain the
// symbol we're looking for. Some DescriptorDatabases return false
// positives.
return false;
}
if (BuildFileFromDatabase(file_proto) == NULL) {
return false;
}
return true;
}
bool DescriptorPool::TryFindExtensionInFallbackDatabase(
const Descriptor* containing_type, int field_number) const {
if (fallback_database_ == NULL) return false;
FileDescriptorProto file_proto;
if (!fallback_database_->FindFileContainingExtension(
containing_type->full_name(), field_number, &file_proto)) {
return false;
}
if (tables_->FindFile(file_proto.name()) != NULL) {
// We've already loaded this file, and it apparently doesn't contain the
// extension we're looking for. Some DescriptorDatabases return false
// positives.
return false;
}
if (BuildFileFromDatabase(file_proto) == NULL) {
return false;
}
return true;
}
// ===================================================================
string FieldDescriptor::DefaultValueAsString(bool quote_string_type) const {
GOOGLE_CHECK(has_default_value()) << "No default value";
switch (cpp_type()) {
case CPPTYPE_INT32:
return SimpleItoa(default_value_int32());
break;
case CPPTYPE_INT64:
return SimpleItoa(default_value_int64());
break;
case CPPTYPE_UINT32:
return SimpleItoa(default_value_uint32());
break;
case CPPTYPE_UINT64:
return SimpleItoa(default_value_uint64());
break;
case CPPTYPE_FLOAT:
return SimpleFtoa(default_value_float());
break;
case CPPTYPE_DOUBLE:
return SimpleDtoa(default_value_double());
break;
case CPPTYPE_BOOL:
return default_value_bool() ? "true" : "false";
break;
case CPPTYPE_STRING:
if (quote_string_type) {
return "\"" + CEscape(default_value_string()) + "\"";
} else {
if (type() == TYPE_BYTES) {
return CEscape(default_value_string());
} else {
return default_value_string();
}
}
break;
case CPPTYPE_ENUM:
return default_value_enum()->name();
break;
case CPPTYPE_MESSAGE:
GOOGLE_LOG(DFATAL) << "Messages can't have default values!";
break;
}
GOOGLE_LOG(FATAL) << "Can't get here: failed to get default value as string";
return "";
}
// CopyTo methods ====================================================
void FileDescriptor::CopyTo(FileDescriptorProto* proto) const {
proto->set_name(name());
if (!package().empty()) proto->set_package(package());
for (int i = 0; i < dependency_count(); i++) {
proto->add_dependency(dependency(i)->name());
}
for (int i = 0; i < message_type_count(); i++) {
message_type(i)->CopyTo(proto->add_message_type());
}
for (int i = 0; i < enum_type_count(); i++) {
enum_type(i)->CopyTo(proto->add_enum_type());
}
for (int i = 0; i < service_count(); i++) {
service(i)->CopyTo(proto->add_service());
}
for (int i = 0; i < extension_count(); i++) {
extension(i)->CopyTo(proto->add_extension());
}
if (&options() != &FileOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options());
}
}
void Descriptor::CopyTo(DescriptorProto* proto) const {
proto->set_name(name());
for (int i = 0; i < field_count(); i++) {
field(i)->CopyTo(proto->add_field());
}
for (int i = 0; i < nested_type_count(); i++) {
nested_type(i)->CopyTo(proto->add_nested_type());
}
for (int i = 0; i < enum_type_count(); i++) {
enum_type(i)->CopyTo(proto->add_enum_type());
}
for (int i = 0; i < extension_range_count(); i++) {
DescriptorProto::ExtensionRange* range = proto->add_extension_range();
range->set_start(extension_range(i)->start);
range->set_end(extension_range(i)->end);
}
for (int i = 0; i < extension_count(); i++) {
extension(i)->CopyTo(proto->add_extension());
}
if (&options() != &MessageOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options());
}
}
void FieldDescriptor::CopyTo(FieldDescriptorProto* proto) const {
proto->set_name(name());
proto->set_number(number());
// Some compilers do not allow static_cast directly between two enum types,
// so we must cast to int first.
proto->set_label(static_cast<FieldDescriptorProto::Label>(
implicit_cast<int>(label())));
proto->set_type(static_cast<FieldDescriptorProto::Type>(
implicit_cast<int>(type())));
if (is_extension()) {
if (!containing_type()->is_unqualified_placeholder_) {
proto->set_extendee(".");
}
proto->mutable_extendee()->append(containing_type()->full_name());
}
if (cpp_type() == CPPTYPE_MESSAGE) {
if (message_type()->is_placeholder_) {
// We don't actually know if the type is a message type. It could be
// an enum.
proto->clear_type();
}
if (!message_type()->is_unqualified_placeholder_) {
proto->set_type_name(".");
}
proto->mutable_type_name()->append(message_type()->full_name());
} else if (cpp_type() == CPPTYPE_ENUM) {
if (!enum_type()->is_unqualified_placeholder_) {
proto->set_type_name(".");
}
proto->mutable_type_name()->append(enum_type()->full_name());
}
if (has_default_value()) {
proto->set_default_value(DefaultValueAsString(false));
}
if (&options() != &FieldOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options());
}
}
void EnumDescriptor::CopyTo(EnumDescriptorProto* proto) const {
proto->set_name(name());
for (int i = 0; i < value_count(); i++) {
value(i)->CopyTo(proto->add_value());
}
if (&options() != &EnumOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options());
}
}
void EnumValueDescriptor::CopyTo(EnumValueDescriptorProto* proto) const {
proto->set_name(name());
proto->set_number(number());
if (&options() != &EnumValueOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options());
}
}
void ServiceDescriptor::CopyTo(ServiceDescriptorProto* proto) const {
proto->set_name(name());
for (int i = 0; i < method_count(); i++) {
method(i)->CopyTo(proto->add_method());
}
if (&options() != &ServiceOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options());
}
}
void MethodDescriptor::CopyTo(MethodDescriptorProto* proto) const {
proto->set_name(name());
if (!input_type()->is_unqualified_placeholder_) {
proto->set_input_type(".");
}
proto->mutable_input_type()->append(input_type()->full_name());
if (!output_type()->is_unqualified_placeholder_) {
proto->set_output_type(".");
}
proto->mutable_output_type()->append(output_type()->full_name());
if (&options() != &MethodOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options());
}
}
// DebugString methods ===============================================
namespace {
// Used by each of the option formatters.
bool RetrieveOptions(const Message &options, vector<string> *option_entries) {
option_entries->clear();
const Reflection* reflection = options.GetReflection();
vector<const FieldDescriptor*> fields;
reflection->ListFields(options, &fields);
for (int i = 0; i < fields.size(); i++) {
// Doesn't make sense to have message type fields here
if (fields[i]->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
continue;
}
int count = 1;
bool repeated = false;
if (fields[i]->is_repeated()) {
count = reflection->FieldSize(options, fields[i]);
repeated = true;
}
for (int j = 0; j < count; j++) {
string fieldval;
TextFormat::PrintFieldValueToString(options, fields[i],
repeated ? count : -1, &fieldval);
option_entries->push_back(fields[i]->name() + " = " + fieldval);
}
}
return !option_entries->empty();
}
// Formats options that all appear together in brackets. Does not include
// brackets.
bool FormatBracketedOptions(const Message &options, string *output) {
vector<string> all_options;
if (RetrieveOptions(options, &all_options)) {
output->append(JoinStrings(all_options, ", "));
}
return !all_options.empty();
}
// Formats options one per line
bool FormatLineOptions(int depth, const Message &options, string *output) {
string prefix(depth * 2, ' ');
vector<string> all_options;
if (RetrieveOptions(options, &all_options)) {
for (int i = 0; i < all_options.size(); i++) {
strings::SubstituteAndAppend(output, "$0option $1;\n",
prefix, all_options[i]);
}
}
return !all_options.empty();
}
} // anonymous namespace
string FileDescriptor::DebugString() const {
string contents = "syntax = \"proto2\";\n\n";
for (int i = 0; i < dependency_count(); i++) {
strings::SubstituteAndAppend(&contents, "import \"$0\";\n",
dependency(i)->name());
}
if (!package().empty()) {
strings::SubstituteAndAppend(&contents, "package $0;\n\n", package());
}
if (FormatLineOptions(0, options(), &contents)) {
contents.append("\n"); // add some space if we had options
}
for (int i = 0; i < enum_type_count(); i++) {
enum_type(i)->DebugString(0, &contents);
contents.append("\n");
}
// Find all the 'group' type extensions; we will not output their nested
// definitions (those will be done with their group field descriptor).
set<const Descriptor*> groups;
for (int i = 0; i < extension_count(); i++) {
if (extension(i)->type() == FieldDescriptor::TYPE_GROUP) {
groups.insert(extension(i)->message_type());
}
}
for (int i = 0; i < message_type_count(); i++) {
if (groups.count(message_type(i)) == 0) {
strings::SubstituteAndAppend(&contents, "message $0",
message_type(i)->name());
message_type(i)->DebugString(0, &contents);
contents.append("\n");
}
}
for (int i = 0; i < service_count(); i++) {
service(i)->DebugString(&contents);
contents.append("\n");
}
const Descriptor* containing_type = NULL;
for (int i = 0; i < extension_count(); i++) {
if (extension(i)->containing_type() != containing_type) {
if (i > 0) contents.append("}\n\n");
containing_type = extension(i)->containing_type();
strings::SubstituteAndAppend(&contents, "extend .$0 {\n",
containing_type->full_name());
}
extension(i)->DebugString(1, &contents);
}
if (extension_count() > 0) contents.append("}\n\n");
return contents;
}
string Descriptor::DebugString() const {
string contents;
strings::SubstituteAndAppend(&contents, "message $0", name());
DebugString(0, &contents);
return contents;
}
void Descriptor::DebugString(int depth, string *contents) const {
string prefix(depth * 2, ' ');
++depth;
contents->append(" {\n");
FormatLineOptions(depth, options(), contents);
// Find all the 'group' types for fields and extensions; we will not output
// their nested definitions (those will be done with their group field
// descriptor).
set<const Descriptor*> groups;
for (int i = 0; i < field_count(); i++) {
if (field(i)->type() == FieldDescriptor::TYPE_GROUP) {
groups.insert(field(i)->message_type());
}
}
for (int i = 0; i < extension_count(); i++) {
if (extension(i)->type() == FieldDescriptor::TYPE_GROUP) {
groups.insert(extension(i)->message_type());
}
}
for (int i = 0; i < nested_type_count(); i++) {
if (groups.count(nested_type(i)) == 0) {
strings::SubstituteAndAppend(contents, "$0 message $1",
prefix, nested_type(i)->name());
nested_type(i)->DebugString(depth, contents);
}
}
for (int i = 0; i < enum_type_count(); i++) {
enum_type(i)->DebugString(depth, contents);
}
for (int i = 0; i < field_count(); i++) {
field(i)->DebugString(depth, contents);
}
for (int i = 0; i < extension_range_count(); i++) {
strings::SubstituteAndAppend(contents, "$0 extensions $1 to $2;\n",
prefix,
extension_range(i)->start,
extension_range(i)->end - 1);
}
// Group extensions by what they extend, so they can be printed out together.
const Descriptor* containing_type = NULL;
for (int i = 0; i < extension_count(); i++) {
if (extension(i)->containing_type() != containing_type) {
if (i > 0) strings::SubstituteAndAppend(contents, "$0 }\n", prefix);
containing_type = extension(i)->containing_type();
strings::SubstituteAndAppend(contents, "$0 extend .$1 {\n",
prefix, containing_type->full_name());
}
extension(i)->DebugString(depth + 1, contents);
}
if (extension_count() > 0)
strings::SubstituteAndAppend(contents, "$0 }\n", prefix);
strings::SubstituteAndAppend(contents, "$0}\n", prefix);
}
string FieldDescriptor::DebugString() const {
string contents;
int depth = 0;
if (is_extension()) {
strings::SubstituteAndAppend(&contents, "extend .$0 {\n",
containing_type()->full_name());
depth = 1;
}
DebugString(depth, &contents);
if (is_extension()) {
contents.append("}\n");
}
return contents;
}
void FieldDescriptor::DebugString(int depth, string *contents) const {
string prefix(depth * 2, ' ');
string field_type;
switch (type()) {
case TYPE_MESSAGE:
field_type = "." + message_type()->full_name();
break;
case TYPE_ENUM:
field_type = "." + enum_type()->full_name();
break;
default:
field_type = kTypeToName[type()];
}
strings::SubstituteAndAppend(contents, "$0$1 $2 $3 = $4",
prefix,
kLabelToName[label()],
field_type,
type() == TYPE_GROUP ? message_type()->name() :
name(),
number());
bool bracketed = false;
if (has_default_value()) {
bracketed = true;
strings::SubstituteAndAppend(contents, " [default = $0",
DefaultValueAsString(true));
}
string formatted_options;
if (FormatBracketedOptions(options(), &formatted_options)) {
contents->append(bracketed ? ", " : " [");
bracketed = true;
contents->append(formatted_options);
}
if (bracketed) {
contents->append("]");
}
if (type() == TYPE_GROUP) {
message_type()->DebugString(depth, contents);
} else {
contents->append(";\n");
}
}
string EnumDescriptor::DebugString() const {
string contents;
DebugString(0, &contents);
return contents;
}
void EnumDescriptor::DebugString(int depth, string *contents) const {
string prefix(depth * 2, ' ');
++depth;
strings::SubstituteAndAppend(contents, "$0enum $1 {\n",
prefix, name());
FormatLineOptions(depth, options(), contents);
for (int i = 0; i < value_count(); i++) {
value(i)->DebugString(depth, contents);
}
strings::SubstituteAndAppend(contents, "$0}\n", prefix);
}
string EnumValueDescriptor::DebugString() const {
string contents;
DebugString(0, &contents);
return contents;
}
void EnumValueDescriptor::DebugString(int depth, string *contents) const {
string prefix(depth * 2, ' ');
strings::SubstituteAndAppend(contents, "$0$1 = $2",
prefix, name(), number());
string formatted_options;
if (FormatBracketedOptions(options(), &formatted_options)) {
strings::SubstituteAndAppend(contents, " [$0]", formatted_options);
}
contents->append(";\n");
}
string ServiceDescriptor::DebugString() const {
string contents;
DebugString(&contents);
return contents;
}
void ServiceDescriptor::DebugString(string *contents) const {
strings::SubstituteAndAppend(contents, "service $0 {\n", name());
FormatLineOptions(1, options(), contents);
for (int i = 0; i < method_count(); i++) {
method(i)->DebugString(1, contents);
}
contents->append("}\n");
}
string MethodDescriptor::DebugString() const {
string contents;
DebugString(0, &contents);
return contents;
}
void MethodDescriptor::DebugString(int depth, string *contents) const {
string prefix(depth * 2, ' ');
++depth;
strings::SubstituteAndAppend(contents, "$0rpc $1(.$2) returns (.$3)",
prefix, name(),
input_type()->full_name(),
output_type()->full_name());
string formatted_options;
if (FormatLineOptions(depth, options(), &formatted_options)) {
strings::SubstituteAndAppend(contents, " {\n$0$1}\n",
formatted_options, prefix);
} else {
contents->append(";\n");
}
}
// ===================================================================
namespace {
// Represents an options message to interpret. Extension names in the option
// name are respolved relative to name_scope. element_name and orig_opt are
// used only for error reporting (since the parser records locations against
// pointers in the original options, not the mutable copy). The Message must be
// one of the Options messages in descriptor.proto.
struct OptionsToInterpret {
OptionsToInterpret(const string& ns,
const string& el,
const Message* orig_opt,
Message* opt)
: name_scope(ns),
element_name(el),
original_options(orig_opt),
options(opt) {
}
string name_scope;
string element_name;
const Message* original_options;
Message* options;
};
} // namespace
class DescriptorBuilder {
public:
DescriptorBuilder(const DescriptorPool* pool,
DescriptorPool::Tables* tables,
DescriptorPool::ErrorCollector* error_collector);
~DescriptorBuilder();
const FileDescriptor* BuildFile(const FileDescriptorProto& proto);
private:
friend class OptionInterpreter;
const DescriptorPool* pool_;
DescriptorPool::Tables* tables_; // for convenience
DescriptorPool::ErrorCollector* error_collector_;
// As we build descriptors we store copies of the options messages in
// them. We put pointers to those copies in this vector, as we build, so we
// can later (after cross-linking) interpret those options.
vector<OptionsToInterpret> options_to_interpret_;
bool had_errors_;
string filename_;
FileDescriptor* file_;
FileDescriptorTables* file_tables_;
// If LookupSymbol() finds a symbol that is in a file which is not a declared
// dependency of this file, it will fail, but will set
// possible_undeclared_dependency_ to point at that file. This is only used
// by AddNotDefinedError() to report a more useful error message.
const FileDescriptor* possible_undeclared_dependency_;
void AddError(const string& element_name,
const Message& descriptor,
DescriptorPool::ErrorCollector::ErrorLocation location,
const string& error);
// Adds an error indicating that undefined_symbol was not defined. Must
// only be called after LookupSymbol() fails.
void AddNotDefinedError(
const string& element_name,
const Message& descriptor,
DescriptorPool::ErrorCollector::ErrorLocation location,
const string& undefined_symbol);
// Silly helper which determines if the given file is in the given package.
// I.e., either file->package() == package_name or file->package() is a
// nested package within package_name.
bool IsInPackage(const FileDescriptor* file, const string& package_name);
// Like tables_->FindSymbol(), but additionally:
// - Search the pool's underlay if not found in tables_.
// - Insure that the resulting Symbol is from one of the file's declared
// dependencies.
Symbol FindSymbol(const string& name);
// Like FindSymbol() but does not require that the symbol is in one of the
// file's declared dependencies.
Symbol FindSymbolNotEnforcingDeps(const string& name);
// Like FindSymbol(), but looks up the name relative to some other symbol
// name. This first searches siblings of relative_to, then siblings of its
// parents, etc. For example, LookupSymbol("foo.bar", "baz.qux.corge") makes
// the following calls, returning the first non-null result:
// FindSymbol("baz.qux.foo.bar"), FindSymbol("baz.foo.bar"),
// FindSymbol("foo.bar"). If AllowUnknownDependencies() has been called
// on the DescriptorPool, this will generate a placeholder type if
// the name is not found (unless the name itself is malformed). The
// placeholder_type parameter indicates what kind of placeholder should be
// constructed in this case. The resolve_mode parameter determines whether
// any symbol is returned, or only symbols that are types. Note, however,
// that LookupSymbol may still return a non-type symbol in LOOKUP_TYPES mode,
// if it believes that's all it could refer to. The caller should always
// check that it receives the type of symbol it was expecting.
enum PlaceholderType {
PLACEHOLDER_MESSAGE,
PLACEHOLDER_ENUM,
PLACEHOLDER_EXTENDABLE_MESSAGE
};
enum ResolveMode {
LOOKUP_ALL, LOOKUP_TYPES
};
Symbol LookupSymbol(const string& name, const string& relative_to,
PlaceholderType placeholder_type = PLACEHOLDER_MESSAGE,
ResolveMode resolve_mode = LOOKUP_ALL);
// Like LookupSymbol() but will not return a placeholder even if
// AllowUnknownDependencies() has been used.
Symbol LookupSymbolNoPlaceholder(const string& name,
const string& relative_to,
ResolveMode resolve_mode = LOOKUP_ALL);
// Creates a placeholder type suitable for return from LookupSymbol(). May
// return kNullSymbol if the name is not a valid type name.
Symbol NewPlaceholder(const string& name, PlaceholderType placeholder_type);
// Creates a placeholder file. Never returns NULL. This is used when an
// import is not found and AllowUnknownDependencies() is enabled.
const FileDescriptor* NewPlaceholderFile(const string& name);
// Calls tables_->AddSymbol() and records an error if it fails. Returns
// true if successful or false if failed, though most callers can ignore
// the return value since an error has already been recorded.
bool AddSymbol(const string& full_name,
const void* parent, const string& name,
const Message& proto, Symbol symbol);
// Like AddSymbol(), but succeeds if the symbol is already defined as long
// as the existing definition is also a package (because it's OK to define
// the same package in two different files). Also adds all parents of the
// packgae to the symbol table (e.g. AddPackage("foo.bar", ...) will add
// "foo.bar" and "foo" to the table).
void AddPackage(const string& name, const Message& proto,
const FileDescriptor* file);
// Checks that the symbol name contains only alphanumeric characters and
// underscores. Records an error otherwise.
void ValidateSymbolName(const string& name, const string& full_name,
const Message& proto);
// Like ValidateSymbolName(), but the name is allowed to contain periods and
// an error is indicated by returning false (not recording the error).
bool ValidateQualifiedName(const string& name);
// Used by BUILD_ARRAY macro (below) to avoid having to have the type
// specified as a macro parameter.
template <typename Type>
inline void AllocateArray(int size, Type** output) {
*output = tables_->AllocateArray<Type>(size);
}
// Allocates a copy of orig_options in tables_ and stores it in the
// descriptor. Remembers its uninterpreted options, to be interpreted
// later. DescriptorT must be one of the Descriptor messages from
// descriptor.proto.
template<class DescriptorT> void AllocateOptions(
const typename DescriptorT::OptionsType& orig_options,
DescriptorT* descriptor);
// Specialization for FileOptions.
void AllocateOptions(const FileOptions& orig_options,
FileDescriptor* descriptor);
// Implementation for AllocateOptions(). Don't call this directly.
template<class DescriptorT> void AllocateOptionsImpl(
const string& name_scope,
const string& element_name,
const typename DescriptorT::OptionsType& orig_options,
DescriptorT* descriptor);
// These methods all have the same signature for the sake of the BUILD_ARRAY
// macro, below.
void BuildMessage(const DescriptorProto& proto,
const Descriptor* parent,
Descriptor* result);
void BuildFieldOrExtension(const FieldDescriptorProto& proto,
const Descriptor* parent,
FieldDescriptor* result,
bool is_extension);
void BuildField(const FieldDescriptorProto& proto,
const Descriptor* parent,
FieldDescriptor* result) {
BuildFieldOrExtension(proto, parent, result, false);
}
void BuildExtension(const FieldDescriptorProto& proto,
const Descriptor* parent,
FieldDescriptor* result) {
BuildFieldOrExtension(proto, parent, result, true);
}
void BuildExtensionRange(const DescriptorProto::ExtensionRange& proto,
const Descriptor* parent,
Descriptor::ExtensionRange* result);
void BuildEnum(const EnumDescriptorProto& proto,
const Descriptor* parent,
EnumDescriptor* result);
void BuildEnumValue(const EnumValueDescriptorProto& proto,
const EnumDescriptor* parent,
EnumValueDescriptor* result);
void BuildService(const ServiceDescriptorProto& proto,
const void* dummy,
ServiceDescriptor* result);
void BuildMethod(const MethodDescriptorProto& proto,
const ServiceDescriptor* parent,
MethodDescriptor* result);
// Must be run only after building.
//
// NOTE: Options will not be available during cross-linking, as they
// have not yet been interpreted. Defer any handling of options to the
// Validate*Options methods.
void CrossLinkFile(FileDescriptor* file, const FileDescriptorProto& proto);
void CrossLinkMessage(Descriptor* message, const DescriptorProto& proto);
void CrossLinkField(FieldDescriptor* field,
const FieldDescriptorProto& proto);
void CrossLinkEnum(EnumDescriptor* enum_type,
const EnumDescriptorProto& proto);
void CrossLinkEnumValue(EnumValueDescriptor* enum_value,
const EnumValueDescriptorProto& proto);
void CrossLinkService(ServiceDescriptor* service,
const ServiceDescriptorProto& proto);
void CrossLinkMethod(MethodDescriptor* method,
const MethodDescriptorProto& proto);
// Must be run only after cross-linking.
void InterpretOptions();
// A helper class for interpreting options.
class OptionInterpreter {
public:
// Creates an interpreter that operates in the context of the pool of the
// specified builder, which must not be NULL. We don't take ownership of the
// builder.
explicit OptionInterpreter(DescriptorBuilder* builder);
~OptionInterpreter();
// Interprets the uninterpreted options in the specified Options message.
// On error, calls AddError() on the underlying builder and returns false.
// Otherwise returns true.
bool InterpretOptions(OptionsToInterpret* options_to_interpret);
private:
// Interprets uninterpreted_option_ on the specified message, which
// must be the mutable copy of the original options message to which
// uninterpreted_option_ belongs.
bool InterpretSingleOption(Message* options);
// Adds the uninterpreted_option to the given options message verbatim.
// Used when AllowUnknownDependencies() is in effect and we can't find
// the option's definition.
void AddWithoutInterpreting(const UninterpretedOption& uninterpreted_option,
Message* options);
// A recursive helper function that drills into the intermediate fields
// in unknown_fields to check if field #field_number is set on the
// innermost message. Returns false and sets an error if so.
bool ExamineIfOptionIsSet(
vector<const FieldDescriptor*>::const_iterator intermediate_fields_iter,
vector<const FieldDescriptor*>::const_iterator intermediate_fields_end,
const FieldDescriptor* innermost_field, const string& debug_msg_name,
const UnknownFieldSet& unknown_fields);
// Validates the value for the option field of the currently interpreted
// option and then sets it on the unknown_field.
bool SetOptionValue(const FieldDescriptor* option_field,
UnknownFieldSet* unknown_fields);
// Convenience functions to set an int field the right way, depending on
// its wire type (a single int CppType can represent multiple wire types).
void SetInt32(int number, int32 value, FieldDescriptor::Type type,
UnknownFieldSet* unknown_fields);
void SetInt64(int number, int64 value, FieldDescriptor::Type type,
UnknownFieldSet* unknown_fields);
void SetUInt32(int number, uint32 value, FieldDescriptor::Type type,
UnknownFieldSet* unknown_fields);
void SetUInt64(int number, uint64 value, FieldDescriptor::Type type,
UnknownFieldSet* unknown_fields);
// A helper function that adds an error at the specified location of the
// option we're currently interpreting, and returns false.
bool AddOptionError(DescriptorPool::ErrorCollector::ErrorLocation location,
const string& msg) {
builder_->AddError(options_to_interpret_->element_name,
*uninterpreted_option_, location, msg);
return false;
}
// A helper function that adds an error at the location of the option name
// and returns false.
bool AddNameError(const string& msg) {
return AddOptionError(DescriptorPool::ErrorCollector::OPTION_NAME, msg);
}
// A helper function that adds an error at the location of the option name
// and returns false.
bool AddValueError(const string& msg) {
return AddOptionError(DescriptorPool::ErrorCollector::OPTION_VALUE, msg);
}
// We interpret against this builder's pool. Is never NULL. We don't own
// this pointer.
DescriptorBuilder* builder_;
// The options we're currently interpreting, or NULL if we're not in a call
// to InterpretOptions.
const OptionsToInterpret* options_to_interpret_;
// The option we're currently interpreting within options_to_interpret_, or
// NULL if we're not in a call to InterpretOptions(). This points to a
// submessage of the original option, not the mutable copy. Therefore we
// can use it to find locations recorded by the parser.
const UninterpretedOption* uninterpreted_option_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(OptionInterpreter);
};
// Work-around for broken compilers: According to the C++ standard,
// OptionInterpreter should have access to the private members of any class
// which has declared DescriptorBuilder as a friend. Unfortunately some old
// versions of GCC and other compilers do not implement this correctly. So,
// we have to have these intermediate methods to provide access. We also
// redundantly declare OptionInterpreter a friend just to make things extra
// clear for these bad compilers.
friend class OptionInterpreter;
static inline bool get_allow_unknown(const DescriptorPool* pool) {
return pool->allow_unknown_;
}
static inline bool get_is_placeholder(const Descriptor* descriptor) {
return descriptor->is_placeholder_;
}
// Must be run only after options have been interpreted.
//
// NOTE: Validation code must only reference the options in the mutable
// descriptors, which are the ones that have been interpreted. The const
// proto references are passed in only so they can be provided to calls to
// AddError(). Do not look at their options, which have not been interpreted.
void ValidateFileOptions(FileDescriptor* file,
const FileDescriptorProto& proto);
void ValidateMessageOptions(Descriptor* message,
const DescriptorProto& proto);
void ValidateFieldOptions(FieldDescriptor* field,
const FieldDescriptorProto& proto);
void ValidateEnumOptions(EnumDescriptor* enm,
const EnumDescriptorProto& proto);
void ValidateEnumValueOptions(EnumValueDescriptor* enum_value,
const EnumValueDescriptorProto& proto);
void ValidateServiceOptions(ServiceDescriptor* service,
const ServiceDescriptorProto& proto);
void ValidateMethodOptions(MethodDescriptor* method,
const MethodDescriptorProto& proto);
void ValidateMapKey(FieldDescriptor* field,
const FieldDescriptorProto& proto);
};
const FileDescriptor* DescriptorPool::BuildFile(
const FileDescriptorProto& proto) {
GOOGLE_CHECK(fallback_database_ == NULL)
<< "Cannot call BuildFile on a DescriptorPool that uses a "
"DescriptorDatabase. You must instead find a way to get your file "
"into the underlying database.";
GOOGLE_CHECK(mutex_ == NULL); // Implied by the above GOOGLE_CHECK.
return DescriptorBuilder(this, tables_.get(), NULL).BuildFile(proto);
}
const FileDescriptor* DescriptorPool::BuildFileCollectingErrors(
const FileDescriptorProto& proto,
ErrorCollector* error_collector) {
GOOGLE_CHECK(fallback_database_ == NULL)
<< "Cannot call BuildFile on a DescriptorPool that uses a "
"DescriptorDatabase. You must instead find a way to get your file "
"into the underlying database.";
GOOGLE_CHECK(mutex_ == NULL); // Implied by the above GOOGLE_CHECK.
return DescriptorBuilder(this, tables_.get(),
error_collector).BuildFile(proto);
}
const FileDescriptor* DescriptorPool::BuildFileFromDatabase(
const FileDescriptorProto& proto) const {
mutex_->AssertHeld();
return DescriptorBuilder(this, tables_.get(),
default_error_collector_).BuildFile(proto);
}
DescriptorBuilder::DescriptorBuilder(
const DescriptorPool* pool,
DescriptorPool::Tables* tables,
DescriptorPool::ErrorCollector* error_collector)
: pool_(pool),
tables_(tables),
error_collector_(error_collector),
had_errors_(false),
possible_undeclared_dependency_(NULL) {}
DescriptorBuilder::~DescriptorBuilder() {}
void DescriptorBuilder::AddError(
const string& element_name,
const Message& descriptor,
DescriptorPool::ErrorCollector::ErrorLocation location,
const string& error) {
if (error_collector_ == NULL) {
if (!had_errors_) {
GOOGLE_LOG(ERROR) << "Invalid proto descriptor for file \"" << filename_
<< "\":";
}
GOOGLE_LOG(ERROR) << " " << element_name << ": " << error;
} else {
error_collector_->AddError(filename_, element_name,
&descriptor, location, error);
}
had_errors_ = true;
}
void DescriptorBuilder::AddNotDefinedError(
const string& element_name,
const Message& descriptor,
DescriptorPool::ErrorCollector::ErrorLocation location,
const string& undefined_symbol) {
if (possible_undeclared_dependency_ == NULL) {
AddError(element_name, descriptor, location,
"\"" + undefined_symbol + "\" is not defined.");
} else {
AddError(element_name, descriptor, location,
"\"" + undefined_symbol + "\" seems to be defined in \""
+ possible_undeclared_dependency_->name() + "\", which is not "
"imported by \"" + filename_ + "\". To use it here, please "
"add the necessary import.");
}
}
bool DescriptorBuilder::IsInPackage(const FileDescriptor* file,
const string& package_name) {
return HasPrefixString(file->package(), package_name) &&
(file->package().size() == package_name.size() ||
file->package()[package_name.size()] == '.');
}
Symbol DescriptorBuilder::FindSymbolNotEnforcingDeps(const string& name) {
Symbol result;
// We need to search our pool and all its underlays.
const DescriptorPool* pool = pool_;
while (true) {
// If we are looking at an underlay, we must lock its mutex_, since we are
// accessing the underlay's tables_ dircetly.
MutexLockMaybe lock((pool == pool_) ? NULL : pool->mutex_);
// Note that we don't have to check fallback_database_ here because the
// symbol has to be in one of its file's direct dependencies, and we have
// already loaded those by the time we get here.
result = pool->tables_->FindSymbol(name);
if (!result.IsNull()) break;
if (pool->underlay_ == NULL) return kNullSymbol;
pool = pool->underlay_;
}
return result;
}
Symbol DescriptorBuilder::FindSymbol(const string& name) {
Symbol result = FindSymbolNotEnforcingDeps(name);
if (!pool_->enforce_dependencies_) {
// Hack for CompilerUpgrader.
return result;
}
// Only find symbols which were defined in this file or one of its
// dependencies.
const FileDescriptor* file = result.GetFile();
if (file == file_) return result;
for (int i = 0; i < file_->dependency_count(); i++) {
if (file == file_->dependency(i)) return result;
}
if (result.type == Symbol::PACKAGE) {
// Arg, this is overcomplicated. The symbol is a package name. It could
// be that the package was defined in multiple files. result.GetFile()
// returns the first file we saw that used this package. We've determined
// that that file is not a direct dependency of the file we are currently
// building, but it could be that some other file which *is* a direct
// dependency also defines the same package. We can't really rule out this
// symbol unless none of the dependencies define it.
if (IsInPackage(file_, name)) return result;
for (int i = 0; i < file_->dependency_count(); i++) {
if (IsInPackage(file_->dependency(i), name)) return result;
}
}
possible_undeclared_dependency_ = file;
return kNullSymbol;
}
Symbol DescriptorBuilder::LookupSymbolNoPlaceholder(
const string& name, const string& relative_to, ResolveMode resolve_mode) {
possible_undeclared_dependency_ = NULL;
if (name.size() > 0 && name[0] == '.') {
// Fully-qualified name.
return FindSymbol(name.substr(1));
}
// If name is something like "Foo.Bar.baz", and symbols named "Foo" are
// defined in multiple parent scopes, we only want to find "Bar.baz" in the
// innermost one. E.g., the following should produce an error:
// message Bar { message Baz {} }
// message Foo {
// message Bar {
// }
// optional Bar.Baz baz = 1;
// }
// So, we look for just "Foo" first, then look for "Bar.baz" within it if
// found.
int name_dot_pos = name.find_first_of('.');
string first_part_of_name;
if (name_dot_pos == string::npos) {
first_part_of_name = name;
} else {
first_part_of_name = name.substr(0, name_dot_pos);
}
string scope_to_try(relative_to);
while (true) {
// Chop off the last component of the scope.
string::size_type dot_pos = scope_to_try.find_last_of('.');
if (dot_pos == string::npos) {
return FindSymbol(name);
} else {
scope_to_try.erase(dot_pos);
}
// Append ".first_part_of_name" and try to find.
string::size_type old_size = scope_to_try.size();
scope_to_try.append(1, '.');
scope_to_try.append(first_part_of_name);
Symbol result = FindSymbol(scope_to_try);
if (!result.IsNull()) {
if (first_part_of_name.size() < name.size()) {
// name is a compound symbol, of which we only found the first part.
// Now try to look up the rest of it.
if (result.IsAggregate()) {
scope_to_try.append(name, first_part_of_name.size(),
name.size() - first_part_of_name.size());
return FindSymbol(scope_to_try);
} else {
// We found a symbol but it's not an aggregate. Continue the loop.
}
} else {
if (resolve_mode == LOOKUP_TYPES && !result.IsType()) {
// We found a symbol but it's not a type. Continue the loop.
} else {
return result;
}
}
}
// Not found. Remove the name so we can try again.
scope_to_try.erase(old_size);
}
}
Symbol DescriptorBuilder::LookupSymbol(
const string& name, const string& relative_to,
PlaceholderType placeholder_type, ResolveMode resolve_mode) {
Symbol result = LookupSymbolNoPlaceholder(
name, relative_to, resolve_mode);
if (result.IsNull() && pool_->allow_unknown_) {
// Not found, but AllowUnknownDependencies() is enabled. Return a
// placeholder instead.
result = NewPlaceholder(name, placeholder_type);
}
return result;
}
Symbol DescriptorBuilder::NewPlaceholder(const string& name,
PlaceholderType placeholder_type) {
// Compute names.
const string* placeholder_full_name;
const string* placeholder_name;
const string* placeholder_package;
if (!ValidateQualifiedName(name)) return kNullSymbol;
if (name[0] == '.') {
// Fully-qualified.
placeholder_full_name = tables_->AllocateString(name.substr(1));
} else {
placeholder_full_name = tables_->AllocateString(name);
}
string::size_type dotpos = placeholder_full_name->find_last_of('.');
if (dotpos != string::npos) {
placeholder_package = tables_->AllocateString(
placeholder_full_name->substr(0, dotpos));
placeholder_name = tables_->AllocateString(
placeholder_full_name->substr(dotpos + 1));
} else {
placeholder_package = &kEmptyString;
placeholder_name = placeholder_full_name;
}
// Create the placeholders.
FileDescriptor* placeholder_file = tables_->Allocate<FileDescriptor>();
memset(placeholder_file, 0, sizeof(*placeholder_file));
placeholder_file->name_ =
tables_->AllocateString(*placeholder_full_name + ".placeholder.proto");
placeholder_file->package_ = placeholder_package;
placeholder_file->pool_ = pool_;
placeholder_file->options_ = &FileOptions::default_instance();
placeholder_file->tables_ = &FileDescriptorTables::kEmpty;
// All other fields are zero or NULL.
if (placeholder_type == PLACEHOLDER_ENUM) {
placeholder_file->enum_type_count_ = 1;
placeholder_file->enum_types_ =
tables_->AllocateArray<EnumDescriptor>(1);
EnumDescriptor* placeholder_enum = &placeholder_file->enum_types_[0];
memset(placeholder_enum, 0, sizeof(*placeholder_enum));
placeholder_enum->full_name_ = placeholder_full_name;
placeholder_enum->name_ = placeholder_name;
placeholder_enum->file_ = placeholder_file;
placeholder_enum->options_ = &EnumOptions::default_instance();
placeholder_enum->is_placeholder_ = true;
placeholder_enum->is_unqualified_placeholder_ = (name[0] != '.');
// Enums must have at least one value.
placeholder_enum->value_count_ = 1;
placeholder_enum->values_ = tables_->AllocateArray<EnumValueDescriptor>(1);
EnumValueDescriptor* placeholder_value = &placeholder_enum->values_[0];
memset(placeholder_value, 0, sizeof(*placeholder_value));
placeholder_value->name_ = tables_->AllocateString("PLACEHOLDER_VALUE");
// Note that enum value names are siblings of their type, not children.
placeholder_value->full_name_ =
placeholder_package->empty() ? placeholder_value->name_ :
tables_->AllocateString(*placeholder_package + ".PLACEHOLDER_VALUE");
placeholder_value->number_ = 0;
placeholder_value->type_ = placeholder_enum;
placeholder_value->options_ = &EnumValueOptions::default_instance();
return Symbol(placeholder_enum);
} else {
placeholder_file->message_type_count_ = 1;
placeholder_file->message_types_ =
tables_->AllocateArray<Descriptor>(1);
Descriptor* placeholder_message = &placeholder_file->message_types_[0];
memset(placeholder_message, 0, sizeof(*placeholder_message));
placeholder_message->full_name_ = placeholder_full_name;
placeholder_message->name_ = placeholder_name;
placeholder_message->file_ = placeholder_file;
placeholder_message->options_ = &MessageOptions::default_instance();
placeholder_message->is_placeholder_ = true;
placeholder_message->is_unqualified_placeholder_ = (name[0] != '.');
if (placeholder_type == PLACEHOLDER_EXTENDABLE_MESSAGE) {
placeholder_message->extension_range_count_ = 1;
placeholder_message->extension_ranges_ =
tables_->AllocateArray<Descriptor::ExtensionRange>(1);
placeholder_message->extension_ranges_->start = 1;
// kMaxNumber + 1 because ExtensionRange::end is exclusive.
placeholder_message->extension_ranges_->end =
FieldDescriptor::kMaxNumber + 1;
}
return Symbol(placeholder_message);
}
}
const FileDescriptor* DescriptorBuilder::NewPlaceholderFile(
const string& name) {
FileDescriptor* placeholder = tables_->Allocate<FileDescriptor>();
memset(placeholder, 0, sizeof(*placeholder));
placeholder->name_ = tables_->AllocateString(name);
placeholder->package_ = &kEmptyString;
placeholder->pool_ = pool_;
placeholder->options_ = &FileOptions::default_instance();
placeholder->tables_ = &FileDescriptorTables::kEmpty;
// All other fields are zero or NULL.
return placeholder;
}
bool DescriptorBuilder::AddSymbol(
const string& full_name, const void* parent, const string& name,
const Message& proto, Symbol symbol) {
// If the caller passed NULL for the parent, the symbol is at file scope.
// Use its file as the parent instead.
if (parent == NULL) parent = file_;
if (tables_->AddSymbol(full_name, symbol)) {
if (!file_tables_->AddAliasUnderParent(parent, name, symbol)) {
GOOGLE_LOG(DFATAL) << "\"" << full_name << "\" not previously defined in "
"symbols_by_name_, but was defined in symbols_by_parent_; "
"this shouldn't be possible.";
return false;
}
return true;
} else {
const FileDescriptor* other_file = tables_->FindSymbol(full_name).GetFile();
if (other_file == file_) {
string::size_type dot_pos = full_name.find_last_of('.');
if (dot_pos == string::npos) {
AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME,
"\"" + full_name + "\" is already defined.");
} else {
AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME,
"\"" + full_name.substr(dot_pos + 1) +
"\" is already defined in \"" +
full_name.substr(0, dot_pos) + "\".");
}
} else {
// Symbol seems to have been defined in a different file.
AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME,
"\"" + full_name + "\" is already defined in file \"" +
other_file->name() + "\".");
}
return false;
}
}
void DescriptorBuilder::AddPackage(
const string& name, const Message& proto, const FileDescriptor* file) {
if (tables_->AddSymbol(name, Symbol(file))) {
// Success. Also add parent package, if any.
string::size_type dot_pos = name.find_last_of('.');
if (dot_pos == string::npos) {
// No parents.
ValidateSymbolName(name, name, proto);
} else {
// Has parent.
string* parent_name = tables_->AllocateString(name.substr(0, dot_pos));
AddPackage(*parent_name, proto, file);
ValidateSymbolName(name.substr(dot_pos + 1), name, proto);
}
} else {
Symbol existing_symbol = tables_->FindSymbol(name);
// It's OK to redefine a package.
if (existing_symbol.type != Symbol::PACKAGE) {
// Symbol seems to have been defined in a different file.
AddError(name, proto, DescriptorPool::ErrorCollector::NAME,
"\"" + name + "\" is already defined (as something other than "
"a package) in file \"" + existing_symbol.GetFile()->name() +
"\".");
}
}
}
void DescriptorBuilder::ValidateSymbolName(
const string& name, const string& full_name, const Message& proto) {
if (name.empty()) {
AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME,
"Missing name.");
} else {
for (int i = 0; i < name.size(); i++) {
// I don't trust isalnum() due to locales. :(
if ((name[i] < 'a' || 'z' < name[i]) &&
(name[i] < 'A' || 'Z' < name[i]) &&
(name[i] < '0' || '9' < name[i]) &&
(name[i] != '_')) {
AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME,
"\"" + name + "\" is not a valid identifier.");
}
}
}
}
bool DescriptorBuilder::ValidateQualifiedName(const string& name) {
bool last_was_period = false;
for (int i = 0; i < name.size(); i++) {
// I don't trust isalnum() due to locales. :(
if (('a' <= name[i] && name[i] <= 'z') ||
('A' <= name[i] && name[i] <= 'Z') ||
('0' <= name[i] && name[i] <= '9') ||
(name[i] == '_')) {
last_was_period = false;
} else if (name[i] == '.') {
if (last_was_period) return false;
last_was_period = true;
} else {
return false;
}
}
return !name.empty() && !last_was_period;
}
// -------------------------------------------------------------------
// This generic implementation is good for all descriptors except
// FileDescriptor.
template<class DescriptorT> void DescriptorBuilder::AllocateOptions(
const typename DescriptorT::OptionsType& orig_options,
DescriptorT* descriptor) {
AllocateOptionsImpl(descriptor->full_name(), descriptor->full_name(),
orig_options, descriptor);
}
// We specialize for FileDescriptor.
void DescriptorBuilder::AllocateOptions(const FileOptions& orig_options,
FileDescriptor* descriptor) {
// We add the dummy token so that LookupSymbol does the right thing.
AllocateOptionsImpl(descriptor->package() + ".dummy", descriptor->name(),
orig_options, descriptor);
}
template<class DescriptorT> void DescriptorBuilder::AllocateOptionsImpl(
const string& name_scope,
const string& element_name,
const typename DescriptorT::OptionsType& orig_options,
DescriptorT* descriptor) {
// We need to use a dummy pointer to work around a bug in older versions of
// GCC. Otherwise, the following two lines could be replaced with:
// typename DescriptorT::OptionsType* options =
// tables_->AllocateMessage<typename DescriptorT::OptionsType>();
typename DescriptorT::OptionsType* const dummy = NULL;
typename DescriptorT::OptionsType* options = tables_->AllocateMessage(dummy);
options->CopyFrom(orig_options);
descriptor->options_ = options;
// Don't add to options_to_interpret_ unless there were uninterpreted
// options. This not only avoids unnecessary work, but prevents a
// bootstrapping problem when building descriptors for descriptor.proto.
// descriptor.proto does not contain any uninterpreted options, but
// attempting to interpret options anyway will cause
// OptionsType::GetDescriptor() to be called which may then deadlock since
// we're still trying to build it.
if (options->uninterpreted_option_size() > 0) {
options_to_interpret_.push_back(
OptionsToInterpret(name_scope, element_name, &orig_options, options));
}
}
// A common pattern: We want to convert a repeated field in the descriptor
// to an array of values, calling some method to build each value.
#define BUILD_ARRAY(INPUT, OUTPUT, NAME, METHOD, PARENT) \
OUTPUT->NAME##_count_ = INPUT.NAME##_size(); \
AllocateArray(INPUT.NAME##_size(), &OUTPUT->NAME##s_); \
for (int i = 0; i < INPUT.NAME##_size(); i++) { \
METHOD(INPUT.NAME(i), PARENT, OUTPUT->NAME##s_ + i); \
}
const FileDescriptor* DescriptorBuilder::BuildFile(
const FileDescriptorProto& proto) {
filename_ = proto.name();
// Check if the file already exists and is identical to the one being built.
// Note: This only works if the input is canonical -- that is, it
// fully-qualifies all type names, has no UninterpretedOptions, etc.
// This is fine, because this idempotency "feature" really only exists to
// accomodate one hack in the proto1->proto2 migration layer.
const FileDescriptor* existing_file = tables_->FindFile(filename_);
if (existing_file != NULL) {
// File already in pool. Compare the existing one to the input.
FileDescriptorProto existing_proto;
existing_file->CopyTo(&existing_proto);
if (existing_proto.SerializeAsString() == proto.SerializeAsString()) {
// They're identical. Return the existing descriptor.
return existing_file;
}
// Not a match. The error will be detected and handled later.
}
// Check to see if this file is already on the pending files list.
// TODO(kenton): Allow recursive imports? It may not work with some
// (most?) programming languages. E.g., in C++, a forward declaration
// of a type is not sufficient to allow it to be used even in a
// generated header file due to inlining. This could perhaps be
// worked around using tricks involving inserting #include statements
// mid-file, but that's pretty ugly, and I'm pretty sure there are
// some languages out there that do not allow recursive dependencies
// at all.
for (int i = 0; i < tables_->pending_files_.size(); i++) {
if (tables_->pending_files_[i] == proto.name()) {
string error_message("File recursively imports itself: ");
for (; i < tables_->pending_files_.size(); i++) {
error_message.append(tables_->pending_files_[i]);
error_message.append(" -> ");
}
error_message.append(proto.name());
AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER,
error_message);
return NULL;
}
}
// If we have a fallback_database_, attempt to load all dependencies now,
// before checkpointing tables_. This avoids confusion with recursive
// checkpoints.
if (pool_->fallback_database_ != NULL) {
tables_->pending_files_.push_back(proto.name());
for (int i = 0; i < proto.dependency_size(); i++) {
if (tables_->FindFile(proto.dependency(i)) == NULL &&
(pool_->underlay_ == NULL ||
pool_->underlay_->FindFileByName(proto.dependency(i)) == NULL)) {
// We don't care what this returns since we'll find out below anyway.
pool_->TryFindFileInFallbackDatabase(proto.dependency(i));
}
}
tables_->pending_files_.pop_back();
}
// Checkpoint the tables so that we can roll back if something goes wrong.
tables_->Checkpoint();
FileDescriptor* result = tables_->Allocate<FileDescriptor>();
file_ = result;
file_tables_ = tables_->AllocateFileTables();
file_->tables_ = file_tables_;
if (!proto.has_name()) {
AddError("", proto, DescriptorPool::ErrorCollector::OTHER,
"Missing field: FileDescriptorProto.name.");
}
result->name_ = tables_->AllocateString(proto.name());
if (proto.has_package()) {
result->package_ = tables_->AllocateString(proto.package());
} else {
// We cannot rely on proto.package() returning a valid string if
// proto.has_package() is false, because we might be running at static
// initialization time, in which case default values have not yet been
// initialized.
result->package_ = tables_->AllocateString("");
}
result->pool_ = pool_;
// Add to tables.
if (!tables_->AddFile(result)) {
AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER,
"A file with this name is already in the pool.");
// Bail out early so that if this is actually the exact same file, we
// don't end up reporting that every single symbol is already defined.
tables_->Rollback();
return NULL;
}
if (!result->package().empty()) {
AddPackage(result->package(), proto, result);
}
// Make sure all dependencies are loaded.
set<string> seen_dependencies;
result->dependency_count_ = proto.dependency_size();
result->dependencies_ =
tables_->AllocateArray<const FileDescriptor*>(proto.dependency_size());
for (int i = 0; i < proto.dependency_size(); i++) {
if (!seen_dependencies.insert(proto.dependency(i)).second) {
AddError(proto.name(), proto,
DescriptorPool::ErrorCollector::OTHER,
"Import \"" + proto.dependency(i) + "\" was listed twice.");
}
const FileDescriptor* dependency = tables_->FindFile(proto.dependency(i));
if (dependency == NULL && pool_->underlay_ != NULL) {
dependency = pool_->underlay_->FindFileByName(proto.dependency(i));
}
if (dependency == NULL) {
if (pool_->allow_unknown_) {
dependency = NewPlaceholderFile(proto.dependency(i));
} else {
string message;
if (pool_->fallback_database_ == NULL) {
message = "Import \"" + proto.dependency(i) +
"\" has not been loaded.";
} else {
message = "Import \"" + proto.dependency(i) +
"\" was not found or had errors.";
}
AddError(proto.name(), proto,
DescriptorPool::ErrorCollector::OTHER,
message);
}
}
result->dependencies_[i] = dependency;
}
// Convert children.
BUILD_ARRAY(proto, result, message_type, BuildMessage , NULL);
BUILD_ARRAY(proto, result, enum_type , BuildEnum , NULL);
BUILD_ARRAY(proto, result, service , BuildService , NULL);
BUILD_ARRAY(proto, result, extension , BuildExtension, NULL);
// Copy options.
if (!proto.has_options()) {
result->options_ = NULL; // Will set to default_instance later.
} else {
AllocateOptions(proto.options(), result);
}
// Note that the following steps must occur in exactly the specified order.
// Cross-link.
CrossLinkFile(result, proto);
// Interpret any remaining uninterpreted options gathered into
// options_to_interpret_ during descriptor building. Cross-linking has made
// extension options known, so all interpretations should now succeed.
if (!had_errors_) {
OptionInterpreter option_interpreter(this);
for (vector<OptionsToInterpret>::iterator iter =
options_to_interpret_.begin();
iter != options_to_interpret_.end(); ++iter) {
option_interpreter.InterpretOptions(&(*iter));
}
options_to_interpret_.clear();
}
// Validate options.
if (!had_errors_) {
ValidateFileOptions(result, proto);
}
if (had_errors_) {
tables_->Rollback();
return NULL;
} else {
tables_->Checkpoint();
return result;
}
}
void DescriptorBuilder::BuildMessage(const DescriptorProto& proto,
const Descriptor* parent,
Descriptor* result) {
const string& scope = (parent == NULL) ?
file_->package() : parent->full_name();
string* full_name = tables_->AllocateString(scope);
if (!full_name->empty()) full_name->append(1, '.');
full_name->append(proto.name());
ValidateSymbolName(proto.name(), *full_name, proto);
result->name_ = tables_->AllocateString(proto.name());
result->full_name_ = full_name;
result->file_ = file_;
result->containing_type_ = parent;
result->is_placeholder_ = false;
result->is_unqualified_placeholder_ = false;
BUILD_ARRAY(proto, result, field , BuildField , result);
BUILD_ARRAY(proto, result, nested_type , BuildMessage , result);
BUILD_ARRAY(proto, result, enum_type , BuildEnum , result);
BUILD_ARRAY(proto, result, extension_range, BuildExtensionRange, result);
BUILD_ARRAY(proto, result, extension , BuildExtension , result);
// Copy options.
if (!proto.has_options()) {
result->options_ = NULL; // Will set to default_instance later.
} else {
AllocateOptions(proto.options(), result);
}
AddSymbol(result->full_name(), parent, result->name(),
proto, Symbol(result));
// Check that no fields have numbers in extension ranges.
for (int i = 0; i < result->field_count(); i++) {
const FieldDescriptor* field = result->field(i);
for (int j = 0; j < result->extension_range_count(); j++) {
const Descriptor::ExtensionRange* range = result->extension_range(j);
if (range->start <= field->number() && field->number() < range->end) {
AddError(field->full_name(), proto.extension_range(j),
DescriptorPool::ErrorCollector::NUMBER,
strings::Substitute(
"Extension range $0 to $1 includes field \"$2\" ($3).",
range->start, range->end - 1,
field->name(), field->number()));
}
}
}
// Check that extension ranges don't overlap.
for (int i = 0; i < result->extension_range_count(); i++) {
const Descriptor::ExtensionRange* range1 = result->extension_range(i);
for (int j = i + 1; j < result->extension_range_count(); j++) {
const Descriptor::ExtensionRange* range2 = result->extension_range(j);
if (range1->end > range2->start && range2->end > range1->start) {
AddError(result->full_name(), proto.extension_range(j),
DescriptorPool::ErrorCollector::NUMBER,
strings::Substitute("Extension range $0 to $1 overlaps with "
"already-defined range $2 to $3.",
range2->start, range2->end - 1,
range1->start, range1->end - 1));
}
}
}
}
void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto,
const Descriptor* parent,
FieldDescriptor* result,
bool is_extension) {
const string& scope = (parent == NULL) ?
file_->package() : parent->full_name();
string* full_name = tables_->AllocateString(scope);
if (!full_name->empty()) full_name->append(1, '.');
full_name->append(proto.name());
ValidateSymbolName(proto.name(), *full_name, proto);
result->name_ = tables_->AllocateString(proto.name());
result->full_name_ = full_name;
result->file_ = file_;
result->number_ = proto.number();
result->is_extension_ = is_extension;
// If .proto files follow the style guide then the name should already be
// lower-cased. If that's the case we can just reuse the string we already
// allocated rather than allocate a new one.
string lowercase_name(proto.name());
LowerString(&lowercase_name);
if (lowercase_name == proto.name()) {
result->lowercase_name_ = result->name_;
} else {
result->lowercase_name_ = tables_->AllocateString(lowercase_name);
}
// Don't bother with the above optimization for camel-case names since
// .proto files that follow the guide shouldn't be using names in this
// format, so the optimization wouldn't help much.
result->camelcase_name_ = tables_->AllocateString(ToCamelCase(proto.name()));
// Some compilers do not allow static_cast directly between two enum types,
// so we must cast to int first.
result->type_ = static_cast<FieldDescriptor::Type>(
implicit_cast<int>(proto.type()));
result->label_ = static_cast<FieldDescriptor::Label>(
implicit_cast<int>(proto.label()));
// Some of these may be filled in when cross-linking.
result->containing_type_ = NULL;
result->extension_scope_ = NULL;
result->experimental_map_key_ = NULL;
result->message_type_ = NULL;
result->enum_type_ = NULL;
result->has_default_value_ = proto.has_default_value();
if (proto.has_default_value() && result->is_repeated()) {
AddError(result->full_name(), proto,
DescriptorPool::ErrorCollector::DEFAULT_VALUE,
"Repeated fields can't have default values.");
}
if (proto.has_type()) {
if (proto.has_default_value()) {
char* end_pos = NULL;
switch (result->cpp_type()) {
case FieldDescriptor::CPPTYPE_INT32:
result->default_value_int32_ =
strtol(proto.default_value().c_str(), &end_pos, 0);
break;
case FieldDescriptor::CPPTYPE_INT64:
result->default_value_int64_ =
strto64(proto.default_value().c_str(), &end_pos, 0);
break;
case FieldDescriptor::CPPTYPE_UINT32:
result->default_value_uint32_ =
strtoul(proto.default_value().c_str(), &end_pos, 0);
break;
case FieldDescriptor::CPPTYPE_UINT64:
result->default_value_uint64_ =
strtou64(proto.default_value().c_str(), &end_pos, 0);
break;
case FieldDescriptor::CPPTYPE_FLOAT:
result->default_value_float_ =
NoLocaleStrtod(proto.default_value().c_str(), &end_pos);
break;
case FieldDescriptor::CPPTYPE_DOUBLE:
result->default_value_double_ =
NoLocaleStrtod(proto.default_value().c_str(), &end_pos);
break;
case FieldDescriptor::CPPTYPE_BOOL:
if (proto.default_value() == "true") {
result->default_value_bool_ = true;
} else if (proto.default_value() == "false") {
result->default_value_bool_ = false;
} else {
AddError(result->full_name(), proto,
DescriptorPool::ErrorCollector::DEFAULT_VALUE,
"Boolean default must be true or false.");
}
break;
case FieldDescriptor::CPPTYPE_ENUM:
// This will be filled in when cross-linking.
result->default_value_enum_ = NULL;
break;
case FieldDescriptor::CPPTYPE_STRING:
if (result->type() == FieldDescriptor::TYPE_BYTES) {
result->default_value_string_ = tables_->AllocateString(
UnescapeCEscapeString(proto.default_value()));
} else {
result->default_value_string_ =
tables_->AllocateString(proto.default_value());
}
break;
case FieldDescriptor::CPPTYPE_MESSAGE:
AddError(result->full_name(), proto,
DescriptorPool::ErrorCollector::DEFAULT_VALUE,
"Messages can't have default values.");
result->has_default_value_ = false;
break;
}
if (end_pos != NULL) {
// end_pos is only set non-NULL by the parsers for numeric types, above.
// This checks that the default was non-empty and had no extra junk
// after the end of the number.
if (proto.default_value().empty() || *end_pos != '\0') {
AddError(result->full_name(), proto,
DescriptorPool::ErrorCollector::DEFAULT_VALUE,
"Couldn't parse default value.");
}
}
} else {
// No explicit default value
switch (result->cpp_type()) {
case FieldDescriptor::CPPTYPE_INT32:
result->default_value_int32_ = 0;
break;
case FieldDescriptor::CPPTYPE_INT64:
result->default_value_int64_ = 0;
break;
case FieldDescriptor::CPPTYPE_UINT32:
result->default_value_uint32_ = 0;
break;
case FieldDescriptor::CPPTYPE_UINT64:
result->default_value_uint64_ = 0;
break;
case FieldDescriptor::CPPTYPE_FLOAT:
result->default_value_float_ = 0.0f;
break;
case FieldDescriptor::CPPTYPE_DOUBLE:
result->default_value_double_ = 0.0;
break;
case FieldDescriptor::CPPTYPE_BOOL:
result->default_value_bool_ = false;
break;
case FieldDescriptor::CPPTYPE_ENUM:
// This will be filled in when cross-linking.
result->default_value_enum_ = NULL;
break;
case FieldDescriptor::CPPTYPE_STRING:
result->default_value_string_ = &kEmptyString;
break;
case FieldDescriptor::CPPTYPE_MESSAGE:
break;
}
}
}
if (result->number() <= 0) {
AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER,
"Field numbers must be positive integers.");
} else if (result->number() > FieldDescriptor::kMaxNumber) {
AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER,
strings::Substitute("Field numbers cannot be greater than $0.",
FieldDescriptor::kMaxNumber));
} else if (result->number() >= FieldDescriptor::kFirstReservedNumber &&
result->number() <= FieldDescriptor::kLastReservedNumber) {
AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER,
strings::Substitute(
"Field numbers $0 through $1 are reserved for the protocol "
"buffer library implementation.",
FieldDescriptor::kFirstReservedNumber,
FieldDescriptor::kLastReservedNumber));
}
if (is_extension) {
if (!proto.has_extendee()) {
AddError(result->full_name(), proto,
DescriptorPool::ErrorCollector::EXTENDEE,
"FieldDescriptorProto.extendee not set for extension field.");
}
result->extension_scope_ = parent;
} else {
if (proto.has_extendee()) {
AddError(result->full_name(), proto,
DescriptorPool::ErrorCollector::EXTENDEE,
"FieldDescriptorProto.extendee set for non-extension field.");
}
result->containing_type_ = parent;
}
// Copy options.
if (!proto.has_options()) {
result->options_ = NULL; // Will set to default_instance later.
} else {
AllocateOptions(proto.options(), result);
}
AddSymbol(result->full_name(), parent, result->name(),
proto, Symbol(result));
}
void DescriptorBuilder::BuildExtensionRange(
const DescriptorProto::ExtensionRange& proto,
const Descriptor* parent,
Descriptor::ExtensionRange* result) {
result->start = proto.start();
result->end = proto.end();
if (result->start <= 0) {
AddError(parent->full_name(), proto,
DescriptorPool::ErrorCollector::NUMBER,
"Extension numbers must be positive integers.");
}
if (result->end > FieldDescriptor::kMaxNumber + 1) {
AddError(parent->full_name(), proto,
DescriptorPool::ErrorCollector::NUMBER,
strings::Substitute("Extension numbers cannot be greater than $0.",
FieldDescriptor::kMaxNumber));
}
if (result->start >= result->end) {
AddError(parent->full_name(), proto,
DescriptorPool::ErrorCollector::NUMBER,
"Extension range end number must be greater than start number.");
}
}
void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto,
const Descriptor* parent,
EnumDescriptor* result) {
const string& scope = (parent == NULL) ?
file_->package() : parent->full_name();
string* full_name = tables_->AllocateString(scope);
if (!full_name->empty()) full_name->append(1, '.');
full_name->append(proto.name());
ValidateSymbolName(proto.name(), *full_name, proto);
result->name_ = tables_->AllocateString(proto.name());
result->full_name_ = full_name;
result->file_ = file_;
result->containing_type_ = parent;
result->is_placeholder_ = false;
result->is_unqualified_placeholder_ = false;
if (proto.value_size() == 0) {
// We cannot allow enums with no values because this would mean there
// would be no valid default value for fields of this type.
AddError(result->full_name(), proto,
DescriptorPool::ErrorCollector::NAME,
"Enums must contain at least one value.");
}
BUILD_ARRAY(proto, result, value, BuildEnumValue, result);
// Copy options.
if (!proto.has_options()) {
result->options_ = NULL; // Will set to default_instance later.
} else {
AllocateOptions(proto.options(), result);
}
AddSymbol(result->full_name(), parent, result->name(),
proto, Symbol(result));
}
void DescriptorBuilder::BuildEnumValue(const EnumValueDescriptorProto& proto,
const EnumDescriptor* parent,
EnumValueDescriptor* result) {
result->name_ = tables_->AllocateString(proto.name());
result->number_ = proto.number();
result->type_ = parent;
// Note: full_name for enum values is a sibling to the parent's name, not a
// child of it.
string* full_name = tables_->AllocateString(*parent->full_name_);
full_name->resize(full_name->size() - parent->name_->size());
full_name->append(*result->name_);
result->full_name_ = full_name;
ValidateSymbolName(proto.name(), *full_name, proto);
// Copy options.
if (!proto.has_options()) {
result->options_ = NULL; // Will set to default_instance later.
} else {
AllocateOptions(proto.options(), result);
}
// Again, enum values are weird because we makes them appear as siblings
// of the enum type instead of children of it. So, we use
// parent->containing_type() as the value's parent.
bool added_to_outer_scope =
AddSymbol(result->full_name(), parent->containing_type(), result->name(),
proto, Symbol(result));
// However, we also want to be able to search for values within a single
// enum type, so we add it as a child of the enum type itself, too.
// Note: This could fail, but if it does, the error has already been
// reported by the above AddSymbol() call, so we ignore the return code.
bool added_to_inner_scope =
file_tables_->AddAliasUnderParent(parent, result->name(), Symbol(result));
if (added_to_inner_scope && !added_to_outer_scope) {
// This value did not conflict with any values defined in the same enum,
// but it did conflict with some other symbol defined in the enum type's
// scope. Let's print an additional error to explain this.
string outer_scope;
if (parent->containing_type() == NULL) {
outer_scope = file_->package();
} else {
outer_scope = parent->containing_type()->full_name();
}
if (outer_scope.empty()) {
outer_scope = "the global scope";
} else {
outer_scope = "\"" + outer_scope + "\"";
}
AddError(result->full_name(), proto,
DescriptorPool::ErrorCollector::NAME,
"Note that enum values use C++ scoping rules, meaning that "
"enum values are siblings of their type, not children of it. "
"Therefore, \"" + result->name() + "\" must be unique within "
+ outer_scope + ", not just within \"" + parent->name() + "\".");
}
// An enum is allowed to define two numbers that refer to the same value.
// FindValueByNumber() should return the first such value, so we simply
// ignore AddEnumValueByNumber()'s return code.
file_tables_->AddEnumValueByNumber(result);
}
void DescriptorBuilder::BuildService(const ServiceDescriptorProto& proto,
const void* dummy,
ServiceDescriptor* result) {
string* full_name = tables_->AllocateString(file_->package());
if (!full_name->empty()) full_name->append(1, '.');
full_name->append(proto.name());
ValidateSymbolName(proto.name(), *full_name, proto);
result->name_ = tables_->AllocateString(proto.name());
result->full_name_ = full_name;
result->file_ = file_;
BUILD_ARRAY(proto, result, method, BuildMethod, result);
// Copy options.
if (!proto.has_options()) {
result->options_ = NULL; // Will set to default_instance later.
} else {
AllocateOptions(proto.options(), result);
}
AddSymbol(result->full_name(), NULL, result->name(),
proto, Symbol(result));
}
void DescriptorBuilder::BuildMethod(const MethodDescriptorProto& proto,
const ServiceDescriptor* parent,
MethodDescriptor* result) {
result->name_ = tables_->AllocateString(proto.name());
result->service_ = parent;
string* full_name = tables_->AllocateString(parent->full_name());
full_name->append(1, '.');
full_name->append(*result->name_);
result->full_name_ = full_name;
ValidateSymbolName(proto.name(), *full_name, proto);
// These will be filled in when cross-linking.
result->input_type_ = NULL;
result->output_type_ = NULL;
// Copy options.
if (!proto.has_options()) {
result->options_ = NULL; // Will set to default_instance later.
} else {
AllocateOptions(proto.options(), result);
}
AddSymbol(result->full_name(), parent, result->name(),
proto, Symbol(result));
}
#undef BUILD_ARRAY
// -------------------------------------------------------------------
void DescriptorBuilder::CrossLinkFile(
FileDescriptor* file, const FileDescriptorProto& proto) {
if (file->options_ == NULL) {
file->options_ = &FileOptions::default_instance();
}
for (int i = 0; i < file->message_type_count(); i++) {
CrossLinkMessage(&file->message_types_[i], proto.message_type(i));
}
for (int i = 0; i < file->extension_count(); i++) {
CrossLinkField(&file->extensions_[i], proto.extension(i));
}
for (int i = 0; i < file->enum_type_count(); i++) {
CrossLinkEnum(&file->enum_types_[i], proto.enum_type(i));
}
for (int i = 0; i < file->service_count(); i++) {
CrossLinkService(&file->services_[i], proto.service(i));
}
}
void DescriptorBuilder::CrossLinkMessage(
Descriptor* message, const DescriptorProto& proto) {
if (message->options_ == NULL) {
message->options_ = &MessageOptions::default_instance();
}
for (int i = 0; i < message->nested_type_count(); i++) {
CrossLinkMessage(&message->nested_types_[i], proto.nested_type(i));
}
for (int i = 0; i < message->enum_type_count(); i++) {
CrossLinkEnum(&message->enum_types_[i], proto.enum_type(i));
}
for (int i = 0; i < message->field_count(); i++) {
CrossLinkField(&message->fields_[i], proto.field(i));
}
for (int i = 0; i < message->extension_count(); i++) {
CrossLinkField(&message->extensions_[i], proto.extension(i));
}
}
void DescriptorBuilder::CrossLinkField(
FieldDescriptor* field, const FieldDescriptorProto& proto) {
if (field->options_ == NULL) {
field->options_ = &FieldOptions::default_instance();
}
if (proto.has_extendee()) {
Symbol extendee = LookupSymbol(proto.extendee(), field->full_name(),
PLACEHOLDER_EXTENDABLE_MESSAGE);
if (extendee.IsNull()) {
AddNotDefinedError(field->full_name(), proto,
DescriptorPool::ErrorCollector::EXTENDEE,
proto.extendee());
return;
} else if (extendee.type != Symbol::MESSAGE) {
AddError(field->full_name(), proto,
DescriptorPool::ErrorCollector::EXTENDEE,
"\"" + proto.extendee() + "\" is not a message type.");
return;
}
field->containing_type_ = extendee.descriptor;
if (!field->containing_type()->IsExtensionNumber(field->number())) {
AddError(field->full_name(), proto,
DescriptorPool::ErrorCollector::NUMBER,
strings::Substitute("\"$0\" does not declare $1 as an "
"extension number.",
field->containing_type()->full_name(),
field->number()));
}
}
if (proto.has_type_name()) {
// Assume we are expecting a message type unless the proto contains some
// evidence that it expects an enum type. This only makes a difference if
// we end up creating a placeholder.
bool expecting_enum = (proto.type() == FieldDescriptorProto::TYPE_ENUM) ||
proto.has_default_value();
Symbol type =
LookupSymbol(proto.type_name(), field->full_name(),
expecting_enum ? PLACEHOLDER_ENUM : PLACEHOLDER_MESSAGE,
LOOKUP_TYPES);
if (type.IsNull()) {
AddNotDefinedError(field->full_name(), proto,
DescriptorPool::ErrorCollector::TYPE,
proto.type_name());
return;
}
if (!proto.has_type()) {
// Choose field type based on symbol.
if (type.type == Symbol::MESSAGE) {
field->type_ = FieldDescriptor::TYPE_MESSAGE;
} else if (type.type == Symbol::ENUM) {
field->type_ = FieldDescriptor::TYPE_ENUM;
} else {
AddError(field->full_name(), proto,
DescriptorPool::ErrorCollector::TYPE,
"\"" + proto.type_name() + "\" is not a type.");
return;
}
}
if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
if (type.type != Symbol::MESSAGE) {
AddError(field->full_name(), proto,
DescriptorPool::ErrorCollector::TYPE,
"\"" + proto.type_name() + "\" is not a message type.");
return;
}
field->message_type_ = type.descriptor;
if (field->has_default_value()) {
AddError(field->full_name(), proto,
DescriptorPool::ErrorCollector::DEFAULT_VALUE,
"Messages can't have default values.");
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) {
if (type.type != Symbol::ENUM) {
AddError(field->full_name(), proto,
DescriptorPool::ErrorCollector::TYPE,
"\"" + proto.type_name() + "\" is not an enum type.");
return;
}
field->enum_type_ = type.enum_descriptor;
if (field->enum_type()->is_placeholder_) {
// We can't look up default values for placeholder types. We'll have
// to just drop them.
field->has_default_value_ = false;
}
if (field->has_default_value()) {
// We can't just use field->enum_type()->FindValueByName() here
// because that locks the pool's mutex, which we have already locked
// at this point.
Symbol default_value =
LookupSymbolNoPlaceholder(proto.default_value(),
field->enum_type()->full_name());
if (default_value.type == Symbol::ENUM_VALUE &&
default_value.enum_value_descriptor->type() == field->enum_type()) {
field->default_value_enum_ = default_value.enum_value_descriptor;
} else {
AddError(field->full_name(), proto,
DescriptorPool::ErrorCollector::DEFAULT_VALUE,
"Enum type \"" + field->enum_type()->full_name() +
"\" has no value named \"" + proto.default_value() + "\".");
}
} else if (field->enum_type()->value_count() > 0) {
// All enums must have at least one value, or we would have reported
// an error elsewhere. We use the first defined value as the default
// if a default is not explicitly defined.
field->default_value_enum_ = field->enum_type()->value(0);
}
} else {
AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
"Field with primitive type has type_name.");
}
} else {
if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE ||
field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) {
AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
"Field with message or enum type missing type_name.");
}
}
// Add the field to the fields-by-number table.
// Note: We have to do this *after* cross-linking because extensions do not
// know their containing type until now.
if (!file_tables_->AddFieldByNumber(field)) {
const FieldDescriptor* conflicting_field =
file_tables_->FindFieldByNumber(field->containing_type(),
field->number());
if (field->is_extension()) {
AddError(field->full_name(), proto,
DescriptorPool::ErrorCollector::NUMBER,
strings::Substitute("Extension number $0 has already been used "
"in \"$1\" by extension \"$2\".",
field->number(),
field->containing_type()->full_name(),
conflicting_field->full_name()));
} else {
AddError(field->full_name(), proto,
DescriptorPool::ErrorCollector::NUMBER,
strings::Substitute("Field number $0 has already been used in "
"\"$1\" by field \"$2\".",
field->number(),
field->containing_type()->full_name(),
conflicting_field->name()));
}
}
if (field->is_extension()) {
// No need for error checking: if the extension number collided,
// we've already been informed of it by the if() above.
tables_->AddExtension(field);
}
// Add the field to the lowercase-name and camelcase-name tables.
file_tables_->AddFieldByStylizedNames(field);
}
void DescriptorBuilder::CrossLinkEnum(
EnumDescriptor* enum_type, const EnumDescriptorProto& proto) {
if (enum_type->options_ == NULL) {
enum_type->options_ = &EnumOptions::default_instance();
}
for (int i = 0; i < enum_type->value_count(); i++) {
CrossLinkEnumValue(&enum_type->values_[i], proto.value(i));
}
}
void DescriptorBuilder::CrossLinkEnumValue(
EnumValueDescriptor* enum_value, const EnumValueDescriptorProto& proto) {
if (enum_value->options_ == NULL) {
enum_value->options_ = &EnumValueOptions::default_instance();
}
}
void DescriptorBuilder::CrossLinkService(
ServiceDescriptor* service, const ServiceDescriptorProto& proto) {
if (service->options_ == NULL) {
service->options_ = &ServiceOptions::default_instance();
}
for (int i = 0; i < service->method_count(); i++) {
CrossLinkMethod(&service->methods_[i], proto.method(i));
}
}
void DescriptorBuilder::CrossLinkMethod(
MethodDescriptor* method, const MethodDescriptorProto& proto) {
if (method->options_ == NULL) {
method->options_ = &MethodOptions::default_instance();
}
Symbol input_type = LookupSymbol(proto.input_type(), method->full_name());
if (input_type.IsNull()) {
AddNotDefinedError(method->full_name(), proto,
DescriptorPool::ErrorCollector::INPUT_TYPE,
proto.input_type());
} else if (input_type.type != Symbol::MESSAGE) {
AddError(method->full_name(), proto,
DescriptorPool::ErrorCollector::INPUT_TYPE,
"\"" + proto.input_type() + "\" is not a message type.");
} else {
method->input_type_ = input_type.descriptor;
}
Symbol output_type = LookupSymbol(proto.output_type(), method->full_name());
if (output_type.IsNull()) {
AddNotDefinedError(method->full_name(), proto,
DescriptorPool::ErrorCollector::OUTPUT_TYPE,
proto.output_type());
} else if (output_type.type != Symbol::MESSAGE) {
AddError(method->full_name(), proto,
DescriptorPool::ErrorCollector::OUTPUT_TYPE,
"\"" + proto.output_type() + "\" is not a message type.");
} else {
method->output_type_ = output_type.descriptor;
}
}
// -------------------------------------------------------------------
#define VALIDATE_OPTIONS_FROM_ARRAY(descriptor, array_name, type) \
for (int i = 0; i < descriptor->array_name##_count(); ++i) { \
Validate##type##Options(descriptor->array_name##s_ + i, \
proto.array_name(i)); \
}
void DescriptorBuilder::ValidateFileOptions(FileDescriptor* file,
const FileDescriptorProto& proto) {
VALIDATE_OPTIONS_FROM_ARRAY(file, message_type, Message);
VALIDATE_OPTIONS_FROM_ARRAY(file, enum_type, Enum);
VALIDATE_OPTIONS_FROM_ARRAY(file, service, Service);
VALIDATE_OPTIONS_FROM_ARRAY(file, extension, Field);
}
void DescriptorBuilder::ValidateMessageOptions(Descriptor* message,
const DescriptorProto& proto) {
VALIDATE_OPTIONS_FROM_ARRAY(message, field, Field);
VALIDATE_OPTIONS_FROM_ARRAY(message, nested_type, Message);
VALIDATE_OPTIONS_FROM_ARRAY(message, enum_type, Enum);
VALIDATE_OPTIONS_FROM_ARRAY(message, extension, Field);
}
void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field,
const FieldDescriptorProto& proto) {
if (field->options().has_experimental_map_key()) {
ValidateMapKey(field, proto);
}
// Only repeated primitive fields may be packed.
if (field->options().packed()) {
if (!field->is_repeated() ||
field->type() == FieldDescriptor::TYPE_STRING ||
field->type() == FieldDescriptor::TYPE_GROUP ||
field->type() == FieldDescriptor::TYPE_MESSAGE ||
field->type() == FieldDescriptor::TYPE_BYTES) {
AddError(
field->full_name(), proto,
DescriptorPool::ErrorCollector::TYPE,
"[packed = true] can only be specified for repeated primitive fields.");
}
}
// Note: Default instance may not yet be initialized here, so we have to
// avoid reading from it.
if (field->containing_type_ != NULL &&
&field->containing_type()->options() !=
&MessageOptions::default_instance() &&
field->containing_type()->options().message_set_wire_format()) {
if (field->is_extension()) {
if (!field->is_optional() ||
field->type() != FieldDescriptor::TYPE_MESSAGE) {
AddError(field->full_name(), proto,
DescriptorPool::ErrorCollector::TYPE,
"Extensions of MessageSets must be optional messages.");
}
} else {
AddError(field->full_name(), proto,
DescriptorPool::ErrorCollector::NAME,
"MessageSets cannot have fields, only extensions.");
}
}
}
void DescriptorBuilder::ValidateEnumOptions(EnumDescriptor* enm,
const EnumDescriptorProto& proto) {
VALIDATE_OPTIONS_FROM_ARRAY(enm, value, EnumValue);
}
void DescriptorBuilder::ValidateEnumValueOptions(
EnumValueDescriptor* enum_value, const EnumValueDescriptorProto& proto) {
// Nothing to do so far.
}
void DescriptorBuilder::ValidateServiceOptions(ServiceDescriptor* service,
const ServiceDescriptorProto& proto) {
VALIDATE_OPTIONS_FROM_ARRAY(service, method, Method);
}
void DescriptorBuilder::ValidateMethodOptions(MethodDescriptor* method,
const MethodDescriptorProto& proto) {
// Nothing to do so far.
}
void DescriptorBuilder::ValidateMapKey(FieldDescriptor* field,
const FieldDescriptorProto& proto) {
if (!field->is_repeated()) {
AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
"map type is only allowed for repeated fields.");
return;
}
if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
"map type is only allowed for fields with a message type.");
return;
}
const Descriptor* item_type = field->message_type();
if (item_type == NULL) {
AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
"Could not find field type.");
return;
}
// Find the field in item_type named by "experimental_map_key"
const string& key_name = field->options().experimental_map_key();
const Symbol key_symbol = LookupSymbol(
key_name,
// We append ".key_name" to the containing type's name since
// LookupSymbol() searches for peers of the supplied name, not
// children of the supplied name.
item_type->full_name() + "." + key_name);
if (key_symbol.IsNull() || key_symbol.field_descriptor->is_extension()) {
AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
"Could not find field named \"" + key_name + "\" in type \"" +
item_type->full_name() + "\".");
return;
}
const FieldDescriptor* key_field = key_symbol.field_descriptor;
if (key_field->is_repeated()) {
AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
"map_key must not name a repeated field.");
return;
}
if (key_field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
"map key must name a scalar or string field.");
return;
}
field->experimental_map_key_ = key_field;
}
#undef VALIDATE_OPTIONS_FROM_ARRAY
// -------------------------------------------------------------------
DescriptorBuilder::OptionInterpreter::OptionInterpreter(
DescriptorBuilder* builder) : builder_(builder) {
GOOGLE_CHECK(builder_);
}
DescriptorBuilder::OptionInterpreter::~OptionInterpreter() {
}
bool DescriptorBuilder::OptionInterpreter::InterpretOptions(
OptionsToInterpret* options_to_interpret) {
// Note that these may be in different pools, so we can't use the same
// descriptor and reflection objects on both.
Message* options = options_to_interpret->options;
const Message* original_options = options_to_interpret->original_options;
bool failed = false;
options_to_interpret_ = options_to_interpret;
// Find the uninterpreted_option field in the mutable copy of the options
// and clear them, since we're about to interpret them.
const FieldDescriptor* uninterpreted_options_field =
options->GetDescriptor()->FindFieldByName("uninterpreted_option");
GOOGLE_CHECK(uninterpreted_options_field != NULL)
<< "No field named \"uninterpreted_option\" in the Options proto.";
options->GetReflection()->ClearField(options, uninterpreted_options_field);
// Find the uninterpreted_option field in the original options.
const FieldDescriptor* original_uninterpreted_options_field =
original_options->GetDescriptor()->
FindFieldByName("uninterpreted_option");
GOOGLE_CHECK(original_uninterpreted_options_field != NULL)
<< "No field named \"uninterpreted_option\" in the Options proto.";
const int num_uninterpreted_options = original_options->GetReflection()->
FieldSize(*original_options, original_uninterpreted_options_field);
for (int i = 0; i < num_uninterpreted_options; ++i) {
uninterpreted_option_ = ::google::protobuf::down_cast<const UninterpretedOption*>(
&original_options->GetReflection()->GetRepeatedMessage(
*original_options, original_uninterpreted_options_field, i));
if (!InterpretSingleOption(options)) {
// Error already added by InterpretSingleOption().
failed = true;
break;
}
}
// Reset these, so we don't have any dangling pointers.
uninterpreted_option_ = NULL;
options_to_interpret_ = NULL;
if (!failed) {
// InterpretSingleOption() added the interpreted options in the
// UnknownFieldSet, in case the option isn't yet known to us. Now we
// serialize the options message and deserialize it back. That way, any
// option fields that we do happen to know about will get moved from the
// UnknownFieldSet into the real fields, and thus be available right away.
// If they are not known, that's OK too. They will get reparsed into the
// UnknownFieldSet and wait there until the message is parsed by something
// that does know about the options.
string buf;
options->AppendToString(&buf);
GOOGLE_CHECK(options->ParseFromString(buf))
<< "Protocol message serialized itself in invalid fashion.";
}
return !failed;
}
bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption(
Message* options) {
// First do some basic validation.
if (uninterpreted_option_->name_size() == 0) {
// This should never happen unless the parser has gone seriously awry or
// someone has manually created the uninterpreted option badly.
return AddNameError("Option must have a name.");
}
if (uninterpreted_option_->name(0).name_part() == "uninterpreted_option") {
return AddNameError("Option must not use reserved name "
"\"uninterpreted_option\".");
}
const Descriptor* options_descriptor = NULL;
// Get the options message's descriptor from the builder's pool, so that we
// get the version that knows about any extension options declared in the
// file we're currently building. The descriptor should be there as long as
// the file we're building imported "google/protobuf/descriptors.proto".
// Note that we use DescriptorBuilder::FindSymbol(), not
// DescriptorPool::FindMessageTypeByName() because we're already holding the
// pool's mutex, and the latter method locks it again.
Symbol symbol = builder_->FindSymbolNotEnforcingDeps(
options->GetDescriptor()->full_name());
if (!symbol.IsNull() && symbol.type == Symbol::MESSAGE) {
options_descriptor = symbol.descriptor;
} else {
// The options message's descriptor was not in the builder's pool, so use
// the standard version from the generated pool. We're not holding the
// generated pool's mutex, so we can search it the straightforward way.
options_descriptor = options->GetDescriptor();
}
GOOGLE_CHECK(options_descriptor);
// We iterate over the name parts to drill into the submessages until we find
// the leaf field for the option. As we drill down we remember the current
// submessage's descriptor in |descriptor| and the next field in that
// submessage in |field|. We also track the fields we're drilling down
// through in |intermediate_fields|. As we go, we reconstruct the full option
// name in |debug_msg_name|, for use in error messages.
const Descriptor* descriptor = options_descriptor;
const FieldDescriptor* field = NULL;
vector<const FieldDescriptor*> intermediate_fields;
string debug_msg_name = "";
for (int i = 0; i < uninterpreted_option_->name_size(); ++i) {
const string& name_part = uninterpreted_option_->name(i).name_part();
if (debug_msg_name.size() > 0) {
debug_msg_name += ".";
}
if (uninterpreted_option_->name(i).is_extension()) {
debug_msg_name += "(" + name_part + ")";
// Search for the extension's descriptor as an extension in the builder's
// pool. Note that we use DescriptorBuilder::LookupSymbol(), not
// DescriptorPool::FindExtensionByName(), for two reasons: 1) It allows
// relative lookups, and 2) because we're already holding the pool's
// mutex, and the latter method locks it again.
Symbol symbol = builder_->LookupSymbol(name_part,
options_to_interpret_->name_scope);
if (!symbol.IsNull() && symbol.type == Symbol::FIELD) {
field = symbol.field_descriptor;
}
// If we don't find the field then the field's descriptor was not in the
// builder's pool, but there's no point in looking in the generated
// pool. We require that you import the file that defines any extensions
// you use, so they must be present in the builder's pool.
} else {
debug_msg_name += name_part;
// Search for the field's descriptor as a regular field.
field = descriptor->FindFieldByName(name_part);
}
if (field == NULL) {
if (get_allow_unknown(builder_->pool_)) {
// We can't find the option, but AllowUnknownDependencies() is enabled,
// so we will just leave it as uninterpreted.
AddWithoutInterpreting(*uninterpreted_option_, options);
return true;
} else {
return AddNameError("Option \"" + debug_msg_name + "\" unknown.");
}
} else if (field->containing_type() != descriptor) {
if (get_is_placeholder(field->containing_type())) {
// The field is an extension of a placeholder type, so we can't
// reliably verify whether it is a valid extension to use here (e.g.
// we don't know if it is an extension of the correct *Options message,
// or if it has a valid field number, etc.). Just leave it as
// uninterpreted instead.
AddWithoutInterpreting(*uninterpreted_option_, options);
return true;
} else {
// This can only happen if, due to some insane misconfiguration of the
// pools, we find the options message in one pool but the field in
// another. This would probably imply a hefty bug somewhere.
return AddNameError("Option field \"" + debug_msg_name +
"\" is not a field or extension of message \"" +
descriptor->name() + "\".");
}
} else if (field->is_repeated()) {
return AddNameError("Option field \"" + debug_msg_name +
"\" is repeated. Repeated options are not "
"supported.");
} else if (i < uninterpreted_option_->name_size() - 1) {
if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
return AddNameError("Option \"" + debug_msg_name +
"\" is an atomic type, not a message.");
} else {
// Drill down into the submessage.
intermediate_fields.push_back(field);
descriptor = field->message_type();
}
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
return AddNameError("Option field \"" + debug_msg_name +
"\" cannot be of message type.");
}
}
// We've found the leaf field. Now we use UnknownFieldSets to set its value
// on the options message. We do so because the message may not yet know
// about its extension fields, so we may not be able to set the fields
// directly. But the UnknownFieldSets will serialize to the same wire-format
// message, so reading that message back in once the extension fields are
// known will populate them correctly.
// First see if the option is already set.
if (!ExamineIfOptionIsSet(
intermediate_fields.begin(),
intermediate_fields.end(),
field, debug_msg_name,
options->GetReflection()->GetUnknownFields(*options))) {
return false; // ExamineIfOptionIsSet() already added the error.
}
// First set the value on the UnknownFieldSet corresponding to the
// innermost message.
scoped_ptr<UnknownFieldSet> unknown_fields(new UnknownFieldSet());
if (!SetOptionValue(field, unknown_fields.get())) {
return false; // SetOptionValue() already added the error.
}
// Now wrap the UnknownFieldSet with UnknownFieldSets corresponding to all
// the intermediate messages.
for (vector<const FieldDescriptor*>::reverse_iterator iter =
intermediate_fields.rbegin();
iter != intermediate_fields.rend(); ++iter) {
scoped_ptr<UnknownFieldSet> parent_unknown_fields(new UnknownFieldSet());
switch ((*iter)->type()) {
case FieldDescriptor::TYPE_MESSAGE: {
io::StringOutputStream outstr(
parent_unknown_fields->AddLengthDelimited((*iter)->number()));
io::CodedOutputStream out(&outstr);
internal::WireFormat::SerializeUnknownFields(*unknown_fields, &out);
GOOGLE_CHECK(!out.HadError())
<< "Unexpected failure while serializing option submessage "
<< debug_msg_name << "\".";
break;
}
case FieldDescriptor::TYPE_GROUP: {
parent_unknown_fields->AddGroup((*iter)->number())
->MergeFrom(*unknown_fields);
break;
}
default:
GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_MESSAGE: "
<< (*iter)->type();
return false;
}
unknown_fields.reset(parent_unknown_fields.release());
}
// Now merge the UnknownFieldSet corresponding to the top-level message into
// the options message.
options->GetReflection()->MutableUnknownFields(options)->MergeFrom(
*unknown_fields);
return true;
}
void DescriptorBuilder::OptionInterpreter::AddWithoutInterpreting(
const UninterpretedOption& uninterpreted_option, Message* options) {
const FieldDescriptor* field =
options->GetDescriptor()->FindFieldByName("uninterpreted_option");
GOOGLE_CHECK(field != NULL);
options->GetReflection()->AddMessage(options, field)
->CopyFrom(uninterpreted_option);
}
bool DescriptorBuilder::OptionInterpreter::ExamineIfOptionIsSet(
vector<const FieldDescriptor*>::const_iterator intermediate_fields_iter,
vector<const FieldDescriptor*>::const_iterator intermediate_fields_end,
const FieldDescriptor* innermost_field, const string& debug_msg_name,
const UnknownFieldSet& unknown_fields) {
// We do linear searches of the UnknownFieldSet and its sub-groups. This
// should be fine since it's unlikely that any one options structure will
// contain more than a handful of options.
if (intermediate_fields_iter == intermediate_fields_end) {
// We're at the innermost submessage.
for (int i = 0; i < unknown_fields.field_count(); i++) {
if (unknown_fields.field(i).number() == innermost_field->number()) {
return AddNameError("Option \"" + debug_msg_name +
"\" was already set.");
}
}
return true;
}
for (int i = 0; i < unknown_fields.field_count(); i++) {
if (unknown_fields.field(i).number() ==
(*intermediate_fields_iter)->number()) {
const UnknownField* unknown_field = &unknown_fields.field(i);
FieldDescriptor::Type type = (*intermediate_fields_iter)->type();
// Recurse into the next submessage.
++intermediate_fields_iter;
switch (type) {
case FieldDescriptor::TYPE_MESSAGE:
if (unknown_field->type() == UnknownField::TYPE_LENGTH_DELIMITED) {
UnknownFieldSet intermediate_unknown_fields;
if (intermediate_unknown_fields.ParseFromString(
unknown_field->length_delimited()) &&
!ExamineIfOptionIsSet(intermediate_fields_iter,
intermediate_fields_end,
innermost_field, debug_msg_name,
intermediate_unknown_fields)) {
return false; // Error already added.
}
}
break;
case FieldDescriptor::TYPE_GROUP:
if (unknown_field->type() == UnknownField::TYPE_GROUP) {
if (!ExamineIfOptionIsSet(intermediate_fields_iter,
intermediate_fields_end,
innermost_field, debug_msg_name,
unknown_field->group())) {
return false; // Error already added.
}
}
break;
default:
GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_MESSAGE: " << type;
return false;
}
}
}
return true;
}
bool DescriptorBuilder::OptionInterpreter::SetOptionValue(
const FieldDescriptor* option_field,
UnknownFieldSet* unknown_fields) {
// We switch on the CppType to validate.
switch (option_field->cpp_type()) {
case FieldDescriptor::CPPTYPE_INT32:
if (uninterpreted_option_->has_positive_int_value()) {
if (uninterpreted_option_->positive_int_value() >
static_cast<uint64>(kint32max)) {
return AddValueError("Value out of range for int32 option \"" +
option_field->full_name() + "\".");
} else {
SetInt32(option_field->number(),
uninterpreted_option_->positive_int_value(),
option_field->type(), unknown_fields);
}
} else if (uninterpreted_option_->has_negative_int_value()) {
if (uninterpreted_option_->negative_int_value() <
static_cast<int64>(kint32min)) {
return AddValueError("Value out of range for int32 option \"" +
option_field->full_name() + "\".");
} else {
SetInt32(option_field->number(),
uninterpreted_option_->negative_int_value(),
option_field->type(), unknown_fields);
}
} else {
return AddValueError("Value must be integer for int32 option \"" +
option_field->full_name() + "\".");
}
break;
case FieldDescriptor::CPPTYPE_INT64:
if (uninterpreted_option_->has_positive_int_value()) {
if (uninterpreted_option_->positive_int_value() >
static_cast<uint64>(kint64max)) {
return AddValueError("Value out of range for int64 option \"" +
option_field->full_name() + "\".");
} else {
SetInt64(option_field->number(),
uninterpreted_option_->positive_int_value(),
option_field->type(), unknown_fields);
}
} else if (uninterpreted_option_->has_negative_int_value()) {
SetInt64(option_field->number(),
uninterpreted_option_->negative_int_value(),
option_field->type(), unknown_fields);
} else {
return AddValueError("Value must be integer for int64 option \"" +
option_field->full_name() + "\".");
}
break;
case FieldDescriptor::CPPTYPE_UINT32:
if (uninterpreted_option_->has_positive_int_value()) {
if (uninterpreted_option_->positive_int_value() > kuint32max) {
return AddValueError("Value out of range for uint32 option \"" +
option_field->name() + "\".");
} else {
SetUInt32(option_field->number(),
uninterpreted_option_->positive_int_value(),
option_field->type(), unknown_fields);
}
} else {
return AddValueError("Value must be non-negative integer for uint32 "
"option \"" + option_field->full_name() + "\".");
}
break;
case FieldDescriptor::CPPTYPE_UINT64:
if (uninterpreted_option_->has_positive_int_value()) {
SetUInt64(option_field->number(),
uninterpreted_option_->positive_int_value(),
option_field->type(), unknown_fields);
} else {
return AddValueError("Value must be non-negative integer for uint64 "
"option \"" + option_field->full_name() + "\".");
}
break;
case FieldDescriptor::CPPTYPE_FLOAT: {
float value;
if (uninterpreted_option_->has_double_value()) {
value = uninterpreted_option_->double_value();
} else if (uninterpreted_option_->has_positive_int_value()) {
value = uninterpreted_option_->positive_int_value();
} else if (uninterpreted_option_->has_negative_int_value()) {
value = uninterpreted_option_->negative_int_value();
} else {
return AddValueError("Value must be number for float option \"" +
option_field->full_name() + "\".");
}
unknown_fields->AddFixed32(option_field->number(),
google::protobuf::internal::WireFormat::EncodeFloat(value));
break;
}
case FieldDescriptor::CPPTYPE_DOUBLE: {
double value;
if (uninterpreted_option_->has_double_value()) {
value = uninterpreted_option_->double_value();
} else if (uninterpreted_option_->has_positive_int_value()) {
value = uninterpreted_option_->positive_int_value();
} else if (uninterpreted_option_->has_negative_int_value()) {
value = uninterpreted_option_->negative_int_value();
} else {
return AddValueError("Value must be number for double option \"" +
option_field->full_name() + "\".");
}
unknown_fields->AddFixed64(option_field->number(),
google::protobuf::internal::WireFormat::EncodeDouble(value));
break;
}
case FieldDescriptor::CPPTYPE_BOOL:
uint64 value;
if (!uninterpreted_option_->has_identifier_value()) {
return AddValueError("Value must be identifier for boolean option "
"\"" + option_field->full_name() + "\".");
}
if (uninterpreted_option_->identifier_value() == "true") {
value = 1;
} else if (uninterpreted_option_->identifier_value() == "false") {
value = 0;
} else {
return AddValueError("Value must be \"true\" or \"false\" for boolean "
"option \"" + option_field->full_name() + "\".");
}
unknown_fields->AddVarint(option_field->number(), value);
break;
case FieldDescriptor::CPPTYPE_ENUM: {
if (!uninterpreted_option_->has_identifier_value()) {
return AddValueError("Value must be identifier for enum-valued option "
"\"" + option_field->full_name() + "\".");
}
const EnumDescriptor* enum_type = option_field->enum_type();
const string& value_name = uninterpreted_option_->identifier_value();
const EnumValueDescriptor* enum_value = NULL;
if (enum_type->file()->pool() != DescriptorPool::generated_pool()) {
// Note that the enum value's fully-qualified name is a sibling of the
// enum's name, not a child of it.
string fully_qualified_name = enum_type->full_name();
fully_qualified_name.resize(fully_qualified_name.size() -
enum_type->name().size());
fully_qualified_name += value_name;
// Search for the enum value's descriptor in the builder's pool. Note
// that we use DescriptorBuilder::FindSymbolNotEnforcingDeps(), not
// DescriptorPool::FindEnumValueByName() because we're already holding
// the pool's mutex, and the latter method locks it again.
Symbol symbol =
builder_->FindSymbolNotEnforcingDeps(fully_qualified_name);
if (!symbol.IsNull() && symbol.type == Symbol::ENUM_VALUE) {
if (symbol.enum_value_descriptor->type() != enum_type) {
return AddValueError("Enum type \"" + enum_type->full_name() +
"\" has no value named \"" + value_name + "\" for option \"" +
option_field->full_name() +
"\". This appears to be a value from a sibling type.");
} else {
enum_value = symbol.enum_value_descriptor;
}
}
} else {
// The enum type is in the generated pool, so we can search for the
// value there.
enum_value = enum_type->FindValueByName(value_name);
}
if (enum_value == NULL) {
return AddValueError("Enum type \"" +
option_field->enum_type()->full_name() +
"\" has no value named \"" + value_name + "\" for "
"option \"" + option_field->full_name() + "\".");
} else {
// Sign-extension is not a problem, since we cast directly from int32 to
// uint64, without first going through uint32.
unknown_fields->AddVarint(option_field->number(),
static_cast<uint64>(static_cast<int64>(enum_value->number())));
}
break;
}
case FieldDescriptor::CPPTYPE_STRING:
if (!uninterpreted_option_->has_string_value()) {
return AddValueError("Value must be quoted string for string option "
"\"" + option_field->full_name() + "\".");
}
// The string has already been unquoted and unescaped by the parser.
unknown_fields->AddLengthDelimited(option_field->number(),
uninterpreted_option_->string_value());
break;
case FieldDescriptor::CPPTYPE_MESSAGE:
// We don't currently support defining a message-typed option, so we
// should never actually get here.
return AddValueError("Option \"" + option_field->full_name() +
"\" is a message. To set fields within it, use "
"syntax like \"" + option_field->name() +
".foo = value\".");
break;
}
return true;
}
void DescriptorBuilder::OptionInterpreter::SetInt32(int number, int32 value,
FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) {
switch (type) {
case FieldDescriptor::TYPE_INT32:
unknown_fields->AddVarint(number,
static_cast<uint64>(static_cast<int64>(value)));
break;
case FieldDescriptor::TYPE_SFIXED32:
unknown_fields->AddFixed32(number, static_cast<uint32>(value));
break;
case FieldDescriptor::TYPE_SINT32:
unknown_fields->AddVarint(number,
google::protobuf::internal::WireFormat::ZigZagEncode32(value));
break;
default:
GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_INT32: " << type;
break;
}
}
void DescriptorBuilder::OptionInterpreter::SetInt64(int number, int64 value,
FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) {
switch (type) {
case FieldDescriptor::TYPE_INT64:
unknown_fields->AddVarint(number, static_cast<uint64>(value));
break;
case FieldDescriptor::TYPE_SFIXED64:
unknown_fields->AddFixed64(number, static_cast<uint64>(value));
break;
case FieldDescriptor::TYPE_SINT64:
unknown_fields->AddVarint(number,
google::protobuf::internal::WireFormat::ZigZagEncode64(value));
break;
default:
GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_INT64: " << type;
break;
}
}
void DescriptorBuilder::OptionInterpreter::SetUInt32(int number, uint32 value,
FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) {
switch (type) {
case FieldDescriptor::TYPE_UINT32:
unknown_fields->AddVarint(number, static_cast<uint64>(value));
break;
case FieldDescriptor::TYPE_FIXED32:
unknown_fields->AddFixed32(number, static_cast<uint32>(value));
break;
default:
GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_UINT32: " << type;
break;
}
}
void DescriptorBuilder::OptionInterpreter::SetUInt64(int number, uint64 value,
FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) {
switch (type) {
case FieldDescriptor::TYPE_UINT64:
unknown_fields->AddVarint(number, value);
break;
case FieldDescriptor::TYPE_FIXED64:
unknown_fields->AddFixed64(number, value);
break;
default:
GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_UINT64: " << type;
break;
}
}
} // namespace protobuf
} // namespace google
|