Skip to content

Commit cd7a7a5

Browse files
authored
Add basic char*_t support for libc (partial WG14 N2653) (#90360)
This PR implements a part of WG14 N2653: - Define C23 char8_t - Define C11 char16_t - Define C11 char32_t Missing goals are: - The type of UTF-8 character literals is changed from unsigned char to char8_t. (Since UTF-8 character literals already have type unsigned char, this is not a semantic change). - New mbrtoc8() and c8rtomb() functions declared in <uchar.h> enable conversions between multibyte characters and UTF-8. - A new ATOMIC_CHAR8_T_LOCK_FREE macro. - A new atomic_char8_t typedef name.
1 parent 89f8335 commit cd7a7a5

File tree

14 files changed

+97
-2
lines changed

14 files changed

+97
-2
lines changed

libc/config/baremetal/api.td

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,10 @@ def TimeAPI : PublicAPI<"time.h"> {
8585
}
8686

8787
def UCharAPI : PublicAPI<"uchar.h"> {
88-
let Types = ["mbstate_t"];
88+
let Types = [
89+
"mbstate_t",
90+
"char8_t",
91+
"char16_t",
92+
"char32_t",
93+
];
8994
}

libc/config/linux/aarch64/headers.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ set(TARGET_PUBLIC_HEADERS
2525
libc.include.threads
2626
libc.include.time
2727
libc.include.unistd
28+
libc.include.wchar
29+
libc.include.uchar
2830

2931
libc.include.sys_ioctl
3032
# Disabled due to epoll_wait syscalls not being available on this platform.

libc/config/linux/api.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,15 @@ def WCharAPI : PublicAPI<"wchar.h"> {
206206
];
207207
}
208208

209+
def UCharAPI : PublicAPI<"uchar.h"> {
210+
let Types = [
211+
"mbstate_t",
212+
"char8_t",
213+
"char16_t",
214+
"char32_t",
215+
];
216+
}
217+
209218
def SysRandomAPI : PublicAPI<"sys/random.h"> {
210219
let Types = ["size_t", "ssize_t"];
211220
}

libc/config/linux/arm/headers.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ set(TARGET_PUBLIC_HEADERS
1212
libc.include.string
1313
libc.include.strings
1414
libc.include.search
15+
libc.include.wchar
16+
libc.include.uchar
1517

1618
# Disabled due to epoll_wait syscalls not being available on this platform.
1719
# libc.include.sys_epoll

libc/config/linux/riscv/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ set(TARGET_PUBLIC_HEADERS
2828
libc.include.time
2929
libc.include.unistd
3030
libc.include.wchar
31+
libc.include.uchar
3132

3233
libc.include.arpa_inet
3334

libc/config/linux/x86_64/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ set(TARGET_PUBLIC_HEADERS
2929
libc.include.time
3030
libc.include.unistd
3131
libc.include.wchar
32+
libc.include.uchar
3233

3334
libc.include.arpa_inet
3435

libc/docs/c23.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,4 @@ Additions:
158158

159159
* mbrtoc8
160160
* c8rtomb
161-
* char*_t
161+
* char*_t |check|

libc/include/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,9 @@ add_gen_header(
603603
DEPENDS
604604
.llvm_libc_common_h
605605
.llvm-libc-types.mbstate_t
606+
.llvm-libc-types.char8_t
607+
.llvm-libc-types.char16_t
608+
.llvm-libc-types.char32_t
606609
)
607610

608611
add_gen_header(

libc/include/llvm-libc-types/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ add_header(tcflag_t HDR tcflag_t.h)
9090
add_header(struct_termios HDR struct_termios.h DEPENDS .cc_t .speed_t .tcflag_t)
9191
add_header(__getoptargv_t HDR __getoptargv_t.h)
9292
add_header(wchar_t HDR wchar_t.h)
93+
add_header(char8_t HDR char8_t.h)
94+
add_header(
95+
char16_t
96+
HDR
97+
char16_t.h
98+
DEPENDS
99+
libc.include.llvm-libc-macros.stdint_macros
100+
)
101+
add_header(
102+
char32_t
103+
HDR
104+
char32_t.h
105+
DEPENDS
106+
libc.include.llvm-libc-macros.stdint_macros
107+
)
93108
add_header(wint_t HDR wint_t.h)
94109
add_header(sa_family_t HDR sa_family_t.h)
95110
add_header(socklen_t HDR socklen_t.h)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- Definition of char16_t type ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_TYPES_CHAR16_T_H
10+
#define LLVM_LIBC_TYPES_CHAR16_T_H
11+
12+
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
13+
#include "../llvm-libc-macros/stdint-macros.h"
14+
typedef uint_least16_t char16_t;
15+
#endif
16+
17+
#endif // LLVM_LIBC_TYPES_CHAR16_T_H
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- Definition of char32_t type ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_TYPES_CHAR32_T_H
10+
#define LLVM_LIBC_TYPES_CHAR32_T_H
11+
12+
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
13+
#include "../llvm-libc-macros/stdint-macros.h"
14+
typedef uint_least32_t char32_t;
15+
#endif
16+
17+
#endif // LLVM_LIBC_TYPES_CHAR32_T_H
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- Definition of char8_t type ----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_TYPES_CHAR8_T_H
10+
#define LLVM_LIBC_TYPES_CHAR8_T_H
11+
12+
#if !defined(__cplusplus) && defined(__STDC_VERSION__) && \
13+
__STDC_VERSION__ >= 202311L
14+
typedef unsigned char char8_t;
15+
#endif
16+
17+
#endif // LLVM_LIBC_TYPES_CHAR8_T_H

libc/spec/spec.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ def SizeTType : NamedType<"size_t">;
6565
def SizeTPtr : PtrType<SizeTType>;
6666
def RestrictedSizeTPtr : RestrictedPtrType<SizeTType>;
6767

68+
def Char8TType : NamedType<"char8_t">;
69+
def Char16TType : NamedType<"char16_t">;
70+
def Char32TType : NamedType<"char32_t">;
6871
def WCharType : NamedType<"wchar_t">;
6972
def WIntType : NamedType<"wint_t">;
7073

libc/spec/stdc.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,9 @@ def StdC : StandardSpec<"stdc"> {
13981398
[], // Macros
13991399
[ //Types
14001400
MBStateTType,
1401+
Char8TType,
1402+
Char16TType,
1403+
Char32TType,
14011404
],
14021405
[], // Enumerations
14031406
[]

0 commit comments

Comments
 (0)