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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */


#include <algorithm>
#include <unordered_map>
#include <cassert>
#include <list>
#include <set>
#include <vector>
#include <memory>

#include <stdlib.h>
#include <string.h>
#include <sal/log.hxx>
#include <osl/interlck.h>
#include <osl/mutex.hxx>
#include <rtl/ustring.hxx>
#include <rtl/ustrbuf.hxx>
#include <rtl/instance.hxx>
#include <osl/diagnose.h>
#include <typelib/typedescription.h>
#include <uno/any2.h>
#include "typelib.hxx"

using namespace std;
using namespace osl;

#ifdef _WIN32
#pragma pack(push, 8)
#endif

/**
 * The double member determines the alignment.
 * Under OS2 and MS-Windows the Alignment is min( 8, sizeof( type ) ).
 * The alignment of a structure is min( 8, sizeof( max basic type ) ), the greatest basic type
 * determines the alignment.
 */
struct AlignSize_Impl
{
    sal_Int16 nInt16;
#ifdef AIX
    //double: doubleword aligned if -qalign=natural/-malign=natural
    //which isn't the default ABI. Otherwise word aligned, While a long long int
    //is always doubleword aligned, so use that instead.
    sal_Int64 dDouble;
#else
    double dDouble;
#endif
};

#ifdef _WIN32
#pragma pack(pop)
#endif

// the value of the maximal alignment
static const sal_Int32 nMaxAlignment = static_cast<sal_Int32>( reinterpret_cast<sal_Size>(&reinterpret_cast<AlignSize_Impl *>(16)->dDouble) - 16);

static sal_Int32 adjustAlignment( sal_Int32 nRequestedAlignment )
{
    if( nRequestedAlignment > nMaxAlignment )
        nRequestedAlignment = nMaxAlignment;
    return nRequestedAlignment;
}

/**
 * Calculate the new size of the structure.
 */
static sal_Int32 newAlignedSize(
    sal_Int32 OldSize, sal_Int32 ElementSize, sal_Int32 NeededAlignment )
{
    NeededAlignment = adjustAlignment( NeededAlignment );
    return (OldSize + NeededAlignment -1) / NeededAlignment * NeededAlignment + ElementSize;
}

static bool reallyWeak( typelib_TypeClass eTypeClass )
{
    return TYPELIB_TYPEDESCRIPTIONREFERENCE_ISREALLYWEAK( eTypeClass );
}

static sal_Int32 getDescriptionSize( typelib_TypeClass eTypeClass )
{
    OSL_ASSERT( typelib_TypeClass_TYPEDEF != eTypeClass );

    sal_Int32 nSize;
    // The reference is the description
    // if the description is empty, then it must be filled with
    // the new description
    switch( eTypeClass )
    {
        case typelib_TypeClass_SEQUENCE:
            nSize = sal_Int32(sizeof( typelib_IndirectTypeDescription ));
        break;

        case typelib_TypeClass_STRUCT:
            nSize = sal_Int32(sizeof( typelib_StructTypeDescription ));
        break;

        case typelib_TypeClass_EXCEPTION:
            nSize = sal_Int32(sizeof( typelib_CompoundTypeDescription ));
        break;

        case typelib_TypeClass_ENUM:
            nSize = sal_Int32(sizeof( typelib_EnumTypeDescription ));
        break;

        case typelib_TypeClass_INTERFACE:
            nSize = sal_Int32(sizeof( typelib_InterfaceTypeDescription ));
        break;

        case typelib_TypeClass_INTERFACE_METHOD:
            nSize = sal_Int32(sizeof( typelib_InterfaceMethodTypeDescription ));
        break;

        case typelib_TypeClass_INTERFACE_ATTRIBUTE:
            nSize = sal_Int32(sizeof( typelib_InterfaceAttributeTypeDescription ));
        break;

        default:
            nSize = sal_Int32(sizeof( typelib_TypeDescription ));
    }
    return nSize;
}


struct equalStr_Impl
{
    bool operator()(const sal_Unicode * const & s1, const sal_Unicode * const & s2) const
        { return 0 == rtl_ustr_compare( s1, s2 ); }
};


struct hashStr_Impl
{
    size_t operator()(const sal_Unicode * const & s) const
        { return rtl_ustr_hashCode( s ); }
};


// Heavy hack, the const sal_Unicode * is hold by the typedescription reference
typedef std::unordered_map< const sal_Unicode *, typelib_TypeDescriptionReference *,
                  hashStr_Impl, equalStr_Impl > WeakMap_Impl;

typedef pair< void *, typelib_typedescription_Callback > CallbackEntry;
typedef list< CallbackEntry > CallbackSet_Impl;
typedef list< typelib_TypeDescription * > TypeDescriptionList_Impl;

// # of cached elements
static sal_Int32 nCacheSize = 256;

struct TypeDescriptor_Init_Impl
{
    //sal_Bool          bDesctructorCalled;
    // all type description references
    std::unique_ptr<WeakMap_Impl> pWeakMap;
    // all type description callbacks
    std::unique_ptr<CallbackSet_Impl> pCallbacks;
    // A cache to hold descriptions
    std::unique_ptr<TypeDescriptionList_Impl> pCache;
    // The mutex to guard all type library accesses
    std::unique_ptr<Mutex>      pMutex;

    inline Mutex & getMutex();

    inline void callChain( typelib_TypeDescription ** ppRet, rtl_uString * pName );

#if OSL_DEBUG_LEVEL > 0
    // only for debugging
    sal_Int32           nTypeDescriptionCount;
    sal_Int32           nCompoundTypeDescriptionCount;
    sal_Int32           nIndirectTypeDescriptionCount;
    sal_Int32           nEnumTypeDescriptionCount;
    sal_Int32           nInterfaceMethodTypeDescriptionCount;
    sal_Int32           nInterfaceAttributeTypeDescriptionCount;
    sal_Int32           nInterfaceTypeDescriptionCount;
    sal_Int32           nTypeDescriptionReferenceCount;
#endif

    TypeDescriptor_Init_Impl()
#if OSL_DEBUG_LEVEL > 0
        : nTypeDescriptionCount(0), nCompoundTypeDescriptionCount(0),
        nIndirectTypeDescriptionCount(0),
        nEnumTypeDescriptionCount(0),
        nInterfaceMethodTypeDescriptionCount(0),
        nInterfaceAttributeTypeDescriptionCount(0),
        nInterfaceTypeDescriptionCount(0), nTypeDescriptionReferenceCount(0)
#endif
    {}

    ~TypeDescriptor_Init_Impl();
};

inline Mutex & TypeDescriptor_Init_Impl::getMutex()
{
    if( !pMutex )
    {
        MutexGuard aGuard( Mutex::getGlobalMutex() );
        if( !pMutex )
            pMutex.reset(new Mutex());
    }
    return * pMutex;
}

inline void TypeDescriptor_Init_Impl::callChain(
    typelib_TypeDescription ** ppRet, rtl_uString * pName )
{
    assert(ppRet != nullptr);
    assert(*ppRet == nullptr);
    if (pCallbacks)
    {
        for( const CallbackEntry & rEntry : *pCallbacks )
        {
            (*rEntry.second)( rEntry.first, ppRet, pName );
            if( *ppRet )
                return;
        }
    }
}


TypeDescriptor_Init_Impl::~TypeDescriptor_Init_Impl()
{
    if( pCache )
    {
        for( typelib_TypeDescription* pItem : *pCache )
        {
            typelib_typedescription_release( pItem );
        }
    }

    if( pWeakMap )
    {
        std::vector< typelib_TypeDescriptionReference * > ppTDR;
        ppTDR.reserve( pWeakMap->size() );

        // save all weak references
        for( const auto& rEntry : *pWeakMap )
        {
            ppTDR.push_back( rEntry.second );
            typelib_typedescriptionreference_acquire( ppTDR.back() );
        }

        for( typelib_TypeDescriptionReference * pTDR : ppTDR )
        {
            OSL_ASSERT( pTDR->nRefCount > pTDR->nStaticRefCount );
            pTDR->nRefCount -= pTDR->nStaticRefCount;

            if( pTDR->pType && !pTDR->pType->bOnDemand )
            {
                pTDR->pType->bOnDemand = true;
                typelib_typedescription_release( pTDR->pType );
            }
            typelib_typedescriptionreference_release( pTDR );
        }

#if defined SAL_LOG_INFO
        for( const auto& rEntry : *pWeakMap )
        {
            typelib_TypeDescriptionReference * pTDR = rEntry.second;
            if (pTDR)
            {
                OString aTypeName( OUStringToOString( pTDR->pTypeName, RTL_TEXTENCODING_ASCII_US ) );
                SAL_INFO("cppu.typelib", "remaining type: " << aTypeName << "; ref count = " << pTDR->nRefCount);
            }
            else
            {
                SAL_INFO("cppu.typelib", "remaining null type entry!?");
            }
        }
#endif
    }
#if OSL_DEBUG_LEVEL > 0
    SAL_INFO_IF( nTypeDescriptionCount, "cppu.typelib", "nTypeDescriptionCount is not zero" );
    SAL_INFO_IF( nCompoundTypeDescriptionCount, "cppu.typelib", "nCompoundTypeDescriptionCount is not zero" );
    SAL_INFO_IF( nIndirectTypeDescriptionCount, "cppu.typelib", "nIndirectTypeDescriptionCount is not zero" );
    SAL_INFO_IF( nEnumTypeDescriptionCount, "cppu.typelib", "nEnumTypeDescriptionCount is not zero" );
    SAL_INFO_IF( nInterfaceMethodTypeDescriptionCount, "cppu.typelib", "nInterfaceMethodTypeDescriptionCount is not zero" );
    SAL_INFO_IF( nInterfaceAttributeTypeDescriptionCount, "cppu.typelib", "nInterfaceAttributeTypeDescriptionCount is not zero" );
    SAL_INFO_IF( nInterfaceTypeDescriptionCount, "cppu.typelib", "nInterfaceTypeDescriptionCount is not zero" );
    SAL_INFO_IF( nTypeDescriptionReferenceCount, "cppu.typelib", "nTypeDescriptionReferenceCount is not zero" );
#endif

    SAL_INFO_IF( pCallbacks && !pCallbacks->empty(), "cppu.typelib", "pCallbacks is not NULL or empty" );
    pCallbacks.reset();
};

namespace { struct Init : public rtl::Static< TypeDescriptor_Init_Impl, Init > {}; }

extern "C" void SAL_CALL typelib_typedescription_registerCallback(
    void * pContext, typelib_typedescription_Callback pCallback )
    SAL_THROW_EXTERN_C()
{
    // todo mt safe: guard is no solution, can not acquire while calling callback!
    TypeDescriptor_Init_Impl &rInit = Init::get();
//      OslGuard aGuard( rInit.getMutex() );
    if( !rInit.pCallbacks )
        rInit.pCallbacks.reset(new CallbackSet_Impl);
    rInit.pCallbacks->push_back( CallbackEntry( pContext, pCallback ) );
}


extern "C" void SAL_CALL typelib_typedescription_revokeCallback(
    void * pContext, typelib_typedescription_Callback pCallback )
    SAL_THROW_EXTERN_C()
{
    TypeDescriptor_Init_Impl &rInit = Init::get();
    if( rInit.pCallbacks )
    {
        // todo mt safe: guard is no solution, can not acquire while calling callback!
//          OslGuard aGuard( rInit.getMutex() );
        CallbackEntry aEntry( pContext, pCallback );
        rInit.pCallbacks->erase(std::remove(rInit.pCallbacks->begin(), rInit.pCallbacks->end(), aEntry),
                                rInit.pCallbacks->end());
    }
}

