Skip to content

[5.10][cxx-interop] code-complete namespace members #69753

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
Nov 9, 2023
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
71 changes: 71 additions & 0 deletions lib/Sema/LookupVisibleDecls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "TypeChecker.h"
#include "clang/AST/DeclObjC.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ClangModuleLoader.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/ImportCache.h"
#include "swift/AST/Initializer.h"
Expand All @@ -29,8 +30,11 @@
#include "swift/AST/SourceFile.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/STLExtras.h"
#include "swift/ClangImporter/ClangImporterRequests.h"
#include "swift/Sema/IDETypeCheckingRequests.h"
#include "swift/Sema/IDETypeChecking.h"
#include "clang/Basic/Module.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/SetVector.h"
#include <set>

Expand Down Expand Up @@ -558,6 +562,63 @@ static void
getReasonForSuper(Reason), Sig, Visited);
}

static void lookupVisibleCxxNamespaceMemberDecls(
EnumDecl *swiftDecl, const clang::NamespaceDecl *clangNamespace,
VisibleDeclConsumer &Consumer, VisitedSet &Visited) {
if (!Visited.insert(swiftDecl).second)
return;
auto &ctx = swiftDecl->getASTContext();
auto namespaceDecl = clangNamespace;

// This is only to keep track of the members we've already seen.
llvm::SmallPtrSet<Decl *, 16> addedMembers;
for (auto redecl : namespaceDecl->redecls()) {
for (auto member : redecl->decls()) {
auto lookupAndAddMembers = [&](DeclName name) {
auto allResults = evaluateOrDefault(
ctx.evaluator, ClangDirectLookupRequest({swiftDecl, redecl, name}),
{});

for (auto found : allResults) {
auto clangMember = found.get<clang::NamedDecl *>();
if (auto importedDecl =
ctx.getClangModuleLoader()->importDeclDirectly(
cast<clang::NamedDecl>(clangMember))) {
if (addedMembers.insert(importedDecl).second) {
if (importedDecl->getDeclContext()->getAsDecl() != swiftDecl) {
return;
}
Consumer.foundDecl(cast<ValueDecl>(importedDecl),
DeclVisibilityKind::MemberOfCurrentNominal);
}
}
}
};

auto namedDecl = dyn_cast<clang::NamedDecl>(member);
if (!namedDecl)
continue;
auto name = ctx.getClangModuleLoader()->importName(namedDecl);
if (!name)
continue;
lookupAndAddMembers(name);

// Unscoped enums could have their enumerators present
// in the parent namespace.
if (auto *ed = dyn_cast<clang::EnumDecl>(member)) {
if (!ed->isScoped()) {
for (const auto *ecd : ed->enumerators()) {
auto name = ctx.getClangModuleLoader()->importName(ecd);
if (!name)
continue;
lookupAndAddMembers(name);
}
}
}
}
}
}

