5 #ifndef V8_TORQUE_TYPES_H_ 6 #define V8_TORQUE_TYPES_H_ 14 #include "src/base/optional.h" 15 #include "src/torque/utils.h" 21 static const char*
const CONSTEXPR_TYPE_PREFIX =
"constexpr ";
22 static const char*
const NEVER_TYPE_STRING =
"never";
23 static const char*
const CONSTEXPR_BOOL_TYPE_STRING =
"constexpr bool";
24 static const char*
const BOOL_TYPE_STRING =
"bool";
25 static const char*
const VOID_TYPE_STRING =
"void";
26 static const char*
const ARGUMENTS_TYPE_STRING =
"constexpr Arguments";
27 static const char*
const CONTEXT_TYPE_STRING =
"Context";
28 static const char*
const OBJECT_TYPE_STRING =
"Object";
29 static const char*
const CONST_STRING_TYPE_STRING =
"constexpr string";
30 static const char*
const CODE_TYPE_STRING =
"Code";
31 static const char*
const INTPTR_TYPE_STRING =
"intptr";
32 static const char*
const CONST_INT31_TYPE_STRING =
"constexpr int31";
33 static const char*
const CONST_INT32_TYPE_STRING =
"constexpr int32";
34 static const char*
const CONST_FLOAT64_TYPE_STRING =
"constexpr float64";
49 bool IsTopType()
const {
return kind() == Kind::kTopType; }
50 bool IsAbstractType()
const {
return kind() == Kind::kAbstractType; }
51 bool IsFunctionPointerType()
const {
52 return kind() == Kind::kFunctionPointerType;
54 bool IsUnionType()
const {
return kind() == Kind::kUnionType; }
55 bool IsStructType()
const {
return kind() == Kind::kStructType; }
58 explicit TypeBase(Kind kind) : kind_(kind) {}
59 Kind kind()
const {
return kind_; }
65 #define DECLARE_TYPE_BOILERPLATE(x) \ 66 static x* cast(TypeBase* declarable) { \ 67 DCHECK(declarable->Is##x()); \ 68 return static_cast<x*>(declarable); \ 70 static const x* cast(const TypeBase* declarable) { \ 71 DCHECK(declarable->Is##x()); \ 72 return static_cast<const x*>(declarable); \ 74 static x* DynamicCast(TypeBase* declarable) { \ 75 if (!declarable) return nullptr; \ 76 if (!declarable->Is##x()) return nullptr; \ 77 return static_cast<x*>(declarable); \ 79 static const x* DynamicCast(const TypeBase* declarable) { \ 80 if (!declarable) return nullptr; \ 81 if (!declarable->Is##x()) return nullptr; \ 82 return static_cast<const x*>(declarable); \ 87 virtual bool IsSubtypeOf(
const Type* supertype)
const;
90 virtual std::string MangledName()
const = 0;
91 bool IsVoid()
const {
return IsAbstractName(VOID_TYPE_STRING); }
92 bool IsNever()
const {
return IsAbstractName(NEVER_TYPE_STRING); }
93 bool IsBool()
const {
return IsAbstractName(BOOL_TYPE_STRING); }
94 bool IsConstexprBool()
const {
95 return IsAbstractName(CONSTEXPR_BOOL_TYPE_STRING);
97 bool IsVoidOrNever()
const {
return IsVoid() || IsNever(); }
98 virtual std::string GetGeneratedTypeName()
const = 0;
99 virtual std::string GetGeneratedTNodeTypeName()
const = 0;
100 virtual bool IsConstexpr()
const = 0;
101 virtual bool IsTransient()
const {
return false; }
102 virtual const Type* NonConstexprVersion()
const = 0;
103 static const Type* CommonSupertype(
const Type* a,
const Type* b);
104 void AddAlias(std::string alias)
const { aliases_.insert(std::move(alias)); }
107 Type(TypeBase::Kind kind,
const Type* parent)
108 :
TypeBase(kind), parent_(parent) {}
109 const Type* parent()
const {
return parent_; }
110 void set_parent(
const Type* t) { parent_ = t; }
112 virtual std::string ToExplicitString()
const = 0;
115 bool IsAbstractName(
const std::string& name)
const;
119 mutable std::set<std::string> aliases_;
122 using TypeVector = std::vector<const Type*>;
124 inline size_t hash_value(
const TypeVector& types) {
126 for (
const Type* t : types) {
127 hash = base::hash_combine(hash, t);
137 std::ostream& operator<<(std::ostream& os,
const NameAndType& name_and_type);
141 DECLARE_TYPE_BOILERPLATE(
TopType);
142 virtual std::string MangledName()
const {
return "top"; }
143 virtual std::string GetGeneratedTypeName()
const { UNREACHABLE(); }
144 virtual std::string GetGeneratedTNodeTypeName()
const {
145 return source_type_->GetGeneratedTNodeTypeName();
147 virtual bool IsConstexpr()
const {
return false; }
148 virtual const Type* NonConstexprVersion()
const {
return nullptr; }
149 virtual std::string ToExplicitString()
const {
151 s <<
"inaccessible " + source_type_->ToString();
155 const Type* source_type()
const {
return source_type_; }
156 const std::string reason()
const {
return reason_; }
160 explicit TopType(std::string reason,
const Type* source_type)
161 :
Type(Kind::kTopType,
nullptr),
162 reason_(std::move(reason)),
163 source_type_(source_type) {}
165 const Type* source_type_;
171 const std::string& name()
const {
return name_; }
172 std::string ToExplicitString()
const override {
return name(); }
173 std::string MangledName()
const override {
return "AT" + name(); }
174 std::string GetGeneratedTypeName()
const override {
175 return IsConstexpr() ? generated_type_
176 :
"compiler::TNode<" + generated_type_ +
">";
178 std::string GetGeneratedTNodeTypeName()
const override;
179 bool IsConstexpr()
const override {
180 return name().substr(0, strlen(CONSTEXPR_TYPE_PREFIX)) ==
181 CONSTEXPR_TYPE_PREFIX;
183 const Type* NonConstexprVersion()
const override {
184 if (IsConstexpr())
return *non_constexpr_version_;
191 const std::string& generated_type,
193 :
Type(Kind::kAbstractType, parent),
194 transient_(
transient),
196 generated_type_(generated_type),
197 non_constexpr_version_(non_constexpr_version) {
198 DCHECK_EQ(non_constexpr_version_.has_value(), IsConstexpr());
199 if (parent) DCHECK(parent->IsConstexpr() == IsConstexpr());
202 bool IsTransient()
const override {
return transient_; }
205 const std::string name_;
206 const std::string generated_type_;
215 std::string ToExplicitString()
const override;
216 std::string MangledName()
const override;
217 std::string GetGeneratedTypeName()
const override {
218 return parent()->GetGeneratedTypeName();
220 std::string GetGeneratedTNodeTypeName()
const override {
221 return parent()->GetGeneratedTNodeTypeName();
223 bool IsConstexpr()
const override {
224 DCHECK(!parent()->IsConstexpr());
227 const Type* NonConstexprVersion()
const override {
return this; }
229 const TypeVector& parameter_types()
const {
return parameter_types_; }
230 const Type* return_type()
const {
return return_type_; }
233 size_t result = base::hash_value(p.return_type_);
234 for (
const Type* parameter : p.parameter_types_) {
235 result = base::hash_combine(result, parameter);
240 return parameter_types_ == other.parameter_types_ &&
241 return_type_ == other.return_type_;
243 size_t function_pointer_type_id()
const {
return function_pointer_type_id_; }
248 const Type* return_type,
size_t function_pointer_type_id)
249 :
Type(Kind::kFunctionPointerType, parent),
250 parameter_types_(parameter_types),
251 return_type_(return_type),
252 function_pointer_type_id_(function_pointer_type_id) {}
254 const TypeVector parameter_types_;
255 const Type*
const return_type_;
256 const size_t function_pointer_type_id_;
259 bool operator<(
const Type& a,
const Type& b);
261 bool operator()(
const Type*
const a,
const Type*
const b)
const {
269 std::string ToExplicitString()
const override;
270 std::string MangledName()
const override;
271 std::string GetGeneratedTypeName()
const override {
272 return "compiler::TNode<" + GetGeneratedTNodeTypeName() +
">";
274 std::string GetGeneratedTNodeTypeName()
const override;
276 bool IsConstexpr()
const override {
277 DCHECK_EQ(
false, parent()->IsConstexpr());
280 const Type* NonConstexprVersion()
const override;
282 friend size_t hash_value(
const UnionType& p) {
284 for (
const Type* t : p.types_) {
285 result = base::hash_combine(result, t);
289 bool operator==(
const UnionType& other)
const {
290 return types_ == other.types_;
294 if (types_.size() == 1) {
295 DCHECK_EQ(*types_.begin(), parent());
296 return *types_.begin();
298 return base::nullopt;
301 bool IsSubtypeOf(
const Type* other)
const override {
302 for (
const Type* member : types_) {
303 if (!member->IsSubtypeOf(other))
return false;
308 bool IsSupertypeOf(
const Type* other)
const {
309 for (
const Type* member : types_) {
310 if (other->IsSubtypeOf(member)) {
317 bool IsTransient()
const override {
318 for (
const Type* member : types_) {
319 if (member->IsTransient()) {
326 void Extend(
const Type* t) {
327 if (
const UnionType* union_type = UnionType::DynamicCast(t)) {
328 for (
const Type* member : union_type->types_) {
332 if (t->IsSubtypeOf(
this))
return;
333 set_parent(CommonSupertype(parent(), t));
334 for (
const Type* member : types_) {
335 if (member->IsSubtypeOf(t)) {
336 types_.erase(member);
343 void Subtract(
const Type* t);
346 const UnionType* union_type = UnionType::DynamicCast(t);
352 void RecomputeParent();
354 std::set<const Type*, TypeLess> types_;
362 std::string ToExplicitString()
const override;
363 std::string MangledName()
const override {
return name_; }
364 std::string GetGeneratedTypeName()
const override;
365 std::string GetGeneratedTNodeTypeName()
const override { UNREACHABLE(); }
366 const Type* NonConstexprVersion()
const override {
return this; }
368 bool IsConstexpr()
const override {
return false; }
370 const std::vector<NameAndType>& fields()
const {
return fields_; }
371 const Type* GetFieldType(
const std::string& fieldname)
const {
373 if (field.name == fieldname)
return field.type;
376 s <<
"\"" << fieldname <<
"\" is not a field of struct type \"" << name()
378 ReportError(s.str());
380 const std::string& name()
const {
return name_; }
381 Namespace* nspace()
const {
return namespace_; }
386 const std::vector<NameAndType>& fields)
387 :
Type(Kind::kStructType,
nullptr),
392 const std::string& GetStructName()
const {
return name_; }
396 std::vector<NameAndType> fields_;
399 inline std::ostream& operator<<(std::ostream& os,
const Type& t) {
408 : type_(
type), constexpr_value_(constexpr_value) {
409 DCHECK(
type->IsConstexpr());
413 : type_(
type), stack_range_(stack_range) {
414 DCHECK(!
type->IsConstexpr());
416 const Type*
type()
const {
return type_; }
417 const std::string& constexpr_value()
const {
return *constexpr_value_; }
418 const StackRange& stack_range()
const {
return *stack_range_; }
419 void SetType(
const Type* new_type) { type_ = new_type; }
420 bool IsOnStack()
const {
return stack_range_ != base::nullopt; }
422 return type_ == other.type_ && constexpr_value_ == other.constexpr_value_ &&
423 stack_range_ == other.stack_range_;
427 const Type* type_ =
nullptr;
433 const std::string& fieldname);
439 : std::vector<VisitResult>(init) {}
440 TypeVector GetTypeVector()
const {
442 for (
auto& visit_result : *
this) {
443 result.push_back(visit_result.type());
449 std::ostream& operator<<(std::ostream& os,
const TypeVector& types);
451 typedef std::vector<NameAndType> NameAndTypeVector;
455 NameAndTypeVector parameters;
458 typedef std::vector<LabelDefinition> LabelDefinitionVector;
465 typedef std::vector<LabelDeclaration> LabelDeclarationVector;
472 std::ostream& operator<<(std::ostream& os,
const ParameterTypes& parameters);
474 enum class ParameterMode { kProcessImplicit, kIgnoreImplicit };
479 : parameter_names(std::move(n)),
480 arguments_variable(arguments_variable),
481 parameter_types(std::move(p)),
484 labels(std::move(l)) {}
485 Signature() : implicit_count(0), return_type(
nullptr) {}
486 const TypeVector& types()
const {
return parameter_types.types; }
487 NameVector parameter_names;
490 size_t implicit_count;
491 const Type* return_type;
492 LabelDeclarationVector labels;
495 ParameterMode mode = ParameterMode::kProcessImplicit)
const;
496 const TypeVector& GetTypes()
const {
return parameter_types.types; }
497 TypeVector GetImplicitTypes()
const {
498 return TypeVector(parameter_types.types.begin(),
499 parameter_types.types.begin() + implicit_count);
501 TypeVector GetExplicitTypes()
const {
502 return TypeVector(parameter_types.types.begin() + implicit_count,
503 parameter_types.types.end());
507 void PrintSignature(std::ostream& os,
const Signature& sig,
bool with_names);
508 std::ostream& operator<<(std::ostream& os,
const Signature& sig);
510 bool IsAssignableFrom(
const Type* to,
const Type* from);
512 TypeVector LowerType(
const Type*
type);
513 size_t LoweredSlotCount(
const Type*
type);
514 TypeVector LowerParameterTypes(
const TypeVector& parameters);
515 TypeVector LowerParameterTypes(
const ParameterTypes& parameter_types,
516 size_t vararg_count = 0);
522 #endif // V8_TORQUE_TYPES_H_