Skip to content

Commit 4eb9f91

Browse files
committed
Import C arrays as Vector with flag
1 parent dbf9c60 commit 4eb9f91

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ UPCOMING_FEATURE(GlobalActorIsolatedTypesUsability, 0434, 6)
224224
UPCOMING_FEATURE(ExistentialAny, 335, 7)
225225
UPCOMING_FEATURE(InternalImportsByDefault, 409, 7)
226226
UPCOMING_FEATURE(MemberImportVisibility, 444, 7)
227+
UPCOMING_FEATURE(ImportCArraysAsVectors, 453, 7)
227228

228229
EXPERIMENTAL_FEATURE(StaticAssert, false)
229230
EXPERIMENTAL_FEATURE(NamedOpaqueTypes, false)

lib/AST/FeatureSet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ UNINTERESTING_FEATURE(ExistentialAny)
6969
UNINTERESTING_FEATURE(InferSendableFromCaptures)
7070
UNINTERESTING_FEATURE(ImplicitOpenExistentials)
7171
UNINTERESTING_FEATURE(MemberImportVisibility)
72+
UNINTERESTING_FEATURE(ImportCArraysAsVectors) // fixme?
7273

7374
// ----------------------------------------------------------------------------
7475
// MARK: - Experimental Features

lib/ClangImporter/ImportType.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -606,25 +606,32 @@ namespace {
606606
}
607607

608608
ImportResult VisitConstantArrayType(const clang::ConstantArrayType *type) {
609-
// FIXME: Map to a real fixed-size Swift array type when we have those.
610-
// Importing as a tuple at least fills the right amount of space, and
611-
// we can cheese static-offset "indexing" using .$n operations.
612-
613609
Type elementType = Impl.importTypeIgnoreIUO(
614610
type->getElementType(), ImportTypeKind::Value, addImportDiagnostic,
615611
AllowNSUIntegerAsInt, Bridgeability::None, ImportTypeAttrs());
616612
if (!elementType)
617613
return Type();
618614

619615
auto size = type->getSize().getZExtValue();
616+
617+
if (size == 1)
618+
return elementType;
619+
620+
auto &ctx = elementType->getASTContext();
621+
622+
if (ctx.LangOpts.hasFeature(Feature::ImportCArraysAsVectors)) {
623+
auto vector = cast<StructDecl>(ctx.getVectorDecl());
624+
auto countType = IntegerType::get(std::to_string(size),
625+
/* isNegative */ false, ctx);
626+
return BoundGenericStructType::get(vector, /* parent */ nullptr,
627+
{countType, elementType});
628+
}
629+
620630
// An array of size N is imported as an N-element tuple which
621631
// takes very long to compile. We chose 4096 as the upper limit because
622632
// we don't want to break arrays of size PATH_MAX.
623633
if (size > 4096)
624634
return Type();
625-
626-
if (size == 1)
627-
return elementType;
628635

629636
SmallVector<TupleTypeElt, 8> elts{static_cast<size_t>(size), elementType};
630637
return TupleType::get(elts, elementType->getASTContext());

0 commit comments

Comments
 (0)