-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add @_private(from: "SourceFile.swift") imports #20428
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
Changes from all commits
963c64e
e4f4dfc
f580778
e4099d0
9feb758
6cc3d37
fbb4236
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,6 +283,15 @@ class ModuleDecl : public DeclContext, public TypeDecl { | |
Bits.ModuleDecl.TestingEnabled = enabled; | ||
} | ||
|
||
/// Returns true if this module was or is begin compile with | ||
/// `-enable-private-imports`. | ||
bool arePrivateImportsEnabled() const { | ||
return Bits.ModuleDecl.PrivateImportsEnabled; | ||
} | ||
void setPrivateImportsEnabled(bool enabled = true) { | ||
Bits.ModuleDecl.PrivateImportsEnabled = true; | ||
} | ||
|
||
/// Returns true if there was an error trying to load this module. | ||
bool failedToLoad() const { | ||
return Bits.ModuleDecl.FailedToLoad; | ||
|
@@ -844,20 +853,38 @@ class SourceFile final : public FileUnit { | |
|
||
/// This source file has access to testable declarations in the imported | ||
/// module. | ||
Testable = 0x2 | ||
Testable = 0x2, | ||
|
||
/// This source file has access to private declarations in the imported | ||
/// module. | ||
PrivateImport = 0x4, | ||
}; | ||
|
||
/// \see ImportFlags | ||
using ImportOptions = OptionSet<ImportFlags>; | ||
|
||
typedef std::pair<ImportOptions, StringRef> ImportOptionsAndFilename; | ||
|
||
struct ImportedModuleDesc { | ||
ModuleDecl::ImportedModule module; | ||
ImportOptions importOptions; | ||
StringRef filename; | ||
|
||
ImportedModuleDesc(ModuleDecl::ImportedModule module, ImportOptions options) | ||
: module(module), importOptions(options) {} | ||
ImportedModuleDesc(ModuleDecl::ImportedModule module, ImportOptions options, | ||
StringRef filename) | ||
: module(module), importOptions(options), filename(filename) {} | ||
}; | ||
|
||
private: | ||
std::unique_ptr<LookupCache> Cache; | ||
LookupCache &getCache() const; | ||
|
||
/// This is the list of modules that are imported by this module. | ||
/// | ||
/// This is filled in by the Name Binding phase. | ||
ArrayRef<std::pair<ModuleDecl::ImportedModule, ImportOptions>> Imports; | ||
ArrayRef<ImportedModuleDesc> Imports; | ||
|
||
/// A unique identifier representing this file; used to mark private decls | ||
/// within the file to keep them from conflicting with other files in the | ||
|
@@ -961,10 +988,9 @@ class SourceFile final : public FileUnit { | |
ImplicitModuleImportKind ModImpKind, bool KeepParsedTokens = false, | ||
bool KeepSyntaxTree = false); | ||
|
||
void | ||
addImports(ArrayRef<std::pair<ModuleDecl::ImportedModule, ImportOptions>> IM); | ||
void addImports(ArrayRef<ImportedModuleDesc> IM); | ||
|
||
bool hasTestableImport(const ModuleDecl *module) const; | ||
bool hasTestableOrPrivateImport(AccessLevel accessLevel, const ValueDecl *ofDecl) const; | ||
|
||
void clearLookupCache(); | ||
|
||
|
@@ -1224,12 +1250,29 @@ class LoadedFile : public FileUnit { | |
assert(classof(this) && "invalid kind"); | ||
} | ||
|
||
/// A map from private/fileprivate decls to the file they were defined in. | ||
llvm::DenseMap<const ValueDecl *, Identifier> FilenameForPrivateDecls; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain why this is necessary? It seems like it should be an on-demand on-disk hash table like the private discriminator map. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works in the same way as When we need to determine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops. I didn't realize we did that eagerly for private discriminators too. …and now I'm working through the reasoning that leads there again: you can't easily look up a decl pointer in a module. My mistake! |
||
|
||
public: | ||
|
||
/// Returns an arbitrary string representing the storage backing this file. | ||
/// | ||
/// This is usually a filesystem path. | ||
virtual StringRef getFilename() const; | ||
|
||
void addFilenameForPrivateDecl(const ValueDecl *decl, Identifier id) { | ||
assert(!FilenameForPrivateDecls.count(decl) || | ||
FilenameForPrivateDecls[decl] == id); | ||
FilenameForPrivateDecls[decl] = id; | ||
} | ||
|
||
StringRef getFilenameForPrivateDecl(const ValueDecl *decl) { | ||
auto it = FilenameForPrivateDecls.find(decl); | ||
if (it == FilenameForPrivateDecls.end()) | ||
return StringRef(); | ||
return it->second.str(); | ||
} | ||
|
||
/// Look up an operator declaration. | ||
/// | ||
/// \param name The operator name ("+", ">>", etc.) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -468,6 +468,12 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options, | |
} | ||
break; | ||
} | ||
|
||
case DAK_PrivateImport: { | ||
Printer.printAttrName("@_private(sourceFile: \""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: The parens and labels shouldn't be part of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. |
||
Printer << cast<PrivateImportAttr>(this)->getSourceFile() << "\")"; | ||
break; | ||
} | ||
|
||
case DAK_SwiftNativeObjCRuntimeBase: { | ||
auto *attr = cast<SwiftNativeObjCRuntimeBaseAttr>(this); | ||
|
@@ -547,7 +553,8 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options, | |
} | ||
|
||
case DAK_DynamicReplacement: { | ||
Printer.printAttrName("@_dynamicReplacement(for: \""); | ||
Printer.printAttrName("@_dynamicReplacement"); | ||
Printer << "(for: \""; | ||
auto *attr = cast<DynamicReplacementAttr>(this); | ||
Printer << attr->getReplacedFunctionName() << "\")"; | ||
break; | ||
|
@@ -621,6 +628,8 @@ StringRef DeclAttribute::getAttrName() const { | |
return "objc"; | ||
case DAK_DynamicReplacement: | ||
return "_dynamicReplacement"; | ||
case DAK_PrivateImport: | ||
return "_private"; | ||
case DAK_RestatedObjCConformance: | ||
return "_restatedObjCConformance"; | ||
case DAK_Inline: { | ||
|
@@ -785,6 +794,22 @@ ObjCAttr *ObjCAttr::clone(ASTContext &context) const { | |
return attr; | ||
} | ||
|
||
PrivateImportAttr::PrivateImportAttr(SourceLoc atLoc, SourceRange baseRange, | ||
StringRef sourceFile, | ||
SourceRange parenRange) | ||
: DeclAttribute(DAK_PrivateImport, atLoc, baseRange, /*Implicit=*/false), | ||
SourceFile(sourceFile) {} | ||
|
||
PrivateImportAttr *PrivateImportAttr::create(ASTContext &Ctxt, SourceLoc AtLoc, | ||
SourceLoc PrivateLoc, | ||
SourceLoc LParenLoc, | ||
StringRef sourceFile, | ||
SourceLoc RParenLoc) { | ||
return new (Ctxt) | ||
PrivateImportAttr(AtLoc, SourceRange(PrivateLoc, RParenLoc), sourceFile, | ||
SourceRange(LParenLoc, RParenLoc)); | ||
} | ||
|
||
DynamicReplacementAttr::DynamicReplacementAttr(SourceLoc atLoc, | ||
SourceRange baseRange, | ||
DeclName name, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused, is it
@_private(sourceFile:)
or@_private(from:)
(like in the commit message :) )