Index: llvm/trunk/include/llvm/ADT/VariadicFunction.h =================================================================== --- llvm/trunk/include/llvm/ADT/VariadicFunction.h +++ llvm/trunk/include/llvm/ADT/VariadicFunction.h @@ -1,330 +0,0 @@ -//===- VariadicFunction.h - Variadic Functions ------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// This file implements compile-time type-safe variadic functions. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_ADT_VARIADICFUNCTION_H -#define LLVM_ADT_VARIADICFUNCTION_H - -#include "llvm/ADT/ArrayRef.h" - -namespace llvm { - -// Define macros to aid in expanding a comma separated series with the index of -// the series pasted onto the last token. -#define LLVM_COMMA_JOIN1(x) x ## 0 -#define LLVM_COMMA_JOIN2(x) LLVM_COMMA_JOIN1(x), x ## 1 -#define LLVM_COMMA_JOIN3(x) LLVM_COMMA_JOIN2(x), x ## 2 -#define LLVM_COMMA_JOIN4(x) LLVM_COMMA_JOIN3(x), x ## 3 -#define LLVM_COMMA_JOIN5(x) LLVM_COMMA_JOIN4(x), x ## 4 -#define LLVM_COMMA_JOIN6(x) LLVM_COMMA_JOIN5(x), x ## 5 -#define LLVM_COMMA_JOIN7(x) LLVM_COMMA_JOIN6(x), x ## 6 -#define LLVM_COMMA_JOIN8(x) LLVM_COMMA_JOIN7(x), x ## 7 -#define LLVM_COMMA_JOIN9(x) LLVM_COMMA_JOIN8(x), x ## 8 -#define LLVM_COMMA_JOIN10(x) LLVM_COMMA_JOIN9(x), x ## 9 -#define LLVM_COMMA_JOIN11(x) LLVM_COMMA_JOIN10(x), x ## 10 -#define LLVM_COMMA_JOIN12(x) LLVM_COMMA_JOIN11(x), x ## 11 -#define LLVM_COMMA_JOIN13(x) LLVM_COMMA_JOIN12(x), x ## 12 -#define LLVM_COMMA_JOIN14(x) LLVM_COMMA_JOIN13(x), x ## 13 -#define LLVM_COMMA_JOIN15(x) LLVM_COMMA_JOIN14(x), x ## 14 -#define LLVM_COMMA_JOIN16(x) LLVM_COMMA_JOIN15(x), x ## 15 -#define LLVM_COMMA_JOIN17(x) LLVM_COMMA_JOIN16(x), x ## 16 -#define LLVM_COMMA_JOIN18(x) LLVM_COMMA_JOIN17(x), x ## 17 -#define LLVM_COMMA_JOIN19(x) LLVM_COMMA_JOIN18(x), x ## 18 -#define LLVM_COMMA_JOIN20(x) LLVM_COMMA_JOIN19(x), x ## 19 -#define LLVM_COMMA_JOIN21(x) LLVM_COMMA_JOIN20(x), x ## 20 -#define LLVM_COMMA_JOIN22(x) LLVM_COMMA_JOIN21(x), x ## 21 -#define LLVM_COMMA_JOIN23(x) LLVM_COMMA_JOIN22(x), x ## 22 -#define LLVM_COMMA_JOIN24(x) LLVM_COMMA_JOIN23(x), x ## 23 -#define LLVM_COMMA_JOIN25(x) LLVM_COMMA_JOIN24(x), x ## 24 -#define LLVM_COMMA_JOIN26(x) LLVM_COMMA_JOIN25(x), x ## 25 -#define LLVM_COMMA_JOIN27(x) LLVM_COMMA_JOIN26(x), x ## 26 -#define LLVM_COMMA_JOIN28(x) LLVM_COMMA_JOIN27(x), x ## 27 -#define LLVM_COMMA_JOIN29(x) LLVM_COMMA_JOIN28(x), x ## 28 -#define LLVM_COMMA_JOIN30(x) LLVM_COMMA_JOIN29(x), x ## 29 -#define LLVM_COMMA_JOIN31(x) LLVM_COMMA_JOIN30(x), x ## 30 -#define LLVM_COMMA_JOIN32(x) LLVM_COMMA_JOIN31(x), x ## 31 - -/// Class which can simulate a type-safe variadic function. -/// -/// The VariadicFunction class template makes it easy to define -/// type-safe variadic functions where all arguments have the same -/// type. -/// -/// Suppose we need a variadic function like this: -/// -/// ResultT Foo(const ArgT &A_0, const ArgT &A_1, ..., const ArgT &A_N); -/// -/// Instead of many overloads of Foo(), we only need to define a helper -/// function that takes an array of arguments: -/// -/// ResultT FooImpl(ArrayRef Args) { -/// // 'Args[i]' is a pointer to the i-th argument passed to Foo(). -/// ... -/// } -/// -/// and then define Foo() like this: -/// -/// const VariadicFunction Foo; -/// -/// VariadicFunction takes care of defining the overloads of Foo(). -/// -/// Actually, Foo is a function object (i.e. functor) instead of a plain -/// function. This object is stateless and its constructor/destructor -/// does nothing, so it's safe to create global objects and call Foo(...) at -/// any time. -/// -/// Sometimes we need a variadic function to have some fixed leading -/// arguments whose types may be different from that of the optional -/// arguments. For example: -/// -/// bool FullMatch(const StringRef &S, const RE &Regex, -/// const ArgT &A_0, ..., const ArgT &A_N); -/// -/// VariadicFunctionN is for such cases, where N is the number of fixed -/// arguments. It is like VariadicFunction, except that it takes N more -/// template arguments for the types of the fixed arguments: -/// -/// bool FullMatchImpl(const StringRef &S, const RE &Regex, -/// ArrayRef Args) { ... } -/// const VariadicFunction2 -/// FullMatch; -/// -/// Currently VariadicFunction and friends support up-to 3 -/// fixed leading arguments and up-to 32 optional arguments. -template )> -struct VariadicFunction { - ResultT operator()() const { - return Func(None); - } - -#define LLVM_DEFINE_OVERLOAD(N) \ - ResultT operator()(LLVM_COMMA_JOIN ## N(const ArgT &A)) const { \ - const ArgT *const Args[] = { LLVM_COMMA_JOIN ## N(&A) }; \ - return Func(makeArrayRef(Args)); \ - } - LLVM_DEFINE_OVERLOAD(1) - LLVM_DEFINE_OVERLOAD(2) - LLVM_DEFINE_OVERLOAD(3) - LLVM_DEFINE_OVERLOAD(4) - LLVM_DEFINE_OVERLOAD(5) - LLVM_DEFINE_OVERLOAD(6) - LLVM_DEFINE_OVERLOAD(7) - LLVM_DEFINE_OVERLOAD(8) - LLVM_DEFINE_OVERLOAD(9) - LLVM_DEFINE_OVERLOAD(10) - LLVM_DEFINE_OVERLOAD(11) - LLVM_DEFINE_OVERLOAD(12) - LLVM_DEFINE_OVERLOAD(13) - LLVM_DEFINE_OVERLOAD(14) - LLVM_DEFINE_OVERLOAD(15) - LLVM_DEFINE_OVERLOAD(16) - LLVM_DEFINE_OVERLOAD(17) - LLVM_DEFINE_OVERLOAD(18) - LLVM_DEFINE_OVERLOAD(19) - LLVM_DEFINE_OVERLOAD(20) - LLVM_DEFINE_OVERLOAD(21) - LLVM_DEFINE_OVERLOAD(22) - LLVM_DEFINE_OVERLOAD(23) - LLVM_DEFINE_OVERLOAD(24) - LLVM_DEFINE_OVERLOAD(25) - LLVM_DEFINE_OVERLOAD(26) - LLVM_DEFINE_OVERLOAD(27) - LLVM_DEFINE_OVERLOAD(28) - LLVM_DEFINE_OVERLOAD(29) - LLVM_DEFINE_OVERLOAD(30) - LLVM_DEFINE_OVERLOAD(31) - LLVM_DEFINE_OVERLOAD(32) -#undef LLVM_DEFINE_OVERLOAD -}; - -template )> -struct VariadicFunction1 { - ResultT operator()(Param0T P0) const { - return Func(P0, None); - } - -#define LLVM_DEFINE_OVERLOAD(N) \ - ResultT operator()(Param0T P0, LLVM_COMMA_JOIN ## N(const ArgT &A)) const { \ - const ArgT *const Args[] = { LLVM_COMMA_JOIN ## N(&A) }; \ - return Func(P0, makeArrayRef(Args)); \ - } - LLVM_DEFINE_OVERLOAD(1) - LLVM_DEFINE_OVERLOAD(2) - LLVM_DEFINE_OVERLOAD(3) - LLVM_DEFINE_OVERLOAD(4) - LLVM_DEFINE_OVERLOAD(5) - LLVM_DEFINE_OVERLOAD(6) - LLVM_DEFINE_OVERLOAD(7) - LLVM_DEFINE_OVERLOAD(8) - LLVM_DEFINE_OVERLOAD(9) - LLVM_DEFINE_OVERLOAD(10) - LLVM_DEFINE_OVERLOAD(11) - LLVM_DEFINE_OVERLOAD(12) - LLVM_DEFINE_OVERLOAD(13) - LLVM_DEFINE_OVERLOAD(14) - LLVM_DEFINE_OVERLOAD(15) - LLVM_DEFINE_OVERLOAD(16) - LLVM_DEFINE_OVERLOAD(17) - LLVM_DEFINE_OVERLOAD(18) - LLVM_DEFINE_OVERLOAD(19) - LLVM_DEFINE_OVERLOAD(20) - LLVM_DEFINE_OVERLOAD(21) - LLVM_DEFINE_OVERLOAD(22) - LLVM_DEFINE_OVERLOAD(23) - LLVM_DEFINE_OVERLOAD(24) - LLVM_DEFINE_OVERLOAD(25) - LLVM_DEFINE_OVERLOAD(26) - LLVM_DEFINE_OVERLOAD(27) - LLVM_DEFINE_OVERLOAD(28) - LLVM_DEFINE_OVERLOAD(29) - LLVM_DEFINE_OVERLOAD(30) - LLVM_DEFINE_OVERLOAD(31) - LLVM_DEFINE_OVERLOAD(32) -#undef LLVM_DEFINE_OVERLOAD -}; - -template )> -struct VariadicFunction2 { - ResultT operator()(Param0T P0, Param1T P1) const { - return Func(P0, P1, None); - } - -#define LLVM_DEFINE_OVERLOAD(N) \ - ResultT operator()(Param0T P0, Param1T P1, \ - LLVM_COMMA_JOIN ## N(const ArgT &A)) const { \ - const ArgT *const Args[] = { LLVM_COMMA_JOIN ## N(&A) }; \ - return Func(P0, P1, makeArrayRef(Args)); \ - } - LLVM_DEFINE_OVERLOAD(1) - LLVM_DEFINE_OVERLOAD(2) - LLVM_DEFINE_OVERLOAD(3) - LLVM_DEFINE_OVERLOAD(4) - LLVM_DEFINE_OVERLOAD(5) - LLVM_DEFINE_OVERLOAD(6) - LLVM_DEFINE_OVERLOAD(7) - LLVM_DEFINE_OVERLOAD(8) - LLVM_DEFINE_OVERLOAD(9) - LLVM_DEFINE_OVERLOAD(10) - LLVM_DEFINE_OVERLOAD(11) - LLVM_DEFINE_OVERLOAD(12) - LLVM_DEFINE_OVERLOAD(13) - LLVM_DEFINE_OVERLOAD(14) - LLVM_DEFINE_OVERLOAD(15) - LLVM_DEFINE_OVERLOAD(16) - LLVM_DEFINE_OVERLOAD(17) - LLVM_DEFINE_OVERLOAD(18) - LLVM_DEFINE_OVERLOAD(19) - LLVM_DEFINE_OVERLOAD(20) - LLVM_DEFINE_OVERLOAD(21) - LLVM_DEFINE_OVERLOAD(22) - LLVM_DEFINE_OVERLOAD(23) - LLVM_DEFINE_OVERLOAD(24) - LLVM_DEFINE_OVERLOAD(25) - LLVM_DEFINE_OVERLOAD(26) - LLVM_DEFINE_OVERLOAD(27) - LLVM_DEFINE_OVERLOAD(28) - LLVM_DEFINE_OVERLOAD(29) - LLVM_DEFINE_OVERLOAD(30) - LLVM_DEFINE_OVERLOAD(31) - LLVM_DEFINE_OVERLOAD(32) -#undef LLVM_DEFINE_OVERLOAD -}; - -template )> -struct VariadicFunction3 { - ResultT operator()(Param0T P0, Param1T P1, Param2T P2) const { - return Func(P0, P1, P2, None); - } - -#define LLVM_DEFINE_OVERLOAD(N) \ - ResultT operator()(Param0T P0, Param1T P1, Param2T P2, \ - LLVM_COMMA_JOIN ## N(const ArgT &A)) const { \ - const ArgT *const Args[] = { LLVM_COMMA_JOIN ## N(&A) }; \ - return Func(P0, P1, P2, makeArrayRef(Args)); \ - } - LLVM_DEFINE_OVERLOAD(1) - LLVM_DEFINE_OVERLOAD(2) - LLVM_DEFINE_OVERLOAD(3) - LLVM_DEFINE_OVERLOAD(4) - LLVM_DEFINE_OVERLOAD(5) - LLVM_DEFINE_OVERLOAD(6) - LLVM_DEFINE_OVERLOAD(7) - LLVM_DEFINE_OVERLOAD(8) - LLVM_DEFINE_OVERLOAD(9) - LLVM_DEFINE_OVERLOAD(10) - LLVM_DEFINE_OVERLOAD(11) - LLVM_DEFINE_OVERLOAD(12) - LLVM_DEFINE_OVERLOAD(13) - LLVM_DEFINE_OVERLOAD(14) - LLVM_DEFINE_OVERLOAD(15) - LLVM_DEFINE_OVERLOAD(16) - LLVM_DEFINE_OVERLOAD(17) - LLVM_DEFINE_OVERLOAD(18) - LLVM_DEFINE_OVERLOAD(19) - LLVM_DEFINE_OVERLOAD(20) - LLVM_DEFINE_OVERLOAD(21) - LLVM_DEFINE_OVERLOAD(22) - LLVM_DEFINE_OVERLOAD(23) - LLVM_DEFINE_OVERLOAD(24) - LLVM_DEFINE_OVERLOAD(25) - LLVM_DEFINE_OVERLOAD(26) - LLVM_DEFINE_OVERLOAD(27) - LLVM_DEFINE_OVERLOAD(28) - LLVM_DEFINE_OVERLOAD(29) - LLVM_DEFINE_OVERLOAD(30) - LLVM_DEFINE_OVERLOAD(31) - LLVM_DEFINE_OVERLOAD(32) -#undef LLVM_DEFINE_OVERLOAD -}; - -// Cleanup the macro namespace. -#undef LLVM_COMMA_JOIN1 -#undef LLVM_COMMA_JOIN2 -#undef LLVM_COMMA_JOIN3 -#undef LLVM_COMMA_JOIN4 -#undef LLVM_COMMA_JOIN5 -#undef LLVM_COMMA_JOIN6 -#undef LLVM_COMMA_JOIN7 -#undef LLVM_COMMA_JOIN8 -#undef LLVM_COMMA_JOIN9 -#undef LLVM_COMMA_JOIN10 -#undef LLVM_COMMA_JOIN11 -#undef LLVM_COMMA_JOIN12 -#undef LLVM_COMMA_JOIN13 -#undef LLVM_COMMA_JOIN14 -#undef LLVM_COMMA_JOIN15 -#undef LLVM_COMMA_JOIN16 -#undef LLVM_COMMA_JOIN17 -#undef LLVM_COMMA_JOIN18 -#undef LLVM_COMMA_JOIN19 -#undef LLVM_COMMA_JOIN20 -#undef LLVM_COMMA_JOIN21 -#undef LLVM_COMMA_JOIN22 -#undef LLVM_COMMA_JOIN23 -#undef LLVM_COMMA_JOIN24 -#undef LLVM_COMMA_JOIN25 -#undef LLVM_COMMA_JOIN26 -#undef LLVM_COMMA_JOIN27 -#undef LLVM_COMMA_JOIN28 -#undef LLVM_COMMA_JOIN29 -#undef LLVM_COMMA_JOIN30 -#undef LLVM_COMMA_JOIN31 -#undef LLVM_COMMA_JOIN32 - -} // end namespace llvm - -#endif // LLVM_ADT_VARIADICFUNCTION_H Index: llvm/trunk/unittests/ADT/CMakeLists.txt =================================================================== --- llvm/trunk/unittests/ADT/CMakeLists.txt +++ llvm/trunk/unittests/ADT/CMakeLists.txt @@ -71,7 +71,6 @@ TinyPtrVectorTest.cpp TripleTest.cpp TwineTest.cpp - VariadicFunctionTest.cpp ) target_link_libraries(ADTTests PRIVATE LLVMTestingSupport) Index: llvm/trunk/unittests/ADT/VariadicFunctionTest.cpp =================================================================== --- llvm/trunk/unittests/ADT/VariadicFunctionTest.cpp +++ llvm/trunk/unittests/ADT/VariadicFunctionTest.cpp @@ -1,109 +0,0 @@ -//===----------- VariadicFunctionTest.cpp - VariadicFunction unit tests ---===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "llvm/ADT/VariadicFunction.h" -#include "llvm/ADT/ArrayRef.h" -#include "gtest/gtest.h" - -using namespace llvm; -namespace { - -// Defines a variadic function StringCat() to join strings. -// StringCat()'s arguments and return value have class types. -std::string StringCatImpl(ArrayRef Args) { - std::string S; - for (unsigned i = 0, e = Args.size(); i < e; ++i) - S += *Args[i]; - return S; -} -const VariadicFunction StringCat = {}; - -TEST(VariadicFunctionTest, WorksForClassTypes) { - EXPECT_EQ("", StringCat()); - EXPECT_EQ("a", StringCat("a")); - EXPECT_EQ("abc", StringCat("a", "bc")); - EXPECT_EQ("0123456789abcdefghijklmnopqrstuv", - StringCat("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", - "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", - "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", - "u", "v")); -} - -// Defines a variadic function Sum(), whose arguments and return value -// have primitive types. -// The return type of SumImp() is deliberately different from its -// argument type, as we want to test that this works. -long SumImpl(ArrayRef Args) { - long Result = 0; - for (unsigned i = 0, e = Args.size(); i < e; ++i) - Result += *Args[i]; - return Result; -} -const VariadicFunction Sum = {}; - -TEST(VariadicFunctionTest, WorksForPrimitiveTypes) { - EXPECT_EQ(0, Sum()); - EXPECT_EQ(1, Sum(1)); - EXPECT_EQ(12, Sum(10, 2)); - EXPECT_EQ(1234567, Sum(1000000, 200000, 30000, 4000, 500, 60, 7)); -} - -// Appends an array of strings to dest and returns the number of -// characters appended. -int StringAppendImpl(std::string *Dest, ArrayRef Args) { - int Chars = 0; - for (unsigned i = 0, e = Args.size(); i < e; ++i) { - Chars += Args[i]->size(); - *Dest += *Args[i]; - } - return Chars; -} -const VariadicFunction1 StringAppend = {}; - -TEST(VariadicFunction1Test, Works) { - std::string S0("hi"); - EXPECT_EQ(0, StringAppend(&S0)); - EXPECT_EQ("hi", S0); - - std::string S1("bin"); - EXPECT_EQ(2, StringAppend(&S1, "go")); - EXPECT_EQ("bingo", S1); - - std::string S4("Fab4"); - EXPECT_EQ(4 + 4 + 6 + 5, - StringAppend(&S4, "John", "Paul", "George", "Ringo")); - EXPECT_EQ("Fab4JohnPaulGeorgeRingo", S4); -} - -// Counts how many optional arguments fall in the given range. -// Returns the result in *num_in_range. We make the return type void -// as we want to test that VariadicFunction* can handle it. -void CountInRangeImpl(int *NumInRange, int Low, int High, - ArrayRef Args) { - *NumInRange = 0; - for (unsigned i = 0, e = Args.size(); i < e; ++i) - if (Low <= *Args[i] && *Args[i] <= High) - ++(*NumInRange); -} -const VariadicFunction3 CountInRange = {}; - -TEST(VariadicFunction3Test, Works) { - int N = -1; - CountInRange(&N, -100, 100); - EXPECT_EQ(0, N); - - CountInRange(&N, -100, 100, 42); - EXPECT_EQ(1, N); - - CountInRange(&N, -100, 100, 1, 999, -200, 42); - EXPECT_EQ(2, N); -} - -} // namespace