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

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: 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 instanceMethodName(LibraryElement lib, SourceString name, int arity) {
41 // TODO(floitsch): mangle, while preserving uniqueness. 42 // TODO(floitsch): mangle, while preserving uniqueness.
42 return '${name.slowToString()}\$$arity'; 43 if (name.isPrivate()) {
kasperl 2012/03/13 13:43:51 How about refactoring this pattern?
ahe 2012/03/17 22:19:44 Done.
44 return '_${getName(lib)}${name.slowToString()}\$$arity';
45 } else {
46 return '${name.slowToString()}\$$arity';
47 }
43 } 48 }
44 49
45 String instanceMethodInvocationName(SourceString name, Selector selector) { 50 String instanceMethodInvocationName(LibraryElement lib, SourceString name,
51 Selector selector) {
46 // TODO(floitsch): mangle, while preserving uniqueness. 52 // TODO(floitsch): mangle, while preserving uniqueness.
47 StringBuffer buffer = new StringBuffer(); 53 StringBuffer buffer = new StringBuffer();
48 List<SourceString> names = selector.getOrderedNamedArguments(); 54 List<SourceString> names = selector.getOrderedNamedArguments();
49 for (SourceString name in names) { 55 for (SourceString name in names) {
50 buffer.add(@'$'); 56 buffer.add(@'$');
51 name.printOn(buffer); 57 name.printOn(buffer);
52 } 58 }
53 return '${name.slowToString()}\$${selector.argumentCount}$buffer'; 59 if (name.isPrivate()) {
60 return '_${getName(lib)}${name.slowToString()}\$${selector.argumentCount}$ buffer';
ngeoffray 2012/03/13 12:47:11 line too long
ahe 2012/03/17 22:19:44 Done.
61 } else {
62 return '${name.slowToString()}\$${selector.argumentCount}$buffer';
63 }
54 } 64 }
55 65
56 String instanceFieldName(SourceString name) { 66 String instanceFieldName(LibraryElement lib, SourceString name) {
57 return name.slowToString(); 67 if (name.isPrivate()) {
68 return '_${getName(lib)}${name.slowToString()}';
69 } else {
70 return name.slowToString();
71 }
58 } 72 }
59 73
60 String setterName(SourceString name) { 74 String setterName(LibraryElement lib, SourceString name) {
61 return 'set\$${name.slowToString()}'; 75 if (name.isPrivate()) {
76 return 'set\$_${getName(lib)}${name.slowToString()}';
77 } else {
78 return 'set\$${name.slowToString()}';
79 }
62 } 80 }
63 81
64 String getterName(SourceString name) { 82 String getterName(LibraryElement lib, SourceString name) {
65 return 'get\$${name.slowToString()}'; 83 if (name.isPrivate()) {
84 return 'get\$_${getName(lib)}${name.slowToString()}';
85 } else {
86 return 'get\$${name.slowToString()}';
87 }
66 } 88 }
67 89
68 String getFreshGlobalName(String proposedName) { 90 String getFreshGlobalName(String proposedName) {
69 int usedCount = usedGlobals[proposedName]; 91 int usedCount = usedGlobals[proposedName];
70 if (usedCount === null) { 92 if (usedCount === null) {
71 // No element with this name has been used before. 93 // No element with this name has been used before.
72 usedGlobals[proposedName] = 1; 94 usedGlobals[proposedName] = 1;
73 return proposedName; 95 return proposedName;
74 } else { 96 } else {
75 // Not the first time we see this name. Append a number to make it unique. 97 // Not the first time we see this name. Append a number to make it unique.
76 String name; 98 String name;
77 do { 99 do {
78 usedCount++; 100 usedCount++;
79 name = '$proposedName$usedCount'; 101 name = '$proposedName$usedCount';
80 } while (usedGlobals[name] !== null); 102 } while (usedGlobals[name] !== null);
81 usedGlobals[proposedName] = usedCount; 103 usedGlobals[proposedName] = usedCount;
82 return name; 104 return name;
83 } 105 }
84 } 106 }
85 107
86 /** 108 /**
87 * Returns a preferred JS-id for the given top-level or static element. 109 * 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. 110 * The returned id is guaranteed to be a valid JS-id.
89 */ 111 */
90 String _computeGuess(Element element) { 112 String _computeGuess(Element element) {
91 assert(!element.isInstanceMember()); 113 assert(!element.isInstanceMember());
114 LibraryElement lib = element.getLibrary();
92 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { 115 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) {
93 FunctionElement functionElement = element; 116 FunctionElement functionElement = element;
94 return instanceMethodName( 117 return instanceMethodName(lib, element.name,
95 element.name, functionElement.parameterCount(compiler)); 118 functionElement.parameterCount(compiler));
96 } else { 119 } else {
97 // TODO(floitsch): deal with named constructors. 120 // TODO(floitsch): deal with named constructors.
98 String name; 121 String name;
99 if (element.kind == ElementKind.GETTER) { 122 if (element.kind == ElementKind.GETTER) {
100 name = getterName(element.name); 123 name = getterName(lib, element.name);
floitsch 2012/03/13 13:28:56 I agree that this makes the code maybe even simple
ahe 2012/03/13 15:48:58 Good point, I didn't think about that. Actually,
101 } else if (element.kind == ElementKind.SETTER) { 124 } else if (element.kind == ElementKind.SETTER) {
102 name = setterName(element.name); 125 name = setterName(lib, element.name);
103 } else if (element.kind == ElementKind.FUNCTION) { 126 } else if (element.kind == ElementKind.FUNCTION) {
104 FunctionElement functionElement = element; 127 FunctionElement functionElement = element;
105 name = element.name.slowToString(); 128 name = element.name.slowToString();
106 name = '$name\$${functionElement.parameterCount(compiler)}'; 129 name = '$name\$${functionElement.parameterCount(compiler)}';
floitsch 2012/03/13 13:28:56 Actually we don't need to care for the parameterCo
ahe 2012/03/13 15:48:58 Another good point I didn't think about.
130 } else if (element.kind === ElementKind.LIBRARY) {
131 name = 'lib';
ngeoffray 2012/03/13 12:47:11 Very surprised by this, it should not happen.
ahe 2012/03/13 15:48:58 It is because I started using getName on libraries
107 } else { 132 } else {
108 name = '${element.name.slowToString()}'; 133 name = '${element.name.slowToString()}';
109 } 134 }
110 // Prefix the name with '$' if it is reserved. 135 // Prefix the name with '$' if it is reserved.
111 if (jsReserved.contains(name)) { 136 if (jsReserved.contains(name)) {
112 name = "\$$name"; 137 name = "\$$name";
113 assert(!jsReserved.contains(name)); 138 assert(!jsReserved.contains(name));
114 } 139 }
115 return name; 140 return name;
116 } 141 }
117 } 142 }
118 143
119 String getBailoutName(Element element) { 144 String getBailoutName(Element element) {
120 return '${getName(element)}\$bailout'; 145 return '${getName(element)}\$bailout';
121 } 146 }
122 147
123 /** 148 /**
124 * Returns a preferred JS-id for the given element. The returned id is 149 * 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 150 * guaranteed to be a valid JS-id. Globals and static fields are furthermore
126 * guaranteed to be unique. 151 * guaranteed to be unique.
127 * 152 *
128 * For accessing statics consider calling 153 * For accessing statics consider calling
129 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. 154 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead.
130 */ 155 */
131 String getName(Element element) { 156 String getName(Element element) {
132 if (element.isInstanceMember()) { 157 if (element.isInstanceMember()) {
133 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) { 158 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) {
134 ConstructorBodyElement bodyElement = element; 159 ConstructorBodyElement bodyElement = element;
135 SourceString name = bodyElement.constructor.name; 160 SourceString name = bodyElement.constructor.name;
136 return instanceMethodName(name, bodyElement.parameterCount(compiler)); 161 return instanceMethodName(element.getLibrary(),
162 name, bodyElement.parameterCount(compiler));
137 } else if (element.kind == ElementKind.FUNCTION) { 163 } else if (element.kind == ElementKind.FUNCTION) {
138 FunctionElement functionElement = element; 164 FunctionElement functionElement = element;
139 return instanceMethodName( 165 return instanceMethodName(element.getLibrary(),
140 element.name, functionElement.parameterCount(compiler)); 166 element.name,
167 functionElement.parameterCount(compiler));
141 } else if (element.kind == ElementKind.GETTER) { 168 } else if (element.kind == ElementKind.GETTER) {
142 return getterName(element.name); 169 return getterName(element.getLibrary(), element.name);
143 } else if (element.kind == ElementKind.SETTER) { 170 } else if (element.kind == ElementKind.SETTER) {
144 return setterName(element.name); 171 return setterName(element.getLibrary(), element.name);
145 } else { 172 } else {
146 return instanceFieldName(element.name); 173 return instanceFieldName(element.getLibrary(), element.name);
147 } 174 }
148 } else { 175 } else {
149 // Dealing with a top-level or static element. 176 // Dealing with a top-level or static element.
150 String cached = globals[element]; 177 String cached = globals[element];
151 if (cached !== null) return cached; 178 if (cached !== null) return cached;
152 179
153 String guess = _computeGuess(element); 180 String guess = _computeGuess(element);
154 switch (element.kind) { 181 switch (element.kind) {
155 case ElementKind.VARIABLE: 182 case ElementKind.VARIABLE:
156 case ElementKind.PARAMETER: 183 case ElementKind.PARAMETER:
157 // The name is not guaranteed to be unique. 184 // The name is not guaranteed to be unique.
158 return guess; 185 return guess;
159 186
160 case ElementKind.GENERATIVE_CONSTRUCTOR: 187 case ElementKind.GENERATIVE_CONSTRUCTOR:
161 case ElementKind.FUNCTION: 188 case ElementKind.FUNCTION:
162 case ElementKind.CLASS: 189 case ElementKind.CLASS:
163 case ElementKind.FIELD: 190 case ElementKind.FIELD:
164 case ElementKind.GETTER: 191 case ElementKind.GETTER:
165 case ElementKind.SETTER: 192 case ElementKind.SETTER:
166 case ElementKind.TYPEDEF: 193 case ElementKind.TYPEDEF:
194 case ElementKind.LIBRARY:
ngeoffray 2012/03/13 12:47:11 ditto.
167 String result = getFreshGlobalName(guess); 195 String result = getFreshGlobalName(guess);
168 globals[element] = result; 196 globals[element] = result;
169 return result; 197 return result;
170 198
171 default: 199 default:
172 compiler.internalError('getName for unknown kind: ${element.kind}', 200 compiler.internalError('getName for unknown kind: ${element.kind}',
173 node: element.parseNode(compiler)); 201 node: element.parseNode(compiler));
174 } 202 }
175 } 203 }
176 } 204 }
(...skipping 11 matching lines...) Expand all
188 } 216 }
189 217
190 String isolateBailoutAccess(Element element) { 218 String isolateBailoutAccess(Element element) {
191 return '${isolateAccess(element)}\$bailout'; 219 return '${isolateAccess(element)}\$bailout';
192 } 220 }
193 221
194 String operatorIs(Element element) { 222 String operatorIs(Element element) {
195 return 'is\$${getName(element)}'; 223 return 'is\$${getName(element)}';
196 } 224 }
197 } 225 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698