]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/src/c++98/locale.cc
aarch64: Add codegen support for AdvSIMD faminmax
[thirdparty/gcc.git] / libstdc++-v3 / src / c++98 / locale.cc
1 // Copyright (C) 1997-2024 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // Under Section 7 of GPL version 3, you are granted additional
15 // permissions described in the GCC Runtime Library Exception, version
16 // 3.1, as published by the Free Software Foundation.
17
18 // You should have received a copy of the GNU General Public License and
19 // a copy of the GCC Runtime Library Exception along with this program;
20 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
21 // <http://www.gnu.org/licenses/>.
22
23 #define _GLIBCXX_USE_CXX11_ABI 1
24 #include <clocale>
25 #include <cstring>
26 #include <cctype>
27 #include <cwctype> // For towupper, etc.
28 #include <locale>
29 #include <ext/concurrence.h>
30
31 namespace
32 {
33 __gnu_cxx::__mutex&
34 get_locale_cache_mutex()
35 {
36 static __gnu_cxx::__mutex locale_cache_mutex;
37 return locale_cache_mutex;
38 }
39 } // anonymous namespace
40
41 // XXX GLIBCXX_ABI Deprecated
42 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
43 # define _GLIBCXX_LOC_ID(mangled) extern std::locale::id mangled
44 _GLIBCXX_LOC_ID (_ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
45 _GLIBCXX_LOC_ID (_ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
46 _GLIBCXX_LOC_ID (_ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
47 _GLIBCXX_LOC_ID (_ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
48 # ifdef _GLIBCXX_USE_WCHAR_T
49 _GLIBCXX_LOC_ID (_ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
50 _GLIBCXX_LOC_ID (_ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
51 _GLIBCXX_LOC_ID (_ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
52 _GLIBCXX_LOC_ID (_ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
53 # endif
54 #endif
55
56 namespace std _GLIBCXX_VISIBILITY(default)
57 {
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
59
60 // Definitions for static const data members of locale.
61 const locale::category locale::none;
62 const locale::category locale::ctype;
63 const locale::category locale::numeric;
64 const locale::category locale::collate;
65 const locale::category locale::time;
66 const locale::category locale::monetary;
67 const locale::category locale::messages;
68 const locale::category locale::all;
69
70 // These are no longer exported.
71 locale::_Impl* locale::_S_classic;
72 locale::_Impl* locale::_S_global;
73
74 #ifdef __GTHREADS
75 __gthread_once_t locale::_S_once = __GTHREAD_ONCE_INIT;
76 #endif
77
78 locale::locale(const locale& __other) throw()
79 : _M_impl(__other._M_impl)
80 {
81 if (_M_impl != _S_classic)
82 _M_impl->_M_add_reference();
83 }
84
85 // This is used to initialize global and classic locales, and
86 // assumes that the _Impl objects are constructed correctly.
87 // The lack of a reference increment is intentional.
88 locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
89 { }
90
91 locale::~locale() throw()
92 {
93 if (_M_impl != _S_classic)
94 _M_impl->_M_remove_reference();
95 }
96
97 bool
98 locale::operator==(const locale& __rhs) const throw()
99 {
100 // Deal first with the common cases, fast to process: refcopies,
101 // unnamed (i.e., !_M_names[0]), "simple" (!_M_names[1] => all the
102 // categories same name, i.e., _M_names[0]). Otherwise fall back
103 // to the general locale::name().
104 bool __ret;
105 if (_M_impl == __rhs._M_impl)
106 __ret = true;
107 else if (!_M_impl->_M_names[0] || !__rhs._M_impl->_M_names[0]
108 || std::strcmp(_M_impl->_M_names[0],
109 __rhs._M_impl->_M_names[0]) != 0)
110 __ret = false;
111 else if (!_M_impl->_M_names[1] && !__rhs._M_impl->_M_names[1])
112 __ret = true;
113 else
114 __ret = this->name() == __rhs.name();
115 return __ret;
116 }
117
118 const locale&
119 locale::operator=(const locale& __other) throw()
120 {
121 if (__other._M_impl != _S_classic)
122 __other._M_impl->_M_add_reference();
123 if (_M_impl != _S_classic)
124 _M_impl->_M_remove_reference();
125 _M_impl = __other._M_impl;
126 return *this;
127 }
128
129 _GLIBCXX_DEFAULT_ABI_TAG
130 string
131 locale::name() const
132 {
133 string __ret;
134 if (!_M_impl->_M_names[0])
135 __ret = '*';
136 else if (_M_impl->_M_check_same_name())
137 __ret = _M_impl->_M_names[0];
138 else
139 {
140 __ret.reserve(128);
141 __ret += _S_categories[0];
142 __ret += '=';
143 __ret += _M_impl->_M_names[0];
144 for (size_t __i = 1; __i < _S_categories_size; ++__i)
145 {
146 __ret += ';';
147 __ret += _S_categories[__i];
148 __ret += '=';
149 __ret += _M_impl->_M_names[__i];
150 }
151 }
152 return __ret;
153 }
154
155 locale::category
156 locale::_S_normalize_category(category __cat)
157 {
158 int __ret = 0;
159 if (__cat == none || ((__cat & all) && !(__cat & ~all)))
160 __ret = __cat;
161 else
162 {
163 // NB: May be a C-style "LC_ALL" category; convert.
164 switch (__cat)
165 {
166 case LC_COLLATE:
167 __ret = collate;
168 break;
169 case LC_CTYPE:
170 __ret = ctype;
171 break;
172 case LC_MONETARY:
173 __ret = monetary;
174 break;
175 case LC_NUMERIC:
176 __ret = numeric;
177 break;
178 case LC_TIME:
179 __ret = time;
180 break;
181 #ifdef _GLIBCXX_HAVE_LC_MESSAGES
182 case LC_MESSAGES:
183 __ret = messages;
184 break;
185 #endif
186 case LC_ALL:
187 __ret = all;
188 break;
189 default:
190 __throw_runtime_error(__N("locale::_S_normalize_category "
191 "category not found"));
192 }
193 }
194 return __ret;
195 }
196
197 // locale::facet
198 __c_locale locale::facet::_S_c_locale;
199
200 const char locale::facet::_S_c_name[2] = "C";
201
202 #ifdef __GTHREADS
203 __gthread_once_t locale::facet::_S_once = __GTHREAD_ONCE_INIT;
204 #endif
205
206 void
207 locale::facet::_S_initialize_once()
208 {
209 // Need to check this because we could get called once from
210 // _S_get_c_locale() when the program is single-threaded, and then again
211 // (via __gthread_once) when it's multi-threaded.
212 if (_S_c_locale)
213 return;
214
215 // Initialize the underlying locale model.
216 _S_create_c_locale(_S_c_locale, _S_c_name);
217 }
218
219 __c_locale
220 locale::facet::_S_get_c_locale()
221 {
222 #ifdef __GTHREADS
223 if (__gthread_active_p())
224 __gthread_once(&_S_once, _S_initialize_once);
225 #endif
226 if (__builtin_expect (!_S_c_locale, 0))
227 _S_initialize_once();
228 return _S_c_locale;
229 }
230
231 const char*
232 locale::facet::_S_get_c_name() throw()
233 { return _S_c_name; }
234
235 locale::facet::
236 ~facet() { }
237
238 // locale::_Impl
239 locale::_Impl::
240 ~_Impl() throw()
241 {
242 if (_M_facets)
243 for (size_t __i = 0; __i < _M_facets_size; ++__i)
244 if (_M_facets[__i])
245 _M_facets[__i]->_M_remove_reference();
246 delete [] _M_facets;
247
248 if (_M_caches)
249 for (size_t __i = 0; __i < _M_facets_size; ++__i)
250 if (_M_caches[__i])
251 _M_caches[__i]->_M_remove_reference();
252 delete [] _M_caches;
253
254 if (_M_names)
255 for (size_t __i = 0; __i < _S_categories_size; ++__i)
256 delete [] _M_names[__i];
257 delete [] _M_names;
258 }
259
260 // Clone existing _Impl object.
261 locale::_Impl::
262 _Impl(const _Impl& __imp, size_t __refs)
263 : _M_refcount(__refs), _M_facets(0), _M_facets_size(__imp._M_facets_size),
264 _M_caches(0), _M_names(0)
265 {
266 __try
267 {
268 _M_facets = new const facet*[_M_facets_size];
269 for (size_t __i = 0; __i < _M_facets_size; ++__i)
270 {
271 _M_facets[__i] = __imp._M_facets[__i];
272 if (_M_facets[__i])
273 _M_facets[__i]->_M_add_reference();
274 }
275 _M_caches = new const facet*[_M_facets_size];
276 for (size_t __j = 0; __j < _M_facets_size; ++__j)
277 {
278 _M_caches[__j] = __imp._M_caches[__j];
279 if (_M_caches[__j])
280 _M_caches[__j]->_M_add_reference();
281 }
282 _M_names = new char*[_S_categories_size];
283 for (size_t __k = 0; __k < _S_categories_size; ++__k)
284 _M_names[__k] = 0;
285
286 // Name the categories.
287 for (size_t __l = 0; (__l < _S_categories_size
288 && __imp._M_names[__l]); ++__l)
289 {
290 const size_t __len = std::strlen(__imp._M_names[__l]) + 1;
291 _M_names[__l] = new char[__len];
292 std::memcpy(_M_names[__l], __imp._M_names[__l], __len);
293 }
294 }
295 __catch(...)
296 {
297 this->~_Impl();
298 __throw_exception_again;
299 }
300 }
301
302 void
303 locale::_Impl::
304 _M_replace_category(const _Impl* __imp,
305 const locale::id* const* __idpp)
306 {
307 for (; *__idpp; ++__idpp)
308 _M_replace_facet(__imp, *__idpp);
309 }
310
311 void
312 locale::_Impl::
313 _M_replace_facet(const _Impl* __imp, const locale::id* __idp)
314 {
315 size_t __index = __idp->_M_id();
316 if ((__index > (__imp->_M_facets_size - 1))
317 || !__imp->_M_facets[__index])
318 __throw_runtime_error(__N("locale::_Impl::_M_replace_facet"));
319 _M_install_facet(__idp, __imp->_M_facets[__index]);
320 }
321
322 void
323 locale::_Impl::
324 _M_install_facet(const locale::id* __idp, const facet* __fp)
325 {
326 if (__builtin_expect(__fp != 0, 1))
327 {
328 size_t __index = __idp->_M_id();
329
330 // Check size of facet vector to ensure adequate room.
331 if (__index > _M_facets_size - 1)
332 {
333 const size_t __new_size = __index + 4;
334
335 // New facet array.
336 const facet** __oldf = _M_facets;
337 const facet** __newf;
338 __newf = new const facet*[__new_size];
339 for (size_t __i = 0; __i < _M_facets_size; ++__i)
340 __newf[__i] = _M_facets[__i];
341 for (size_t __l = _M_facets_size; __l < __new_size; ++__l)
342 __newf[__l] = 0;
343
344 // New cache array.
345 const facet** __oldc = _M_caches;
346 const facet** __newc;
347 __try
348 {
349 __newc = new const facet*[__new_size];
350 }
351 __catch(...)
352 {
353 delete [] __newf;
354 __throw_exception_again;
355 }
356 for (size_t __j = 0; __j < _M_facets_size; ++__j)
357 __newc[__j] = _M_caches[__j];
358 for (size_t __k = _M_facets_size; __k < __new_size; ++__k)
359 __newc[__k] = 0;
360
361 _M_facets_size = __new_size;
362 _M_facets = __newf;
363 _M_caches = __newc;
364 delete [] __oldf;
365 delete [] __oldc;
366 }
367
368 __fp->_M_add_reference();
369 const facet*& __fpr = _M_facets[__index];
370 if (__fpr)
371 {
372 #if _GLIBCXX_USE_DUAL_ABI
373 // If this is a twinned facet replace its twin with a shim.
374 for (const id* const* p = _S_twinned_facets; *p != 0; p += 2)
375 {
376 if (p[0]->_M_id() == __index)
377 {
378 // replacing the old ABI facet, also replace new ABI twin
379 const facet*& __fpr2 = _M_facets[p[1]->_M_id()];
380 if (__fpr2)
381 {
382 const facet* __fp2 = __fp->_M_sso_shim(p[1]);
383 __fp2->_M_add_reference();
384 __fpr2->_M_remove_reference();
385 __fpr2 = __fp2;
386 }
387 break;
388 }
389 else if (p[1]->_M_id() == __index)
390 {
391 // replacing the new ABI facet, also replace old ABI twin
392 const facet*& __fpr2 = _M_facets[p[0]->_M_id()];
393 if (__fpr2)
394 {
395 const facet* __fp2 = __fp->_M_cow_shim(p[0]);
396 __fp2->_M_add_reference();
397 __fpr2->_M_remove_reference();
398 __fpr2 = __fp2;
399 }
400 break;
401 }
402 }
403 #endif
404 // Replacing an existing facet. Order matters.
405 __fpr->_M_remove_reference();
406 __fpr = __fp;
407 }
408 else
409 {
410 // Installing a newly created facet into an empty
411 // _M_facets container, say a newly-constructed,
412 // swanky-fresh _Impl.
413 _M_facets[__index] = __fp;
414 }
415
416 // Ideally, it would be nice to only remove the caches that
417 // are now incorrect. However, some of the caches depend on
418 // multiple facets, and we only know about one facet
419 // here. It's no great loss: the first use of the new facet
420 // will create a new, correctly cached facet anyway.
421 for (size_t __i = 0; __i < _M_facets_size; ++__i)
422 {
423 const facet* __cpr = _M_caches[__i];
424 if (__cpr)
425 {
426 __cpr->_M_remove_reference();
427 _M_caches[__i] = 0;
428 }
429 }
430 }
431 }
432
433 void
434 locale::_Impl::
435 _M_install_cache(const facet* __cache, size_t __index)
436 {
437 __gnu_cxx::__scoped_lock sentry(get_locale_cache_mutex());
438 #if _GLIBCXX_USE_DUAL_ABI
439 // If this cache is for one of the facets that is instantiated twice,
440 // for old and new std::string ABI, install it in both slots.
441 size_t __index2 = -1;
442 for (const id* const* p = _S_twinned_facets; *p != 0; p += 2)
443 {
444 if (p[0]->_M_id() == __index)
445 {
446 __index2 = p[1]->_M_id();
447 break;
448 }
449 else if (p[1]->_M_id() == __index)
450 {
451 __index2 = __index;
452 __index = p[0]->_M_id();
453 break;
454 }
455 }
456 #endif
457 if (_M_caches[__index] != 0)
458 {
459 // Some other thread got in first.
460 delete __cache;
461 }
462 else
463 {
464 __cache->_M_add_reference();
465 _M_caches[__index] = __cache;
466 #if _GLIBCXX_USE_DUAL_ABI
467 if (__index2 != size_t(-1))
468 {
469 __cache->_M_add_reference();
470 _M_caches[__index2] = __cache;
471 }
472 #endif
473 }
474 }
475
476 // locale::id
477 // Definitions for static const data members of locale::id
478 _Atomic_word locale::id::_S_refcount; // init'd to 0 by linker
479
480 // XXX GLIBCXX_ABI Deprecated
481 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
482 namespace {
483 inline locale::id*
484 find_ldbl_sync_facet(const locale::id* __idp)
485 {
486 # define _GLIBCXX_SYNC_ID(facet, mangled) \
487 if (__idp == &::mangled) \
488 return &facet::id
489
490 _GLIBCXX_SYNC_ID (num_get<char>, _ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
491 _GLIBCXX_SYNC_ID (num_put<char>, _ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
492 _GLIBCXX_SYNC_ID (money_get<char>, _ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
493 _GLIBCXX_SYNC_ID (money_put<char>, _ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
494 # ifdef _GLIBCXX_USE_WCHAR_T
495 _GLIBCXX_SYNC_ID (num_get<wchar_t>, _ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
496 _GLIBCXX_SYNC_ID (num_put<wchar_t>, _ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
497 _GLIBCXX_SYNC_ID (money_get<wchar_t>, _ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
498 _GLIBCXX_SYNC_ID (money_put<wchar_t>, _ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
499 # endif
500 return 0;
501 }
502 } // namespace
503 #endif
504
505 size_t
506 locale::id::_M_id() const throw()
507 {
508 if (!_M_index)
509 {
510 // XXX GLIBCXX_ABI Deprecated
511 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
512 if (locale::id* f = find_ldbl_sync_facet(this))
513 {
514 const size_t sync_id = f->_M_id();
515 _M_index = 1 + sync_id;
516 return sync_id;
517 }
518 #endif
519
520 #ifdef __GTHREADS
521 if (!__gnu_cxx::__is_single_threaded())
522 {
523 if (__atomic_always_lock_free(sizeof(_M_index), &_M_index))
524 {
525 const _Atomic_word next
526 = 1 + __gnu_cxx::__exchange_and_add(&_S_refcount, 1);
527 size_t expected = 0;
528 __atomic_compare_exchange_n(&_M_index, &expected, next,
529 /* weak = */ false,
530 /* success = */ __ATOMIC_ACQ_REL,
531 /* failure = */ __ATOMIC_ACQUIRE);
532 }
533 else
534 {
535 static __gnu_cxx::__mutex m;
536 __gnu_cxx::__scoped_lock l(m);
537 if (!_M_index)
538 _M_index = ++_S_refcount;
539 }
540 }
541 else
542 #endif
543 _M_index = ++_S_refcount; // single-threaded case
544 }
545 return _M_index - 1;
546 }
547
548 _GLIBCXX_END_NAMESPACE_VERSION
549 } // namespace