Chromium Code Reviews| 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 class Constant implements Hashable { | 5 class Constant implements Hashable { |
| 6 const Constant(); | 6 const Constant(); |
| 7 | 7 |
| 8 bool isNull() => false; | 8 bool isNull() => false; |
| 9 bool isBool() => false; | 9 bool isBool() => false; |
| 10 bool isTrue() => false; | 10 bool isTrue() => false; |
| 11 bool isFalse() => false; | 11 bool isFalse() => false; |
| 12 bool isInt() => false; | 12 bool isInt() => false; |
| 13 bool isDouble() => false; | 13 bool isDouble() => false; |
| 14 bool isNum() => false; | 14 bool isNum() => false; |
| 15 bool isString() => false; | 15 bool isString() => false; |
| 16 bool isList() => false; | 16 bool isList() => false; |
| 17 bool isMap() => false; | 17 bool isMap() => false; |
| 18 bool isConstructedObject() => false; | 18 bool isConstructedObject() => false; |
| 19 bool isFunction() => false; | 19 bool isFunction() => false; |
| 20 /** Returns true if the constant is null, a bool, a number or a string. */ | 20 /** Returns true if the constant is null, a bool, a number or a string. */ |
| 21 bool isPrimitive() => false; | 21 bool isPrimitive() => false; |
| 22 /** Returns true if the constant is a list, a map or a constructed object. */ | 22 /** Returns true if the constant is a list, a map or a constructed object. */ |
| 23 bool isObject() => false; | 23 bool isObject() => false; |
| 24 | 24 |
| 25 bool isNaN() => false; | 25 bool isNaN() => false; |
| 26 | 26 |
| 27 abstract DartType computeType(ConstantHandler handler); | |
|
ngeoffray
2012/09/06 09:28:55
Please take a Compiler here instead of the Constan
Lasse Reichstein Nielsen
2012/09/06 12:03:36
Good point. I was trying to not make Constant depe
Lasse Reichstein Nielsen
2012/09/11 12:20:23
Done by someone else. Yey.
| |
| 28 | |
| 27 abstract void _writeJsCode(CodeBuffer buffer, ConstantHandler handler); | 29 abstract void _writeJsCode(CodeBuffer buffer, ConstantHandler handler); |
| 28 /** | 30 /** |
| 29 * Unless the constant can be emitted multiple times (as for numbers and | 31 * Unless the constant can be emitted multiple times (as for numbers and |
| 30 * strings) adds its canonical name to the buffer. | 32 * strings) adds its canonical name to the buffer. |
| 31 */ | 33 */ |
| 32 abstract void _writeCanonicalizedJsCode(CodeBuffer buffer, | 34 abstract void _writeCanonicalizedJsCode(CodeBuffer buffer, |
| 33 ConstantHandler handler); | 35 ConstantHandler handler); |
| 34 abstract List<Constant> getDependencies(); | 36 abstract List<Constant> getDependencies(); |
| 35 } | 37 } |
| 36 | 38 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 } | 89 } |
| 88 | 90 |
| 89 class NullConstant extends PrimitiveConstant { | 91 class NullConstant extends PrimitiveConstant { |
| 90 /** The value a Dart null is compiled to in JavaScript. */ | 92 /** The value a Dart null is compiled to in JavaScript. */ |
| 91 static const String JsNull = "null"; | 93 static const String JsNull = "null"; |
| 92 | 94 |
| 93 factory NullConstant() => const NullConstant._internal(); | 95 factory NullConstant() => const NullConstant._internal(); |
| 94 const NullConstant._internal(); | 96 const NullConstant._internal(); |
| 95 bool isNull() => true; | 97 bool isNull() => true; |
| 96 get value => null; | 98 get value => null; |
| 99 DartType computeType(ConstantHandler handler) { | |
| 100 return handler.compiler.nullClass.computeType(handler.compiler); | |
| 101 } | |
| 97 | 102 |
| 98 void _writeJsCode(CodeBuffer buffer, ConstantHandler handler) { | 103 void _writeJsCode(CodeBuffer buffer, ConstantHandler handler) { |
| 99 buffer.add(JsNull); | 104 buffer.add(JsNull); |
| 100 } | 105 } |
| 101 | 106 |
| 102 // The magic constant has no meaning. It is just a random value. | 107 // The magic constant has no meaning. It is just a random value. |
| 103 int hashCode() => 785965825; | 108 int hashCode() => 785965825; |
| 104 DartString toDartString() => const LiteralDartString("null"); | 109 DartString toDartString() => const LiteralDartString("null"); |
| 105 } | 110 } |
| 106 | 111 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 126 case 9: return const IntConstant._internal(9); | 131 case 9: return const IntConstant._internal(9); |
| 127 case 10: return const IntConstant._internal(10); | 132 case 10: return const IntConstant._internal(10); |
| 128 case -1: return const IntConstant._internal(-1); | 133 case -1: return const IntConstant._internal(-1); |
| 129 case -2: return const IntConstant._internal(-2); | 134 case -2: return const IntConstant._internal(-2); |
| 130 default: return new IntConstant._internal(value); | 135 default: return new IntConstant._internal(value); |
| 131 } | 136 } |
| 132 } | 137 } |
| 133 const IntConstant._internal(this.value); | 138 const IntConstant._internal(this.value); |
| 134 bool isInt() => true; | 139 bool isInt() => true; |
| 135 | 140 |
| 141 DartType computeType(ConstantHandler handler) { | |
| 142 return handler.compiler.intClass.computeType(handler.compiler); | |
| 143 } | |
| 144 | |
| 136 void _writeJsCode(CodeBuffer buffer, ConstantHandler handler) { | 145 void _writeJsCode(CodeBuffer buffer, ConstantHandler handler) { |
| 137 buffer.add("$value"); | 146 buffer.add("$value"); |
| 138 } | 147 } |
| 139 | 148 |
| 140 // We have to override the equality operator so that ints and doubles are | 149 // We have to override the equality operator so that ints and doubles are |
| 141 // treated as separate constants. | 150 // treated as separate constants. |
| 142 // The is [:!IntConstant:] check at the beginning of the function makes sure | 151 // The is [:!IntConstant:] check at the beginning of the function makes sure |
| 143 // that we compare only equal to integer constants. | 152 // that we compare only equal to integer constants. |
| 144 bool operator ==(var other) { | 153 bool operator ==(var other) { |
| 145 if (other is !IntConstant) return false; | 154 if (other is !IntConstant) return false; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 165 } else if (value == 1.0) { | 174 } else if (value == 1.0) { |
| 166 return const DoubleConstant._internal(1.0); | 175 return const DoubleConstant._internal(1.0); |
| 167 } else { | 176 } else { |
| 168 return new DoubleConstant._internal(value); | 177 return new DoubleConstant._internal(value); |
| 169 } | 178 } |
| 170 } | 179 } |
| 171 const DoubleConstant._internal(this.value); | 180 const DoubleConstant._internal(this.value); |
| 172 bool isDouble() => true; | 181 bool isDouble() => true; |
| 173 bool isNaN() => value.isNaN(); | 182 bool isNaN() => value.isNaN(); |
| 174 | 183 |
| 184 DartType computeType(ConstantHandler handler) { | |
| 185 return handler.compiler.doubleClass.computeType(handler.compiler); | |
| 186 } | |
| 187 | |
| 188 | |
| 175 void _writeJsCode(CodeBuffer buffer, ConstantHandler handler) { | 189 void _writeJsCode(CodeBuffer buffer, ConstantHandler handler) { |
| 176 if (value.isNaN()) { | 190 if (value.isNaN()) { |
| 177 buffer.add("(0/0)"); | 191 buffer.add("(0/0)"); |
| 178 } else if (value == double.INFINITY) { | 192 } else if (value == double.INFINITY) { |
| 179 buffer.add("(1/0)"); | 193 buffer.add("(1/0)"); |
| 180 } else if (value == -double.INFINITY) { | 194 } else if (value == -double.INFINITY) { |
| 181 buffer.add("(-1/0)"); | 195 buffer.add("(-1/0)"); |
| 182 } else { | 196 } else { |
| 183 buffer.add("$value"); | 197 buffer.add("$value"); |
| 184 } | 198 } |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 201 DartString toDartString() => new DartString.literal(value.toString()); | 215 DartString toDartString() => new DartString.literal(value.toString()); |
| 202 } | 216 } |
| 203 | 217 |
| 204 class BoolConstant extends PrimitiveConstant { | 218 class BoolConstant extends PrimitiveConstant { |
| 205 factory BoolConstant(value) { | 219 factory BoolConstant(value) { |
| 206 return value ? new TrueConstant() : new FalseConstant(); | 220 return value ? new TrueConstant() : new FalseConstant(); |
| 207 } | 221 } |
| 208 const BoolConstant._internal(); | 222 const BoolConstant._internal(); |
| 209 bool isBool() => true; | 223 bool isBool() => true; |
| 210 | 224 |
| 225 DartType computeType(ConstantHandler handler) { | |
| 226 return handler.compiler.boolClass.computeType(handler.compiler); | |
| 227 } | |
| 228 | |
| 211 BoolConstant unaryFold(String op) { | 229 BoolConstant unaryFold(String op) { |
| 212 if (op == "!") return new BoolConstant(!value); | 230 if (op == "!") return new BoolConstant(!value); |
| 213 return null; | 231 return null; |
| 214 } | 232 } |
| 215 | 233 |
| 216 abstract BoolConstant negate(); | 234 abstract BoolConstant negate(); |
| 217 } | 235 } |
| 218 | 236 |
| 219 class TrueConstant extends BoolConstant { | 237 class TrueConstant extends BoolConstant { |
| 220 final bool value = true; | 238 final bool value = true; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 final Node node; | 280 final Node node; |
| 263 | 281 |
| 264 StringConstant(this.value, this.node) { | 282 StringConstant(this.value, this.node) { |
| 265 // TODO(floitsch): cache StringConstants. | 283 // TODO(floitsch): cache StringConstants. |
| 266 // TODO(floitsch): compute hashcode without calling toString() on the | 284 // TODO(floitsch): compute hashcode without calling toString() on the |
| 267 // DartString. | 285 // DartString. |
| 268 _hashCode = value.slowToString().hashCode(); | 286 _hashCode = value.slowToString().hashCode(); |
| 269 } | 287 } |
| 270 bool isString() => true; | 288 bool isString() => true; |
| 271 | 289 |
| 290 DartType computeType(ConstantHandler handler) { | |
| 291 return handler.compiler.stringClass.computeType(handler.compiler); | |
| 292 } | |
| 293 | |
| 272 void _writeJsCode(CodeBuffer buffer, ConstantHandler handler) { | 294 void _writeJsCode(CodeBuffer buffer, ConstantHandler handler) { |
| 273 buffer.add("'"); | 295 buffer.add("'"); |
| 274 ConstantHandler.writeEscapedString(value, buffer, (reason) { | 296 ConstantHandler.writeEscapedString(value, buffer, (reason) { |
| 275 handler.compiler.reportError(node, reason); | 297 handler.compiler.reportError(node, reason); |
| 276 }); | 298 }); |
| 277 buffer.add("'"); | 299 buffer.add("'"); |
| 278 } | 300 } |
| 279 | 301 |
| 280 bool operator ==(var other) { | 302 bool operator ==(var other) { |
| 281 if (other is !StringConstant) return false; | 303 if (other is !StringConstant) return false; |
| 282 StringConstant otherString = other; | 304 StringConstant otherString = other; |
| 283 return (_hashCode == otherString._hashCode) && (value == otherString.value); | 305 return (_hashCode == otherString._hashCode) && (value == otherString.value); |
| 284 } | 306 } |
| 285 | 307 |
| 286 int hashCode() => _hashCode; | 308 int hashCode() => _hashCode; |
| 287 DartString toDartString() => value; | 309 DartString toDartString() => value; |
| 288 int get length => value.length; | 310 int get length => value.length; |
| 289 } | 311 } |
| 290 | 312 |
| 291 class ObjectConstant extends Constant { | 313 class ObjectConstant extends Constant { |
| 292 final DartType type; | 314 final DartType type; |
| 293 | 315 |
| 294 ObjectConstant(this.type); | 316 ObjectConstant(this.type); |
| 295 bool isObject() => true; | 317 bool isObject() => true; |
| 296 | 318 |
| 297 // TODO(1603): The class should be marked as abstract, but the VM doesn't | |
| 298 // currently allow this. | |
| 299 abstract int hashCode(); | 319 abstract int hashCode(); |
| 300 | 320 |
| 321 DartType computeType(ConstantHandler handler) => type; | |
| 322 | |
| 301 void _writeCanonicalizedJsCode(CodeBuffer buffer, ConstantHandler handler) { | 323 void _writeCanonicalizedJsCode(CodeBuffer buffer, ConstantHandler handler) { |
| 302 String name = handler.getNameForConstant(this); | 324 String name = handler.getNameForConstant(this); |
| 303 buffer.add(handler.compiler.namer.isolatePropertiesAccessForConstant(name)); | 325 buffer.add(handler.compiler.namer.isolatePropertiesAccessForConstant(name)); |
| 304 } | 326 } |
| 305 } | 327 } |
| 306 | 328 |
| 307 class ListConstant extends ObjectConstant { | 329 class ListConstant extends ObjectConstant { |
| 308 final List<Constant> entries; | 330 final List<Constant> entries; |
| 309 int _hashCode; | 331 int _hashCode; |
| 310 | 332 |
| (...skipping 899 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1210 Constant fieldValue = fieldValues[field]; | 1232 Constant fieldValue = fieldValues[field]; |
| 1211 if (fieldValue === null) { | 1233 if (fieldValue === null) { |
| 1212 // Use the default value. | 1234 // Use the default value. |
| 1213 fieldValue = compiler.compileVariable(field); | 1235 fieldValue = compiler.compileVariable(field); |
| 1214 } | 1236 } |
| 1215 jsNewArguments.add(fieldValue); | 1237 jsNewArguments.add(fieldValue); |
| 1216 }); | 1238 }); |
| 1217 return jsNewArguments; | 1239 return jsNewArguments; |
| 1218 } | 1240 } |
| 1219 } | 1241 } |
| OLD | NEW |