mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[Serialization] Filter Decl to deserialize by their attributes
Add an alternative to getTopLevelDecls and getDeclChecked to limit which decls are deserialized by first looking at their attributes. If the attributes are accepted by a function passed as argument the decl is fully deserialized, otherwise it is ignored. The filter is included in the signature of existing functions in the Serilalization services, but I’ve added new methods for it in FileUnit and its subclasses to leave existing implementations untouched.
This commit is contained in:
@@ -644,6 +644,12 @@ void ModuleDecl::getTopLevelDecls(SmallVectorImpl<Decl*> &Results) const {
|
||||
FORWARD(getTopLevelDecls, (Results));
|
||||
}
|
||||
|
||||
void ModuleDecl::getTopLevelDeclsWhereAttributesMatch(
|
||||
SmallVectorImpl<Decl*> &Results,
|
||||
llvm::function_ref<bool(DeclAttributes)> matchAttributes) const {
|
||||
FORWARD(getTopLevelDeclsWhereAttributesMatch, (Results, matchAttributes));
|
||||
}
|
||||
|
||||
void SourceFile::getTopLevelDecls(SmallVectorImpl<Decl*> &Results) const {
|
||||
Results.append(Decls.begin(), Decls.end());
|
||||
}
|
||||
@@ -1873,6 +1879,22 @@ void *FileUnit::operator new(size_t Bytes, ASTContext &C, unsigned Alignment) {
|
||||
return C.Allocate(Bytes, Alignment);
|
||||
}
|
||||
|
||||
void FileUnit::getTopLevelDeclsWhereAttributesMatch(
|
||||
SmallVectorImpl<Decl*> &Results,
|
||||
llvm::function_ref<bool(DeclAttributes)> matchAttributes) const {
|
||||
auto prevSize = Results.size();
|
||||
getTopLevelDecls(Results);
|
||||
|
||||
// Filter out unwanted decls that were just added to Results.
|
||||
// Note: We could apply this check in all implementations of
|
||||
// getTopLevelDecls instead or in everything that creates a Decl.
|
||||
auto newEnd = std::remove_if(Results.begin() + prevSize, Results.end(),
|
||||
[&matchAttributes](const Decl *D) -> bool {
|
||||
return !matchAttributes(D->getAttrs());
|
||||
});
|
||||
Results.erase(newEnd, Results.end());
|
||||
}
|
||||
|
||||
StringRef LoadedFile::getFilename() const {
|
||||
return "";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user