[NFC] Attach SynthesizedFile to any FileUnit

Gives us a place to stuff synthesized declarations for, among other things, imported Clang decls.
This commit is contained in:
Becca Royal-Gordon
2021-10-27 12:23:52 -07:00
parent b9480f12d0
commit 21f58ec78f
7 changed files with 48 additions and 50 deletions

View File

@@ -2857,7 +2857,8 @@ ASTScope &SourceFile::getScope() {
Identifier
SourceFile::getDiscriminatorForPrivateValue(const ValueDecl *D) const {
assert(D->getDeclContext()->getModuleScopeContext() == this);
assert(D->getDeclContext()->getModuleScopeContext() == this ||
D->getDeclContext()->getModuleScopeContext() == getSynthesizedFile());
if (!PrivateDiscriminator.empty())
return PrivateDiscriminator;
@@ -2896,11 +2897,19 @@ SourceFile::getDiscriminatorForPrivateValue(const ValueDecl *D) const {
return PrivateDiscriminator;
}
SynthesizedFileUnit &SourceFile::getOrCreateSynthesizedFile() {
if (SynthesizedFile)
return *SynthesizedFile;
SynthesizedFile = new (getASTContext()) SynthesizedFileUnit(*this);
getParentModule()->addFile(*SynthesizedFile);
SynthesizedFileUnit *FileUnit::getSynthesizedFile() const {
return cast_or_null<SynthesizedFileUnit>(SynthesizedFileAndKind.getPointer());
}
SynthesizedFileUnit &FileUnit::getOrCreateSynthesizedFile() {
auto SynthesizedFile = getSynthesizedFile();
if (!SynthesizedFile) {
if (auto thisSynth = dyn_cast<SynthesizedFileUnit>(this))
return *thisSynth;
SynthesizedFile = new (getASTContext()) SynthesizedFileUnit(*this);
SynthesizedFileAndKind.setPointer(SynthesizedFile);
getParentModule()->addFile(*SynthesizedFile);
}
return *SynthesizedFile;
}
@@ -2950,9 +2959,9 @@ SourceFile::lookupOpaqueResultType(StringRef MangledName) {
// SynthesizedFileUnit Implementation
//===----------------------------------------------------------------------===//
SynthesizedFileUnit::SynthesizedFileUnit(SourceFile &SF)
: FileUnit(FileUnitKind::Synthesized, *SF.getParentModule()), SF(SF) {
SF.getASTContext().addDestructorCleanup(*this);
SynthesizedFileUnit::SynthesizedFileUnit(FileUnit &FU)
: FileUnit(FileUnitKind::Synthesized, *FU.getParentModule()), FU(FU) {
FU.getASTContext().addDestructorCleanup(*this);
}
Identifier
@@ -2963,23 +2972,15 @@ SynthesizedFileUnit::getDiscriminatorForPrivateValue(const ValueDecl *D) const {
if (!PrivateDiscriminator.empty())
return PrivateDiscriminator;
StringRef sourceFileName = getSourceFile().getFilename();
if (sourceFileName.empty()) {
assert(1 == count_if(getParentModule()->getFiles(),
[](const FileUnit *FU) -> bool {
return isa<SourceFile>(FU) &&
cast<SourceFile>(FU)->getFilename().empty();
}) &&
"Cannot promise uniqueness if multiple source files are nameless");
}
// Start with the discriminator that the file we belong to would use.
auto ownerDiscriminator = getFileUnit().getDiscriminatorForPrivateValue(D);
// Use a discriminator invariant across Swift version: a hash of the module
// name, the parent source file name, and a special string.
llvm::MD5 hash;
hash.update(getParentModule()->getName().str());
hash.update(llvm::sys::path::filename(sourceFileName));
// Hash that with a special string to produce a different value that preserves
// the entropy of the original.
// TODO: Use a more robust discriminator for synthesized files. Pick something
// that cannot conflict with `SourceFile` discriminators.
llvm::MD5 hash;
hash.update(ownerDiscriminator.str());
hash.update("SYNTHESIZED FILE");
llvm::MD5::MD5Result result;
hash.final(result);