Skip to content

Commit fcb5906

Browse files
committed
[cxx-interop] Support char8_t C++20 type
https://en.cppreference.com/w/cpp/keyword/char8_t This is based on a patch from Varun Gandhi: swiftlang#26153 rdar://39988329 / resolves swiftlang#68726
1 parent 488581f commit fcb5906

File tree

12 files changed

+25
-7
lines changed

12 files changed

+25
-7
lines changed

include/swift/ClangImporter/BuiltinMappedTypes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ MAP_BUILTIN_INTEGER_TYPE(UInt, CUnsignedInt)
4747
MAP_BUILTIN_INTEGER_TYPE(ULong, CUnsignedLong)
4848
MAP_BUILTIN_INTEGER_TYPE(ULongLong, CUnsignedLongLong)
4949
MAP_BUILTIN_INTEGER_TYPE(UInt128, CUnsignedInt128)
50+
MAP_BUILTIN_INTEGER_TYPE(Char8, CChar8)
5051
MAP_BUILTIN_INTEGER_TYPE(Char16, CChar16)
5152
MAP_BUILTIN_INTEGER_TYPE(Char32, CChar32)
5253
MAP_BUILTIN_INTEGER_TYPE(SChar, CSignedChar)

include/swift/PrintAsClang/ClangMacros.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#endif
7474

7575
CLANG_MACRO_DEFINED("SWIFT_TYPEDEFS")
76+
CLANG_MACRO_DEFINED("char8_t")
7677
CLANG_MACRO_DEFINED("char16_t")
7778
CLANG_MACRO_DEFINED("char32_t")
7879

lib/ClangImporter/ImportMacro.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ builtinTypeForToken(const clang::Token &tok, const clang::ASTContext &context) {
301301
return clang::QualType(context.WCharTy);
302302
case clang::tok::kw_bool:
303303
return clang::QualType(context.BoolTy);
304+
case clang::tok::kw_char8_t:
305+
return clang::QualType(context.Char8Ty);
304306
case clang::tok::kw_char16_t:
305307
return clang::QualType(context.Char16Ty);
306308
case clang::tok::kw_char32_t:

lib/ClangImporter/ImportType.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ namespace {
312312
case clang::BuiltinType::BFloat16:
313313
case clang::BuiltinType::Float128:
314314
case clang::BuiltinType::NullPtr:
315-
case clang::BuiltinType::Char8:
316315
case clang::BuiltinType::Ibm128:
317316
return Type();
318317

lib/PrintAsClang/PrimitiveTypeMapping.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
3838

3939
MAP(CChar, "char", false);
4040
MAP(CWideChar, "wchar_t", false);
41+
MAP(CChar8, "char8_t", false);
4142
MAP(CChar16, "char16_t", false);
4243
MAP(CChar32, "char32_t", false);
4344

lib/PrintAsClang/PrintAsClang.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
138138
"# if __has_include(<uchar.h>)\n"
139139
"# include <uchar.h>\n"
140140
"# elif !defined(__cplusplus)\n"
141+
"typedef unsigned char char8_t;\n"
141142
"typedef uint_least16_t char16_t;\n"
142143
"typedef uint_least32_t char32_t;\n"
143144
"# endif\n"

stdlib/public/core/CTypes.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ public typealias CWideChar = UInt16
134134
public typealias CWideChar = Unicode.Scalar
135135
#endif
136136

137+
/// The C++20 'char8_t' type, which has UTF-8 encoding.
138+
public typealias CChar8 = UInt8
139+
137140
// FIXME: Swift should probably have a UTF-16 type other than UInt16.
138141
//
139142
/// The C++11 'char16_t' type, which has UTF-16 encoding.

test/IRGen/objc_type_encoding.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ import gizmo
7474
// CHECK-watchos: private unnamed_addr constant [23 x i8] c"v44@0:8C16S20I24Q28Q36\00"
7575
// CHECK-xros: private unnamed_addr constant [23 x i8] c"v44@0:8C16S20I24Q28Q36\00"
7676

77-
@objc func testCChars(_ basic: CChar, wchar wide: CWideChar, char16: CChar16, char32: CChar32) {}
78-
// CHECK-macosx: private unnamed_addr constant [20 x i8] c"v32@0:8c16i20S24i28\00"
79-
// CHECK-ios: private unnamed_addr constant [20 x i8] c"v32@0:8c16i20S24i28\00"
80-
// CHECK-tvos: private unnamed_addr constant [20 x i8] c"v32@0:8c16i20S24i28\00"
77+
@objc func testCChars(_ basic: CChar, wchar wide: CWideChar, char8: CChar8, char16: CChar16, char32: CChar32) {}
78+
// CHECK-macosx: private unnamed_addr constant [23 x i8] c"v36@0:8c16i20C24S28i32\00"
79+
// CHECK-ios: private unnamed_addr constant [23 x i8] c"v36@0:8c16i20C24S28i32\00"
80+
// CHECK-tvos: private unnamed_addr constant [23 x i8] c"v36@0:8c16i20C24S28i32\00"
8181
// CHECK-watchos: private unnamed_addr constant [20 x i8] c"v32@0:8c16i20S24i28\00"
8282
// CHECK-xros: private unnamed_addr constant [20 x i8] c"v32@0:8c16i20S24i28\00"
8383

test/Interop/C/chars/Inputs/import-cchar-types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
extern char a_char;
77
extern wchar_t a_wchar;
88

9+
#if __cplusplus
10+
extern char8_t small_char;
11+
#endif
12+
913
#endif // TEST_INTEROP_C_CHARS_INPUTS_IMPORT_CCHAR_TYPES_H
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// RUN: %target-swift-ide-test -print-module -module-to-print=ImportCCharTypes -I %S/Inputs -source-filename=x | %FileCheck %s
2+
// RUN: %target-swift-ide-test -print-module -module-to-print=ImportCCharTypes -I %S/Inputs -source-filename=x -cxx-interoperability-mode=default -Xcc -std=c++20 | %FileCheck %s --check-prefix=CHECK-CXX
23

34
// CHECK: var a_char: CChar
45
// CHECK: var a_wchar: wchar_t
6+
7+
// CHECK-CXX: var small_char: UInt8

0 commit comments

Comments
 (0)