xref: /aosp_15_r20/external/libcxx/src/iostream.cpp (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard Worker //===------------------------ iostream.cpp --------------------------------===//
2*58b9f456SAndroid Build Coastguard Worker //
3*58b9f456SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*58b9f456SAndroid Build Coastguard Worker //
5*58b9f456SAndroid Build Coastguard Worker // This file is dual licensed under the MIT and the University of Illinois Open
6*58b9f456SAndroid Build Coastguard Worker // Source Licenses. See LICENSE.TXT for details.
7*58b9f456SAndroid Build Coastguard Worker //
8*58b9f456SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*58b9f456SAndroid Build Coastguard Worker 
10*58b9f456SAndroid Build Coastguard Worker #include "__std_stream"
11*58b9f456SAndroid Build Coastguard Worker #include "string"
12*58b9f456SAndroid Build Coastguard Worker #include "new"
13*58b9f456SAndroid Build Coastguard Worker 
14*58b9f456SAndroid Build Coastguard Worker #define _str(s) #s
15*58b9f456SAndroid Build Coastguard Worker #define str(s) _str(s)
16*58b9f456SAndroid Build Coastguard Worker #define _LIBCPP_ABI_NAMESPACE_STR str(_LIBCPP_ABI_NAMESPACE)
17*58b9f456SAndroid Build Coastguard Worker 
18*58b9f456SAndroid Build Coastguard Worker _LIBCPP_BEGIN_NAMESPACE_STD
19*58b9f456SAndroid Build Coastguard Worker 
20*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_HAS_NO_STDIN
21*58b9f456SAndroid Build Coastguard Worker _ALIGNAS_TYPE (istream) _LIBCPP_FUNC_VIS char cin[sizeof(istream)]
22*58b9f456SAndroid Build Coastguard Worker #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
23*58b9f456SAndroid Build Coastguard Worker __asm__("?cin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
24*58b9f456SAndroid Build Coastguard Worker #endif
25*58b9f456SAndroid Build Coastguard Worker ;
26*58b9f456SAndroid Build Coastguard Worker _ALIGNAS_TYPE (__stdinbuf<char> ) static char __cin[sizeof(__stdinbuf <char>)];
27*58b9f456SAndroid Build Coastguard Worker static mbstate_t mb_cin;
28*58b9f456SAndroid Build Coastguard Worker _ALIGNAS_TYPE (wistream) _LIBCPP_FUNC_VIS char wcin[sizeof(wistream)]
29*58b9f456SAndroid Build Coastguard Worker #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
30*58b9f456SAndroid Build Coastguard Worker __asm__("?wcin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
31*58b9f456SAndroid Build Coastguard Worker #endif
32*58b9f456SAndroid Build Coastguard Worker ;
33*58b9f456SAndroid Build Coastguard Worker _ALIGNAS_TYPE (__stdinbuf<wchar_t> ) static char __wcin[sizeof(__stdinbuf <wchar_t>)];
34*58b9f456SAndroid Build Coastguard Worker static mbstate_t mb_wcin;
35*58b9f456SAndroid Build Coastguard Worker #endif
36*58b9f456SAndroid Build Coastguard Worker 
37*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_HAS_NO_STDOUT
38*58b9f456SAndroid Build Coastguard Worker _ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char cout[sizeof(ostream)]
39*58b9f456SAndroid Build Coastguard Worker #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
40*58b9f456SAndroid Build Coastguard Worker __asm__("?cout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
41*58b9f456SAndroid Build Coastguard Worker #endif
42*58b9f456SAndroid Build Coastguard Worker ;
43*58b9f456SAndroid Build Coastguard Worker _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)];
44*58b9f456SAndroid Build Coastguard Worker static mbstate_t mb_cout;
45*58b9f456SAndroid Build Coastguard Worker _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcout[sizeof(wostream)]
46*58b9f456SAndroid Build Coastguard Worker #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
47*58b9f456SAndroid Build Coastguard Worker __asm__("?wcout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
48*58b9f456SAndroid Build Coastguard Worker #endif
49*58b9f456SAndroid Build Coastguard Worker ;
50*58b9f456SAndroid Build Coastguard Worker _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)];
51*58b9f456SAndroid Build Coastguard Worker static mbstate_t mb_wcout;
52*58b9f456SAndroid Build Coastguard Worker #endif
53*58b9f456SAndroid Build Coastguard Worker 
54*58b9f456SAndroid Build Coastguard Worker _ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char cerr[sizeof(ostream)]
55*58b9f456SAndroid Build Coastguard Worker #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
56*58b9f456SAndroid Build Coastguard Worker __asm__("?cerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
57*58b9f456SAndroid Build Coastguard Worker #endif
58*58b9f456SAndroid Build Coastguard Worker ;
59*58b9f456SAndroid Build Coastguard Worker _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)];
60*58b9f456SAndroid Build Coastguard Worker static mbstate_t mb_cerr;
61*58b9f456SAndroid Build Coastguard Worker _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcerr[sizeof(wostream)]
62*58b9f456SAndroid Build Coastguard Worker #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
63*58b9f456SAndroid Build Coastguard Worker __asm__("?wcerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
64*58b9f456SAndroid Build Coastguard Worker #endif
65*58b9f456SAndroid Build Coastguard Worker ;
66*58b9f456SAndroid Build Coastguard Worker _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)];
67*58b9f456SAndroid Build Coastguard Worker static mbstate_t mb_wcerr;
68*58b9f456SAndroid Build Coastguard Worker 
69*58b9f456SAndroid Build Coastguard Worker _ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char clog[sizeof(ostream)]
70*58b9f456SAndroid Build Coastguard Worker #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
71*58b9f456SAndroid Build Coastguard Worker __asm__("?clog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
72*58b9f456SAndroid Build Coastguard Worker #endif
73*58b9f456SAndroid Build Coastguard Worker ;
74*58b9f456SAndroid Build Coastguard Worker _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wclog[sizeof(wostream)]
75*58b9f456SAndroid Build Coastguard Worker #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
76*58b9f456SAndroid Build Coastguard Worker __asm__("?wclog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
77*58b9f456SAndroid Build Coastguard Worker #endif
78*58b9f456SAndroid Build Coastguard Worker ;
79*58b9f456SAndroid Build Coastguard Worker 
80*58b9f456SAndroid Build Coastguard Worker _LIBCPP_HIDDEN ios_base::Init __start_std_streams;
81*58b9f456SAndroid Build Coastguard Worker 
Init()82*58b9f456SAndroid Build Coastguard Worker ios_base::Init::Init()
83*58b9f456SAndroid Build Coastguard Worker {
84*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_HAS_NO_STDIN
85*58b9f456SAndroid Build Coastguard Worker     istream* cin_ptr  = ::new(cin)  istream(::new(__cin)  __stdinbuf <char>(stdin, &mb_cin));
86*58b9f456SAndroid Build Coastguard Worker     wistream* wcin_ptr  = ::new(wcin)  wistream(::new(__wcin)  __stdinbuf <wchar_t>(stdin, &mb_wcin));
87*58b9f456SAndroid Build Coastguard Worker #endif
88*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_HAS_NO_STDOUT
89*58b9f456SAndroid Build Coastguard Worker     ostream* cout_ptr = ::new(cout) ostream(::new(__cout) __stdoutbuf<char>(stdout, &mb_cout));
90*58b9f456SAndroid Build Coastguard Worker     wostream* wcout_ptr = ::new(wcout) wostream(::new(__wcout) __stdoutbuf<wchar_t>(stdout, &mb_wcout));
91*58b9f456SAndroid Build Coastguard Worker #endif
92*58b9f456SAndroid Build Coastguard Worker     ostream* cerr_ptr = ::new(cerr) ostream(::new(__cerr) __stdoutbuf<char>(stderr, &mb_cerr));
93*58b9f456SAndroid Build Coastguard Worker                         ::new(clog) ostream(cerr_ptr->rdbuf());
94*58b9f456SAndroid Build Coastguard Worker     wostream* wcerr_ptr = ::new(wcerr) wostream(::new(__wcerr) __stdoutbuf<wchar_t>(stderr, &mb_wcerr));
95*58b9f456SAndroid Build Coastguard Worker                           ::new(wclog) wostream(wcerr_ptr->rdbuf());
96*58b9f456SAndroid Build Coastguard Worker 
97*58b9f456SAndroid Build Coastguard Worker #if !defined(_LIBCPP_HAS_NO_STDIN) && !defined(_LIBCPP_HAS_NO_STDOUT)
98*58b9f456SAndroid Build Coastguard Worker     cin_ptr->tie(cout_ptr);
99*58b9f456SAndroid Build Coastguard Worker     wcin_ptr->tie(wcout_ptr);
100*58b9f456SAndroid Build Coastguard Worker #endif
101*58b9f456SAndroid Build Coastguard Worker     _VSTD::unitbuf(*cerr_ptr);
102*58b9f456SAndroid Build Coastguard Worker     _VSTD::unitbuf(*wcerr_ptr);
103*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_HAS_NO_STDOUT
104*58b9f456SAndroid Build Coastguard Worker     cerr_ptr->tie(cout_ptr);
105*58b9f456SAndroid Build Coastguard Worker     wcerr_ptr->tie(wcout_ptr);
106*58b9f456SAndroid Build Coastguard Worker #endif
107*58b9f456SAndroid Build Coastguard Worker }
108*58b9f456SAndroid Build Coastguard Worker 
~Init()109*58b9f456SAndroid Build Coastguard Worker ios_base::Init::~Init()
110*58b9f456SAndroid Build Coastguard Worker {
111*58b9f456SAndroid Build Coastguard Worker #ifndef _LIBCPP_HAS_NO_STDOUT
112*58b9f456SAndroid Build Coastguard Worker     ostream* cout_ptr = reinterpret_cast<ostream*>(cout);
113*58b9f456SAndroid Build Coastguard Worker     wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout);
114*58b9f456SAndroid Build Coastguard Worker     cout_ptr->flush();
115*58b9f456SAndroid Build Coastguard Worker     wcout_ptr->flush();
116*58b9f456SAndroid Build Coastguard Worker #endif
117*58b9f456SAndroid Build Coastguard Worker 
118*58b9f456SAndroid Build Coastguard Worker     ostream* clog_ptr = reinterpret_cast<ostream*>(clog);
119*58b9f456SAndroid Build Coastguard Worker     wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog);
120*58b9f456SAndroid Build Coastguard Worker     clog_ptr->flush();
121*58b9f456SAndroid Build Coastguard Worker     wclog_ptr->flush();
122*58b9f456SAndroid Build Coastguard Worker }
123*58b9f456SAndroid Build Coastguard Worker 
124*58b9f456SAndroid Build Coastguard Worker _LIBCPP_END_NAMESPACE_STD
125