static void typelib_typedescription_initTables(
    typelib_TypeDescription * pTD )
{
    typelib_InterfaceTypeDescription * pITD = reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD);

    std::vector<bool> aReadWriteAttributes(pITD->nAllMembers);
    for ( sal_Int32 i = pITD->nAllMembers; i--; )
    {
        aReadWriteAttributes[i] = false;
        if( typelib_TypeClass_INTERFACE_ATTRIBUTE == pITD->ppAllMembers[i]->eTypeClass )
        {
            typelib_TypeDescription * pM = nullptr;
            TYPELIB_DANGER_GET( &pM, pITD->ppAllMembers[i] );
            OSL_ASSERT( pM );
            if (pM)
            {
                aReadWriteAttributes[i] = !reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>(pM)->bReadOnly;
                TYPELIB_DANGER_RELEASE( pM );
            }
            else
            {
                SAL_INFO( "cppu.typelib", "cannot get attribute type description: " <<  pITD->ppAllMembers[i]->pTypeName );
            }
        }
    }

    MutexGuard aGuard( Init::get().getMutex() );
    if( !pTD->bComplete )
    {
        // create the index table from member to function table
        pITD->pMapMemberIndexToFunctionIndex = new sal_Int32[ pITD->nAllMembers ];
        sal_Int32 nAdditionalOffset = 0; // +1 for read/write attributes
        sal_Int32 i;
        for( i = 0; i < pITD->nAllMembers; i++ )
        {
            // index to the get method of the attribute
            pITD->pMapMemberIndexToFunctionIndex[i] = i + nAdditionalOffset;
            // extra offset if it is a read/write attribute?
            if (aReadWriteAttributes[i])
            {
                // a read/write attribute
                nAdditionalOffset++;
            }
        }

        // create the index table from function to member table
        pITD->pMapFunctionIndexToMemberIndex = new sal_Int32[ pITD->nAllMembers + nAdditionalOffset ];
        nAdditionalOffset = 0; // +1 for read/write attributes
        for( i = 0; i < pITD->nAllMembers; i++ )
        {
            // index to the get method of the attribute
            pITD->pMapFunctionIndexToMemberIndex[i + nAdditionalOffset] = i;
            // extra offset if it is a read/write attribute?
            if (aReadWriteAttributes[i])
            {
                // a read/write attribute
                pITD->pMapFunctionIndexToMemberIndex[i + ++nAdditionalOffset] = i;
            }
        }
        // must be the last action after all initialization is done
        pITD->nMapFunctionIndexToMemberIndex = pITD->nAllMembers + nAdditionalOffset;
        pTD->bComplete = true;
    }
}

namespace {

template<typename T> T * allocTypeDescription() {
    return reinterpret_cast<T *>(new char[sizeof (T)]);
}

void freeTypeDescription(typelib_TypeDescription const * desc) {
    delete[] reinterpret_cast<char const *>(desc);
}

// In some situations (notably typelib_typedescription_newInterfaceMethod and
// typelib_typedescription_newInterfaceAttribute), only the members nMembers,
// ppMembers, nAllMembers, and ppAllMembers of an incomplete interface type
// description are necessary, but not the additional
// pMapMemberIndexToFunctionIndex, nMapFunctionIndexToMemberIndex, and
// pMapFunctionIndexToMemberIndex (which are computed by
// typelib_typedescription_initTables).  Furthermore, in those situations, it
// might be illegal to compute those tables, as the creation of the interface
// member type descriptions would recursively require a complete interface type
// description.  The parameter initTables controls whether or not to call
// typelib_typedescription_initTables in those situations.
bool complete(typelib_TypeDescription ** ppTypeDescr, bool initTables) {
    if (! (*ppTypeDescr)->bComplete)
    {
        OSL_ASSERT( (typelib_TypeClass_STRUCT == (*ppTypeDescr)->eTypeClass ||
                     typelib_TypeClass_EXCEPTION == (*ppTypeDescr)->eTypeClass ||
                     typelib_TypeClass_ENUM == (*ppTypeDescr)->eTypeClass ||
                     typelib_TypeClass_INTERFACE == (*ppTypeDescr)->eTypeClass) &&
                    !reallyWeak( (*ppTypeDescr)->eTypeClass ) );

        if (typelib_TypeClass_INTERFACE == (*ppTypeDescr)->eTypeClass &&
            reinterpret_cast<typelib_InterfaceTypeDescription *>(*ppTypeDescr)->ppAllMembers)
        {
            if (initTables) {
                typelib_typedescription_initTables( *ppTypeDescr );
            }
            return true;
        }

        typelib_TypeDescription * pTD = nullptr;
        // on demand access of complete td
        TypeDescriptor_Init_Impl &rInit = Init::get();
        rInit.callChain( &pTD, (*ppTypeDescr)->pTypeName );
        if (pTD)
        {
            if (typelib_TypeClass_TYPEDEF == pTD->eTypeClass)
            {
                typelib_typedescriptionreference_getDescription(
                    &pTD, reinterpret_cast<typelib_IndirectTypeDescription *>(pTD)->pType );
                OSL_ASSERT( pTD );
                if (! pTD)
                    return false;
            }

            OSL_ASSERT( typelib_TypeClass_TYPEDEF != pTD->eTypeClass );
            // typedescription found
            // set to on demand
            pTD->bOnDemand = true;

            if (pTD->eTypeClass == typelib_TypeClass_INTERFACE
                && !pTD->bComplete && initTables)
            {
                // mandatory info from callback chain
                OSL_ASSERT( reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD)->ppAllMembers );
                // complete except of tables init
                typelib_typedescription_initTables( pTD );
                pTD->bComplete = true;
            }

            // The type description is hold by the reference until
            // on demand is activated.
            ::typelib_typedescription_register( &pTD ); // replaces incomplete one
            OSL_ASSERT( pTD == *ppTypeDescr ); // has to merge into existing one

            // insert into the cache
            MutexGuard aGuard( rInit.getMutex() );
            if( !rInit.pCache )
                rInit.pCache.reset( new TypeDescriptionList_Impl );
            if( static_cast<sal_Int32>(rInit.pCache->size()) >= nCacheSize )
            {
                typelib_typedescription_release( rInit.pCache->front() );
                rInit.pCache->pop_front();
            }
            // descriptions in the cache must be acquired!
            typelib_typedescription_acquire( pTD );
            rInit.pCache->push_back( pTD );

            OSL_ASSERT(
                pTD->bComplete
                || (pTD->eTypeClass == typelib_TypeClass_INTERFACE
                    && !initTables));

            ::typelib_typedescription_release( *ppTypeDescr );
            *ppTypeDescr = pTD;
        }
        else
        {
            SAL_INFO( "cppu.typelib", "type cannot be completed: " <<  (*ppTypeDescr)->pTypeName );
            return false;
        }
    }
    return true;
}

}


extern "C" void typelib_typedescription_newEmpty(
    typelib_TypeDescription ** ppRet,
    typelib_TypeClass eTypeClass, rtl_uString * pTypeName )
    SAL_THROW_EXTERN_C()
{
    if( *ppRet )
    {
        typelib_typedescription_release( *ppRet );
        *ppRet = nullptr;
    }

    OSL_ASSERT( typelib_TypeClass_TYPEDEF != eTypeClass );

    typelib_TypeDescription * pRet;
    switch( eTypeClass )
    {
        case typelib_TypeClass_SEQUENCE:
        {
            auto pTmp = allocTypeDescription<typelib_IndirectTypeDescription>();
            pRet = &pTmp->aBase;
#if OSL_DEBUG_LEVEL > 0
            osl_atomic_increment( &Init::get().nIndirectTypeDescriptionCount );
#endif
            pTmp->pType = nullptr;
            // coverity[leaked_storage] - this is on purpose
        }
        break;

        case typelib_TypeClass_STRUCT:
        {
            // FEATURE_EMPTYCLASS
            auto pTmp = allocTypeDescription<typelib_StructTypeDescription>();
            pRet = &pTmp->aBase.aBase;
#if OSL_DEBUG_LEVEL > 0
            osl_atomic_increment( &Init::get().nCompoundTypeDescriptionCount );
#endif
            pTmp->aBase.pBaseTypeDescription = nullptr;
            pTmp->aBase.nMembers = 0;
            pTmp->aBase.pMemberOffsets = nullptr;
            pTmp->aBase.ppTypeRefs = nullptr;
            pTmp->aBase.ppMemberNames = nullptr;
            pTmp->pParameterizedTypes = nullptr;
            // coverity[leaked_storage] - this is on purpose
        }
        break;

        case typelib_TypeClass_EXCEPTION:
        {
            // FEATURE_EMPTYCLASS
            auto pTmp = allocTypeDescription<typelib_CompoundTypeDescription>();
            pRet = &pTmp->aBase;
#if OSL_DEBUG_LEVEL > 0
            osl_atomic_increment( &Init::get().nCompoundTypeDescriptionCount );
#endif
            pTmp->pBaseTypeDescription = nullptr;
            pTmp->nMembers = 0;
            pTmp->pMemberOffsets = nullptr;
            pTmp->ppTypeRefs = nullptr;
            pTmp->ppMemberNames = nullptr;
            // coverity[leaked_storage] - this is on purpose
        }
        break;

        case typelib_TypeClass_ENUM:
        {
            auto pTmp = allocTypeDescription<typelib_EnumTypeDescription>();
            pRet = &pTmp->aBase;
#if OSL_DEBUG_LEVEL > 0
            osl_atomic_increment( &Init::get().nEnumTypeDescriptionCount );
#endif
            pTmp->nDefaultEnumValue = 0;
            pTmp->nEnumValues       = 0;
            pTmp->ppEnumNames       = nullptr;
            pTmp->pEnumValues       = nullptr;
            // coverity[leaked_storage] - this is on purpose
        }
        break;

        case typelib_TypeClass_INTERFACE:
        {
            auto pTmp = allocTypeDescription<
                typelib_InterfaceTypeDescription>();
            pRet = &pTmp->aBase;
#if OSL_DEBUG_LEVEL > 0
            osl_atomic_increment( &Init::get().nInterfaceTypeDescriptionCount );
#endif
            pTmp->pBaseTypeDescription = nullptr;
            pTmp->nMembers = 0;
            pTmp->ppMembers = nullptr;
            pTmp->nAllMembers = 0;
            pTmp->ppAllMembers = nullptr;
            pTmp->nMapFunctionIndexToMemberIndex = 0;
            pTmp->pMapFunctionIndexToMemberIndex = nullptr;
            pTmp->pMapMemberIndexToFunctionIndex= nullptr;
            pTmp->nBaseTypes = 0;
            pTmp->ppBaseTypes = nullptr;
            // coverity[leaked_storage] - this is on purpose
        }
        break;

        case typelib_TypeClass_INTERFACE_METHOD:
        {
            auto pTmp = allocTypeDescription<
                typelib_InterfaceMethodTypeDescription>();
            pRet = &pTmp->aBase.aBase;
#if OSL_DEBUG_LEVEL > 0
            osl_atomic_increment( &Init::get().nInterfaceMethodTypeDescriptionCount );
#endif
            pTmp->aBase.pMemberName = nullptr;
            pTmp->pReturnTypeRef = nullptr;
            pTmp->nParams = 0;
            pTmp->pParams = nullptr;
            pTmp->nExceptions = 0;
            pTmp->ppExceptions = nullptr;
            pTmp->pInterface = nullptr;
            pTmp->pBaseRef = nullptr;
            pTmp->nIndex = 0;
            // coverity[leaked_storage] - this is on purpose
        }
        break;

        case typelib_TypeClass_INTERFACE_ATTRIBUTE:
        {
            auto * pTmp = allocTypeDescription<
                typelib_InterfaceAttributeTypeDescription>();
            pRet = &pTmp->aBase.aBase;
#if OSL_DEBUG_LEVEL > 0
            osl_atomic_increment( &Init::get().nInterfaceAttributeTypeDescriptionCount );
#endif
            pTmp->aBase.pMemberName = nullptr;
            pTmp->pAttributeTypeRef = nullptr;
            pTmp->pInterface = nullptr;
            pTmp->pBaseRef = nullptr;
            pTmp->nIndex = 0;
            pTmp->nGetExceptions = 0;
            pTmp->ppGetExceptions = nullptr;
            pTmp->nSetExceptions = 0;
            pTmp->ppSetExceptions = nullptr;
            // coverity[leaked_storage] - this is on purpose
        }
        break;

        default:
        {
            pRet = allocTypeDescription<typelib_TypeDescription>();
#if OSL_DEBUG_LEVEL > 0
            osl_atomic_increment( &Init::get().nTypeDescriptionCount );
#endif
        }
    }

    pRet->nRefCount = 1; // reference count is initially 1
    pRet->nStaticRefCount = 0;
    pRet->eTypeClass = eTypeClass;
    pRet->pTypeName = nullptr;
    pRet->pUniqueIdentifier = nullptr;
    pRet->pReserved = nullptr;
    rtl_uString_acquire( pRet->pTypeName = pTypeName );
    pRet->pSelf = pRet;
    pRet->bComplete = true;
    pRet->nSize = 0;
    pRet->nAlignment = 0;
    pRet->pWeakRef = nullptr;
    pRet->bOnDemand = false;
    *ppRet = pRet;
}


