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 template <class T, T v>
474 struct integral_constant {
475 typedef T value_type;
476 static const value_type value = v;
477 typedef integral_constant<T,v> type;
478 operator value_type() { return value; }
481 typedef integral_constant<bool, true> true_type;
482 typedef integral_constant<bool, false> false_type;
484 // is_error_code_enum
486 template <class Tp> struct is_error_code_enum : public false_type {};
488 // is_error_condition_enum
490 template <class Tp> struct is_error_condition_enum : public false_type {};
492 // Some error codes are not present on all platforms, so we provide equivalents
499 address_family_not_supported = EAFNOSUPPORT,
500 address_in_use = EADDRINUSE,
501 address_not_available = EADDRNOTAVAIL,
502 already_connected = EISCONN,
503 argument_list_too_long = E2BIG,
504 argument_out_of_domain = EDOM,
505 bad_address = EFAULT,
506 bad_file_descriptor = EBADF,
508 bad_message = EBADMSG,
510 bad_message = EINVAL,
513 connection_aborted = ECONNABORTED,
514 connection_already_in_progress = EALREADY,
515 connection_refused = ECONNREFUSED,
516 connection_reset = ECONNRESET,
517 cross_device_link = EXDEV,
518 destination_address_required = EDESTADDRREQ,
519 device_or_resource_busy = EBUSY,
520 directory_not_empty = ENOTEMPTY,
521 executable_format_error = ENOEXEC,
522 file_exists = EEXIST,
523 file_too_large = EFBIG,
524 filename_too_long = ENAMETOOLONG,
525 function_not_supported = ENOSYS,
526 host_unreachable = EHOSTUNREACH,
527 identifier_removed = EIDRM,
528 illegal_byte_sequence = EILSEQ,
529 inappropriate_io_control_operation = ENOTTY,
531 invalid_argument = EINVAL,
532 invalid_seek = ESPIPE,
534 is_a_directory = EISDIR,
535 message_size = EMSGSIZE,
536 network_down = ENETDOWN,
537 network_reset = ENETRESET,
538 network_unreachable = ENETUNREACH,
539 no_buffer_space = ENOBUFS,
540 no_child_process = ECHILD,
546 no_lock_available = ENOLCK,
548 no_message_available = ENODATA,
550 no_message_available = ENOMSG,
553 no_protocol_option = ENOPROTOOPT,
554 no_space_on_device = ENOSPC,
556 no_stream_resources = ENOSR,
558 no_stream_resources = ENOMEM,
560 no_such_device_or_address = ENXIO,
561 no_such_device = ENODEV,
562 no_such_file_or_directory = ENOENT,
563 no_such_process = ESRCH,
564 not_a_directory = ENOTDIR,
565 not_a_socket = ENOTSOCK,
567 not_a_stream = ENOSTR,
569 not_a_stream = EINVAL,
571 not_connected = ENOTCONN,
572 not_enough_memory = ENOMEM,
573 not_supported = ENOTSUP,
575 operation_canceled = ECANCELED,
577 operation_canceled = EINVAL,
579 operation_in_progress = EINPROGRESS,
580 operation_not_permitted = EPERM,
581 operation_not_supported = EOPNOTSUPP,
582 operation_would_block = EWOULDBLOCK,
584 owner_dead = EOWNERDEAD,
588 permission_denied = EACCES,
590 protocol_error = EPROTO,
592 protocol_error = EINVAL,
594 protocol_not_supported = EPROTONOSUPPORT,
595 read_only_file_system = EROFS,
596 resource_deadlock_would_occur = EDEADLK,
597 resource_unavailable_try_again = EAGAIN,
598 result_out_of_range = ERANGE,
599 #ifdef ENOTRECOVERABLE
600 state_not_recoverable = ENOTRECOVERABLE,
602 state_not_recoverable = EINVAL,
605 stream_timeout = ETIME,
607 stream_timeout = ETIMEDOUT,
609 text_file_busy = ETXTBSY,
610 timed_out = ETIMEDOUT,
611 too_many_files_open_in_system = ENFILE,
612 too_many_files_open = EMFILE,
613 too_many_links = EMLINK,
614 too_many_symbolic_link_levels = ELOOP,
615 value_too_large = EOVERFLOW,
616 wrong_protocol_type = EPROTOTYPE
622 operator int() const {return v_;}
625 template <> struct is_error_condition_enum<errc> : true_type { };
627 template <> struct is_error_condition_enum<errc::_> : true_type { };
629 class error_condition;
632 // class error_category
639 virtual ~error_category();
643 error_category(const error_category&);// = delete;
644 error_category& operator=(const error_category&);// = delete;
647 virtual const char* name() const = 0;
648 virtual error_condition default_error_condition(int _ev) const;
649 virtual bool equivalent(int _code, const error_condition& _condition) const;
650 virtual bool equivalent(const error_code& _code, int _condition) const;
651 virtual std::string message(int _ev) const = 0;
653 bool operator==(const error_category& _rhs) const {return this == &_rhs;}
655 bool operator!=(const error_category& _rhs) const {return !(*this == _rhs);}
657 bool operator< (const error_category& _rhs) const {return this < &_rhs;}
659 friend class _do_message;
662 class _do_message : public error_category
665 virtual std::string message(int ev) const;
668 const error_category& generic_category();
669 const error_category& system_category();
671 /// Get the error_category used for errno values from POSIX functions. This is
672 /// the same as the system_category on POSIX systems, but is the same as the
673 /// generic_category on Windows.
674 const error_category& posix_category();
676 class error_condition
679 const error_category* _cat_;
681 error_condition() : _val_(0), _cat_(&generic_category()) {}
683 error_condition(int _val, const error_category& _cat)
684 : _val_(_val), _cat_(&_cat) {}
687 error_condition(E _e, typename enable_if_c<
688 is_error_condition_enum<E>::value
690 {*this = make_error_condition(_e);}
692 void assign(int _val, const error_category& _cat) {
700 is_error_condition_enum<E>::value,
704 {*this = make_error_condition(_e); return *this;}
708 _cat_ = &generic_category();
711 int value() const {return _val_;}
713 const error_category& category() const {return *_cat_;}
714 std::string message() const;
716 typedef void (*unspecified_bool_type)();
717 static void unspecified_bool_true() {}
719 operator unspecified_bool_type() const { // true if error
720 return _val_ == 0 ? 0 : unspecified_bool_true;
724 inline error_condition make_error_condition(errc _e) {
725 return error_condition(static_cast<int>(_e), generic_category());
728 inline bool operator<(const error_condition& _x, const error_condition& _y) {
729 return _x.category() < _y.category()
730 || (_x.category() == _y.category() && _x.value() < _y.value());
737 const error_category* _cat_;
739 error_code() : _val_(0), _cat_(&system_category()) {}
741 error_code(int _val, const error_category& _cat)
742 : _val_(_val), _cat_(&_cat) {}
745 error_code(E _e, typename enable_if_c<
746 is_error_code_enum<E>::value
748 *this = make_error_code(_e);
751 void assign(int _val, const error_category& _cat) {
759 is_error_code_enum<E>::value,
763 {*this = make_error_code(_e); return *this;}
767 _cat_ = &system_category();
770 int value() const {return _val_;}
772 const error_category& category() const {return *_cat_;}
774 error_condition default_error_condition() const
775 {return _cat_->default_error_condition(_val_);}
777 std::string message() const;
779 typedef void (*unspecified_bool_type)();
780 static void unspecified_bool_true() {}
782 operator unspecified_bool_type() const { // true if error
783 return _val_ == 0 ? 0 : unspecified_bool_true;
787 inline error_code make_error_code(errc _e) {
788 return error_code(static_cast<int>(_e), generic_category());
791 inline bool operator<(const error_code& _x, const error_code& _y) {
792 return _x.category() < _y.category()
793 || (_x.category() == _y.category() && _x.value() < _y.value());
796 inline bool operator==(const error_code& _x, const error_code& _y) {
797 return _x.category() == _y.category() && _x.value() == _y.value();
800 inline bool operator==(const error_code& _x, const error_condition& _y) {
801 return _x.category().equivalent(_x.value(), _y)
802 || _y.category().equivalent(_x, _y.value());
805 inline bool operator==(const error_condition& _x, const error_code& _y) {
809 inline bool operator==(const error_condition& _x, const error_condition& _y) {
810 return _x.category() == _y.category() && _x.value() == _y.value();
813 inline bool operator!=(const error_code& _x, const error_code& _y) {
817 inline bool operator!=(const error_code& _x, const error_condition& _y) {
821 inline bool operator!=(const error_condition& _x, const error_code& _y) {
825 inline bool operator!=(const error_condition& _x, const error_condition& _y) {
831 // To construct an error_code after an API error:
833 // error_code( ::GetLastError(), system_category() )
834 struct windows_error {
837 // These names and values are based on Windows WinError.h
838 // This is not a complete list. Add to this list if you need to explicitly
840 invalid_function = 1, // ERROR_INVALID_FUNCTION,
841 file_not_found = 2, // ERROR_FILE_NOT_FOUND,
842 path_not_found = 3, // ERROR_PATH_NOT_FOUND,
843 too_many_open_files = 4, // ERROR_TOO_MANY_OPEN_FILES,
844 access_denied = 5, // ERROR_ACCESS_DENIED,
845 invalid_handle = 6, // ERROR_INVALID_HANDLE,
846 arena_trashed = 7, // ERROR_ARENA_TRASHED,
847 not_enough_memory = 8, // ERROR_NOT_ENOUGH_MEMORY,
848 invalid_block = 9, // ERROR_INVALID_BLOCK,
849 bad_environment = 10, // ERROR_BAD_ENVIRONMENT,
850 bad_format = 11, // ERROR_BAD_FORMAT,
851 invalid_access = 12, // ERROR_INVALID_ACCESS,
852 outofmemory = 14, // ERROR_OUTOFMEMORY,
853 invalid_drive = 15, // ERROR_INVALID_DRIVE,
854 current_directory = 16, // ERROR_CURRENT_DIRECTORY,
855 not_same_device = 17, // ERROR_NOT_SAME_DEVICE,
856 no_more_files = 18, // ERROR_NO_MORE_FILES,
857 write_protect = 19, // ERROR_WRITE_PROTECT,
858 bad_unit = 20, // ERROR_BAD_UNIT,
859 not_ready = 21, // ERROR_NOT_READY,
860 bad_command = 22, // ERROR_BAD_COMMAND,
861 crc = 23, // ERROR_CRC,
862 bad_length = 24, // ERROR_BAD_LENGTH,
863 seek = 25, // ERROR_SEEK,
864 not_dos_disk = 26, // ERROR_NOT_DOS_DISK,
865 sector_not_found = 27, // ERROR_SECTOR_NOT_FOUND,
866 out_of_paper = 28, // ERROR_OUT_OF_PAPER,
867 write_fault = 29, // ERROR_WRITE_FAULT,
868 read_fault = 30, // ERROR_READ_FAULT,
869 gen_failure = 31, // ERROR_GEN_FAILURE,
870 sharing_violation = 32, // ERROR_SHARING_VIOLATION,
871 lock_violation = 33, // ERROR_LOCK_VIOLATION,
872 wrong_disk = 34, // ERROR_WRONG_DISK,
873 sharing_buffer_exceeded = 36, // ERROR_SHARING_BUFFER_EXCEEDED,
874 handle_eof = 38, // ERROR_HANDLE_EOF,
875 handle_disk_full = 39, // ERROR_HANDLE_DISK_FULL,
876 rem_not_list = 51, // ERROR_REM_NOT_LIST,
877 dup_name = 52, // ERROR_DUP_NAME,
878 bad_net_path = 53, // ERROR_BAD_NETPATH,
879 network_busy = 54, // ERROR_NETWORK_BUSY,
880 file_exists = 80, // ERROR_FILE_EXISTS,
881 cannot_make = 82, // ERROR_CANNOT_MAKE,
882 broken_pipe = 109, // ERROR_BROKEN_PIPE,
883 open_failed = 110, // ERROR_OPEN_FAILED,
884 buffer_overflow = 111, // ERROR_BUFFER_OVERFLOW,
885 disk_full = 112, // ERROR_DISK_FULL,
886 insufficient_buffer = 122, // ERROR_INSUFFICIENT_BUFFER,
887 lock_failed = 167, // ERROR_LOCK_FAILED,
888 busy = 170, // ERROR_BUSY,
889 cancel_violation = 173, // ERROR_CANCEL_VIOLATION,
890 already_exists = 183 // ERROR_ALREADY_EXISTS
894 windows_error(_ v) : v_(v) {}
895 explicit windows_error(int v) : v_(_(v)) {}
896 operator int() const {return v_;}
900 template <> struct is_error_code_enum<windows_error> : true_type { };
902 template <> struct is_error_code_enum<windows_error::_> : true_type { };
904 inline error_code make_error_code(windows_error e) {
905 return error_code(static_cast<int>(e), system_category());
908 } // end namespace llvm