21 Scope* specialization_requester) {
23 CurrentSourcePosition::Scope scope(decl->
pos);
24 Scope* current_scope = CurrentScope::Get();
25 if (specialized_from) {
28 {requester_position, specialization_requester,
31 CurrentScope::Scope new_current_scope_scope(current_scope);
32 if (specialized_from) {
33 auto& params = specialized_from->generic->generic_parameters();
34 auto arg_types_iterator = specialized_from->specialized_types.begin();
35 for (
auto param : params) {
44#define ENUM_ITEM(name) \
45 case AstNode::Kind::k##name: \
46 return ComputeType(name::cast(decl), specialized_from);
62std::string ComputeGeneratesType(std::optional<std::string> opt_gen,
63 bool enforce_tnode_type) {
64 if (!opt_gen)
return "";
65 const std::string& generates = *opt_gen;
66 if (enforce_tnode_type) {
75 std::string generates =
78 const Type* parent_type =
nullptr;
85 "\" cannot extend a type union");
90 ReportError(
"cannot declare a transient type that is also constexpr");
93 const Type* non_constexpr_version =
nullptr;
97 non_constexpr_version = *
type;
102 decl->
flags, generates,
103 non_constexpr_version, specialized_from);
107 const std::vector<Declaration*>& methods) {
109 CurrentSourcePosition::Scope pos_scope(
declaration->pos);
117 const std::string& method_name(
method->name->value);
124 m->SetIdentifierPosition(
method->name->pos);
130 CurrentSourcePosition::Scope position_scope(decl->
pos);
131 if (specialized_from.has_value()) {
132 ReportError(
"Bitfield struct specialization is not supported");
137 "Bitfield struct must extend from an unsigned integer type, not ",
140 auto opt_size =
SizeOf(parent);
141 if (!opt_size.has_value()) {
143 " because of unsized parent type ", parent->
ToString());
145 const size_t size = 8 * std::get<0>(*opt_size);
151 for (
const auto& field : decl->
fields) {
152 CurrentSourcePosition::Scope field_position_scope(
153 field.name_and_type.type->pos);
157 field.name_and_type.name->value);
163 size_t field_type_size = 0;
167 auto opt_field_type_size =
SizeOf(field_type);
168 if (!opt_field_type_size.has_value()) {
171 field_type_size = 8 * std::get<0>(*opt_field_type_size);
174 if (field.num_bits < 1 ||
175 static_cast<size_t>(field.num_bits) > field_type_size) {
177 field.name_and_type.name->value);
179 type->RegisterField({field.name_and_type.name->pos,
180 {field.name_and_type.name->value, field_type},
184 if (
static_cast<size_t>(
offset) > size) {
195 CurrentScope::Scope struct_namespace_scope(struct_type->
nspace());
196 CurrentSourcePosition::Scope decl_position_activator(decl->
pos);
199 for (
auto& field : decl->
fields) {
200 CurrentSourcePosition::Scope position_activator(
201 field.name_and_type.type->pos);
204 ReportError(
"struct field \"", field.name_and_type.name->value,
205 "\" carries constexpr type \"", *field_type,
"\"");
207 Field f{field.name_and_type.name->pos,
210 {field.name_and_type.name->value, field_type},
213 field.const_qualified,
215 auto optional_size =
SizeOf(f.name_and_type.type);
223 if (optional_size.has_value()) {
224 size_t field_size = 0;
225 std::tie(field_size, std::ignore) = *optional_size;
244 std::string generates = decl->
name->
value;
248 ReportError(
"Shapes must be extern, add \"extern\" to the declaration.");
251 ReportError(
"Shapes need to define their layout.");
253 const ClassType* super_class = ClassType::DynamicCast(super_type);
256 Error(
"Shapes need to extend a subclass of ",
262 generates = super_class->
name();
265 const ClassType* super_class = ClassType::DynamicCast(super_type);
269 "\" must extend either StrongTagged or an already declared class");
274 "\" defines its layout but extends a class which does not")
279 Error(
"cannot export class ", decl->
name,
280 " because superclass is neither @export or extern");
287 "\" requires a layout but doesn't have one");
292 ", non-extern classes always have a unique map.");
296 " shouldn't be used together, because abstract classes are never "
303 " shouldn't be used together, because abstract classes are never "
308 bool enforce_tnode_type =
true;
309 std::string explicit_generates =
310 ComputeGeneratesType(decl->
generates, enforce_tnode_type);
311 if (explicit_generates == generates) {
312 Lint(
"Unnecessary 'generates' clause for class ", decl->
name->
value);
314 generates = explicit_generates;
317 Error(
"cannot export a class that is marked extern");
321 ReportError(
"Only extern classes can specify a generated type.");
325 Error(
"non-external classes must have defined layouts");
331 Error(
"non-extern Torque-defined classes must have unique instance types");
336 "classes that inherit their instance type must be annotated with "
337 "@doNotGenerateCast");
341 generates, decl, alias);
345 if (
auto* basic = BasicTypeExpression::DynamicCast(type_expression)) {
348 auto&
args = basic->generic_arguments;
354 type = alias->type();
355 pos = alias->GetDeclarationPosition();
357 if (alias->IsUserDefined()) {
366 pos = generic_type->declaration()->name->pos;
377 if (
auto* union_type = UnionTypeExpression::DynamicCast(type_expression)) {
381 if (
auto* function_type_exp =
382 FunctionTypeExpression::DynamicCast(type_expression)) {
388 std::move(argument_types),
ComputeType(function_type_exp->return_type));
390 auto* precomputed = PrecomputedTypeExpression::cast(type_expression);
391 return precomputed->type;
398 definition_vector.push_back(def);
400 std::optional<std::string> arguments_variable;
418 size_t header_size = 0;
420 class_offset = super_class->
size();
425 class_declaration->
fields) {
426 CurrentSourcePosition::Scope position_activator(
432 "in-object properties only support subtypes of Object, but "
437 ReportError(
"in-object properties cannot use @customWeakMarking");
440 std::optional<ClassFieldIndexInfo> array_length = field_expression.
index;
459 IntegerLiteralExpression::DynamicCast(field.
index->expr)) {
460 if (
auto value =
literal->value.TryTo<
size_t>()) {
461 field_size *= *
value;
470 class_offset += field_size;
476 Error(
"Indexed fields have to be at the end of the object")
482 class_type->
size_ = class_offset;
494 const std::vector<const Type*>& term_argument_types) {
495 auto* basic = BasicTypeExpression::DynamicCast(type_expression);
497 ReportError(
"expected basic type expression referring to struct");
502 std::optional<GenericType*> maybe_generic_type =
507 ? StructDeclaration::DynamicCast((*maybe_generic_type)->declaration())
511 if (!(maybe_generic_type && decl)) {
513 if (!type->IsStructType() && !type->IsBitFieldStructType()) {
515 " is not a struct or bitfield struct, but used like one");
520 auto generic_type = *maybe_generic_type;
523 std::vector<TypeExpression*> term_parameters;
524 auto& fields = decl->
fields;
525 term_parameters.reserve(fields.size());
526 for (
auto& field : fields) {
527 term_parameters.push_back(field.name_and_type.type);
530 CurrentScope::Scope generic_scope(generic_type->ParentScope());
532 generic_type->generic_parameters(), explicit_type_arguments,
537 ReportError(
"failed to infer type arguments for struct ", basic->name,
542 generic_type->declaration()->name->pos);
544 return StructType::cast(
Namespace * nspace() const
const std::string & name() const
virtual const Field & RegisterField(Field field)
const ClassType * GetSuperClass() const
const Field & RegisterField(Field field) override
size_t header_size() const
ResidueClass size() const
bool HasUndefinedLayout() const
bool ShouldExport() const
void SetIsUserDefined(bool is_user_defined)
static TypeAlias * DeclareType(const Identifier *name, const Type *type)
static Method * CreateMethod(AggregateType *class_type, const std::string &name, Signature signature, Statement *body)
static const TypeAlias * LookupTypeAlias(const QualifiedName &name)
static GenericType * LookupUniqueGenericType(const QualifiedName &name)
static std::optional< GenericType * > TryLookupGenericType(const QualifiedName &name)
static std::optional< const Type * > TryLookupType(const QualifiedName &name)
static bool collect_language_server_data()
static bool collect_kythe_data()
static V8_EXPORT_PRIVATE void AddTypeUse(SourcePosition use_position, const Declarable *type_decl)
static V8_EXPORT_PRIVATE void AddDefinition(SourcePosition token, SourcePosition definition)
MessageBuilder & Position(SourcePosition position)
std::optional< size_t > SingleValue() const
static ResidueClass Unknown()
void SetSpecializationRequester(const SpecializationRequester &requester)
std::optional< TypeDeclaration * > delayed_
TypeVector GetResult() const
const std::string & GetFailureReason()
static const Type * GetStrongTaggedType()
static Namespace * CreateGenericTypeInstantiationNamespace()
static const Type * GetUnionType(UnionType type)
static const AbstractType * GetAbstractType(const Type *parent, std::string name, AbstractTypeFlags flags, std::string generated, const Type *non_constexpr_version, MaybeSpecializationKey specialized_from)
static BitFieldStructType * GetBitFieldStructType(const Type *parent, const BitFieldStructDeclaration *decl)
static const BuiltinPointerType * GetBuiltinPointerType(TypeVector argument_types, const Type *return_type)
static const Type * GetObjectType()
static const Type * GetGenericTypeInstance(GenericType *generic_type, TypeVector arg_types)
static const Type * GetJSObjectType()
static const Type * GetBoolType()
static ClassType * GetClassType(const Type *parent, const std::string &name, ClassFlags flags, const std::string &generates, ClassDeclaration *decl, const TypeAlias *alias)
static StructType * GetStructType(const StructDeclaration *decl, MaybeSpecializationKey specialized_from)
static TypeVector ComputeTypeVector(const std::vector< TypeExpression * > &v)
static void VisitStructMethods(StructType *struct_type, const StructDeclaration *struct_declaration)
static const Type * ComputeType(TypeExpression *type_expression)
static Signature MakeSignature(const CallableDeclaration *declaration)
static const Type * ComputeTypeForStructExpression(TypeExpression *type_expression, const std::vector< const Type * > &term_argument_types)
static void VisitClassFieldsAndMethods(ClassType *class_type, const ClassDeclaration *class_declaration)
virtual bool IsSubtypeOf(const Type *supertype) const
static std::string ComputeName(const std::string &basename, MaybeSpecializationKey specialized_from)
std::string ToString() const
virtual bool IsConstexpr() const
base::Vector< const DirectHandle< Object > > args
ZoneVector< RpoNumber > & result
FunctionLiteral * literal
static const char *const kThisParameterName
std::string UnwrapTNodeTypeName(const std::string &generates)
void ReportError(Args &&... args)
bool IsAnyUnsignedInteger(const Type *type)
std::vector< LabelDeclaration > LabelDeclarationVector
T * MakeNode(Args... args)
MessageBuilder Lint(Args &&... args)
std::optional< SpecializationKey< GenericType > > MaybeSpecializationKey
static const char *const ANNOTATION_GENERATE_FACTORY_FUNCTION
bool IsAllowedAsBitField(const Type *type)
std::optional< std::tuple< size_t, std::string > > SizeOf(const Type *type)
std::vector< T > TransformVector(const std::vector< U > &v, F f)
std::vector< const Type * > TypeVector
std::string GetNonConstexprName(const std::string &name)
static const char *const ANNOTATION_ABSTRACT
static const char *const ANNOTATION_GENERATE_UNIQUE_MAP
@ kHasSameInstanceTypeAsParent
@ kGenerateBodyDescriptor
@ kGenerateFactoryFunction
MessageBuilder Error(Args &&... args)
void DeclareMethods(AggregateType *container_type, const std::vector< Declaration * > &methods)
#define DCHECK_EQ(v1, v2)
#define DCHECK_GT(v1, v2)
std::optional< TypeExpression * > extends
std::optional< std::string > generates
std::vector< BitFieldDeclaration > fields
std::vector< ClassFieldExpression > fields
std::optional< std::string > generates
std::vector< Declaration * > methods
NameAndTypeExpression name_and_type
FieldSynchronization synchronization
std::optional< ClassFieldIndexInfo > index
std::optional< ClassFieldIndexInfo > index
std::tuple< size_t, std::string > GetFieldSizeInformation() const
void ValidateAlignment(ResidueClass at_offset) const
NameVector parameter_names
ParameterTypes parameter_types
std::optional< std::string > arguments_variable
static SourcePosition Invalid()
std::vector< StructFieldExpression > fields
std::vector< Declaration * > methods
#define AST_TYPE_DECLARATION_NODE_KIND_LIST(V)