Skip to content

Commit 52583b3

Browse files
authored
[libc] Character converter skeleton class (#143619)
Made CharacterConverter class skeleton
1 parent c70658e commit 52583b3

File tree

8 files changed

+258
-0
lines changed

8 files changed

+258
-0
lines changed

libc/hdr/types/char32_t.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of char32_t.h ------------------------------------------===//
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_HDR_TYPES_CHAR32_T_H
10+
#define LLVM_LIBC_HDR_TYPES_CHAR32_T_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-types/char32_t.h"
15+
16+
#else // overlay mode
17+
18+
#include "hdr/uchar_overlay.h"
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_TYPES_CHAR32_T_H

libc/hdr/types/char8_t.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of char8_t.h -------------------------------------------===//
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_HDR_TYPES_CHAR8_T_H
10+
#define LLVM_LIBC_HDR_TYPES_CHAR8_T_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-types/char8_t.h"
15+
16+
#else // overlay mode
17+
18+
#include "hdr/uchar_overlay.h"
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_TYPES_CHAR8_T_H

libc/hdr/uchar_overlay.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//===-- Including uchar.h in overlay mode ---------------------------------===//
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_HDR_UCHAR_OVERLAY_H
10+
#define LLVM_LIBC_HDR_UCHAR_OVERLAY_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
#error "This header should only be included in overlay mode"
14+
#endif
15+
16+
// Overlay mode
17+
18+
// glibc <uchar.h> header might provide extern inline definitions for few
19+
// functions, causing external alias errors. They are guarded by
20+
// `__USE_EXTERN_INLINES` macro. We temporarily disable `__USE_EXTERN_INLINES`
21+
// macro by defining `__NO_INLINE__` before including <uchar.h>.
22+
// And the same with `__USE_FORTIFY_LEVEL`, which will be temporarily disabled
23+
// with `_FORTIFY_SOURCE`.
24+
25+
#ifdef _FORTIFY_SOURCE
26+
#define LIBC_OLD_FORTIFY_SOURCE _FORTIFY_SOURCE
27+
#undef _FORTIFY_SOURCE
28+
#endif
29+
30+
#ifndef __NO_INLINE__
31+
#define __NO_INLINE__ 1
32+
#define LIBC_SET_NO_INLINE
33+
#endif
34+
35+
#ifdef __USE_EXTERN_INLINES
36+
#define LIBC_OLD_USE_EXTERN_INLINES
37+
#undef __USE_EXTERN_INLINES
38+
#endif
39+
40+
#ifdef __USE_FORTIFY_LEVEL
41+
#define LIBC_OLD_USE_FORTIFY_LEVEL __USE_FORTIFY_LEVEL
42+
#undef __USE_FORTIFY_LEVEL
43+
#define __USE_FORTIFY_LEVEL 0
44+
#endif
45+
46+
#include <uchar.h>
47+
48+
#ifdef LIBC_OLD_FORTIFY_SOURCE
49+
#define _FORTIFY_SOURCE LIBC_OLD_FORTIFY_SOURCE
50+
#undef LIBC_OLD_FORTIFY_SOURCE
51+
#endif
52+
53+
#ifdef LIBC_SET_NO_INLINE
54+
#undef __NO_INLINE__
55+
#undef LIBC_SET_NO_INLINE
56+
#endif
57+
58+
#ifdef LIBC_OLD_USE_FORTIFY_LEVEL
59+
#undef __USE_FORTIFY_LEVEL
60+
#define __USE_FORTIFY_LEVEL LIBC_OLD_USE_FORTIFY_LEVEL
61+
#undef LIBC_OLD_USE_FORTIFY_LEVEL
62+
#endif
63+
64+
#ifdef LIBC_OLD_USE_EXTERN_INLINES
65+
#define __USE_EXTERN_INLINES
66+
#undef LIBC_OLD_USE_EXTERN_INLINES
67+
#endif
68+
69+
#endif // LLVM_LIBC_HDR_UCHAR_OVERLAY_H
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
add_header_library(
2+
mbstate
3+
HDRS
4+
mbstate.h
5+
DEPENDS
6+
libc.hdr.types.char32_t
7+
)
8+
9+
add_object_library(
10+
character_converter
11+
HDRS
12+
character_converter.h
13+
SRCS
14+
character_converter.cpp
15+
DEPENDS
16+
libc.hdr.types.char8_t
17+
libc.hdr.types.char32_t
18+
.mbstate
19+
.utf_ret
20+
)
21+
22+
add_header_library(
23+
utf_ret
24+
HDRS
25+
utf_ret.h
26+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===-- Implementation of a class for conversion --------------------------===//
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+
#include "hdr/types/char32_t.h"
10+
#include "hdr/types/char8_t.h"
11+
#include "src/__support/wchar/mbstate.h"
12+
#include "src/__support/wchar/utf_ret.h"
13+
14+
#include "character_converter.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
namespace internal {
18+
19+
CharacterConverter::CharacterConverter(mbstate_t *mbstate) { state = mbstate; }
20+
21+
bool CharacterConverter::isComplete() {}
22+
23+
int CharacterConverter::push(char8_t utf8_byte) {}
24+
25+
int CharacterConverter::push(char32_t utf32) {}
26+
27+
utf_ret<char8_t> CharacterConverter::pop_utf8() {}
28+
29+
utf_ret<char32_t> CharacterConverter::pop_utf32() {}
30+
31+
} // namespace internal
32+
} // namespace LIBC_NAMESPACE_DECL
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- Definition of a class for mbstate_t and conversion -----*-- C++ -*-===//
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_SRC___SUPPORT_CHARACTER_CONVERTER_H
10+
#define LLVM_LIBC_SRC___SUPPORT_CHARACTER_CONVERTER_H
11+
12+
#include "hdr/types/char32_t.h"
13+
#include "hdr/types/char8_t.h"
14+
#include "src/__support/wchar/mbstate.h"
15+
#include "src/__support/wchar/utf_ret.h"
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
namespace internal {
19+
20+
class CharacterConverter {
21+
private:
22+
mbstate_t *state;
23+
24+
public:
25+
CharacterConverter(mbstate_t *mbstate);
26+
27+
bool isComplete();
28+
29+
int push(char8_t utf8_byte);
30+
int push(char32_t utf32);
31+
32+
utf_ret<char8_t> pop_utf8();
33+
utf_ret<char32_t> pop_utf32();
34+
};
35+
36+
} // namespace internal
37+
} // namespace LIBC_NAMESPACE_DECL
38+
39+
#endif // LLVM_LIBC_SRC___SUPPORT_CHARACTER_CONVERTER_H

libc/src/__support/wchar/mbstate.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Definition of mbstate-----------------------------------*-- C++ -*-===//
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_SRC___SUPPORT_MBSTATE_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MBSTATE_H
11+
12+
#include "hdr/types/char32_t.h"
13+
#include <stdint.h>
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
namespace internal {
17+
18+
struct mbstate {
19+
char32_t partial;
20+
uint8_t bits_processed;
21+
uint8_t total_bytes;
22+
};
23+
24+
} // namespace internal
25+
} // namespace LIBC_NAMESPACE_DECL
26+
27+
#endif // LLVM_LIBC_SRC___SUPPORT_MBSTATE_H

libc/src/__support/wchar/utf_ret.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Definition of utf_ret ----------------------------------*-- C++ -*-===//
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_SRC___SUPPORT_UTF_RET_H
10+
#define LLVM_LIBC_SRC___SUPPORT_UTF_RET_H
11+
12+
namespace LIBC_NAMESPACE_DECL {
13+
14+
template <typename T> struct utf_ret {
15+
T out;
16+
int error;
17+
};
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC___SUPPORT_UTF_RET_H

0 commit comments

Comments
 (0)