mirror of
https://github.com/dart-lang/sdk
synced 2024-11-05 18:22:09 +00:00
Revert "Merge the JavaScript constant system into the base constant system and"
This reverts commit cf51eb2fba
.
Reason for revert: <INSERT REASONING HERE>
Original change's description:
> Merge the JavaScript constant system into the base constant system and
> flatten the inheritance hierarchy.
>
> dart2js only needs one constant system at most.
>
> Change-Id: I72e446652ea44298677e986b06e63152e381353b
> Reviewed-on: https://dart-review.googlesource.com/c/93880
> Reviewed-by: Sigmund Cherem <sigmund@google.com>
> Reviewed-by: Johnni Winther <johnniwinther@google.com>
TBR=johnniwinther@google.com,sigmund@google.com,fishythefish@google.com
Change-Id: I2d403834ac3ac41371e041d21eec3d83d152343a
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://dart-review.googlesource.com/c/94102
Reviewed-by: Mayank Patke <fishythefish@google.com>
Commit-Queue: Mayank Patke <fishythefish@google.com>
This commit is contained in:
parent
2c3dfa9e71
commit
388f8e7731
7 changed files with 282 additions and 212 deletions
|
@ -25,19 +25,19 @@ abstract class BinaryOperation extends Operation {
|
|||
apply(left, right);
|
||||
}
|
||||
|
||||
class BitNotOperation implements UnaryOperation {
|
||||
class JavaScriptBitNotOperation implements UnaryOperation {
|
||||
final String name = '~';
|
||||
const BitNotOperation();
|
||||
const JavaScriptBitNotOperation();
|
||||
|
||||
ConstantValue fold(ConstantValue constant) {
|
||||
if (ConstantSystem.only.isInt(constant)) {
|
||||
if (JavaScriptConstantSystem.only.isInt(constant)) {
|
||||
// In JavaScript we don't check for -0 and treat it as if it was zero.
|
||||
if (constant.isMinusZero) {
|
||||
constant = ConstantSystem.only.createInt(BigInt.zero);
|
||||
constant = JavaScriptConstantSystem.only.createInt(BigInt.zero);
|
||||
}
|
||||
IntConstantValue intConstant = constant;
|
||||
// We convert the result of bit-operations to 32 bit unsigned integers.
|
||||
return ConstantSystem.only.createInt32(~intConstant.intValue);
|
||||
return JavaScriptConstantSystem.only.createInt32(~intConstant.intValue);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -49,11 +49,12 @@ class NegateOperation implements UnaryOperation {
|
|||
ConstantValue fold(ConstantValue constant) {
|
||||
if (constant.isInt) {
|
||||
IntConstantValue intConstant = constant;
|
||||
return ConstantSystem.only.createInt(-intConstant.intValue);
|
||||
return JavaScriptConstantSystem.only.createInt(-intConstant.intValue);
|
||||
}
|
||||
if (constant.isDouble) {
|
||||
DoubleConstantValue doubleConstant = constant;
|
||||
return ConstantSystem.only.createDouble(-doubleConstant.doubleValue);
|
||||
return JavaScriptConstantSystem.only
|
||||
.createDouble(-doubleConstant.doubleValue);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -70,7 +71,7 @@ class JavaScriptNegateOperation implements UnaryOperation {
|
|||
if (constant.isInt) {
|
||||
IntConstantValue intConstant = constant;
|
||||
if (intConstant.intValue == BigInt.zero) {
|
||||
return ConstantSystem.only.createDouble(-0.0);
|
||||
return JavaScriptConstantSystem.only.createDouble(-0.0);
|
||||
}
|
||||
}
|
||||
return dartNegateOperation.fold(constant);
|
||||
|
@ -83,7 +84,7 @@ class NotOperation implements UnaryOperation {
|
|||
ConstantValue fold(ConstantValue constant) {
|
||||
if (constant.isBool) {
|
||||
BoolConstantValue boolConstant = constant;
|
||||
return ConstantSystem.only.createBool(!boolConstant.boolValue);
|
||||
return JavaScriptConstantSystem.only.createBool(!boolConstant.boolValue);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -98,7 +99,7 @@ abstract class BinaryBitOperation implements BinaryOperation {
|
|||
IntConstantValue rightInt = right;
|
||||
BigInt resultValue = foldInts(leftInt.intValue, rightInt.intValue);
|
||||
if (resultValue == null) return null;
|
||||
return ConstantSystem.only.createInt(resultValue);
|
||||
return JavaScriptConstantSystem.only.createInt(resultValue);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -118,15 +119,15 @@ class JavaScriptBinaryBitOperation implements BinaryOperation {
|
|||
ConstantValue fold(ConstantValue left, ConstantValue right) {
|
||||
// In JavaScript we don't check for -0 and treat it as if it was zero.
|
||||
if (left.isMinusZero) {
|
||||
left = ConstantSystem.only.createInt(BigInt.zero);
|
||||
left = JavaScriptConstantSystem.only.createInt(BigInt.zero);
|
||||
}
|
||||
if (right.isMinusZero) {
|
||||
right = ConstantSystem.only.createInt(BigInt.zero);
|
||||
right = JavaScriptConstantSystem.only.createInt(BigInt.zero);
|
||||
}
|
||||
IntConstantValue result = dartBitOperation.fold(left, right);
|
||||
if (result != null) {
|
||||
// We convert the result of bit-operations to 32 bit unsigned integers.
|
||||
return ConstantSystem.only.createInt32(result.intValue);
|
||||
return JavaScriptConstantSystem.only.createInt32(result.intValue);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -187,7 +188,7 @@ class JavaScriptShiftRightOperation extends JavaScriptBinaryBitOperation {
|
|||
if (left.isInt) {
|
||||
IntConstantValue intConstant = left;
|
||||
BigInt value = intConstant.intValue;
|
||||
BigInt truncatedValue = value & ConstantSystem.only.BITS32;
|
||||
BigInt truncatedValue = value & JavaScriptConstantSystem.only.BITS32;
|
||||
if (value < BigInt.zero) {
|
||||
// Sign-extend if the input was negative. The current semantics don't
|
||||
// make much sense, since we only look at bit 31.
|
||||
|
@ -203,7 +204,7 @@ class JavaScriptShiftRightOperation extends JavaScriptBinaryBitOperation {
|
|||
truncatedValue -= BigInt.two * (truncatedValue & SIGN_BIT);
|
||||
}
|
||||
if (value != truncatedValue) {
|
||||
left = ConstantSystem.only.createInt(truncatedValue);
|
||||
left = JavaScriptConstantSystem.only.createInt(truncatedValue);
|
||||
}
|
||||
}
|
||||
return super.fold(left, right);
|
||||
|
@ -217,7 +218,7 @@ abstract class BinaryBoolOperation implements BinaryOperation {
|
|||
BoolConstantValue leftBool = left;
|
||||
BoolConstantValue rightBool = right;
|
||||
bool resultValue = foldBools(leftBool.boolValue, rightBool.boolValue);
|
||||
return ConstantSystem.only.createBool(resultValue);
|
||||
return JavaScriptConstantSystem.only.createBool(resultValue);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -257,9 +258,9 @@ abstract class ArithmeticNumOperation implements BinaryOperation {
|
|||
if (foldedValue == null) return null;
|
||||
if (left.isInt && right.isInt && !isDivide() || isTruncatingDivide()) {
|
||||
assert(foldedValue is BigInt);
|
||||
return ConstantSystem.only.createInt(foldedValue);
|
||||
return JavaScriptConstantSystem.only.createInt(foldedValue);
|
||||
} else {
|
||||
return ConstantSystem.only.createDouble(foldedValue);
|
||||
return JavaScriptConstantSystem.only.createDouble(foldedValue);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
@ -281,7 +282,7 @@ class JavaScriptBinaryArithmeticOperation implements BinaryOperation {
|
|||
ConstantValue fold(ConstantValue left, ConstantValue right) {
|
||||
ConstantValue result = dartArithmeticOperation.fold(left, right);
|
||||
if (result == null) return result;
|
||||
return ConstantSystem.only.convertToJavaScriptConstant(result);
|
||||
return JavaScriptConstantSystem.only.convertToJavaScriptConstant(result);
|
||||
}
|
||||
|
||||
apply(left, right) => dartArithmeticOperation.apply(left, right);
|
||||
|
@ -364,17 +365,17 @@ class AddOperation implements BinaryOperation {
|
|||
IntConstantValue leftInt = left;
|
||||
IntConstantValue rightInt = right;
|
||||
BigInt result = leftInt.intValue + rightInt.intValue;
|
||||
return ConstantSystem.only.createInt(result);
|
||||
return JavaScriptConstantSystem.only.createInt(result);
|
||||
} else if (left.isNum && right.isNum) {
|
||||
NumConstantValue leftNum = left;
|
||||
NumConstantValue rightNum = right;
|
||||
double result = leftNum.doubleValue + rightNum.doubleValue;
|
||||
return ConstantSystem.only.createDouble(result);
|
||||
return JavaScriptConstantSystem.only.createDouble(result);
|
||||
} else if (left.isString && right.isString) {
|
||||
StringConstantValue leftString = left;
|
||||
StringConstantValue rightString = right;
|
||||
String result = leftString.stringValue + rightString.stringValue;
|
||||
return ConstantSystem.only.createString(result);
|
||||
return JavaScriptConstantSystem.only.createString(result);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
@ -392,7 +393,7 @@ class JavaScriptAddOperation implements BinaryOperation {
|
|||
ConstantValue fold(ConstantValue left, ConstantValue right) {
|
||||
ConstantValue result = _addOperation.fold(left, right);
|
||||
if (result != null && result.isNum) {
|
||||
return ConstantSystem.only.convertToJavaScriptConstant(result);
|
||||
return JavaScriptConstantSystem.only.convertToJavaScriptConstant(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -415,7 +416,7 @@ abstract class RelationalNumOperation implements BinaryOperation {
|
|||
foldedValue = foldNums(leftNum.doubleValue, rightNum.doubleValue);
|
||||
}
|
||||
assert(foldedValue != null);
|
||||
return ConstantSystem.only.createBool(foldedValue);
|
||||
return JavaScriptConstantSystem.only.createBool(foldedValue);
|
||||
}
|
||||
|
||||
bool foldInts(BigInt left, BigInt right);
|
||||
|
@ -464,26 +465,26 @@ class EqualsOperation implements BinaryOperation {
|
|||
IntConstantValue leftInt = left;
|
||||
IntConstantValue rightInt = right;
|
||||
bool result = leftInt.intValue == rightInt.intValue;
|
||||
return ConstantSystem.only.createBool(result);
|
||||
return JavaScriptConstantSystem.only.createBool(result);
|
||||
}
|
||||
|
||||
if (left.isNum && right.isNum) {
|
||||
NumConstantValue leftNum = left;
|
||||
NumConstantValue rightNum = right;
|
||||
bool result = leftNum.doubleValue == rightNum.doubleValue;
|
||||
return ConstantSystem.only.createBool(result);
|
||||
return JavaScriptConstantSystem.only.createBool(result);
|
||||
}
|
||||
|
||||
if (left.isConstructedObject) {
|
||||
if (right.isNull) {
|
||||
return ConstantSystem.only.createBool(false);
|
||||
return JavaScriptConstantSystem.only.createBool(false);
|
||||
}
|
||||
// Unless we know that the user-defined object does not implement the
|
||||
// equality operator we cannot fold here.
|
||||
return null;
|
||||
}
|
||||
|
||||
return ConstantSystem.only.createBool(left == right);
|
||||
return JavaScriptConstantSystem.only.createBool(left == right);
|
||||
}
|
||||
|
||||
apply(left, right) => left == right;
|
||||
|
@ -497,7 +498,7 @@ class IdentityOperation implements BinaryOperation {
|
|||
// constant fold NaN === NaN. Otherwise the output depends on inlined
|
||||
// variables and other optimizations.
|
||||
if (left.isNaN && right.isNaN) return null;
|
||||
return ConstantSystem.only.createBool(left == right);
|
||||
return JavaScriptConstantSystem.only.createBool(left == right);
|
||||
}
|
||||
|
||||
apply(left, right) => identical(left, right);
|
||||
|
@ -561,7 +562,7 @@ class CodeUnitAtRuntimeOperation extends CodeUnitAtOperation {
|
|||
int index = indexConstant.intValue.toInt();
|
||||
if (index < 0 || index >= string.length) return null;
|
||||
int value = string.codeUnitAt(index);
|
||||
return ConstantSystem.only.createIntFromInt(value);
|
||||
return JavaScriptConstantSystem.only.createIntFromInt(value);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -569,7 +570,7 @@ class CodeUnitAtRuntimeOperation extends CodeUnitAtOperation {
|
|||
|
||||
class JavaScriptRoundOperation implements UnaryOperation {
|
||||
const JavaScriptRoundOperation();
|
||||
String get name => ConstantSystem.only.round.name;
|
||||
String get name => JavaScriptConstantSystem.only.round.name;
|
||||
ConstantValue fold(ConstantValue constant) {
|
||||
// Be careful to round() only values that do not throw on either the host or
|
||||
// target platform.
|
||||
|
@ -583,7 +584,7 @@ class JavaScriptRoundOperation implements UnaryOperation {
|
|||
double rounded1 = (value * (1.0 + severalULP)).roundToDouble();
|
||||
double rounded2 = (value * (1.0 - severalULP)).roundToDouble();
|
||||
if (rounded != rounded1 || rounded != rounded2) return null;
|
||||
return ConstantSystem.only.convertToJavaScriptConstant(
|
||||
return JavaScriptConstantSystem.only.convertToJavaScriptConstant(
|
||||
new IntConstantValue(new BigInt.from(value.round())));
|
||||
}
|
||||
|
||||
|
@ -614,192 +615,71 @@ class UnfoldedUnaryOperation implements UnaryOperation {
|
|||
}
|
||||
}
|
||||
|
||||
/// Constant system following the semantics for Dart code that has been
|
||||
/// compiled to JavaScript.
|
||||
class ConstantSystem {
|
||||
final BITS32 = new BigInt.from(0xFFFFFFFF);
|
||||
/// A [ConstantSystem] is responsible for creating constants and folding them.
|
||||
abstract class ConstantSystem {
|
||||
BinaryOperation get add;
|
||||
BinaryOperation get bitAnd;
|
||||
UnaryOperation get bitNot;
|
||||
BinaryOperation get bitOr;
|
||||
BinaryOperation get bitXor;
|
||||
BinaryOperation get booleanAnd;
|
||||
BinaryOperation get booleanOr;
|
||||
BinaryOperation get divide;
|
||||
BinaryOperation get equal;
|
||||
BinaryOperation get greaterEqual;
|
||||
BinaryOperation get greater;
|
||||
BinaryOperation get identity;
|
||||
BinaryOperation get ifNull;
|
||||
BinaryOperation get lessEqual;
|
||||
BinaryOperation get less;
|
||||
BinaryOperation get modulo;
|
||||
BinaryOperation get multiply;
|
||||
UnaryOperation get negate;
|
||||
UnaryOperation get not;
|
||||
BinaryOperation get remainder;
|
||||
BinaryOperation get shiftLeft;
|
||||
BinaryOperation get shiftRight;
|
||||
BinaryOperation get subtract;
|
||||
BinaryOperation get truncatingDivide;
|
||||
|
||||
final add = const JavaScriptAddOperation();
|
||||
final bitAnd = const JavaScriptBinaryBitOperation(const BitAndOperation());
|
||||
final bitNot = const BitNotOperation();
|
||||
final bitOr = const JavaScriptBinaryBitOperation(const BitOrOperation());
|
||||
final bitXor = const JavaScriptBinaryBitOperation(const BitXorOperation());
|
||||
final booleanAnd = const BooleanAndOperation();
|
||||
final booleanOr = const BooleanOrOperation();
|
||||
final divide =
|
||||
const JavaScriptBinaryArithmeticOperation(const DivideOperation());
|
||||
final equal = const EqualsOperation();
|
||||
final greaterEqual = const GreaterEqualOperation();
|
||||
final greater = const GreaterOperation();
|
||||
final identity = const JavaScriptIdentityOperation();
|
||||
final ifNull = const IfNullOperation();
|
||||
final lessEqual = const LessEqualOperation();
|
||||
final less = const LessOperation();
|
||||
final modulo =
|
||||
const JavaScriptBinaryArithmeticOperation(const ModuloOperation());
|
||||
final multiply =
|
||||
const JavaScriptBinaryArithmeticOperation(const MultiplyOperation());
|
||||
final negate = const JavaScriptNegateOperation();
|
||||
final not = const NotOperation();
|
||||
final remainder = const JavaScriptRemainderOperation();
|
||||
final shiftLeft =
|
||||
const JavaScriptBinaryBitOperation(const ShiftLeftOperation());
|
||||
final shiftRight = const JavaScriptShiftRightOperation();
|
||||
final subtract =
|
||||
const JavaScriptBinaryArithmeticOperation(const SubtractOperation());
|
||||
final truncatingDivide = const JavaScriptBinaryArithmeticOperation(
|
||||
const TruncatingDivideOperation());
|
||||
final codeUnitAt = const CodeUnitAtRuntimeOperation();
|
||||
final round = const JavaScriptRoundOperation();
|
||||
final abs = const UnfoldedUnaryOperation('abs');
|
||||
BinaryOperation get codeUnitAt;
|
||||
UnaryOperation get round;
|
||||
UnaryOperation get abs;
|
||||
|
||||
static final ConstantSystem only = new ConstantSystem._internal();
|
||||
const ConstantSystem();
|
||||
|
||||
ConstantSystem._internal();
|
||||
ConstantValue createInt(BigInt i);
|
||||
ConstantValue createIntFromInt(int i) => createInt(new BigInt.from(i));
|
||||
ConstantValue createDouble(double d);
|
||||
ConstantValue createString(String string);
|
||||
ConstantValue createBool(bool value);
|
||||
ConstantValue createNull();
|
||||
ConstantValue createList(InterfaceType type, List<ConstantValue> values);
|
||||
ConstantValue createSet(CommonElements commonElements, InterfaceType type,
|
||||
List<ConstantValue> values);
|
||||
ConstantValue createMap(CommonElements commonElements, InterfaceType type,
|
||||
List<ConstantValue> keys, List<ConstantValue> values);
|
||||
ConstantValue createType(CommonElements commonElements, DartType type);
|
||||
ConstantValue createSymbol(CommonElements commonElements, String text);
|
||||
|
||||
/// Returns true if [value] will turn into NaN or infinity
|
||||
/// at runtime.
|
||||
bool integerBecomesNanOrInfinity(BigInt value) {
|
||||
double doubleValue = value.toDouble();
|
||||
return doubleValue.isNaN || doubleValue.isInfinite;
|
||||
}
|
||||
|
||||
NumConstantValue convertToJavaScriptConstant(NumConstantValue constant) {
|
||||
if (constant.isInt) {
|
||||
IntConstantValue intConstant = constant;
|
||||
BigInt intValue = intConstant.intValue;
|
||||
if (integerBecomesNanOrInfinity(intValue)) {
|
||||
return new DoubleConstantValue(intValue.toDouble());
|
||||
}
|
||||
// If the integer loses precision with JavaScript numbers, use
|
||||
// the floored version JavaScript will use.
|
||||
BigInt floorValue = new BigInt.from(intValue.toDouble());
|
||||
if (floorValue != intValue) {
|
||||
return new IntConstantValue(floorValue);
|
||||
}
|
||||
} else if (constant.isDouble) {
|
||||
DoubleConstantValue doubleResult = constant;
|
||||
double doubleValue = doubleResult.doubleValue;
|
||||
if (!doubleValue.isInfinite &&
|
||||
!doubleValue.isNaN &&
|
||||
!constant.isMinusZero) {
|
||||
double truncated = doubleValue.truncateToDouble();
|
||||
if (truncated == doubleValue) {
|
||||
return new IntConstantValue(new BigInt.from(truncated));
|
||||
}
|
||||
}
|
||||
}
|
||||
return constant;
|
||||
}
|
||||
|
||||
NumConstantValue createInt(BigInt i) =>
|
||||
convertToJavaScriptConstant(new IntConstantValue(i));
|
||||
NumConstantValue createIntFromInt(int i) => createInt(new BigInt.from(i));
|
||||
NumConstantValue createInt32(BigInt i) => new IntConstantValue(i & BITS32);
|
||||
NumConstantValue createDouble(double d) =>
|
||||
convertToJavaScriptConstant(new DoubleConstantValue(d));
|
||||
StringConstantValue createString(String string) =>
|
||||
new StringConstantValue(string);
|
||||
BoolConstantValue createBool(bool value) => new BoolConstantValue(value);
|
||||
NullConstantValue createNull() => new NullConstantValue();
|
||||
ListConstantValue createList(
|
||||
InterfaceType type, List<ConstantValue> values) =>
|
||||
new ListConstantValue(type, values);
|
||||
|
||||
ConstantValue createType(CommonElements commonElements, DartType type) {
|
||||
InterfaceType instanceType = commonElements.typeLiteralType;
|
||||
return new TypeConstantValue(type, instanceType);
|
||||
}
|
||||
// We need to special case the subtype check for JavaScript constant
|
||||
// system because an int is a double at runtime.
|
||||
bool isSubtype(DartTypes types, DartType s, DartType t);
|
||||
|
||||
/// Returns true if the [constant] is an integer at runtime.
|
||||
///
|
||||
/// Integer checks report true for -0.0, INFINITY, and -INFINITY. At
|
||||
/// runtime an 'X is int' check is implemented as:
|
||||
///
|
||||
/// typeof(X) === "number" && Math.floor(X) === X
|
||||
///
|
||||
/// We consistently match that runtime semantics at compile time as well.
|
||||
bool isInt(ConstantValue constant) =>
|
||||
constant.isInt ||
|
||||
constant.isMinusZero ||
|
||||
constant.isPositiveInfinity ||
|
||||
constant.isNegativeInfinity;
|
||||
bool isInt(ConstantValue constant);
|
||||
|
||||
/// Returns true if the [constant] is a double at runtime.
|
||||
bool isDouble(ConstantValue constant) =>
|
||||
constant.isDouble && !constant.isMinusZero;
|
||||
bool isDouble(ConstantValue constant);
|
||||
|
||||
/// Returns true if the [constant] is a string at runtime.
|
||||
bool isString(ConstantValue constant) => constant.isString;
|
||||
bool isString(ConstantValue constant);
|
||||
|
||||
/// Returns true if the [constant] is a boolean at runtime.
|
||||
bool isBool(ConstantValue constant) => constant.isBool;
|
||||
bool isBool(ConstantValue constant);
|
||||
|
||||
/// Returns true if the [constant] is null at runtime.
|
||||
bool isNull(ConstantValue constant) => constant.isNull;
|
||||
|
||||
bool isSubtype(DartTypes types, DartType s, DartType t) {
|
||||
// At runtime, an integer is both an integer and a double: the
|
||||
// integer type check is Math.floor, which will return true only
|
||||
// for real integers, and our double type check is 'typeof number'
|
||||
// which will return true for both integers and doubles.
|
||||
if (s == types.commonElements.intType &&
|
||||
t == types.commonElements.doubleType) {
|
||||
return true;
|
||||
}
|
||||
return types.isSubtype(s, t);
|
||||
}
|
||||
|
||||
SetConstantValue createSet(CommonElements commonElements,
|
||||
InterfaceType sourceType, List<ConstantValue> values) {
|
||||
InterfaceType type = commonElements.getConstantSetTypeFor(sourceType);
|
||||
return new JavaScriptSetConstant(commonElements, type, values);
|
||||
}
|
||||
|
||||
MapConstantValue createMap(
|
||||
CommonElements commonElements,
|
||||
InterfaceType sourceType,
|
||||
List<ConstantValue> keys,
|
||||
List<ConstantValue> values) {
|
||||
bool onlyStringKeys = true;
|
||||
ConstantValue protoValue = null;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
dynamic key = keys[i];
|
||||
if (key.isString) {
|
||||
if (key.stringValue == JavaScriptMapConstant.PROTO_PROPERTY) {
|
||||
protoValue = values[i];
|
||||
}
|
||||
} else {
|
||||
onlyStringKeys = false;
|
||||
// Don't handle __proto__ values specially in the general map case.
|
||||
protoValue = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool hasProtoKey = (protoValue != null);
|
||||
InterfaceType keysType;
|
||||
if (sourceType.treatAsRaw) {
|
||||
keysType = commonElements.listType();
|
||||
} else {
|
||||
keysType = commonElements.listType(sourceType.typeArguments.first);
|
||||
}
|
||||
ListConstantValue keysList = new ListConstantValue(keysType, keys);
|
||||
InterfaceType type = commonElements.getConstantMapTypeFor(sourceType,
|
||||
hasProtoKey: hasProtoKey, onlyStringKeys: onlyStringKeys);
|
||||
return new JavaScriptMapConstant(
|
||||
type, keysList, values, protoValue, onlyStringKeys);
|
||||
}
|
||||
|
||||
ConstantValue createSymbol(CommonElements commonElements, String text) {
|
||||
InterfaceType type = commonElements.symbolImplementationType;
|
||||
FieldEntity field = commonElements.symbolField;
|
||||
ConstantValue argument = createString(text);
|
||||
// TODO(johnniwinther): Use type arguments when all uses no longer expect
|
||||
// a [FieldElement].
|
||||
var fields = <FieldEntity, ConstantValue>{field: argument};
|
||||
return new ConstructedConstantValue(type, fields);
|
||||
}
|
||||
bool isNull(ConstantValue constant);
|
||||
|
||||
UnaryOperation lookupUnary(UnaryOperator operator) {
|
||||
switch (operator.kind) {
|
||||
|
@ -860,12 +740,202 @@ class ConstantSystem {
|
|||
}
|
||||
}
|
||||
|
||||
/// Constant system following the semantics for Dart code that has been
|
||||
/// compiled to JavaScript.
|
||||
class JavaScriptConstantSystem extends ConstantSystem {
|
||||
final BITS32 = new BigInt.from(0xFFFFFFFF);
|
||||
|
||||
final add = const JavaScriptAddOperation();
|
||||
final bitAnd = const JavaScriptBinaryBitOperation(const BitAndOperation());
|
||||
final bitNot = const JavaScriptBitNotOperation();
|
||||
final bitOr = const JavaScriptBinaryBitOperation(const BitOrOperation());
|
||||
final bitXor = const JavaScriptBinaryBitOperation(const BitXorOperation());
|
||||
final booleanAnd = const BooleanAndOperation();
|
||||
final booleanOr = const BooleanOrOperation();
|
||||
final divide =
|
||||
const JavaScriptBinaryArithmeticOperation(const DivideOperation());
|
||||
final equal = const EqualsOperation();
|
||||
final greaterEqual = const GreaterEqualOperation();
|
||||
final greater = const GreaterOperation();
|
||||
final identity = const JavaScriptIdentityOperation();
|
||||
final ifNull = const IfNullOperation();
|
||||
final lessEqual = const LessEqualOperation();
|
||||
final less = const LessOperation();
|
||||
final modulo =
|
||||
const JavaScriptBinaryArithmeticOperation(const ModuloOperation());
|
||||
final multiply =
|
||||
const JavaScriptBinaryArithmeticOperation(const MultiplyOperation());
|
||||
final negate = const JavaScriptNegateOperation();
|
||||
final not = const NotOperation();
|
||||
final remainder = const JavaScriptRemainderOperation();
|
||||
final shiftLeft =
|
||||
const JavaScriptBinaryBitOperation(const ShiftLeftOperation());
|
||||
final shiftRight = const JavaScriptShiftRightOperation();
|
||||
final subtract =
|
||||
const JavaScriptBinaryArithmeticOperation(const SubtractOperation());
|
||||
final truncatingDivide = const JavaScriptBinaryArithmeticOperation(
|
||||
const TruncatingDivideOperation());
|
||||
final codeUnitAt = const CodeUnitAtRuntimeOperation();
|
||||
final round = const JavaScriptRoundOperation();
|
||||
final abs = const UnfoldedUnaryOperation('abs');
|
||||
|
||||
static final JavaScriptConstantSystem only =
|
||||
new JavaScriptConstantSystem._internal();
|
||||
|
||||
JavaScriptConstantSystem._internal();
|
||||
|
||||
/// Returns true if [value] will turn into NaN or infinity
|
||||
/// at runtime.
|
||||
bool integerBecomesNanOrInfinity(BigInt value) {
|
||||
double doubleValue = value.toDouble();
|
||||
return doubleValue.isNaN || doubleValue.isInfinite;
|
||||
}
|
||||
|
||||
NumConstantValue convertToJavaScriptConstant(NumConstantValue constant) {
|
||||
if (constant.isInt) {
|
||||
IntConstantValue intConstant = constant;
|
||||
BigInt intValue = intConstant.intValue;
|
||||
if (integerBecomesNanOrInfinity(intValue)) {
|
||||
return new DoubleConstantValue(intValue.toDouble());
|
||||
}
|
||||
// If the integer loses precision with JavaScript numbers, use
|
||||
// the floored version JavaScript will use.
|
||||
BigInt floorValue = new BigInt.from(intValue.toDouble());
|
||||
if (floorValue != intValue) {
|
||||
return new IntConstantValue(floorValue);
|
||||
}
|
||||
} else if (constant.isDouble) {
|
||||
DoubleConstantValue doubleResult = constant;
|
||||
double doubleValue = doubleResult.doubleValue;
|
||||
if (!doubleValue.isInfinite &&
|
||||
!doubleValue.isNaN &&
|
||||
!constant.isMinusZero) {
|
||||
double truncated = doubleValue.truncateToDouble();
|
||||
if (truncated == doubleValue) {
|
||||
return new IntConstantValue(new BigInt.from(truncated));
|
||||
}
|
||||
}
|
||||
}
|
||||
return constant;
|
||||
}
|
||||
|
||||
@override
|
||||
NumConstantValue createInt(BigInt i) {
|
||||
return convertToJavaScriptConstant(new IntConstantValue(i));
|
||||
}
|
||||
|
||||
NumConstantValue createInt32(BigInt i) => new IntConstantValue(i & BITS32);
|
||||
NumConstantValue createDouble(double d) =>
|
||||
convertToJavaScriptConstant(new DoubleConstantValue(d));
|
||||
StringConstantValue createString(String string) {
|
||||
return new StringConstantValue(string);
|
||||
}
|
||||
|
||||
BoolConstantValue createBool(bool value) => new BoolConstantValue(value);
|
||||
NullConstantValue createNull() => new NullConstantValue();
|
||||
|
||||
@override
|
||||
ListConstantValue createList(InterfaceType type, List<ConstantValue> values) {
|
||||
return new ListConstantValue(type, values);
|
||||
}
|
||||
|
||||
@override
|
||||
ConstantValue createType(CommonElements commonElements, DartType type) {
|
||||
InterfaceType instanceType = commonElements.typeLiteralType;
|
||||
return new TypeConstantValue(type, instanceType);
|
||||
}
|
||||
|
||||
// Integer checks report true for -0.0, INFINITY, and -INFINITY. At
|
||||
// runtime an 'X is int' check is implemented as:
|
||||
//
|
||||
// typeof(X) === "number" && Math.floor(X) === X
|
||||
//
|
||||
// We consistently match that runtime semantics at compile time as well.
|
||||
bool isInt(ConstantValue constant) {
|
||||
return constant.isInt ||
|
||||
constant.isMinusZero ||
|
||||
constant.isPositiveInfinity ||
|
||||
constant.isNegativeInfinity;
|
||||
}
|
||||
|
||||
bool isDouble(ConstantValue constant) =>
|
||||
constant.isDouble && !constant.isMinusZero;
|
||||
bool isString(ConstantValue constant) => constant.isString;
|
||||
bool isBool(ConstantValue constant) => constant.isBool;
|
||||
bool isNull(ConstantValue constant) => constant.isNull;
|
||||
|
||||
bool isSubtype(DartTypes types, DartType s, DartType t) {
|
||||
// At runtime, an integer is both an integer and a double: the
|
||||
// integer type check is Math.floor, which will return true only
|
||||
// for real integers, and our double type check is 'typeof number'
|
||||
// which will return true for both integers and doubles.
|
||||
if (s == types.commonElements.intType &&
|
||||
t == types.commonElements.doubleType) {
|
||||
return true;
|
||||
}
|
||||
return types.isSubtype(s, t);
|
||||
}
|
||||
|
||||
@override
|
||||
SetConstantValue createSet(CommonElements commonElements,
|
||||
InterfaceType sourceType, List<ConstantValue> values) {
|
||||
InterfaceType type = commonElements.getConstantSetTypeFor(sourceType);
|
||||
return new JavaScriptSetConstant(commonElements, type, values);
|
||||
}
|
||||
|
||||
MapConstantValue createMap(
|
||||
CommonElements commonElements,
|
||||
InterfaceType sourceType,
|
||||
List<ConstantValue> keys,
|
||||
List<ConstantValue> values) {
|
||||
bool onlyStringKeys = true;
|
||||
ConstantValue protoValue = null;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
dynamic key = keys[i];
|
||||
if (key.isString) {
|
||||
if (key.stringValue == JavaScriptMapConstant.PROTO_PROPERTY) {
|
||||
protoValue = values[i];
|
||||
}
|
||||
} else {
|
||||
onlyStringKeys = false;
|
||||
// Don't handle __proto__ values specially in the general map case.
|
||||
protoValue = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool hasProtoKey = (protoValue != null);
|
||||
InterfaceType keysType;
|
||||
if (sourceType.treatAsRaw) {
|
||||
keysType = commonElements.listType();
|
||||
} else {
|
||||
keysType = commonElements.listType(sourceType.typeArguments.first);
|
||||
}
|
||||
ListConstantValue keysList = new ListConstantValue(keysType, keys);
|
||||
InterfaceType type = commonElements.getConstantMapTypeFor(sourceType,
|
||||
hasProtoKey: hasProtoKey, onlyStringKeys: onlyStringKeys);
|
||||
return new JavaScriptMapConstant(
|
||||
type, keysList, values, protoValue, onlyStringKeys);
|
||||
}
|
||||
|
||||
@override
|
||||
ConstantValue createSymbol(CommonElements commonElements, String text) {
|
||||
InterfaceType type = commonElements.symbolImplementationType;
|
||||
FieldEntity field = commonElements.symbolField;
|
||||
ConstantValue argument = createString(text);
|
||||
// TODO(johnniwinther): Use type arguments when all uses no longer expect
|
||||
// a [FieldElement].
|
||||
var fields = <FieldEntity, ConstantValue>{field: argument};
|
||||
return new ConstructedConstantValue(type, fields);
|
||||
}
|
||||
}
|
||||
|
||||
class JavaScriptSetConstant extends SetConstantValue {
|
||||
final MapConstantValue entries;
|
||||
|
||||
JavaScriptSetConstant(CommonElements commonElements, InterfaceType type,
|
||||
List<ConstantValue> values)
|
||||
: entries = ConstantSystem.only.createMap(
|
||||
: entries = JavaScriptConstantSystem.only.createMap(
|
||||
commonElements,
|
||||
commonElements.mapType(
|
||||
type.typeArguments.first, commonElements.nullType),
|
||||
|
|
|
@ -24,7 +24,7 @@ class JavaScriptConstantTask extends ConstantCompilerTask {
|
|||
String get name => 'ConstantHandler';
|
||||
|
||||
@override
|
||||
ConstantSystem get constantSystem => ConstantSystem.only;
|
||||
ConstantSystem get constantSystem => JavaScriptConstantSystem.only;
|
||||
}
|
||||
|
||||
/// The [JavaScriptConstantCompiler] is used to keep track of compile-time
|
||||
|
@ -37,7 +37,7 @@ class JavaScriptConstantCompiler implements BackendConstantEnvironment {
|
|||
|
||||
JavaScriptConstantCompiler();
|
||||
|
||||
ConstantSystem get constantSystem => ConstantSystem.only;
|
||||
ConstantSystem get constantSystem => JavaScriptConstantSystem.only;
|
||||
|
||||
@override
|
||||
void registerLazyStatic(FieldEntity element) {
|
||||
|
|
|
@ -2491,7 +2491,7 @@ class JsConstantEnvironment implements ConstantEnvironment {
|
|||
JsConstantEnvironment(this._elementMap, this._environment);
|
||||
|
||||
@override
|
||||
ConstantSystem get constantSystem => ConstantSystem.only;
|
||||
ConstantSystem get constantSystem => JavaScriptConstantSystem.only;
|
||||
|
||||
ConstantValue _getConstantValue(
|
||||
Spannable spannable, ConstantExpression expression,
|
||||
|
|
|
@ -208,7 +208,7 @@ class JsClosedWorld implements JClosedWorld {
|
|||
sink.end(tag);
|
||||
}
|
||||
|
||||
ConstantSystem get constantSystem => ConstantSystem.only;
|
||||
ConstantSystem get constantSystem => JavaScriptConstantSystem.only;
|
||||
|
||||
JElementEnvironment get elementEnvironment => elementMap.elementEnvironment;
|
||||
|
||||
|
|
|
@ -1874,7 +1874,7 @@ class KernelConstantEnvironment implements ConstantEnvironment {
|
|||
KernelConstantEnvironment(this._elementMap, this._environment);
|
||||
|
||||
@override
|
||||
ConstantSystem get constantSystem => ConstantSystem.only;
|
||||
ConstantSystem get constantSystem => JavaScriptConstantSystem.only;
|
||||
|
||||
ConstantValue _getConstantValue(
|
||||
Spannable spannable, ConstantExpression expression,
|
||||
|
|
|
@ -8,7 +8,7 @@ import "package:compiler/src/inferrer/abstract_value_domain.dart";
|
|||
import "package:compiler/src/ssa/nodes.dart";
|
||||
import "package:compiler/src/ssa/value_range_analyzer.dart";
|
||||
|
||||
ValueRangeInfo info = new ValueRangeInfo(ConstantSystem.only);
|
||||
ValueRangeInfo info = new ValueRangeInfo(JavaScriptConstantSystem.only);
|
||||
|
||||
class AbstractValueDomainMock implements AbstractValueDomain {
|
||||
const AbstractValueDomainMock();
|
||||
|
|
|
@ -633,7 +633,7 @@ Future testData(TestData data) async {
|
|||
MemoryEnvironment environment =
|
||||
new MemoryEnvironment(getEnvironment(compiler, field), env);
|
||||
ConstantValue value =
|
||||
constant.evaluate(environment, ConstantSystem.only);
|
||||
constant.evaluate(environment, JavaScriptConstantSystem.only);
|
||||
|
||||
Expect.isNotNull(
|
||||
value,
|
||||
|
|
Loading…
Reference in a new issue