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.library_builder; | 5 library fasta.library_builder; |
6 | 6 |
7 import '../combinator.dart' show Combinator; | 7 import '../combinator.dart' show Combinator; |
8 | 8 |
9 import '../errors.dart' show InputError, internalError, printUnexpected; | 9 import '../errors.dart' show InputError, internalError, printUnexpected; |
10 | 10 |
11 import '../export.dart' show Export; | 11 import '../export.dart' show Export; |
12 | 12 |
13 import '../loader.dart' show Loader; | 13 import '../loader.dart' show Loader; |
14 | 14 |
15 import '../messages.dart' show nit, warning; | 15 import '../messages.dart' show nit, warning; |
16 | 16 |
17 import '../util/relativize.dart' show relativizeUri; | 17 import '../util/relativize.dart' show relativizeUri; |
18 | 18 |
19 import 'builder.dart' | 19 import 'builder.dart' |
20 show | 20 show |
21 Builder, | 21 Builder, |
22 DynamicTypeBuilder, | 22 DynamicTypeBuilder, |
23 ClassBuilder, | 23 ClassBuilder, |
24 Scope, | 24 Scope, |
| 25 ScopeBuilder, |
25 TypeBuilder, | 26 TypeBuilder, |
26 VoidTypeBuilder; | 27 VoidTypeBuilder; |
27 | 28 |
28 abstract class LibraryBuilder<T extends TypeBuilder, R> extends Builder { | 29 abstract class LibraryBuilder<T extends TypeBuilder, R> extends Builder { |
| 30 final Scope scope; |
| 31 |
| 32 final Scope exports; |
| 33 |
| 34 final ScopeBuilder scopeBuilder; |
| 35 |
| 36 final ScopeBuilder exportScopeBuilder; |
| 37 |
29 final List<Export> exporters = <Export>[]; | 38 final List<Export> exporters = <Export>[]; |
30 | 39 |
31 final List<InputError> compileTimeErrors = <InputError>[]; | 40 final List<InputError> compileTimeErrors = <InputError>[]; |
32 | 41 |
| 42 final Uri fileUri; |
| 43 |
| 44 final String relativeFileUri; |
| 45 |
33 LibraryBuilder partOfLibrary; | 46 LibraryBuilder partOfLibrary; |
34 | 47 |
| 48 LibraryBuilder(Uri fileUri, this.scope, this.exports) |
| 49 : fileUri = fileUri, |
| 50 relativeFileUri = relativizeUri(fileUri), |
| 51 scopeBuilder = new ScopeBuilder(scope), |
| 52 exportScopeBuilder = new ScopeBuilder(exports), |
| 53 super(null, -1, fileUri); |
| 54 |
35 Loader get loader; | 55 Loader get loader; |
36 | 56 |
37 Uri get uri; | 57 Uri get uri; |
38 | 58 |
39 final Uri fileUri; | |
40 final String relativeFileUri; | |
41 | |
42 Map<String, Builder> get members; | |
43 | |
44 // TODO(ahe): Move this to SourceLibraryBuilder. | |
45 Scope get scope; | |
46 | |
47 Map<String, Builder> get exports; | |
48 | |
49 LibraryBuilder(Uri fileUri) | |
50 : fileUri = fileUri, | |
51 relativeFileUri = relativizeUri(fileUri), | |
52 super(null, -1, fileUri); | |
53 | |
54 Builder addBuilder(String name, Builder builder, int charOffset); | 59 Builder addBuilder(String name, Builder builder, int charOffset); |
55 | 60 |
56 void addExporter( | 61 void addExporter( |
57 LibraryBuilder exporter, List<Combinator> combinators, int charOffset) { | 62 LibraryBuilder exporter, List<Combinator> combinators, int charOffset) { |
58 exporters.add(new Export(exporter, this, combinators, charOffset)); | 63 exporters.add(new Export(exporter, this, combinators, charOffset)); |
59 } | 64 } |
60 | 65 |
61 void addCompileTimeError(int charOffset, Object message, | 66 void addCompileTimeError(int charOffset, Object message, |
62 {Uri fileUri, bool silent: false}) { | 67 {Uri fileUri, bool silent: false}) { |
63 fileUri ??= this.fileUri; | 68 fileUri ??= this.fileUri; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 102 |
98 /// Looks up [constructorName] in the class named [className]. It's an error | 103 /// Looks up [constructorName] in the class named [className]. It's an error |
99 /// if no such class is exported by this library, or if the class doesn't | 104 /// if no such class is exported by this library, or if the class doesn't |
100 /// have a matching constructor (or factory). | 105 /// have a matching constructor (or factory). |
101 /// | 106 /// |
102 /// If [constructorName] is null or the empty string, it's assumed to be an | 107 /// If [constructorName] is null or the empty string, it's assumed to be an |
103 /// unnamed constructor. | 108 /// unnamed constructor. |
104 Builder getConstructor(String className, | 109 Builder getConstructor(String className, |
105 {String constructorName, bool isPrivate: false}) { | 110 {String constructorName, bool isPrivate: false}) { |
106 constructorName ??= ""; | 111 constructorName ??= ""; |
107 Builder cls = (isPrivate ? members : exports)[className]; | 112 Builder cls = (isPrivate ? scope : exports).lookup(className, -1, null); |
108 if (cls is ClassBuilder) { | 113 if (cls is ClassBuilder) { |
109 // TODO(ahe): This code is similar to code in `endNewExpression` in | 114 // TODO(ahe): This code is similar to code in `endNewExpression` in |
110 // `body_builder.dart`, try to share it. | 115 // `body_builder.dart`, try to share it. |
111 Builder constructor = | 116 Builder constructor = |
112 cls.findConstructorOrFactory(constructorName, -1, null); | 117 cls.findConstructorOrFactory(constructorName, -1, null); |
113 if (constructor == null) { | 118 if (constructor == null) { |
114 // Fall-through to internal error below. | 119 // Fall-through to internal error below. |
115 } else if (constructor.isConstructor) { | 120 } else if (constructor.isConstructor) { |
116 if (!cls.isAbstract) { | 121 if (!cls.isAbstract) { |
117 return constructor; | 122 return constructor; |
118 } | 123 } |
119 } else if (constructor.isFactory) { | 124 } else if (constructor.isFactory) { |
120 return constructor; | 125 return constructor; |
121 } | 126 } |
122 } | 127 } |
123 throw internalError("Internal error: No constructor named" | 128 throw internalError("Internal error: No constructor named" |
124 " '$className::$constructorName' in '$uri'."); | 129 " '$className::$constructorName' in '$uri'."); |
125 } | 130 } |
126 | 131 |
127 int finishTypeVariables(ClassBuilder object) => 0; | 132 int finishTypeVariables(ClassBuilder object) => 0; |
128 | 133 |
129 void becomeCoreLibrary(dynamicType, voidType) { | 134 void becomeCoreLibrary(dynamicType, voidType) { |
130 addBuilder("dynamic", | 135 addBuilder("dynamic", |
131 new DynamicTypeBuilder<T, dynamic>(dynamicType, this, -1), -1); | 136 new DynamicTypeBuilder<T, dynamic>(dynamicType, this, -1), -1); |
132 addBuilder("void", new VoidTypeBuilder<T, dynamic>(voidType, this, -1), -1); | 137 addBuilder("void", new VoidTypeBuilder<T, dynamic>(voidType, this, -1), -1); |
133 } | 138 } |
134 | 139 |
135 void forEach(void f(String name, Builder builder)) { | 140 void forEach(void f(String name, Builder builder)) { |
136 members.forEach(f); | 141 scope.forEach(f); |
137 } | 142 } |
138 | 143 |
139 /// Don't use for scope lookup. Only use when an element is known to exist | 144 /// Don't use for scope lookup. Only use when an element is known to exist |
140 /// (and not a setter). | 145 /// (and not a setter). |
141 Builder operator [](String name) { | 146 Builder operator [](String name) { |
142 return members[name] ?? internalError("Not found: '$name'."); | 147 return scope.local[name] ?? internalError("Not found: '$name'."); |
143 } | 148 } |
144 | 149 |
145 Builder lookup(String name, int charOffset, Uri fileUri) { | 150 Builder lookup(String name, int charOffset, Uri fileUri) { |
146 return members[name]; | 151 return scope.lookup(name, charOffset, fileUri); |
147 } | 152 } |
148 } | 153 } |
OLD | NEW |