Skip to content

Commit 15db739

Browse files
committed
Serialize IntegerType
Use BCBlob
1 parent bfffd7e commit 15db739

File tree

10 files changed

+63
-5
lines changed

10 files changed

+63
-5
lines changed

include/swift/AST/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7669,7 +7669,7 @@ class IntegerType final : public TypeBase, public llvm::FoldingSetNode {
76697669
static IntegerType *get(StringRef value, bool isNegative,
76707670
const ASTContext &ctx);
76717671

7672-
APInt getValue();
7672+
APInt getValue() const;
76737673

76747674
StringRef getDigitsText() const {
76757675
return Value;

lib/AST/Type.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4803,7 +4803,7 @@ SILFunctionType::withPatternSpecialization(CanGenericSignature sig,
48034803
witnessConformance);
48044804
}
48054805

4806-
APInt IntegerType::getValue() {
4806+
APInt IntegerType::getValue() const {
48074807
return BuiltinIntegerWidth::arbitrary().parse(getDigitsText(), /*radix*/ 0,
48084808
isNegative());
48094809
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3856,8 +3856,10 @@ ResolveRawLayoutTypeRequest::evaluate(Evaluator &evaluator, StructDecl *sd,
38563856
return fixedTy->getType();
38573857
}
38583858

3859+
TypeResolutionOptions options(TypeResolverContext::RawLayoutAttr);
3860+
38593861
// Resolve the type in the struct's context.
3860-
return TypeResolution::resolveContextualType(typeRepr, sd, std::nullopt,
3862+
return TypeResolution::resolveContextualType(typeRepr, sd, options,
38613863
// Unbound generics and
38623864
// placeholders are not allowed
38633865
// within this attribute.

lib/Sema/TypeCheckType.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ static unsigned getGenericRequirementKind(TypeResolutionOptions options) {
176176
case TypeResolverContext::CustomAttr:
177177
case TypeResolverContext::Inverted:
178178
case TypeResolverContext::ValueGenericArgument:
179+
case TypeResolverContext::RawLayoutAttr:
179180
break;
180181
}
181182

@@ -4950,11 +4951,12 @@ TypeResolver::resolveDeclRefTypeRepr(DeclRefTypeRepr *repr,
49504951

49514952
// Referencing a value generic by name, e.g. 'let N' and referencing 'N', is
49524953
// only valid as a generic argument, in generic requirements, when being
4953-
// used as an expression, and in SIL mode.
4954+
// used as an expression, inside of the @_rawLayout attribute, and in SIL mode.
49544955
if (result->isValueParameter() &&
49554956
!(options.isGenericArgument() ||
49564957
options.isGenericRequirement() ||
49574958
options.isAnyExpr() ||
4959+
options.is(TypeResolverContext::RawLayoutAttr) ||
49584960
options.contains(TypeResolutionFlags::SILMode))) {
49594961
if (!options.contains(TypeResolutionFlags::SilenceErrors)) {
49604962
diagnose(repr->getNameLoc(), diag::value_generic_unexpected, result);
@@ -5276,6 +5278,7 @@ NeverNullType TypeResolver::resolveImplicitlyUnwrappedOptionalType(
52765278
case TypeResolverContext::CustomAttr:
52775279
case TypeResolverContext::Inverted:
52785280
case TypeResolverContext::ValueGenericArgument:
5281+
case TypeResolverContext::RawLayoutAttr:
52795282
doDiag = true;
52805283
break;
52815284
}

lib/Sema/TypeCheckType.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ enum class TypeResolverContext : uint8_t {
210210

211211
/// Whether this is the argument of an inverted constraint (~).
212212
Inverted,
213+
214+
/// Whether this is inside a @_rawLayout attribute.
215+
RawLayoutAttr,
213216
};
214217

215218
/// Options that determine how type resolution should work.
@@ -308,6 +311,7 @@ class TypeResolutionOptions {
308311
case Context::Inverted:
309312
case Context::ValueGenericArgument:
310313
case Context::PatternBindingDecl:
314+
case Context::RawLayoutAttr:
311315
return false;
312316
}
313317
llvm_unreachable("unhandled kind");
@@ -353,6 +357,7 @@ class TypeResolutionOptions {
353357
case Context::AbstractFunctionDecl:
354358
case Context::CustomAttr:
355359
case Context::ValueGenericArgument:
360+
case Context::RawLayoutAttr:
356361
return true;
357362
}
358363
}
@@ -396,6 +401,7 @@ class TypeResolutionOptions {
396401
case Context::AbstractFunctionDecl:
397402
case Context::CustomAttr:
398403
case Context::ValueGenericArgument:
404+
case Context::RawLayoutAttr:
399405
return false;
400406
}
401407
}
@@ -440,6 +446,7 @@ class TypeResolutionOptions {
440446
case Context::Inverted:
441447
case Context::GenericParameterInherited:
442448
case Context::AssociatedTypeInherited:
449+
case Context::RawLayoutAttr:
443450
return false;
444451
}
445452
}
@@ -485,6 +492,7 @@ class TypeResolutionOptions {
485492
case Context::CustomAttr:
486493
case Context::Inverted:
487494
case Context::ValueGenericArgument:
495+
case Context::RawLayoutAttr:
488496
return false;
489497
}
490498
}

lib/Serialization/DeclTypeRecordNodes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ TYPE(SIL_PACK)
120120

121121
TYPE(ERROR)
122122

123+
TYPE(INTEGER)
124+
123125
FIRST_DECL(TYPE_ALIAS, 40)
124126
DECL(GENERIC_TYPE_PARAM)
125127
DECL(ASSOCIATED_TYPE)

lib/Serialization/Deserialization.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7945,6 +7945,17 @@ Expected<Type> DESERIALIZE_TYPE(ERROR_TYPE)(ModuleFile &MF,
79457945
return ErrorType::get(ctx);
79467946
return ErrorType::get(origTy);
79477947
}
7948+
7949+
Expected<Type> DESERIALIZE_TYPE(INTEGER_TYPE)(ModuleFile &MF,
7950+
SmallVectorImpl<uint64_t> &scratch,
7951+
StringRef blobData) {
7952+
auto &ctx = MF.getContext();
7953+
bool isNegative;
7954+
7955+
decls_block::IntegerTypeLayout::readRecord(scratch, isNegative);
7956+
7957+
return IntegerType::get(blobData, isNegative, ctx);
7958+
}
79487959
} // namespace decls_block
79497960
} // namespace serialization
79507961
}

lib/Serialization/ModuleFormat.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,12 @@ namespace decls_block {
14751475
BCArray<TypeIDField>// component types
14761476
);
14771477

1478+
TYPE_LAYOUT(IntegerTypeLayout,
1479+
INTEGER_TYPE,
1480+
BCFixed<1>, // is negative?
1481+
BCBlob // integer value text
1482+
);
1483+
14781484
using TypeAliasLayout = BCRecordLayout<
14791485
TYPE_ALIAS_DECL,
14801486
IdentifierIDField, // name

lib/Serialization/Serialization.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5896,7 +5896,12 @@ class Serializer::TypeSerializer : public TypeVisitor<TypeSerializer> {
58965896
}
58975897

58985898
void visitIntegerType(const IntegerType *integer) {
5899-
llvm_unreachable("implement me");
5899+
using namespace decls_block;
5900+
5901+
unsigned abbrCode = S.DeclTypeAbbrCodes[IntegerTypeLayout::Code];
5902+
IntegerTypeLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
5903+
integer->isNegative(),
5904+
integer->getDigitsText());
59005905
}
59015906
};
59025907

