| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 library kernel.clone; | 4 library kernel.clone; |
| 5 | 5 |
| 6 import 'ast.dart'; | 6 import 'ast.dart'; |
| 7 import 'type_algebra.dart'; | 7 import 'type_algebra.dart'; |
| 8 | 8 |
| 9 /// Visitor that return a clone of a tree, maintaining references to cloned | 9 /// Visitor that return a clone of a tree, maintaining references to cloned |
| 10 /// objects. | 10 /// objects. |
| 11 /// | 11 /// |
| 12 /// It is safe to clone members, but cloning a class or library is not | 12 /// It is safe to clone members, but cloning a class or library is not |
| 13 /// supported. | 13 /// supported. |
| 14 class CloneVisitor extends TreeVisitor { | 14 class CloneVisitor extends TreeVisitor { |
| 15 final Map<VariableDeclaration, VariableDeclaration> variables = | 15 final Map<VariableDeclaration, VariableDeclaration> variables = |
| 16 <VariableDeclaration, VariableDeclaration>{}; | 16 <VariableDeclaration, VariableDeclaration>{}; |
| 17 final Map<LabeledStatement, LabeledStatement> labels = | 17 final Map<LabeledStatement, LabeledStatement> labels = |
| 18 <LabeledStatement, LabeledStatement>{}; | 18 <LabeledStatement, LabeledStatement>{}; |
| 19 final Map<SwitchCase, SwitchCase> switchCases = <SwitchCase, SwitchCase>{}; | 19 final Map<SwitchCase, SwitchCase> switchCases = <SwitchCase, SwitchCase>{}; |
| 20 final Map<TypeParameter, DartType> typeSubstitution; | 20 final Map<TypeParameter, DartType> typeSubstitution; |
| 21 | 21 |
| 22 CloneVisitor({Map<TypeParameter, DartType> typeSubstitution}) | 22 CloneVisitor({Map<TypeParameter, DartType> typeSubstitution}) |
| 23 : this.typeSubstitution = typeSubstitution ?? <TypeParameter, DartType>{}; | 23 : this.typeSubstitution = ensureMutable(typeSubstitution); |
| 24 |
| 25 static Map<TypeParameter, DartType> ensureMutable( |
| 26 Map<TypeParameter, DartType> map) { |
| 27 // We need to mutate this map, so make sure we don't use a constant map. |
| 28 if (map == null || map.isEmpty) { |
| 29 return <TypeParameter, DartType>{}; |
| 30 } |
| 31 return map; |
| 32 } |
| 24 | 33 |
| 25 TreeNode visitLibrary(Library node) { | 34 TreeNode visitLibrary(Library node) { |
| 26 throw 'Cloning of libraries is not implemented'; | 35 throw 'Cloning of libraries is not implemented'; |
| 27 } | 36 } |
| 28 | 37 |
| 29 TreeNode visitClass(Class node) { | 38 TreeNode visitClass(Class node) { |
| 30 throw 'Cloning of classes is not implemented'; | 39 throw 'Cloning of classes is not implemented'; |
| 31 } | 40 } |
| 32 | 41 |
| 33 TreeNode clone(TreeNode node) => | 42 TreeNode clone(TreeNode node) => |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 visitArguments(Arguments node) { | 390 visitArguments(Arguments node) { |
| 382 return new Arguments(node.positional.map(clone).toList(), | 391 return new Arguments(node.positional.map(clone).toList(), |
| 383 types: node.types.map(visitType).toList(), | 392 types: node.types.map(visitType).toList(), |
| 384 named: node.named.map(clone).toList()); | 393 named: node.named.map(clone).toList()); |
| 385 } | 394 } |
| 386 | 395 |
| 387 visitNamedExpression(NamedExpression node) { | 396 visitNamedExpression(NamedExpression node) { |
| 388 return new NamedExpression(node.name, clone(node.value)); | 397 return new NamedExpression(node.name, clone(node.value)); |
| 389 } | 398 } |
| 390 } | 399 } |
| OLD | NEW |