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
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 false_type {};
53 template <class T> struct is_error_condition_enum
54 : public 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> : 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"
228 #include "llvm/Support/type_traits.h"
232 // This must be here instead of a .inc file because it is used in the definition
233 // of the enum values below.
236 // The following numbers were taken from VS2010.
237 # ifndef EAFNOSUPPORT
238 # define EAFNOSUPPORT 102
241 # define EADDRINUSE 100
243 # ifndef EADDRNOTAVAIL
244 # define EADDRNOTAVAIL 101
267 # ifndef ECONNABORTED
268 # define ECONNABORTED 106
271 # define EALREADY 103
273 # ifndef ECONNREFUSED
274 # define ECONNREFUSED 107
277 # define ECONNRESET 108
282 # ifndef EDESTADDRREQ
283 # define EDESTADDRREQ 109
289 # define ENOTEMPTY 41
300 # ifndef ENAMETOOLONG
301 # define ENAMETOOLONG 38
306 # ifndef EHOSTUNREACH
307 # define EHOSTUNREACH 110
334 # define EMSGSIZE 115
337 # define ENETDOWN 116
340 # define ENETRESET 117
343 # define ENETUNREACH 118
364 # define ENOPROTOOPT 123
388 # define ENOTSOCK 128
394 # define ENOTCONN 126
403 # define ECANCELED 105
406 # define EINPROGRESS 112
412 # define EOPNOTSUPP 130
415 # define EWOULDBLOCK 140
418 # define EOWNERDEAD 133
426 # ifndef EPROTONOSUPPORT
427 # define EPROTONOSUPPORT 135
441 # ifndef ENOTRECOVERABLE
442 # define ENOTRECOVERABLE 127
451 # define ETIMEDOUT 138
466 # define EOVERFLOW 132
469 # define EPROTOTYPE 136
475 // is_error_code_enum
477 template <class Tp> struct is_error_code_enum : public false_type {};
479 // is_error_condition_enum
481 template <class Tp> struct is_error_condition_enum : public false_type {};
483 // Some error codes are not present on all platforms, so we provide equivalents
490 address_family_not_supported = EAFNOSUPPORT,
491 address_in_use = EADDRINUSE,
492 address_not_available = EADDRNOTAVAIL,
493 already_connected = EISCONN,
494 argument_list_too_long = E2BIG,
495 argument_out_of_domain = EDOM,
496 bad_address = EFAULT,
497 bad_file_descriptor = EBADF,
499 bad_message = EBADMSG,
501 bad_message = EINVAL,
504 connection_aborted = ECONNABORTED,
505 connection_already_in_progress = EALREADY,
506 connection_refused = ECONNREFUSED,
507 connection_reset = ECONNRESET,
508 cross_device_link = EXDEV,
509 destination_address_required = EDESTADDRREQ,
510 device_or_resource_busy = EBUSY,
511 directory_not_empty = ENOTEMPTY,
512 executable_format_error = ENOEXEC,
513 file_exists = EEXIST,
514 file_too_large = EFBIG,
515 filename_too_long = ENAMETOOLONG,
516 function_not_supported = ENOSYS,
517 host_unreachable = EHOSTUNREACH,
518 identifier_removed = EIDRM,
519 illegal_byte_sequence = EILSEQ,
520 inappropriate_io_control_operation = ENOTTY,
522 invalid_argument = EINVAL,
523 invalid_seek = ESPIPE,
525 is_a_directory = EISDIR,
526 message_size = EMSGSIZE,
527 network_down = ENETDOWN,
528 network_reset = ENETRESET,
529 network_unreachable = ENETUNREACH,
530 no_buffer_space = ENOBUFS,
531 no_child_process = ECHILD,
537 no_lock_available = ENOLCK,
539 no_message_available = ENODATA,
541 no_message_available = ENOMSG,
544 no_protocol_option = ENOPROTOOPT,
545 no_space_on_device = ENOSPC,
547 no_stream_resources = ENOSR,
549 no_stream_resources = ENOMEM,
551 no_such_device_or_address = ENXIO,
552 no_such_device = ENODEV,
553 no_such_file_or_directory = ENOENT,
554 no_such_process = ESRCH,
555 not_a_directory = ENOTDIR,
556 not_a_socket = ENOTSOCK,
558 not_a_stream = ENOSTR,
560 not_a_stream = EINVAL,
562 not_connected = ENOTCONN,
563 not_enough_memory = ENOMEM,
564 not_supported = ENOTSUP,
566 operation_canceled = ECANCELED,
568 operation_canceled = EINVAL,
570 operation_in_progress = EINPROGRESS,
571 operation_not_permitted = EPERM,
572 operation_not_supported = EOPNOTSUPP,
573 operation_would_block = EWOULDBLOCK,
575 owner_dead = EOWNERDEAD,
579 permission_denied = EACCES,
581 protocol_error = EPROTO,
583 protocol_error = EINVAL,
585 protocol_not_supported = EPROTONOSUPPORT,
586 read_only_file_system = EROFS,
587 resource_deadlock_would_occur = EDEADLK,
588 resource_unavailable_try_again = EAGAIN,
589 result_out_of_range = ERANGE,
590 #ifdef ENOTRECOVERABLE
591 state_not_recoverable = ENOTRECOVERABLE,
593 state_not_recoverable = EINVAL,
596 stream_timeout = ETIME,
598 stream_timeout = ETIMEDOUT,
600 text_file_busy = ETXTBSY,
601 timed_out = ETIMEDOUT,
602 too_many_files_open_in_system = ENFILE,
603 too_many_files_open = EMFILE,
604 too_many_links = EMLINK,
605 too_many_symbolic_link_levels = ELOOP,
606 value_too_large = EOVERFLOW,
607 wrong_protocol_type = EPROTOTYPE
613 operator int() const {return v_;}
616 template <> struct is_error_condition_enum<errc> : true_type { };
618 template <> struct is_error_condition_enum<errc::_> : true_type { };
620 class error_condition;
623 // class error_category
630 virtual ~error_category();
634 error_category(const error_category&) LLVM_DELETED_FUNCTION;
635 error_category& operator=(const error_category&) LLVM_DELETED_FUNCTION;
638 virtual const char* name() const = 0;
639 virtual error_condition default_error_condition(int _ev) const;
640 virtual bool equivalent(int _code, const error_condition& _condition) const;
641 virtual bool equivalent(const error_code& _code, int _condition) const;
642 virtual std::string message(int _ev) const = 0;
644 bool operator==(const error_category& _rhs) const {return this == &_rhs;}
646 bool operator!=(const error_category& _rhs) const {return !(*this == _rhs);}
648 bool operator< (const error_category& _rhs) const {return this < &_rhs;}
650 friend class _do_message;
653 class _do_message : public error_category
656 virtual std::string message(int ev) const LLVM_OVERRIDE;
659 const error_category& generic_category();
660 const error_category& system_category();
662 /// Get the error_category used for errno values from POSIX functions. This is
663 /// the same as the system_category on POSIX systems, but is the same as the
664 /// generic_category on Windows.
665 const error_category& posix_category();
667 class error_condition
670 const error_category* _cat_;
672 error_condition() : _val_(0), _cat_(&generic_category()) {}
674 error_condition(int _val, const error_category& _cat)
675 : _val_(_val), _cat_(&_cat) {}
678 error_condition(E _e, typename enable_if_c<
679 is_error_condition_enum<E>::value
681 {*this = make_error_condition(_e);}
683 void assign(int _val, const error_category& _cat) {
691 is_error_condition_enum<E>::value,
695 {*this = make_error_condition(_e); return *this;}
699 _cat_ = &generic_category();
702 int value() const {return _val_;}
704 const error_category& category() const {return *_cat_;}
705 std::string message() const;
707 typedef void (*unspecified_bool_type)();
708 static void unspecified_bool_true() {}
710 operator unspecified_bool_type() const { // true if error
711 return _val_ == 0 ? 0 : unspecified_bool_true;
715 inline error_condition make_error_condition(errc _e) {
716 return error_condition(static_cast<int>(_e), generic_category());
719 inline bool operator<(const error_condition& _x, const error_condition& _y) {
720 return _x.category() < _y.category()
721 || (_x.category() == _y.category() && _x.value() < _y.value());
728 const error_category* _cat_;
730 error_code() : _val_(0), _cat_(&system_category()) {}
732 static error_code success() {
736 error_code(int _val, const error_category& _cat)
737 : _val_(_val), _cat_(&_cat) {}
740 error_code(E _e, typename enable_if_c<
741 is_error_code_enum<E>::value
743 *this = make_error_code(_e);
746 void assign(int _val, const error_category& _cat) {
754 is_error_code_enum<E>::value,
758 {*this = make_error_code(_e); return *this;}
762 _cat_ = &system_category();
765 int value() const {return _val_;}
767 const error_category& category() const {return *_cat_;}
769 error_condition default_error_condition() const
770 {return _cat_->default_error_condition(_val_);}
772 std::string message() const;
774 typedef void (*unspecified_bool_type)();
775 static void unspecified_bool_true() {}
777 operator unspecified_bool_type() const { // true if error
778 return _val_ == 0 ? 0 : unspecified_bool_true;
782 inline error_code make_error_code(errc _e) {
783 return error_code(static_cast<int>(_e), generic_category());
786 inline bool operator<(const error_code& _x, const error_code& _y) {
787 return _x.category() < _y.category()
788 || (_x.category() == _y.category() && _x.value() < _y.value());
791 inline bool operator==(const error_code& _x, const error_code& _y) {
792 return _x.category() == _y.category() && _x.value() == _y.value();
795 inline bool operator==(const error_code& _x, const error_condition& _y) {
796 return _x.category().equivalent(_x.value(), _y)
797 || _y.category().equivalent(_x, _y.value());
800 inline bool operator==(const error_condition& _x, const error_code& _y) {
804 inline bool operator==(const error_condition& _x, const error_condition& _y) {
805 return _x.category() == _y.category() && _x.value() == _y.value();
808 inline bool operator!=(const error_code& _x, const error_code& _y) {
812 inline bool operator!=(const error_code& _x, const error_condition& _y) {
816 inline bool operator!=(const error_condition& _x, const error_code& _y) {
820 inline bool operator!=(const error_condition& _x, const error_condition& _y) {
826 // To construct an error_code after an API error:
828 // error_code( ::GetLastError(), system_category() )
829 struct windows_error {
832 // These names and values are based on Windows WinError.h
833 // This is not a complete list. Add to this list if you need to explicitly
835 invalid_function = 1, // ERROR_INVALID_FUNCTION,
836 file_not_found = 2, // ERROR_FILE_NOT_FOUND,
837 path_not_found = 3, // ERROR_PATH_NOT_FOUND,
838 too_many_open_files = 4, // ERROR_TOO_MANY_OPEN_FILES,
839 access_denied = 5, // ERROR_ACCESS_DENIED,
840 invalid_handle = 6, // ERROR_INVALID_HANDLE,
841 arena_trashed = 7, // ERROR_ARENA_TRASHED,
842 not_enough_memory = 8, // ERROR_NOT_ENOUGH_MEMORY,
843 invalid_block = 9, // ERROR_INVALID_BLOCK,
844 bad_environment = 10, // ERROR_BAD_ENVIRONMENT,
845 bad_format = 11, // ERROR_BAD_FORMAT,
846 invalid_access = 12, // ERROR_INVALID_ACCESS,
847 outofmemory = 14, // ERROR_OUTOFMEMORY,
848 invalid_drive = 15, // ERROR_INVALID_DRIVE,
849 current_directory = 16, // ERROR_CURRENT_DIRECTORY,
850 not_same_device = 17, // ERROR_NOT_SAME_DEVICE,
851 no_more_files = 18, // ERROR_NO_MORE_FILES,
852 write_protect = 19, // ERROR_WRITE_PROTECT,
853 bad_unit = 20, // ERROR_BAD_UNIT,
854 not_ready = 21, // ERROR_NOT_READY,
855 bad_command = 22, // ERROR_BAD_COMMAND,
856 crc = 23, // ERROR_CRC,
857 bad_length = 24, // ERROR_BAD_LENGTH,
858 seek = 25, // ERROR_SEEK,
859 not_dos_disk = 26, // ERROR_NOT_DOS_DISK,
860 sector_not_found = 27, // ERROR_SECTOR_NOT_FOUND,
861 out_of_paper = 28, // ERROR_OUT_OF_PAPER,
862 write_fault = 29, // ERROR_WRITE_FAULT,
863 read_fault = 30, // ERROR_READ_FAULT,
864 gen_failure = 31, // ERROR_GEN_FAILURE,
865 sharing_violation = 32, // ERROR_SHARING_VIOLATION,
866 lock_violation = 33, // ERROR_LOCK_VIOLATION,
867 wrong_disk = 34, // ERROR_WRONG_DISK,
868 sharing_buffer_exceeded = 36, // ERROR_SHARING_BUFFER_EXCEEDED,
869 handle_eof = 38, // ERROR_HANDLE_EOF,
870 handle_disk_full = 39, // ERROR_HANDLE_DISK_FULL,
871 rem_not_list = 51, // ERROR_REM_NOT_LIST,
872 dup_name = 52, // ERROR_DUP_NAME,
873 bad_net_path = 53, // ERROR_BAD_NETPATH,
874 network_busy = 54, // ERROR_NETWORK_BUSY,
875 file_exists = 80, // ERROR_FILE_EXISTS,
876 cannot_make = 82, // ERROR_CANNOT_MAKE,
877 broken_pipe = 109, // ERROR_BROKEN_PIPE,
878 open_failed = 110, // ERROR_OPEN_FAILED,
879 buffer_overflow = 111, // ERROR_BUFFER_OVERFLOW,
880 disk_full = 112, // ERROR_DISK_FULL,
881 insufficient_buffer = 122, // ERROR_INSUFFICIENT_BUFFER,
882 lock_failed = 167, // ERROR_LOCK_FAILED,
883 busy = 170, // ERROR_BUSY,
884 cancel_violation = 173, // ERROR_CANCEL_VIOLATION,
885 already_exists = 183 // ERROR_ALREADY_EXISTS
889 windows_error(_ v) : v_(v) {}
890 explicit windows_error(int v) : v_(_(v)) {}
891 operator int() const {return v_;}
895 template <> struct is_error_code_enum<windows_error> : true_type { };
897 template <> struct is_error_code_enum<windows_error::_> : true_type { };
899 inline error_code make_error_code(windows_error e) {
900 return error_code(static_cast<int>(e), system_category());
903 } // end namespace llvm