Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: lib/clone.dart

Issue 2477873002: Ensure the substitution map in the CloneVisitor is mutable. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698