Chromium Code Reviews| 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.source_library_builder; | 5 library fasta.source_library_builder; |
| 6 | 6 |
| 7 import 'package:kernel/ast.dart' show | 7 import 'package:kernel/ast.dart' show |
| 8 AsyncMarker, | 8 AsyncMarker, |
| 9 ProcedureKind; | 9 ProcedureKind; |
| 10 | 10 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 ProcedureBuilder, | 40 ProcedureBuilder, |
| 41 TypeBuilder, | 41 TypeBuilder, |
| 42 TypeDeclarationBuilder, | 42 TypeDeclarationBuilder, |
| 43 TypeVariableBuilder, | 43 TypeVariableBuilder, |
| 44 Unhandled; | 44 Unhandled; |
| 45 | 45 |
| 46 abstract class SourceLibraryBuilder<T extends TypeBuilder, R> | 46 abstract class SourceLibraryBuilder<T extends TypeBuilder, R> |
| 47 extends LibraryBuilder<T, R> { | 47 extends LibraryBuilder<T, R> { |
| 48 final SourceLoader loader; | 48 final SourceLoader loader; |
| 49 | 49 |
| 50 final Map<String, Builder> members = <String, Builder>{}; | 50 final BuilderScope<T> libraryScope = new BuilderScope<T>(<String, Builder>{}); |
| 51 | |
| 52 final List<T> types = <T>[]; | |
| 53 | 51 |
| 54 final List<ConstructorReferenceBuilder> constructorReferences = | 52 final List<ConstructorReferenceBuilder> constructorReferences = |
| 55 <ConstructorReferenceBuilder>[]; | 53 <ConstructorReferenceBuilder>[]; |
| 56 | 54 |
| 57 final List<LibraryBuilder> parts = <LibraryBuilder>[]; | 55 final List<LibraryBuilder> parts = <LibraryBuilder>[]; |
| 58 | 56 |
| 59 final List<Import> imports = <Import>[]; | 57 final List<Import> imports = <Import>[]; |
| 60 | 58 |
| 61 final Map<String, Builder> exports = <String, Builder>{}; | 59 final Map<String, Builder> exports = <String, Builder>{}; |
| 62 | 60 |
| 63 final Scope scope = new Scope(<String, Builder>{}, null, isModifiable: false); | 61 final Scope scope = new Scope(<String, Builder>{}, null, isModifiable: false); |
| 64 | 62 |
| 65 final Uri fileUri; | 63 final Uri fileUri; |
| 66 | 64 |
| 67 String name; | 65 String name; |
| 68 | 66 |
| 69 String partOf; | 67 String partOf; |
| 70 | 68 |
| 71 List<MetadataBuilder> metadata; | 69 List<MetadataBuilder> metadata; |
| 72 | 70 |
| 73 Map<String, MemberBuilder> classMembers; | 71 BuilderScope<T> innerScope; |
|
Siggi Cherem (dart-lang)
2017/02/06 18:05:11
+dartdoc:
/// Type-scope for the current builder
ahe
2017/02/08 13:31:41
Added a bit more information.
| |
| 74 | |
| 75 // TODO(ahe): Rename this. It's not just for classes. | |
| 76 List<T> classTypes; | |
| 77 | 72 |
| 78 SourceLibraryBuilder(this.loader, this.fileUri); | 73 SourceLibraryBuilder(this.loader, this.fileUri); |
| 79 | 74 |
| 80 Uri get uri; | 75 Uri get uri; |
| 81 | 76 |
| 82 bool get isPart => partOf != null; | 77 bool get isPart => partOf != null; |
| 83 | 78 |
| 79 Map<String, Builder> get members => libraryScope.members; | |
| 80 | |
| 81 List<T> get types => libraryScope.types; | |
| 82 | |
| 83 BuilderScope<T> get builderScope => innerScope ?? libraryScope; | |
|
Siggi Cherem (dart-lang)
2017/02/06 18:05:11
thinking more about it - here is an alternative id
Siggi Cherem (dart-lang)
2017/02/06 18:05:11
I'm not against this name, another idea could be `
ahe
2017/02/08 13:31:40
I like that idea. Perhaps `currentDeclaration`.
ahe
2017/02/08 16:57:21
Done in CL 2682993002.
| |
| 84 | |
| 85 Map<String, MemberBuilder> get classMembers { | |
|
Siggi Cherem (dart-lang)
2017/02/06 18:05:11
+dartdoc, when currently within a class, this retu
ahe
2017/02/08 13:31:41
Done.
| |
| 86 assert(innerScope == builderScope); | |
| 87 assert(innerScope.parent == libraryScope); | |
| 88 return builderScope.members; | |
|
Siggi Cherem (dart-lang)
2017/02/06 18:05:11
since you are anyways assuming this is only called
ahe
2017/02/08 13:31:41
Yes, but you suggestion above makes this unnecessa
| |
| 89 } | |
| 90 | |
| 91 List<T> get declarationTypes { | |
|
Siggi Cherem (dart-lang)
2017/02/06 18:05:11
+dartdoc: the type variables declared within the c
ahe
2017/02/08 13:31:41
I already have CL on top of this that removes this
| |
| 92 assert(innerScope == builderScope); | |
| 93 assert(innerScope.parent == libraryScope); | |
| 94 return builderScope.types; | |
|
Siggi Cherem (dart-lang)
2017/02/06 18:05:11
ditto => innerScope.types;
| |
| 95 } | |
| 96 | |
| 84 T addInterfaceType(String name, List<T> arguments); | 97 T addInterfaceType(String name, List<T> arguments); |
| 85 | 98 |
| 86 T addMixinApplication(T supertype, List<T> mixins); | 99 T addMixinApplication(T supertype, List<T> mixins); |
| 87 | 100 |
| 88 T addType(T type) { | 101 T addType(T type) { |
| 89 List<T> types = classTypes ?? this.types; | 102 builderScope.addType(type); |
|
Siggi Cherem (dart-lang)
2017/02/06 18:05:11
+ assert(innerScope != null) ?
ahe
2017/02/08 13:31:41
I don't think that will work right now. I don't en
| |
| 90 types.add(type); | |
| 91 return type; | 103 return type; |
| 92 } | 104 } |
| 93 | 105 |
| 94 T addVoidType(); | 106 T addVoidType(); |
| 95 | 107 |
| 96 ConstructorReferenceBuilder addConstructorReference( | 108 ConstructorReferenceBuilder addConstructorReference( |
| 97 String name, List<T> typeArguments, String suffix) { | 109 String name, List<T> typeArguments, String suffix) { |
| 98 ConstructorReferenceBuilder ref = | 110 ConstructorReferenceBuilder ref = |
| 99 new ConstructorReferenceBuilder(name, typeArguments, suffix); | 111 new ConstructorReferenceBuilder(name, typeArguments, suffix); |
| 100 constructorReferences.add(ref); | 112 constructorReferences.add(ref); |
| 101 return ref; | 113 return ref; |
| 102 } | 114 } |
| 103 | 115 |
| 104 void beginNestedScope() { | 116 void beginNestedScope({bool hasMembers}) { |
| 105 classMembers = <String, MemberBuilder>{}; | 117 innerScope = new BuilderScope(<String, MemberBuilder>{}, builderScope); |
| 106 classTypes = <T>[]; | |
| 107 } | 118 } |
| 108 | 119 |
| 109 void endNestedScope() { | 120 BuilderScope<T> endNestedScope() { |
| 110 classMembers = null; | 121 BuilderScope<T> previous = innerScope; |
| 111 classTypes = null; | 122 innerScope = innerScope.parent; |
| 123 return previous; | |
| 112 } | 124 } |
| 113 | 125 |
| 114 Uri resolve(String path) => uri.resolve(path); | 126 Uri resolve(String path) => uri.resolve(path); |
| 115 | 127 |
| 116 void addExport(List<MetadataBuilder> metadata, String uri, | 128 void addExport(List<MetadataBuilder> metadata, String uri, |
| 117 Unhandled conditionalUris, List<Combinator> combinators) { | 129 Unhandled conditionalUris, List<Combinator> combinators) { |
| 118 loader.read(resolve(uri)).addExporter(this, combinators); | 130 loader.read(resolve(uri)).addExporter(this, combinators); |
| 119 } | 131 } |
| 120 | 132 |
| 121 void addImport(List<MetadataBuilder> metadata, String uri, | 133 void addImport(List<MetadataBuilder> metadata, String uri, |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 181 FormalParameterBuilder addFormalParameter( | 193 FormalParameterBuilder addFormalParameter( |
| 182 List<MetadataBuilder> metadata, int modifiers, | 194 List<MetadataBuilder> metadata, int modifiers, |
| 183 T type, String name, bool hasThis); | 195 T type, String name, bool hasThis); |
| 184 | 196 |
| 185 TypeVariableBuilder addTypeVariable(String name, T bound); | 197 TypeVariableBuilder addTypeVariable(String name, T bound); |
| 186 | 198 |
| 187 Builder addBuilder(String name, Builder builder) { | 199 Builder addBuilder(String name, Builder builder) { |
| 188 // TODO(ahe): Set the parent correctly here. Could then change the | 200 // TODO(ahe): Set the parent correctly here. Could then change the |
| 189 // implementation of MemberBuilder.isTopLevel to test explicitly for a | 201 // implementation of MemberBuilder.isTopLevel to test explicitly for a |
| 190 // LibraryBuilder. | 202 // LibraryBuilder. |
| 191 if (classMembers == null) { | 203 if (builderScope == libraryScope) { |
| 192 if (builder is MemberBuilder) { | 204 if (builder is MemberBuilder) { |
| 193 builder.parent = this; | 205 builder.parent = this; |
| 194 } else if (builder is TypeDeclarationBuilder) { | 206 } else if (builder is TypeDeclarationBuilder) { |
| 195 builder.parent = this; | 207 builder.parent = this; |
| 196 } else if (builder is PrefixBuilder) { | 208 } else if (builder is PrefixBuilder) { |
| 197 assert(builder.parent == this); | 209 assert(builder.parent == this); |
| 198 } else { | 210 } else { |
| 199 return internalError("Unhandled: ${builder.runtimeType}"); | 211 return internalError("Unhandled: ${builder.runtimeType}"); |
| 200 } | 212 } |
| 213 } else { | |
| 214 assert(builderScope.parent == libraryScope); | |
| 201 } | 215 } |
| 202 Map<String, Builder> members = classMembers ?? this.members; | 216 Map<String, Builder> members = builderScope.members; |
| 203 Builder existing = members[name]; | 217 Builder existing = members[name]; |
| 204 builder.next = existing; | 218 builder.next = existing; |
| 205 if (builder is PrefixBuilder && existing is PrefixBuilder) { | 219 if (builder is PrefixBuilder && existing is PrefixBuilder) { |
| 206 assert(existing.next == null); | 220 assert(existing.next == null); |
| 207 builder.exports.forEach((String name, Builder builder) { | 221 builder.exports.forEach((String name, Builder builder) { |
| 208 Builder other = existing.exports.putIfAbsent(name, () => builder); | 222 Builder other = existing.exports.putIfAbsent(name, () => builder); |
| 209 if (other != builder) { | 223 if (other != builder) { |
| 210 existing.exports[name] = | 224 existing.exports[name] = |
| 211 other.combineAmbiguousImport(name, builder, this); | 225 other.combineAmbiguousImport(name, builder, this); |
| 212 } | 226 } |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 } | 347 } |
| 334 | 348 |
| 335 int resolveConstructors(_) { | 349 int resolveConstructors(_) { |
| 336 int count = 0; | 350 int count = 0; |
| 337 members.forEach((String name, Builder member) { | 351 members.forEach((String name, Builder member) { |
| 338 count += member.resolveConstructors(this); | 352 count += member.resolveConstructors(this); |
| 339 }); | 353 }); |
| 340 return count; | 354 return count; |
| 341 } | 355 } |
| 342 } | 356 } |
| 357 | |
| 358 /// Unlike [Scope], this scope is used during construction of builders to | |
| 359 /// ensure types and members are added to and resolved in the correct location. | |
| 360 class BuilderScope<T extends TypeBuilder> { | |
|
Siggi Cherem (dart-lang)
2017/02/06 18:05:11
Another idea - TypeScope or TypeBuilderScope ?
ahe
2017/02/08 13:31:41
Not a bad idea, but the class does more than handl
| |
| 361 final BuilderScope<T> parent; | |
| 362 | |
| 363 final Map<String, Builder> members; | |
| 364 | |
| 365 final List<T> types = <T>[]; | |
| 366 | |
| 367 BuilderScope(this.members, [this.parent]); | |
| 368 | |
| 369 void addMember(String name, MemberBuilder builder) { | |
| 370 if (members == null) { | |
| 371 parent.addMember(name, builder); | |
| 372 } else { | |
| 373 members[name] = builder; | |
| 374 } | |
| 375 } | |
| 376 | |
| 377 MemberBuilder lookupMember(String name) { | |
| 378 return members == null ? parent.lookupMember(name) : members[name]; | |
| 379 } | |
| 380 | |
| 381 void addType(T type) { | |
| 382 types.add(type); | |
| 383 } | |
| 384 | |
| 385 /// Resolves type variables in [types] and propagate other types to [parent]. | |
| 386 void resolveTypes(List<TypeVariableBuilder> typeVariables) { | |
| 387 if (typeVariables == null) { | |
| 388 parent.types.addAll(types); | |
|
Siggi Cherem (dart-lang)
2017/02/06 18:05:11
it might be worth adding some comments to explain
ahe
2017/02/08 16:57:21
Done.
| |
| 389 } else { | |
| 390 Map<String, TypeVariableBuilder> map = <String, TypeVariableBuilder>{}; | |
| 391 for (TypeVariableBuilder builder in typeVariables) { | |
| 392 map[builder.name] = builder; | |
| 393 } | |
| 394 for (T type in types) { | |
| 395 String name = type.name; | |
| 396 TypeVariableBuilder builder; | |
| 397 if (name != null) { | |
| 398 builder = map[name]; | |
| 399 } | |
| 400 if (builder == null) { | |
| 401 parent.addType(type); | |
|
Siggi Cherem (dart-lang)
2017/02/06 18:05:11
here too.
ahe
2017/02/08 16:57:21
Done.
| |
| 402 } else { | |
| 403 type.bind(builder); | |
| 404 } | |
| 405 } | |
| 406 } | |
| 407 types.clear(); | |
| 408 } | |
| 409 } | |
| OLD | NEW |