Skip to content

Revert "Merge pull request #81840 from compnerd/aliases" #82207

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

Closed
wants to merge 1 commit into from
Closed
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
109 changes: 1 addition & 108 deletions lib/ClangImporter/ImportMacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticsClangImporter.h"
#include "swift/AST/Expr.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/Types.h"
#include "swift/Basic/Assertions.h"
Expand All @@ -32,7 +31,6 @@
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/DelayedDiagnostic.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Sema.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
#include "llvm/ADT/SmallString.h"
Expand Down Expand Up @@ -373,104 +371,6 @@ getIntegerConstantForMacroToken(ClangImporter::Implementation &impl,
return std::nullopt;
}

namespace {
ValueDecl *importDeclAlias(ClangImporter::Implementation &clang,
swift::DeclContext *DC, const clang::ValueDecl *D,
Identifier alias) {
// Variadic functions cannot be imported into Swift.
// FIXME(compnerd) emit a diagnostic for the missing diagnostic.
if (const auto *FD = dyn_cast<clang::FunctionDecl>(D))
if (FD->isVariadic())
return nullptr;

// Ignore self-referential macros.
if (D->getName() == alias.str())
return nullptr;

swift::ValueDecl *VD =
dyn_cast_or_null<ValueDecl>(clang.importDecl(D, clang.CurrentVersion));
if (VD == nullptr)
return nullptr;

// If the imported decl is named identically, avoid the aliasing.
if (VD->getBaseIdentifier().str() == alias.str())
return nullptr;

swift::ASTContext &Ctx = DC->getASTContext();
ImportedType Ty =
clang.importType(D->getType(), ImportTypeKind::Abstract,
[&clang, &D](Diagnostic &&Diag) {
clang.addImportDiagnostic(D, std::move(Diag),
D->getLocation());
}, /*AllowsNSUIntegerAsInt*/true,
Bridgeability::None, { });
swift::Type GetterTy = FunctionType::get({}, Ty.getType(), ASTExtInfo{});
swift::Type SetterTy =
FunctionType::get({AnyFunctionType::Param(Ty.getType())},
Ctx.TheEmptyTupleType, ASTExtInfo{});

/* Storage */
swift::VarDecl *V =
new (Ctx) VarDecl(/*IsStatic*/false, VarDecl::Introducer::Var,
SourceLoc(), alias, DC);
V->setAccess(swift::AccessLevel::Public);
V->setInterfaceType(Ty.getType());
V->getAttrs().add(new (Ctx) TransparentAttr(/*Implicit*/true));
V->getAttrs().add(new (Ctx) InlineAttr(InlineKind::Always));

/* Accessor */
swift::AccessorDecl *G = nullptr;
{
G = AccessorDecl::createImplicit(Ctx, AccessorKind::Get, V, false, false,
TypeLoc(), GetterTy, DC);
G->setAccess(swift::AccessLevel::Public);
G->setInterfaceType(GetterTy);
G->setIsTransparent(true);
G->setParameters(ParameterList::createEmpty(Ctx));

DeclRefExpr *DRE =
new (Ctx) DeclRefExpr(ConcreteDeclRef(VD), {}, /*Implicit*/true,
AccessSemantics::Ordinary, Ty.getType());
ReturnStmt *RS = ReturnStmt::createImplicit(Ctx, DRE);

G->setBody(BraceStmt::createImplicit(Ctx, {RS}),
AbstractFunctionDecl::BodyKind::TypeChecked);
}

swift::AccessorDecl *S = nullptr;
if (isa<clang::VarDecl>(D) &&
!cast<clang::VarDecl>(D)->getType().isConstQualified()) {
S = AccessorDecl::createImplicit(Ctx, AccessorKind::Set, V, false, false,
TypeLoc(), Ctx.TheEmptyTupleType, DC);
S->setAccess(swift::AccessLevel::Public);
S->setInterfaceType(SetterTy);
S->setIsTransparent(true);
S->setParameters(ParameterList::create(Ctx, {
ParamDecl::createImplicit(Ctx, Identifier(), Ctx.getIdentifier("newValue"),
Ty.getType(), DC)
}));

DeclRefExpr *LHS =
new (Ctx) DeclRefExpr(ConcreteDeclRef(VD), {}, /*Implicit*/true,
AccessSemantics::Ordinary, Ty.getType());
DeclRefExpr *RHS =
new (Ctx) DeclRefExpr(S->getParameters()->get(0), {}, /*Implicit*/true,
AccessSemantics::Ordinary, Ty.getType());
AssignExpr *AE = new (Ctx) AssignExpr(LHS, SourceLoc(), RHS, true);
AE->setType(Ctx.TheEmptyTupleType);
S->setBody(BraceStmt::createImplicit(Ctx, {AE}),
AbstractFunctionDecl::BodyKind::TypeChecked);
}

/* Bind */
V->setImplInfo(S ? StorageImplInfo::getMutableComputed()
: StorageImplInfo::getImmutableComputed());
V->setAccessors(SourceLoc(), S ? ArrayRef{G,S} : ArrayRef{G}, SourceLoc());

return V;
}
}

static ValueDecl *importMacro(ClangImporter::Implementation &impl,
llvm::SmallSet<StringRef, 4> &visitedMacros,
DeclContext *DC, Identifier name,
Expand Down Expand Up @@ -609,14 +509,7 @@ static ValueDecl *importMacro(ClangImporter::Implementation &impl,
}
}

/* Create an alias for any Decl */
clang::Sema &S = impl.getClangSema();
clang::LookupResult R(S, {{tok.getIdentifierInfo()}, {}},
clang::Sema::LookupAnyName);
if (S.LookupName(R, S.TUScope))
if (R.getResultKind() == clang::LookupResult::LookupResultKind::Found)
if (const auto *VD = dyn_cast<clang::ValueDecl>(R.getFoundDecl()))
return importDeclAlias(impl, DC, VD, name);
// FIXME: If the identifier refers to a declaration, alias it?
}

// TODO(https://github.com/apple/swift/issues/57735): Seems rare to have a single token that is neither a literal nor an identifier, but add diagnosis.
Expand Down
2 changes: 2 additions & 0 deletions test/ClangImporter/CoreGraphics_test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ public func testRenames(transform: CGAffineTransform, context: CGContext,
blackHole(point.applying(transform))
var rect = rect.applying(transform)
blackHole(size.applying(transform))
// CHECK: %{{.*}} = {{(tail )?}}call { double, double } @CGPointApplyAffineTransform(double %{{.*}}, double %{{.*}}, ptr {{.*}})
// CHECK: call void @CGRectApplyAffineTransform(ptr {{.*}}, ptr {{.*}}, ptr {{.*}})
// CHECK: %{{.*}} = {{(tail )?}}call { double, double } @CGSizeApplyAffineTransform(double %{{.*}}, double %{{.*}}, ptr {{.*}})

context.concatenate(transform)
context.rotate(by: CGFloat.pi)
Expand Down
53 changes: 0 additions & 53 deletions test/ClangImporter/Inputs/custom-modules/Aliases.h

This file was deleted.

4 changes: 0 additions & 4 deletions test/ClangImporter/Inputs/custom-modules/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,6 @@ module "Weird C Module" {
header "WeirdCModule.h"
}

module Aliases {
header "Aliases.h"
}

module RetroactiveVersioning {
header "versioning.h"
}
20 changes: 0 additions & 20 deletions test/ClangImporter/alias-invalid.swift

This file was deleted.

64 changes: 0 additions & 64 deletions test/ClangImporter/alias.swift

This file was deleted.