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
|
//===--- ClangDerivedConformances.cpp -------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "ClangDerivedConformances.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/ClangImporter/ClangImporterRequests.h"
#include "clang/Sema/DelayedDiagnostic.h"
#include "clang/Sema/Overload.h"
using namespace swift;
using namespace swift::importer;
/// Alternative to `NominalTypeDecl::lookupDirect`.
/// This function does not attempt to load extensions of the nominal decl.
static TinyPtrVector<ValueDecl *>
lookupDirectWithoutExtensions(NominalTypeDecl *decl, Identifier id) {
ASTContext &ctx = decl->getASTContext();
auto *importer = static_cast<ClangImporter *>(ctx.getClangModuleLoader());
TinyPtrVector<ValueDecl *> result;
if (id.isOperator()) {
auto underlyingId =
ctx.getIdentifier(getPrivateOperatorName(std::string(id)));
TinyPtrVector<ValueDecl *> underlyingFuncs = evaluateOrDefault(
ctx.evaluator, ClangRecordMemberLookup({decl, underlyingId}), {});
for (auto it : underlyingFuncs) {
if (auto synthesizedFunc =
importer->getCXXSynthesizedOperatorFunc(cast<FuncDecl>(it)))
result.push_back(synthesizedFunc);
}
} else {
// See if there is a Clang decl with the given name.
result = evaluateOrDefault(ctx.evaluator,
ClangRecordMemberLookup({decl, id}), {});
}
// Check if there are any synthesized Swift members that match the name.
for (auto member : decl->getCurrentMembersWithoutLoading()) {
if (auto namedMember = dyn_cast<ValueDecl>(member)) {
if (namedMember->hasName() && !namedMember->getName().isSpecial() &&
namedMember->getName().getBaseIdentifier().is(id.str()) &&
// Make sure we don't add duplicate entries, as that would wrongly
// imply that lookup is ambiguous.
!llvm::is_contained(result, namedMember)) {
result.push_back(namedMember);
}
}
}
return result;
}
template <typename Decl>
static Decl *lookupDirectSingleWithoutExtensions(NominalTypeDecl *decl,
Identifier id) {
auto results = lookupDirectWithoutExtensions(decl, id);
if (results.size() != 1)
return nullptr;
return dyn_cast<Decl>(results.front());
}
/// Similar to ModuleDecl::conformsToProtocol, but doesn't introduce a
/// dependency on Sema.
static bool isConcreteAndValid(ProtocolConformanceRef conformanceRef,
ModuleDecl *module) {
if (conformanceRef.isInvalid())
return false;
if (!conformanceRef.isConcrete())
return false;
auto conformance = conformanceRef.getConcrete();
auto subMap = conformance->getSubstitutionMap();
return llvm::all_of(subMap.getConformances(),
[&](ProtocolConformanceRef each) -> bool {
return isConcreteAndValid(each, module);
});
}
static FuncDecl *getInsertFunc(NominalTypeDecl *decl,
TypeAliasDecl *valueType) {
ASTContext &ctx = decl->getASTContext();
auto insertId = ctx.getIdentifier("__insertUnsafe");
auto inserts = lookupDirectWithoutExtensions(decl, insertId);
FuncDecl *insert = nullptr;
for (auto candidate : inserts) {
if (auto candidateMethod = dyn_cast<FuncDecl>(candidate)) {
if (!candidateMethod->hasParameterList())
continue;
auto params = candidateMethod->getParameters();
if (params->size() != 1)
continue;
auto param = params->front();
if (param->getTypeInContext()->getCanonicalType() !=
valueType->getUnderlyingType()->getCanonicalType())
continue;
insert = candidateMethod;
break;
}
}
return insert;
}
static bool isStdDecl(const clang::CXXRecordDecl *clangDecl,
llvm::ArrayRef<StringRef> names) {
if (!clangDecl->isInStdNamespace())
return false;
if (!clangDecl->getIdentifier())
return false;
StringRef name = clangDecl->getName();
return llvm::is_contained(names, name);
}
static clang::TypeDecl *
getIteratorCategoryDecl(const clang::CXXRecordDecl *clangDecl) {
clang::IdentifierInfo *iteratorCategoryDeclName =
&clangDecl->getASTContext().Idents.get("iterator_category");
auto iteratorCategories = clangDecl->lookup(iteratorCategoryDeclName);
// If this is a templated typedef, Clang might have instantiated several
// equivalent typedef decls. If they aren't equivalent, Clang has already
// complained about this. Let's assume that they are equivalent. (see
// filterNonConflictingPreviousTypedefDecls in clang/Sema/SemaDecl.cpp)
if (iteratorCategories.empty())
return nullptr;
auto iteratorCategory = iteratorCategories.front();
return dyn_cast_or_null<clang::TypeDecl>(iteratorCategory);
}
static ValueDecl *lookupOperator(NominalTypeDecl *decl, Identifier id,
function_ref<bool(ValueDecl *)> isValid) {
// First look for operator declared as a member.
auto memberResults = lookupDirectWithoutExtensions(decl, id);
for (const auto &member : memberResults) {
if (isValid(member))
return member;
}
// If no member operator was found, look for out-of-class definitions in the
// same module.
auto module = decl->getModuleContext();
SmallVector<ValueDecl *> nonMemberResults;
module->lookupValue(id, NLKind::UnqualifiedLookup, nonMemberResults);
for (const auto &nonMember : nonMemberResults) {
if (isValid(nonMember))
return nonMember;
}
return nullptr;
}
static ValueDecl *getEqualEqualOperator(NominalTypeDecl *decl) {
auto isValid = [&](ValueDecl *equalEqualOp) -> bool {
auto equalEqual = dyn_cast<FuncDecl>(equalEqualOp);
if (!equalEqual || !equalEqual->hasParameterList())
return false;
auto params = equalEqual->getParameters();
if (params->size() != 2)
return false;
auto lhs = params->get(0);
auto rhs = params->get(1);
if (lhs->isInOut() || rhs->isInOut())
return false;
auto lhsTy = lhs->getTypeInContext();
auto rhsTy = rhs->getTypeInContext();
if (!lhsTy || !rhsTy)
return false;
auto lhsNominal = lhsTy->getAnyNominal();
auto rhsNominal = rhsTy->getAnyNominal();
if (lhsNominal != rhsNominal || lhsNominal != decl)
return false;
return true;
};
return lookupOperator(decl, decl->getASTContext().Id_EqualsOperator, isValid);
}
static FuncDecl *getMinusOperator(NominalTypeDecl *decl) {
auto binaryIntegerProto =
decl->getASTContext().getProtocol(KnownProtocolKind::BinaryInteger);
auto module = decl->getModuleContext();
auto isValid = [&](ValueDecl *minusOp) -> bool {
auto minus = dyn_cast<FuncDecl>(minusOp);
if (!minus || !minus->hasParameterList())
return false;
auto params = minus->getParameters();
if (params->size() != 2)
return false;
auto lhs = params->get(0);
auto rhs = params->get(1);
if (lhs->isInOut() || rhs->isInOut())
return false;
auto lhsTy = lhs->getTypeInContext();
auto rhsTy = rhs->getTypeInContext();
if (!lhsTy || !rhsTy)
return false;
auto lhsNominal = lhsTy->getAnyNominal();
auto rhsNominal = rhsTy->getAnyNominal();
if (lhsNominal != rhsNominal || lhsNominal != decl)
return false;
auto returnTy = minus->getResultInterfaceType();
auto conformanceRef =
module->lookupConformance(returnTy, binaryIntegerProto);
if (!isConcreteAndValid(conformanceRef, module))
return false;
return true;
};
ValueDecl *result =
lookupOperator(decl, decl->getASTContext().getIdentifier("-"), isValid);
return dyn_cast_or_null<FuncDecl>(result);
}
static FuncDecl *getPlusEqualOperator(NominalTypeDecl *decl, Type distanceTy) {
auto isValid = [&](ValueDecl *plusEqualOp) -> bool {
auto plusEqual = dyn_cast<FuncDecl>(plusEqualOp);
if (!plusEqual || !plusEqual->hasParameterList())
return false;
auto params = plusEqual->getParameters();
if (params->size() != 2)
return false;
auto lhs = params->get(0);
auto rhs = params->get(1);
if (rhs->isInOut())
return false;
auto lhsTy = lhs->getTypeInContext();
auto rhsTy = rhs->getTypeInContext();
if (!lhsTy || !rhsTy)
return false;
if (rhsTy->getCanonicalType() != distanceTy->getCanonicalType())
return false;
auto lhsNominal = lhsTy->getAnyNominal();
if (lhsNominal != decl)
return false;
auto returnTy = plusEqual->getResultInterfaceType();
if (!returnTy->isVoid())
return false;
return true;
};
ValueDecl *result =
lookupOperator(decl, decl->getASTContext().getIdentifier("+="), isValid);
return dyn_cast_or_null<FuncDecl>(result);
}
static clang::FunctionDecl *
instantiateTemplatedOperator(ClangImporter::Implementation &impl,
const clang::CXXRecordDecl *classDecl,
clang::BinaryOperatorKind operatorKind) {
clang::ASTContext &clangCtx = impl.getClangASTContext();
clang::Sema &clangSema = impl.getClangSema();
clang::UnresolvedSet<1> ops;
auto qualType = clang::QualType(classDecl->getTypeForDecl(), 0);
auto arg = new (clangCtx)
clang::CXXThisExpr(clang::SourceLocation(), qualType, false);
arg->setType(clang::QualType(classDecl->getTypeForDecl(), 0));
clang::OverloadedOperatorKind opKind =
clang::BinaryOperator::getOverloadedOperator(operatorKind);
clang::OverloadCandidateSet candidateSet(
classDecl->getLocation(), clang::OverloadCandidateSet::CSK_Operator,
clang::OverloadCandidateSet::OperatorRewriteInfo(opKind,
clang::SourceLocation(), false));
clangSema.LookupOverloadedBinOp(candidateSet, opKind, ops, {arg, arg}, true);
clang::OverloadCandidateSet::iterator best;
switch (candidateSet.BestViableFunction(clangSema, clang::SourceLocation(),
best)) {
case clang::OR_Success: {
if (auto clangCallee = best->Function) {
auto lookupTable = impl.findLookupTable(classDecl);
addEntryToLookupTable(*lookupTable, clangCallee, impl.getNameImporter());
return clangCallee;
}
break;
}
case clang::OR_No_Viable_Function:
case clang::OR_Ambiguous:
case clang::OR_Deleted:
break;
}
return nullptr;
}
/// Warning: This function emits an error and stops compilation if the
/// underlying operator function is unavailable in Swift for the current target
/// (see `clang::Sema::DiagnoseAvailabilityOfDecl`).
static bool synthesizeCXXOperator(ClangImporter::Implementation &impl,
const clang::CXXRecordDecl *classDecl,
clang::BinaryOperatorKind operatorKind,
clang::QualType lhsTy, clang::QualType rhsTy,
clang::QualType returnTy) {
auto &clangCtx = impl.getClangASTContext();
auto &clangSema = impl.getClangSema();
clang::OverloadedOperatorKind opKind =
clang::BinaryOperator::getOverloadedOperator(operatorKind);
const char *opSpelling = clang::getOperatorSpelling(opKind);
auto declName = clang::DeclarationName(&clangCtx.Idents.get(opSpelling));
// Determine the Clang decl context where the new operator function will be
// created. We use the translation unit as the decl context of the new
// operator, otherwise, the operator might get imported as a static member
// function of a different type (e.g. an operator declared inside of a C++
// namespace would get imported as a member function of a Swift enum), which
// would make the operator un-discoverable to Swift name lookup.
auto declContext =
const_cast<clang::CXXRecordDecl *>(classDecl)->getDeclContext();
while (!declContext->isTranslationUnit()) {
declContext = declContext->getParent();
}
auto equalEqualTy = clangCtx.getFunctionType(
returnTy, {lhsTy, rhsTy}, clang::FunctionProtoType::ExtProtoInfo());
// Create a `bool operator==(T, T)` function.
auto equalEqualDecl = clang::FunctionDecl::Create(
clangCtx, declContext, clang::SourceLocation(), clang::SourceLocation(),
declName, equalEqualTy, clangCtx.getTrivialTypeSourceInfo(returnTy),
clang::StorageClass::SC_Static);
equalEqualDecl->setImplicit();
equalEqualDecl->setImplicitlyInline();
// If this is a static member function of a class, it needs to be public.
equalEqualDecl->setAccess(clang::AccessSpecifier::AS_public);
// Create the parameters of the function. They are not referenced from source
// code, so they don't need to have a name.
auto lhsParamId = nullptr;
auto lhsTyInfo = clangCtx.getTrivialTypeSourceInfo(lhsTy);
auto lhsParamDecl = clang::ParmVarDecl::Create(
clangCtx, equalEqualDecl, clang::SourceLocation(),
clang::SourceLocation(), lhsParamId, lhsTy, lhsTyInfo,
clang::StorageClass::SC_None, /*DefArg*/ nullptr);
auto lhsParamRefExpr = new (clangCtx) clang::DeclRefExpr(
clangCtx, lhsParamDecl, false, lhsTy, clang::ExprValueKind::VK_LValue,
clang::SourceLocation());
auto rhsParamId = nullptr;
auto rhsTyInfo = clangCtx.getTrivialTypeSourceInfo(rhsTy);
auto rhsParamDecl = clang::ParmVarDecl::Create(
clangCtx, equalEqualDecl, clang::SourceLocation(),
clang::SourceLocation(), rhsParamId, rhsTy, rhsTyInfo,
clang::StorageClass::SC_None, nullptr);
auto rhsParamRefExpr = new (clangCtx) clang::DeclRefExpr(
clangCtx, rhsParamDecl, false, rhsTy, clang::ExprValueKind::VK_LValue,
clang::SourceLocation());
equalEqualDecl->setParams({lhsParamDecl, rhsParamDecl});
// Lookup the `operator==` function that will be called under the hood.
clang::UnresolvedSet<16> operators;
clang::sema::DelayedDiagnosticPool diagPool{
impl.getClangSema().DelayedDiagnostics.getCurrentPool()};
auto diagState = impl.getClangSema().DelayedDiagnostics.push(diagPool);
// Note: calling `CreateOverloadedBinOp` emits an error if the looked up
// function is unavailable for the current target.
auto underlyingCallResult = clangSema.CreateOverloadedBinOp(
clang::SourceLocation(), operatorKind, operators, lhsParamRefExpr,
rhsParamRefExpr);
impl.getClangSema().DelayedDiagnostics.popWithoutEmitting(diagState);
if (!diagPool.empty())
return false;
if (!underlyingCallResult.isUsable())
return false;
auto underlyingCall = underlyingCallResult.get();
auto equalEqualBody = clang::ReturnStmt::Create(
clangCtx, clang::SourceLocation(), underlyingCall, nullptr);
equalEqualDecl->setBody(equalEqualBody);
impl.synthesizedAndAlwaysVisibleDecls.insert(equalEqualDecl);
auto lookupTable = impl.findLookupTable(classDecl);
addEntryToLookupTable(*lookupTable, equalEqualDecl, impl.getNameImporter());
return true;
}
bool swift::isIterator(const clang::CXXRecordDecl *clangDecl) {
return getIteratorCategoryDecl(clangDecl);
}
void swift::conformToCxxIteratorIfNeeded(
ClangImporter::Implementation &impl, NominalTypeDecl *decl,
const clang::CXXRecordDecl *clangDecl) {
PrettyStackTraceDecl trace("conforming to UnsafeCxxInputIterator", decl);
assert(decl);
assert(clangDecl);
ASTContext &ctx = decl->getASTContext();
clang::ASTContext &clangCtx = clangDecl->getASTContext();
if (!ctx.getProtocol(KnownProtocolKind::UnsafeCxxInputIterator))
return;
// We consider a type to be an input iterator if it defines an
// `iterator_category` that inherits from `std::input_iterator_tag`, e.g.
// `using iterator_category = std::input_iterator_tag`.
auto iteratorCategory = getIteratorCategoryDecl(clangDecl);
if (!iteratorCategory)
return;
// If `iterator_category` is a typedef or a using-decl, retrieve the
// underlying struct decl.
clang::CXXRecordDecl *underlyingCategoryDecl = nullptr;
if (auto typedefDecl = dyn_cast<clang::TypedefNameDecl>(iteratorCategory)) {
auto type = typedefDecl->getUnderlyingType();
underlyingCategoryDecl = type->getAsCXXRecordDecl();
} else {
underlyingCategoryDecl = dyn_cast<clang::CXXRecordDecl>(iteratorCategory);
}
if (underlyingCategoryDecl) {
underlyingCategoryDecl = underlyingCategoryDecl->getDefinition();
}
if (!underlyingCategoryDecl)
return;
auto isIteratorCategoryDecl = [&](const clang::CXXRecordDecl *base,
StringRef tag) {
return base->isInStdNamespace() && base->getIdentifier() &&
base->getName() == tag;
};
auto isInputIteratorDecl = [&](const clang::CXXRecordDecl *base) {
return isIteratorCategoryDecl(base, "input_iterator_tag");
};
auto isRandomAccessIteratorDecl = [&](const clang::CXXRecordDecl *base) {
return isIteratorCategoryDecl(base, "random_access_iterator_tag");
};
// Traverse all transitive bases of `underlyingDecl` to check if
// it inherits from `std::input_iterator_tag`.
bool isInputIterator = isInputIteratorDecl(underlyingCategoryDecl);
bool isRandomAccessIterator =
isRandomAccessIteratorDecl(underlyingCategoryDecl);
underlyingCategoryDecl->forallBases([&](const clang::CXXRecordDecl *base) {
if (isInputIteratorDecl(base)) {
isInputIterator = true;
}
if (isRandomAccessIteratorDecl(base)) {
isRandomAccessIterator = true;
isInputIterator = true;
return false;
}
return true;
});
if (!isInputIterator)
return;
// Check if present: `var pointee: Pointee { get }`
auto pointeeId = ctx.getIdentifier("pointee");
auto pointee = lookupDirectSingleWithoutExtensions<VarDecl>(decl, pointeeId);
if (!pointee || pointee->isGetterMutating() || pointee->getTypeInContext()->hasError())
return;
// Check if `var pointee: Pointee` is settable. This is required for the
// conformance to UnsafeCxxMutableInputIterator but is not necessary for
// UnsafeCxxInputIterator.
bool pointeeSettable = pointee->isSettable(nullptr);
// Check if present: `func successor() -> Self`
auto successorId = ctx.getIdentifier("successor");
auto successor =
lookupDirectSingleWithoutExtensions<FuncDecl>(decl, successorId);
if (!successor || successor->isMutating())
return;
auto successorTy = successor->getResultInterfaceType();
if (!successorTy || successorTy->getAnyNominal() != decl)
return;
// Check if present: `func ==`
auto equalEqual = getEqualEqualOperator(decl);
if (!equalEqual) {
// If this class is inherited, `operator==` might be defined for a base
// class. If this is a templated class, `operator==` might be templated as
// well. Try to instantiate it.
clang::FunctionDecl *instantiated = instantiateTemplatedOperator(
impl, clangDecl, clang::BinaryOperatorKind::BO_EQ);
if (instantiated && !impl.isUnavailableInSwift(instantiated)) {
// If `operator==` was instantiated successfully, try to find `func ==`
// again.
equalEqual = getEqualEqualOperator(decl);
if (!equalEqual) {
// If `func ==` still can't be found, it might be defined for a base
// class of the current class.
auto paramTy = clangCtx.getRecordType(clangDecl);
synthesizeCXXOperator(impl, clangDecl, clang::BinaryOperatorKind::BO_EQ,
paramTy, paramTy, clangCtx.BoolTy);
equalEqual = getEqualEqualOperator(decl);
}
}
}
if (!equalEqual)
return;
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Pointee"),
pointee->getTypeInContext());
if (pointeeSettable)
impl.addSynthesizedProtocolAttrs(
decl, {KnownProtocolKind::UnsafeCxxMutableInputIterator});
else
impl.addSynthesizedProtocolAttrs(
decl, {KnownProtocolKind::UnsafeCxxInputIterator});
if (!isRandomAccessIterator ||
!ctx.getProtocol(KnownProtocolKind::UnsafeCxxRandomAccessIterator))
return;
// Try to conform to UnsafeCxxRandomAccessIterator if possible.
// Check if present: `func -`
auto minus = getMinusOperator(decl);
if (!minus) {
clang::FunctionDecl *instantiated = instantiateTemplatedOperator(
impl, clangDecl, clang::BinaryOperatorKind::BO_Sub);
if (instantiated && !impl.isUnavailableInSwift(instantiated)) {
minus = getMinusOperator(decl);
if (!minus) {
clang::QualType returnTy = instantiated->getReturnType();
auto paramTy = clangCtx.getRecordType(clangDecl);
synthesizeCXXOperator(impl, clangDecl,
clang::BinaryOperatorKind::BO_Sub, paramTy,
paramTy, returnTy);
minus = getMinusOperator(decl);
}
}
}
if (!minus)
return;
auto distanceTy = minus->getResultInterfaceType();
// distanceTy conforms to BinaryInteger, this is ensured by getMinusOperator.
auto plusEqual = getPlusEqualOperator(decl, distanceTy);
if (!plusEqual) {
clang::FunctionDecl *instantiated = instantiateTemplatedOperator(
impl, clangDecl, clang::BinaryOperatorKind::BO_AddAssign);
if (instantiated && !impl.isUnavailableInSwift(instantiated)) {
plusEqual = getPlusEqualOperator(decl, distanceTy);
if (!plusEqual) {
clang::QualType returnTy = instantiated->getReturnType();
auto clangMinus = cast<clang::FunctionDecl>(minus->getClangDecl());
auto lhsTy = clangCtx.getRecordType(clangDecl);
auto rhsTy = clangMinus->getReturnType();
synthesizeCXXOperator(impl, clangDecl,
clang::BinaryOperatorKind::BO_AddAssign, lhsTy,
rhsTy, returnTy);
plusEqual = getPlusEqualOperator(decl, distanceTy);
}
}
}
if (!plusEqual)
return;
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Distance"), distanceTy);
if (pointeeSettable)
impl.addSynthesizedProtocolAttrs(
decl, {KnownProtocolKind::UnsafeCxxMutableRandomAccessIterator});
else
impl.addSynthesizedProtocolAttrs(
decl, {KnownProtocolKind::UnsafeCxxRandomAccessIterator});
}
void swift::conformToCxxConvertibleToBoolIfNeeded(
ClangImporter::Implementation &impl, swift::NominalTypeDecl *decl,
const clang::CXXRecordDecl *clangDecl) {
PrettyStackTraceDecl trace("conforming to CxxConvertibleToBool", decl);
assert(decl);
assert(clangDecl);
ASTContext &ctx = decl->getASTContext();
auto conversionId = ctx.getIdentifier("__convertToBool");
auto conversions = lookupDirectWithoutExtensions(decl, conversionId);
// Find a non-mutating overload of `__convertToBool`.
FuncDecl *conversion = nullptr;
for (auto c : conversions) {
auto candidate = dyn_cast<FuncDecl>(c);
if (!candidate || candidate->isMutating())
continue;
if (conversion)
// Overload ambiguity?
return;
conversion = candidate;
}
if (!conversion)
return;
auto conversionTy = conversion->getResultInterfaceType();
if (!conversionTy->isBool())
return;
impl.addSynthesizedProtocolAttrs(decl,
{KnownProtocolKind::CxxConvertibleToBool});
}
void swift::conformToCxxOptionalIfNeeded(
ClangImporter::Implementation &impl, NominalTypeDecl *decl,
const clang::CXXRecordDecl *clangDecl) {
PrettyStackTraceDecl trace("conforming to CxxOptional", decl);
assert(decl);
assert(clangDecl);
ASTContext &ctx = decl->getASTContext();
if (!isStdDecl(clangDecl, {"optional"}))
return;
ProtocolDecl *cxxOptionalProto =
ctx.getProtocol(KnownProtocolKind::CxxOptional);
// If the Cxx module is missing, or does not include one of the necessary
// protocol, bail.
if (!cxxOptionalProto)
return;
auto pointeeId = ctx.getIdentifier("pointee");
auto pointees = lookupDirectWithoutExtensions(decl, pointeeId);
if (pointees.size() != 1)
return;
auto pointee = dyn_cast<VarDecl>(pointees.front());
if (!pointee)
return;
auto pointeeTy = pointee->getInterfaceType();
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Wrapped"), pointeeTy);
impl.addSynthesizedProtocolAttrs(decl, {KnownProtocolKind::CxxOptional});
}
void swift::conformToCxxSequenceIfNeeded(
ClangImporter::Implementation &impl, NominalTypeDecl *decl,
const clang::CXXRecordDecl *clangDecl) {
PrettyStackTraceDecl trace("conforming to CxxSequence", decl);
assert(decl);
assert(clangDecl);
ASTContext &ctx = decl->getASTContext();
ProtocolDecl *cxxIteratorProto =
ctx.getProtocol(KnownProtocolKind::UnsafeCxxInputIterator);
ProtocolDecl *cxxSequenceProto =
ctx.getProtocol(KnownProtocolKind::CxxSequence);
ProtocolDecl *cxxConvertibleProto =
ctx.getProtocol(KnownProtocolKind::CxxConvertibleToCollection);
// If the Cxx module is missing, or does not include one of the necessary
// protocols, bail.
if (!cxxIteratorProto || !cxxSequenceProto)
return;
// Check if present: `func __beginUnsafe() -> RawIterator`
auto beginId = ctx.getIdentifier("__beginUnsafe");
auto begin = lookupDirectSingleWithoutExtensions<FuncDecl>(decl, beginId);
if (!begin)
return;
auto rawIteratorTy = begin->getResultInterfaceType();
// Check if present: `func __endUnsafe() -> RawIterator`
auto endId = ctx.getIdentifier("__endUnsafe");
auto end = lookupDirectSingleWithoutExtensions<FuncDecl>(decl, endId);
if (!end)
return;
// Check if `begin()` and `end()` are non-mutating.
if (begin->isMutating() || end->isMutating())
return;
// Check if `__beginUnsafe` and `__endUnsafe` have the same return type.
auto endTy = end->getResultInterfaceType();
if (!endTy || endTy->getCanonicalType() != rawIteratorTy->getCanonicalType())
return;
// Check if RawIterator conforms to UnsafeCxxInputIterator.
ModuleDecl *module = decl->getModuleContext();
auto rawIteratorConformanceRef =
module->lookupConformance(rawIteratorTy, cxxIteratorProto);
if (!isConcreteAndValid(rawIteratorConformanceRef, module))
return;
auto rawIteratorConformance = rawIteratorConformanceRef.getConcrete();
auto pointeeDecl =
cxxIteratorProto->getAssociatedType(ctx.getIdentifier("Pointee"));
assert(pointeeDecl &&
"UnsafeCxxInputIterator must have a Pointee associated type");
auto pointeeTy = rawIteratorConformance->getTypeWitness(pointeeDecl);
assert(pointeeTy && "valid conformance must have a Pointee witness");
// Take the default definition of `Iterator` from CxxSequence protocol. This
// type is currently `CxxIterator<Self>`.
auto iteratorDecl = cxxSequenceProto->getAssociatedType(ctx.Id_Iterator);
auto iteratorTy = iteratorDecl->getDefaultDefinitionType();
// Substitute generic `Self` parameter.
auto cxxSequenceSelfTy = cxxSequenceProto->getSelfInterfaceType();
auto declSelfTy = decl->getDeclaredInterfaceType();
iteratorTy = iteratorTy.subst(
[&](SubstitutableType *dependentType) {
if (dependentType->isEqual(cxxSequenceSelfTy))
return declSelfTy;
return Type(dependentType);
},
LookUpConformanceInModule(module));
impl.addSynthesizedTypealias(decl, ctx.Id_Element, pointeeTy);
impl.addSynthesizedTypealias(decl, ctx.Id_Iterator, iteratorTy);
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("RawIterator"),
rawIteratorTy);
// Not conforming the type to CxxSequence protocol here:
// The current implementation of CxxSequence triggers extra copies of the C++
// collection when creating a CxxIterator instance. It needs a more efficient
// implementation, which is not possible with the existing Swift features.
// impl.addSynthesizedProtocolAttrs(decl, {KnownProtocolKind::CxxSequence});
// Try to conform to CxxRandomAccessCollection if possible.
auto tryToConformToRandomAccessCollection = [&]() -> bool {
auto cxxRAIteratorProto =
ctx.getProtocol(KnownProtocolKind::UnsafeCxxRandomAccessIterator);
if (!cxxRAIteratorProto ||
!ctx.getProtocol(KnownProtocolKind::CxxRandomAccessCollection))
return false;
// Check if RawIterator conforms to UnsafeCxxRandomAccessIterator.
auto rawIteratorRAConformanceRef =
decl->getModuleContext()->lookupConformance(rawIteratorTy,
cxxRAIteratorProto);
if (!isConcreteAndValid(rawIteratorRAConformanceRef, module))
return false;
// CxxRandomAccessCollection always uses Int as an Index.
auto indexTy = ctx.getIntType();
auto sliceTy = ctx.getSliceType();
sliceTy = sliceTy.subst(
[&](SubstitutableType *dependentType) {
if (dependentType->isEqual(cxxSequenceSelfTy))
return declSelfTy;
return Type(dependentType);
},
LookUpConformanceInModule(module));
auto indicesTy = ctx.getRangeType();
indicesTy = indicesTy.subst(
[&](SubstitutableType *dependentType) {
if (dependentType->isEqual(cxxSequenceSelfTy))
return indexTy;
return Type(dependentType);
},
LookUpConformanceInModule(module));
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Element"), pointeeTy);
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Index"), indexTy);
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Indices"), indicesTy);
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("SubSequence"),
sliceTy);
impl.addSynthesizedProtocolAttrs(
decl, {KnownProtocolKind::CxxRandomAccessCollection});
return true;
};
bool conformedToRAC = tryToConformToRandomAccessCollection();
// If the collection does not support random access, let's still allow the
// developer to explicitly convert a C++ sequence to a Swift Array (making a
// copy of the sequence's elements) by conforming the type to
// CxxCollectionConvertible. This enables an overload of Array.init declared
// in the Cxx module.
if (!conformedToRAC && cxxConvertibleProto) {
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Element"), pointeeTy);
impl.addSynthesizedProtocolAttrs(
decl, {KnownProtocolKind::CxxConvertibleToCollection});
}
}
static bool isStdSetType(const clang::CXXRecordDecl *clangDecl) {
return isStdDecl(clangDecl, {"set", "unordered_set", "multiset"});
}
static bool isStdMapType(const clang::CXXRecordDecl *clangDecl) {
return isStdDecl(clangDecl, {"map", "unordered_map", "multimap"});
}
bool swift::isUnsafeStdMethod(const clang::CXXMethodDecl *methodDecl) {
auto parentDecl =
dyn_cast<clang::CXXRecordDecl>(methodDecl->getDeclContext());
if (!parentDecl)
return false;
if (!isStdSetType(parentDecl) && !isStdMapType(parentDecl))
return false;
if (methodDecl->getDeclName().isIdentifier() &&
methodDecl->getName() == "insert")
return true;
return false;
}
void swift::conformToCxxSetIfNeeded(ClangImporter::Implementation &impl,
NominalTypeDecl *decl,
const clang::CXXRecordDecl *clangDecl) {
PrettyStackTraceDecl trace("conforming to CxxSet", decl);
assert(decl);
assert(clangDecl);
ASTContext &ctx = decl->getASTContext();
// Only auto-conform types from the C++ standard library. Custom user types
// might have a similar interface but different semantics.
if (!isStdSetType(clangDecl))
return;
auto valueType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
decl, ctx.getIdentifier("value_type"));
auto sizeType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
decl, ctx.getIdentifier("size_type"));
if (!valueType || !sizeType)
return;
auto insert = getInsertFunc(decl, valueType);
if (!insert)
return;
impl.addSynthesizedTypealias(decl, ctx.Id_Element,
valueType->getUnderlyingType());
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Size"),
sizeType->getUnderlyingType());
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("InsertionResult"),
insert->getResultInterfaceType());
impl.addSynthesizedProtocolAttrs(decl, {KnownProtocolKind::CxxSet});
// If this isn't a std::multiset, try to also synthesize the conformance to
// CxxUniqueSet.
if (!isStdDecl(clangDecl, {"set", "unordered_set"}))
return;
ProtocolDecl *cxxIteratorProto =
ctx.getProtocol(KnownProtocolKind::UnsafeCxxInputIterator);
if (!cxxIteratorProto)
return;
auto rawMutableIteratorType =
lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
decl, ctx.getIdentifier("iterator"));
if (!rawMutableIteratorType)
return;
auto rawMutableIteratorTy = rawMutableIteratorType->getUnderlyingType();
// Check if RawMutableIterator conforms to UnsafeCxxInputIterator.
ModuleDecl *module = decl->getModuleContext();
auto rawIteratorConformanceRef =
module->lookupConformance(rawMutableIteratorTy, cxxIteratorProto);
if (!isConcreteAndValid(rawIteratorConformanceRef, module))
return;
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("RawMutableIterator"),
rawMutableIteratorTy);
impl.addSynthesizedProtocolAttrs(decl, {KnownProtocolKind::CxxUniqueSet});
}
void swift::conformToCxxPairIfNeeded(ClangImporter::Implementation &impl,
NominalTypeDecl *decl,
const clang::CXXRecordDecl *clangDecl) {
PrettyStackTraceDecl trace("conforming to CxxPair", decl);
assert(decl);
assert(clangDecl);
ASTContext &ctx = decl->getASTContext();
// Only auto-conform types from the C++ standard library. Custom user types
// might have a similar interface but different semantics.
if (!isStdDecl(clangDecl, {"pair"}))
return;
auto firstType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
decl, ctx.getIdentifier("first_type"));
auto secondType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
decl, ctx.getIdentifier("second_type"));
if (!firstType || !secondType)
return;
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("First"),
firstType->getUnderlyingType());
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Second"),
secondType->getUnderlyingType());
impl.addSynthesizedProtocolAttrs(decl, {KnownProtocolKind::CxxPair});
}
void swift::conformToCxxDictionaryIfNeeded(
ClangImporter::Implementation &impl, NominalTypeDecl *decl,
const clang::CXXRecordDecl *clangDecl) {
PrettyStackTraceDecl trace("conforming to CxxDictionary", decl);
assert(decl);
assert(clangDecl);
ASTContext &ctx = decl->getASTContext();
// Only auto-conform types from the C++ standard library. Custom user types
// might have a similar interface but different semantics.
if (!isStdMapType(clangDecl))
return;
auto keyType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
decl, ctx.getIdentifier("key_type"));
auto valueType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
decl, ctx.getIdentifier("mapped_type"));
auto iterType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
decl, ctx.getIdentifier("const_iterator"));
auto mutableIterType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
decl, ctx.getIdentifier("iterator"));
auto sizeType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
decl, ctx.getIdentifier("size_type"));
auto keyValuePairType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
decl, ctx.getIdentifier("value_type"));
if (!keyType || !valueType || !iterType || !mutableIterType || !sizeType ||
!keyValuePairType)
return;
auto insert = getInsertFunc(decl, keyValuePairType);
if (!insert)
return;
ProtocolDecl *cxxInputIteratorProto =
ctx.getProtocol(KnownProtocolKind::UnsafeCxxInputIterator);
ProtocolDecl *cxxMutableInputIteratorProto =
ctx.getProtocol(KnownProtocolKind::UnsafeCxxMutableInputIterator);
if (!cxxInputIteratorProto || !cxxMutableInputIteratorProto)
return;
auto rawIteratorTy = iterType->getUnderlyingType();
auto rawMutableIteratorTy = mutableIterType->getUnderlyingType();
// Check if RawIterator conforms to UnsafeCxxInputIterator.
ModuleDecl *module = decl->getModuleContext();
auto rawIteratorConformanceRef =
module->lookupConformance(rawIteratorTy, cxxInputIteratorProto);
if (!isConcreteAndValid(rawIteratorConformanceRef, module))
return;
// Check if RawMutableIterator conforms to UnsafeCxxMutableInputIterator.
auto rawMutableIteratorConformanceRef = module->lookupConformance(
rawMutableIteratorTy, cxxMutableInputIteratorProto);
if (!isConcreteAndValid(rawMutableIteratorConformanceRef, module))
return;
// Make the original subscript that returns a non-optional value unavailable.
// CxxDictionary adds another subscript that returns an optional value,
// similarly to Swift.Dictionary.
for (auto member : decl->getCurrentMembersWithoutLoading()) {
if (auto subscript = dyn_cast<SubscriptDecl>(member)) {
impl.markUnavailable(subscript,
"use subscript with optional return value");
}
}
impl.addSynthesizedTypealias(decl, ctx.Id_Key, keyType->getUnderlyingType());
impl.addSynthesizedTypealias(decl, ctx.Id_Value,
valueType->getUnderlyingType());
impl.addSynthesizedTypealias(decl, ctx.Id_Element,
keyValuePairType->getUnderlyingType());
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("RawIterator"),
rawIteratorTy);
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("RawMutableIterator"),
rawMutableIteratorTy);
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Size"),
sizeType->getUnderlyingType());
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("InsertionResult"),
insert->getResultInterfaceType());
impl.addSynthesizedProtocolAttrs(decl, {KnownProtocolKind::CxxDictionary});
}
void swift::conformToCxxVectorIfNeeded(ClangImporter::Implementation &impl,
NominalTypeDecl *decl,
const clang::CXXRecordDecl *clangDecl) {
PrettyStackTraceDecl trace("conforming to CxxVector", decl);
assert(decl);
assert(clangDecl);
ASTContext &ctx = decl->getASTContext();
// Only auto-conform types from the C++ standard library. Custom user types
// might have a similar interface but different semantics.
if (!isStdDecl(clangDecl, {"vector"}))
return;
auto valueType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
decl, ctx.getIdentifier("value_type"));
auto iterType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
decl, ctx.getIdentifier("const_iterator"));
if (!valueType || !iterType)
return;
ProtocolDecl *cxxRandomAccessIteratorProto =
ctx.getProtocol(KnownProtocolKind::UnsafeCxxRandomAccessIterator);
if (!cxxRandomAccessIteratorProto)
return;
auto rawIteratorTy = iterType->getUnderlyingType();
// Check if RawIterator conforms to UnsafeCxxRandomAccessIterator.
ModuleDecl *module = decl->getModuleContext();
auto rawIteratorConformanceRef =
module->lookupConformance(rawIteratorTy, cxxRandomAccessIteratorProto);
if (!isConcreteAndValid(rawIteratorConformanceRef, module))
return;
impl.addSynthesizedTypealias(decl, ctx.Id_Element,
valueType->getUnderlyingType());
impl.addSynthesizedTypealias(decl, ctx.Id_ArrayLiteralElement,
valueType->getUnderlyingType());
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("RawIterator"),
rawIteratorTy);
impl.addSynthesizedProtocolAttrs(decl, {KnownProtocolKind::CxxVector});
}
|