//===- Sequence.h - Utility for producing sequences of values ---*- 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 // //===----------------------------------------------------------------------===// /// \file /// Provides some synthesis utilities to produce sequences of values. The names /// are intentionally kept very short as they tend to occur in common and /// widely used contexts. /// /// The `seq(A, B)` function produces a sequence of values from `A` to up to /// (but not including) `B`, i.e., [`A`, `B`), that can be safely iterated over. /// `seq` supports both integral (e.g., `int`, `char`, `uint32_t`) and enum /// types. `seq_inclusive(A, B)` produces a sequence of values from `A` to `B`, /// including `B`. /// /// Examples with integral types: /// ``` /// for (int x : seq(0, 3)) /// outs() << x << " "; /// ``` /// /// Prints: `0 1 2 `. /// /// ``` /// for (int x : seq_inclusive(0, 3)) /// outs() << x << " "; /// ``` /// /// Prints: `0 1 2 3 `. /// /// Similar to `seq` and `seq_inclusive`, the `enum_seq` and /// `enum_seq_inclusive` functions produce sequences of enum values that can be /// iterated over. /// To enable iteration with enum types, you need to either mark enums as safe /// to iterate on by specializing `enum_iteration_traits`, or opt into /// potentially unsafe iteration at every callsite by passing /// `force_iteration_on_noniterable_enum`. /// /// Examples with enum types: /// ``` /// namespace X { /// enum class MyEnum : unsigned {A = 0, B, C}; /// } // namespace X /// /// template <> struct enum_iteration_traits<X::MyEnum> { /// static contexpr bool is_iterable = true; /// }; /// /// class MyClass { /// public: /// enum Safe { D = 3, E, F }; /// enum MaybeUnsafe { G = 1, H = 2, I = 4 }; /// }; /// /// template <> struct enum_iteration_traits<MyClass::Safe> { /// static contexpr bool is_iterable = true; /// }; /// ``` /// /// ``` /// for (auto v : enum_seq(MyClass::Safe::D, MyClass::Safe::F)) /// outs() << int(v) << " "; /// ``` /// /// Prints: `3 4 `. /// /// ``` /// for (auto v : enum_seq(MyClass::MaybeUnsafe::H, MyClass::MaybeUnsafe::I, /// force_iteration_on_noniterable_enum)) /// outs() << int(v) << " "; /// ``` /// /// Prints: `2 3 `. /// //===----------------------------------------------------------------------===// #ifndef LLVM_ADT_SEQUENCE_H #define LLVM_ADT_SEQUENCE_H #include <cassert> // assert #include <iterator> // std::random_access_iterator_tag #include <limits> // std::numeric_limits #include <type_traits> // std::is_integral, std::is_enum, std::underlying_type, // std::enable_if #include "llvm/Support/MathExtras.h" // AddOverflow / SubOverflow namespace llvm { // Enum traits that marks enums as safe or unsafe to iterate over. // By default, enum types are *not* considered safe for iteration. // To allow iteration for your enum type, provide a specialization with // `is_iterable` set to `true` in the `llvm` namespace. // Alternatively, you can pass the `force_iteration_on_noniterable_enum` tag // to `enum_seq` or `enum_seq_inclusive`. template <typename EnumT> struct enum_iteration_traits { … }; struct force_iteration_on_noniterable_enum_t { … }; inline constexpr force_iteration_on_noniterable_enum_t force_iteration_on_noniterable_enum; namespace detail { // Returns whether a value of type U can be represented with type T. template <typename T, typename U> bool canTypeFitValue(const U Value) { … } // An integer type that asserts when: // - constructed from a value that doesn't fit into intmax_t, // - casted to a type that cannot hold the current value, // - its internal representation overflows. struct CheckedInt { … }; template <typename T, bool IsReverse> struct SafeIntIterator { … }; } // namespace detail template <typename T> struct iota_range { … }; /// Iterate over an integral type from Begin up to - but not including - End. /// Note: Begin and End values have to be within [INTMAX_MIN, INTMAX_MAX] for /// forward iteration (resp. [INTMAX_MIN + 1, INTMAX_MAX] for reverse /// iteration). template <typename T, typename = std::enable_if_t<std::is_integral<T>::value && !std::is_enum<T>::value>> auto seq(T Begin, T End) { … } /// Iterate over an integral type from 0 up to - but not including - Size. /// Note: Size value has to be within [INTMAX_MIN, INTMAX_MAX - 1] for /// forward iteration (resp. [INTMAX_MIN + 1, INTMAX_MAX - 1] for reverse /// iteration). template <typename T, typename = std::enable_if_t<std::is_integral<T>::value && !std::is_enum<T>::value>> auto seq(T Size) { … } /// Iterate over an integral type from Begin to End inclusive. /// Note: Begin and End values have to be within [INTMAX_MIN, INTMAX_MAX - 1] /// for forward iteration (resp. [INTMAX_MIN + 1, INTMAX_MAX - 1] for reverse /// iteration). template <typename T, typename = std::enable_if_t<std::is_integral<T>::value && !std::is_enum<T>::value>> auto seq_inclusive(T Begin, T End) { … } /// Iterate over an enum type from Begin up to - but not including - End. /// Note: `enum_seq` will generate each consecutive value, even if no /// enumerator with that value exists. /// Note: Begin and End values have to be within [INTMAX_MIN, INTMAX_MAX] for /// forward iteration (resp. [INTMAX_MIN + 1, INTMAX_MAX] for reverse /// iteration). template <typename EnumT, typename = std::enable_if_t<std::is_enum<EnumT>::value>> auto enum_seq(EnumT Begin, EnumT End) { … } /// Iterate over an enum type from Begin up to - but not including - End, even /// when `EnumT` is not marked as safely iterable by `enum_iteration_traits`. /// Note: `enum_seq` will generate each consecutive value, even if no /// enumerator with that value exists. /// Note: Begin and End values have to be within [INTMAX_MIN, INTMAX_MAX] for /// forward iteration (resp. [INTMAX_MIN + 1, INTMAX_MAX] for reverse /// iteration). template <typename EnumT, typename = std::enable_if_t<std::is_enum<EnumT>::value>> auto enum_seq(EnumT Begin, EnumT End, force_iteration_on_noniterable_enum_t) { … } /// Iterate over an enum type from Begin to End inclusive. /// Note: `enum_seq_inclusive` will generate each consecutive value, even if no /// enumerator with that value exists. /// Note: Begin and End values have to be within [INTMAX_MIN, INTMAX_MAX - 1] /// for forward iteration (resp. [INTMAX_MIN + 1, INTMAX_MAX - 1] for reverse /// iteration). template <typename EnumT, typename = std::enable_if_t<std::is_enum<EnumT>::value>> auto enum_seq_inclusive(EnumT Begin, EnumT End) { … } /// Iterate over an enum type from Begin to End inclusive, even when `EnumT` /// is not marked as safely iterable by `enum_iteration_traits`. /// Note: `enum_seq_inclusive` will generate each consecutive value, even if no /// enumerator with that value exists. /// Note: Begin and End values have to be within [INTMAX_MIN, INTMAX_MAX - 1] /// for forward iteration (resp. [INTMAX_MIN + 1, INTMAX_MAX - 1] for reverse /// iteration). template <typename EnumT, typename = std::enable_if_t<std::is_enum<EnumT>::value>> auto enum_seq_inclusive(EnumT Begin, EnumT End, force_iteration_on_noniterable_enum_t) { … } } // end namespace llvm #endif // LLVM_ADT_SEQUENCE_H