namespace {

void newTypeDescription(
    typelib_TypeDescription ** ppRet, typelib_TypeClass eTypeClass,
    rtl_uString * pTypeName, typelib_TypeDescriptionReference * pType,
    sal_Int32 nMembers, typelib_CompoundMember_Init * pCompoundMembers,
    typelib_StructMember_Init * pStructMembers)
{
    OSL_ASSERT(
        (pCompoundMembers == nullptr || pStructMembers == nullptr)
        && (pStructMembers == nullptr || eTypeClass == typelib_TypeClass_STRUCT));
    if (typelib_TypeClass_TYPEDEF == eTypeClass)
    {
        SAL_WARN("cppu.typelib", "unexpected typedef!" );
        typelib_typedescriptionreference_getDescription( ppRet, pType );
        return;
    }

    typelib_typedescription_newEmpty( ppRet, eTypeClass, pTypeName );

    switch( eTypeClass )
    {
        case typelib_TypeClass_SEQUENCE:
        {
            OSL_ASSERT( nMembers == 0 );
            typelib_typedescriptionreference_acquire( pType );
            reinterpret_cast<typelib_IndirectTypeDescription *>(*ppRet)->pType = pType;
        }
        break;

        case typelib_TypeClass_EXCEPTION:
        case typelib_TypeClass_STRUCT:
        {
            // FEATURE_EMPTYCLASS
            typelib_CompoundTypeDescription * pTmp = reinterpret_cast<typelib_CompoundTypeDescription*>(*ppRet);

            sal_Int32 nOffset = 0;
            if( pType )
            {
                typelib_typedescriptionreference_getDescription(
                    reinterpret_cast<typelib_TypeDescription **>(&pTmp->pBaseTypeDescription), pType );
                nOffset = pTmp->pBaseTypeDescription->aBase.nSize;
                OSL_ENSURE( newAlignedSize( 0, pTmp->pBaseTypeDescription->aBase.nSize, pTmp->pBaseTypeDescription->aBase.nAlignment ) == pTmp->pBaseTypeDescription->aBase.nSize, "### unexpected offset!" );
            }
            if( nMembers )
            {
                pTmp->nMembers = nMembers;
                pTmp->pMemberOffsets = new sal_Int32[ nMembers ];
                pTmp->ppTypeRefs = new typelib_TypeDescriptionReference *[ nMembers ];
                pTmp->ppMemberNames = new rtl_uString *[ nMembers ];
                bool polymorphic = eTypeClass == typelib_TypeClass_STRUCT
                    && OUString::unacquired(&pTypeName).indexOf('<') >= 0;
                OSL_ASSERT(!polymorphic || pStructMembers != nullptr);
                if (polymorphic) {
                    reinterpret_cast< typelib_StructTypeDescription * >(pTmp)->
                        pParameterizedTypes = new sal_Bool[nMembers];
                }
                for( sal_Int32 i = 0 ; i < nMembers; i++ )
                {
                    // read the type and member names
                    pTmp->ppTypeRefs[i] = nullptr;
                    if (pCompoundMembers != nullptr) {
                        typelib_typedescriptionreference_new(
                            pTmp->ppTypeRefs +i, pCompoundMembers[i].eTypeClass,
                            pCompoundMembers[i].pTypeName );
                        rtl_uString_acquire(
                            pTmp->ppMemberNames[i]
                            = pCompoundMembers[i].pMemberName );
                    } else {
                        typelib_typedescriptionreference_new(
                            pTmp->ppTypeRefs +i,
                            pStructMembers[i].aBase.eTypeClass,
                            pStructMembers[i].aBase.pTypeName );
                        rtl_uString_acquire(
                            pTmp->ppMemberNames[i]
                            = pStructMembers[i].aBase.pMemberName );
                    }
                    // write offset
                    sal_Int32 size;
                    sal_Int32 alignment;
                    if (pTmp->ppTypeRefs[i]->eTypeClass ==
                        typelib_TypeClass_SEQUENCE)
                    {
                        // Take care of recursion like
                        // struct S { sequence<S> x; };
                        size = sizeof(void *);
                        alignment = adjustAlignment(size);
                    } else {
                        typelib_TypeDescription * pTD = nullptr;
                        TYPELIB_DANGER_GET( &pTD, pTmp->ppTypeRefs[i] );
                        OSL_ENSURE( pTD->nSize, "### void member?" );
                        size = pTD->nSize;
                        alignment = pTD->nAlignment;
                        TYPELIB_DANGER_RELEASE( pTD );
                    }
                    nOffset = newAlignedSize( nOffset, size, alignment );
                    pTmp->pMemberOffsets[i] = nOffset - size;

                    if (polymorphic) {
                        reinterpret_cast< typelib_StructTypeDescription * >(
                            pTmp)->pParameterizedTypes[i]
                            = pStructMembers[i].bParameterizedType;
                    }
                }
            }
        }
        break;

        default:
        break;
    }

    if( !reallyWeak( eTypeClass ) )
        (*ppRet)->pWeakRef = reinterpret_cast<typelib_TypeDescriptionReference *>(*ppRet);
    if( eTypeClass != typelib_TypeClass_VOID )
    {
        // sizeof(void) not allowed
        (*ppRet)->nSize = typelib_typedescription_getAlignedUnoSize( (*ppRet), 0, (*ppRet)->nAlignment );
        (*ppRet)->nAlignment = adjustAlignment( (*ppRet)->nAlignment );
    }
}

}

extern "C" void SAL_CALL typelib_typedescription_new(
    typelib_TypeDescription ** ppRet,
    typelib_TypeClass eTypeClass,
    rtl_uString * pTypeName,
    typelib_TypeDescriptionReference * pType,
    sal_Int32 nMembers,
    typelib_CompoundMember_Init * pMembers )
    SAL_THROW_EXTERN_C()
{
    newTypeDescription(
        ppRet, eTypeClass, pTypeName, pType, nMembers, pMembers, nullptr);
}

extern "C" void SAL_CALL typelib_typedescription_newStruct(
    typelib_TypeDescription ** ppRet,
    rtl_uString * pTypeName,
    typelib_TypeDescriptionReference * pType,
    sal_Int32 nMembers,
    typelib_StructMember_Init * pMembers )
    SAL_THROW_EXTERN_C()
{
    newTypeDescription(
        ppRet, typelib_TypeClass_STRUCT, pTypeName, pType, nMembers, nullptr,
        pMembers);
}


extern "C" void SAL_CALL typelib_typedescription_newEnum(
    typelib_TypeDescription ** ppRet,
    rtl_uString * pTypeName,
    sal_Int32 nDefaultValue,
    sal_Int32 nEnumValues,
    rtl_uString ** ppEnumNames,
    sal_Int32 * pEnumValues )
    SAL_THROW_EXTERN_C()
{
    typelib_typedescription_newEmpty( ppRet, typelib_TypeClass_ENUM, pTypeName );
    typelib_EnumTypeDescription * pEnum = reinterpret_cast<typelib_EnumTypeDescription *>(*ppRet);

    pEnum->nDefaultEnumValue = nDefaultValue;
    pEnum->nEnumValues       = nEnumValues;
    pEnum->ppEnumNames       = new rtl_uString * [ nEnumValues ];
    for ( sal_Int32 nPos = nEnumValues; nPos--; )
    {
        rtl_uString_acquire( pEnum->ppEnumNames[nPos] = ppEnumNames[nPos] );
    }
    pEnum->pEnumValues      = new sal_Int32[ nEnumValues ];
    ::memcpy( pEnum->pEnumValues, pEnumValues, nEnumValues * sizeof(sal_Int32) );

    (*ppRet)->pWeakRef = reinterpret_cast<typelib_TypeDescriptionReference *>(*ppRet);
    // sizeof(void) not allowed
    (*ppRet)->nSize = typelib_typedescription_getAlignedUnoSize( (*ppRet), 0, (*ppRet)->nAlignment );
    (*ppRet)->nAlignment = adjustAlignment( (*ppRet)->nAlignment );
}


extern "C" void SAL_CALL typelib_typedescription_newInterface(
    typelib_InterfaceTypeDescription ** ppRet,
    rtl_uString * pTypeName,
    SAL_UNUSED_PARAMETER sal_uInt32, SAL_UNUSED_PARAMETER sal_uInt16,
    SAL_UNUSED_PARAMETER sal_uInt16, SAL_UNUSED_PARAMETER sal_uInt32,
    SAL_UNUSED_PARAMETER sal_uInt32,
    typelib_TypeDescriptionReference * pBaseInterface,
    sal_Int32 nMembers,
    typelib_TypeDescriptionReference ** ppMembers )
    SAL_THROW_EXTERN_C()
{
    // coverity[callee_ptr_arith] - not a bug
    typelib_typedescription_newMIInterface(
        ppRet, pTypeName, 0, 0, 0, 0, 0, pBaseInterface == nullptr ? 0 : 1,
        &pBaseInterface, nMembers, ppMembers);
}

namespace {

class BaseList {
public:
    struct Entry {
        sal_Int32 memberOffset;
        sal_Int32 directBaseIndex;
        sal_Int32 directBaseMemberOffset;
        typelib_InterfaceTypeDescription const * base;
    };

    typedef std::vector< Entry > List;

    explicit BaseList(typelib_InterfaceTypeDescription const * desc);

    List const & getList() const { return list; }

    sal_Int32 getBaseMembers() const { return members; }

private:
    typedef std::set< OUString > Set;

    void calculate(
        sal_Int32 directBaseIndex, Set & directBaseSet,
        sal_Int32 * directBaseMembers,
        typelib_InterfaceTypeDescription const * desc);

    Set set;
    List list;
    sal_Int32 members;
};

BaseList::BaseList(typelib_InterfaceTypeDescription const * desc) {
    members = 0;
    for (sal_Int32 i = 0; i < desc->nBaseTypes; ++i) {
        Set directBaseSet;
        sal_Int32 directBaseMembers = 0;
        calculate(i, directBaseSet, &directBaseMembers, desc->ppBaseTypes[i]);
    }
}

void BaseList::calculate(
    sal_Int32 directBaseIndex, Set & directBaseSet,
    sal_Int32 * directBaseMembers,
    typelib_InterfaceTypeDescription const * desc)
{
    for (sal_Int32 i = 0; i < desc->nBaseTypes; ++i) {
        calculate(
            directBaseIndex, directBaseSet, directBaseMembers,
            desc->ppBaseTypes[i]);
    }
    if (set.insert(desc->aBase.pTypeName).second) {
        Entry e;
        e.memberOffset = members;
        e.directBaseIndex = directBaseIndex;
        e.directBaseMemberOffset = *directBaseMembers;
        e.base = desc;
        list.push_back(e);
        OSL_ASSERT(desc->ppAllMembers != nullptr);
        members += desc->nMembers;
    }
    if (directBaseSet.insert(desc->aBase.pTypeName).second) {
        OSL_ASSERT(desc->ppAllMembers != nullptr);
        *directBaseMembers += desc->nMembers;
    }
}

}

