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.dill_library_builder; | 5 library fasta.dill_library_builder; |
6 | 6 |
7 import 'dart:convert' show JSON; | |
8 | |
9 import 'package:kernel/ast.dart' | 7 import 'package:kernel/ast.dart' |
10 show | 8 show |
11 Class, | 9 Class, |
12 Field, | 10 Field, |
13 Library, | 11 Library, |
14 ListLiteral, | 12 ListLiteral, |
15 Member, | 13 Member, |
| 14 Reference, |
16 StaticGet, | 15 StaticGet, |
17 StringLiteral, | |
18 Typedef; | 16 Typedef; |
19 | 17 |
20 import '../fasta_codes.dart' show templateUnspecified; | |
21 | |
22 import '../problems.dart' show unimplemented; | 18 import '../problems.dart' show unimplemented; |
23 | 19 |
24 import '../kernel/kernel_builder.dart' | 20 import '../kernel/kernel_builder.dart' |
25 show | 21 show |
26 Builder, | 22 Builder, |
27 InvalidTypeBuilder, | 23 InvalidTypeBuilder, |
28 KernelInvalidTypeBuilder, | 24 KernelInvalidTypeBuilder, |
29 KernelTypeBuilder, | 25 KernelTypeBuilder, |
30 LibraryBuilder, | 26 LibraryBuilder, |
31 Scope; | 27 Scope; |
32 | 28 |
33 import '../kernel/redirecting_factory_body.dart' show RedirectingFactoryBody; | 29 import '../kernel/redirecting_factory_body.dart' show RedirectingFactoryBody; |
34 | 30 |
35 import 'dill_class_builder.dart' show DillClassBuilder; | 31 import 'dill_class_builder.dart' show DillClassBuilder; |
36 | 32 |
37 import 'dill_member_builder.dart' show DillMemberBuilder; | 33 import 'dill_member_builder.dart' show DillMemberBuilder; |
38 | 34 |
39 import 'dill_loader.dart' show DillLoader; | 35 import 'dill_loader.dart' show DillLoader; |
40 | 36 |
41 import 'dill_typedef_builder.dart' show DillFunctionTypeAliasBuilder; | 37 import 'dill_typedef_builder.dart' show DillFunctionTypeAliasBuilder; |
42 | 38 |
43 class DillLibraryBuilder extends LibraryBuilder<KernelTypeBuilder, Library> { | 39 class DillLibraryBuilder extends LibraryBuilder<KernelTypeBuilder, Library> { |
44 final Uri uri; | 40 final Uri uri; |
45 | 41 |
46 final DillLoader loader; | 42 final DillLoader loader; |
47 | 43 |
48 Library library; | 44 Library library; |
49 | 45 |
50 /// Exports in addition to the members declared in this library. | 46 /// References to nodes exported by `export` declarations that: |
51 /// | 47 /// - aren't ambiguous, or |
52 /// Each entry in the list is either two or three elements long. | 48 /// - aren't hidden by local declarations. |
53 /// | 49 List<Reference> additionalExports; |
54 /// The first element is the library URI, if it is null, this is an ambiguous | |
55 /// export and the list has three elements. Otherwise the list has two | |
56 /// elements. | |
57 /// | |
58 /// The second element is the name of the exported element. | |
59 /// | |
60 /// The third element (if present) is an error message. | |
61 List<List<String>> additionalExports; | |
62 | 50 |
63 DillLibraryBuilder(this.uri, this.loader) | 51 DillLibraryBuilder(this.uri, this.loader) |
64 : super(uri, new Scope.top(), new Scope.top()); | 52 : super(uri, new Scope.top(), new Scope.top()); |
65 | 53 |
66 Uri get fileUri => uri; | 54 Uri get fileUri => uri; |
67 | 55 |
68 @override | 56 @override |
69 Library get target => library; | 57 Library get target => library; |
70 | 58 |
71 void addClass(Class cls) { | 59 void addClass(Class cls) { |
72 DillClassBuilder classBulder = new DillClassBuilder(cls, this); | 60 DillClassBuilder classBulder = new DillClassBuilder(cls, this); |
73 addBuilder(cls.name, classBulder, cls.fileOffset); | 61 addBuilder(cls.name, classBulder, cls.fileOffset); |
74 cls.procedures.forEach(classBulder.addMember); | 62 cls.procedures.forEach(classBulder.addMember); |
75 cls.constructors.forEach(classBulder.addMember); | 63 cls.constructors.forEach(classBulder.addMember); |
76 for (Field field in cls.fields) { | 64 for (Field field in cls.fields) { |
77 if (field.name.name == "_redirecting#") { | 65 if (field.name.name == "_redirecting#") { |
78 ListLiteral initializer = field.initializer; | 66 ListLiteral initializer = field.initializer; |
79 for (StaticGet get in initializer.expressions) { | 67 for (StaticGet get in initializer.expressions) { |
80 RedirectingFactoryBody.restoreFromDill(get.target); | 68 RedirectingFactoryBody.restoreFromDill(get.target); |
81 } | 69 } |
82 initializer.expressions.clear(); | 70 initializer.expressions.clear(); |
83 } else { | 71 } else { |
84 classBulder.addMember(field); | 72 classBulder.addMember(field); |
85 } | 73 } |
86 } | 74 } |
87 } | 75 } |
88 | 76 |
89 void addMember(Member member) { | 77 void addMember(Member member) { |
90 String name = member.name.name; | 78 String name = member.name.name; |
91 if (name == "_exports#") { | 79 addBuilder(name, new DillMemberBuilder(member, this), member.fileOffset); |
92 Field field = member; | |
93 StringLiteral string = field.initializer; | |
94 additionalExports = JSON.decode(string.value); | |
95 } else { | |
96 addBuilder(name, new DillMemberBuilder(member, this), member.fileOffset); | |
97 } | |
98 } | 80 } |
99 | 81 |
100 Builder addBuilder(String name, Builder builder, int charOffset) { | 82 Builder addBuilder(String name, Builder builder, int charOffset) { |
101 if (name == null || name.isEmpty) return null; | 83 if (name == null || name.isEmpty) return null; |
102 bool isSetter = builder.isSetter; | 84 bool isSetter = builder.isSetter; |
103 if (isSetter) { | 85 if (isSetter) { |
104 scopeBuilder.addSetter(name, builder); | 86 scopeBuilder.addSetter(name, builder); |
105 } else { | 87 } else { |
106 scopeBuilder.addMember(name, builder); | 88 scopeBuilder.addMember(name, builder); |
107 } | 89 } |
(...skipping 30 matching lines...) Expand all Loading... |
138 if (builder.parent == this) return builder; | 120 if (builder.parent == this) return builder; |
139 return new KernelInvalidTypeBuilder(name, charOffset, fileUri); | 121 return new KernelInvalidTypeBuilder(name, charOffset, fileUri); |
140 } | 122 } |
141 | 123 |
142 @override | 124 @override |
143 String get fullNameForErrors { | 125 String get fullNameForErrors { |
144 return library.name ?? "<library '${library.fileUri}'>"; | 126 return library.name ?? "<library '${library.fileUri}'>"; |
145 } | 127 } |
146 | 128 |
147 void finalizeExports() { | 129 void finalizeExports() { |
148 if (additionalExports != null) { | 130 for (var reference in library.additionalExports) { |
149 for (List<String> additionalExport in additionalExports) { | 131 var node = reference.node; |
150 String uriString = additionalExport[0]; | 132 Uri libraryUri; |
151 String name = additionalExport[1]; | 133 String name; |
152 Builder builder; | 134 if (node is Class) { |
153 if (uriString == null) { | 135 libraryUri = node.enclosingLibrary.importUri; |
154 builder = new KernelInvalidTypeBuilder(name, -1, null, | 136 name = node.name; |
155 templateUnspecified.withArguments(additionalExport[2])); | 137 } else if (node is Member) { |
156 } else { | 138 libraryUri = node.enclosingLibrary.importUri; |
157 DillLibraryBuilder library = loader.read(uri.resolve(uriString), -1); | 139 name = node.name.name; |
158 builder = library?.exportScopeBuilder[name] ?? | 140 } else if (node is Typedef) { |
159 new KernelInvalidTypeBuilder(name, -1, null); | 141 libraryUri = node.enclosingLibrary.importUri; |
160 } | 142 name = node.name; |
| 143 } else { |
| 144 unimplemented('${node.runtimeType}', -1, uri); |
| 145 } |
| 146 var library = loader.read(libraryUri, -1); |
| 147 Builder builder = library.exportScopeBuilder[name]; |
| 148 assert(node == builder.target); |
| 149 if (builder.isSetter) { |
| 150 exportScopeBuilder.addSetter(name, builder); |
| 151 } else { |
161 exportScopeBuilder.addMember(name, builder); | 152 exportScopeBuilder.addMember(name, builder); |
162 } | 153 } |
163 } | 154 } |
164 } | 155 } |
165 } | 156 } |
OLD | NEW |