/* ----------------------------------------------------------------------------- Copyright 2019-2021 Kevin P. Barry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ----------------------------------------------------------------------------- */ // Author: Kevin P. Barry [ta0kira@gmail.com] #ifndef FUNCTION_HPP_ #define FUNCTION_HPP_ #include #include "returns.hpp" #include "types.hpp" struct CategoryFunction { const int param_count; const int arg_count; const int return_count; const char* const category; const char* const function; const CategoryId collection; const int function_num; }; struct TypeFunction { const int param_count; const int arg_count; const int return_count; const char* const category; const char* const function; const CategoryId collection; const int function_num; }; struct ValueFunction { const int param_count; const int arg_count; const int return_count; const char* const category; const char* const function; const CategoryId collection; const int function_num; }; inline std::ostream& operator << (std::ostream& output, const CategoryFunction& func) { return output << func.category << ":" << func.function; } inline std::ostream& operator << (std::ostream& output, const TypeFunction& func) { return output << func.category << "." << func.function; } inline std::ostream& operator << (std::ostream& output, const ValueFunction& func) { return output << func.category << "." << func.function; } struct ParamsArgs { virtual int NumParams() const = 0; virtual const S& GetParam(int pos) const = 0; virtual int NumArgs() const = 0; virtual const BoxedValue& GetArg(int pos) const = 0; }; template struct PassArgs : public ParamsArgs { template PassArgs(const Ps&... passed) { Init<0, 0>(passed...); } template void Init() {} template void Init(const S& param, const Ps&... passed) { params[Pn] = param; Init(passed...); } template void Init(const BoxedValue& arg, const Ps&... passed) { args[An] = &arg; Init(passed...); } int NumParams() const final { return P; } const S& GetParam(int pos) const final { if (pos < 0 || pos >= P) { FAIL() << "Bad param index"; } return params[pos]; } int NumArgs() const final { return A; } const BoxedValue& GetArg(int pos) const final { if (pos < 0 || pos >= A) { FAIL() << "Bad arg index"; } return *args[pos]; } S params[P]; const BoxedValue* args[A]; }; template<> struct PassArgs<0, 0> : public ParamsArgs { constexpr PassArgs() {} int NumParams() const final { return 0; } const S& GetParam(int pos) const final { FAIL() << "Bad param index"; __builtin_unreachable(); } int NumArgs() const final { return 0; } const BoxedValue& GetArg(int pos) const final { FAIL() << "Bad arg index"; __builtin_unreachable(); } }; template struct PassReturns : public ParamsArgs { template PassReturns(const Ps&... passed) { Init<0>(passed...); } template void Init(const S& param, const Ps&... passed) { params[Pn] = param; Init(passed...); } template void Init(const ReturnTuple& forward) { returns = &forward; } int NumParams() const final { return P; } const S& GetParam(int pos) const final { if (pos < 0 || pos >= P) { FAIL() << "Bad param index"; } return params[pos]; } int NumArgs() const final { return returns->Size(); } const BoxedValue& GetArg(int pos) const final { return returns->At(pos); } S params[P]; const ReturnTuple* returns; }; template struct AutoArgs; template struct AutoArgs { using Type = PassArgs; }; template struct AutoArgs { using Type = typename AutoArgs::Type; }; template struct AutoCall { using Type = typename AutoArgs::Type; }; template struct AutoCall { using Type = PassReturns

; }; template struct AutoCall, Ps...> { using Type = typename AutoCall::Type; }; template struct GetCall { using Type = typename AutoCall<0, Ps...>::Type; }; template typename GetCall::Type PassParamsArgs(const Ps&... passed) { return typename GetCall::Type(passed...); } #endif // FUNCTION_HPP_