Skip to content

Commit dff944a

Browse files
committed
Introduce type sugar for InlineArray
Parse e.g `[3 x Int]` as type sugar for InlineArray. Gated behind an experimental feature flag for now.
1 parent ae16df9 commit dff944a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+740
-6
lines changed

docs/ABI/Mangling.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,7 @@ productions:
954954
type ::= base-type "XSq" // sugared Optional type
955955
type ::= base-type "XSa" // sugared Array type
956956
type ::= key-type value-type "XSD" // sugared Dictionary type
957+
type ::= count-type element-type "XSA" // sugared InlineArray type
957958

958959
Generics
959960
~~~~~~~~

include/swift/AST/ASTBridging.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2702,6 +2702,11 @@ BridgedImplicitlyUnwrappedOptionalTypeRepr_createParsed(
27022702
BridgedASTContext cContext, BridgedTypeRepr base,
27032703
BridgedSourceLoc cExclamationLoc);
27042704

2705+
SWIFT_NAME("BridgedInlineArrayTypeRepr.createParsed(_:count:element:brackets:)")
2706+
BridgedInlineArrayTypeRepr BridgedInlineArrayTypeRepr_createParsed(
2707+
BridgedASTContext cContext, BridgedTypeRepr cCountType,
2708+
BridgedTypeRepr cElementType, BridgedSourceRange cBracketsRange);
2709+
27052710
SWIFT_NAME("BridgedInverseTypeRepr.createParsed(_:tildeLoc:constraint:)")
27062711
BridgedInverseTypeRepr
27072712
BridgedInverseTypeRepr_createParsed(BridgedASTContext cContext,

include/swift/AST/ASTDemangler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ class ASTBuilder {
248248

249249
Type createArrayType(Type base);
250250

251+
Type createInlineArrayType(Type count, Type element);
252+
251253
Type createDictionaryType(Type key, Type value);
252254

253255
Type createIntegerType(intptr_t value);

include/swift/AST/DiagnosticsParse.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,10 @@ NOTE(subscript_array_element_fix_it_add_comma, none, "add a separator between "
878878
NOTE(subscript_array_element_fix_it_remove_space, none,
879879
"remove the space between the elements to silence this warning", ())
880880

881+
// Inline Array
882+
ERROR(expected_rsquare_inline_array,none,
883+
"expected ']' in inline array type", ())
884+
881885
// Tuple Types
882886
ERROR(expected_rparen_tuple_type_list,none,
883887
"expected ')' at end of tuple list", ())

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6069,7 +6069,7 @@ NOTE(note_recursive_enum_case_here,none,
60696069

60706070
ERROR(sugar_type_not_found,none,
60716071
"broken standard library: cannot find "
6072-
"%select{Array|Optional|ImplicitlyUnwrappedOptional|Dictionary|"
6072+
"%select{Array|Optional|InlineArray|Dictionary|"
60736073
"Error}0 type", (unsigned))
60746074
ERROR(optional_intrinsics_not_found,none,
60756075
"broken standard library: cannot find intrinsic operations on "
@@ -8334,6 +8334,9 @@ ERROR(invalid_value_for_type_same_type,none,
83348334
ERROR(inlinearray_literal_incorrect_count,none,
83358335
"expected %0 elements in inline array literal, but got %1", (Type, Type))
83368336

8337+
ERROR(inline_array_type_backwards,none,
8338+
"element count must precede inline array element type", ())
8339+
83378340
//===----------------------------------------------------------------------===//
83388341
// MARK: @abi Attribute
83398342
//===----------------------------------------------------------------------===//

include/swift/AST/PrintOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,10 @@ struct PrintOptions {
590590
/// Whether to always desugar array types from `[base_type]` to `Array<base_type>`
591591
bool AlwaysDesugarArraySliceTypes = false;
592592

593+
/// Whether to always desugar inline array types from
594+
/// `[<count> x <element>]` to `InlineArray<count, element>`
595+
bool AlwaysDesugarInlineArrayTypes = false;
596+
593597
/// Whether to always desugar dictionary types
594598
/// from `[key_type:value_type]` to `Dictionary<key_type,value_type>`
595599
bool AlwaysDesugarDictionaryTypes = false;

include/swift/AST/TypeNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ ABSTRACT_SUGARED_TYPE(Sugar, Type)
217217
SUGARED_TYPE(Optional, UnarySyntaxSugarType)
218218
SUGARED_TYPE(VariadicSequence, UnarySyntaxSugarType)
219219
TYPE_RANGE(UnarySyntaxSugar, ArraySlice, VariadicSequence)
220+
SUGARED_TYPE(InlineArray, SyntaxSugarType)
220221
SUGARED_TYPE(Dictionary, SyntaxSugarType)
221222
TYPE_RANGE(SyntaxSugar, ArraySlice, Dictionary)
222223
TYPE_RANGE(Sugar, TypeAlias, Dictionary)

include/swift/AST/TypeRepr.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,35 @@ class ArrayTypeRepr : public TypeRepr {
641641
friend class TypeRepr;
642642
};
643643

644+
/// An InlineArray type e.g `[2 x Foo]`, sugar for `InlineArray<2, Foo>`.
645+
class InlineArrayTypeRepr : public TypeRepr {
646+
TypeRepr *Count;
647+
TypeRepr *Element;
648+
SourceRange Brackets;
649+
650+
InlineArrayTypeRepr(TypeRepr *count, TypeRepr *element, SourceRange brackets)
651+
: TypeRepr(TypeReprKind::InlineArray), Count(count), Element(element),
652+
Brackets(brackets) {}
653+
654+
public:
655+
static InlineArrayTypeRepr *create(ASTContext &ctx, TypeRepr *count,
656+
TypeRepr *element, SourceRange brackets);
657+
658+
TypeRepr *getCount() const { return Count; }
659+
TypeRepr *getElement() const { return Element; }
660+
SourceRange getBrackets() const { return Brackets; }
661+
662+
static bool classof(const TypeRepr *T) {
663+
return T->getKind() == TypeReprKind::InlineArray;
664+
}
665+
666+
private:
667+
SourceLoc getStartLocImpl() const { return Brackets.Start; }
668+
SourceLoc getEndLocImpl() const { return Brackets.End; }
669+
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
670+
friend class TypeRepr;
671+
};
672+
644673
/// A dictionary type.
645674
/// \code
646675
/// [K : V]
@@ -1626,6 +1655,7 @@ inline bool TypeRepr::isSimple() const {
16261655
case TypeReprKind::Fixed:
16271656
case TypeReprKind::Self:
16281657
case TypeReprKind::Array:
1658+
case TypeReprKind::InlineArray:
16291659
case TypeReprKind::SILBox:
16301660
case TypeReprKind::Isolated:
16311661
case TypeReprKind::Sending:

include/swift/AST/TypeReprNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ ABSTRACT_TYPEREPR(DeclRef, TypeRepr)
4949
TYPEREPR(UnqualifiedIdent, DeclRefTypeRepr)
5050
TYPEREPR(QualifiedIdent, DeclRefTypeRepr)
5151
TYPEREPR(Function, TypeRepr)
52+
TYPEREPR(InlineArray, TypeRepr)
5253
TYPEREPR(Array, TypeRepr)
5354
TYPEREPR(Dictionary, TypeRepr)
5455
TYPEREPR(Optional, TypeRepr)

include/swift/AST/TypeTransform.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,25 @@ case TypeKind::Id:
894894
return ArraySliceType::get(baseTy);
895895
}
896896

897+
case TypeKind::InlineArray: {
898+
auto ty = cast<InlineArrayType>(base);
899+
auto countTy = doIt(ty->getCountType(), TypePosition::Invariant);
900+
if (!countTy)
901+
return Type();
902+
903+
// Currently the element type is invariant for InlineArray.
904+
// FIXME: Should we allow covariance?
905+
auto eltTy = doIt(ty->getElementType(), TypePosition::Invariant);
906+
if (!eltTy)
907+
return Type();
908+
909+
if (countTy.getPointer() == ty->getCountType().getPointer() &&
910+
eltTy.getPointer() == ty->getElementType().getPointer())
911+
return t;
912+
913+
return InlineArrayType::get(countTy, eltTy);
914+
}
915+
897916
case TypeKind::Optional: {
898917
auto optional = cast<OptionalType>(base);
899918
auto baseTy = doIt(optional->getBaseType(), pos);

0 commit comments

Comments
 (0)