Skip to content

Commit b570fce

Browse files
committed
[cxx-interop] Use clang lookups for std::vector conformance
Instead of looking up the *imported* Swift type aliases, this patch shifts the conformance to look up these typedefs from Clang, and *then* imports them to Swift types to satisfy CxxVector conformance. Doing so removes the conformance's dependency on eagerly importing such typedefs. This patch also drops a conformance check that RawIterator conforms to UnsafeCxxRandomAccessIterator. It shouldn't be necessary, because we are looking at std::vector, which we assume comes from a conforming stdlib. Even if it were necessary, it's not clear that this is the right place and strategy for doing conformance checking. Besides we are fairly inconsistent about checking other associated types. Let's be optimistic about conformance for now (-:
1 parent e7f493e commit b570fce

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

lib/ClangImporter/ClangDerivedConformances.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,35 +1223,33 @@ static void conformToCxxVector(ClangImporter::Implementation &impl,
12231223
const clang::CXXRecordDecl *clangDecl) {
12241224
PrettyStackTraceDecl trace("conforming to CxxVector", decl);
12251225
ASTContext &ctx = decl->getASTContext();
1226+
clang::Sema &clangSema = impl.getClangSema();
12261227

1227-
auto valueType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
1228-
decl, ctx.getIdentifier("value_type"));
1229-
auto iterType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
1230-
decl, ctx.getIdentifier("const_iterator"));
1231-
auto sizeType = lookupDirectSingleWithoutExtensions<TypeAliasDecl>(
1232-
decl, ctx.getIdentifier("size_type"));
1233-
if (!valueType || !iterType || !sizeType)
1234-
return;
1235-
1236-
ProtocolDecl *cxxRandomAccessIteratorProto =
1237-
ctx.getProtocol(KnownProtocolKind::UnsafeCxxRandomAccessIterator);
1238-
if (!cxxRandomAccessIteratorProto)
1239-
return;
1240-
1241-
auto rawIteratorTy = iterType->getUnderlyingType();
1228+
auto *value_type = lookupCxxTypeMember(clangSema, clangDecl, "value_type",
1229+
/*mustBeComplete=*/true);
1230+
auto *size_type = lookupCxxTypeMember(clangSema, clangDecl, "size_type",
1231+
/*mustBeComplete=*/true);
1232+
auto *const_iterator =
1233+
lookupCxxTypeMember(clangSema, clangDecl, "const_iterator",
1234+
/*mustBeComplete=*/true);
12421235

1243-
// Check if RawIterator conforms to UnsafeCxxRandomAccessIterator.
1244-
if (!checkConformance(rawIteratorTy, cxxRandomAccessIteratorProto))
1236+
auto *Element = dyn_cast_or_null<TypeAliasDecl>(
1237+
impl.importDecl(value_type, impl.CurrentVersion));
1238+
auto *Size = dyn_cast_or_null<TypeAliasDecl>(
1239+
impl.importDecl(size_type, impl.CurrentVersion));
1240+
auto *RawIterator = dyn_cast_or_null<TypeAliasDecl>(
1241+
impl.importDecl(const_iterator, impl.CurrentVersion));
1242+
if (!Element || !Size || !RawIterator)
12451243
return;
12461244

12471245
impl.addSynthesizedTypealias(decl, ctx.Id_Element,
1248-
valueType->getUnderlyingType());
1246+
Element->getUnderlyingType());
12491247
impl.addSynthesizedTypealias(decl, ctx.Id_ArrayLiteralElement,
1250-
valueType->getUnderlyingType());
1248+
Element->getUnderlyingType());
12511249
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Size"),
1252-
sizeType->getUnderlyingType());
1250+
Size->getUnderlyingType());
12531251
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("RawIterator"),
1254-
rawIteratorTy);
1252+
RawIterator->getUnderlyingType());
12551253
impl.addSynthesizedProtocolAttrs(decl, {KnownProtocolKind::CxxVector});
12561254
}
12571255

0 commit comments

Comments
 (0)