extern "C" void SAL_CALL typelib_typedescription_newMIInterface(
    typelib_InterfaceTypeDescription ** ppRet,
    rtl_uString * pTypeName,
    SAL_UNUSED_PARAMETER sal_uInt32, SAL_UNUSED_PARAMETER sal_uInt16,
    SAL_UNUSED_PARAMETER sal_uInt16, SAL_UNUSED_PARAMETER sal_uInt32,
    SAL_UNUSED_PARAMETER sal_uInt32,
    sal_Int32 nBaseInterfaces,
    typelib_TypeDescriptionReference ** ppBaseInterfaces,
    sal_Int32 nMembers,
    typelib_TypeDescriptionReference ** ppMembers )
    SAL_THROW_EXTERN_C()
{
    if (*ppRet != nullptr) {
        typelib_typedescription_release(&(*ppRet)->aBase);
        *ppRet = nullptr;
    }

    typelib_InterfaceTypeDescription * pITD = nullptr;
    typelib_typedescription_newEmpty(
        reinterpret_cast<typelib_TypeDescription **>(&pITD), typelib_TypeClass_INTERFACE, pTypeName );

    pITD->nBaseTypes = nBaseInterfaces;
    pITD->ppBaseTypes = new typelib_InterfaceTypeDescription *[nBaseInterfaces];
    for (sal_Int32 i = 0; i < nBaseInterfaces; ++i) {
        pITD->ppBaseTypes[i] = nullptr;
        typelib_typedescriptionreference_getDescription(
            reinterpret_cast< typelib_TypeDescription ** >(
                &pITD->ppBaseTypes[i]),
            ppBaseInterfaces[i]);
        if (pITD->ppBaseTypes[i] == nullptr
            || !complete(
                reinterpret_cast< typelib_TypeDescription ** >(
                    &pITD->ppBaseTypes[i]),
                false))
        {
            OSL_ASSERT(false);
            return;
        }
        OSL_ASSERT(pITD->ppBaseTypes[i] != nullptr);
    }
    if (nBaseInterfaces > 0) {
        pITD->pBaseTypeDescription = pITD->ppBaseTypes[0];
    }
    // set the
    pITD->aUik.m_Data1 = 0;
    pITD->aUik.m_Data2 = 0;
    pITD->aUik.m_Data3 = 0;
    pITD->aUik.m_Data4 = 0;
    pITD->aUik.m_Data5 = 0;

    BaseList aBaseList(pITD);
    pITD->nAllMembers = nMembers + aBaseList.getBaseMembers();
    pITD->nMembers = nMembers;

    if( pITD->nAllMembers )
    {
        // at minimum one member exist, allocate the memory
        pITD->ppAllMembers = new typelib_TypeDescriptionReference *[ pITD->nAllMembers ];
        sal_Int32 n = 0;

        BaseList::List const & rList = aBaseList.getList();
        for (const auto& rEntry : rList)
        {
            typelib_InterfaceTypeDescription const * pBase = rEntry.base;
            typelib_InterfaceTypeDescription const * pDirectBase
                = pITD->ppBaseTypes[rEntry.directBaseIndex];
            OSL_ASSERT(pBase->ppAllMembers != nullptr);
            for (sal_Int32 j = 0; j < pBase->nMembers; ++j) {
                typelib_TypeDescriptionReference const * pDirectBaseMember
                    = pDirectBase->ppAllMembers[rEntry.directBaseMemberOffset + j];
                OUStringBuffer aBuf(pDirectBaseMember->pTypeName);
                aBuf.append(":@");
                aBuf.append(rEntry.directBaseIndex);
                aBuf.append(',');
                aBuf.append(rEntry.memberOffset + j);
                aBuf.append(':');
                aBuf.append(pITD->aBase.pTypeName);
                OUString aName(aBuf.makeStringAndClear());
                typelib_TypeDescriptionReference * pDerivedMember = nullptr;
                typelib_typedescriptionreference_new(
                    &pDerivedMember, pDirectBaseMember->eTypeClass,
                    aName.pData);
                pITD->ppAllMembers[n++] = pDerivedMember;
            }
        }

        if( nMembers )
        {
            pITD->ppMembers = pITD->ppAllMembers + aBaseList.getBaseMembers();
        }

        // add own members
        for( sal_Int32 i = 0; i < nMembers; i++ )
        {
            typelib_typedescriptionreference_acquire( ppMembers[i] );
            pITD->ppAllMembers[n++] = ppMembers[i];
        }
    }

    typelib_TypeDescription * pTmp = &pITD->aBase;
    if( !reallyWeak( typelib_TypeClass_INTERFACE ) )
        pTmp->pWeakRef = reinterpret_cast<typelib_TypeDescriptionReference *>(pTmp);
    pTmp->nSize = typelib_typedescription_getAlignedUnoSize( pTmp, 0, pTmp->nAlignment );
    pTmp->nAlignment = adjustAlignment( pTmp->nAlignment );
    pTmp->bComplete = false;

    *ppRet = pITD;
}


namespace {

typelib_TypeDescriptionReference ** copyExceptions(
    sal_Int32 count, rtl_uString ** typeNames)
{
    OSL_ASSERT(count >= 0);
    if (count == 0) {
        return nullptr;
    }
    typelib_TypeDescriptionReference ** p
        = new typelib_TypeDescriptionReference *[count];
    for (sal_Int32 i = 0; i < count; ++i) {
        p[i] = nullptr;
        typelib_typedescriptionreference_new(
            p + i, typelib_TypeClass_EXCEPTION, typeNames[i]);
    }
    return p;
}

}

extern "C" void SAL_CALL typelib_typedescription_newInterfaceMethod(
    typelib_InterfaceMethodTypeDescription ** ppRet,
    sal_Int32 nAbsolutePosition,
    sal_Bool bOneWay,
    rtl_uString * pTypeName,
    typelib_TypeClass eReturnTypeClass,
    rtl_uString * pReturnTypeName,
    sal_Int32 nParams,
    typelib_Parameter_Init * pParams,
    sal_Int32 nExceptions,
    rtl_uString ** ppExceptionNames )
    SAL_THROW_EXTERN_C()
{
    if (*ppRet != nullptr) {
        typelib_typedescription_release(&(*ppRet)->aBase.aBase);
        *ppRet = nullptr;
    }
    sal_Int32 nOffset = rtl_ustr_lastIndexOfChar_WithLength(
        pTypeName->buffer, pTypeName->length, ':');
    if (nOffset <= 0 || pTypeName->buffer[nOffset - 1] != ':') {
        OSL_FAIL("Bad interface method type name");
        return;
    }
    OUString aInterfaceTypeName(pTypeName->buffer, nOffset - 1);
    typelib_InterfaceTypeDescription * pInterface = nullptr;
    typelib_typedescription_getByName(
        reinterpret_cast< typelib_TypeDescription ** >(&pInterface),
        aInterfaceTypeName.pData);
    if (pInterface == nullptr
        || pInterface->aBase.eTypeClass != typelib_TypeClass_INTERFACE
        || !complete(
            reinterpret_cast< typelib_TypeDescription ** >(&pInterface), false))
    {
        OSL_FAIL("No interface corresponding to interface method");
        return;
    }

    typelib_typedescription_newEmpty(
        reinterpret_cast<typelib_TypeDescription **>(ppRet), typelib_TypeClass_INTERFACE_METHOD, pTypeName );
    typelib_TypeDescription * pTmp = reinterpret_cast<typelib_TypeDescription *>(*ppRet);

    rtl_uString_newFromStr_WithLength( &(*ppRet)->aBase.pMemberName,
                                       pTypeName->buffer + nOffset +1,
                                       pTypeName->length - nOffset -1 );
    (*ppRet)->aBase.nPosition = nAbsolutePosition;
    (*ppRet)->bOneWay = bOneWay;
    typelib_typedescriptionreference_new( &(*ppRet)->pReturnTypeRef, eReturnTypeClass, pReturnTypeName );
    (*ppRet)->nParams = nParams;
    if( nParams )
    {
        (*ppRet)->pParams = new typelib_MethodParameter[ nParams ];

        for( sal_Int32 i = 0; i < nParams; i++ )
        {
            // get the name of the parameter
            (*ppRet)->pParams[ i ].pName = nullptr;
            rtl_uString_acquire( (*ppRet)->pParams[ i ].pName = pParams[i].pParamName );
            (*ppRet)->pParams[ i ].pTypeRef = nullptr;
            // get the type name of the parameter and create the weak reference
            typelib_typedescriptionreference_new(
                &(*ppRet)->pParams[ i ].pTypeRef, pParams[i].eTypeClass, pParams[i].pTypeName );
            (*ppRet)->pParams[ i ].bIn = pParams[i].bIn;
            (*ppRet)->pParams[ i ].bOut = pParams[i].bOut;
        }
    }
    (*ppRet)->nExceptions = nExceptions;
    (*ppRet)->ppExceptions = copyExceptions(nExceptions, ppExceptionNames);
    (*ppRet)->pInterface = pInterface;
    (*ppRet)->pBaseRef = nullptr;
    OSL_ASSERT(
        (nAbsolutePosition >= pInterface->nAllMembers - pInterface->nMembers)
        && nAbsolutePosition < pInterface->nAllMembers);
    (*ppRet)->nIndex = nAbsolutePosition
        - (pInterface->nAllMembers - pInterface->nMembers);
    if( !reallyWeak( typelib_TypeClass_INTERFACE_METHOD ) )
        pTmp->pWeakRef = reinterpret_cast<typelib_TypeDescriptionReference *>(pTmp);
}


extern "C" void SAL_CALL typelib_typedescription_newInterfaceAttribute(<--- The function 'typelib_typedescription_newInterfaceAttribute' is never used.
    typelib_InterfaceAttributeTypeDescription ** ppRet,
    sal_Int32 nAbsolutePosition,
    rtl_uString * pTypeName,
    typelib_TypeClass eAttributeTypeClass,
    rtl_uString * pAttributeTypeName,
    sal_Bool bReadOnly )
    SAL_THROW_EXTERN_C()
{
    typelib_typedescription_newExtendedInterfaceAttribute(
        ppRet, nAbsolutePosition, pTypeName, eAttributeTypeClass,
        pAttributeTypeName, bReadOnly, 0, nullptr, 0, nullptr);
}


extern "C" void SAL_CALL typelib_typedescription_newExtendedInterfaceAttribute(
    typelib_InterfaceAttributeTypeDescription ** ppRet,
    sal_Int32 nAbsolutePosition,
    rtl_uString * pTypeName,
    typelib_TypeClass eAttributeTypeClass,
    rtl_uString * pAttributeTypeName,
    sal_Bool bReadOnly,
    sal_Int32 nGetExceptions, rtl_uString ** ppGetExceptionNames,
    sal_Int32 nSetExceptions, rtl_uString ** ppSetExceptionNames )
    SAL_THROW_EXTERN_C()
{
    if (*ppRet != nullptr) {
        typelib_typedescription_release(&(*ppRet)->aBase.aBase);
        *ppRet = nullptr;
    }
    sal_Int32 nOffset = rtl_ustr_lastIndexOfChar_WithLength(
        pTypeName->buffer, pTypeName->length, ':');
    if (nOffset <= 0 || pTypeName->buffer[nOffset - 1] != ':') {
        OSL_FAIL("Bad interface attribute type name");
        return;
    }
    OUString aInterfaceTypeName(pTypeName->buffer, nOffset - 1);
    typelib_InterfaceTypeDescription * pInterface = nullptr;
    typelib_typedescription_getByName(
        reinterpret_cast< typelib_TypeDescription ** >(&pInterface),
        aInterfaceTypeName.pData);
    if (pInterface == nullptr
        || pInterface->aBase.eTypeClass != typelib_TypeClass_INTERFACE
        || !complete(
            reinterpret_cast< typelib_TypeDescription ** >(&pInterface), false))
    {
        OSL_FAIL("No interface corresponding to interface attribute");
        return;
    }

    typelib_typedescription_newEmpty(
        reinterpret_cast<typelib_TypeDescription **>(ppRet), typelib_TypeClass_INTERFACE_ATTRIBUTE, pTypeName );
    typelib_TypeDescription * pTmp = reinterpret_cast<typelib_TypeDescription *>(*ppRet);

    rtl_uString_newFromStr_WithLength( &(*ppRet)->aBase.pMemberName,
                                       pTypeName->buffer + nOffset +1,
                                       pTypeName->length - nOffset -1 );
    (*ppRet)->aBase.nPosition = nAbsolutePosition;
    typelib_typedescriptionreference_new( &(*ppRet)->pAttributeTypeRef, eAttributeTypeClass, pAttributeTypeName );
    (*ppRet)->bReadOnly = bReadOnly;
    (*ppRet)->pInterface = pInterface;
    (*ppRet)->pBaseRef = nullptr;
    OSL_ASSERT(
        (nAbsolutePosition >= pInterface->nAllMembers - pInterface->nMembers)
        && nAbsolutePosition < pInterface->nAllMembers);
    (*ppRet)->nIndex = nAbsolutePosition
        - (pInterface->nAllMembers - pInterface->nMembers);
    (*ppRet)->nGetExceptions = nGetExceptions;
    (*ppRet)->ppGetExceptions = copyExceptions(
        nGetExceptions, ppGetExceptionNames);
    (*ppRet)->nSetExceptions = nSetExceptions;
    (*ppRet)->ppSetExceptions = copyExceptions(
        nSetExceptions, ppSetExceptionNames);
    if( !reallyWeak( typelib_TypeClass_INTERFACE_ATTRIBUTE ) )
        pTmp->pWeakRef = reinterpret_cast<typelib_TypeDescriptionReference *>(pTmp);
}


