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
14 namespace v8 {
15 namespace internal {
16 namespace torque {
17
18 typedef std::vector<std::string> NameVector;
19
20 std::string StringLiteralUnquote(const std::string& s);
21 std::string StringLiteralQuote(const std::string& s);
22
23 [[noreturn]] void ReportError(const std::string& error);
24
25 std::string CamelifyString(const std::string& underscore_string);
26 std::string DashifyString(const std::string& underscore_string);
27
28 void ReplaceFileContentsIfDifferent(const std::string& file_path,
29 const std::string& contents);
30
31 std::string CurrentPositionAsString();
32
33 template <class T>
34 class Deduplicator {
35 public:
Add(T x)36 const T* Add(T x) { return &*(storage_.insert(std::move(x)).first); }
37
38 private:
39 std::unordered_set<T, base::hash<T>> storage_;
40 };
41
42 template <class C, class T>
PrintCommaSeparatedList(std::ostream & os,const T & list,C transform)43 void PrintCommaSeparatedList(std::ostream& os, const T& list, C transform) {
44 bool first = true;
45 for (auto& e : list) {
46 if (first) {
47 first = false;
48 } else {
49 os << ", ";
50 }
51 os << transform(e);
52 }
53 }
54
55 template <class T,
56 typename std::enable_if<
57 std::is_pointer<typename T::value_type>::value, int>::type = 0>
PrintCommaSeparatedList(std::ostream & os,const T & list)58 void PrintCommaSeparatedList(std::ostream& os, const T& list) {
59 bool first = true;
60 for (auto& e : list) {
61 if (first) {
62 first = false;
63 } else {
64 os << ", ";
65 }
66 os << *e;
67 }
68 }
69
70 template <class T,
71 typename std::enable_if<
72 !std::is_pointer<typename T::value_type>::value, int>::type = 0>
PrintCommaSeparatedList(std::ostream & os,const T & list)73 void PrintCommaSeparatedList(std::ostream& os, const T& list) {
74 bool first = true;
75 for (auto& e : list) {
76 if (first) {
77 first = false;
78 } else {
79 os << ", ";
80 }
81 os << e;
82 }
83 }
84
85 } // namespace torque
86 } // namespace internal
87 } // namespace v8
88
89 #endif // V8_TORQUE_UTILS_H_
90