Skip to content

Add serialization/deserialization support for designated protocols fo… #19194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6283,6 +6283,12 @@ class OperatorDecl : public Decl {
DesignatedProtocolName(DesignatedProtocolName),
DesignatedProtocolNameLoc(DesignatedProtocolNameLoc) {}

OperatorDecl(DeclKind kind, DeclContext *DC, SourceLoc OperatorLoc,
Identifier Name, SourceLoc NameLoc,
ProtocolDecl *DesignatedProtocol)
: Decl(kind, DC), OperatorLoc(OperatorLoc), NameLoc(NameLoc), name(Name),
DesignatedProtocol(DesignatedProtocol) {}

SourceLoc getLoc() const { return NameLoc; }

SourceLoc getOperatorLoc() const { return OperatorLoc; }
Expand Down Expand Up @@ -6333,6 +6339,15 @@ class InfixOperatorDecl : public OperatorDecl {
SecondIdentifierLoc(secondIdentifierLoc),
FirstIdentifier(firstIdentifier), SecondIdentifier(secondIdentifier) {}

InfixOperatorDecl(DeclContext *DC, SourceLoc operatorLoc, Identifier name,
SourceLoc nameLoc, SourceLoc colonLoc,
Identifier firstIdentifier, SourceLoc firstIdentifierLoc,
ProtocolDecl *designatedProtocol)
: OperatorDecl(DeclKind::InfixOperator, DC, operatorLoc, name, nameLoc,
designatedProtocol),
ColonLoc(colonLoc), FirstIdentifierLoc(firstIdentifierLoc),
FirstIdentifier(firstIdentifier) {}

SourceLoc getEndLoc() const {
if (!SecondIdentifier.empty())
return SecondIdentifierLoc;
Expand Down Expand Up @@ -6381,6 +6396,11 @@ class PrefixOperatorDecl : public OperatorDecl {
: OperatorDecl(DeclKind::PrefixOperator, DC, OperatorLoc, Name, NameLoc,
DesignatedProtocolName, DesignatedProtocolNameLoc) {}

PrefixOperatorDecl(DeclContext *DC, SourceLoc OperatorLoc, Identifier Name,
SourceLoc NameLoc, ProtocolDecl *DesignatedProtocol)
: OperatorDecl(DeclKind::PrefixOperator, DC, OperatorLoc, Name, NameLoc,
DesignatedProtocol) {}

SourceRange getSourceRange() const {
return { getOperatorLoc(), getNameLoc() };
}
Expand Down Expand Up @@ -6410,6 +6430,11 @@ class PostfixOperatorDecl : public OperatorDecl {
: OperatorDecl(DeclKind::PostfixOperator, DC, OperatorLoc, Name, NameLoc,
DesignatedProtocolName, DesignatedProtocolNameLoc) {}

PostfixOperatorDecl(DeclContext *DC, SourceLoc OperatorLoc, Identifier Name,
SourceLoc NameLoc, ProtocolDecl *DesignatedProtocol)
: OperatorDecl(DeclKind::PostfixOperator, DC, OperatorLoc, Name, NameLoc,
DesignatedProtocol) {}

SourceRange getSourceRange() const {
return { getOperatorLoc(), getNameLoc() };
}
Expand Down
12 changes: 7 additions & 5 deletions include/swift/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -55,7 +55,7 @@ const uint16_t VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t VERSION_MINOR = 441; // Last change: remove end_borrow src arg
const uint16_t VERSION_MINOR = 442; // Last change: operator protocol

using DeclIDField = BCFixed<31>;

Expand Down Expand Up @@ -1089,8 +1089,9 @@ namespace decls_block {
template <unsigned Code>
using UnaryOperatorLayout = BCRecordLayout<
Code, // ID field
IdentifierIDField, // name
DeclContextIDField // context decl
IdentifierIDField, // name
DeclContextIDField, // context decl
DeclIDField // protocol
>;

using PrefixOperatorLayout = UnaryOperatorLayout<PREFIX_OPERATOR_DECL>;
Expand All @@ -1100,7 +1101,8 @@ namespace decls_block {
INFIX_OPERATOR_DECL,
IdentifierIDField, // name
DeclContextIDField,// context decl
DeclIDField // precedence group
DeclIDField, // precedence group
DeclIDField // protocol
>;

using PrecedenceGroupLayout = BCRecordLayout<
Expand Down
54 changes: 37 additions & 17 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -3378,37 +3378,54 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
case decls_block::PREFIX_OPERATOR_DECL: {
IdentifierID nameID;
DeclContextID contextID;
DeclID protoID;

decls_block::PrefixOperatorLayout::readRecord(scratch, nameID,
contextID);
decls_block::PrefixOperatorLayout::readRecord(scratch, nameID, contextID,
protoID);
auto DC = getDeclContext(contextID);
declOrOffset = createDecl<PrefixOperatorDecl>(DC, SourceLoc(),
getIdentifier(nameID),
SourceLoc());

Expected<Decl *> protocol = getDeclChecked(protoID);
if (!protocol)
return protocol.takeError();

auto result = createDecl<PrefixOperatorDecl>(
DC, SourceLoc(), getIdentifier(nameID), SourceLoc(),
cast_or_null<ProtocolDecl>(protocol.get()));

declOrOffset = result;
break;
}

case decls_block::POSTFIX_OPERATOR_DECL: {
IdentifierID nameID;
DeclContextID contextID;
DeclID protoID;

decls_block::PostfixOperatorLayout::readRecord(scratch, nameID,
contextID);
decls_block::PostfixOperatorLayout::readRecord(scratch, nameID, contextID,
protoID);

auto DC = getDeclContext(contextID);
declOrOffset = createDecl<PostfixOperatorDecl>(DC, SourceLoc(),
getIdentifier(nameID),
SourceLoc());

Expected<Decl *> protocol = getDeclChecked(protoID);
if (!protocol)
return protocol.takeError();

auto result = createDecl<PostfixOperatorDecl>(
DC, SourceLoc(), getIdentifier(nameID), SourceLoc(),
cast_or_null<ProtocolDecl>(protocol.get()));

declOrOffset = result;
break;
}

case decls_block::INFIX_OPERATOR_DECL: {
IdentifierID nameID;
DeclContextID contextID;
DeclID precedenceGroupID;
DeclID protoID;

decls_block::InfixOperatorLayout::readRecord(scratch, nameID, contextID,
precedenceGroupID);
precedenceGroupID, protoID);

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

auto DC = getDeclContext(contextID);

auto result = createDecl<InfixOperatorDecl>(DC, SourceLoc(),
getIdentifier(nameID),
SourceLoc(), SourceLoc(),
precedenceGroupName,
SourceLoc());
Expected<Decl *> protocol = getDeclChecked(protoID);
if (!protocol)
return protocol.takeError();

auto result = createDecl<InfixOperatorDecl>(
DC, SourceLoc(), getIdentifier(nameID), SourceLoc(), SourceLoc(),
precedenceGroupName, SourceLoc(),
cast_or_null<ProtocolDecl>(protocol.get()));
result->setPrecedenceGroup(precedenceGroup);

declOrOffset = result;
Expand Down
11 changes: 7 additions & 4 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2819,10 +2819,11 @@ void Serializer::writeDecl(const Decl *D) {
auto contextID = addDeclContextRef(op->getDeclContext());
auto nameID = addDeclBaseNameRef(op->getName());
auto groupID = addDeclRef(op->getPrecedenceGroup());
auto protoID = addDeclRef(op->getDesignatedProtocol());

unsigned abbrCode = DeclTypeAbbrCodes[InfixOperatorLayout::Code];
InfixOperatorLayout::emitRecord(Out, ScratchRecord, abbrCode,
nameID, contextID, groupID);
InfixOperatorLayout::emitRecord(Out, ScratchRecord, abbrCode, nameID,
contextID, groupID, protoID);
break;
}

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

auto contextID = addDeclContextRef(op->getDeclContext());
auto protoID = addDeclRef(op->getDesignatedProtocol());

unsigned abbrCode = DeclTypeAbbrCodes[PrefixOperatorLayout::Code];
PrefixOperatorLayout::emitRecord(Out, ScratchRecord, abbrCode,
addDeclBaseNameRef(op->getName()),
contextID);
contextID, protoID);
break;
}

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

auto contextID = addDeclContextRef(op->getDeclContext());
auto protoID = addDeclRef(op->getDesignatedProtocol());

unsigned abbrCode = DeclTypeAbbrCodes[PostfixOperatorLayout::Code];
PostfixOperatorLayout::emitRecord(Out, ScratchRecord, abbrCode,
addDeclBaseNameRef(op->getName()),
contextID);
contextID, protoID);
break;
}

Expand Down