| 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 /** | 7 /** |
| 8 * The [ConstantHandler] keeps track of compile-time constants, | 8 * The [ConstantHandler] keeps track of compile-time constants, |
| 9 * initializations of global and static fields, and default values of | 9 * initializations of global and static fields, and default values of |
| 10 * optional parameters. | 10 * optional parameters. |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 if (compiler.enableTypeAssertions | 168 if (compiler.enableTypeAssertions |
| 169 && value != null | 169 && value != null |
| 170 && element.isField()) { | 170 && element.isField()) { |
| 171 DartType elementType = element.computeType(compiler); | 171 DartType elementType = element.computeType(compiler); |
| 172 DartType constantType = value.computeType(compiler); | 172 DartType constantType = value.computeType(compiler); |
| 173 if (elementType.isMalformed || constantType.isMalformed || | 173 if (elementType.isMalformed || constantType.isMalformed || |
| 174 !constantSystem.isSubtype(compiler, constantType, elementType)) { | 174 !constantSystem.isSubtype(compiler, constantType, elementType)) { |
| 175 if (isConst) { | 175 if (isConst) { |
| 176 compiler.reportError(node, new CompileTimeConstantError( | 176 compiler.reportError(node, new CompileTimeConstantError( |
| 177 MessageKind.NOT_ASSIGNABLE, | 177 MessageKind.NOT_ASSIGNABLE, |
| 178 {'fromType': elementType, 'toType': constantType})); | 178 {'fromType': constantType, 'toType': elementType})); |
| 179 } else { | 179 } else { |
| 180 // If the field can be lazily initialized, we will throw | 180 // If the field can be lazily initialized, we will throw |
| 181 // the exception at runtime. | 181 // the exception at runtime. |
| 182 value = null; | 182 value = null; |
| 183 } | 183 } |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 } | 186 } |
| 187 if (value != null) { | 187 if (value != null) { |
| 188 initialVariableValues[element] = value; | 188 initialVariableValues[element] = value; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 if (!node.isConst()) { | 330 if (!node.isConst()) { |
| 331 return signalNotCompileTimeConstant(node); | 331 return signalNotCompileTimeConstant(node); |
| 332 } | 332 } |
| 333 List<Constant> arguments = <Constant>[]; | 333 List<Constant> arguments = <Constant>[]; |
| 334 for (Link<Node> link = node.elements.nodes; | 334 for (Link<Node> link = node.elements.nodes; |
| 335 !link.isEmpty; | 335 !link.isEmpty; |
| 336 link = link.tail) { | 336 link = link.tail) { |
| 337 arguments.add(evaluateConstant(link.head)); | 337 arguments.add(evaluateConstant(link.head)); |
| 338 } | 338 } |
| 339 // TODO(floitsch): get type parameters. | 339 // TODO(floitsch): get type parameters. |
| 340 DartType type = new InterfaceType(compiler.listClass); | 340 compiler.listClass.computeType(compiler); |
| 341 DartType type = compiler.listClass.rawType; |
| 341 Constant constant = new ListConstant(type, arguments); | 342 Constant constant = new ListConstant(type, arguments); |
| 342 handler.registerCompileTimeConstant(constant); | 343 handler.registerCompileTimeConstant(constant); |
| 343 return constant; | 344 return constant; |
| 344 } | 345 } |
| 345 | 346 |
| 346 Constant visitLiteralMap(LiteralMap node) { | 347 Constant visitLiteralMap(LiteralMap node) { |
| 347 if (!node.isConst()) { | 348 if (!node.isConst()) { |
| 348 return signalNotCompileTimeConstant(node); | 349 return signalNotCompileTimeConstant(node); |
| 349 } | 350 } |
| 350 List<StringConstant> keys = <StringConstant>[]; | 351 List<StringConstant> keys = <StringConstant>[]; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 366 Constant protoValue = null; | 367 Constant protoValue = null; |
| 367 for (StringConstant key in keys) { | 368 for (StringConstant key in keys) { |
| 368 if (key.value == MapConstant.PROTO_PROPERTY) { | 369 if (key.value == MapConstant.PROTO_PROPERTY) { |
| 369 protoValue = map[key]; | 370 protoValue = map[key]; |
| 370 } else { | 371 } else { |
| 371 values.add(map[key]); | 372 values.add(map[key]); |
| 372 } | 373 } |
| 373 } | 374 } |
| 374 bool hasProtoKey = (protoValue != null); | 375 bool hasProtoKey = (protoValue != null); |
| 375 // TODO(floitsch): this should be a List<String> type. | 376 // TODO(floitsch): this should be a List<String> type. |
| 376 DartType keysType = new InterfaceType(compiler.listClass); | 377 compiler.listClass.computeType(compiler); |
| 378 DartType keysType = compiler.listClass.rawType; |
| 377 ListConstant keysList = new ListConstant(keysType, keys); | 379 ListConstant keysList = new ListConstant(keysType, keys); |
| 378 handler.registerCompileTimeConstant(keysList); | 380 handler.registerCompileTimeConstant(keysList); |
| 379 SourceString className = hasProtoKey | 381 SourceString className = hasProtoKey |
| 380 ? MapConstant.DART_PROTO_CLASS | 382 ? MapConstant.DART_PROTO_CLASS |
| 381 : MapConstant.DART_CLASS; | 383 : MapConstant.DART_CLASS; |
| 382 ClassElement classElement = compiler.jsHelperLibrary.find(className); | 384 ClassElement classElement = compiler.jsHelperLibrary.find(className); |
| 383 classElement.ensureResolved(compiler); | 385 classElement.ensureResolved(compiler); |
| 384 // TODO(floitsch): copy over the generic type. | 386 // TODO(floitsch): copy over the generic type. |
| 385 DartType type = new InterfaceType(classElement); | 387 DartType type = classElement.rawType; |
| 386 handler.registerInstantiatedClass(classElement); | 388 handler.registerInstantiatedClass(classElement); |
| 387 Constant constant = new MapConstant(type, keysList, values, protoValue); | 389 Constant constant = new MapConstant(type, keysList, values, protoValue); |
| 388 handler.registerCompileTimeConstant(constant); | 390 handler.registerCompileTimeConstant(constant); |
| 389 return constant; | 391 return constant; |
| 390 } | 392 } |
| 391 | 393 |
| 392 Constant visitLiteralNull(LiteralNull node) { | 394 Constant visitLiteralNull(LiteralNull node) { |
| 393 return constantSystem.createNull(); | 395 return constantSystem.createNull(); |
| 394 } | 396 } |
| 395 | 397 |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 653 Selector selector = elements.getSelector(send); | 655 Selector selector = elements.getSelector(send); |
| 654 List<Constant> arguments = evaluateArgumentsToConstructor( | 656 List<Constant> arguments = evaluateArgumentsToConstructor( |
| 655 node, selector, send.arguments, constructor); | 657 node, selector, send.arguments, constructor); |
| 656 ConstructorEvaluator evaluator = | 658 ConstructorEvaluator evaluator = |
| 657 new ConstructorEvaluator(node, constructor, handler, compiler); | 659 new ConstructorEvaluator(node, constructor, handler, compiler); |
| 658 evaluator.evaluateConstructorFieldValues(arguments); | 660 evaluator.evaluateConstructorFieldValues(arguments); |
| 659 List<Constant> jsNewArguments = evaluator.buildJsNewArguments(classElement); | 661 List<Constant> jsNewArguments = evaluator.buildJsNewArguments(classElement); |
| 660 | 662 |
| 661 handler.registerInstantiatedClass(classElement); | 663 handler.registerInstantiatedClass(classElement); |
| 662 // TODO(floitsch): take generic types into account. | 664 // TODO(floitsch): take generic types into account. |
| 663 DartType type = classElement.computeType(compiler); | 665 classElement.computeType(compiler); |
| 666 DartType type = classElement.rawType; |
| 664 Constant constant = new ConstructedConstant(type, jsNewArguments); | 667 Constant constant = new ConstructedConstant(type, jsNewArguments); |
| 665 handler.registerCompileTimeConstant(constant); | 668 handler.registerCompileTimeConstant(constant); |
| 666 return constant; | 669 return constant; |
| 667 } | 670 } |
| 668 | 671 |
| 669 Constant visitParenthesizedExpression(ParenthesizedExpression node) { | 672 Constant visitParenthesizedExpression(ParenthesizedExpression node) { |
| 670 return node.expression.accept(this); | 673 return node.expression.accept(this); |
| 671 } | 674 } |
| 672 | 675 |
| 673 error(Node node) { | 676 error(Node node) { |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 879 // Use the default value. | 882 // Use the default value. |
| 880 fieldValue = handler.compileConstant(field); | 883 fieldValue = handler.compileConstant(field); |
| 881 } | 884 } |
| 882 jsNewArguments.add(fieldValue); | 885 jsNewArguments.add(fieldValue); |
| 883 }, | 886 }, |
| 884 includeBackendMembers: true, | 887 includeBackendMembers: true, |
| 885 includeSuperMembers: true); | 888 includeSuperMembers: true); |
| 886 return jsNewArguments; | 889 return jsNewArguments; |
| 887 } | 890 } |
| 888 } | 891 } |
| OLD | NEW |