| 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 | 4 |
| 5 library fasta.kernel_function_type_alias_builder; | 5 library fasta.kernel_function_type_alias_builder; |
| 6 | 6 |
| 7 import 'package:front_end/src/fasta/util/relativize.dart'; | |
| 8 import 'package:kernel/ast.dart' | 7 import 'package:kernel/ast.dart' |
| 9 show | 8 show |
| 10 DartType, | 9 DartType, |
| 11 DynamicType, | 10 DynamicType, |
| 12 FunctionType, | 11 FunctionType, |
| 13 InvalidType, | 12 InvalidType, |
| 14 NamedType, | 13 NamedType, |
| 15 TypeParameter, | 14 TypeParameter, |
| 16 Typedef; | 15 Typedef; |
| 16 |
| 17 import 'package:kernel/type_algebra.dart' show substitute; | 17 import 'package:kernel/type_algebra.dart' show substitute; |
| 18 | 18 |
| 19 import '../messages.dart' show warning; | 19 import '../messages.dart' show warning; |
| 20 |
| 20 import 'kernel_builder.dart' | 21 import 'kernel_builder.dart' |
| 21 show | 22 show |
| 22 FormalParameterBuilder, | 23 FormalParameterBuilder, |
| 23 FunctionTypeAliasBuilder, | 24 FunctionTypeAliasBuilder, |
| 24 KernelFormalParameterBuilder, | 25 KernelFormalParameterBuilder, |
| 25 KernelLibraryBuilder, | |
| 26 KernelTypeBuilder, | 26 KernelTypeBuilder, |
| 27 KernelTypeVariableBuilder, | 27 KernelTypeVariableBuilder, |
| 28 LibraryBuilder, | 28 LibraryBuilder, |
| 29 MetadataBuilder, | 29 MetadataBuilder, |
| 30 TypeVariableBuilder, | 30 TypeVariableBuilder, |
| 31 computeDefaultTypeArguments; | 31 computeDefaultTypeArguments; |
| 32 | 32 |
| 33 class KernelFunctionTypeAliasBuilder | 33 class KernelFunctionTypeAliasBuilder |
| 34 extends FunctionTypeAliasBuilder<KernelTypeBuilder, DartType> { | 34 extends FunctionTypeAliasBuilder<KernelTypeBuilder, DartType> { |
| 35 final Typedef target; |
| 36 |
| 35 DartType thisType; | 37 DartType thisType; |
| 36 | 38 |
| 37 DartType type; | |
| 38 | |
| 39 KernelFunctionTypeAliasBuilder( | 39 KernelFunctionTypeAliasBuilder( |
| 40 List<MetadataBuilder> metadata, | 40 List<MetadataBuilder> metadata, |
| 41 KernelTypeBuilder returnType, | 41 KernelTypeBuilder returnType, |
| 42 String name, | 42 String name, |
| 43 List<TypeVariableBuilder> typeVariables, | 43 List<TypeVariableBuilder> typeVariables, |
| 44 List<FormalParameterBuilder> formals, | 44 List<FormalParameterBuilder> formals, |
| 45 LibraryBuilder parent, | 45 LibraryBuilder parent, |
| 46 int charOffset) | 46 int charOffset, |
| 47 : super(metadata, returnType, name, typeVariables, formals, parent, | 47 [Typedef target]) |
| 48 : target = target ?? |
| 49 (new Typedef(name, null, fileUri: parent.target.fileUri) |
| 50 ..fileOffset = charOffset), |
| 51 super(metadata, returnType, name, typeVariables, formals, parent, |
| 48 charOffset); | 52 charOffset); |
| 49 | 53 |
| 50 Typedef build(KernelLibraryBuilder libraryBuilder) { | 54 Typedef build(LibraryBuilder libraryBuilder) { |
| 51 DartType type = buildThisType(libraryBuilder); | 55 // TODO(ahe): We need to move type parameters from [thisType] to [target]. |
| 52 var typedef_ = new Typedef(name, type); | 56 return target..type ??= buildThisType(libraryBuilder); |
| 53 typedef_.fileUri = relativizeUri(parent.fileUri); | |
| 54 typedef_.fileOffset = charOffset; | |
| 55 return typedef_; | |
| 56 } | 57 } |
| 57 | 58 |
| 58 DartType buildThisType(LibraryBuilder library) { | 59 DartType buildThisType(LibraryBuilder library) { |
| 59 if (thisType != null) { | 60 if (thisType != null) { |
| 60 if (thisType == const InvalidType()) { | 61 if (thisType == const InvalidType()) { |
| 61 thisType = const DynamicType(); | 62 thisType = const DynamicType(); |
| 62 // TODO(ahe): Build an error somehow. | 63 // TODO(ahe): Build an error somehow. |
| 63 warning( | 64 warning( |
| 64 parent.uri, -1, "The typedef '$name' has a reference to itself."); | 65 parent.uri, -1, "The typedef '$name' has a reference to itself."); |
| 65 } | 66 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 92 for (KernelTypeVariableBuilder t in typeVariables) { | 93 for (KernelTypeVariableBuilder t in typeVariables) { |
| 93 typeParameters.add(t.parameter); | 94 typeParameters.add(t.parameter); |
| 94 } | 95 } |
| 95 } | 96 } |
| 96 return thisType = new FunctionType(positionalParameters, returnType, | 97 return thisType = new FunctionType(positionalParameters, returnType, |
| 97 namedParameters: namedParameters ?? const <NamedType>[], | 98 namedParameters: namedParameters ?? const <NamedType>[], |
| 98 typeParameters: typeParameters ?? const <TypeParameter>[], | 99 typeParameters: typeParameters ?? const <TypeParameter>[], |
| 99 requiredParameterCount: requiredParameterCount); | 100 requiredParameterCount: requiredParameterCount); |
| 100 } | 101 } |
| 101 | 102 |
| 102 DartType buildType( | |
| 103 LibraryBuilder library, List<KernelTypeBuilder> arguments) { | |
| 104 var thisType = buildThisType(library); | |
| 105 if (thisType is DynamicType) return thisType; | |
| 106 FunctionType result = thisType; | |
| 107 if (result.typeParameters.isEmpty && arguments == null) return result; | |
| 108 // Otherwise, substitute. | |
| 109 List<DartType> builtArguments = <DartType>[]; | |
| 110 if (arguments != null) { | |
| 111 for (int i = 0; i < arguments.length; i++) { | |
| 112 builtArguments.add(arguments[i].build(library)); | |
| 113 } | |
| 114 } | |
| 115 return buildTypesWithBuiltArguments(library, builtArguments); | |
| 116 } | |
| 117 | |
| 118 /// [arguments] have already been built. | 103 /// [arguments] have already been built. |
| 119 DartType buildTypesWithBuiltArguments( | 104 DartType buildTypesWithBuiltArguments( |
| 120 LibraryBuilder library, List<DartType> arguments) { | 105 LibraryBuilder library, List<DartType> arguments) { |
| 121 var thisType = buildThisType(library); | 106 var thisType = buildThisType(library); |
| 122 if (thisType is DynamicType) return thisType; | 107 if (thisType is DynamicType) return thisType; |
| 123 FunctionType result = thisType; | 108 FunctionType result = thisType; |
| 124 if (result.typeParameters.isEmpty && arguments == null) return result; | 109 if (result.typeParameters.isEmpty && arguments == null) return result; |
| 125 arguments = | 110 arguments = |
| 126 computeDefaultTypeArguments(library, result.typeParameters, arguments); | 111 computeDefaultTypeArguments(library, result.typeParameters, arguments); |
| 127 Map<TypeParameter, DartType> substitution = <TypeParameter, DartType>{}; | 112 Map<TypeParameter, DartType> substitution = <TypeParameter, DartType>{}; |
| 128 for (int i = 0; i < result.typeParameters.length; i++) { | 113 for (int i = 0; i < result.typeParameters.length; i++) { |
| 129 substitution[result.typeParameters[i]] = arguments[i]; | 114 substitution[result.typeParameters[i]] = arguments[i]; |
| 130 } | 115 } |
| 131 return substitute(result.withoutTypeParameters, substitution); | 116 return substitute(result.withoutTypeParameters, substitution); |
| 132 } | 117 } |
| 118 |
| 119 @override |
| 120 DartType buildType( |
| 121 LibraryBuilder library, List<KernelTypeBuilder> arguments) { |
| 122 var thisType = buildThisType(library); |
| 123 if (thisType is DynamicType) return thisType; |
| 124 FunctionType result = thisType; |
| 125 if (result.typeParameters.isEmpty && arguments == null) return result; |
| 126 // Otherwise, substitute. |
| 127 List<DartType> builtArguments = <DartType>[]; |
| 128 if (arguments != null) { |
| 129 for (int i = 0; i < arguments.length; i++) { |
| 130 builtArguments.add(arguments[i].build(library)); |
| 131 } |
| 132 } |
| 133 return buildTypesWithBuiltArguments(library, builtArguments); |
| 134 } |
| 133 } | 135 } |
| OLD | NEW |