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 |
176 /// Substitutes the Nth parameter in [parameters] with the Nth type in | 184 /// Substitutes the Nth parameter in [parameters] with the Nth type in |
177 /// [types]. | 185 /// [types]. |
178 static Substitution fromPairs( | 186 static Substitution fromPairs( |
179 List<TypeParameter> parameters, List<DartType> types) { | 187 List<TypeParameter> parameters, List<DartType> types) { |
180 // TODO(asgerf): Investigate if it is more efficient to implement | 188 // TODO(asgerf): Investigate if it is more efficient to implement |
181 // substitution based on parallel pairwise lists instead of Maps. | 189 // substitution based on parallel pairwise lists instead of Maps. |
182 assert(parameters.length == types.length); | 190 assert(parameters.length == types.length); |
183 if (parameters.isEmpty) return _NullSubstitution.instance; | 191 if (parameters.isEmpty) return _NullSubstitution.instance; |
184 return fromMap( | 192 return fromMap( |
185 new Map<TypeParameter, DartType>.fromIterables(parameters, types)); | 193 new Map<TypeParameter, DartType>.fromIterables(parameters, types)); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 DartType visitVector(VectorType node) => node; | 376 DartType visitVector(VectorType node) => node; |
369 | 377 |
370 DartType visitInterfaceType(InterfaceType node) { | 378 DartType visitInterfaceType(InterfaceType node) { |
371 if (node.typeArguments.isEmpty) return node; | 379 if (node.typeArguments.isEmpty) return node; |
372 int before = useCounter; | 380 int before = useCounter; |
373 var typeArguments = node.typeArguments.map(visit).toList(); | 381 var typeArguments = node.typeArguments.map(visit).toList(); |
374 if (useCounter == before) return node; | 382 if (useCounter == before) return node; |
375 return new InterfaceType(node.classNode, typeArguments); | 383 return new InterfaceType(node.classNode, typeArguments); |
376 } | 384 } |
377 | 385 |
| 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 |
378 List<TypeParameter> freshTypeParameters(List<TypeParameter> parameters) { | 394 List<TypeParameter> freshTypeParameters(List<TypeParameter> parameters) { |
379 if (parameters.isEmpty) return const <TypeParameter>[]; | 395 if (parameters.isEmpty) return const <TypeParameter>[]; |
380 return parameters.map(freshTypeParameter).toList(); | 396 return parameters.map(freshTypeParameter).toList(); |
381 } | 397 } |
382 | 398 |
383 TypeParameter freshTypeParameter(TypeParameter node); | 399 TypeParameter freshTypeParameter(TypeParameter node); |
384 | 400 |
385 DartType visitFunctionType(FunctionType node) { | 401 DartType visitFunctionType(FunctionType node) { |
386 // This is a bit tricky because we have to generate fresh type parameters | 402 // This is a bit tricky because we have to generate fresh type parameters |
387 // in order to change the bounds. At the same time, if the function type | 403 // 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... |
648 | 664 |
649 bool visitInvalidType(InvalidType node) => false; | 665 bool visitInvalidType(InvalidType node) => false; |
650 bool visitDynamicType(DynamicType node) => false; | 666 bool visitDynamicType(DynamicType node) => false; |
651 bool visitVoidType(VoidType node) => false; | 667 bool visitVoidType(VoidType node) => false; |
652 bool visitVectorType(VectorType node) => false; | 668 bool visitVectorType(VectorType node) => false; |
653 | 669 |
654 bool visitInterfaceType(InterfaceType node) { | 670 bool visitInterfaceType(InterfaceType node) { |
655 return node.typeArguments.any(visit); | 671 return node.typeArguments.any(visit); |
656 } | 672 } |
657 | 673 |
| 674 bool visitTypedefType(TypedefType node) { |
| 675 return node.typeArguments.any(visit); |
| 676 } |
| 677 |
658 bool visitFunctionType(FunctionType node) { | 678 bool visitFunctionType(FunctionType node) { |
659 return node.typeParameters.any(handleTypeParameter) || | 679 return node.typeParameters.any(handleTypeParameter) || |
660 node.positionalParameters.any(visit) || | 680 node.positionalParameters.any(visit) || |
661 node.namedParameters.any(visitNamedType) || | 681 node.namedParameters.any(visitNamedType) || |
662 visit(node.returnType); | 682 visit(node.returnType); |
663 } | 683 } |
664 | 684 |
665 bool visitTypeParameterType(TypeParameterType node) { | 685 bool visitTypeParameterType(TypeParameterType node) { |
666 return variables == null || variables.contains(node.parameter); | 686 return variables == null || variables.contains(node.parameter); |
667 } | 687 } |
668 | 688 |
669 bool handleTypeParameter(TypeParameter node) { | 689 bool handleTypeParameter(TypeParameter node) { |
670 assert(!variables.contains(node)); | 690 assert(!variables.contains(node)); |
671 return node.bound.accept(this); | 691 return node.bound.accept(this); |
672 } | 692 } |
673 } | 693 } |
OLD | NEW |