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

Side by Side Diff: lib/compiler/implementation/js_backend/namer.dart

Issue 10915104: Move namer into javascript backend. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 * Assigns JavaScript identifiers to Dart variables, class-names and members. 6 * Assigns JavaScript identifiers to Dart variables, class-names and members.
7 */ 7 */
8 class Namer { 8 class Namer {
9 final Compiler compiler; 9 final Compiler compiler;
10 10
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 // We sometimes handle continue targets differently from break targets, 54 // We sometimes handle continue targets differently from break targets,
55 // so we have special continue-only labels. 55 // so we have special continue-only labels.
56 String continueLabelName(LabelElement label) { 56 String continueLabelName(LabelElement label) {
57 return 'c\$${label.labelName}\$${label.target.nestingLevel}'; 57 return 'c\$${label.labelName}\$${label.target.nestingLevel}';
58 } 58 }
59 59
60 String implicitContinueLabelName(TargetElement target) { 60 String implicitContinueLabelName(TargetElement target) {
61 return 'c\$${target.nestingLevel}'; 61 return 'c\$${target.nestingLevel}';
62 } 62 }
63 63
64 /** Returns a non-unique name for the given closure element. */
65 String closureName(Element element) {
66 List<String> parts = <String>[];
67 SourceString ownName = element.name;
68 if (ownName == null || ownName.stringValue == "") {
69 parts.add("anon");
70 } else {
71 parts.add(ownName.slowToString());
72 }
73 for (Element enclosingElement = element.enclosingElement;
74 enclosingElement != null &&
75 (enclosingElement.kind === ElementKind.GENERATIVE_CONSTRUCTOR_BODY
76 || enclosingElement.kind === ElementKind.CLASS
77 || enclosingElement.kind === ElementKind.FUNCTION
78 || enclosingElement.kind === ElementKind.GETTER
79 || enclosingElement.kind === ElementKind.SETTER);
80 enclosingElement = enclosingElement.enclosingElement) {
81 SourceString surroundingName = enclosingElement.name;
82 if (surroundingName != null) {
83 String surroundingNameString = surroundingName.slowToString();
84 if (surroundingNameString != "") parts.add(surroundingNameString);
85 }
86 }
87 // Invert the parts.
88 for (int i = 0, j = parts.length - 1; i < j; i++, j--) {
89 var tmp = parts[i];
90 parts[i] = parts[j];
91 parts[j] = tmp;
92 }
93 return safeName(Strings.join(parts, "_"));
94 }
95
96 String privateName(LibraryElement lib, SourceString name) { 64 String privateName(LibraryElement lib, SourceString name) {
97 if (name.isPrivate()) { 65 if (name.isPrivate()) {
98 String nameString = name.slowToString(); 66 String nameString = name.slowToString();
99 // The first library asking for a short private name wins. 67 // The first library asking for a short private name wins.
100 LibraryElement owner = 68 LibraryElement owner =
101 shortPrivateNameOwners.putIfAbsent(nameString, () => lib); 69 shortPrivateNameOwners.putIfAbsent(nameString, () => lib);
102 // If a private name could clash with a mangled private name we don't 70 // If a private name could clash with a mangled private name we don't
103 // use the short name. For example a private name "_lib3_foo" would 71 // use the short name. For example a private name "_lib3_foo" would
104 // clash with "_foo" from "lib3". 72 // clash with "_foo" from "lib3".
105 if (owner === lib && !nameString.startsWith('_$LIBRARY_PREFIX')) { 73 if (owner === lib && !nameString.startsWith('_$LIBRARY_PREFIX')) {
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 } 272 }
305 273
306 String safeName(String name) { 274 String safeName(String name) {
307 if (jsReserved.contains(name) || name.startsWith('\$')) { 275 if (jsReserved.contains(name) || name.startsWith('\$')) {
308 name = "\$$name"; 276 name = "\$$name";
309 assert(!jsReserved.contains(name)); 277 assert(!jsReserved.contains(name));
310 } 278 }
311 return name; 279 return name;
312 } 280 }
313 } 281 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698