| 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 and the [defaultValue] if the parameter is optional. | 17 /// the parameter. |
| 18 void forEachParameter(FunctionEntity function, | 18 void forEachParameter( |
| 19 void f(DartType type, String name, ConstantValue defaultValue)); | 19 FunctionEntity function, void f(DartType type, String name)); |
| 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] has a constant initializer. | |
| 31 bool hasConstantFieldInitializer(FieldEntity field); | |
| 32 | |
| 33 /// Returns the constant initializer for [field]. | |
| 34 ConstantValue getConstantFieldInitializer(FieldEntity field); | |
| 35 | |
| 36 /// Returns `true` if [member] is invoked as a setter. | 30 /// Returns `true` if [member] is invoked as a setter. |
| 37 bool hasInvokedSetter(MemberEntity member, ClosedWorld world); | 31 bool hasInvokedSetter(MemberEntity member, ClosedWorld world); |
| 38 | 32 |
| 39 bool hasInvokedGetter(MemberEntity member, ClosedWorld world); | 33 bool hasInvokedGetter(MemberEntity member, ClosedWorld world); |
| 40 | 34 |
| 41 Map<Selector, SelectorConstraints> invocationsByName(String name); | 35 Map<Selector, SelectorConstraints> invocationsByName(String name); |
| 42 | 36 |
| 43 Map<Selector, SelectorConstraints> getterInvocationsByName(String name); | 37 Map<Selector, SelectorConstraints> getterInvocationsByName(String name); |
| 44 | 38 |
| 45 Map<Selector, SelectorConstraints> setterInvocationsByName(String name); | 39 Map<Selector, SelectorConstraints> setterInvocationsByName(String name); |
| (...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 /// the constant use was new to the world. | 535 /// the constant use was new to the world. |
| 542 bool registerConstantUse(ConstantUse use) { | 536 bool registerConstantUse(ConstantUse use) { |
| 543 if (use.kind == ConstantUseKind.DIRECT) { | 537 if (use.kind == ConstantUseKind.DIRECT) { |
| 544 addCompileTimeConstantForEmission(use.value); | 538 addCompileTimeConstantForEmission(use.value); |
| 545 } | 539 } |
| 546 return _constantValues.add(use.value); | 540 return _constantValues.add(use.value); |
| 547 } | 541 } |
| 548 } | 542 } |
| 549 | 543 |
| 550 class ElementCodegenWorldBuilderImpl extends CodegenWorldBuilderImpl { | 544 class ElementCodegenWorldBuilderImpl extends CodegenWorldBuilderImpl { |
| 551 final JavaScriptConstantCompiler _constants; | |
| 552 | |
| 553 ElementCodegenWorldBuilderImpl( | 545 ElementCodegenWorldBuilderImpl( |
| 554 this._constants, | |
| 555 ElementEnvironment elementEnvironment, | 546 ElementEnvironment elementEnvironment, |
| 556 NativeBasicData nativeBasicData, | 547 NativeBasicData nativeBasicData, |
| 557 ClosedWorld world, | 548 ClosedWorld world, |
| 558 SelectorConstraintsStrategy selectorConstraintsStrategy) | 549 SelectorConstraintsStrategy selectorConstraintsStrategy) |
| 559 : super(elementEnvironment, nativeBasicData, world, | 550 : super(elementEnvironment, nativeBasicData, world, |
| 560 selectorConstraintsStrategy); | 551 selectorConstraintsStrategy); |
| 561 | 552 |
| 562 @override | |
| 563 bool hasConstantFieldInitializer(FieldElement field) { | |
| 564 return field.constant != null; | |
| 565 } | |
| 566 | |
| 567 @override | |
| 568 ConstantValue getConstantFieldInitializer(FieldElement field) { | |
| 569 assert(field.constant != null, | |
| 570 failedAt(field, "Field $field doesn't have a constant initial value.")); | |
| 571 return _constants.getConstantValue(field.constant); | |
| 572 } | |
| 573 | |
| 574 /// Calls [f] with every instance field, together with its declarer, in an | 553 /// Calls [f] with every instance field, together with its declarer, in an |
| 575 /// instance of [cls]. | 554 /// instance of [cls]. |
| 576 void forEachInstanceField( | 555 void forEachInstanceField( |
| 577 ClassElement cls, void f(ClassEntity declarer, FieldEntity field)) { | 556 ClassElement cls, void f(ClassEntity declarer, FieldEntity field)) { |
| 578 cls.implementation | 557 cls.implementation |
| 579 .forEachInstanceField(f, includeSuperAndInjectedMembers: true); | 558 .forEachInstanceField(f, includeSuperAndInjectedMembers: true); |
| 580 } | 559 } |
| 581 | 560 |
| 582 @override | 561 @override |
| 583 void forEachParameter(MethodElement function, | 562 void forEachParameter( |
| 584 void f(DartType type, String name, ConstantValue defaultValue)) { | 563 MethodElement function, void f(DartType type, String name)) { |
| 585 FunctionSignature parameters = function.functionSignature; | 564 FunctionSignature parameters = function.functionSignature; |
| 586 parameters.forEachParameter((_parameter) { | 565 parameters.forEachParameter((_parameter) { |
| 587 ParameterElement parameter = _parameter; | 566 ParameterElement parameter = _parameter; |
| 588 ConstantValue value; | 567 f(parameter.type, parameter.name); |
| 589 if (parameter.constant != null) { | |
| 590 value = _constants.getConstantValue(parameter.constant); | |
| 591 } else { | |
| 592 value = new NullConstantValue(); | |
| 593 } | |
| 594 f(parameter.type, parameter.name, value); | |
| 595 }); | 568 }); |
| 596 } | 569 } |
| 597 | 570 |
| 598 @override | 571 @override |
| 599 void _processInstantiatedClassMember( | 572 void _processInstantiatedClassMember( |
| 600 ClassEntity cls, MemberElement member, MemberUsedCallback memberUsed) { | 573 ClassEntity cls, MemberElement member, MemberUsedCallback memberUsed) { |
| 601 assert(member.isDeclaration, failedAt(member)); | 574 assert(member.isDeclaration, failedAt(member)); |
| 602 if (member.isMalformed) return; | 575 if (member.isMalformed) return; |
| 603 super._processInstantiatedClassMember(cls, member, memberUsed); | 576 super._processInstantiatedClassMember(cls, member, memberUsed); |
| 604 } | 577 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 620 void registerIsCheck(ResolutionDartType type) { | 593 void registerIsCheck(ResolutionDartType type) { |
| 621 // Even in checked mode, type annotations for return type and argument | 594 // Even in checked mode, type annotations for return type and argument |
| 622 // types do not imply type checks, so there should never be a check | 595 // types do not imply type checks, so there should never be a check |
| 623 // against the type variable of a typedef. | 596 // against the type variable of a typedef. |
| 624 assert(!type.isTypeVariable || !type.element.enclosingElement.isTypedef); | 597 assert(!type.isTypeVariable || !type.element.enclosingElement.isTypedef); |
| 625 super.registerIsCheck(type); | 598 super.registerIsCheck(type); |
| 626 } | 599 } |
| 627 } | 600 } |
| 628 | 601 |
| 629 class KernelCodegenWorldBuilder extends CodegenWorldBuilderImpl { | 602 class KernelCodegenWorldBuilder extends CodegenWorldBuilderImpl { |
| 630 KernelToElementMapImpl _elementMap; | |
| 631 | |
| 632 KernelCodegenWorldBuilder( | 603 KernelCodegenWorldBuilder( |
| 633 this._elementMap, | |
| 634 ElementEnvironment elementEnvironment, | 604 ElementEnvironment elementEnvironment, |
| 635 NativeBasicData nativeBasicData, | 605 NativeBasicData nativeBasicData, |
| 636 ClosedWorld world, | 606 ClosedWorld world, |
| 637 SelectorConstraintsStrategy selectorConstraintsStrategy) | 607 SelectorConstraintsStrategy selectorConstraintsStrategy) |
| 638 : super(elementEnvironment, nativeBasicData, world, | 608 : super(elementEnvironment, nativeBasicData, world, |
| 639 selectorConstraintsStrategy); | 609 selectorConstraintsStrategy); |
| 640 | 610 |
| 641 @override | 611 @override |
| 642 bool hasConstantFieldInitializer(FieldEntity field) { | 612 void forEachParameter( |
| 643 return _elementMap.hasConstantFieldInitializer(field); | 613 FunctionEntity function, void f(DartType type, String name)) { |
| 614 throw new UnimplementedError('KernelCodegenWorldBuilder.forEachParameter'); |
| 644 } | 615 } |
| 645 | 616 |
| 646 @override | 617 @override |
| 647 ConstantValue getConstantFieldInitializer(FieldEntity field) { | |
| 648 return _elementMap.getConstantFieldInitializer(field); | |
| 649 } | |
| 650 | |
| 651 @override | |
| 652 void forEachParameter(FunctionEntity function, | |
| 653 void f(DartType type, String name, ConstantValue defaultValue)) { | |
| 654 _elementMap.forEachParameter(function, f); | |
| 655 } | |
| 656 | |
| 657 @override | |
| 658 void forEachInstanceField( | 618 void forEachInstanceField( |
| 659 ClassEntity cls, void f(ClassEntity declarer, FieldEntity field)) { | 619 ClassEntity cls, void f(ClassEntity declarer, FieldEntity field)) { |
| 660 _elementEnvironment.forEachClassMember(cls, | 620 _elementEnvironment.forEachClassMember(cls, |
| 661 (ClassEntity declarer, MemberEntity member) { | 621 (ClassEntity declarer, MemberEntity member) { |
| 662 if (member.isField && member.isInstanceMember) f(declarer, member); | 622 if (member.isField && member.isInstanceMember) f(declarer, member); |
| 663 }); | 623 }); |
| 664 } | 624 } |
| 665 } | 625 } |
| OLD | NEW |