V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
utils.h
1 // Copyright 2017 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_TORQUE_UTILS_H_
6 #define V8_TORQUE_UTILS_H_
7 
8 #include <string>
9 #include <unordered_set>
10 #include <vector>
11 
12 #include "src/base/functional.h"
13 #include "src/torque/contextual.h"
14 
15 namespace v8 {
16 namespace internal {
17 namespace torque {
18 
19 typedef std::vector<std::string> NameVector;
20 
21 std::string StringLiteralUnquote(const std::string& s);
22 std::string StringLiteralQuote(const std::string& s);
23 
24 class LintErrorStatus : public ContextualClass<LintErrorStatus> {
25  public:
26  LintErrorStatus() : has_lint_errors_(false) {}
27 
28  static bool HasLintErrors() { return Get().has_lint_errors_; }
29  static void SetLintError() { Get().has_lint_errors_ = true; }
30 
31  private:
32  bool has_lint_errors_;
33 };
34 
35 void LintError(const std::string& error);
36 
37 // Prints a LintError with the format "{type} '{name}' doesn't follow
38 // '{convention}' naming convention".
39 void NamingConventionError(const std::string& type, const std::string& name,
40  const std::string& convention);
41 
42 bool IsLowerCamelCase(const std::string& s);
43 bool IsUpperCamelCase(const std::string& s);
44 bool IsSnakeCase(const std::string& s);
45 bool IsValidNamespaceConstName(const std::string& s);
46 bool IsValidTypeName(const std::string& s);
47 
48 [[noreturn]] void ReportErrorString(const std::string& error);
49 template <class... Args>
50 [[noreturn]] void ReportError(Args&&... args) {
51  std::stringstream s;
52  USE((s << std::forward<Args>(args))...);
53  ReportErrorString(s.str());
54 }
55 
56 std::string CamelifyString(const std::string& underscore_string);
57 std::string DashifyString(const std::string& underscore_string);
58 
59 void ReplaceFileContentsIfDifferent(const std::string& file_path,
60  const std::string& contents);
61 
62 std::string CurrentPositionAsString();
63 
64 template <class T>
65 class Deduplicator {
66  public:
67  const T* Add(T x) { return &*(storage_.insert(std::move(x)).first); }
68 
69  private:
70  std::unordered_set<T, base::hash<T>> storage_;
71 };
72 
73 template <class C, class T>
74 void PrintCommaSeparatedList(std::ostream& os, const T& list, C transform) {
75  bool first = true;
76  for (auto& e : list) {
77  if (first) {
78  first = false;
79  } else {
80  os << ", ";
81  }
82  os << transform(e);
83  }
84 }
85 
86 template <class T,
87  typename std::enable_if<
88  std::is_pointer<typename T::value_type>::value, int>::type = 0>
89 void PrintCommaSeparatedList(std::ostream& os, const T& list) {
90  bool first = true;
91  for (auto& e : list) {
92  if (first) {
93  first = false;
94  } else {
95  os << ", ";
96  }
97  os << *e;
98  }
99 }
100 
101 template <class T,
102  typename std::enable_if<
103  !std::is_pointer<typename T::value_type>::value, int>::type = 0>
104 void PrintCommaSeparatedList(std::ostream& os, const T& list) {
105  bool first = true;
106  for (auto& e : list) {
107  if (first) {
108  first = false;
109  } else {
110  os << ", ";
111  }
112  os << e;
113  }
114 }
115 
116 struct BottomOffset {
117  size_t offset;
118  BottomOffset& operator++() {
119  ++offset;
120  return *this;
121  }
122  BottomOffset operator+(size_t x) const { return BottomOffset{offset + x}; }
123  BottomOffset operator-(size_t x) const {
124  DCHECK_LE(x, offset);
125  return BottomOffset{offset - x};
126  }
127  bool operator<(const BottomOffset& other) const {
128  return offset < other.offset;
129  }
130  bool operator<=(const BottomOffset& other) const {
131  return offset <= other.offset;
132  }
133  bool operator==(const BottomOffset& other) const {
134  return offset == other.offset;
135  }
136  bool operator!=(const BottomOffset& other) const {
137  return offset != other.offset;
138  }
139 };
140 
141 inline std::ostream& operator<<(std::ostream& out, BottomOffset from_bottom) {
142  return out << "BottomOffset{" << from_bottom.offset << "}";
143 }
144 
145 // An iterator-style range of stack slots.
146 class StackRange {
147  public:
148  StackRange(BottomOffset begin, BottomOffset end) : begin_(begin), end_(end) {
149  DCHECK_LE(begin_, end_);
150  }
151 
152  bool operator==(const StackRange& other) const {
153  return begin_ == other.begin_ && end_ == other.end_;
154  }
155 
156  void Extend(StackRange adjacent) {
157  DCHECK_EQ(end_, adjacent.begin_);
158  end_ = adjacent.end_;
159  }
160 
161  size_t Size() const { return end_.offset - begin_.offset; }
162  BottomOffset begin() const { return begin_; }
163  BottomOffset end() const { return end_; }
164 
165  private:
166  BottomOffset begin_;
167  BottomOffset end_;
168 };
169 
170 template <class T>
171 class Stack {
172  public:
173  using value_type = T;
174  Stack() = default;
175  Stack(std::initializer_list<T> initializer)
176  : Stack(std::vector<T>(initializer)) {}
177  explicit Stack(std::vector<T> v) : elements_(std::move(v)) {}
178  size_t Size() const { return elements_.size(); }
179  const T& Peek(BottomOffset from_bottom) const {
180  return elements_.at(from_bottom.offset);
181  }
182  void Poke(BottomOffset from_bottom, T x) {
183  elements_.at(from_bottom.offset) = std::move(x);
184  }
185  void Push(T x) { elements_.push_back(std::move(x)); }
186  StackRange TopRange(size_t slot_count) const {
187  DCHECK_GE(Size(), slot_count);
188  return StackRange{AboveTop() - slot_count, AboveTop()};
189  }
190  StackRange PushMany(const std::vector<T>& v) {
191  for (const T& x : v) {
192  Push(x);
193  }
194  return TopRange(v.size());
195  }
196  const T& Top() const { return Peek(AboveTop() - 1); }
197  T Pop() {
198  T result = std::move(elements_.back());
199  elements_.pop_back();
200  return result;
201  }
202  std::vector<T> PopMany(size_t count) {
203  DCHECK_GE(elements_.size(), count);
204  std::vector<T> result;
205  result.reserve(count);
206  for (auto it = elements_.end() - count; it != elements_.end(); ++it) {
207  result.push_back(std::move(*it));
208  }
209  elements_.resize(elements_.size() - count);
210  return result;
211  }
212  // The invalid offset above the top element. This is useful for StackRange.
213  BottomOffset AboveTop() const { return BottomOffset{Size()}; }
214  // Delete the slots in {range}, moving higher slots to fill the gap.
215  void DeleteRange(StackRange range) {
216  DCHECK_LE(range.end(), AboveTop());
217  if (range.Size() == 0) return;
218  for (BottomOffset i = range.end(); i < AboveTop(); ++i) {
219  elements_[i.offset - range.Size()] = std::move(elements_[i.offset]);
220  }
221  elements_.resize(elements_.size() - range.Size());
222  }
223 
224  bool operator==(const Stack& other) const {
225  return elements_ == other.elements_;
226  }
227  bool operator!=(const Stack& other) const {
228  return elements_ != other.elements_;
229  }
230 
231  T* begin() { return elements_.data(); }
232  T* end() { return begin() + elements_.size(); }
233  const T* begin() const { return elements_.data(); }
234  const T* end() const { return begin() + elements_.size(); }
235 
236  private:
237  std::vector<T> elements_;
238 };
239 
240 template <class T>
241 T* CheckNotNull(T* x) {
242  CHECK_NOT_NULL(x);
243  return x;
244 }
245 
246 template <class T>
247 inline std::ostream& operator<<(std::ostream& os, Stack<T>& t) {
248  os << "Stack{";
249  PrintCommaSeparatedList(os, t);
250  os << "}";
251  return os;
252 }
253 class ToString {
254  public:
255  template <class T>
256  ToString& operator<<(T&& x) {
257  s_ << std::forward<T>(x);
258  return *this;
259  }
260  operator std::string() { return s_.str(); }
261 
262  private:
263  std::stringstream s_;
264 };
265 
266 } // namespace torque
267 } // namespace internal
268 } // namespace v8
269 
270 #endif // V8_TORQUE_UTILS_H_
Definition: libplatform.h:13