Skip to content

Commit 83d4cc5

Browse files
committed
Move registerStdSpanTypeMapping to SwiftifyInfoPrinter (NFC)
`registerStdSpanTypeMapping` used to be a lambda inside `swiftify`. By moving it, along with the `typeMapping` state, inside `SwiftifyInfoPrinter` we will simplify changes necessary to support `_SwiftifyImportProtocol` in ClangImporter.
1 parent 7e13751 commit 83d4cc5

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9046,6 +9046,8 @@ class SwiftifyInfoPrinter {
90469046
clang::ASTContext &ctx;
90479047
llvm::raw_ostream &out;
90489048
bool firstParam = true;
9049+
llvm::StringMap<std::string> typeMapping;
9050+
90499051
SwiftifyInfoPrinter(clang::ASTContext &ctx, llvm::raw_ostream &out)
90509052
: ctx(ctx), out(out) {
90519053
out << "@_SwiftifyImport(";
@@ -9091,14 +9093,24 @@ class SwiftifyInfoPrinter {
90919093
out << ")";
90929094
}
90939095

9094-
void printTypeMapping(const llvm::StringMap<std::string> &mapping) {
9096+
bool registerStdSpanTypeMapping(Type swiftType, const clang::QualType clangType) {
9097+
const auto *decl = clangType->getAsTagDecl();
9098+
if (decl && decl->isInStdNamespace() && decl->getName() == "span") {
9099+
typeMapping.insert(std::make_pair(
9100+
swiftType->getString(), swiftType->getDesugaredType()->getString()));
9101+
return true;
9102+
}
9103+
return false;
9104+
}
9105+
9106+
void printTypeMapping() {
90959107
printSeparator();
90969108
out << "typeMappings: [";
9097-
if (mapping.empty()) {
9109+
if (typeMapping.empty()) {
90989110
out << ":]";
90999111
return;
91009112
}
9101-
llvm::interleaveComma(mapping, out, [&](const auto &entry) {
9113+
llvm::interleaveComma(typeMapping, out, [&](const auto &entry) {
91029114
out << '"' << entry.getKey() << "\" : \"" << entry.getValue() << '"';
91039115
});
91049116
out << "]";
@@ -9140,21 +9152,9 @@ void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {
91409152
bool attachMacro = false;
91419153
{
91429154
llvm::raw_svector_ostream out(MacroString);
9143-
llvm::StringMap<std::string> typeMapping;
9144-
9145-
auto registerStdSpanTypeMapping =
9146-
[&typeMapping](Type swiftType, const clang::QualType clangType) {
9147-
const auto *decl = clangType->getAsTagDecl();
9148-
if (decl && decl->isInStdNamespace() && decl->getName() == "span") {
9149-
typeMapping.insert(
9150-
std::make_pair(swiftType->getString(),
9151-
swiftType->getDesugaredType()->getString()));
9152-
return true;
9153-
}
9154-
return false;
9155-
};
9155+
91569156
SwiftifyInfoPrinter printer(getClangASTContext(), out);
9157-
bool returnIsStdSpan = registerStdSpanTypeMapping(
9157+
bool returnIsStdSpan = printer.registerStdSpanTypeMapping(
91589158
MappedDecl->getResultInterfaceType(), ClangDecl->getReturnType());
91599159
if (auto CAT =
91609160
ClangDecl->getReturnType()->getAs<clang::CountAttributedType>()) {
@@ -9176,7 +9176,7 @@ void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {
91769176
printer.printCountedBy(CAT, index);
91779177
attachMacro = paramHasBoundsInfo = true;
91789178
}
9179-
bool paramIsStdSpan = registerStdSpanTypeMapping(
9179+
bool paramIsStdSpan = printer.registerStdSpanTypeMapping(
91809180
swiftParam->getInterfaceType(), clangParamTy);
91819181
paramHasBoundsInfo |= paramIsStdSpan;
91829182

@@ -9197,7 +9197,7 @@ void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {
91979197
}
91989198
if (returnIsStdSpan && returnHasLifetimeInfo)
91999199
attachMacro = true;
9200-
printer.printTypeMapping(typeMapping);
9200+
printer.printTypeMapping();
92019201
}
92029202

92039203
if (attachMacro)

0 commit comments

Comments
 (0)