|
| 1 | +//===--- ClangDerivedConformances.cpp -------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "ClangDerivedConformances.h" |
| 14 | +#include "swift/AST/NameLookup.h" |
| 15 | +#include "swift/AST/ParameterList.h" |
| 16 | + |
| 17 | +using namespace swift; |
| 18 | + |
| 19 | +static clang::TypeDecl * |
| 20 | +getIteratorCategoryDecl(const clang::CXXRecordDecl *clangDecl) { |
| 21 | + clang::IdentifierInfo *iteratorCategoryDeclName = |
| 22 | + &clangDecl->getASTContext().Idents.get("iterator_category"); |
| 23 | + auto iteratorCategories = clangDecl->lookup(iteratorCategoryDeclName); |
| 24 | + if (!iteratorCategories.isSingleResult()) |
| 25 | + return nullptr; |
| 26 | + auto iteratorCategory = iteratorCategories.front(); |
| 27 | + |
| 28 | + return dyn_cast_or_null<clang::TypeDecl>(iteratorCategory); |
| 29 | +} |
| 30 | + |
| 31 | +bool swift::isIterator(const clang::CXXRecordDecl *clangDecl) { |
| 32 | + return getIteratorCategoryDecl(clangDecl); |
| 33 | +} |
| 34 | + |
| 35 | +void swift::conformToCxxIteratorIfNeeded( |
| 36 | + ClangImporter::Implementation &impl, NominalTypeDecl *decl, |
| 37 | + const clang::CXXRecordDecl *clangDecl) { |
| 38 | + assert(decl); |
| 39 | + assert(clangDecl); |
| 40 | + ASTContext &ctx = decl->getASTContext(); |
| 41 | + |
| 42 | + // We consider a type to be an input iterator if it defines an |
| 43 | + // `iterator_category` that inherits from `std::input_iterator_tag`, e.g. |
| 44 | + // `using iterator_category = std::input_iterator_tag`. |
| 45 | + auto iteratorCategory = getIteratorCategoryDecl(clangDecl); |
| 46 | + if (!iteratorCategory) |
| 47 | + return; |
| 48 | + |
| 49 | + // If `iterator_category` is a typedef or a using-decl, retrieve the |
| 50 | + // underlying struct decl. |
| 51 | + clang::CXXRecordDecl *underlyingCategoryDecl = nullptr; |
| 52 | + if (auto typedefDecl = dyn_cast<clang::TypedefNameDecl>(iteratorCategory)) { |
| 53 | + auto type = typedefDecl->getUnderlyingType(); |
| 54 | + underlyingCategoryDecl = type->getAsCXXRecordDecl(); |
| 55 | + } else { |
| 56 | + underlyingCategoryDecl = dyn_cast<clang::CXXRecordDecl>(iteratorCategory); |
| 57 | + } |
| 58 | + if (underlyingCategoryDecl) { |
| 59 | + underlyingCategoryDecl = underlyingCategoryDecl->getDefinition(); |
| 60 | + } |
| 61 | + |
| 62 | + if (!underlyingCategoryDecl) |
| 63 | + return; |
| 64 | + |
| 65 | + auto isInputIteratorDecl = [&](const clang::CXXRecordDecl *base) { |
| 66 | + return base->isInStdNamespace() && base->getIdentifier() && |
| 67 | + base->getName() == "input_iterator_tag"; |
| 68 | + }; |
| 69 | + |
| 70 | + // Traverse all transitive bases of `underlyingDecl` to check if |
| 71 | + // it inherits from `std::input_iterator_tag`. |
| 72 | + bool isInputIterator = isInputIteratorDecl(underlyingCategoryDecl); |
| 73 | + underlyingCategoryDecl->forallBases([&](const clang::CXXRecordDecl *base) { |
| 74 | + if (isInputIteratorDecl(base)) { |
| 75 | + isInputIterator = true; |
| 76 | + return false; |
| 77 | + } |
| 78 | + return true; |
| 79 | + }); |
| 80 | + |
| 81 | + if (!isInputIterator) |
| 82 | + return; |
| 83 | + |
| 84 | + // Check if present: `var pointee: Pointee { get }` |
| 85 | + auto pointeeId = ctx.getIdentifier("pointee"); |
| 86 | + auto pointees = decl->lookupDirect(pointeeId); |
| 87 | + if (pointees.size() != 1) |
| 88 | + return; |
| 89 | + auto pointee = dyn_cast<VarDecl>(pointees.front()); |
| 90 | + if (!pointee || pointee->isGetterMutating()) |
| 91 | + return; |
| 92 | + |
| 93 | + // Check if present: `func successor() -> Self` |
| 94 | + auto successorId = ctx.getIdentifier("successor"); |
| 95 | + auto successors = decl->lookupDirect(successorId); |
| 96 | + if (successors.size() != 1) |
| 97 | + return; |
| 98 | + auto successor = dyn_cast<FuncDecl>(successors.front()); |
| 99 | + if (!successor || successor->isMutating()) |
| 100 | + return; |
| 101 | + auto successorTy = successor->getResultInterfaceType(); |
| 102 | + if (!successorTy || successorTy->getAnyNominal() != decl) |
| 103 | + return; |
| 104 | + |
| 105 | + // Check if present: `func ==` |
| 106 | + // FIXME: this only detects `operator==` declared as a member. |
| 107 | + auto equalEquals = decl->lookupDirect(ctx.Id_EqualsOperator); |
| 108 | + if (equalEquals.empty()) |
| 109 | + return; |
| 110 | + auto equalEqual = dyn_cast<FuncDecl>(equalEquals.front()); |
| 111 | + if (!equalEqual || !equalEqual->hasParameterList()) |
| 112 | + return; |
| 113 | + auto equalEqualParams = equalEqual->getParameters(); |
| 114 | + if (equalEqualParams->size() != 2) |
| 115 | + return; |
| 116 | + auto equalEqualLHS = equalEqualParams->get(0); |
| 117 | + auto equalEqualRHS = equalEqualParams->get(1); |
| 118 | + if (equalEqualLHS->isInOut() || equalEqualRHS->isInOut()) |
| 119 | + return; |
| 120 | + auto equalEqualLHSTy = equalEqualLHS->getType(); |
| 121 | + auto equalEqualRHSTy = equalEqualRHS->getType(); |
| 122 | + if (!equalEqualLHSTy || !equalEqualRHSTy || |
| 123 | + equalEqualLHSTy->getAnyNominal() != equalEqualRHSTy->getAnyNominal()) |
| 124 | + return; |
| 125 | + |
| 126 | + impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Pointee"), |
| 127 | + pointee->getType()); |
| 128 | + impl.addSynthesizedProtocolAttrs(decl, |
| 129 | + {KnownProtocolKind::UnsafeCxxInputIterator}); |
| 130 | +} |
0 commit comments