Skip to content

Commit d45c3d1

Browse files
creliercommit-bot@chromium.org
authored andcommitted
[VM/nnbd] Make Nullability and NNBDMode class enums to avoid name conflicts.
Change-Id: Ic78d3f48964bb61cbd1d90a69fcd68b4f29071e4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/124587 Commit-Queue: Régis Crelier <[email protected]> Reviewed-by: Alexander Markov <[email protected]>
1 parent cfec969 commit d45c3d1

File tree

7 files changed

+76
-50
lines changed

7 files changed

+76
-50
lines changed

runtime/lib/mirrors.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -633,16 +633,17 @@ static RawInstance* CreateTypeMirror(const AbstractType& type) {
633633
// TODO(regis): Until mirrors reflect nullability, force kLegacy, except for
634634
// Null type, which should remain nullable.
635635
if (!type.IsNullType()) {
636-
const Type& legacy_type =
637-
Type::Handle(Type::Cast(type).ToNullability(kLegacy, Heap::kOld));
636+
const Type& legacy_type = Type::Handle(
637+
Type::Cast(type).ToNullability(Nullability::kLegacy, Heap::kOld));
638638
return CreateClassMirror(cls, legacy_type, Bool::False(),
639639
Object::null_instance());
640640
}
641641
return CreateClassMirror(cls, type, Bool::False(), Object::null_instance());
642642
} else if (type.IsTypeParameter()) {
643643
// TODO(regis): Until mirrors reflect nullability, force kLegacy.
644-
const TypeParameter& legacy_type = TypeParameter::Handle(
645-
TypeParameter::Cast(type).ToNullability(kLegacy, Heap::kOld));
644+
const TypeParameter& legacy_type =
645+
TypeParameter::Handle(TypeParameter::Cast(type).ToNullability(
646+
Nullability::kLegacy, Heap::kOld));
646647
return CreateTypeVariableMirror(legacy_type, Object::null_instance());
647648
}
648649
UNREACHABLE();

runtime/vm/compiler/frontend/bytecode_reader.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,12 @@ void BytecodeReaderHelper::ReadClosureDeclaration(const Function& function,
502502
closures_->SetAt(closureIndex, closure);
503503

504504
Type& signature_type = Type::Handle(
505-
Z, ReadFunctionSignature(
506-
closure, (flags & kHasOptionalPositionalParamsFlag) != 0,
507-
(flags & kHasOptionalNamedParamsFlag) != 0,
508-
(flags & kHasTypeParamsFlag) != 0,
509-
/* has_positional_param_names = */ true, kNonNullable));
505+
Z, ReadFunctionSignature(closure,
506+
(flags & kHasOptionalPositionalParamsFlag) != 0,
507+
(flags & kHasOptionalNamedParamsFlag) != 0,
508+
(flags & kHasTypeParamsFlag) != 0,
509+
/* has_positional_param_names = */ true,
510+
Nullability::kNonNullable));
510511

511512
closure.SetSignatureType(signature_type);
512513

@@ -1508,7 +1509,7 @@ RawObject* BytecodeReaderHelper::ReadObjectContents(uint32_t header) {
15081509
const Nullability nullability =
15091510
bytecode_component_->GetVersion() >= 24
15101511
? static_cast<Nullability>((flags & kNullabilityMask) / kFlagBit4)
1511-
: kLegacy;
1512+
: Nullability::kLegacy;
15121513
return ReadType(tag, nullability);
15131514
}
15141515
default:

