1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_TORQUE_TYPES_H_
#define V8_TORQUE_TYPES_H_
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include "src/base/optional.h"
#include "src/torque/utils.h"
namespace v8 {
namespace internal {
namespace torque {
static const char* const CONSTEXPR_TYPE_PREFIX = "constexpr ";
static const char* const NEVER_TYPE_STRING = "never";
static const char* const CONSTEXPR_BOOL_TYPE_STRING = "constexpr bool";
static const char* const BOOL_TYPE_STRING = "bool";
static const char* const VOID_TYPE_STRING = "void";
static const char* const ARGUMENTS_TYPE_STRING = "constexpr Arguments";
static const char* const CONTEXT_TYPE_STRING = "Context";
static const char* const OBJECT_TYPE_STRING = "Object";
static const char* const CONST_STRING_TYPE_STRING = "constexpr string";
static const char* const CODE_TYPE_STRING = "Code";
static const char* const INTPTR_TYPE_STRING = "intptr";
static const char* const CONST_INT31_TYPE_STRING = "constexpr int31";
static const char* const CONST_INT32_TYPE_STRING = "constexpr int32";
static const char* const CONST_FLOAT64_TYPE_STRING = "constexpr float64";
class Label;
class Value;
class TypeBase {
public:
enum class Kind {
kAbstractType,
kFunctionPointerType,
kUnionType,
kStructType
};
virtual ~TypeBase() {}
bool IsAbstractType() const { return kind() == Kind::kAbstractType; }
bool IsFunctionPointerType() const {
return kind() == Kind::kFunctionPointerType;
}
bool IsUnionType() const { return kind() == Kind::kUnionType; }
bool IsStructType() const { return kind() == Kind::kStructType; }
protected:
explicit TypeBase(Kind kind) : kind_(kind) {}
Kind kind() const { return kind_; }
private:
const Kind kind_;
};
#define DECLARE_TYPE_BOILERPLATE(x) \
static x* cast(TypeBase* declarable) { \
DCHECK(declarable->Is##x()); \
return static_cast<x*>(declarable); \
} \
static const x* cast(const TypeBase* declarable) { \
DCHECK(declarable->Is##x()); \
return static_cast<const x*>(declarable); \
} \
static x* DynamicCast(TypeBase* declarable) { \
if (!declarable) return nullptr; \
if (!declarable->Is##x()) return nullptr; \
return static_cast<x*>(declarable); \
} \
static const x* DynamicCast(const TypeBase* declarable) { \
if (!declarable) return nullptr; \
if (!declarable->Is##x()) return nullptr; \
return static_cast<const x*>(declarable); \
}
class Type : public TypeBase {
public:
virtual bool IsSubtypeOf(const Type* supertype) const;
std::string ToString() const;
virtual std::string MangledName() const = 0;
bool IsVoid() const { return IsAbstractName(VOID_TYPE_STRING); }
bool IsNever() const { return IsAbstractName(NEVER_TYPE_STRING); }
bool IsBool() const { return IsAbstractName(BOOL_TYPE_STRING); }
bool IsConstexprBool() const {
return IsAbstractName(CONSTEXPR_BOOL_TYPE_STRING);
}
bool IsVoidOrNever() const { return IsVoid() || IsNever(); }
virtual std::string GetGeneratedTypeName() const = 0;
virtual std::string GetGeneratedTNodeTypeName() const = 0;
virtual bool IsConstexpr() const = 0;
virtual const Type* NonConstexprVersion() const = 0;
static const Type* CommonSupertype(const Type* a, const Type* b);
void AddAlias(std::string alias) const { aliases_.insert(std::move(alias)); }
protected:
Type(TypeBase::Kind kind, const Type* parent)
: TypeBase(kind), parent_(parent) {}
const Type* parent() const { return parent_; }
void set_parent(const Type* t) { parent_ = t; }
int Depth() const;
virtual std::string ToExplicitString() const = 0;
private:
bool IsAbstractName(const std::string& name) const;
// If {parent_} is not nullptr, then this type is a subtype of {parent_}.
const Type* parent_;
mutable std::set<std::string> aliases_;
};
using TypeVector = std::vector<const Type*>;
struct NameAndType {
std::string name;
const Type* type;
};
std::ostream& operator<<(std::ostream& os, const NameAndType& name_and_type);
class AbstractType final : public Type {
public:
DECLARE_TYPE_BOILERPLATE(AbstractType);
const std::string& name() const { return name_; }
std::string ToExplicitString() const override { return name(); }
std::string MangledName() const override { return "AT" + name(); }
std::string GetGeneratedTypeName() const override { return generated_type_; }
std::string GetGeneratedTNodeTypeName() const override;
bool IsConstexpr() const override {
return name().substr(0, strlen(CONSTEXPR_TYPE_PREFIX)) ==
CONSTEXPR_TYPE_PREFIX;
}
const Type* NonConstexprVersion() const override {
if (IsConstexpr()) return *non_constexpr_version_;
return this;
}
private:
friend class TypeOracle;
AbstractType(const Type* parent, const std::string& name,
const std::string& generated_type,
base::Optional<const AbstractType*> non_constexpr_version)
: Type(Kind::kAbstractType, parent),
name_(name),
generated_type_(generated_type),
non_constexpr_version_(non_constexpr_version) {
DCHECK_EQ(non_constexpr_version_.has_value(), IsConstexpr());
if (parent) DCHECK(parent->IsConstexpr() == IsConstexpr());
}
const std::string name_;
const std::string generated_type_;
base::Optional<const AbstractType*> non_constexpr_version_;
};
// For now, function pointers are restricted to Code objects of Torque-defined
// builtins.
class FunctionPointerType final : public Type {
public:
DECLARE_TYPE_BOILERPLATE(FunctionPointerType);
std::string ToExplicitString() const override;
std::string MangledName() const override;
std::string GetGeneratedTypeName() const override {
return parent()->GetGeneratedTypeName();
}
std::string GetGeneratedTNodeTypeName() const override {
return parent()->GetGeneratedTNodeTypeName();
}
bool IsConstexpr() const override {
DCHECK(!parent()->IsConstexpr());
return false;
}
const Type* NonConstexprVersion() const override { return this; }
const TypeVector& parameter_types() const { return parameter_types_; }
const Type* return_type() const { return return_type_; }
friend size_t hash_value(const FunctionPointerType& p) {
size_t result = base::hash_value(p.return_type_);
for (const Type* parameter : p.parameter_types_) {
result = base::hash_combine(result, parameter);
}
return result;
}
bool operator==(const FunctionPointerType& other) const {
return parameter_types_ == other.parameter_types_ &&
return_type_ == other.return_type_;
}
private:
friend class TypeOracle;
FunctionPointerType(const Type* parent, TypeVector parameter_types,
const Type* return_type)
: Type(Kind::kFunctionPointerType, parent),
parameter_types_(parameter_types),
return_type_(return_type) {}
const TypeVector parameter_types_;
const Type* const return_type_;
};
bool operator<(const Type& a, const Type& b);
struct TypeLess {
bool operator()(const Type* const a, const Type* const b) const {
return *a < *b;
}
};
class UnionType final : public Type {
public:
DECLARE_TYPE_BOILERPLATE(UnionType);
std::string ToExplicitString() const override;
std::string MangledName() const override;
std::string GetGeneratedTypeName() const override {
return "TNode<" + GetGeneratedTNodeTypeName() + ">";
}
std::string GetGeneratedTNodeTypeName() const override;
bool IsConstexpr() const override {
DCHECK_EQ(false, parent()->IsConstexpr());
return false;
}
const Type* NonConstexprVersion() const override;
friend size_t hash_value(const UnionType& p) {
size_t result = 0;
for (const Type* t : p.types_) {
result = base::hash_combine(result, t);
}
return result;
}
bool operator==(const UnionType& other) const {
return types_ == other.types_;
}
base::Optional<const Type*> GetSingleMember() const {
if (types_.size() == 1) {
DCHECK_EQ(*types_.begin(), parent());
return *types_.begin();
}
return base::nullopt;
}
const Type* Normalize() const {
if (types_.size() == 1) {
return parent();
}
return this;
}
bool IsSubtypeOf(const Type* other) const override {
for (const Type* member : types_) {
if (!member->IsSubtypeOf(other)) return false;
}
return true;
}
bool IsSupertypeOf(const Type* other) const {
for (const Type* member : types_) {
if (other->IsSubtypeOf(member)) {
return true;
}
}
return false;
}
void Extend(const Type* t) {
if (const UnionType* union_type = UnionType::DynamicCast(t)) {
for (const Type* member : union_type->types_) {
Extend(member);
}
} else {
if (t->IsSubtypeOf(this)) return;
set_parent(CommonSupertype(parent(), t));
for (const Type* member : types_) {
if (member->IsSubtypeOf(t)) {
types_.erase(member);
}
}
types_.insert(t);
}
}
static UnionType FromType(const Type* t) {
const UnionType* union_type = UnionType::DynamicCast(t);
return union_type ? UnionType(*union_type) : UnionType(t);
}
private:
explicit UnionType(const Type* t) : Type(Kind::kUnionType, t), types_({t}) {}
std::set<const Type*, TypeLess> types_;
};
class StructType final : public Type {
public:
DECLARE_TYPE_BOILERPLATE(StructType);
std::string ToExplicitString() const override;
std::string MangledName() const override { return name_; }
std::string GetGeneratedTypeName() const override { return GetStructName(); }
std::string GetGeneratedTNodeTypeName() const override { UNREACHABLE(); }
const Type* NonConstexprVersion() const override { return this; }
bool IsConstexpr() const override { return false; }
const std::vector<NameAndType>& fields() const { return fields_; }
const std::string& name() const { return name_; }
Module* module() const { return module_; }
private:
friend class TypeOracle;
StructType(Module* module, const std::string& name,
const std::vector<NameAndType>& fields)
: Type(Kind::kStructType, nullptr),
module_(module),
name_(name),
fields_(fields) {}
const std::string& GetStructName() const { return name_; }
Module* module_;
std::string name_;
std::vector<NameAndType> fields_;
};
inline std::ostream& operator<<(std::ostream& os, const Type& t) {
os << t.ToString();
return os;
}
class VisitResult {
public:
VisitResult() {}
VisitResult(const Type* type, const std::string& value)
: type_(type), value_(value), declarable_{} {}
VisitResult(const Type* type, const Value* declarable);
const Type* type() const { return type_; }
// const std::string& variable() const { return variable_; }
base::Optional<const Value*> declarable() const { return declarable_; }
std::string LValue() const;
std::string RValue() const;
void SetType(const Type* new_type) { type_ = new_type; }
private:
const Type* type_;
std::string value_;
base::Optional<const Value*> declarable_;
};
class VisitResultVector : public std::vector<VisitResult> {
public:
VisitResultVector() : std::vector<VisitResult>() {}
VisitResultVector(std::initializer_list<VisitResult> init)
: std::vector<VisitResult>(init) {}
TypeVector GetTypeVector() const {
TypeVector result;
for (auto& visit_result : *this) {
result.push_back(visit_result.type());
}
return result;
}
};
std::ostream& operator<<(std::ostream& os, const TypeVector& types);
typedef std::vector<NameAndType> NameAndTypeVector;
struct LabelDefinition {
std::string name;
NameAndTypeVector parameters;
};
typedef std::vector<LabelDefinition> LabelDefinitionVector;
struct LabelDeclaration {
std::string name;
TypeVector types;
};
typedef std::vector<LabelDeclaration> LabelDeclarationVector;
struct ParameterTypes {
TypeVector types;
bool var_args;
};
std::ostream& operator<<(std::ostream& os, const ParameterTypes& parameters);
struct Signature {
const TypeVector& types() const { return parameter_types.types; }
NameVector parameter_names;
ParameterTypes parameter_types;
const Type* return_type;
LabelDeclarationVector labels;
bool HasSameTypesAs(const Signature& other) const;
};
struct Arguments {
VisitResultVector parameters;
std::vector<Label*> labels;
};
void PrintSignature(std::ostream& os, const Signature& sig, bool with_names);
std::ostream& operator<<(std::ostream& os, const Signature& sig);
bool IsAssignableFrom(const Type* to, const Type* from);
bool IsCompatibleSignature(const Signature& sig, const TypeVector& types,
const std::vector<Label*>& labels);
} // namespace torque
} // namespace internal
} // namespace v8
#endif // V8_TORQUE_TYPES_H_
|