| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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.expressions; | 5 library dart2js.constants.expressions; |
| 6 | 6 |
| 7 import '../constants/constant_system.dart'; | 7 import '../constants/constant_system.dart'; |
| 8 import '../dart2jslib.dart' show assertDebugMode, Compiler; | 8 import '../dart2jslib.dart' show assertDebugMode, Compiler; |
| 9 import '../dart_types.dart'; | 9 import '../dart_types.dart'; |
| 10 import '../elements/elements.dart' show | 10 import '../elements/elements.dart' show |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 /// | 254 /// |
| 255 /// There is no one-to-one mapping between [ConstantExpression] and | 255 /// There is no one-to-one mapping between [ConstantExpression] and |
| 256 /// [ConstantValue], because different expressions can denote the same constant. | 256 /// [ConstantValue], because different expressions can denote the same constant. |
| 257 /// For instance, multiple `const` constructors may be used to create the same | 257 /// For instance, multiple `const` constructors may be used to create the same |
| 258 /// object, and different `const` variables may hold the same value. | 258 /// object, and different `const` variables may hold the same value. |
| 259 abstract class ConstantExpression { | 259 abstract class ConstantExpression { |
| 260 int _hashCode; | 260 int _hashCode; |
| 261 | 261 |
| 262 ConstantExpressionKind get kind; | 262 ConstantExpressionKind get kind; |
| 263 | 263 |
| 264 /// Returns the value of this constant expression. | |
| 265 // TODO(johnniwinther): Replace this with an evaluation method that takes | |
| 266 // a constant system and an environment. | |
| 267 ConstantValue get value; | |
| 268 | |
| 269 // TODO(johnniwinther): Unify precedence handled between constants, front-end | 264 // TODO(johnniwinther): Unify precedence handled between constants, front-end |
| 270 // and back-end. | 265 // and back-end. |
| 271 int get precedence => 16; | 266 int get precedence => 16; |
| 272 | 267 |
| 273 accept(ConstantExpressionVisitor visitor, [context]); | 268 accept(ConstantExpressionVisitor visitor, [context]); |
| 274 | 269 |
| 275 /// Substitute free variables using arguments. | 270 /// Substitute free variables using arguments. |
| 276 ConstantExpression apply(NormalizedArguments arguments) => this; | 271 ConstantExpression apply(NormalizedArguments arguments) => this; |
| 277 | 272 |
| 278 /// Compute the [ConstantValue] for this expression using the [environment] | 273 /// Compute the [ConstantValue] for this expression using the [environment] |
| (...skipping 28 matching lines...) Expand all Loading... |
| 307 | 302 |
| 308 String toString() { | 303 String toString() { |
| 309 assertDebugMode('Use ConstantExpression.getText() instead of ' | 304 assertDebugMode('Use ConstantExpression.getText() instead of ' |
| 310 'ConstantExpression.toString()'); | 305 'ConstantExpression.toString()'); |
| 311 return getText(); | 306 return getText(); |
| 312 } | 307 } |
| 313 } | 308 } |
| 314 | 309 |
| 315 /// A synthetic constant used to recover from errors. | 310 /// A synthetic constant used to recover from errors. |
| 316 class ErroneousConstantExpression extends ConstantExpression { | 311 class ErroneousConstantExpression extends ConstantExpression { |
| 317 final PrimitiveConstantValue value = new NullConstantValue(); | |
| 318 | |
| 319 ConstantExpressionKind get kind => ConstantExpressionKind.ERRONEOUS; | 312 ConstantExpressionKind get kind => ConstantExpressionKind.ERRONEOUS; |
| 320 | 313 |
| 321 accept(ConstantExpressionVisitor visitor, [context]) { | 314 accept(ConstantExpressionVisitor visitor, [context]) { |
| 322 // Do nothing. This is an error. | 315 // Do nothing. This is an error. |
| 323 } | 316 } |
| 324 | 317 |
| 325 @override | 318 @override |
| 326 ConstantValue evaluate(Environment environment, | 319 ConstantValue evaluate(Environment environment, |
| 327 ConstantSystem constantSystem) { | 320 ConstantSystem constantSystem) { |
| 328 // TODO(johnniwinther): Use non-constant values for errors. | 321 // TODO(johnniwinther): Use non-constant values for errors. |
| 329 return value; | 322 return new NonConstantValue(); |
| 330 } | 323 } |
| 331 | 324 |
| 332 @override | 325 @override |
| 333 int _computeHashCode() => 13; | 326 int _computeHashCode() => 13; |
| 334 | 327 |
| 335 @override | 328 @override |
| 336 bool _equals(ErroneousConstantExpression other) => true; | 329 bool _equals(ErroneousConstantExpression other) => true; |
| 337 } | 330 } |
| 338 | 331 |
| 339 /// A boolean, int, double, string, or null constant. | 332 /// A boolean, int, double, string, or null constant. |
| 340 abstract class PrimitiveConstantExpression extends ConstantExpression { | 333 abstract class PrimitiveConstantExpression extends ConstantExpression { |
| 341 final PrimitiveConstantValue value; | |
| 342 | |
| 343 PrimitiveConstantExpression(this.value); | |
| 344 | |
| 345 /// The primitive value of this contant expression. | 334 /// The primitive value of this contant expression. |
| 346 get primitiveValue; | 335 get primitiveValue; |
| 347 } | 336 } |
| 348 | 337 |
| 349 /// Boolean literal constant. | 338 /// Boolean literal constant. |
| 350 class BoolConstantExpression extends PrimitiveConstantExpression { | 339 class BoolConstantExpression extends PrimitiveConstantExpression { |
| 351 final bool primitiveValue; | 340 final bool primitiveValue; |
| 352 | 341 |
| 353 BoolConstantExpression(this.primitiveValue, | 342 BoolConstantExpression(this.primitiveValue); |
| 354 PrimitiveConstantValue value) : super(value); | |
| 355 | 343 |
| 356 ConstantExpressionKind get kind => ConstantExpressionKind.BOOL; | 344 ConstantExpressionKind get kind => ConstantExpressionKind.BOOL; |
| 357 | 345 |
| 358 accept(ConstantExpressionVisitor visitor, [context]) { | 346 accept(ConstantExpressionVisitor visitor, [context]) { |
| 359 return visitor.visitBool(this, context); | 347 return visitor.visitBool(this, context); |
| 360 } | 348 } |
| 361 | 349 |
| 362 @override | 350 @override |
| 363 ConstantValue evaluate(Environment environment, | 351 ConstantValue evaluate(Environment environment, |
| 364 ConstantSystem constantSystem) { | 352 ConstantSystem constantSystem) { |
| 365 return constantSystem.createBool(primitiveValue); | 353 return constantSystem.createBool(primitiveValue); |
| 366 } | 354 } |
| 367 | 355 |
| 368 @override | 356 @override |
| 369 int _computeHashCode() => 13 * primitiveValue.hashCode; | 357 int _computeHashCode() => 13 * primitiveValue.hashCode; |
| 370 | 358 |
| 371 @override | 359 @override |
| 372 bool _equals(BoolConstantExpression other) { | 360 bool _equals(BoolConstantExpression other) { |
| 373 return primitiveValue == other.primitiveValue; | 361 return primitiveValue == other.primitiveValue; |
| 374 } | 362 } |
| 375 } | 363 } |
| 376 | 364 |
| 377 /// Integer literal constant. | 365 /// Integer literal constant. |
| 378 class IntConstantExpression extends PrimitiveConstantExpression { | 366 class IntConstantExpression extends PrimitiveConstantExpression { |
| 379 final int primitiveValue; | 367 final int primitiveValue; |
| 380 | 368 |
| 381 IntConstantExpression(this.primitiveValue, | 369 IntConstantExpression(this.primitiveValue); |
| 382 PrimitiveConstantValue value) : super(value); | |
| 383 | 370 |
| 384 ConstantExpressionKind get kind => ConstantExpressionKind.INT; | 371 ConstantExpressionKind get kind => ConstantExpressionKind.INT; |
| 385 | 372 |
| 386 accept(ConstantExpressionVisitor visitor, [context]) { | 373 accept(ConstantExpressionVisitor visitor, [context]) { |
| 387 return visitor.visitInt(this, context); | 374 return visitor.visitInt(this, context); |
| 388 } | 375 } |
| 389 | 376 |
| 390 @override | 377 @override |
| 391 ConstantValue evaluate(Environment environment, | 378 ConstantValue evaluate(Environment environment, |
| 392 ConstantSystem constantSystem) { | 379 ConstantSystem constantSystem) { |
| 393 return constantSystem.createInt(primitiveValue); | 380 return constantSystem.createInt(primitiveValue); |
| 394 } | 381 } |
| 395 | 382 |
| 396 @override | 383 @override |
| 397 int _computeHashCode() => 17 * primitiveValue.hashCode; | 384 int _computeHashCode() => 17 * primitiveValue.hashCode; |
| 398 | 385 |
| 399 @override | 386 @override |
| 400 bool _equals(IntConstantExpression other) { | 387 bool _equals(IntConstantExpression other) { |
| 401 return primitiveValue == other.primitiveValue; | 388 return primitiveValue == other.primitiveValue; |
| 402 } | 389 } |
| 403 } | 390 } |
| 404 | 391 |
| 405 /// Double literal constant. | 392 /// Double literal constant. |
| 406 class DoubleConstantExpression extends PrimitiveConstantExpression { | 393 class DoubleConstantExpression extends PrimitiveConstantExpression { |
| 407 final double primitiveValue; | 394 final double primitiveValue; |
| 408 | 395 |
| 409 DoubleConstantExpression(this.primitiveValue, | 396 DoubleConstantExpression(this.primitiveValue); |
| 410 PrimitiveConstantValue value) : super(value); | |
| 411 | 397 |
| 412 ConstantExpressionKind get kind => ConstantExpressionKind.DOUBLE; | 398 ConstantExpressionKind get kind => ConstantExpressionKind.DOUBLE; |
| 413 | 399 |
| 414 accept(ConstantExpressionVisitor visitor, [context]) { | 400 accept(ConstantExpressionVisitor visitor, [context]) { |
| 415 return visitor.visitDouble(this, context); | 401 return visitor.visitDouble(this, context); |
| 416 } | 402 } |
| 417 | 403 |
| 418 @override | 404 @override |
| 419 ConstantValue evaluate(Environment environment, | 405 ConstantValue evaluate(Environment environment, |
| 420 ConstantSystem constantSystem) { | 406 ConstantSystem constantSystem) { |
| 421 return constantSystem.createDouble(primitiveValue); | 407 return constantSystem.createDouble(primitiveValue); |
| 422 } | 408 } |
| 423 | 409 |
| 424 @override | 410 @override |
| 425 int _computeHashCode() => 19 * primitiveValue.hashCode; | 411 int _computeHashCode() => 19 * primitiveValue.hashCode; |
| 426 | 412 |
| 427 @override | 413 @override |
| 428 bool _equals(DoubleConstantExpression other) { | 414 bool _equals(DoubleConstantExpression other) { |
| 429 return primitiveValue == other.primitiveValue; | 415 return primitiveValue == other.primitiveValue; |
| 430 } | 416 } |
| 431 } | 417 } |
| 432 | 418 |
| 433 /// String literal constant. | 419 /// String literal constant. |
| 434 class StringConstantExpression extends PrimitiveConstantExpression { | 420 class StringConstantExpression extends PrimitiveConstantExpression { |
| 435 final String primitiveValue; | 421 final String primitiveValue; |
| 436 | 422 |
| 437 StringConstantExpression(this.primitiveValue, | 423 StringConstantExpression(this.primitiveValue); |
| 438 PrimitiveConstantValue value) : super(value); | |
| 439 | 424 |
| 440 ConstantExpressionKind get kind => ConstantExpressionKind.STRING; | 425 ConstantExpressionKind get kind => ConstantExpressionKind.STRING; |
| 441 | 426 |
| 442 accept(ConstantExpressionVisitor visitor, [context]) { | 427 accept(ConstantExpressionVisitor visitor, [context]) { |
| 443 return visitor.visitString(this, context); | 428 return visitor.visitString(this, context); |
| 444 } | 429 } |
| 445 | 430 |
| 446 @override | 431 @override |
| 447 ConstantValue evaluate(Environment environment, | 432 ConstantValue evaluate(Environment environment, |
| 448 ConstantSystem constantSystem) { | 433 ConstantSystem constantSystem) { |
| 449 return constantSystem.createString(new DartString.literal(primitiveValue)); | 434 return constantSystem.createString(new DartString.literal(primitiveValue)); |
| 450 } | 435 } |
| 451 | 436 |
| 452 @override | 437 @override |
| 453 int _computeHashCode() => 23 * primitiveValue.hashCode; | 438 int _computeHashCode() => 23 * primitiveValue.hashCode; |
| 454 | 439 |
| 455 @override | 440 @override |
| 456 bool _equals(StringConstantExpression other) { | 441 bool _equals(StringConstantExpression other) { |
| 457 return primitiveValue == other.primitiveValue; | 442 return primitiveValue == other.primitiveValue; |
| 458 } | 443 } |
| 459 } | 444 } |
| 460 | 445 |
| 461 /// Null literal constant. | 446 /// Null literal constant. |
| 462 class NullConstantExpression extends PrimitiveConstantExpression { | 447 class NullConstantExpression extends PrimitiveConstantExpression { |
| 463 NullConstantExpression(PrimitiveConstantValue value) : super(value); | 448 NullConstantExpression(); |
| 464 | 449 |
| 465 ConstantExpressionKind get kind => ConstantExpressionKind.NULL; | 450 ConstantExpressionKind get kind => ConstantExpressionKind.NULL; |
| 466 | 451 |
| 467 accept(ConstantExpressionVisitor visitor, [context]) { | 452 accept(ConstantExpressionVisitor visitor, [context]) { |
| 468 return visitor.visitNull(this, context); | 453 return visitor.visitNull(this, context); |
| 469 } | 454 } |
| 470 | 455 |
| 471 @override | 456 @override |
| 472 ConstantValue evaluate(Environment environment, | 457 ConstantValue evaluate(Environment environment, |
| 473 ConstantSystem constantSystem) { | 458 ConstantSystem constantSystem) { |
| 474 return constantSystem.createNull(); | 459 return constantSystem.createNull(); |
| 475 } | 460 } |
| 476 | 461 |
| 477 get primitiveValue => null; | 462 get primitiveValue => null; |
| 478 | 463 |
| 479 @override | 464 @override |
| 480 int _computeHashCode() => 29; | 465 int _computeHashCode() => 29; |
| 481 | 466 |
| 482 @override | 467 @override |
| 483 bool _equals(NullConstantExpression other) => true; | 468 bool _equals(NullConstantExpression other) => true; |
| 484 } | 469 } |
| 485 | 470 |
| 486 /// Literal list constant. | 471 /// Literal list constant. |
| 487 class ListConstantExpression extends ConstantExpression { | 472 class ListConstantExpression extends ConstantExpression { |
| 488 final ListConstantValue value; | |
| 489 final InterfaceType type; | 473 final InterfaceType type; |
| 490 final List<ConstantExpression> values; | 474 final List<ConstantExpression> values; |
| 491 | 475 |
| 492 ListConstantExpression(this.value, this.type, this.values); | 476 ListConstantExpression(this.type, this.values); |
| 493 | 477 |
| 494 ConstantExpressionKind get kind => ConstantExpressionKind.LIST; | 478 ConstantExpressionKind get kind => ConstantExpressionKind.LIST; |
| 495 | 479 |
| 496 accept(ConstantExpressionVisitor visitor, [context]) { | 480 accept(ConstantExpressionVisitor visitor, [context]) { |
| 497 return visitor.visitList(this, context); | 481 return visitor.visitList(this, context); |
| 498 } | 482 } |
| 499 | 483 |
| 500 @override | 484 @override |
| 501 ConstantValue evaluate(Environment environment, | 485 ConstantValue evaluate(Environment environment, |
| 502 ConstantSystem constantSystem) { | 486 ConstantSystem constantSystem) { |
| 503 return constantSystem.createList(type, | 487 return constantSystem.createList(type, |
| 504 values.map((v) => v.evaluate(environment, constantSystem)).toList()); | 488 values.map((v) => v.evaluate(environment, constantSystem)).toList()); |
| 505 } | 489 } |
| 506 | 490 |
| 507 ConstantExpression apply(NormalizedArguments arguments) { | 491 ConstantExpression apply(NormalizedArguments arguments) { |
| 508 return new ListConstantExpression(null, type, | 492 return new ListConstantExpression( |
| 509 values.map((v) => v.apply(arguments)).toList()); | 493 type, values.map((v) => v.apply(arguments)).toList()); |
| 510 } | 494 } |
| 511 | 495 |
| 512 @override | 496 @override |
| 513 int _computeHashCode() { | 497 int _computeHashCode() { |
| 514 int hashCode = 13 * type.hashCode + 17 * values.length; | 498 int hashCode = 13 * type.hashCode + 17 * values.length; |
| 515 for (ConstantExpression value in values) { | 499 for (ConstantExpression value in values) { |
| 516 hashCode ^= 19 * value.hashCode; | 500 hashCode ^= 19 * value.hashCode; |
| 517 } | 501 } |
| 518 return hashCode; | 502 return hashCode; |
| 519 } | 503 } |
| 520 | 504 |
| 521 @override | 505 @override |
| 522 bool _equals(ListConstantExpression other) { | 506 bool _equals(ListConstantExpression other) { |
| 523 if (type != other.type) return false; | 507 if (type != other.type) return false; |
| 524 if (values.length != other.values.length) return false; | 508 if (values.length != other.values.length) return false; |
| 525 for (int i = 0; i < values.length; i++) { | 509 for (int i = 0; i < values.length; i++) { |
| 526 if (values[i] != other.values[i]) return false; | 510 if (values[i] != other.values[i]) return false; |
| 527 } | 511 } |
| 528 return true; | 512 return true; |
| 529 } | 513 } |
| 530 } | 514 } |
| 531 | 515 |
| 532 /// Literal map constant. | 516 /// Literal map constant. |
| 533 class MapConstantExpression extends ConstantExpression { | 517 class MapConstantExpression extends ConstantExpression { |
| 534 final MapConstantValue value; | |
| 535 final InterfaceType type; | 518 final InterfaceType type; |
| 536 final List<ConstantExpression> keys; | 519 final List<ConstantExpression> keys; |
| 537 final List<ConstantExpression> values; | 520 final List<ConstantExpression> values; |
| 538 | 521 |
| 539 MapConstantExpression(this.value, this.type, this.keys, this.values); | 522 MapConstantExpression(this.type, this.keys, this.values); |
| 540 | 523 |
| 541 ConstantExpressionKind get kind => ConstantExpressionKind.MAP; | 524 ConstantExpressionKind get kind => ConstantExpressionKind.MAP; |
| 542 | 525 |
| 543 accept(ConstantExpressionVisitor visitor, [context]) { | 526 accept(ConstantExpressionVisitor visitor, [context]) { |
| 544 return visitor.visitMap(this, context); | 527 return visitor.visitMap(this, context); |
| 545 } | 528 } |
| 546 | 529 |
| 547 @override | 530 @override |
| 548 ConstantValue evaluate(Environment environment, | 531 ConstantValue evaluate(Environment environment, |
| 549 ConstantSystem constantSystem) { | 532 ConstantSystem constantSystem) { |
| 550 return constantSystem.createMap(environment.compiler, | 533 return constantSystem.createMap(environment.compiler, |
| 551 type, | 534 type, |
| 552 keys.map((k) => k.evaluate(environment, constantSystem)).toList(), | 535 keys.map((k) => k.evaluate(environment, constantSystem)).toList(), |
| 553 values.map((v) => v.evaluate(environment, constantSystem)).toList()); | 536 values.map((v) => v.evaluate(environment, constantSystem)).toList()); |
| 554 } | 537 } |
| 555 | 538 |
| 556 ConstantExpression apply(NormalizedArguments arguments) { | 539 ConstantExpression apply(NormalizedArguments arguments) { |
| 557 return new MapConstantExpression(null, type, | 540 return new MapConstantExpression( |
| 541 type, |
| 558 keys.map((k) => k.apply(arguments)).toList(), | 542 keys.map((k) => k.apply(arguments)).toList(), |
| 559 values.map((v) => v.apply(arguments)).toList()); | 543 values.map((v) => v.apply(arguments)).toList()); |
| 560 } | 544 } |
| 561 | 545 |
| 562 @override | 546 @override |
| 563 int _computeHashCode() { | 547 int _computeHashCode() { |
| 564 int hashCode = 13 * type.hashCode + 17 * values.length; | 548 int hashCode = 13 * type.hashCode + 17 * values.length; |
| 565 for (ConstantExpression value in values) { | 549 for (ConstantExpression value in values) { |
| 566 hashCode ^= 19 * value.hashCode; | 550 hashCode ^= 19 * value.hashCode; |
| 567 } | 551 } |
| 568 return hashCode; | 552 return hashCode; |
| 569 } | 553 } |
| 570 | 554 |
| 571 @override | 555 @override |
| 572 bool _equals(MapConstantExpression other) { | 556 bool _equals(MapConstantExpression other) { |
| 573 if (type != other.type) return false; | 557 if (type != other.type) return false; |
| 574 if (values.length != other.values.length) return false; | 558 if (values.length != other.values.length) return false; |
| 575 for (int i = 0; i < values.length; i++) { | 559 for (int i = 0; i < values.length; i++) { |
| 576 if (keys[i] != other.keys[i]) return false; | 560 if (keys[i] != other.keys[i]) return false; |
| 577 if (values[i] != other.values[i]) return false; | 561 if (values[i] != other.values[i]) return false; |
| 578 } | 562 } |
| 579 return true; | 563 return true; |
| 580 } | 564 } |
| 581 } | 565 } |
| 582 | 566 |
| 583 /// Invocation of a const constructor. | 567 /// Invocation of a const constructor. |
| 584 class ConstructedConstantExpression extends ConstantExpression { | 568 class ConstructedConstantExpression extends ConstantExpression { |
| 585 final ConstantValue value; | |
| 586 final InterfaceType type; | 569 final InterfaceType type; |
| 587 final ConstructorElement target; | 570 final ConstructorElement target; |
| 588 final CallStructure callStructure; | 571 final CallStructure callStructure; |
| 589 final List<ConstantExpression> arguments; | 572 final List<ConstantExpression> arguments; |
| 590 | 573 |
| 591 ConstructedConstantExpression( | 574 ConstructedConstantExpression( |
| 592 this.value, | |
| 593 this.type, | 575 this.type, |
| 594 this.target, | 576 this.target, |
| 595 this.callStructure, | 577 this.callStructure, |
| 596 this.arguments) { | 578 this.arguments) { |
| 597 assert(type.element == target.enclosingClass); | 579 assert(type.element == target.enclosingClass); |
| 598 assert(!arguments.contains(null)); | 580 assert(!arguments.contains(null)); |
| 599 } | 581 } |
| 600 | 582 |
| 601 ConstantExpressionKind get kind => ConstantExpressionKind.CONSTRUCTED; | 583 ConstantExpressionKind get kind => ConstantExpressionKind.CONSTRUCTED; |
| 602 | 584 |
| 603 accept(ConstantExpressionVisitor visitor, [context]) { | 585 accept(ConstantExpressionVisitor visitor, [context]) { |
| 604 return visitor.visitConstructed(this, context); | 586 return visitor.visitConstructed(this, context); |
| 605 } | 587 } |
| 606 | 588 |
| 607 Map<FieldElement, ConstantExpression> computeInstanceFields() { | 589 Map<FieldElement, ConstantExpression> computeInstanceFields() { |
| 608 return target.constantConstructor.computeInstanceFields( | 590 return target.constantConstructor.computeInstanceFields( |
| 609 arguments, callStructure); | 591 arguments, callStructure); |
| 610 } | 592 } |
| 611 | 593 |
| 612 InterfaceType computeInstanceType() { | 594 InterfaceType computeInstanceType() { |
| 613 return target.constantConstructor.computeInstanceType(type); | 595 return target.constantConstructor.computeInstanceType(type); |
| 614 } | 596 } |
| 615 | 597 |
| 616 ConstructedConstantExpression apply(NormalizedArguments arguments) { | 598 ConstructedConstantExpression apply(NormalizedArguments arguments) { |
| 617 return new ConstructedConstantExpression(null, | 599 return new ConstructedConstantExpression( |
| 618 type, target, callStructure, | 600 type, target, callStructure, |
| 619 this.arguments.map((a) => a.apply(arguments)).toList()); | 601 this.arguments.map((a) => a.apply(arguments)).toList()); |
| 620 } | 602 } |
| 621 | 603 |
| 622 @override | 604 @override |
| 623 ConstantValue evaluate(Environment environment, | 605 ConstantValue evaluate(Environment environment, |
| 624 ConstantSystem constantSystem) { | 606 ConstantSystem constantSystem) { |
| 625 Map<FieldElement, ConstantValue> fieldValues = | 607 Map<FieldElement, ConstantValue> fieldValues = |
| 626 <FieldElement, ConstantValue>{}; | 608 <FieldElement, ConstantValue>{}; |
| 627 computeInstanceFields().forEach( | 609 computeInstanceFields().forEach( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 650 if (callStructure != other.callStructure) return false; | 632 if (callStructure != other.callStructure) return false; |
| 651 for (int i = 0; i < arguments.length; i++) { | 633 for (int i = 0; i < arguments.length; i++) { |
| 652 if (arguments[i] != other.arguments[i]) return false; | 634 if (arguments[i] != other.arguments[i]) return false; |
| 653 } | 635 } |
| 654 return true; | 636 return true; |
| 655 } | 637 } |
| 656 } | 638 } |
| 657 | 639 |
| 658 /// String literal with juxtaposition and/or interpolations. | 640 /// String literal with juxtaposition and/or interpolations. |
| 659 class ConcatenateConstantExpression extends ConstantExpression { | 641 class ConcatenateConstantExpression extends ConstantExpression { |
| 660 final StringConstantValue value; | |
| 661 final List<ConstantExpression> expressions; | 642 final List<ConstantExpression> expressions; |
| 662 | 643 |
| 663 ConcatenateConstantExpression(this.value, this.expressions); | 644 ConcatenateConstantExpression(this.expressions); |
| 664 | 645 |
| 665 ConstantExpressionKind get kind => ConstantExpressionKind.CONCATENATE; | 646 ConstantExpressionKind get kind => ConstantExpressionKind.CONCATENATE; |
| 666 | 647 |
| 667 accept(ConstantExpressionVisitor visitor, [context]) { | 648 accept(ConstantExpressionVisitor visitor, [context]) { |
| 668 return visitor.visitConcatenate(this, context); | 649 return visitor.visitConcatenate(this, context); |
| 669 } | 650 } |
| 670 | 651 |
| 671 ConstantExpression apply(NormalizedArguments arguments) { | 652 ConstantExpression apply(NormalizedArguments arguments) { |
| 672 return new ConcatenateConstantExpression(null, | 653 return new ConcatenateConstantExpression( |
| 673 expressions.map((a) => a.apply(arguments)).toList()); | 654 expressions.map((a) => a.apply(arguments)).toList()); |
| 674 } | 655 } |
| 675 | 656 |
| 676 @override | 657 @override |
| 677 ConstantValue evaluate(Environment environment, | 658 ConstantValue evaluate(Environment environment, |
| 678 ConstantSystem constantSystem) { | 659 ConstantSystem constantSystem) { |
| 679 DartString accumulator; | 660 DartString accumulator; |
| 680 for (ConstantExpression expression in expressions) { | 661 for (ConstantExpression expression in expressions) { |
| 681 ConstantValue value = expression.evaluate(environment, constantSystem); | 662 ConstantValue value = expression.evaluate(environment, constantSystem); |
| 682 DartString valueString; | 663 DartString valueString; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 715 if (expressions.length != other.expressions.length) return false; | 696 if (expressions.length != other.expressions.length) return false; |
| 716 for (int i = 0; i < expressions.length; i++) { | 697 for (int i = 0; i < expressions.length; i++) { |
| 717 if (expressions[i] != other.expressions[i]) return false; | 698 if (expressions[i] != other.expressions[i]) return false; |
| 718 } | 699 } |
| 719 return true; | 700 return true; |
| 720 } | 701 } |
| 721 } | 702 } |
| 722 | 703 |
| 723 /// Symbol literal. | 704 /// Symbol literal. |
| 724 class SymbolConstantExpression extends ConstantExpression { | 705 class SymbolConstantExpression extends ConstantExpression { |
| 725 final ConstructedConstantValue value; | |
| 726 final String name; | 706 final String name; |
| 727 | 707 |
| 728 SymbolConstantExpression(this.value, this.name); | 708 SymbolConstantExpression(this.name); |
| 729 | 709 |
| 730 ConstantExpressionKind get kind => ConstantExpressionKind.SYMBOL; | 710 ConstantExpressionKind get kind => ConstantExpressionKind.SYMBOL; |
| 731 | 711 |
| 732 accept(ConstantExpressionVisitor visitor, [context]) { | 712 accept(ConstantExpressionVisitor visitor, [context]) { |
| 733 return visitor.visitSymbol(this, context); | 713 return visitor.visitSymbol(this, context); |
| 734 } | 714 } |
| 735 | 715 |
| 736 @override | 716 @override |
| 737 int _computeHashCode() => 13 * name.hashCode; | 717 int _computeHashCode() => 13 * name.hashCode; |
| 738 | 718 |
| 739 @override | 719 @override |
| 740 bool _equals(SymbolConstantExpression other) { | 720 bool _equals(SymbolConstantExpression other) { |
| 741 return name == other.name; | 721 return name == other.name; |
| 742 } | 722 } |
| 743 | 723 |
| 744 @override | 724 @override |
| 745 ConstantValue evaluate(Environment environment, | 725 ConstantValue evaluate(Environment environment, |
| 746 ConstantSystem constantSystem) { | 726 ConstantSystem constantSystem) { |
| 747 // TODO(johnniwinther): Implement this. | 727 // TODO(johnniwinther): Implement this. |
| 748 throw new UnsupportedError('SymbolConstantExpression.evaluate'); | 728 throw new UnsupportedError('SymbolConstantExpression.evaluate'); |
| 749 } | 729 } |
| 750 } | 730 } |
| 751 | 731 |
| 752 /// Type literal. | 732 /// Type literal. |
| 753 class TypeConstantExpression extends ConstantExpression { | 733 class TypeConstantExpression extends ConstantExpression { |
| 754 final TypeConstantValue value; | |
| 755 /// Either [DynamicType] or a raw [GenericType]. | 734 /// Either [DynamicType] or a raw [GenericType]. |
| 756 final DartType type; | 735 final DartType type; |
| 757 | 736 |
| 758 TypeConstantExpression(this.value, this.type) { | 737 TypeConstantExpression(this.type) { |
| 759 assert(type is GenericType || type is DynamicType); | 738 assert(type is GenericType || type is DynamicType); |
| 760 } | 739 } |
| 761 | 740 |
| 762 ConstantExpressionKind get kind => ConstantExpressionKind.TYPE; | 741 ConstantExpressionKind get kind => ConstantExpressionKind.TYPE; |
| 763 | 742 |
| 764 accept(ConstantExpressionVisitor visitor, [context]) { | 743 accept(ConstantExpressionVisitor visitor, [context]) { |
| 765 return visitor.visitType(this, context); | 744 return visitor.visitType(this, context); |
| 766 } | 745 } |
| 767 | 746 |
| 768 @override | 747 @override |
| 769 ConstantValue evaluate(Environment environment, | 748 ConstantValue evaluate(Environment environment, |
| 770 ConstantSystem constantSystem) { | 749 ConstantSystem constantSystem) { |
| 771 return constantSystem.createType(environment.compiler, type); | 750 return constantSystem.createType(environment.compiler, type); |
| 772 } | 751 } |
| 773 | 752 |
| 774 @override | 753 @override |
| 775 int _computeHashCode() => 13 * type.hashCode; | 754 int _computeHashCode() => 13 * type.hashCode; |
| 776 | 755 |
| 777 @override | 756 @override |
| 778 bool _equals(TypeConstantExpression other) { | 757 bool _equals(TypeConstantExpression other) { |
| 779 return type == other.type; | 758 return type == other.type; |
| 780 } | 759 } |
| 781 } | 760 } |
| 782 | 761 |
| 783 /// Reference to a constant local, top-level, or static variable. | 762 /// Reference to a constant local, top-level, or static variable. |
| 784 class VariableConstantExpression extends ConstantExpression { | 763 class VariableConstantExpression extends ConstantExpression { |
| 785 final ConstantValue value; | |
| 786 final VariableElement element; | 764 final VariableElement element; |
| 787 | 765 |
| 788 VariableConstantExpression(this.value, this.element); | 766 VariableConstantExpression(this.element); |
| 789 | 767 |
| 790 ConstantExpressionKind get kind => ConstantExpressionKind.VARIABLE; | 768 ConstantExpressionKind get kind => ConstantExpressionKind.VARIABLE; |
| 791 | 769 |
| 792 accept(ConstantExpressionVisitor visitor, [context]) { | 770 accept(ConstantExpressionVisitor visitor, [context]) { |
| 793 return visitor.visitVariable(this, context); | 771 return visitor.visitVariable(this, context); |
| 794 } | 772 } |
| 795 | 773 |
| 796 @override | 774 @override |
| 797 ConstantValue evaluate(Environment environment, | 775 ConstantValue evaluate(Environment environment, |
| 798 ConstantSystem constantSystem) { | 776 ConstantSystem constantSystem) { |
| 799 return element.constant.evaluate(environment, constantSystem); | 777 return element.constant.evaluate(environment, constantSystem); |
| 800 } | 778 } |
| 801 | 779 |
| 802 @override | 780 @override |
| 803 int _computeHashCode() => 13 * element.hashCode; | 781 int _computeHashCode() => 13 * element.hashCode; |
| 804 | 782 |
| 805 @override | 783 @override |
| 806 bool _equals(VariableConstantExpression other) { | 784 bool _equals(VariableConstantExpression other) { |
| 807 return element == other.element; | 785 return element == other.element; |
| 808 } | 786 } |
| 809 } | 787 } |
| 810 | 788 |
| 811 /// Reference to a top-level or static function. | 789 /// Reference to a top-level or static function. |
| 812 class FunctionConstantExpression extends ConstantExpression { | 790 class FunctionConstantExpression extends ConstantExpression { |
| 813 final FunctionConstantValue value; | |
| 814 final FunctionElement element; | 791 final FunctionElement element; |
| 815 | 792 |
| 816 FunctionConstantExpression(this.value, this.element); | 793 FunctionConstantExpression(this.element); |
| 817 | 794 |
| 818 ConstantExpressionKind get kind => ConstantExpressionKind.FUNCTION; | 795 ConstantExpressionKind get kind => ConstantExpressionKind.FUNCTION; |
| 819 | 796 |
| 820 accept(ConstantExpressionVisitor visitor, [context]) { | 797 accept(ConstantExpressionVisitor visitor, [context]) { |
| 821 return visitor.visitFunction(this, context); | 798 return visitor.visitFunction(this, context); |
| 822 } | 799 } |
| 823 | 800 |
| 824 @override | 801 @override |
| 825 ConstantValue evaluate(Environment environment, | 802 ConstantValue evaluate(Environment environment, |
| 826 ConstantSystem constantSystem) { | 803 ConstantSystem constantSystem) { |
| 827 return new FunctionConstantValue(element); | 804 return new FunctionConstantValue(element); |
| 828 } | 805 } |
| 829 | 806 |
| 830 @override | 807 @override |
| 831 int _computeHashCode() => 13 * element.hashCode; | 808 int _computeHashCode() => 13 * element.hashCode; |
| 832 | 809 |
| 833 @override | 810 @override |
| 834 bool _equals(FunctionConstantExpression other) { | 811 bool _equals(FunctionConstantExpression other) { |
| 835 return element == other.element; | 812 return element == other.element; |
| 836 } | 813 } |
| 837 } | 814 } |
| 838 | 815 |
| 839 /// A constant binary expression like `a * b`. | 816 /// A constant binary expression like `a * b`. |
| 840 class BinaryConstantExpression extends ConstantExpression { | 817 class BinaryConstantExpression extends ConstantExpression { |
| 841 final ConstantValue value; | |
| 842 final ConstantExpression left; | 818 final ConstantExpression left; |
| 843 final BinaryOperator operator; | 819 final BinaryOperator operator; |
| 844 final ConstantExpression right; | 820 final ConstantExpression right; |
| 845 | 821 |
| 846 BinaryConstantExpression(this.value, this.left, this.operator, this.right) { | 822 BinaryConstantExpression(this.left, this.operator, this.right) { |
| 847 assert(PRECEDENCE_MAP[operator.kind] != null); | 823 assert(PRECEDENCE_MAP[operator.kind] != null); |
| 848 } | 824 } |
| 849 | 825 |
| 850 ConstantExpressionKind get kind => ConstantExpressionKind.BINARY; | 826 ConstantExpressionKind get kind => ConstantExpressionKind.BINARY; |
| 851 | 827 |
| 852 accept(ConstantExpressionVisitor visitor, [context]) { | 828 accept(ConstantExpressionVisitor visitor, [context]) { |
| 853 return visitor.visitBinary(this, context); | 829 return visitor.visitBinary(this, context); |
| 854 } | 830 } |
| 855 | 831 |
| 856 @override | 832 @override |
| 857 ConstantValue evaluate(Environment environment, | 833 ConstantValue evaluate(Environment environment, |
| 858 ConstantSystem constantSystem) { | 834 ConstantSystem constantSystem) { |
| 859 return constantSystem.lookupBinary(operator).fold( | 835 return constantSystem.lookupBinary(operator).fold( |
| 860 left.evaluate(environment, constantSystem), | 836 left.evaluate(environment, constantSystem), |
| 861 right.evaluate(environment, constantSystem)); | 837 right.evaluate(environment, constantSystem)); |
| 862 } | 838 } |
| 863 | 839 |
| 864 ConstantExpression apply(NormalizedArguments arguments) { | 840 ConstantExpression apply(NormalizedArguments arguments) { |
| 865 return new BinaryConstantExpression( | 841 return new BinaryConstantExpression( |
| 866 value, | |
| 867 left.apply(arguments), | 842 left.apply(arguments), |
| 868 operator, | 843 operator, |
| 869 right.apply(arguments)); | 844 right.apply(arguments)); |
| 870 } | 845 } |
| 871 | 846 |
| 872 int get precedence => PRECEDENCE_MAP[operator.kind]; | 847 int get precedence => PRECEDENCE_MAP[operator.kind]; |
| 873 | 848 |
| 874 @override | 849 @override |
| 875 int _computeHashCode() { | 850 int _computeHashCode() { |
| 876 return 13 * operator.hashCode + | 851 return 13 * operator.hashCode + |
| (...skipping 26 matching lines...) Expand all Loading... |
| 903 BinaryOperatorKind.GT: 7, | 878 BinaryOperatorKind.GT: 7, |
| 904 BinaryOperatorKind.LT: 7, | 879 BinaryOperatorKind.LT: 7, |
| 905 BinaryOperatorKind.GTEQ: 7, | 880 BinaryOperatorKind.GTEQ: 7, |
| 906 BinaryOperatorKind.LTEQ: 7, | 881 BinaryOperatorKind.LTEQ: 7, |
| 907 BinaryOperatorKind.MOD: 13, | 882 BinaryOperatorKind.MOD: 13, |
| 908 }; | 883 }; |
| 909 } | 884 } |
| 910 | 885 |
| 911 /// A constant identical invocation like `identical(a, b)`. | 886 /// A constant identical invocation like `identical(a, b)`. |
| 912 class IdenticalConstantExpression extends ConstantExpression { | 887 class IdenticalConstantExpression extends ConstantExpression { |
| 913 final ConstantValue value; | |
| 914 final ConstantExpression left; | 888 final ConstantExpression left; |
| 915 final ConstantExpression right; | 889 final ConstantExpression right; |
| 916 | 890 |
| 917 IdenticalConstantExpression(this.value, this.left, this.right); | 891 IdenticalConstantExpression(this.left, this.right); |
| 918 | 892 |
| 919 ConstantExpressionKind get kind => ConstantExpressionKind.IDENTICAL; | 893 ConstantExpressionKind get kind => ConstantExpressionKind.IDENTICAL; |
| 920 | 894 |
| 921 accept(ConstantExpressionVisitor visitor, [context]) { | 895 accept(ConstantExpressionVisitor visitor, [context]) { |
| 922 return visitor.visitIdentical(this, context); | 896 return visitor.visitIdentical(this, context); |
| 923 } | 897 } |
| 924 | 898 |
| 925 @override | 899 @override |
| 926 ConstantValue evaluate(Environment environment, | 900 ConstantValue evaluate(Environment environment, |
| 927 ConstantSystem constantSystem) { | 901 ConstantSystem constantSystem) { |
| 928 return constantSystem.identity.fold( | 902 return constantSystem.identity.fold( |
| 929 left.evaluate(environment, constantSystem), | 903 left.evaluate(environment, constantSystem), |
| 930 right.evaluate(environment, constantSystem)); | 904 right.evaluate(environment, constantSystem)); |
| 931 } | 905 } |
| 932 | 906 |
| 933 ConstantExpression apply(NormalizedArguments arguments) { | 907 ConstantExpression apply(NormalizedArguments arguments) { |
| 934 return new IdenticalConstantExpression( | 908 return new IdenticalConstantExpression( |
| 935 value, | |
| 936 left.apply(arguments), | 909 left.apply(arguments), |
| 937 right.apply(arguments)); | 910 right.apply(arguments)); |
| 938 } | 911 } |
| 939 | 912 |
| 940 int get precedence => 15; | 913 int get precedence => 15; |
| 941 | 914 |
| 942 @override | 915 @override |
| 943 int _computeHashCode() { | 916 int _computeHashCode() { |
| 944 return 17 * left.hashCode + | 917 return 17 * left.hashCode + |
| 945 19 * right.hashCode; | 918 19 * right.hashCode; |
| 946 } | 919 } |
| 947 | 920 |
| 948 @override | 921 @override |
| 949 bool _equals(IdenticalConstantExpression other) { | 922 bool _equals(IdenticalConstantExpression other) { |
| 950 return left == other.left && | 923 return left == other.left && |
| 951 right == other.right; | 924 right == other.right; |
| 952 } | 925 } |
| 953 } | 926 } |
| 954 | 927 |
| 955 /// A unary constant expression like `-a`. | 928 /// A unary constant expression like `-a`. |
| 956 class UnaryConstantExpression extends ConstantExpression { | 929 class UnaryConstantExpression extends ConstantExpression { |
| 957 final ConstantValue value; | |
| 958 final UnaryOperator operator; | 930 final UnaryOperator operator; |
| 959 final ConstantExpression expression; | 931 final ConstantExpression expression; |
| 960 | 932 |
| 961 UnaryConstantExpression(this.value, this.operator, this.expression) { | 933 UnaryConstantExpression(this.operator, this.expression) { |
| 962 assert(PRECEDENCE_MAP[operator.kind] != null); | 934 assert(PRECEDENCE_MAP[operator.kind] != null); |
| 963 } | 935 } |
| 964 | 936 |
| 965 ConstantExpressionKind get kind => ConstantExpressionKind.UNARY; | 937 ConstantExpressionKind get kind => ConstantExpressionKind.UNARY; |
| 966 | 938 |
| 967 accept(ConstantExpressionVisitor visitor, [context]) { | 939 accept(ConstantExpressionVisitor visitor, [context]) { |
| 968 return visitor.visitUnary(this, context); | 940 return visitor.visitUnary(this, context); |
| 969 } | 941 } |
| 970 | 942 |
| 971 @override | 943 @override |
| 972 ConstantValue evaluate(Environment environment, | 944 ConstantValue evaluate(Environment environment, |
| 973 ConstantSystem constantSystem) { | 945 ConstantSystem constantSystem) { |
| 974 return constantSystem.lookupUnary(operator).fold( | 946 return constantSystem.lookupUnary(operator).fold( |
| 975 expression.evaluate(environment, constantSystem)); | 947 expression.evaluate(environment, constantSystem)); |
| 976 } | 948 } |
| 977 | 949 |
| 978 ConstantExpression apply(NormalizedArguments arguments) { | 950 ConstantExpression apply(NormalizedArguments arguments) { |
| 979 return new UnaryConstantExpression( | 951 return new UnaryConstantExpression( |
| 980 value, | |
| 981 operator, | 952 operator, |
| 982 expression.apply(arguments)); | 953 expression.apply(arguments)); |
| 983 } | 954 } |
| 984 | 955 |
| 985 int get precedence => PRECEDENCE_MAP[operator.kind]; | 956 int get precedence => PRECEDENCE_MAP[operator.kind]; |
| 986 | 957 |
| 987 @override | 958 @override |
| 988 int _computeHashCode() { | 959 int _computeHashCode() { |
| 989 return 13 * operator.hashCode + | 960 return 13 * operator.hashCode + |
| 990 17 * expression.hashCode; | 961 17 * expression.hashCode; |
| 991 } | 962 } |
| 992 | 963 |
| 993 @override | 964 @override |
| 994 bool _equals(UnaryConstantExpression other) { | 965 bool _equals(UnaryConstantExpression other) { |
| 995 return operator == other.operator && | 966 return operator == other.operator && |
| 996 expression == other.expression; | 967 expression == other.expression; |
| 997 } | 968 } |
| 998 | 969 |
| 999 static const Map<UnaryOperatorKind, int> PRECEDENCE_MAP = const { | 970 static const Map<UnaryOperatorKind, int> PRECEDENCE_MAP = const { |
| 1000 UnaryOperatorKind.NOT: 14, | 971 UnaryOperatorKind.NOT: 14, |
| 1001 UnaryOperatorKind.COMPLEMENT: 14, | 972 UnaryOperatorKind.COMPLEMENT: 14, |
| 1002 UnaryOperatorKind.NEGATE: 14, | 973 UnaryOperatorKind.NEGATE: 14, |
| 1003 }; | 974 }; |
| 1004 } | 975 } |
| 1005 | 976 |
| 1006 | 977 |
| 1007 /// A string length constant expression like `a.length`. | 978 /// A string length constant expression like `a.length`. |
| 1008 class StringLengthConstantExpression extends ConstantExpression { | 979 class StringLengthConstantExpression extends ConstantExpression { |
| 1009 final ConstantValue value; | |
| 1010 final ConstantExpression expression; | 980 final ConstantExpression expression; |
| 1011 | 981 |
| 1012 StringLengthConstantExpression(this.value, this.expression); | 982 StringLengthConstantExpression(this.expression); |
| 1013 | 983 |
| 1014 ConstantExpressionKind get kind => ConstantExpressionKind.STRING_LENGTH; | 984 ConstantExpressionKind get kind => ConstantExpressionKind.STRING_LENGTH; |
| 1015 | 985 |
| 1016 accept(ConstantExpressionVisitor visitor, [context]) { | 986 accept(ConstantExpressionVisitor visitor, [context]) { |
| 1017 return visitor.visitStringLength(this, context); | 987 return visitor.visitStringLength(this, context); |
| 1018 } | 988 } |
| 1019 | 989 |
| 1020 @override | 990 @override |
| 1021 ConstantValue evaluate(Environment environment, | 991 ConstantValue evaluate(Environment environment, |
| 1022 ConstantSystem constantSystem) { | 992 ConstantSystem constantSystem) { |
| 1023 ConstantValue value = expression.evaluate(environment, constantSystem); | 993 ConstantValue value = expression.evaluate(environment, constantSystem); |
| 1024 if (value.isString) { | 994 if (value.isString) { |
| 1025 StringConstantValue stringValue = value; | 995 StringConstantValue stringValue = value; |
| 1026 return constantSystem.createInt(stringValue.primitiveValue.length); | 996 return constantSystem.createInt(stringValue.primitiveValue.length); |
| 1027 } | 997 } |
| 1028 return new NonConstantValue(); | 998 return new NonConstantValue(); |
| 1029 } | 999 } |
| 1030 | 1000 |
| 1031 ConstantExpression apply(NormalizedArguments arguments) { | 1001 ConstantExpression apply(NormalizedArguments arguments) { |
| 1032 return new StringLengthConstantExpression( | 1002 return new StringLengthConstantExpression(expression.apply(arguments)); |
| 1033 value, | |
| 1034 expression.apply(arguments)); | |
| 1035 } | 1003 } |
| 1036 | 1004 |
| 1037 int get precedence => 15; | 1005 int get precedence => 15; |
| 1038 | 1006 |
| 1039 @override | 1007 @override |
| 1040 int _computeHashCode() { | 1008 int _computeHashCode() { |
| 1041 return 23 * expression.hashCode; | 1009 return 23 * expression.hashCode; |
| 1042 } | 1010 } |
| 1043 | 1011 |
| 1044 @override | 1012 @override |
| 1045 bool _equals(StringLengthConstantExpression other) { | 1013 bool _equals(StringLengthConstantExpression other) { |
| 1046 return expression == other.expression; | 1014 return expression == other.expression; |
| 1047 } | 1015 } |
| 1048 } | 1016 } |
| 1049 | 1017 |
| 1050 /// A constant conditional expression like `a ? b : c`. | 1018 /// A constant conditional expression like `a ? b : c`. |
| 1051 class ConditionalConstantExpression extends ConstantExpression { | 1019 class ConditionalConstantExpression extends ConstantExpression { |
| 1052 final ConstantValue value; | |
| 1053 final ConstantExpression condition; | 1020 final ConstantExpression condition; |
| 1054 final ConstantExpression trueExp; | 1021 final ConstantExpression trueExp; |
| 1055 final ConstantExpression falseExp; | 1022 final ConstantExpression falseExp; |
| 1056 | 1023 |
| 1057 ConditionalConstantExpression(this.value, | 1024 ConditionalConstantExpression(this.condition, |
| 1058 this.condition, | |
| 1059 this.trueExp, | 1025 this.trueExp, |
| 1060 this.falseExp); | 1026 this.falseExp); |
| 1061 | 1027 |
| 1062 ConstantExpressionKind get kind => ConstantExpressionKind.CONDITIONAL; | 1028 ConstantExpressionKind get kind => ConstantExpressionKind.CONDITIONAL; |
| 1063 | 1029 |
| 1064 accept(ConstantExpressionVisitor visitor, [context]) { | 1030 accept(ConstantExpressionVisitor visitor, [context]) { |
| 1065 return visitor.visitConditional(this, context); | 1031 return visitor.visitConditional(this, context); |
| 1066 } | 1032 } |
| 1067 | 1033 |
| 1068 ConstantExpression apply(NormalizedArguments arguments) { | 1034 ConstantExpression apply(NormalizedArguments arguments) { |
| 1069 return new ConditionalConstantExpression( | 1035 return new ConditionalConstantExpression( |
| 1070 value, | |
| 1071 condition.apply(arguments), | 1036 condition.apply(arguments), |
| 1072 trueExp.apply(arguments), | 1037 trueExp.apply(arguments), |
| 1073 falseExp.apply(arguments)); | 1038 falseExp.apply(arguments)); |
| 1074 } | 1039 } |
| 1075 | 1040 |
| 1076 int get precedence => 3; | 1041 int get precedence => 3; |
| 1077 | 1042 |
| 1078 @override | 1043 @override |
| 1079 int _computeHashCode() { | 1044 int _computeHashCode() { |
| 1080 return 13 * condition.hashCode + | 1045 return 13 * condition.hashCode + |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1115 PositionalArgumentReference(this.index); | 1080 PositionalArgumentReference(this.index); |
| 1116 | 1081 |
| 1117 ConstantExpressionKind get kind { | 1082 ConstantExpressionKind get kind { |
| 1118 return ConstantExpressionKind.POSITIONAL_REFERENCE; | 1083 return ConstantExpressionKind.POSITIONAL_REFERENCE; |
| 1119 } | 1084 } |
| 1120 | 1085 |
| 1121 accept(ConstantExpressionVisitor visitor, [context]) { | 1086 accept(ConstantExpressionVisitor visitor, [context]) { |
| 1122 return visitor.visitPositional(this, context); | 1087 return visitor.visitPositional(this, context); |
| 1123 } | 1088 } |
| 1124 | 1089 |
| 1125 ConstantValue get value { | |
| 1126 throw new UnsupportedError('PositionalArgumentReference.value'); | |
| 1127 } | |
| 1128 | |
| 1129 ConstantExpression apply(NormalizedArguments arguments) { | 1090 ConstantExpression apply(NormalizedArguments arguments) { |
| 1130 return arguments.getPositionalArgument(index); | 1091 return arguments.getPositionalArgument(index); |
| 1131 } | 1092 } |
| 1132 | 1093 |
| 1133 @override | 1094 @override |
| 1134 int _computeHashCode() => 13 * index.hashCode; | 1095 int _computeHashCode() => 13 * index.hashCode; |
| 1135 | 1096 |
| 1136 @override | 1097 @override |
| 1137 bool _equals(PositionalArgumentReference other) => index == other.index; | 1098 bool _equals(PositionalArgumentReference other) => index == other.index; |
| 1138 | 1099 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1150 NamedArgumentReference(this.name); | 1111 NamedArgumentReference(this.name); |
| 1151 | 1112 |
| 1152 ConstantExpressionKind get kind { | 1113 ConstantExpressionKind get kind { |
| 1153 return ConstantExpressionKind.NAMED_REFERENCE; | 1114 return ConstantExpressionKind.NAMED_REFERENCE; |
| 1154 } | 1115 } |
| 1155 | 1116 |
| 1156 accept(ConstantExpressionVisitor visitor, [context]) { | 1117 accept(ConstantExpressionVisitor visitor, [context]) { |
| 1157 return visitor.visitNamed(this, context); | 1118 return visitor.visitNamed(this, context); |
| 1158 } | 1119 } |
| 1159 | 1120 |
| 1160 ConstantValue get value { | |
| 1161 throw new UnsupportedError('NamedArgumentReference.value'); | |
| 1162 } | |
| 1163 | |
| 1164 ConstantExpression apply(NormalizedArguments arguments) { | 1121 ConstantExpression apply(NormalizedArguments arguments) { |
| 1165 return arguments.getNamedArgument(name); | 1122 return arguments.getNamedArgument(name); |
| 1166 } | 1123 } |
| 1167 | 1124 |
| 1168 @override | 1125 @override |
| 1169 int _computeHashCode() => 13 * name.hashCode; | 1126 int _computeHashCode() => 13 * name.hashCode; |
| 1170 | 1127 |
| 1171 @override | 1128 @override |
| 1172 bool _equals(NamedArgumentReference other) => name == other.name; | 1129 bool _equals(NamedArgumentReference other) => name == other.name; |
| 1173 | 1130 |
| 1174 @override | 1131 @override |
| 1175 ConstantValue evaluate(Environment environment, | 1132 ConstantValue evaluate(Environment environment, |
| 1176 ConstantSystem constantSystem) { | 1133 ConstantSystem constantSystem) { |
| 1177 throw new UnsupportedError('NamedArgumentReference.evaluate'); | 1134 throw new UnsupportedError('NamedArgumentReference.evaluate'); |
| 1178 } | 1135 } |
| 1179 } | 1136 } |
| 1180 | 1137 |
| 1181 abstract class FromEnvironmentConstantExpression extends ConstantExpression { | 1138 abstract class FromEnvironmentConstantExpression extends ConstantExpression { |
| 1182 final ConstantValue value; | |
| 1183 final ConstantExpression name; | 1139 final ConstantExpression name; |
| 1184 final ConstantExpression defaultValue; | 1140 final ConstantExpression defaultValue; |
| 1185 | 1141 |
| 1186 FromEnvironmentConstantExpression(this.value, this.name, this.defaultValue); | 1142 FromEnvironmentConstantExpression(this.name, this.defaultValue); |
| 1187 | 1143 |
| 1188 @override | 1144 @override |
| 1189 int _computeHashCode() { | 1145 int _computeHashCode() { |
| 1190 return 13 * name.hashCode + | 1146 return 13 * name.hashCode + |
| 1191 17 * defaultValue.hashCode; | 1147 17 * defaultValue.hashCode; |
| 1192 } | 1148 } |
| 1193 | 1149 |
| 1194 @override | 1150 @override |
| 1195 bool _equals(FromEnvironmentConstantExpression other) { | 1151 bool _equals(FromEnvironmentConstantExpression other) { |
| 1196 return name == other.name && | 1152 return name == other.name && |
| 1197 defaultValue == other.defaultValue; | 1153 defaultValue == other.defaultValue; |
| 1198 } | 1154 } |
| 1199 } | 1155 } |
| 1200 | 1156 |
| 1201 /// A `const bool.fromEnvironment` constant. | 1157 /// A `const bool.fromEnvironment` constant. |
| 1202 class BoolFromEnvironmentConstantExpression | 1158 class BoolFromEnvironmentConstantExpression |
| 1203 extends FromEnvironmentConstantExpression { | 1159 extends FromEnvironmentConstantExpression { |
| 1204 | 1160 |
| 1205 BoolFromEnvironmentConstantExpression( | 1161 BoolFromEnvironmentConstantExpression( |
| 1206 ConstantValue value, | |
| 1207 ConstantExpression name, | 1162 ConstantExpression name, |
| 1208 ConstantExpression defaultValue) | 1163 ConstantExpression defaultValue) |
| 1209 : super(value, name, defaultValue); | 1164 : super(name, defaultValue); |
| 1210 | 1165 |
| 1211 ConstantExpressionKind get kind { | 1166 ConstantExpressionKind get kind { |
| 1212 return ConstantExpressionKind.BOOL_FROM_ENVIRONMENT; | 1167 return ConstantExpressionKind.BOOL_FROM_ENVIRONMENT; |
| 1213 } | 1168 } |
| 1214 | 1169 |
| 1215 accept(ConstantExpressionVisitor visitor, [context]) { | 1170 accept(ConstantExpressionVisitor visitor, [context]) { |
| 1216 return visitor.visitBoolFromEnvironment(this, context); | 1171 return visitor.visitBoolFromEnvironment(this, context); |
| 1217 } | 1172 } |
| 1218 | 1173 |
| 1219 @override | 1174 @override |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1238 return constantSystem.createBool(true); | 1193 return constantSystem.createBool(true); |
| 1239 } else if (text == 'false') { | 1194 } else if (text == 'false') { |
| 1240 return constantSystem.createBool(false); | 1195 return constantSystem.createBool(false); |
| 1241 } else { | 1196 } else { |
| 1242 return defaultConstantValue; | 1197 return defaultConstantValue; |
| 1243 } | 1198 } |
| 1244 } | 1199 } |
| 1245 | 1200 |
| 1246 ConstantExpression apply(NormalizedArguments arguments) { | 1201 ConstantExpression apply(NormalizedArguments arguments) { |
| 1247 return new BoolFromEnvironmentConstantExpression( | 1202 return new BoolFromEnvironmentConstantExpression( |
| 1248 null, | |
| 1249 name.apply(arguments), | 1203 name.apply(arguments), |
| 1250 defaultValue != null ? defaultValue.apply(arguments) : null); | 1204 defaultValue != null ? defaultValue.apply(arguments) : null); |
| 1251 } | 1205 } |
| 1252 } | 1206 } |
| 1253 | 1207 |
| 1254 /// A `const int.fromEnvironment` constant. | 1208 /// A `const int.fromEnvironment` constant. |
| 1255 class IntFromEnvironmentConstantExpression | 1209 class IntFromEnvironmentConstantExpression |
| 1256 extends FromEnvironmentConstantExpression { | 1210 extends FromEnvironmentConstantExpression { |
| 1257 | 1211 |
| 1258 IntFromEnvironmentConstantExpression( | 1212 IntFromEnvironmentConstantExpression( |
| 1259 ConstantValue value, | |
| 1260 ConstantExpression name, | 1213 ConstantExpression name, |
| 1261 ConstantExpression defaultValue) | 1214 ConstantExpression defaultValue) |
| 1262 : super(value, name, defaultValue); | 1215 : super(name, defaultValue); |
| 1263 | 1216 |
| 1264 ConstantExpressionKind get kind { | 1217 ConstantExpressionKind get kind { |
| 1265 return ConstantExpressionKind.INT_FROM_ENVIRONMENT; | 1218 return ConstantExpressionKind.INT_FROM_ENVIRONMENT; |
| 1266 } | 1219 } |
| 1267 | 1220 |
| 1268 accept(ConstantExpressionVisitor visitor, [context]) { | 1221 accept(ConstantExpressionVisitor visitor, [context]) { |
| 1269 return visitor.visitIntFromEnvironment(this, context); | 1222 return visitor.visitIntFromEnvironment(this, context); |
| 1270 } | 1223 } |
| 1271 | 1224 |
| 1272 @override | 1225 @override |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1293 } | 1246 } |
| 1294 if (value == null) { | 1247 if (value == null) { |
| 1295 return defaultConstantValue; | 1248 return defaultConstantValue; |
| 1296 } else { | 1249 } else { |
| 1297 return constantSystem.createInt(value); | 1250 return constantSystem.createInt(value); |
| 1298 } | 1251 } |
| 1299 } | 1252 } |
| 1300 | 1253 |
| 1301 ConstantExpression apply(NormalizedArguments arguments) { | 1254 ConstantExpression apply(NormalizedArguments arguments) { |
| 1302 return new IntFromEnvironmentConstantExpression( | 1255 return new IntFromEnvironmentConstantExpression( |
| 1303 null, | |
| 1304 name.apply(arguments), | 1256 name.apply(arguments), |
| 1305 defaultValue != null ? defaultValue.apply(arguments) : null); | 1257 defaultValue != null ? defaultValue.apply(arguments) : null); |
| 1306 } | 1258 } |
| 1307 } | 1259 } |
| 1308 | 1260 |
| 1309 /// A `const String.fromEnvironment` constant. | 1261 /// A `const String.fromEnvironment` constant. |
| 1310 class StringFromEnvironmentConstantExpression | 1262 class StringFromEnvironmentConstantExpression |
| 1311 extends FromEnvironmentConstantExpression { | 1263 extends FromEnvironmentConstantExpression { |
| 1312 | 1264 |
| 1313 StringFromEnvironmentConstantExpression( | 1265 StringFromEnvironmentConstantExpression( |
| 1314 ConstantValue value, | |
| 1315 ConstantExpression name, | 1266 ConstantExpression name, |
| 1316 ConstantExpression defaultValue) | 1267 ConstantExpression defaultValue) |
| 1317 : super(value, name, defaultValue); | 1268 : super(name, defaultValue); |
| 1318 | 1269 |
| 1319 ConstantExpressionKind get kind { | 1270 ConstantExpressionKind get kind { |
| 1320 return ConstantExpressionKind.STRING_FROM_ENVIRONMENT; | 1271 return ConstantExpressionKind.STRING_FROM_ENVIRONMENT; |
| 1321 } | 1272 } |
| 1322 | 1273 |
| 1323 accept(ConstantExpressionVisitor visitor, [context]) { | 1274 accept(ConstantExpressionVisitor visitor, [context]) { |
| 1324 return visitor.visitStringFromEnvironment(this, context); | 1275 return visitor.visitStringFromEnvironment(this, context); |
| 1325 } | 1276 } |
| 1326 | 1277 |
| 1327 @override | 1278 @override |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1344 nameStringConstantValue.primitiveValue.slowToString()); | 1295 nameStringConstantValue.primitiveValue.slowToString()); |
| 1345 if (text == null) { | 1296 if (text == null) { |
| 1346 return defaultConstantValue; | 1297 return defaultConstantValue; |
| 1347 } else { | 1298 } else { |
| 1348 return constantSystem.createString(new DartString.literal(text)); | 1299 return constantSystem.createString(new DartString.literal(text)); |
| 1349 } | 1300 } |
| 1350 } | 1301 } |
| 1351 | 1302 |
| 1352 ConstantExpression apply(NormalizedArguments arguments) { | 1303 ConstantExpression apply(NormalizedArguments arguments) { |
| 1353 return new StringFromEnvironmentConstantExpression( | 1304 return new StringFromEnvironmentConstantExpression( |
| 1354 null, | |
| 1355 name.apply(arguments), | 1305 name.apply(arguments), |
| 1356 defaultValue != null ? defaultValue.apply(arguments) : null); | 1306 defaultValue != null ? defaultValue.apply(arguments) : null); |
| 1357 } | 1307 } |
| 1358 } | 1308 } |
| 1359 | 1309 |
| 1360 /// A constant expression referenced with a deferred prefix. | 1310 /// A constant expression referenced with a deferred prefix. |
| 1361 /// For example `lib.C`. | 1311 /// For example `lib.C`. |
| 1362 class DeferredConstantExpression extends ConstantExpression { | 1312 class DeferredConstantExpression extends ConstantExpression { |
| 1363 final ConstantValue value; | |
| 1364 final ConstantExpression expression; | 1313 final ConstantExpression expression; |
| 1365 final PrefixElement prefix; | 1314 final PrefixElement prefix; |
| 1366 | 1315 |
| 1367 DeferredConstantExpression(this.value, this.expression, this.prefix); | 1316 DeferredConstantExpression(this.expression, this.prefix); |
| 1368 | 1317 |
| 1369 ConstantExpressionKind get kind => ConstantExpressionKind.DEFERRED; | 1318 ConstantExpressionKind get kind => ConstantExpressionKind.DEFERRED; |
| 1370 | 1319 |
| 1371 @override | 1320 @override |
| 1372 ConstantValue evaluate(Environment environment, | 1321 ConstantValue evaluate(Environment environment, |
| 1373 ConstantSystem constantSystem) { | 1322 ConstantSystem constantSystem) { |
| 1374 return expression.evaluate(environment, constantSystem); | 1323 return expression.evaluate(environment, constantSystem); |
| 1375 } | 1324 } |
| 1376 | 1325 |
| 1377 @override | 1326 @override |
| 1378 int _computeHashCode() { | 1327 int _computeHashCode() { |
| 1379 return 13 * expression.hashCode; | 1328 return 13 * expression.hashCode; |
| 1380 } | 1329 } |
| 1381 | 1330 |
| 1382 ConstantExpression apply(NormalizedArguments arguments) { | 1331 ConstantExpression apply(NormalizedArguments arguments) { |
| 1383 return new DeferredConstantExpression( | 1332 return new DeferredConstantExpression( |
| 1384 value, expression.apply(arguments), prefix); | 1333 expression.apply(arguments), prefix); |
| 1385 } | 1334 } |
| 1386 | 1335 |
| 1387 @override | 1336 @override |
| 1388 bool _equals(DeferredConstantExpression other) { | 1337 bool _equals(DeferredConstantExpression other) { |
| 1389 return expression == other.expression; | 1338 return expression == other.expression; |
| 1390 } | 1339 } |
| 1391 | 1340 |
| 1392 @override | 1341 @override |
| 1393 accept(ConstantExpressionVisitor visitor, [context]) { | 1342 accept(ConstantExpressionVisitor visitor, [context]) { |
| 1394 return visitor.visitDeferred(this, context); | 1343 return visitor.visitDeferred(this, context); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1565 sb.write(exp.callStructure.namedArguments[index]); | 1514 sb.write(exp.callStructure.namedArguments[index]); |
| 1566 sb.write(': '); | 1515 sb.write(': '); |
| 1567 visit(exp.arguments[namedOffset + index]); | 1516 visit(exp.arguments[namedOffset + index]); |
| 1568 needsComma = true; | 1517 needsComma = true; |
| 1569 } | 1518 } |
| 1570 sb.write(')'); | 1519 sb.write(')'); |
| 1571 } | 1520 } |
| 1572 | 1521 |
| 1573 @override | 1522 @override |
| 1574 void visitConcatenate(ConcatenateConstantExpression exp, [_]) { | 1523 void visitConcatenate(ConcatenateConstantExpression exp, [_]) { |
| 1575 sb.write(exp.value.unparse()); | 1524 sb.write('"'); |
| 1525 for (ConstantExpression expression in exp.expressions) { |
| 1526 if (expression.kind == ConstantExpressionKind.STRING) { |
| 1527 StringConstantExpression string = expression; |
| 1528 // TODO(johnniwinther): Ensure correct escaping. |
| 1529 sb.write('${string.primitiveValue}'); |
| 1530 } else { |
| 1531 sb.write(r"${"); |
| 1532 visit(expression); |
| 1533 sb.write("}"); |
| 1534 } |
| 1535 |
| 1536 } |
| 1537 sb.write('"'); |
| 1576 } | 1538 } |
| 1577 | 1539 |
| 1578 @override | 1540 @override |
| 1579 void visitSymbol(SymbolConstantExpression exp, [_]) { | 1541 void visitSymbol(SymbolConstantExpression exp, [_]) { |
| 1580 sb.write('#'); | 1542 sb.write('#'); |
| 1581 sb.write(exp.name); | 1543 sb.write(exp.name); |
| 1582 } | 1544 } |
| 1583 | 1545 |
| 1584 @override | 1546 @override |
| 1585 void visitType(TypeConstantExpression exp, [_]) { | 1547 void visitType(TypeConstantExpression exp, [_]) { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1692 visit(exp.name); | 1654 visit(exp.name); |
| 1693 if (exp.defaultValue != null) { | 1655 if (exp.defaultValue != null) { |
| 1694 sb.write(', defaultValue: '); | 1656 sb.write(', defaultValue: '); |
| 1695 visit(exp.defaultValue); | 1657 visit(exp.defaultValue); |
| 1696 } | 1658 } |
| 1697 sb.write(')'); | 1659 sb.write(')'); |
| 1698 } | 1660 } |
| 1699 | 1661 |
| 1700 String toString() => sb.toString(); | 1662 String toString() => sb.toString(); |
| 1701 } | 1663 } |
| OLD | NEW |