v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
types.h
Go to the documentation of this file.
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_TYPES_H_
6#define V8_TORQUE_TYPES_H_
7
8#include <algorithm>
9#include <optional>
10#include <set>
11#include <string>
12#include <vector>
13
14#include "src/torque/ast.h"
17#include "src/torque/utils.h"
18
19namespace v8::internal::torque {
20
21class AggregateType;
22struct Identifier;
23class Macro;
24class Method;
25class GenericType;
26class StructType;
27class Type;
28class ClassType;
29class Value;
30class Namespace;
31
32class TypeBase {
33 public:
43 virtual ~TypeBase() = default;
44 bool IsTopType() const { return kind() == Kind::kTopType; }
45 bool IsAbstractType() const { return kind() == Kind::kAbstractType; }
46 bool IsBuiltinPointerType() const {
48 }
49 bool IsUnionType() const { return kind() == Kind::kUnionType; }
50 bool IsBitFieldStructType() const {
52 }
53 bool IsStructType() const { return kind() == Kind::kStructType; }
54 bool IsClassType() const { return kind() == Kind::kClassType; }
55 bool IsAggregateType() const { return IsStructType() || IsClassType(); }
56
57 protected:
58 explicit TypeBase(Kind kind) : kind_(kind) {}
59 Kind kind() const { return kind_; }
60
61 private:
62 const Kind kind_;
63};
64
65#define DECLARE_TYPE_BOILERPLATE(x) \
66 static x* cast(TypeBase* declarable) { \
67 DCHECK(declarable->Is##x()); \
68 return static_cast<x*>(declarable); \
69 } \
70 static const x* cast(const TypeBase* declarable) { \
71 DCHECK(declarable->Is##x()); \
72 return static_cast<const x*>(declarable); \
73 } \
74 static x* DynamicCast(TypeBase* declarable) { \
75 if (!declarable) return nullptr; \
76 if (!declarable->Is##x()) return nullptr; \
77 return static_cast<x*>(declarable); \
78 } \
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); \
83 }
84
85using TypeVector = std::vector<const Type*>;
86
87template <typename T>
92
93using MaybeSpecializationKey = std::optional<SpecializationKey<GenericType>>;
94
96 // The type of the object. This string is not guaranteed to correspond to a
97 // C++ class, but just to a type checker function: for any type "Foo" here,
98 // the function IsFoo must exist.
99 std::string type;
100 // If {type} is "MaybeObject", then {weak_ref_to} indicates the corresponding
101 // strong object type. Otherwise, {weak_ref_to} is empty.
102 std::string weak_ref_to;
103};
104
106 public:
107 Type& operator=(const Type& other) = delete;
108 virtual bool IsSubtypeOf(const Type* supertype) const;
109
110 // Default rendering for error messages etc.
111 std::string ToString() const;
112
113 // This name is not unique, but short and somewhat descriptive.
114 // Used for naming generated code.
115 virtual std::string SimpleName() const;
116
117 enum class HandleKind { kIndirect, kDirect };
118 std::string GetHandleTypeName(HandleKind kind,
119 const std::string& type_name) const;
120
121 std::string TagglifiedCppTypeName() const;
122 std::string HandlifiedCppTypeName(HandleKind kind) const;
123
124 const Type* parent() const { return parent_; }
125 bool IsVoid() const { return IsAbstractName(VOID_TYPE_STRING); }
126 bool IsNever() const { return IsAbstractName(NEVER_TYPE_STRING); }
127 bool IsBool() const { return IsAbstractName(BOOL_TYPE_STRING); }
128 bool IsConstexprBool() const {
129 return IsAbstractName(CONSTEXPR_BOOL_TYPE_STRING);
130 }
131 bool IsVoidOrNever() const { return IsVoid() || IsNever(); }
132 bool IsFloat32() const { return IsAbstractName(FLOAT32_TYPE_STRING); }
133 bool IsFloat64() const { return IsAbstractName(FLOAT64_TYPE_STRING); }
134 std::string GetGeneratedTypeName() const;
135 std::string GetGeneratedTNodeTypeName() const;
136 virtual bool IsConstexpr() const {
137 if (parent()) DCHECK(!parent()->IsConstexpr());
138 return false;
139 }
140 virtual bool IsTransient() const { return false; }
141 virtual const Type* NonConstexprVersion() const { return this; }
142 virtual std::string GetConstexprGeneratedTypeName() const;
143 std::optional<const ClassType*> ClassSupertype() const;
144 std::optional<const StructType*> StructSupertype() const;
145 std::optional<const AggregateType*> AggregateSupertype() const;
146 virtual std::vector<TypeChecker> GetTypeCheckers() const { return {}; }
147 virtual std::string GetRuntimeType() const;
148 virtual std::string GetDebugType() const;
149 static const Type* CommonSupertype(const Type* a, const Type* b);
150 void AddAlias(std::string alias) const { aliases_.insert(std::move(alias)); }
151 size_t id() const { return id_; }
153 return specialized_from_;
154 }
155
156 static std::optional<const Type*> MatchUnaryGeneric(const Type* type,
157 GenericType* generic);
158
159 static std::string ComputeName(const std::string& basename,
160 MaybeSpecializationKey specialized_from);
161 virtual void SetConstexprVersion(const Type* type) const {
162 constexpr_version_ = type;
163 }
164
165 virtual const Type* ConstexprVersion() const {
166 if (constexpr_version_) return constexpr_version_;
167 if (IsConstexpr()) return this;
168 if (parent()) return parent()->ConstexprVersion();
169 return nullptr;
170 }
171
172 virtual size_t AlignmentLog2() const;
173
174 protected:
175 Type(TypeBase::Kind kind, const Type* parent,
176 MaybeSpecializationKey specialized_from = std::nullopt);
177 Type(const Type& other) V8_NOEXCEPT;
178 void set_parent(const Type* t) { parent_ = t; }
179 int Depth() const;
180 virtual std::string ToExplicitString() const = 0;
181 virtual std::string GetGeneratedTypeNameImpl() const = 0;
182 virtual std::string GetGeneratedTNodeTypeNameImpl() const = 0;
183 virtual std::string SimpleNameImpl() const = 0;
184
185 private:
186 bool IsAbstractName(const std::string& name) const;
187
188 // If {parent_} is not nullptr, then this type is a subtype of {parent_}.
189 const Type* parent_;
190 mutable std::set<std::string> aliases_;
191 size_t id_;
193 mutable const Type* constexpr_version_ = nullptr;
194};
195
196inline size_t hash_value(const TypeVector& types) {
197 size_t hash = 0;
198 for (const Type* t : types) {
199 hash = base::hash_combine(hash, t);
200 }
201 return hash;
202}
203
205 std::string name;
206 const Type* type;
207};
208
209std::ostream& operator<<(std::ostream& os, const NameAndType& name_and_type);
210
211struct Field {
212 // TODO(danno): This likely should be refactored, the handling of the types
213 // using the universal grab-bag utility with std::tie, as well as the
214 // reliance of string types is quite clunky.
215 std::tuple<size_t, std::string> GetFieldSizeInformation() const;
216
217 void ValidateAlignment(ResidueClass at_offset) const;
218
221 std::optional<ClassFieldIndexInfo> index;
223
224 // The byte offset of this field from the beginning of the containing class or
225 // struct. Most structs are never packed together in memory, and are only used
226 // to hold a batch of related CSA TNode values, in which case |offset| is
227 // irrelevant.
228 // The offset may be unknown because the field is after an indexed field or
229 // because we don't support the struct field for on-heap layouts.
230 std::optional<size_t> offset;
231
235};
236
237std::ostream& operator<<(std::ostream& os, const Field& name_and_type);
238
239class TopType final : public Type {
240 public:
242 std::string GetGeneratedTypeNameImpl() const override { UNREACHABLE(); }
243 std::string GetGeneratedTNodeTypeNameImpl() const override {
244 return source_type_->GetGeneratedTNodeTypeName();
245 }
246 std::string ToExplicitString() const override {
247 std::stringstream s;
248 s << "inaccessible " + source_type_->ToString();
249 return s.str();
250 }
251
252 const Type* source_type() const { return source_type_; }
253 const std::string reason() const { return reason_; }
254
255 private:
256 friend class TypeOracle;
257 explicit TopType(std::string reason, const Type* source_type)
258 : Type(Kind::kTopType, nullptr),
259 reason_(std::move(reason)),
261 std::string SimpleNameImpl() const override { return "TopType"; }
262
263 std::string reason_;
265};
266
267class AbstractType final : public Type {
268 public:
270 const std::string& name() const { return name_; }
271 std::string ToExplicitString() const override { return name(); }
272 std::string GetGeneratedTypeNameImpl() const override;
273 std::string GetGeneratedTNodeTypeNameImpl() const override;
274 bool IsConstexpr() const final {
275 const bool is_constexpr = flags_ & AbstractTypeFlag::kConstexpr;
276 DCHECK_IMPLIES(non_constexpr_version_ != nullptr, is_constexpr);
277 return is_constexpr;
278 }
279
280 const Type* NonConstexprVersion() const override {
282 if (!IsConstexpr()) return this;
283 if (parent()) return parent()->NonConstexprVersion();
284 return nullptr;
285 }
286
287 std::vector<TypeChecker> GetTypeCheckers() const override;
288
289 size_t AlignmentLog2() const override;
290
291 private:
292 friend class TypeOracle;
294 const std::string& name, const std::string& generated_type,
295 const Type* non_constexpr_version,
296 MaybeSpecializationKey specialized_from)
297 : Type(Kind::kAbstractType, parent, specialized_from),
298 flags_(flags),
299 name_(name),
300 generated_type_(generated_type),
301 non_constexpr_version_(non_constexpr_version) {
302 if (parent) DCHECK_EQ(parent->IsConstexpr(), IsConstexpr());
306 }
307
308 std::string SimpleNameImpl() const override {
309 if (IsConstexpr()) {
310 const Type* non_constexpr_version = NonConstexprVersion();
311 if (non_constexpr_version == nullptr) {
312 ReportError("Cannot find non-constexpr type corresponding to ", *this);
313 }
314 return "constexpr_" + non_constexpr_version->SimpleName();
315 }
316 return name();
317 }
318
319 bool IsTransient() const override {
321 }
322
326
328 const std::string name_;
329 const std::string generated_type_;
331};
332
333// For now, builtin pointers are restricted to Torque-defined builtins.
335 public:
337 std::string ToExplicitString() const override;
338 std::string GetGeneratedTypeNameImpl() const override {
339 return parent()->GetGeneratedTypeName();
340 }
341 std::string GetGeneratedTNodeTypeNameImpl() const override {
342 return parent()->GetGeneratedTNodeTypeName();
343 }
344
345 const TypeVector& parameter_types() const { return parameter_types_; }
346 const Type* return_type() const { return return_type_; }
347
348 friend size_t hash_value(const BuiltinPointerType& p) {
349 size_t result = base::hash_value(p.return_type_);
350 for (const Type* parameter : p.parameter_types_) {
351 result = base::hash_combine(result, parameter);
352 }
353 return result;
354 }
355 bool operator==(const BuiltinPointerType& other) const {
356 return parameter_types_ == other.parameter_types_ &&
357 return_type_ == other.return_type_;
358 }
359 size_t function_pointer_type_id() const { return function_pointer_type_id_; }
360
361 std::vector<TypeChecker> GetTypeCheckers() const override {
362 return {{"Smi", ""}};
363 }
364
365 bool HasContextParameter() const;
366
367 private:
368 friend class TypeOracle;
369 BuiltinPointerType(const Type* parent, TypeVector parameter_types,
370 const Type* return_type, size_t function_pointer_type_id)
371 : Type(Kind::kBuiltinPointerType, parent),
372 parameter_types_(parameter_types),
373 return_type_(return_type),
374 function_pointer_type_id_(function_pointer_type_id) {}
375 std::string SimpleNameImpl() const override;
376
378 const Type* const return_type_;
380};
381
382bool operator<(const Type& a, const Type& b);
383struct TypeLess {
384 bool operator()(const Type* const a, const Type* const b) const {
385 return *a < *b;
386 }
387};
388
389class V8_EXPORT_PRIVATE UnionType final : public Type {
390 public:
392 std::string GetGeneratedTypeNameImpl() const override {
393 return "TNode<" + GetGeneratedTNodeTypeName() + ">";
394 }
395 std::string GetGeneratedTNodeTypeNameImpl() const override;
396 std::string GetRuntimeType() const override;
397 std::string GetDebugType() const override;
398 std::string GetConstexprGeneratedTypeName() const override;
399
400 friend size_t hash_value(const UnionType& p) {
401 size_t result = 0;
402 for (const Type* t : p.types_) {
403 result = base::hash_combine(result, t);
404 }
405 return result;
406 }
407 bool operator==(const UnionType& other) const {
408 return types_ == other.types_;
409 }
410
411 std::optional<const Type*> GetSingleMember() const {
412 if (types_.size() == 1) {
413 DCHECK_EQ(*types_.begin(), parent());
414 return *types_.begin();
415 }
416 return std::nullopt;
417 }
418
419 bool IsSubtypeOf(const Type* other) const override {
420 for (const Type* member : types_) {
421 if (!member->IsSubtypeOf(other)) return false;
422 }
423 return true;
424 }
425
426 bool IsSupertypeOf(const Type* other) const {
427 for (const Type* member : types_) {
428 if (other->IsSubtypeOf(member)) {
429 return true;
430 }
431 }
432 return false;
433 }
434
435 bool IsTransient() const override {
436 for (const Type* member : types_) {
437 if (member->IsTransient()) {
438 return true;
439 }
440 }
441 return false;
442 }
443
444 bool IsConstexpr() const override { return parent()->IsConstexpr(); }
445
446 const Type* NonConstexprVersion() const override {
447 if (!IsConstexpr()) return this;
448 return parent()->NonConstexprVersion();
449 }
450
451 void Extend(const Type* t) {
452 if (const UnionType* union_type = UnionType::DynamicCast(t)) {
453 for (const Type* member : union_type->types_) {
454 Extend(member);
455 }
456 } else {
457 if (t->IsSubtypeOf(this)) return;
458 set_parent(CommonSupertype(parent(), t));
459 EraseIf(&types_,
460 [&](const Type* member) { return member->IsSubtypeOf(t); });
461 types_.insert(t);
462 }
463 }
464 std::string ToExplicitString() const override;
465
466 void Subtract(const Type* t);
467
468 static UnionType FromType(const Type* t) {
469 const UnionType* union_type = UnionType::DynamicCast(t);
470 return union_type ? UnionType(*union_type) : UnionType(t);
471 }
472
473 std::vector<TypeChecker> GetTypeCheckers() const override {
474 std::vector<TypeChecker> result;
475 for (const Type* member : types_) {
476 std::vector<TypeChecker> sub_result = member->GetTypeCheckers();
477 result.insert(result.end(), sub_result.begin(), sub_result.end());
478 }
479 return result;
480 }
481
482 private:
483 explicit UnionType(const Type* t) : Type(Kind::kUnionType, t), types_({t}) {}
484 void RecomputeParent();
485 std::string SimpleNameImpl() const override;
486
487 static void InsertGeneratedTNodeTypeName(std::set<std::string>& names,
488 const Type* t);
489 static void InsertConstexprGeneratedTypeName(std::set<std::string>& names,
490 const Type* t);
491
492 std::set<const Type*, TypeLess> types_;
493};
494
495const Type* SubtractType(const Type* a, const Type* b);
496
503
505 public:
507 std::string ToExplicitString() const override;
508 std::string GetGeneratedTypeNameImpl() const override {
509 return parent()->GetGeneratedTypeName();
510 }
511 std::string GetGeneratedTNodeTypeNameImpl() const override {
512 return parent()->GetGeneratedTNodeTypeName();
513 }
514
515 std::vector<TypeChecker> GetTypeCheckers() const override {
516 return parent()->GetTypeCheckers();
517 }
518
519 void SetConstexprVersion(const Type*) const override { UNREACHABLE(); }
520 const Type* ConstexprVersion() const override {
521 return parent()->ConstexprVersion();
522 }
523
524 void RegisterField(BitField field) { fields_.push_back(std::move(field)); }
525
526 const std::string& name() const { return decl_->name->value; }
527 const std::vector<BitField>& fields() const { return fields_; }
528
529 const BitField& LookupField(const std::string& name) const;
530
531 const SourcePosition GetPosition() const { return decl_->pos; }
532
533 private:
534 friend class TypeOracle;
535 BitFieldStructType(Namespace* nspace, const Type* parent,
536 const BitFieldStructDeclaration* decl)
537 : Type(Kind::kBitFieldStructType, parent),
538 namespace_(nspace),
539 decl_(decl) {}
540 std::string SimpleNameImpl() const override { return name(); }
541
544 std::vector<BitField> fields_;
545};
546
547class AggregateType : public Type {
548 public:
550 std::string GetGeneratedTypeNameImpl() const override { UNREACHABLE(); }
551 std::string GetGeneratedTNodeTypeNameImpl() const override { UNREACHABLE(); }
552
553 virtual void Finalize() const = 0;
554
555 void SetFields(std::vector<Field> fields) { fields_ = std::move(fields); }
556 const std::vector<Field>& fields() const {
557 if (!is_finalized_) Finalize();
558 return fields_;
559 }
560 bool HasField(const std::string& name) const;
561 const Field& LookupField(const std::string& name) const;
562 const std::string& name() const { return name_; }
563 Namespace* nspace() const { return namespace_; }
564
565 virtual const Field& RegisterField(Field field) {
566 fields_.push_back(field);
567 return fields_.back();
568 }
569
571 const std::vector<Method*>& Methods() const {
572 if (!is_finalized_) Finalize();
573 return methods_;
574 }
575 std::vector<Method*> Methods(const std::string& name) const;
576
577 std::vector<const AggregateType*> GetHierarchy() const;
578 std::vector<TypeChecker> GetTypeCheckers() const override {
579 return {{name_, ""}};
580 }
581
582 const Field& LastField() const {
583 for (std::optional<const AggregateType*> current = this;
584 current.has_value();
585 current = (*current)->parent()->AggregateSupertype()) {
586 const std::vector<Field>& fields = (*current)->fields_;
587 if (!fields.empty()) return fields[fields.size() - 1];
588 }
589 ReportError("Can't get last field of empty aggregate type");
590 }
591
592 protected:
594 const std::string& name,
595 MaybeSpecializationKey specialized_from = std::nullopt)
596 : Type(kind, parent, specialized_from),
597 is_finalized_(false),
599 name_(name) {}
600
601 void CheckForDuplicateFields() const;
602 // Use this lookup if you do not want to trigger finalization on this type.
603 const Field& LookupFieldInternal(const std::string& name) const;
604 std::string SimpleNameImpl() const override { return name_; }
605
606 protected:
607 mutable bool is_finalized_;
608 std::vector<Field> fields_;
609
610 private:
612 std::string name_;
613 std::vector<Method*> methods_;
614};
615
616class StructType final : public AggregateType {
617 public:
619
620 std::string GetGeneratedTypeNameImpl() const override;
621
622 // Returns the sum of the size of all members.
623 size_t PackedSize() const;
624
625 size_t AlignmentLog2() const override;
626
628 kEmpty = 0,
629 kStrongTagged = 1 << 0,
630 kWeakTagged = 1 << 1,
631 kUntagged = 1 << 2,
632 };
634
635 // Classifies a struct as containing tagged data, untagged data, or both.
637
638 SourcePosition GetPosition() const { return decl_->pos; }
639
640 private:
641 friend class TypeOracle;
643 MaybeSpecializationKey specialized_from = std::nullopt);
644
645 void Finalize() const override;
646 std::string ToExplicitString() const override;
647 std::string SimpleNameImpl() const override;
648
651};
652
653class TypeAlias;
654
655enum class ObjectSlotKind : uint8_t {
660};
661
662inline std::optional<ObjectSlotKind> Combine(ObjectSlotKind a,
663 ObjectSlotKind b) {
664 if (a == b) return {a};
665 if (std::min(a, b) == ObjectSlotKind::kStrongPointer &&
666 std::max(a, b) == ObjectSlotKind::kMaybeObjectPointer) {
668 }
669 return std::nullopt;
670}
671
672class ClassType final : public AggregateType {
673 public:
675 std::string ToExplicitString() const override;
676 std::string GetGeneratedTypeNameImpl() const override;
677 std::string GetGeneratedTNodeTypeNameImpl() const override;
678 bool IsExtern() const { return flags_ & ClassFlag::kExtern; }
679 bool ShouldGeneratePrint() const {
680 if (flags_ & ClassFlag::kCppObjectDefinition) return false;
682 if (!IsExtern()) return true;
683 if (!ShouldGenerateCppClassDefinitions()) return false;
684 return !IsAbstract() && !HasUndefinedLayout();
685 }
686 bool ShouldGenerateVerify() const {
687 if (flags_ & ClassFlag::kCppObjectDefinition) return false;
689 if (!IsExtern()) return true;
690 if (!ShouldGenerateCppClassDefinitions()) return false;
691 return !HasUndefinedLayout() && !IsShape();
692 }
694 if (flags_ & ClassFlag::kCppObjectDefinition) return false;
697 return !IsAbstract() && !IsExtern();
698 }
699 bool DoNotGenerateCast() const {
701 }
702 bool IsTransient() const override { return flags_ & ClassFlag::kTransient; }
703 bool IsAbstract() const { return flags_ & ClassFlag::kAbstract; }
722 bool ShouldGenerateFullClassDefinition() const { return !IsExtern(); }
725 (!IsExtern() && !IsAbstract());
726 }
731 bool ShouldExport() const { return flags_ & ClassFlag::kExport; }
732 bool IsShape() const { return flags_ & ClassFlag::kIsShape; }
733 bool HasStaticSize() const;
734 size_t header_size() const {
735 if (!is_finalized_) Finalize();
736 return header_size_;
737 }
739 if (!is_finalized_) Finalize();
740 return size_;
741 }
742 const ClassType* GetSuperClass() const {
743 if (parent() == nullptr) return nullptr;
744 return parent()->IsClassType() ? ClassType::DynamicCast(parent()) : nullptr;
745 }
746 void GenerateAccessors();
747 bool AllowInstantiation() const;
748 const Field& RegisterField(Field field) override {
749 return AggregateType::RegisterField(field);
750 }
751 void Finalize() const override;
752
753 std::vector<Field> ComputeAllFields() const;
754 std::vector<Field> ComputeHeaderFields() const;
755 std::vector<Field> ComputeArrayFields() const;
756 // The slots of an object are the tagged pointer sized offsets in an object
757 // that may or may not require GC visiting. These helper functions determine
758 // what kind of GC visiting the individual slots require.
759 std::vector<ObjectSlotKind> ComputeHeaderSlotKinds() const;
760 std::optional<ObjectSlotKind> ComputeArraySlotKind() const;
761 bool HasNoPointerSlotsExceptMap() const;
763 const Field* GetFieldPreceding(size_t field_index) const;
764
765 // Given that the field exists in this class or a superclass, returns the
766 // specific class that declared the field.
767 const ClassType* GetClassDeclaringField(const Field& f) const;
768
769 std::string GetSliceMacroName(const Field& field) const;
770
772 return decl_->instance_type_constraints;
773 }
780 bool HasUndefinedLayout() const {
782 }
783 SourcePosition GetPosition() const { return decl_->pos; }
785
786 // TODO(turbofan): We should no longer pass around types as const pointers, so
787 // that we can avoid mutable fields and const initializers for
788 // late-initialized portions of types like this one.
789 void InitializeInstanceTypes(std::optional<int> own,
790 std::optional<std::pair<int, int>> range) const;
791 std::optional<int> OwnInstanceType() const;
792 std::optional<std::pair<int, int>> InstanceTypeRange() const;
793
794 private:
795 friend class TypeOracle;
796 friend class TypeVisitor;
797 ClassType(const Type* parent, Namespace* nspace, const std::string& name,
798 ClassFlags flags, const std::string& generates,
799 const ClassDeclaration* decl, const TypeAlias* alias);
800
801 void GenerateSliceAccessor(size_t field_index);
802
806 const std::string generates_;
809 mutable std::optional<int> own_instance_type_;
810 mutable std::optional<std::pair<int, int>> instance_type_range_;
811};
812
813inline std::ostream& operator<<(std::ostream& os, const Type& t) {
814 os << t.ToString();
815 return os;
816}
817
818template <bool success = false>
819std::ostream& operator<<(std::ostream& os, const Type* t) {
820 static_assert(success,
821 "Using Type* with an ostream is usually a mistake. Did you "
822 "mean to use Type& instead? If you actually intended to print "
823 "a pointer, use static_cast<const void*>.");
824 return os;
825}
826
827// Don't emit an error if a Type* is printed due to CHECK macros.
828inline std::ostream& operator<<(base::CheckMessageStream& os, const Type* t) {
829 return os << static_cast<const void*>(t);
830}
831
833 public:
834 VisitResult() = default;
835 VisitResult(const Type* type, const std::string& constexpr_value)
837 DCHECK(type->IsConstexpr());
838 }
839 static VisitResult NeverResult();
840 static VisitResult TopTypeResult(std::string top_reason,
841 const Type* from_type);
843 : type_(type), stack_range_(stack_range) {
844 DCHECK(!type->IsConstexpr());
845 }
846 const Type* type() const { return type_; }
847 const std::string& constexpr_value() const { return *constexpr_value_; }
848 const StackRange& stack_range() const { return *stack_range_; }
849 void SetType(const Type* new_type) { type_ = new_type; }
850 bool IsOnStack() const { return stack_range_ != std::nullopt; }
851 bool operator==(const VisitResult& other) const {
852 return type_ == other.type_ && constexpr_value_ == other.constexpr_value_ &&
853 stack_range_ == other.stack_range_;
854 }
855
856 private:
857 const Type* type_ = nullptr;
858 std::optional<std::string> constexpr_value_;
859 std::optional<StackRange> stack_range_;
860};
861
863 const std::string& fieldname);
864
865class VisitResultVector : public std::vector<VisitResult> {
866 public:
868 VisitResultVector(std::initializer_list<VisitResult> init)
869 : std::vector<VisitResult>(init) {}
872 for (auto& visit_result : *this) {
873 result.push_back(visit_result.type());
874 }
875 return result;
876 }
877};
878
879std::ostream& operator<<(std::ostream& os, const TypeVector& types);
880
881using NameAndTypeVector = std::vector<NameAndType>;
882
887
888using LabelDefinitionVector = std::vector<LabelDefinition>;
889
894
895using LabelDeclarationVector = std::vector<LabelDeclaration>;
896
901
902std::ostream& operator<<(std::ostream& os, const ParameterTypes& parameters);
903
905
906using NameVector = std::vector<Identifier*>;
907
908struct Signature {
909 Signature(NameVector n, std::optional<std::string> arguments_variable,
910 ParameterTypes p, size_t i, const Type* r, LabelDeclarationVector l,
911 bool transitioning)
912 : parameter_names(std::move(n)),
914 parameter_types(std::move(p)),
916 return_type(r),
917 labels(std::move(l)),
918 transitioning(transitioning) {}
919 Signature() = default;
920 const TypeVector& types() const { return parameter_types.types; }
922 std::optional<std::string> arguments_variable;
924 size_t implicit_count = 0;
925 size_t ExplicitCount() const { return types().size() - implicit_count; }
928 bool transitioning = false;
929 bool HasSameTypesAs(
930 const Signature& other,
940 bool HasContextParameter() const;
941};
942
943void PrintSignature(std::ostream& os, const Signature& sig, bool with_names);
944std::ostream& operator<<(std::ostream& os, const Signature& sig);
945
946bool IsAssignableFrom(const Type* to, const Type* from);
947
948TypeVector LowerType(const Type* type);
949size_t LoweredSlotCount(const Type* type);
950TypeVector LowerParameterTypes(const TypeVector& parameters);
951TypeVector LowerParameterTypes(const ParameterTypes& parameter_types,
952 size_t vararg_count = 0);
953
954std::optional<std::tuple<size_t, std::string>> SizeOf(const Type* type);
955bool IsAnyUnsignedInteger(const Type* type);
956bool IsAllowedAsBitField(const Type* type);
957bool IsPointerSizeIntegralType(const Type* type);
958bool Is32BitIntegralType(const Type* type);
959
960std::optional<NameAndType> ExtractSimpleFieldArraySize(
961 const ClassType& class_type, Expression* array_size);
962
963} // namespace v8::internal::torque
964
965#endif // V8_TORQUE_TYPES_H_
AsmType * return_type_
Definition asm-types.cc:224
const char * name
Definition builtins.cc:39
Builtins::Kind kind
Definition builtins.cc:40
NameAndType name_and_type
AbstractType(const Type *parent, AbstractTypeFlags flags, const std::string &name, const std::string &generated_type, const Type *non_constexpr_version, MaybeSpecializationKey specialized_from)
Definition types.h:293
std::vector< TypeChecker > GetTypeCheckers() const override
Definition types.cc:220
const Type * non_constexpr_version_
Definition types.h:330
bool IsTransient() const override
Definition types.h:319
std::string SimpleNameImpl() const override
Definition types.h:308
const std::string & name() const
Definition types.h:270
bool IsConstexpr() const final
Definition types.h:274
std::string GetGeneratedTypeNameImpl() const override
Definition types.cc:198
std::string ToExplicitString() const override
Definition types.h:271
size_t AlignmentLog2() const override
Definition types.cc:1264
const Type * NonConstexprVersion() const override
Definition types.h:280
const std::string generated_type_
Definition types.h:329
std::string GetGeneratedTNodeTypeNameImpl() const override
Definition types.cc:215
const Field & LookupFieldInternal(const std::string &name) const
Definition types.cc:487
const Field & LastField() const
Definition types.h:582
std::vector< Field > fields_
Definition types.h:608
AggregateType(Kind kind, const Type *parent, Namespace *nspace, const std::string &name, MaybeSpecializationKey specialized_from=std::nullopt)
Definition types.h:593
Namespace * nspace() const
Definition types.h:563
const Field & LookupField(const std::string &name) const
Definition types.cc:499
const std::string & name() const
Definition types.h:562
const std::vector< Field > & fields() const
Definition types.h:556
std::string GetGeneratedTNodeTypeNameImpl() const override
Definition types.h:551
virtual void Finalize() const =0
void SetFields(std::vector< Field > fields)
Definition types.h:555
std::string GetGeneratedTypeNameImpl() const override
Definition types.h:550
virtual const Field & RegisterField(Field field)
Definition types.h:565
std::vector< TypeChecker > GetTypeCheckers() const override
Definition types.h:578
std::vector< Method * > methods_
Definition types.h:613
void RegisterMethod(Method *method)
Definition types.h:570
std::vector< const AggregateType * > GetHierarchy() const
Definition types.cc:459
std::string SimpleNameImpl() const override
Definition types.h:604
const std::vector< Method * > & Methods() const
Definition types.h:571
bool HasField(const std::string &name) const
Definition types.cc:474
const std::vector< BitField > & fields() const
Definition types.h:527
const SourcePosition GetPosition() const
Definition types.h:531
std::vector< TypeChecker > GetTypeCheckers() const override
Definition types.h:515
const std::string & name() const
Definition types.h:526
std::string GetGeneratedTNodeTypeNameImpl() const override
Definition types.h:511
BitFieldStructType(Namespace *nspace, const Type *parent, const BitFieldStructDeclaration *decl)
Definition types.h:535
std::vector< BitField > fields_
Definition types.h:544
std::string SimpleNameImpl() const override
Definition types.h:540
void RegisterField(BitField field)
Definition types.h:524
const BitFieldStructDeclaration * decl_
Definition types.h:543
const Type * ConstexprVersion() const override
Definition types.h:520
std::string GetGeneratedTypeNameImpl() const override
Definition types.h:508
void SetConstexprVersion(const Type *) const override
Definition types.h:519
BuiltinPointerType(const Type *parent, TypeVector parameter_types, const Type *return_type, size_t function_pointer_type_id)
Definition types.h:369
const TypeVector & parameter_types() const
Definition types.h:345
std::string GetGeneratedTypeNameImpl() const override
Definition types.h:338
const Type * return_type() const
Definition types.h:346
std::vector< TypeChecker > GetTypeCheckers() const override
Definition types.h:361
friend size_t hash_value(const BuiltinPointerType &p)
Definition types.h:348
bool operator==(const BuiltinPointerType &other) const
Definition types.h:355
std::string GetGeneratedTNodeTypeNameImpl() const override
Definition types.h:341
const ClassType * GetSuperClass() const
Definition types.h:742
bool DoNotGenerateCast() const
Definition types.h:699
const Field & RegisterField(Field field) override
Definition types.h:748
bool IsLayoutDefinedInCpp() const
Definition types.h:704
bool IsLowestInstanceTypeWithinParent() const
Definition types.h:777
bool ShouldGenerateCppObjectLayoutDefinitionAsserts() const
Definition types.h:718
SourcePosition GetPosition() const
Definition types.h:783
bool ShouldGenerateFactoryFunction() const
Definition types.h:727
const ClassType * GetClassDeclaringField(const Field &f) const
Definition types.cc:805
std::optional< ObjectSlotKind > ComputeArraySlotKind() const
Definition types.cc:757
bool ShouldGenerateFullClassDefinition() const
Definition types.h:722
const InstanceTypeConstraints & GetInstanceTypeConstraints() const
Definition types.h:771
std::optional< std::pair< int, int > > InstanceTypeRange() const
Definition types.cc:698
bool IsHighestInstanceTypeWithinParent() const
Definition types.h:774
bool ShouldGenerateBodyDescriptor() const
Definition types.h:693
void Finalize() const override
Definition types.cc:638
size_t header_size() const
Definition types.h:734
std::optional< int > own_instance_type_
Definition types.h:809
ClassType(const Type *parent, Namespace *nspace, const std::string &name, ClassFlags flags, const std::string &generates, const ClassDeclaration *decl, const TypeAlias *alias)
Definition types.cc:612
bool ShouldGenerateUniqueMap() const
Definition types.h:723
std::string GetGeneratedTNodeTypeNameImpl() const override
Definition types.cc:623
std::optional< int > OwnInstanceType() const
Definition types.cc:693
bool HasNoPointerSlotsExceptMap() const
Definition types.cc:771
const Field * GetFieldPreceding(size_t field_index) const
Definition types.cc:795
ResidueClass size() const
Definition types.h:738
bool IsTransient() const override
Definition types.h:702
void InitializeInstanceTypes(std::optional< int > own, std::optional< std::pair< int, int > > range) const
Definition types.cc:685
std::string GetGeneratedTypeNameImpl() const override
Definition types.cc:627
bool HasSameInstanceTypeAsParent() const
Definition types.h:707
std::string GetSliceMacroName(const Field &field) const
Definition types.cc:812
bool ShouldGenerateVerify() const
Definition types.h:686
void GenerateSliceAccessor(size_t field_index)
Definition types.cc:915
std::string ToExplicitString() const override
Definition types.cc:632
bool ShouldGenerateCppClassDefinitions() const
Definition types.h:710
const ClassDeclaration * decl_
Definition types.h:807
const TypeAlias * alias_
Definition types.h:808
bool AllowInstantiation() const
Definition types.cc:634
std::vector< Field > ComputeHeaderFields() const
Definition types.cc:659
std::vector< Field > ComputeAllFields() const
Definition types.cc:648
SourceId AttributedToFile() const
Definition types.cc:1052
bool HasUndefinedLayout() const
Definition types.h:780
bool HasIndexedFieldsIncludingInParents() const
Definition types.cc:785
const std::string generates_
Definition types.h:806
std::vector< Field > ComputeArrayFields() const
Definition types.cc:671
std::vector< ObjectSlotKind > ComputeHeaderSlotKinds() const
Definition types.cc:747
bool ShouldGenerateCppObjectDefinitionAsserts() const
Definition types.h:715
bool ShouldGeneratePrint() const
Definition types.h:679
std::optional< std::pair< int, int > > instance_type_range_
Definition types.h:810
std::string GetGeneratedTypeNameImpl() const override
Definition types.cc:517
size_t AlignmentLog2() const override
Definition types.cc:1305
const StructDeclaration * decl_
Definition types.h:649
std::string SimpleNameImpl() const override
Definition types.cc:570
SourcePosition GetPosition() const
Definition types.h:638
void Finalize() const override
Definition types.cc:601
Classification ClassifyContents() const
Definition types.cc:529
std::string ToExplicitString() const override
Definition types.cc:599
StructType(Namespace *nspace, const StructDeclaration *decl, MaybeSpecializationKey specialized_from=std::nullopt)
Definition types.cc:504
std::string GetGeneratedTypeNameImpl() const override
Definition types.h:242
const Type * source_type() const
Definition types.h:252
std::string ToExplicitString() const override
Definition types.h:246
const Type * source_type_
Definition types.h:264
TopType(std::string reason, const Type *source_type)
Definition types.h:257
std::string SimpleNameImpl() const override
Definition types.h:261
std::string GetGeneratedTNodeTypeNameImpl() const override
Definition types.h:243
const std::string reason() const
Definition types.h:253
bool IsAggregateType() const
Definition types.h:55
bool IsStructType() const
Definition types.h:53
bool IsBuiltinPointerType() const
Definition types.h:46
bool IsAbstractType() const
Definition types.h:45
bool IsBitFieldStructType() const
Definition types.h:50
const Type * parent_
Definition types.h:189
bool IsNever() const
Definition types.h:126
Type & operator=(const Type &other)=delete
bool IsConstexprBool() const
Definition types.h:128
virtual bool IsTransient() const
Definition types.h:140
virtual void SetConstexprVersion(const Type *type) const
Definition types.h:161
virtual std::string GetGeneratedTNodeTypeNameImpl() const =0
MaybeSpecializationKey specialized_from_
Definition types.h:192
virtual bool IsSubtypeOf(const Type *supertype) const
Definition types.cc:101
virtual const Type * NonConstexprVersion() const
Definition types.h:141
std::optional< const AggregateType * > AggregateSupertype() const
Definition types.cc:142
virtual std::string ToExplicitString() const =0
virtual std::string SimpleNameImpl() const =0
virtual std::string GetGeneratedTypeNameImpl() const =0
bool IsBool() const
Definition types.h:127
bool IsVoid() const
Definition types.h:125
bool IsVoidOrNever() const
Definition types.h:131
size_t id() const
Definition types.h:151
const MaybeSpecializationKey & GetSpecializedFrom() const
Definition types.h:152
virtual const Type * ConstexprVersion() const
Definition types.h:165
void AddAlias(std::string alias) const
Definition types.h:150
void set_parent(const Type *t)
Definition types.h:178
virtual std::string SimpleName() const
Definition types.cc:57
virtual std::vector< TypeChecker > GetTypeCheckers() const
Definition types.h:146
std::set< std::string > aliases_
Definition types.h:190
bool IsFloat64() const
Definition types.h:133
bool IsFloat32() const
Definition types.h:132
virtual bool IsConstexpr() const
Definition types.h:136
const Type * parent() const
Definition types.h:124
std::set< const Type *, TypeLess > types_
Definition types.h:492
void Extend(const Type *t)
Definition types.h:451
bool IsSupertypeOf(const Type *other) const
Definition types.h:426
bool IsSubtypeOf(const Type *other) const override
Definition types.h:419
static UnionType FromType(const Type *t)
Definition types.h:468
friend size_t hash_value(const UnionType &p)
Definition types.h:400
std::optional< const Type * > GetSingleMember() const
Definition types.h:411
const Type * NonConstexprVersion() const override
Definition types.h:446
bool IsConstexpr() const override
Definition types.h:444
bool IsTransient() const override
Definition types.h:435
std::vector< TypeChecker > GetTypeCheckers() const override
Definition types.h:473
bool operator==(const UnionType &other) const
Definition types.h:407
TypeVector ComputeTypeVector() const
Definition types.h:870
VisitResultVector(std::initializer_list< VisitResult > init)
Definition types.h:868
static VisitResult NeverResult()
Definition types.cc:1236
static VisitResult TopTypeResult(std::string top_reason, const Type *from_type)
Definition types.cc:1242
bool operator==(const VisitResult &other) const
Definition types.h:851
const Type * type() const
Definition types.h:846
VisitResult(const Type *type, const std::string &constexpr_value)
Definition types.h:835
VisitResult(const Type *type, StackRange stack_range)
Definition types.h:842
const StackRange & stack_range() const
Definition types.h:848
const std::string & constexpr_value() const
Definition types.h:847
std::optional< std::string > constexpr_value_
Definition types.h:858
std::optional< StackRange > stack_range_
Definition types.h:859
void SetType(const Type *new_type)
Definition types.h:849
constexpr const char * ToString(DataViewOp op)
ZoneVector< RpoNumber > & result
int s
Definition mul-fft.cc:297
int r
Definition mul-fft.cc:298
STL namespace.
V8_INLINE size_t hash_combine(size_t seed, size_t hash)
Definition hashing.h:77
bool operator<(const Type &a, const Type &b)
Definition types.cc:1175
bool IsAssignableFrom(const Type *to, const Type *from)
Definition types.cc:1169
std::ostream & operator<<(std::ostream &os, Identifier *id)
Definition ast.h:263
size_t LoweredSlotCount(const Type *type)
Definition types.cc:1216
std::optional< NameAndType > ExtractSimpleFieldArraySize(const ClassType &class_type, Expression *array_size)
Definition types.cc:1433
void EraseIf(Container *container, F f)
Definition utils.h:328
std::vector< NameAndType > NameAndTypeVector
Definition types.h:881
void ReportError(Args &&... args)
Definition utils.h:96
bool IsAnyUnsignedInteger(const Type *type)
Definition types.cc:1402
std::vector< LabelDeclaration > LabelDeclarationVector
Definition types.h:895
void PrintSignature(std::ostream &os, const Signature &sig, bool with_names)
Definition types.cc:1061
VisitResult ProjectStructField(VisitResult structure, const std::string &fieldname)
Definition types.cc:1177
std::vector< LabelDefinition > LabelDefinitionVector
Definition types.h:888
std::vector< Identifier * > NameVector
Definition types.h:906
std::optional< SpecializationKey< GenericType > > MaybeSpecializationKey
Definition types.h:93
bool Is32BitIntegralType(const Type *type)
Definition types.cc:1427
bool IsAllowedAsBitField(const Type *type)
Definition types.cc:1410
std::optional< std::tuple< size_t, std::string > > SizeOf(const Type *type)
Definition types.cc:1337
bool IsPointerSizeIntegralType(const Type *type)
Definition types.cc:1422
std::optional< ObjectSlotKind > Combine(ObjectSlotKind a, ObjectSlotKind b)
Definition types.h:662
std::vector< const Type * > TypeVector
Definition types.h:85
bool IsConstexprName(const std::string &name)
Definition constants.h:144
size_t hash_value(const TypeVector &types)
Definition types.h:196
TypeVector LowerType(const Type *type)
Definition types.cc:1210
const Type * SubtractType(const Type *a, const Type *b)
Definition types.cc:411
TypeVector LowerParameterTypes(const TypeVector &parameters)
Definition types.cc:1218
kWasmInternalFunctionIndirectPointerTag kProtectedInstanceDataOffset sig
BytecodeSequenceNode * parent_
#define V8_NOEXCEPT
#define UNREACHABLE()
Definition logging.h:67
#define DCHECK_IMPLIES(v1, v2)
Definition logging.h:493
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_EXPORT_PRIVATE
Definition macros.h:460
std::optional< ClassFieldIndexInfo > index
Definition types.h:221
NameAndType name_and_type
Definition types.h:222
FieldSynchronization synchronization
Definition types.h:234
SourcePosition pos
Definition types.h:219
std::optional< size_t > offset
Definition types.h:230
std::tuple< size_t, std::string > GetFieldSizeInformation() const
Definition types.cc:1249
void ValidateAlignment(ResidueClass at_offset) const
Definition types.cc:1317
const AggregateType * aggregate
Definition types.h:220
LabelDeclarationVector labels
Definition types.h:927
ParameterTypes parameter_types
Definition types.h:923
Signature(NameVector n, std::optional< std::string > arguments_variable, ParameterTypes p, size_t i, const Type *r, LabelDeclarationVector l, bool transitioning)
Definition types.h:909
size_t ExplicitCount() const
Definition types.h:925
bool HasSameTypesAs(const Signature &other, ParameterMode mode=ParameterMode::kProcessImplicit) const
Definition types.cc:1128
TypeVector GetExplicitTypes() const
Definition types.h:936
TypeVector GetImplicitTypes() const
Definition types.h:932
const TypeVector & types() const
Definition types.h:920
std::optional< std::string > arguments_variable
Definition types.h:922
bool operator()(const Type *const a, const Type *const b) const
Definition types.h:384
Symbol method
#define DECLARE_TYPE_BOILERPLATE(x)
Definition types.h:65
wasm::ValueType type