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.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SYSTEM_SYSTEM_ERROR_H
15 #define LLVM_SYSTEM_SYSTEM_ERROR_H
26 virtual ~error_category();
28 error_category(const error_category&) = delete;
29 error_category& operator=(const error_category&) = delete;
31 virtual const char* name() const = 0;
32 virtual error_condition default_error_condition(int ev) const;
33 virtual bool equivalent(int code, const error_condition& condition) const;
34 virtual bool equivalent(const error_code& code, int condition) const;
35 virtual std::string message(int ev) const = 0;
37 bool operator==(const error_category& rhs) const;
38 bool operator!=(const error_category& rhs) const;
39 bool operator<(const error_category& rhs) const;
42 const error_category& generic_category();
43 const error_category& system_category();
45 template <class T> struct is_error_code_enum
46 : public false_type {};
48 template <class T> struct is_error_condition_enum
49 : public false_type {};
56 error_code(int val, const error_category& cat);
57 template <class ErrorCodeEnum>
58 error_code(ErrorCodeEnum e);
61 void assign(int val, const error_category& cat);
62 template <class ErrorCodeEnum>
63 error_code& operator=(ErrorCodeEnum e);
68 const error_category& category() const;
69 error_condition default_error_condition() const;
70 std::string message() const;
71 explicit operator bool() const;
74 // non-member functions:
75 bool operator<(const error_code& lhs, const error_code& rhs);
76 template <class charT, class traits>
77 basic_ostream<charT,traits>&
78 operator<<(basic_ostream<charT,traits>& os, const error_code& ec);
85 error_condition(int val, const error_category& cat);
86 template <class ErrorConditionEnum>
87 error_condition(ErrorConditionEnum e);
90 void assign(int val, const error_category& cat);
91 template <class ErrorConditionEnum>
92 error_condition& operator=(ErrorConditionEnum e);
97 const error_category& category() const;
98 std::string message() const;
99 explicit operator bool() const;
102 bool operator<(const error_condition& lhs, const error_condition& rhs);
105 : public runtime_error
108 system_error(error_code ec, const std::string& what_arg);
109 system_error(error_code ec, const char* what_arg);
110 system_error(error_code ec);
111 system_error(int ev, const error_category& ecat, const std::string& what_arg);
112 system_error(int ev, const error_category& ecat, const char* what_arg);
113 system_error(int ev, const error_category& ecat);
115 const error_code& code() const throw();
116 const char* what() const throw();
121 address_family_not_supported, // EAFNOSUPPORT
122 address_in_use, // EADDRINUSE
123 address_not_available, // EADDRNOTAVAIL
124 already_connected, // EISCONN
125 argument_list_too_long, // E2BIG
126 argument_out_of_domain, // EDOM
127 bad_address, // EFAULT
128 bad_file_descriptor, // EBADF
129 bad_message, // EBADMSG
130 broken_pipe, // EPIPE
131 connection_aborted, // ECONNABORTED
132 connection_already_in_progress, // EALREADY
133 connection_refused, // ECONNREFUSED
134 connection_reset, // ECONNRESET
135 cross_device_link, // EXDEV
136 destination_address_required, // EDESTADDRREQ
137 device_or_resource_busy, // EBUSY
138 directory_not_empty, // ENOTEMPTY
139 executable_format_error, // ENOEXEC
140 file_exists, // EEXIST
141 file_too_large, // EFBIG
142 filename_too_long, // ENAMETOOLONG
143 function_not_supported, // ENOSYS
144 host_unreachable, // EHOSTUNREACH
145 identifier_removed, // EIDRM
146 illegal_byte_sequence, // EILSEQ
147 inappropriate_io_control_operation, // ENOTTY
148 interrupted, // EINTR
149 invalid_argument, // EINVAL
150 invalid_seek, // ESPIPE
152 is_a_directory, // EISDIR
153 message_size, // EMSGSIZE
154 network_down, // ENETDOWN
155 network_reset, // ENETRESET
156 network_unreachable, // ENETUNREACH
157 no_buffer_space, // ENOBUFS
158 no_child_process, // ECHILD
160 no_lock_available, // ENOLCK
161 no_message_available, // ENODATA
162 no_message, // ENOMSG
163 no_protocol_option, // ENOPROTOOPT
164 no_space_on_device, // ENOSPC
165 no_stream_resources, // ENOSR
166 no_such_device_or_address, // ENXIO
167 no_such_device, // ENODEV
168 no_such_file_or_directory, // ENOENT
169 no_such_process, // ESRCH
170 not_a_directory, // ENOTDIR
171 not_a_socket, // ENOTSOCK
172 not_a_stream, // ENOSTR
173 not_connected, // ENOTCONN
174 not_enough_memory, // ENOMEM
175 not_supported, // ENOTSUP
176 operation_canceled, // ECANCELED
177 operation_in_progress, // EINPROGRESS
178 operation_not_permitted, // EPERM
179 operation_not_supported, // EOPNOTSUPP
180 operation_would_block, // EWOULDBLOCK
181 owner_dead, // EOWNERDEAD
182 permission_denied, // EACCES
183 protocol_error, // EPROTO
184 protocol_not_supported, // EPROTONOSUPPORT
185 read_only_file_system, // EROFS
186 resource_deadlock_would_occur, // EDEADLK
187 resource_unavailable_try_again, // EAGAIN
188 result_out_of_range, // ERANGE
189 state_not_recoverable, // ENOTRECOVERABLE
190 stream_timeout, // ETIME
191 text_file_busy, // ETXTBSY
192 timed_out, // ETIMEDOUT
193 too_many_files_open_in_system, // ENFILE
194 too_many_files_open, // EMFILE
195 too_many_links, // EMLINK
196 too_many_symbolic_link_levels, // ELOOP
197 value_too_large, // EOVERFLOW
198 wrong_protocol_type // EPROTOTYPE
201 template <> struct is_error_condition_enum<errc> : true_type { }
203 error_code make_error_code(errc e);
204 error_condition make_error_condition(errc e);
206 // Comparison operators:
207 bool operator==(const error_code& lhs, const error_code& rhs);
208 bool operator==(const error_code& lhs, const error_condition& rhs);
209 bool operator==(const error_condition& lhs, const error_code& rhs);
210 bool operator==(const error_condition& lhs, const error_condition& rhs);
211 bool operator!=(const error_code& lhs, const error_code& rhs);
212 bool operator!=(const error_code& lhs, const error_condition& rhs);
213 bool operator!=(const error_condition& lhs, const error_code& rhs);
214 bool operator!=(const error_condition& lhs, const error_condition& rhs);
216 template <> struct hash<std::error_code>;
222 #include "llvm/Config/config.h"
223 #include "llvm/Support/type_traits.h"
228 // VS 2008 needs this for some of the defines below.
229 # include <WinSock2.h>
231 // The following numbers were taken from VS2010.
232 # ifndef EAFNOSUPPORT
233 # define EAFNOSUPPORT WSAEAFNOSUPPORT
236 # define EADDRINUSE WSAEADDRINUSE
238 # ifndef EADDRNOTAVAIL
239 # define EADDRNOTAVAIL WSAEADDRNOTAVAIL
242 # define EISCONN WSAEISCONN
245 # define E2BIG WSAE2BIG
248 # define EDOM WSAEDOM
251 # define EFAULT WSAEFAULT
254 # define EBADF WSAEBADF
260 # define EPIPE WSAEPIPE
262 # ifndef ECONNABORTED
263 # define ECONNABORTED WSAECONNABORTED
266 # define EALREADY WSAEALREADY
268 # ifndef ECONNREFUSED
269 # define ECONNREFUSED WSAECONNREFUSED
272 # define ECONNRESET WSAECONNRESET
275 # define EXDEV WSAEXDEV
277 # ifndef EDESTADDRREQ
278 # define EDESTADDRREQ WSAEDESTADDRREQ
281 # define EBUSY WSAEBUSY
284 # define ENOTEMPTY WSAENOTEMPTY
287 # define ENOEXEC WSAENOEXEC
290 # define EEXIST WSAEEXIST
293 # define EFBIG WSAEFBIG
295 # ifndef ENAMETOOLONG
296 # define ENAMETOOLONG WSAENAMETOOLONG
299 # define ENOSYS WSAENOSYS
301 # ifndef EHOSTUNREACH
302 # define EHOSTUNREACH WSAEHOSTUNREACH
308 # define EILSEQ WSAEILSEQ
311 # define ENOTTY WSAENOTTY
314 # define EINTR WSAEINTR
317 # define EINVAL WSAEINVAL
320 # define ESPIPE WSAESPIPE
326 # define EISDIR WSAEISDIR
329 # define EMSGSIZE WSAEMSGSIZE
332 # define ENETDOWN WSAENETDOWN
335 # define ENETRESET WSAENETRESET
338 # define ENETUNREACH WSAENETUNREACH
341 # define ENOBUFS WSAENOBUFS
344 # define ECHILD WSAECHILD
350 # define ENOLCK WSAENOLCK
359 # define ENOPROTOOPT WSAENOPROTOOPT
362 # define ENOSPC WSAENOSPC
368 # define ENXIO WSAENXIO
371 # define ENODEV WSAENODEV
374 # define ENOENT WSAENOENT
377 # define ESRCH WSAESRCH
380 # define ENOTDIR WSAENOTDIR
383 # define ENOTSOCK WSAENOTSOCK
389 # define ENOTCONN WSAENOTCONN
392 # define ENOMEM WSAENOMEM
398 # define ECANCELED 105
401 # define EINPROGRESS WSAEINPROGRESS
404 # define EPERM WSAEPERM
407 # define EOPNOTSUPP WSAEOPNOTSUPP
410 # define EWOULDBLOCK WSAEWOULDBLOCK
413 # define EOWNERDEAD 133
416 # define EACCES WSAEACCES
421 # ifndef EPROTONOSUPPORT
422 # define EPROTONOSUPPORT WSAEPROTONOSUPPORT
425 # define EROFS WSAEROFS
428 # define EDEADLK WSAEDEADLK
431 # define EAGAIN WSAEAGAIN
434 # define ERANGE WSAERANGE
436 # ifndef ENOTRECOVERABLE
437 # define ENOTRECOVERABLE 127
446 # define ETIMEDOUT WSAETIMEDOUT
449 # define ENFILE WSAENFILE
452 # define EMFILE WSAEMFILE
455 # define EMLINK WSAEMLINK
458 # define ELOOP WSAELOOP
461 # define EOVERFLOW 132
464 # define EPROTOTYPE WSAEPROTOTYPE
470 template <class T, T v>
471 struct integral_constant {
472 typedef T value_type;
473 static const value_type value = v;
474 typedef integral_constant<T,v> type;
475 operator value_type() { return value; }
478 typedef integral_constant<bool, true> true_type;
479 typedef integral_constant<bool, false> false_type;
481 // is_error_code_enum
483 template <class Tp> struct is_error_code_enum : public false_type {};
485 // is_error_condition_enum
487 template <class Tp> struct is_error_condition_enum : public false_type {};
489 // Some error codes are not present on all platforms, so we provide equivalents
496 address_family_not_supported = EAFNOSUPPORT,
497 address_in_use = EADDRINUSE,
498 address_not_available = EADDRNOTAVAIL,
499 already_connected = EISCONN,
500 argument_list_too_long = E2BIG,
501 argument_out_of_domain = EDOM,
502 bad_address = EFAULT,
503 bad_file_descriptor = EBADF,
504 bad_message = EBADMSG,
506 connection_aborted = ECONNABORTED,
507 connection_already_in_progress = EALREADY,
508 connection_refused = ECONNREFUSED,
509 connection_reset = ECONNRESET,
510 cross_device_link = EXDEV,
511 destination_address_required = EDESTADDRREQ,
512 device_or_resource_busy = EBUSY,
513 directory_not_empty = ENOTEMPTY,
514 executable_format_error = ENOEXEC,
515 file_exists = EEXIST,
516 file_too_large = EFBIG,
517 filename_too_long = ENAMETOOLONG,
518 function_not_supported = ENOSYS,
519 host_unreachable = EHOSTUNREACH,
520 identifier_removed = EIDRM,
521 illegal_byte_sequence = EILSEQ,
522 inappropriate_io_control_operation = ENOTTY,
524 invalid_argument = EINVAL,
525 invalid_seek = ESPIPE,
527 is_a_directory = EISDIR,
528 message_size = EMSGSIZE,
529 network_down = ENETDOWN,
530 network_reset = ENETRESET,
531 network_unreachable = ENETUNREACH,
532 no_buffer_space = ENOBUFS,
533 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,
563 operation_canceled = ECANCELED,
564 operation_in_progress = EINPROGRESS,
565 operation_not_permitted = EPERM,
566 operation_not_supported = EOPNOTSUPP,
567 operation_would_block = EWOULDBLOCK,
568 owner_dead = EOWNERDEAD,
569 permission_denied = EACCES,
570 protocol_error = EPROTO,
571 protocol_not_supported = EPROTONOSUPPORT,
572 read_only_file_system = EROFS,
573 resource_deadlock_would_occur = EDEADLK,
574 resource_unavailable_try_again = EAGAIN,
575 result_out_of_range = ERANGE,
576 state_not_recoverable = ENOTRECOVERABLE,
578 stream_timeout = ETIME,
580 stream_timeout = ETIMEDOUT,
582 text_file_busy = ETXTBSY,
583 timed_out = ETIMEDOUT,
584 too_many_files_open_in_system = ENFILE,
585 too_many_files_open = EMFILE,
586 too_many_links = EMLINK,
587 too_many_symbolic_link_levels = ELOOP,
588 value_too_large = EOVERFLOW,
589 wrong_protocol_type = EPROTOTYPE
595 operator int() const {return v_;}
598 template <> struct is_error_condition_enum<errc> : true_type { };
600 template <> struct is_error_condition_enum<errc::_> : true_type { };
602 class error_condition;
605 // class error_category
612 virtual ~error_category();
616 error_category(const error_category&);// = delete;
617 error_category& operator=(const error_category&);// = delete;
620 virtual const char* name() const = 0;
621 virtual error_condition default_error_condition(int _ev) const;
622 virtual bool equivalent(int _code, const error_condition& _condition) const;
623 virtual bool equivalent(const error_code& _code, int _condition) const;
624 virtual std::string message(int _ev) const = 0;
626 bool operator==(const error_category& _rhs) const {return this == &_rhs;}
628 bool operator!=(const error_category& _rhs) const {return !(*this == _rhs);}
630 bool operator< (const error_category& _rhs) const {return this < &_rhs;}
632 friend class _do_message;
635 class _do_message : public error_category
638 virtual std::string message(int ev) const;
641 const error_category& generic_category();
642 const error_category& system_category();
644 class error_condition
647 const error_category* _cat_;
649 error_condition() : _val_(0), _cat_(&generic_category()) {}
651 error_condition(int _val, const error_category& _cat)
652 : _val_(_val), _cat_(&_cat) {}
655 error_condition(E _e, typename enable_if_c<
656 is_error_condition_enum<E>::value
658 {*this = make_error_condition(_e);}
660 void assign(int _val, const error_category& _cat) {
668 is_error_condition_enum<E>::value,
672 {*this = make_error_condition(_e); return *this;}
676 _cat_ = &generic_category();
679 int value() const {return _val_;}
681 const error_category& category() const {return *_cat_;}
682 std::string message() const;
685 operator bool() const {return _val_ != 0;}
688 inline error_condition make_error_condition(errc _e) {
689 return error_condition(static_cast<int>(_e), generic_category());
692 inline bool operator<(const error_condition& _x, const error_condition& _y) {
693 return _x.category() < _y.category()
694 || _x.category() == _y.category() && _x.value() < _y.value();
701 const error_category* _cat_;
703 error_code() : _val_(0), _cat_(&system_category()) {}
705 error_code(int _val, const error_category& _cat)
706 : _val_(_val), _cat_(&_cat) {}
709 error_code(E _e, typename enable_if_c<
710 is_error_code_enum<E>::value
712 *this = make_error_code(_e);
715 void assign(int _val, const error_category& _cat) {
723 is_error_code_enum<E>::value,
727 {*this = make_error_code(_e); return *this;}
731 _cat_ = &system_category();
734 int value() const {return _val_;}
736 const error_category& category() const {return *_cat_;}
738 error_condition default_error_condition() const
739 {return _cat_->default_error_condition(_val_);}
741 std::string message() const;
744 operator bool() const {return _val_ != 0;}
747 inline error_code make_error_code(errc _e) {
748 return error_code(static_cast<int>(_e), generic_category());
751 inline bool operator<(const error_code& _x, const error_code& _y) {
752 return _x.category() < _y.category()
753 || _x.category() == _y.category() && _x.value() < _y.value();
756 inline bool operator==(const error_code& _x, const error_code& _y) {
757 return _x.category() == _y.category() && _x.value() == _y.value();
760 inline bool operator==(const error_code& _x, const error_condition& _y) {
761 return _x.category().equivalent(_x.value(), _y)
762 || _y.category().equivalent(_x, _y.value());
765 inline bool operator==(const error_condition& _x, const error_code& _y) {
769 inline bool operator==(const error_condition& _x, const error_condition& _y) {
770 return _x.category() == _y.category() && _x.value() == _y.value();
773 inline bool operator!=(const error_code& _x, const error_code& _y) {
777 inline bool operator!=(const error_code& _x, const error_condition& _y) {
781 inline bool operator!=(const error_condition& _x, const error_code& _y) {
785 inline bool operator!=(const error_condition& _x, const error_condition& _y) {
791 class system_error : public std::runtime_error {
794 system_error(error_code _ec, const std::string& _what_arg);
795 system_error(error_code _ec, const char* _what_arg);
796 system_error(error_code _ec);
797 system_error(int _ev, const error_category& _ecat,
798 const std::string& _what_arg);
799 system_error(int _ev, const error_category& _ecat, const char* _what_arg);
800 system_error(int _ev, const error_category& _ecat);
801 ~system_error() throw();
803 const error_code& code() const throw() {return _ec_;}
806 static std::string _init(const error_code&, std::string);
809 void _throw_system_error(int ev, const char* what_arg);
811 } // end namespace llvm
815 #include <WinError.h>
819 // To construct an error_code after a API error:
821 // error_code( ::GetLastError(), system_category() )
822 struct windows_error {
825 // These names and values are based on Windows winerror.h
826 invalid_function = ERROR_INVALID_FUNCTION,
827 file_not_found = ERROR_FILE_NOT_FOUND,
828 path_not_found = ERROR_PATH_NOT_FOUND,
829 too_many_open_files = ERROR_TOO_MANY_OPEN_FILES,
830 access_denied = ERROR_ACCESS_DENIED,
831 invalid_handle = ERROR_INVALID_HANDLE,
832 arena_trashed = ERROR_ARENA_TRASHED,
833 not_enough_memory = ERROR_NOT_ENOUGH_MEMORY,
834 invalid_block = ERROR_INVALID_BLOCK,
835 bad_environment = ERROR_BAD_ENVIRONMENT,
836 bad_format = ERROR_BAD_FORMAT,
837 invalid_access = ERROR_INVALID_ACCESS,
838 outofmemory = ERROR_OUTOFMEMORY,
839 invalid_drive = ERROR_INVALID_DRIVE,
840 current_directory = ERROR_CURRENT_DIRECTORY,
841 not_same_device = ERROR_NOT_SAME_DEVICE,
842 no_more_files = ERROR_NO_MORE_FILES,
843 write_protect = ERROR_WRITE_PROTECT,
844 bad_unit = ERROR_BAD_UNIT,
845 not_ready = ERROR_NOT_READY,
846 bad_command = ERROR_BAD_COMMAND,
848 bad_length = ERROR_BAD_LENGTH,
850 not_dos_disk = ERROR_NOT_DOS_DISK,
851 sector_not_found = ERROR_SECTOR_NOT_FOUND,
852 out_of_paper = ERROR_OUT_OF_PAPER,
853 write_fault = ERROR_WRITE_FAULT,
854 read_fault = ERROR_READ_FAULT,
855 gen_failure = ERROR_GEN_FAILURE,
856 sharing_violation = ERROR_SHARING_VIOLATION,
857 lock_violation = ERROR_LOCK_VIOLATION,
858 wrong_disk = ERROR_WRONG_DISK,
859 sharing_buffer_exceeded = ERROR_SHARING_BUFFER_EXCEEDED,
860 handle_eof = ERROR_HANDLE_EOF,
861 handle_disk_full= ERROR_HANDLE_DISK_FULL,
862 rem_not_list = ERROR_REM_NOT_LIST,
863 dup_name = ERROR_DUP_NAME,
864 bad_net_path = ERROR_BAD_NETPATH,
865 network_busy = ERROR_NETWORK_BUSY,
867 file_exists = ERROR_FILE_EXISTS,
868 cannot_make = ERROR_CANNOT_MAKE,
870 broken_pipe = ERROR_BROKEN_PIPE,
871 open_failed = ERROR_OPEN_FAILED,
872 buffer_overflow = ERROR_BUFFER_OVERFLOW,
873 disk_full= ERROR_DISK_FULL,
875 lock_failed = ERROR_LOCK_FAILED,
877 cancel_violation = ERROR_CANCEL_VIOLATION,
878 already_exists = ERROR_ALREADY_EXISTS
881 // TODO: add more Windows errors
885 windows_error(_ v) : v_(v) {}
886 explicit windows_error(DWORD v) : v_(_(v)) {}
887 operator int() const {return v_;}
891 template <> struct is_error_code_enum<windows_error> : true_type { };
893 template <> struct is_error_code_enum<windows_error::_> : 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
901 #endif // LLVM_ON_WINDOWS