1*38fd1498Szrj // Iostreams base classes -*- C++ -*-
2*38fd1498Szrj 
3*38fd1498Szrj // Copyright (C) 2014-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj 
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj 
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj 
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj 
25*38fd1498Szrj //
26*38fd1498Szrj // ISO C++ 14882:2011: 27.5.3.1.1  Class ios_base::failure
27*38fd1498Szrj //
28*38fd1498Szrj 
29*38fd1498Szrj #define _GLIBCXX_USE_CXX11_ABI 1
30*38fd1498Szrj #include <ios>
31*38fd1498Szrj #include <bits/functexcept.h>
32*38fd1498Szrj #include <cxxabi.h>
33*38fd1498Szrj 
34*38fd1498Szrj #ifdef _GLIBCXX_USE_NLS
35*38fd1498Szrj # include <libintl.h>
36*38fd1498Szrj # define _(msgid)   gettext (msgid)
37*38fd1498Szrj #else
38*38fd1498Szrj # define _(msgid)   (msgid)
39*38fd1498Szrj #endif
40*38fd1498Szrj 
41*38fd1498Szrj #if ! _GLIBCXX_USE_DUAL_ABI
42*38fd1498Szrj # error This file should not be compiled for this configuration.
43*38fd1498Szrj #endif
44*38fd1498Szrj 
45*38fd1498Szrj namespace
46*38fd1498Szrj {
47*38fd1498Szrj   struct io_error_category : std::error_category
48*38fd1498Szrj   {
49*38fd1498Szrj     virtual const char*
name__anon540ba73c0111::io_error_category50*38fd1498Szrj     name() const noexcept
51*38fd1498Szrj     { return "iostream"; }
52*38fd1498Szrj 
53*38fd1498Szrj     _GLIBCXX_DEFAULT_ABI_TAG
message__anon540ba73c0111::io_error_category54*38fd1498Szrj     virtual std::string message(int __ec) const
55*38fd1498Szrj     {
56*38fd1498Szrj       std::string __msg;
57*38fd1498Szrj       switch (std::io_errc(__ec))
58*38fd1498Szrj       {
59*38fd1498Szrj       case std::io_errc::stream:
60*38fd1498Szrj           __msg = "iostream error";
61*38fd1498Szrj           break;
62*38fd1498Szrj       default:
63*38fd1498Szrj           __msg = "Unknown error";
64*38fd1498Szrj           break;
65*38fd1498Szrj       }
66*38fd1498Szrj       return __msg;
67*38fd1498Szrj     }
68*38fd1498Szrj   };
69*38fd1498Szrj 
70*38fd1498Szrj   const io_error_category&
__io_category_instance()71*38fd1498Szrj   __io_category_instance() noexcept
72*38fd1498Szrj   {
73*38fd1498Szrj     static const io_error_category __ec{};
74*38fd1498Szrj     return __ec;
75*38fd1498Szrj   }
76*38fd1498Szrj 
77*38fd1498Szrj } // namespace
78*38fd1498Szrj 
79*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
80*38fd1498Szrj {
81*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
82*38fd1498Szrj 
83*38fd1498Szrj   const error_category&
iostream_category()84*38fd1498Szrj   iostream_category() noexcept
85*38fd1498Szrj   { return __io_category_instance(); }
86*38fd1498Szrj 
failure(const string & __str)87*38fd1498Szrj   ios_base::failure::failure(const string& __str)
88*38fd1498Szrj   : system_error(io_errc::stream, __str) { }
89*38fd1498Szrj 
failure(const string & __str,const error_code & __ec)90*38fd1498Szrj   ios_base::failure::failure(const string& __str, const error_code& __ec)
91*38fd1498Szrj   : system_error(__ec, __str) { }
92*38fd1498Szrj 
failure(const char * __str,const error_code & __ec)93*38fd1498Szrj   ios_base::failure::failure(const char* __str, const error_code& __ec)
94*38fd1498Szrj   : system_error(__ec, __str) { }
95*38fd1498Szrj 
~failure()96*38fd1498Szrj   ios_base::failure::~failure()
97*38fd1498Szrj   { }
98*38fd1498Szrj 
99*38fd1498Szrj   const char*
what() const100*38fd1498Szrj   ios_base::failure::what() const throw()
101*38fd1498Szrj   { return runtime_error::what(); }
102*38fd1498Szrj 
103*38fd1498Szrj #if __cpp_rtti
104*38fd1498Szrj   // These functions are defined in src/c++98/ios_failure.cc
105*38fd1498Szrj   extern void __construct_ios_failure(void*, const char*);
106*38fd1498Szrj   extern void __destroy_ios_failure(void*);
107*38fd1498Szrj   extern bool __is_ios_failure_handler(const __cxxabiv1::__class_type_info*);
108*38fd1498Szrj 
109*38fd1498Szrj   // The type thrown to report errors during stream buffer operations.
110*38fd1498Szrj   // In addition to the ios::failure[abi:cxx11] base class it also has a
111*38fd1498Szrj   // member of the gcc4-compatible ios::failure type (in an opaque buffer).
112*38fd1498Szrj   struct __ios_failure : std::ios::failure
113*38fd1498Szrj   {
__ios_failurestd::__ios_failure114*38fd1498Szrj     __ios_failure(const char* s) : failure(s)
115*38fd1498Szrj     { __construct_ios_failure(buf, runtime_error::what()); }
116*38fd1498Szrj 
~__ios_failurestd::__ios_failure117*38fd1498Szrj     ~__ios_failure()
118*38fd1498Szrj     { __destroy_ios_failure(buf); }
119*38fd1498Szrj 
120*38fd1498Szrj     // Use std::runtime_error as a proxy for the gcc4-compatible ios::failure
121*38fd1498Szrj     // (which can't be declared here because _GLIBCXX_USE_CXX11_ABI == 1).
122*38fd1498Szrj     // There are assertions in src/c++98/ios_failure.cc to ensure the size
123*38fd1498Szrj     // and alignment assumptions are valid.
124*38fd1498Szrj     alignas(runtime_error) unsigned char buf[sizeof(runtime_error)];
125*38fd1498Szrj   };
126*38fd1498Szrj 
127*38fd1498Szrj   // Custom type info for __ios_failure.
128*38fd1498Szrj   class __iosfail_type_info : __cxxabiv1::__si_class_type_info
129*38fd1498Szrj   {
130*38fd1498Szrj     ~__iosfail_type_info();
131*38fd1498Szrj 
132*38fd1498Szrj     bool
133*38fd1498Szrj     __do_upcast (const __class_type_info *dst_type,
134*38fd1498Szrj 		 void **obj_ptr) const override;
135*38fd1498Szrj   };
136*38fd1498Szrj 
137*38fd1498Szrj   __iosfail_type_info::~__iosfail_type_info() = default;
138*38fd1498Szrj 
139*38fd1498Szrj   // This function gets called to see if an exception of type
140*38fd1498Szrj   // __ios_failure can be upcast to the type in a catch handler.
141*38fd1498Szrj   bool
__do_upcast(const __class_type_info * dst_type,void ** obj_ptr) const142*38fd1498Szrj   __iosfail_type_info::__do_upcast(const __class_type_info *dst_type,
143*38fd1498Szrj 				   void **obj_ptr) const
144*38fd1498Szrj   {
145*38fd1498Szrj     // If the handler is for the gcc4-compatible ios::failure type then
146*38fd1498Szrj     // catch the object stored in __ios_failure::buf instead of
147*38fd1498Szrj     // the __ios_failure exception object itself.
148*38fd1498Szrj     if (__is_ios_failure_handler(dst_type))
149*38fd1498Szrj       {
150*38fd1498Szrj 	*obj_ptr = static_cast<__ios_failure*>(*obj_ptr)->buf;
151*38fd1498Szrj 	return true;
152*38fd1498Szrj       }
153*38fd1498Szrj     // Otherwise proceed as normal to see if the handler matches.
154*38fd1498Szrj     return __class_type_info::__do_upcast(dst_type, obj_ptr);
155*38fd1498Szrj   }
156*38fd1498Szrj #else // ! __cpp_rtti
157*38fd1498Szrj   using __ios_failure = ios::failure;
158*38fd1498Szrj #endif
159*38fd1498Szrj 
160*38fd1498Szrj   void
__throw_ios_failure(const char * __s)161*38fd1498Szrj   __throw_ios_failure(const char* __s __attribute__((unused)))
162*38fd1498Szrj   { _GLIBCXX_THROW_OR_ABORT(__ios_failure(_(__s))); }
163*38fd1498Szrj 
164*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
165*38fd1498Szrj } // namespace
166