extern "C" void SAL_CALL typelib_typedescription_acquire(
    typelib_TypeDescription * pTypeDescription )
    SAL_THROW_EXTERN_C()
{
    osl_atomic_increment( &pTypeDescription->nRefCount );
}


namespace {

void deleteExceptions(
    sal_Int32 count, typelib_TypeDescriptionReference ** exceptions)
{
    for (sal_Int32 i = 0; i < count; ++i) {
        typelib_typedescriptionreference_release(exceptions[i]);
    }
    delete[] exceptions;
}

}

// frees anything except typelib_TypeDescription base!
static void typelib_typedescription_destructExtendedMembers(
    typelib_TypeDescription * pTD )
{
    OSL_ASSERT( typelib_TypeClass_TYPEDEF != pTD->eTypeClass );

    switch( pTD->eTypeClass )
    {
    case typelib_TypeClass_SEQUENCE:
        if( reinterpret_cast<typelib_IndirectTypeDescription*>(pTD)->pType )
            typelib_typedescriptionreference_release( reinterpret_cast<typelib_IndirectTypeDescription*>(pTD)->pType );
        break;
    case typelib_TypeClass_STRUCT:
        delete[] reinterpret_cast< typelib_StructTypeDescription * >(pTD)->
            pParameterizedTypes;
        [[fallthrough]];
    case typelib_TypeClass_EXCEPTION:
    {
        typelib_CompoundTypeDescription * pCTD = reinterpret_cast<typelib_CompoundTypeDescription*>(pTD);
        if( pCTD->pBaseTypeDescription )
            typelib_typedescription_release( &pCTD->pBaseTypeDescription->aBase );
        sal_Int32 i;
        for( i = 0; i < pCTD->nMembers; i++ )
        {
            typelib_typedescriptionreference_release( pCTD->ppTypeRefs[i] );
        }
        if (pCTD->ppMemberNames)
        {
            for ( i = 0; i < pCTD->nMembers; i++ )
            {
                rtl_uString_release( pCTD->ppMemberNames[i] );
            }
            delete [] pCTD->ppMemberNames;
        }
        delete [] pCTD->ppTypeRefs;
        delete [] pCTD->pMemberOffsets;
    }
    break;
    case typelib_TypeClass_INTERFACE:
    {
        typelib_InterfaceTypeDescription * pITD = reinterpret_cast<typelib_InterfaceTypeDescription*>(pTD);
        for( sal_Int32 i = 0; i < pITD->nAllMembers; i++ )
        {
            typelib_typedescriptionreference_release( pITD->ppAllMembers[i] );
        }
        delete [] pITD->ppAllMembers;
        delete [] pITD->pMapMemberIndexToFunctionIndex;
        delete [] pITD->pMapFunctionIndexToMemberIndex;
        for (sal_Int32 i = 0; i < pITD->nBaseTypes; ++i) {
            typelib_typedescription_release(
                reinterpret_cast< typelib_TypeDescription * >(
                    pITD->ppBaseTypes[i]));
        }
        delete[] pITD->ppBaseTypes;
        break;
    }
    case typelib_TypeClass_INTERFACE_METHOD:
    {
        typelib_InterfaceMethodTypeDescription * pIMTD = reinterpret_cast<typelib_InterfaceMethodTypeDescription*>(pTD);
        if( pIMTD->pReturnTypeRef )
            typelib_typedescriptionreference_release( pIMTD->pReturnTypeRef );
        for( sal_Int32 i = 0; i < pIMTD->nParams; i++ )
        {
            rtl_uString_release( pIMTD->pParams[ i ].pName );
            typelib_typedescriptionreference_release( pIMTD->pParams[ i ].pTypeRef );
        }
        delete [] pIMTD->pParams;
        deleteExceptions(pIMTD->nExceptions, pIMTD->ppExceptions);
        rtl_uString_release( pIMTD->aBase.pMemberName );
        typelib_typedescription_release(&pIMTD->pInterface->aBase);
        if (pIMTD->pBaseRef != nullptr) {
            typelib_typedescriptionreference_release(pIMTD->pBaseRef);
        }
    }
    break;
    case typelib_TypeClass_INTERFACE_ATTRIBUTE:
    {
        typelib_InterfaceAttributeTypeDescription * pIATD = reinterpret_cast<typelib_InterfaceAttributeTypeDescription*>(pTD);
        deleteExceptions(pIATD->nGetExceptions, pIATD->ppGetExceptions);
        deleteExceptions(pIATD->nSetExceptions, pIATD->ppSetExceptions);
        if( pIATD->pAttributeTypeRef )
            typelib_typedescriptionreference_release( pIATD->pAttributeTypeRef );
        if( pIATD->aBase.pMemberName )
            rtl_uString_release( pIATD->aBase.pMemberName );
        typelib_typedescription_release(&pIATD->pInterface->aBase);
        if (pIATD->pBaseRef != nullptr) {
            typelib_typedescriptionreference_release(pIATD->pBaseRef);
        }
    }
    break;
    case typelib_TypeClass_ENUM:
    {
        typelib_EnumTypeDescription * pEnum = reinterpret_cast<typelib_EnumTypeDescription *>(pTD);
        for ( sal_Int32 nPos = pEnum->nEnumValues; nPos--; )
        {
            rtl_uString_release( pEnum->ppEnumNames[nPos] );
        }
        delete [] pEnum->ppEnumNames;
        delete [] pEnum->pEnumValues;
    }
    break;
    default:
    break;
    }
}


extern "C" void SAL_CALL typelib_typedescription_release(
    typelib_TypeDescription * pTD )
    SAL_THROW_EXTERN_C()
{
    sal_Int32 ref = osl_atomic_decrement( &pTD->nRefCount );
    OSL_ASSERT(ref >= 0);
    if (0 == ref)
    {
        TypeDescriptor_Init_Impl &rInit = Init::get();
        if( reallyWeak( pTD->eTypeClass ) )
        {
            if( pTD->pWeakRef )
            {
                {
                MutexGuard aGuard( rInit.getMutex() );
                // remove this description from the weak reference
                pTD->pWeakRef->pType = nullptr;
                }
                typelib_typedescriptionreference_release( pTD->pWeakRef );
            }
        }
        else
        {
            // this description is a reference too, so remove it from the hash table
            if( rInit.pWeakMap )
            {
                MutexGuard aGuard( rInit.getMutex() );
                WeakMap_Impl::iterator aIt = rInit.pWeakMap->find( pTD->pTypeName->buffer );
                if( aIt != rInit.pWeakMap->end() && static_cast<void *>((*aIt).second) == static_cast<void *>(pTD) )
                {
                    // remove only if it contains the same object
                    rInit.pWeakMap->erase( aIt );
                }
            }
        }

        typelib_typedescription_destructExtendedMembers( pTD );
        rtl_uString_release( pTD->pTypeName );

#if OSL_DEBUG_LEVEL > 0
        switch( pTD->eTypeClass )
        {
        case typelib_TypeClass_SEQUENCE:
            osl_atomic_decrement( &rInit.nIndirectTypeDescriptionCount );
            break;
        case typelib_TypeClass_STRUCT:
        case typelib_TypeClass_EXCEPTION:
            osl_atomic_decrement( &rInit.nCompoundTypeDescriptionCount );
            break;
        case typelib_TypeClass_INTERFACE:
            osl_atomic_decrement( &rInit.nInterfaceTypeDescriptionCount );
            break;
        case typelib_TypeClass_INTERFACE_METHOD:
            osl_atomic_decrement( &rInit.nInterfaceMethodTypeDescriptionCount );
            break;
        case typelib_TypeClass_INTERFACE_ATTRIBUTE:
            osl_atomic_decrement( &rInit.nInterfaceAttributeTypeDescriptionCount );
            break;
        case typelib_TypeClass_ENUM:
            osl_atomic_decrement( &rInit.nEnumTypeDescriptionCount );
            break;
        default:
            osl_atomic_decrement( &rInit.nTypeDescriptionCount );
        }
#endif

        freeTypeDescription(pTD);
    }
}


