Skip to content

Make initial changes for module selectors #81459

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 4 commits into from
May 15, 2025
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
13 changes: 13 additions & 0 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,23 @@ BridgedDeclNameLoc BridgedDeclNameLoc_createParsed(
BridgedSourceLoc cLParenLoc, BridgedArrayRef cLabelLocs,
BridgedSourceLoc cRParenLoc);

SWIFT_NAME("BridgedDeclNameLoc.createParsed(_:moduleSelectorLoc:baseNameLoc:"
"lParenLoc:argumentLabelLocs:rParenLoc:)")
BridgedDeclNameLoc BridgedDeclNameLoc_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cModuleSelectorLoc,
BridgedSourceLoc cBaseNameLoc, BridgedSourceLoc cLParenLoc,
BridgedArrayRef cLabelLocs, BridgedSourceLoc cRParenLoc);

SWIFT_NAME("BridgedDeclNameLoc.createParsed(_:)")
BridgedDeclNameLoc
BridgedDeclNameLoc_createParsed(BridgedSourceLoc cBaseNameLoc);

SWIFT_NAME("BridgedDeclNameLoc.createParsed(_:moduleSelectorLoc:baseNameLoc:)")
BridgedDeclNameLoc
BridgedDeclNameLoc_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cModuleSelectorLoc,
BridgedSourceLoc cBaseNameLoc);

//===----------------------------------------------------------------------===//
// MARK: ASTContext
//===----------------------------------------------------------------------===//
Expand Down
17 changes: 17 additions & 0 deletions include/swift/AST/DeclNameLoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "swift/Basic/LLVM.h"
#include "swift/Basic/SourceLoc.h"

#include "llvm/ADT/ArrayRef.h"

class BridgedDeclNameLoc;

