1 //===---------------------------- system_error ----------------------------===//
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/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.
233 // VS 2008 needs this for some of the defines below.
234 # include <WinSock2.h>
236 // The following numbers were taken from VS2010.
237 # ifndef EAFNOSUPPORT
238 # define EAFNOSUPPORT WSAEAFNOSUPPORT
241 # define EADDRINUSE WSAEADDRINUSE
243 # ifndef EADDRNOTAVAIL
244 # define EADDRNOTAVAIL WSAEADDRNOTAVAIL
247 # define EISCONN WSAEISCONN
250 # define E2BIG WSAE2BIG
253 # define EDOM WSAEDOM
256 # define EFAULT WSAEFAULT
259 # define EBADF WSAEBADF
265 # define EPIPE WSAEPIPE
267 # ifndef ECONNABORTED
268 # define ECONNABORTED WSAECONNABORTED
271 # define EALREADY WSAEALREADY
273 # ifndef ECONNREFUSED
274 # define ECONNREFUSED WSAECONNREFUSED
277 # define ECONNRESET WSAECONNRESET
280 # define EXDEV WSAEXDEV
282 # ifndef EDESTADDRREQ
283 # define EDESTADDRREQ WSAEDESTADDRREQ
286 # define EBUSY WSAEBUSY
289 # define ENOTEMPTY WSAENOTEMPTY
292 # define ENOEXEC WSAENOEXEC
295 # define EEXIST WSAEEXIST
298 # define EFBIG WSAEFBIG
300 # ifndef ENAMETOOLONG
301 # define ENAMETOOLONG WSAENAMETOOLONG
304 # define ENOSYS WSAENOSYS
306 # ifndef EHOSTUNREACH
307 # define EHOSTUNREACH WSAEHOSTUNREACH
313 # define EILSEQ WSAEILSEQ
316 # define ENOTTY WSAENOTTY
319 # define EINTR WSAEINTR
322 # define EINVAL WSAEINVAL
325 # define ESPIPE WSAESPIPE
331 # define EISDIR WSAEISDIR
334 # define EMSGSIZE WSAEMSGSIZE
337 # define ENETDOWN WSAENETDOWN
340 # define ENETRESET WSAENETRESET
343 # define ENETUNREACH WSAENETUNREACH
346 # define ENOBUFS WSAENOBUFS
349 # define ECHILD WSAECHILD
355 # define ENOLCK WSAENOLCK
364 # define ENOPROTOOPT WSAENOPROTOOPT
367 # define ENOSPC WSAENOSPC
373 # define ENXIO WSAENXIO
376 # define ENODEV WSAENODEV
379 # define ENOENT WSAENOENT
382 # define ESRCH WSAESRCH
385 # define ENOTDIR WSAENOTDIR
388 # define ENOTSOCK WSAENOTSOCK
394 # define ENOTCONN WSAENOTCONN
397 # define ENOMEM WSAENOMEM
403 # define ECANCELED 105
406 # define EINPROGRESS WSAEINPROGRESS
409 # define EPERM WSAEPERM
412 # define EOPNOTSUPP WSAEOPNOTSUPP
415 # define EWOULDBLOCK WSAEWOULDBLOCK
418 # define EOWNERDEAD 133
421 # define EACCES WSAEACCES
426 # ifndef EPROTONOSUPPORT
427 # define EPROTONOSUPPORT WSAEPROTONOSUPPORT
430 # define EROFS WSAEROFS
433 # define EDEADLK WSAEDEADLK
436 # define EAGAIN WSAEAGAIN
439 # define ERANGE WSAERANGE
441 # ifndef ENOTRECOVERABLE
442 # define ENOTRECOVERABLE 127
451 # define ETIMEDOUT WSAETIMEDOUT
454 # define ENFILE WSAENFILE
457 # define EMFILE WSAEMFILE
460 # define EMLINK WSAEMLINK
463 # define ELOOP WSAELOOP
466 # define EOVERFLOW 132
469 # define EPROTOTYPE WSAEPROTOTYPE
475 template <class T, T v>
476 struct integral_constant {
477 typedef T value_type;
478 static const value_type value = v;
479 typedef integral_constant<T,v> type;
480 operator value_type() { return value; }
483 typedef integral_constant<bool, true> true_type;
484 typedef integral_constant<bool, false> false_type;
486 // is_error_code_enum
488 template <class Tp> struct is_error_code_enum : public false_type {};
490 // is_error_condition_enum
492 template <class Tp> struct is_error_condition_enum : public false_type {};
494 // Some error codes are not present on all platforms, so we provide equivalents
501 address_family_not_supported = EAFNOSUPPORT,
502 address_in_use = EADDRINUSE,
503 address_not_available = EADDRNOTAVAIL,
504 already_connected = EISCONN,
505 argument_list_too_long = E2BIG,
506 argument_out_of_domain = EDOM,
507 bad_address = EFAULT,
508 bad_file_descriptor = EBADF,
510 bad_message = EBADMSG,
512 bad_message = EINVAL,
515 connection_aborted = ECONNABORTED,
516 connection_already_in_progress = EALREADY,
517 connection_refused = ECONNREFUSED,
518 connection_reset = ECONNRESET,
519 cross_device_link = EXDEV,
520 destination_address_required = EDESTADDRREQ,
521 device_or_resource_busy = EBUSY,
522 directory_not_empty = ENOTEMPTY,
523 executable_format_error = ENOEXEC,
524 file_exists = EEXIST,
525 file_too_large = EFBIG,
526 filename_too_long = ENAMETOOLONG,
527 function_not_supported = ENOSYS,
528 host_unreachable = EHOSTUNREACH,
529 identifier_removed = EIDRM,
530 illegal_byte_sequence = EILSEQ,
531 inappropriate_io_control_operation = ENOTTY,
533 invalid_argument = EINVAL,
534 invalid_seek = ESPIPE,
536 is_a_directory = EISDIR,
537 message_size = EMSGSIZE,
538 network_down = ENETDOWN,
539 network_reset = ENETRESET,
540 network_unreachable = ENETUNREACH,
541 no_buffer_space = ENOBUFS,
542 no_child_process = ECHILD,
548 no_lock_available = ENOLCK,
550 no_message_available = ENODATA,
552 no_message_available = ENOMSG,
555 no_protocol_option = ENOPROTOOPT,
556 no_space_on_device = ENOSPC,
558 no_stream_resources = ENOSR,
560 no_stream_resources = ENOMEM,
562 no_such_device_or_address = ENXIO,
563 no_such_device = ENODEV,
564 no_such_file_or_directory = ENOENT,
565 no_such_process = ESRCH,
566 not_a_directory = ENOTDIR,
567 not_a_socket = ENOTSOCK,
569 not_a_stream = ENOSTR,
571 not_a_stream = EINVAL,
573 not_connected = ENOTCONN,
574 not_enough_memory = ENOMEM,
575 not_supported = ENOTSUP,
577 operation_canceled = ECANCELED,
579 operation_canceled = EINVAL,
581 operation_in_progress = EINPROGRESS,
582 operation_not_permitted = EPERM,
583 operation_not_supported = EOPNOTSUPP,
584 operation_would_block = EWOULDBLOCK,
586 owner_dead = EOWNERDEAD,
590 permission_denied = EACCES,
592 protocol_error = EPROTO,
594 protocol_error = EINVAL,
596 protocol_not_supported = EPROTONOSUPPORT,
597 read_only_file_system = EROFS,
598 resource_deadlock_would_occur = EDEADLK,
599 resource_unavailable_try_again = EAGAIN,
600 result_out_of_range = ERANGE,
601 #ifdef ENOTRECOVERABLE
602 state_not_recoverable = ENOTRECOVERABLE,
604 state_not_recoverable = EINVAL,
607 stream_timeout = ETIME,
609 stream_timeout = ETIMEDOUT,
611 text_file_busy = ETXTBSY,
612 timed_out = ETIMEDOUT,
613 too_many_files_open_in_system = ENFILE,
614 too_many_files_open = EMFILE,
615 too_many_links = EMLINK,
616 too_many_symbolic_link_levels = ELOOP,
617 value_too_large = EOVERFLOW,
618 wrong_protocol_type = EPROTOTYPE
624 operator int() const {return v_;}
627 template <> struct is_error_condition_enum<errc> : true_type { };
629 template <> struct is_error_condition_enum<errc::_> : true_type { };
631 class error_condition;
634 // class error_category
641 virtual ~error_category();
645 error_category(const error_category&);// = delete;
646 error_category& operator=(const error_category&);// = delete;
649 virtual const char* name() const = 0;
650 virtual error_condition default_error_condition(int _ev) const;
651 virtual bool equivalent(int _code, const error_condition& _condition) const;
652 virtual bool equivalent(const error_code& _code, int _condition) const;
653 virtual std::string message(int _ev) const = 0;
655 bool operator==(const error_category& _rhs) const {return this == &_rhs;}
657 bool operator!=(const error_category& _rhs) const {return !(*this == _rhs);}
659 bool operator< (const error_category& _rhs) const {return this < &_rhs;}
661 friend class _do_message;
664 class _do_message : public error_category
667 virtual std::string message(int ev) const;
670 const error_category& generic_category();
671 const error_category& system_category();
673 class error_condition
676 const error_category* _cat_;
678 error_condition() : _val_(0), _cat_(&generic_category()) {}
680 error_condition(int _val, const error_category& _cat)
681 : _val_(_val), _cat_(&_cat) {}
684 error_condition(E _e, typename enable_if_c<
685 is_error_condition_enum<E>::value
687 {*this = make_error_condition(_e);}
689 void assign(int _val, const error_category& _cat) {
697 is_error_condition_enum<E>::value,
701 {*this = make_error_condition(_e); return *this;}
705 _cat_ = &generic_category();
708 int value() const {return _val_;}
710 const error_category& category() const {return *_cat_;}
711 std::string message() const;
714 operator bool() const {return _val_ != 0;}
717 inline error_condition make_error_condition(errc _e) {
718 return error_condition(static_cast<int>(_e), generic_category());
721 inline bool operator<(const error_condition& _x, const error_condition& _y) {
722 return _x.category() < _y.category()
723 || (_x.category() == _y.category() && _x.value() < _y.value());
730 const error_category* _cat_;
732 error_code() : _val_(0), _cat_(&system_category()) {}
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;
773 operator bool() const {return _val_ != 0;}
776 inline error_code make_error_code(errc _e) {
777 return error_code(static_cast<int>(_e), generic_category());
780 inline bool operator<(const error_code& _x, const error_code& _y) {
781 return _x.category() < _y.category()
782 || (_x.category() == _y.category() && _x.value() < _y.value());
785 inline bool operator==(const error_code& _x, const error_code& _y) {
786 return _x.category() == _y.category() && _x.value() == _y.value();
789 inline bool operator==(const error_code& _x, const error_condition& _y) {
790 return _x.category().equivalent(_x.value(), _y)
791 || _y.category().equivalent(_x, _y.value());
794 inline bool operator==(const error_condition& _x, const error_code& _y) {
798 inline bool operator==(const error_condition& _x, const error_condition& _y) {
799 return _x.category() == _y.category() && _x.value() == _y.value();
802 inline bool operator!=(const error_code& _x, const error_code& _y) {
806 inline bool operator!=(const error_code& _x, const error_condition& _y) {
810 inline bool operator!=(const error_condition& _x, const error_code& _y) {
814 inline bool operator!=(const error_condition& _x, const error_condition& _y) {
820 } // end namespace llvm
822 // This needs to stay here for KillTheDoctor.
824 // FIXME: These two headers really really really need to be removed from here.
825 // Not only is it a violation of System, they define the stupid min and
828 #include <WinError.h>
832 // To construct an error_code after an API error:
834 // error_code( ::GetLastError(), system_category() )
835 struct windows_error {
838 // These names and values are based on Windows winerror.h
839 // This is not a complete list.
840 invalid_function = ERROR_INVALID_FUNCTION,
841 file_not_found = ERROR_FILE_NOT_FOUND,
842 path_not_found = ERROR_PATH_NOT_FOUND,
843 too_many_open_files = ERROR_TOO_MANY_OPEN_FILES,
844 access_denied = ERROR_ACCESS_DENIED,
845 invalid_handle = ERROR_INVALID_HANDLE,
846 arena_trashed = ERROR_ARENA_TRASHED,
847 not_enough_memory = ERROR_NOT_ENOUGH_MEMORY,
848 invalid_block = ERROR_INVALID_BLOCK,
849 bad_environment = ERROR_BAD_ENVIRONMENT,
850 bad_format = ERROR_BAD_FORMAT,
851 invalid_access = ERROR_INVALID_ACCESS,
852 outofmemory = ERROR_OUTOFMEMORY,
853 invalid_drive = ERROR_INVALID_DRIVE,
854 current_directory = ERROR_CURRENT_DIRECTORY,
855 not_same_device = ERROR_NOT_SAME_DEVICE,
856 no_more_files = ERROR_NO_MORE_FILES,
857 write_protect = ERROR_WRITE_PROTECT,
858 bad_unit = ERROR_BAD_UNIT,
859 not_ready = ERROR_NOT_READY,
860 bad_command = ERROR_BAD_COMMAND,
862 bad_length = ERROR_BAD_LENGTH,
864 not_dos_disk = ERROR_NOT_DOS_DISK,
865 sector_not_found = ERROR_SECTOR_NOT_FOUND,
866 out_of_paper = ERROR_OUT_OF_PAPER,
867 write_fault = ERROR_WRITE_FAULT,
868 read_fault = ERROR_READ_FAULT,
869 gen_failure = ERROR_GEN_FAILURE,
870 sharing_violation = ERROR_SHARING_VIOLATION,
871 lock_violation = ERROR_LOCK_VIOLATION,
872 wrong_disk = ERROR_WRONG_DISK,
873 sharing_buffer_exceeded = ERROR_SHARING_BUFFER_EXCEEDED,
874 handle_eof = ERROR_HANDLE_EOF,
875 handle_disk_full = ERROR_HANDLE_DISK_FULL,
876 rem_not_list = ERROR_REM_NOT_LIST,
877 dup_name = ERROR_DUP_NAME,
878 bad_net_path = ERROR_BAD_NETPATH,
879 network_busy = ERROR_NETWORK_BUSY,
880 file_exists = ERROR_FILE_EXISTS,
881 cannot_make = ERROR_CANNOT_MAKE,
882 broken_pipe = ERROR_BROKEN_PIPE,
883 open_failed = ERROR_OPEN_FAILED,
884 buffer_overflow = ERROR_BUFFER_OVERFLOW,
885 disk_full = ERROR_DISK_FULL,
886 lock_failed = ERROR_LOCK_FAILED,
888 cancel_violation = ERROR_CANCEL_VIOLATION,
889 already_exists = ERROR_ALREADY_EXISTS
893 windows_error(_ v) : v_(v) {}
894 explicit windows_error(DWORD v) : v_(_(v)) {}
895 operator int() const {return v_;}
899 template <> struct is_error_code_enum<windows_error> : true_type { };
901 template <> struct is_error_code_enum<windows_error::_> : true_type { };
903 inline error_code make_error_code(windows_error e) {
904 return error_code(static_cast<int>(e), system_category());
907 } // end namespace llvm
909 #endif // LLVM_ON_WINDOWS