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
|
//===-- ClangASTSource.cpp ---------------------------------------*- C++-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "ClangASTSource.h"
#include "ASTDumper.h"
#include "ClangModulesDeclVendor.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleList.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Symbol/ClangUtil.h"
#include "lldb/Symbol/CompilerDeclContext.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/SymbolFile.h"
#include "lldb/Symbol/SymbolVendor.h"
#include "lldb/Symbol/TaggedASTType.h"
#include "lldb/Target/ObjCLanguageRuntime.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/Log.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecordLayout.h"
#include <vector>
using namespace clang;
using namespace lldb_private;
//------------------------------------------------------------------
// Scoped class that will remove an active lexical decl from the set
// when it goes out of scope.
//------------------------------------------------------------------
namespace {
class ScopedLexicalDeclEraser {
public:
ScopedLexicalDeclEraser(std::set<const clang::Decl *> &decls,
const clang::Decl *decl)
: m_active_lexical_decls(decls), m_decl(decl) {}
~ScopedLexicalDeclEraser() { m_active_lexical_decls.erase(m_decl); }
private:
std::set<const clang::Decl *> &m_active_lexical_decls;
const clang::Decl *m_decl;
};
}
ClangASTSource::ClangASTSource(const lldb::TargetSP &target)
: m_import_in_progress(false), m_lookups_enabled(false), m_target(target),
m_ast_context(NULL), m_active_lexical_decls(), m_active_lookups() {
if (!target->GetUseModernTypeLookup()) {
m_ast_importer_sp = m_target->GetClangASTImporter();
}
}
void ClangASTSource::InstallASTContext(clang::ASTContext &ast_context,
clang::FileManager &file_manager,
bool is_shared_context) {
m_ast_context = &ast_context;
m_file_manager = &file_manager;
if (m_target->GetUseModernTypeLookup()) {
// Configure the ExternalASTMerger. The merger needs to be able to import
// types from any source that we would do lookups in, which includes the
// persistent AST context as well as the modules and Objective-C runtime
// AST contexts.
lldbassert(!m_merger_up);
clang::ExternalASTMerger::ImporterTarget target = {ast_context,
file_manager};
std::vector<clang::ExternalASTMerger::ImporterSource> sources;
for (lldb::ModuleSP module_sp : m_target->GetImages().Modules()) {
if (auto *module_ast_ctx = llvm::cast_or_null<ClangASTContext>(
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeC))) {
lldbassert(module_ast_ctx->getASTContext());
lldbassert(module_ast_ctx->getFileManager());
sources.push_back({*module_ast_ctx->getASTContext(),
*module_ast_ctx->getFileManager(),
module_ast_ctx->GetOriginMap()
});
}
}
do {
lldb::ProcessSP process(m_target->GetProcessSP());
if (!process)
break;
ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
if (!language_runtime)
break;
DeclVendor *runtime_decl_vendor = language_runtime->GetDeclVendor();
if (!runtime_decl_vendor)
break;
sources.push_back(runtime_decl_vendor->GetImporterSource());
} while (0);
do {
DeclVendor *modules_decl_vendor =
m_target->GetClangModulesDeclVendor();
if (!modules_decl_vendor)
break;
sources.push_back(modules_decl_vendor->GetImporterSource());
} while (0);
if (!is_shared_context) {
// Update the scratch AST context's merger to reflect any new sources we
// might have come across since the last time an expression was parsed.
auto scratch_ast_context = static_cast<ClangASTContextForExpressions*>(
m_target->GetScratchClangASTContext());
scratch_ast_context->GetMergerUnchecked().AddSources(sources);
sources.push_back({*scratch_ast_context->getASTContext(),
*scratch_ast_context->getFileManager(),
scratch_ast_context->GetOriginMap()});
} while (0);
m_merger_up =
llvm::make_unique<clang::ExternalASTMerger>(target, sources);
} else {
m_ast_importer_sp->InstallMapCompleter(&ast_context, *this);
}
}
ClangASTSource::~ClangASTSource() {
if (m_ast_importer_sp)
m_ast_importer_sp->ForgetDestination(m_ast_context);
// We are in the process of destruction, don't create clang ast context on
// demand
// by passing false to Target::GetScratchClangASTContext(create_on_demand).
ClangASTContext *scratch_clang_ast_context =
m_target->GetScratchClangASTContext(false);
if (!scratch_clang_ast_context)
return;
clang::ASTContext *scratch_ast_context =
scratch_clang_ast_context->getASTContext();
if (!scratch_ast_context)
return;
if (m_ast_context != scratch_ast_context && m_ast_importer_sp)
m_ast_importer_sp->ForgetSource(scratch_ast_context, m_ast_context);
}
void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) {
if (!m_ast_context)
return;
m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
}
// The core lookup interface.
bool ClangASTSource::FindExternalVisibleDeclsByName(
const DeclContext *decl_ctx, DeclarationName clang_decl_name) {
if (!m_ast_context) {
SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
return false;
}
if (GetImportInProgress()) {
SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
return false;
}
std::string decl_name(clang_decl_name.getAsString());
// if (m_decl_map.DoingASTImport ())
// return DeclContext::lookup_result();
//
switch (clang_decl_name.getNameKind()) {
// Normal identifiers.
case DeclarationName::Identifier: {
clang::IdentifierInfo *identifier_info =
clang_decl_name.getAsIdentifierInfo();
if (!identifier_info || identifier_info->getBuiltinID() != 0) {
SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
return false;
}
} break;
// Operator names.
case DeclarationName::CXXOperatorName:
case DeclarationName::CXXLiteralOperatorName:
break;
// Using directives found in this context.
// Tell Sema we didn't find any or we'll end up getting asked a *lot*.
case DeclarationName::CXXUsingDirective:
SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
return false;
case DeclarationName::ObjCZeroArgSelector:
case DeclarationName::ObjCOneArgSelector:
case DeclarationName::ObjCMultiArgSelector: {
llvm::SmallVector<NamedDecl *, 1> method_decls;
NameSearchContext method_search_context(*this, method_decls,
clang_decl_name, decl_ctx);
FindObjCMethodDecls(method_search_context);
SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, method_decls);
return (method_decls.size() > 0);
}
// These aren't possible in the global context.
case DeclarationName::CXXConstructorName:
case DeclarationName::CXXDestructorName:
case DeclarationName::CXXConversionFunctionName:
case DeclarationName::CXXDeductionGuideName:
SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
return false;
}
if (!GetLookupsEnabled()) {
// Wait until we see a '$' at the start of a name before we start doing
// any lookups so we can avoid lookup up all of the builtin types.
if (!decl_name.empty() && decl_name[0] == '$') {
SetLookupsEnabled(true);
} else {
SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
return false;
}
}
ConstString const_decl_name(decl_name.c_str());
const char *uniqued_const_decl_name = const_decl_name.GetCString();
if (m_active_lookups.find(uniqued_const_decl_name) !=
m_active_lookups.end()) {
// We are currently looking up this name...
SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
return false;
}
m_active_lookups.insert(uniqued_const_decl_name);
// static uint32_t g_depth = 0;
// ++g_depth;
// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth,
// uniqued_const_decl_name);
llvm::SmallVector<NamedDecl *, 4> name_decls;
NameSearchContext name_search_context(*this, name_decls, clang_decl_name,
decl_ctx);
FindExternalVisibleDecls(name_search_context);
SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, name_decls);
// --g_depth;
m_active_lookups.erase(uniqued_const_decl_name);
return (name_decls.size() != 0);
}
void ClangASTSource::CompleteType(TagDecl *tag_decl) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
static unsigned int invocation_id = 0;
unsigned int current_id = invocation_id++;
if (log) {
log->Printf(" CompleteTagDecl[%u] on (ASTContext*)%p Completing "
"(TagDecl*)%p named %s",
current_id, static_cast<void *>(m_ast_context),
static_cast<void *>(tag_decl),
tag_decl->getName().str().c_str());
log->Printf(" CTD[%u] Before:", current_id);
ASTDumper dumper((Decl *)tag_decl);
dumper.ToLog(log, " [CTD] ");
}
auto iter = m_active_lexical_decls.find(tag_decl);
if (iter != m_active_lexical_decls.end())
return;
m_active_lexical_decls.insert(tag_decl);
ScopedLexicalDeclEraser eraser(m_active_lexical_decls, tag_decl);
if (!m_ast_importer_sp) {
if (HasMerger()) {
GetMergerUnchecked().CompleteType(tag_decl);
}
return;
}
if (!m_ast_importer_sp->CompleteTagDecl(tag_decl)) {
// We couldn't complete the type. Maybe there's a definition
// somewhere else that can be completed.
if (log)
log->Printf(" CTD[%u] Type could not be completed in the module in "
"which it was first found.",
current_id);
bool found = false;
DeclContext *decl_ctx = tag_decl->getDeclContext();
if (const NamespaceDecl *namespace_context =
dyn_cast<NamespaceDecl>(decl_ctx)) {
ClangASTImporter::NamespaceMapSP namespace_map =
m_ast_importer_sp->GetNamespaceMap(namespace_context);
if (log && log->GetVerbose())
log->Printf(" CTD[%u] Inspecting namespace map %p (%d entries)",
current_id, static_cast<void *>(namespace_map.get()),
static_cast<int>(namespace_map->size()));
if (!namespace_map)
return;
for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(),
e = namespace_map->end();
i != e && !found; ++i) {
if (log)
log->Printf(" CTD[%u] Searching namespace %s in module %s",
current_id, i->second.GetName().AsCString(),
i->first->GetFileSpec().GetFilename().GetCString());
TypeList types;
SymbolContext null_sc;
ConstString name(tag_decl->getName().str().c_str());
i->first->FindTypesInNamespace(null_sc, name, &i->second, UINT32_MAX,
types);
for (uint32_t ti = 0, te = types.GetSize(); ti != te && !found; ++ti) {
lldb::TypeSP type = types.GetTypeAtIndex(ti);
if (!type)
continue;
CompilerType clang_type(type->GetFullCompilerType());
if (!ClangUtil::IsClangType(clang_type))
continue;
const TagType *tag_type =
ClangUtil::GetQualType(clang_type)->getAs<TagType>();
if (!tag_type)
continue;
TagDecl *candidate_tag_decl =
const_cast<TagDecl *>(tag_type->getDecl());
if (m_ast_importer_sp->CompleteTagDeclWithOrigin(tag_decl,
candidate_tag_decl))
found = true;
}
}
} else {
TypeList types;
SymbolContext null_sc;
ConstString name(tag_decl->getName().str().c_str());
CompilerDeclContext namespace_decl;
const ModuleList &module_list = m_target->GetImages();
bool exact_match = false;
llvm::DenseSet<SymbolFile *> searched_symbol_files;
module_list.FindTypes(null_sc, name, exact_match, UINT32_MAX,
searched_symbol_files, types);
for (uint32_t ti = 0, te = types.GetSize(); ti != te && !found; ++ti) {
lldb::TypeSP type = types.GetTypeAtIndex(ti);
if (!type)
continue;
CompilerType clang_type(type->GetFullCompilerType());
if (!ClangUtil::IsClangType(clang_type))
continue;
const TagType *tag_type =
ClangUtil::GetQualType(clang_type)->getAs<TagType>();
if (!tag_type)
continue;
TagDecl *candidate_tag_decl =
const_cast<TagDecl *>(tag_type->getDecl());
// We have found a type by basename and we need to make sure the decl
// contexts
// are the same before we can try to complete this type with another
if (!ClangASTContext::DeclsAreEquivalent(tag_decl, candidate_tag_decl))
continue;
if (m_ast_importer_sp->CompleteTagDeclWithOrigin(tag_decl,
candidate_tag_decl))
found = true;
}
}
}
if (log) {
log->Printf(" [CTD] After:");
ASTDumper dumper((Decl *)tag_decl);
dumper.ToLog(log, " [CTD] ");
}
}
void ClangASTSource::CompleteType(clang::ObjCInterfaceDecl *interface_decl) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
if (log) {
log->Printf(" [CompleteObjCInterfaceDecl] on (ASTContext*)%p Completing "
"an ObjCInterfaceDecl named %s",
static_cast<void *>(m_ast_context),
interface_decl->getName().str().c_str());
log->Printf(" [COID] Before:");
ASTDumper dumper((Decl *)interface_decl);
dumper.ToLog(log, " [COID] ");
}
if (!m_ast_importer_sp) {
if (HasMerger()) {
ObjCInterfaceDecl *complete_iface_decl =
GetCompleteObjCInterface(interface_decl);
if (complete_iface_decl && (complete_iface_decl != interface_decl)) {
m_merger_up->ForceRecordOrigin(interface_decl, {complete_iface_decl, &complete_iface_decl->getASTContext()});
}
GetMergerUnchecked().CompleteType(interface_decl);
} else {
lldbassert(0 && "No mechanism for completing a type!");
}
return;
}
Decl *original_decl = NULL;
ASTContext *original_ctx = NULL;
if (m_ast_importer_sp->ResolveDeclOrigin(interface_decl, &original_decl,
&original_ctx)) {
if (ObjCInterfaceDecl *original_iface_decl =
dyn_cast<ObjCInterfaceDecl>(original_decl)) {
ObjCInterfaceDecl *complete_iface_decl =
GetCompleteObjCInterface(original_iface_decl);
if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) {
m_ast_importer_sp->SetDeclOrigin(interface_decl, complete_iface_decl);
}
}
}
m_ast_importer_sp->CompleteObjCInterfaceDecl(interface_decl);
if (interface_decl->getSuperClass() &&
interface_decl->getSuperClass() != interface_decl)
CompleteType(interface_decl->getSuperClass());
if (log) {
log->Printf(" [COID] After:");
ASTDumper dumper((Decl *)interface_decl);
dumper.ToLog(log, " [COID] ");
}
}
clang::ObjCInterfaceDecl *ClangASTSource::GetCompleteObjCInterface(
const clang::ObjCInterfaceDecl *interface_decl) {
lldb::ProcessSP process(m_target->GetProcessSP());
if (!process)
return NULL;
ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
if (!language_runtime)
return NULL;
ConstString class_name(interface_decl->getNameAsString().c_str());
lldb::TypeSP complete_type_sp(
language_runtime->LookupInCompleteClassCache(class_name));
if (!complete_type_sp)
return NULL;
TypeFromUser complete_type =
TypeFromUser(complete_type_sp->GetFullCompilerType());
lldb::opaque_compiler_type_t complete_opaque_type =
complete_type.GetOpaqueQualType();
if (!complete_opaque_type)
return NULL;
const clang::Type *complete_clang_type =
QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
const ObjCInterfaceType *complete_interface_type =
dyn_cast<ObjCInterfaceType>(complete_clang_type);
if (!complete_interface_type)
return NULL;
ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl());
return complete_iface_decl;
}
void ClangASTSource::FindExternalLexicalDecls(
const DeclContext *decl_context,
llvm::function_ref<bool(Decl::Kind)> predicate,
llvm::SmallVectorImpl<Decl *> &decls) {
if (HasMerger()) {
if (auto *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_context)) {
ObjCInterfaceDecl *complete_iface_decl =
GetCompleteObjCInterface(interface_decl);
if (complete_iface_decl && (complete_iface_decl != interface_decl)) {
m_merger_up->ForceRecordOrigin(interface_decl, {complete_iface_decl, &complete_iface_decl->getASTContext()});
}
}
return GetMergerUnchecked().FindExternalLexicalDecls(decl_context,
predicate,
decls);
} else if (!m_ast_importer_sp)
return;
ClangASTMetrics::RegisterLexicalQuery();
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
const Decl *context_decl = dyn_cast<Decl>(decl_context);
if (!context_decl)
return;
auto iter = m_active_lexical_decls.find(context_decl);
if (iter != m_active_lexical_decls.end())
return;
m_active_lexical_decls.insert(context_decl);
ScopedLexicalDeclEraser eraser(m_active_lexical_decls, context_decl);
static unsigned int invocation_id = 0;
unsigned int current_id = invocation_id++;
if (log) {
if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
log->Printf(
"FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p",
current_id, static_cast<void *>(m_ast_context),
context_named_decl->getNameAsString().c_str(),
context_decl->getDeclKindName(),
static_cast<const void *>(context_decl));
else if (context_decl)
log->Printf(
"FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p",
current_id, static_cast<void *>(m_ast_context),
context_decl->getDeclKindName(),
static_cast<const void *>(context_decl));
else
log->Printf(
"FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context",
current_id, static_cast<const void *>(m_ast_context));
}
Decl *original_decl = NULL;
ASTContext *original_ctx = NULL;
if (!m_ast_importer_sp->ResolveDeclOrigin(context_decl, &original_decl,
&original_ctx))
return;
if (log) {
log->Printf(" FELD[%u] Original decl (ASTContext*)%p (Decl*)%p:",
current_id, static_cast<void *>(original_ctx),
static_cast<void *>(original_decl));
ASTDumper(original_decl).ToLog(log, " ");
}
if (ObjCInterfaceDecl *original_iface_decl =
dyn_cast<ObjCInterfaceDecl>(original_decl)) {
ObjCInterfaceDecl *complete_iface_decl =
GetCompleteObjCInterface(original_iface_decl);
if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) {
original_decl = complete_iface_decl;
original_ctx = &complete_iface_decl->getASTContext();
m_ast_importer_sp->SetDeclOrigin(context_decl, complete_iface_decl);
}
}
if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl)) {
ExternalASTSource *external_source = original_ctx->getExternalSource();
if (external_source)
external_source->CompleteType(original_tag_decl);
}
const DeclContext *original_decl_context =
dyn_cast<DeclContext>(original_decl);
if (!original_decl_context)
return;
for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
iter != original_decl_context->decls_end(); ++iter) {
Decl *decl = *iter;
if (predicate(decl->getKind())) {
if (log) {
ASTDumper ast_dumper(decl);
if (const NamedDecl *context_named_decl =
dyn_cast<NamedDecl>(context_decl))
log->Printf(" FELD[%d] Adding [to %sDecl %s] lexical %sDecl %s",
current_id, context_named_decl->getDeclKindName(),
context_named_decl->getNameAsString().c_str(),
decl->getDeclKindName(), ast_dumper.GetCString());
else
log->Printf(" FELD[%d] Adding lexical %sDecl %s", current_id,
decl->getDeclKindName(), ast_dumper.GetCString());
}
Decl *copied_decl = CopyDecl(decl);
if (!copied_decl)
continue;
if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl)) {
QualType copied_field_type = copied_field->getType();
m_ast_importer_sp->RequireCompleteType(copied_field_type);
}
DeclContext *decl_context_non_const =
const_cast<DeclContext *>(decl_context);
if (copied_decl->getDeclContext() != decl_context) {
if (copied_decl->getDeclContext()->containsDecl(copied_decl))
copied_decl->getDeclContext()->removeDecl(copied_decl);
copied_decl->setDeclContext(decl_context_non_const);
}
if (!decl_context_non_const->containsDecl(copied_decl))
decl_context_non_const->addDeclInternal(copied_decl);
}
}
return;
}
void ClangASTSource::FindExternalVisibleDecls(NameSearchContext &context) {
assert(m_ast_context);
ClangASTMetrics::RegisterVisibleQuery();
const ConstString name(context.m_decl_name.getAsString().c_str());
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
static unsigned int invocation_id = 0;
unsigned int current_id = invocation_id++;
if (log) {
if (!context.m_decl_context)
log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on "
"(ASTContext*)%p for '%s' in a NULL DeclContext",
current_id, static_cast<void *>(m_ast_context),
name.GetCString());
else if (const NamedDecl *context_named_decl =
dyn_cast<NamedDecl>(context.m_decl_context))
log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on "
"(ASTContext*)%p for '%s' in '%s'",
current_id, static_cast<void *>(m_ast_context),
name.GetCString(),
context_named_decl->getNameAsString().c_str());
else
log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on "
"(ASTContext*)%p for '%s' in a '%s'",
current_id, static_cast<void *>(m_ast_context),
name.GetCString(), context.m_decl_context->getDeclKindName());
}
if (HasMerger() && !isa<TranslationUnitDecl>(context.m_decl_context)
/* possibly handle NamespaceDecls here? */) {
if (auto *interface_decl =
dyn_cast<ObjCInterfaceDecl>(context.m_decl_context)) {
ObjCInterfaceDecl *complete_iface_decl =
GetCompleteObjCInterface(interface_decl);
if (complete_iface_decl && (complete_iface_decl != interface_decl)) {
GetMergerUnchecked().ForceRecordOrigin(
interface_decl,
{complete_iface_decl, &complete_iface_decl->getASTContext()});
}
}
GetMergerUnchecked().FindExternalVisibleDeclsByName(context.m_decl_context,
context.m_decl_name);
return; // otherwise we may need to fall back
}
context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
if (const NamespaceDecl *namespace_context =
dyn_cast<NamespaceDecl>(context.m_decl_context)) {
ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer_sp ?
m_ast_importer_sp->GetNamespaceMap(namespace_context) : nullptr;
if (log && log->GetVerbose())
log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
current_id, static_cast<void *>(namespace_map.get()),
static_cast<int>(namespace_map->size()));
if (!namespace_map)
return;
for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(),
e = namespace_map->end();
i != e; ++i) {
if (log)
log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
current_id, i->second.GetName().AsCString(),
i->first->GetFileSpec().GetFilename().GetCString());
FindExternalVisibleDecls(context, i->first, i->second, current_id);
}
} else if (isa<ObjCInterfaceDecl>(context.m_decl_context) && !HasMerger()) {
FindObjCPropertyAndIvarDecls(context);
} else if (!isa<TranslationUnitDecl>(context.m_decl_context)) {
// we shouldn't be getting FindExternalVisibleDecls calls for these
return;
} else {
CompilerDeclContext namespace_decl;
if (log)
log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl,
current_id);
}
if (!context.m_namespace_map->empty()) {
if (log && log->GetVerbose())
log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
current_id,
static_cast<void *>(context.m_namespace_map.get()),
static_cast<int>(context.m_namespace_map->size()));
NamespaceDecl *clang_namespace_decl =
AddNamespace(context, context.m_namespace_map);
if (clang_namespace_decl)
clang_namespace_decl->setHasExternalVisibleStorage();
}
}
bool ClangASTSource::IgnoreName(const ConstString name,
bool ignore_all_dollar_names) {
static const ConstString id_name("id");
static const ConstString Class_name("Class");
if (name == id_name || name == Class_name)
return true;
StringRef name_string_ref = name.GetStringRef();
// The ClangASTSource is not responsible for finding $-names.
if (name_string_ref.empty() ||
(ignore_all_dollar_names && name_string_ref.startswith("$")) ||
name_string_ref.startswith("_$"))
return true;
return false;
}
void ClangASTSource::FindExternalVisibleDecls(
NameSearchContext &context, lldb::ModuleSP module_sp,
CompilerDeclContext &namespace_decl, unsigned int current_id) {
assert(m_ast_context);
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
SymbolContextList sc_list;
const ConstString name(context.m_decl_name.getAsString().c_str());
if (IgnoreName(name, true))
return;
if (module_sp && namespace_decl) {
CompilerDeclContext found_namespace_decl;
SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
if (symbol_vendor) {
SymbolContext null_sc;
found_namespace_decl =
symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
if (found_namespace_decl) {
context.m_namespace_map->push_back(
std::pair<lldb::ModuleSP, CompilerDeclContext>(
module_sp, found_namespace_decl));
if (log)
log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
current_id, name.GetCString(),
module_sp->GetFileSpec().GetFilename().GetCString());
}
}
} else if (!HasMerger()) {
const ModuleList &target_images = m_target->GetImages();
std::lock_guard<std::recursive_mutex> guard(target_images.GetMutex());
for (size_t i = 0, e = target_images.GetSize(); i < e; ++i) {
lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
if (!image)
continue;
CompilerDeclContext found_namespace_decl;
SymbolVendor *symbol_vendor = image->GetSymbolVendor();
if (!symbol_vendor)
continue;
SymbolContext null_sc;
found_namespace_decl =
symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
if (found_namespace_decl) {
context.m_namespace_map->push_back(
std::pair<lldb::ModuleSP, CompilerDeclContext>(
image, found_namespace_decl));
if (log)
log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
current_id, name.GetCString(),
image->GetFileSpec().GetFilename().GetCString());
}
}
}
do {
if (context.m_found.type)
break;
TypeList types;
SymbolContext null_sc;
const bool exact_match = false;
llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
if (module_sp && namespace_decl)
module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types);
else
m_target->GetImages().FindTypes(null_sc, name, exact_match, 1,
searched_symbol_files, types);
if (size_t num_types = types.GetSize()) {
for (size_t ti = 0; ti < num_types; ++ti) {
lldb::TypeSP type_sp = types.GetTypeAtIndex(ti);
if (log) {
const char *name_string = type_sp->GetName().GetCString();
log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
current_id, name.GetCString(),
(name_string ? name_string : "<anonymous>"));
}
CompilerType full_type = type_sp->GetFullCompilerType();
CompilerType copied_clang_type(GuardedCopyType(full_type));
if (!copied_clang_type) {
if (log)
log->Printf(" CAS::FEVD[%u] - Couldn't export a type", current_id);
continue;
}
context.AddTypeDecl(copied_clang_type);
context.m_found.type = true;
break;
}
}
if (!context.m_found.type) {
// Try the modules next.
do {
if (ClangModulesDeclVendor *modules_decl_vendor =
m_target->GetClangModulesDeclVendor()) {
bool append = false;
uint32_t max_matches = 1;
std::vector<clang::NamedDecl *> decls;
if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls))
break;
if (log) {
log->Printf(" CAS::FEVD[%u] Matching entity found for \"%s\" in "
"the modules",
current_id, name.GetCString());
}
clang::NamedDecl *const decl_from_modules = decls[0];
if (llvm::isa<clang::TypeDecl>(decl_from_modules) ||
llvm::isa<clang::ObjCContainerDecl>(decl_from_modules) ||
llvm::isa<clang::EnumConstantDecl>(decl_from_modules)) {
clang::Decl *copied_decl = CopyDecl(decl_from_modules);
clang::NamedDecl *copied_named_decl =
copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr;
if (!copied_named_decl) {
if (log)
log->Printf(
" CAS::FEVD[%u] - Couldn't export a type from the modules",
current_id);
break;
}
context.AddNamedDecl(copied_named_decl);
context.m_found.type = true;
}
}
} while (0);
}
if (!context.m_found.type) {
do {
// Couldn't find any types elsewhere. Try the Objective-C runtime if
// one exists.
lldb::ProcessSP process(m_target->GetProcessSP());
if (!process)
break;
ObjCLanguageRuntime *language_runtime(
process->GetObjCLanguageRuntime());
if (!language_runtime)
break;
DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
if (!decl_vendor)
break;
bool append = false;
uint32_t max_matches = 1;
std::vector<clang::NamedDecl *> decls;
if (!decl_vendor->FindDecls(name, append, max_matches, decls))
break;
if (log) {
log->Printf(
" CAS::FEVD[%u] Matching type found for \"%s\" in the runtime",
current_id, name.GetCString());
}
clang::Decl *copied_decl = CopyDecl(decls[0]);
clang::NamedDecl *copied_named_decl =
copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr;
if (!copied_named_decl) {
if (log)
log->Printf(
" CAS::FEVD[%u] - Couldn't export a type from the runtime",
current_id);
break;
}
context.AddNamedDecl(copied_named_decl);
} while (0);
}
} while (0);
}
template <class D> class TaggedASTDecl {
public:
TaggedASTDecl() : decl(NULL) {}
TaggedASTDecl(D *_decl) : decl(_decl) {}
bool IsValid() const { return (decl != NULL); }
bool IsInvalid() const { return !IsValid(); }
D *operator->() const { return decl; }
D *decl;
};
template <class D2, template <class D> class TD, class D1>
TD<D2> DynCast(TD<D1> source) {
return TD<D2>(dyn_cast<D2>(source.decl));
}
template <class D = Decl> class DeclFromParser;
template <class D = Decl> class DeclFromUser;
template <class D> class DeclFromParser : public TaggedASTDecl<D> {
public:
DeclFromParser() : TaggedASTDecl<D>() {}
DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromUser<D> GetOrigin(ClangASTSource &source);
};
template <class D> class DeclFromUser : public TaggedASTDecl<D> {
public:
DeclFromUser() : TaggedASTDecl<D>() {}
DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromParser<D> Import(ClangASTSource &source);
};
template <class D>
DeclFromUser<D> DeclFromParser<D>::GetOrigin(ClangASTSource &source) {
DeclFromUser<> origin_decl;
source.ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
if (origin_decl.IsInvalid())
return DeclFromUser<D>();
return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
}
template <class D>
DeclFromParser<D> DeclFromUser<D>::Import(ClangASTSource &source) {
DeclFromParser<> parser_generic_decl(source.CopyDecl(this->decl));
if (parser_generic_decl.IsInvalid())
return DeclFromParser<D>();
return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
}
bool ClangASTSource::FindObjCMethodDeclsWithOrigin(
unsigned int current_id, NameSearchContext &context,
ObjCInterfaceDecl *original_interface_decl, const char *log_info) {
const DeclarationName &decl_name(context.m_decl_name);
clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
Selector original_selector;
if (decl_name.isObjCZeroArgSelector()) {
IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
original_selector = original_ctx->Selectors.getSelector(0, &ident);
} else if (decl_name.isObjCOneArgSelector()) {
const std::string &decl_name_string = decl_name.getAsString();
std::string decl_name_string_without_colon(decl_name_string.c_str(),
decl_name_string.length() - 1);
IdentifierInfo *ident =
&original_ctx->Idents.get(decl_name_string_without_colon);
original_selector = original_ctx->Selectors.getSelector(1, &ident);
} else {
SmallVector<IdentifierInfo *, 4> idents;
clang::Selector sel = decl_name.getObjCSelector();
unsigned num_args = sel.getNumArgs();
for (unsigned i = 0; i != num_args; ++i) {
idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
}
original_selector =
original_ctx->Selectors.getSelector(num_args, idents.data());
}
DeclarationName original_decl_name(original_selector);
llvm::SmallVector<NamedDecl *, 1> methods;
ClangASTContext::GetCompleteDecl(original_ctx, original_interface_decl);
if (ObjCMethodDecl *instance_method_decl =
original_interface_decl->lookupInstanceMethod(original_selector)) {
methods.push_back(instance_method_decl);
} else if (ObjCMethodDecl *class_method_decl =
original_interface_decl->lookupClassMethod(
original_selector)) {
methods.push_back(class_method_decl);
}
if (methods.empty()) {
return false;
}
for (NamedDecl *named_decl : methods) {
if (!named_decl)
continue;
ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(named_decl);
if (!result_method)
continue;
Decl *copied_decl = CopyDecl(result_method);
if (!copied_decl)
continue;
ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
if (!copied_method_decl)
continue;
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
if (log) {
ASTDumper dumper((Decl *)copied_method_decl);
log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info,
dumper.GetCString());
}
context.AddNamedDecl(copied_method_decl);
}
return true;
}
void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
if (HasMerger()) {
if (auto *interface_decl = dyn_cast<ObjCInterfaceDecl>(context.m_decl_context)) {
ObjCInterfaceDecl *complete_iface_decl =
GetCompleteObjCInterface(interface_decl);
if (complete_iface_decl && (complete_iface_decl != context.m_decl_context)) {
m_merger_up->ForceRecordOrigin(interface_decl, {complete_iface_decl, &complete_iface_decl->getASTContext()});
}
}
GetMergerUnchecked().FindExternalVisibleDeclsByName(context.m_decl_context,
context.m_decl_name);
return;
}
static unsigned int invocation_id = 0;
unsigned int current_id = invocation_id++;
const DeclarationName &decl_name(context.m_decl_name);
const DeclContext *decl_ctx(context.m_decl_context);
const ObjCInterfaceDecl *interface_decl =
dyn_cast<ObjCInterfaceDecl>(decl_ctx);
if (!interface_decl)
return;
do {
Decl *original_decl = NULL;
ASTContext *original_ctx = NULL;
m_ast_importer_sp->ResolveDeclOrigin(interface_decl, &original_decl,
&original_ctx);
if (!original_decl)
break;
ObjCInterfaceDecl *original_interface_decl =
dyn_cast<ObjCInterfaceDecl>(original_decl);
if (FindObjCMethodDeclsWithOrigin(current_id, context,
original_interface_decl, "at origin"))
return; // found it, no need to look any further
} while (0);
StreamString ss;
if (decl_name.isObjCZeroArgSelector()) {
ss.Printf("%s", decl_name.getAsString().c_str());
} else if (decl_name.isObjCOneArgSelector()) {
ss.Printf("%s", decl_name.getAsString().c_str());
} else {
clang::Selector sel = decl_name.getObjCSelector();
for (unsigned i = 0, e = sel.getNumArgs(); i != e; ++i) {
llvm::StringRef r = sel.getNameForSlot(i);
ss.Printf("%s:", r.str().c_str());
}
}
ss.Flush();
if (ss.GetString().contains("$__lldb"))
return; // we don't need any results
ConstString selector_name(ss.GetString());
if (log)
log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p "
"for selector [%s %s]",
current_id, static_cast<void *>(m_ast_context),
interface_decl->getNameAsString().c_str(),
selector_name.AsCString());
SymbolContextList sc_list;
const bool include_symbols = false;
const bool include_inlines = false;
const bool append = false;
std::string interface_name = interface_decl->getNameAsString();
do {
StreamString ms;
ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
ms.Flush();
ConstString instance_method_name(ms.GetString());
m_target->GetImages().FindFunctions(
instance_method_name, lldb::eFunctionNameTypeFull, include_symbols,
include_inlines, append, sc_list);
if (sc_list.GetSize())
break;
ms.Clear();
ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
ms.Flush();
ConstString class_method_name(ms.GetString());
m_target->GetImages().FindFunctions(
class_method_name, lldb::eFunctionNameTypeFull, include_symbols,
include_inlines, append, sc_list);
if (sc_list.GetSize())
break;
// Fall back and check for methods in categories. If we find methods this
// way, we need to check that they're actually in
// categories on the desired class.
SymbolContextList candidate_sc_list;
m_target->GetImages().FindFunctions(
selector_name, lldb::eFunctionNameTypeSelector, include_symbols,
include_inlines, append, candidate_sc_list);
for (uint32_t ci = 0, ce = candidate_sc_list.GetSize(); ci != ce; ++ci) {
SymbolContext candidate_sc;
if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
continue;
if (!candidate_sc.function)
continue;
const char *candidate_name = candidate_sc.function->GetName().AsCString();
const char *cursor = candidate_name;
if (*cursor != '+' && *cursor != '-')
continue;
++cursor;
if (*cursor != '[')
continue;
++cursor;
size_t interface_len = interface_name.length();
if (strncmp(cursor, interface_name.c_str(), interface_len))
continue;
cursor += interface_len;
if (*cursor == ' ' || *cursor == '(')
sc_list.Append(candidate_sc);
}
} while (0);
if (sc_list.GetSize()) {
// We found a good function symbol. Use that.
for (uint32_t i = 0, e = sc_list.GetSize(); i != e; ++i) {
SymbolContext sc;
if (!sc_list.GetContextAtIndex(i, sc))
continue;
if (!sc.function)
continue;
CompilerDeclContext function_decl_ctx = sc.function->GetDeclContext();
if (!function_decl_ctx)
continue;
ObjCMethodDecl *method_decl =
ClangASTContext::DeclContextGetAsObjCMethodDecl(function_decl_ctx);
if (!method_decl)
continue;
ObjCInterfaceDecl *found_interface_decl =
method_decl->getClassInterface();
if (!found_interface_decl)
continue;
if (found_interface_decl->getName() == interface_decl->getName()) {
Decl *copied_decl = CopyDecl(method_decl);
if (!copied_decl)
continue;
ObjCMethodDecl *copied_method_decl =
dyn_cast<ObjCMethodDecl>(copied_decl);
if (!copied_method_decl)
continue;
if (log) {
ASTDumper dumper((Decl *)copied_method_decl);
log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id,
dumper.GetCString());
}
context.AddNamedDecl(copied_method_decl);
}
}
return;
}
// Try the debug information.
do {
ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(
const_cast<ObjCInterfaceDecl *>(interface_decl));
if (!complete_interface_decl)
break;
// We found the complete interface. The runtime never needs to be queried
// in this scenario.
DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(
complete_interface_decl);
if (complete_interface_decl == interface_decl)
break; // already checked this one
if (log)
log->Printf("CAS::FOPD[%d] trying origin "
"(ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
current_id, static_cast<void *>(complete_interface_decl),
static_cast<void *>(&complete_iface_decl->getASTContext()));
FindObjCMethodDeclsWithOrigin(current_id, context, complete_interface_decl,
"in debug info");
return;
} while (0);
do {
// Check the modules only if the debug information didn't have a complete
// interface.
if (ClangModulesDeclVendor *modules_decl_vendor =
m_target->GetClangModulesDeclVendor()) {
ConstString interface_name(interface_decl->getNameAsString().c_str());
bool append = false;
uint32_t max_matches = 1;
std::vector<clang::NamedDecl *> decls;
if (!modules_decl_vendor->FindDecls(interface_name, append, max_matches,
decls))
break;
ObjCInterfaceDecl *interface_decl_from_modules =
dyn_cast<ObjCInterfaceDecl>(decls[0]);
if (!interface_decl_from_modules)
break;
if (FindObjCMethodDeclsWithOrigin(
current_id, context, interface_decl_from_modules, "in modules"))
return;
}
} while (0);
do {
// Check the runtime only if the debug information didn't have a complete
// interface and the modules don't get us anywhere.
lldb::ProcessSP process(m_target->GetProcessSP());
if (!process)
break;
ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
if (!language_runtime)
break;
DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
if (!decl_vendor)
break;
ConstString interface_name(interface_decl->getNameAsString().c_str());
bool append = false;
uint32_t max_matches = 1;
std::vector<clang::NamedDecl *> decls;
if (!decl_vendor->FindDecls(interface_name, append, max_matches, decls))
break;
ObjCInterfaceDecl *runtime_interface_decl =
dyn_cast<ObjCInterfaceDecl>(decls[0]);
if (!runtime_interface_decl)
break;
FindObjCMethodDeclsWithOrigin(current_id, context, runtime_interface_decl,
"in runtime");
} while (0);
}
static bool FindObjCPropertyAndIvarDeclsWithOrigin(
unsigned int current_id, NameSearchContext &context, ClangASTSource &source,
DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
if (origin_iface_decl.IsInvalid())
return false;
std::string name_str = context.m_decl_name.getAsString();
StringRef name(name_str);
IdentifierInfo &name_identifier(
origin_iface_decl->getASTContext().Idents.get(name));
DeclFromUser<ObjCPropertyDecl> origin_property_decl(
origin_iface_decl->FindPropertyDeclaration(
&name_identifier, ObjCPropertyQueryKind::OBJC_PR_query_instance));
bool found = false;
if (origin_property_decl.IsValid()) {
DeclFromParser<ObjCPropertyDecl> parser_property_decl(
origin_property_decl.Import(source));
if (parser_property_decl.IsValid()) {
if (log) {
ASTDumper dumper((Decl *)parser_property_decl.decl);
log->Printf(" CAS::FOPD[%d] found %s", current_id,
dumper.GetCString());
}
context.AddNamedDecl(parser_property_decl.decl);
found = true;
}
}
DeclFromUser<ObjCIvarDecl> origin_ivar_decl(
origin_iface_decl->getIvarDecl(&name_identifier));
if (origin_ivar_decl.IsValid()) {
DeclFromParser<ObjCIvarDecl> parser_ivar_decl(
origin_ivar_decl.Import(source));
if (parser_ivar_decl.IsValid()) {
if (log) {
ASTDumper dumper((Decl *)parser_ivar_decl.decl);
log->Printf(" CAS::FOPD[%d] found %s", current_id,
dumper.GetCString());
}
context.AddNamedDecl(parser_ivar_decl.decl);
found = true;
}
}
return found;
}
void ClangASTSource::FindObjCPropertyAndIvarDecls(NameSearchContext &context) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
static unsigned int invocation_id = 0;
unsigned int current_id = invocation_id++;
DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(
cast<ObjCInterfaceDecl>(context.m_decl_context));
DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(
parser_iface_decl.GetOrigin(*this));
ConstString class_name(parser_iface_decl->getNameAsString().c_str());
if (log)
log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on "
"(ASTContext*)%p for '%s.%s'",
current_id, static_cast<void *>(m_ast_context),
parser_iface_decl->getNameAsString().c_str(),
context.m_decl_name.getAsString().c_str());
if (FindObjCPropertyAndIvarDeclsWithOrigin(
current_id, context, *this, origin_iface_decl))
return;
if (log)
log->Printf("CAS::FOPD[%d] couldn't find the property on origin "
"(ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching "
"elsewhere...",
current_id, static_cast<const void *>(origin_iface_decl.decl),
static_cast<void *>(&origin_iface_decl->getASTContext()));
SymbolContext null_sc;
TypeList type_list;
do {
ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(
const_cast<ObjCInterfaceDecl *>(parser_iface_decl.decl));
if (!complete_interface_decl)
break;
// We found the complete interface. The runtime never needs to be queried
// in this scenario.
DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(
complete_interface_decl);
if (complete_iface_decl.decl == origin_iface_decl.decl)
break; // already checked this one
if (log)
log->Printf("CAS::FOPD[%d] trying origin "
"(ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
current_id,
static_cast<const void *>(complete_iface_decl.decl),
static_cast<void *>(&complete_iface_decl->getASTContext()));
FindObjCPropertyAndIvarDeclsWithOrigin(current_id, context, *this,
complete_iface_decl);
return;
} while (0);
do {
// Check the modules only if the debug information didn't have a complete
// interface.
ClangModulesDeclVendor *modules_decl_vendor =
m_target->GetClangModulesDeclVendor();
if (!modules_decl_vendor)
break;
bool append = false;
uint32_t max_matches = 1;
std::vector<clang::NamedDecl *> decls;
if (!modules_decl_vendor->FindDecls(class_name, append, max_matches, decls))
break;
DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_modules(
dyn_cast<ObjCInterfaceDecl>(decls[0]));
if (!interface_decl_from_modules.IsValid())
break;
if (log)
log->Printf(
"CAS::FOPD[%d] trying module "
"(ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
current_id,
static_cast<const void *>(interface_decl_from_modules.decl),
static_cast<void *>(&interface_decl_from_modules->getASTContext()));
if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id, context, *this,
interface_decl_from_modules))
return;
} while (0);
do {
// Check the runtime only if the debug information didn't have a complete
// interface
// and nothing was in the modules.
lldb::ProcessSP process(m_target->GetProcessSP());
if (!process)
return;
ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
if (!language_runtime)
return;
DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
if (!decl_vendor)
break;
bool append = false;
uint32_t max_matches = 1;
std::vector<clang::NamedDecl *> decls;
if (!decl_vendor->FindDecls(class_name, append, max_matches, decls))
break;
DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_runtime(
dyn_cast<ObjCInterfaceDecl>(decls[0]));
if (!interface_decl_from_runtime.IsValid())
break;
if (log)
log->Printf(
"CAS::FOPD[%d] trying runtime "
"(ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
current_id,
static_cast<const void *>(interface_decl_from_runtime.decl),
static_cast<void *>(&interface_decl_from_runtime->getASTContext()));
if (FindObjCPropertyAndIvarDeclsWithOrigin(
current_id, context, *this, interface_decl_from_runtime))
return;
} while (0);
}
typedef llvm::DenseMap<const FieldDecl *, uint64_t> FieldOffsetMap;
typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetMap;
template <class D, class O>
static bool ImportOffsetMap(llvm::DenseMap<const D *, O> &destination_map,
llvm::DenseMap<const D *, O> &source_map,
ClangASTSource &source) {
// When importing fields into a new record, clang has a hard requirement that
// fields be imported in field offset order. Since they are stored in a
// DenseMap
// with a pointer as the key type, this means we cannot simply iterate over
// the
// map, as the order will be non-deterministic. Instead we have to sort by
// the offset
// and then insert in sorted order.
typedef llvm::DenseMap<const D *, O> MapType;
typedef typename MapType::value_type PairType;
std::vector<PairType> sorted_items;
sorted_items.reserve(source_map.size());
sorted_items.assign(source_map.begin(), source_map.end());
std::sort(sorted_items.begin(), sorted_items.end(),
[](const PairType &lhs, const PairType &rhs) {
return lhs.second < rhs.second;
});
for (const auto &item : sorted_items) {
DeclFromUser<D> user_decl(const_cast<D *>(item.first));
DeclFromParser<D> parser_decl(user_decl.Import(source));
if (parser_decl.IsInvalid())
return false;
destination_map.insert(
std::pair<const D *, O>(parser_decl.decl, item.second));
}
return true;
}
template <bool IsVirtual>
bool ExtractBaseOffsets(const ASTRecordLayout &record_layout,
DeclFromUser<const CXXRecordDecl> &record,
BaseOffsetMap &base_offsets) {
for (CXXRecordDecl::base_class_const_iterator
bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
be = (IsVirtual ? record->vbases_end() : record->bases_end());
bi != be; ++bi) {
if (!IsVirtual && bi->isVirtual())
continue;
const clang::Type *origin_base_type = bi->getType().getTypePtr();
const clang::RecordType *origin_base_record_type =
origin_base_type->getAs<RecordType>();
if (!origin_base_record_type)
return false;
DeclFromUser<RecordDecl> origin_base_record(
origin_base_record_type->getDecl());
if (origin_base_record.IsInvalid())
return false;
DeclFromUser<CXXRecordDecl> origin_base_cxx_record(
DynCast<CXXRecordDecl>(origin_base_record));
if (origin_base_cxx_record.IsInvalid())
return false;
CharUnits base_offset;
if (IsVirtual)
base_offset =
record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
else
base_offset =
record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(
origin_base_cxx_record.decl, base_offset));
}
return true;
}
bool ClangASTSource::layoutRecordType(const RecordDecl *record, uint64_t &size,
uint64_t &alignment,
FieldOffsetMap &field_offsets,
BaseOffsetMap &base_offsets,
BaseOffsetMap &virtual_base_offsets) {
ClangASTMetrics::RegisterRecordLayout();
static unsigned int invocation_id = 0;
unsigned int current_id = invocation_id++;
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
if (log)
log->Printf("LayoutRecordType[%u] on (ASTContext*)%p for (RecordDecl*)%p "
"[name = '%s']",
current_id, static_cast<void *>(m_ast_context),
static_cast<const void *>(record),
record->getNameAsString().c_str());
DeclFromParser<const RecordDecl> parser_record(record);
DeclFromUser<const RecordDecl> origin_record(
parser_record.GetOrigin(*this));
if (origin_record.IsInvalid())
return false;
FieldOffsetMap origin_field_offsets;
BaseOffsetMap origin_base_offsets;
BaseOffsetMap origin_virtual_base_offsets;
ClangASTContext::GetCompleteDecl(
&origin_record->getASTContext(),
const_cast<RecordDecl *>(origin_record.decl));
clang::RecordDecl *definition = origin_record.decl->getDefinition();
if (!definition || !definition->isCompleteDefinition())
return false;
const ASTRecordLayout &record_layout(
origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
int field_idx = 0, field_count = record_layout.getFieldCount();
for (RecordDecl::field_iterator fi = origin_record->field_begin(),
fe = origin_record->field_end();
fi != fe; ++fi) {
if (field_idx >= field_count)
return false; // Layout didn't go well. Bail out.
uint64_t field_offset = record_layout.getFieldOffset(field_idx);
origin_field_offsets.insert(
std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
field_idx++;
}
lldbassert(&record->getASTContext() == m_ast_context);
DeclFromUser<const CXXRecordDecl> origin_cxx_record(
DynCast<const CXXRecordDecl>(origin_record));
if (origin_cxx_record.IsValid()) {
if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record,
origin_base_offsets) ||
!ExtractBaseOffsets<true>(record_layout, origin_cxx_record,
origin_virtual_base_offsets))
return false;
}
if (!ImportOffsetMap(field_offsets, origin_field_offsets, *this) ||
!ImportOffsetMap(base_offsets, origin_base_offsets, *this) ||
!ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets,
*this))
return false;
size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
alignment = record_layout.getAlignment().getQuantity() *
m_ast_context->getCharWidth();
if (log) {
log->Printf("LRT[%u] returned:", current_id);
log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id,
static_cast<const void *>(origin_record.decl));
log->Printf("LRT[%u] Size = %" PRId64, current_id, size);
log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment);
log->Printf("LRT[%u] Fields:", current_id);
for (RecordDecl::field_iterator fi = record->field_begin(),
fe = record->field_end();
fi != fe; ++fi) {
log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64
" bits",
current_id, static_cast<void *>(*fi),
fi->getNameAsString().c_str(), field_offsets[*fi]);
}
DeclFromParser<const CXXRecordDecl> parser_cxx_record =
DynCast<const CXXRecordDecl>(parser_record);
if (parser_cxx_record.IsValid()) {
log->Printf("LRT[%u] Bases:", current_id);
for (CXXRecordDecl::base_class_const_iterator
bi = parser_cxx_record->bases_begin(),
be = parser_cxx_record->bases_end();
bi != be; ++bi) {
bool is_virtual = bi->isVirtual();
QualType base_type = bi->getType();
const RecordType *base_record_type = base_type->getAs<RecordType>();
DeclFromParser<RecordDecl> base_record(base_record_type->getDecl());
DeclFromParser<CXXRecordDecl> base_cxx_record =
DynCast<CXXRecordDecl>(base_record);
log->Printf(
"LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64
" chars",
current_id, (is_virtual ? "Virtual " : ""),
static_cast<void *>(base_cxx_record.decl),
base_cxx_record.decl->getNameAsString().c_str(),
(is_virtual
? virtual_base_offsets[base_cxx_record.decl].getQuantity()
: base_offsets[base_cxx_record.decl].getQuantity()));
}
} else {
log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
}
}
return true;
}
void ClangASTSource::CompleteNamespaceMap(
ClangASTImporter::NamespaceMapSP &namespace_map, const ConstString &name,
ClangASTImporter::NamespaceMapSP &parent_map) const {
static unsigned int invocation_id = 0;
unsigned int current_id = invocation_id++;
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
if (log) {
if (parent_map && parent_map->size())
log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for "
"namespace %s in namespace %s",
current_id, static_cast<void *>(m_ast_context),
name.GetCString(),
parent_map->begin()->second.GetName().AsCString());
else
log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for "
"namespace %s",
current_id, static_cast<void *>(m_ast_context),
name.GetCString());
}
if (parent_map) {
for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(),
e = parent_map->end();
i != e; ++i) {
CompilerDeclContext found_namespace_decl;
lldb::ModuleSP module_sp = i->first;
CompilerDeclContext module_parent_namespace_decl = i->second;
SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
if (!symbol_vendor)
continue;
SymbolContext null_sc;
found_namespace_decl = symbol_vendor->FindNamespace(
null_sc, name, &module_parent_namespace_decl);
if (!found_namespace_decl)
continue;
namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>(
module_sp, found_namespace_decl));
if (log)
log->Printf(" CMN[%u] Found namespace %s in module %s", current_id,
name.GetCString(),
module_sp->GetFileSpec().GetFilename().GetCString());
}
} else {
const ModuleList &target_images = m_target->GetImages();
std::lock_guard<std::recursive_mutex> guard(target_images.GetMutex());
CompilerDeclContext null_namespace_decl;
for (size_t i = 0, e = target_images.GetSize(); i < e; ++i) {
lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
if (!image)
continue;
CompilerDeclContext found_namespace_decl;
SymbolVendor *symbol_vendor = image->GetSymbolVendor();
if (!symbol_vendor)
continue;
SymbolContext null_sc;
found_namespace_decl =
symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
if (!found_namespace_decl)
continue;
namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>(
image, found_namespace_decl));
if (log)
log->Printf(" CMN[%u] Found namespace %s in module %s", current_id,
name.GetCString(),
image->GetFileSpec().GetFilename().GetCString());
}
}
}
NamespaceDecl *ClangASTSource::AddNamespace(
NameSearchContext &context,
ClangASTImporter::NamespaceMapSP &namespace_decls) {
if (!namespace_decls)
return nullptr;
const CompilerDeclContext &namespace_decl = namespace_decls->begin()->second;
clang::ASTContext *src_ast =
ClangASTContext::DeclContextGetClangASTContext(namespace_decl);
if (!src_ast)
return nullptr;
clang::NamespaceDecl *src_namespace_decl =
ClangASTContext::DeclContextGetAsNamespaceDecl(namespace_decl);
if (!src_namespace_decl)
return nullptr;
Decl *copied_decl = CopyDecl(src_namespace_decl);
if (!copied_decl)
return nullptr;
NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
if (!copied_namespace_decl)
return nullptr;
context.m_decls.push_back(copied_namespace_decl);
m_ast_importer_sp->RegisterNamespaceMap(copied_namespace_decl,
namespace_decls);
return dyn_cast<NamespaceDecl>(copied_decl);
}
clang::QualType ClangASTSource::CopyTypeWithMerger(
clang::ASTContext &from_context,
clang::ExternalASTMerger &merger,
clang::QualType type) {
if (!merger.HasImporterForOrigin(from_context)) {
lldbassert(0 && "Couldn't find the importer for a source context!");
return QualType();
}
return merger.ImporterForOrigin(from_context).Import(type);
}
clang::Decl *ClangASTSource::CopyDecl(Decl *src_decl) {
clang::ASTContext &from_context = src_decl->getASTContext();
if (m_ast_importer_sp) {
return m_ast_importer_sp->CopyDecl(m_ast_context, &from_context, src_decl);
} else if (m_merger_up) {
if (!m_merger_up->HasImporterForOrigin(from_context)) {
lldbassert(0 && "Couldn't find the importer for a source context!");
return nullptr;
}
return m_merger_up->ImporterForOrigin(from_context).Import(src_decl);
} else {
lldbassert(0 && "No mechanism for copying a decl!");
return nullptr;
}
}
bool ClangASTSource::ResolveDeclOrigin(const clang::Decl *decl,
clang::Decl **original_decl,
clang::ASTContext **original_ctx) {
if (m_ast_importer_sp) {
return m_ast_importer_sp->ResolveDeclOrigin(decl, original_decl,
original_ctx);
} else if (m_merger_up) {
return false; // Implement this correctly in ExternalASTMerger
} else {
// this can happen early enough that no ExternalASTSource is installed.
return false;
}
}
clang::ExternalASTMerger &ClangASTSource::GetMergerUnchecked() {
lldbassert(m_merger_up != nullptr);
return *m_merger_up;
}
CompilerType ClangASTSource::GuardedCopyType(const CompilerType &src_type) {
ClangASTContext *src_ast =
llvm::dyn_cast_or_null<ClangASTContext>(src_type.GetTypeSystem());
if (src_ast == nullptr)
return CompilerType();
ClangASTMetrics::RegisterLLDBImport();
SetImportInProgress(true);
QualType copied_qual_type;
if (m_ast_importer_sp) {
copied_qual_type =
m_ast_importer_sp->CopyType(m_ast_context, src_ast->getASTContext(),
ClangUtil::GetQualType(src_type));
} else if (m_merger_up) {
copied_qual_type =
CopyTypeWithMerger(*src_ast->getASTContext(), *m_merger_up,
ClangUtil::GetQualType(src_type));
} else {
lldbassert(0 && "No mechanism for copying a type!");
return CompilerType();
}
SetImportInProgress(false);
if (copied_qual_type.getAsOpaquePtr() &&
copied_qual_type->getCanonicalTypeInternal().isNull())
// this shouldn't happen, but we're hardening because the AST importer seems
// to be generating bad types
// on occasion.
return CompilerType();
return CompilerType(m_ast_context, copied_qual_type);
}
clang::NamedDecl *NameSearchContext::AddVarDecl(const CompilerType &type) {
assert(type && "Type for variable must be valid!");
if (!type.IsValid())
return NULL;
ClangASTContext *lldb_ast =
llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
if (!lldb_ast)
return NULL;
IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
clang::ASTContext *ast = lldb_ast->getASTContext();
clang::NamedDecl *Decl = VarDecl::Create(
*ast, const_cast<DeclContext *>(m_decl_context), SourceLocation(),
SourceLocation(), ii, ClangUtil::GetQualType(type), 0, SC_Static);
m_decls.push_back(Decl);
return Decl;
}
clang::NamedDecl *NameSearchContext::AddFunDecl(const CompilerType &type,
bool extern_c) {
assert(type && "Type for variable must be valid!");
if (!type.IsValid())
return NULL;
if (m_function_types.count(type))
return NULL;
ClangASTContext *lldb_ast =
llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
if (!lldb_ast)
return NULL;
m_function_types.insert(type);
QualType qual_type(ClangUtil::GetQualType(type));
clang::ASTContext *ast = lldb_ast->getASTContext();
const bool isInlineSpecified = false;
const bool hasWrittenPrototype = true;
const bool isConstexprSpecified = false;
clang::DeclContext *context = const_cast<DeclContext *>(m_decl_context);
if (extern_c) {
context = LinkageSpecDecl::Create(
*ast, context, SourceLocation(), SourceLocation(),
clang::LinkageSpecDecl::LanguageIDs::lang_c, false);
}
// Pass the identifier info for functions the decl_name is needed for
// operators
clang::DeclarationName decl_name =
m_decl_name.getNameKind() == DeclarationName::Identifier
? m_decl_name.getAsIdentifierInfo()
: m_decl_name;
clang::FunctionDecl *func_decl = FunctionDecl::Create(
*ast, context, SourceLocation(), SourceLocation(), decl_name, qual_type,
NULL, SC_Extern, isInlineSpecified, hasWrittenPrototype,
isConstexprSpecified);
// We have to do more than just synthesize the FunctionDecl. We have to
// synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
// this, we raid the function's FunctionProtoType for types.
const FunctionProtoType *func_proto_type =
qual_type.getTypePtr()->getAs<FunctionProtoType>();
if (func_proto_type) {
unsigned NumArgs = func_proto_type->getNumParams();
unsigned ArgIndex;
SmallVector<ParmVarDecl *, 5> parm_var_decls;
for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex) {
QualType arg_qual_type(func_proto_type->getParamType(ArgIndex));
parm_var_decls.push_back(ParmVarDecl::Create(
*ast, const_cast<DeclContext *>(context), SourceLocation(),
SourceLocation(), NULL, arg_qual_type, NULL, SC_Static, NULL));
}
func_decl->setParams(ArrayRef<ParmVarDecl *>(parm_var_decls));
} else {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
if (log)
log->Printf("Function type wasn't a FunctionProtoType");
}
m_decls.push_back(func_decl);
return func_decl;
}
clang::NamedDecl *NameSearchContext::AddGenericFunDecl() {
FunctionProtoType::ExtProtoInfo proto_info;
proto_info.Variadic = true;
QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType(
m_ast_source.m_ast_context->UnknownAnyTy, // result
ArrayRef<QualType>(), // argument types
proto_info));
return AddFunDecl(
CompilerType(m_ast_source.m_ast_context, generic_function_type), true);
}
clang::NamedDecl *
NameSearchContext::AddTypeDecl(const CompilerType &clang_type) {
if (ClangUtil::IsClangType(clang_type)) {
QualType qual_type = ClangUtil::GetQualType(clang_type);
if (const TypedefType *typedef_type =
llvm::dyn_cast<TypedefType>(qual_type)) {
TypedefNameDecl *typedef_name_decl = typedef_type->getDecl();
m_decls.push_back(typedef_name_decl);
return (NamedDecl *)typedef_name_decl;
} else if (const TagType *tag_type = qual_type->getAs<TagType>()) {
TagDecl *tag_decl = tag_type->getDecl();
m_decls.push_back(tag_decl);
return tag_decl;
} else if (const ObjCObjectType *objc_object_type =
qual_type->getAs<ObjCObjectType>()) {
ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
m_decls.push_back((NamedDecl *)interface_decl);
return (NamedDecl *)interface_decl;
}
}
return NULL;
}
void NameSearchContext::AddLookupResult(clang::DeclContextLookupResult result) {
for (clang::NamedDecl *decl : result)
m_decls.push_back(decl);
}
void NameSearchContext::AddNamedDecl(clang::NamedDecl *decl) {
m_decls.push_back(decl);
}
|