diff --git a/flang/lib/common/kind-variant.h b/flang/lib/common/kind-variant.h deleted file mode 100644 index 95058df81aac..000000000000 --- a/flang/lib/common/kind-variant.h +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. -// -// 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. - -#ifndef FORTRAN_COMMON_KIND_VARIANT_H_ -#define FORTRAN_COMMON_KIND_VARIANT_H_ - -#include "template.h" -#include -#include - -namespace Fortran::common { - -// A KindVariant instantiates a std::variant over a collection of types -// derived by applying a given template to each of a list of "kind" arguments, -// wraps that variant as the sole data member ("u"), and supplies some helpful -// member functions and member function templates to perform reverse -// mappings of both alternative indices and alternative types back to their -// kinds, invoke kind-dependent templates based on dynamic kind values, &c. -template class TYPE, KIND... KINDS> -struct KindVariant { - using Kind = KIND; - - static constexpr auto kinds{sizeof...(KINDS)}; - static constexpr Kind kindValue[kinds]{KINDS...}; - template using KindType = TYPE; - - using Variant = std::variant...>; - - CLASS_BOILERPLATE(KindVariant) - template KindVariant(const A &x) : u{x} {} - template - KindVariant(std::enable_if_t, A> &&x) - : u{std::move(x)} {} - - template KindVariant &operator=(const A &x) { - u = x; - return *this; - } - template KindVariant &operator=(A &&x) { - u = std::move(x); - return *this; - } - - static constexpr Kind IndexToKind(int index) { return kindValue[index]; } - - template - static constexpr Kind TypeToKind{ - IndexToKind(TypeIndex...>)}; - - Kind kind() const { return IndexToKind(u.index()); } - - // Accessors for alternatives as identified by kind or type. - template KindType *GetIfKind() { - if (auto *p{std::get_if>(u)}) { - return p; - } - return nullptr; - } - template const KindType *GetIfKind() const { - if (const auto *p{std::get_if>(u)}) { - return p; - } - return nullptr; - } - template std::optional> GetIf() const { - return common::GetIf>(u); - } - - // Given an instance of some class A with a member template function - // "template void action();", AtKind(A &a, Kind k) will - // invoke a.action with a *dynamic* kind value. -private: - template static void Helper(A &a, Kind k) { - static constexpr Kind K{IndexToKind(J)}; - if (k == K) { - a.template action(); - } else if constexpr (J + 1 < kinds) { - Helper(a, k); - } - } - -public: - template static void AtKind(A &a, Kind k) { Helper(a, k); } - - // When each of the alternatives of a KindVariant has a constructor that - // accepts an rvalue reference to some (same) type A, this template can be - // used to create a KindVariant instance of a forced kind. -private: - template struct SetResult { - explicit SetResult(A &&x) : value{std::move(x)} {} - template void action() { - CHECK(!result.has_value()); - result = KindVariant{KindType{std::move(value)}}; - } - std::optional result; - A value; - }; - -public: - template - static std::optional ForceKind(Kind k, A &&x) { - SetResult setter{std::move(x)}; - AtKind(setter, k); - return std::move(setter.result); - } - - Variant u; -}; -} // namespace Fortran::common -#endif // FORTRAN_COMMON_KIND_VARIANT_H_