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

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
« no previous file with comments | « frog/leg/lib/core.dart ('k') | frog/leg/ssa/builder.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
11 static final CLOSURE_INVOCATION_NAME = const SourceString('\$call');
12
11 static Set<String> _jsReserved = null; 13 static Set<String> _jsReserved = null;
12 Set<String> get jsReserved() { 14 Set<String> get jsReserved() {
13 if (_jsReserved === null) { 15 if (_jsReserved === null) {
14 _jsReserved = new Set<String>(); 16 _jsReserved = new Set<String>();
15 _jsReserved.addAll(JsNames.javaScriptKeywords); 17 _jsReserved.addAll(JsNames.javaScriptKeywords);
16 _jsReserved.addAll(JsNames.reservedPropertySymbols); 18 _jsReserved.addAll(JsNames.reservedPropertySymbols);
17 } 19 }
18 return _jsReserved; 20 return _jsReserved;
19 } 21 }
20 22
21 Map<Element, String> globals; 23 Map<Element, String> globals;
22 Map<String, int> usedGlobals; 24 Map<String, int> usedGlobals;
23 25
24 Namer(this.compiler) 26 Namer(this.compiler)
25 : globals = new Map<Element, String>(), 27 : globals = new Map<Element, String>(),
26 usedGlobals = new Map<String, int>(); 28 usedGlobals = new Map<String, int>();
27 29
28 final String CURRENT_ISOLATE = "\$"; 30 final String CURRENT_ISOLATE = "\$";
29 final String ISOLATE = "Isolate"; 31 final String ISOLATE = "Isolate";
30 32
31 33
32 String closureInvocationName() { 34 String closureInvocationName(int arity) {
33 // TODO(floitsch): mangle, while not conflicting with instance names. 35 // TODO(floitsch): mangle, while not conflicting with instance names.
34 return '\$call'; 36 return instanceMethodName(CLOSURE_INVOCATION_NAME, arity);
35 } 37 }
36 38
37 String instanceName(SourceString name) { 39 String instanceMethodName(SourceString name, int arity) {
38 String candidate = '$name';
39 // TODO(floitsch): mangle, while preserving uniqueness. 40 // TODO(floitsch): mangle, while preserving uniqueness.
40 return candidate; 41 return '$name\$$arity';
42 }
43
44 String instanceFieldName(SourceString name) {
45 return '$name';
41 } 46 }
42 47
43 String setterName(SourceString name) { 48 String setterName(SourceString name) {
44 return 'set\$$name'; 49 return 'set\$$name';
45 } 50 }
46 51
47 String getterName(SourceString name) { 52 String getterName(SourceString name) {
48 return 'get\$$name'; 53 return 'get\$$name';
49 } 54 }
50 55
51 /** 56 /**
52 * The constructor-body name is computed from the corresponding 57 * 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 58 * 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 */ 59 */
72 String _computeGuess(Element element) { 60 String _computeGuess(Element element) {
73 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) { 61 assert(!element.isInstanceMember());
74 ConstructorBodyElement bodyElement = element; 62 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) {
75 return constructorBodyName(bodyElement.constructor); 63 SourceString name = getConstructorName(element);
64 return instanceMethodName(name, element.parameterCount(compiler));
65 } else {
66 // TODO(floitsch): deal with named constructors.
67 String name = '${element.name}';
68 if (element.kind == ElementKind.FUNCTION) {
69 FunctionElement functionElement = element;
70 name = '$name\$${functionElement.parameterCount(compiler)}';
71 }
72 // Prefix the name with '$' if it is reserved.
73 if (jsReserved.contains(name)) {
74 name = "\$$name";
75 assert(!jsReserved.contains(name));
76 }
77 return name;
76 } 78 }
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 } 79 }
89 80
90 String getBailoutName(Element element) { 81 String getBailoutName(Element element) {
91 return '${getName(element)}\$bailout'; 82 return '${getName(element)}\$bailout';
92 } 83 }
93 84
94 SourceString getConstructorName(FunctionElement constructor) { 85 SourceString getConstructorName(FunctionElement constructor) {
95 String dartName = constructor.name.stringValue; 86 String dartName = constructor.name.stringValue;
96 return new SourceString(dartName.replaceFirst('\.', '\$')); 87 return new SourceString(dartName.replaceFirst('\.', '\$'));
97 } 88 }
98 89
99 /** 90 /**
100 * Returns a preferred JS-id for the given element. The returned id is 91 * 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 92 * guaranteed to be a valid JS-id. Globals and static fields are furthermore
102 * guaranteed to be unique. 93 * guaranteed to be unique.
103 * 94 *
104 * For accessing statics consider calling 95 * For accessing statics consider calling
105 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead. 96 * [isolateAccess]/[isolateBailoutAccess] or [isolatePropertyAccess] instead.
106 */ 97 */
107 String getName(Element element) { 98 String getName(Element element) {
108 if (element.isInstanceMember()) { 99 if (element.isInstanceMember()) {
109 SourceString name; 100 SourceString name;
110 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) { 101 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY) {
111 ConstructorBodyElement bodyElement = element; 102 ConstructorBodyElement bodyElement = element;
112 name = getConstructorName(bodyElement.constructor); 103 SourceString name = getConstructorName(bodyElement.constructor);
104 return instanceMethodName(name, bodyElement.parameterCount(compiler));
105 } else if (element.kind == ElementKind.FUNCTION) {
106 FunctionElement functionElement = element;
107 int parameterCount = functionElement.parameterCount(compiler);
108 return instanceMethodName(element.name, parameterCount);
113 } else { 109 } else {
114 name = element.name; 110 return instanceFieldName(element.name);
115 } 111 }
116 return instanceName(name); 112 } else {
117 } 113 // Dealing with a top-level or static element.
118 String cached = globals[element]; 114 String cached = globals[element];
119 if (cached !== null) return cached; 115 if (cached !== null) return cached;
120 116
121 String guess; 117 String guess = _computeGuess(element);
122 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { 118 switch (element.kind) {
123 guess = getConstructorName(element).stringValue; 119 case ElementKind.VARIABLE:
124 } else { 120 case ElementKind.PARAMETER:
125 guess = _computeGuess(element); 121 // The name is not guaranteed to be unique.
126 } 122 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 123
133 case ElementKind.GENERATIVE_CONSTRUCTOR: 124 case ElementKind.GENERATIVE_CONSTRUCTOR:
134 case ElementKind.FUNCTION: 125 case ElementKind.FUNCTION:
135 case ElementKind.CLASS: 126 case ElementKind.CLASS:
136 case ElementKind.FIELD: 127 case ElementKind.FIELD:
137 // We need to make sure the name is unique. 128 // We need to make sure the name is unique.
138 int usedCount = usedGlobals[guess]; 129 int usedCount = usedGlobals[guess];
139 if (usedCount === null) { 130 if (usedCount === null) {
140 // No element with this name has been used before. 131 // No element with this name has been used before.
141 usedGlobals[guess] = 1; 132 usedGlobals[guess] = 1;
142 globals[element] = guess; 133 globals[element] = guess;
143 return guess; 134 return guess;
144 } else { 135 } else {
145 // Not the first time we see an element with this name. Append a 136 // Not the first time we see an element with this name. Append a
146 // number to make it unique. 137 // number to make it unique.
147 String name; 138 String name;
148 do { 139 do {
149 usedCount++; 140 usedCount++;
150 name = '$guess$usedCount'; 141 name = '$guess$usedCount';
151 } while (usedGlobals[name] !== null); 142 } while (usedGlobals[name] !== null);
152 usedGlobals[guess] = usedCount; 143 usedGlobals[guess] = usedCount;
153 globals[element] = name; 144 globals[element] = name;
154 return name; 145 return name;
155 } 146 }
156 147
157 default: 148 default:
158 compiler.internalError('getName for unknown kind: ${element.kind}', 149 compiler.internalError('getName for unknown kind: ${element.kind}',
159 node: element.parseNode(compiler, compiler)); 150 node: element.parseNode(compiler, compiler));
151 }
160 } 152 }
161 } 153 }
162 154
163 String isolateAccess(Element element) { 155 String isolateAccess(Element element) {
164 return "$CURRENT_ISOLATE.${getName(element)}"; 156 return "$CURRENT_ISOLATE.${getName(element)}";
165 } 157 }
166 158
167 String isolatePropertyAccess(Element element) { 159 String isolatePropertyAccess(Element element) {
168 return "$ISOLATE.prototype.${getName(element)}"; 160 return "$ISOLATE.prototype.${getName(element)}";
169 } 161 }
170 162
171 String isolateBailoutPropertyAccess(Element element) { 163 String isolateBailoutPropertyAccess(Element element) {
172 return '${isolatePropertyAccess(element)}\$bailout'; 164 return '${isolatePropertyAccess(element)}\$bailout';
173 } 165 }
174 166
175 String isolateBailoutAccess(Element element) { 167 String isolateBailoutAccess(Element element) {
176 return '${isolateAccess(element)}\$bailout'; 168 return '${isolateAccess(element)}\$bailout';
177 } 169 }
178 170
179 String operatorIs(ClassElement element) { 171 String operatorIs(ClassElement element) {
180 return 'is\$${getName(element)}'; 172 return 'is\$${getName(element)}';
181 } 173 }
182 } 174 }
OLDNEW
« no previous file with comments | « frog/leg/lib/core.dart ('k') | frog/leg/ssa/builder.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698