Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: pkg/front_end/lib/src/fasta/kernel/kernel_mixin_application_builder.dart

Issue 2689303003: Implement type variables in mixin applications. (Closed)
Patch Set: Restore duplication handling and set mixedInType. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 7 import 'package:kernel/ast.dart' show
8 Class,
9 InterfaceType, 8 InterfaceType,
10 Supertype; 9 Supertype,
10 setParents;
11
12 import '../modifier.dart' show
13 abstractMask;
11 14
12 import 'kernel_builder.dart' show 15 import 'kernel_builder.dart' show
16 Builder,
17 ConstructorReferenceBuilder,
18 KernelLibraryBuilder,
19 KernelNamedTypeBuilder,
13 KernelTypeBuilder, 20 KernelTypeBuilder,
14 MixinApplicationBuilder; 21 KernelTypeVariableBuilder,
22 MixinApplicationBuilder,
23 TypeBuilder,
24 TypeVariableBuilder;
15 25
16 import '../util/relativize.dart' show 26 import '../util/relativize.dart' show
17 relativizeUri; 27 relativizeUri;
18 28
29 import '../source/source_class_builder.dart' show
30 SourceClassBuilder;
31
19 class KernelMixinApplicationBuilder 32 class KernelMixinApplicationBuilder
20 extends MixinApplicationBuilder<KernelTypeBuilder> 33 extends MixinApplicationBuilder<KernelTypeBuilder>
21 implements KernelTypeBuilder { 34 implements KernelTypeBuilder {
22 final int charOffset; 35 final int charOffset;
23 36
24 final String relativeFileUri; 37 final String relativeFileUri;
25 38
39 final KernelLibraryBuilder library;
40
26 Supertype builtType; 41 Supertype builtType;
27 42
43 List<TypeVariableBuilder> typeVariables;
44
45 /// If this mixin application uses type variables, it needs a unique name
46 /// based on its subclass.
47 //
48 // TODO(ahe): This is to reduce diff against dartk. Consider if this is
49 // necessary.
50 String subclassName;
karlklose 2017/02/15 09:09:30 Could you move (or copy) this documentation to `Mi
ahe 2017/02/15 09:36:50 Done.
51
28 KernelMixinApplicationBuilder(KernelTypeBuilder supertype, 52 KernelMixinApplicationBuilder(KernelTypeBuilder supertype,
29 List<KernelTypeBuilder> mixins, int charOffset, Uri fileUri) 53 List<KernelTypeBuilder> mixins, this.library, int charOffset, Uri fileUri)
30 : charOffset = charOffset, 54 : charOffset = charOffset,
31 relativeFileUri = relativizeUri(fileUri), 55 relativeFileUri = relativizeUri(fileUri),
32 super(supertype, mixins, charOffset, fileUri); 56 super(supertype, mixins, charOffset, fileUri);
33 57
34 InterfaceType build() => buildSupertype().asInterfaceType; 58 InterfaceType build() => buildSupertype().asInterfaceType;
35 59
36 Supertype buildSupertype() { 60 Supertype buildSupertype() {
37 if (builtType != null) return builtType; 61 if (builtType != null) return builtType;
38 Supertype supertype = 62 KernelTypeBuilder s = this.supertype;
39 this.supertype.buildSupertype()?.classNode?.asRawSupertype; 63 for (KernelTypeBuilder builder in mixins) {
40 if (supertype == null) { 64 s = applyMixin(s, builder);
41 return null;
42 } 65 }
43 for (KernelTypeBuilder builder in mixins) { 66 builtType = s.buildSupertype();
44 Supertype mixin = builder.buildSupertype()?.classNode?.asRawSupertype;
45 if (mixin == null) {
46 return null;
47 }
48 Class application = new Class(
49 name: "${supertype.classNode.name}&${mixin.classNode.name}",
50 isAbstract: true,
51 supertype: supertype,
52 mixedInType: mixin,
53 typeParameters: null, // TODO(ahe): Compute these.
54 fileUri: relativeFileUri);
55 application.fileOffset = charOffset;
56 // TODO(ahe): Use asThisSupertype instead and translate type variables.
57 supertype = application.asRawSupertype;
58 }
59 builtType = supertype;
60 return builtType; 67 return builtType;
61 } 68 }
69
70 TypeBuilder applyMixin(TypeBuilder supertype, TypeBuilder mixin) {
71 KernelLibraryBuilder library = this.library.partOfLibrary ?? this.library;
72 List<TypeVariableBuilder> newTypeVariables;
73 List<KernelTypeBuilder> typeArguments;
74 if (typeVariables != null) {
75 newTypeVariables = library.copyTypeVariables(typeVariables);
76 Map<TypeVariableBuilder, TypeBuilder> substitution =
77 <TypeVariableBuilder, TypeBuilder>{};
78 typeArguments = <KernelTypeBuilder>[];
79 for (int i = 0; i < typeVariables.length; i++) {
80 substitution[typeVariables[i]] = newTypeVariables[i].asTypeBuilder();
81 typeArguments.add(typeVariables[i].asTypeBuilder());
82 }
83 supertype = supertype.subst(substitution);
84 mixin = mixin.subst(substitution);
85 }
86 String name = subclassName != null
karlklose 2017/02/15 09:09:30 Could you add a comment on when these cases happen
ahe 2017/02/15 09:36:50 Done.
87 ? "${subclassName}^${mixin.name}"
88 : "${supertype.name}&${mixin.name}";
89 SourceClassBuilder cls = new SourceClassBuilder(null, abstractMask, name,
90 newTypeVariables, supertype, null, <String, Builder>{}, library,
91 <ConstructorReferenceBuilder>[], charOffset, null, mixin);
92 library.addImplementationBuilder(name, cls, charOffset);
93 if (newTypeVariables != null) {
94 for (KernelTypeVariableBuilder t in newTypeVariables) {
95 cls.cls.typeParameters.add(t.parameter);
96 }
97 setParents(cls.cls.typeParameters, cls.cls);
98 }
99 return new KernelNamedTypeBuilder(name, typeArguments, charOffset,
100 library.fileUri)
101 ..builder = cls;
102 }
62 } 103 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698