Files
sdk/pkg/kernel/lib/src/nnbd_top_merge.dart
T
Johnni Winther 8467186ca0 [kernel] Initial migration of package kernel wave 1
This CL completes the migration of the first wave of
interdependent libraries in package:kernel, including ast.dart.

In order to ensure non-nullability on AST properties, the Transformer
has been split in 2 variants: Transformer which doesn't support
removal of nodes and RemovingTransformer which supports removal where
allowed by the context using 'removal sentinels'.

Start reviewing Transformer and RemovingTransformer in visitors.dart
since many of the changes are caused by the changes here.

Included in the migration are the mixin_deduplication.dart and
unreachable_code_elimination.dart since these needed porting to
the RemovingTransformer which was aided by opting in the libraries
which only depended on ast.dart.

TEST=existing

Change-Id: I9e63b985bd24896c25edd4ee51e37770187bcc17
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/184786
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
2021-02-18 16:01:17 +00:00

132 lines
4.3 KiB
Dart

// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE.md file.
import '../ast.dart';
import '../core_types.dart';
import 'merge_visitor.dart';
/// Returns the NNBD_TOP_MERGE of [a] and [b]. If [a] and [b] have no defined
/// NNBD_TOP_MERGE `null` is returned.
Supertype? nnbdTopMergeSupertype(
CoreTypes coreTypes, Supertype a, Supertype b) {
assert(a.classNode == b.classNode);
if (a.typeArguments.isEmpty) {
return a;
}
List<DartType> newTypeArguments =
new List<DartType>.filled(a.typeArguments.length, dummyDartType);
for (int i = 0; i < a.typeArguments.length; i++) {
DartType? newTypeArgument =
nnbdTopMerge(coreTypes, a.typeArguments[i], b.typeArguments[i]);
if (newTypeArgument == null) return null;
newTypeArguments[i] = newTypeArgument;
}
return new Supertype(a.classNode, newTypeArguments);
}
/// Returns the NNBD_TOP_MERGE of [a] and [b]. If [a] and [b] have no defined
/// NNBD_TOP_MERGE `null` is returned.
DartType? nnbdTopMerge(CoreTypes coreTypes, DartType a, DartType b) {
if (a == b) return a;
return a.accept1(new NnbdTopMergeVisitor(coreTypes), b);
}
class NnbdTopMergeVisitor extends MergeVisitor {
final CoreTypes coreTypes;
NnbdTopMergeVisitor(this.coreTypes);
@override
Nullability? mergeNullability(Nullability a, Nullability b) {
if (a == b) {
return a;
} else if (a == Nullability.legacy) {
return b;
} else if (b == Nullability.legacy) {
return a;
}
return null;
}
@override
DartType? visitInterfaceType(InterfaceType a, DartType b) {
if (a == coreTypes.objectNullableRawType) {
if (b is DynamicType) {
// NNBD_TOP_MERGE(Object?, dynamic) = Object?
return coreTypes.objectNullableRawType;
} else if (b is VoidType) {
// NNBD_TOP_MERGE(Object?, void) = Object?
return coreTypes.objectNullableRawType;
} else if (b == coreTypes.objectNullableRawType) {
// NNBD_TOP_MERGE(Object?, Object?) = Object?
return coreTypes.objectNullableRawType;
}
} else if (a == coreTypes.objectLegacyRawType) {
if (b is DynamicType) {
// NNBD_TOP_MERGE(Object*, dynamic) = Object?
return coreTypes.objectNullableRawType;
} else if (b is VoidType) {
// NNBD_TOP_MERGE(Object*, void) = Object?
return coreTypes.objectNullableRawType;
}
}
return super.visitInterfaceType(a, b);
}
@override
DartType? visitVoidType(VoidType a, DartType b) {
if (b is DynamicType) {
// NNBD_TOP_MERGE(void, dynamic) = Object?
return coreTypes.objectNullableRawType;
} else if (b is VoidType) {
// NNBD_TOP_MERGE(void, void) = void
return const VoidType();
} else if (b == coreTypes.objectNullableRawType) {
// NNBD_TOP_MERGE(void, Object?) = Object?
return coreTypes.objectNullableRawType;
} else if (b == coreTypes.objectLegacyRawType) {
// NNBD_TOP_MERGE(void, Object*) = Object?
return coreTypes.objectNullableRawType;
}
return null;
}
@override
DartType? visitDynamicType(DynamicType a, DartType b) {
if (b is DynamicType) {
// NNBD_TOP_MERGE(dynamic, dynamic) = dynamic
return const DynamicType();
} else if (b is VoidType) {
// NNBD_TOP_MERGE(dynamic, void) = Object?
return coreTypes.objectNullableRawType;
} else if (b == coreTypes.objectNullableRawType) {
// NNBD_TOP_MERGE(dynamic, Object?) = Object?
return coreTypes.objectNullableRawType;
} else if (b == coreTypes.objectLegacyRawType) {
// NNBD_TOP_MERGE(dynamic, Object*) = Object?
return coreTypes.objectNullableRawType;
}
return null;
}
@override
DartType? visitNeverType(NeverType a, DartType b) {
if (a.nullability == Nullability.legacy && b is NullType) {
// NNBD_TOP_MERGE(Never*, Null) = Null
return const NullType();
}
return super.visitNeverType(a, b);
}
@override
DartType? visitNullType(NullType a, DartType b) {
if (b is NeverType && b.nullability == Nullability.legacy) {
// NNBD_TOP_MERGE(Null, Never*) = Null
return const NullType();
}
return super.visitNullType(a, b);
}
}