Skip to content

Commit c5a641b

Browse files
committed
WIP: Import C arrays as Vector with flag
1 parent 08ce821 commit c5a641b

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
@@ -225,6 +225,7 @@ UPCOMING_FEATURE(GlobalActorIsolatedTypesUsability, 0434, 6)
225225
UPCOMING_FEATURE(ExistentialAny, 335, 7)
226226
UPCOMING_FEATURE(InternalImportsByDefault, 409, 7)
227227
UPCOMING_FEATURE(MemberImportVisibility, 444, 7)
228+
UPCOMING_FEATURE(ImportCArraysAsVectors, 453, 7)
228229

229230
EXPERIMENTAL_FEATURE(StaticAssert, false)
230231
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
@@ -634,25 +634,32 @@ namespace {
634634
}
635635

636636
ImportResult VisitConstantArrayType(const clang::ConstantArrayType *type) {
637-
// FIXME: Map to a real fixed-size Swift array type when we have those.
638-
// Importing as a tuple at least fills the right amount of space, and
639-
// we can cheese static-offset "indexing" using .$n operations.
640-
641637
Type elementType = Impl.importTypeIgnoreIUO(
642638
type->getElementType(), ImportTypeKind::Value, addImportDiagnostic,
643639
AllowNSUIntegerAsInt, Bridgeability::None, ImportTypeAttrs());
644640
if (!elementType)
645641
return Type();
646642

647643
auto size = type->getSize().getZExtValue();
644+
645+
if (size == 1)
646+
return elementType;
647+
648+
auto &ctx = elementType->getASTContext();
649+
650+
if (ctx.LangOpts.hasFeature(Feature::ImportCArraysAsVectors)) {
651+
auto vector = cast<StructDecl>(ctx.getVectorDecl());
652+
auto countType = IntegerType::get(std::to_string(size),
653+
/* isNegative */ false, ctx);
654+
return BoundGenericStructType::get(vector, /* parent */ nullptr,
655+
{countType, elementType});
656+
}
657+
648658
// An array of size N is imported as an N-element tuple which
649659
// takes very long to compile. We chose 4096 as the upper limit because
650660
// we don't want to break arrays of size PATH_MAX.
651661
if (size > 4096)
652662
return Type();
653-
654-
if (size == 1)
655-
return elementType;
656663

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

0 commit comments

Comments
 (0)