namespace swift {
Expand Down Expand Up @@ -71,13 +73,24 @@ class DeclNameLoc {
explicit DeclNameLoc(SourceLoc baseNameLoc)
: DeclNameLoc(baseNameLoc.getOpaquePointerValue(), 0) {}

explicit DeclNameLoc(ASTContext &ctx, SourceLoc moduleSelectorLoc,
SourceLoc baseNameLoc)
: DeclNameLoc(baseNameLoc) { }

/// Create declaration name location information for a compound
/// name.
DeclNameLoc(ASTContext &ctx, SourceLoc baseNameLoc,
SourceLoc lParenLoc,
ArrayRef<SourceLoc> argumentLabelLocs,
SourceLoc rParenLoc);

DeclNameLoc(ASTContext &ctx, SourceLoc moduleSelectorLoc,
SourceLoc baseNameLoc,
SourceLoc lParenLoc,
ArrayRef<SourceLoc> argumentLabelLocs,
SourceLoc rParenLoc)
: DeclNameLoc(ctx, baseNameLoc, lParenLoc, argumentLabelLocs, rParenLoc) { }

/// Whether the location information is valid.
bool isValid() const { return getBaseNameLoc().isValid(); }

Expand Down Expand Up @@ -111,6 +124,10 @@ class DeclNameLoc {
return getSourceLocs()[FirstArgumentLabelIndex + index];
}

SourceLoc getModuleSelectorLoc() const {
return SourceLoc();
}

SourceLoc getStartLoc() const {
return getBaseNameLoc();
}
Expand Down
55 changes: 31 additions & 24 deletions include/swift/AST/Identifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -693,65 +693,73 @@ class DeclNameRef {
void *getOpaqueValue() const { return FullName.getOpaqueValue(); }
static DeclNameRef getFromOpaqueValue(void *p);

explicit DeclNameRef(ASTContext &C, Identifier moduleSelector,
DeclName fullName)
: FullName(fullName) { }

explicit DeclNameRef(ASTContext &C, Identifier moduleSelector,
DeclBaseName baseName, ArrayRef<Identifier> argLabels)
: FullName(C, baseName, argLabels) { }

explicit DeclNameRef(DeclName FullName)
: FullName(FullName) { }

explicit DeclNameRef(DeclBaseName BaseName)
: FullName(BaseName) { }
bool hasModuleSelector() const {
return false;
}

explicit DeclNameRef(Identifier BaseName)
: FullName(BaseName) { }
Identifier getModuleSelector() const {
return Identifier();
}

/// The name of the declaration being referenced.
DeclName getFullName() const {
return FullName;
}

DeclName &getFullName() {
return FullName;
}

/// The base name of the declaration being referenced.
DeclBaseName getBaseName() const {
return FullName.getBaseName();
return getFullName().getBaseName();
}

Identifier getBaseIdentifier() const {
return FullName.getBaseIdentifier();
return getFullName().getBaseIdentifier();
}

ArrayRef<Identifier> getArgumentNames() const {
return FullName.getArgumentNames();
return getFullName().getArgumentNames();
}

bool isSimpleName() const {
return FullName.isSimpleName();
return getFullName().isSimpleName();
}

bool isSimpleName(DeclBaseName name) const {
return FullName.isSimpleName(name);
return getFullName().isSimpleName(name);
}

bool isSimpleName(StringRef name) const {
return FullName.isSimpleName(name);
return getFullName().isSimpleName(name);
}

bool isSpecial() const {
return FullName.isSpecial();
return getFullName().isSpecial();
}

bool isOperator() const {
return FullName.isOperator();
return getFullName().isOperator();
}

bool mustAlwaysBeEscaped() const { return FullName.mustAlwaysBeEscaped(); }
bool mustAlwaysBeEscaped() const {
return getFullName().mustAlwaysBeEscaped();
}

bool isCompoundName() const {
return FullName.isCompoundName();
return getFullName().isCompoundName();
}

explicit operator bool() const {
return (bool)FullName;
return (bool)getFullName();
}

/// Compare two declaration names, producing -1 if \c *this comes before
Expand Down Expand Up @@ -791,7 +799,7 @@ class DeclNameRef {
return lhs.compare(rhs) >= 0;
}

DeclNameRef withoutArgumentLabels() const;
DeclNameRef withoutArgumentLabels(ASTContext &C) const;
DeclNameRef withArgumentLabels(ASTContext &C,
ArrayRef<Identifier> argumentNames) const;

Expand Down Expand Up @@ -824,16 +832,15 @@ inline DeclNameRef DeclNameRef::getFromOpaqueValue(void *p) {
return DeclNameRef(DeclName::getFromOpaqueValue(p));
}

inline DeclNameRef DeclNameRef::withoutArgumentLabels() const {
return DeclNameRef(getBaseName());
inline DeclNameRef DeclNameRef::withoutArgumentLabels(ASTContext &C) const {
return DeclNameRef(C, getModuleSelector(), getBaseName());
}

inline DeclNameRef DeclNameRef::withArgumentLabels(
ASTContext &C, ArrayRef<Identifier> argumentNames) const {
return DeclNameRef(DeclName(C, getBaseName(), argumentNames));
return DeclNameRef(C, getModuleSelector(), getBaseName(), argumentNames);
}


inline DeclNameRef DeclNameRef::createSubscript() {
return DeclNameRef(DeclBaseName::createSubscript());
}
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,9 @@ EXPERIMENTAL_FEATURE(CDecl, false)
/// Allow use of `@extensible` on public enums
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(ExtensibleAttribute, false)

/// Allow use of `Module::name` syntax
EXPERIMENTAL_FEATURE(ModuleSelector, false)

#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
#undef EXPERIMENTAL_FEATURE
#undef UPCOMING_FEATURE
Expand Down
20 changes: 19 additions & 1 deletion lib/AST/Bridging/DeclBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,23 @@ BridgedDeclNameLoc BridgedDeclNameLoc_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cBaseNameLoc,
BridgedSourceLoc cLParenLoc, BridgedArrayRef cLabelLocs,
BridgedSourceLoc cRParenLoc) {
return BridgedDeclNameLoc_createParsed(
cContext, BridgedSourceLoc(), cBaseNameLoc, cLParenLoc, cLabelLocs,
cRParenLoc);
}

BridgedDeclNameLoc BridgedDeclNameLoc_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cModuleSelectorLoc,
BridgedSourceLoc cBaseNameLoc, BridgedSourceLoc cLParenLoc,
BridgedArrayRef cLabelLocs, BridgedSourceLoc cRParenLoc) {

ASTContext &context = cContext.unbridged();
SmallVector<SourceLoc, 4> labelLocs;
for (auto &cLabelLoc : cLabelLocs.unbridged<BridgedSourceLoc>())
labelLocs.push_back(cLabelLoc.unbridged());

return DeclNameLoc(context, cBaseNameLoc.unbridged(), cLParenLoc.unbridged(),
return DeclNameLoc(context, cModuleSelectorLoc.unbridged(),
cBaseNameLoc.unbridged(), cLParenLoc.unbridged(),
labelLocs, cRParenLoc.unbridged());
}

Expand All @@ -85,6 +95,14 @@ BridgedDeclNameLoc_createParsed(BridgedSourceLoc cBaseNameLoc) {
return DeclNameLoc(cBaseNameLoc.unbridged());
}

BridgedDeclNameLoc
BridgedDeclNameLoc_createParsed(
BridgedASTContext cContext, BridgedSourceLoc cModuleSelectorLoc,
BridgedSourceLoc cBaseNameLoc) {
return DeclNameLoc(cContext.unbridged(), cModuleSelectorLoc.unbridged(),
cBaseNameLoc.unbridged());
}

//===----------------------------------------------------------------------===//
// MARK: Decls
//===----------------------------------------------------------------------===//
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/FeatureSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ UNINTERESTING_FEATURE(MacrosOnImports)
UNINTERESTING_FEATURE(NonisolatedNonsendingByDefault)
UNINTERESTING_FEATURE(KeyPathWithMethodMembers)

// TODO: Return true for inlinable function bodies with module selectors in them
UNINTERESTING_FEATURE(ModuleSelector)

static bool usesFeatureNonescapableTypes(Decl *decl) {
auto containsNonEscapable =
[](SmallVectorImpl<InverseRequirement> &inverseReqs) {
Expand Down
24 changes: 18 additions & 6 deletions lib/AST/Identifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ void swift::simple_display(llvm::raw_ostream &out, DeclName name) {
}

raw_ostream &llvm::operator<<(raw_ostream &OS, DeclNameRef I) {
if (I.hasModuleSelector())
OS << I.getModuleSelector() << "::";
OS << I.getFullName();
return OS;
}
Expand Down Expand Up @@ -208,17 +210,27 @@ void DeclNameRef::dump() const {
}

StringRef DeclNameRef::getString(llvm::SmallVectorImpl<char> &scratch,
bool skipEmptyArgumentNames) const {
return FullName.getString(scratch, skipEmptyArgumentNames);
bool skipEmptyArgumentNames) const {
{
llvm::raw_svector_ostream out(scratch);
print(out, skipEmptyArgumentNames);
}

return StringRef(scratch.data(), scratch.size());
}

llvm::raw_ostream &DeclNameRef::print(llvm::raw_ostream &os,
bool skipEmptyArgumentNames) const {
return FullName.print(os, skipEmptyArgumentNames);
llvm::raw_ostream &
DeclNameRef::print(llvm::raw_ostream &os,
bool skipEmptyArgumentNames) const {
if (hasModuleSelector())
os << getModuleSelector() << "::";
return getFullName().print(os, skipEmptyArgumentNames);
}

llvm::raw_ostream &DeclNameRef::printPretty(llvm::raw_ostream &os) const {
return FullName.printPretty(os);
if (hasModuleSelector())
os << getModuleSelector() << "::";
return getFullName().printPretty(os);
}

ObjCSelector::ObjCSelector(ASTContext &ctx, unsigned numArgs,
Expand Down
5 changes: 3 additions & 2 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4449,8 +4449,9 @@ bool MissingMemberFailure::diagnoseAsError() {
auto &cs = getConstraintSystem();

auto result = cs.performMemberLookup(
ConstraintKind::ValueMember, getName().withoutArgumentLabels(),
metatypeTy, FunctionRefInfo::doubleBaseNameApply(), getLocator(),
ConstraintKind::ValueMember,
getName().withoutArgumentLabels(getASTContext()), metatypeTy,
FunctionRefInfo::doubleBaseNameApply(), getLocator(),
/*includeInaccessibleMembers=*/true);

// If there are no `init` members at all produce a tailored
Expand Down
5 changes: 3 additions & 2 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8389,9 +8389,10 @@ ValueDecl *RenamedDeclRequest::evaluate(Evaluator &evaluator,
if (attr->Rename.empty())
return nullptr;

auto &ctx = attached->getASTContext();
auto attachedContext = attached->getDeclContext();
auto parsedName = parseDeclName(attr->Rename);
auto nameRef = parsedName.formDeclNameRef(attached->getASTContext());
auto nameRef = parsedName.formDeclNameRef(ctx);

// Handle types separately
if (isa<NominalTypeDecl>(attached)) {
Expand All @@ -8400,7 +8401,7 @@ ValueDecl *RenamedDeclRequest::evaluate(Evaluator &evaluator,

SmallVector<ValueDecl *, 1> lookupResults;
attachedContext->lookupQualified(attachedContext->getParentModule(),
nameRef.withoutArgumentLabels(),
nameRef.withoutArgumentLabels(ctx),
attr->getLocation(), NL_OnlyTypes,
lookupResults);
if (lookupResults.size() == 1)
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ static EnumElementDecl *
lookupUnqualifiedEnumMemberElement(DeclContext *DC, DeclNameRef name,
SourceLoc UseLoc) {
// FIXME: We should probably pay attention to argument labels someday.
name = name.withoutArgumentLabels();
name = name.withoutArgumentLabels(DC->getASTContext());

auto lookup =
TypeChecker::lookupUnqualified(DC, name, UseLoc,
Expand All @@ -153,7 +153,7 @@ static LookupResult lookupMembers(DeclContext *DC, Type ty, DeclNameRef name,
return LookupResult();

// FIXME: We should probably pay attention to argument labels someday.
name = name.withoutArgumentLabels();
name = name.withoutArgumentLabels(DC->getASTContext());

// Look up the case inside the enum.
// FIXME: We should be able to tell if this is a private lookup.
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,7 @@ swift::lookupValueWitnesses(DeclContext *DC, ValueDecl *req, bool *ignoringNames
lookupValueWitnessesViaImplementsAttr(DC, req, witnesses);

auto reqName = req->createNameRef();
auto reqBaseName = reqName.withoutArgumentLabels();
auto reqBaseName = reqName.withoutArgumentLabels(DC->getASTContext());

// An operator function is the only kind of witness that requires global
// lookup. However, because global lookup doesn't enter local contexts,
Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/DeclTypeRecordNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ OTHER(DERIVATIVE_FUNCTION_CONFIGURATION, 154)

OTHER(ERROR_FLAG, 155)
OTHER(ABI_ONLY_COUNTERPART, 156)
OTHER(DECL_NAME_REF, 157)

TRAILING_INFO(CONDITIONAL_SUBSTITUTION)
TRAILING_INFO(CONDITIONAL_SUBSTITUTION_COND)

OTHER(LIFETIME_DEPENDENCE, 160)

TRAILING_INFO(INHERITED_PROTOCOLS)

#ifndef DECL_ATTR
Expand Down
Loading