-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Implement Swift serialization and deserialization of Clang types #29670
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
rjmccall
merged 3 commits into
swiftlang:master
from
rjmccall:ciliainig-teyepee-sieeriiieaileiizieaitieieoiniie
Feb 7, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//===- SwiftAbstractBasicReader.h - Clang serialization adapter -*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2017 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 | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file provides an intermediate CRTP class which implements most of | ||
// Clang's AbstractBasicReader interface, paralleling the behavior defined | ||
// in SwiftAbstractBasicWriter. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SWIFT_CLANGIMPORTER_SWIFTABSTRACTBASICREADER_H | ||
#define SWIFT_CLANGIMPORTER_SWIFTABSTRACTBASICREADER_H | ||
|
||
#include "clang/AST/AbstractTypeReader.h" | ||
|
||
// This include is required to instantiate the template code in | ||
// AbstractBasicReader.h, i.e. it's a workaround to an include-what-you-use | ||
// violation. | ||
#include "clang/AST/DeclObjC.h" | ||
|
||
namespace swift { | ||
|
||
/// An implementation of Clang's AbstractBasicReader interface for a Swift | ||
/// datastream-based reader. This is paired with the AbstractBasicWriter | ||
/// implementation in SwiftAbstractBasicWriter.h. Note that the general | ||
/// expectation is that the types and declarations involved will have passed | ||
/// a serializability check when this is used for actual deserialization. | ||
/// | ||
/// The subclass must implement: | ||
/// uint64_t readUInt64(); | ||
/// clang::IdentifierInfo *readIdentifier(); | ||
/// clang::Stmt *readStmtRef(); | ||
/// clang::Decl *readDeclRef(); | ||
template <class Impl> | ||
class DataStreamBasicReader | ||
: public clang::serialization::DataStreamBasicReader<Impl> { | ||
using super = clang::serialization::DataStreamBasicReader<Impl>; | ||
public: | ||
using super::asImpl; | ||
using super::getASTContext; | ||
|
||
DataStreamBasicReader(clang::ASTContext &ctx) : super(ctx) {} | ||
|
||
/// Perform all the calls necessary to write out the given type. | ||
clang::QualType readTypeRef() { | ||
auto kind = clang::Type::TypeClass(asImpl().readUInt64()); | ||
return clang::serialization::AbstractTypeReader<Impl>(asImpl()).read(kind); | ||
} | ||
|
||
bool readBool() { | ||
return asImpl().readUInt64() != 0; | ||
} | ||
|
||
uint32_t readUInt32() { | ||
return uint32_t(asImpl().readUInt64()); | ||
} | ||
|
||
clang::Selector readSelector() { | ||
uint64_t numArgsPlusOne = asImpl().readUInt64(); | ||
|
||
// The null case. | ||
if (numArgsPlusOne == 0) | ||
return clang::Selector(); | ||
|
||
unsigned numArgs = unsigned(numArgsPlusOne - 1); | ||
SmallVector<clang::IdentifierInfo *, 4> chunks; | ||
for (unsigned i = 0, e = std::max(numArgs, 1U); i != e; ++i) | ||
chunks.push_back(asImpl().readIdentifier()); | ||
|
||
return getASTContext().Selectors.getSelector(numArgs, chunks.data()); | ||
} | ||
|
||
clang::SourceLocation readSourceLocation() { | ||
// Always read null. | ||
return clang::SourceLocation(); | ||
rjmccall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
clang::QualType readQualType() { | ||
clang::Qualifiers quals = asImpl().readQualifiers(); | ||
clang::QualType type = asImpl().readTypeRef(); | ||
return getASTContext().getQualifiedType(type, quals); | ||
} | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//===- SwiftAbstractBasicWriter.h - Clang serialization adapter -*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2017 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 | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file provides an intermediate CRTP class which implements most of | ||
// Clang's AbstractBasicWriter interface, allowing largely the same logic | ||
// to be used for both the importer's "can this be serialized" checks and | ||
// the serializer's actual serialization logic. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SWIFT_CLANGIMPORTER_SWIFTABSTRACTBASICWRITER_H | ||
#define SWIFT_CLANGIMPORTER_SWIFTABSTRACTBASICWRITER_H | ||
|
||
#include "clang/AST/AbstractTypeWriter.h" | ||
|
||
namespace swift { | ||
|
||
/// An implementation of Clang's AbstractBasicWriter interface for a Swift | ||
/// datastream-based reader. This is paired with the AbstractBasicReader | ||
/// implementation in SwiftAbstractBasicReader.h. Note that the general | ||
/// expectation is that the types and declarations involved will have passed | ||
/// a serializability check when this is used for actual serialization. | ||
/// The code in this class is also used when implementing that | ||
/// serializability check and so must be a little more cautious. | ||
/// | ||
/// The subclass must implement: | ||
/// void writeUInt64(uint64_t value); | ||
/// void writeIdentifier(const clang::IdentifierInfo *ident); | ||
/// void writeStmtRef(const clang::Stmt *stmt); | ||
/// void writeDeclRef(const clang::Decl *decl); | ||
template <class Impl> | ||
class DataStreamBasicWriter | ||
: public clang::serialization::DataStreamBasicWriter<Impl> { | ||
using super = clang::serialization::DataStreamBasicWriter<Impl>; | ||
public: | ||
using super::asImpl; | ||
|
||
/// Perform all the calls necessary to write out the given type. | ||
void writeTypeRef(const clang::Type *type) { | ||
asImpl().writeUInt64(uint64_t(type->getTypeClass())); | ||
clang::serialization::AbstractTypeWriter<Impl>(asImpl()).write(type); | ||
} | ||
|
||
void writeBool(bool value) { | ||
asImpl().writeUInt64(uint64_t(value)); | ||
} | ||
|
||
void writeUInt32(uint32_t value) { | ||
asImpl().writeUInt64(uint64_t(value)); | ||
} | ||
|
||
void writeSelector(clang::Selector selector) { | ||
if (selector.isNull()) { | ||
rjmccall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
asImpl().writeUInt64(0); | ||
return; | ||
} | ||
|
||
asImpl().writeUInt64(selector.getNumArgs() + 1); | ||
for (unsigned i = 0, e = std::max(selector.getNumArgs(), 1U); i != e; ++i) | ||
asImpl().writeIdentifier(selector.getIdentifierInfoForSlot(i)); | ||
} | ||
|
||
void writeSourceLocation(clang::SourceLocation loc) { | ||
// DataStreamBasicReader will always read null; the serializability | ||
// check overrides this to complain about non-null source locations. | ||
} | ||
|
||
void writeQualType(clang::QualType type) { | ||
assert(!type.isNull()); | ||
|
||
auto split = type.split(); | ||
asImpl().writeQualifiers(split.Quals); | ||
|
||
// Just recursively visit the given type. | ||
asImpl().writeTypeRef(split.Ty); | ||
} | ||
}; | ||
|
||
} | ||
|
||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.