1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ******************************************************************************
5 *
6 *   Copyright (C) 1999-2015, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 ******************************************************************************
10 *   file name:  umachine.h
11 *   encoding:   UTF-8
12 *   tab size:   8 (not used)
13 *   indentation:4
14 *
15 *   created on: 1999sep13
16 *   created by: Markus W. Scherer
17 *
18 *   This file defines basic types and constants for ICU to be
19 *   platform-independent. umachine.h and utf.h are included into
20 *   utypes.h to provide all the general definitions for ICU.
21 *   All of these definitions used to be in utypes.h before
22 *   the UTF-handling macros made this unmaintainable.
23 */
24 
25 #ifndef __UMACHINE_H__
26 #define __UMACHINE_H__
27 /**
28  * \file
29  * \brief Basic types and constants for UTF
30  *
31  * <h2> Basic types and constants for UTF </h2>
32  *   This file defines basic types and constants for utf.h to be
33  *   platform-independent. umachine.h and utf.h are included into
34  *   utypes.h to provide all the general definitions for ICU.
35  *   All of these definitions used to be in utypes.h before
36  *   the UTF-handling macros made this unmaintainable.
37  *
38  */
39 /*==========================================================================*/
40 /* Include platform-dependent definitions                                   */
41 /* which are contained in the platform-specific file platform.h             */
42 /*==========================================================================*/
43 
44 #include "unicode/ptypes.h" /* platform.h is included in ptypes.h */
45 
46 /*
47  * ANSI C headers:
48  * stddef.h defines wchar_t
49  */
50 #include <stdbool.h>
51 #include <stddef.h>
52 
53 /*==========================================================================*/
54 /* For C wrappers, we use the symbol U_CAPI.                                */
55 /* This works properly if the includer is C or C++.                         */
56 /* Functions are declared   U_CAPI return-type U_EXPORT2 function-name()... */
57 /*==========================================================================*/
58 
59 /**
60  * \def U_CFUNC
61  * This is used in a declaration of a library private ICU C function.
62  * @stable ICU 2.4
63  */
64 
65 /**
66  * \def U_CDECL_BEGIN
67  * This is used to begin a declaration of a library private ICU C API.
68  * @stable ICU 2.4
69  */
70 
71 /**
72  * \def U_CDECL_END
73  * This is used to end a declaration of a library private ICU C API
74  * @stable ICU 2.4
75  */
76 
77 #ifdef __cplusplus
78 #   define U_CFUNC extern "C"
79 #   define U_CDECL_BEGIN extern "C" {
80 #   define U_CDECL_END   }
81 #else
82 #   define U_CFUNC extern
83 #   define U_CDECL_BEGIN
84 #   define U_CDECL_END
85 #endif
86 
87 #ifndef U_ATTRIBUTE_DEPRECATED
88 /**
89  * \def U_ATTRIBUTE_DEPRECATED
90  *  This is used for GCC specific attributes
91  * @internal
92  */
93 #if U_GCC_MAJOR_MINOR >= 302
94 #    define U_ATTRIBUTE_DEPRECATED __attribute__ ((deprecated))
95 /**
96  * \def U_ATTRIBUTE_DEPRECATED
97  * This is used for Visual C++ specific attributes
98  * @internal
99  */
100 #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
101 #    define U_ATTRIBUTE_DEPRECATED __declspec(deprecated)
102 #else
103 #    define U_ATTRIBUTE_DEPRECATED
104 #endif
105 #endif
106 
107 /** This is used to declare a function as a public ICU C API @stable ICU 2.0*/
108 #define U_CAPI U_CFUNC U_EXPORT
109 /** Obsolete/same as U_CAPI; was used to declare a function as a stable public ICU C API*/
110 #define U_STABLE U_CAPI
111 /** Obsolete/same as U_CAPI; was used to declare a function as a draft public ICU C API  */
112 #define U_DRAFT  U_CAPI
113 /** This is used to declare a function as a deprecated public ICU C API  */
114 #define U_DEPRECATED U_CAPI U_ATTRIBUTE_DEPRECATED
115 /** Obsolete/same as U_CAPI; was used to declare a function as an obsolete public ICU C API  */
116 #define U_OBSOLETE U_CAPI
117 /** Obsolete/same as U_CAPI; was used to declare a function as an internal ICU C API  */
118 #define U_INTERNAL U_CAPI
119 
120 /**
121  * \def U_OVERRIDE
122  * Defined to the C++11 "override" keyword if available.
123  * Denotes a class or member which is an override of the base class.
124  * May result in an error if it applied to something not an override.
125  * @internal
126  */
127 #ifndef U_OVERRIDE
128 #define U_OVERRIDE override
129 #endif
130 
131 /**
132  * \def U_FINAL
133  * Defined to the C++11 "final" keyword if available.
134  * Denotes a class or member which may not be overridden in subclasses.
135  * May result in an error if subclasses attempt to override.
136  * @internal
137  */
138 #if !defined(U_FINAL) || defined(U_IN_DOXYGEN)
139 #define U_FINAL final
140 #endif
141 
142 // Before ICU 65, function-like, multi-statement ICU macros were just defined as
143 // series of statements wrapped in { } blocks and the caller could choose to
144 // either treat them as if they were actual functions and end the invocation
145 // with a trailing ; creating an empty statement after the block or else omit
146 // this trailing ; using the knowledge that the macro would expand to { }.
147 //
148 // But doing so doesn't work well with macros that look like functions and
149 // compiler warnings about empty statements (ICU-20601) and ICU 65 therefore
150 // switches to the standard solution of wrapping such macros in do { } while.
151 //
152 // This will however break existing code that depends on being able to invoke
153 // these macros without a trailing ; so to be able to remain compatible with
154 // such code the wrapper is itself defined as macros so that it's possible to
155 // build ICU 65 and later with the old macro behaviour, like this:
156 //
157 // export CPPFLAGS='-DUPRV_BLOCK_MACRO_BEGIN="" -DUPRV_BLOCK_MACRO_END=""'
158 // runConfigureICU ...
159 //
160 
161 /**
162  * \def UPRV_BLOCK_MACRO_BEGIN
163  * Defined as the "do" keyword by default.
164  * @internal
165  */
166 #ifndef UPRV_BLOCK_MACRO_BEGIN
167 #define UPRV_BLOCK_MACRO_BEGIN do
168 #endif
169 
170 /**
171  * \def UPRV_BLOCK_MACRO_END
172  * Defined as "while (false)" by default.
173  * @internal
174  */
175 #ifndef UPRV_BLOCK_MACRO_END
176 #define UPRV_BLOCK_MACRO_END while (false)
177 #endif
178 
179 /*==========================================================================*/
180 /* limits for int32_t etc., like in POSIX inttypes.h                        */
181 /*==========================================================================*/
182 
183 #ifndef INT8_MIN
184 /** The smallest value an 8 bit signed integer can hold @stable ICU 2.0 */
185 #   define INT8_MIN        ((int8_t)(-128))
186 #endif
187 #ifndef INT16_MIN
188 /** The smallest value a 16 bit signed integer can hold @stable ICU 2.0 */
189 #   define INT16_MIN       ((int16_t)(-32767-1))
190 #endif
191 #ifndef INT32_MIN
192 /** The smallest value a 32 bit signed integer can hold @stable ICU 2.0 */
193 #   define INT32_MIN       ((int32_t)(-2147483647-1))
194 #endif
195 
196 #ifndef INT8_MAX
197 /** The largest value an 8 bit signed integer can hold @stable ICU 2.0 */
198 #   define INT8_MAX        ((int8_t)(127))
199 #endif
200 #ifndef INT16_MAX
201 /** The largest value a 16 bit signed integer can hold @stable ICU 2.0 */
202 #   define INT16_MAX       ((int16_t)(32767))
203 #endif
204 #ifndef INT32_MAX
205 /** The largest value a 32 bit signed integer can hold @stable ICU 2.0 */
206 #   define INT32_MAX       ((int32_t)(2147483647))
207 #endif
208 
209 #ifndef UINT8_MAX
210 /** The largest value an 8 bit unsigned integer can hold @stable ICU 2.0 */
211 #   define UINT8_MAX       ((uint8_t)(255U))
212 #endif
213 #ifndef UINT16_MAX
214 /** The largest value a 16 bit unsigned integer can hold @stable ICU 2.0 */
215 #   define UINT16_MAX      ((uint16_t)(65535U))
216 #endif
217 #ifndef UINT32_MAX
218 /** The largest value a 32 bit unsigned integer can hold @stable ICU 2.0 */
219 #   define UINT32_MAX      ((uint32_t)(4294967295U))
220 #endif
221 
222 #if defined(U_INT64_T_UNAVAILABLE)
223 # error int64_t is required for decimal format and rule-based number format.
224 #else
225 # ifndef INT64_C
226 /**
227  * Provides a platform independent way to specify a signed 64-bit integer constant.
228  * note: may be wrong for some 64 bit platforms - ensure your compiler provides INT64_C
229  * @stable ICU 2.8
230  */
231 #   define INT64_C(c) c ## LL
232 # endif
233 # ifndef UINT64_C
234 /**
235  * Provides a platform independent way to specify an unsigned 64-bit integer constant.
236  * note: may be wrong for some 64 bit platforms - ensure your compiler provides UINT64_C
237  * @stable ICU 2.8
238  */
239 #   define UINT64_C(c) c ## ULL
240 # endif
241 # ifndef U_INT64_MIN
242 /** The smallest value a 64 bit signed integer can hold @stable ICU 2.8 */
243 #     define U_INT64_MIN       ((int64_t)(INT64_C(-9223372036854775807)-1))
244 # endif
245 # ifndef U_INT64_MAX
246 /** The largest value a 64 bit signed integer can hold @stable ICU 2.8 */
247 #     define U_INT64_MAX       ((int64_t)(INT64_C(9223372036854775807)))
248 # endif
249 # ifndef U_UINT64_MAX
250 /** The largest value a 64 bit unsigned integer can hold @stable ICU 2.8 */
251 #     define U_UINT64_MAX      ((uint64_t)(UINT64_C(18446744073709551615)))
252 # endif
253 #endif
254 
255 /*==========================================================================*/
256 /* Boolean data type                                                        */
257 /*==========================================================================*/
258 
259 /**
260  * The ICU boolean type, a signed-byte integer.
261  * ICU-specific for historical reasons: The C and C++ standards used to not define type bool.
262  * Also provides a fixed type definition, as opposed to
263  * type bool whose details (e.g., sizeof) may vary by compiler and between C and C++.
264  *
265  * @stable ICU 2.0
266  */
267 typedef int8_t UBool;
268 
269 /**
270  * \def U_DEFINE_FALSE_AND_TRUE
271  * Normally turns off defining macros FALSE=0 & TRUE=1 in public ICU headers.
272  * These obsolete macros sometimes break compilation of other code that
273  * defines enum constants or similar with these names.
274  * C++ has long defined bool/false/true.
275  * C99 also added definitions for these, although as macros; see stdbool.h.
276  *
277  * You may transitionally define U_DEFINE_FALSE_AND_TRUE=1 if you need time to migrate code.
278  *
279  * @internal ICU 68
280  */
281 #ifdef U_DEFINE_FALSE_AND_TRUE
282     // Use the predefined value.
283 #elif defined(U_COMBINED_IMPLEMENTATION) || \
284         defined(U_COMMON_IMPLEMENTATION) || defined(U_I18N_IMPLEMENTATION) || \
285         defined(U_IO_IMPLEMENTATION) || defined(U_LAYOUTEX_IMPLEMENTATION) || \
286         defined(U_TOOLUTIL_IMPLEMENTATION)
287     // Inside ICU: Keep FALSE & TRUE available.
288 #   define U_DEFINE_FALSE_AND_TRUE 1
289 #else
290     // Outside ICU: Avoid collision with non-macro definitions of FALSE & TRUE.
291 #   define U_DEFINE_FALSE_AND_TRUE 1
292 #endif
293 
294 #if U_DEFINE_FALSE_AND_TRUE || defined(U_IN_DOXYGEN)
295 #ifndef TRUE
296 /**
297  * The TRUE value of a UBool.
298  *
299  * @deprecated ICU 68 Use standard "true" instead.
300  */
301 #   define TRUE  1
302 #endif
303 #ifndef FALSE
304 /**
305  * The FALSE value of a UBool.
306  *
307  * @deprecated ICU 68 Use standard "false" instead.
308  */
309 #   define FALSE 0
310 #endif
311 #endif  // U_DEFINE_FALSE_AND_TRUE
312 
313 /*==========================================================================*/
314 /* Unicode data types                                                       */
315 /*==========================================================================*/
316 
317 /* wchar_t-related definitions -------------------------------------------- */
318 
319 /*
320  * \def U_WCHAR_IS_UTF16
321  * Defined if wchar_t uses UTF-16.
322  *
323  * @stable ICU 2.0
324  */
325 /*
326  * \def U_WCHAR_IS_UTF32
327  * Defined if wchar_t uses UTF-32.
328  *
329  * @stable ICU 2.0
330  */
331 #if !defined(U_WCHAR_IS_UTF16) && !defined(U_WCHAR_IS_UTF32)
332 #   ifdef __STDC_ISO_10646__
333 #       if (U_SIZEOF_WCHAR_T==2)
334 #           define U_WCHAR_IS_UTF16
335 #       elif (U_SIZEOF_WCHAR_T==4)
336 #           define  U_WCHAR_IS_UTF32
337 #       endif
338 #   elif defined __UCS2__
339 #       if (U_PF_OS390 <= U_PLATFORM && U_PLATFORM <= U_PF_OS400) && (U_SIZEOF_WCHAR_T==2)
340 #           define U_WCHAR_IS_UTF16
341 #       endif
342 #   elif defined(__UCS4__) || (U_PLATFORM == U_PF_OS400 && defined(__UTF32__))
343 #       if (U_SIZEOF_WCHAR_T==4)
344 #           define U_WCHAR_IS_UTF32
345 #       endif
346 #   elif U_PLATFORM_IS_DARWIN_BASED || (U_SIZEOF_WCHAR_T==4 && U_PLATFORM_IS_LINUX_BASED)
347 #       define U_WCHAR_IS_UTF32
348 #   elif U_PLATFORM_HAS_WIN32_API
349 #       define U_WCHAR_IS_UTF16
350 #   endif
351 #endif
352 
353 /* UChar and UChar32 definitions -------------------------------------------- */
354 
355 /** Number of bytes in a UChar. @stable ICU 2.0 */
356 #define U_SIZEOF_UCHAR 2
357 
358 /**
359  * \def U_CHAR16_IS_TYPEDEF
360  * If 1, then char16_t is a typedef and not a real type (yet)
361  * @internal
362  */
363 #if (U_PLATFORM == U_PF_AIX) && defined(__cplusplus) &&(U_CPLUSPLUS_VERSION < 11)
364 // for AIX, uchar.h needs to be included
365 # include <uchar.h>
366 # define U_CHAR16_IS_TYPEDEF 1
367 #elif defined(_MSC_VER) && (_MSC_VER < 1900)
368 // Versions of Visual Studio/MSVC below 2015 do not support char16_t as a real type,
369 // and instead use a typedef.  https://msdn.microsoft.com/library/bb531344.aspx
370 # define U_CHAR16_IS_TYPEDEF 1
371 #else
372 # define U_CHAR16_IS_TYPEDEF 0
373 #endif
374 /**
375  * \var UChar
376  *
377  * The base type for UTF-16 code units and pointers.
378  * Unsigned 16-bit integer.
379  * Starting with ICU 59, C++ API uses char16_t directly, while C API continues to use UChar.
380  *
381  * UChar is configurable by defining the macro UCHAR_TYPE
382  * on the preprocessor or compiler command line:
383  * -DUCHAR_TYPE=uint16_t or -DUCHAR_TYPE=wchar_t (if U_SIZEOF_WCHAR_T==2) etc.
384  * (The UCHAR_TYPE can also be \#defined earlier in this file, for outside the ICU library code.)
385  * This is for transitional use from application code that uses uint16_t or wchar_t for UTF-16.
386  *
387  * The default is UChar=char16_t.
388  *
389  * C++11 defines char16_t as bit-compatible with uint16_t, but as a distinct type.
390  *
391  * In C, char16_t is a simple typedef of uint_least16_t.
392  * ICU requires uint_least16_t=uint16_t for data memory mapping.
393  * On macOS, char16_t is not available because the uchar.h standard header is missing.
394  *
395  * @stable ICU 4.4
396  */
397 
398 #if 1
399     // #if 1 is normal. UChar defaults to char16_t in C++.
400     // For configuration testing of UChar=uint16_t temporarily change this to #if 0.
401     // The intltest Makefile #defines UCHAR_TYPE=char16_t,
402     // so we only #define it to uint16_t if it is undefined so far.
403 #elif !defined(UCHAR_TYPE)
404 #   define UCHAR_TYPE uint16_t
405 #endif
406 
407 #if defined(U_COMBINED_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION) || \
408         defined(U_I18N_IMPLEMENTATION) || defined(U_IO_IMPLEMENTATION)
409     // Inside the ICU library code, never configurable.
410     typedef char16_t UChar;
411 #elif defined(UCHAR_TYPE)
412     typedef UCHAR_TYPE UChar;
413 #elif (U_CPLUSPLUS_VERSION >= 11)
414     typedef char16_t UChar;
415 #else
416     typedef uint16_t UChar;
417 #endif
418 
419 /**
420  * \var OldUChar
421  * Default ICU 58 definition of UChar.
422  * A base type for UTF-16 code units and pointers.
423  * Unsigned 16-bit integer.
424  *
425  * Define OldUChar to be wchar_t if that is 16 bits wide.
426  * If wchar_t is not 16 bits wide, then define UChar to be uint16_t.
427  *
428  * This makes the definition of OldUChar platform-dependent
429  * but allows direct string type compatibility with platforms with
430  * 16-bit wchar_t types.
431  *
432  * This is how UChar was defined in ICU 58, for transition convenience.
433  * Exception: ICU 58 UChar was defined to UCHAR_TYPE if that macro was defined.
434  * The current UChar responds to UCHAR_TYPE but OldUChar does not.
435  *
436  * @stable ICU 59
437  */
438 #if U_SIZEOF_WCHAR_T==2
439     typedef wchar_t OldUChar;
440 #elif defined(__CHAR16_TYPE__)
441     typedef __CHAR16_TYPE__ OldUChar;
442 #else
443     typedef uint16_t OldUChar;
444 #endif
445 
446 /**
447  * Define UChar32 as a type for single Unicode code points.
448  * UChar32 is a signed 32-bit integer (same as int32_t).
449  *
450  * The Unicode code point range is 0..0x10ffff.
451  * All other values (negative or >=0x110000) are illegal as Unicode code points.
452  * They may be used as sentinel values to indicate "done", "error"
453  * or similar non-code point conditions.
454  *
455  * Before ICU 2.4 (Jitterbug 2146), UChar32 was defined
456  * to be wchar_t if that is 32 bits wide (wchar_t may be signed or unsigned)
457  * or else to be uint32_t.
458  * That is, the definition of UChar32 was platform-dependent.
459  *
460  * @see U_SENTINEL
461  * @stable ICU 2.4
462  */
463 typedef int32_t UChar32;
464 
465 /**
466  * This value is intended for sentinel values for APIs that
467  * (take or) return single code points (UChar32).
468  * It is outside of the Unicode code point range 0..0x10ffff.
469  *
470  * For example, a "done" or "error" value in a new API
471  * could be indicated with U_SENTINEL.
472  *
473  * ICU APIs designed before ICU 2.4 usually define service-specific "done"
474  * values, mostly 0xffff.
475  * Those may need to be distinguished from
476  * actual U+ffff text contents by calling functions like
477  * CharacterIterator::hasNext() or UnicodeString::length().
478  *
479  * @return -1
480  * @see UChar32
481  * @stable ICU 2.4
482  */
483 #define U_SENTINEL (-1)
484 
485 #endif
486