OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 tree_ir_builder; | 5 library tree_ir_builder; |
6 | 6 |
7 import '../common.dart'; | 7 import '../common.dart'; |
8 import '../constants/values.dart'; | 8 import '../constants/values.dart'; |
9 import '../cps_ir/cps_ir_nodes.dart' as cps_ir; | 9 import '../cps_ir/cps_ir_nodes.dart' as cps_ir; |
10 import '../elements/elements.dart'; | 10 import '../elements/elements.dart'; |
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
618 node.dartType, | 618 node.dartType, |
619 node.target, | 619 node.target, |
620 node.selector, | 620 node.selector, |
621 arguments, | 621 arguments, |
622 node.sourceInformation); | 622 node.sourceInformation); |
623 } | 623 } |
624 | 624 |
625 visitForeignCode(cps_ir.ForeignCode node) { | 625 visitForeignCode(cps_ir.ForeignCode node) { |
626 List<Expression> arguments = | 626 List<Expression> arguments = |
627 node.arguments.map(getVariableUse).toList(growable: false); | 627 node.arguments.map(getVariableUse).toList(growable: false); |
628 if (HasCapturedArguments.check(node.codeTemplate.ast)) { | 628 List<bool> nullableArguments = node.arguments |
629 for (Expression arg in arguments) { | 629 .map((argument) => argument.definition.type.isNullable) |
630 if (arg is VariableUse) { | 630 .toList(growable: false); |
631 arg.variable.isCaptured = true; | |
632 } else { | |
633 // TODO(asgerf): Avoid capture of 'this'. | |
634 } | |
635 } | |
636 } | |
637 if (node.codeTemplate.isExpression) { | 631 if (node.codeTemplate.isExpression) { |
638 return new ForeignExpression( | 632 return new ForeignExpression( |
639 node.codeTemplate, | 633 node.codeTemplate, |
640 node.type, | 634 node.type, |
641 arguments, | 635 arguments, |
642 node.nativeBehavior, | 636 node.nativeBehavior, |
| 637 nullableArguments, |
643 node.dependency); | 638 node.dependency); |
644 } else { | 639 } else { |
645 return (Statement next) { | 640 return (Statement next) { |
646 assert(next is Unreachable); // We are not using the `next` statement. | 641 assert(next is Unreachable); // We are not using the `next` statement. |
647 return new ForeignStatement( | 642 return new ForeignStatement( |
648 node.codeTemplate, | 643 node.codeTemplate, |
649 node.type, | 644 node.type, |
650 arguments, | 645 arguments, |
651 node.nativeBehavior, | 646 node.nativeBehavior, |
| 647 nullableArguments, |
652 node.dependency); | 648 node.dependency); |
653 }; | 649 }; |
654 } | 650 } |
655 } | 651 } |
656 | 652 |
657 visitNullCheck(cps_ir.NullCheck node) => (Statement next) { | 653 visitNullCheck(cps_ir.NullCheck node) => (Statement next) { |
658 return new NullCheck( | 654 return new NullCheck( |
659 condition: getVariableUseOrNull(node.condition), | 655 condition: getVariableUseOrNull(node.condition), |
660 value: getVariableUse(node.value), | 656 value: getVariableUse(node.value), |
661 selector: node.selector, | 657 selector: node.selector, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
697 internalError(CURRENT_ELEMENT_SPANNABLE, 'Unexpected IR node: $node'); | 693 internalError(CURRENT_ELEMENT_SPANNABLE, 'Unexpected IR node: $node'); |
698 } | 694 } |
699 | 695 |
700 visitFunctionDefinition(cps_ir.FunctionDefinition node) { | 696 visitFunctionDefinition(cps_ir.FunctionDefinition node) { |
701 unexpectedNode(node); | 697 unexpectedNode(node); |
702 } | 698 } |
703 visitParameter(cps_ir.Parameter node) => unexpectedNode(node); | 699 visitParameter(cps_ir.Parameter node) => unexpectedNode(node); |
704 visitContinuation(cps_ir.Continuation node) => unexpectedNode(node); | 700 visitContinuation(cps_ir.Continuation node) => unexpectedNode(node); |
705 visitMutableVariable(cps_ir.MutableVariable node) => unexpectedNode(node); | 701 visitMutableVariable(cps_ir.MutableVariable node) => unexpectedNode(node); |
706 } | 702 } |
707 | |
708 class HasCapturedArguments extends js.BaseVisitor { | |
709 static bool check(js.Node node) { | |
710 HasCapturedArguments visitor = new HasCapturedArguments(); | |
711 node.accept(visitor); | |
712 return visitor.found; | |
713 } | |
714 | |
715 int enclosingFunctions = 0; | |
716 bool found = false; | |
717 | |
718 @override | |
719 visitFun(js.Fun node) { | |
720 ++enclosingFunctions; | |
721 node.visitChildren(this); | |
722 --enclosingFunctions; | |
723 } | |
724 | |
725 @override | |
726 visitInterpolatedNode(js.InterpolatedNode node) { | |
727 if (enclosingFunctions > 0) { | |
728 found = true; | |
729 } | |
730 } | |
731 } | |
OLD | NEW |