extern "C" void SAL_CALL typelib_typedescription_register(
    typelib_TypeDescription ** ppNewDescription )
    SAL_THROW_EXTERN_C()
{
    // connect the description with the weak reference
    TypeDescriptor_Init_Impl &rInit = Init::get();
    ClearableMutexGuard aGuard( rInit.getMutex() );

    typelib_TypeDescriptionReference * pTDR = nullptr;
    typelib_typedescriptionreference_getByName( &pTDR, (*ppNewDescription)->pTypeName );

    OSL_ASSERT( (*ppNewDescription)->pWeakRef || reallyWeak( (*ppNewDescription)->eTypeClass ) );
    if( pTDR )
    {
        OSL_ASSERT( (*ppNewDescription)->eTypeClass == pTDR->eTypeClass );
        if( pTDR->pType )
        {
            if (reallyWeak( pTDR->eTypeClass ))
            {
                // pRef->pType->pWeakRef == 0 means that the description is empty
                if (pTDR->pType->pWeakRef)
                {
                    if (osl_atomic_increment( &pTDR->pType->nRefCount ) > 1)
                    {
                        // The reference is incremented. The object cannot be destroyed.
                        // Release the guard at the earliest point.
                        aGuard.clear();
                        ::typelib_typedescription_release( *ppNewDescription );
                        *ppNewDescription = pTDR->pType;
                        ::typelib_typedescriptionreference_release( pTDR );
                        return;
                    }
                    // destruction of this type in progress (another thread!)
                    (void)osl_atomic_decrement( &pTDR->pType->nRefCount );
                }
                // take new descr
                pTDR->pType = *ppNewDescription;
                OSL_ASSERT( ! (*ppNewDescription)->pWeakRef );
                (*ppNewDescription)->pWeakRef = pTDR;
                return;
            }
            // !reallyWeak

            if ((static_cast<void *>(pTDR) != static_cast<void *>(*ppNewDescription)) && // if different
                (!pTDR->pType->pWeakRef || // uninit: ref data only set
                 // new one is complete:
                 (!pTDR->pType->bComplete && (*ppNewDescription)->bComplete) ||
                 // new one may be partly initialized interface (except of tables):
                 (typelib_TypeClass_INTERFACE == pTDR->pType->eTypeClass &&
                  !reinterpret_cast<typelib_InterfaceTypeDescription *>(pTDR->pType)->ppAllMembers &&
                  (*reinterpret_cast<typelib_InterfaceTypeDescription **>(ppNewDescription))->ppAllMembers)))
            {
                // uninitialized or incomplete

                if (pTDR->pType->pWeakRef) // if init
                {
                    typelib_typedescription_destructExtendedMembers( pTDR->pType );
                }

                // pTDR->pType->pWeakRef == 0 means that the description is empty
                // description is not weak and the not the same
                sal_Int32 nSize = getDescriptionSize( (*ppNewDescription)->eTypeClass );

                // copy all specific data for the descriptions
                memcpy(
                    pTDR->pType +1,
                    *ppNewDescription +1,
                    nSize - sizeof(typelib_TypeDescription) );

                pTDR->pType->bComplete = (*ppNewDescription)->bComplete;
                pTDR->pType->nSize = (*ppNewDescription)->nSize;
                pTDR->pType->nAlignment = (*ppNewDescription)->nAlignment;

                memset(
                    *ppNewDescription +1,
                    0,
                    nSize - sizeof( typelib_TypeDescription ) );

                if( pTDR->pType->bOnDemand && !(*ppNewDescription)->bOnDemand )
                {
                    // switch from OnDemand to !OnDemand, so the description must be acquired
                    typelib_typedescription_acquire( pTDR->pType );
                }
                else if( !pTDR->pType->bOnDemand && (*ppNewDescription)->bOnDemand )
                {
                    // switch from !OnDemand to OnDemand, so the description must be released
                    assert(pTDR->pType->nRefCount > 1);
                    // coverity[freed_arg] - pType's nRefCount is > 1 here
                    typelib_typedescription_release( pTDR->pType );
                }

                pTDR->pType->bOnDemand = (*ppNewDescription)->bOnDemand;
                // initialized
                pTDR->pType->pWeakRef = pTDR;
            }

            typelib_typedescription_release( *ppNewDescription );
            // pTDR was acquired by getByName(), so it must not be acquired again
            *ppNewDescription = pTDR->pType;
            return;
        }
    }
    else if( reallyWeak( (*ppNewDescription)->eTypeClass) )
    {
        typelib_typedescriptionreference_new(
            &pTDR, (*ppNewDescription)->eTypeClass, (*ppNewDescription)->pTypeName );
    }
    else
    {
        pTDR = reinterpret_cast<typelib_TypeDescriptionReference *>(*ppNewDescription);
        if( !rInit.pWeakMap )
            rInit.pWeakMap.reset( new WeakMap_Impl );

        // description is the weak itself, so register it
        (*rInit.pWeakMap)[pTDR->pTypeName->buffer] = pTDR;
        OSL_ASSERT( static_cast<void *>(*ppNewDescription) == static_cast<void *>(pTDR) );
    }

    // By default this reference is not really weak. The reference hold the description
    // and the description hold the reference.
    if( !(*ppNewDescription)->bOnDemand )
    {
        // nor OnDemand so the description must be acquired if registered
        typelib_typedescription_acquire( *ppNewDescription );
    }

    pTDR->pType = *ppNewDescription;
    (*ppNewDescription)->pWeakRef = pTDR;
    OSL_ASSERT( rtl_ustr_compare( pTDR->pTypeName->buffer, (*ppNewDescription)->pTypeName->buffer ) == 0 );
    OSL_ASSERT( pTDR->eTypeClass == (*ppNewDescription)->eTypeClass );
}


static bool type_equals(
    typelib_TypeDescriptionReference const * p1, typelib_TypeDescriptionReference const * p2 )
{
    return (p1 == p2 ||
            (p1->eTypeClass == p2->eTypeClass &&
             p1->pTypeName->length == p2->pTypeName->length &&
             rtl_ustr_compare( p1->pTypeName->buffer, p2->pTypeName->buffer ) == 0));
}
extern "C" sal_Bool SAL_CALL typelib_typedescription_equals(
    const typelib_TypeDescription * p1, const typelib_TypeDescription * p2 )
    SAL_THROW_EXTERN_C()
{
    return type_equals(
        reinterpret_cast<typelib_TypeDescriptionReference const *>(p1), reinterpret_cast<typelib_TypeDescriptionReference const *>(p2) );
}


extern "C" sal_Int32 typelib_typedescription_getAlignedUnoSize(
    const typelib_TypeDescription * pTypeDescription,
    sal_Int32 nOffset, sal_Int32 & rMaxIntegralTypeSize )
    SAL_THROW_EXTERN_C()
{
    sal_Int32 nSize;
    if( pTypeDescription->nSize )
    {
        // size and alignment are set
        rMaxIntegralTypeSize = pTypeDescription->nAlignment;
        nSize = pTypeDescription->nSize;
    }
    else
    {
        nSize = 0;
        rMaxIntegralTypeSize = 1;

        OSL_ASSERT( typelib_TypeClass_TYPEDEF != pTypeDescription->eTypeClass );

        switch( pTypeDescription->eTypeClass )
        {
            case typelib_TypeClass_INTERFACE:
                // FEATURE_INTERFACE
                nSize = rMaxIntegralTypeSize = sal_Int32(sizeof( void * ));
                break;
            case typelib_TypeClass_ENUM:
                nSize = rMaxIntegralTypeSize = sal_Int32(sizeof( typelib_TypeClass ));
                break;
            case typelib_TypeClass_STRUCT:
            case typelib_TypeClass_EXCEPTION:
                // FEATURE_EMPTYCLASS
                {
                typelib_CompoundTypeDescription const * pTmp = reinterpret_cast<typelib_CompoundTypeDescription const *>(pTypeDescription);
                sal_Int32 nStructSize = 0;
                if( pTmp->pBaseTypeDescription )
                {
                    // inherit structs extends the base struct.
                    nStructSize = pTmp->pBaseTypeDescription->aBase.nSize;
                    rMaxIntegralTypeSize = pTmp->pBaseTypeDescription->aBase.nAlignment;
                 }
                for( sal_Int32 i = 0; i < pTmp->nMembers; i++ )
                {
                    typelib_TypeDescription * pMemberType = nullptr;
                    typelib_TypeDescriptionReference * pMemberRef = pTmp->ppTypeRefs[i];

                    sal_Int32 nMaxIntegral;
                    if (pMemberRef->eTypeClass == typelib_TypeClass_INTERFACE
                        || pMemberRef->eTypeClass == typelib_TypeClass_SEQUENCE)
                    {
                        nMaxIntegral = sal_Int32(sizeof(void *));
                        nStructSize = newAlignedSize( nStructSize, nMaxIntegral, nMaxIntegral );
                    }
                    else
                    {
                        TYPELIB_DANGER_GET( &pMemberType, pMemberRef );
                        nStructSize = typelib_typedescription_getAlignedUnoSize(
                            pMemberType, nStructSize, nMaxIntegral );
                        TYPELIB_DANGER_RELEASE( pMemberType );
                    }
                    if( nMaxIntegral > rMaxIntegralTypeSize )
                        rMaxIntegralTypeSize = nMaxIntegral;
                }
#ifdef __m68k__
                // Anything that is at least 16 bits wide is aligned on a 16-bit
                // boundary on the m68k default abi
                sal_Int32 nMaxAlign = std::min(rMaxIntegralTypeSize, sal_Int32( 2 ));
                nStructSize = (nStructSize + nMaxAlign -1) / nMaxAlign * nMaxAlign;
#else
                // Example: A { double; int; } structure has a size of 16 instead of 10. The
                // compiler must follow this rule if it is possible to access members in arrays through:
                // (Element *)((char *)pArray + sizeof( Element ) * ElementPos)
                nStructSize = (nStructSize + rMaxIntegralTypeSize -1)
                                / rMaxIntegralTypeSize * rMaxIntegralTypeSize;
#endif
                nSize += nStructSize;
                }
                break;
            case typelib_TypeClass_SEQUENCE:
                nSize = rMaxIntegralTypeSize = sal_Int32(sizeof( void * ));
                break;
            case typelib_TypeClass_ANY:
                // FEATURE_ANY
                nSize = sal_Int32(sizeof( uno_Any ));
                rMaxIntegralTypeSize = sal_Int32(sizeof( void * ));
                break;
            case typelib_TypeClass_TYPE:
                nSize = rMaxIntegralTypeSize = sal_Int32(sizeof( typelib_TypeDescriptionReference * ));
                break;
            case typelib_TypeClass_BOOLEAN:
                nSize = rMaxIntegralTypeSize = sal_Int32(sizeof( sal_Bool ));
                break;
            case typelib_TypeClass_CHAR:
                nSize = rMaxIntegralTypeSize = sal_Int32(sizeof( sal_Unicode ));
                break;
            case typelib_TypeClass_STRING:
                // FEATURE_STRING
                nSize = rMaxIntegralTypeSize = sal_Int32(sizeof( rtl_uString * ));
                break;
            case typelib_TypeClass_FLOAT:
                nSize = rMaxIntegralTypeSize = sal_Int32(sizeof( float ));
                break;
            case typelib_TypeClass_DOUBLE:
#ifdef AIX
                //See previous AIX ifdef comment for an explanation
                nSize = (sal_Int32)(sizeof(double));
                rMaxIntegralTypeSize = (sal_Int32)(sizeof(void*));
#else
                nSize = rMaxIntegralTypeSize = sal_Int32(sizeof( double ));
#endif
                break;
            case typelib_TypeClass_BYTE:
                nSize = rMaxIntegralTypeSize = sal_Int32(sizeof( sal_Int8 ));
                break;
            case typelib_TypeClass_SHORT:
            case typelib_TypeClass_UNSIGNED_SHORT:
                nSize = rMaxIntegralTypeSize = sal_Int32(sizeof( sal_Int16 ));
                break;
            case typelib_TypeClass_LONG:
            case typelib_TypeClass_UNSIGNED_LONG:
                nSize = rMaxIntegralTypeSize = sal_Int32(sizeof( sal_Int32 ));
                break;
            case typelib_TypeClass_HYPER:
            case typelib_TypeClass_UNSIGNED_HYPER:
                nSize = rMaxIntegralTypeSize = sal_Int32(sizeof( sal_Int64 ));
                break;
            case typelib_TypeClass_UNKNOWN:
            case typelib_TypeClass_SERVICE:
            case typelib_TypeClass_MODULE:
            default:
                OSL_FAIL( "not convertible type" );
        };
    }

    return newAlignedSize( nOffset, nSize, rMaxIntegralTypeSize );
}


