Skip to content

Commit 64f455f

Browse files
committed
WIP Serialize IntegerType
1 parent dc8f265 commit 64f455f

File tree

10 files changed

+64
-5
lines changed

10 files changed

+64
-5
lines changed

include/swift/AST/Types.h

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

7744-
APInt getValue();
7744+
APInt getValue() const;
77457745

77467746
StringRef getDigitsText() const {
77477747
return Value;

lib/AST/Type.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5911,7 +5911,7 @@ SILFunctionType::withPatternSpecialization(CanGenericSignature sig,
59115911
witnessConformance);
59125912
}
59135913

5914-
APInt IntegerType::getValue() {
5914+
APInt IntegerType::getValue() const {
59155915
return BuiltinIntegerWidth::arbitrary().parse(getDigitsText(), /*radix*/ 0,
59165916
isNegative());
59175917
}

lib/Sema/TypeCheckAttr.cpp

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

3853+
TypeResolutionOptions options(TypeResolverContext::RawLayoutAttr);
3854+
38533855
// Resolve the type in the struct's context.
3854-
return TypeResolution::resolveContextualType(typeRepr, sd, std::nullopt,
3856+
return TypeResolution::resolveContextualType(typeRepr, sd, options,
38553857
// Unbound generics and
38563858
// placeholders are not allowed
38573859
// within this attribute.

lib/Sema/TypeCheckType.cpp

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

@@ -4944,11 +4945,12 @@ TypeResolver::resolveDeclRefTypeRepr(DeclRefTypeRepr *repr,
49444945

49454946
// Referencing a value generic by name, e.g. 'let N' and referencing 'N', is
49464947
// only valid as a generic argument, in generic requirements, when being
4947-
// used as an expression, and in SIL mode.
4948+
// used as an expression, inside of the @_rawLayout attribute, and in SIL mode.
49484949
if (result->isValueParameter() &&
49494950
!(options.isGenericArgument() ||
49504951
options.isGenericRequirement() ||
49514952
options.isAnyExpr() ||
4953+
options.is(TypeResolverContext::RawLayoutAttr) ||
49524954
options.contains(TypeResolutionFlags::SILMode))) {
49534955
if (!options.contains(TypeResolutionFlags::SilenceErrors)) {
49544956
diagnose(repr->getNameLoc(), diag::value_generic_unexpected, result);
@@ -5270,6 +5272,7 @@ NeverNullType TypeResolver::resolveImplicitlyUnwrappedOptionalType(
52705272
case TypeResolverContext::CustomAttr:
52715273
case TypeResolverContext::Inverted:
52725274
case TypeResolverContext::ValueGenericArgument:
5275+
case TypeResolverContext::RawLayoutAttr:
52735276
doDiag = true;
52745277
break;
52755278
}

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
@@ -7942,6 +7942,17 @@ Expected<Type> DESERIALIZE_TYPE(ERROR_TYPE)(ModuleFile &MF,
79427942
return ErrorType::get(ctx);
79437943
return ErrorType::get(origTy);
79447944
}
7945+
7946+
Expected<Type> DESERIALIZE_TYPE(INTEGER_TYPE)(ModuleFile &MF,
7947+
SmallVectorImpl<uint64_t> &scratch,
7948+
StringRef blobData) {
7949+
auto &ctx = MF.getContext();
7950+
bool isNegative;
7951+
uint64_t value;
7952+
decls_block::IntegerTypeLayout::readRecord(scratch, isNegative, value);
7953+
7954+
return IntegerType::get(std::to_string(value), isNegative, ctx);
7955+
}
79457956
} // namespace decls_block
79467957
} // namespace serialization
79477958
}

lib/Serialization/ModuleFormat.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,12 @@ namespace decls_block {
14691469
BCArray<TypeIDField>// component types
14701470
);
14711471

1472+
TYPE_LAYOUT(IntegerTypeLayout,
1473+
INTEGER_TYPE,
1474+
BCFixed<1>, // is negative?
1475+
BCFixed<64> // integer value
1476+
);
1477+
14721478
using TypeAliasLayout = BCRecordLayout<
14731479
TYPE_ALIAS_DECL,
14741480
IdentifierIDField, // name

lib/Serialization/Serialization.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5893,7 +5893,14 @@ class Serializer::TypeSerializer : public TypeVisitor<TypeSerializer> {
58935893
}
58945894

58955895
void visitIntegerType(const IntegerType *integer) {
5896-
llvm_unreachable("implement me");
5896+
using namespace decls_block;
5897+
5898+
uint64_t value = integer->getValue().getZExtValue();
5899+
5900+
unsigned abbrCode = S.DeclTypeAbbrCodes[IntegerTypeLayout::Code];
5901+
IntegerTypeLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
5902+
integer->isNegative(),
5903+
value);
58975904
}
58985905
};
58995906

@@ -6080,6 +6087,7 @@ void Serializer::writeAllDeclsAndTypes() {
60806087
registerDeclTypeAbbr<PackElementTypeLayout>();
60816088
registerDeclTypeAbbr<PackTypeLayout>();
60826089
registerDeclTypeAbbr<SILPackTypeLayout>();
6090+
registerDeclTypeAbbr<IntegerTypeLayout>();
60836091

60846092
registerDeclTypeAbbr<ErrorFlagLayout>();
60856093
registerDeclTypeAbbr<ErrorTypeLayout>();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
extension Vector where Count == 2 {
15+
func something2() {}
16+
}
17+
18+
// CHECK: func something<let N : Int>(_: borrowing Vector<Int, N>)
19+
func something<let N: Int>(_: borrowing Vector<Int, N>) {}

0 commit comments

Comments
 (0)