Skip to content

[Macros] Mangle attached macros for accessors #65559

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
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
37 changes: 36 additions & 1 deletion lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4062,7 +4062,42 @@ std::string ASTMangler::mangleAttachedMacroExpansion(
// dependencies.
const Decl *attachedTo = decl;
DeclBaseName attachedToName;
if (auto valueDecl = dyn_cast<ValueDecl>(decl)) {
if (auto accessor = dyn_cast<AccessorDecl>(decl)) {
auto storage = accessor->getStorage();
appendContextOf(storage);

// Introduce an identifier mangling that includes var/subscript, accessor
// kind, and static.
// FIXME: THIS IS A HACK. We need something different.
{
llvm::SmallString<16> name;
{
llvm::raw_svector_ostream out(name);
out << storage->getName().getBaseName().userFacingName()
<< "__";
if (isa<VarDecl>(storage)) {
out << "v";
} else {
assert(isa<SubscriptDecl>(storage));
out << "i";
}

out << getCodeForAccessorKind(accessor->getAccessorKind());
if (storage->isStatic())
out << "Z";
}

attachedToName = decl->getASTContext().getIdentifier(name);
}

appendDeclName(storage, attachedToName);

// For member attribute macros, the attribute is attached to the enclosing
// declaration.
if (role == MacroRole::MemberAttribute) {
attachedTo = storage->getDeclContext()->getAsDecl();
}
} else if (auto valueDecl = dyn_cast<ValueDecl>(decl)) {
appendContextOf(valueDecl);

// Mangle the name, replacing special names with their user-facing names.
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10518,6 +10518,9 @@ DeclContext *
MacroDiscriminatorContext::getInnermostMacroContext(DeclContext *dc) {
switch (dc->getContextKind()) {
case DeclContextKind::SubscriptDecl:
// For a subscript, return its parent context.
return getInnermostMacroContext(dc->getParent());

case DeclContextKind::EnumElementDecl:
case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::SerializedLocal:
Expand Down
9 changes: 0 additions & 9 deletions lib/Sema/TypeCheckStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3319,15 +3319,6 @@ static void finishNSManagedImplInfo(VarDecl *var,
}
}

static Expr *getParentExecutableInitializer(VarDecl *var) {
if (auto *PBD = var->getParentPatternBinding()) {
const auto i = PBD->getPatternEntryIndexForVarDecl(var);
return PBD->getExecutableInit(i);
}

return nullptr;
}

static void finishStorageImplInfo(AbstractStorageDecl *storage,
StorageImplInfo &info) {
auto dc = storage->getDeclContext();
Expand Down
12 changes: 12 additions & 0 deletions test/Macros/macro_expand_attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,15 @@ struct OldStorage {
// The deprecation warning below comes from the deprecation attribute
// introduced by @wrapStoredProperties on OldStorage.
_ = OldStorage(x: 5).x // expected-warning{{'x' is deprecated: hands off my data}}

@wrapStoredProperties(#"available(*, deprecated, message: "hands off my data")"#)
class C2: P {
var x: Int = 0
var y: Int = 0

var squareOfLength: Int {
return x*x + y*y // expected-warning 4{{hands off my data}}
}

var blah: Int { squareOfLength }
}