1/*
2 * Copyright (C) 2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <type_traits>
29
30namespace WTF {
31
32template<typename T>
33struct FunctionTraits;
34
35#if USE(JSVALUE32_64)
36
37template<typename T>
38static constexpr unsigned slotsForCCallArgument()
39{
40 static_assert(!std::is_class<T>::value || sizeof(T) <= sizeof(void*), "This doesn't support complex structs.");
41 static_assert(sizeof(T) == 8 || sizeof(T) <= 4, "");
42 // This assumes that all integral values are passed on the stack.
43 if (sizeof(T) == 8)
44 return 2;
45
46 return 1;
47}
48
49template<typename T>
50static constexpr unsigned computeCCallSlots() { return slotsForCCallArgument<T>(); }
51
52template<typename T, typename... Ts>
53static constexpr std::enable_if_t<!!sizeof...(Ts), unsigned> computeCCallSlots() { return computeCCallSlots<Ts...>() + slotsForCCallArgument<T>(); }
54
55#endif
56
57template<typename Result, typename... Args>
58struct FunctionTraits<Result(Args...)> {
59 using ResultType = Result;
60
61 static constexpr bool hasResult = !std::is_same<ResultType, void>::value;
62
63 static constexpr std::size_t arity = sizeof...(Args);
64
65 template <std::size_t n, typename = std::enable_if_t<(n < arity)>>
66 using ArgumentType = typename std::tuple_element<n, std::tuple<Args...>>::type;
67 using ArgumentTypes = std::tuple<Args...>;
68
69
70#if USE(JSVALUE64)
71 static constexpr unsigned cCallArity() { return arity; }
72#else
73
74 static constexpr unsigned cCallArity() { return computeCCallSlots<Args...>(); }
75#endif // USE(JSVALUE64)
76
77};
78
79template<typename Result, typename... Args>
80struct FunctionTraits<Result(*)(Args...)> : public FunctionTraits<Result(Args...)> {
81};
82
83} // namespace WTF
84
85using WTF::FunctionTraits;
86