| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 library kernel.type_algebra; | 4 library kernel.type_algebra; |
| 5 | 5 |
| 6 import 'ast.dart'; | 6 import 'ast.dart'; |
| 7 | 7 |
| 8 /// Returns a type where all occurrences of the given type parameters have been | 8 /// Returns a type where all occurrences of the given type parameters have been |
| 9 /// replaced with the corresponding types. | 9 /// replaced with the corresponding types. |
| 10 /// | 10 /// |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 } | 166 } |
| 167 | 167 |
| 168 /// Substitutes the type parameters on the class of [type] with the | 168 /// Substitutes the type parameters on the class of [type] with the |
| 169 /// type arguments provided in [type]. | 169 /// type arguments provided in [type]. |
| 170 static Substitution fromInterfaceType(InterfaceType type) { | 170 static Substitution fromInterfaceType(InterfaceType type) { |
| 171 if (type.typeArguments.isEmpty) return _NullSubstitution.instance; | 171 if (type.typeArguments.isEmpty) return _NullSubstitution.instance; |
| 172 return fromMap(new Map<TypeParameter, DartType>.fromIterables( | 172 return fromMap(new Map<TypeParameter, DartType>.fromIterables( |
| 173 type.classNode.typeParameters, type.typeArguments)); | 173 type.classNode.typeParameters, type.typeArguments)); |
| 174 } | 174 } |
| 175 | 175 |
| 176 /// Substitutes the type parameters on the typedef of [type] with the | |
| 177 /// type arguments provided in [type]. | |
| 178 static Substitution fromTypedefType(TypedefType type) { | |
| 179 if (type.typeArguments.isEmpty) return _NullSubstitution.instance; | |
| 180 return fromMap(new Map<TypeParameter, DartType>.fromIterables( | |
| 181 type.typedefNode.typeParameters, type.typeArguments)); | |
| 182 } | |
| 183 | |
| 184 /// Substitutes the Nth parameter in [parameters] with the Nth type in | 176 /// Substitutes the Nth parameter in [parameters] with the Nth type in |
| 185 /// [types]. | 177 /// [types]. |
| 186 static Substitution fromPairs( | 178 static Substitution fromPairs( |
| 187 List<TypeParameter> parameters, List<DartType> types) { | 179 List<TypeParameter> parameters, List<DartType> types) { |
| 188 // TODO(asgerf): Investigate if it is more efficient to implement | 180 // TODO(asgerf): Investigate if it is more efficient to implement |
| 189 // substitution based on parallel pairwise lists instead of Maps. | 181 // substitution based on parallel pairwise lists instead of Maps. |
| 190 assert(parameters.length == types.length); | 182 assert(parameters.length == types.length); |
| 191 if (parameters.isEmpty) return _NullSubstitution.instance; | 183 if (parameters.isEmpty) return _NullSubstitution.instance; |
| 192 return fromMap( | 184 return fromMap( |
| 193 new Map<TypeParameter, DartType>.fromIterables(parameters, types)); | 185 new Map<TypeParameter, DartType>.fromIterables(parameters, types)); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 DartType visitVector(VectorType node) => node; | 368 DartType visitVector(VectorType node) => node; |
| 377 | 369 |
| 378 DartType visitInterfaceType(InterfaceType node) { | 370 DartType visitInterfaceType(InterfaceType node) { |
| 379 if (node.typeArguments.isEmpty) return node; | 371 if (node.typeArguments.isEmpty) return node; |
| 380 int before = useCounter; | 372 int before = useCounter; |
| 381 var typeArguments = node.typeArguments.map(visit).toList(); | 373 var typeArguments = node.typeArguments.map(visit).toList(); |
| 382 if (useCounter == before) return node; | 374 if (useCounter == before) return node; |
| 383 return new InterfaceType(node.classNode, typeArguments); | 375 return new InterfaceType(node.classNode, typeArguments); |
| 384 } | 376 } |
| 385 | 377 |
| 386 DartType visitTypedefType(TypedefType node) { | |
| 387 if (node.typeArguments.isEmpty) return node; | |
| 388 int before = useCounter; | |
| 389 var typeArguments = node.typeArguments.map(visit).toList(); | |
| 390 if (useCounter == before) return node; | |
| 391 return new TypedefType(node.typedefNode, typeArguments); | |
| 392 } | |
| 393 | |
| 394 List<TypeParameter> freshTypeParameters(List<TypeParameter> parameters) { | 378 List<TypeParameter> freshTypeParameters(List<TypeParameter> parameters) { |
| 395 if (parameters.isEmpty) return const <TypeParameter>[]; | 379 if (parameters.isEmpty) return const <TypeParameter>[]; |
| 396 return parameters.map(freshTypeParameter).toList(); | 380 return parameters.map(freshTypeParameter).toList(); |
| 397 } | 381 } |
| 398 | 382 |
| 399 TypeParameter freshTypeParameter(TypeParameter node); | 383 TypeParameter freshTypeParameter(TypeParameter node); |
| 400 | 384 |
| 401 DartType visitFunctionType(FunctionType node) { | 385 DartType visitFunctionType(FunctionType node) { |
| 402 // This is a bit tricky because we have to generate fresh type parameters | 386 // This is a bit tricky because we have to generate fresh type parameters |
| 403 // in order to change the bounds. At the same time, if the function type | 387 // in order to change the bounds. At the same time, if the function type |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 | 648 |
| 665 bool visitInvalidType(InvalidType node) => false; | 649 bool visitInvalidType(InvalidType node) => false; |
| 666 bool visitDynamicType(DynamicType node) => false; | 650 bool visitDynamicType(DynamicType node) => false; |
| 667 bool visitVoidType(VoidType node) => false; | 651 bool visitVoidType(VoidType node) => false; |
| 668 bool visitVectorType(VectorType node) => false; | 652 bool visitVectorType(VectorType node) => false; |
| 669 | 653 |
| 670 bool visitInterfaceType(InterfaceType node) { | 654 bool visitInterfaceType(InterfaceType node) { |
| 671 return node.typeArguments.any(visit); | 655 return node.typeArguments.any(visit); |
| 672 } | 656 } |
| 673 | 657 |
| 674 bool visitTypedefType(TypedefType node) { | |
| 675 return node.typeArguments.any(visit); | |
| 676 } | |
| 677 | |
| 678 bool visitFunctionType(FunctionType node) { | 658 bool visitFunctionType(FunctionType node) { |
| 679 return node.typeParameters.any(handleTypeParameter) || | 659 return node.typeParameters.any(handleTypeParameter) || |
| 680 node.positionalParameters.any(visit) || | 660 node.positionalParameters.any(visit) || |
| 681 node.namedParameters.any(visitNamedType) || | 661 node.namedParameters.any(visitNamedType) || |
| 682 visit(node.returnType); | 662 visit(node.returnType); |
| 683 } | 663 } |
| 684 | 664 |
| 685 bool visitTypeParameterType(TypeParameterType node) { | 665 bool visitTypeParameterType(TypeParameterType node) { |
| 686 return variables == null || variables.contains(node.parameter); | 666 return variables == null || variables.contains(node.parameter); |
| 687 } | 667 } |
| 688 | 668 |
| 689 bool handleTypeParameter(TypeParameter node) { | 669 bool handleTypeParameter(TypeParameter node) { |
| 690 assert(!variables.contains(node)); | 670 assert(!variables.contains(node)); |
| 691 return node.bound.accept(this); | 671 return node.bound.accept(this); |
| 692 } | 672 } |
| 693 } | 673 } |
| OLD | NEW |