| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 part of world_builder; | 5 part of world_builder; |
| 6 | 6 |
| 7 /// World builder specific to codegen. | 7 /// World builder specific to codegen. |
| 8 /// | 8 /// |
| 9 /// This adds additional access to liveness of selectors and elements. | 9 /// This adds additional access to liveness of selectors and elements. |
| 10 abstract class CodegenWorldBuilder implements WorldBuilder { | 10 abstract class CodegenWorldBuilder implements WorldBuilder { |
| 11 /// Calls [f] with every instance field, together with its declarer, in an | 11 /// Calls [f] with every instance field, together with its declarer, in an |
| 12 /// instance of [cls]. | 12 /// instance of [cls]. |
| 13 void forEachInstanceField( | 13 void forEachInstanceField( |
| 14 ClassEntity cls, void f(ClassEntity declarer, FieldEntity field)); | 14 ClassEntity cls, void f(ClassEntity declarer, FieldEntity field)); |
| 15 | 15 |
| 16 /// Calls [f] for each parameter of [function] providing the type and name of | 16 /// Calls [f] for each parameter of [function] providing the type and name of |
| 17 /// the parameter. | 17 /// the parameter and the [defaultValue] if the parameter is optional. |
| 18 void forEachParameter( | 18 void forEachParameter(FunctionEntity function, |
| 19 FunctionEntity function, void f(DartType type, String name)); | 19 void f(DartType type, String name, ConstantValue defaultValue)); |
| 20 | 20 |
| 21 void forEachInvokedName( | 21 void forEachInvokedName( |
| 22 f(String name, Map<Selector, SelectorConstraints> selectors)); | 22 f(String name, Map<Selector, SelectorConstraints> selectors)); |
| 23 | 23 |
| 24 void forEachInvokedGetter( | 24 void forEachInvokedGetter( |
| 25 f(String name, Map<Selector, SelectorConstraints> selectors)); | 25 f(String name, Map<Selector, SelectorConstraints> selectors)); |
| 26 | 26 |
| 27 void forEachInvokedSetter( | 27 void forEachInvokedSetter( |
| 28 f(String name, Map<Selector, SelectorConstraints> selectors)); | 28 f(String name, Map<Selector, SelectorConstraints> selectors)); |
| 29 | 29 |
| 30 /// Returns `true` if [field] constant or final with a constant initializer. |
| 31 bool hasConstantFieldInitializer(FieldEntity field); |
| 32 |
| 30 /// Returns `true` if [member] is invoked as a setter. | 33 /// Returns `true` if [member] is invoked as a setter. |
| 31 bool hasInvokedSetter(MemberEntity member, ClosedWorld world); | 34 bool hasInvokedSetter(MemberEntity member, ClosedWorld world); |
| 32 | 35 |
| 33 bool hasInvokedGetter(MemberEntity member, ClosedWorld world); | 36 bool hasInvokedGetter(MemberEntity member, ClosedWorld world); |
| 34 | 37 |
| 35 Map<Selector, SelectorConstraints> invocationsByName(String name); | 38 Map<Selector, SelectorConstraints> invocationsByName(String name); |
| 36 | 39 |
| 37 Map<Selector, SelectorConstraints> getterInvocationsByName(String name); | 40 Map<Selector, SelectorConstraints> getterInvocationsByName(String name); |
| 38 | 41 |
| 39 Map<Selector, SelectorConstraints> setterInvocationsByName(String name); | 42 Map<Selector, SelectorConstraints> setterInvocationsByName(String name); |
| (...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 /// the constant use was new to the world. | 538 /// the constant use was new to the world. |
| 536 bool registerConstantUse(ConstantUse use) { | 539 bool registerConstantUse(ConstantUse use) { |
| 537 if (use.kind == ConstantUseKind.DIRECT) { | 540 if (use.kind == ConstantUseKind.DIRECT) { |
| 538 addCompileTimeConstantForEmission(use.value); | 541 addCompileTimeConstantForEmission(use.value); |
| 539 } | 542 } |
| 540 return _constantValues.add(use.value); | 543 return _constantValues.add(use.value); |
| 541 } | 544 } |
| 542 } | 545 } |
| 543 | 546 |
| 544 class ElementCodegenWorldBuilderImpl extends CodegenWorldBuilderImpl { | 547 class ElementCodegenWorldBuilderImpl extends CodegenWorldBuilderImpl { |
| 548 final JavaScriptConstantCompiler _constants; |
| 549 |
| 545 ElementCodegenWorldBuilderImpl( | 550 ElementCodegenWorldBuilderImpl( |
| 551 this._constants, |
| 546 ElementEnvironment elementEnvironment, | 552 ElementEnvironment elementEnvironment, |
| 547 NativeBasicData nativeBasicData, | 553 NativeBasicData nativeBasicData, |
| 548 ClosedWorld world, | 554 ClosedWorld world, |
| 549 SelectorConstraintsStrategy selectorConstraintsStrategy) | 555 SelectorConstraintsStrategy selectorConstraintsStrategy) |
| 550 : super(elementEnvironment, nativeBasicData, world, | 556 : super(elementEnvironment, nativeBasicData, world, |
| 551 selectorConstraintsStrategy); | 557 selectorConstraintsStrategy); |
| 552 | 558 |
| 559 @override |
| 560 bool hasConstantFieldInitializer(FieldElement field) { |
| 561 return field.constant != null; |
| 562 } |
| 563 |
| 553 /// Calls [f] with every instance field, together with its declarer, in an | 564 /// Calls [f] with every instance field, together with its declarer, in an |
| 554 /// instance of [cls]. | 565 /// instance of [cls]. |
| 555 void forEachInstanceField( | 566 void forEachInstanceField( |
| 556 ClassElement cls, void f(ClassEntity declarer, FieldEntity field)) { | 567 ClassElement cls, void f(ClassEntity declarer, FieldEntity field)) { |
| 557 cls.implementation | 568 cls.implementation |
| 558 .forEachInstanceField(f, includeSuperAndInjectedMembers: true); | 569 .forEachInstanceField(f, includeSuperAndInjectedMembers: true); |
| 559 } | 570 } |
| 560 | 571 |
| 561 @override | 572 @override |
| 562 void forEachParameter( | 573 void forEachParameter(MethodElement function, |
| 563 MethodElement function, void f(DartType type, String name)) { | 574 void f(DartType type, String name, ConstantValue defaultValue)) { |
| 564 FunctionSignature parameters = function.functionSignature; | 575 FunctionSignature parameters = function.functionSignature; |
| 565 parameters.forEachParameter((ParameterElement parameter) { | 576 parameters.forEachParameter((ParameterElement parameter) { |
| 566 f(parameter.type, parameter.name); | 577 ConstantValue value; |
| 578 if (parameter.constant != null) { |
| 579 value = _constants.getConstantValue(parameter.constant); |
| 580 } else { |
| 581 value = new NullConstantValue(); |
| 582 } |
| 583 f(parameter.type, parameter.name, value); |
| 567 }); | 584 }); |
| 568 } | 585 } |
| 569 | 586 |
| 570 @override | 587 @override |
| 571 void _processInstantiatedClassMember( | 588 void _processInstantiatedClassMember( |
| 572 ClassEntity cls, MemberElement member, MemberUsedCallback memberUsed) { | 589 ClassEntity cls, MemberElement member, MemberUsedCallback memberUsed) { |
| 573 assert(member.isDeclaration, failedAt(member)); | 590 assert(member.isDeclaration, failedAt(member)); |
| 574 if (member.isMalformed) return; | 591 if (member.isMalformed) return; |
| 575 super._processInstantiatedClassMember(cls, member, memberUsed); | 592 super._processInstantiatedClassMember(cls, member, memberUsed); |
| 576 } | 593 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 592 void registerIsCheck(ResolutionDartType type) { | 609 void registerIsCheck(ResolutionDartType type) { |
| 593 // Even in checked mode, type annotations for return type and argument | 610 // Even in checked mode, type annotations for return type and argument |
| 594 // types do not imply type checks, so there should never be a check | 611 // types do not imply type checks, so there should never be a check |
| 595 // against the type variable of a typedef. | 612 // against the type variable of a typedef. |
| 596 assert(!type.isTypeVariable || !type.element.enclosingElement.isTypedef); | 613 assert(!type.isTypeVariable || !type.element.enclosingElement.isTypedef); |
| 597 super.registerIsCheck(type); | 614 super.registerIsCheck(type); |
| 598 } | 615 } |
| 599 } | 616 } |
| 600 | 617 |
| 601 class KernelCodegenWorldBuilder extends CodegenWorldBuilderImpl { | 618 class KernelCodegenWorldBuilder extends CodegenWorldBuilderImpl { |
| 619 KernelToElementMapImpl _elementMap; |
| 620 |
| 602 KernelCodegenWorldBuilder( | 621 KernelCodegenWorldBuilder( |
| 622 this._elementMap, |
| 603 ElementEnvironment elementEnvironment, | 623 ElementEnvironment elementEnvironment, |
| 604 NativeBasicData nativeBasicData, | 624 NativeBasicData nativeBasicData, |
| 605 ClosedWorld world, | 625 ClosedWorld world, |
| 606 SelectorConstraintsStrategy selectorConstraintsStrategy) | 626 SelectorConstraintsStrategy selectorConstraintsStrategy) |
| 607 : super(elementEnvironment, nativeBasicData, world, | 627 : super(elementEnvironment, nativeBasicData, world, |
| 608 selectorConstraintsStrategy); | 628 selectorConstraintsStrategy); |
| 609 | 629 |
| 610 @override | 630 @override |
| 611 void forEachParameter( | 631 bool hasConstantFieldInitializer(FieldEntity field) { |
| 612 FunctionEntity function, void f(DartType type, String name)) { | 632 return _elementMap.hasConstantFieldInitializer(field); |
| 613 throw new UnimplementedError('KernelCodegenWorldBuilder.forEachParameter'); | |
| 614 } | 633 } |
| 615 | 634 |
| 616 @override | 635 @override |
| 636 void forEachParameter(FunctionEntity function, |
| 637 void f(DartType type, String name, ConstantValue defaultValue)) { |
| 638 _elementMap.forEachParameter(function, f); |
| 639 } |
| 640 |
| 641 @override |
| 617 void forEachInstanceField( | 642 void forEachInstanceField( |
| 618 ClassEntity cls, void f(ClassEntity declarer, FieldEntity field)) { | 643 ClassEntity cls, void f(ClassEntity declarer, FieldEntity field)) { |
| 619 _elementEnvironment.forEachClassMember(cls, | 644 _elementEnvironment.forEachClassMember(cls, |
| 620 (ClassEntity declarer, MemberEntity member) { | 645 (ClassEntity declarer, MemberEntity member) { |
| 621 if (member.isField && member.isInstanceMember) f(declarer, member); | 646 if (member.isField && member.isInstanceMember) f(declarer, member); |
| 622 }); | 647 }); |
| 623 } | 648 } |
| 624 } | 649 } |
| OLD | NEW |