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/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: 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 abstract class ConstantVisitor<R> { 7 abstract class ConstantVisitor<R> {
8 R visitFunction(FunctionConstant constant); 8 R visitFunction(FunctionConstant constant);
9 R visitNull(NullConstant constant); 9 R visitNull(NullConstant constant);
10 R visitInt(IntConstant constant); 10 R visitInt(IntConstant constant);
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 class MapConstant extends ObjectConstant { 367 class MapConstant extends ObjectConstant {
368 /** 368 /**
369 * The [PROTO_PROPERTY] must not be used as normal property in any JavaScript 369 * The [PROTO_PROPERTY] must not be used as normal property in any JavaScript
370 * object. It would change the prototype chain. 370 * object. It would change the prototype chain.
371 */ 371 */
372 static const LiteralDartString PROTO_PROPERTY = 372 static const LiteralDartString PROTO_PROPERTY =
373 const LiteralDartString("__proto__"); 373 const LiteralDartString("__proto__");
374 374
375 /** The dart class implementing constant map literals. */ 375 /** The dart class implementing constant map literals. */
376 static const SourceString DART_CLASS = const SourceString("ConstantMap"); 376 static const SourceString DART_CLASS = const SourceString("ConstantMap");
377 static const SourceString DART_STRING_CLASS =
378 const SourceString("ConstantStringMap");
377 static const SourceString DART_PROTO_CLASS = 379 static const SourceString DART_PROTO_CLASS =
378 const SourceString("ConstantProtoMap"); 380 const SourceString("ConstantProtoMap");
381 static const SourceString DART_GENERAL_CLASS =
382 const SourceString("GeneralConstantMap");
379 static const SourceString LENGTH_NAME = const SourceString("length"); 383 static const SourceString LENGTH_NAME = const SourceString("length");
380 static const SourceString JS_OBJECT_NAME = const SourceString("_jsObject"); 384 static const SourceString JS_OBJECT_NAME = const SourceString("_jsObject");
381 static const SourceString KEYS_NAME = const SourceString("_keys"); 385 static const SourceString KEYS_NAME = const SourceString("_keys");
382 static const SourceString PROTO_VALUE = const SourceString("_protoValue"); 386 static const SourceString PROTO_VALUE = const SourceString("_protoValue");
387 static const SourceString JS_DATA_NAME = const SourceString("_jsData");
383 388
384 final ListConstant keys; 389 final ListConstant keys;
385 final List<Constant> values; 390 final List<Constant> values;
386 final Constant protoValue; 391 final Constant protoValue;
387 final int hashCode; 392 final int hashCode;
393 final bool hasNonStringKey;
388 394
389 MapConstant(DartType type, this.keys, List<Constant> values, this.protoValue) 395 MapConstant(DartType type, this.keys, List<Constant> values, this.protoValue,
396 this.hasNonStringKey)
390 : this.values = values, 397 : this.values = values,
391 this.hashCode = computeHash(type, values), 398 this.hashCode = computeHash(type, values),
392 super(type); 399 super(type);
393 bool isMap() => true; 400 bool isMap() => true;
394 401
395 static int computeHash(DartType type, List<Constant> values) { 402 static int computeHash(DartType type, List<Constant> values) {
396 // TODO(floitsch): create a better hash. 403 // TODO(floitsch): create a better hash.
397 int hash = 0; 404 int hash = 0;
398 for (Constant value in values) { 405 for (Constant value in values) {
399 hash ^= value.hashCode; 406 hash ^= value.hashCode;
400 } 407 }
401 hash ^= type.hashCode; 408 hash ^= type.hashCode;
402 return hash; 409 return hash;
403 } 410 }
404 411
405 bool operator ==(var other) { 412 bool operator ==(var other) {
406 if (other is !MapConstant) return false; 413 if (other is !MapConstant) return false;
407 MapConstant otherMap = other; 414 MapConstant otherMap = other;
408 if (hashCode != otherMap.hashCode) return false; 415 if (hashCode != otherMap.hashCode) return false;
409 if (type != other.type) return false; 416 if (type != other.type) return false;
410 if (keys != otherMap.keys) return false; 417 if (keys != otherMap.keys) return false;
411 for (int i = 0; i < values.length; i++) { 418 for (int i = 0; i < values.length; i++) {
412 if (values[i] != otherMap.values[i]) return false; 419 if (values[i] != otherMap.values[i]) return false;
413 } 420 }
414 return true; 421 return true;
415 } 422 }
416 423
417 List<Constant> getDependencies() { 424 List<Constant> getDependencies() {
418 List<Constant> result = <Constant>[keys]; 425 List<Constant> result = <Constant>[];
426 if (hasNonStringKey) {
ngeoffray 2013/09/13 07:30:15 This seems fishy. Why can't you just add keys? Is
Johnni Winther 2013/09/18 12:37:21 If I just add [keys], we will create an unused con
427 result.addAll(keys.entries);
428 } else {
429 result.add(keys);
430 }
419 result.addAll(values); 431 result.addAll(values);
420 return result; 432 return result;
421 } 433 }
422 434
423 int get length => keys.length; 435 int get length => keys.length;
424 436
425 accept(ConstantVisitor visitor) => visitor.visitMap(this); 437 accept(ConstantVisitor visitor) => visitor.visitMap(this);
426 } 438 }
427 439
428 class InterceptorConstant extends Constant { 440 class InterceptorConstant extends Constant {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 // TODO(ahe): Refactor constant system to store this information directly. 502 // TODO(ahe): Refactor constant system to store this information directly.
491 ClassElement classElement = type.element; 503 ClassElement classElement = type.element;
492 int count = 0; 504 int count = 0;
493 Map<Element, Constant> result = new Map<Element, Constant>(); 505 Map<Element, Constant> result = new Map<Element, Constant>();
494 classElement.implementation.forEachInstanceField((holder, field) { 506 classElement.implementation.forEachInstanceField((holder, field) {
495 result[field] = fields[count++]; 507 result[field] = fields[count++];
496 }, includeSuperAndInjectedMembers: true); 508 }, includeSuperAndInjectedMembers: true);
497 return result; 509 return result;
498 } 510 }
499 } 511 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698