c17e7bf7983381aadd896936a12e676bd7e09f32
[oota-llvm.git] / include / llvm / System / system_error.h
1 //===---------------------------- system_error ----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_SYSTEM_SYSTEM_ERROR_H
18 #define LLVM_SYSTEM_SYSTEM_ERROR_H
19
20 /*
21     system_error synopsis
22
23 namespace std
24 {
25
26 class error_category
27 {
28 public:
29     virtual ~error_category();
30
31     error_category(const error_category&) = delete;
32     error_category& operator=(const error_category&) = delete;
33
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;
39
40     bool operator==(const error_category& rhs) const;
41     bool operator!=(const error_category& rhs) const;
42     bool operator<(const error_category& rhs) const;
43 };
44
45 const error_category& generic_category();
46 const error_category& system_category();
47
48 template <class T> struct is_error_code_enum
49     : public false_type {};
50
51 template <class T> struct is_error_condition_enum
52     : public false_type {};
53
54 class error_code
55 {
56 public:
57     // constructors:
58     error_code();
59     error_code(int val, const error_category& cat);
60     template <class ErrorCodeEnum>
61         error_code(ErrorCodeEnum e);
62
63     // modifiers:
64     void assign(int val, const error_category& cat);
65     template <class ErrorCodeEnum>
66         error_code& operator=(ErrorCodeEnum e);
67     void clear();
68
69     // observers:
70     int value() const;
71     const error_category& category() const;
72     error_condition default_error_condition() const;
73     std::string message() const;
74     explicit operator bool() const;
75 };
76
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);
82
83 class error_condition
84 {
85 public:
86     // constructors:
87     error_condition();
88     error_condition(int val, const error_category& cat);
89     template <class ErrorConditionEnum>
90         error_condition(ErrorConditionEnum e);
91
92     // modifiers:
93     void assign(int val, const error_category& cat);
94     template <class ErrorConditionEnum>
95         error_condition& operator=(ErrorConditionEnum e);
96     void clear();
97
98     // observers:
99     int value() const;
100     const error_category& category() const;
101     std::string message() const;
102     explicit operator bool() const;
103 };
104
105 bool operator<(const error_condition& lhs, const error_condition& rhs);
106
107 class system_error
108     : public runtime_error
109 {
110 public:
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);
117
118     const error_code& code() const throw();
119     const char* what() const throw();
120 };
121
122 enum class errc
123 {
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
154     io_error,                           // EIO
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
162     no_link,                            // ENOLINK
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
202 };
203
204 template <> struct is_error_condition_enum<errc> : true_type { }
205
206 error_code make_error_code(errc e);
207 error_condition make_error_condition(errc e);
208
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);
218
219 template <> struct hash<std::error_code>;
220
221 }  // std
222
223 */
224
225 #include "llvm/Config/config.h"
226 #include "llvm/Support/type_traits.h"
227 #include <cerrno>
228 #include <string>
229
230 // This must be here instead of a .inc file because it is used in the definition
231 // of the enum values below.
232 #ifdef LLVM_ON_WIN32
233   // VS 2008 needs this for some of the defines below.
234 # include <WinSock2.h>
235
236   // The following numbers were taken from VS2010.
237 # ifndef EAFNOSUPPORT
238 #   define EAFNOSUPPORT WSAEAFNOSUPPORT
239 # endif
240 # ifndef EADDRINUSE
241 #   define EADDRINUSE WSAEADDRINUSE
242 # endif
243 # ifndef EADDRNOTAVAIL
244 #   define EADDRNOTAVAIL WSAEADDRNOTAVAIL
245 # endif
246 # ifndef EISCONN
247 #   define EISCONN WSAEISCONN
248 # endif
249 # ifndef E2BIG
250 #   define E2BIG WSAE2BIG
251 # endif
252 # ifndef EDOM
253 #   define EDOM WSAEDOM
254 # endif
255 # ifndef EFAULT
256 #   define EFAULT WSAEFAULT
257 # endif
258 # ifndef EBADF
259 #   define EBADF WSAEBADF
260 # endif
261 # ifndef EBADMSG
262 #   define EBADMSG 104
263 # endif
264 # ifndef EPIPE
265 #   define EPIPE WSAEPIPE
266 # endif
267 # ifndef ECONNABORTED
268 #   define ECONNABORTED WSAECONNABORTED
269 # endif
270 # ifndef EALREADY
271 #   define EALREADY WSAEALREADY
272 # endif
273 # ifndef ECONNREFUSED
274 #   define ECONNREFUSED WSAECONNREFUSED
275 # endif
276 # ifndef ECONNRESET
277 #   define ECONNRESET WSAECONNRESET
278 # endif
279 # ifndef EXDEV
280 #   define EXDEV WSAEXDEV
281 # endif
282 # ifndef EDESTADDRREQ
283 #   define EDESTADDRREQ WSAEDESTADDRREQ
284 # endif
285 # ifndef EBUSY
286 #   define EBUSY WSAEBUSY
287 # endif
288 # ifndef ENOTEMPTY
289 #   define ENOTEMPTY WSAENOTEMPTY
290 # endif
291 # ifndef ENOEXEC
292 #   define ENOEXEC WSAENOEXEC
293 # endif
294 # ifndef EEXIST
295 #   define EEXIST WSAEEXIST
296 # endif
297 # ifndef EFBIG
298 #   define EFBIG WSAEFBIG
299 # endif
300 # ifndef ENAMETOOLONG
301 #   define ENAMETOOLONG WSAENAMETOOLONG
302 # endif
303 # ifndef ENOSYS
304 #   define ENOSYS WSAENOSYS
305 # endif
306 # ifndef EHOSTUNREACH
307 #   define EHOSTUNREACH WSAEHOSTUNREACH
308 # endif
309 # ifndef EIDRM
310 #   define EIDRM 111
311 # endif
312 # ifndef EILSEQ
313 #   define EILSEQ WSAEILSEQ
314 # endif
315 # ifndef ENOTTY
316 #   define ENOTTY WSAENOTTY
317 # endif
318 # ifndef EINTR
319 #   define EINTR WSAEINTR
320 # endif
321 # ifndef EINVAL
322 #   define EINVAL WSAEINVAL
323 # endif
324 # ifndef ESPIPE
325 #   define ESPIPE WSAESPIPE
326 # endif
327 # ifndef EIO
328 #   define EIO WSAEIO
329 # endif
330 # ifndef EISDIR
331 #   define EISDIR WSAEISDIR
332 # endif
333 # ifndef EMSGSIZE
334 #   define EMSGSIZE WSAEMSGSIZE
335 # endif
336 # ifndef ENETDOWN
337 #   define ENETDOWN WSAENETDOWN
338 # endif
339 # ifndef ENETRESET
340 #   define ENETRESET WSAENETRESET
341 # endif
342 # ifndef ENETUNREACH
343 #   define ENETUNREACH WSAENETUNREACH
344 # endif
345 # ifndef ENOBUFS
346 #   define ENOBUFS WSAENOBUFS
347 # endif
348 # ifndef ECHILD
349 #   define ECHILD WSAECHILD
350 # endif
351 # ifndef ENOLINK
352 #   define ENOLINK 121
353 # endif
354 # ifndef ENOLCK
355 #   define ENOLCK WSAENOLCK
356 # endif
357 # ifndef ENODATA
358 #   define ENODATA 120
359 # endif
360 # ifndef ENOMSG
361 #   define ENOMSG 122
362 # endif
363 # ifndef ENOPROTOOPT
364 #   define ENOPROTOOPT WSAENOPROTOOPT
365 # endif
366 # ifndef ENOSPC
367 #   define ENOSPC WSAENOSPC
368 # endif
369 # ifndef ENOSR
370 #   define ENOSR 124
371 # endif
372 # ifndef ENXIO
373 #   define ENXIO WSAENXIO
374 # endif
375 # ifndef ENODEV
376 #   define ENODEV WSAENODEV
377 # endif
378 # ifndef ENOENT
379 #   define ENOENT WSAENOENT
380 # endif
381 # ifndef ESRCH
382 #   define ESRCH WSAESRCH
383 # endif
384 # ifndef ENOTDIR
385 #   define ENOTDIR WSAENOTDIR
386 # endif
387 # ifndef ENOTSOCK
388 #   define ENOTSOCK WSAENOTSOCK
389 # endif
390 # ifndef ENOSTR
391 #   define ENOSTR 125
392 # endif
393 # ifndef ENOTCONN
394 #   define ENOTCONN WSAENOTCONN
395 # endif
396 # ifndef ENOMEM
397 #   define ENOMEM WSAENOMEM
398 # endif
399 # ifndef ENOTSUP
400 #   define ENOTSUP 129
401 # endif
402 # ifndef ECANCELED
403 #   define ECANCELED 105
404 # endif
405 # ifndef EINPROGRESS
406 #   define EINPROGRESS WSAEINPROGRESS
407 # endif
408 # ifndef EPERM
409 #   define EPERM WSAEPERM
410 # endif
411 # ifndef EOPNOTSUPP
412 #   define EOPNOTSUPP WSAEOPNOTSUPP
413 # endif
414 # ifndef EWOULDBLOCK
415 #   define EWOULDBLOCK WSAEWOULDBLOCK
416 # endif
417 # ifndef EOWNERDEAD
418 #   define EOWNERDEAD 133
419 # endif
420 # ifndef EACCES
421 #   define EACCES WSAEACCES
422 # endif
423 # ifndef EPROTO
424 #   define EPROTO 134
425 # endif
426 # ifndef EPROTONOSUPPORT
427 #   define EPROTONOSUPPORT WSAEPROTONOSUPPORT
428 # endif
429 # ifndef EROFS
430 #   define EROFS WSAEROFS
431 # endif
432 # ifndef EDEADLK
433 #   define EDEADLK WSAEDEADLK
434 # endif
435 # ifndef EAGAIN
436 #   define EAGAIN WSAEAGAIN
437 # endif
438 # ifndef ERANGE
439 #   define ERANGE WSAERANGE
440 # endif
441 # ifndef ENOTRECOVERABLE
442 #   define ENOTRECOVERABLE 127
443 # endif
444 # ifndef ETIME
445 #   define ETIME 137
446 # endif
447 # ifndef ETXTBSY
448 #   define ETXTBSY 139
449 # endif
450 # ifndef ETIMEDOUT
451 #   define ETIMEDOUT WSAETIMEDOUT
452 # endif
453 # ifndef ENFILE
454 #   define ENFILE WSAENFILE
455 # endif
456 # ifndef EMFILE
457 #   define EMFILE WSAEMFILE
458 # endif
459 # ifndef EMLINK
460 #   define EMLINK WSAEMLINK
461 # endif
462 # ifndef ELOOP
463 #   define ELOOP WSAELOOP
464 # endif
465 # ifndef EOVERFLOW
466 #   define EOVERFLOW 132
467 # endif
468 # ifndef EPROTOTYPE
469 #   define EPROTOTYPE WSAEPROTOTYPE
470 # endif
471 #endif
472
473 namespace llvm {
474
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; }
481 };
482
483 typedef integral_constant<bool, true> true_type;
484 typedef integral_constant<bool, false> false_type;
485
486 // is_error_code_enum
487
488 template <class Tp> struct is_error_code_enum : public false_type {};
489
490 // is_error_condition_enum
491
492 template <class Tp> struct is_error_condition_enum : public false_type {};
493
494 // Some error codes are not present on all platforms, so we provide equivalents
495 // for them:
496
497 //enum class errc
498 struct errc {
499 enum _ {
500   success                             = 0,
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,
509 #ifdef EBADMSG
510   bad_message                         = EBADMSG,
511 #else
512   bad_message                         = EINVAL,
513 #endif
514   broken_pipe                         = EPIPE,
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,
532   interrupted                         = EINTR,
533   invalid_argument                    = EINVAL,
534   invalid_seek                        = ESPIPE,
535   io_error                            = EIO,
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,
543 #ifdef ENOLINK
544   no_link                             = ENOLINK,
545 #else
546   no_link                             = EINVAL,
547 #endif
548   no_lock_available                   = ENOLCK,
549 #ifdef ENODATA
550   no_message_available                = ENODATA,
551 #else
552   no_message_available                = ENOMSG,
553 #endif
554   no_message                          = ENOMSG,
555   no_protocol_option                  = ENOPROTOOPT,
556   no_space_on_device                  = ENOSPC,
557 #ifdef ENOSR
558   no_stream_resources                 = ENOSR,
559 #else
560   no_stream_resources                 = ENOMEM,
561 #endif
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,
568 #ifdef ENOSTR
569   not_a_stream                        = ENOSTR,
570 #else
571   not_a_stream                        = EINVAL,
572 #endif
573   not_connected                       = ENOTCONN,
574   not_enough_memory                   = ENOMEM,
575   not_supported                       = ENOTSUP,
576 #ifdef ECANCELED
577   operation_canceled                  = ECANCELED,
578 #else
579   operation_canceled                  = EINVAL,
580 #endif
581   operation_in_progress               = EINPROGRESS,
582   operation_not_permitted             = EPERM,
583   operation_not_supported             = EOPNOTSUPP,
584   operation_would_block               = EWOULDBLOCK,
585 #ifdef EOWNERDEAD
586   owner_dead                          = EOWNERDEAD,
587 #else
588   owner_dead                          = EINVAL,
589 #endif
590   permission_denied                   = EACCES,
591 #ifdef EPROTO
592   protocol_error                      = EPROTO,
593 #else
594   protocol_error                      = EINVAL,
595 #endif
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,
603 #else
604   state_not_recoverable               = EINVAL,
605 #endif
606 #ifdef ETIME
607   stream_timeout                      = ETIME,
608 #else
609   stream_timeout                      = ETIMEDOUT,
610 #endif
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
619 };
620
621   _ v_;
622
623   errc(_ v) : v_(v) {}
624   operator int() const {return v_;}
625 };
626
627 template <> struct is_error_condition_enum<errc> : true_type { };
628
629 template <> struct is_error_condition_enum<errc::_> : true_type { };
630
631 class error_condition;
632 class error_code;
633
634 // class error_category
635
636 class _do_message;
637
638 class error_category
639 {
640 public:
641   virtual ~error_category();
642
643 private:
644   error_category();
645   error_category(const error_category&);// = delete;
646   error_category& operator=(const error_category&);// = delete;
647
648 public:
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;
654
655   bool operator==(const error_category& _rhs) const {return this == &_rhs;}
656
657   bool operator!=(const error_category& _rhs) const {return !(*this == _rhs);}
658
659   bool operator< (const error_category& _rhs) const {return this < &_rhs;}
660
661   friend class _do_message;
662 };
663
664 class _do_message : public error_category
665 {
666 public:
667   virtual std::string message(int ev) const;
668 };
669
670 const error_category& generic_category();
671 const error_category& system_category();
672
673 class error_condition
674 {
675   int _val_;
676   const error_category* _cat_;
677 public:
678   error_condition() : _val_(0), _cat_(&generic_category()) {}
679
680   error_condition(int _val, const error_category& _cat)
681     : _val_(_val), _cat_(&_cat) {}
682
683   template <class E>
684   error_condition(E _e, typename enable_if_c<
685                           is_error_condition_enum<E>::value
686                         >::type* = 0)
687     {*this = make_error_condition(_e);}
688
689   void assign(int _val, const error_category& _cat) {
690     _val_ = _val;
691     _cat_ = &_cat;
692   }
693
694   template <class E>
695     typename enable_if_c
696     <
697       is_error_condition_enum<E>::value,
698       error_condition&
699     >::type
700     operator=(E _e)
701       {*this = make_error_condition(_e); return *this;}
702
703   void clear() {
704     _val_ = 0;
705     _cat_ = &generic_category();
706   }
707
708   int value() const {return _val_;}
709
710   const error_category& category() const {return *_cat_;}
711   std::string message() const;
712
713   // explicit
714   operator bool() const {return _val_ != 0;}
715 };
716
717 inline error_condition make_error_condition(errc _e) {
718   return error_condition(static_cast<int>(_e), generic_category());
719 }
720
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());
724 }
725
726 // error_code
727
728 class error_code {
729   int _val_;
730   const error_category* _cat_;
731 public:
732   error_code() : _val_(0), _cat_(&system_category()) {}
733
734   error_code(int _val, const error_category& _cat)
735     : _val_(_val), _cat_(&_cat) {}
736
737   template <class E>
738   error_code(E _e, typename enable_if_c<
739                      is_error_code_enum<E>::value
740                    >::type* = 0) {
741     *this = make_error_code(_e);
742   }
743
744   void assign(int _val, const error_category& _cat) {
745       _val_ = _val;
746       _cat_ = &_cat;
747   }
748
749   template <class E>
750     typename enable_if_c
751     <
752       is_error_code_enum<E>::value,
753       error_code&
754     >::type
755     operator=(E _e)
756       {*this = make_error_code(_e); return *this;}
757
758   void clear() {
759     _val_ = 0;
760     _cat_ = &system_category();
761   }
762
763   int value() const {return _val_;}
764
765   const error_category& category() const {return *_cat_;}
766
767   error_condition default_error_condition() const
768     {return _cat_->default_error_condition(_val_);}
769
770   std::string message() const;
771
772   // explicit
773   operator bool() const {return _val_ != 0;}
774 };
775
776 inline error_code make_error_code(errc _e) {
777   return error_code(static_cast<int>(_e), generic_category());
778 }
779
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());
783 }
784
785 inline bool operator==(const error_code& _x, const error_code& _y) {
786   return _x.category() == _y.category() && _x.value() == _y.value();
787 }
788
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());
792 }
793
794 inline bool operator==(const error_condition& _x, const error_code& _y) {
795   return _y == _x;
796 }
797
798 inline bool operator==(const error_condition& _x, const error_condition& _y) {
799    return _x.category() == _y.category() && _x.value() == _y.value();
800 }
801
802 inline bool operator!=(const error_code& _x, const error_code& _y) {
803   return !(_x == _y);
804 }
805
806 inline bool operator!=(const error_code& _x, const error_condition& _y) {
807   return !(_x == _y);
808 }
809
810 inline bool operator!=(const error_condition& _x, const error_code& _y) {
811   return !(_x == _y);
812 }
813
814 inline bool operator!=(const error_condition& _x, const error_condition& _y) {
815   return !(_x == _y);
816 }
817
818 // system_error
819
820 } // end namespace llvm
821
822 // This needs to stay here for KillTheDoctor.
823 #ifdef LLVM_ON_WIN32
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
826 //        max macros :(.
827 #include <Windows.h>
828 #include <WinError.h>
829
830 namespace llvm {
831
832 //  To construct an error_code after an API error:
833 //
834 //      error_code( ::GetLastError(), system_category() )
835 struct windows_error {
836 enum _ {
837   success = 0,
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,
861   crc                     = ERROR_CRC,
862   bad_length              = ERROR_BAD_LENGTH,
863   seek                    = ERROR_SEEK,
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,
887   busy                    = ERROR_BUSY,
888   cancel_violation        = ERROR_CANCEL_VIOLATION,
889   already_exists          = ERROR_ALREADY_EXISTS
890 };
891   _ v_;
892
893   windows_error(_ v) : v_(v) {}
894   explicit windows_error(DWORD v) : v_(_(v)) {}
895   operator int() const {return v_;}
896 };
897
898
899 template <> struct is_error_code_enum<windows_error> : true_type { };
900
901 template <> struct is_error_code_enum<windows_error::_> : true_type { };
902
903 inline error_code make_error_code(windows_error e) {
904   return error_code(static_cast<int>(e), system_category());
905 }
906
907 } // end namespace llvm
908
909 #endif // LLVM_ON_WINDOWS
910
911 #endif