1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03
10 
11 // <functional>
12 
13 // class function<R(ArgTypes...)>
14 
15 // function(Fp);
16 
17 // Ensure that __not_null works for all function types.
18 // See https://llvm.org/PR23589
19 
20 //------------------------------------------------------------------------------
21 // TESTING std::function<...>::__not_null(Callable)
22 //
23 // Concerns:
24 //  1) The call __not_null(Callable) is well formed and correct for each
25 //     possible 'Callable' type category. These categories include:
26 //      1a) function pointers
27 //      1b) member function pointer
28 //      1c) member data pointer
29 //      1d) callable class type
30 //      1e) lambdas
31 //    Categories 1a, 1b, and 1c are 'Nullable' types. Only objects of these
32 //    types can be null. The other categories are not tested here.
33 //  3) '__not_null(Callable)' is well formed when the call signature includes
34 //      varargs.
35 //  4) '__not_null(Callable)' works for Callable types with all arities less
36 //     than or equal to 3 in C++03.
37 //  5) '__not_null(Callable)' works when 'Callable' is a member function
38 //     pointer to a cv or ref qualified function type.
39 //
40 // Plan:
41 //  1 For categories 1a, 1b and 1c define a set of
42 //    'Callable' objects for this category. This set should include examples
43 //    of arity 0, 1, 2 and possible 3 including versions with varargs as the
44 //    last parameter.
45 //
46 //  2 For each 'Callable' object in categories 1a, 1b and 1c do the following.
47 //
48 //    1 Define a type 'std::function<Sig>' as 'F' where 'Sig' is compatible with
49 //      the signature of the 'Callable' object.
50 //
51 //    2 Create an object of type 'F' using a null pointer of type 'Callable'.
52 //      Check that 'F.target<Callable>()' is null.
53 //
54 //    3 Create an object of type 'F' that is not null. Check that
55 //      'F.target<Callable>()' is not null and is equal to the original
56 //      argument.
57 
58 #include <functional>
59 #include <type_traits>
60 #include <cassert>
61 
62 #include "test_macros.h"
63 
64 ///////////////////////////////////////////////////////////////////////////////
foo()65 int foo() { return 42; }
foo(int)66 int foo(int) { return 42; }
foo(int,int)67 int foo(int, int) { return 42; }
foo(int,int,int)68 int foo(int, int, int) { return 42; }
69 
foo(...)70 int foo(...) { return 42; }
foo(int,...)71 int foo(int, ...) { return 42; }
foo(int,int,...)72 int foo(int, int, ...) { return 42; }
foo(int,int,int,...)73 int foo(int, int, int, ...) { return 42; }
74 
75 ///////////////////////////////////////////////////////////////////////////////
76 struct MemFun03 {
fooMemFun0377     int foo() { return 42; }
fooMemFun0378     int foo() const { return 42; }
fooMemFun0379     int foo() volatile { return 42; }
fooMemFun0380     int foo() const volatile { return 42; }
81 
fooMemFun0382     int foo(int) { return 42; }
fooMemFun0383     int foo(int) const { return 42; }
fooMemFun0384     int foo(int) volatile { return 42; }
fooMemFun0385     int foo(int) const volatile { return 42; }
86 
fooMemFun0387     int foo(int, int) { return 42; }
fooMemFun0388     int foo(int, int) const { return 42; }
fooMemFun0389     int foo(int, int) volatile { return 42; }
fooMemFun0390     int foo(int, int) const volatile { return 42; }
91 
fooMemFun0392     int foo(int, int, int) { return 42; }
fooMemFun0393     int foo(int, int, int) const { return 42; }
fooMemFun0394     int foo(int, int, int) volatile { return 42; }
fooMemFun0395     int foo(int, int, int) const volatile { return 42; }
96 
fooMemFun0397     int foo(...) { return 42; }
fooMemFun0398     int foo(...) const { return 42; }
fooMemFun0399     int foo(...) volatile { return 42; }
fooMemFun03100     int foo(...) const volatile { return 42; }
101 
fooMemFun03102     int foo(int, ...) { return 42; }
fooMemFun03103     int foo(int, ...) const { return 42; }
fooMemFun03104     int foo(int, ...) volatile { return 42; }
fooMemFun03105     int foo(int, ...) const volatile { return 42; }
106 
fooMemFun03107     int foo(int, int, ...) { return 42; }
fooMemFun03108     int foo(int, int, ...) const { return 42; }
fooMemFun03109     int foo(int, int, ...) volatile { return 42; }
fooMemFun03110     int foo(int, int, ...) const volatile { return 42; }
111 
fooMemFun03112     int foo(int, int, int, ...) { return 42; }
fooMemFun03113     int foo(int, int, int, ...) const { return 42; }
fooMemFun03114     int foo(int, int, int, ...) volatile { return 42; }
fooMemFun03115     int foo(int, int, int, ...) const volatile { return 42; }
116 };
117 
118 #if TEST_STD_VER >= 11
119 struct MemFun11 {
fooMemFun11120     int foo() & { return 42; }
fooMemFun11121     int foo() const & { return 42; }
fooMemFun11122     int foo() volatile & { return 42; }
fooMemFun11123     int foo() const volatile & { return 42; }
124 
fooMemFun11125     int foo(...) & { return 42; }
fooMemFun11126     int foo(...) const & { return 42; }
fooMemFun11127     int foo(...) volatile & { return 42; }
fooMemFun11128     int foo(...) const volatile & { return 42; }
129 
fooMemFun11130     int foo() && { return 42; }
fooMemFun11131     int foo() const && { return 42; }
fooMemFun11132     int foo() volatile && { return 42; }
fooMemFun11133     int foo() const volatile && { return 42; }
134 
fooMemFun11135     int foo(...) && { return 42; }
fooMemFun11136     int foo(...) const && { return 42; }
fooMemFun11137     int foo(...) volatile && { return 42; }
fooMemFun11138     int foo(...) const volatile && { return 42; }
139 };
140 #endif // TEST_STD_VER >= 11
141 
142 struct MemData {
143     int foo;
144 };
145 
146 // Create a non-null free function by taking the address of
147 // &static_cast<Tp&>(foo);
148 template <class Tp>
149 struct Creator {
createCreator150     static Tp create() {
151         return &foo;
152     }
153 };
154 
155 // Create a non-null member pointer.
156 template <class Ret, class Class>
157 struct Creator<Ret Class::*> {
158     typedef Ret Class::*ReturnType;
createCreator159     static ReturnType create() {
160         return &Class::foo;
161     }
162 };
163 
164 template <class TestFn, class Fn>
test_imp()165 void test_imp() {
166     { // Check that the null value is detected
167         TestFn tf = nullptr;
168         std::function<Fn> f = tf;
169         RTTI_ASSERT(f.template target<TestFn>() == nullptr);
170     }
171     { // Check that the non-null value is detected.
172         TestFn tf = Creator<TestFn>::create();
173         assert(tf != nullptr);
174         std::function<Fn> f = tf;
175         RTTI_ASSERT(f.template target<TestFn>() != nullptr);
176         RTTI_ASSERT(*f.template target<TestFn>() == tf);
177     }
178 }
179 
test_func()180 void test_func() {
181     test_imp<int(*)(), int()>();
182     test_imp<int(*)(...), int()>();
183     test_imp<int(*)(int), int(int)>();
184     test_imp<int(*)(int, ...), int(int)>();
185     test_imp<int(*)(int, int), int(int, int)>();
186     test_imp<int(*)(int, int, ...), int(int, int)>();
187     test_imp<int(*)(int, int, int), int(int, int, int)>();
188     test_imp<int(*)(int, int, int, ...), int(int, int, int)>();
189 }
190 
test_mf()191 void test_mf() {
192     test_imp<int(MemFun03::*)(), int(MemFun03&)>();
193     test_imp<int(MemFun03::*)(...), int(MemFun03&)>();
194     test_imp<int(MemFun03::*)() const, int(MemFun03&)>();
195     test_imp<int(MemFun03::*)(...) const, int(MemFun03&)>();
196     test_imp<int(MemFun03::*)() volatile, int(MemFun03&)>();
197     test_imp<int(MemFun03::*)(...) volatile, int(MemFun03&)>();
198     test_imp<int(MemFun03::*)() const volatile, int(MemFun03&)>();
199     test_imp<int(MemFun03::*)(...) const volatile, int(MemFun03&)>();
200 
201     test_imp<int(MemFun03::*)(int), int(MemFun03&, int)>();
202     test_imp<int(MemFun03::*)(int, ...), int(MemFun03&, int)>();
203     test_imp<int(MemFun03::*)(int) const, int(MemFun03&, int)>();
204     test_imp<int(MemFun03::*)(int, ...) const, int(MemFun03&, int)>();
205     test_imp<int(MemFun03::*)(int) volatile, int(MemFun03&, int)>();
206     test_imp<int(MemFun03::*)(int, ...) volatile, int(MemFun03&, int)>();
207     test_imp<int(MemFun03::*)(int) const volatile, int(MemFun03&, int)>();
208     test_imp<int(MemFun03::*)(int, ...) const volatile, int(MemFun03&, int)>();
209 
210     test_imp<int(MemFun03::*)(int, int), int(MemFun03&, int, int)>();
211     test_imp<int(MemFun03::*)(int, int, ...), int(MemFun03&, int, int)>();
212     test_imp<int(MemFun03::*)(int, int) const, int(MemFun03&, int, int)>();
213     test_imp<int(MemFun03::*)(int, int, ...) const, int(MemFun03&, int, int)>();
214     test_imp<int(MemFun03::*)(int, int) volatile, int(MemFun03&, int, int)>();
215     test_imp<int(MemFun03::*)(int, int, ...) volatile, int(MemFun03&, int, int)>();
216     test_imp<int(MemFun03::*)(int, int) const volatile, int(MemFun03&, int, int)>();
217     test_imp<int(MemFun03::*)(int, int, ...) const volatile, int(MemFun03&, int, int)>();
218 
219 #if TEST_STD_VER >= 11
220     test_imp<int(MemFun11::*)() &, int(MemFun11&)>();
221     test_imp<int(MemFun11::*)(...) &, int(MemFun11&)>();
222     test_imp<int(MemFun11::*)() const &, int(MemFun11&)>();
223     test_imp<int(MemFun11::*)(...) const &, int(MemFun11&)>();
224     test_imp<int(MemFun11::*)() volatile &, int(MemFun11&)>();
225     test_imp<int(MemFun11::*)(...) volatile &, int(MemFun11&)>();
226     test_imp<int(MemFun11::*)() const volatile &, int(MemFun11&)>();
227     test_imp<int(MemFun11::*)(...) const volatile &, int(MemFun11&)>();
228 
229     test_imp<int(MemFun11::*)() &&, int(MemFun11&&)>();
230     test_imp<int(MemFun11::*)(...) &&, int(MemFun11&&)>();
231     test_imp<int(MemFun11::*)() const &&, int(MemFun11&&)>();
232     test_imp<int(MemFun11::*)(...) const &&, int(MemFun11&&)>();
233     test_imp<int(MemFun11::*)() volatile &&, int(MemFun11&&)>();
234     test_imp<int(MemFun11::*)(...) volatile &&, int(MemFun11&&)>();
235     test_imp<int(MemFun11::*)() const volatile &&, int(MemFun11&&)>();
236     test_imp<int(MemFun11::*)(...) const volatile &&, int(MemFun11&&)>();
237 #endif
238 }
239 
test_md()240 void test_md() {
241     test_imp<int MemData::*, int(MemData&)>();
242 }
243 
main(int,char **)244 int main(int, char**) {
245     test_func();
246     test_mf();
247     test_md();
248 
249   return 0;
250 }
251