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

Side by Side Diff: pkg/compiler/lib/src/dart_backend/renamer.dart

Issue 839063002: Handle platform access through prefix in dart2dart (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Split fixed names into static and dynamic to avoid unneeded prefixes. Created 5 years, 11 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 part of dart_backend; 5 part of dart_backend;
6 6
7 Comparator get _compareNodes => 7 Comparator get _compareNodes =>
8 compareBy((n) => n.getBeginToken().charOffset); 8 compareBy((n) => n.getBeginToken().charOffset);
9 9
10 abstract class Renamable implements Comparable { 10 abstract class Renamable implements Comparable {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 return placeholderRenamer._generateUniqueTopLevelName(""); 65 return placeholderRenamer._generateUniqueTopLevelName("");
66 } 66 }
67 } 67 }
68 68
69 /** 69 /**
70 * Renames only top-level elements that would lead to ambiguity if not renamed. 70 * Renames only top-level elements that would lead to ambiguity if not renamed.
71 */ 71 */
72 class PlaceholderRenamer { 72 class PlaceholderRenamer {
73 /// After running [computeRenames] this will contain the computed renames. 73 /// After running [computeRenames] this will contain the computed renames.
74 final Map<Node, String> renames = new Map<Node, String>(); 74 final Map<Node, String> renames = new Map<Node, String>();
75 /// After running [computeRenames] this will contain the used platform 75 /// After running [computeRenames] this will map the used platform
76 /// libraries. 76 /// libraries to their respective prefixes.
77 final Set<LibraryElement> platformImports = new Set<LibraryElement>(); 77 final Map<LibraryElement, String> platformImports =
78 <LibraryElement, String>{};
78 79
79 final bool enableMinification; 80 final bool enableMinification;
80 final Set<String> fixedMemberNames; 81 final Set<String> fixedDynamicNames;
82 final Set<String> fixedStaticNames;
81 final Map<Element, LibraryElement> reexportingLibraries; 83 final Map<Element, LibraryElement> reexportingLibraries;
82 final bool cutDeclarationTypes; 84 final bool cutDeclarationTypes;
83 85
84 final Map<Entity, String> _renamedCache = new Map<Entity, String>(); 86 final Map<Entity, String> _renamedCache = new Map<Entity, String>();
85 final Map<Entity, Map<String, String>> _privateCache = 87 final Map<Entity, Map<String, String>> _privateCache =
86 new Map<Entity, Map<String, String>>(); 88 new Map<Entity, Map<String, String>>();
87 89
88 // Identifiers that has already been used, or are reserved by the 90 // Identifiers that has already been used, or are reserved by the
89 // language/platform. 91 // language/platform.
90 Set<String> _forbiddenIdentifiers; 92 Set<String> _forbiddenIdentifiers;
91 Set<String> _allNamedParameterIdentifiers; 93 Set<String> _allNamedParameterIdentifiers;
92 94
93 Generator _generator; 95 Generator _generator;
94 96
95 PlaceholderRenamer(this.fixedMemberNames, 97 PlaceholderRenamer(this.fixedDynamicNames,
98 this.fixedStaticNames,
96 this.reexportingLibraries, 99 this.reexportingLibraries,
97 {this.enableMinification, this.cutDeclarationTypes}); 100 {this.enableMinification, this.cutDeclarationTypes});
98 101
99 void _renameNodes(Iterable<Node> nodes, String renamer(Node node)) { 102 void _renameNodes(Iterable<Node> nodes, String renamer(Node node)) {
100 for (Node node in sorted(nodes, _compareNodes)) { 103 for (Node node in sorted(nodes, _compareNodes)) {
101 renames[node] = renamer(node); 104 renames[node] = renamer(node);
102 } 105 }
103 } 106 }
104 107
105 String _generateUniqueTopLevelName(originalName) { 108 String _generateUniqueTopLevelName(String originalName) {
106 String newName = _generator.generate(originalName, (name) { 109 String newName = _generator.generate(originalName, (name) {
107 return _forbiddenIdentifiers.contains(name) || 110 return _forbiddenIdentifiers.contains(name) ||
108 _allNamedParameterIdentifiers.contains(name); 111 _allNamedParameterIdentifiers.contains(name);
109 }); 112 });
110 _forbiddenIdentifiers.add(newName); 113 _forbiddenIdentifiers.add(newName);
111 return newName; 114 return newName;
112 } 115 }
113 116
114 String _generateMemberName(String original) { 117 String _generateMemberName(String original) {
115 return _generator.generate(original, _forbiddenIdentifiers.contains); 118 return _generator.generate(original, _forbiddenIdentifiers.contains);
(...skipping 19 matching lines...) Expand all
135 Elements.isErroneousElement(entity) || 138 Elements.isErroneousElement(entity) ||
136 Elements.isStaticOrTopLevel(entity) || 139 Elements.isStaticOrTopLevel(entity) ||
137 entity is TypeVariableElement); 140 entity is TypeVariableElement);
138 // TODO(smok): We may want to reuse class static field and method names. 141 // TODO(smok): We may want to reuse class static field and method names.
139 if (entity is Element) { 142 if (entity is Element) {
140 LibraryElement library = entity.library; 143 LibraryElement library = entity.library;
141 if (reexportingLibraries.containsKey(entity)) { 144 if (reexportingLibraries.containsKey(entity)) {
142 library = reexportingLibraries[entity]; 145 library = reexportingLibraries[entity];
143 } 146 }
144 if (library.isPlatformLibrary) { 147 if (library.isPlatformLibrary) {
145 if (library.canonicalUri != Compiler.DART_CORE) { 148 // TODO(johnniwinther): Handle prefixes for dart:core.
146 platformImports.add(library); 149 if (library.canonicalUri == Compiler.DART_CORE) return entity.name;
147 }
148 if (library.isInternalLibrary) { 150 if (library.isInternalLibrary) {
149 throw new SpannableAssertionFailure(entity, 151 throw new SpannableAssertionFailure(entity,
150 "Internal library $library should never have been imported from " 152 "Internal library $library should never have been imported from "
151 "the code compiled by dart2dart."); 153 "the code compiled by dart2dart.");
152 } 154 }
155
156 String prefix = platformImports.putIfAbsent(library, () => null);
157 if (entity.isTopLevel &&
158 fixedDynamicNames.contains(entity.name)) {
159 if (prefix == null) {
160 prefix = _generateUniqueTopLevelName('');
161 platformImports[library] = prefix;
162 }
163 return '$prefix.${entity.name}';
164 }
153 return entity.name; 165 return entity.name;
154 } 166 }
155 } 167 }
156 String name = _renamedCache.putIfAbsent(entity, 168 String name = _renamedCache.putIfAbsent(entity,
157 () => _generateUniqueTopLevelName(entity.name)); 169 () => _generateUniqueTopLevelName(entity.name));
158 // Look up in [_renamedCache] for a name for [entity] . 170 // Look up in [_renamedCache] for a name for [entity] .
159 // If it was not renamed before, generate a new name. 171 // If it was not renamed before, generate a new name.
160 return name; 172 return name;
161 } 173 }
162 174
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 /// Finds renamings for all the placeholders in [placeholderCollector] and 266 /// Finds renamings for all the placeholders in [placeholderCollector] and
255 /// stores them in [renames]. 267 /// stores them in [renames].
256 /// Also adds to [platformImports] all the platform-libraries that are used. 268 /// Also adds to [platformImports] all the platform-libraries that are used.
257 void computeRenames(PlaceholderCollector placeholderCollector) { 269 void computeRenames(PlaceholderCollector placeholderCollector) {
258 _allNamedParameterIdentifiers = new Set<String>(); 270 _allNamedParameterIdentifiers = new Set<String>();
259 for (FunctionScope functionScope in 271 for (FunctionScope functionScope in
260 placeholderCollector.functionScopes.values) { 272 placeholderCollector.functionScopes.values) {
261 _allNamedParameterIdentifiers.addAll(functionScope.parameterIdentifiers); 273 _allNamedParameterIdentifiers.addAll(functionScope.parameterIdentifiers);
262 } 274 }
263 275
264 _forbiddenIdentifiers = new Set<String>.from(fixedMemberNames); 276 _forbiddenIdentifiers = new Set<String>.from(fixedDynamicNames);
277 _forbiddenIdentifiers.addAll(fixedStaticNames);
265 _forbiddenIdentifiers.addAll(Keyword.keywords.keys); 278 _forbiddenIdentifiers.addAll(Keyword.keywords.keys);
266 _forbiddenIdentifiers.add('main'); 279 _forbiddenIdentifiers.add('main');
267 280
268 if (enableMinification) { 281 if (enableMinification) {
269 _computeMinifiedRenames(placeholderCollector); 282 _computeMinifiedRenames(placeholderCollector);
270 } else { 283 } else {
271 _computeNonMinifiedRenames(placeholderCollector); 284 _computeNonMinifiedRenames(placeholderCollector);
272 } 285 }
273 286
274 // Rename constructors. 287 // Rename constructors.
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 MinifyingGenerator(); 366 MinifyingGenerator();
354 367
355 String generate(String originalName, bool isForbidden(String name)) { 368 String generate(String originalName, bool isForbidden(String name)) {
356 String result; 369 String result;
357 do { 370 do {
358 result = generateMiniId(index++); 371 result = generateMiniId(index++);
359 } while (isForbidden(result)); 372 } while (isForbidden(result));
360 return result; 373 return result;
361 } 374 }
362 } 375 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698