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