v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
cpp-builder.h
Go to the documentation of this file.
1// Copyright 2021 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_CPP_BUILDER_H_
6#define V8_TORQUE_CPP_BUILDER_H_
7
8#include <stack>
9
10#include "src/torque/ast.h"
11#include "src/torque/types.h"
12
13namespace v8 {
14namespace internal {
15namespace torque {
16namespace cpp {
17
19 explicit TemplateParameter(std::string name) : name(std::move(name)) {}
20 TemplateParameter(std::string type, std::string name)
21 : name(std::move(name)), type(std::move(type)) {}
22
23 std::string name;
24 std::string type;
25};
26
27class Class {
28 public:
29 explicit Class(std::string name) : name_(std::move(name)) {}
30 Class(std::vector<TemplateParameter> template_parameters, std::string name)
31 : template_parameters_(std::move(template_parameters)),
32 name_(std::move(name)) {}
33
34 std::string GetName() const { return name_; }
35 std::vector<TemplateParameter> GetTemplateParameters() const {
37 }
38
39 private:
40 std::vector<TemplateParameter> template_parameters_;
41 std::string name_;
42};
43
44#define FUNCTION_FLAG_LIST(V) \
45 V(Inline, 0x01) \
46 V(V8Inline, 0x03) \
47 V(Const, 0x04) \
48 V(Constexpr, 0x08) \
49 V(Export, 0x10) \
50 V(Static, 0x20) \
51 V(Override, 0x40)
52
53class Function {
54 public:
56#define ENTRY(name, value) k##name = value,
58#undef ENTRY
59 };
60
61 struct Parameter {
62 std::string type;
63 std::string name;
64 std::string default_value;
65
66 Parameter(std::string type, std::string name,
67 std::string default_value = {})
68 : type(std::move(type)),
69 name(std::move(name)),
71 };
72
73 explicit Function(std::string name)
74 : pos_(CurrentSourcePosition::Get()),
75 owning_class_(nullptr),
76 name_(std::move(name)) {}
77 Function(Class* owning_class, std::string name)
78 : pos_(CurrentSourcePosition::Get()),
79 owning_class_(owning_class),
80 name_(std::move(name)) {}
81 ~Function() = default;
82
83 static Function DefaultGetter(std::string return_type, Class* owner,
84 std::string name) {
85 Function getter(owner, std::move(name));
86 getter.SetReturnType(std::move(return_type));
87 getter.SetInline();
88 getter.SetConst();
89 return getter;
90 }
91
92 static Function DefaultSetter(Class* owner, std::string name,
93 std::string parameter_type,
94 std::string parameter_name) {
95 Function setter(owner, std::move(name));
96 setter.SetReturnType("void");
97 setter.AddParameter(std::move(parameter_type), std::move(parameter_name));
98 setter.SetInline();
99 return setter;
100 }
101
102 void SetFlag(FunctionFlag flag, bool value = true) {
103 if (value) {
104 flags_ = flags_ | flag;
105 } else {
106 flags_ = flags_.without(flag);
107 }
108 }
109 void SetFlags(base::Flags<FunctionFlag> flags, bool value = true) {
110 if (value) {
111 flags_ |= flags;
112 } else {
113 flags_ &= ~flags;
114 }
115 }
116 bool HasFlag(FunctionFlag flag) const { return (flags_ & flag) == flag; }
117#define ACCESSOR(name, value) \
118 void Set##name(bool v = true) { SetFlag(k##name, v); } \
119 bool Is##name() const { return HasFlag(k##name); }
121#undef ACCESSOR
122
123 void SetDescription(std::string description) {
124 description_ = std::move(description);
125 }
126 void SetName(std::string name) { name_ = std::move(name); }
127 void SetReturnType(std::string return_type) {
128 return_type_ = std::move(return_type);
129 }
130 void AddParameter(std::string type, std::string name = {},
131 std::string default_value = {}) {
132 parameters_.emplace_back(std::move(type), std::move(name),
133 std::move(default_value));
134 }
135 void InsertParameter(int index, std::string type, std::string name = {},
136 std::string default_value = {}) {
137 DCHECK_GE(index, 0);
138 DCHECK_LE(index, parameters_.size());
139 parameters_.insert(
140 parameters_.begin() + index,
141 Parameter(std::move(type), std::move(name), std::move(default_value)));
142 }
143 std::vector<Parameter> GetParameters() const { return parameters_; }
144 std::vector<std::string> GetParameterNames() const {
145 std::vector<std::string> names;
146 std::transform(parameters_.begin(), parameters_.end(),
147 std::back_inserter(names),
148 [](const Parameter& p) { return p.name; });
149 return names;
150 }
151
152 static constexpr int kAutomaticIndentation = -1;
153 void PrintDeclaration(std::ostream& stream,
154 int indentation = kAutomaticIndentation) const;
155 void PrintDefinition(std::ostream& stream,
156 const std::function<void(std::ostream&)>& builder,
157 int indentation = 0) const;
158 void PrintInlineDefinition(std::ostream& stream,
159 const std::function<void(std::ostream&)>& builder,
160 int indentation = 2) const;
161 void PrintBeginDefinition(std::ostream& stream, int indentation = 0) const;
162 void PrintEndDefinition(std::ostream& stream, int indentation = 0) const;
163
164 protected:
165 void PrintDeclarationHeader(std::ostream& stream, int indentation) const;
166
167 private:
170 std::string description_;
171 std::string name_;
172 std::string return_type_;
173 std::vector<Parameter> parameters_;
175};
176
178#undef FUNCTION_FLAG_LIST
179
180class File {
181 public:
182 explicit File(std::ostream& stream) : stream_(&stream) {}
183
184 void BeginIncludeGuard(const std::string& name);
185 void EndIncludeGuard(const std::string& name);
186 void BeginNamespace(std::string name);
187 void BeginNamespace(std::string name0, std::string name1);
188 void EndNamespace(const std::string& name);
189 void EndNamespace(const std::string& name0, const std::string& name1);
190
191 void AddInclude(std::string include) { includes_.insert(std::move(include)); }
192
193 template <typename T>
194 File& operator<<(const T& value) {
195 s() << value;
196 return *this;
197 }
198
199 protected:
200 std::ostream& s() { return *stream_; }
201
202 private:
203 std::ostream* stream_;
204 std::set<std::string> includes_;
205 std::stack<std::string> namespaces_;
206};
207
209 public:
210 explicit IncludeGuardScope(File* file, std::string name)
211 : file_(file), name_(std::move(name)) {
213 }
216 name_() {
217 std::swap(file_, other.file_);
218 std::swap(name_, other.name_);
219 }
221 if (file_) {
223 }
224 }
227 if (this != &other) {
229 DCHECK(name_.empty());
230 std::swap(file_, other.file_);
231 std::swap(name_, other.name_);
232 }
233 return *this;
234 }
235
236 private:
238 std::string name_;
239};
240
241} // namespace cpp
242} // namespace torque
243} // namespace internal
244} // namespace v8
245
246#endif // V8_TORQUE_CPP_BUILDER_H_
#define DEFINE_OPERATORS_FOR_FLAGS(Type)
Definition flags.h:100
PropertyT * getter
#define ENTRY(Name,...)
Flags without(flag_type flag) const
Definition flags.h:92
std::string GetName() const
Definition cpp-builder.h:34
Class(std::vector< TemplateParameter > template_parameters, std::string name)
Definition cpp-builder.h:30
std::vector< TemplateParameter > template_parameters_
Definition cpp-builder.h:40
std::vector< TemplateParameter > GetTemplateParameters() const
Definition cpp-builder.h:35
void EndIncludeGuard(const std::string &name)
File(std::ostream &stream)
File & operator<<(const T &value)
std::stack< std::string > namespaces_
void AddInclude(std::string include)
void BeginIncludeGuard(const std::string &name)
void EndNamespace(const std::string &name)
std::set< std::string > includes_
void BeginNamespace(std::string name)
std::vector< Parameter > GetParameters() const
void AddParameter(std::string type, std::string name={}, std::string default_value={})
void PrintDeclaration(std::ostream &stream, int indentation=kAutomaticIndentation) const
bool HasFlag(FunctionFlag flag) const
void InsertParameter(int index, std::string type, std::string name={}, std::string default_value={})
std::vector< std::string > GetParameterNames() const
void PrintEndDefinition(std::ostream &stream, int indentation=0) const
static constexpr int kAutomaticIndentation
void SetName(std::string name)
base::Flags< FunctionFlag > flags_
void SetFlag(FunctionFlag flag, bool value=true)
void PrintInlineDefinition(std::ostream &stream, const std::function< void(std::ostream &)> &builder, int indentation=2) const
void SetFlags(base::Flags< FunctionFlag > flags, bool value=true)
void PrintBeginDefinition(std::ostream &stream, int indentation=0) const
static Function DefaultSetter(Class *owner, std::string name, std::string parameter_type, std::string parameter_name)
Definition cpp-builder.h:92
void PrintDeclarationHeader(std::ostream &stream, int indentation) const
static Function DefaultGetter(std::string return_type, Class *owner, std::string name)
Definition cpp-builder.h:83
std::vector< Parameter > parameters_
void PrintDefinition(std::ostream &stream, const std::function< void(std::ostream &)> &builder, int indentation=0) const
void SetReturnType(std::string return_type)
Function(Class *owning_class, std::string name)
Definition cpp-builder.h:77
void SetDescription(std::string description)
IncludeGuardScope & operator=(const IncludeGuardScope &)=delete
IncludeGuardScope & operator=(IncludeGuardScope &&other) V8_NOEXCEPT
IncludeGuardScope(IncludeGuardScope &&other) V8_NOEXCEPT
IncludeGuardScope(const IncludeGuardScope &)=delete
IncludeGuardScope(File *file, std::string name)
#define FUNCTION_FLAG_LIST(V)
Definition cpp-builder.h:44
#define ACCESSOR(name, value)
STL namespace.
V8_INLINE const Operation & Get(const Graph &graph, OpIndex index)
Definition graph.h:1231
Flag flags[]
Definition flags.cc:3797
return value
Definition map-inl.h:893
#define V8_NOEXCEPT
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define DCHECK_NULL(val)
Definition logging.h:491
#define DCHECK_GE(v1, v2)
Definition logging.h:488
#define DCHECK(condition)
Definition logging.h:482
Parameter(std::string type, std::string name, std::string default_value={})
Definition cpp-builder.h:66
TemplateParameter(std::string type, std::string name)
Definition cpp-builder.h:20