@@ -6083,6 +6088,7 @@ void Serializer::writeAllDeclsAndTypes() {
60836088
registerDeclTypeAbbr<PackElementTypeLayout>();
60846089
registerDeclTypeAbbr<PackTypeLayout>();
60856090
registerDeclTypeAbbr<SILPackTypeLayout>();
6091+
registerDeclTypeAbbr<IntegerTypeLayout>();
60866092

60876093
registerDeclTypeAbbr<ErrorFlagLayout>();
60886094
registerDeclTypeAbbr<ErrorTypeLayout>();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -emit-module -enable-experimental-feature RawLayout -parse-as-library -o %t
3+
// RUN: %target-sil-opt -enable-sil-verify-all %t/value_generics.swiftmodule -o - | %FileCheck %s
4+
5+
// CHECK: @_rawLayout(likeArrayOf: Element, count: Count) struct Vector<Element, let Count : Int> : ~Copyable where Element : ~Copyable {
6+
@_rawLayout(likeArrayOf: Element, count: Count)
7+
struct Vector<Element: ~Copyable, let Count: Int>: ~Copyable {}
8+
9+
// CHECK: extension Vector where Element == Int
10+
extension Vector where Element == Int {
11+
func something() {}
12+
}
13+
14+
// CHECK: extension Vector where Count == 2
15+
extension Vector where Count == 2 {
16+
func something2() {}
17+
}
18+
19+
// CHECK: func something<let N : Int>(_: borrowing Vector<Int, N>)
20+
func something<let N: Int>(_: borrowing Vector<Int, N>) {}

0 commit comments

Comments
 (0)