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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/compile_time_constants.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: Updated cf. comments 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 dart2js; 5 part of dart2js;
6 6
7 /** 7 /**
8 * The [ConstantHandler] keeps track of compile-time constants, 8 * The [ConstantHandler] keeps track of compile-time constants,
9 * initializations of global and static fields, and default values of 9 * initializations of global and static fields, and default values of
10 * optional parameters. 10 * optional parameters.
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 handler.registerInstantiatedType(type, elements); 379 handler.registerInstantiatedType(type, elements);
380 Constant constant = new ListConstant(type, arguments); 380 Constant constant = new ListConstant(type, arguments);
381 handler.registerCompileTimeConstant(constant, elements); 381 handler.registerCompileTimeConstant(constant, elements);
382 return constant; 382 return constant;
383 } 383 }
384 384
385 Constant visitLiteralMap(LiteralMap node) { 385 Constant visitLiteralMap(LiteralMap node) {
386 if (!node.isConst()) { 386 if (!node.isConst()) {
387 return signalNotCompileTimeConstant(node); 387 return signalNotCompileTimeConstant(node);
388 } 388 }
389 List<StringConstant> keys = <StringConstant>[]; 389 List<Constant> keys = <Constant>[];
390 Map<StringConstant, Constant> map = new Map<StringConstant, Constant>(); 390 Map<Constant, Constant> map = new Map<Constant, Constant>();
391 for (Link<Node> link = node.entries.nodes; 391 for (Link<Node> link = node.entries.nodes;
392 !link.isEmpty; 392 !link.isEmpty;
393 link = link.tail) { 393 link = link.tail) {
394 LiteralMapEntry entry = link.head; 394 LiteralMapEntry entry = link.head;
395 Constant key = evaluateConstant(entry.key); 395 Constant key = evaluateConstant(entry.key);
396 if (!key.isString() || entry.key.asStringNode() == null) {
397 compiler.reportFatalError(
398 entry.key, MessageKind.KEY_NOT_A_STRING_LITERAL);
399 }
400 StringConstant keyConstant = key;
401 if (!map.containsKey(key)) keys.add(key); 396 if (!map.containsKey(key)) keys.add(key);
402 map[key] = evaluateConstant(entry.value); 397 map[key] = evaluateConstant(entry.value);
403 } 398 }
404 List<Constant> values = <Constant>[]; 399
400 bool onlyStringKeys = true;
405 Constant protoValue = null; 401 Constant protoValue = null;
406 for (StringConstant key in keys) { 402 for (var key in keys) {
407 if (key.value == MapConstant.PROTO_PROPERTY) { 403 if (key.isString()) {
408 protoValue = map[key]; 404 if (key.value == MapConstant.PROTO_PROPERTY) {
405 protoValue = map[key];
406 }
409 } else { 407 } else {
410 values.add(map[key]); 408 onlyStringKeys = false;
409 // Don't handle __proto__ values specially in the general map case.
410 protoValue = null;
411 break;
411 } 412 }
412 } 413 }
414
413 bool hasProtoKey = (protoValue != null); 415 bool hasProtoKey = (protoValue != null);
416 List<Constant> values = map.values.toList();
414 InterfaceType sourceType = elements.getType(node); 417 InterfaceType sourceType = elements.getType(node);
415 Link<DartType> arguments = 418 Link<DartType> arguments =
416 new Link<DartType>.fromList([compiler.stringClass.rawType]); 419 new Link<DartType>.fromList([compiler.stringClass.rawType]);
417 DartType keysType = new InterfaceType(compiler.listClass, arguments); 420 DartType keysType = new InterfaceType(compiler.listClass, arguments);
418 ListConstant keysList = new ListConstant(keysType, keys); 421 ListConstant keysList = new ListConstant(keysType, keys);
419 handler.registerCompileTimeConstant(keysList, elements); 422 if (onlyStringKeys) {
420 SourceString className = hasProtoKey 423 handler.registerCompileTimeConstant(keysList, elements);
421 ? MapConstant.DART_PROTO_CLASS 424 }
422 : MapConstant.DART_CLASS; 425 SourceString className = onlyStringKeys
426 ? (hasProtoKey ? MapConstant.DART_PROTO_CLASS
427 : MapConstant.DART_STRING_CLASS)
428 : MapConstant.DART_GENERAL_CLASS;
423 ClassElement classElement = compiler.jsHelperLibrary.find(className); 429 ClassElement classElement = compiler.jsHelperLibrary.find(className);
424 classElement.ensureResolved(compiler); 430 classElement.ensureResolved(compiler);
425 Link<DartType> typeArgument = sourceType.typeArguments.tail; 431 Link<DartType> typeArgument = sourceType.typeArguments;
426 InterfaceType type = new InterfaceType(classElement, typeArgument); 432 InterfaceType type = new InterfaceType(classElement, typeArgument);
427 handler.registerInstantiatedType(type, elements); 433 handler.registerInstantiatedType(type, elements);
428 Constant constant = new MapConstant(type, keysList, values, protoValue); 434 Constant constant =
435 new MapConstant(type, keysList, values, protoValue, onlyStringKeys);
429 handler.registerCompileTimeConstant(constant, elements); 436 handler.registerCompileTimeConstant(constant, elements);
430 return constant; 437 return constant;
431 } 438 }
432 439
433 Constant visitLiteralNull(LiteralNull node) { 440 Constant visitLiteralNull(LiteralNull node) {
434 return constantSystem.createNull(); 441 return constantSystem.createNull();
435 } 442 }
436 443
437 Constant visitLiteralString(LiteralString node) { 444 Constant visitLiteralString(LiteralString node) {
438 handler.registerStringInstance(elements); 445 handler.registerStringInstance(elements);
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 if (fieldValue == null) { 957 if (fieldValue == null) {
951 // Use the default value. 958 // Use the default value.
952 fieldValue = handler.compileConstant(field); 959 fieldValue = handler.compileConstant(field);
953 } 960 }
954 jsNewArguments.add(fieldValue); 961 jsNewArguments.add(fieldValue);
955 }, 962 },
956 includeSuperAndInjectedMembers: true); 963 includeSuperAndInjectedMembers: true);
957 return jsNewArguments; 964 return jsNewArguments;
958 } 965 }
959 } 966 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698