| 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(); | |
| 174 | |
| 175 /// This value in Dart syntax. | 168 /// This value in Dart syntax. |
| 176 String toDartText() => primitiveValue.toString(); | 169 String toDartText() => primitiveValue.toString(); |
| 177 } | 170 } |
| 178 | 171 |
| 179 class NullConstantValue extends PrimitiveConstantValue { | 172 class NullConstantValue extends PrimitiveConstantValue { |
| 180 /** The value a Dart null is compiled to in JavaScript. */ | 173 /** The value a Dart null is compiled to in JavaScript. */ |
| 181 static const String JsNull = "null"; | 174 static const String JsNull = "null"; |
| 182 | 175 |
| 183 const factory NullConstantValue() = NullConstantValue._internal; | 176 const factory NullConstantValue() = NullConstantValue._internal; |
| 184 | 177 |
| 185 const NullConstantValue._internal(); | 178 const NullConstantValue._internal(); |
| 186 | 179 |
| 187 bool get isNull => true; | 180 bool get isNull => true; |
| 188 | 181 |
| 189 get primitiveValue => null; | 182 get primitiveValue => null; |
| 190 | 183 |
| 191 DartType getType(CommonElements types) => types.nullType; | 184 DartType getType(CommonElements types) => types.nullType; |
| 192 | 185 |
| 193 // The magic constant has no meaning. It is just a random value. | 186 // The magic constant has no meaning. It is just a random value. |
| 194 int get hashCode => 785965825; | 187 int get hashCode => 785965825; |
| 195 | 188 |
| 196 DartString toDartString() => const LiteralDartString("null"); | |
| 197 | |
| 198 accept(ConstantValueVisitor visitor, arg) => visitor.visitNull(this, arg); | 189 accept(ConstantValueVisitor visitor, arg) => visitor.visitNull(this, arg); |
| 199 | 190 |
| 200 ConstantValueKind get kind => ConstantValueKind.NULL; | 191 ConstantValueKind get kind => ConstantValueKind.NULL; |
| 201 | 192 |
| 202 String toStructuredText() => 'NullConstant'; | 193 String toStructuredText() => 'NullConstant'; |
| 203 } | 194 } |
| 204 | 195 |
| 205 abstract class NumConstantValue extends PrimitiveConstantValue { | 196 abstract class NumConstantValue extends PrimitiveConstantValue { |
| 206 const NumConstantValue(); | 197 const NumConstantValue(); |
| 207 | 198 |
| (...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 | 258 // The is [:!IntConstant:] check at the beginning of the function makes sure |
| 268 // that we compare only equal to integer constants. | 259 // that we compare only equal to integer constants. |
| 269 bool operator ==(var other) { | 260 bool operator ==(var other) { |
| 270 if (other is! IntConstantValue) return false; | 261 if (other is! IntConstantValue) return false; |
| 271 IntConstantValue otherInt = other; | 262 IntConstantValue otherInt = other; |
| 272 return primitiveValue == otherInt.primitiveValue; | 263 return primitiveValue == otherInt.primitiveValue; |
| 273 } | 264 } |
| 274 | 265 |
| 275 int get hashCode => primitiveValue & Hashing.SMI_MASK; | 266 int get hashCode => primitiveValue & Hashing.SMI_MASK; |
| 276 | 267 |
| 277 DartString toDartString() { | |
| 278 return new DartString.literal(primitiveValue.toString()); | |
| 279 } | |
| 280 | |
| 281 accept(ConstantValueVisitor visitor, arg) => visitor.visitInt(this, arg); | 268 accept(ConstantValueVisitor visitor, arg) => visitor.visitInt(this, arg); |
| 282 | 269 |
| 283 ConstantValueKind get kind => ConstantValueKind.INT; | 270 ConstantValueKind get kind => ConstantValueKind.INT; |
| 284 | 271 |
| 285 String toStructuredText() => 'IntConstant(${toDartText()})'; | 272 String toStructuredText() => 'IntConstant(${toDartText()})'; |
| 286 } | 273 } |
| 287 | 274 |
| 288 class DoubleConstantValue extends NumConstantValue { | 275 class DoubleConstantValue extends NumConstantValue { |
| 289 final double primitiveValue; | 276 final double primitiveValue; |
| 290 | 277 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 return primitiveValue.isNegative == otherValue.isNegative; | 318 return primitiveValue.isNegative == otherValue.isNegative; |
| 332 } else if (primitiveValue.isNaN) { | 319 } else if (primitiveValue.isNaN) { |
| 333 return otherValue.isNaN; | 320 return otherValue.isNaN; |
| 334 } else { | 321 } else { |
| 335 return primitiveValue == otherValue; | 322 return primitiveValue == otherValue; |
| 336 } | 323 } |
| 337 } | 324 } |
| 338 | 325 |
| 339 int get hashCode => primitiveValue.hashCode; | 326 int get hashCode => primitiveValue.hashCode; |
| 340 | 327 |
| 341 DartString toDartString() { | |
| 342 return new DartString.literal(primitiveValue.toString()); | |
| 343 } | |
| 344 | |
| 345 accept(ConstantValueVisitor visitor, arg) => visitor.visitDouble(this, arg); | 328 accept(ConstantValueVisitor visitor, arg) => visitor.visitDouble(this, arg); |
| 346 | 329 |
| 347 ConstantValueKind get kind => ConstantValueKind.DOUBLE; | 330 ConstantValueKind get kind => ConstantValueKind.DOUBLE; |
| 348 | 331 |
| 349 String toStructuredText() => 'DoubleConstant(${toDartText()})'; | 332 String toStructuredText() => 'DoubleConstant(${toDartText()})'; |
| 350 } | 333 } |
| 351 | 334 |
| 352 abstract class BoolConstantValue extends PrimitiveConstantValue { | 335 abstract class BoolConstantValue extends PrimitiveConstantValue { |
| 353 factory BoolConstantValue(value) { | 336 factory BoolConstantValue(value) { |
| 354 return value ? new TrueConstantValue() : new FalseConstantValue(); | 337 return value ? new TrueConstantValue() : new FalseConstantValue(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 378 | 361 |
| 379 bool get primitiveValue => true; | 362 bool get primitiveValue => true; |
| 380 | 363 |
| 381 FalseConstantValue negate() => new FalseConstantValue(); | 364 FalseConstantValue negate() => new FalseConstantValue(); |
| 382 | 365 |
| 383 bool operator ==(var other) => identical(this, other); | 366 bool operator ==(var other) => identical(this, other); |
| 384 | 367 |
| 385 // The magic constant is just a random value. It does not have any | 368 // The magic constant is just a random value. It does not have any |
| 386 // significance. | 369 // significance. |
| 387 int get hashCode => 499; | 370 int get hashCode => 499; |
| 388 | |
| 389 DartString toDartString() => const LiteralDartString("true"); | |
| 390 } | 371 } |
| 391 | 372 |
| 392 class FalseConstantValue extends BoolConstantValue { | 373 class FalseConstantValue extends BoolConstantValue { |
| 393 factory FalseConstantValue() => const FalseConstantValue._internal(); | 374 factory FalseConstantValue() => const FalseConstantValue._internal(); |
| 394 | 375 |
| 395 const FalseConstantValue._internal() : super._internal(); | 376 const FalseConstantValue._internal() : super._internal(); |
| 396 | 377 |
| 397 bool get isFalse => true; | 378 bool get isFalse => true; |
| 398 | 379 |
| 399 bool get primitiveValue => false; | 380 bool get primitiveValue => false; |
| 400 | 381 |
| 401 TrueConstantValue negate() => new TrueConstantValue(); | 382 TrueConstantValue negate() => new TrueConstantValue(); |
| 402 | 383 |
| 403 bool operator ==(var other) => identical(this, other); | 384 bool operator ==(var other) => identical(this, other); |
| 404 | 385 |
| 405 // The magic constant is just a random value. It does not have any | 386 // The magic constant is just a random value. It does not have any |
| 406 // significance. | 387 // significance. |
| 407 int get hashCode => 536555975; | 388 int get hashCode => 536555975; |
| 408 | |
| 409 DartString toDartString() => const LiteralDartString("false"); | |
| 410 } | 389 } |
| 411 | 390 |
| 412 class StringConstantValue extends PrimitiveConstantValue { | 391 class StringConstantValue extends PrimitiveConstantValue { |
| 413 final DartString primitiveValue; | 392 final String primitiveValue; |
| 414 | 393 |
| 415 final int hashCode; | 394 final int hashCode; |
| 416 | 395 |
| 417 // TODO(floitsch): cache StringConstants. | 396 // TODO(floitsch): cache StringConstants. |
| 418 // TODO(floitsch): compute hashcode without calling toString() on the | 397 StringConstantValue(String value) |
| 419 // DartString. | |
| 420 StringConstantValue(DartString value) | |
| 421 : this.primitiveValue = value, | 398 : this.primitiveValue = value, |
| 422 this.hashCode = value.slowToString().hashCode; | 399 this.hashCode = value.hashCode; |
| 423 | |
| 424 StringConstantValue.fromString(String value) | |
| 425 : this(new DartString.literal(value)); | |
| 426 | 400 |
| 427 bool get isString => true; | 401 bool get isString => true; |
| 428 | 402 |
| 429 DartType getType(CommonElements types) => types.stringType; | 403 DartType getType(CommonElements types) => types.stringType; |
| 430 | 404 |
| 431 bool operator ==(var other) { | 405 bool operator ==(var other) { |
| 432 if (identical(this, other)) return true; | 406 if (identical(this, other)) return true; |
| 433 if (other is! StringConstantValue) return false; | 407 if (other is! StringConstantValue) return false; |
| 434 StringConstantValue otherString = other; | 408 StringConstantValue otherString = other; |
| 435 return hashCode == otherString.hashCode && | 409 return hashCode == otherString.hashCode && |
| 436 primitiveValue == otherString.primitiveValue; | 410 primitiveValue == otherString.primitiveValue; |
| 437 } | 411 } |
| 438 | 412 |
| 439 DartString toDartString() => primitiveValue; | 413 String toDartString() => primitiveValue; |
| 440 | 414 |
| 441 int get length => primitiveValue.length; | 415 int get length => primitiveValue.length; |
| 442 | 416 |
| 443 accept(ConstantValueVisitor visitor, arg) => visitor.visitString(this, arg); | 417 accept(ConstantValueVisitor visitor, arg) => visitor.visitString(this, arg); |
| 444 | 418 |
| 445 ConstantValueKind get kind => ConstantValueKind.STRING; | 419 ConstantValueKind get kind => ConstantValueKind.STRING; |
| 446 | 420 |
| 447 // TODO(johnniwinther): Ensure correct escaping. | 421 // TODO(johnniwinther): Ensure correct escaping. |
| 448 String toDartText() => '"${primitiveValue.slowToString()}"'; | 422 String toDartText() => '"${primitiveValue}"'; |
| 449 | 423 |
| 450 String toStructuredText() => 'StringConstant(${toDartText()})'; | 424 String toStructuredText() => 'StringConstant(${toDartText()})'; |
| 451 } | 425 } |
| 452 | 426 |
| 453 abstract class ObjectConstantValue extends ConstantValue { | 427 abstract class ObjectConstantValue extends ConstantValue { |
| 454 final InterfaceType type; | 428 final InterfaceType type; |
| 455 | 429 |
| 456 ObjectConstantValue(this.type); | 430 ObjectConstantValue(this.type); |
| 457 | 431 |
| 458 bool get isObject => true; | 432 bool get isObject => true; |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 820 DartType getType(CommonElements types) => types.dynamicType; | 794 DartType getType(CommonElements types) => types.dynamicType; |
| 821 | 795 |
| 822 ConstantValueKind get kind => ConstantValueKind.NON_CONSTANT; | 796 ConstantValueKind get kind => ConstantValueKind.NON_CONSTANT; |
| 823 | 797 |
| 824 @override | 798 @override |
| 825 String toStructuredText() => 'NonConstant'; | 799 String toStructuredText() => 'NonConstant'; |
| 826 | 800 |
| 827 @override | 801 @override |
| 828 String toDartText() => '>>non-constant<<'; | 802 String toDartText() => '>>non-constant<<'; |
| 829 } | 803 } |
| OLD | NEW |