From 420e809feefa647fbe6f624f7ca02206ad6aa7ee Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 7 Aug 2020 09:33:05 +0200 Subject: Shell: Store ListValue's values in a NonnullRefPtrVector A ListValue never stores null Values, so it makes sense to restrict it. This also propagates use of NonnullRefPtr to the create() helpers. There's a small bit of awkwardness around the use of initializer_list, since we cannot directly construct a NonnullRefPtrVector from one. --- Shell/AST.cpp | 20 ++++++++++---------- Shell/AST.h | 10 +++++----- Shell/Builtin.cpp | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Shell/AST.cpp b/Shell/AST.cpp index bb441d907d..69e80ee612 100644 --- a/Shell/AST.cpp +++ b/Shell/AST.cpp @@ -36,13 +36,13 @@ namespace AST { template -static inline RefPtr create(Args... args) +static inline NonnullRefPtr create(Args... args) { return adopt(*new T(args...)); } template -static inline RefPtr create(std::initializer_list> arg) +static inline NonnullRefPtr create(std::initializer_list> arg) { return adopt(*new T(arg)); } @@ -226,7 +226,7 @@ RefPtr ListConcatenate::run(RefPtr shell) for (auto& element : m_list) { if (!result) { - result = create({ element->run(shell)->resolve_without_cast(shell) }); + result = create({ element->run(shell)->resolve_without_cast(shell).release_nonnull() }); continue; } auto element_value = element->run(shell)->resolve_without_cast(shell); @@ -239,7 +239,7 @@ RefPtr ListConcatenate::run(RefPtr shell) else result = create(move(joined_commands)); } else { - Vector> values; + NonnullRefPtrVector values; if (result->is_list_without_resolution()) { values.append(static_cast(result.ptr())->values()); @@ -248,7 +248,7 @@ RefPtr ListConcatenate::run(RefPtr shell) values.append(create(result)); } - values.append(move(element_value)); + values.append(element_value.release_nonnull()); result = create(move(values)); } @@ -496,7 +496,7 @@ RefPtr CastToList::run(RefPtr shell) return inner_value; auto values = inner_value->resolve_as_list(shell); - Vector> cast_values; + NonnullRefPtrVector cast_values; for (auto& value : values) cast_values.append(create(value)); @@ -754,7 +754,7 @@ RefPtr ForLoop::run(RefPtr shell) if (!m_block) return create({}); - Vector> values; + NonnullRefPtrVector values; auto resolved = m_iterated_expression->run(shell)->resolve_without_cast(shell); if (resolved->is_list_without_resolution()) values = static_cast(resolved.ptr())->values(); @@ -1899,16 +1899,16 @@ Vector ListValue::resolve_as_list(RefPtr shell) { Vector values; for (auto& value : m_contained_values) - values.append(value->resolve_as_list(shell)); + values.append(value.resolve_as_list(shell)); return values; } RefPtr ListValue::resolve_without_cast(RefPtr shell) { - Vector> values; + NonnullRefPtrVector values; for (auto& value : m_contained_values) - values.append(value->resolve_without_cast(shell)); + values.append(value.resolve_without_cast(shell).release_nonnull()); return create(move(values)); } diff --git a/Shell/AST.h b/Shell/AST.h index 4cdd805e00..546c9882a4 100644 --- a/Shell/AST.h +++ b/Shell/AST.h @@ -238,16 +238,16 @@ public: virtual bool is_list() const override { return true; } virtual bool is_list_without_resolution() const override { return true; } ListValue(Vector values); - ListValue(Vector> values) - : m_contained_values(move(values)) + ListValue(Vector> values) + : m_contained_values(move(static_cast&>(values))) { } - const Vector>& values() const { return m_contained_values; } - Vector>& values() { return m_contained_values; } + const NonnullRefPtrVector& values() const { return m_contained_values; } + NonnullRefPtrVector& values() { return m_contained_values; } private: - Vector> m_contained_values; + NonnullRefPtrVector m_contained_values; }; class StringValue final : public Value { diff --git a/Shell/Builtin.cpp b/Shell/Builtin.cpp index 8e547f0c0d..9ca91a71ee 100644 --- a/Shell/Builtin.cpp +++ b/Shell/Builtin.cpp @@ -664,7 +664,7 @@ int Shell::builtin_shift(int argc, const char** argv) } if (!argv_->is_list()) - argv_ = adopt(*new AST::ListValue({ argv_ })); + argv_ = adopt(*new AST::ListValue({ argv_.release_nonnull() })); auto& values = static_cast(argv_.ptr())->values(); if ((size_t)count > values.size()) { -- cgit v1.2.3