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

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

Issue 574683002: Use ConstExp for storing constants. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove ConstExpBuilder. Created 6 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 const ConstantVisitor(); 8 const ConstantVisitor();
9 9
10 R visitFunction(FunctionConstant constant); 10 R visitFunction(FunctionConstant constant);
11 R visitNull(NullConstant constant); 11 R visitNull(NullConstant constant);
12 R visitInt(IntConstant constant); 12 R visitInt(IntConstant constant);
13 R visitDouble(DoubleConstant constant); 13 R visitDouble(DoubleConstant constant);
14 R visitTrue(TrueConstant constant); 14 R visitTrue(TrueConstant constant);
15 R visitFalse(FalseConstant constant); 15 R visitFalse(FalseConstant constant);
16 R visitString(StringConstant constant); 16 R visitString(StringConstant constant);
17 R visitList(ListConstant constant); 17 R visitList(ListConstant constant);
18 R visitMap(MapConstant constant); 18 R visitMap(MapConstant constant);
19 R visitConstructed(ConstructedConstant constant); 19 R visitConstructed(ConstructedConstant constant);
20 R visitType(TypeConstant constant); 20 R visitType(TypeConstant constant);
21 R visitInterceptor(InterceptorConstant constant); 21 R visitInterceptor(InterceptorConstant constant);
22 R visitDummy(DummyConstant constant); 22 R visitDummy(DummyConstant constant);
23 R visitDeferred(DeferredConstant constant); 23 R visitDeferred(DeferredConstant constant);
24 } 24 }
25 25
26 abstract class Constant { 26 abstract class Constant {
sigurdm 2014/09/17 10:29:40 We might consider renaming this `ConstantValue` th
Johnni Winther 2014/09/17 12:20:39 Added a TODO.
27 const Constant(); 27 const Constant();
28 28
29 bool get isNull => false; 29 bool get isNull => false;
30 bool get isBool => false; 30 bool get isBool => false;
31 bool get isTrue => false; 31 bool get isTrue => false;
32 bool get isFalse => false; 32 bool get isFalse => false;
33 bool get isInt => false; 33 bool get isInt => false;
34 bool get isDouble => false; 34 bool get isDouble => false;
35 bool get isNum => false; 35 bool get isNum => false;
36 bool get isString => false; 36 bool get isString => false;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 98
99 bool operator ==(var other) { 99 bool operator ==(var other) {
100 if (other is !PrimitiveConstant) return false; 100 if (other is !PrimitiveConstant) return false;
101 PrimitiveConstant otherPrimitive = other; 101 PrimitiveConstant otherPrimitive = other;
102 // We use == instead of 'identical' so that DartStrings compare correctly. 102 // We use == instead of 'identical' so that DartStrings compare correctly.
103 return value == otherPrimitive.value; 103 return value == otherPrimitive.value;
104 } 104 }
105 105
106 int get hashCode => throw new UnsupportedError('PrimitiveConstant.hashCode'); 106 int get hashCode => throw new UnsupportedError('PrimitiveConstant.hashCode');
107 107
108 String toString() => value.toString(); 108 String toString() => toSyntax();
109 // Primitive constants don't have dependencies. 109 // Primitive constants don't have dependencies.
110 List<Constant> getDependencies() => const <Constant>[]; 110 List<Constant> getDependencies() => const <Constant>[];
111 DartString toDartString(); 111 DartString toDartString();
112
113 /// This value in Dart syntax.
114 // TODO(johnniwinther): Move this to [Constant].
115 String toSyntax() => value.toString();
sigurdm 2014/09/17 10:29:41 maybe call it `unparse` or something similar. `toS
Johnni Winther 2014/09/17 12:20:39 Done.
112 } 116 }
113 117
114 class NullConstant extends PrimitiveConstant { 118 class NullConstant extends PrimitiveConstant {
115 /** The value a Dart null is compiled to in JavaScript. */ 119 /** The value a Dart null is compiled to in JavaScript. */
116 static const String JsNull = "null"; 120 static const String JsNull = "null";
117 121
118 factory NullConstant() => const NullConstant._internal(); 122 factory NullConstant() => const NullConstant._internal();
119 const NullConstant._internal(); 123 const NullConstant._internal();
120 bool get isNull => true; 124 bool get isNull => true;
121 get value => null; 125 get value => null;
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 if (other is !StringConstant) return false; 338 if (other is !StringConstant) return false;
335 StringConstant otherString = other; 339 StringConstant otherString = other;
336 return (hashCode == otherString.hashCode) && (value == otherString.value); 340 return (hashCode == otherString.hashCode) && (value == otherString.value);
337 } 341 }
338 342
339 DartString toDartString() => value; 343 DartString toDartString() => value;
340 int get length => value.length; 344 int get length => value.length;
341 345
342 accept(ConstantVisitor visitor) => visitor.visitString(this); 346 accept(ConstantVisitor visitor) => visitor.visitString(this);
343 347
348 // TODO(johnniwinther): Ensure correct escaping.
349 String toSyntax() => '"${value.slowToString()}"';
350
344 String toString() { 351 String toString() {
345 return 'StringConstant("${value.slowToString()}")'; 352 return 'StringConstant(${toSyntax()})';
346 } 353 }
347 } 354 }
348 355
349 abstract class ObjectConstant extends Constant { 356 abstract class ObjectConstant extends Constant {
350 final DartType type; 357 final DartType type;
351 358
352 ObjectConstant(this.type); 359 ObjectConstant(this.type);
353 360
354 bool get isObject => true; 361 bool get isObject => true;
355 362
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 569
563 String toString() { 570 String toString() {
564 return 'DummyConstant($typeMask)'; 571 return 'DummyConstant($typeMask)';
565 } 572 }
566 } 573 }
567 574
568 class ConstructedConstant extends ObjectConstant { 575 class ConstructedConstant extends ObjectConstant {
569 final List<Constant> fields; 576 final List<Constant> fields;
570 final int hashCode; 577 final int hashCode;
571 578
572 ConstructedConstant(DartType type, List<Constant> fields, 579 ConstructedConstant(DartType type, List<Constant> fields)
573 {this.isLiteralSymbol: false})
574 : this.fields = fields, 580 : this.fields = fields,
575 hashCode = computeHash(type, fields), 581 hashCode = computeHash(type, fields),
576 super(type) { 582 super(type) {
577 assert(type != null); 583 assert(type != null);
578 } 584 }
585
579 bool get isConstructedObject => true; 586 bool get isConstructedObject => true;
580 587
581 /// True if this constant is constructed as a literal symbol.
582 final bool isLiteralSymbol;
583
584 static int computeHash(DartType type, List<Constant> fields) { 588 static int computeHash(DartType type, List<Constant> fields) {
585 // TODO(floitsch): create a better hash. 589 // TODO(floitsch): create a better hash.
586 int hash = 0; 590 int hash = 0;
587 for (Constant field in fields) { 591 for (Constant field in fields) {
588 hash ^= field.hashCode; 592 hash ^= field.hashCode;
589 } 593 }
590 hash ^= type.hashCode; 594 hash ^= type.hashCode;
591 return hash; 595 return hash;
592 } 596 }
593 597
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 DartType computeType(Compiler compiler) => referenced.computeType(compiler); 672 DartType computeType(Compiler compiler) => referenced.computeType(compiler);
669 673
670 ti.TypeMask computeMask(Compiler compiler) { 674 ti.TypeMask computeMask(Compiler compiler) {
671 return referenced.computeMask(compiler); 675 return referenced.computeMask(compiler);
672 } 676 }
673 677
674 String toString() { 678 String toString() {
675 return 'DeferredConstant($referenced)'; 679 return 'DeferredConstant($referenced)';
676 } 680 }
677 } 681 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698