5 #ifndef V8_TORQUE_UTILS_H_ 6 #define V8_TORQUE_UTILS_H_ 9 #include <unordered_set> 12 #include "src/base/functional.h" 13 #include "src/torque/contextual.h" 19 typedef std::vector<std::string> NameVector;
21 std::string StringLiteralUnquote(
const std::string& s);
22 std::string StringLiteralQuote(
const std::string& s);
28 static bool HasLintErrors() {
return Get().has_lint_errors_; }
29 static void SetLintError() { Get().has_lint_errors_ =
true; }
32 bool has_lint_errors_;
35 void LintError(
const std::string& error);
39 void NamingConventionError(
const std::string&
type,
const std::string& name,
40 const std::string& convention);
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);
48 [[noreturn]]
void ReportErrorString(
const std::string& error);
49 template <
class... Args>
50 [[noreturn]]
void ReportError(Args&&... args) {
52 USE((s << std::forward<Args>(args))...);
53 ReportErrorString(s.str());
56 std::string CamelifyString(
const std::string& underscore_string);
57 std::string DashifyString(
const std::string& underscore_string);
59 void ReplaceFileContentsIfDifferent(
const std::string& file_path,
60 const std::string& contents);
62 std::string CurrentPositionAsString();
67 const T* Add(
T x) {
return &*(storage_.insert(std::move(x)).first); }
70 std::unordered_set<T, base::hash<T>> storage_;
73 template <
class C,
class T>
74 void PrintCommaSeparatedList(std::ostream& os,
const T& list, C transform) {
76 for (
auto& e : list) {
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) {
91 for (
auto& e : list) {
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) {
106 for (
auto& e : list) {
124 DCHECK_LE(x, offset);
128 return offset < other.offset;
131 return offset <= other.offset;
134 return offset == other.offset;
137 return offset != other.offset;
141 inline std::ostream& operator<<(std::ostream& out,
BottomOffset from_bottom) {
142 return out <<
"BottomOffset{" << from_bottom.offset <<
"}";
149 DCHECK_LE(begin_, end_);
152 bool operator==(
const StackRange& other)
const {
153 return begin_ == other.begin_ && end_ == other.end_;
157 DCHECK_EQ(end_, adjacent.begin_);
158 end_ = adjacent.end_;
161 size_t Size()
const {
return end_.offset - begin_.offset; }
173 using value_type = T;
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(); }
180 return elements_.at(from_bottom.offset);
183 elements_.at(from_bottom.offset) = std::move(x);
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()};
190 StackRange PushMany(
const std::vector<T>& v) {
191 for (
const T& x : v) {
194 return TopRange(v.size());
196 const T& Top()
const {
return Peek(AboveTop() - 1); }
198 T result = std::move(elements_.back());
199 elements_.pop_back();
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));
209 elements_.resize(elements_.size() - count);
216 DCHECK_LE(range.end(), AboveTop());
217 if (range.Size() == 0)
return;
219 elements_[
i.offset - range.Size()] = std::move(elements_[
i.offset]);
221 elements_.resize(elements_.size() - range.Size());
224 bool operator==(
const Stack& other)
const {
225 return elements_ == other.elements_;
227 bool operator!=(
const Stack& other)
const {
228 return elements_ != other.elements_;
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(); }
237 std::vector<T> elements_;
241 T* CheckNotNull(T* x) {
247 inline std::ostream& operator<<(std::ostream& os, Stack<T>& t) {
249 PrintCommaSeparatedList(os, t);
257 s_ << std::forward<T>(x);
260 operator std::string() {
return s_.str(); }
263 std::stringstream s_;
270 #endif // V8_TORQUE_UTILS_H_