From 43a50c010bf90ac03a4b51455377135d5950ebd5 Mon Sep 17 00:00:00 2001 From: Benjamin Bannier Date: Wed, 12 May 2021 11:20:44 +0200 Subject: [PATCH] Make sure container sizes are runtime integers. For the HILTI runtime containers we previously directly forwarded the `size` functions of the underlying stdlib containers. This could have lead to them returning a type not expected by our runtime, e.g., on macos these might return `unsigned long` values which our runtime e.g., doesn't know how to print. This patch changes these functions so that now they return types which we expect our runtimes to be able to work with. --- hilti/runtime/include/types/map.h | 7 ++----- hilti/runtime/include/types/set.h | 6 +++--- hilti/runtime/include/types/vector.h | 5 +++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/hilti/runtime/include/types/map.h b/hilti/runtime/include/types/map.h index c0ca7f8ec..1d1cf5fb7 100644 --- a/hilti/runtime/include/types/map.h +++ b/hilti/runtime/include/types/map.h @@ -176,6 +176,7 @@ class Map : protected std::map { using key_type = typename M::key_type; using value_type = typename M::value_type; + using size_type = uint64_t; using iterator = typename map::Iterator; using const_iterator = typename map::ConstIterator; @@ -257,13 +258,12 @@ class Map : protected std::map { auto end() const { return this->cend(); } auto begin() { return iterator(static_cast(*this).begin(), _control); } - auto end() { return iterator(static_cast(*this).end(), _control); } auto cbegin() const { return const_iterator(static_cast(*this).begin(), _control); } - auto cend() const { return const_iterator(static_cast(*this).end(), _control); } + size_type size() const { return M::size(); } /** Erases all elements from the map. * @@ -292,9 +292,6 @@ class Map : protected std::map { return removed; } - // Methods of `std::map`. - using M::size; - friend bool operator==(const Map& a, const Map& b) { return static_cast(a) == static_cast(b); } friend bool operator!=(const Map& a, const Map& b) { return ! (a == b); } diff --git a/hilti/runtime/include/types/set.h b/hilti/runtime/include/types/set.h index 679065244..de3d27a65 100644 --- a/hilti/runtime/include/types/set.h +++ b/hilti/runtime/include/types/set.h @@ -120,7 +120,7 @@ class Set : protected std::set { using key_type = T; using value_type = T; - using size_type = typename V::size_type; + using size_type = uint64_t; Set() = default; Set(const Set&) = default; @@ -140,9 +140,10 @@ class Set : protected std::set { bool contains(const T& t) const { return this->count(t); } auto begin() const { return iterator(static_cast(*this).begin(), empty() ? nullptr : _control); } - auto end() const { return iterator(static_cast(*this).end(), empty() ? nullptr : _control); } + size_type size() const { return V::size(); } + /** Removes an element from the set. * * This function invalidates all iterators into the set. @@ -171,7 +172,6 @@ class Set : protected std::set { // Methods of `std::set`. These methods *must not* cause any iterator invalidation. using V::empty; using V::insert; - using V::size; friend bool operator==(const Set& a, const Set& b) { return static_cast(a) == static_cast(b); } friend bool operator!=(const Set& a, const Set& b) { return ! (a == b); } diff --git a/hilti/runtime/include/types/vector.h b/hilti/runtime/include/types/vector.h index 30d66bb66..929dabd7f 100644 --- a/hilti/runtime/include/types/vector.h +++ b/hilti/runtime/include/types/vector.h @@ -253,7 +253,7 @@ class Vector : protected std::vector { using V = std::vector; - using size_type = typename V::size_type; + using size_type = uint64_t; using reference = T&; using const_reference = const T&; using iterator = vector::Iterator; @@ -465,6 +465,8 @@ class Vector : protected std::vector { auto cbegin() const { return const_iterator(0u, _control); } auto cend() const { return const_iterator(size(), _control); } + size_t size() const { return V::size(); } + // Methods of `std::vector`. using typename V::value_type; using V::at; @@ -475,7 +477,6 @@ class Vector : protected std::vector { using V::push_back; using V::reserve; using V::resize; - using V::size; friend bool operator==(const Vector& a, const Vector& b) { return static_cast(a) == static_cast(b);