Skip to content

Commit 16c1e1d

Browse files
committed
Format
1 parent 65473f5 commit 16c1e1d

File tree

4 files changed

+107
-84
lines changed

4 files changed

+107
-84
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@
7171
#include "llvm/Support/FileSystem.h"
7272
#include "llvm/Support/Memory.h"
7373
#include "llvm/Support/Path.h"
74+
#include "llvm/Support/VirtualFileSystem.h"
7475
#include "llvm/Support/YAMLParser.h"
7576
#include "llvm/Support/YAMLTraits.h"
76-
#include "llvm/Support/VirtualFileSystem.h"
7777
#include <algorithm>
7878
#include <string>
7979
#include <memory>
80+
#include <string>
8081

8182
using namespace swift;
8283
using namespace importer;
@@ -88,15 +89,15 @@ using clang::CompilerInvocation;
8889
#pragma mark Internal data structures
8990

9091
namespace {
91-
static std::string getOperatorNameForToken(std::string OperatorToken) {
92-
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
93-
if(OperatorToken == Spelling) { \
94-
return #Name; \
95-
};
92+
static std::string getOperatorNameForToken(std::string OperatorToken) {
93+
#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
94+
if (OperatorToken == Spelling) { \
95+
return #Name; \
96+
};
9697
#include "clang/Basic/OperatorKinds.def"
97-
return "None";
98-
}
98+
return "None";
9999
}
100+
} // namespace
100101

