Skip to content

[interop][swiftToCxx] emit empty skeleton C++ classes for Swift struc… #59078

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 26, 2022
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
3 changes: 2 additions & 1 deletion lib/PrintAsClang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ add_swift_host_library(swiftPrintAsClang STATIC
ModuleContentsWriter.cpp
PrimitiveTypeMapping.cpp
PrintAsClang.cpp
PrintClangFunction.cpp)
PrintClangFunction.cpp
PrintClangValueType.cpp)
target_link_libraries(swiftPrintAsClang PRIVATE
swiftAST
swiftClangImporter
Expand Down
9 changes: 9 additions & 0 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "ClangSyntaxPrinter.h"
#include "PrimitiveTypeMapping.h"
#include "PrintClangFunction.h"
#include "PrintClangValueType.h"

#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTMangler.h"
Expand Down Expand Up @@ -327,6 +328,14 @@ class DeclAndTypePrinter::Implementation
os << "@end\n";
}

void visitStructDecl(StructDecl *SD) {
if (outputLang != OutputLanguageMode::Cxx)
return;
// FIXME: Print struct's availability.
ClangValueTypePrinter printer(os);
printer.printStructDecl(SD);
}

void visitExtensionDecl(ExtensionDecl *ED) {
if (isEmptyExtensionDecl(ED))
return;
Expand Down
9 changes: 9 additions & 0 deletions lib/PrintAsClang/ModuleContentsWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,13 @@ class ModuleWriter {
return true;
}

bool writeStruct(const StructDecl *SD) {
if (addImport(SD))
return true;
printer.print(SD);
return true;
}

bool writeProtocol(const ProtocolDecl *PD) {
if (addImport(PD))
return true;
Expand Down Expand Up @@ -584,6 +591,8 @@ class ModuleWriter {
if (outputLangMode == OutputLanguageMode::Cxx) {
if (auto FD = dyn_cast<FuncDecl>(D))
success = writeFunc(FD);
if (auto SD = dyn_cast<StructDecl>(D))
success = writeStruct(SD);
// FIXME: Warn on unsupported exported decl.
} else if (isa<ValueDecl>(D)) {
if (auto CD = dyn_cast<ClassDecl>(D))
Expand Down
33 changes: 33 additions & 0 deletions lib/PrintAsClang/PrintClangValueType.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===--- PrintClangValueType.cpp - Printer for C/C++ value types *- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "PrintClangValueType.h"
#include "ClangSyntaxPrinter.h"
#include "DeclAndTypePrinter.h"
#include "OutputLanguageMode.h"
#include "PrimitiveTypeMapping.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Type.h"
#include "swift/AST/TypeVisitor.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "llvm/ADT/STLExtras.h"

using namespace swift;

void ClangValueTypePrinter::printStructDecl(const StructDecl *SD) {
os << "class ";
ClangSyntaxPrinter(os).printIdentifier(SD->getName().str());
os << " final {\n";
// FIXME: Print the members of the struct.
os << "};\n";
}
39 changes: 39 additions & 0 deletions lib/PrintAsClang/PrintClangValueType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//===--- PrintClangValueType.h - Printer for C/C++ value types --*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_PRINTASCLANG_PRINTCLANGVALUETYPE_H
#define SWIFT_PRINTASCLANG_PRINTCLANGVALUETYPE_H

#include "swift/Basic/LLVM.h"
#include "llvm/Support/raw_ostream.h"

namespace swift {

class StructDecl;

/// Responsible for printing a Swift struct or enum decl or in C or C++ mode, to
/// be included in a Swift module's generated clang header.
class ClangValueTypePrinter {
public:
ClangValueTypePrinter(raw_ostream &os) : os(os) {}

/// Print the C struct thunk or the C++ class definition that
/// corresponds to the given structure declaration.
void printStructDecl(const StructDecl *SD);

private:
raw_ostream &os;
};

} // end namespace swift

#endif
20 changes: 20 additions & 0 deletions test/Interop/SwiftToCxx/structs/swift-struct-in-cxx.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -typecheck -module-name Structs -clang-header-expose-public-decls -emit-clang-header-path %t/structs.h
// RUN: %FileCheck %s < %t/structs.h

// RUN: %check-interop-cxx-header-in-clang(%t/structs.h)

// CHECK: namespace Structs {

// CHECK: class StructWithIntField final {
// CHECK-NEXT: };
struct StructWithIntField {
let field: Int
}

// Special name gets renamed in C++.
// CHECK: class register_ final {
struct register {
}

// CHECK: } // namespace Structs