| 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 '../common.dart'; | 7 import '../common.dart'; |
| 8 import '../constants/constant_system.dart'; | 8 import '../constants/constant_system.dart'; |
| 9 import '../common_elements.dart'; | 9 import '../common_elements.dart'; |
| 10 import '../elements/entities.dart'; | 10 import '../elements/entities.dart'; |
| 11 import '../elements/operators.dart'; | 11 import '../elements/operators.dart'; |
| 12 import '../elements/types.dart'; | 12 import '../elements/types.dart'; |
| 13 import '../tree/dartstring.dart' show DartString; | |
| 14 import '../universe/call_structure.dart' show CallStructure; | 13 import '../universe/call_structure.dart' show CallStructure; |
| 15 import 'constructors.dart'; | 14 import 'constructors.dart'; |
| 16 import 'evaluation.dart'; | 15 import 'evaluation.dart'; |
| 17 import 'values.dart'; | 16 import 'values.dart'; |
| 18 | 17 |
| 19 enum ConstantExpressionKind { | 18 enum ConstantExpressionKind { |
| 20 BINARY, | 19 BINARY, |
| 21 BOOL, | 20 BOOL, |
| 22 BOOL_FROM_ENVIRONMENT, | 21 BOOL_FROM_ENVIRONMENT, |
| 23 CONCATENATE, | 22 CONCATENATE, |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 } | 320 } |
| 322 | 321 |
| 323 @override | 322 @override |
| 324 void _createStructuredText(StringBuffer sb) { | 323 void _createStructuredText(StringBuffer sb) { |
| 325 sb.write('String(value=${primitiveValue})'); | 324 sb.write('String(value=${primitiveValue})'); |
| 326 } | 325 } |
| 327 | 326 |
| 328 @override | 327 @override |
| 329 ConstantValue evaluate( | 328 ConstantValue evaluate( |
| 330 EvaluationEnvironment environment, ConstantSystem constantSystem) { | 329 EvaluationEnvironment environment, ConstantSystem constantSystem) { |
| 331 return constantSystem.createString(new DartString.literal(primitiveValue)); | 330 return constantSystem.createString(primitiveValue); |
| 332 } | 331 } |
| 333 | 332 |
| 334 @override | 333 @override |
| 335 int _computeHashCode() => 23 * primitiveValue.hashCode; | 334 int _computeHashCode() => 23 * primitiveValue.hashCode; |
| 336 | 335 |
| 337 @override | 336 @override |
| 338 bool _equals(StringConstantExpression other) { | 337 bool _equals(StringConstantExpression other) { |
| 339 return primitiveValue == other.primitiveValue; | 338 return primitiveValue == other.primitiveValue; |
| 340 } | 339 } |
| 341 | 340 |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 646 } | 645 } |
| 647 | 646 |
| 648 ConstantExpression apply(NormalizedArguments arguments) { | 647 ConstantExpression apply(NormalizedArguments arguments) { |
| 649 return new ConcatenateConstantExpression( | 648 return new ConcatenateConstantExpression( |
| 650 expressions.map((a) => a.apply(arguments)).toList()); | 649 expressions.map((a) => a.apply(arguments)).toList()); |
| 651 } | 650 } |
| 652 | 651 |
| 653 @override | 652 @override |
| 654 ConstantValue evaluate( | 653 ConstantValue evaluate( |
| 655 EvaluationEnvironment environment, ConstantSystem constantSystem) { | 654 EvaluationEnvironment environment, ConstantSystem constantSystem) { |
| 656 DartString accumulator; | 655 StringBuffer sb = new StringBuffer(); |
| 657 for (ConstantExpression expression in expressions) { | 656 for (ConstantExpression expression in expressions) { |
| 658 ConstantValue value = expression.evaluate(environment, constantSystem); | 657 ConstantValue value = expression.evaluate(environment, constantSystem); |
| 659 DartString valueString; | 658 if (value.isPrimitive) { |
| 660 if (value.isNum || value.isBool || value.isNull) { | |
| 661 PrimitiveConstantValue primitive = value; | 659 PrimitiveConstantValue primitive = value; |
| 662 valueString = | 660 sb.write(primitive.primitiveValue); |
| 663 new DartString.literal(primitive.primitiveValue.toString()); | |
| 664 } else if (value.isString) { | |
| 665 PrimitiveConstantValue primitive = value; | |
| 666 valueString = primitive.primitiveValue; | |
| 667 } else { | 661 } else { |
| 668 // TODO(johnniwinther): Specialize message to indicated that the problem | 662 // TODO(johnniwinther): Specialize message to indicated that the problem |
| 669 // is not constness but the types of the const expressions. | 663 // is not constness but the types of the const expressions. |
| 670 return new NonConstantValue(); | 664 return new NonConstantValue(); |
| 671 } | 665 } |
| 672 if (accumulator == null) { | |
| 673 accumulator = valueString; | |
| 674 } else { | |
| 675 accumulator = new DartString.concat(accumulator, valueString); | |
| 676 } | |
| 677 } | 666 } |
| 678 return constantSystem.createString(accumulator); | 667 return constantSystem.createString(sb.toString()); |
| 679 } | 668 } |
| 680 | 669 |
| 681 @override | 670 @override |
| 682 int _computeHashCode() { | 671 int _computeHashCode() { |
| 683 int hashCode = 17 * expressions.length; | 672 int hashCode = 17 * expressions.length; |
| 684 for (ConstantExpression value in expressions) { | 673 for (ConstantExpression value in expressions) { |
| 685 hashCode ^= 19 * value.hashCode; | 674 hashCode ^= 19 * value.hashCode; |
| 686 } | 675 } |
| 687 return hashCode; | 676 return hashCode; |
| 688 } | 677 } |
| (...skipping 737 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1426 ConstantValue defaultConstantValue; | 1415 ConstantValue defaultConstantValue; |
| 1427 if (defaultValue != null) { | 1416 if (defaultValue != null) { |
| 1428 defaultConstantValue = defaultValue.evaluate(environment, constantSystem); | 1417 defaultConstantValue = defaultValue.evaluate(environment, constantSystem); |
| 1429 } else { | 1418 } else { |
| 1430 defaultConstantValue = constantSystem.createBool(false); | 1419 defaultConstantValue = constantSystem.createBool(false); |
| 1431 } | 1420 } |
| 1432 if (!nameConstantValue.isString) { | 1421 if (!nameConstantValue.isString) { |
| 1433 return new NonConstantValue(); | 1422 return new NonConstantValue(); |
| 1434 } | 1423 } |
| 1435 StringConstantValue nameStringConstantValue = nameConstantValue; | 1424 StringConstantValue nameStringConstantValue = nameConstantValue; |
| 1436 String text = environment.readFromEnvironment( | 1425 String text = |
| 1437 nameStringConstantValue.primitiveValue.slowToString()); | 1426 environment.readFromEnvironment(nameStringConstantValue.primitiveValue); |
| 1438 if (text == 'true') { | 1427 if (text == 'true') { |
| 1439 return constantSystem.createBool(true); | 1428 return constantSystem.createBool(true); |
| 1440 } else if (text == 'false') { | 1429 } else if (text == 'false') { |
| 1441 return constantSystem.createBool(false); | 1430 return constantSystem.createBool(false); |
| 1442 } else { | 1431 } else { |
| 1443 return defaultConstantValue; | 1432 return defaultConstantValue; |
| 1444 } | 1433 } |
| 1445 } | 1434 } |
| 1446 | 1435 |
| 1447 ConstantExpression apply(NormalizedArguments arguments) { | 1436 ConstantExpression apply(NormalizedArguments arguments) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1490 ConstantValue defaultConstantValue; | 1479 ConstantValue defaultConstantValue; |
| 1491 if (defaultValue != null) { | 1480 if (defaultValue != null) { |
| 1492 defaultConstantValue = defaultValue.evaluate(environment, constantSystem); | 1481 defaultConstantValue = defaultValue.evaluate(environment, constantSystem); |
| 1493 } else { | 1482 } else { |
| 1494 defaultConstantValue = constantSystem.createNull(); | 1483 defaultConstantValue = constantSystem.createNull(); |
| 1495 } | 1484 } |
| 1496 if (!nameConstantValue.isString) { | 1485 if (!nameConstantValue.isString) { |
| 1497 return new NonConstantValue(); | 1486 return new NonConstantValue(); |
| 1498 } | 1487 } |
| 1499 StringConstantValue nameStringConstantValue = nameConstantValue; | 1488 StringConstantValue nameStringConstantValue = nameConstantValue; |
| 1500 String text = environment.readFromEnvironment( | 1489 String text = |
| 1501 nameStringConstantValue.primitiveValue.slowToString()); | 1490 environment.readFromEnvironment(nameStringConstantValue.primitiveValue); |
| 1502 int value; | 1491 int value; |
| 1503 if (text != null) { | 1492 if (text != null) { |
| 1504 value = int.parse(text, onError: (_) => null); | 1493 value = int.parse(text, onError: (_) => null); |
| 1505 } | 1494 } |
| 1506 if (value == null) { | 1495 if (value == null) { |
| 1507 return defaultConstantValue; | 1496 return defaultConstantValue; |
| 1508 } else { | 1497 } else { |
| 1509 return constantSystem.createInt(value); | 1498 return constantSystem.createInt(value); |
| 1510 } | 1499 } |
| 1511 } | 1500 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1556 ConstantValue defaultConstantValue; | 1545 ConstantValue defaultConstantValue; |
| 1557 if (defaultValue != null) { | 1546 if (defaultValue != null) { |
| 1558 defaultConstantValue = defaultValue.evaluate(environment, constantSystem); | 1547 defaultConstantValue = defaultValue.evaluate(environment, constantSystem); |
| 1559 } else { | 1548 } else { |
| 1560 defaultConstantValue = constantSystem.createNull(); | 1549 defaultConstantValue = constantSystem.createNull(); |
| 1561 } | 1550 } |
| 1562 if (!nameConstantValue.isString) { | 1551 if (!nameConstantValue.isString) { |
| 1563 return new NonConstantValue(); | 1552 return new NonConstantValue(); |
| 1564 } | 1553 } |
| 1565 StringConstantValue nameStringConstantValue = nameConstantValue; | 1554 StringConstantValue nameStringConstantValue = nameConstantValue; |
| 1566 String text = environment.readFromEnvironment( | 1555 String text = |
| 1567 nameStringConstantValue.primitiveValue.slowToString()); | 1556 environment.readFromEnvironment(nameStringConstantValue.primitiveValue); |
| 1568 if (text == null) { | 1557 if (text == null) { |
| 1569 return defaultConstantValue; | 1558 return defaultConstantValue; |
| 1570 } else { | 1559 } else { |
| 1571 return constantSystem.createString(new DartString.literal(text)); | 1560 return constantSystem.createString(text); |
| 1572 } | 1561 } |
| 1573 } | 1562 } |
| 1574 | 1563 |
| 1575 ConstantExpression apply(NormalizedArguments arguments) { | 1564 ConstantExpression apply(NormalizedArguments arguments) { |
| 1576 return new StringFromEnvironmentConstantExpression(name.apply(arguments), | 1565 return new StringFromEnvironmentConstantExpression(name.apply(arguments), |
| 1577 defaultValue != null ? defaultValue.apply(arguments) : null); | 1566 defaultValue != null ? defaultValue.apply(arguments) : null); |
| 1578 } | 1567 } |
| 1579 | 1568 |
| 1580 @override | 1569 @override |
| 1581 InterfaceType getKnownType(CommonElements commonElements) => | 1570 InterfaceType getKnownType(CommonElements commonElements) => |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1936 visit(exp.name); | 1925 visit(exp.name); |
| 1937 if (exp.defaultValue != null) { | 1926 if (exp.defaultValue != null) { |
| 1938 sb.write(', defaultValue: '); | 1927 sb.write(', defaultValue: '); |
| 1939 visit(exp.defaultValue); | 1928 visit(exp.defaultValue); |
| 1940 } | 1929 } |
| 1941 sb.write(')'); | 1930 sb.write(')'); |
| 1942 } | 1931 } |
| 1943 | 1932 |
| 1944 String toString() => sb.toString(); | 1933 String toString() => sb.toString(); |
| 1945 } | 1934 } |
| OLD | NEW |