Skip to content

[cxx-interop] Remove symbolic import mode #81257

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 1 commit into from
May 5, 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
5 changes: 0 additions & 5 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,6 @@ EXPERIMENTAL_FEATURE(ParserASTGen, false)
/// corresponding syntax tree.
EXPERIMENTAL_FEATURE(BuiltinMacros, false)

/// Import C++ class templates as semantically-meaningless symbolic
/// Swift types and C++ methods as symbolic functions with blank
/// signatures.
EXPERIMENTAL_FEATURE(ImportSymbolicCXXDecls, false)

/// Generate bindings for functions that 'throw' in the C++ section of the generated Clang header.
EXPERIMENTAL_FEATURE(GenerateBindingsForThrowingFunctionsInCXX, false)

Expand Down
6 changes: 0 additions & 6 deletions include/swift/ClangImporter/ClangImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -690,12 +690,6 @@ class ClangImporter final : public ClangModuleLoader {
/// of the provided baseType.
void diagnoseMemberValue(const DeclName &name, const Type &baseType) override;

/// Enable the symbolic import experimental feature for the given callback.
void withSymbolicFeatureEnabled(llvm::function_ref<void(void)> callback);

/// Returns true when the symbolic import experimental feature is enabled.
bool isSymbolicImportEnabled() const;

const clang::TypedefType *getTypeDefForCXXCFOptionsDefinition(
const clang::Decl *candidateDecl) override;

Expand Down
1 change: 0 additions & 1 deletion lib/AST/FeatureSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ UNINTERESTING_FEATURE(UnqualifiedLookupValidation)
UNINTERESTING_FEATURE(ImplicitSome)
UNINTERESTING_FEATURE(ParserASTGen)
UNINTERESTING_FEATURE(BuiltinMacros)
UNINTERESTING_FEATURE(ImportSymbolicCXXDecls)
UNINTERESTING_FEATURE(GenerateBindingsForThrowingFunctionsInCXX)
UNINTERESTING_FEATURE(ReferenceBindings)
UNINTERESTING_FEATURE(BuiltinModule)
Expand Down
19 changes: 0 additions & 19 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2726,8 +2726,6 @@ ClangImporter::Implementation::Implementation(
DisableOverlayModules(ctx.ClangImporterOpts.DisableOverlayModules),
EnableClangSPI(ctx.ClangImporterOpts.EnableClangSPI),
UseClangIncludeTree(ctx.ClangImporterOpts.UseClangIncludeTree),
importSymbolicCXXDecls(
ctx.LangOpts.hasFeature(Feature::ImportSymbolicCXXDecls)),
IsReadingBridgingPCH(false),
CurrentVersion(ImportNameVersion::fromOptions(ctx.LangOpts)),
Walker(DiagnosticWalker(*this)), BuffersForDiagnostics(ctx.SourceMgr),
Expand Down Expand Up @@ -8569,23 +8567,6 @@ bool ClangDeclExplicitSafety::isCached() const {
return isa<clang::RecordDecl>(std::get<0>(getStorage()).decl);
}

void ClangImporter::withSymbolicFeatureEnabled(
llvm::function_ref<void(void)> callback) {
llvm::SaveAndRestore<bool> oldImportSymbolicCXXDecls(
Impl.importSymbolicCXXDecls, true);
Impl.nameImporter->enableSymbolicImportFeature(true);
auto importedDeclsCopy = Impl.ImportedDecls;
Impl.ImportedDecls.clear();
callback();
Impl.ImportedDecls = std::move(importedDeclsCopy);
Impl.nameImporter->enableSymbolicImportFeature(
oldImportSymbolicCXXDecls.get());
}

bool ClangImporter::isSymbolicImportEnabled() const {
return Impl.importSymbolicCXXDecls;
}

const clang::TypedefType *ClangImporter::getTypeDefForCXXCFOptionsDefinition(
const clang::Decl *candidateDecl) {

Expand Down
75 changes: 14 additions & 61 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2141,8 +2141,7 @@ namespace {
}

// TODO(https://github.com/apple/swift/issues/56206): Fix this once we support dependent types.
if (decl->getTypeForDecl()->isDependentType() &&
!Impl.importSymbolicCXXDecls) {
if (decl->getTypeForDecl()->isDependentType()) {
Impl.addImportDiagnostic(
decl, Diagnostic(
diag::record_is_dependent,
Expand Down Expand Up @@ -3068,11 +3067,8 @@ namespace {
auto semanticsKind = evaluateOrDefault(
Impl.SwiftContext.evaluator,
CxxRecordSemantics({decl, Impl.SwiftContext, &Impl}), {});
if ((semanticsKind == CxxRecordSemanticsKind::MissingLifetimeOperation ||
semanticsKind == CxxRecordSemanticsKind::UnavailableConstructors) &&
// Let un-specialized class templates through. We'll sort out their
// members once they're instantiated.
!Impl.importSymbolicCXXDecls) {
if (semanticsKind == CxxRecordSemanticsKind::MissingLifetimeOperation ||
semanticsKind == CxxRecordSemanticsKind::UnavailableConstructors) {

HeaderLoc loc(decl->getLocation());
if (hasUnsafeAPIAttr(decl))
Expand Down Expand Up @@ -3267,12 +3263,6 @@ namespace {

Decl *VisitClassTemplateSpecializationDecl(
const clang::ClassTemplateSpecializationDecl *decl) {
// Treat a specific specialization like the unspecialized class template
// when importing it in symbolic mode.
if (Impl.importSymbolicCXXDecls)
return Impl.importDecl(decl->getSpecializedTemplate(),
Impl.CurrentVersion);

bool isPair = decl->getSpecializedTemplate()->isInStdNamespace() &&
decl->getSpecializedTemplate()->getName() == "pair";

Expand Down Expand Up @@ -3953,8 +3943,6 @@ namespace {
Impl.SwiftContext, SourceLoc(), templateParams, SourceLoc());
}

bool importFuncWithoutSignature =
isa<clang::CXXMethodDecl>(decl) && Impl.importSymbolicCXXDecls;
if (!dc->isModuleScopeContext() && !isClangNamespace(dc) &&
!isa<clang::CXXMethodDecl>(decl)) {
// Handle initializers.
Expand Down Expand Up @@ -4021,39 +4009,12 @@ namespace {
importedType =
Impl.importFunctionReturnType(dc, decl, allowNSUIntegerAsInt);
} else {
if (importFuncWithoutSignature) {
importedType = ImportedType{Impl.SwiftContext.getVoidType(), false};
if (decl->param_empty())
bodyParams = ParameterList::createEmpty(Impl.SwiftContext);
else {
llvm::SmallVector<ParamDecl *, 4> params;
for (const auto &param : decl->parameters()) {

Identifier bodyName =
Impl.importFullName(param, Impl.CurrentVersion)
.getBaseIdentifier(Impl.SwiftContext);
auto paramInfo = Impl.createDeclWithClangNode<ParamDecl>(
param, AccessLevel::Private, SourceLoc(), SourceLoc(),
Identifier(), Impl.importSourceLoc(param->getLocation()),
bodyName, Impl.ImportedHeaderUnit);
paramInfo->setSpecifier(ParamSpecifier::Default);
paramInfo->setInterfaceType(Impl.SwiftContext.TheAnyType);
if (param->hasDefaultArg()) {
paramInfo->setDefaultArgumentKind(DefaultArgumentKind::Normal);
paramInfo->setDefaultValueStringRepresentation("cxxDefaultArg");
}
params.push_back(paramInfo);
}
bodyParams = ParameterList::create(Impl.SwiftContext, params);
}
} else {
// Import the function type. If we have parameters, make sure their
// names get into the resulting function type.
importedType = Impl.importFunctionParamsAndReturnType(
dc, decl, {decl->param_begin(), decl->param_size()},
decl->isVariadic(), isInSystemModule(dc), name, bodyParams,
templateParams);
}
// Import the function type. If we have parameters, make sure their
// names get into the resulting function type.
importedType = Impl.importFunctionParamsAndReturnType(
dc, decl, {decl->param_begin(), decl->param_size()},
decl->isVariadic(), isInSystemModule(dc), name, bodyParams,
templateParams);

if (auto *mdecl = dyn_cast<clang::CXXMethodDecl>(decl)) {
if (mdecl->isStatic()) {
Expand Down Expand Up @@ -4160,12 +4121,10 @@ namespace {
}
func->setAccess(importer::convertClangAccess(decl->getAccess()));

if (!importFuncWithoutSignature) {
bool success = processSpecialImportedFunc(
func, importedName, decl->getOverloadedOperator());
if (!success)
return nullptr;
}
bool success = processSpecialImportedFunc(
func, importedName, decl->getOverloadedOperator());
if (!success)
return nullptr;
}

result->setIsObjC(false);
Expand Down Expand Up @@ -4480,8 +4439,7 @@ namespace {
}

Decl *VisitFieldDecl(const clang::FieldDecl *decl) {
if (!Impl.importSymbolicCXXDecls &&
decl->hasAttr<clang::NoUniqueAddressAttr>()) {
if (decl->hasAttr<clang::NoUniqueAddressAttr>()) {
if (const auto *rd = decl->getType()->getAsRecordDecl()) {
// Clang can store the next field in the padding of this one. Swift
// does not support this yet so let's not import the field and
Expand Down Expand Up @@ -4743,11 +4701,6 @@ namespace {
if (name.empty())
return nullptr;

if (Impl.importSymbolicCXXDecls)
// Import an unspecialized C++ class template as a Swift value/class
// type in symbolic mode.
return Impl.importDecl(decl->getTemplatedDecl(), Impl.CurrentVersion);

auto loc = Impl.importSourceLoc(decl->getLocation());
auto dc = Impl.importDeclContextOf(
decl, importedName.getEffectiveContext());
Expand Down
5 changes: 0 additions & 5 deletions lib/ClangImporter/ImportName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2274,11 +2274,6 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,

if (auto classTemplateSpecDecl =
dyn_cast<clang::ClassTemplateSpecializationDecl>(D)) {
/// Symbolic specializations get imported as the symbolic class template
/// type.
if (importSymbolicCXXDecls)
return importNameImpl(classTemplateSpecDecl->getSpecializedTemplate(),
version, givenName);
if (!isa<clang::ClassTemplatePartialSpecializationDecl>(D)) {
auto name = printClassTemplateSpecializationName(classTemplateSpecDecl,
swiftCtx, this, version);
Expand Down
8 changes: 0 additions & 8 deletions lib/ClangImporter/ImportName.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,17 +423,13 @@ class NameImporter {
llvm::DenseMap<std::pair<const clang::ObjCInterfaceDecl *, char>,
std::unique_ptr<InheritedNameSet>> allProperties;

bool importSymbolicCXXDecls;

ClangImporter::Implementation *importerImpl;

public:
NameImporter(ASTContext &ctx, const PlatformAvailability &avail,
clang::Sema &cSema, ClangImporter::Implementation *importerImpl)
: swiftCtx(ctx), availability(avail), clangSema(cSema),
enumInfos(clangSema.getPreprocessor()),
importSymbolicCXXDecls(
ctx.LangOpts.hasFeature(Feature::ImportSymbolicCXXDecls)),
importerImpl(importerImpl) {}

/// Determine the Swift name for a Clang decl
Expand Down Expand Up @@ -498,10 +494,6 @@ class NameImporter {
clang::ObjCInterfaceDecl *classDecl,
bool forInstance);

inline void enableSymbolicImportFeature(bool isEnabled) {
importSymbolicCXXDecls = isEnabled;
}

/// Retrieve a purported custom name even if it is invalid.
static std::optional<StringRef>
findCustomName(const clang::Decl *decl, ImportNameVersion version);
Expand Down
8 changes: 0 additions & 8 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ bool ClangImporter::Implementation::isOverAligned(const clang::TypeDecl *decl) {
}

bool ClangImporter::Implementation::isOverAligned(clang::QualType type) {
// Do not check type layout for a clang type in symbolic mode as the
// type could be a dependent type.
if (importSymbolicCXXDecls)
return false;
auto align = getClangASTContext().getTypeAlignInChars(type);
return align > clang::CharUnits::fromQuantity(MaximumAlignment);
}
Expand Down Expand Up @@ -2348,10 +2344,6 @@ findGenericTypeInGenericDecls(ClangImporter::Implementation &impl,
llvm::find_if(genericParams, [name](GenericTypeParamDecl *generic) {
return generic->getName().str() == name;
});
// We sometimes are unable compute the exact Swift type
// of symbolic declarations. Fallback to using `Any` in that case.
if (impl.importSymbolicCXXDecls && genericParamIter == genericParams.end())
return impl.SwiftContext.TheAnyType;
// TODO: once we support generics in class types, replace this with
// "return nullptr". Once support for template classes, this will need to
// be updated, though. I'm leaving the assert here to make it easier to
Expand Down
1 change: 0 additions & 1 deletion lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
const bool DisableOverlayModules;
const bool EnableClangSPI;
const bool UseClangIncludeTree;
bool importSymbolicCXXDecls;

bool IsReadingBridgingPCH;
llvm::SmallVector<clang::serialization::SubmoduleID, 2> PCHImportedSubmodules;
Expand Down
5 changes: 0 additions & 5 deletions lib/ClangImporter/SwiftDeclSynthesizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2020,11 +2020,6 @@ clang::CXXMethodDecl *SwiftDeclSynthesizer::synthesizeCXXForwardingMethod(
assert(!method->isStatic() ||
method->getNameInfo().getName().getCXXOverloadedOperator() ==
clang::OO_Call);
// When emitting symbolic decls, the method might not have a concrete
// record type as this type.
if (ImporterImpl.importSymbolicCXXDecls && !method->isStatic() &&
!method->getThisType()->getPointeeCXXRecordDecl())
return nullptr;

// Create a new method in the derived class that calls the base method.
clang::DeclarationName name = method->getNameInfo().getName();
Expand Down
24 changes: 0 additions & 24 deletions lib/IDE/ModuleInterfacePrinting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,27 +836,3 @@ void swift::ide::printHeaderInterface(
}
Printer.forceNewlines();
}

void swift::ide::printSymbolicSwiftClangModuleInterface(
ModuleDecl *M, ASTPrinter &Printer, const clang::Module *clangModule) {
std::string headerComment;
llvm::raw_string_ostream(headerComment)
<< "// Swift interface for " << (clangModule->IsSystem ? "system " : "")
<< "module '" << clangModule->Name << "'\n";
Printer.printText(headerComment);

ModuleTraversalOptions opts;
opts |= ModuleTraversal::VisitSubmodules;
auto popts =
PrintOptions::printModuleInterface(/*printFullConvention=*/false);
popts.PrintDocumentationComments = false;
popts.SkipInlineCXXNamespace = true;

auto &SwiftContext = M->getTopLevelModule()->getASTContext();
auto &Importer =
static_cast<ClangImporter &>(*SwiftContext.getClangModuleLoader());
Importer.withSymbolicFeatureEnabled([&]() {
printModuleInterface(M, {}, opts, Printer, popts,
/*SynthesizeExtensions=*/false);
});
}
Loading