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

Side by Side Diff: lib/compiler/implementation/dart_backend/renamer.dart

Issue 10829076: Support imports for dart: libs. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 4 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 /** 5 /**
6 * Renames only top-level elements that would let to ambiguity if not renamed. 6 * Renames only top-level elements that would let to ambiguity if not renamed.
7 * TODO(smok): Make sure that top-level fields are correctly renamed. 7 * TODO(smok): Make sure that top-level fields are correctly renamed.
8 */ 8 */
9 class ConflictingRenamer extends Renamer { 9 class ConflictingRenamer extends Renamer {
10 final Compiler compiler; 10 final Compiler compiler;
11 final Map<Element, String> renamed; 11 final Map<Element, String> renamed;
12 final Set<String> usedTopLevelIdentifiers; 12 final Set<String> usedTopLevelIdentifiers;
13 final Map<LibraryElement, String> imports;
13 TreeElements contextElements; 14 TreeElements contextElements;
14 Element context; 15 Element context;
15 16
16 Map<Element, TreeElements> get resolvedElements() => 17 Map<Element, TreeElements> get resolvedElements() =>
17 compiler.enqueuer.resolution.resolvedElements; 18 compiler.enqueuer.resolution.resolvedElements;
18 19
19 ConflictingRenamer(this.compiler) : 20 ConflictingRenamer(this.compiler) :
20 renamed = new Map<Element, String>(), 21 renamed = new Map<Element, String>(),
21 usedTopLevelIdentifiers = new Set<String>(); 22 usedTopLevelIdentifiers = new Set<String>(),
23 imports = new Map<LibraryElement, String>();
22 24
23 void setContext(Element element) { 25 void setContext(Element element) {
24 this.context = element; 26 this.context = element;
25 contextElements = resolvedElements[element]; 27 contextElements = resolvedElements[element];
26 } 28 }
27 29
28 String getFactoryName(FunctionExpression node) => 30 String getFactoryName(FunctionExpression node) =>
29 node.name.asSend().selector.asIdentifier().source.slowToString(); 31 node.name.asSend().selector.asIdentifier().source.slowToString();
30 32
31 bool isNamedConstructor(Element element) => 33 bool isNamedConstructor(Element element) =>
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 return renameElement(enclosingClass); 77 return renameElement(enclosingClass);
76 } 78 }
77 } 79 }
78 if (context.isFunction() && context.cachedNode.name == node) { 80 if (context.isFunction() && context.cachedNode.name == node) {
79 return renameElement(context); 81 return renameElement(context);
80 } 82 }
81 return null; 83 return null;
82 } 84 }
83 85
84 String renameElement(Element element) { 86 String renameElement(Element element) {
87 generateUniqueName(name) {
88 while (usedTopLevelIdentifiers.contains(name)) {
89 name = "x$name";
Roman 2012/07/30 14:43:30 Why 'x'? 'x' is an ordinary letter, it will be har
Anton Muhin 2012/07/30 14:54:31 Agree, but apparently due to the bug in DartVM, '_
Roman 2012/07/30 15:03:42 That's sad. Maybe '$' ? or anything else?
Anton Muhin 2012/07/30 15:37:16 Done.
90 }
91 usedTopLevelIdentifiers.add(name);
92 return name;
93 }
94
85 String originalName = element.name.slowToString(); 95 String originalName = element.name.slowToString();
86 if (element.getLibrary() == compiler.coreLibrary || !element.isTopLevel()) { 96 // TODO(antonm): we should rename lib private names as well.
87 return originalName; 97 if (!element.isTopLevel()) return originalName;
98 final library = element.getLibrary();
99 if (isDartCoreLib(compiler, library)) {
100 final prefix = imports.putIfAbsent(library, () => generateUniqueName('p')) ;
Roman 2012/07/30 14:43:30 Why 'p' as a prefix? Easier to read would be somet
Anton Muhin 2012/07/30 14:54:31 I am not sure it's a valid Dart identifier. Plus
Roman 2012/07/30 15:03:42 We can fix it if for some reason this will not be
101 return '$prefix.$originalName';
88 } 102 }
89 if (renamed[element] !== null) { 103 if (renamed[element] !== null) {
Roman 2012/07/30 14:43:30 Should this check be moved to before generating re
Anton Muhin 2012/07/30 14:54:31 Done.
90 return renamed[element]; 104 return renamed[element];
91 } 105 }
92 106
93 // Not renamed and top element. 107 // Not renamed and top element.
94 // TODO(smok): Make sure that the new name does not conflict with existing 108 // TODO(smok): Make sure that the new name does not conflict with existing
95 // local identifiers. 109 // local identifiers.
96 String name = originalName; 110 return renamed[element] = generateUniqueName(originalName);
97 while (usedTopLevelIdentifiers.contains(name)) {
98 name = "_$name";
99 }
100 usedTopLevelIdentifiers.add(name);
101 renamed[element] = name;
102 return name;
103 } 111 }
104 } 112 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698