|
| 1 | +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE.md file. |
| 4 | + |
| 5 | +import 'package:analyzer/dart/element/element.dart'; |
| 6 | +import 'package:analyzer/dart/element/nullability_suffix.dart'; |
| 7 | +import 'package:analyzer/dart/element/type.dart'; |
| 8 | +import 'package:analyzer/src/dart/element/replacement_visitor.dart'; |
| 9 | +import 'package:analyzer/src/dart/element/type.dart'; |
| 10 | +import 'package:analyzer/src/dart/element/type_visitor.dart'; |
| 11 | + |
| 12 | +/// Returns [type] in which all promoted type variables have been replace with |
| 13 | +/// their unpromoted equivalents, and, if [library] is non-nullable by default, |
| 14 | +/// replaces all legacy types with their non-nullable equivalents. |
| 15 | +DartType demoteType(LibraryElement library, DartType type) { |
| 16 | + if (library.isNonNullableByDefault) { |
| 17 | + var visitor = const _DemotionNonNullification(); |
| 18 | + return visitor.visit(type) ?? type; |
| 19 | + } else { |
| 20 | + var visitor = const _DemotionNonNullification(nonNullifyTypes: false); |
| 21 | + return visitor.visit(type) ?? type; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/// Returns `true` if type contains a promoted type variable. |
| 26 | +bool hasPromotedTypeVariable(DartType type) { |
| 27 | + return const _HasPromotedTypeVariableVisitor()._visit(type); |
| 28 | +} |
| 29 | + |
| 30 | +/// Returns [type] in which all legacy types have been replaced with |
| 31 | +/// non-nullable types. |
| 32 | +DartType nonNullifyType(LibraryElement library, DartType type) { |
| 33 | + if (library.isNonNullableByDefault) { |
| 34 | + var visitor = const _DemotionNonNullification(demoteTypeVariables: false); |
| 35 | + return visitor.visit(type) ?? type; |
| 36 | + } |
| 37 | + return type; |
| 38 | +} |
| 39 | + |
| 40 | +/// Visitor that replaces all promoted type variables the type variable itself |
| 41 | +/// and/or replaces all legacy types with non-nullable types. |
| 42 | +/// |
| 43 | +/// The visitor returns `null` if the type wasn't changed. |
| 44 | +class _DemotionNonNullification extends ReplacementVisitor { |
| 45 | + final bool demoteTypeVariables; |
| 46 | + final bool nonNullifyTypes; |
| 47 | + |
| 48 | + const _DemotionNonNullification({ |
| 49 | + this.demoteTypeVariables = true, |
| 50 | + this.nonNullifyTypes = true, |
| 51 | + }) : assert(demoteTypeVariables || nonNullifyTypes); |
| 52 | + |
| 53 | + @override |
| 54 | + NullabilitySuffix visitNullability(DartType type) { |
| 55 | + if (nonNullifyTypes && type.nullabilitySuffix == NullabilitySuffix.star) { |
| 56 | + return NullabilitySuffix.none; |
| 57 | + } |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + @override |
| 62 | + DartType visitTypeParameterType(TypeParameterType type) { |
| 63 | + var newNullability = visitNullability(type); |
| 64 | + |
| 65 | + if (demoteTypeVariables) { |
| 66 | + var typeImpl = type as TypeParameterTypeImpl; |
| 67 | + if (typeImpl.promotedBound != null) { |
| 68 | + return TypeParameterTypeImpl( |
| 69 | + element: type.element, |
| 70 | + nullabilitySuffix: newNullability ?? type.nullabilitySuffix, |
| 71 | + ); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return createTypeParameterType( |
| 76 | + type: type, |
| 77 | + newNullability: newNullability, |
| 78 | + ); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// Visitor that returns `true` if a type contains a promoted type variable. |
| 83 | +class _HasPromotedTypeVariableVisitor extends DartTypeVisitor<bool> { |
| 84 | + const _HasPromotedTypeVariableVisitor(); |
| 85 | + |
| 86 | + @override |
| 87 | + bool defaultDartType(DartType type) => false; |
| 88 | + |
| 89 | + @override |
| 90 | + bool visitFunctionType(FunctionType type) { |
| 91 | + if (_visit(type.returnType)) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + for (var parameter in type.parameters) { |
| 96 | + if (_visit(parameter.type)) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + @override |
| 105 | + bool visitInterfaceType(InterfaceType type) { |
| 106 | + for (var typeArgument in type.typeArguments) { |
| 107 | + if (_visit(typeArgument)) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + } |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + @override |
| 115 | + bool visitTypeParameterType(TypeParameterType type) { |
| 116 | + return (type as TypeParameterTypeImpl).promotedBound != null; |
| 117 | + } |
| 118 | + |
| 119 | + bool _visit(DartType type) { |
| 120 | + return DartTypeVisitor.visit(type, this); |
| 121 | + } |
| 122 | +} |
0 commit comments