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