|
| 1 | +// Copyright (c) 2025, 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 file. |
| 4 | + |
| 5 | +import 'package:_fe_analyzer_shared/src/type_inference/type_analyzer_operations.dart'; |
| 6 | +import 'package:_fe_analyzer_shared/src/type_inference/type_constraint.dart'; |
| 7 | +import 'package:_fe_analyzer_shared/src/types/shared_type.dart'; |
| 8 | + |
| 9 | +import 'mini_ast.dart'; |
| 10 | +import 'mini_types.dart'; |
| 11 | + |
| 12 | +class TypeConstraintGatherer extends TypeConstraintGenerator<Type, |
| 13 | + NamedFunctionParameter, Var, TypeParameter, Type, String, Node> |
| 14 | + with |
| 15 | + TypeConstraintGeneratorMixin<Type, NamedFunctionParameter, Var, |
| 16 | + TypeParameter, Type, String, Node> { |
| 17 | + @override |
| 18 | + final Set<TypeParameter> typeParametersToConstrain = <TypeParameter>{}; |
| 19 | + |
| 20 | + @override |
| 21 | + final bool enableDiscrepantObliviousnessOfNullabilitySuffixOfFutureOr; |
| 22 | + |
| 23 | + @override |
| 24 | + final MiniAstOperations typeAnalyzerOperations = MiniAstOperations(); |
| 25 | + |
| 26 | + final constraints = <String>[]; |
| 27 | + |
| 28 | + TypeConstraintGatherer(Set<String> typeVariablesBeingConstrained, |
| 29 | + {this.enableDiscrepantObliviousnessOfNullabilitySuffixOfFutureOr = false}) |
| 30 | + : super(inferenceUsingBoundsIsEnabled: false) { |
| 31 | + for (var typeVariableName in typeVariablesBeingConstrained) { |
| 32 | + typeParametersToConstrain |
| 33 | + .add(TypeRegistry.addTypeParameter(typeVariableName)); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @override |
| 38 | + TypeConstraintGeneratorState get currentState => |
| 39 | + TypeConstraintGeneratorState(constraints.length); |
| 40 | + |
| 41 | + @override |
| 42 | + void addLowerConstraintForParameter(TypeParameter typeParameter, Type lower, |
| 43 | + {required Node? astNodeForTesting}) { |
| 44 | + constraints.add('$lower <: $typeParameter'); |
| 45 | + } |
| 46 | + |
| 47 | + @override |
| 48 | + void addUpperConstraintForParameter(TypeParameter typeParameter, Type upper, |
| 49 | + {required Node? astNodeForTesting}) { |
| 50 | + constraints.add('$typeParameter <: $upper'); |
| 51 | + } |
| 52 | + |
| 53 | + @override |
| 54 | + Map<TypeParameter, |
| 55 | + MergedTypeConstraint<Type, TypeParameter, Var, Type, String>> |
| 56 | + computeConstraints() { |
| 57 | + // TODO(cstefantsova): implement computeConstraints |
| 58 | + throw UnimplementedError(); |
| 59 | + } |
| 60 | + |
| 61 | + @override |
| 62 | + void eliminateTypeParametersInGeneratedConstraints( |
| 63 | + Object eliminator, TypeConstraintGeneratorState eliminationStartState, |
| 64 | + {required Node? astNodeForTesting}) { |
| 65 | + // TODO(paulberry): implement eliminateTypeParametersInGeneratedConstraints |
| 66 | + } |
| 67 | + |
| 68 | + @override |
| 69 | + List<Type>? getTypeArgumentsAsInstanceOf(Type type, String typeDeclaration) { |
| 70 | + // We just have a few cases hardcoded here to make the tests work. |
| 71 | + // TODO(paulberry): if this gets too unwieldy, replace it with a more |
| 72 | + // general implementation. |
| 73 | + switch ((type, typeDeclaration)) { |
| 74 | + case (PrimaryType(name: 'List', :var args), 'Iterable'): |
| 75 | + // List<T> inherits from Iterable<T> |
| 76 | + return args; |
| 77 | + case (PrimaryType(name: 'MyListOfInt'), 'List'): |
| 78 | + // MyListOfInt inherits from List<int> |
| 79 | + return [Type('int')]; |
| 80 | + case (PrimaryType(name: 'Future'), 'int'): |
| 81 | + case (PrimaryType(name: 'int'), 'String'): |
| 82 | + case (PrimaryType(name: 'List'), 'Future'): |
| 83 | + case (PrimaryType(name: 'String'), 'int'): |
| 84 | + case (PrimaryType(name: 'Future'), 'String'): |
| 85 | + // Unrelated types |
| 86 | + return null; |
| 87 | + default: |
| 88 | + throw UnimplementedError( |
| 89 | + 'getTypeArgumentsAsInstanceOf($type, $typeDeclaration)'); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @override |
| 94 | + ( |
| 95 | + Type, |
| 96 | + Type, { |
| 97 | + List<TypeParameter> typeParametersToEliminate |
| 98 | + }) instantiateFunctionTypesAndProvideFreshTypeParameters( |
| 99 | + SharedFunctionTypeStructure<Type, TypeParameter, NamedFunctionParameter> |
| 100 | + p, |
| 101 | + SharedFunctionTypeStructure<Type, TypeParameter, NamedFunctionParameter> |
| 102 | + q, |
| 103 | + {required bool leftSchema}) { |
| 104 | + // TODO(paulberry): implement instantiateFunctionTypesAndProvideEliminator |
| 105 | + throw UnimplementedError(); |
| 106 | + } |
| 107 | + |
| 108 | + @override |
| 109 | + void restoreState(TypeConstraintGeneratorState state) { |
| 110 | + constraints.length = state.count; |
| 111 | + } |
| 112 | +} |
0 commit comments