runtime/vm/compiler/frontend/kernel_fingerprints.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ void KernelFingerprintHelper::CalculateDartTypeFingerprint() {
246246
break;
247247
case kTypeParameterType: {
248248
Nullability nullability = ReadNullability();
249-
BuildHash(nullability);
249+
BuildHash(static_cast<uint32_t>(nullability));
250250
ReadUInt(); // read index for parameter.
251251
CalculateOptionalDartTypeFingerprint(); // read bound bound.
252252
break;
@@ -269,7 +269,7 @@ void KernelFingerprintHelper::CalculateOptionalDartTypeFingerprint() {
269269

270270
void KernelFingerprintHelper::CalculateInterfaceTypeFingerprint(bool simple) {
271271
Nullability nullability = ReadNullability();
272-
BuildHash(nullability);
272+
BuildHash(static_cast<uint32_t>(nullability));
273273
NameIndex kernel_class = ReadCanonicalNameReference();
274274
ASSERT(H.IsClass(kernel_class));
275275
const String& class_name = H.DartClassName(kernel_class);
@@ -285,7 +285,7 @@ void KernelFingerprintHelper::CalculateInterfaceTypeFingerprint(bool simple) {
285285

286286
void KernelFingerprintHelper::CalculateFunctionTypeFingerprint(bool simple) {
287287
Nullability nullability = ReadNullability();
288-
BuildHash(nullability);
288+
BuildHash(static_cast<uint32_t>(nullability));
289289

290290
if (!simple) {
291291
CalculateTypeParametersListFingerprint(); // read type_parameters.

runtime/vm/compiler/frontend/kernel_translation_helper.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2769,7 +2769,7 @@ void TypeTranslator::BuildTypeInternal() {
27692769
break;
27702770
case kBottomType:
27712771
result_ = Class::Handle(Z, I->object_store()->null_class())
2772-
.DeclarationType(kNullable);
2772+
.DeclarationType(Nullability::kNullable);
27732773
ASSERT(result_.IsNullable());
27742774
break;
27752775
case kNeverType:

runtime/vm/object.cc

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -944,13 +944,13 @@ void Object::Init(Isolate* isolate) {
944944
cls.set_is_type_finalized();
945945

946946
cls = dynamic_class_;
947-
*dynamic_type_ = Type::NewNonParameterizedType(cls, kNullable);
947+
*dynamic_type_ = Type::NewNonParameterizedType(cls, Nullability::kNullable);
948948

949949
cls = void_class_;
950-
*void_type_ = Type::NewNonParameterizedType(cls, kNullable);
950+
*void_type_ = Type::NewNonParameterizedType(cls, Nullability::kNullable);
951951

952952
cls = never_class_;
953-
*never_type_ = Type::NewNonParameterizedType(cls, kNonNullable);
953+
*never_type_ = Type::NewNonParameterizedType(cls, Nullability::kNonNullable);
954954

955955
// Since TypeArguments objects are passed as function arguments, make them
956956
// behave as Dart instances, although they are just VM objects.
@@ -1934,7 +1934,7 @@ RawError* Object::Init(Isolate* isolate,
19341934
// name is a built-in identifier (this is wrong). The corresponding types
19351935
// are stored in the object store.
19361936
cls = object_store->null_class();
1937-
type = Type::NewNonParameterizedType(cls, kNullable);
1937+
type = Type::NewNonParameterizedType(cls, Nullability::kNullable);
19381938
cls.set_declaration_type(type);
19391939
object_store->set_null_type(type);
19401940
ASSERT(type.IsNullable());
@@ -4345,7 +4345,7 @@ RawType* Class::DeclarationType(Nullability nullability) const {
43454345
ASSERT(is_declaration_loaded());
43464346
if (IsNullClass()) {
43474347
// Ignore requested nullability (e.g. by mirrors).
4348-
nullability = kNullable;
4348+
nullability = Nullability::kNullable;
43494349
}
43504350
Type& type = Type::Handle(declaration_type());
43514351
if (!type.IsNull()) {
@@ -16664,8 +16664,8 @@ RawAbstractType* Instance::GetType(Heap::Space space) const {
1666416664
}
1666516665
// TODO(regis): The runtime type of a non-null instance should be
1666616666
// non-nullable instead of legacy. Revisit.
16667-
type = Type::New(cls, type_arguments, TokenPosition::kNoSource, kLegacy,
16668-
space);
16667+
type = Type::New(cls, type_arguments, TokenPosition::kNoSource,
16668+
Nullability::kLegacy, space);
1666916669
type.SetIsFinalized();
1667016670
type ^= type.Canonicalize();
1667116671
}
@@ -17019,7 +17019,7 @@ TokenPosition AbstractType::token_pos() const {
1701917019
Nullability AbstractType::nullability() const {
1702017020
// AbstractType is an abstract class.
1702117021
UNREACHABLE();
17022-
return kNullable;
17022+
return Nullability::kNullable;
1702317023
}
1702417024

1702517025
bool AbstractType::IsInstantiated(Genericity genericity,
@@ -17201,8 +17201,21 @@ RawString* AbstractType::PrintURIs(URIs* uris) {
1720117201
return Symbols::FromConcatAll(thread, pieces);
1720217202
}
1720317203

17204-
// Keep in sync with Nullability enum in runtime/vm/object.h.
17205-
static const char* nullability_suffix[4] = {"%", "?", "", "*"};
17204+
static const String& NullabilitySuffix(Nullability value) {
17205+
// Keep in sync with Nullability enum in runtime/vm/object.h.
17206+
switch (value) {
17207+
case Nullability::kUndetermined:
17208+
return Symbols::Percent();
17209+
case Nullability::kNullable:
17210+
return Symbols::QuestionMark();
17211+
case Nullability::kNonNullable:
17212+
return Symbols::Empty();
17213+
case Nullability::kLegacy:
17214+
return Symbols::Star();
17215+
default:
17216+
UNREACHABLE();
17217+
}
17218+
}
1720617219

1720717220
RawString* AbstractType::BuildName(NameVisibility name_visibility) const {
1720817221
ASSERT(name_visibility != kScrubbedName);
@@ -17212,7 +17225,7 @@ RawString* AbstractType::BuildName(NameVisibility name_visibility) const {
1721217225
if (FLAG_show_nullability) {
1721317226
return Symbols::FromConcat(
1721417227
thread, String::Handle(zone, TypeParameter::Cast(*this).name()),
17215-
String::Handle(zone, String::New(nullability_suffix[nullability()])));
17228+
NullabilitySuffix(nullability()));
1721617229
}
1721717230
return TypeParameter::Cast(*this).name();
1721817231
}
@@ -17230,8 +17243,7 @@ RawString* AbstractType::BuildName(NameVisibility name_visibility) const {
1723017243
return Symbols::FromConcat(
1723117244
thread,
1723217245
String::Handle(zone, signature_function.UserVisibleSignature()),
17233-
String::Handle(zone,
17234-
String::New(nullability_suffix[nullability()])));
17246+
NullabilitySuffix(nullability()));
1723517247
}
1723617248
return signature_function.UserVisibleSignature();
1723717249
}
@@ -17241,10 +17253,9 @@ RawString* AbstractType::BuildName(NameVisibility name_visibility) const {
1724117253
if (!IsFinalized() || IsBeingFinalized()) {
1724217254
// TODO(regis): Check if this is dead code.
1724317255
if (FLAG_show_nullability) {
17244-
return Symbols::FromConcat(
17245-
thread, String::Handle(zone, class_name.raw()),
17246-
String::Handle(zone,
17247-
String::New(nullability_suffix[nullability()])));
17256+
return Symbols::FromConcat(thread,
17257+
String::Handle(zone, class_name.raw()),
17258+
NullabilitySuffix(nullability()));
1724817259
}
1724917260
return class_name.raw();
1725017261
}
@@ -17287,8 +17298,7 @@ RawString* AbstractType::BuildName(NameVisibility name_visibility) const {
1728717298
pieces.Add(args_name);
1728817299
}
1728917300
if (FLAG_show_nullability) {
17290-
pieces.Add(
17291-
String::Handle(zone, String::New(nullability_suffix[nullability()])));
17301+
pieces.Add(NullabilitySuffix(nullability()));
1729217302
}
1729317303
// The name is only used for type checking and debugging purposes.
1729417304
// Unless profiling data shows otherwise, it is not worth caching the name in
@@ -17327,7 +17337,7 @@ bool AbstractType::IsTopType(NNBDMode mode) const {
1732717337
return false;
1732817338
}
1732917339
if (cid == kDynamicCid || cid == kVoidCid ||
17330-
(cid == kInstanceCid && (mode != kStrong || IsNullable()))) {
17340+
(cid == kInstanceCid && (mode != NNBDMode::kStrong || IsNullable()))) {
1733117341
return true;
1733217342
}
1733317343
// FutureOr<T> where T is a top type behaves as a top type.
@@ -18463,7 +18473,7 @@ void TypeParameter::SetGenericCovariantImpl(bool value) const {
1846318473
}
1846418474

1846518475
void TypeParameter::set_nullability(Nullability value) const {
18466-
StoreNonPointer(&raw_ptr()->nullability_, value);
18476+
StoreNonPointer(&raw_ptr()->nullability_, static_cast<int8_t>(value));
1846718477
}
1846818478

1846918479
RawTypeParameter* TypeParameter::ToNullability(Nullability value,
@@ -18662,7 +18672,7 @@ RawTypeParameter* TypeParameter::New(const Class& parameterized_class,
1866218672
result.set_name(name);
1866318673
result.set_bound(bound);
1866418674
result.set_flags(0);
18665-
result.set_nullability(kLegacy);
18675+
result.set_nullability(Nullability::kLegacy);
1866618676
result.SetGenericCovariantImpl(is_generic_covariant_impl);
1866718677
result.SetHash(0);
1866818678
result.set_token_pos(token_pos);

runtime/vm/object.h

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -838,15 +838,15 @@ typedef ZoneGrowableHandlePtrArray<const AbstractType>* TrailPtr;
838838
typedef ZoneGrowableHandlePtrArray<const String> URIs;
839839

840840
// Keep in sync with package:kernel/lib/ast.dart
841-
enum Nullability {
841+
enum class Nullability {
842842
kUndetermined = 0,
843843
kNullable = 1,
844844
kNonNullable = 2,
845845
kLegacy = 3,
846846
};
847847

848848
// Nullability aware subtype checking modes.
849-
enum NNBDMode {
849+
enum class NNBDMode {
850850
kUnaware,
851851
kWeak,
852852
kStrong,
@@ -954,7 +954,8 @@ class Class : public Object {
954954
// variant may be requested. The first requested type gets cached in the class
955955
// and subsequent nullability variants get cached in the object store.
956956
// TODO(regis): Is this caching still useful or should we eliminate it?
957-
RawType* DeclarationType(Nullability nullability = kLegacy) const;
957+
RawType* DeclarationType(
958+
Nullability nullability = Nullability::kLegacy) const;
958959

959960
static intptr_t declaration_type_offset() {
960961
return OFFSET_OF(RawClass, declaration_type_);
@@ -2189,7 +2190,8 @@ class Function : public Object {
21892190
// function type with uninstantiated type arguments 'T' and 'R' as elements of
21902191
// its type argument vector.
21912192
// A function type is non-nullable by default.
2192-
RawType* SignatureType(Nullability nullability = kNonNullable) const;
2193+
RawType* SignatureType(
2194+
Nullability nullability = Nullability::kNonNullable) const;
21932195
RawType* ExistingSignatureType() const;
21942196

21952197
// Update the signature type (with a canonical version).
@@ -6819,10 +6821,18 @@ class AbstractType : public Instance {
68196821
virtual void SetIsBeingFinalized() const;
68206822

68216823
virtual Nullability nullability() const;
6822-
virtual bool IsUndetermined() const { return nullability() == kUndetermined; }
6823-
virtual bool IsNullable() const { return nullability() == kNullable; }
6824-
virtual bool IsNonNullable() const { return nullability() == kNonNullable; }
6825-
virtual bool IsLegacy() const { return nullability() == kLegacy; }
6824+
virtual bool IsUndetermined() const {
6825+
return nullability() == Nullability::kUndetermined;
6826+
}
6827+
virtual bool IsNullable() const {
6828+
return nullability() == Nullability::kNullable;
6829+
}
6830+
virtual bool IsNonNullable() const {
6831+
return nullability() == Nullability::kNonNullable;
6832+
}
6833+
virtual bool IsLegacy() const {
6834+
return nullability() == Nullability::kLegacy;
6835+
}
68266836

68276837
virtual bool HasTypeClass() const { return type_class_id() != kIllegalCid; }
68286838
virtual classid_t type_class_id() const;
@@ -6944,7 +6954,7 @@ class AbstractType : public Instance {
69446954

69456955
// Check if this type represents a top type.
69466956
// TODO(regis): Remove default kUnaware mode as implementation progresses.
6947-
bool IsTopType(NNBDMode mode = kUnaware) const;
6957+
bool IsTopType(NNBDMode mode = NNBDMode::kUnaware) const;
69486958

69496959
// Check if this type represents the 'bool' type.
69506960
bool IsBoolType() const;
@@ -7057,8 +7067,8 @@ class Type : public AbstractType {
70577067
}
70587068
void set_nullability(Nullability value) const {
70597069
ASSERT(!IsCanonical());
7060-
ASSERT(value != kUndetermined);
7061-
StoreNonPointer(&raw_ptr()->nullability_, value);
7070+
ASSERT(value != Nullability::kUndetermined);
7071+
StoreNonPointer(&raw_ptr()->nullability_, static_cast<int8_t>(value));
70627072
}
70637073
RawType* ToNullability(Nullability value, Heap::Space space) const;
70647074
virtual classid_t type_class_id() const;
@@ -7157,13 +7167,14 @@ class Type : public AbstractType {
71577167
static RawType* DartTypeType();
71587168

71597169
// The finalized type of the given non-parameterized class.
7160-
static RawType* NewNonParameterizedType(const Class& type_class,
7161-
Nullability nullability = kLegacy);
7170+
static RawType* NewNonParameterizedType(
7171+
const Class& type_class,
7172+
Nullability nullability = Nullability::kLegacy);
71627173

71637174
static RawType* New(const Class& clazz,
71647175
const TypeArguments& arguments,
71657176
TokenPosition token_pos,
7166-
Nullability nullability = kLegacy,
7177+
Nullability nullability = Nullability::kLegacy,
71677178
Heap::Space space = Heap::kOld);
71687179

71697180
private:

runtime/vm/symbols.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@ class Symbols : public AllStatic {
580580
static const String& Percent() {
581581
return *(symbol_handles_[kNullCharId + '%']);
582582
}
583+
static const String& QuestionMark() {
584+
return *(symbol_handles_[kNullCharId + '?']);
585+
}
583586
static const String& Caret() { return *(symbol_handles_[kNullCharId + '^']); }
584587
static const String& Tilde() { return *(symbol_handles_[kNullCharId + '~']); }
585588

0 commit comments

Comments
 (0)