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

Side by Side Diff: dart/frog/leg/namer.dart

Issue 9689045: Library privacy. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Address review comments Created 8 years, 9 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 16 matching lines...) Expand all
27 Namer(this.compiler) 27 Namer(this.compiler)
28 : globals = new Map<Element, String>(), 28 : globals = new Map<Element, String>(),
29 usedGlobals = new Map<String, int>(); 29 usedGlobals = new Map<String, int>();
30 30
31 final String CURRENT_ISOLATE = "\$"; 31 final String CURRENT_ISOLATE = "\$";
32 final String ISOLATE = "Isolate"; 32 final String ISOLATE = "Isolate";
33 33
34 34
35 String closureInvocationName(Selector selector) { 35 String closureInvocationName(Selector selector) {
36 // TODO(floitsch): mangle, while not conflicting with instance names. 36 // TODO(floitsch): mangle, while not conflicting with instance names.
37 return instanceMethodInvocationName(CLOSURE_INVOCATION_NAME, selector); 37 return instanceMethodInvocationName(null, CLOSURE_INVOCATION_NAME,
38 selector);
38 } 39 }
39 40
40 String instanceMethodName(SourceString name, int arity) { 41 String privateName(LibraryElement lib, SourceString name) {
41 // TODO(floitsch): mangle, while preserving uniqueness. 42 if (name.isPrivate()) {
42 return '${name.slowToString()}\$$arity'; 43 return '_${getName(lib)}${name.slowToString()}';
floitsch 2012/03/18 18:29:30 Unless I'm wrong this means that a user can "fake"
ahe 2012/03/18 19:17:05 I think I covered that by prefixing with _
44 } else {
45 return '${name.slowToString()}';
46 }
43 } 47 }
44 48
45 String instanceMethodInvocationName(SourceString name, Selector selector) { 49 String instanceMethodName(LibraryElement lib, SourceString name, int arity) {
50 // TODO(floitsch): mangle, while preserving uniqueness.
floitsch 2012/03/18 18:29:30 I think you can remove the TODO here.
ahe 2012/03/18 21:12:04 Done.
51 return '${privateName(lib, name)}\$$arity';
52 }
53
54 String instanceMethodInvocationName(LibraryElement lib, SourceString name,
55 Selector selector) {
46 // TODO(floitsch): mangle, while preserving uniqueness. 56 // TODO(floitsch): mangle, while preserving uniqueness.
47 StringBuffer buffer = new StringBuffer(); 57 StringBuffer buffer = new StringBuffer();
48 List<SourceString> names = selector.getOrderedNamedArguments(); 58 List<SourceString> names = selector.getOrderedNamedArguments();
49 for (SourceString argumentName in names) { 59 for (SourceString argumentName in names) {
50 buffer.add(@'$'); 60 buffer.add(@'$');
51 argumentName.printOn(buffer); 61 argumentName.printOn(buffer);
52 } 62 }
53 return '${name.slowToString()}\$${selector.argumentCount}$buffer'; 63 return '${privateName(lib, name)}\$${selector.argumentCount}$buffer';
54 } 64 }
55 65
56 String instanceFieldName(SourceString name) { 66 String instanceFieldName(LibraryElement lib, SourceString name) {
57 return name.slowToString(); 67 return privateName(lib, name);
58 } 68 }
59 69
60 String setterName(SourceString name) { 70 String setterName(LibraryElement lib, SourceString name) {
61 return 'set\$${name.slowToString()}'; 71 return 'set\$${privateName(lib, name)}';
62 } 72 }
63 73
64 String getterName(SourceString name) { 74 String getterName(LibraryElement lib, SourceString name) {
65 return 'get\$${name.slowToString()}'; 75 return 'get\$${privateName(lib, name)}';
66 } 76 }
67 77
68 String getFreshGlobalName(String proposedName) { 78 String getFreshGlobalName(String proposedName) {
69 int usedCount = usedGlobals[proposedName]; 79 int usedCount = usedGlobals[proposedName];
70 if (usedCount === null) { 80 if (usedCount === null) {
71 // No element with this name has been used before. 81 // No element with this name has been used before.
72 usedGlobals[proposedName] = 1; 82 usedGlobals[proposedName] = 1;
73 return proposedName; 83 return proposedName;
74 } else { 84 } else {
75 // Not the first time we see this name. Append a number to make it unique. 85 // Not the first time we see this name. Append a number to make it unique.
76 String name; 86 String name;
77 do { 87 do {
78 usedCount++; 88 usedCount++;
79 name = '$proposedName$usedCount'; 89 name = '$proposedName$usedCount';
80 } while (usedGlobals[name] !== null); 90 } while (usedGlobals[name] !== null);
81 usedGlobals[proposedName] = usedCount; 91 usedGlobals[proposedName] = usedCount;
82 return name; 92 return name;
83 } 93 }
84 } 94 }
85 95
86 /** 96 /**
87 * Returns a preferred JS-id for the given top-level or static element. 97 * Returns a preferred JS-id for the given top-level or static element.
88 * The returned id is guaranteed to be a valid JS-id. 98 * The returned id is guaranteed to be a valid JS-id.
89 */ 99 */
90 String _computeGuess(Element element) { 100 String _computeGuess(Element element) {
91 assert(!element.isInstanceMember()); 101 assert(!element.isInstanceMember());
102 LibraryElement lib = element.getLibrary();
92 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { 103 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) {
93 FunctionElement functionElement = element; 104 FunctionElement functionElement = element;
94 return instanceMethodName( 105 return instanceMethodName(lib, element.name,
95 element.name, functionElement.parameterCount(compiler)); 106 functionElement.parameterCount(compiler));
96 } else { 107 } else {
97 // TODO(floitsch): deal with named constructors. 108 // TODO(floitsch): deal with named constructors.
98 String name; 109 String name;
99 if (element.kind == ElementKind.GETTER) { 110 if (Elements.isStaticOrTopLevel(element)) {
100 name = getterName(element.name); 111 name = element.name.slowToString();
112 } else if (element.kind == ElementKind.GETTER) {
113 name = getterName(lib, element.name);
101 } else if (element.kind == ElementKind.SETTER) { 114 } else if (element.kind == ElementKind.SETTER) {
102 name = setterName(element.name); 115 name = setterName(lib, element.name);
103 } else if (element.kind == ElementKind.FUNCTION) { 116 } else if (element.kind == ElementKind.FUNCTION) {
104 FunctionElement functionElement = element; 117 FunctionElement functionElement = element;
105 name = element.name.slowToString(); 118 name = element.name.slowToString();
106 name = '$name\$${functionElement.parameterCount(compiler)}'; 119 name = '$name\$${functionElement.parameterCount(compiler)}';
120 } else if (element.kind === ElementKind.LIBRARY) {
121 name = 'lib';
107 } else { 122 } else {
108 name = '${element.name.slowToString()}'; 123 name = element.name.slowToString();
109 } 124 }
110 // Prefix the name with '$' if it is reserved. 125 // Prefix the name with '$' if it is reserved.
111 if (jsReserved.contains(name)) { 126 if (jsReserved.contains(name)) {
112 name = "\$$name"; 127 name = "\$$name";
113 assert(!jsReserved.contains(name)); 128 assert(!jsReserved.contains(name));
114 } 129 }
115 return name; 130 return name;
116 } 131 }
117 } 132 }
118 133
119 String getBailoutName(Element element) { 134 String getBailoutName(Element element) {
120 return '${getName(element)}\$bailout'; 135 return '${getName(element)}\$bailout';
121 } 136 }
122 137
123 /** 138 /**
124 * Returns a preferred JS-id for the given element. The returned id is 139 * Returns a preferred JS-id for the given element. The returned id is
125 * guaranteed to be a valid JS-id. Globals and static fields are furthermore 140 * guaranteed to be a valid JS-id. Globals and static fields are furthermore
126 * guaranteed to be unique. 141 * guaranteed to be unique.
127 * 142 *
128 * For accessing statics consider calling 143 * For accessing statics consider calling
129 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. 144 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead.
130 */ 145 */
131 String getName(Element element) { 146 String getName(Element element) {
132 if (element.isInstanceMember()) { 147 if (element.isInstanceMember()) {
133 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) { 148 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) {
134 ConstructorBodyElement bodyElement = element; 149 ConstructorBodyElement bodyElement = element;
135 SourceString name = bodyElement.constructor.name; 150 SourceString name = bodyElement.constructor.name;
136 return instanceMethodName(name, bodyElement.parameterCount(compiler)); 151 return instanceMethodName(element.getLibrary(),
152 name, bodyElement.parameterCount(compiler));
137 } else if (element.kind == ElementKind.FUNCTION) { 153 } else if (element.kind == ElementKind.FUNCTION) {
138 FunctionElement functionElement = element; 154 FunctionElement functionElement = element;
139 return instanceMethodName( 155 return instanceMethodName(element.getLibrary(),
140 element.name, functionElement.parameterCount(compiler)); 156 element.name,
157 functionElement.parameterCount(compiler));
141 } else if (element.kind == ElementKind.GETTER) { 158 } else if (element.kind == ElementKind.GETTER) {
142 return getterName(element.name); 159 return getterName(element.getLibrary(), element.name);
143 } else if (element.kind == ElementKind.SETTER) { 160 } else if (element.kind == ElementKind.SETTER) {
144 return setterName(element.name); 161 return setterName(element.getLibrary(), element.name);
145 } else { 162 } else {
146 return instanceFieldName(element.name); 163 return instanceFieldName(element.getLibrary(), element.name);
147 } 164 }
148 } else { 165 } else {
149 // Dealing with a top-level or static element. 166 // Dealing with a top-level or static element.
150 String cached = globals[element]; 167 String cached = globals[element];
151 if (cached !== null) return cached; 168 if (cached !== null) return cached;
152 169
153 String guess = _computeGuess(element); 170 String guess = _computeGuess(element);
154 switch (element.kind) { 171 switch (element.kind) {
155 case ElementKind.VARIABLE: 172 case ElementKind.VARIABLE:
156 case ElementKind.PARAMETER: 173 case ElementKind.PARAMETER:
157 // The name is not guaranteed to be unique. 174 // The name is not guaranteed to be unique.
158 return guess; 175 return guess;
159 176
160 case ElementKind.GENERATIVE_CONSTRUCTOR: 177 case ElementKind.GENERATIVE_CONSTRUCTOR:
161 case ElementKind.FUNCTION: 178 case ElementKind.FUNCTION:
162 case ElementKind.CLASS: 179 case ElementKind.CLASS:
163 case ElementKind.FIELD: 180 case ElementKind.FIELD:
164 case ElementKind.GETTER: 181 case ElementKind.GETTER:
165 case ElementKind.SETTER: 182 case ElementKind.SETTER:
166 case ElementKind.TYPEDEF: 183 case ElementKind.TYPEDEF:
184 case ElementKind.LIBRARY:
167 String result = getFreshGlobalName(guess); 185 String result = getFreshGlobalName(guess);
168 globals[element] = result; 186 globals[element] = result;
169 return result; 187 return result;
170 188
171 default: 189 default:
172 compiler.internalError('getName for unknown kind: ${element.kind}', 190 compiler.internalError('getName for unknown kind: ${element.kind}',
173 node: element.parseNode(compiler)); 191 node: element.parseNode(compiler));
174 } 192 }
175 } 193 }
176 } 194 }
(...skipping 11 matching lines...) Expand all
188 } 206 }
189 207
190 String isolateBailoutAccess(Element element) { 208 String isolateBailoutAccess(Element element) {
191 return '${isolateAccess(element)}\$bailout'; 209 return '${isolateAccess(element)}\$bailout';
192 } 210 }
193 211
194 String operatorIs(Element element) { 212 String operatorIs(Element element) {
195 return 'is\$${getName(element)}'; 213 return 'is\$${getName(element)}';
196 } 214 }
197 } 215 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698