| 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.kernel_target; | 5 library fasta.kernel_target; |
| 6 | 6 |
| 7 import 'dart:async' show Future; | 7 import 'dart:async' show Future; |
| 8 | 8 |
| 9 import 'dart:io' show File, IOSink; | 9 import 'dart:io' show File, IOSink; |
| 10 | 10 |
| 11 import 'package:kernel/ast.dart' | 11 import 'package:kernel/ast.dart' |
| 12 show | 12 show |
| 13 Arguments, | 13 Arguments, |
| 14 AsyncMarker, | 14 AsyncMarker, |
| 15 Class, | 15 Class, |
| 16 Constructor, | 16 Constructor, |
| 17 DartType, |
| 17 EmptyStatement, | 18 EmptyStatement, |
| 18 Expression, | 19 Expression, |
| 19 ExpressionStatement, | 20 ExpressionStatement, |
| 20 Field, | 21 Field, |
| 21 FieldInitializer, | 22 FieldInitializer, |
| 22 FunctionNode, | 23 FunctionNode, |
| 23 Initializer, | 24 Initializer, |
| 24 InvalidInitializer, | 25 InvalidInitializer, |
| 25 Library, | 26 Library, |
| 26 Name, | 27 Name, |
| 27 NamedExpression, | 28 NamedExpression, |
| 28 NullLiteral, | 29 NullLiteral, |
| 29 ProcedureKind, | 30 ProcedureKind, |
| 30 Program, | 31 Program, |
| 31 RedirectingInitializer, | 32 RedirectingInitializer, |
| 32 Source, | 33 Source, |
| 33 StringLiteral, | 34 StringLiteral, |
| 34 SuperInitializer, | 35 SuperInitializer, |
| 35 Throw, | 36 Throw, |
| 37 TypeParameter, |
| 36 VariableDeclaration, | 38 VariableDeclaration, |
| 37 VariableGet, | 39 VariableGet, |
| 38 VoidType; | 40 VoidType; |
| 39 | 41 |
| 40 import 'package:kernel/binary/ast_to_binary.dart' show BinaryPrinter; | 42 import 'package:kernel/binary/ast_to_binary.dart' show BinaryPrinter; |
| 41 | 43 |
| 42 import 'package:kernel/text/ast_to_text.dart' show Printer; | 44 import 'package:kernel/text/ast_to_text.dart' show Printer; |
| 43 | 45 |
| 44 import 'package:kernel/transformations/erasure.dart' show Erasure; | 46 import 'package:kernel/transformations/erasure.dart' show Erasure; |
| 45 | 47 |
| 46 import 'package:kernel/transformations/continuation.dart' as transformAsync; | 48 import 'package:kernel/transformations/continuation.dart' as transformAsync; |
| 47 | 49 |
| 48 import 'package:kernel/transformations/mixin_full_resolution.dart' | 50 import 'package:kernel/transformations/mixin_full_resolution.dart' |
| 49 show MixinFullResolution; | 51 show MixinFullResolution; |
| 50 | 52 |
| 51 import 'package:kernel/transformations/setup_builtin_library.dart' | 53 import 'package:kernel/transformations/setup_builtin_library.dart' |
| 52 as setup_builtin_library; | 54 as setup_builtin_library; |
| 53 | 55 |
| 56 import 'package:kernel/type_algebra.dart' show substitute; |
| 57 |
| 54 import '../source/source_loader.dart' show SourceLoader; | 58 import '../source/source_loader.dart' show SourceLoader; |
| 55 | 59 |
| 56 import '../source/source_class_builder.dart' show SourceClassBuilder; | 60 import '../source/source_class_builder.dart' show SourceClassBuilder; |
| 57 | 61 |
| 58 import '../target_implementation.dart' show TargetImplementation; | 62 import '../target_implementation.dart' show TargetImplementation; |
| 59 | 63 |
| 60 import '../translate_uri.dart' show TranslateUri; | 64 import '../translate_uri.dart' show TranslateUri; |
| 61 | 65 |
| 62 import '../dill/dill_target.dart' show DillTarget; | 66 import '../dill/dill_target.dart' show DillTarget; |
| 63 | 67 |
| 64 import '../errors.dart' | 68 import '../errors.dart' |
| 65 show InputError, internalError, reportCrash, resetCrashReporting; | 69 show InputError, internalError, reportCrash, resetCrashReporting; |
| 66 | 70 |
| 67 import '../util/relativize.dart' show relativizeUri; | 71 import '../util/relativize.dart' show relativizeUri; |
| 68 | 72 |
| 69 import '../compiler_context.dart' show CompilerContext; | 73 import '../compiler_context.dart' show CompilerContext; |
| 70 | 74 |
| 71 import 'kernel_builder.dart' | 75 import 'kernel_builder.dart' |
| 72 show | 76 show |
| 73 Builder, | 77 Builder, |
| 74 ClassBuilder, | 78 ClassBuilder, |
| 75 KernelClassBuilder, | 79 KernelClassBuilder, |
| 76 KernelLibraryBuilder, | 80 KernelLibraryBuilder, |
| 77 KernelNamedTypeBuilder, | 81 KernelNamedTypeBuilder, |
| 78 KernelProcedureBuilder, | 82 KernelProcedureBuilder, |
| 79 LibraryBuilder, | 83 LibraryBuilder, |
| 80 MixinApplicationBuilder, | 84 MixinApplicationBuilder, |
| 81 NamedMixinApplicationBuilder, | 85 NamedMixinApplicationBuilder, |
| 82 NamedTypeBuilder, | 86 NamedTypeBuilder, |
| 83 TypeBuilder; | 87 TypeBuilder, |
| 88 TypeVariableBuilder; |
| 84 | 89 |
| 85 class KernelTarget extends TargetImplementation { | 90 class KernelTarget extends TargetImplementation { |
| 86 final DillTarget dillTarget; | 91 final DillTarget dillTarget; |
| 87 | 92 |
| 88 /// Shared with [CompilerContext]. | 93 /// Shared with [CompilerContext]. |
| 89 final Map<String, Source> uriToSource; | 94 final Map<String, Source> uriToSource; |
| 90 | 95 |
| 91 SourceLoader<Library> loader; | 96 SourceLoader<Library> loader; |
| 92 Program program; | 97 Program program; |
| 93 | 98 |
| 94 final List errors = []; | 99 final List errors = []; |
| 95 | 100 |
| 101 final TypeBuilder dynamicType = |
| 102 new KernelNamedTypeBuilder("dynamic", null, -1, null); |
| 103 |
| 96 KernelTarget(DillTarget dillTarget, TranslateUri uriTranslator, | 104 KernelTarget(DillTarget dillTarget, TranslateUri uriTranslator, |
| 97 [Map<String, Source> uriToSource]) | 105 [Map<String, Source> uriToSource]) |
| 98 : dillTarget = dillTarget, | 106 : dillTarget = dillTarget, |
| 99 uriToSource = uriToSource ?? CompilerContext.current.uriToSource, | 107 uriToSource = uriToSource ?? CompilerContext.current.uriToSource, |
| 100 super(dillTarget.ticker, uriTranslator) { | 108 super(dillTarget.ticker, uriTranslator) { |
| 101 resetCrashReporting(); | 109 resetCrashReporting(); |
| 102 loader = createLoader(); | 110 loader = createLoader(); |
| 103 } | 111 } |
| 104 | 112 |
| 105 SourceLoader<Library> createLoader() => new SourceLoader<Library>(this); | 113 SourceLoader<Library> createLoader() => new SourceLoader<Library>(this); |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 return loader.coreLibrary.exports["Object"]; | 390 return loader.coreLibrary.exports["Object"]; |
| 383 } | 391 } |
| 384 | 392 |
| 385 Class get objectClass => objectClassBuilder.cls; | 393 Class get objectClass => objectClassBuilder.cls; |
| 386 | 394 |
| 387 /// If [builder] doesn't have a constructors, install the defaults. | 395 /// If [builder] doesn't have a constructors, install the defaults. |
| 388 void installDefaultConstructor(SourceClassBuilder builder) { | 396 void installDefaultConstructor(SourceClassBuilder builder) { |
| 389 if (builder.isMixinApplication || builder.constructors.isNotEmpty) return; | 397 if (builder.isMixinApplication || builder.constructors.isNotEmpty) return; |
| 390 | 398 |
| 391 /// Quotes below are from [Dart Programming Language Specification, 4th | 399 /// Quotes below are from [Dart Programming Language Specification, 4th |
| 392 /// Edition](http://www.ecma-international.org/publications/files/ECMA-ST/EC
MA-408.pdf): | 400 /// Edition]( |
| 401 /// http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-408.pd
f): |
| 393 if (builder is NamedMixinApplicationBuilder) { | 402 if (builder is NamedMixinApplicationBuilder) { |
| 394 /// >A mixin application of the form S with M; defines a class C with | 403 /// >A mixin application of the form S with M; defines a class C with |
| 395 /// >superclass S. | 404 /// >superclass S. |
| 396 /// >... | 405 /// >... |
| 397 | 406 |
| 398 /// >Let LM be the library in which M is declared. For each generative | 407 /// >Let LM be the library in which M is declared. For each generative |
| 399 /// >constructor named qi(Ti1 ai1, . . . , Tiki aiki), i in 1..n of S | 408 /// >constructor named qi(Ti1 ai1, . . . , Tiki aiki), i in 1..n of S |
| 400 /// >that is accessible to LM , C has an implicitly declared constructor | 409 /// >that is accessible to LM , C has an implicitly declared constructor |
| 401 /// >named q'i = [C/S]qi of the form q'i(ai1,...,aiki) : | 410 /// >named q'i = [C/S]qi of the form q'i(ai1,...,aiki) : |
| 402 /// >super(ai1,...,aiki);. | 411 /// >super(ai1,...,aiki);. |
| 403 Builder supertype = builder; | 412 Builder supertype = builder; |
| 404 while (supertype is NamedMixinApplicationBuilder) { | 413 while (supertype is NamedMixinApplicationBuilder) { |
| 405 NamedMixinApplicationBuilder named = supertype; | 414 NamedMixinApplicationBuilder named = supertype; |
| 406 TypeBuilder type = named.mixinApplication; | 415 TypeBuilder type = named.mixinApplication; |
| 407 if (type is MixinApplicationBuilder) { | 416 if (type is MixinApplicationBuilder) { |
| 408 MixinApplicationBuilder t = type; | 417 MixinApplicationBuilder t = type; |
| 409 type = t.supertype; | 418 type = t.supertype; |
| 410 } | 419 } |
| 411 if (type is NamedTypeBuilder) { | 420 if (type is NamedTypeBuilder) { |
| 412 supertype = type.builder; | 421 supertype = type.builder; |
| 413 } else { | 422 } else { |
| 414 internalError("Unhandled: ${type.runtimeType}"); | 423 internalError("Unhandled: ${type.runtimeType}"); |
| 415 } | 424 } |
| 416 } | 425 } |
| 417 if (supertype is KernelClassBuilder) { | 426 if (supertype is KernelClassBuilder) { |
| 427 Map<TypeParameter, DartType> substitutionMap = |
| 428 computeKernelSubstitutionMap( |
| 429 builder.getSubstitutionMap(supertype, builder.fileUri, |
| 430 builder.charOffset, dynamicType), |
| 431 builder.parent); |
| 418 for (Constructor constructor in supertype.cls.constructors) { | 432 for (Constructor constructor in supertype.cls.constructors) { |
| 419 builder.addSyntheticConstructor( | 433 builder.addSyntheticConstructor(makeMixinApplicationConstructor( |
| 420 makeMixinApplicationConstructor(builder.cls.mixin, constructor)); | 434 builder.cls.mixin, constructor, substitutionMap)); |
| 421 } | 435 } |
| 422 } else { | 436 } else { |
| 423 internalError("Unhandled: ${supertype.runtimeType}"); | 437 internalError("Unhandled: ${supertype.runtimeType}"); |
| 424 } | 438 } |
| 425 } else { | 439 } else { |
| 426 /// >Iff no constructor is specified for a class C, it implicitly has a | 440 /// >Iff no constructor is specified for a class C, it implicitly has a |
| 427 /// >default constructor C() : super() {}, unless C is class Object. | 441 /// >default constructor C() : super() {}, unless C is class Object. |
| 428 // The superinitializer is installed below in [finishConstructors]. | 442 // The superinitializer is installed below in [finishConstructors]. |
| 429 builder.addSyntheticConstructor(makeDefaultConstructor()); | 443 builder.addSyntheticConstructor(makeDefaultConstructor()); |
| 430 } | 444 } |
| 431 } | 445 } |
| 432 | 446 |
| 433 Constructor makeMixinApplicationConstructor( | 447 Map<TypeParameter, DartType> computeKernelSubstitutionMap( |
| 434 Class mixin, Constructor constructor) { | 448 Map<TypeVariableBuilder, TypeBuilder> substitutionMap, |
| 449 LibraryBuilder library) { |
| 450 if (substitutionMap == null) return const <TypeParameter, DartType>{}; |
| 451 Map<TypeParameter, DartType> result = <TypeParameter, DartType>{}; |
| 452 substitutionMap |
| 453 .forEach((TypeVariableBuilder variable, TypeBuilder argument) { |
| 454 result[variable.target] = argument.build(library); |
| 455 }); |
| 456 return result; |
| 457 } |
| 458 |
| 459 Constructor makeMixinApplicationConstructor(Class mixin, |
| 460 Constructor constructor, Map<TypeParameter, DartType> substitutionMap) { |
| 435 VariableDeclaration copyFormal(VariableDeclaration formal) { | 461 VariableDeclaration copyFormal(VariableDeclaration formal) { |
| 436 // TODO(ahe): Handle initializers. | 462 // TODO(ahe): Handle initializers. |
| 437 return new VariableDeclaration(formal.name, | 463 return new VariableDeclaration(formal.name, |
| 438 type: formal.type, isFinal: formal.isFinal, isConst: formal.isConst); | 464 type: substitute(formal.type, substitutionMap), |
| 465 isFinal: formal.isFinal, |
| 466 isConst: formal.isConst); |
| 439 } | 467 } |
| 440 | 468 |
| 441 List<VariableDeclaration> positionalParameters = <VariableDeclaration>[]; | 469 List<VariableDeclaration> positionalParameters = <VariableDeclaration>[]; |
| 442 List<VariableDeclaration> namedParameters = <VariableDeclaration>[]; | 470 List<VariableDeclaration> namedParameters = <VariableDeclaration>[]; |
| 443 List<Expression> positional = <Expression>[]; | 471 List<Expression> positional = <Expression>[]; |
| 444 List<NamedExpression> named = <NamedExpression>[]; | 472 List<NamedExpression> named = <NamedExpression>[]; |
| 445 for (VariableDeclaration formal | 473 for (VariableDeclaration formal |
| 446 in constructor.function.positionalParameters) { | 474 in constructor.function.positionalParameters) { |
| 447 positionalParameters.add(copyFormal(formal)); | 475 positionalParameters.add(copyFormal(formal)); |
| 448 positional.add(new VariableGet(positionalParameters.last)); | 476 positional.add(new VariableGet(positionalParameters.last)); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 } | 632 } |
| 605 for (Constructor constructor in superclass.constructors) { | 633 for (Constructor constructor in superclass.constructors) { |
| 606 if (constructor.name.name.isEmpty) { | 634 if (constructor.name.name.isEmpty) { |
| 607 return constructor.function.requiredParameterCount == 0 | 635 return constructor.function.requiredParameterCount == 0 |
| 608 ? constructor | 636 ? constructor |
| 609 : null; | 637 : null; |
| 610 } | 638 } |
| 611 } | 639 } |
| 612 return null; | 640 return null; |
| 613 } | 641 } |
| OLD | NEW |