| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:collection'; | 5 import 'dart:collection'; |
| 6 | 6 |
| 7 import '../js_ast/js_ast.dart'; | 7 import '../js_ast/js_ast.dart'; |
| 8 | 8 |
| 9 /// The ES6 name for the Dart SDK. All dart:* libraries are in this module. | 9 /// The ES6 name for the Dart SDK. All dart:* libraries are in this module. |
| 10 const String dartSdkModule = 'dart_sdk'; | 10 const String dartSdkModule = 'dart_sdk'; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 /// New names assigned for temps and identifiers. | 103 /// New names assigned for temps and identifiers. |
| 104 final renames = new HashMap<Object, String>(); | 104 final renames = new HashMap<Object, String>(); |
| 105 | 105 |
| 106 _FunctionScope(this.parent); | 106 _FunctionScope(this.parent); |
| 107 } | 107 } |
| 108 | 108 |
| 109 /// Collects all names used in the visited tree. | 109 /// Collects all names used in the visited tree. |
| 110 class _RenameVisitor extends VariableDeclarationVisitor { | 110 class _RenameVisitor extends VariableDeclarationVisitor { |
| 111 final pendingRenames = new Map<Object, Set<_FunctionScope>>(); | 111 final pendingRenames = new Map<Object, Set<_FunctionScope>>(); |
| 112 | 112 |
| 113 final _FunctionScope globalScope = new _FunctionScope(null); |
| 113 final _FunctionScope rootScope = new _FunctionScope(null); | 114 final _FunctionScope rootScope = new _FunctionScope(null); |
| 114 _FunctionScope scope; | 115 _FunctionScope scope; |
| 115 | 116 |
| 116 _RenameVisitor.build(Node root) { | 117 _RenameVisitor.build(Node root) { |
| 117 scope = rootScope; | 118 scope = rootScope; |
| 118 root.accept(this); | 119 root.accept(this); |
| 119 _finishFunctions(); | 120 _finishFunctions(); |
| 120 _finishNames(); | 121 _finishNames(); |
| 121 } | 122 } |
| 122 | 123 |
| 123 declare(Identifier node) { | 124 declare(Identifier node) { |
| 124 var id = identifierKey(node); | 125 var id = identifierKey(node); |
| 125 var notAlreadyDeclared = scope.declared.add(id); | 126 var notAlreadyDeclared = scope.declared.add(id); |
| 126 // Normal identifiers can be declared multiple times, because we don't | 127 // Normal identifiers can be declared multiple times, because we don't |
| 127 // implement block scope yet. However temps should only be declared once. | 128 // implement block scope yet. However temps should only be declared once. |
| 128 assert(notAlreadyDeclared || node is! TemporaryId); | 129 assert(notAlreadyDeclared || node is! TemporaryId); |
| 129 _markUsed(node, id, scope); | 130 _markUsed(node, id, scope); |
| 130 } | 131 } |
| 131 | 132 |
| 132 visitIdentifier(Identifier node) { | 133 visitIdentifier(Identifier node) { |
| 133 var id = identifierKey(node); | 134 var id = identifierKey(node); |
| 134 | 135 |
| 135 // Find where the node was declared. | 136 // Find where the node was declared. |
| 136 var declScope = scope; | 137 var declScope = scope; |
| 137 while (declScope != null && !declScope.declared.contains(id)) { | 138 while (declScope != null && !declScope.declared.contains(id)) { |
| 138 declScope = declScope.parent; | 139 declScope = declScope.parent; |
| 139 } | 140 } |
| 140 if (declScope == null) { | 141 if (declScope == null) { |
| 141 // Assume it comes from the global scope. | 142 // Assume it comes from the global scope. |
| 142 declScope = rootScope; | 143 declScope = globalScope; |
| 143 declScope.declared.add(id); | 144 declScope.declared.add(id); |
| 144 } | 145 } |
| 145 _markUsed(node, id, declScope); | 146 _markUsed(node, id, declScope); |
| 146 } | 147 } |
| 147 | 148 |
| 148 _markUsed(Identifier node, Object id, _FunctionScope declScope) { | 149 _markUsed(Identifier node, Object id, _FunctionScope declScope) { |
| 149 // If it needs rename, we can't add it to the used name set yet, instead we | 150 // If it needs rename, we can't add it to the used name set yet, instead we |
| 150 // will record all scopes it is visible in. | 151 // will record all scopes it is visible in. |
| 151 Set<_FunctionScope> usedIn = null; | 152 Set<_FunctionScope> usedIn = null; |
| 152 var rename = declScope != rootScope && needsRename(node); | 153 var rename = declScope != globalScope && needsRename(node); |
| 153 if (rename) { | 154 if (rename) { |
| 154 usedIn = pendingRenames.putIfAbsent(id, () => new HashSet()); | 155 usedIn = pendingRenames.putIfAbsent(id, () => new HashSet()); |
| 155 } | 156 } |
| 156 for (var s = scope, end = declScope.parent; s != end; s = s.parent) { | 157 for (var s = scope, end = declScope.parent; s != end; s = s.parent) { |
| 157 if (usedIn != null) { | 158 if (usedIn != null) { |
| 158 usedIn.add(s); | 159 usedIn.add(s); |
| 159 } else { | 160 } else { |
| 160 s.used.add(node.name); | 161 s.used.add(node.name); |
| 161 } | 162 } |
| 162 } | 163 } |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 bool invalidStaticFieldName(String name) { | 291 bool invalidStaticFieldName(String name) { |
| 291 switch (name) { | 292 switch (name) { |
| 292 case "arguments": | 293 case "arguments": |
| 293 case "caller": | 294 case "caller": |
| 294 case "callee": | 295 case "callee": |
| 295 case "name": | 296 case "name": |
| 296 return true; | 297 return true; |
| 297 } | 298 } |
| 298 return false; | 299 return false; |
| 299 } | 300 } |
| OLD | NEW |