Skip to content

Commit 50f5221

Browse files
authored
[NFC] promote getOperatorName() to a non-static function (#80466)
1 parent 06937d8 commit 50f5221

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

lib/ClangImporter/ClangDerivedConformances.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "ClangDerivedConformances.h"
14+
#include "ImporterImpl.h"
1415
#include "swift/AST/ConformanceLookup.h"
1516
#include "swift/AST/ParameterList.h"
1617
#include "swift/AST/PrettyStackTrace.h"
@@ -33,8 +34,7 @@ lookupDirectWithoutExtensions(NominalTypeDecl *decl, Identifier id) {
3334
TinyPtrVector<ValueDecl *> result;
3435

3536
if (id.isOperator()) {
36-
auto underlyingId =
37-
ctx.getIdentifier(getPrivateOperatorName(std::string(id)));
37+
auto underlyingId = getOperatorName(ctx, id);
3838
TinyPtrVector<ValueDecl *> underlyingFuncs = evaluateOrDefault(
3939
ctx.evaluator, ClangRecordMemberLookup({decl, underlyingId}), {});
4040
for (auto it : underlyingFuncs) {

lib/ClangImporter/ClangImporter.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4748,9 +4748,8 @@ bool ClangImporter::Implementation::lookupValue(SwiftLookupTable &table,
47484748
// If CXXInterop is enabled we need to check the modified operator name as
47494749
// well
47504750
if (SwiftContext.LangOpts.EnableCXXInterop) {
4751-
auto funcBaseName =
4752-
DeclBaseName(SwiftContext.getIdentifier(getPrivateOperatorName(
4753-
name.getBaseName().getIdentifier().str().str())));
4751+
auto funcBaseName = DeclBaseName(
4752+
getOperatorName(SwiftContext, name.getBaseName().getIdentifier()));
47544753
for (auto entry : table.lookupMemberOperators(funcBaseName)) {
47554754
if (isVisibleClangEntry(entry)) {
47564755
if (auto func = dyn_cast_or_null<FuncDecl>(

lib/ClangImporter/ImportName.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,26 @@ using namespace importer;
6262
using clang::CompilerInstance;
6363
using clang::CompilerInvocation;
6464

65-
static const char *getOperatorName(clang::OverloadedOperatorKind Operator) {
66-
switch (Operator) {
65+
Identifier importer::getOperatorName(ASTContext &ctx,
66+
clang::OverloadedOperatorKind op) {
67+
switch (op) {
6768
case clang::OO_None:
6869
case clang::NUM_OVERLOADED_OPERATORS:
69-
return nullptr;
70-
70+
return Identifier{};
7171
#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
7272
case clang::OO_##Name: \
73-
return #Name;
73+
return ctx.getIdentifier("__operator" #Name);
7474
#include "clang/Basic/OperatorKinds.def"
7575
}
76+
}
77+
78+
Identifier importer::getOperatorName(ASTContext &ctx, Identifier op) {
79+
#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
80+
if (op.str() == Spelling) \
81+
return ctx.getIdentifier("__operator" #Name);
82+
#include "clang/Basic/OperatorKinds.def"
7683

77-
llvm_unreachable("Invalid OverloadedOperatorKind!");
84+
return Identifier{};
7885
}
7986

8087
/// Determine whether the given Clang selector matches the given
@@ -1960,10 +1967,11 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
19601967
case clang::OverloadedOperatorKind::OO_GreaterEqual:
19611968
case clang::OverloadedOperatorKind::OO_AmpAmp:
19621969
case clang::OverloadedOperatorKind::OO_PipePipe: {
1963-
auto operatorName = isa<clang::CXXMethodDecl>(functionDecl)
1964-
? "__operator" + std::string{getOperatorName(op)}
1965-
: clang::getOperatorSpelling(op);
1966-
baseName = swiftCtx.getIdentifier(operatorName).str();
1970+
auto operatorName =
1971+
isa<clang::CXXMethodDecl>(functionDecl)
1972+
? getOperatorName(swiftCtx, op)
1973+
: swiftCtx.getIdentifier(clang::getOperatorSpelling(op));
1974+
baseName = operatorName.str();
19671975
isFunction = true;
19681976
addDefaultArgNamesForClangFunction(functionDecl, argumentNames);
19691977
if (auto cxxMethod = dyn_cast<clang::CXXMethodDecl>(functionDecl)) {

lib/ClangImporter/ImporterImpl.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,14 +2059,19 @@ findAnonymousEnumForTypedef(const ASTContext &ctx,
20592059
return std::nullopt;
20602060
}
20612061

2062-
inline std::string getPrivateOperatorName(const std::string &OperatorToken) {
2063-
#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
2064-
if (OperatorToken == Spelling) { \
2065-
return "__operator" #Name; \
2066-
};
2067-
#include "clang/Basic/OperatorKinds.def"
2068-
return "None";
2069-
}
2062+
/// Construct the imported Swift name for an imported Clang operator kind,
2063+
/// e.g., \c "__operatorPlus" for Clang::OO_Plus.
2064+
///
2065+
/// Returns an empty identifier (internally, a nullptr) when \a op does not
2066+
/// represent an actual operator, i.e., OO_None or NUM_OVERLOADED_OPERATORS.
2067+
Identifier getOperatorName(ASTContext &ctx, clang::OverloadedOperatorKind op);
2068+
2069+
/// Construct the imported Swift name corresponding to an operator identifier,
2070+
/// e.g., \c "__operatorPlus" for \c "+".
2071+
///
2072+
/// Returns an empty identifier (internally, a nullptr) when \a op does not
2073+
/// correspond to an overloaded C++ operator.
2074+
Identifier getOperatorName(ASTContext &ctx, Identifier op);
20702075

20712076
bool hasOwnedValueAttr(const clang::RecordDecl *decl);
20722077
bool hasUnsafeAPIAttr(const clang::Decl *decl);

0 commit comments

Comments
 (0)