172445Sassar//===----------------------------------------------------------------------===//
2233294Sstas//
372445Sassar// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
472445Sassar// See https://llvm.org/LICENSE.txt for license information.
572445Sassar// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
672445Sassar//
772445Sassar//===----------------------------------------------------------------------===//
872445Sassar
972445Sassar#include <__locale>
1072445Sassar#include <__std_stream>
1172445Sassar#include <new>
1272445Sassar#include <string>
1372445Sassar
1472445Sassar#define _str(s) #s
1572445Sassar#define str(s) _str(s)
1672445Sassar#define _LIBCPP_ABI_NAMESPACE_STR str(_LIBCPP_ABI_NAMESPACE)
1772445Sassar
1872445Sassar_LIBCPP_BEGIN_NAMESPACE_STD
1972445Sassar
2072445Sassar_ALIGNAS_TYPE (istream) _LIBCPP_FUNC_VIS char cin[sizeof(istream)]
2172445Sassar#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
2272445Sassar__asm__("?cin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
2372445Sassar#endif
2472445Sassar;
2572445Sassar_ALIGNAS_TYPE (__stdinbuf<char> ) static char __cin[sizeof(__stdinbuf <char>)];
2672445Sassarstatic mbstate_t mb_cin;
2772445Sassar
2872445Sassar#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2972445Sassar_ALIGNAS_TYPE (wistream) _LIBCPP_FUNC_VIS char wcin[sizeof(wistream)]
3072445Sassar#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
3172445Sassar__asm__("?wcin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
3272445Sassar#endif
3372445Sassar;
34233294Sstas_ALIGNAS_TYPE (__stdinbuf<wchar_t> ) static char __wcin[sizeof(__stdinbuf <wchar_t>)];
3572445Sassarstatic mbstate_t mb_wcin;
3672445Sassar#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
3772445Sassar
3872445Sassar_ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char cout[sizeof(ostream)]
3972445Sassar#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
4072445Sassar__asm__("?cout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
4172445Sassar#endif
4272445Sassar;
4372445Sassar_ALIGNAS_TYPE (__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)];
4472445Sassarstatic mbstate_t mb_cout;
4572445Sassar
4672445Sassar#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4772445Sassar_ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcout[sizeof(wostream)]
4872445Sassar#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
49178825Sdfr__asm__("?wcout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
50178825Sdfr#endif
51233294Sstas;
52233294Sstas_ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)];
53178825Sdfrstatic mbstate_t mb_wcout;
54178825Sdfr#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
55233294Sstas
56178825Sdfr_ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char cerr[sizeof(ostream)]
57178825Sdfr#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
58178825Sdfr__asm__("?cerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
59233294Sstas#endif
60178825Sdfr;
6172445Sassar_ALIGNAS_TYPE (__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)];
62178825Sdfrstatic mbstate_t mb_cerr;
63233294Sstas
64178825Sdfr#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
6572445Sassar_ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcerr[sizeof(wostream)]
66178825Sdfr#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
67233294Sstas__asm__("?wcerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
68178825Sdfr#endif
6972445Sassar;
70178825Sdfr_ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)];
71233294Sstasstatic mbstate_t mb_wcerr;
72178825Sdfr#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
7372445Sassar
74233294Sstas_ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char clog[sizeof(ostream)]
75178825Sdfr#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
7672445Sassar__asm__("?clog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
77178825Sdfr#endif
78233294Sstas;
79178825Sdfr
8072445Sassar#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
81178825Sdfr_ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wclog[sizeof(wostream)]
82233294Sstas#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
83178825Sdfr__asm__("?wclog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
8472445Sassar#endif
85178825Sdfr;
86233294Sstas#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
87178825Sdfr
8872445Sassar// Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority
8972445Sassar// attribute with a value that's reserved for the implementation (we're the implementation).
9072445Sassar#include "iostream_init.h"
91
92// On Windows the TLS storage for locales needs to be initialized before we create
93// the standard streams, otherwise it may not be alive during program termination
94// when we flush the streams.
95static void force_locale_initialization() {
96#if defined(_LIBCPP_MSVCRT_LIKE)
97  static bool once = []() {
98    auto loc = newlocale(LC_ALL_MASK, "C", 0);
99    {
100        __libcpp_locale_guard g(loc); // forces initialization of locale TLS
101        ((void)g);
102    }
103    freelocale(loc);
104    return true;
105  }();
106  ((void)once);
107#endif
108}
109
110class DoIOSInit {
111public:
112    DoIOSInit();
113    ~DoIOSInit();
114};
115
116DoIOSInit::DoIOSInit()
117{
118    force_locale_initialization();
119
120    istream* cin_ptr  = ::new(cin)  istream(::new(__cin)  __stdinbuf <char>(stdin, &mb_cin));
121    ostream* cout_ptr = ::new(cout) ostream(::new(__cout) __stdoutbuf<char>(stdout, &mb_cout));
122    ostream* cerr_ptr = ::new(cerr) ostream(::new(__cerr) __stdoutbuf<char>(stderr, &mb_cerr));
123                        ::new(clog) ostream(cerr_ptr->rdbuf());
124    cin_ptr->tie(cout_ptr);
125    _VSTD::unitbuf(*cerr_ptr);
126    cerr_ptr->tie(cout_ptr);
127
128#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
129    wistream* wcin_ptr  = ::new(wcin)  wistream(::new(__wcin)  __stdinbuf <wchar_t>(stdin, &mb_wcin));
130    wostream* wcout_ptr = ::new(wcout) wostream(::new(__wcout) __stdoutbuf<wchar_t>(stdout, &mb_wcout));
131    wostream* wcerr_ptr = ::new(wcerr) wostream(::new(__wcerr) __stdoutbuf<wchar_t>(stderr, &mb_wcerr));
132                          ::new(wclog) wostream(wcerr_ptr->rdbuf());
133
134    wcin_ptr->tie(wcout_ptr);
135    _VSTD::unitbuf(*wcerr_ptr);
136    wcerr_ptr->tie(wcout_ptr);
137#endif
138}
139
140DoIOSInit::~DoIOSInit()
141{
142    ostream* cout_ptr = reinterpret_cast<ostream*>(cout);
143    cout_ptr->flush();
144    ostream* clog_ptr = reinterpret_cast<ostream*>(clog);
145    clog_ptr->flush();
146
147#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
148    wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout);
149    wcout_ptr->flush();
150    wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog);
151    wclog_ptr->flush();
152#endif
153}
154
155ios_base::Init::Init()
156{
157    static DoIOSInit init_the_streams; // gets initialized once
158}
159
160ios_base::Init::~Init()
161{
162}
163
164_LIBCPP_END_NAMESPACE_STD
165