Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Fletch 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 library fletchc.closure_environment; | 5 library fletchc.closure_environment; |
| 6 | 6 |
| 7 import 'package:compiler/src/util/util.dart' show | |
| 8 SpannableAssertionFailure; | |
| 9 | |
| 10 import 'package:compiler/src/resolution/semantic_visitor.dart'; | 7 import 'package:compiler/src/resolution/semantic_visitor.dart'; |
| 11 | 8 |
| 12 import 'package:compiler/src/resolution/operators.dart' show | 9 import 'package:compiler/src/resolution/operators.dart' show |
| 13 AssignmentOperator, | 10 AssignmentOperator, |
| 14 BinaryOperator, | 11 BinaryOperator, |
| 15 IncDecOperator, | 12 IncDecOperator, |
| 16 UnaryOperator; | 13 UnaryOperator; |
| 17 | 14 |
| 18 import 'package:compiler/src/elements/elements.dart'; | 15 import 'package:compiler/src/elements/elements.dart'; |
| 19 import 'package:compiler/src/resolution/resolution.dart'; | 16 import 'package:compiler/src/resolution/tree_elements.dart'; |
| 17 import 'package:compiler/src/resolution/send_resolver.dart'; | |
| 20 import 'package:compiler/src/tree/tree.dart'; | 18 import 'package:compiler/src/tree/tree.dart'; |
| 21 import 'package:compiler/src/universe/universe.dart'; | 19 import 'package:compiler/src/universe/selector.dart'; |
| 22 import 'package:compiler/src/util/util.dart' show Spannable; | 20 import 'package:compiler/src/universe/call_structure.dart'; |
| 21 import 'package:compiler/src/diagnostics/spannable.dart' show | |
| 22 Spannable, | |
| 23 SpannableAssertionFailure; | |
| 23 import 'package:compiler/src/dart_types.dart'; | 24 import 'package:compiler/src/dart_types.dart'; |
| 24 | 25 |
| 25 enum CaptureMode { | 26 enum CaptureMode { |
| 26 /** | 27 /** |
| 27 * If a local is marked [ByValue], the local is read in closures. | 28 * If a local is marked [ByValue], the local is read in closures. |
| 28 */ | 29 */ |
| 29 ByValue, | 30 ByValue, |
| 30 | 31 |
| 31 /** | 32 /** |
| 32 * If a local is marked [ByReference], a write to the local can be observed | 33 * If a local is marked [ByReference], a write to the local can be observed |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 ClosureVisitor(this.element, TreeElements elements) | 94 ClosureVisitor(this.element, TreeElements elements) |
| 94 : super(elements); | 95 : super(elements); |
| 95 | 96 |
| 96 SemanticSendVisitor get sendVisitor => this; | 97 SemanticSendVisitor get sendVisitor => this; |
| 97 SemanticDeclarationVisitor get declVisitor => this; | 98 SemanticDeclarationVisitor get declVisitor => this; |
| 98 | 99 |
| 99 ClosureEnvironment compute() { | 100 ClosureEnvironment compute() { |
| 100 assert(element.memberContext == element); | 101 assert(element.memberContext == element); |
| 101 assert(currentElement == null); | 102 assert(currentElement == null); |
| 102 currentElement = element; | 103 currentElement = element; |
| 103 if (element.node != null) element.node.accept(this); | 104 if (element.node != null) element.resolvedAst.node.accept(this); |
| 104 assert(currentElement == element); | 105 assert(currentElement == element); |
| 105 return closureEnvironment; | 106 return closureEnvironment; |
| 106 } | 107 } |
| 107 | 108 |
| 108 void visitNode(Node node) { | 109 void visitNode(Node node) { |
| 109 node.visitChildren(this); | 110 node.visitChildren(this); |
| 110 } | 111 } |
| 111 | 112 |
| 112 void visitVariableDefinitions(VariableDefinitions node) { | 113 void visitVariableDefinitions(VariableDefinitions node) { |
| 113 for (Node definition in node.definitions) { | 114 for (Node definition in node.definitions) { |
| 114 VariableElement element = elements[definition]; | 115 VariableElement element = elements[definition]; |
| 115 Expression initializer = element.initializer; | 116 Expression initializer = element.initializer; |
| 116 if (initializer != null) initializer.accept(this); | 117 if (initializer != null) initializer.accept(this); |
| 117 } | 118 } |
| 118 } | 119 } |
| 119 | 120 |
| 120 void visitFunctionExpression(FunctionExpression node) { | 121 void visitFunctionExpression(FunctionExpression node) { |
| 121 ExecutableElement oldElement = currentElement; | 122 ExecutableElement oldElement = currentElement; |
| 122 currentElement = elements[node]; | 123 currentElement = elements.getFunctionDefinition(node); |
| 123 if (currentElement != element) { | 124 if (currentElement != element) { |
| 124 ClosureInfo info = new ClosureInfo(); | 125 ClosureInfo info = new ClosureInfo(); |
| 125 closureEnvironment.closures[currentElement] = info; | 126 closureEnvironment.closures[currentElement] = info; |
| 126 } | 127 } |
| 127 if (currentElement.isConstructor) { | 128 if (currentElement.isConstructor) { |
| 128 inInitializers = true; | 129 inInitializers = true; |
| 129 visitInitializers(node, null); | 130 visitInitializers(node, null); |
| 130 inInitializers = false; | 131 inInitializers = false; |
| 131 } | 132 } |
| 132 node.body.accept(this); | 133 node.body.accept(this); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 void handleLocalInvoke( | 221 void handleLocalInvoke( |
| 221 Send node, | 222 Send node, |
| 222 LocalElement element, | 223 LocalElement element, |
| 223 NodeList arguments, | 224 NodeList arguments, |
| 224 CallStructure callStructure, | 225 CallStructure callStructure, |
| 225 _) { | 226 _) { |
| 226 markUsed(element, CaptureMode.ByValue); | 227 markUsed(element, CaptureMode.ByValue); |
| 227 arguments.accept(this); | 228 arguments.accept(this); |
| 228 } | 229 } |
| 229 | 230 |
| 230 void visitThisPropertySet(Send node, Selector selector, Node rhs, _) { | 231 void visitThisPropertySet(Send node, Name name, Node rhs, _) { |
| 231 markThisUsed(); | 232 markThisUsed(); |
| 232 super.visitThisPropertySet(node, selector, rhs, null); | 233 super.visitThisPropertySet(node, name, rhs, null); |
| 233 } | 234 } |
| 234 | 235 |
| 235 void visitLocalVariablePrefix( | 236 void visitLocalVariablePrefix( |
| 236 SendSet node, | 237 SendSet node, |
| 237 LocalVariableElement element, | 238 LocalVariableElement element, |
| 238 IncDecOperator operator, | 239 IncDecOperator operator, |
| 239 _) { | 240 _) { |
| 240 markUsed(element, CaptureMode.ByReference); | 241 markUsed(element, CaptureMode.ByReference); |
| 241 } | 242 } |
| 242 | 243 |
| 243 void visitLocalVariablePostfix( | 244 void visitLocalVariablePostfix( |
| 244 SendSet node, | 245 SendSet node, |
| 245 LocalVariableElement element, | 246 LocalVariableElement element, |
| 246 IncDecOperator operator, | 247 IncDecOperator operator, |
| 247 _) { | 248 _) { |
| 248 markUsed(element, CaptureMode.ByReference); | 249 markUsed(element, CaptureMode.ByReference); |
| 249 } | 250 } |
| 250 | 251 |
| 251 void visitThisPropertyInvoke( | 252 void visitThisPropertyInvoke( |
| 252 Send node, | 253 Send node, |
| 253 NodeList arguments, | 254 NodeList arguments, |
| 254 Selector selector, | 255 Selector selector, |
| 255 _) { | 256 _) { |
| 256 markThisUsed(); | 257 markThisUsed(); |
| 257 arguments.accept(this); | 258 arguments.accept(this); |
| 258 } | 259 } |
| 259 | 260 |
| 260 void visitThisPropertyGet( | 261 void visitThisPropertyGet( |
| 261 Send node, | 262 Send node, |
| 262 Selector selector, | 263 Name name, |
| 263 _) { | 264 _) { |
| 264 markThisUsed(); | 265 markThisUsed(); |
| 265 } | 266 } |
| 266 | 267 |
| 267 void visitThisGet(Node node, _) { | 268 void visitThisGet(Node node, _) { |
| 268 markThisUsed(); | 269 markThisUsed(); |
| 269 } | 270 } |
| 270 | 271 |
| 271 void visitIs(Send node, Node expression, DartType type, _) { | 272 void visitIs(Send node, Node expression, DartType type, _) { |
| 272 // TODO(ajohnsen): Type is used ByValue. | 273 // TODO(ajohnsen): Type is used ByValue. |
| 273 expression.accept(this); | 274 expression.accept(this); |
| 274 } | 275 } |
| 275 | 276 |
| 276 void visitIsNot(Send node, Node expression, DartType type, _) { | 277 void visitIsNot(Send node, Node expression, DartType type, _) { |
| 277 // TODO(ajohnsen): Type is used ByValue. | 278 // TODO(ajohnsen): Type is used ByValue. |
| 278 expression.accept(this); | 279 expression.accept(this); |
| 279 } | 280 } |
| 280 | 281 |
| 281 void visitAs(Send node, Node expression, DartType type, _) { | 282 void visitAs(Send node, Node expression, DartType type, _) { |
| 282 // TODO(ajohnsen): Type is used ByValue. | 283 // TODO(ajohnsen): Type is used ByValue. |
| 283 expression.accept(this); | 284 expression.accept(this); |
| 284 } | 285 } |
| 285 | 286 |
| 286 void visitAssert(Send node, Node expression, _) { | 287 void visitAssert(Assert node) { |
| 287 // TODO(ajohnsen): Only visit in checked mode. | 288 // TODO(ajohnsen): Only visit in checked mode. |
| 288 expression.accept(this); | 289 node.condition.accept(this); |
| 290 node.message?.accept(this); | |
| 289 } | 291 } |
| 290 | 292 |
| 291 void visitLocalVariableCompound( | 293 void visitLocalVariableCompound( |
| 292 Send node, | 294 Send node, |
| 293 LocalVariableElement variable, | 295 LocalVariableElement variable, |
| 294 AssignmentOperator operator, | 296 AssignmentOperator operator, |
| 295 Node rhs, | 297 Node rhs, |
| 296 _) { | 298 _) { |
| 297 markUsed(variable, CaptureMode.ByReference); | 299 markUsed(variable, CaptureMode.ByReference); |
| 298 super.visitLocalVariableCompound(node, variable, operator, rhs, null); | 300 super.visitLocalVariableCompound(node, variable, operator, rhs, null); |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 460 FunctionElement function, | 462 FunctionElement function, |
| 461 Node index, | 463 Node index, |
| 462 Node rhs, | 464 Node rhs, |
| 463 _) { | 465 _) { |
| 464 markThisUsed(); | 466 markThisUsed(); |
| 465 super.visitSuperIndexSet(node, function, index, rhs, null); | 467 super.visitSuperIndexSet(node, function, index, rhs, null); |
| 466 } | 468 } |
| 467 | 469 |
| 468 void visitThisPropertyCompound( | 470 void visitThisPropertyCompound( |
| 469 Send node, | 471 Send node, |
| 472 Name name, | |
| 470 AssignmentOperator operator, | 473 AssignmentOperator operator, |
| 471 Node rhs, | 474 Node rhs, |
| 472 Selector getterSelector, | |
| 473 Selector setterSelector, | |
| 474 _) { | 475 _) { |
| 475 markThisUsed(); | 476 markThisUsed(); |
| 476 super.visitThisPropertyCompound( | 477 super.visitThisPropertyCompound( |
| 477 node, operator, rhs, getterSelector, setterSelector, null); | 478 node, name, operator, rhs, null); |
| 478 } | 479 } |
| 479 | 480 |
| 480 void visitParameterCompound( | 481 void visitParameterCompound( |
| 481 Send node, | 482 Send node, |
| 482 ParameterElement parameter, | 483 ParameterElement parameter, |
| 483 AssignmentOperator operator, | 484 AssignmentOperator operator, |
| 484 Node rhs, | 485 Node rhs, |
| 485 _) { | 486 _) { |
| 486 markUsed(parameter, CaptureMode.ByReference); | 487 markUsed(parameter, CaptureMode.ByReference); |
| 487 super.visitParameterCompound(node, parameter, operator, rhs, null); | 488 super.visitParameterCompound(node, parameter, operator, rhs, null); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 562 Send node, | 563 Send node, |
| 563 ParameterElement parameter, | 564 ParameterElement parameter, |
| 564 IncDecOperator operator, | 565 IncDecOperator operator, |
| 565 _) { | 566 _) { |
| 566 markUsed(parameter, CaptureMode.ByReference); | 567 markUsed(parameter, CaptureMode.ByReference); |
| 567 super.visitParameterPrefix(node, parameter, operator, null); | 568 super.visitParameterPrefix(node, parameter, operator, null); |
| 568 } | 569 } |
| 569 | 570 |
| 570 void visitThisPropertyPrefix( | 571 void visitThisPropertyPrefix( |
| 571 Send node, | 572 Send node, |
| 573 Name name, | |
| 572 IncDecOperator operator, | 574 IncDecOperator operator, |
| 573 Selector getterSelector, | |
| 574 Selector setterSelector, | |
| 575 _) { | 575 _) { |
| 576 markThisUsed(); | 576 markThisUsed(); |
| 577 super.visitThisPropertyPrefix( | 577 super.visitThisPropertyPrefix( |
| 578 node, operator, getterSelector, setterSelector, null); | 578 node, name, operator, null); |
| 579 } | 579 } |
| 580 | 580 |
| 581 void visitSuperFieldPrefix( | 581 void visitSuperFieldPrefix( |
| 582 Send node, | 582 Send node, |
| 583 FieldElement field, | 583 FieldElement field, |
| 584 IncDecOperator operator, | 584 IncDecOperator operator, |
| 585 _) { | 585 _) { |
| 586 markThisUsed(); | 586 markThisUsed(); |
| 587 super.visitSuperFieldPrefix(node, field, operator, null); | 587 super.visitSuperFieldPrefix(node, field, operator, null); |
| 588 } | 588 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 651 Send node, | 651 Send node, |
| 652 ParameterElement parameter, | 652 ParameterElement parameter, |
| 653 IncDecOperator operator, | 653 IncDecOperator operator, |
| 654 _) { | 654 _) { |
| 655 markUsed(parameter, CaptureMode.ByReference); | 655 markUsed(parameter, CaptureMode.ByReference); |
| 656 super.visitParameterPostfix(node, parameter, operator, null); | 656 super.visitParameterPostfix(node, parameter, operator, null); |
| 657 } | 657 } |
| 658 | 658 |
| 659 void visitThisPropertyPostfix( | 659 void visitThisPropertyPostfix( |
| 660 Send node, | 660 Send node, |
| 661 Name name, | |
| 661 IncDecOperator operator, | 662 IncDecOperator operator, |
| 662 Selector getterSelector, | |
| 663 Selector setterSelector, | |
| 664 _) { | 663 _) { |
| 665 markThisUsed(); | 664 markThisUsed(); |
| 666 super.visitThisPropertyPostfix( | 665 super.visitThisPropertyPostfix( |
| 667 node, operator, getterSelector, setterSelector, null); | 666 node, name, operator, null); |
| 668 } | 667 } |
| 669 | 668 |
| 670 void visitSuperFieldPostfix( | 669 void visitSuperFieldPostfix( |
| 671 Send node, | 670 Send node, |
| 672 FieldElement field, | 671 FieldElement field, |
| 673 IncDecOperator operator, | 672 IncDecOperator operator, |
| 674 _) { | 673 _) { |
| 675 markThisUsed(); | 674 markThisUsed(); |
| 676 super.visitSuperFieldPostfix(node, field, operator, null); | 675 super.visitSuperFieldPostfix(node, field, operator, null); |
| 677 } | 676 } |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 741 FunctionElement indexFunction, | 740 FunctionElement indexFunction, |
| 742 FunctionElement indexSetFunction, | 741 FunctionElement indexSetFunction, |
| 743 Node index, | 742 Node index, |
| 744 IncDecOperator operator, | 743 IncDecOperator operator, |
| 745 _) { | 744 _) { |
| 746 markThisUsed(); | 745 markThisUsed(); |
| 747 super.visitSuperIndexPrefix( | 746 super.visitSuperIndexPrefix( |
| 748 node, indexFunction, indexSetFunction, index, operator, null); | 747 node, indexFunction, indexSetFunction, index, operator, null); |
| 749 } | 748 } |
| 750 | 749 |
| 750 @override | |
| 751 void visitTypeAnnotation(TypeAnnotation node) { | |
|
Johnni Winther
2015/11/19 10:17:26
Add a comment about why this is needed.
sigurdm
2015/11/19 14:33:46
Done.
| |
| 752 } | |
| 753 | |
| 751 void handleImmutableLocalSet( | 754 void handleImmutableLocalSet( |
| 752 SendSet node, | 755 SendSet node, |
| 753 LocalElement element, | 756 LocalElement element, |
| 754 Node rhs, | 757 Node rhs, |
| 755 _) { | 758 _) { |
| 756 apply(rhs); | 759 apply(rhs); |
| 757 } | 760 } |
| 758 | 761 |
| 759 void bulkHandleNode(Node node, String message, _) { | 762 void bulkHandleNode(Node node, String message, _) { |
| 760 } | 763 } |
| 761 | 764 |
| 762 void applyInitializers(FunctionExpression initializers, _) { | 765 void applyInitializers(FunctionExpression initializers, _) { |
| 763 } | 766 } |
| 764 | 767 |
| 765 void applyParameters(NodeList parameters, _) { | 768 void applyParameters(NodeList parameters, _) { |
| 766 } | 769 } |
| 767 } | 770 } |
| OLD | NEW |