Skip to content

Commit 7f597c6

Browse files
authored
Merge pull request #76726 from swiftlang/egorzhdan/char8_t
[cxx-interop] Support `char8_t` C++20 type
2 parents 452447e + fcb5906 commit 7f597c6

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

test/PrintAsObjC/classes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class NotObjC {}
204204
// CHECK-NEXT: - (void)testSelector:(SEL _Nonnull)sel boolean:(BOOL)b;
205205
// CHECK-NEXT: - (void)testCSignedTypes:(signed char)a b:(short)b c:(int)c d:(long)d e:(long long)e;
206206
// CHECK-NEXT: - (void)testCUnsignedTypes:(unsigned char)a b:(unsigned short)b c:(unsigned int)c d:(unsigned long)d e:(unsigned long long)e;
207-
// CHECK-NEXT: - (void)testCChars:(char)basic wchar:(wchar_t)wide char16:(char16_t)char16 char32:(char32_t)char32;
207+
// CHECK-NEXT: - (void)testCChars:(char)basic wchar:(wchar_t)wide char8:(char8_t)char8 char16:(char16_t)char16 char32:(char32_t)char32;
208208
// CHECK-NEXT: - (void)testCFloats:(float)a b:(double)b;
209209
// CHECK-NEXT: - (void)testCBool:(bool)a;
210210
// CHECK-NEXT: - (void)testSizedSignedTypes:(int8_t)a b:(int16_t)b c:(int32_t)c d:(int64_t)d;
@@ -250,7 +250,7 @@ class NotObjC {}
250250

251251
@objc func testCSignedTypes(_ a: CSignedChar, b: CShort, c: CInt, d: CLong, e: CLongLong) {}
252252
@objc func testCUnsignedTypes(_ a: CUnsignedChar, b: CUnsignedShort, c: CUnsignedInt, d: CUnsignedLong, e: CUnsignedLongLong) {}
253-
@objc func testCChars(_ basic: CChar, wchar wide: CWideChar, char16: CChar16, char32: CChar32) {}
253+
@objc func testCChars(_ basic: CChar, wchar wide: CWideChar, char8: CChar8, char16: CChar16, char32: CChar32) {}
254254
@objc func testCFloats(_ a: CFloat, b: CDouble) {}
255255
@objc func testCBool(_ a: CBool) {}
256256

test/stdlib/PrintInteger.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ PrintTests.test("CustomStringConvertible") {
3939
#else
4040
hasDescription(CWideChar(42)!)
4141
#endif
42+
hasDescription(CChar8(42))
4243
hasDescription(CChar16(42))
4344
hasDescription(CChar32(42)!)
4445
}
@@ -60,6 +61,7 @@ PrintTests.test("Printable") {
6061
#else
6162
expectPrinted("*", CWideChar(42)!)
6263
#endif
64+
expectPrinted("42", CChar8(42))
6365
expectPrinted("42", CChar16(42))
6466
expectPrinted("*", CChar32(42)!)
6567

@@ -155,6 +157,7 @@ PrintTests.test("Printable") {
155157
#else
156158
expectPrinted("*", CWideChar(42)!)
157159
#endif
160+
expectPrinted("42", CChar8(42))
158161
expectPrinted("42", CChar16(42))
159162
expectPrinted("*", CChar32(42)!)
160163
}

0 commit comments

Comments
 (0)