Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 library dart2js.constants.values; | 5 library dart2js.constants.values; |
| 6 | 6 |
| 7 import '../common.dart'; | 7 import '../common.dart'; |
| 8 import '../common_elements.dart'; | 8 import '../common_elements.dart'; |
| 9 import '../elements/entities.dart'; | 9 import '../elements/entities.dart'; |
| 10 import '../elements/types.dart'; | 10 import '../elements/types.dart'; |
| 11 import '../tree/dartstring.dart'; | |
| 12 import '../util/util.dart' show Hashing; | 11 import '../util/util.dart' show Hashing; |
| 13 | 12 |
| 14 enum ConstantValueKind { | 13 enum ConstantValueKind { |
| 15 FUNCTION, | 14 FUNCTION, |
| 16 NULL, | 15 NULL, |
| 17 INT, | 16 INT, |
| 18 DOUBLE, | 17 DOUBLE, |
| 19 BOOL, | 18 BOOL, |
| 20 STRING, | 19 STRING, |
| 21 LIST, | 20 LIST, |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 | 118 |
| 120 bool get isFunction => true; | 119 bool get isFunction => true; |
| 121 | 120 |
| 122 bool operator ==(var other) { | 121 bool operator ==(var other) { |
| 123 if (other is! FunctionConstantValue) return false; | 122 if (other is! FunctionConstantValue) return false; |
| 124 return identical(other.element, element); | 123 return identical(other.element, element); |
| 125 } | 124 } |
| 126 | 125 |
| 127 List<ConstantValue> getDependencies() => const <ConstantValue>[]; | 126 List<ConstantValue> getDependencies() => const <ConstantValue>[]; |
| 128 | 127 |
| 129 DartString toDartString() { | |
| 130 return new DartString.literal(element.name); | |
| 131 } | |
| 132 | |
| 133 DartType getType(CommonElements types) => type; | 128 DartType getType(CommonElements types) => type; |
| 134 | 129 |
| 135 int get hashCode => (17 * element.hashCode) & 0x7fffffff; | 130 int get hashCode => (17 * element.hashCode) & 0x7fffffff; |
| 136 | 131 |
| 137 accept(ConstantValueVisitor visitor, arg) => visitor.visitFunction(this, arg); | 132 accept(ConstantValueVisitor visitor, arg) => visitor.visitFunction(this, arg); |
| 138 | 133 |
| 139 ConstantValueKind get kind => ConstantValueKind.FUNCTION; | 134 ConstantValueKind get kind => ConstantValueKind.FUNCTION; |
| 140 | 135 |
| 141 String toDartText() { | 136 String toDartText() { |
| 142 if (element.enclosingClass != null) { | 137 if (element.enclosingClass != null) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 163 PrimitiveConstantValue otherPrimitive = other; | 158 PrimitiveConstantValue otherPrimitive = other; |
| 164 // We use == instead of 'identical' so that DartStrings compare correctly. | 159 // We use == instead of 'identical' so that DartStrings compare correctly. |
| 165 return primitiveValue == otherPrimitive.primitiveValue; | 160 return primitiveValue == otherPrimitive.primitiveValue; |
| 166 } | 161 } |
| 167 | 162 |
| 168 int get hashCode => throw new UnsupportedError('PrimitiveConstant.hashCode'); | 163 int get hashCode => throw new UnsupportedError('PrimitiveConstant.hashCode'); |
| 169 | 164 |
| 170 // Primitive constants don't have dependencies. | 165 // Primitive constants don't have dependencies. |
| 171 List<ConstantValue> getDependencies() => const <ConstantValue>[]; | 166 List<ConstantValue> getDependencies() => const <ConstantValue>[]; |
| 172 | 167 |
| 173 DartString toDartString(); | 168 /// Returns the constant value as its string representation. |
| 169 String toDartString() => primitiveValue.toString(); | |
| 174 | 170 |
| 175 /// This value in Dart syntax. | 171 /// This value in Dart syntax. |
| 176 String toDartText() => primitiveValue.toString(); | 172 String toDartText() => primitiveValue.toString(); |
| 177 } | 173 } |
| 178 | 174 |
| 179 class NullConstantValue extends PrimitiveConstantValue { | 175 class NullConstantValue extends PrimitiveConstantValue { |
| 180 /** The value a Dart null is compiled to in JavaScript. */ | 176 /** The value a Dart null is compiled to in JavaScript. */ |
| 181 static const String JsNull = "null"; | 177 static const String JsNull = "null"; |
| 182 | 178 |
| 183 const factory NullConstantValue() = NullConstantValue._internal; | 179 const factory NullConstantValue() = NullConstantValue._internal; |
| 184 | 180 |
| 185 const NullConstantValue._internal(); | 181 const NullConstantValue._internal(); |
| 186 | 182 |
| 187 bool get isNull => true; | 183 bool get isNull => true; |
| 188 | 184 |
| 189 get primitiveValue => null; | 185 get primitiveValue => null; |
| 190 | 186 |
| 191 DartType getType(CommonElements types) => types.nullType; | 187 DartType getType(CommonElements types) => types.nullType; |
| 192 | 188 |
| 193 // The magic constant has no meaning. It is just a random value. | 189 // The magic constant has no meaning. It is just a random value. |
| 194 int get hashCode => 785965825; | 190 int get hashCode => 785965825; |
| 195 | 191 |
| 196 DartString toDartString() => const LiteralDartString("null"); | |
| 197 | |
| 198 accept(ConstantValueVisitor visitor, arg) => visitor.visitNull(this, arg); | 192 accept(ConstantValueVisitor visitor, arg) => visitor.visitNull(this, arg); |
| 199 | 193 |
| 200 ConstantValueKind get kind => ConstantValueKind.NULL; | 194 ConstantValueKind get kind => ConstantValueKind.NULL; |
| 201 | 195 |
| 202 String toStructuredText() => 'NullConstant'; | 196 String toStructuredText() => 'NullConstant'; |
| 203 } | 197 } |
| 204 | 198 |
| 205 abstract class NumConstantValue extends PrimitiveConstantValue { | 199 abstract class NumConstantValue extends PrimitiveConstantValue { |
| 206 const NumConstantValue(); | 200 const NumConstantValue(); |
| 207 | 201 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 267 // The is [:!IntConstant:] check at the beginning of the function makes sure | 261 // The is [:!IntConstant:] check at the beginning of the function makes sure |
| 268 // that we compare only equal to integer constants. | 262 // that we compare only equal to integer constants. |
| 269 bool operator ==(var other) { | 263 bool operator ==(var other) { |
| 270 if (other is! IntConstantValue) return false; | 264 if (other is! IntConstantValue) return false; |
| 271 IntConstantValue otherInt = other; | 265 IntConstantValue otherInt = other; |
| 272 return primitiveValue == otherInt.primitiveValue; | 266 return primitiveValue == otherInt.primitiveValue; |
| 273 } | 267 } |
| 274 | 268 |
| 275 int get hashCode => primitiveValue & Hashing.SMI_MASK; | 269 int get hashCode => primitiveValue & Hashing.SMI_MASK; |
| 276 | 270 |
| 277 DartString toDartString() { | |
| 278 return new DartString.literal(primitiveValue.toString()); | |
| 279 } | |
| 280 | |
| 281 accept(ConstantValueVisitor visitor, arg) => visitor.visitInt(this, arg); | 271 accept(ConstantValueVisitor visitor, arg) => visitor.visitInt(this, arg); |
| 282 | 272 |
| 283 ConstantValueKind get kind => ConstantValueKind.INT; | 273 ConstantValueKind get kind => ConstantValueKind.INT; |
| 284 | 274 |
| 285 String toStructuredText() => 'IntConstant(${toDartText()})'; | 275 String toStructuredText() => 'IntConstant(${toDartText()})'; |
| 286 } | 276 } |
| 287 | 277 |
| 288 class DoubleConstantValue extends NumConstantValue { | 278 class DoubleConstantValue extends NumConstantValue { |
| 289 final double primitiveValue; | 279 final double primitiveValue; |
| 290 | 280 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 return primitiveValue.isNegative == otherValue.isNegative; | 321 return primitiveValue.isNegative == otherValue.isNegative; |
| 332 } else if (primitiveValue.isNaN) { | 322 } else if (primitiveValue.isNaN) { |
| 333 return otherValue.isNaN; | 323 return otherValue.isNaN; |
| 334 } else { | 324 } else { |
| 335 return primitiveValue == otherValue; | 325 return primitiveValue == otherValue; |
| 336 } | 326 } |
| 337 } | 327 } |
| 338 | 328 |
| 339 int get hashCode => primitiveValue.hashCode; | 329 int get hashCode => primitiveValue.hashCode; |
| 340 | 330 |
| 341 DartString toDartString() { | |
| 342 return new DartString.literal(primitiveValue.toString()); | |
| 343 } | |
| 344 | |
| 345 accept(ConstantValueVisitor visitor, arg) => visitor.visitDouble(this, arg); | 331 accept(ConstantValueVisitor visitor, arg) => visitor.visitDouble(this, arg); |
| 346 | 332 |
| 347 ConstantValueKind get kind => ConstantValueKind.DOUBLE; | 333 ConstantValueKind get kind => ConstantValueKind.DOUBLE; |
| 348 | 334 |
| 349 String toStructuredText() => 'DoubleConstant(${toDartText()})'; | 335 String toStructuredText() => 'DoubleConstant(${toDartText()})'; |
| 350 } | 336 } |
| 351 | 337 |
| 352 abstract class BoolConstantValue extends PrimitiveConstantValue { | 338 abstract class BoolConstantValue extends PrimitiveConstantValue { |
| 353 factory BoolConstantValue(value) { | 339 factory BoolConstantValue(value) { |
| 354 return value ? new TrueConstantValue() : new FalseConstantValue(); | 340 return value ? new TrueConstantValue() : new FalseConstantValue(); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 378 | 364 |
| 379 bool get primitiveValue => true; | 365 bool get primitiveValue => true; |
| 380 | 366 |
| 381 FalseConstantValue negate() => new FalseConstantValue(); | 367 FalseConstantValue negate() => new FalseConstantValue(); |
| 382 | 368 |
| 383 bool operator ==(var other) => identical(this, other); | 369 bool operator ==(var other) => identical(this, other); |
| 384 | 370 |
| 385 // The magic constant is just a random value. It does not have any | 371 // The magic constant is just a random value. It does not have any |
| 386 // significance. | 372 // significance. |
| 387 int get hashCode => 499; | 373 int get hashCode => 499; |
| 388 | |
| 389 DartString toDartString() => const LiteralDartString("true"); | |
| 390 } | 374 } |
| 391 | 375 |
| 392 class FalseConstantValue extends BoolConstantValue { | 376 class FalseConstantValue extends BoolConstantValue { |
| 393 factory FalseConstantValue() => const FalseConstantValue._internal(); | 377 factory FalseConstantValue() => const FalseConstantValue._internal(); |
| 394 | 378 |
| 395 const FalseConstantValue._internal() : super._internal(); | 379 const FalseConstantValue._internal() : super._internal(); |
| 396 | 380 |
| 397 bool get isFalse => true; | 381 bool get isFalse => true; |
| 398 | 382 |
| 399 bool get primitiveValue => false; | 383 bool get primitiveValue => false; |
| 400 | 384 |
| 401 TrueConstantValue negate() => new TrueConstantValue(); | 385 TrueConstantValue negate() => new TrueConstantValue(); |
| 402 | 386 |
| 403 bool operator ==(var other) => identical(this, other); | 387 bool operator ==(var other) => identical(this, other); |
| 404 | 388 |
| 405 // The magic constant is just a random value. It does not have any | 389 // The magic constant is just a random value. It does not have any |
| 406 // significance. | 390 // significance. |
| 407 int get hashCode => 536555975; | 391 int get hashCode => 536555975; |
| 408 | |
| 409 DartString toDartString() => const LiteralDartString("false"); | |
| 410 } | 392 } |
| 411 | 393 |
| 412 class StringConstantValue extends PrimitiveConstantValue { | 394 class StringConstantValue extends PrimitiveConstantValue { |
| 413 final DartString primitiveValue; | 395 final String primitiveValue; |
| 414 | 396 |
| 415 final int hashCode; | 397 final int hashCode; |
| 416 | 398 |
| 417 // TODO(floitsch): cache StringConstants. | 399 // TODO(floitsch): cache StringConstants. |
| 418 // TODO(floitsch): compute hashcode without calling toString() on the | 400 StringConstantValue(String value) |
| 419 // DartString. | |
| 420 StringConstantValue(DartString value) | |
| 421 : this.primitiveValue = value, | 401 : this.primitiveValue = value, |
| 422 this.hashCode = value.slowToString().hashCode; | 402 this.hashCode = value.hashCode; |
| 423 | |
| 424 StringConstantValue.fromString(String value) | |
| 425 : this(new DartString.literal(value)); | |
| 426 | 403 |
| 427 bool get isString => true; | 404 bool get isString => true; |
| 428 | 405 |
| 429 DartType getType(CommonElements types) => types.stringType; | 406 DartType getType(CommonElements types) => types.stringType; |
| 430 | 407 |
| 431 bool operator ==(var other) { | 408 bool operator ==(var other) { |
| 432 if (identical(this, other)) return true; | 409 if (identical(this, other)) return true; |
| 433 if (other is! StringConstantValue) return false; | 410 if (other is! StringConstantValue) return false; |
| 434 StringConstantValue otherString = other; | 411 StringConstantValue otherString = other; |
| 435 return hashCode == otherString.hashCode && | 412 return hashCode == otherString.hashCode && |
| 436 primitiveValue == otherString.primitiveValue; | 413 primitiveValue == otherString.primitiveValue; |
| 437 } | 414 } |
| 438 | 415 |
| 439 DartString toDartString() => primitiveValue; | 416 String toDartString() => primitiveValue; |
|
Siggi Cherem (dart-lang)
2017/05/08 22:24:04
we could also get rid of the 'toDartString' entire
Johnni Winther
2017/05/09 08:18:13
Done.
| |
| 440 | 417 |
| 441 int get length => primitiveValue.length; | 418 int get length => primitiveValue.length; |
| 442 | 419 |
| 443 accept(ConstantValueVisitor visitor, arg) => visitor.visitString(this, arg); | 420 accept(ConstantValueVisitor visitor, arg) => visitor.visitString(this, arg); |
| 444 | 421 |
| 445 ConstantValueKind get kind => ConstantValueKind.STRING; | 422 ConstantValueKind get kind => ConstantValueKind.STRING; |
| 446 | 423 |
| 447 // TODO(johnniwinther): Ensure correct escaping. | 424 // TODO(johnniwinther): Ensure correct escaping. |
| 448 String toDartText() => '"${primitiveValue.slowToString()}"'; | 425 String toDartText() => '"${primitiveValue}"'; |
| 449 | 426 |
| 450 String toStructuredText() => 'StringConstant(${toDartText()})'; | 427 String toStructuredText() => 'StringConstant(${toDartText()})'; |
| 451 } | 428 } |
| 452 | 429 |
| 453 abstract class ObjectConstantValue extends ConstantValue { | 430 abstract class ObjectConstantValue extends ConstantValue { |
| 454 final InterfaceType type; | 431 final InterfaceType type; |
| 455 | 432 |
| 456 ObjectConstantValue(this.type); | 433 ObjectConstantValue(this.type); |
| 457 | 434 |
| 458 bool get isObject => true; | 435 bool get isObject => true; |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 820 DartType getType(CommonElements types) => types.dynamicType; | 797 DartType getType(CommonElements types) => types.dynamicType; |
| 821 | 798 |
| 822 ConstantValueKind get kind => ConstantValueKind.NON_CONSTANT; | 799 ConstantValueKind get kind => ConstantValueKind.NON_CONSTANT; |
| 823 | 800 |
| 824 @override | 801 @override |
| 825 String toStructuredText() => 'NonConstant'; | 802 String toStructuredText() => 'NonConstant'; |
| 826 | 803 |
| 827 @override | 804 @override |
| 828 String toDartText() => '>>non-constant<<'; | 805 String toDartText() => '>>non-constant<<'; |
| 829 } | 806 } |
| OLD | NEW |