Skip to content

Commit 5bbf141

Browse files
alexmarkovCommit Bot
authored and
Commit Bot
committed
[vm] Support arbitrary superclasses for enums
With enhanced enums language feature enum classes can have a mixin, so its superclass is an arbitrary class and not necessarily _Enum. This change removes lookups of 'index' and '_name' fields in the superclass of an enum class. They are now looked up once in _Enum class and saved in the object store. TEST=co19/LanguageFeatures/Enhanced-Enum/semantics_A01_t01 Issue: #47861 Change-Id: I2b93d389ab92e0274641e0f1bd65471e6407f3f9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/228220 Reviewed-by: Ben Konyi <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent a5554dc commit 5bbf141

File tree

9 files changed

+120
-104
lines changed

9 files changed

+120
-104
lines changed

pkg/front_end/testcases/strong.status

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ static_field_lowering/opt_in: SemiFuzzFailure
1515

1616
constructor_tearoffs/call_instantiation: TypeCheckError
1717
constructor_tearoffs/lowering/invalid_redirect: VerificationError
18-
enhanced_enums/enum_as_supertype: RuntimeError
1918
enhanced_enums/simple_mixins: RuntimeError
2019
extension_types/access_setter_as_getter: ExpectationFileMismatchSerialized # Expected.
2120
extension_types/call_not_get: ExpectationFileMismatchSerialized # Expected.

pkg/front_end/testcases/text_serialization.status

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
constructor_tearoffs/call_instantiation: TypeCheckError
1010
constructor_tearoffs/lowering/invalid_redirect: VerificationError
11-
enhanced_enums/enum_as_supertype: RuntimeError
1211
enhanced_enums/simple_mixins: RuntimeError
1312
extension_types/access_setter_as_getter: ExpectationFileMismatchSerialized # Expected.
1413
extension_types/call_not_get: ExpectationFileMismatchSerialized # Expected.

pkg/front_end/testcases/weak.status

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ static_field_lowering/opt_in: SemiFuzzFailure
2222

2323
constructor_tearoffs/call_instantiation: TypeCheckError
2424
constructor_tearoffs/lowering/invalid_redirect: VerificationError
25-
enhanced_enums/enum_as_supertype: RuntimeError
2625
enhanced_enums/simple_mixins: RuntimeError
2726
extension_types/access_setter_as_getter: ExpectationFileMismatchSerialized # Expected.
2827
extension_types/call_not_get: ExpectationFileMismatchSerialized # Expected.

runtime/vm/class_finalizer.cc

