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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/js_backend/constant_emitter.dart

Issue 22909056: Support general expressions as keys in literal maps. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 part of js_backend; 5 part of js_backend;
6 6
7 class ConstantEmitter { 7 class ConstantEmitter {
8 ConstantReferenceEmitter _referenceEmitter; 8 ConstantReferenceEmitter _referenceEmitter;
9 ConstantInitializerEmitter _initializerEmitter; 9 ConstantInitializerEmitter _initializerEmitter;
10 10
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 jsAst.Expression valueExpression = 225 jsAst.Expression valueExpression =
226 _reference(constant.values[valueIndex++]); 226 _reference(constant.values[valueIndex++]);
227 properties.add(new jsAst.Property(keyExpression, valueExpression)); 227 properties.add(new jsAst.Property(keyExpression, valueExpression));
228 } 228 }
229 if (valueIndex != constant.values.length) { 229 if (valueIndex != constant.values.length) {
230 compiler.internalError("Bad value count."); 230 compiler.internalError("Bad value count.");
231 } 231 }
232 return new jsAst.ObjectInitializer(properties); 232 return new jsAst.ObjectInitializer(properties);
233 } 233 }
234 234
235 void badFieldCountError() { 235 jsAst.Expression jsGeneralMap() {
236 compiler.internalError( 236 List<jsAst.Expression> data = <jsAst.Expression>[];
237 "Compiler and ConstantMap disagree on number of fields."); 237 int valueIndex = 0;
238 for (int i = 0; i < constant.keys.entries.length; i++) {
239 jsAst.Expression keyExpression =
240 _reference(constant.keys.entries[valueIndex]);
241 jsAst.Expression valueExpression =
242 _reference(constant.values[valueIndex++]);
243 data.add(keyExpression);
244 data.add(valueExpression);
245 }
246 if (valueIndex != constant.values.length) {
247 compiler.internalError("Bad value count.");
248 }
249 return new jsAst.ArrayInitializer.from(data);
238 } 250 }
239 251
240 ClassElement classElement = constant.type.element; 252 ClassElement classElement = constant.type.element;
253 SourceString className = classElement.name;
241 254
242 List<jsAst.Expression> arguments = <jsAst.Expression>[]; 255 List<jsAst.Expression> arguments = <jsAst.Expression>[];
243 256
244 // The arguments of the JavaScript constructor for any given Dart class 257 // The arguments of the JavaScript constructor for any given Dart class
245 // are in the same order as the members of the class element. 258 // are in the same order as the members of the class element.
246 int emittedArgumentCount = 0; 259 int emittedArgumentCount = 0;
247 classElement.implementation.forEachInstanceField( 260 classElement.implementation.forEachInstanceField(
248 (ClassElement enclosing, Element field) { 261 (ClassElement enclosing, Element field) {
249 if (field.name == MapConstant.LENGTH_NAME) { 262 if (field.name == MapConstant.LENGTH_NAME) {
250 arguments.add( 263 arguments.add(
251 new jsAst.LiteralNumber('${constant.keys.entries.length}')); 264 new jsAst.LiteralNumber('${constant.keys.entries.length}'));
252 } else if (field.name == MapConstant.JS_OBJECT_NAME) { 265 } else if (field.name == MapConstant.JS_OBJECT_NAME) {
253 arguments.add(jsMap()); 266 arguments.add(jsMap());
254 } else if (field.name == MapConstant.KEYS_NAME) { 267 } else if (field.name == MapConstant.KEYS_NAME) {
255 arguments.add(_reference(constant.keys)); 268 arguments.add(_reference(constant.keys));
256 } else if (field.name == MapConstant.PROTO_VALUE) { 269 } else if (field.name == MapConstant.PROTO_VALUE) {
257 assert(constant.protoValue != null); 270 assert(constant.protoValue != null);
258 arguments.add(_reference(constant.protoValue)); 271 arguments.add(_reference(constant.protoValue));
272 } else if (field.name == MapConstant.JS_DATA_NAME) {
273 arguments.add(jsGeneralMap());
259 } else { 274 } else {
260 badFieldCountError(); 275 compiler.internalError(
276 "Compiler has unexpected field ${field.name} for "
277 "${className}.");
261 } 278 }
262 emittedArgumentCount++; 279 emittedArgumentCount++;
263 }, 280 },
264 includeSuperAndInjectedMembers: true); 281 includeSuperAndInjectedMembers: true);
265 282 if ((className == MapConstant.DART_STRING_CLASS &&
266 if ((constant.protoValue == null && emittedArgumentCount != 3) || 283 emittedArgumentCount != 3) ||
267 (constant.protoValue != null && emittedArgumentCount != 4)) { 284 (className == MapConstant.DART_PROTO_CLASS &&
268 badFieldCountError(); 285 emittedArgumentCount != 4) ||
286 (className == MapConstant.DART_GENERAL_CLASS &&
287 emittedArgumentCount != 1)) {
288 compiler.internalError(
289 "Compiler and ${className} disagree on number of fields.");
269 } 290 }
270 291
271 jsAst.Expression value = new jsAst.New( 292 jsAst.Expression value = new jsAst.New(
272 new jsAst.VariableUse(getJsConstructor(classElement)), 293 new jsAst.VariableUse(getJsConstructor(classElement)),
273 arguments); 294 arguments);
274 return maybeAddTypeArguments(constant.type, value); 295 return maybeAddTypeArguments(constant.type, value);
275 } 296 }
276 297
277 JavaScriptBackend get backend => compiler.backend; 298 JavaScriptBackend get backend => compiler.backend;
278 299
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 .toList(growable: false) 351 .toList(growable: false)
331 .map((DartType type) => rti.getTypeRepresentation(type, (_){})); 352 .map((DartType type) => rti.getTypeRepresentation(type, (_){}));
332 jsAst.Expression argumentList = 353 jsAst.Expression argumentList =
333 new jsAst.LiteralString('[${arguments.join(', ')}]'); 354 new jsAst.LiteralString('[${arguments.join(', ')}]');
334 return new jsAst.Call(getHelperProperty(backend.getSetRuntimeTypeInfo()), 355 return new jsAst.Call(getHelperProperty(backend.getSetRuntimeTypeInfo()),
335 [value, argumentList]); 356 [value, argumentList]);
336 } 357 }
337 return value; 358 return value;
338 } 359 }
339 } 360 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698