101102
namespace {
102103
class HeaderImportCallbacks : public clang::PPCallbacks {
@@ -2880,7 +2881,7 @@ class FilteringVisibleDeclConsumer : public swift::VisibleDeclConsumer {
28802881
void foundDecl(ValueDecl *VD, DeclVisibilityKind Reason,
28812882
DynamicLookupInfo dynamicLookupInfo) override {
28822883
if (!VD->hasClangNode() || isVisibleFromModule(ModuleFilter, VD))
2883-
NextConsumer.foundDecl(VD, Reason, dynamicLookupInfo);
2884+
NextConsumer.foundDecl(VD, Reason, dynamicLookupInfo);
28842885
}
28852886
};
28862887

@@ -4123,32 +4124,35 @@ bool ClangImporter::Implementation::lookupValue(SwiftLookupTable &table,
41234124

41244125
if (name.isOperator()) {
41254126

4126-
auto findAndConsumeBaseNameFromTable = [this, &table, &consumer, &declFound, &name](DeclBaseName declBaseName) {
4127-
for (auto entry : table.lookupMemberOperators(declBaseName)) {
4128-
if (isVisibleClangEntry(entry)) {
4129-
if (auto decl = dyn_cast_or_null<ValueDecl>(
4130-
importDeclReal(entry->getMostRecentDecl(), CurrentVersion))) {
4131-
consumer.foundDecl(decl, DeclVisibilityKind::VisibleAtTopLevel);
4132-
declFound = true;
4133-
for (auto alternate : getAlternateDecls(decl)) {
4134-
if (alternate->getName().matchesRef(name)) {
4135-
consumer.foundDecl(alternate, DeclVisibilityKind::DynamicLookup,
4136-
DynamicLookupInfo::AnyObject);
4137-
}
4138-
}
4139-
}
4127+
auto findAndConsumeBaseNameFromTable = [this, &table, &consumer, &declFound,
4128+
&name](DeclBaseName declBaseName) {
4129+
for (auto entry : table.lookupMemberOperators(declBaseName)) {
4130+
if (isVisibleClangEntry(entry)) {
4131+
if (auto decl = dyn_cast_or_null<ValueDecl>(
4132+
importDeclReal(entry->getMostRecentDecl(), CurrentVersion))) {
4133+
consumer.foundDecl(decl, DeclVisibilityKind::VisibleAtTopLevel);
4134+
declFound = true;
4135+
for (auto alternate : getAlternateDecls(decl)) {
4136+
if (alternate->getName().matchesRef(name)) {
4137+
consumer.foundDecl(alternate, DeclVisibilityKind::DynamicLookup,
4138+
DynamicLookupInfo::AnyObject);
41404139
}
4140+
}
41414141
}
4142-
};
4142+
}
4143+
}
4144+
};
41434145

4144-
findAndConsumeBaseNameFromTable(name.getBaseName());
4146+
findAndConsumeBaseNameFromTable(name.getBaseName());
41454147

4146-
// If CXXInterop is enabled we need to check the modified operator name as well
4147-
if(SwiftContext.LangOpts.EnableCXXInterop) {
4148-
auto declBaseName = DeclBaseName(SwiftContext.getIdentifier(
4149-
"__operator" + getOperatorNameForToken(name.getBaseName().getIdentifier().str().str())));
4150-
findAndConsumeBaseNameFromTable(declBaseName);
4151-
}
4148+
// If CXXInterop is enabled we need to check the modified operator name as
4149+
// well
4150+
if (SwiftContext.LangOpts.EnableCXXInterop) {
4151+
auto declBaseName = DeclBaseName(SwiftContext.getIdentifier(
4152+
"__operator" + getOperatorNameForToken(
4153+
name.getBaseName().getIdentifier().str().str())));
4154+
findAndConsumeBaseNameFromTable(declBaseName);
4155+
}
41524156
}
41534157

41544158
for (auto entry : table.lookup(name.getBaseName(), clangTU)) {

lib/ClangImporter/ImportDecl.cpp

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3597,15 +3597,20 @@ namespace {
35973597

35983598
// Check if this method _is_ an overloaded operator but is not a
35993599
// call / subscript. Those 2 operators do not need static versions
3600-
if(cxxOperatorKind != clang::OverloadedOperatorKind::OO_None &&
3600+
if (cxxOperatorKind != clang::OverloadedOperatorKind::OO_None &&
36013601
cxxOperatorKind != clang::OverloadedOperatorKind::OO_Call &&
3602-
cxxOperatorKind != clang::OverloadedOperatorKind::OO_Subscript) {
3602+
cxxOperatorKind !=
3603+
clang::OverloadedOperatorKind::OO_Subscript) {
36033604

36043605
auto opFuncDecl = makeOperator(MD, cxxMethod);
36053606

36063607
Impl.addAlternateDecl(MD, opFuncDecl);
36073608

3608-
Impl.markUnavailable(MD, "use " + std::string{clang::getOperatorSpelling(cxxOperatorKind)} + " instead");
3609+
Impl.markUnavailable(
3610+
MD,
3611+
"use " +
3612+
std::string{clang::getOperatorSpelling(cxxOperatorKind)} +
3613+
" instead");
36093614

36103615
// Make the actual member operator private.
36113616
MD->overwriteAccess(AccessLevel::Private);
@@ -5397,7 +5402,8 @@ namespace {
53975402
/// \param setter function returning `UnsafeMutablePointer<T>`
53985403
/// \return subscript declaration
53995404
SubscriptDecl *makeSubscript(FuncDecl *getter, FuncDecl *setter);
5400-
FuncDecl *makeOperator(FuncDecl *operatorMethod, clang::CXXMethodDecl *clangOperator);
5405+
FuncDecl *makeOperator(FuncDecl *operatorMethod,
5406+
clang::CXXMethodDecl *clangOperator);
54015407

54025408
VarDecl *makeComputedPropertyFromCXXMethods(FuncDecl *getter,
54035409
FuncDecl *setter);
@@ -8087,48 +8093,55 @@ SwiftDeclConverter::makeSubscript(FuncDecl *getter, FuncDecl *setter) {
80878093
return subscript;
80888094
}
80898095

8090-
80918096
static std::pair<BraceStmt *, bool>
80928097
synthesizeOperatorMethodBody(AbstractFunctionDecl *afd, void *context) {
80938098
ASTContext &ctx = afd->getASTContext();
80948099

80958100
auto funcDecl = cast<FuncDecl>(afd);
8096-
auto methodDecl = static_cast<FuncDecl *>(context); /* Swift version of CXXMethod */
8101+
auto methodDecl =
8102+
static_cast<FuncDecl *>(context); /* Swift version of CXXMethod */
80978103

80988104
SmallVector<Expr *, 8> forwardingParams;
80998105

8100-
// We start from +1 since the first param is our lhs. All other params are forwarded
8101-
for(auto itr = funcDecl->getParameters()->begin() + 1; itr != funcDecl->getParameters()->end(); itr++) {
8102-
auto param = *itr;
8103-
Expr *paramRefExpr = new (ctx) DeclRefExpr(param, DeclNameLoc(), /*Implicit=*/true);
8104-
paramRefExpr->setType(param->getType());
8106+
// We start from +1 since the first param is our lhs. All other params are
8107+
// forwarded
8108+
for (auto itr = funcDecl->getParameters()->begin() + 1;
8109+
itr != funcDecl->getParameters()->end(); itr++) {
8110+
auto param = *itr;
8111+
Expr *paramRefExpr =
8112+
new (ctx) DeclRefExpr(param, DeclNameLoc(), /*Implicit=*/true);
8113+
paramRefExpr->setType(param->getType());
81058114

8106-
if (param->isInOut()) {
8107-
paramRefExpr->setType(LValueType::get(param->getType()));
8115+
if (param->isInOut()) {
8116+
paramRefExpr->setType(LValueType::get(param->getType()));
81088117

8109-
paramRefExpr = new (ctx) InOutExpr(
8110-
SourceLoc(), paramRefExpr, param->getType(), /*isImplicit*/ true);
8111-
paramRefExpr->setType(InOutType::get(param->getType()));
8112-
}
8118+
paramRefExpr = new (ctx) InOutExpr(SourceLoc(), paramRefExpr,
8119+
param->getType(), /*isImplicit*/ true);
8120+
paramRefExpr->setType(InOutType::get(param->getType()));
8121+
}
81138122

8114-
forwardingParams.push_back(paramRefExpr);
8123+
forwardingParams.push_back(paramRefExpr);
81158124
}
81168125

8117-
auto methodExpr = new (ctx) DeclRefExpr(methodDecl, DeclNameLoc(), /*implicit*/ true);
8126+
auto methodExpr =
8127+
new (ctx) DeclRefExpr(methodDecl, DeclNameLoc(), /*implicit*/ true);
81188128
methodExpr->setType(methodDecl->getInterfaceType());
81198129

81208130
// Lhs parameter
81218131
auto baseParam = funcDecl->getParameters()->front();
8122-
Expr *baseExpr = new (ctx) DeclRefExpr(baseParam, DeclNameLoc(), /*implicit*/ true);
8132+
Expr *baseExpr =
8133+
new (ctx) DeclRefExpr(baseParam, DeclNameLoc(), /*implicit*/ true);
81238134
baseExpr->setType(baseParam->getType());
81248135
if (baseParam->isInOut()) {
8125-
baseExpr->setType(LValueType::get(baseParam->getType()));
8136+
baseExpr->setType(LValueType::get(baseParam->getType()));
81268137

8127-
baseExpr = new (ctx) InOutExpr(SourceLoc(), baseExpr, baseParam->getType(), /*isImplicit*/ true);
8128-
baseExpr->setType(InOutType::get(baseParam->getType()));
8138+
baseExpr = new (ctx) InOutExpr(SourceLoc(), baseExpr, baseParam->getType(),
8139+
/*isImplicit*/ true);
8140+
baseExpr->setType(InOutType::get(baseParam->getType()));
81298141
}
81308142

8131-
auto dotCallExpr = DotSyntaxCallExpr::create(ctx, methodExpr, SourceLoc(), baseExpr);
8143+
auto dotCallExpr =
8144+
DotSyntaxCallExpr::create(ctx, methodExpr, SourceLoc(), baseExpr);
81328145
dotCallExpr->setType(methodDecl->getMethodInterfaceType());
81338146
dotCallExpr->setThrows(false);
81348147

@@ -8140,27 +8153,30 @@ synthesizeOperatorMethodBody(AbstractFunctionDecl *afd, void *context) {
81408153
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), callExpr,
81418154
/*implicit=*/true);
81428155

8143-
81448156
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
81458157
/*implicit=*/true);
81468158
return {body, /*isTypeChecked=*/true};
81478159
}
81488160

8149-
FuncDecl *SwiftDeclConverter::makeOperator(FuncDecl *operatorMethod, clang::CXXMethodDecl *clangOperator) {
8161+
FuncDecl *
8162+
SwiftDeclConverter::makeOperator(FuncDecl *operatorMethod,
8163+
clang::CXXMethodDecl *clangOperator) {
81508164
auto &ctx = Impl.SwiftContext;
8151-
auto opName = clang::getOperatorSpelling(clangOperator->getOverloadedOperator());
8165+
auto opName =
8166+
clang::getOperatorSpelling(clangOperator->getOverloadedOperator());
81528167
auto paramList = operatorMethod->getParameters();
81538168
auto genericParamList = operatorMethod->getGenericParams();
81548169

81558170
auto opId = ctx.getIdentifier(opName);
81568171

81578172
auto parentCtx = operatorMethod->getDeclContext();
81588173

8159-
auto lhsParam = new (ctx)
8160-
ParamDecl(SourceLoc(), SourceLoc(), Identifier(), SourceLoc(),
8161-
ctx.getIdentifier("lhs"), parentCtx);
8174+
auto lhsParam =
8175+
new (ctx) ParamDecl(SourceLoc(), SourceLoc(), Identifier(), SourceLoc(),
8176+
ctx.getIdentifier("lhs"), parentCtx);
81628177

8163-
lhsParam->setInterfaceType(operatorMethod->getDeclContext()->getSelfInterfaceType());
8178+
lhsParam->setInterfaceType(
8179+
operatorMethod->getDeclContext()->getSelfInterfaceType());
81648180

81658181
if (operatorMethod->isMutating()) {
81668182
// This implicitly makes the parameter indirect.
@@ -8172,35 +8188,32 @@ FuncDecl *SwiftDeclConverter::makeOperator(FuncDecl *operatorMethod, clang::CXXM
81728188
SmallVector<ParamDecl *, 4> newParams;
81738189
newParams.push_back(lhsParam);
81748190

8175-
for(auto param : *paramList) {
8176-
newParams.push_back(param);
8191+
for (auto param : *paramList) {
8192+
newParams.push_back(param);
81778193
}
81788194

81798195
auto oldArgNames = operatorMethod->getName().getArgumentNames();
81808196
SmallVector<Identifier, 4> newArgNames;
81818197
newArgNames.push_back(Identifier());
81828198

8183-
for(auto id : oldArgNames) {
8184-
newArgNames.push_back(id);
8199+
for (auto id : oldArgNames) {
8200+
newArgNames.push_back(id);
81858201
}
81868202

8187-
auto opDeclName = DeclName(ctx, opId, {newArgNames.begin(), newArgNames.end()});
8203+
auto opDeclName =
8204+
DeclName(ctx, opId, {newArgNames.begin(), newArgNames.end()});
81888205

81898206
auto topLevelStaticFuncDecl = FuncDecl::createImplicit(
8190-
ctx,
8191-
StaticSpellingKind::None,
8192-
opDeclName, SourceLoc(),
8193-
false, false,
8194-
genericParamList, ParameterList::create(ctx, newParams),
8195-
operatorMethod->getResultInterfaceType(),
8196-
parentCtx
8197-
);
8207+
ctx, StaticSpellingKind::None, opDeclName, SourceLoc(),
8208+
/*Async*/ false, /*Throws*/ false, genericParamList,
8209+
ParameterList::create(ctx, newParams),
8210+
operatorMethod->getResultInterfaceType(), parentCtx);
81988211

81998212
topLevelStaticFuncDecl->setAccess(AccessLevel::Public);
8200-
topLevelStaticFuncDecl->setImplicit();
82018213
topLevelStaticFuncDecl->setIsDynamic(false);
82028214
topLevelStaticFuncDecl->setStatic();
8203-
topLevelStaticFuncDecl->setBodySynthesizer(synthesizeOperatorMethodBody, operatorMethod);
8215+
topLevelStaticFuncDecl->setBodySynthesizer(synthesizeOperatorMethodBody,
8216+
operatorMethod);
82048217

82058218
return topLevelStaticFuncDecl;
82068219
}

lib/ClangImporter/ImportName.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ static const char *getOperatorName(clang::OverloadedOperatorKind Operator) {
6363
case clang::NUM_OVERLOADED_OPERATORS:
6464
return nullptr;
6565

66-
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
67-
case clang::OO_##Name: return #Name;
66+
#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
67+
case clang::OO_##Name: \
68+
return #Name;
6869
#include "clang/Basic/OperatorKinds.def"
6970
}
7071

@@ -1856,7 +1857,13 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
18561857
case clang::OverloadedOperatorKind::OO_GreaterEqual:
18571858
case clang::OverloadedOperatorKind::OO_AmpAmp:
18581859
case clang::OverloadedOperatorKind::OO_PipePipe:
1859-
baseName = isa<clang::CXXMethodDecl>(functionDecl) ? swiftCtx.getIdentifier("__operator" + std::string{getOperatorName(op)}).str() : swiftCtx.getIdentifier(clang::getOperatorSpelling(op)).str();
1860+
baseName =
1861+
isa<clang::CXXMethodDecl>(functionDecl)
1862+
? swiftCtx
1863+
.getIdentifier("__operator" +
1864+
std::string{getOperatorName(op)})
1865+
.str()
1866+
: swiftCtx.getIdentifier(clang::getOperatorSpelling(op)).str();
18601867
isFunction = true;
18611868
addEmptyArgNamesForClangFunction(functionDecl, argumentNames);
18621869
break;

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,10 +3477,9 @@ TypeConverter::getDeclRefRepresentation(SILDeclRef c) {
34773477
bool staticOperator = method->isOverloadedOperator() &&
34783478
method->getOverloadedOperator() != clang::OO_Call &&
34793479
method->getOverloadedOperator() != clang::OO_Subscript;
3480-
return isa<clang::CXXConstructorDecl>(method) ||
3481-
method->isStatic()
3482-
? SILFunctionTypeRepresentation::CFunctionPointer
3483-
: SILFunctionTypeRepresentation::CXXMethod;
3480+
return isa<clang::CXXConstructorDecl>(method) || method->isStatic()
3481+
? SILFunctionTypeRepresentation::CFunctionPointer
3482+
: SILFunctionTypeRepresentation::CXXMethod;
34843483
}
34853484

34863485

0 commit comments

Comments
 (0)