File tree Expand file tree Collapse file tree 6 files changed +112
-1
lines changed
test/Interop/SwiftToCxx/structs Expand file tree Collapse file tree 6 files changed +112
-1
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,8 @@ add_swift_host_library(swiftPrintAsClang STATIC
55 ModuleContentsWriter.cpp
66 PrimitiveTypeMapping.cpp
77 PrintAsClang.cpp
8- PrintClangFunction.cpp)
8+ PrintClangFunction.cpp
9+ PrintClangValueType.cpp)
910target_link_libraries (swiftPrintAsClang PRIVATE
1011 swiftAST
1112 swiftClangImporter
Original file line number Diff line number Diff line change 1414#include " ClangSyntaxPrinter.h"
1515#include " PrimitiveTypeMapping.h"
1616#include " PrintClangFunction.h"
17+ #include " PrintClangValueType.h"
1718
1819#include " swift/AST/ASTContext.h"
1920#include " swift/AST/ASTMangler.h"
@@ -327,6 +328,14 @@ class DeclAndTypePrinter::Implementation
327328 os << " @end\n " ;
328329 }
329330
331+ void visitStructDecl (StructDecl *SD) {
332+ if (outputLang != OutputLanguageMode::Cxx)
333+ return ;
334+ // FIXME: Print struct's availability.
335+ ClangValueTypePrinter printer (os);
336+ printer.printStructDecl (SD);
337+ }
338+
330339 void visitExtensionDecl (ExtensionDecl *ED) {
331340 if (isEmptyExtensionDecl (ED))
332341 return ;
Original file line number Diff line number Diff line change @@ -409,6 +409,13 @@ class ModuleWriter {
409409 return true ;
410410 }
411411
412+ bool writeStruct (const StructDecl *SD) {
413+ if (addImport (SD))
414+ return true ;
415+ printer.print (SD);
416+ return true ;
417+ }
418+
412419 bool writeProtocol (const ProtocolDecl *PD) {
413420 if (addImport (PD))
414421 return true ;
@@ -584,6 +591,8 @@ class ModuleWriter {
584591 if (outputLangMode == OutputLanguageMode::Cxx) {
585592 if (auto FD = dyn_cast<FuncDecl>(D))
586593 success = writeFunc (FD);
594+ if (auto SD = dyn_cast<StructDecl>(D))
595+ success = writeStruct (SD);
587596 // FIXME: Warn on unsupported exported decl.
588597 } else if (isa<ValueDecl>(D)) {
589598 if (auto CD = dyn_cast<ClassDecl>(D))
Original file line number Diff line number Diff line change 1+ // ===--- PrintClangValueType.cpp - Printer for C/C++ value types *- C++ -*-===//
2+ //
3+ // This source file is part of the Swift.org open source project
4+ //
5+ // Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+ // Licensed under Apache License v2.0 with Runtime Library Exception
7+ //
8+ // See https://swift.org/LICENSE.txt for license information
9+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+ //
11+ // ===----------------------------------------------------------------------===//
12+
13+ #include " PrintClangValueType.h"
14+ #include " ClangSyntaxPrinter.h"
15+ #include " DeclAndTypePrinter.h"
16+ #include " OutputLanguageMode.h"
17+ #include " PrimitiveTypeMapping.h"
18+ #include " swift/AST/Decl.h"
19+ #include " swift/AST/ParameterList.h"
20+ #include " swift/AST/Type.h"
21+ #include " swift/AST/TypeVisitor.h"
22+ #include " swift/ClangImporter/ClangImporter.h"
23+ #include " llvm/ADT/STLExtras.h"
24+
25+ using namespace swift ;
26+
27+ void ClangValueTypePrinter::printStructDecl (const StructDecl *SD) {
28+ os << " class " ;
29+ ClangSyntaxPrinter (os).printIdentifier (SD->getName ().str ());
30+ os << " final {\n " ;
31+ // FIXME: Print the members of the struct.
32+ os << " };\n " ;
33+ }
Original file line number Diff line number Diff line change 1+ // ===--- PrintClangValueType.h - Printer for C/C++ value types --*- C++ -*-===//
2+ //
3+ // This source file is part of the Swift.org open source project
4+ //
5+ // Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+ // Licensed under Apache License v2.0 with Runtime Library Exception
7+ //
8+ // See https://swift.org/LICENSE.txt for license information
9+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+ //
11+ // ===----------------------------------------------------------------------===//
12+
13+ #ifndef SWIFT_PRINTASCLANG_PRINTCLANGVALUETYPE_H
14+ #define SWIFT_PRINTASCLANG_PRINTCLANGVALUETYPE_H
15+
16+ #include " swift/Basic/LLVM.h"
17+ #include " llvm/Support/raw_ostream.h"
18+
19+ namespace swift {
20+
21+ class StructDecl ;
22+
23+ // / Responsible for printing a Swift struct or enum decl or in C or C++ mode, to
24+ // / be included in a Swift module's generated clang header.
25+ class ClangValueTypePrinter {
26+ public:
27+ ClangValueTypePrinter (raw_ostream &os) : os(os) {}
28+
29+ // / Print the C struct thunk or the C++ class definition that
30+ // / corresponds to the given structure declaration.
31+ void printStructDecl (const StructDecl *SD);
32+
33+ private:
34+ raw_ostream &os;
35+ };
36+
37+ } // end namespace swift
38+
39+ #endif
Original file line number Diff line number Diff line change 1+ // RUN: %empty-directory(%t)
2+ // RUN: %target-swift-frontend %s -typecheck -module-name Structs -clang-header-expose-public-decls -emit-clang-header-path %t/structs.h
3+ // RUN: %FileCheck %s < %t/structs.h
4+
5+ // RUN: %check-interop-cxx-header-in-clang(%t/structs.h)
6+
7+ // CHECK: namespace Structs {
8+
9+ // CHECK: class StructWithIntField final {
10+ // CHECK-NEXT: };
11+ struct StructWithIntField {
12+ let field : Int
13+ }
14+
15+ // Special name gets renamed in C++.
16+ // CHECK: class register_ final {
17+ struct register {
18+ }
19+
20+ // CHECK: } // namespace Structs
You can’t perform that action at this time.
0 commit comments