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_SYSTEM_SYSTEM_ERROR_H
18 #define LLVM_SYSTEM_SYSTEM_ERROR_H
29 virtual ~error_category();
31 error_category(const error_category&) = delete;
32 error_category& operator=(const error_category&) = delete;
34 virtual const char* name() const = 0;
35 virtual error_condition default_error_condition(int ev) const;
36 virtual bool equivalent(int code, const error_condition& condition) const;
37 virtual bool equivalent(const error_code& code, int condition) const;
38 virtual std::string message(int ev) const = 0;
40 bool operator==(const error_category& rhs) const;
41 bool operator!=(const error_category& rhs) const;
42 bool operator<(const error_category& rhs) const;
45 const error_category& generic_category();
46 const error_category& system_category();
48 template <class T> struct is_error_code_enum
49 : public false_type {};
51 template <class T> struct is_error_condition_enum
52 : public false_type {};
59 error_code(int val, const error_category& cat);
60 template <class ErrorCodeEnum>
61 error_code(ErrorCodeEnum e);
64 void assign(int val, const error_category& cat);
65 template <class ErrorCodeEnum>
66 error_code& operator=(ErrorCodeEnum e);
71 const error_category& category() const;
72 error_condition default_error_condition() const;
73 std::string message() const;
74 explicit operator bool() const;
77 // non-member functions:
78 bool operator<(const error_code& lhs, const error_code& rhs);
79 template <class charT, class traits>
80 basic_ostream<charT,traits>&
81 operator<<(basic_ostream<charT,traits>& os, const error_code& ec);
88 error_condition(int val, const error_category& cat);
89 template <class ErrorConditionEnum>
90 error_condition(ErrorConditionEnum e);
93 void assign(int val, const error_category& cat);
94 template <class ErrorConditionEnum>
95 error_condition& operator=(ErrorConditionEnum e);
100 const error_category& category() const;
101 std::string message() const;
102 explicit operator bool() const;
105 bool operator<(const error_condition& lhs, const error_condition& rhs);
108 : public runtime_error
111 system_error(error_code ec, const std::string& what_arg);
112 system_error(error_code ec, const char* what_arg);
113 system_error(error_code ec);
114 system_error(int ev, const error_category& ecat, const std::string& what_arg);
115 system_error(int ev, const error_category& ecat, const char* what_arg);
116 system_error(int ev, const error_category& ecat);
118 const error_code& code() const throw();
119 const char* what() const throw();
124 address_family_not_supported, // EAFNOSUPPORT
125 address_in_use, // EADDRINUSE
126 address_not_available, // EADDRNOTAVAIL
127 already_connected, // EISCONN
128 argument_list_too_long, // E2BIG
129 argument_out_of_domain, // EDOM
130 bad_address, // EFAULT
131 bad_file_descriptor, // EBADF
132 bad_message, // EBADMSG
133 broken_pipe, // EPIPE
134 connection_aborted, // ECONNABORTED
135 connection_already_in_progress, // EALREADY
136 connection_refused, // ECONNREFUSED
137 connection_reset, // ECONNRESET
138 cross_device_link, // EXDEV
139 destination_address_required, // EDESTADDRREQ
140 device_or_resource_busy, // EBUSY
141 directory_not_empty, // ENOTEMPTY
142 executable_format_error, // ENOEXEC
143 file_exists, // EEXIST
144 file_too_large, // EFBIG
145 filename_too_long, // ENAMETOOLONG
146 function_not_supported, // ENOSYS
147 host_unreachable, // EHOSTUNREACH
148 identifier_removed, // EIDRM
149 illegal_byte_sequence, // EILSEQ
150 inappropriate_io_control_operation, // ENOTTY
151 interrupted, // EINTR
152 invalid_argument, // EINVAL
153 invalid_seek, // ESPIPE
155 is_a_directory, // EISDIR
156 message_size, // EMSGSIZE
157 network_down, // ENETDOWN
158 network_reset, // ENETRESET
159 network_unreachable, // ENETUNREACH
160 no_buffer_space, // ENOBUFS
161 no_child_process, // ECHILD
163 no_lock_available, // ENOLCK
164 no_message_available, // ENODATA
165 no_message, // ENOMSG
166 no_protocol_option, // ENOPROTOOPT
167 no_space_on_device, // ENOSPC
168 no_stream_resources, // ENOSR
169 no_such_device_or_address, // ENXIO
170 no_such_device, // ENODEV
171 no_such_file_or_directory, // ENOENT
172 no_such_process, // ESRCH
173 not_a_directory, // ENOTDIR
174 not_a_socket, // ENOTSOCK
175 not_a_stream, // ENOSTR
176 not_connected, // ENOTCONN
177 not_enough_memory, // ENOMEM
178 not_supported, // ENOTSUP
179 operation_canceled, // ECANCELED
180 operation_in_progress, // EINPROGRESS
181 operation_not_permitted, // EPERM
182 operation_not_supported, // EOPNOTSUPP
183 operation_would_block, // EWOULDBLOCK
184 owner_dead, // EOWNERDEAD
185 permission_denied, // EACCES
186 protocol_error, // EPROTO
187 protocol_not_supported, // EPROTONOSUPPORT
188 read_only_file_system, // EROFS
189 resource_deadlock_would_occur, // EDEADLK
190 resource_unavailable_try_again, // EAGAIN
191 result_out_of_range, // ERANGE
192 state_not_recoverable, // ENOTRECOVERABLE
193 stream_timeout, // ETIME
194 text_file_busy, // ETXTBSY
195 timed_out, // ETIMEDOUT
196 too_many_files_open_in_system, // ENFILE
197 too_many_files_open, // EMFILE
198 too_many_links, // EMLINK
199 too_many_symbolic_link_levels, // ELOOP
200 value_too_large, // EOVERFLOW
201 wrong_protocol_type // EPROTOTYPE
204 template <> struct is_error_condition_enum<errc> : true_type { }
206 error_code make_error_code(errc e);
207 error_condition make_error_condition(errc e);
209 // Comparison operators:
210 bool operator==(const error_code& lhs, const error_code& rhs);
211 bool operator==(const error_code& lhs, const error_condition& rhs);
212 bool operator==(const error_condition& lhs, const error_code& rhs);
213 bool operator==(const error_condition& lhs, const error_condition& rhs);
214 bool operator!=(const error_code& lhs, const error_code& rhs);
215 bool operator!=(const error_code& lhs, const error_condition& rhs);
216 bool operator!=(const error_condition& lhs, const error_code& rhs);
217 bool operator!=(const error_condition& lhs, const error_condition& rhs);
219 template <> struct hash<std::error_code>;
225 #include "llvm/Config/llvm-config.h"
226 #include "llvm/Support/type_traits.h"
230 // This must be here instead of a .inc file because it is used in the definition
231 // of the enum values below.
234 // The following numbers were taken from VS2010.
235 # ifndef EAFNOSUPPORT
236 # define EAFNOSUPPORT 102
239 # define EADDRINUSE 100
241 # ifndef EADDRNOTAVAIL
242 # define EADDRNOTAVAIL 101
265 # ifndef ECONNABORTED
266 # define ECONNABORTED 106
269 # define EALREADY 103
271 # ifndef ECONNREFUSED
272 # define ECONNREFUSED 107
275 # define ECONNRESET 108
280 # ifndef EDESTADDRREQ
281 # define EDESTADDRREQ 109
287 # define ENOTEMPTY 41
298 # ifndef ENAMETOOLONG
299 # define ENAMETOOLONG 38
304 # ifndef EHOSTUNREACH
305 # define EHOSTUNREACH 110
332 # define EMSGSIZE 115
335 # define ENETDOWN 116
338 # define ENETRESET 117
341 # define ENETUNREACH 118
362 # define ENOPROTOOPT 123
386 # define ENOTSOCK 128
392 # define ENOTCONN 126
401 # define ECANCELED 105
404 # define EINPROGRESS 112
410 # define EOPNOTSUPP 130
413 # define EWOULDBLOCK 140
416 # define EOWNERDEAD 133
424 # ifndef EPROTONOSUPPORT
425 # define EPROTONOSUPPORT 135
439 # ifndef ENOTRECOVERABLE
440 # define ENOTRECOVERABLE 127
449 # define ETIMEDOUT 138
464 # define EOVERFLOW 132
467 # define EPROTOTYPE 136
473 // is_error_code_enum
475 template <class Tp> struct is_error_code_enum : public false_type {};
477 // is_error_condition_enum
479 template <class Tp> struct is_error_condition_enum : public false_type {};
481 // Some error codes are not present on all platforms, so we provide equivalents
488 address_family_not_supported = EAFNOSUPPORT,
489 address_in_use = EADDRINUSE,
490 address_not_available = EADDRNOTAVAIL,
491 already_connected = EISCONN,
492 argument_list_too_long = E2BIG,
493 argument_out_of_domain = EDOM,
494 bad_address = EFAULT,
495 bad_file_descriptor = EBADF,
497 bad_message = EBADMSG,
499 bad_message = EINVAL,
502 connection_aborted = ECONNABORTED,
503 connection_already_in_progress = EALREADY,
504 connection_refused = ECONNREFUSED,
505 connection_reset = ECONNRESET,
506 cross_device_link = EXDEV,
507 destination_address_required = EDESTADDRREQ,
508 device_or_resource_busy = EBUSY,
509 directory_not_empty = ENOTEMPTY,
510 executable_format_error = ENOEXEC,
511 file_exists = EEXIST,
512 file_too_large = EFBIG,
513 filename_too_long = ENAMETOOLONG,
514 function_not_supported = ENOSYS,
515 host_unreachable = EHOSTUNREACH,
516 identifier_removed = EIDRM,
517 illegal_byte_sequence = EILSEQ,
518 inappropriate_io_control_operation = ENOTTY,
520 invalid_argument = EINVAL,
521 invalid_seek = ESPIPE,
523 is_a_directory = EISDIR,
524 message_size = EMSGSIZE,
525 network_down = ENETDOWN,
526 network_reset = ENETRESET,
527 network_unreachable = ENETUNREACH,
528 no_buffer_space = ENOBUFS,
529 no_child_process = ECHILD,
535 no_lock_available = ENOLCK,
537 no_message_available = ENODATA,
539 no_message_available = ENOMSG,
542 no_protocol_option = ENOPROTOOPT,
543 no_space_on_device = ENOSPC,
545 no_stream_resources = ENOSR,
547 no_stream_resources = ENOMEM,
549 no_such_device_or_address = ENXIO,
550 no_such_device = ENODEV,
551 no_such_file_or_directory = ENOENT,
552 no_such_process = ESRCH,
553 not_a_directory = ENOTDIR,
554 not_a_socket = ENOTSOCK,
556 not_a_stream = ENOSTR,
558 not_a_stream = EINVAL,
560 not_connected = ENOTCONN,
561 not_enough_memory = ENOMEM,
562 not_supported = ENOTSUP,
564 operation_canceled = ECANCELED,
566 operation_canceled = EINVAL,
568 operation_in_progress = EINPROGRESS,
569 operation_not_permitted = EPERM,
570 operation_not_supported = EOPNOTSUPP,
571 operation_would_block = EWOULDBLOCK,
573 owner_dead = EOWNERDEAD,
577 permission_denied = EACCES,
579 protocol_error = EPROTO,
581 protocol_error = EINVAL,
583 protocol_not_supported = EPROTONOSUPPORT,
584 read_only_file_system = EROFS,
585 resource_deadlock_would_occur = EDEADLK,
586 resource_unavailable_try_again = EAGAIN,
587 result_out_of_range = ERANGE,
588 #ifdef ENOTRECOVERABLE
589 state_not_recoverable = ENOTRECOVERABLE,
591 state_not_recoverable = EINVAL,
594 stream_timeout = ETIME,
596 stream_timeout = ETIMEDOUT,
598 text_file_busy = ETXTBSY,
599 timed_out = ETIMEDOUT,
600 too_many_files_open_in_system = ENFILE,
601 too_many_files_open = EMFILE,
602 too_many_links = EMLINK,
603 too_many_symbolic_link_levels = ELOOP,
604 value_too_large = EOVERFLOW,
605 wrong_protocol_type = EPROTOTYPE
611 operator int() const {return v_;}
614 template <> struct is_error_condition_enum<errc> : true_type { };
616 template <> struct is_error_condition_enum<errc::_> : true_type { };
618 class error_condition;
621 // class error_category
628 virtual ~error_category();
632 error_category(const error_category&);// = delete;
633 error_category& operator=(const error_category&);// = delete;
636 virtual const char* name() const = 0;
637 virtual error_condition default_error_condition(int _ev) const;
638 virtual bool equivalent(int _code, const error_condition& _condition) const;
639 virtual bool equivalent(const error_code& _code, int _condition) const;
640 virtual std::string message(int _ev) const = 0;
642 bool operator==(const error_category& _rhs) const {return this == &_rhs;}
644 bool operator!=(const error_category& _rhs) const {return !(*this == _rhs);}
646 bool operator< (const error_category& _rhs) const {return this < &_rhs;}
648 friend class _do_message;
651 class _do_message : public error_category
654 virtual std::string message(int ev) const;
657 const error_category& generic_category();
658 const error_category& system_category();
660 /// Get the error_category used for errno values from POSIX functions. This is
661 /// the same as the system_category on POSIX systems, but is the same as the
662 /// generic_category on Windows.
663 const error_category& posix_category();
665 class error_condition
668 const error_category* _cat_;
670 error_condition() : _val_(0), _cat_(&generic_category()) {}
672 error_condition(int _val, const error_category& _cat)
673 : _val_(_val), _cat_(&_cat) {}
676 error_condition(E _e, typename enable_if_c<
677 is_error_condition_enum<E>::value
679 {*this = make_error_condition(_e);}
681 void assign(int _val, const error_category& _cat) {
689 is_error_condition_enum<E>::value,
693 {*this = make_error_condition(_e); return *this;}
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 ? 0 : 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 enable_if_c<
739 is_error_code_enum<E>::value
741 *this = make_error_code(_e);
744 void assign(int _val, const error_category& _cat) {
752 is_error_code_enum<E>::value,
756 {*this = make_error_code(_e); return *this;}
760 _cat_ = &system_category();
763 int value() const {return _val_;}
765 const error_category& category() const {return *_cat_;}
767 error_condition default_error_condition() const
768 {return _cat_->default_error_condition(_val_);}
770 std::string message() const;
772 typedef void (*unspecified_bool_type)();
773 static void unspecified_bool_true() {}
775 operator unspecified_bool_type() const { // true if error
776 return _val_ == 0 ? 0 : unspecified_bool_true;
780 inline error_code make_error_code(errc _e) {
781 return error_code(static_cast<int>(_e), generic_category());
784 inline bool operator<(const error_code& _x, const error_code& _y) {
785 return _x.category() < _y.category()
786 || (_x.category() == _y.category() && _x.value() < _y.value());
789 inline bool operator==(const error_code& _x, const error_code& _y) {
790 return _x.category() == _y.category() && _x.value() == _y.value();
793 inline bool operator==(const error_code& _x, const error_condition& _y) {
794 return _x.category().equivalent(_x.value(), _y)
795 || _y.category().equivalent(_x, _y.value());
798 inline bool operator==(const error_condition& _x, const error_code& _y) {
802 inline bool operator==(const error_condition& _x, const error_condition& _y) {
803 return _x.category() == _y.category() && _x.value() == _y.value();
806 inline bool operator!=(const error_code& _x, const error_code& _y) {
810 inline bool operator!=(const error_code& _x, const error_condition& _y) {
814 inline bool operator!=(const error_condition& _x, const error_code& _y) {
818 inline bool operator!=(const error_condition& _x, const error_condition& _y) {
824 // To construct an error_code after an API error:
826 // error_code( ::GetLastError(), system_category() )
827 struct windows_error {
830 // These names and values are based on Windows WinError.h
831 // This is not a complete list. Add to this list if you need to explicitly
833 invalid_function = 1, // ERROR_INVALID_FUNCTION,
834 file_not_found = 2, // ERROR_FILE_NOT_FOUND,
835 path_not_found = 3, // ERROR_PATH_NOT_FOUND,
836 too_many_open_files = 4, // ERROR_TOO_MANY_OPEN_FILES,
837 access_denied = 5, // ERROR_ACCESS_DENIED,
838 invalid_handle = 6, // ERROR_INVALID_HANDLE,
839 arena_trashed = 7, // ERROR_ARENA_TRASHED,
840 not_enough_memory = 8, // ERROR_NOT_ENOUGH_MEMORY,
841 invalid_block = 9, // ERROR_INVALID_BLOCK,
842 bad_environment = 10, // ERROR_BAD_ENVIRONMENT,
843 bad_format = 11, // ERROR_BAD_FORMAT,
844 invalid_access = 12, // ERROR_INVALID_ACCESS,
845 outofmemory = 14, // ERROR_OUTOFMEMORY,
846 invalid_drive = 15, // ERROR_INVALID_DRIVE,
847 current_directory = 16, // ERROR_CURRENT_DIRECTORY,
848 not_same_device = 17, // ERROR_NOT_SAME_DEVICE,
849 no_more_files = 18, // ERROR_NO_MORE_FILES,
850 write_protect = 19, // ERROR_WRITE_PROTECT,
851 bad_unit = 20, // ERROR_BAD_UNIT,
852 not_ready = 21, // ERROR_NOT_READY,
853 bad_command = 22, // ERROR_BAD_COMMAND,
854 crc = 23, // ERROR_CRC,
855 bad_length = 24, // ERROR_BAD_LENGTH,
856 seek = 25, // ERROR_SEEK,
857 not_dos_disk = 26, // ERROR_NOT_DOS_DISK,
858 sector_not_found = 27, // ERROR_SECTOR_NOT_FOUND,
859 out_of_paper = 28, // ERROR_OUT_OF_PAPER,
860 write_fault = 29, // ERROR_WRITE_FAULT,
861 read_fault = 30, // ERROR_READ_FAULT,
862 gen_failure = 31, // ERROR_GEN_FAILURE,
863 sharing_violation = 32, // ERROR_SHARING_VIOLATION,
864 lock_violation = 33, // ERROR_LOCK_VIOLATION,
865 wrong_disk = 34, // ERROR_WRONG_DISK,
866 sharing_buffer_exceeded = 36, // ERROR_SHARING_BUFFER_EXCEEDED,
867 handle_eof = 38, // ERROR_HANDLE_EOF,
868 handle_disk_full = 39, // ERROR_HANDLE_DISK_FULL,
869 rem_not_list = 51, // ERROR_REM_NOT_LIST,
870 dup_name = 52, // ERROR_DUP_NAME,
871 bad_net_path = 53, // ERROR_BAD_NETPATH,
872 network_busy = 54, // ERROR_NETWORK_BUSY,
873 file_exists = 80, // ERROR_FILE_EXISTS,
874 cannot_make = 82, // ERROR_CANNOT_MAKE,
875 broken_pipe = 109, // ERROR_BROKEN_PIPE,
876 open_failed = 110, // ERROR_OPEN_FAILED,
877 buffer_overflow = 111, // ERROR_BUFFER_OVERFLOW,
878 disk_full = 112, // ERROR_DISK_FULL,
879 insufficient_buffer = 122, // ERROR_INSUFFICIENT_BUFFER,
880 lock_failed = 167, // ERROR_LOCK_FAILED,
881 busy = 170, // ERROR_BUSY,
882 cancel_violation = 173, // ERROR_CANCEL_VIOLATION,
883 already_exists = 183 // ERROR_ALREADY_EXISTS
887 windows_error(_ v) : v_(v) {}
888 explicit windows_error(int v) : v_(_(v)) {}
889 operator int() const {return v_;}
893 template <> struct is_error_code_enum<windows_error> : true_type { };
895 template <> struct is_error_code_enum<windows_error::_> : true_type { };
897 inline error_code make_error_code(windows_error e) {
898 return error_code(static_cast<int>(e), system_category());
901 } // end namespace llvm