Skip to content

Commit 90bbecf

Browse files
committed
refactor: create span class; replace expression::array_ptr
1 parent b612eb6 commit 90bbecf

File tree

3 files changed

+89
-41
lines changed

3 files changed

+89
-41
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ quick_lint_js_add_library(
216216
quick-lint-js/port/process.h
217217
quick-lint-js/port/simd.h
218218
quick-lint-js/port/source-location.h
219+
quick-lint-js/port/span.h
219220
quick-lint-js/port/thread.cpp
220221
quick-lint-js/port/thread.h
221222
quick-lint-js/port/type-traits.h

src/quick-lint-js/fe/expression.h

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <quick-lint-js/fe/source-code-span.h>
1919
#include <quick-lint-js/fe/token.h>
2020
#include <quick-lint-js/port/char8.h>
21+
#include <quick-lint-js/port/span.h>
2122
#include <quick-lint-js/port/unreachable.h>
2223
#include <quick-lint-js/port/warning.h>
2324
#include <quick-lint-js/util/narrow-cast.h>
@@ -153,8 +154,9 @@ struct object_property_value_pair {
153154

154155
class expression_arena {
155156
public:
156-
template <class>
157-
class array_ptr;
157+
// TODO(strager): Inline.
158+
template <class T>
159+
using array_ptr = span<const T>;
158160

159161
using buffering_visitor_ptr = buffering_visitor *;
160162

@@ -301,41 +303,6 @@ Derived *expression_cast(expression *p) noexcept {
301303
template <class Derived, class Expression>
302304
Derived *expression_cast(Expression *) noexcept = delete;
303305

304-
template <class T>
305-
class expression_arena::array_ptr {
306-
public:
307-
explicit array_ptr() noexcept : data_(nullptr), size_(0) {}
308-
309-
explicit array_ptr(const T *data, int size) noexcept
310-
: data_(data), size_(size) {}
311-
312-
explicit array_ptr(const T *begin, const T *end) noexcept
313-
: data_(begin), size_(narrow_cast<int>(end - begin)) {}
314-
315-
T operator[](int index) const noexcept {
316-
QLJS_ASSERT(index >= 0);
317-
QLJS_ASSERT(index < this->size());
318-
return this->data_[index];
319-
}
320-
321-
T front() const noexcept { return (*this)[0]; }
322-
323-
T back() const noexcept { return (*this)[this->size() - 1]; }
324-
325-
const T *data() const noexcept { return this->data_; }
326-
327-
int size() const noexcept { return this->size_; }
328-
329-
const T *begin() const noexcept { return this->data_; }
330-
const T *end() const noexcept { return this->data_ + this->size_; }
331-
332-
bool empty() const noexcept { return this->size() == 0; }
333-
334-
private:
335-
const T *data_;
336-
int size_;
337-
};
338-
339306
template <class Expression, class... Args>
340307
expression *expression_arena::make_expression(Args &&... args) {
341308
expression *result(this->allocate<Expression>(std::forward<Args>(args)...));
@@ -348,7 +315,7 @@ inline expression_arena::array_ptr<T> expression_arena::make_array(
348315
bump_vector<T, monotonic_allocator> &&elements) {
349316
QLJS_ASSERT(elements.get_allocator() == &this->allocator_);
350317
// NOTE(strager): Adopt the pointer instead of copying.
351-
array_ptr<T> result(elements.data(), narrow_cast<int>(elements.size()));
318+
array_ptr<T> result(elements.data(), elements.size());
352319
elements.release();
353320
return result;
354321
}
@@ -357,7 +324,7 @@ template <class T>
357324
inline expression_arena::array_ptr<T> expression_arena::make_array(T *begin,
358325
T *end) {
359326
T *result_begin = this->allocate_array_move(begin, end);
360-
int size = narrow_cast<int>(end - begin);
327+
span_size size = narrow_cast<span_size>(end - begin);
361328
return array_ptr<T>(result_begin, size);
362329
}
363330

@@ -1161,7 +1128,8 @@ inline token_type expression::variable_identifier_token_type() const noexcept {
11611128
}
11621129

11631130
inline int expression::child_count() const noexcept {
1164-
return this->children().size();
1131+
// TODO(strager): Remove this cast.
1132+
return narrow_cast<int>(this->children().size());
11651133
}
11661134

11671135
inline expression *expression::child(int index) const noexcept {
@@ -1286,7 +1254,8 @@ inline expression *expression::without_paren() const noexcept {
12861254
inline int expression::object_entry_count() const noexcept {
12871255
switch (this->kind_) {
12881256
case expression_kind::object:
1289-
return static_cast<const expression::object *>(this)->entries_.size();
1257+
// TODO(strager): Remove this cast.
1258+
return narrow_cast<int>(static_cast<const expression::object *>(this)->entries_.size());
12901259

12911260
default:
12921261
QLJS_UNEXPECTED_EXPRESSION_KIND();

src/quick-lint-js/port/span.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (C) 2020 Matthew "strager" Glazar
2+
// See end of file for extended copyright information.
3+
4+
#ifndef QUICK_LINT_JS_PORT_SPAN_H
5+
#define QUICK_LINT_JS_PORT_SPAN_H
6+
7+
#include <array>
8+
#include <cstddef>
9+
#include <quick-lint-js/assert.h>
10+
#include <quick-lint-js/util/narrow-cast.h>
11+
12+
namespace quick_lint_js {
13+
using span_size = std::ptrdiff_t;
14+
15+
// Like std::span.
16+
template <class T>
17+
class span {
18+
public:
19+
using value_type = T;
20+
using size_type = span_size;
21+
using difference_type = span_size;
22+
using reference = T &;
23+
using const_reference = const T &;
24+
using pointer = T *;
25+
using const_pointer = const T *;
26+
using iterator = T *;
27+
28+
explicit span() noexcept : data_(nullptr), size_(0) {}
29+
30+
explicit span(T *data, size_type size) noexcept : data_(data), size_(size) {}
31+
32+
explicit span(T *begin, T *end) noexcept
33+
: data_(begin), size_(narrow_cast<span_size>(end - begin)) {}
34+
35+
T &operator[](size_type index) const noexcept {
36+
QLJS_ASSERT(index >= 0);
37+
QLJS_ASSERT(index < this->size());
38+
return this->data_[index];
39+
}
40+
41+
T front() const noexcept { return (*this)[0]; }
42+
43+
T back() const noexcept { return (*this)[this->size() - 1]; }
44+
45+
T *data() const noexcept { return this->data_; }
46+
47+
size_type size() const noexcept { return this->size_; }
48+
49+
T *begin() const noexcept { return this->data_; }
50+
T *end() const noexcept { return this->data_ + this->size_; }
51+
52+
bool empty() const noexcept { return this->size() == 0; }
53+
54+
private:
55+
T *data_;
56+
span_size size_;
57+
};
58+
}
59+
60+
#endif
61+
62+
// quick-lint-js finds bugs in JavaScript programs.
63+
// Copyright (C) 2020 Matthew "strager" Glazar
64+
//
65+
// This file is part of quick-lint-js.
66+
//
67+
// quick-lint-js is free software: you can redistribute it and/or modify
68+
// it under the terms of the GNU General Public License as published by
69+
// the Free Software Foundation, either version 3 of the License, or
70+
// (at your option) any later version.
71+
//
72+
// quick-lint-js is distributed in the hope that it will be useful,
73+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
74+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
75+
// GNU General Public License for more details.
76+
//
77+
// You should have received a copy of the GNU General Public License
78+
// along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>.

0 commit comments

Comments
 (0)