namespace {

typelib_TypeDescriptionReference ** copyExceptions(
    sal_Int32 count, typelib_TypeDescriptionReference ** source)
{
    typelib_TypeDescriptionReference ** p
        = new typelib_TypeDescriptionReference *[count];
    for (sal_Int32 i = 0; i < count; ++i) {
        typelib_typedescriptionreference_acquire(p[i] = source[i]);
    }
    return p;
}

bool createDerivedInterfaceMemberDescription(
    typelib_TypeDescription ** result, OUString const & name,
    typelib_TypeDescriptionReference * baseRef,
    typelib_TypeDescription const * base, typelib_TypeDescription * interface,
    sal_Int32 index, sal_Int32 position)
{
    if (baseRef != nullptr && base != nullptr && interface != nullptr) {
        switch (base->eTypeClass) {
        case typelib_TypeClass_INTERFACE_METHOD:
            {
                typelib_typedescription_newEmpty(
                    result, typelib_TypeClass_INTERFACE_METHOD, name.pData);
                typelib_InterfaceMethodTypeDescription const * baseMethod
                    = reinterpret_cast<
                    typelib_InterfaceMethodTypeDescription const * >(base);
                typelib_InterfaceMethodTypeDescription * newMethod
                    = reinterpret_cast<
                    typelib_InterfaceMethodTypeDescription * >(*result);
                newMethod->aBase.nPosition = position;
                rtl_uString_acquire(
                    newMethod->aBase.pMemberName
                    = baseMethod->aBase.pMemberName);
                typelib_typedescriptionreference_acquire(
                    newMethod->pReturnTypeRef = baseMethod->pReturnTypeRef);
                newMethod->nParams = baseMethod->nParams;
                newMethod->pParams = new typelib_MethodParameter[
                    newMethod->nParams];
                for (sal_Int32 i = 0; i < newMethod->nParams; ++i) {
                    rtl_uString_acquire(
                        newMethod->pParams[i].pName
                        = baseMethod->pParams[i].pName);
                    typelib_typedescriptionreference_acquire(
                        newMethod->pParams[i].pTypeRef
                        = baseMethod->pParams[i].pTypeRef);
                    newMethod->pParams[i].bIn = baseMethod->pParams[i].bIn;
                    newMethod->pParams[i].bOut = baseMethod->pParams[i].bOut;
                }
                newMethod->nExceptions = baseMethod->nExceptions;
                newMethod->ppExceptions = copyExceptions(
                    baseMethod->nExceptions, baseMethod->ppExceptions);
                newMethod->bOneWay = baseMethod->bOneWay;
                newMethod->pInterface
                    = reinterpret_cast< typelib_InterfaceTypeDescription * >(
                        interface);
                newMethod->pBaseRef = baseRef;
                newMethod->nIndex = index;
                return true;
            }

        case typelib_TypeClass_INTERFACE_ATTRIBUTE:
            {
                typelib_typedescription_newEmpty(
                    result, typelib_TypeClass_INTERFACE_ATTRIBUTE, name.pData);
                typelib_InterfaceAttributeTypeDescription const * baseAttribute
                    = reinterpret_cast<
                    typelib_InterfaceAttributeTypeDescription const * >(base);
                typelib_InterfaceAttributeTypeDescription * newAttribute
                    = reinterpret_cast<
                    typelib_InterfaceAttributeTypeDescription * >(*result);
                newAttribute->aBase.nPosition = position;
                rtl_uString_acquire(
                    newAttribute->aBase.pMemberName
                    = baseAttribute->aBase.pMemberName);
                newAttribute->bReadOnly = baseAttribute->bReadOnly;
                typelib_typedescriptionreference_acquire(
                    newAttribute->pAttributeTypeRef
                    = baseAttribute->pAttributeTypeRef);
                newAttribute->pInterface
                    = reinterpret_cast< typelib_InterfaceTypeDescription * >(
                        interface);
                newAttribute->pBaseRef = baseRef;
                newAttribute->nIndex = index;
                newAttribute->nGetExceptions = baseAttribute->nGetExceptions;
                newAttribute->ppGetExceptions = copyExceptions(
                    baseAttribute->nGetExceptions,
                    baseAttribute->ppGetExceptions);
                newAttribute->nSetExceptions = baseAttribute->nSetExceptions;
                newAttribute->ppSetExceptions = copyExceptions(
                    baseAttribute->nSetExceptions,
                    baseAttribute->ppSetExceptions);
                return true;
            }

        default:
            break;
        }
    }
    return false;
}

}

