1 //===---------------------------- system_error ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This was lifted from libc++ and modified for C++03. This is called
11 // system_error even though it does not define that class because that's what
12 // it's called in C++0x. We don't define system_error because it is only used
13 // for exception handling, which we don't use in LLVM.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_SUPPORT_SYSTEM_ERROR_H
18 #define LLVM_SUPPORT_SYSTEM_ERROR_H
20 #include "llvm/Support/Compiler.h"
31 virtual ~error_category();
33 error_category(const error_category&) = delete;
34 error_category& operator=(const error_category&) = delete;
36 virtual const char* name() const = 0;
37 virtual error_condition default_error_condition(int ev) const;
38 virtual bool equivalent(int code, const error_condition& condition) const;
39 virtual bool equivalent(const error_code& code, int condition) const;
40 virtual std::string message(int ev) const = 0;
42 bool operator==(const error_category& rhs) const;
43 bool operator!=(const error_category& rhs) const;
44 bool operator<(const error_category& rhs) const;
47 const error_category& generic_category();
48 const error_category& system_category();
50 template <class T> struct is_error_code_enum
51 : public std::false_type {};
53 template <class T> struct is_error_condition_enum
54 : public std::false_type {};
61 error_code(int val, const error_category& cat);
62 template <class ErrorCodeEnum>
63 error_code(ErrorCodeEnum e);
66 void assign(int val, const error_category& cat);
67 template <class ErrorCodeEnum>
68 error_code& operator=(ErrorCodeEnum e);
73 const error_category& category() const;
74 error_condition default_error_condition() const;
75 std::string message() const;
76 explicit operator bool() const;
79 // non-member functions:
80 bool operator<(const error_code& lhs, const error_code& rhs);
81 template <class charT, class traits>
82 basic_ostream<charT,traits>&
83 operator<<(basic_ostream<charT,traits>& os, const error_code& ec);
90 error_condition(int val, const error_category& cat);
91 template <class ErrorConditionEnum>
92 error_condition(ErrorConditionEnum e);
95 void assign(int val, const error_category& cat);
96 template <class ErrorConditionEnum>
97 error_condition& operator=(ErrorConditionEnum e);
102 const error_category& category() const;
103 std::string message() const;
104 explicit operator bool() const;
107 bool operator<(const error_condition& lhs, const error_condition& rhs);
110 : public runtime_error
113 system_error(error_code ec, const std::string& what_arg);
114 system_error(error_code ec, const char* what_arg);
115 system_error(error_code ec);
116 system_error(int ev, const error_category& ecat, const std::string& what_arg);
117 system_error(int ev, const error_category& ecat, const char* what_arg);
118 system_error(int ev, const error_category& ecat);
120 const error_code& code() const throw();
121 const char* what() const throw();
126 address_family_not_supported, // EAFNOSUPPORT
127 address_in_use, // EADDRINUSE
128 address_not_available, // EADDRNOTAVAIL
129 already_connected, // EISCONN
130 argument_list_too_long, // E2BIG
131 argument_out_of_domain, // EDOM
132 bad_address, // EFAULT
133 bad_file_descriptor, // EBADF
134 bad_message, // EBADMSG
135 broken_pipe, // EPIPE
136 connection_aborted, // ECONNABORTED
137 connection_already_in_progress, // EALREADY
138 connection_refused, // ECONNREFUSED
139 connection_reset, // ECONNRESET
140 cross_device_link, // EXDEV
141 destination_address_required, // EDESTADDRREQ
142 device_or_resource_busy, // EBUSY
143 directory_not_empty, // ENOTEMPTY
144 executable_format_error, // ENOEXEC
145 file_exists, // EEXIST
146 file_too_large, // EFBIG
147 filename_too_long, // ENAMETOOLONG
148 function_not_supported, // ENOSYS
149 host_unreachable, // EHOSTUNREACH
150 identifier_removed, // EIDRM
151 illegal_byte_sequence, // EILSEQ
152 inappropriate_io_control_operation, // ENOTTY
153 interrupted, // EINTR
154 invalid_argument, // EINVAL
155 invalid_seek, // ESPIPE
157 is_a_directory, // EISDIR
158 message_size, // EMSGSIZE
159 network_down, // ENETDOWN
160 network_reset, // ENETRESET
161 network_unreachable, // ENETUNREACH
162 no_buffer_space, // ENOBUFS
163 no_child_process, // ECHILD
165 no_lock_available, // ENOLCK
166 no_message_available, // ENODATA
167 no_message, // ENOMSG
168 no_protocol_option, // ENOPROTOOPT
169 no_space_on_device, // ENOSPC
170 no_stream_resources, // ENOSR
171 no_such_device_or_address, // ENXIO
172 no_such_device, // ENODEV
173 no_such_file_or_directory, // ENOENT
174 no_such_process, // ESRCH
175 not_a_directory, // ENOTDIR
176 not_a_socket, // ENOTSOCK
177 not_a_stream, // ENOSTR
178 not_connected, // ENOTCONN
179 not_enough_memory, // ENOMEM
180 not_supported, // ENOTSUP
181 operation_canceled, // ECANCELED
182 operation_in_progress, // EINPROGRESS
183 operation_not_permitted, // EPERM
184 operation_not_supported, // EOPNOTSUPP
185 operation_would_block, // EWOULDBLOCK
186 owner_dead, // EOWNERDEAD
187 permission_denied, // EACCES
188 protocol_error, // EPROTO
189 protocol_not_supported, // EPROTONOSUPPORT
190 read_only_file_system, // EROFS
191 resource_deadlock_would_occur, // EDEADLK
192 resource_unavailable_try_again, // EAGAIN
193 result_out_of_range, // ERANGE
194 state_not_recoverable, // ENOTRECOVERABLE
195 stream_timeout, // ETIME
196 text_file_busy, // ETXTBSY
197 timed_out, // ETIMEDOUT
198 too_many_files_open_in_system, // ENFILE
199 too_many_files_open, // EMFILE
200 too_many_links, // EMLINK
201 too_many_symbolic_link_levels, // ELOOP
202 value_too_large, // EOVERFLOW
203 wrong_protocol_type // EPROTOTYPE
206 template <> struct is_error_condition_enum<errc> : std::true_type { }
208 error_code make_error_code(errc e);
209 error_condition make_error_condition(errc e);
211 // Comparison operators:
212 bool operator==(const error_code& lhs, const error_code& rhs);
213 bool operator==(const error_code& lhs, const error_condition& rhs);
214 bool operator==(const error_condition& lhs, const error_code& rhs);
215 bool operator==(const error_condition& lhs, const error_condition& rhs);
216 bool operator!=(const error_code& lhs, const error_code& rhs);
217 bool operator!=(const error_code& lhs, const error_condition& rhs);
218 bool operator!=(const error_condition& lhs, const error_code& rhs);
219 bool operator!=(const error_condition& lhs, const error_condition& rhs);
221 template <> struct hash<std::error_code>;
227 #include "llvm/Config/llvm-config.h"
231 // This must be here instead of a .inc file because it is used in the definition
232 // of the enum values below.
235 // The following numbers were taken from VS2010.
236 # ifndef EAFNOSUPPORT
237 # define EAFNOSUPPORT 102
240 # define EADDRINUSE 100
242 # ifndef EADDRNOTAVAIL
243 # define EADDRNOTAVAIL 101
266 # ifndef ECONNABORTED
267 # define ECONNABORTED 106
270 # define EALREADY 103
272 # ifndef ECONNREFUSED
273 # define ECONNREFUSED 107
276 # define ECONNRESET 108
281 # ifndef EDESTADDRREQ
282 # define EDESTADDRREQ 109
288 # define ENOTEMPTY 41
299 # ifndef ENAMETOOLONG
300 # define ENAMETOOLONG 38
305 # ifndef EHOSTUNREACH
306 # define EHOSTUNREACH 110
333 # define EMSGSIZE 115
336 # define ENETDOWN 116
339 # define ENETRESET 117
342 # define ENETUNREACH 118
363 # define ENOPROTOOPT 123
387 # define ENOTSOCK 128
393 # define ENOTCONN 126
402 # define ECANCELED 105
405 # define EINPROGRESS 112
411 # define EOPNOTSUPP 130
414 # define EWOULDBLOCK 140
417 # define EOWNERDEAD 133
425 # ifndef EPROTONOSUPPORT
426 # define EPROTONOSUPPORT 135
440 # ifndef ENOTRECOVERABLE
441 # define ENOTRECOVERABLE 127
450 # define ETIMEDOUT 138
465 # define EOVERFLOW 132
468 # define EPROTOTYPE 136
474 // is_error_code_enum
476 template <class Tp> struct is_error_code_enum : public std::false_type {};
478 // is_error_condition_enum
480 template <class Tp> struct is_error_condition_enum : public std::false_type {};
482 // Some error codes are not present on all platforms, so we provide equivalents
489 address_family_not_supported = EAFNOSUPPORT,
490 address_in_use = EADDRINUSE,
491 address_not_available = EADDRNOTAVAIL,
492 already_connected = EISCONN,
493 argument_list_too_long = E2BIG,
494 argument_out_of_domain = EDOM,
495 bad_address = EFAULT,
496 bad_file_descriptor = EBADF,
498 bad_message = EBADMSG,
500 bad_message = EINVAL,
503 connection_aborted = ECONNABORTED,
504 connection_already_in_progress = EALREADY,
505 connection_refused = ECONNREFUSED,
506 connection_reset = ECONNRESET,
507 cross_device_link = EXDEV,
508 destination_address_required = EDESTADDRREQ,
509 device_or_resource_busy = EBUSY,
510 directory_not_empty = ENOTEMPTY,
511 executable_format_error = ENOEXEC,
512 file_exists = EEXIST,
513 file_too_large = EFBIG,
514 filename_too_long = ENAMETOOLONG,
515 function_not_supported = ENOSYS,
516 host_unreachable = EHOSTUNREACH,
517 identifier_removed = EIDRM,
518 illegal_byte_sequence = EILSEQ,
519 inappropriate_io_control_operation = ENOTTY,
521 invalid_argument = EINVAL,
522 invalid_seek = ESPIPE,
524 is_a_directory = EISDIR,
525 message_size = EMSGSIZE,
526 network_down = ENETDOWN,
527 network_reset = ENETRESET,
528 network_unreachable = ENETUNREACH,
529 no_buffer_space = ENOBUFS,
530 no_child_process = ECHILD,
536 no_lock_available = ENOLCK,
538 no_message_available = ENODATA,
540 no_message_available = ENOMSG,
543 no_protocol_option = ENOPROTOOPT,
544 no_space_on_device = ENOSPC,
546 no_stream_resources = ENOSR,
548 no_stream_resources = ENOMEM,
550 no_such_device_or_address = ENXIO,
551 no_such_device = ENODEV,
552 no_such_file_or_directory = ENOENT,
553 no_such_process = ESRCH,
554 not_a_directory = ENOTDIR,
555 not_a_socket = ENOTSOCK,
557 not_a_stream = ENOSTR,
559 not_a_stream = EINVAL,
561 not_connected = ENOTCONN,
562 not_enough_memory = ENOMEM,
563 not_supported = ENOTSUP,
565 operation_canceled = ECANCELED,
567 operation_canceled = EINVAL,
569 operation_in_progress = EINPROGRESS,
570 operation_not_permitted = EPERM,
571 operation_not_supported = EOPNOTSUPP,
572 operation_would_block = EWOULDBLOCK,
574 owner_dead = EOWNERDEAD,
578 permission_denied = EACCES,
580 protocol_error = EPROTO,
582 protocol_error = EINVAL,
584 protocol_not_supported = EPROTONOSUPPORT,
585 read_only_file_system = EROFS,
586 resource_deadlock_would_occur = EDEADLK,
587 resource_unavailable_try_again = EAGAIN,
588 result_out_of_range = ERANGE,
589 #ifdef ENOTRECOVERABLE
590 state_not_recoverable = ENOTRECOVERABLE,
592 state_not_recoverable = EINVAL,
595 stream_timeout = ETIME,
597 stream_timeout = ETIMEDOUT,
599 text_file_busy = ETXTBSY,
600 timed_out = ETIMEDOUT,
601 too_many_files_open_in_system = ENFILE,
602 too_many_files_open = EMFILE,
603 too_many_links = EMLINK,
604 too_many_symbolic_link_levels = ELOOP,
605 value_too_large = EOVERFLOW,
606 wrong_protocol_type = EPROTOTYPE
612 operator int() const {return v_;}
615 template <> struct is_error_condition_enum<errc> : std::true_type { };
617 template <> struct is_error_condition_enum<errc::_> : std::true_type { };
619 class error_condition;
622 // class error_category
629 virtual ~error_category();
633 error_category(const error_category&) LLVM_DELETED_FUNCTION;
634 error_category& operator=(const error_category&) LLVM_DELETED_FUNCTION;
637 virtual const char* name() const = 0;
638 virtual error_condition default_error_condition(int _ev) const;
639 virtual bool equivalent(int _code, const error_condition& _condition) const;
640 virtual bool equivalent(const error_code& _code, int _condition) const;
641 virtual std::string message(int _ev) const = 0;
643 bool operator==(const error_category& _rhs) const {return this == &_rhs;}
645 bool operator!=(const error_category& _rhs) const {return !(*this == _rhs);}
647 bool operator< (const error_category& _rhs) const {return this < &_rhs;}
649 friend class _do_message;
652 class _do_message : public error_category
655 std::string message(int ev) const override;
658 const error_category& generic_category();
659 const error_category& system_category();
661 /// Get the error_category used for errno values from POSIX functions. This is
662 /// the same as the system_category on POSIX systems, but is the same as the
663 /// generic_category on Windows.
664 const error_category& posix_category();
666 class error_condition
669 const error_category* _cat_;
671 error_condition() : _val_(0), _cat_(&generic_category()) {}
673 error_condition(int _val, const error_category& _cat)
674 : _val_(_val), _cat_(&_cat) {}
677 error_condition(E _e, typename std::enable_if<
678 is_error_condition_enum<E>::value
680 {*this = make_error_condition(_e);}
682 void assign(int _val, const error_category& _cat) {
688 typename std::enable_if<is_error_condition_enum<E>::value,
689 error_condition &>::type
691 *this = make_error_condition(_e);
697 _cat_ = &generic_category();
700 int value() const {return _val_;}
702 const error_category& category() const {return *_cat_;}
703 std::string message() const;
705 typedef void (*unspecified_bool_type)();
706 static void unspecified_bool_true() {}
708 operator unspecified_bool_type() const { // true if error
709 return _val_ == 0 ? nullptr : unspecified_bool_true;
713 inline error_condition make_error_condition(errc _e) {
714 return error_condition(static_cast<int>(_e), generic_category());
717 inline bool operator<(const error_condition& _x, const error_condition& _y) {
718 return _x.category() < _y.category()
719 || (_x.category() == _y.category() && _x.value() < _y.value());
726 const error_category* _cat_;
728 error_code() : _val_(0), _cat_(&system_category()) {}
730 static error_code success() {
734 error_code(int _val, const error_category& _cat)
735 : _val_(_val), _cat_(&_cat) {}
738 error_code(E _e, typename std::enable_if<
739 is_error_code_enum<E>::value
741 *this = make_error_code(_e);
744 void assign(int _val, const error_category& _cat) {
750 typename std::enable_if<is_error_code_enum<E>::value, error_code &>::type
752 *this = make_error_code(_e);
758 _cat_ = &system_category();
761 int value() const {return _val_;}
763 const error_category& category() const {return *_cat_;}
765 error_condition default_error_condition() const
766 {return _cat_->default_error_condition(_val_);}
768 std::string message() const;
770 typedef void (*unspecified_bool_type)();
771 static void unspecified_bool_true() {}
773 operator unspecified_bool_type() const { // true if error
774 return _val_ == 0 ? nullptr : unspecified_bool_true;
778 inline error_code make_error_code(errc _e) {
779 return error_code(static_cast<int>(_e), generic_category());
782 inline bool operator<(const error_code& _x, const error_code& _y) {
783 return _x.category() < _y.category()
784 || (_x.category() == _y.category() && _x.value() < _y.value());
787 inline bool operator==(const error_code& _x, const error_code& _y) {
788 return _x.category() == _y.category() && _x.value() == _y.value();
791 inline bool operator==(const error_code& _x, const error_condition& _y) {
792 return _x.category().equivalent(_x.value(), _y)
793 || _y.category().equivalent(_x, _y.value());
796 inline bool operator==(const error_condition& _x, const error_code& _y) {
800 inline bool operator==(const error_condition& _x, const error_condition& _y) {
801 return _x.category() == _y.category() && _x.value() == _y.value();
804 inline bool operator!=(const error_code& _x, const error_code& _y) {
808 inline bool operator!=(const error_code& _x, const error_condition& _y) {
812 inline bool operator!=(const error_condition& _x, const error_code& _y) {
816 inline bool operator!=(const error_condition& _x, const error_condition& _y) {
822 // To construct an error_code after an API error:
824 // error_code( ::GetLastError(), system_category() )
825 struct windows_error {
828 // These names and values are based on Windows WinError.h
829 // This is not a complete list. Add to this list if you need to explicitly
831 invalid_function = 1, // ERROR_INVALID_FUNCTION,
832 file_not_found = 2, // ERROR_FILE_NOT_FOUND,
833 path_not_found = 3, // ERROR_PATH_NOT_FOUND,
834 too_many_open_files = 4, // ERROR_TOO_MANY_OPEN_FILES,
835 access_denied = 5, // ERROR_ACCESS_DENIED,
836 invalid_handle = 6, // ERROR_INVALID_HANDLE,
837 arena_trashed = 7, // ERROR_ARENA_TRASHED,
838 not_enough_memory = 8, // ERROR_NOT_ENOUGH_MEMORY,
839 invalid_block = 9, // ERROR_INVALID_BLOCK,
840 bad_environment = 10, // ERROR_BAD_ENVIRONMENT,
841 bad_format = 11, // ERROR_BAD_FORMAT,
842 invalid_access = 12, // ERROR_INVALID_ACCESS,
843 outofmemory = 14, // ERROR_OUTOFMEMORY,
844 invalid_drive = 15, // ERROR_INVALID_DRIVE,
845 current_directory = 16, // ERROR_CURRENT_DIRECTORY,
846 not_same_device = 17, // ERROR_NOT_SAME_DEVICE,
847 no_more_files = 18, // ERROR_NO_MORE_FILES,
848 write_protect = 19, // ERROR_WRITE_PROTECT,
849 bad_unit = 20, // ERROR_BAD_UNIT,
850 not_ready = 21, // ERROR_NOT_READY,
851 bad_command = 22, // ERROR_BAD_COMMAND,
852 crc = 23, // ERROR_CRC,
853 bad_length = 24, // ERROR_BAD_LENGTH,
854 seek = 25, // ERROR_SEEK,
855 not_dos_disk = 26, // ERROR_NOT_DOS_DISK,
856 sector_not_found = 27, // ERROR_SECTOR_NOT_FOUND,
857 out_of_paper = 28, // ERROR_OUT_OF_PAPER,
858 write_fault = 29, // ERROR_WRITE_FAULT,
859 read_fault = 30, // ERROR_READ_FAULT,
860 gen_failure = 31, // ERROR_GEN_FAILURE,
861 sharing_violation = 32, // ERROR_SHARING_VIOLATION,
862 lock_violation = 33, // ERROR_LOCK_VIOLATION,
863 wrong_disk = 34, // ERROR_WRONG_DISK,
864 sharing_buffer_exceeded = 36, // ERROR_SHARING_BUFFER_EXCEEDED,
865 handle_eof = 38, // ERROR_HANDLE_EOF,
866 handle_disk_full = 39, // ERROR_HANDLE_DISK_FULL,
867 rem_not_list = 51, // ERROR_REM_NOT_LIST,
868 dup_name = 52, // ERROR_DUP_NAME,
869 bad_net_path = 53, // ERROR_BAD_NETPATH,
870 network_busy = 54, // ERROR_NETWORK_BUSY,
871 file_exists = 80, // ERROR_FILE_EXISTS,
872 cannot_make = 82, // ERROR_CANNOT_MAKE,
873 broken_pipe = 109, // ERROR_BROKEN_PIPE,
874 open_failed = 110, // ERROR_OPEN_FAILED,
875 buffer_overflow = 111, // ERROR_BUFFER_OVERFLOW,
876 disk_full = 112, // ERROR_DISK_FULL,
877 insufficient_buffer = 122, // ERROR_INSUFFICIENT_BUFFER,
878 lock_failed = 167, // ERROR_LOCK_FAILED,
879 busy = 170, // ERROR_BUSY,
880 cancel_violation = 173, // ERROR_CANCEL_VIOLATION,
881 already_exists = 183 // ERROR_ALREADY_EXISTS
885 windows_error(_ v) : v_(v) {}
886 explicit windows_error(int v) : v_(_(v)) {}
887 operator int() const {return v_;}
891 template <> struct is_error_code_enum<windows_error> : std::true_type { };
893 template <> struct is_error_code_enum<windows_error::_> : std::true_type { };
895 inline error_code make_error_code(windows_error e) {
896 return error_code(static_cast<int>(e), system_category());
899 } // end namespace llvm