diff options
| author | Arseney300 <Arseney300@gmail.com> | 2026-04-12 02:01:25 +0700 |
|---|---|---|
| committer | Arseney300 <Arseney300@gmail.com> | 2026-04-12 02:01:25 +0700 |
| commit | 2496ffd6d97c3ccd3e325687442d31ab035479a1 (patch) | |
| tree | 5d176669f823182fe74261f076dc11c8e8f88025 /kernel/lib/libcxx/include/array | |
| parent | 496246c92dabe6757480147016490ea6ae43bb66 (diff) | |
bastion: add freestanding C++ standard library (libcxx)feature/freestanding-libcxx
Header-only implementation of a C++ standard library subset for the
freestanding kernel, using Clang builtins wherever possible. Provides
19 public headers (~35 internal files): type_traits, utility, memory,
algorithm, functional, concepts, array, string_view, span, optional,
expected, variant, tuple, bit, limits, new, initializer_list, cstdint,
cstddef, and source_location.
Integration changes:
- kernel/Makefile: add -Ilib/libcxx/include to include path
- kernel/lib/string.cpp: add memchr and strncmp (needed by string_view)
- kernel/lib/cxxabi.cpp: guard placement new against <new> header conflict
- kernel/include/kernel/kernel.h: Panic() now uses std::source_location
- kernel/core/kernel.cpp: Panic() implementation updated to match
Builds cleanly on both x86_64 and aarch64 with zero new warnings.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'kernel/lib/libcxx/include/array')
| -rw-r--r-- | kernel/lib/libcxx/include/array | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/kernel/lib/libcxx/include/array b/kernel/lib/libcxx/include/array new file mode 100644 index 0000000..cb022a3 --- /dev/null +++ b/kernel/lib/libcxx/include/array @@ -0,0 +1,191 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the BastionOS freestanding C++ standard library. +// +// std::array — fixed-size aggregate container. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBBASTION_ARRAY +#define _LIBBASTION_ARRAY + +#include <__config> +#include <__type_traits/integral_constant.h> +#include <__type_traits/type_modifications.h> +#include <__utility/move.h> +#include <__utility/swap.h> +#include <__algorithm/fill.h> +#include <__algorithm/comparison.h> +#include <cstddef> + +_LIBBASTION_BEGIN_NAMESPACE_STD + +template<class _Tp, size_t _Np> +struct array { + using value_type = _Tp; + using size_type = size_t; + using difference_type = ptrdiff_t; + using reference = _Tp&; + using const_reference = const _Tp&; + using pointer = _Tp*; + using const_pointer = const _Tp*; + using iterator = _Tp*; + using const_iterator = const _Tp*; + + // Aggregate — public data member. + _Tp __data_[_Np]; + + // Element access + _LIBBASTION_NODISCARD constexpr reference at(size_type __pos) { _LIBBASTION_ASSERT(__pos < _Np, "array::at out of range"); return __data_[__pos]; } + _LIBBASTION_NODISCARD constexpr const_reference at(size_type __pos) const { _LIBBASTION_ASSERT(__pos < _Np, "array::at out of range"); return __data_[__pos]; } + _LIBBASTION_NODISCARD constexpr reference operator[](size_type __pos) { return __data_[__pos]; } + _LIBBASTION_NODISCARD constexpr const_reference operator[](size_type __pos) const { return __data_[__pos]; } + _LIBBASTION_NODISCARD constexpr reference front() { return __data_[0]; } + _LIBBASTION_NODISCARD constexpr const_reference front() const { return __data_[0]; } + _LIBBASTION_NODISCARD constexpr reference back() { return __data_[_Np - 1]; } + _LIBBASTION_NODISCARD constexpr const_reference back() const { return __data_[_Np - 1]; } + _LIBBASTION_NODISCARD constexpr pointer data() noexcept { return __data_; } + _LIBBASTION_NODISCARD constexpr const_pointer data() const noexcept { return __data_; } + + // Iterators + _LIBBASTION_NODISCARD constexpr iterator begin() noexcept { return __data_; } + _LIBBASTION_NODISCARD constexpr const_iterator begin() const noexcept { return __data_; } + _LIBBASTION_NODISCARD constexpr const_iterator cbegin() const noexcept { return __data_; } + _LIBBASTION_NODISCARD constexpr iterator end() noexcept { return __data_ + _Np; } + _LIBBASTION_NODISCARD constexpr const_iterator end() const noexcept { return __data_ + _Np; } + _LIBBASTION_NODISCARD constexpr const_iterator cend() const noexcept { return __data_ + _Np; } + + // Capacity + _LIBBASTION_NODISCARD constexpr bool empty() const noexcept { return _Np == 0; } + _LIBBASTION_NODISCARD constexpr size_type size() const noexcept { return _Np; } + _LIBBASTION_NODISCARD constexpr size_type max_size() const noexcept { return _Np; } + + // Operations + constexpr void fill(const _Tp& __val) { std::fill(begin(), end(), __val); } + + constexpr void swap(array& __other) noexcept(is_nothrow_swappable_v<_Tp>) { + for (size_type __i = 0; __i < _Np; ++__i) + std::swap(__data_[__i], __other.__data_[__i]); + } +}; + +// Zero-size specialization +template<class _Tp> +struct array<_Tp, 0> { + using value_type = _Tp; + using size_type = size_t; + using difference_type = ptrdiff_t; + using reference = _Tp&; + using const_reference = const _Tp&; + using pointer = _Tp*; + using const_pointer = const _Tp*; + using iterator = _Tp*; + using const_iterator = const _Tp*; + + // No __data_ member for zero-size array. + + _LIBBASTION_NODISCARD constexpr reference at(size_type) { _LIBBASTION_TRAP(); } + _LIBBASTION_NODISCARD constexpr const_reference at(size_type) const { _LIBBASTION_TRAP(); } + _LIBBASTION_NODISCARD constexpr reference operator[](size_type) { _LIBBASTION_TRAP(); } + _LIBBASTION_NODISCARD constexpr const_reference operator[](size_type) const { _LIBBASTION_TRAP(); } + _LIBBASTION_NODISCARD constexpr reference front() { _LIBBASTION_TRAP(); } + _LIBBASTION_NODISCARD constexpr const_reference front() const { _LIBBASTION_TRAP(); } + _LIBBASTION_NODISCARD constexpr reference back() { _LIBBASTION_TRAP(); } + _LIBBASTION_NODISCARD constexpr const_reference back() const { _LIBBASTION_TRAP(); } + _LIBBASTION_NODISCARD constexpr pointer data() noexcept { return nullptr; } + _LIBBASTION_NODISCARD constexpr const_pointer data() const noexcept { return nullptr; } + + _LIBBASTION_NODISCARD constexpr iterator begin() noexcept { return nullptr; } + _LIBBASTION_NODISCARD constexpr const_iterator begin() const noexcept { return nullptr; } + _LIBBASTION_NODISCARD constexpr const_iterator cbegin() const noexcept { return nullptr; } + _LIBBASTION_NODISCARD constexpr iterator end() noexcept { return nullptr; } + _LIBBASTION_NODISCARD constexpr const_iterator end() const noexcept { return nullptr; } + _LIBBASTION_NODISCARD constexpr const_iterator cend() const noexcept { return nullptr; } + + _LIBBASTION_NODISCARD constexpr bool empty() const noexcept { return true; } + _LIBBASTION_NODISCARD constexpr size_type size() const noexcept { return 0; } + _LIBBASTION_NODISCARD constexpr size_type max_size() const noexcept { return 0; } + + constexpr void fill(const _Tp&) {} + constexpr void swap(array&) noexcept {} +}; + +// Deduction guide +template<class _Tp, class... _Up> +array(_Tp, _Up...) -> array<_Tp, 1 + sizeof...(_Up)>; + +// Non-member swap +template<class _Tp, size_t _Np> +inline constexpr void swap(array<_Tp, _Np>& __a, array<_Tp, _Np>& __b) noexcept(noexcept(__a.swap(__b))) { + __a.swap(__b); +} + +// Comparison +template<class _Tp, size_t _Np> +_LIBBASTION_NODISCARD constexpr bool operator==(const array<_Tp, _Np>& __a, const array<_Tp, _Np>& __b) { + return std::equal(__a.begin(), __a.end(), __b.begin()); +} + +template<class _Tp, size_t _Np> +_LIBBASTION_NODISCARD constexpr bool operator!=(const array<_Tp, _Np>& __a, const array<_Tp, _Np>& __b) { + return !(__a == __b); +} + +// get<I> for structured bindings +template<size_t _Ip, class _Tp, size_t _Np> +_LIBBASTION_NODISCARD constexpr _Tp& get(array<_Tp, _Np>& __a) noexcept { + static_assert(_Ip < _Np, "array index out of range"); + return __a.__data_[_Ip]; +} + +template<size_t _Ip, class _Tp, size_t _Np> +_LIBBASTION_NODISCARD constexpr const _Tp& get(const array<_Tp, _Np>& __a) noexcept { + static_assert(_Ip < _Np, "array index out of range"); + return __a.__data_[_Ip]; +} + +template<size_t _Ip, class _Tp, size_t _Np> +_LIBBASTION_NODISCARD constexpr _Tp&& get(array<_Tp, _Np>&& __a) noexcept { + static_assert(_Ip < _Np, "array index out of range"); + return std::move(__a.__data_[_Ip]); +} + +// tuple_size / tuple_element for structured bindings +template<class _Tp, size_t _Np> +struct tuple_size<array<_Tp, _Np>> : integral_constant<size_t, _Np> {}; + +template<size_t _Ip, class _Tp, size_t _Np> +struct tuple_element<_Ip, array<_Tp, _Np>> { + static_assert(_Ip < _Np, "array index out of range"); + using type = _Tp; +}; + +// to_array (C++20) +namespace __detail { + +template<class _Tp, size_t _Np, size_t... _Idx> +constexpr array<remove_cv_t<_Tp>, _Np> __to_array_impl(_Tp (&__a)[_Np], index_sequence<_Idx...>) { + return {{__a[_Idx]...}}; +} + +template<class _Tp, size_t _Np, size_t... _Idx> +constexpr array<remove_cv_t<_Tp>, _Np> __to_array_impl(_Tp (&&__a)[_Np], index_sequence<_Idx...>) { + return {{std::move(__a[_Idx])...}}; +} + +} // namespace __detail + +template<class _Tp, size_t _Np> +constexpr array<remove_cv_t<_Tp>, _Np> to_array(_Tp (&__a)[_Np]) { + return __detail::__to_array_impl(__a, make_index_sequence<_Np>{}); +} + +template<class _Tp, size_t _Np> +constexpr array<remove_cv_t<_Tp>, _Np> to_array(_Tp (&&__a)[_Np]) { + return __detail::__to_array_impl(std::move(__a), make_index_sequence<_Np>{}); +} + +_LIBBASTION_END_NAMESPACE_STD + +#endif // _LIBBASTION_ARRAY |
