//===----------------------------------------------------------------------===//
//
// Part of libcu++, the C++ Standard Library for your entire system,
// 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
// SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

// MIT License
//
// Modifications Copyright (C) 2025-2026 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#ifndef _CUDA_STD_INITIALIZER_LIST
#define _CUDA_STD_INITIALIZER_LIST

#include <cuda/std/detail/__config>

#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
#  pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
#  pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
#  pragma system_header
#endif // no system header

#if defined(_CCCL_COMPILER_HIPRTC) && __has_include(<initializer_list>)
// NOTE(HIP/AMD): Under HIPRTC WITHOUT -nostdinc++ (e.g. libhipcxx's own lit tests), the
// system <initializer_list> is on the include path. Prefer it so we share the exact same
// std::initializer_list definition as any leaked system headers and avoid "redefinition of
// 'initializer_list'" errors. Mirrors how NVRTC's hermeticity makes __has_include(<version>)
// naturally false there (see cuda/std/version) -- HIPRTC gates on __has_include instead.
#  include <initializer_list>
#elif defined(_CCCL_COMPILER_HIPRTC)
// NOTE(HIP/AMD): Under HIPRTC with -nostdinc++ there is no system <initializer_list>
// and HIPRTC has no builtin std::initializer_list, so declare the minimal
// compiler-magic type locally so `using ::std::initializer_list` below resolves. clang
// accepts this as long as it lives in namespace std with exactly a {const T*, size} layout.
namespace std
{
template <class _Ep>
class initializer_list
{
  const _Ep* __begin_;
  decltype(sizeof(int)) __size_;

  _CCCL_API constexpr initializer_list(const _Ep* __b, decltype(sizeof(int)) __s) noexcept
      : __begin_(__b)
      , __size_(__s)
  {}

public:
  using value_type      = _Ep;
  using reference       = const _Ep&;
  using const_reference = const _Ep&;
  using size_type       = decltype(sizeof(int));
  using iterator        = const _Ep*;
  using const_iterator  = const _Ep*;

  _CCCL_API constexpr initializer_list() noexcept
      : __begin_(nullptr)
      , __size_(0)
  {}

  [[nodiscard]] _CCCL_API constexpr size_type size() const noexcept
  {
    return __size_;
  }
  [[nodiscard]] _CCCL_API constexpr const _Ep* begin() const noexcept
  {
    return __begin_;
  }
  [[nodiscard]] _CCCL_API constexpr const _Ep* end() const noexcept
  {
    return __begin_ + __size_;
  }
};
} // namespace std
#elif !_CCCL_COMPILER(NVRTC)
// On the host (and hipcc host pass, and libhipcxx's HIPRTC CI) include the system
// <initializer_list> so we share the same definition as system headers.
#  include <initializer_list>
#else // ^^^ !_CCCL_COMPILER(NVRTC) ^^^ / vvv _CCCL_COMPILER(NVRTC) vvv
#  if !defined(__NV_BUILTIN_INITIALIZER_LIST)
#    error "libcu++ requires -builtin-initializer-list=true when compiling with NVRTC"
#  endif // !__NV_BUILTIN_INITIALIZER_LIST
#endif // ^^^ _CCCL_COMPILER(NVRTC) ^^^

#include <cuda/std/__cccl/prologue.h>

_CCCL_BEGIN_NAMESPACE_CUDA_STD

using ::std::initializer_list;

template <class _Tp>
inline constexpr bool __is_cuda_std_initializer_list = false;

template <class _Ep>
inline constexpr bool __is_cuda_std_initializer_list<initializer_list<_Ep>> = true;

_CCCL_END_NAMESPACE_CUDA_STD

#include <cuda/std/__cccl/epilogue.h>

#endif // _CUDA_STD_INITIALIZER_LIST