+3-7
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,7 @@ ErrorPtr ClassFinalizer::LoadClassMembers(const Class& cls) {
12231223
void ClassFinalizer::AllocateEnumValues(const Class& enum_cls) {
12241224
Thread* thread = Thread::Current();
12251225
Zone* zone = thread->zone();
1226+
ObjectStore* object_store = thread->isolate_group()->object_store();
12261227

12271228
const auto& values_field =
12281229
Field::Handle(zone, enum_cls.LookupStaticField(Symbols::Values()));
@@ -1237,16 +1238,11 @@ void ClassFinalizer::AllocateEnumValues(const Class& enum_cls) {
12371238
ASSERT(values.IsArray());
12381239
}
12391240

1240-
// The enum_cls is the actual declared class.
1241-
// The shared super-class holds the fields for index and name.
1242-
const auto& super_cls = Class::Handle(zone, enum_cls.SuperClass());
1243-
12441241
const auto& index_field =
1245-
Field::Handle(zone, super_cls.LookupInstanceField(Symbols::Index()));
1242+
Field::Handle(zone, object_store->enum_index_field());
12461243
ASSERT(!index_field.IsNull());
12471244

1248-
const auto& name_field = Field::Handle(
1249-
zone, super_cls.LookupInstanceFieldAllowPrivate(Symbols::_name()));
1245+
const auto& name_field = Field::Handle(zone, object_store->enum_name_field());
12501246
ASSERT(!name_field.IsNull());
12511247

12521248
const auto& enum_name = String::Handle(zone, enum_cls.ScrubbedName());

runtime/vm/compiler/runtime_offsets_extracted.h

+92-88
Large diffs are not rendered by default.

runtime/vm/object_reload.cc

+5-6
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,11 @@ void Class::ReplaceEnum(ProgramReloadContext* reload_context,
302302
ASSERT(is_finalized());
303303
ASSERT(old_enum.is_finalized());
304304

305-
Zone* zone = Thread::Current()->zone();
305+
Thread* thread = Thread::Current();
306+
Zone* zone = thread->zone();
307+
ObjectStore* object_store = thread->isolate_group()->object_store();
306308

307309
Field& field = Field::Handle(zone);
308-
Class& cls = Class::Handle(zone);
309310
String& enum_ident = String::Handle();
310311
Instance& old_enum_value = Instance::Handle(zone);
311312
Instance& enum_value = Instance::Handle(zone);
@@ -338,8 +339,7 @@ void Class::ReplaceEnum(ProgramReloadContext* reload_context,
338339
old_deleted_enum_sentinel ^= field.StaticConstFieldValue();
339340
ASSERT(!old_deleted_enum_sentinel.IsNull());
340341

341-
cls = old_enum.SuperClass();
342-
field = cls.LookupInstanceFieldAllowPrivate(Symbols::_name());
342+
field = object_store->enum_name_field();
343343
ASSERT(!field.IsNull());
344344

345345
UnorderedHashMap<EnumMapTraits> enum_map(enum_map_storage.ptr());
@@ -374,8 +374,7 @@ void Class::ReplaceEnum(ProgramReloadContext* reload_context,
374374
deleted_enum_sentinel ^= field.StaticConstFieldValue();
375375
ASSERT(!deleted_enum_sentinel.IsNull());
376376

377-
cls = SuperClass();
378-
field = cls.LookupInstanceFieldAllowPrivate(Symbols::_name());
377+
field = object_store->enum_name_field();
379378
ASSERT(!field.IsNull());
380379

381380
UnorderedHashMap<EnumMapTraits> enum_map(enum_map_storage.ptr());

runtime/vm/object_store.cc

+17
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ void ObjectStore::LazyInitCoreMembers() {
361361
if (list_class_.load() == Type::null()) {
362362
ASSERT(non_nullable_list_rare_type_.load() == Type::null());
363363
ASSERT(non_nullable_map_rare_type_.load() == Type::null());
364+
ASSERT(enum_index_field_.load() == Field::null());
365+
ASSERT(enum_name_field_.load() == Field::null());
364366
ASSERT(_object_equals_function_.load() == Function::null());
365367
ASSERT(_object_hash_code_function_.load() == Function::null());
366368
ASSERT(_object_to_string_function_.load() == Function::null());
@@ -382,6 +384,21 @@ void ObjectStore::LazyInitCoreMembers() {
382384
type ^= cls.RareType();
383385
non_nullable_map_rare_type_.store(type.ptr());
384386

387+
auto& field = Field::Handle(zone);
388+
389+
cls = core_lib.LookupClassAllowPrivate(Symbols::_Enum());
390+
ASSERT(!cls.IsNull());
391+
const auto& error = cls.EnsureIsFinalized(thread);
392+
ASSERT(error == Error::null());
393+
394+
field = cls.LookupInstanceField(Symbols::Index());
395+
ASSERT(!field.IsNull());
396+
enum_index_field_.store(field.ptr());
397+
398+
field = cls.LookupInstanceFieldAllowPrivate(Symbols::_name());
399+
ASSERT(!field.IsNull());
400+
enum_name_field_.store(field.ptr());
401+
385402
auto& function = Function::Handle(zone);
386403

387404
function = core_lib.LookupFunctionAllowPrivate(Symbols::_objectHashCode());

runtime/vm/object_store.h

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class ObjectPointerVisitor;
4747
LAZY_CORE(Class, list_class) \
4848
LAZY_CORE(Type, non_nullable_list_rare_type) \
4949
LAZY_CORE(Type, non_nullable_map_rare_type) \
50+
LAZY_CORE(Field, enum_index_field) \
51+
LAZY_CORE(Field, enum_name_field) \
5052
LAZY_CORE(Function, _object_equals_function) \
5153
LAZY_CORE(Function, _object_hash_code_function) \
5254
LAZY_CORE(Function, _object_to_string_function) \

runtime/vm/symbols.h

+1
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ class ObjectPointerVisitor;
293293
V(_DeletedEnumPrefix, "Deleted enum value from ") \
294294
V(_DeletedEnumSentinel, "_deleted_enum_sentinel") \
295295
V(_Double, "_Double") \
296+
V(_Enum, "_Enum") \
296297
V(_ExternalFloat32Array, "_ExternalFloat32Array") \
297298
V(_ExternalFloat32x4Array, "_ExternalFloat32x4Array") \
298299
V(_ExternalFloat64Array, "_ExternalFloat64Array") \

0 commit comments

Comments
 (0)