extern "C" void SAL_CALL typelib_typedescription_getByName(
    typelib_TypeDescription ** ppRet, rtl_uString * pName )
    SAL_THROW_EXTERN_C()
{
    if( *ppRet )
    {
        typelib_typedescription_release( *ppRet );
        *ppRet = nullptr;
    }

    static bool bInited = false;
    TypeDescriptor_Init_Impl &rInit = Init::get();

    if( !bInited )<--- outer condition: !bInited
    {
        // guard against multi thread access
        MutexGuard aGuard( rInit.getMutex() );
        if( !bInited )<--- identical inner condition: !bInited
        {
            // avoid recursion during the next ...new calls
            bInited = true;

            typelib_TypeDescription * pType = nullptr;
            typelib_typedescription_new( &pType, typelib_TypeClass_TYPE, OUString("type").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_new( &pType, typelib_TypeClass_VOID, OUString("void").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_new( &pType, typelib_TypeClass_BOOLEAN, OUString("boolean").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_new( &pType, typelib_TypeClass_CHAR, OUString("char").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_new( &pType, typelib_TypeClass_BYTE, OUString("byte").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_new( &pType, typelib_TypeClass_STRING, OUString("string").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_new( &pType, typelib_TypeClass_SHORT, OUString("short").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_new( &pType, typelib_TypeClass_UNSIGNED_SHORT, OUString("unsigned short").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_new( &pType, typelib_TypeClass_LONG, OUString("long").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_new( &pType, typelib_TypeClass_UNSIGNED_LONG, OUString("unsigned long").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_new( &pType, typelib_TypeClass_HYPER, OUString("hyper").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_new( &pType, typelib_TypeClass_UNSIGNED_HYPER, OUString("unsigned hyper").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_new( &pType, typelib_TypeClass_FLOAT, OUString("float").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_new( &pType, typelib_TypeClass_DOUBLE, OUString("double").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_new( &pType, typelib_TypeClass_ANY, OUString("any").pData, nullptr, 0, nullptr );
            typelib_typedescription_register( &pType );
            typelib_typedescription_release( pType );
        }
    }

    typelib_TypeDescriptionReference * pTDR = nullptr;
    typelib_typedescriptionreference_getByName( &pTDR, pName );
    if( pTDR )
    {
        {
        // guard against multi thread access
        MutexGuard aGuard( rInit.getMutex() );
        // pTDR->pType->pWeakRef == 0 means that the description is empty
        if( pTDR->pType && pTDR->pType->pWeakRef )
        {
            typelib_typedescription_acquire( pTDR->pType );
            *ppRet = pTDR->pType;
        }
        }
        typelib_typedescriptionreference_release( pTDR );
    }

    if (nullptr == *ppRet)
    {
        // check for sequence
        OUString const & name = OUString::unacquired( &pName );
        if (2 < name.getLength() && '[' == name[ 0 ])
        {
            OUString element_name( name.copy( 2 ) );
            typelib_TypeDescription * element_td = nullptr;
            typelib_typedescription_getByName( &element_td, element_name.pData );
            if (nullptr != element_td)
            {
                typelib_typedescription_new(
                    ppRet, typelib_TypeClass_SEQUENCE, pName, element_td->pWeakRef, 0, nullptr );
                // register?
                typelib_typedescription_release( element_td );
            }
        }
        if (nullptr == *ppRet)
        {
            // Check for derived interface member type:
            sal_Int32 i1 = name.lastIndexOf(":@");
            if (i1 >= 0) {
                sal_Int32 i2 = i1 + RTL_CONSTASCII_LENGTH(":@");
                sal_Int32 i3 = name.indexOf(',', i2);
                if (i3 >= 0) {
                    sal_Int32 i4 = name.indexOf(':', i3);
                    if (i4 >= 0) {
                        typelib_TypeDescriptionReference * pBaseRef = nullptr;
                        typelib_TypeDescription * pBase = nullptr;
                        typelib_TypeDescription * pInterface = nullptr;
                        typelib_typedescriptionreference_getByName(
                            &pBaseRef, name.copy(0, i1).pData);
                        if (pBaseRef != nullptr) {
                            typelib_typedescriptionreference_getDescription(
                                &pBase, pBaseRef);
                        }
                        typelib_typedescription_getByName(
                            &pInterface, name.copy(i4 + 1).pData);
                        if (!createDerivedInterfaceMemberDescription(
                                ppRet, name, pBaseRef, pBase, pInterface,
                                name.copy(i2, i3 - i2).toInt32(),
                                name.copy(i3 + 1, i4 - i3 - 1).toInt32()))
                        {
                            if (pInterface != nullptr) {
                                typelib_typedescription_release(pInterface);
                            }
                            if (pBase != nullptr) {
                                typelib_typedescription_release(pBase);
                            }
                            if (pBaseRef != nullptr) {
                                typelib_typedescriptionreference_release(
                                    pBaseRef);
                            }
                        }
                    }
                }
            }
        }
        if (nullptr == *ppRet)
        {
            // on demand access
            rInit.callChain( ppRet, pName );
        }

        if( *ppRet )
        {
            // typedescription found
            if (typelib_TypeClass_TYPEDEF == (*ppRet)->eTypeClass)
            {
                typelib_TypeDescription * pTD = nullptr;
                typelib_typedescriptionreference_getDescription(
                    &pTD, reinterpret_cast<typelib_IndirectTypeDescription *>(*ppRet)->pType );
                typelib_typedescription_release( *ppRet );
                *ppRet = pTD;
            }
            else
            {
                // set to on demand
                (*ppRet)->bOnDemand = true;
                // The type description is hold by the reference until
                // on demand is activated.
                typelib_typedescription_register( ppRet );

                // insert into the cache
                MutexGuard aGuard( rInit.getMutex() );
                if( !rInit.pCache )
                    rInit.pCache.reset( new TypeDescriptionList_Impl );
                if( static_cast<sal_Int32>(rInit.pCache->size()) >= nCacheSize )
                {
                    typelib_typedescription_release( rInit.pCache->front() );
                    rInit.pCache->pop_front();
                }
                // descriptions in the cache must be acquired!
                typelib_typedescription_acquire( *ppRet );
                rInit.pCache->push_back( *ppRet );
            }
        }
    }
}

extern "C" void SAL_CALL typelib_typedescriptionreference_newByAsciiName(<--- The function 'typelib_typedescriptionreference_newByAsciiName' is never used.
    typelib_TypeDescriptionReference ** ppTDR,
    typelib_TypeClass eTypeClass,
    const sal_Char * pTypeName )
    SAL_THROW_EXTERN_C()
{
    OUString aTypeName( OUString::createFromAscii( pTypeName ) );
    typelib_typedescriptionreference_new( ppTDR, eTypeClass, aTypeName.pData );
}

extern "C" void SAL_CALL typelib_typedescriptionreference_new(
    typelib_TypeDescriptionReference ** ppTDR,
    typelib_TypeClass eTypeClass, rtl_uString * pTypeName )
    SAL_THROW_EXTERN_C()
{
    TypeDescriptor_Init_Impl &rInit = Init::get();
    if( eTypeClass == typelib_TypeClass_TYPEDEF )
    {
        // on demand access
        typelib_TypeDescription * pRet = nullptr;
        rInit.callChain( &pRet, pTypeName );
        if( pRet )
        {
            // typedescription found
            if (typelib_TypeClass_TYPEDEF == pRet->eTypeClass)
            {
                typelib_typedescriptionreference_acquire(
                    reinterpret_cast<typelib_IndirectTypeDescription *>(pRet)->pType );
                if (*ppTDR)
                    typelib_typedescriptionreference_release( *ppTDR );
                *ppTDR = reinterpret_cast<typelib_IndirectTypeDescription *>(pRet)->pType;
                typelib_typedescription_release( pRet );
            }
            else
            {
                // set to on demand
                pRet->bOnDemand = true;
                // The type description is hold by the reference until
                // on demand is activated.
                typelib_typedescription_register( &pRet );

                // insert into the cache
                MutexGuard aGuard( rInit.getMutex() );
                if( !rInit.pCache )
                    rInit.pCache.reset( new TypeDescriptionList_Impl );
                if( static_cast<sal_Int32>(rInit.pCache->size()) >= nCacheSize )
                {
                    typelib_typedescription_release( rInit.pCache->front() );
                    rInit.pCache->pop_front();
                }
                rInit.pCache->push_back( pRet );
                // pRet kept acquired for cache

                typelib_typedescriptionreference_acquire( pRet->pWeakRef );
                if (*ppTDR)
                    typelib_typedescriptionreference_release( *ppTDR );
                *ppTDR = pRet->pWeakRef;
            }
        }
        else if (*ppTDR)
        {
            SAL_INFO("cppu.typelib", "typedef not found : " << pTypeName);
            typelib_typedescriptionreference_release( *ppTDR );
            *ppTDR = nullptr;
        }
        return;
    }

    MutexGuard aGuard( rInit.getMutex() );
    typelib_typedescriptionreference_getByName( ppTDR, pTypeName );
    if( *ppTDR )
        return;

    if( reallyWeak( eTypeClass ) )
    {
        typelib_TypeDescriptionReference * pTDR = new typelib_TypeDescriptionReference;
#if OSL_DEBUG_LEVEL > 0
        osl_atomic_increment( &rInit.nTypeDescriptionReferenceCount );
#endif
        pTDR->nRefCount = 1;
        pTDR->nStaticRefCount = 0;
        pTDR->eTypeClass = eTypeClass;
        pTDR->pUniqueIdentifier = nullptr;
        pTDR->pReserved = nullptr;
        rtl_uString_acquire( pTDR->pTypeName = pTypeName );
        pTDR->pType = nullptr;
        *ppTDR = pTDR;
    }
    else
    {
        typelib_typedescription_newEmpty( reinterpret_cast<typelib_TypeDescription ** >(ppTDR), eTypeClass, pTypeName );
        // description will be registered but not acquired
        (*reinterpret_cast<typelib_TypeDescription **>(ppTDR))->bOnDemand = true;
        (*reinterpret_cast<typelib_TypeDescription **>(ppTDR))->bComplete = false;
    }

    if( !rInit.pWeakMap )
        rInit.pWeakMap.reset( new WeakMap_Impl );
    // Heavy hack, the const sal_Unicode * is hold by the typedescription reference
    // not registered
    rInit.pWeakMap->operator[]( (*ppTDR)->pTypeName->buffer ) = *ppTDR;
}


extern "C" void SAL_CALL typelib_typedescriptionreference_acquire(
    typelib_TypeDescriptionReference * pRef )
    SAL_THROW_EXTERN_C()
{
    osl_atomic_increment( &pRef->nRefCount );
}


extern "C" void SAL_CALL typelib_typedescriptionreference_release(
    typelib_TypeDescriptionReference * pRef )
    SAL_THROW_EXTERN_C()
{
    // Is it a type description?
    if( reallyWeak( pRef->eTypeClass ) )
    {
        if( ! osl_atomic_decrement( &pRef->nRefCount ) )
        {
            TypeDescriptor_Init_Impl &rInit = Init::get();
            if( rInit.pWeakMap )
            {
                MutexGuard aGuard( rInit.getMutex() );
                WeakMap_Impl::iterator aIt = rInit.pWeakMap->find( pRef->pTypeName->buffer );
                if( aIt != rInit.pWeakMap->end() && (*aIt).second == pRef )
                {
                    // remove only if it contains the same object
                    rInit.pWeakMap->erase( aIt );
                }
            }

            rtl_uString_release( pRef->pTypeName );
            OSL_ASSERT( pRef->pType == nullptr );
#if OSL_DEBUG_LEVEL > 0
            osl_atomic_decrement( &rInit.nTypeDescriptionReferenceCount );
#endif
            delete pRef;
        }
    }
    else
    {
        typelib_typedescription_release( reinterpret_cast<typelib_TypeDescription *>(pRef) );
    }
}


extern "C" void SAL_CALL typelib_typedescriptionreference_getDescription(
    typelib_TypeDescription ** ppRet, typelib_TypeDescriptionReference * pRef )
    SAL_THROW_EXTERN_C()
{
    if( *ppRet )
    {
        typelib_typedescription_release( *ppRet );
        *ppRet = nullptr;
    }

    if( !reallyWeak( pRef->eTypeClass ) && pRef->pType && pRef->pType->pWeakRef )
    {
        // reference is a description and initialized
        osl_atomic_increment( &reinterpret_cast<typelib_TypeDescription *>(pRef)->nRefCount );
        *ppRet = reinterpret_cast<typelib_TypeDescription *>(pRef);
        return;
    }

    {
    MutexGuard aGuard( Init::get().getMutex() );
    // pRef->pType->pWeakRef == 0 means that the description is empty
    if( pRef->pType && pRef->pType->pWeakRef )
    {
        sal_Int32 n = osl_atomic_increment( &pRef->pType->nRefCount );
        if( n > 1 )
        {
            // The reference is incremented. The object cannot be destroyed.
            // Release the guard at the earliest point.
            *ppRet = pRef->pType;
            return;
        }
        (void)osl_atomic_decrement( &pRef->pType->nRefCount );
        // destruction of this type in progress (another thread!)
        // no access through this weak reference
        pRef->pType = nullptr;
    }
    }

    typelib_typedescription_getByName( ppRet, pRef->pTypeName );
    OSL_ASSERT( !*ppRet || rtl_ustr_compare( pRef->pTypeName->buffer, (*ppRet)->pTypeName->buffer ) == 0 );
    OSL_ASSERT( !*ppRet || pRef->eTypeClass == (*ppRet)->eTypeClass );
    OSL_ASSERT( !*ppRet || pRef == (*ppRet)->pWeakRef );
    pRef->pType = *ppRet;
}


extern "C" void typelib_typedescriptionreference_getByName(
    typelib_TypeDescriptionReference ** ppRet, rtl_uString const * pName )
    SAL_THROW_EXTERN_C()
{
    if( *ppRet )
    {
        typelib_typedescriptionreference_release( *ppRet );
        *ppRet = nullptr;
    }
    TypeDescriptor_Init_Impl &rInit = Init::get();
    if( rInit.pWeakMap )
    {
        MutexGuard aGuard( rInit.getMutex() );
        WeakMap_Impl::const_iterator aIt = rInit.pWeakMap->find( pName->buffer );
        if( aIt != rInit.pWeakMap->end() )
        {
            sal_Int32 n = osl_atomic_increment( &(*aIt).second->nRefCount );
            if( n > 1 )
            {
                // The reference is incremented. The object cannot be destroyed.
                // Release the guard at the earliest point.
                *ppRet = (*aIt).second;
            }
            else
            {
                // destruction of this type in progress (another thread!)
                // no access through this weak reference
                (void)osl_atomic_decrement( &(*aIt).second->nRefCount );
            }
        }
    }
}


extern "C" sal_Bool SAL_CALL typelib_typedescriptionreference_equals(
    const typelib_TypeDescriptionReference * p1,
    const typelib_TypeDescriptionReference * p2 )
    SAL_THROW_EXTERN_C()
{
    return (p1 == p2 ||
            (p1->eTypeClass == p2->eTypeClass &&
             p1->pTypeName->length == p2->pTypeName->length &&
             rtl_ustr_compare( p1->pTypeName->buffer, p2->pTypeName->buffer ) == 0));
}


extern "C" void SAL_CALL typelib_typedescriptionreference_assign(
    typelib_TypeDescriptionReference ** ppDest,
    typelib_TypeDescriptionReference * pSource )
    SAL_THROW_EXTERN_C()
{
    if (*ppDest != pSource)
    {
        ::typelib_typedescriptionreference_acquire( pSource );
        ::typelib_typedescriptionreference_release( *ppDest );
        *ppDest = pSource;
    }
}


extern "C" void SAL_CALL typelib_setCacheSize( sal_Int32 nNewSize )<--- The function 'typelib_setCacheSize' is never used.
    SAL_THROW_EXTERN_C()
{
    OSL_ENSURE( nNewSize >= 0, "### illegal cache size given!" );
    if (nNewSize >= 0)
    {
        TypeDescriptor_Init_Impl &rInit = Init::get();
        MutexGuard aGuard( rInit.getMutex() );
        if ((nNewSize < nCacheSize) && rInit.pCache)
        {
            while (static_cast<sal_Int32>(rInit.pCache->size()) != nNewSize)
            {
                typelib_typedescription_release( rInit.pCache->front() );
                rInit.pCache->pop_front();
            }
        }
        nCacheSize = nNewSize;
    }
}


static const bool s_aAssignableFromTab[11][11] =
{
                          /* from CH,    BO,    BY,    SH,    US,    LO,    UL,    HY,    UH,    FL,    DO */
/* TypeClass_CHAR */            { true,  false, false, false, false, false, false, false, false, false, false },
/* TypeClass_BOOLEAN */         { false, true,  false, false, false, false, false, false, false, false, false },
/* TypeClass_BYTE */            { false, false, true,  false, false, false, false, false, false, false, false },
/* TypeClass_SHORT */           { false, false, true,  true,  true,  false, false, false, false, false, false },
/* TypeClass_UNSIGNED_SHORT */  { false, false, true,  true,  true,  false, false, false, false, false, false },
/* TypeClass_LONG */            { false, false, true,  true,  true,  true,  true,  false, false, false, false },
/* TypeClass_UNSIGNED_LONG */   { false, false, true,  true,  true,  true,  true,  false, false, false, false },
/* TypeClass_HYPER */           { false, false, true,  true,  true,  true,  true,  true,  true,  false, false },
/* TypeClass_UNSIGNED_HYPER */  { false, false, true,  true,  true,  true,  true,  true,  true,  false, false },
/* TypeClass_FLOAT */           { false, false, true,  true,  true,  false, false, false, false, true,  false },
/* TypeClass_DOUBLE */          { false, false, true,  true,  true,  true,  true,  false, false, true,  true  }
};


extern "C" sal_Bool SAL_CALL typelib_typedescriptionreference_isAssignableFrom(
    typelib_TypeDescriptionReference * pAssignable,
    typelib_TypeDescriptionReference * pFrom )
    SAL_THROW_EXTERN_C()
{
    if (pAssignable && pFrom)
    {
        typelib_TypeClass eAssignable = pAssignable->eTypeClass;
        typelib_TypeClass eFrom       = pFrom->eTypeClass;

        if (eAssignable == typelib_TypeClass_ANY) // anything can be assigned to an any .)
            return true;
        if (eAssignable == eFrom)
        {
            if (type_equals( pAssignable, pFrom )) // first shot
            {
                return true;
            }
            switch (eAssignable)
            {
            case typelib_TypeClass_STRUCT:
            case typelib_TypeClass_EXCEPTION:
            {
                typelib_TypeDescription * pFromDescr = nullptr;
                TYPELIB_DANGER_GET( &pFromDescr, pFrom );
                if (!reinterpret_cast<typelib_CompoundTypeDescription *>(pFromDescr)->pBaseTypeDescription)
                {
                    TYPELIB_DANGER_RELEASE( pFromDescr );
                    return false;
                }
                bool bRet = typelib_typedescriptionreference_isAssignableFrom(
                    pAssignable,
                    reinterpret_cast<typelib_CompoundTypeDescription *>(pFromDescr)->pBaseTypeDescription->aBase.pWeakRef );
                TYPELIB_DANGER_RELEASE( pFromDescr );
                return bRet;
            }
            case typelib_TypeClass_INTERFACE:
            {
                typelib_TypeDescription * pFromDescr = nullptr;
                TYPELIB_DANGER_GET( &pFromDescr, pFrom );
                typelib_InterfaceTypeDescription * pFromIfc
                    = reinterpret_cast<
                        typelib_InterfaceTypeDescription * >(pFromDescr);
                bool bRet = false;
                for (sal_Int32 i = 0; i < pFromIfc->nBaseTypes; ++i) {
                    if (typelib_typedescriptionreference_isAssignableFrom(
                            pAssignable,
                            pFromIfc->ppBaseTypes[i]->aBase.pWeakRef))
                    {
                        bRet = true;
                        break;
                    }
                }
                TYPELIB_DANGER_RELEASE( pFromDescr );
                return bRet;
            }
            default:
            {
                return false;
            }
            }
        }
        return (eAssignable >= typelib_TypeClass_CHAR && eAssignable <= typelib_TypeClass_DOUBLE &&
                eFrom >= typelib_TypeClass_CHAR && eFrom <= typelib_TypeClass_DOUBLE &&
                s_aAssignableFromTab[eAssignable-1][eFrom-1]);
    }
    return false;
}

extern "C" sal_Bool SAL_CALL typelib_typedescription_isAssignableFrom(
    typelib_TypeDescription * pAssignable,
    typelib_TypeDescription * pFrom )
    SAL_THROW_EXTERN_C()
{
    return typelib_typedescriptionreference_isAssignableFrom(
        pAssignable->pWeakRef, pFrom->pWeakRef );
}


extern "C" sal_Bool SAL_CALL typelib_typedescription_complete(
    typelib_TypeDescription ** ppTypeDescr )
    SAL_THROW_EXTERN_C()
{
    return complete(ppTypeDescr, true);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */