| 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 /** | 5 /** |
| 6 * The [ConstantHandler] keeps track of compile-time constants, | 6 * The [ConstantHandler] keeps track of compile-time constants, |
| 7 * initializations of global and static fields, and default values of | 7 * initializations of global and static fields, and default values of |
| 8 * optional parameters. | 8 * optional parameters. |
| 9 */ | 9 */ |
| 10 class ConstantHandler extends CompilerTask { | 10 class ConstantHandler extends CompilerTask { |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 | 118 |
| 119 SendSet assignment = node.asSendSet(); | 119 SendSet assignment = node.asSendSet(); |
| 120 Constant value; | 120 Constant value; |
| 121 if (assignment === null) { | 121 if (assignment === null) { |
| 122 // No initial value. | 122 // No initial value. |
| 123 value = new NullConstant(); | 123 value = new NullConstant(); |
| 124 } else { | 124 } else { |
| 125 Node right = assignment.arguments.head; | 125 Node right = assignment.arguments.head; |
| 126 value = | 126 value = |
| 127 compileNodeWithDefinitions(right, definitions, isConst: isConst); | 127 compileNodeWithDefinitions(right, definitions, isConst: isConst); |
| 128 if (compiler.enableTypeAssertions |
| 129 && value != null |
| 130 && element.isField()) { |
| 131 DartType elementType = element.computeType(compiler); |
| 132 DartType constantType = value.computeType(compiler); |
| 133 if (!compiler.types.isSubtype(constantType, elementType)) { |
| 134 // If the type of the constant does not implement the |
| 135 // declared type of the variable, we make the initialization |
| 136 // of this variable lazy so that at runtime we get the type |
| 137 // error only if the variable is used. |
| 138 value = null; |
| 139 } |
| 140 } |
| 128 } | 141 } |
| 129 if (value != null) { | 142 if (value != null) { |
| 130 initialVariableValues[element] = value; | 143 initialVariableValues[element] = value; |
| 131 } else { | 144 } else { |
| 132 assert(!isConst); | 145 assert(!isConst || compiler.enableTypeAssertions); |
| 133 lazyStatics.add(element); | 146 lazyStatics.add(element); |
| 134 } | 147 } |
| 135 pendingVariables.remove(element); | 148 pendingVariables.remove(element); |
| 136 return value; | 149 return value; |
| 137 }); | 150 }); |
| 138 } | 151 } |
| 139 | 152 |
| 140 Constant compileNodeWithDefinitions(Node node, | 153 Constant compileNodeWithDefinitions(Node node, |
| 141 TreeElements definitions, | 154 TreeElements definitions, |
| 142 [bool isConst]) { | 155 [bool isConst]) { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 Constant visitLiteralList(LiteralList node) { | 279 Constant visitLiteralList(LiteralList node) { |
| 267 if (!node.isConst()) { | 280 if (!node.isConst()) { |
| 268 return signalNotCompileTimeConstant(node); | 281 return signalNotCompileTimeConstant(node); |
| 269 } | 282 } |
| 270 List<Constant> arguments = <Constant>[]; | 283 List<Constant> arguments = <Constant>[]; |
| 271 for (Link<Node> link = node.elements.nodes; | 284 for (Link<Node> link = node.elements.nodes; |
| 272 !link.isEmpty(); | 285 !link.isEmpty(); |
| 273 link = link.tail) { | 286 link = link.tail) { |
| 274 arguments.add(evaluateConstant(link.head)); | 287 arguments.add(evaluateConstant(link.head)); |
| 275 } | 288 } |
| 276 // TODO(floitsch): get type from somewhere. | 289 // TODO(floitsch): get type parameters. |
| 277 DartType type = null; | 290 DartType type = new InterfaceType(compiler.listClass); |
| 278 Constant constant = new ListConstant(type, arguments); | 291 Constant constant = new ListConstant(type, arguments); |
| 279 compiler.constantHandler.registerCompileTimeConstant(constant); | 292 compiler.constantHandler.registerCompileTimeConstant(constant); |
| 280 return constant; | 293 return constant; |
| 281 } | 294 } |
| 282 | 295 |
| 283 Constant visitLiteralMap(LiteralMap node) { | 296 Constant visitLiteralMap(LiteralMap node) { |
| 284 if (!node.isConst()) { | 297 if (!node.isConst()) { |
| 285 signalNotCompileTimeConstant(node); | 298 signalNotCompileTimeConstant(node); |
| 286 error(node); | 299 error(node); |
| 287 } | 300 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 304 Constant protoValue = null; | 317 Constant protoValue = null; |
| 305 for (StringConstant key in keys) { | 318 for (StringConstant key in keys) { |
| 306 if (key.value == MapConstant.PROTO_PROPERTY) { | 319 if (key.value == MapConstant.PROTO_PROPERTY) { |
| 307 protoValue = map[key]; | 320 protoValue = map[key]; |
| 308 } else { | 321 } else { |
| 309 values.add(map[key]); | 322 values.add(map[key]); |
| 310 } | 323 } |
| 311 } | 324 } |
| 312 bool hasProtoKey = (protoValue !== null); | 325 bool hasProtoKey = (protoValue !== null); |
| 313 // TODO(floitsch): this should be a List<String> type. | 326 // TODO(floitsch): this should be a List<String> type. |
| 314 DartType keysType = null; | 327 DartType keysType = new InterfaceType(compiler.listClass); |
| 315 ListConstant keysList = new ListConstant(keysType, keys); | 328 ListConstant keysList = new ListConstant(keysType, keys); |
| 316 compiler.constantHandler.registerCompileTimeConstant(keysList); | 329 compiler.constantHandler.registerCompileTimeConstant(keysList); |
| 317 SourceString className = hasProtoKey | 330 SourceString className = hasProtoKey |
| 318 ? MapConstant.DART_PROTO_CLASS | 331 ? MapConstant.DART_PROTO_CLASS |
| 319 : MapConstant.DART_CLASS; | 332 : MapConstant.DART_CLASS; |
| 320 ClassElement classElement = compiler.jsHelperLibrary.find(className); | 333 ClassElement classElement = compiler.jsHelperLibrary.find(className); |
| 321 classElement.ensureResolved(compiler); | 334 classElement.ensureResolved(compiler); |
| 322 // TODO(floitsch): copy over the generic type. | 335 // TODO(floitsch): copy over the generic type. |
| 323 DartType type = new InterfaceType(classElement); | 336 DartType type = new InterfaceType(classElement); |
| 324 compiler.enqueuer.codegen.registerInstantiatedClass(classElement); | 337 compiler.enqueuer.codegen.registerInstantiatedClass(classElement); |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 737 Constant fieldValue = fieldValues[field]; | 750 Constant fieldValue = fieldValues[field]; |
| 738 if (fieldValue === null) { | 751 if (fieldValue === null) { |
| 739 // Use the default value. | 752 // Use the default value. |
| 740 fieldValue = compiler.compileConstant(field); | 753 fieldValue = compiler.compileConstant(field); |
| 741 } | 754 } |
| 742 jsNewArguments.add(fieldValue); | 755 jsNewArguments.add(fieldValue); |
| 743 }); | 756 }); |
| 744 return jsNewArguments; | 757 return jsNewArguments; |
| 745 } | 758 } |
| 746 } | 759 } |
| OLD | NEW |