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_mixin_application_builder; | 5 library fasta.kernel_mixin_application_builder; |
6 | 6 |
7 import 'package:kernel/ast.dart' show InterfaceType, Supertype, setParents; | 7 import 'package:kernel/ast.dart' show InterfaceType, Supertype; |
8 | 8 |
9 import '../modifier.dart' show abstractMask; | 9 import '../errors.dart' show internalError; |
10 | |
11 import '../source/source_class_builder.dart' show SourceClassBuilder; | |
12 | 10 |
13 import '../util/relativize.dart' show relativizeUri; | 11 import '../util/relativize.dart' show relativizeUri; |
14 | 12 |
15 import 'kernel_builder.dart' | 13 import 'kernel_builder.dart' |
16 show | 14 show |
17 ConstructorReferenceBuilder, | |
18 KernelLibraryBuilder, | 15 KernelLibraryBuilder, |
19 KernelNamedTypeBuilder, | |
20 KernelTypeBuilder, | 16 KernelTypeBuilder, |
21 KernelTypeVariableBuilder, | |
22 LibraryBuilder, | 17 LibraryBuilder, |
23 MemberBuilder, | |
24 MixinApplicationBuilder, | 18 MixinApplicationBuilder, |
25 Scope, | |
26 TypeBuilder, | |
27 TypeVariableBuilder; | 19 TypeVariableBuilder; |
28 | 20 |
29 class KernelMixinApplicationBuilder | 21 class KernelMixinApplicationBuilder |
30 extends MixinApplicationBuilder<KernelTypeBuilder> | 22 extends MixinApplicationBuilder<KernelTypeBuilder> |
31 implements KernelTypeBuilder { | 23 implements KernelTypeBuilder { |
32 final int charOffset; | 24 final int charOffset; |
33 | 25 |
34 final String relativeFileUri; | 26 final String relativeFileUri; |
35 | 27 |
36 final KernelLibraryBuilder library; | 28 final KernelLibraryBuilder library; |
37 | 29 |
38 Supertype builtType; | 30 Supertype builtType; |
39 | 31 |
40 List<TypeVariableBuilder> typeVariables; | 32 List<TypeVariableBuilder> typeVariables; |
41 | 33 |
42 String subclassName; | 34 String subclassName; |
43 | 35 |
44 KernelMixinApplicationBuilder(KernelTypeBuilder supertype, | 36 KernelMixinApplicationBuilder(KernelTypeBuilder supertype, |
45 List<KernelTypeBuilder> mixins, this.library, int charOffset, Uri fileUri) | 37 List<KernelTypeBuilder> mixins, this.library, int charOffset, Uri fileUri) |
46 : charOffset = charOffset, | 38 : charOffset = charOffset, |
47 relativeFileUri = relativizeUri(fileUri), | 39 relativeFileUri = relativizeUri(fileUri), |
48 super(supertype, mixins, charOffset, fileUri); | 40 super(supertype, mixins, charOffset, fileUri); |
49 | 41 |
50 InterfaceType build(LibraryBuilder library) { | 42 InterfaceType build(LibraryBuilder library) { |
51 return buildSupertype(library)?.asInterfaceType; | 43 return internalError("Unsupported operation."); |
52 } | 44 } |
53 | 45 |
54 Supertype buildSupertype(LibraryBuilder library) { | 46 Supertype buildSupertype(LibraryBuilder library) { |
55 if (builtType != null) return builtType; | 47 return internalError("Unsupported operation."); |
56 KernelTypeBuilder s = this.supertype; | |
57 for (KernelTypeBuilder builder in mixins) { | |
58 s = applyMixin(s, builder); | |
59 } | |
60 builtType = s.buildSupertype(library); | |
61 return builtType; | |
62 } | |
63 | |
64 TypeBuilder applyMixin(TypeBuilder supertype, TypeBuilder mixin) { | |
65 KernelLibraryBuilder library = this.library.partOfLibrary ?? this.library; | |
66 List<KernelTypeBuilder> typeArguments; | |
67 List<TypeVariableBuilder> newTypeVariables; | |
68 if (typeVariables != null) { | |
69 assert(subclassName != null); | |
70 newTypeVariables = library.copyTypeVariables(typeVariables); | |
71 Map<TypeVariableBuilder, TypeBuilder> substitution = | |
72 <TypeVariableBuilder, TypeBuilder>{}; | |
73 typeArguments = <KernelTypeBuilder>[]; | |
74 for (int i = 0; i < typeVariables.length; i++) { | |
75 substitution[typeVariables[i]] = newTypeVariables[i].asTypeBuilder(); | |
76 typeArguments.add(typeVariables[i].asTypeBuilder()); | |
77 } | |
78 supertype = supertype.subst(substitution); | |
79 mixin = mixin.subst(substitution); | |
80 } | |
81 // To reduce diff against dartk, we create a different name for mixin | |
82 // applications that have free type variables. We do this by setting | |
83 // [subclassName] when setting typeVariables. | |
84 String name = subclassName != null | |
85 ? "${subclassName}^${mixin.name}" | |
86 : "${supertype.name}&${mixin.name}"; | |
87 | |
88 SourceClassBuilder cls = | |
89 library.mixinApplicationClasses.putIfAbsent(name, () { | |
90 SourceClassBuilder cls = new SourceClassBuilder( | |
91 null, | |
92 abstractMask, | |
93 name, | |
94 newTypeVariables, | |
95 supertype, | |
96 null, | |
97 new Scope(<String, MemberBuilder>{}, <String, MemberBuilder>{}, | |
98 library.scope.withTypeVariables(newTypeVariables), | |
99 isModifiable: false), | |
100 new Scope(<String, MemberBuilder>{}, null, null, isModifiable: false), | |
101 library, | |
102 <ConstructorReferenceBuilder>[], | |
103 charOffset, | |
104 null, | |
105 mixin); | |
106 library.addImplementationBuilder(name, cls, charOffset); | |
107 if (newTypeVariables != null) { | |
108 for (KernelTypeVariableBuilder t in newTypeVariables) { | |
109 cls.cls.typeParameters.add(t.parameter); | |
110 } | |
111 setParents(cls.cls.typeParameters, cls.cls); | |
112 } | |
113 return cls; | |
114 }); | |
115 return new KernelNamedTypeBuilder( | |
116 name, typeArguments, charOffset, library.fileUri) | |
117 ..builder = cls; | |
118 } | 48 } |
119 } | 49 } |
OLD | NEW |