Skip to content

Commit 447dce6

Browse files
authored
Merge pull request #9004 from itaiferber/swift-archival-serialization
Swift Archival & Serialization API
2 parents 9296437 + 433c192 commit 447dce6

Some content is hidden

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

47 files changed

+5795
-8
lines changed

include/swift/AST/Decl.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,6 +2912,9 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
29122912
private:
29132913
/// Predicate used to filter StoredPropertyRange.
29142914
struct ToStoredProperty {
2915+
ToStoredProperty(bool skipInaccessible = false) :
2916+
skipUserInaccessible(skipInaccessible) {}
2917+
bool skipUserInaccessible;
29152918
Optional<VarDecl *> operator()(Decl *decl) const;
29162919
};
29172920

@@ -2921,8 +2924,9 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
29212924
ToStoredProperty>;
29222925

29232926
/// Return a collection of the stored member variables of this type.
2924-
StoredPropertyRange getStoredProperties() const {
2925-
return StoredPropertyRange(getMembers(), ToStoredProperty());
2927+
StoredPropertyRange getStoredProperties(bool skipInaccessible = false) const {
2928+
return StoredPropertyRange(getMembers(),
2929+
ToStoredProperty(skipInaccessible));
29262930
}
29272931

29282932
// Implement isa/cast/dyncast/etc.
@@ -6119,7 +6123,8 @@ inline bool ValueDecl::isSettable(const DeclContext *UseDC,
61196123
inline Optional<VarDecl *>
61206124
NominalTypeDecl::ToStoredProperty::operator()(Decl *decl) const {
61216125
if (auto var = dyn_cast<VarDecl>(decl)) {
6122-
if (!var->isStatic() && var->hasStorage())
6126+
if (!var->isStatic() && var->hasStorage() &&
6127+
(!skipUserInaccessible || var->isUserAccessible()))
61236128
return var;
61246129
}
61256130

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,12 @@ ERROR(broken_int_integer_literal_convertible_conformance,none,
19661966
"Int type is broken: does not conform to ExpressibleByIntegerLiteral", ())
19671967
ERROR(no_equal_overload_for_int,none,
19681968
"no overload of '==' for Int", ())
1969+
ERROR(broken_coding_key_requirement,none,
1970+
"CodingKey protocol is broken: unexpected requirement", ())
1971+
ERROR(broken_encodable_requirement,none,
1972+
"Encodable protocol is broken: unexpected requirement", ())
1973+
ERROR(broken_decodable_requirement,none,
1974+
"Decodable protocol is broken: unexpected requirement", ())
19691975

19701976
// Dynamic Self
19711977
ERROR(dynamic_self_non_method,none,

include/swift/AST/KnownIdentifiers.def

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,40 @@ IDENTIFIER(Any)
2929
IDENTIFIER(atIndexedSubscript)
3030
IDENTIFIER_(bridgeToObjectiveC)
3131
IDENTIFIER_WITH_NAME(code_, "_code")
32+
IDENTIFIER(CodingKeys)
33+
IDENTIFIER(container)
3234
IDENTIFIER(CoreGraphics)
3335
IDENTIFIER(CoreMedia)
3436
IDENTIFIER(CGFloat)
3537
IDENTIFIER(CVarArg)
3638
IDENTIFIER(Darwin)
3739
IDENTIFIER(dealloc)
40+
IDENTIFIER(Decodable)
41+
IDENTIFIER(decode)
42+
IDENTIFIER(Decoder)
43+
IDENTIFIER(decoder)
3844
IDENTIFIER(deinit)
3945
IDENTIFIER(Element)
46+
IDENTIFIER(Encodable)
47+
IDENTIFIER(encode)
48+
IDENTIFIER(Encoder)
49+
IDENTIFIER(encoder)
4050
IDENTIFIER(error)
4151
IDENTIFIER(forKeyedSubscript)
4252
IDENTIFIER(Foundation)
53+
IDENTIFIER(forKey)
54+
IDENTIFIER(from)
4355
IDENTIFIER(fromRaw)
4456
IDENTIFIER(hashValue)
4557
IDENTIFIER(init)
4658
IDENTIFIER(initialize)
4759
IDENTIFIER(initStorage)
4860
IDENTIFIER(initialValue)
61+
IDENTIFIER(intValue)
4962
IDENTIFIER(Key)
63+
IDENTIFIER(KeyedDecodingContainer)
64+
IDENTIFIER(KeyedEncodingContainer)
65+
IDENTIFIER(keyedBy)
5066
IDENTIFIER(keyPath)
5167
IDENTIFIER(makeIterator)
5268
IDENTIFIER(Iterator)
@@ -70,10 +86,16 @@ IDENTIFIER(setObject)
7086
IDENTIFIER(simd)
7187
IDENTIFIER(some)
7288
IDENTIFIER(storage)
89+
IDENTIFIER(stringValue)
7390
IDENTIFIER(subscript)
91+
IDENTIFIER(super)
92+
IDENTIFIER(superDecoder)
93+
IDENTIFIER(superEncoder)
7494
IDENTIFIER(SwiftObject)
95+
IDENTIFIER(to)
7596
IDENTIFIER(toRaw)
7697
IDENTIFIER(Type)
98+
IDENTIFIER(type)
7799
IDENTIFIER(Value)
78100
IDENTIFIER(value)
79101
IDENTIFIER_WITH_NAME(value_, "_value")

include/swift/AST/KnownProtocols.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ PROTOCOL_(ErrorCodeProtocol)
6060
PROTOCOL(OptionSet)
6161
PROTOCOL_(BridgedNSError)
6262
PROTOCOL_(BridgedStoredNSError)
63+
PROTOCOL(CodingKey)
64+
PROTOCOL(Encodable)
65+
PROTOCOL(Decodable)
6366

6467
PROTOCOL_(ObjectiveCBridgeable)
6568
PROTOCOL_(DestructorSafeContainer)

include/swift/AST/KnownStdlibTypes.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,9 @@ KNOWN_STDLIB_TYPE_DECL(Unmanaged, NominalTypeDecl, 1)
7171

7272
KNOWN_STDLIB_TYPE_DECL(Never, NominalTypeDecl, 0)
7373

74+
KNOWN_STDLIB_TYPE_DECL(Encoder, ProtocolDecl, 1)
75+
KNOWN_STDLIB_TYPE_DECL(Decoder, ProtocolDecl, 1)
76+
KNOWN_STDLIB_TYPE_DECL(KeyedEncodingContainer, NominalTypeDecl, 1)
77+
KNOWN_STDLIB_TYPE_DECL(KeyedDecodingContainer, NominalTypeDecl, 1)
78+
7479
#undef KNOWN_STDLIB_TYPE_DECL

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ struct SynthesizedExtensionAnalyzer::Implementation {
203203
unsigned countInherits(ExtensionDecl *ED) {
204204
unsigned Count = 0;
205205
for (auto TL : ED->getInherited()) {
206-
if (shouldPrint(TL.getType()->getAnyNominal(), Options))
206+
auto *nominal = TL.getType()->getAnyNominal();
207+
if (nominal && shouldPrint(nominal, Options))
207208
Count ++;
208209
}
209210
return Count;

lib/AST/Decl.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,9 +2106,42 @@ bool NominalTypeDecl::derivesProtocolConformance(ProtocolDecl *protocol) const {
21062106
return isObjC() && enumDecl->hasCases()
21072107
&& enumDecl->hasOnlyCasesWithoutAssociatedValues();
21082108

2109+
// Enums without associated values and enums with a raw type of String
2110+
// or Int can explicitly derive CodingKey conformance.
2111+
case KnownProtocolKind::CodingKey: {
2112+
Type rawType = enumDecl->getRawType();
2113+
if (rawType) {
2114+
auto parentDC = enumDecl->getDeclContext();
2115+
ASTContext &C = parentDC->getASTContext();
2116+
2117+
auto nominal = rawType->getAnyNominal();
2118+
return nominal == C.getStringDecl() || nominal == C.getIntDecl();
2119+
}
2120+
2121+
// hasOnlyCasesWithoutAssociatedValues will return true for empty enums;
2122+
// empty enumas are allowed to conform as well.
2123+
return enumDecl->hasOnlyCasesWithoutAssociatedValues();
2124+
}
2125+
21092126
default:
21102127
return false;
21112128
}
2129+
} else if (isa<StructDecl>(this) || isa<ClassDecl>(this)) {
2130+
// Structs and classes can explicitly derive Encodable and Decodable
2131+
// conformance (explicitly meaning we can synthesize an implementation if
2132+
// a type conforms manually).
2133+
if (*knownProtocol == KnownProtocolKind::Encodable ||
2134+
*knownProtocol == KnownProtocolKind::Decodable) {
2135+
// FIXME: This is not actually correct. We cannot promise to always
2136+
// provide a witness here for all structs and classes. Unfortunately,
2137+
// figuring out whether this is actually possible requires much more
2138+
// context -- a TypeChecker and the parent decl context at least -- and is
2139+
// tightly coupled to the logic within DerivedConformance.
2140+
// This unfortunately means that we expect a witness even if one will not
2141+
// be produced, which requires DerivedConformance::deriveCodable to output
2142+
// its own diagnostics.
2143+
return true;
2144+
}
21122145
}
21132146
return false;
21142147
}

lib/IRGen/GenMeta.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5538,6 +5538,9 @@ SpecialProtocol irgen::getSpecialProtocolID(ProtocolDecl *P) {
55385538
case KnownProtocolKind::ErrorCodeProtocol:
55395539
case KnownProtocolKind::ExpressibleByBuiltinConstStringLiteral:
55405540
case KnownProtocolKind::ExpressibleByBuiltinConstUTF16StringLiteral:
5541+
case KnownProtocolKind::CodingKey:
5542+
case KnownProtocolKind::Encodable:
5543+
case KnownProtocolKind::Decodable:
55415544
return SpecialProtocol::None;
55425545
}
55435546

lib/Sema/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ add_swift_library(swiftSema STATIC
1616
ConstraintGraph.cpp
1717
ConstraintLocator.cpp
1818
ConstraintSystem.cpp
19+
DerivedConformanceCodable.cpp
20+
DerivedConformanceCodingKey.cpp
1921
DerivedConformanceEquatableHashable.cpp
2022
DerivedConformanceError.cpp
2123
DerivedConformanceRawRepresentable.cpp

0 commit comments

Comments
 (0)