| 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 13 matching lines...) Expand all Loading... |
| 24 Scope; | 24 Scope; |
| 25 | 25 |
| 26 import '../builder/builder.dart' show | 26 import '../builder/builder.dart' show |
| 27 Builder, | 27 Builder, |
| 28 ConstructorReferenceBuilder, | 28 ConstructorReferenceBuilder, |
| 29 FormalParameterBuilder, | 29 FormalParameterBuilder, |
| 30 LibraryBuilder, | 30 LibraryBuilder, |
| 31 MemberBuilder, | 31 MemberBuilder, |
| 32 MetadataBuilder, | 32 MetadataBuilder, |
| 33 PrefixBuilder, | 33 PrefixBuilder, |
| 34 ProcedureBuilder, |
| 34 TypeBuilder, | 35 TypeBuilder, |
| 35 TypeDeclarationBuilder, | 36 TypeDeclarationBuilder, |
| 36 TypeVariableBuilder, | 37 TypeVariableBuilder, |
| 37 Unhandled; | 38 Unhandled; |
| 38 | 39 |
| 39 abstract class SourceLibraryBuilder<T extends TypeBuilder, R> | 40 abstract class SourceLibraryBuilder<T extends TypeBuilder, R> |
| 40 extends LibraryBuilder<T, R> { | 41 extends LibraryBuilder<T, R> { |
| 41 final SourceLoader loader; | 42 final SourceLoader loader; |
| 42 | 43 |
| 43 final DeclarationBuilder<T> libraryDeclaration = | 44 final DeclarationBuilder<T> libraryDeclaration = |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 AsyncMarker asyncModifier, ConstructorReferenceBuilder redirectionTarget, | 190 AsyncMarker asyncModifier, ConstructorReferenceBuilder redirectionTarget, |
| 190 int charOffset); | 191 int charOffset); |
| 191 | 192 |
| 192 FormalParameterBuilder addFormalParameter( | 193 FormalParameterBuilder addFormalParameter( |
| 193 List<MetadataBuilder> metadata, int modifiers, | 194 List<MetadataBuilder> metadata, int modifiers, |
| 194 T type, String name, bool hasThis, int charOffset); | 195 T type, String name, bool hasThis, int charOffset); |
| 195 | 196 |
| 196 TypeVariableBuilder addTypeVariable(String name, T bound, int charOffset); | 197 TypeVariableBuilder addTypeVariable(String name, T bound, int charOffset); |
| 197 | 198 |
| 198 Builder addBuilder(String name, Builder builder, int charOffset) { | 199 Builder addBuilder(String name, Builder builder, int charOffset) { |
| 200 if (name.indexOf(".") != -1) { |
| 201 addCompileTimeError(charOffset, "Only constructors and factories can have" |
| 202 " names containing a period ('.'): $name"); |
| 203 } |
| 199 // TODO(ahe): Set the parent correctly here. Could then change the | 204 // TODO(ahe): Set the parent correctly here. Could then change the |
| 200 // implementation of MemberBuilder.isTopLevel to test explicitly for a | 205 // implementation of MemberBuilder.isTopLevel to test explicitly for a |
| 201 // LibraryBuilder. | 206 // LibraryBuilder. |
| 202 if (currentDeclaration == libraryDeclaration) { | 207 if (currentDeclaration == libraryDeclaration) { |
| 203 if (builder is MemberBuilder) { | 208 if (builder is MemberBuilder) { |
| 204 builder.parent = this; | 209 builder.parent = this; |
| 205 } else if (builder is TypeDeclarationBuilder) { | 210 } else if (builder is TypeDeclarationBuilder) { |
| 206 builder.parent = this; | 211 builder.parent = this; |
| 207 } else if (builder is PrefixBuilder) { | 212 } else if (builder is PrefixBuilder) { |
| 208 assert(builder.parent == this); | 213 assert(builder.parent == this); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 int typeCount = types.length; | 337 int typeCount = types.length; |
| 333 for (T t in types) { | 338 for (T t in types) { |
| 334 t.resolveIn(scope); | 339 t.resolveIn(scope); |
| 335 } | 340 } |
| 336 members.forEach((String name, Builder member) { | 341 members.forEach((String name, Builder member) { |
| 337 typeCount += member.resolveTypes(this); | 342 typeCount += member.resolveTypes(this); |
| 338 }); | 343 }); |
| 339 return typeCount; | 344 return typeCount; |
| 340 } | 345 } |
| 341 | 346 |
| 342 int convertConstructors(_) { | |
| 343 int count = 0; | |
| 344 members.forEach((String name, Builder member) { | |
| 345 count += member.convertConstructors(this); | |
| 346 }); | |
| 347 return count; | |
| 348 } | |
| 349 | |
| 350 int resolveConstructors(_) { | 347 int resolveConstructors(_) { |
| 351 int count = 0; | 348 int count = 0; |
| 352 members.forEach((String name, Builder member) { | 349 members.forEach((String name, Builder member) { |
| 353 count += member.resolveConstructors(this); | 350 count += member.resolveConstructors(this); |
| 354 }); | 351 }); |
| 355 return count; | 352 return count; |
| 356 } | 353 } |
| 354 |
| 355 List<TypeVariableBuilder> copyTypeVariables( |
| 356 List<TypeVariableBuilder> original); |
| 357 } | 357 } |
| 358 | 358 |
| 359 /// Unlike [Scope], this scope is used during construction of builders to | 359 /// Unlike [Scope], this scope is used during construction of builders to |
| 360 /// ensure types and members are added to and resolved in the correct location. | 360 /// ensure types and members are added to and resolved in the correct location. |
| 361 class DeclarationBuilder<T extends TypeBuilder> { | 361 class DeclarationBuilder<T extends TypeBuilder> { |
| 362 final DeclarationBuilder<T> parent; | 362 final DeclarationBuilder<T> parent; |
| 363 | 363 |
| 364 final Map<String, Builder> members; | 364 final Map<String, Builder> members; |
| 365 | 365 |
| 366 final List<T> types = <T>[]; | 366 final List<T> types = <T>[]; |
| 367 | 367 |
| 368 final String name; | 368 final String name; |
| 369 | 369 |
| 370 final Map<ProcedureBuilder, DeclarationBuilder<T>> factoryDeclarations = |
| 371 <ProcedureBuilder, DeclarationBuilder<T>>{}; |
| 372 |
| 370 DeclarationBuilder(this.members, this.name, [this.parent]); | 373 DeclarationBuilder(this.members, this.name, [this.parent]); |
| 371 | 374 |
| 372 void addMember(String name, MemberBuilder builder) { | 375 void addMember(String name, MemberBuilder builder) { |
| 373 if (members == null) { | 376 if (members == null) { |
| 374 parent.addMember(name, builder); | 377 parent.addMember(name, builder); |
| 375 } else { | 378 } else { |
| 376 members[name] = builder; | 379 members[name] = builder; |
| 377 } | 380 } |
| 378 } | 381 } |
| 379 | 382 |
| 380 MemberBuilder lookupMember(String name) { | 383 MemberBuilder lookupMember(String name) { |
| 381 return members == null ? parent.lookupMember(name) : members[name]; | 384 return members == null ? parent.lookupMember(name) : members[name]; |
| 382 } | 385 } |
| 383 | 386 |
| 384 void addType(T type) { | 387 void addType(T type) { |
| 385 types.add(type); | 388 types.add(type); |
| 386 } | 389 } |
| 387 | 390 |
| 388 /// Resolves type variables in [types] and propagate other types to [parent]. | 391 /// Resolves type variables in [types] and propagate other types to [parent]. |
| 389 void resolveTypes(List<TypeVariableBuilder> typeVariables) { | 392 void resolveTypes(List<TypeVariableBuilder> typeVariables, |
| 393 SourceLibraryBuilder library) { |
| 390 // TODO(ahe): The input to this method, [typeVariables], shouldn't be just | 394 // TODO(ahe): The input to this method, [typeVariables], shouldn't be just |
| 391 // type variables. It should be everything that's in scope, for example, | 395 // type variables. It should be everything that's in scope, for example, |
| 392 // members (of a class) or formal parameters (of a method). | 396 // members (of a class) or formal parameters (of a method). |
| 393 if (typeVariables == null) { | 397 if (typeVariables == null) { |
| 394 // If there are no type variables in the scope, propagate our types to be | 398 // If there are no type variables in the scope, propagate our types to be |
| 395 // resolved in the parent declaration. | 399 // resolved in the parent declaration. |
| 400 factoryDeclarations.forEach((_, DeclarationBuilder<T> declaration) { |
| 401 parent.types.addAll(declaration.types); |
| 402 }); |
| 396 parent.types.addAll(types); | 403 parent.types.addAll(types); |
| 397 } else { | 404 } else { |
| 405 factoryDeclarations.forEach( |
| 406 (ProcedureBuilder procedure, DeclarationBuilder<T> declaration) { |
| 407 assert(procedure.typeVariables.isEmpty); |
| 408 procedure.typeVariables.addAll( |
| 409 library.copyTypeVariables(typeVariables)); |
| 410 declaration.resolveTypes(procedure.typeVariables, library); |
| 411 }); |
| 398 Map<String, TypeVariableBuilder> map = <String, TypeVariableBuilder>{}; | 412 Map<String, TypeVariableBuilder> map = <String, TypeVariableBuilder>{}; |
| 399 for (TypeVariableBuilder builder in typeVariables) { | 413 for (TypeVariableBuilder builder in typeVariables) { |
| 400 map[builder.name] = builder; | 414 map[builder.name] = builder; |
| 401 } | 415 } |
| 402 for (T type in types) { | 416 for (T type in types) { |
| 403 String name = type.name; | 417 String name = type.name; |
| 404 TypeVariableBuilder builder; | 418 TypeVariableBuilder builder; |
| 405 if (name != null) { | 419 if (name != null) { |
| 406 builder = map[name]; | 420 builder = map[name]; |
| 407 } | 421 } |
| 408 if (builder == null) { | 422 if (builder == null) { |
| 409 // Since name didn't resolve in this scope, propagate it to the | 423 // Since name didn't resolve in this scope, propagate it to the |
| 410 // parent declaration. | 424 // parent declaration. |
| 411 parent.addType(type); | 425 parent.addType(type); |
| 412 } else { | 426 } else { |
| 413 type.bind(builder); | 427 type.bind(builder); |
| 414 } | 428 } |
| 415 } | 429 } |
| 416 } | 430 } |
| 417 types.clear(); | 431 types.clear(); |
| 418 } | 432 } |
| 433 |
| 434 /// Called to register [procedure] as a factory whose types are collected in |
| 435 /// [factoryDeclaration]. Later, once the class has been built, we can |
| 436 /// synthesize type variables on the factory matching the class'. |
| 437 void addFactoryDeclaration( |
| 438 ProcedureBuilder procedure, DeclarationBuilder<T> factoryDeclaration) { |
| 439 factoryDeclarations[procedure] = factoryDeclaration; |
| 440 } |
| 419 } | 441 } |
| OLD | NEW |