Skip to content

Commit 3f74fbc

Browse files
authored
Merge pull request #19194 from rudkx/serialization-for-designated-protocols
Add serialization/deserialization support for designated protocols fo…
2 parents ab89158 + 5aca0aa commit 3f74fbc

File tree

4 files changed

+76
-26
lines changed

4 files changed

+76
-26
lines changed

include/swift/AST/Decl.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6287,6 +6287,12 @@ class OperatorDecl : public Decl {
62876287
DesignatedProtocolName(DesignatedProtocolName),
62886288
DesignatedProtocolNameLoc(DesignatedProtocolNameLoc) {}
62896289

6290+
OperatorDecl(DeclKind kind, DeclContext *DC, SourceLoc OperatorLoc,
6291+
Identifier Name, SourceLoc NameLoc,
6292+
ProtocolDecl *DesignatedProtocol)
6293+
: Decl(kind, DC), OperatorLoc(OperatorLoc), NameLoc(NameLoc), name(Name),
6294+
DesignatedProtocol(DesignatedProtocol) {}
6295+
62906296
SourceLoc getLoc() const { return NameLoc; }
62916297

62926298
SourceLoc getOperatorLoc() const { return OperatorLoc; }
@@ -6337,6 +6343,15 @@ class InfixOperatorDecl : public OperatorDecl {
63376343
SecondIdentifierLoc(secondIdentifierLoc),
63386344
FirstIdentifier(firstIdentifier), SecondIdentifier(secondIdentifier) {}
63396345

6346+
InfixOperatorDecl(DeclContext *DC, SourceLoc operatorLoc, Identifier name,
6347+
SourceLoc nameLoc, SourceLoc colonLoc,
6348+
Identifier firstIdentifier, SourceLoc firstIdentifierLoc,
6349+
ProtocolDecl *designatedProtocol)
6350+
: OperatorDecl(DeclKind::InfixOperator, DC, operatorLoc, name, nameLoc,
6351+
designatedProtocol),
6352+
ColonLoc(colonLoc), FirstIdentifierLoc(firstIdentifierLoc),
6353+
FirstIdentifier(firstIdentifier) {}
6354+
63406355
SourceLoc getEndLoc() const {
63416356
if (!SecondIdentifier.empty())
63426357
return SecondIdentifierLoc;
@@ -6385,6 +6400,11 @@ class PrefixOperatorDecl : public OperatorDecl {
63856400
: OperatorDecl(DeclKind::PrefixOperator, DC, OperatorLoc, Name, NameLoc,
63866401
DesignatedProtocolName, DesignatedProtocolNameLoc) {}
63876402

6403+
PrefixOperatorDecl(DeclContext *DC, SourceLoc OperatorLoc, Identifier Name,
6404+
SourceLoc NameLoc, ProtocolDecl *DesignatedProtocol)
6405+
: OperatorDecl(DeclKind::PrefixOperator, DC, OperatorLoc, Name, NameLoc,
6406+
DesignatedProtocol) {}
6407+
63886408
SourceRange getSourceRange() const {
63896409
return { getOperatorLoc(), getNameLoc() };
63906410
}
@@ -6414,6 +6434,11 @@ class PostfixOperatorDecl : public OperatorDecl {
64146434
: OperatorDecl(DeclKind::PostfixOperator, DC, OperatorLoc, Name, NameLoc,
64156435
DesignatedProtocolName, DesignatedProtocolNameLoc) {}
64166436

6437+
PostfixOperatorDecl(DeclContext *DC, SourceLoc OperatorLoc, Identifier Name,
6438+
SourceLoc NameLoc, ProtocolDecl *DesignatedProtocol)
6439+
: OperatorDecl(DeclKind::PostfixOperator, DC, OperatorLoc, Name, NameLoc,
6440+
DesignatedProtocol) {}
6441+
64176442
SourceRange getSourceRange() const {
64186443
return { getOperatorLoc(), getNameLoc() };
64196444
}

include/swift/Serialization/ModuleFormat.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -55,7 +55,7 @@ const uint16_t VERSION_MAJOR = 0;
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
5757
/// Don't worry about adhering to the 80-column limit for this line.
58-
const uint16_t VERSION_MINOR = 441; // Last change: remove end_borrow src arg
58+
const uint16_t VERSION_MINOR = 442; // Last change: operator protocol
5959

6060
using DeclIDField = BCFixed<31>;
6161

@@ -1089,8 +1089,9 @@ namespace decls_block {
10891089
template <unsigned Code>
10901090
using UnaryOperatorLayout = BCRecordLayout<
10911091
Code, // ID field
1092-
IdentifierIDField, // name
1093-
DeclContextIDField // context decl
1092+
IdentifierIDField, // name
1093+
DeclContextIDField, // context decl
1094+
DeclIDField // protocol
10941095
>;
10951096

10961097
using PrefixOperatorLayout = UnaryOperatorLayout<PREFIX_OPERATOR_DECL>;
@@ -1100,7 +1101,8 @@ namespace decls_block {
11001101
INFIX_OPERATOR_DECL,
11011102
IdentifierIDField, // name
11021103
DeclContextIDField,// context decl
1103-
DeclIDField // precedence group
1104+
DeclIDField, // precedence group
1105+
DeclIDField // protocol
11041106
>;
11051107

11061108
using PrecedenceGroupLayout = BCRecordLayout<

lib/Serialization/Deserialization.cpp

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -3378,37 +3378,54 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
33783378
case decls_block::PREFIX_OPERATOR_DECL: {
33793379
IdentifierID nameID;
33803380
DeclContextID contextID;
3381+
DeclID protoID;
33813382

3382-
decls_block::PrefixOperatorLayout::readRecord(scratch, nameID,
3383-
contextID);
3383+
decls_block::PrefixOperatorLayout::readRecord(scratch, nameID, contextID,
3384+
protoID);
33843385
auto DC = getDeclContext(contextID);
3385-
declOrOffset = createDecl<PrefixOperatorDecl>(DC, SourceLoc(),
3386-
getIdentifier(nameID),
3387-
SourceLoc());
3386+
3387+
Expected<Decl *> protocol = getDeclChecked(protoID);
3388+
if (!protocol)
3389+
return protocol.takeError();
3390+
3391+
auto result = createDecl<PrefixOperatorDecl>(
3392+
DC, SourceLoc(), getIdentifier(nameID), SourceLoc(),
3393+
cast_or_null<ProtocolDecl>(protocol.get()));
3394+
3395+
declOrOffset = result;
33883396
break;
33893397
}
33903398

33913399
case decls_block::POSTFIX_OPERATOR_DECL: {
33923400
IdentifierID nameID;
33933401
DeclContextID contextID;
3402+
DeclID protoID;
33943403

3395-
decls_block::PostfixOperatorLayout::readRecord(scratch, nameID,
3396-
contextID);
3404+
decls_block::PostfixOperatorLayout::readRecord(scratch, nameID, contextID,
3405+
protoID);
33973406

33983407
auto DC = getDeclContext(contextID);
3399-
declOrOffset = createDecl<PostfixOperatorDecl>(DC, SourceLoc(),
3400-
getIdentifier(nameID),
3401-
SourceLoc());
3408+
3409+
Expected<Decl *> protocol = getDeclChecked(protoID);
3410+
if (!protocol)
3411+
return protocol.takeError();
3412+
3413+
auto result = createDecl<PostfixOperatorDecl>(
3414+
DC, SourceLoc(), getIdentifier(nameID), SourceLoc(),
3415+
cast_or_null<ProtocolDecl>(protocol.get()));
3416+
3417+
declOrOffset = result;
34023418
break;
34033419
}
34043420

34053421
case decls_block::INFIX_OPERATOR_DECL: {
34063422
IdentifierID nameID;
34073423
DeclContextID contextID;
34083424
DeclID precedenceGroupID;
3425+
DeclID protoID;
34093426

34103427
decls_block::InfixOperatorLayout::readRecord(scratch, nameID, contextID,
3411-
precedenceGroupID);
3428+
precedenceGroupID, protoID);
34123429

34133430
PrecedenceGroupDecl *precedenceGroup = nullptr;
34143431
Identifier precedenceGroupName;
@@ -3422,11 +3439,14 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
34223439

34233440
auto DC = getDeclContext(contextID);
34243441

3425-
auto result = createDecl<InfixOperatorDecl>(DC, SourceLoc(),
3426-
getIdentifier(nameID),
3427-
SourceLoc(), SourceLoc(),
3428-
precedenceGroupName,
3429-
SourceLoc());
3442+
Expected<Decl *> protocol = getDeclChecked(protoID);
3443+
if (!protocol)
3444+
return protocol.takeError();
3445+
3446+
auto result = createDecl<InfixOperatorDecl>(
3447+
DC, SourceLoc(), getIdentifier(nameID), SourceLoc(), SourceLoc(),
3448+
precedenceGroupName, SourceLoc(),
3449+
cast_or_null<ProtocolDecl>(protocol.get()));
34303450
result->setPrecedenceGroup(precedenceGroup);
34313451

34323452
declOrOffset = result;

lib/Serialization/Serialization.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2819,10 +2819,11 @@ void Serializer::writeDecl(const Decl *D) {
28192819
auto contextID = addDeclContextRef(op->getDeclContext());
28202820
auto nameID = addDeclBaseNameRef(op->getName());
28212821
auto groupID = addDeclRef(op->getPrecedenceGroup());
2822+
auto protoID = addDeclRef(op->getDesignatedProtocol());
28222823

28232824
unsigned abbrCode = DeclTypeAbbrCodes[InfixOperatorLayout::Code];
2824-
InfixOperatorLayout::emitRecord(Out, ScratchRecord, abbrCode,
2825-
nameID, contextID, groupID);
2825+
InfixOperatorLayout::emitRecord(Out, ScratchRecord, abbrCode, nameID,
2826+
contextID, groupID, protoID);
28262827
break;
28272828
}
28282829

@@ -2831,11 +2832,12 @@ void Serializer::writeDecl(const Decl *D) {
28312832
verifyAttrSerializable(op);
28322833

28332834
auto contextID = addDeclContextRef(op->getDeclContext());
2835+
auto protoID = addDeclRef(op->getDesignatedProtocol());
28342836

28352837
unsigned abbrCode = DeclTypeAbbrCodes[PrefixOperatorLayout::Code];
28362838
PrefixOperatorLayout::emitRecord(Out, ScratchRecord, abbrCode,
28372839
addDeclBaseNameRef(op->getName()),
2838-
contextID);
2840+
contextID, protoID);
28392841
break;
28402842
}
28412843

@@ -2844,11 +2846,12 @@ void Serializer::writeDecl(const Decl *D) {
28442846
verifyAttrSerializable(op);
28452847

28462848
auto contextID = addDeclContextRef(op->getDeclContext());
2849+
auto protoID = addDeclRef(op->getDesignatedProtocol());
28472850

28482851
unsigned abbrCode = DeclTypeAbbrCodes[PostfixOperatorLayout::Code];
28492852
PostfixOperatorLayout::emitRecord(Out, ScratchRecord, abbrCode,
28502853
addDeclBaseNameRef(op->getName()),
2851-
contextID);
2854+
contextID, protoID);
28522855
break;
28532856
}
28542857

0 commit comments

Comments
 (0)