[libcxx] Rewrite C++03 __invoke.

Summary:
This patch rewrites the C++03 `__invoke` and related meta-programming. There are a number of major changes.

`__invoke` in C++03 now has a fallback overload for when the invoke expression is ill-formed (similar to C++11). This means that the `__invoke_return` traits will return `__nat` when `__invoke(...)` is ill formed. This would previously cause a compile error.

Bullets 1-4 of `__invoke` have been rewritten. In the old version `__invoke` had 32 overloads for bullets 1 and 2,
one for each possible cv-qualified function signature with arities 0-3. 64 overloads would be needed to support member functions
with varargs. Currently these overloads were fundamentally broken. An example overload looked like:
```
template <class Rp, class Tp, class T1, class A0>
Rp __invoke(Rp (Tp::*pm)(A0) const, T1&, A0&)
```
Because `A0` appeared in two different deducible contexts it would have to deduce to be an exact match or the overload
would be rejected. This is made even worse because `A0` appears without a reference qualifier in the member function signature
and with a reference qualifier as an `__invoke` parameter. This means that only member functions that took all
of their arguments by value could be matched.

One possible fix would be to make the second occurrence of `A0` appear in a non-deducible context. This way
any type convertible to `A0` could be passed as the first parameter. The benefit of this approach is that the
signature of the member function enforces the arity and types taken by the `__invoke` signature it generates. However
nothing in the `INVOKE` specification requires this behavior.

My solution is to use a `__invoke_enable_if<PM_Type, Tp>`  metafunction to selectively enable the `__invoke` overloads for bullets 1, 2, 3 and 4.  It uses `__member_function_traits` to inspect and extract the return type and class type of the pointer to member. Using `__member_function_traits` to inspect `PM_Type` also allows us to reduce the number of `__invoke` overloads from 32 to 8 and add
varargs support at the same time.

Because `__invoke_enable_if` knows the exact return type of `__invoke` for bullets 1-4 we no longer need to use `decltype(__invoke(...))` to
compute the return type in the `__invoke_return*` traits. This will reduce the problems caused by `#define decltype(X) __typeof__(X)` in C++03.

Tests for this change have already been committed. All tests in `test/std/utilities/function.objects` now pass in C++03, previously there were 20 failures.

Reviewers: K-ballo, howard.hinnant, mclow.lists

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D11553

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@246068 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/__functional_base b/include/__functional_base
index 3ff2e35..ef9cc03 100644
--- a/include/__functional_base
+++ b/include/__functional_base
@@ -515,44 +515,116 @@
 #ifndef _LIBCPP_HAS_NO_VARIADICS
     // invoke
     template <class... _ArgTypes>
-       _LIBCPP_INLINE_VISIBILITY
-       typename __invoke_of<type&, _ArgTypes...>::type
-          operator() (_ArgTypes&&... __args) const
-          {
-              return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
-          }
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_of<type&, _ArgTypes...>::type
+    operator() (_ArgTypes&&... __args) const {
+        return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
+    }
 #else
 
     _LIBCPP_INLINE_VISIBILITY
     typename __invoke_return<type>::type
-       operator() () const
-       {
-           return __invoke(get());
-       }
+    operator() () const {
+        return __invoke(get());
+    }
 
     template <class _A0>
-       _LIBCPP_INLINE_VISIBILITY
-       typename __invoke_return0<type&, _A0>::type
-          operator() (_A0& __a0) const
-          {
-              return __invoke<type&, _A0>(get(), __a0);
-          }
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_return0<type, _A0>::type
+    operator() (_A0& __a0) const {
+        return __invoke(get(), __a0);
+    }
+
+    template <class _A0>
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_return0<type, _A0 const>::type
+    operator() (_A0 const& __a0) const {
+        return __invoke(get(), __a0);
+    }
 
     template <class _A0, class _A1>
-       _LIBCPP_INLINE_VISIBILITY
-       typename __invoke_return1<type&, _A0, _A1>::type
-          operator() (_A0& __a0, _A1& __a1) const
-          {
-              return __invoke<type&, _A0, _A1>(get(), __a0, __a1);
-          }
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_return1<type, _A0, _A1>::type
+    operator() (_A0& __a0, _A1& __a1) const {
+        return __invoke(get(), __a0, __a1);
+    }
+
+    template <class _A0, class _A1>
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_return1<type, _A0 const, _A1>::type
+    operator() (_A0 const& __a0, _A1& __a1) const {
+        return __invoke(get(), __a0, __a1);
+    }
+
+    template <class _A0, class _A1>
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_return1<type, _A0, _A1 const>::type
+    operator() (_A0& __a0, _A1 const& __a1) const {
+        return __invoke(get(), __a0, __a1);
+    }
+
+    template <class _A0, class _A1>
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_return1<type, _A0 const, _A1 const>::type
+    operator() (_A0 const& __a0, _A1 const& __a1) const {
+        return __invoke(get(), __a0, __a1);
+    }
 
     template <class _A0, class _A1, class _A2>
-       _LIBCPP_INLINE_VISIBILITY
-       typename __invoke_return2<type&, _A0, _A1, _A2>::type
-          operator() (_A0& __a0, _A1& __a1, _A2& __a2) const
-          {
-              return __invoke<type&, _A0, _A1, _A2>(get(), __a0, __a1, __a2);
-          }
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_return2<type, _A0, _A1, _A2>::type
+    operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
+        return __invoke(get(), __a0, __a1, __a2);
+    }
+
+    template <class _A0, class _A1, class _A2>
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_return2<type, _A0 const, _A1, _A2>::type
+    operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
+        return __invoke(get(), __a0, __a1, __a2);
+    }
+
+    template <class _A0, class _A1, class _A2>
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_return2<type, _A0, _A1 const, _A2>::type
+    operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
+        return __invoke(get(), __a0, __a1, __a2);
+    }
+
+    template <class _A0, class _A1, class _A2>
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_return2<type, _A0, _A1, _A2 const>::type
+    operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
+        return __invoke(get(), __a0, __a1, __a2);
+    }
+
+    template <class _A0, class _A1, class _A2>
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
+    operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
+        return __invoke(get(), __a0, __a1, __a2);
+    }
+
+    template <class _A0, class _A1, class _A2>
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
+    operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
+        return __invoke(get(), __a0, __a1, __a2);
+    }
+
+    template <class _A0, class _A1, class _A2>
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
+    operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
+        return __invoke(get(), __a0, __a1, __a2);
+    }
+
+    template <class _A0, class _A1, class _A2>
+    _LIBCPP_INLINE_VISIBILITY
+    typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
+    operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
+        return __invoke(get(), __a0, __a1, __a2);
+    }
 #endif // _LIBCPP_HAS_NO_VARIADICS
 };