| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 interface ConstantVisitor<R> { | 5 interface ConstantVisitor<R> { |
| 6 R visitSentinel(SentinelConstant constant); | 6 R visitSentinel(SentinelConstant constant); |
| 7 R visitFunction(FunctionConstant constant); | 7 R visitFunction(FunctionConstant constant); |
| 8 R visitNull(NullConstant constant); | 8 R visitNull(NullConstant constant); |
| 9 R visitInt(IntConstant constant); | 9 R visitInt(IntConstant constant); |
| 10 R visitDouble(DoubleConstant constant); | 10 R visitDouble(DoubleConstant constant); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 bool isFunction() => false; | 33 bool isFunction() => false; |
| 34 /** Returns true if the constant is null, a bool, a number or a string. */ | 34 /** Returns true if the constant is null, a bool, a number or a string. */ |
| 35 bool isPrimitive() => false; | 35 bool isPrimitive() => false; |
| 36 /** Returns true if the constant is a list, a map or a constructed object. */ | 36 /** Returns true if the constant is a list, a map or a constructed object. */ |
| 37 bool isObject() => false; | 37 bool isObject() => false; |
| 38 bool isSentinel() => false; | 38 bool isSentinel() => false; |
| 39 | 39 |
| 40 bool isNaN() => false; | 40 bool isNaN() => false; |
| 41 bool isMinusZero() => false; | 41 bool isMinusZero() => false; |
| 42 | 42 |
| 43 abstract DartType computeType(Compiler compiler); |
| 44 |
| 43 abstract List<Constant> getDependencies(); | 45 abstract List<Constant> getDependencies(); |
| 44 | 46 |
| 45 abstract accept(ConstantVisitor); | 47 abstract accept(ConstantVisitor); |
| 46 } | 48 } |
| 47 | 49 |
| 48 class SentinelConstant extends Constant { | 50 class SentinelConstant extends Constant { |
| 49 const SentinelConstant(); | 51 const SentinelConstant(); |
| 50 static final SENTINEL = const SentinelConstant(); | 52 static final SENTINEL = const SentinelConstant(); |
| 51 | 53 |
| 52 List<Constant> getDependencies() => const <Constant>[]; | 54 List<Constant> getDependencies() => const <Constant>[]; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 70 if (other is !FunctionConstant) return false; | 72 if (other is !FunctionConstant) return false; |
| 71 return other.element === element; | 73 return other.element === element; |
| 72 } | 74 } |
| 73 | 75 |
| 74 String toString() => element.toString(); | 76 String toString() => element.toString(); |
| 75 List<Constant> getDependencies() => const <Constant>[]; | 77 List<Constant> getDependencies() => const <Constant>[]; |
| 76 DartString toDartString() { | 78 DartString toDartString() { |
| 77 return new DartString.literal(element.name.slowToString()); | 79 return new DartString.literal(element.name.slowToString()); |
| 78 } | 80 } |
| 79 | 81 |
| 82 DartType computeType(Compiler compiler) { |
| 83 return compiler.functionClass.computeType(compiler); |
| 84 } |
| 85 |
| 80 int hashCode() => (17 * element.hashCode()) & 0x7fffffff; | 86 int hashCode() => (17 * element.hashCode()) & 0x7fffffff; |
| 81 | 87 |
| 82 accept(ConstantVisitor visitor) => visitor.visitFunction(this); | 88 accept(ConstantVisitor visitor) => visitor.visitFunction(this); |
| 83 } | 89 } |
| 84 | 90 |
| 85 class PrimitiveConstant extends Constant { | 91 class PrimitiveConstant extends Constant { |
| 86 abstract get value; | 92 abstract get value; |
| 87 const PrimitiveConstant(); | 93 const PrimitiveConstant(); |
| 88 bool isPrimitive() => true; | 94 bool isPrimitive() => true; |
| 89 | 95 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 102 | 108 |
| 103 class NullConstant extends PrimitiveConstant { | 109 class NullConstant extends PrimitiveConstant { |
| 104 /** The value a Dart null is compiled to in JavaScript. */ | 110 /** The value a Dart null is compiled to in JavaScript. */ |
| 105 static const String JsNull = "null"; | 111 static const String JsNull = "null"; |
| 106 | 112 |
| 107 factory NullConstant() => const NullConstant._internal(); | 113 factory NullConstant() => const NullConstant._internal(); |
| 108 const NullConstant._internal(); | 114 const NullConstant._internal(); |
| 109 bool isNull() => true; | 115 bool isNull() => true; |
| 110 get value => null; | 116 get value => null; |
| 111 | 117 |
| 118 DartType computeType(Compiler compiler) { |
| 119 return compiler.nullClass.computeType(compiler); |
| 120 } |
| 121 |
| 112 void _writeJsCode(CodeBuffer buffer, ConstantHandler handler) { | 122 void _writeJsCode(CodeBuffer buffer, ConstantHandler handler) { |
| 113 buffer.add(JsNull); | 123 buffer.add(JsNull); |
| 114 } | 124 } |
| 115 | 125 |
| 116 // The magic constant has no meaning. It is just a random value. | 126 // The magic constant has no meaning. It is just a random value. |
| 117 int hashCode() => 785965825; | 127 int hashCode() => 785965825; |
| 118 DartString toDartString() => const LiteralDartString("null"); | 128 DartString toDartString() => const LiteralDartString("null"); |
| 119 | 129 |
| 120 accept(ConstantVisitor visitor) => visitor.visitNull(this); | 130 accept(ConstantVisitor visitor) => visitor.visitNull(this); |
| 121 } | 131 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 142 case 9: return const IntConstant._internal(9); | 152 case 9: return const IntConstant._internal(9); |
| 143 case 10: return const IntConstant._internal(10); | 153 case 10: return const IntConstant._internal(10); |
| 144 case -1: return const IntConstant._internal(-1); | 154 case -1: return const IntConstant._internal(-1); |
| 145 case -2: return const IntConstant._internal(-2); | 155 case -2: return const IntConstant._internal(-2); |
| 146 default: return new IntConstant._internal(value); | 156 default: return new IntConstant._internal(value); |
| 147 } | 157 } |
| 148 } | 158 } |
| 149 const IntConstant._internal(this.value); | 159 const IntConstant._internal(this.value); |
| 150 bool isInt() => true; | 160 bool isInt() => true; |
| 151 | 161 |
| 162 DartType computeType(Compiler compiler) { |
| 163 return compiler.intClass.computeType(compiler); |
| 164 } |
| 165 |
| 152 // We have to override the equality operator so that ints and doubles are | 166 // We have to override the equality operator so that ints and doubles are |
| 153 // treated as separate constants. | 167 // treated as separate constants. |
| 154 // The is [:!IntConstant:] check at the beginning of the function makes sure | 168 // The is [:!IntConstant:] check at the beginning of the function makes sure |
| 155 // that we compare only equal to integer constants. | 169 // that we compare only equal to integer constants. |
| 156 bool operator ==(var other) { | 170 bool operator ==(var other) { |
| 157 if (other is !IntConstant) return false; | 171 if (other is !IntConstant) return false; |
| 158 IntConstant otherInt = other; | 172 IntConstant otherInt = other; |
| 159 return value == otherInt.value; | 173 return value == otherInt.value; |
| 160 } | 174 } |
| 161 | 175 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 181 } else { | 195 } else { |
| 182 return new DoubleConstant._internal(value); | 196 return new DoubleConstant._internal(value); |
| 183 } | 197 } |
| 184 } | 198 } |
| 185 const DoubleConstant._internal(this.value); | 199 const DoubleConstant._internal(this.value); |
| 186 bool isDouble() => true; | 200 bool isDouble() => true; |
| 187 bool isNaN() => value.isNaN(); | 201 bool isNaN() => value.isNaN(); |
| 188 // We need to check for the negative sign since -0.0 == 0.0. | 202 // We need to check for the negative sign since -0.0 == 0.0. |
| 189 bool isMinusZero() => value == 0.0 && value.isNegative(); | 203 bool isMinusZero() => value == 0.0 && value.isNegative(); |
| 190 | 204 |
| 205 DartType computeType(Compiler compiler) { |
| 206 return compiler.doubleClass.computeType(compiler); |
| 207 } |
| 208 |
| 191 bool operator ==(var other) { | 209 bool operator ==(var other) { |
| 192 if (other is !DoubleConstant) return false; | 210 if (other is !DoubleConstant) return false; |
| 193 DoubleConstant otherDouble = other; | 211 DoubleConstant otherDouble = other; |
| 194 double otherValue = otherDouble.value; | 212 double otherValue = otherDouble.value; |
| 195 if (value == 0.0 && otherValue == 0.0) { | 213 if (value == 0.0 && otherValue == 0.0) { |
| 196 return value.isNegative() == otherValue.isNegative(); | 214 return value.isNegative() == otherValue.isNegative(); |
| 197 } else if (value.isNaN()) { | 215 } else if (value.isNaN()) { |
| 198 return otherValue.isNaN(); | 216 return otherValue.isNaN(); |
| 199 } else { | 217 } else { |
| 200 return value == otherValue; | 218 return value == otherValue; |
| 201 } | 219 } |
| 202 } | 220 } |
| 203 | 221 |
| 204 int hashCode() => value.hashCode(); | 222 int hashCode() => value.hashCode(); |
| 205 DartString toDartString() => new DartString.literal(value.toString()); | 223 DartString toDartString() => new DartString.literal(value.toString()); |
| 206 | 224 |
| 207 accept(ConstantVisitor visitor) => visitor.visitDouble(this); | 225 accept(ConstantVisitor visitor) => visitor.visitDouble(this); |
| 208 } | 226 } |
| 209 | 227 |
| 210 class BoolConstant extends PrimitiveConstant { | 228 class BoolConstant extends PrimitiveConstant { |
| 211 factory BoolConstant(value) { | 229 factory BoolConstant(value) { |
| 212 return value ? new TrueConstant() : new FalseConstant(); | 230 return value ? new TrueConstant() : new FalseConstant(); |
| 213 } | 231 } |
| 214 const BoolConstant._internal(); | 232 const BoolConstant._internal(); |
| 215 bool isBool() => true; | 233 bool isBool() => true; |
| 216 | 234 |
| 235 DartType computeType(Compiler compiler) { |
| 236 return compiler.boolClass.computeType(compiler); |
| 237 } |
| 238 |
| 217 abstract BoolConstant negate(); | 239 abstract BoolConstant negate(); |
| 218 } | 240 } |
| 219 | 241 |
| 220 class TrueConstant extends BoolConstant { | 242 class TrueConstant extends BoolConstant { |
| 221 final bool value = true; | 243 final bool value = true; |
| 222 | 244 |
| 223 factory TrueConstant() => const TrueConstant._internal(); | 245 factory TrueConstant() => const TrueConstant._internal(); |
| 224 const TrueConstant._internal() : super._internal(); | 246 const TrueConstant._internal() : super._internal(); |
| 225 bool isTrue() => true; | 247 bool isTrue() => true; |
| 226 | 248 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 final Node node; | 281 final Node node; |
| 260 | 282 |
| 261 StringConstant(this.value, this.node) { | 283 StringConstant(this.value, this.node) { |
| 262 // TODO(floitsch): cache StringConstants. | 284 // TODO(floitsch): cache StringConstants. |
| 263 // TODO(floitsch): compute hashcode without calling toString() on the | 285 // TODO(floitsch): compute hashcode without calling toString() on the |
| 264 // DartString. | 286 // DartString. |
| 265 _hashCode = value.slowToString().hashCode(); | 287 _hashCode = value.slowToString().hashCode(); |
| 266 } | 288 } |
| 267 bool isString() => true; | 289 bool isString() => true; |
| 268 | 290 |
| 291 DartType computeType(Compiler compiler) { |
| 292 return compiler.stringClass.computeType(compiler); |
| 293 } |
| 294 |
| 269 bool operator ==(var other) { | 295 bool operator ==(var other) { |
| 270 if (other is !StringConstant) return false; | 296 if (other is !StringConstant) return false; |
| 271 StringConstant otherString = other; | 297 StringConstant otherString = other; |
| 272 return (_hashCode == otherString._hashCode) && (value == otherString.value); | 298 return (_hashCode == otherString._hashCode) && (value == otherString.value); |
| 273 } | 299 } |
| 274 | 300 |
| 275 int hashCode() => _hashCode; | 301 int hashCode() => _hashCode; |
| 276 DartString toDartString() => value; | 302 DartString toDartString() => value; |
| 277 int get length => value.length; | 303 int get length => value.length; |
| 278 | 304 |
| 279 accept(ConstantVisitor visitor) => visitor.visitString(this); | 305 accept(ConstantVisitor visitor) => visitor.visitString(this); |
| 280 } | 306 } |
| 281 | 307 |
| 282 class ObjectConstant extends Constant { | 308 class ObjectConstant extends Constant { |
| 283 final DartType type; | 309 final DartType type; |
| 284 | 310 |
| 285 ObjectConstant(this.type); | 311 ObjectConstant(this.type); |
| 286 bool isObject() => true; | 312 bool isObject() => true; |
| 287 | 313 |
| 314 DartType computeType(Compiler compiler) => type; |
| 315 |
| 288 // TODO(1603): The class should be marked as abstract, but the VM doesn't | 316 // TODO(1603): The class should be marked as abstract, but the VM doesn't |
| 289 // currently allow this. | 317 // currently allow this. |
| 290 abstract int hashCode(); | 318 abstract int hashCode(); |
| 291 } | 319 } |
| 292 | 320 |
| 293 class ListConstant extends ObjectConstant { | 321 class ListConstant extends ObjectConstant { |
| 294 final List<Constant> entries; | 322 final List<Constant> entries; |
| 295 int _hashCode; | 323 int _hashCode; |
| 296 | 324 |
| 297 ListConstant(DartType type, this.entries) : super(type) { | 325 ListConstant(DartType type, this.entries) : super(type) { |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 if (fields[i] != other.fields[i]) return false; | 434 if (fields[i] != other.fields[i]) return false; |
| 407 } | 435 } |
| 408 return true; | 436 return true; |
| 409 } | 437 } |
| 410 | 438 |
| 411 int hashCode() => _hashCode; | 439 int hashCode() => _hashCode; |
| 412 List<Constant> getDependencies() => fields; | 440 List<Constant> getDependencies() => fields; |
| 413 | 441 |
| 414 accept(ConstantVisitor visitor) => visitor.visitConstructed(this); | 442 accept(ConstantVisitor visitor) => visitor.visitConstructed(this); |
| 415 } | 443 } |
| OLD | NEW |