static void lookupVisibleMemberDeclsImpl(
Type BaseTy, VisibleDeclConsumer &Consumer, const DeclContext *CurrDC,
LookupState LS, DeclVisibilityKind Reason, GenericSignature Sig,
Expand Down Expand Up @@ -647,6 +708,16 @@ static void lookupVisibleMemberDeclsImpl(
return;
}

// Lookup members of C++ namespace without looking type members, as
// C++ namespace uses lazy lookup.
if (auto *ET = BaseTy->getAs<EnumType>()) {
if (auto *clangNamespace = dyn_cast_or_null<clang::NamespaceDecl>(
ET->getDecl()->getClangDecl())) {
lookupVisibleCxxNamespaceMemberDecls(ET->getDecl(), clangNamespace,
Consumer, Visited);
}
}

// If we're looking into a type parameter and we have a GenericSignature,
// query the signature to resolve where we should look.
if (BaseTy->isTypeParameter() && Sig) {
Expand Down
14 changes: 14 additions & 0 deletions test/Interop/Cxx/namespace/Inputs/enums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef TEST_INTEROP_CXX_NAMESPACE_INPUTS_ENUMS_H
#define TEST_INTEROP_CXX_NAMESPACE_INPUTS_ENUMS_H

namespace EnumNS1 {

enum AnEnum {
one,
two,
three
};

} // namespace EnumNS1

#endif // TEST_INTEROP_CXX_NAMESPACE_INPUTS_ENUMS_H
5 changes: 5 additions & 0 deletions test/Interop/Cxx/namespace/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ module Extensions {
header "extensions.h"
requires cplusplus
}

module Enums {
header "enums.h"
requires cplusplus
}
59 changes: 59 additions & 0 deletions test/Interop/Cxx/namespace/namespace-completion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// RUN: %target-swift-ide-test -code-completion -enable-experimental-cxx-interop -source-filename %s -code-completion-token=NS1 -I %S/Inputs | %FileCheck %s -check-prefix=CHECK-NS1
// RUN: %target-swift-ide-test -code-completion -enable-experimental-cxx-interop -source-filename %s -code-completion-token=NS2 -I %S/Inputs | %FileCheck %s -check-prefix=CHECK-NS2
// RUN: %target-swift-ide-test -code-completion -enable-experimental-cxx-interop -source-filename %s -code-completion-token=TemplatesNS1 -I %S/Inputs | %FileCheck %s -check-prefix=CHECK-TNS1
// RUN: %target-swift-ide-test -code-completion -enable-experimental-cxx-interop -source-filename %s -code-completion-token=EnumsNS1 -I %S/Inputs | %FileCheck %s -check-prefix=CHECK-ENS1

import Submodules
import Templates
import Enums

func ns1() {
NS1.#^NS1^#
}
// CHECK-NS1: Begin completions, 5 items
// CHECK-NS1-NEXT: Keyword[self]/CurrNominal: self[#NS1.Type#]; name=self
// CHECK-NS1-NEXT: Keyword/CurrNominal: Type[#NS1.Type#]; name=Type
// CHECK-NS1-NEXT: Decl[Enum]/CurrNominal: NS2[#NS1.NS2#]; name=NS2
// CHECK-NS1-NEXT: Decl[Struct]/CurrNominal: BasicB[#NS1.BasicB#]; name=BasicB
// CHECK-NS1-NEXT: Decl[Struct]/CurrNominal: BasicA[#NS1.BasicA#]; name=BasicA
// CHECK-NS1-NEXT: End completions

func ns2() {
NS1.NS2.#^NS2^#
}

// CHECK-NS2: Begin completions, 4 items
// CHECK-NS2-NEXT: Keyword[self]/CurrNominal: self[#NS1.NS2.Type#]; name=self
// CHECK-NS2-NEXT: Keyword/CurrNominal: Type[#NS1.NS2.Type#]; name=Type
// CHECK-NS2-NEXT: Decl[Struct]/CurrNominal: BasicDeepB[#NS1.NS2.BasicDeepB#]; name=BasicDeepB
// CHECK-NS2-NEXT: Decl[Struct]/CurrNominal: BasicDeepA[#NS1.NS2.BasicDeepA#]; name=BasicDeepA
// CHECK-NS2-NEXT: End completions

func nsTemplates() {
TemplatesNS1.#^TemplatesNS1^#
}

// CHECK-TNS1: Begin completions, 10 items
// CHECK-TNS1-NEXT: Keyword[self]/CurrNominal: self[#TemplatesNS1.Type#]; name=self
// CHECK-TNS1-NEXT: Keyword/CurrNominal: Type[#TemplatesNS1.Type#]; name=Type
// CHECK-TNS1-NEXT: Decl[StaticMethod]/CurrNominal: basicFunctionTemplateDefinedInDefs({#T#})[#UnsafePointer<CChar>!#]; name=basicFunctionTemplateDefinedInDefs()
// CHECK-TNS1-NEXT: Decl[TypeAlias]/CurrNominal: UseTemplate[#TemplatesNS4.HasSpecialization<CChar>#]; name=UseTemplate
// CHECK-TNS1-NEXT: Decl[TypeAlias]/CurrNominal: UseSpecialized[#TemplatesNS4.HasSpecialization<CInt>#]; name=UseSpecialized
// CHECK-TNS1-NEXT: Decl[Enum]/CurrNominal: TemplatesNS2[#TemplatesNS1.TemplatesNS2#]; name=TemplatesNS2
// CHECK-TNS1-NEXT: Decl[Enum]/CurrNominal: TemplatesNS3[#TemplatesNS1.TemplatesNS3#]; name=TemplatesNS3
// CHECK-TNS1-NEXT: Decl[TypeAlias]/CurrNominal: ForwardDeclaredClassTemplateChar[#TemplatesNS1.TemplatesNS2.ForwardDeclaredClassTemplate<CChar>#]; name=ForwardDeclaredClassTemplateChar
// CHECK-TNS1-NEXT: Decl[StaticMethod]/CurrNominal: basicFunctionTemplate({#T#})[#UnsafePointer<CChar>!#]; name=basicFunctionTemplate()
// CHECK-TNS1-NEXT: Decl[TypeAlias]/CurrNominal: BasicClassTemplateChar[#TemplatesNS1.BasicClassTemplate<CChar>#]; name=BasicClassTemplateChar
// CHECK-TNS1-NEXT: End completions

func nsEnums() {
EnumNS1.#^EnumsNS1^#
}

// CHECK-ENS1: Keyword[self]/CurrNominal: self[#EnumNS1.Type#]; name=self
// CHECK-ENS1-NEXT: Keyword/CurrNominal: Type[#EnumNS1.Type#]; name=Type
// CHECK-ENS1-NEXT: Decl[Struct]/CurrNominal: AnEnum[#EnumNS1.AnEnum#]; name=AnEnum
// CHECK-ENS1-NEXT: Decl[StaticVar]/CurrNominal: one[#EnumNS1.AnEnum#]; name=one
// CHECK-ENS1-NEXT: Decl[StaticVar]/CurrNominal: two[#EnumNS1.AnEnum#]; name=two
// CHECK-ENS1-NEXT: Decl[StaticVar]/CurrNominal: three[#EnumNS1.AnEnum#]; name=three
// CHECK-ENS1-NEXT: End completions