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 part of dart2js; | 5 part of dart2js; |
| 6 | 6 |
| 7 abstract class ConstantVisitor<R> { | 7 abstract class ConstantVisitor<R> { |
| 8 const ConstantVisitor(); | 8 const ConstantVisitor(); |
| 9 | 9 |
| 10 R visitFunction(FunctionConstant constant); | 10 R visitFunction(FunctionConstant constant); |
| 11 R visitNull(NullConstant constant); | 11 R visitNull(NullConstant constant); |
| 12 R visitInt(IntConstant constant); | 12 R visitInt(IntConstant constant); |
| 13 R visitDouble(DoubleConstant constant); | 13 R visitDouble(DoubleConstant constant); |
| 14 R visitTrue(TrueConstant constant); | 14 R visitTrue(TrueConstant constant); |
| 15 R visitFalse(FalseConstant constant); | 15 R visitFalse(FalseConstant constant); |
| 16 R visitString(StringConstant constant); | 16 R visitString(StringConstant constant); |
| 17 R visitList(ListConstant constant); | 17 R visitList(ListConstant constant); |
| 18 R visitMap(MapConstant constant); | 18 R visitMap(MapConstant constant); |
| 19 R visitConstructed(ConstructedConstant constant); | 19 R visitConstructed(ConstructedConstant constant); |
| 20 R visitType(TypeConstant constant); | 20 R visitType(TypeConstant constant); |
| 21 R visitInterceptor(InterceptorConstant constant); | 21 R visitInterceptor(InterceptorConstant constant); |
| 22 R visitDummy(DummyConstant constant); | 22 R visitDummy(DummyConstant constant); |
| 23 R visitDeferred(DeferredConstant constant); | 23 R visitDeferred(DeferredConstant constant); |
| 24 } | 24 } |
| 25 | 25 |
| 26 // TODO(johnniwinther): Rename this to `ConstantValue`. | |
| 26 abstract class Constant { | 27 abstract class Constant { |
| 27 const Constant(); | 28 const Constant(); |
| 28 | 29 |
| 29 bool get isNull => false; | 30 bool get isNull => false; |
| 30 bool get isBool => false; | 31 bool get isBool => false; |
| 31 bool get isTrue => false; | 32 bool get isTrue => false; |
| 32 bool get isFalse => false; | 33 bool get isFalse => false; |
| 33 bool get isInt => false; | 34 bool get isInt => false; |
| 34 bool get isDouble => false; | 35 bool get isDouble => false; |
| 35 bool get isNum => false; | 36 bool get isNum => false; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 52 bool get isOne => false; | 53 bool get isOne => false; |
| 53 | 54 |
| 54 // TODO(johnniwinther): Replace with a 'type' getter. | 55 // TODO(johnniwinther): Replace with a 'type' getter. |
| 55 DartType computeType(Compiler compiler); | 56 DartType computeType(Compiler compiler); |
| 56 | 57 |
| 57 ti.TypeMask computeMask(Compiler compiler); | 58 ti.TypeMask computeMask(Compiler compiler); |
| 58 | 59 |
| 59 List<Constant> getDependencies(); | 60 List<Constant> getDependencies(); |
| 60 | 61 |
| 61 accept(ConstantVisitor visitor); | 62 accept(ConstantVisitor visitor); |
| 63 | |
| 64 /// This value in Dart syntax, if possible. | |
|
karlklose
2014/09/25 08:43:25
'This value' -> 'the value of this constant'?
Johnni Winther
2014/09/29 08:24:44
Done.
| |
| 65 /// | |
| 66 /// For [ConstructedConstant]s there is no way to create a valid const | |
| 67 /// expression from the value so the unparse of these is best effort. | |
|
karlklose
2014/09/25 08:43:25
Why is there no way? Don't we have the constructor
Johnni Winther
2014/09/29 08:24:44
Constant doesn't store the constructor only the cl
| |
| 68 /// | |
| 69 /// For the synthetic constants, [DeferredConstant], [DummyConstant], | |
| 70 /// [InterceptorConstant] the unparse is descriptive only. | |
| 71 String unparse(); | |
| 72 | |
| 73 /// Returns a structured representation of this constant suited for debugging. | |
| 74 String toStructuredString(); | |
| 75 | |
| 76 String toString() { | |
| 77 assertDebugMode("Use Constant.unparse() or Constant.toStructuredString() " | |
|
karlklose
2014/09/25 08:43:25
A comment should be enough, I don't think we have
Johnni Winther
2014/09/29 08:24:44
The misuse of toString for semantic purpose hides
| |
| 78 "instead of Constant.toString()."); | |
| 79 return toStructuredString(); | |
| 80 } | |
| 62 } | 81 } |
| 63 | 82 |
| 64 class FunctionConstant extends Constant { | 83 class FunctionConstant extends Constant { |
| 65 Element element; | 84 Element element; |
| 66 | 85 |
| 67 FunctionConstant(this.element); | 86 FunctionConstant(this.element); |
| 68 | 87 |
| 69 bool get isFunction => true; | 88 bool get isFunction => true; |
| 70 | 89 |
| 71 bool operator ==(var other) { | 90 bool operator ==(var other) { |
| 72 if (other is !FunctionConstant) return false; | 91 if (other is !FunctionConstant) return false; |
| 73 return identical(other.element, element); | 92 return identical(other.element, element); |
| 74 } | 93 } |
| 75 | 94 |
| 76 String toString() => element.toString(); | |
| 77 List<Constant> getDependencies() => const <Constant>[]; | 95 List<Constant> getDependencies() => const <Constant>[]; |
| 96 | |
| 78 DartString toDartString() { | 97 DartString toDartString() { |
| 79 return new DartString.literal(element.name); | 98 return new DartString.literal(element.name); |
| 80 } | 99 } |
| 81 | 100 |
| 82 // TODO(johnniwinther): remove computeType. | 101 // TODO(johnniwinther): remove computeType. |
| 83 DartType computeType(Compiler compiler) => element.computeType(compiler); | 102 DartType computeType(Compiler compiler) => element.computeType(compiler); |
| 84 | 103 |
| 85 ti.TypeMask computeMask(Compiler compiler) { | 104 ti.TypeMask computeMask(Compiler compiler) { |
| 86 return compiler.typesTask.functionType; | 105 return compiler.typesTask.functionType; |
| 87 } | 106 } |
| 88 | 107 |
| 89 int get hashCode => (17 * element.hashCode) & 0x7fffffff; | 108 int get hashCode => (17 * element.hashCode) & 0x7fffffff; |
| 90 | 109 |
| 91 accept(ConstantVisitor visitor) => visitor.visitFunction(this); | 110 accept(ConstantVisitor visitor) => visitor.visitFunction(this); |
| 111 | |
| 112 String unparse() { | |
| 113 if (element.isStatic) { | |
| 114 return '${element.enclosingClass.name}.${element.name}'; | |
| 115 } else { | |
| 116 return '${element.name}'; | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 String toStructuredString() { | |
| 121 return 'FunctionConstant(${unparse()})'; | |
| 122 } | |
| 92 } | 123 } |
| 93 | 124 |
| 94 abstract class PrimitiveConstant extends Constant { | 125 abstract class PrimitiveConstant extends Constant { |
| 126 // TODO(johnniwinther): Rename to `primitiveValue`. | |
| 95 get value; | 127 get value; |
| 128 | |
| 96 const PrimitiveConstant(); | 129 const PrimitiveConstant(); |
| 130 | |
| 97 bool get isPrimitive => true; | 131 bool get isPrimitive => true; |
| 98 | 132 |
| 99 bool operator ==(var other) { | 133 bool operator ==(var other) { |
| 100 if (other is !PrimitiveConstant) return false; | 134 if (other is !PrimitiveConstant) return false; |
| 101 PrimitiveConstant otherPrimitive = other; | 135 PrimitiveConstant otherPrimitive = other; |
| 102 // We use == instead of 'identical' so that DartStrings compare correctly. | 136 // We use == instead of 'identical' so that DartStrings compare correctly. |
| 103 return value == otherPrimitive.value; | 137 return value == otherPrimitive.value; |
| 104 } | 138 } |
| 105 | 139 |
| 106 int get hashCode => throw new UnsupportedError('PrimitiveConstant.hashCode'); | 140 int get hashCode => throw new UnsupportedError('PrimitiveConstant.hashCode'); |
| 107 | 141 |
| 108 String toString() => value.toString(); | |
| 109 // Primitive constants don't have dependencies. | 142 // Primitive constants don't have dependencies. |
| 110 List<Constant> getDependencies() => const <Constant>[]; | 143 List<Constant> getDependencies() => const <Constant>[]; |
| 144 | |
| 111 DartString toDartString(); | 145 DartString toDartString(); |
| 146 | |
| 147 /// This value in Dart syntax. | |
| 148 String unparse() => value.toString(); | |
| 112 } | 149 } |
| 113 | 150 |
| 114 class NullConstant extends PrimitiveConstant { | 151 class NullConstant extends PrimitiveConstant { |
| 115 /** The value a Dart null is compiled to in JavaScript. */ | 152 /** The value a Dart null is compiled to in JavaScript. */ |
| 116 static const String JsNull = "null"; | 153 static const String JsNull = "null"; |
| 117 | 154 |
| 118 factory NullConstant() => const NullConstant._internal(); | 155 factory NullConstant() => const NullConstant._internal(); |
| 156 | |
| 119 const NullConstant._internal(); | 157 const NullConstant._internal(); |
| 158 | |
| 120 bool get isNull => true; | 159 bool get isNull => true; |
| 160 | |
| 121 get value => null; | 161 get value => null; |
| 122 | 162 |
| 123 DartType computeType(Compiler compiler) { | 163 DartType computeType(Compiler compiler) { |
| 124 return compiler.nullClass.computeType(compiler); | 164 return compiler.nullClass.computeType(compiler); |
| 125 } | 165 } |
| 126 | 166 |
| 127 ti.TypeMask computeMask(Compiler compiler) { | 167 ti.TypeMask computeMask(Compiler compiler) { |
| 128 return compiler.typesTask.nullType; | 168 return compiler.typesTask.nullType; |
| 129 } | 169 } |
| 130 | 170 |
| 131 // The magic constant has no meaning. It is just a random value. | 171 // The magic constant has no meaning. It is just a random value. |
| 132 int get hashCode => 785965825; | 172 int get hashCode => 785965825; |
| 173 | |
| 133 DartString toDartString() => const LiteralDartString("null"); | 174 DartString toDartString() => const LiteralDartString("null"); |
| 134 | 175 |
| 135 accept(ConstantVisitor visitor) => visitor.visitNull(this); | 176 accept(ConstantVisitor visitor) => visitor.visitNull(this); |
| 177 | |
| 178 String toStructuredString() => 'NullConstant'; | |
| 136 } | 179 } |
| 137 | 180 |
| 138 abstract class NumConstant extends PrimitiveConstant { | 181 abstract class NumConstant extends PrimitiveConstant { |
| 182 const NumConstant(); | |
| 183 | |
| 139 num get value; | 184 num get value; |
| 140 const NumConstant(); | 185 |
| 141 bool get isNum => true; | 186 bool get isNum => true; |
| 142 } | 187 } |
| 143 | 188 |
| 144 class IntConstant extends NumConstant { | 189 class IntConstant extends NumConstant { |
| 145 final int value; | 190 final int value; |
| 191 | |
| 146 factory IntConstant(int value) { | 192 factory IntConstant(int value) { |
| 147 switch (value) { | 193 switch (value) { |
| 148 case 0: return const IntConstant._internal(0); | 194 case 0: return const IntConstant._internal(0); |
| 149 case 1: return const IntConstant._internal(1); | 195 case 1: return const IntConstant._internal(1); |
| 150 case 2: return const IntConstant._internal(2); | 196 case 2: return const IntConstant._internal(2); |
| 151 case 3: return const IntConstant._internal(3); | 197 case 3: return const IntConstant._internal(3); |
| 152 case 4: return const IntConstant._internal(4); | 198 case 4: return const IntConstant._internal(4); |
| 153 case 5: return const IntConstant._internal(5); | 199 case 5: return const IntConstant._internal(5); |
| 154 case 6: return const IntConstant._internal(6); | 200 case 6: return const IntConstant._internal(6); |
| 155 case 7: return const IntConstant._internal(7); | 201 case 7: return const IntConstant._internal(7); |
| 156 case 8: return const IntConstant._internal(8); | 202 case 8: return const IntConstant._internal(8); |
| 157 case 9: return const IntConstant._internal(9); | 203 case 9: return const IntConstant._internal(9); |
| 158 case 10: return const IntConstant._internal(10); | 204 case 10: return const IntConstant._internal(10); |
| 159 case -1: return const IntConstant._internal(-1); | 205 case -1: return const IntConstant._internal(-1); |
| 160 case -2: return const IntConstant._internal(-2); | 206 case -2: return const IntConstant._internal(-2); |
| 161 default: return new IntConstant._internal(value); | 207 default: return new IntConstant._internal(value); |
| 162 } | 208 } |
| 163 } | 209 } |
| 210 | |
| 164 const IntConstant._internal(this.value); | 211 const IntConstant._internal(this.value); |
| 212 | |
| 165 bool get isInt => true; | 213 bool get isInt => true; |
| 214 | |
| 166 bool isUInt31() => value >= 0 && value < (1 << 31); | 215 bool isUInt31() => value >= 0 && value < (1 << 31); |
| 216 | |
| 167 bool isUInt32() => value >= 0 && value < (1 << 32); | 217 bool isUInt32() => value >= 0 && value < (1 << 32); |
| 218 | |
| 168 bool isPositive() => value >= 0; | 219 bool isPositive() => value >= 0; |
| 220 | |
| 169 bool get isZero => value == 0; | 221 bool get isZero => value == 0; |
| 222 | |
| 170 bool get isOne => value == 1; | 223 bool get isOne => value == 1; |
| 171 | 224 |
| 172 DartType computeType(Compiler compiler) { | 225 DartType computeType(Compiler compiler) { |
| 173 return compiler.intClass.rawType; | 226 return compiler.intClass.rawType; |
| 174 } | 227 } |
| 175 | 228 |
| 176 ti.TypeMask computeMask(Compiler compiler) { | 229 ti.TypeMask computeMask(Compiler compiler) { |
| 177 if (isUInt31()) return compiler.typesTask.uint31Type; | 230 if (isUInt31()) return compiler.typesTask.uint31Type; |
| 178 if (isUInt32()) return compiler.typesTask.uint32Type; | 231 if (isUInt32()) return compiler.typesTask.uint32Type; |
| 179 if (isPositive()) return compiler.typesTask.positiveIntType; | 232 if (isPositive()) return compiler.typesTask.positiveIntType; |
| 180 return compiler.typesTask.intType; | 233 return compiler.typesTask.intType; |
| 181 } | 234 } |
| 182 | 235 |
| 183 // We have to override the equality operator so that ints and doubles are | 236 // We have to override the equality operator so that ints and doubles are |
| 184 // treated as separate constants. | 237 // treated as separate constants. |
| 185 // The is [:!IntConstant:] check at the beginning of the function makes sure | 238 // The is [:!IntConstant:] check at the beginning of the function makes sure |
| 186 // that we compare only equal to integer constants. | 239 // that we compare only equal to integer constants. |
| 187 bool operator ==(var other) { | 240 bool operator ==(var other) { |
| 188 if (other is !IntConstant) return false; | 241 if (other is !IntConstant) return false; |
| 189 IntConstant otherInt = other; | 242 IntConstant otherInt = other; |
| 190 return value == otherInt.value; | 243 return value == otherInt.value; |
| 191 } | 244 } |
| 192 | 245 |
| 193 int get hashCode => value & SMI_MASK; | 246 int get hashCode => value & SMI_MASK; |
| 247 | |
| 194 DartString toDartString() => new DartString.literal(value.toString()); | 248 DartString toDartString() => new DartString.literal(value.toString()); |
| 195 | 249 |
| 196 accept(ConstantVisitor visitor) => visitor.visitInt(this); | 250 accept(ConstantVisitor visitor) => visitor.visitInt(this); |
| 251 | |
| 252 String toStructuredString() => 'IntConstant(${unparse()})'; | |
| 197 } | 253 } |
| 198 | 254 |
| 199 class DoubleConstant extends NumConstant { | 255 class DoubleConstant extends NumConstant { |
| 200 final double value; | 256 final double value; |
| 257 | |
| 201 factory DoubleConstant(double value) { | 258 factory DoubleConstant(double value) { |
| 202 if (value.isNaN) { | 259 if (value.isNaN) { |
| 203 return const DoubleConstant._internal(double.NAN); | 260 return const DoubleConstant._internal(double.NAN); |
| 204 } else if (value == double.INFINITY) { | 261 } else if (value == double.INFINITY) { |
| 205 return const DoubleConstant._internal(double.INFINITY); | 262 return const DoubleConstant._internal(double.INFINITY); |
| 206 } else if (value == -double.INFINITY) { | 263 } else if (value == -double.INFINITY) { |
| 207 return const DoubleConstant._internal(-double.INFINITY); | 264 return const DoubleConstant._internal(-double.INFINITY); |
| 208 } else if (value == 0.0 && !value.isNegative) { | 265 } else if (value == 0.0 && !value.isNegative) { |
| 209 return const DoubleConstant._internal(0.0); | 266 return const DoubleConstant._internal(0.0); |
| 210 } else if (value == 1.0) { | 267 } else if (value == 1.0) { |
| 211 return const DoubleConstant._internal(1.0); | 268 return const DoubleConstant._internal(1.0); |
| 212 } else { | 269 } else { |
| 213 return new DoubleConstant._internal(value); | 270 return new DoubleConstant._internal(value); |
| 214 } | 271 } |
| 215 } | 272 } |
| 273 | |
| 216 const DoubleConstant._internal(this.value); | 274 const DoubleConstant._internal(this.value); |
| 275 | |
| 217 bool get isDouble => true; | 276 bool get isDouble => true; |
| 277 | |
| 218 bool get isNaN => value.isNaN; | 278 bool get isNaN => value.isNaN; |
| 279 | |
| 219 // We need to check for the negative sign since -0.0 == 0.0. | 280 // We need to check for the negative sign since -0.0 == 0.0. |
| 220 bool get isMinusZero => value == 0.0 && value.isNegative; | 281 bool get isMinusZero => value == 0.0 && value.isNegative; |
| 282 | |
| 221 bool get isZero => value == 0.0; | 283 bool get isZero => value == 0.0; |
| 284 | |
| 222 bool get isOne => value == 1.0; | 285 bool get isOne => value == 1.0; |
| 223 | 286 |
| 224 DartType computeType(Compiler compiler) { | 287 DartType computeType(Compiler compiler) { |
| 225 return compiler.doubleClass.rawType; | 288 return compiler.doubleClass.rawType; |
| 226 } | 289 } |
| 227 | 290 |
| 228 ti.TypeMask computeMask(Compiler compiler) { | 291 ti.TypeMask computeMask(Compiler compiler) { |
| 229 // We have to distinguish -0.0 from 0, but for all practical purposes | 292 // We have to distinguish -0.0 from 0, but for all practical purposes |
| 230 // -0.0 is an integer. | 293 // -0.0 is an integer. |
| 231 // TODO(17235): this kind of special casing should only happen in the | 294 // TODO(17235): this kind of special casing should only happen in the |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 244 if (value == 0.0 && otherValue == 0.0) { | 307 if (value == 0.0 && otherValue == 0.0) { |
| 245 return value.isNegative == otherValue.isNegative; | 308 return value.isNegative == otherValue.isNegative; |
| 246 } else if (value.isNaN) { | 309 } else if (value.isNaN) { |
| 247 return otherValue.isNaN; | 310 return otherValue.isNaN; |
| 248 } else { | 311 } else { |
| 249 return value == otherValue; | 312 return value == otherValue; |
| 250 } | 313 } |
| 251 } | 314 } |
| 252 | 315 |
| 253 int get hashCode => value.hashCode; | 316 int get hashCode => value.hashCode; |
| 317 | |
| 254 DartString toDartString() => new DartString.literal(value.toString()); | 318 DartString toDartString() => new DartString.literal(value.toString()); |
| 255 | 319 |
| 256 accept(ConstantVisitor visitor) => visitor.visitDouble(this); | 320 accept(ConstantVisitor visitor) => visitor.visitDouble(this); |
| 321 | |
| 322 String toStructuredString() => 'DoubleConstant(${unparse()})'; | |
| 257 } | 323 } |
| 258 | 324 |
| 259 abstract class BoolConstant extends PrimitiveConstant { | 325 abstract class BoolConstant extends PrimitiveConstant { |
| 260 factory BoolConstant(value) { | 326 factory BoolConstant(value) { |
| 261 return value ? new TrueConstant() : new FalseConstant(); | 327 return value ? new TrueConstant() : new FalseConstant(); |
| 262 } | 328 } |
| 329 | |
| 263 const BoolConstant._internal(); | 330 const BoolConstant._internal(); |
| 331 | |
| 264 bool get isBool => true; | 332 bool get isBool => true; |
| 265 | 333 |
| 266 DartType computeType(Compiler compiler) { | 334 DartType computeType(Compiler compiler) { |
| 267 return compiler.boolClass.rawType; | 335 return compiler.boolClass.rawType; |
| 268 } | 336 } |
| 269 | 337 |
| 270 ti.TypeMask computeMask(Compiler compiler) { | 338 ti.TypeMask computeMask(Compiler compiler) { |
| 271 return compiler.typesTask.boolType; | 339 return compiler.typesTask.boolType; |
| 272 } | 340 } |
| 273 | 341 |
| 274 BoolConstant negate(); | 342 BoolConstant negate(); |
| 343 | |
| 344 String toStructuredString() => 'BoolConstant(${unparse()})'; | |
| 275 } | 345 } |
| 276 | 346 |
| 277 class TrueConstant extends BoolConstant { | 347 class TrueConstant extends BoolConstant { |
| 278 final bool value = true; | 348 factory TrueConstant() => const TrueConstant._internal(); |
| 279 | 349 |
| 280 factory TrueConstant() => const TrueConstant._internal(); | |
| 281 const TrueConstant._internal() : super._internal(); | 350 const TrueConstant._internal() : super._internal(); |
| 351 | |
| 282 bool get isTrue => true; | 352 bool get isTrue => true; |
| 283 | 353 |
| 354 bool get value => true; | |
| 355 | |
| 284 FalseConstant negate() => new FalseConstant(); | 356 FalseConstant negate() => new FalseConstant(); |
| 285 | 357 |
| 286 bool operator ==(var other) => identical(this, other); | 358 bool operator ==(var other) => identical(this, other); |
| 359 | |
| 287 // The magic constant is just a random value. It does not have any | 360 // The magic constant is just a random value. It does not have any |
| 288 // significance. | 361 // significance. |
| 289 int get hashCode => 499; | 362 int get hashCode => 499; |
| 363 | |
| 290 DartString toDartString() => const LiteralDartString("true"); | 364 DartString toDartString() => const LiteralDartString("true"); |
| 291 | 365 |
| 292 accept(ConstantVisitor visitor) => visitor.visitTrue(this); | 366 accept(ConstantVisitor visitor) => visitor.visitTrue(this); |
| 293 } | 367 } |
| 294 | 368 |
| 295 class FalseConstant extends BoolConstant { | 369 class FalseConstant extends BoolConstant { |
| 296 final bool value = false; | 370 factory FalseConstant() => const FalseConstant._internal(); |
| 297 | 371 |
| 298 factory FalseConstant() => const FalseConstant._internal(); | |
| 299 const FalseConstant._internal() : super._internal(); | 372 const FalseConstant._internal() : super._internal(); |
| 373 | |
| 300 bool get isFalse => true; | 374 bool get isFalse => true; |
| 301 | 375 |
| 376 bool get value => false; | |
| 377 | |
| 302 TrueConstant negate() => new TrueConstant(); | 378 TrueConstant negate() => new TrueConstant(); |
| 303 | 379 |
| 304 bool operator ==(var other) => identical(this, other); | 380 bool operator ==(var other) => identical(this, other); |
| 381 | |
| 305 // The magic constant is just a random value. It does not have any | 382 // The magic constant is just a random value. It does not have any |
| 306 // significance. | 383 // significance. |
| 307 int get hashCode => 536555975; | 384 int get hashCode => 536555975; |
| 385 | |
| 308 DartString toDartString() => const LiteralDartString("false"); | 386 DartString toDartString() => const LiteralDartString("false"); |
| 309 | 387 |
| 310 accept(ConstantVisitor visitor) => visitor.visitFalse(this); | 388 accept(ConstantVisitor visitor) => visitor.visitFalse(this); |
| 311 } | 389 } |
| 312 | 390 |
| 313 class StringConstant extends PrimitiveConstant { | 391 class StringConstant extends PrimitiveConstant { |
| 314 final DartString value; | 392 final DartString value; |
| 393 | |
| 315 final int hashCode; | 394 final int hashCode; |
| 316 | 395 |
| 317 // TODO(floitsch): cache StringConstants. | 396 // TODO(floitsch): cache StringConstants. |
| 318 // TODO(floitsch): compute hashcode without calling toString() on the | 397 // TODO(floitsch): compute hashcode without calling toString() on the |
| 319 // DartString. | 398 // DartString. |
| 320 StringConstant(DartString value) | 399 StringConstant(DartString value) |
| 321 : this.value = value, | 400 : this.value = value, |
| 322 this.hashCode = value.slowToString().hashCode; | 401 this.hashCode = value.slowToString().hashCode; |
| 402 | |
| 323 bool get isString => true; | 403 bool get isString => true; |
| 324 | 404 |
| 325 DartType computeType(Compiler compiler) { | 405 DartType computeType(Compiler compiler) { |
| 326 return compiler.stringClass.rawType; | 406 return compiler.stringClass.rawType; |
| 327 } | 407 } |
| 328 | 408 |
| 329 ti.TypeMask computeMask(Compiler compiler) { | 409 ti.TypeMask computeMask(Compiler compiler) { |
| 330 return compiler.typesTask.stringType; | 410 return compiler.typesTask.stringType; |
| 331 } | 411 } |
| 332 | 412 |
| 333 bool operator ==(var other) { | 413 bool operator ==(var other) { |
| 334 if (other is !StringConstant) return false; | 414 if (other is !StringConstant) return false; |
| 335 StringConstant otherString = other; | 415 StringConstant otherString = other; |
| 336 return (hashCode == otherString.hashCode) && (value == otherString.value); | 416 return (hashCode == otherString.hashCode) && (value == otherString.value); |
| 337 } | 417 } |
| 338 | 418 |
| 339 DartString toDartString() => value; | 419 DartString toDartString() => value; |
| 420 | |
| 340 int get length => value.length; | 421 int get length => value.length; |
| 341 | 422 |
| 342 accept(ConstantVisitor visitor) => visitor.visitString(this); | 423 accept(ConstantVisitor visitor) => visitor.visitString(this); |
| 343 | 424 |
| 344 String toString() { | 425 // TODO(johnniwinther): Ensure correct escaping. |
| 345 return 'StringConstant("${value.slowToString()}")'; | 426 String unparse() => '"${value.slowToString()}"'; |
| 346 } | 427 |
| 428 String toStructuredString() => 'StringConstant(${unparse()})'; | |
| 347 } | 429 } |
| 348 | 430 |
| 349 abstract class ObjectConstant extends Constant { | 431 abstract class ObjectConstant extends Constant { |
| 350 final DartType type; | 432 final InterfaceType type; |
| 351 | 433 |
| 352 ObjectConstant(this.type); | 434 ObjectConstant(this.type); |
| 353 | 435 |
| 354 bool get isObject => true; | 436 bool get isObject => true; |
| 355 | 437 |
| 356 DartType computeType(Compiler compiler) => type; | 438 DartType computeType(Compiler compiler) => type; |
| 439 | |
| 440 void _unparseTypeArguments(StringBuffer sb) { | |
| 441 if (!type.treatAsRaw) { | |
| 442 sb.write('<'); | |
| 443 sb.write(type.typeArguments.join(', ')); | |
| 444 sb.write('>'); | |
| 445 } | |
| 446 } | |
| 357 } | 447 } |
| 358 | 448 |
| 359 class TypeConstant extends ObjectConstant { | 449 class TypeConstant extends ObjectConstant { |
| 360 /// The user type that this constant represents. | 450 /// The user type that this constant represents. |
| 361 final DartType representedType; | 451 final DartType representedType; |
| 362 | 452 |
| 363 TypeConstant(this.representedType, type) : super(type); | 453 TypeConstant(this.representedType, InterfaceType type) : super(type); |
| 364 | 454 |
| 365 bool get isType => true; | 455 bool get isType => true; |
| 366 | 456 |
| 367 bool operator ==(other) { | 457 bool operator ==(other) { |
| 368 return other is TypeConstant && representedType == other.representedType; | 458 return other is TypeConstant && representedType == other.representedType; |
| 369 } | 459 } |
| 370 | 460 |
| 371 ti.TypeMask computeMask(Compiler compiler) { | 461 ti.TypeMask computeMask(Compiler compiler) { |
| 372 return compiler.typesTask.typeType; | 462 return compiler.typesTask.typeType; |
| 373 } | 463 } |
| 374 | 464 |
| 375 int get hashCode => representedType.hashCode * 13; | 465 int get hashCode => representedType.hashCode * 13; |
| 376 | 466 |
| 377 List<Constant> getDependencies() => const <Constant>[]; | 467 List<Constant> getDependencies() => const <Constant>[]; |
| 378 | 468 |
| 379 accept(ConstantVisitor visitor) => visitor.visitType(this); | 469 accept(ConstantVisitor visitor) => visitor.visitType(this); |
| 380 | 470 |
| 381 String toString() => 'TypeConstant(${representedType})'; | 471 String unparse() => '$representedType'; |
| 472 | |
| 473 String toStructuredString() => 'TypeConstant(${representedType})'; | |
| 382 } | 474 } |
| 383 | 475 |
| 384 class ListConstant extends ObjectConstant { | 476 class ListConstant extends ObjectConstant { |
| 385 final List<Constant> entries; | 477 final List<Constant> entries; |
| 386 final int hashCode; | 478 final int hashCode; |
| 387 | 479 |
| 388 ListConstant(DartType type, List<Constant> entries) | 480 ListConstant(InterfaceType type, List<Constant> entries) |
| 389 : this.entries = entries, | 481 : this.entries = entries, |
| 390 hashCode = _computeHash(type, entries), | 482 hashCode = _computeHash(type, entries), |
| 391 super(type); | 483 super(type); |
| 484 | |
| 392 bool get isList => true; | 485 bool get isList => true; |
| 393 | 486 |
| 394 static int _computeHash(DartType type, List<Constant> entries) { | 487 static int _computeHash(DartType type, List<Constant> entries) { |
| 395 // TODO(floitsch): create a better hash. | 488 // TODO(floitsch): create a better hash. |
| 396 int hash = 7; | 489 int hash = 7; |
| 397 for (Constant input in entries) { | 490 for (Constant input in entries) { |
| 398 hash ^= input.hashCode; | 491 hash ^= input.hashCode; |
| 399 } | 492 } |
| 400 hash ^= type.hashCode; | 493 hash ^= type.hashCode; |
| 401 return hash; | 494 return hash; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 416 List<Constant> getDependencies() => entries; | 509 List<Constant> getDependencies() => entries; |
| 417 | 510 |
| 418 int get length => entries.length; | 511 int get length => entries.length; |
| 419 | 512 |
| 420 ti.TypeMask computeMask(Compiler compiler) { | 513 ti.TypeMask computeMask(Compiler compiler) { |
| 421 return compiler.typesTask.constListType; | 514 return compiler.typesTask.constListType; |
| 422 } | 515 } |
| 423 | 516 |
| 424 accept(ConstantVisitor visitor) => visitor.visitList(this); | 517 accept(ConstantVisitor visitor) => visitor.visitList(this); |
| 425 | 518 |
| 426 String toString() { | 519 String unparse() { |
| 520 StringBuffer sb = new StringBuffer(); | |
| 521 _unparseTypeArguments(sb); | |
| 522 sb.write('['); | |
| 523 for (int i = 0 ; i < length ; i++) { | |
| 524 if (i > 0) sb.write(','); | |
| 525 sb.write(entries[i].unparse()); | |
| 526 } | |
| 527 sb.write(']'); | |
| 528 return sb.toString(); | |
| 529 } | |
| 530 | |
| 531 String toStructuredString() { | |
| 427 StringBuffer sb = new StringBuffer(); | 532 StringBuffer sb = new StringBuffer(); |
| 428 sb.write('ListConstant(['); | 533 sb.write('ListConstant(['); |
| 429 for (int i = 0 ; i < entries.length ; i++) { | 534 for (int i = 0 ; i < length ; i++) { |
| 430 if (i > 0) sb.write(','); | 535 if (i > 0) sb.write(','); |
| 431 sb.write(entries[i]); | 536 sb.write(entries[i].toStructuredString()); |
| 432 } | 537 } |
| 433 sb.write('])'); | 538 sb.write('])'); |
| 434 return sb.toString(); | 539 return sb.toString(); |
| 435 } | 540 } |
| 436 } | 541 } |
| 437 | 542 |
| 438 class MapConstant extends ObjectConstant { | 543 class MapConstant extends ObjectConstant { |
| 439 final List<Constant> keys; | 544 final List<Constant> keys; |
| 440 final List<Constant> values; | 545 final List<Constant> values; |
| 441 final int hashCode; | 546 final int hashCode; |
| 442 | 547 |
| 443 MapConstant(DartType type, List<Constant> keys, List<Constant> values) | 548 MapConstant(InterfaceType type, List<Constant> keys, List<Constant> values) |
| 444 : this.keys = keys, | 549 : this.keys = keys, |
| 445 this.values = values, | 550 this.values = values, |
| 446 this.hashCode = computeHash(type, keys, values), | 551 this.hashCode = computeHash(type, keys, values), |
| 447 super(type) { | 552 super(type) { |
| 448 assert(keys.length == values.length); | 553 assert(keys.length == values.length); |
| 449 } | 554 } |
| 450 | 555 |
| 451 bool get isMap => true; | 556 bool get isMap => true; |
| 452 | 557 |
| 453 static int computeHash(DartType type, | 558 static int computeHash(DartType type, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 486 List<Constant> result = <Constant>[]; | 591 List<Constant> result = <Constant>[]; |
| 487 result.addAll(keys); | 592 result.addAll(keys); |
| 488 result.addAll(values); | 593 result.addAll(values); |
| 489 return result; | 594 return result; |
| 490 } | 595 } |
| 491 | 596 |
| 492 int get length => keys.length; | 597 int get length => keys.length; |
| 493 | 598 |
| 494 accept(ConstantVisitor visitor) => visitor.visitMap(this); | 599 accept(ConstantVisitor visitor) => visitor.visitMap(this); |
| 495 | 600 |
| 496 String toString() { | 601 String unparse() { |
| 602 StringBuffer sb = new StringBuffer(); | |
| 603 _unparseTypeArguments(sb); | |
| 604 sb.write('{'); | |
| 605 for (int i = 0 ; i < length ; i++) { | |
| 606 if (i > 0) sb.write(','); | |
| 607 sb.write(keys[i].unparse()); | |
| 608 sb.write(':'); | |
| 609 sb.write(values[i].unparse()); | |
| 610 } | |
| 611 sb.write('}'); | |
| 612 return sb.toString(); | |
| 613 } | |
| 614 | |
| 615 String toStructuredString() { | |
| 497 StringBuffer sb = new StringBuffer(); | 616 StringBuffer sb = new StringBuffer(); |
| 498 sb.write('MapConstant({'); | 617 sb.write('MapConstant({'); |
| 499 for (int i = 0; i < length; i++) { | 618 for (int i = 0; i < length; i++) { |
| 500 if (i > 0) sb.write(','); | 619 if (i > 0) sb.write(','); |
| 501 sb.write(keys[i]); | 620 sb.write(keys[i].toStructuredString()); |
| 502 sb.write(':'); | 621 sb.write(':'); |
| 503 sb.write(values[i]); | 622 sb.write(values[i].toStructuredString()); |
| 504 } | 623 } |
| 505 sb.write('})'); | 624 sb.write('})'); |
| 506 return sb.toString(); | 625 return sb.toString(); |
| 507 } | 626 } |
| 508 } | 627 } |
| 509 | 628 |
| 510 class InterceptorConstant extends Constant { | 629 class InterceptorConstant extends Constant { |
| 511 /// The type for which this interceptor holds the methods. The constant | 630 /// The type for which this interceptor holds the methods. The constant |
| 512 /// is a dispatch table for this type. | 631 /// is a dispatch table for this type. |
| 513 final DartType dispatchedType; | 632 final DartType dispatchedType; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 526 List<Constant> getDependencies() => const <Constant>[]; | 645 List<Constant> getDependencies() => const <Constant>[]; |
| 527 | 646 |
| 528 accept(ConstantVisitor visitor) => visitor.visitInterceptor(this); | 647 accept(ConstantVisitor visitor) => visitor.visitInterceptor(this); |
| 529 | 648 |
| 530 DartType computeType(Compiler compiler) => const DynamicType(); | 649 DartType computeType(Compiler compiler) => const DynamicType(); |
| 531 | 650 |
| 532 ti.TypeMask computeMask(Compiler compiler) { | 651 ti.TypeMask computeMask(Compiler compiler) { |
| 533 return compiler.typesTask.nonNullType; | 652 return compiler.typesTask.nonNullType; |
| 534 } | 653 } |
| 535 | 654 |
| 536 String toString() { | 655 String unparse() { |
| 656 return 'interceptor($dispatchedType)'; | |
| 657 } | |
| 658 | |
| 659 String toStructuredString() { | |
| 537 return 'InterceptorConstant(${dispatchedType.getStringAsDeclared("o")})'; | 660 return 'InterceptorConstant(${dispatchedType.getStringAsDeclared("o")})'; |
| 538 } | 661 } |
| 539 } | 662 } |
| 540 | 663 |
| 541 class DummyConstant extends Constant { | 664 class DummyConstant extends Constant { |
| 542 final ti.TypeMask typeMask; | 665 final ti.TypeMask typeMask; |
| 543 | 666 |
| 544 DummyConstant(this.typeMask); | 667 DummyConstant(this.typeMask); |
| 545 | 668 |
| 546 bool get isDummy => true; | 669 bool get isDummy => true; |
| 547 | 670 |
| 548 bool operator ==(other) { | 671 bool operator ==(other) { |
| 549 return other is DummyConstant | 672 return other is DummyConstant |
| 550 && typeMask == other.typeMask; | 673 && typeMask == other.typeMask; |
| 551 } | 674 } |
| 552 | 675 |
| 553 get hashCode => typeMask.hashCode; | 676 get hashCode => typeMask.hashCode; |
| 554 | 677 |
| 555 List<Constant> getDependencies() => const <Constant>[]; | 678 List<Constant> getDependencies() => const <Constant>[]; |
| 556 | 679 |
| 557 accept(ConstantVisitor visitor) => visitor.visitDummy(this); | 680 accept(ConstantVisitor visitor) => visitor.visitDummy(this); |
| 558 | 681 |
| 559 DartType computeType(Compiler compiler) => const DynamicType(); | 682 DartType computeType(Compiler compiler) => const DynamicType(); |
| 560 | 683 |
| 561 ti.TypeMask computeMask(Compiler compiler) => typeMask; | 684 ti.TypeMask computeMask(Compiler compiler) => typeMask; |
| 562 | 685 |
| 563 String toString() { | 686 String unparse() => 'dummy($typeMask)'; |
| 564 return 'DummyConstant($typeMask)'; | 687 |
| 565 } | 688 String toStructuredString() => 'DummyConstant($typeMask)'; |
| 566 } | 689 } |
| 567 | 690 |
| 568 class ConstructedConstant extends ObjectConstant { | 691 class ConstructedConstant extends ObjectConstant { |
| 569 final List<Constant> fields; | 692 final List<Constant> fields; |
| 570 final int hashCode; | 693 final int hashCode; |
| 571 | 694 |
| 572 ConstructedConstant(DartType type, List<Constant> fields, | 695 ConstructedConstant(InterfaceType type, List<Constant> fields) |
| 573 {this.isLiteralSymbol: false}) | |
| 574 : this.fields = fields, | 696 : this.fields = fields, |
| 575 hashCode = computeHash(type, fields), | 697 hashCode = computeHash(type, fields), |
| 576 super(type) { | 698 super(type) { |
| 577 assert(type != null); | 699 assert(type != null); |
| 578 } | 700 } |
| 701 | |
| 579 bool get isConstructedObject => true; | 702 bool get isConstructedObject => true; |
| 580 | 703 |
| 581 /// True if this constant is constructed as a literal symbol. | |
| 582 final bool isLiteralSymbol; | |
| 583 | |
| 584 static int computeHash(DartType type, List<Constant> fields) { | 704 static int computeHash(DartType type, List<Constant> fields) { |
| 585 // TODO(floitsch): create a better hash. | 705 // TODO(floitsch): create a better hash. |
| 586 int hash = 0; | 706 int hash = 0; |
| 587 for (Constant field in fields) { | 707 for (Constant field in fields) { |
| 588 hash ^= field.hashCode; | 708 hash ^= field.hashCode; |
| 589 } | 709 } |
| 590 hash ^= type.hashCode; | 710 hash ^= type.hashCode; |
| 591 return hash; | 711 return hash; |
| 592 } | 712 } |
| 593 | 713 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 618 // TODO(ahe): Refactor constant system to store this information directly. | 738 // TODO(ahe): Refactor constant system to store this information directly. |
| 619 ClassElement classElement = type.element; | 739 ClassElement classElement = type.element; |
| 620 int count = 0; | 740 int count = 0; |
| 621 Map<Element, Constant> result = new Map<Element, Constant>(); | 741 Map<Element, Constant> result = new Map<Element, Constant>(); |
| 622 classElement.implementation.forEachInstanceField((holder, field) { | 742 classElement.implementation.forEachInstanceField((holder, field) { |
| 623 result[field] = fields[count++]; | 743 result[field] = fields[count++]; |
| 624 }, includeSuperAndInjectedMembers: true); | 744 }, includeSuperAndInjectedMembers: true); |
| 625 return result; | 745 return result; |
| 626 } | 746 } |
| 627 | 747 |
| 628 String toString() { | 748 String unparse() { |
| 749 StringBuffer sb = new StringBuffer(); | |
| 750 sb.write(type.name); | |
| 751 _unparseTypeArguments(sb); | |
| 752 sb.write('('); | |
| 753 int i = 0; | |
| 754 fieldElements.forEach((Element field, Constant value) { | |
| 755 if (i > 0) sb.write(','); | |
| 756 sb.write(field.name); | |
| 757 sb.write('='); | |
| 758 sb.write(value.unparse()); | |
| 759 i++; | |
| 760 }); | |
| 761 sb.write(')'); | |
| 762 return sb.toString(); | |
| 763 } | |
| 764 | |
| 765 String toStructuredString() { | |
| 629 StringBuffer sb = new StringBuffer(); | 766 StringBuffer sb = new StringBuffer(); |
| 630 sb.write('ConstructedConstant('); | 767 sb.write('ConstructedConstant('); |
| 631 sb.write(type); | 768 sb.write(type); |
| 632 sb.write('('); | 769 sb.write('('); |
| 633 int i = 0; | 770 int i = 0; |
| 634 fieldElements.forEach((Element field, Constant value) { | 771 fieldElements.forEach((Element field, Constant value) { |
| 635 if (i > 0) sb.write(','); | 772 if (i > 0) sb.write(','); |
| 636 sb.write(field.name); | 773 sb.write(field.name); |
| 637 sb.write('='); | 774 sb.write('='); |
| 638 sb.write(value); | 775 sb.write(value.toStructuredString()); |
| 639 i++; | 776 i++; |
| 640 }); | 777 }); |
| 641 sb.write('))'); | 778 sb.write('))'); |
| 642 return sb.toString(); | 779 return sb.toString(); |
| 643 } | 780 } |
| 644 } | 781 } |
| 645 | 782 |
| 646 /// A reference to a constant in another output unit. | 783 /// A reference to a constant in another output unit. |
| 647 /// Used for referring to deferred constants. | 784 /// Used for referring to deferred constants. |
| 648 class DeferredConstant extends Constant { | 785 class DeferredConstant extends Constant { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 664 List<Constant> getDependencies() => <Constant>[referenced]; | 801 List<Constant> getDependencies() => <Constant>[referenced]; |
| 665 | 802 |
| 666 accept(ConstantVisitor visitor) => visitor.visitDeferred(this); | 803 accept(ConstantVisitor visitor) => visitor.visitDeferred(this); |
| 667 | 804 |
| 668 DartType computeType(Compiler compiler) => referenced.computeType(compiler); | 805 DartType computeType(Compiler compiler) => referenced.computeType(compiler); |
| 669 | 806 |
| 670 ti.TypeMask computeMask(Compiler compiler) { | 807 ti.TypeMask computeMask(Compiler compiler) { |
| 671 return referenced.computeMask(compiler); | 808 return referenced.computeMask(compiler); |
| 672 } | 809 } |
| 673 | 810 |
| 674 String toString() { | 811 String unparse() => 'deferred(${referenced.unparse()})'; |
| 675 return 'DeferredConstant($referenced)'; | 812 |
| 676 } | 813 String toStructuredString() => 'DeferredConstant($referenced)'; |
| 677 } | 814 } |
| OLD | NEW |