| 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 library dart2js.compile_time_constant_evaluator; | 5 library dart2js.compile_time_constant_evaluator; |
| 6 | 6 |
| 7 import 'constant_system_dart.dart'; | 7 import 'constant_system_dart.dart'; |
| 8 import 'constants/constant_system.dart'; | 8 import 'constants/constant_system.dart'; |
| 9 import 'constants/expressions.dart'; | 9 import 'constants/expressions.dart'; |
| 10 import 'constants/values.dart'; | 10 import 'constants/values.dart'; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 return new ErroneousConstantExpression(); | 152 return new ErroneousConstantExpression(); |
| 153 } | 153 } |
| 154 return null; | 154 return null; |
| 155 } | 155 } |
| 156 pendingVariables.add(element); | 156 pendingVariables.add(element); |
| 157 | 157 |
| 158 Expression initializer = element.initializer; | 158 Expression initializer = element.initializer; |
| 159 ConstantExpression value; | 159 ConstantExpression value; |
| 160 if (initializer == null) { | 160 if (initializer == null) { |
| 161 // No initial value. | 161 // No initial value. |
| 162 value = new PrimitiveConstantExpression(new NullConstantValue()); | 162 value = new NullConstantExpression(new NullConstantValue()); |
| 163 } else { | 163 } else { |
| 164 value = compileNodeWithDefinitions( | 164 value = compileNodeWithDefinitions( |
| 165 initializer, definitions, isConst: isConst); | 165 initializer, definitions, isConst: isConst); |
| 166 if (compiler.enableTypeAssertions && | 166 if (compiler.enableTypeAssertions && |
| 167 value != null && | 167 value != null && |
| 168 element.isField) { | 168 element.isField) { |
| 169 DartType elementType = element.type; | 169 DartType elementType = element.type; |
| 170 if (elementType.isMalformed && !value.value.isNull) { | 170 if (elementType.isMalformed && !value.value.isNull) { |
| 171 if (isConst) { | 171 if (isConst) { |
| 172 ErroneousElement element = elementType.element; | 172 ErroneousElement element = elementType.element; |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 assert(result != null); | 294 assert(result != null); |
| 295 return result; | 295 return result; |
| 296 } | 296 } |
| 297 | 297 |
| 298 AstConstant visitNode(Node node) { | 298 AstConstant visitNode(Node node) { |
| 299 return signalNotCompileTimeConstant(node); | 299 return signalNotCompileTimeConstant(node); |
| 300 } | 300 } |
| 301 | 301 |
| 302 AstConstant visitLiteralBool(LiteralBool node) { | 302 AstConstant visitLiteralBool(LiteralBool node) { |
| 303 return new AstConstant( | 303 return new AstConstant( |
| 304 context, node, new PrimitiveConstantExpression( | 304 context, node, new BoolConstantExpression( |
| 305 node.value, |
| 305 constantSystem.createBool(node.value))); | 306 constantSystem.createBool(node.value))); |
| 306 } | 307 } |
| 307 | 308 |
| 308 AstConstant visitLiteralDouble(LiteralDouble node) { | 309 AstConstant visitLiteralDouble(LiteralDouble node) { |
| 309 return new AstConstant( | 310 return new AstConstant( |
| 310 context, node, new PrimitiveConstantExpression( | 311 context, node, new DoubleConstantExpression( |
| 312 node.value, |
| 311 constantSystem.createDouble(node.value))); | 313 constantSystem.createDouble(node.value))); |
| 312 } | 314 } |
| 313 | 315 |
| 314 AstConstant visitLiteralInt(LiteralInt node) { | 316 AstConstant visitLiteralInt(LiteralInt node) { |
| 315 return new AstConstant( | 317 return new AstConstant( |
| 316 context, node, new PrimitiveConstantExpression( | 318 context, node, new IntConstantExpression( |
| 319 node.value, |
| 317 constantSystem.createInt(node.value))); | 320 constantSystem.createInt(node.value))); |
| 318 } | 321 } |
| 319 | 322 |
| 320 AstConstant visitLiteralList(LiteralList node) { | 323 AstConstant visitLiteralList(LiteralList node) { |
| 321 if (!node.isConst) { | 324 if (!node.isConst) { |
| 322 return signalNotCompileTimeConstant(node); | 325 return signalNotCompileTimeConstant(node); |
| 323 } | 326 } |
| 324 List<ConstantExpression> argumentExpressions = <ConstantExpression>[]; | 327 List<ConstantExpression> argumentExpressions = <ConstantExpression>[]; |
| 325 List<ConstantValue> argumentValues = <ConstantValue>[]; | 328 List<ConstantValue> argumentValues = <ConstantValue>[]; |
| 326 for (Link<Node> link = node.elements.nodes; | 329 for (Link<Node> link = node.elements.nodes; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 context, node, new MapConstantExpression( | 378 context, node, new MapConstantExpression( |
| 376 constantSystem.createMap(compiler, type, keyValues, | 379 constantSystem.createMap(compiler, type, keyValues, |
| 377 valueExpressions.map((e) => e.value).toList()), | 380 valueExpressions.map((e) => e.value).toList()), |
| 378 type, | 381 type, |
| 379 keyExpressions, | 382 keyExpressions, |
| 380 valueExpressions)); | 383 valueExpressions)); |
| 381 } | 384 } |
| 382 | 385 |
| 383 AstConstant visitLiteralNull(LiteralNull node) { | 386 AstConstant visitLiteralNull(LiteralNull node) { |
| 384 return new AstConstant( | 387 return new AstConstant( |
| 385 context, node, new PrimitiveConstantExpression( | 388 context, node, new NullConstantExpression( |
| 386 constantSystem.createNull())); | 389 constantSystem.createNull())); |
| 387 } | 390 } |
| 388 | 391 |
| 389 AstConstant visitLiteralString(LiteralString node) { | 392 AstConstant visitLiteralString(LiteralString node) { |
| 390 return new AstConstant( | 393 return new AstConstant( |
| 391 context, node, new PrimitiveConstantExpression( | 394 context, node, new StringConstantExpression( |
| 395 node.dartString.slowToString(), |
| 392 constantSystem.createString(node.dartString))); | 396 constantSystem.createString(node.dartString))); |
| 393 } | 397 } |
| 394 | 398 |
| 395 AstConstant visitStringJuxtaposition(StringJuxtaposition node) { | 399 AstConstant visitStringJuxtaposition(StringJuxtaposition node) { |
| 396 AstConstant left = evaluate(node.first); | 400 AstConstant left = evaluate(node.first); |
| 397 AstConstant right = evaluate(node.second); | 401 AstConstant right = evaluate(node.second); |
| 398 if (left == null || right == null) return null; | 402 if (left == null || right == null) return null; |
| 399 StringConstantValue leftValue = left.value; | 403 StringConstantValue leftValue = left.value; |
| 400 StringConstantValue rightValue = right.value; | 404 StringConstantValue rightValue = right.value; |
| 401 return new AstConstant( | 405 return new AstConstant( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 context, node, new ConcatenateConstantExpression( | 451 context, node, new ConcatenateConstantExpression( |
| 448 constantSystem.createString(accumulator), | 452 constantSystem.createString(accumulator), |
| 449 subexpressions)); | 453 subexpressions)); |
| 450 } | 454 } |
| 451 | 455 |
| 452 AstConstant visitLiteralSymbol(LiteralSymbol node) { | 456 AstConstant visitLiteralSymbol(LiteralSymbol node) { |
| 453 InterfaceType type = compiler.symbolClass.rawType; | 457 InterfaceType type = compiler.symbolClass.rawType; |
| 454 String text = node.slowNameString; | 458 String text = node.slowNameString; |
| 455 List<AstConstant> arguments = | 459 List<AstConstant> arguments = |
| 456 <AstConstant>[new AstConstant(context, node, | 460 <AstConstant>[new AstConstant(context, node, |
| 457 new PrimitiveConstantExpression(constantSystem.createString( | 461 new StringConstantExpression( |
| 458 new DartString.literal(text))))]; | 462 text, |
| 463 constantSystem.createString(new LiteralDartString(text))))]; |
| 459 AstConstant constant = makeConstructedConstant( | 464 AstConstant constant = makeConstructedConstant( |
| 460 compiler, handler, context, node, type, compiler.symbolConstructor, | 465 compiler, handler, context, node, type, compiler.symbolConstructor, |
| 461 CallStructure.ONE_ARG, | 466 CallStructure.ONE_ARG, |
| 462 arguments, arguments); | 467 arguments, arguments); |
| 463 return new AstConstant( | 468 return new AstConstant( |
| 464 context, node, new SymbolConstantExpression(constant.value, text)); | 469 context, node, new SymbolConstantExpression(constant.value, text)); |
| 465 } | 470 } |
| 466 | 471 |
| 467 AstConstant makeTypeConstant(Node node, DartType elementType) { | 472 AstConstant makeTypeConstant(Node node, DartType elementType) { |
| 468 DartType constantType = | 473 DartType constantType = |
| (...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 889 // and present some kind of stack-trace. | 894 // and present some kind of stack-trace. |
| 890 compiler.reportError(node, message); | 895 compiler.reportError(node, message); |
| 891 } | 896 } |
| 892 | 897 |
| 893 AstConstant signalNotCompileTimeConstant(Node node, | 898 AstConstant signalNotCompileTimeConstant(Node node, |
| 894 {MessageKind message: MessageKind.NOT_A_COMPILE_TIME_CONSTANT}) { | 899 {MessageKind message: MessageKind.NOT_A_COMPILE_TIME_CONSTANT}) { |
| 895 if (isEvaluatingConstant) { | 900 if (isEvaluatingConstant) { |
| 896 error(node, message); | 901 error(node, message); |
| 897 | 902 |
| 898 return new AstConstant( | 903 return new AstConstant( |
| 899 null, node, new PrimitiveConstantExpression(new NullConstantValue())); | 904 null, node, new NullConstantExpression(new NullConstantValue())); |
| 900 } | 905 } |
| 901 // Else we don't need to do anything. The final handler is only | 906 // Else we don't need to do anything. The final handler is only |
| 902 // optimistically trying to compile constants. So it is normal that we | 907 // optimistically trying to compile constants. So it is normal that we |
| 903 // sometimes see non-compile time constants. | 908 // sometimes see non-compile time constants. |
| 904 // Simply return [:null:] which is used to propagate a failing | 909 // Simply return [:null:] which is used to propagate a failing |
| 905 // compile-time compilation. | 910 // compile-time compilation. |
| 906 return null; | 911 return null; |
| 907 } | 912 } |
| 908 } | 913 } |
| 909 | 914 |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1138 ConstantValue get value => expression.value; | 1143 ConstantValue get value => expression.value; |
| 1139 | 1144 |
| 1140 String toString() => expression.toString(); | 1145 String toString() => expression.toString(); |
| 1141 } | 1146 } |
| 1142 | 1147 |
| 1143 /// A synthetic constant used to recover from errors. | 1148 /// A synthetic constant used to recover from errors. |
| 1144 class ErroneousAstConstant extends AstConstant { | 1149 class ErroneousAstConstant extends AstConstant { |
| 1145 ErroneousAstConstant(Element element, Node node) | 1150 ErroneousAstConstant(Element element, Node node) |
| 1146 : super(element, node, new ErroneousConstantExpression()); | 1151 : super(element, node, new ErroneousConstantExpression()); |
| 1147 } | 1152 } |
| OLD | NEW |