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

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

Issue 9193016: Add the arity to calls, and support noSuchMethodException. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 8 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) 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 11 matching lines...) Expand all
22 Map<String, int> usedGlobals; 22 Map<String, int> usedGlobals;
23 23
24 Namer(this.compiler) 24 Namer(this.compiler)
25 : globals = new Map<Element, String>(), 25 : globals = new Map<Element, String>(),
26 usedGlobals = new Map<String, int>(); 26 usedGlobals = new Map<String, int>();
27 27
28 final String CURRENT_ISOLATE = "\$"; 28 final String CURRENT_ISOLATE = "\$";
29 final String ISOLATE = "Isolate"; 29 final String ISOLATE = "Isolate";
30 30
31 31
32 String closureInvocationName() { 32 String closureInvocationName(int arity) {
33 // TODO(floitsch): mangle, while not conflicting with instance names. 33 // TODO(floitsch): mangle, while not conflicting with instance names.
34 return '\$call'; 34 return '\$call\$$arity';
floitsch 2012/01/24 11:46:03 return instanceMethodeName(const SourceString(@'$c
ngeoffray 2012/01/24 12:09:39 Done.
35 } 35 }
36 36
37 String instanceName(SourceString name) { 37 String instanceMethodName(SourceString name, int arity) {
38 String candidate = '$name';
39 // TODO(floitsch): mangle, while preserving uniqueness. 38 // TODO(floitsch): mangle, while preserving uniqueness.
40 return candidate; 39 return '$name\$$arity';
40 }
41
42 String instanceFieldName(SourceString name) {
43 return '$name';
41 } 44 }
42 45
43 String setterName(SourceString name) { 46 String setterName(SourceString name) {
44 return 'set\$$name'; 47 return 'set\$$name';
45 } 48 }
46 49
47 String getterName(SourceString name) { 50 String getterName(SourceString name) {
48 return 'get\$$name'; 51 return 'get\$$name';
49 } 52 }
50 53
51 /** 54 /**
52 * The constructor-body name is computed from the corresponding 55 * Returns a preferred JS-id for the given top-level or static element.
53 * constructor element because, in the case of a super-initialization, the 56 * The returned id is guaranteed to be a valid JS-id.
54 * body element is not accessible.
55 */
56 String constructorBodyName(Element element) {
57 assert(element.kind == ElementKind.GENERATIVE_CONSTRUCTOR);
58 // TODO(floitsch): the constructor-body name must not conflict with other
59 // instance fields.
60 // TOD(floitsch): deal with named constructors.
61 return instanceName(element.name);
62 }
63
64 /**
65 * Returns a preferred JS-id for the given element. The returned id is
66 * guaranteed to be a valid JS-id.
67 *
68 * For instance-members the returned strings are guaranteed not to clash. For
69 * static variables there might be clashes. In the latter case the caller
70 * needs to ensure uniqueness.
71 */ 57 */
72 String _computeGuess(Element element) { 58 String _computeGuess(Element element) {
73 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) { 59 assert(!element.isInstanceMember());
74 ConstructorBodyElement bodyElement = element; 60 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) {
75 return constructorBodyName(bodyElement.constructor); 61 SourceString name = getConstructorName(element);
62 return instanceMethodName(name, element.parameterCount(compiler));
63 } else {
64 // TODO(floitsch): deal with named constructors.
65 String name = '${element.name}';
66 if (element.kind == ElementKind.FUNCTION) {
67 FunctionElement functionElement = element;
68 name = '$name\$${functionElement.parameterCount(compiler)}';
69 }
70 // Prefix the name with '$' if it is reserved.
71 if (jsReserved.contains(name)) {
72 name = "\$$name";
73 assert(!jsReserved.contains(name));
74 }
75 return name;
76 } 76 }
77
78 if (element.isInstanceMember()) return instanceName(element.name);
79
80 // TODO(floitsch): deal with named constructors.
81 String name = '${element.name}';
82 // Prefix the name with '$' if it is reserved.
83 if (jsReserved.contains(name)) {
84 name = "\$$name";
85 assert(!jsReserved.contains(name));
86 }
87 return name;
88 } 77 }
89 78
90 String getBailoutName(Element element) { 79 String getBailoutName(Element element) {
91 return '${getName(element)}\$bailout'; 80 return '${getName(element)}\$bailout';
92 } 81 }
93 82
94 SourceString getConstructorName(FunctionElement constructor) { 83 SourceString getConstructorName(FunctionElement constructor) {
95 String dartName = constructor.name.stringValue; 84 String dartName = constructor.name.stringValue;
96 return new SourceString(dartName.replaceFirst('\.', '\$')); 85 return new SourceString(dartName.replaceFirst('\.', '\$'));
97 } 86 }
98 87
99 /** 88 /**
100 * Returns a preferred JS-id for the given element. The returned id is 89 * Returns a preferred JS-id for the given element. The returned id is
101 * guaranteed to be a valid JS-id. Globals and static fields are furthermore 90 * guaranteed to be a valid JS-id. Globals and static fields are furthermore
102 * guaranteed to be unique. 91 * guaranteed to be unique.
103 * 92 *
104 * For accessing statics consider calling 93 * For accessing statics consider calling
105 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. 94 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead.
106 */ 95 */
107 String getName(Element element) { 96 String getName(Element element) {
108 if (element.isInstanceMember()) { 97 if (element.isInstanceMember()) {
109 SourceString name; 98 SourceString name;
110 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) { 99 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) {
111 ConstructorBodyElement bodyElement = element; 100 ConstructorBodyElement bodyElement = element;
112 name = getConstructorName(bodyElement.constructor); 101 SourceString name = getConstructorName(bodyElement.constructor);
102 return instanceMethodName(name, bodyElement.parameterCount(compiler));
103 } else if (element.kind == ElementKind.FUNCTION) {
104 FunctionElement functionElement = element;
105 int parameterCount = functionElement.parameterCount(compiler);
106 return instanceMethodName(element.name, parameterCount);
113 } else { 107 } else {
114 name = element.name; 108 return instanceFieldName(element.name);
115 } 109 }
116 return instanceName(name); 110 } else {
117 } 111 // Dealing with a top-level or static element.
118 String cached = globals[element]; 112 String cached = globals[element];
119 if (cached !== null) return cached; 113 if (cached !== null) return cached;
120 114
121 String guess; 115 String guess = _computeGuess(element);
122 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { 116 switch (element.kind) {
123 guess = getConstructorName(element).stringValue; 117 case ElementKind.VARIABLE:
124 } else { 118 case ElementKind.PARAMETER:
125 guess = _computeGuess(element); 119 // The name is not guaranteed to be unique.
126 } 120 return guess;
127 switch (element.kind) {
128 case ElementKind.VARIABLE:
129 case ElementKind.PARAMETER:
130 // The name is not guaranteed to be unique.
131 return guess;
132 121
133 case ElementKind.GENERATIVE_CONSTRUCTOR: 122 case ElementKind.GENERATIVE_CONSTRUCTOR:
134 case ElementKind.FUNCTION: 123 case ElementKind.FUNCTION:
135 case ElementKind.CLASS: 124 case ElementKind.CLASS:
136 case ElementKind.FIELD: 125 case ElementKind.FIELD:
137 // We need to make sure the name is unique. 126 // We need to make sure the name is unique.
138 int usedCount = usedGlobals[guess]; 127 int usedCount = usedGlobals[guess];
139 if (usedCount === null) { 128 if (usedCount === null) {
140 // No element with this name has been used before. 129 // No element with this name has been used before.
141 usedGlobals[guess] = 1; 130 usedGlobals[guess] = 1;
142 globals[element] = guess; 131 globals[element] = guess;
143 return guess; 132 return guess;
144 } else { 133 } else {
145 // Not the first time we see an element with this name. Append a 134 // Not the first time we see an element with this name. Append a
146 // number to make it unique. 135 // number to make it unique.
147 String name; 136 String name;
148 do { 137 do {
149 usedCount++; 138 usedCount++;
150 name = '$guess$usedCount'; 139 name = '$guess$usedCount';
151 } while (usedGlobals[name] !== null); 140 } while (usedGlobals[name] !== null);
152 usedGlobals[guess] = usedCount; 141 usedGlobals[guess] = usedCount;
153 globals[element] = name; 142 globals[element] = name;
154 return name; 143 return name;
155 } 144 }
156 145
157 default: 146 default:
158 compiler.internalError('getName for unknown kind: ${element.kind}', 147 compiler.internalError('getName for unknown kind: ${element.kind}',
159 node: element.parseNode(compiler, compiler)); 148 node: element.parseNode(compiler, compiler));
149 }
160 } 150 }
161 } 151 }
162 152
163 String isolateAccess(Element element) { 153 String isolateAccess(Element element) {
164 return "$CURRENT_ISOLATE.${getName(element)}"; 154 return "$CURRENT_ISOLATE.${getName(element)}";
165 } 155 }
166 156
167 String isolatePropertyAccess(Element element) { 157 String isolatePropertyAccess(Element element) {
168 return "$ISOLATE.prototype.${getName(element)}"; 158 return "$ISOLATE.prototype.${getName(element)}";
169 } 159 }
170 160
171 String isolateBailoutPropertyAccess(Element element) { 161 String isolateBailoutPropertyAccess(Element element) {
172 return '${isolatePropertyAccess(element)}\$bailout'; 162 return '${isolatePropertyAccess(element)}\$bailout';
173 } 163 }
174 164
175 String isolateBailoutAccess(Element element) { 165 String isolateBailoutAccess(Element element) {
176 return '${isolateAccess(element)}\$bailout'; 166 return '${isolateAccess(element)}\$bailout';
177 } 167 }
178 168
179 String operatorIs(ClassElement element) { 169 String operatorIs(ClassElement element) {
180 return 'is\$${getName(element)}'; 170 return 'is\$${getName(element)}';
181 } 171 }
182 } 172 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698