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 25 matching lines...) Expand all Loading... |
36 { bool this.isMetadata: false }) | 36 { bool this.isMetadata: false }) |
37 : initialVariableValues = new Map<VariableElement, dynamic>(), | 37 : initialVariableValues = new Map<VariableElement, dynamic>(), |
38 compiledConstants = new Set<Constant>(), | 38 compiledConstants = new Set<Constant>(), |
39 pendingVariables = new Set<VariableElement>(), | 39 pendingVariables = new Set<VariableElement>(), |
40 lazyStatics = new Set<VariableElement>(), | 40 lazyStatics = new Set<VariableElement>(), |
41 super(compiler); | 41 super(compiler); |
42 | 42 |
43 String get name => 'ConstantHandler'; | 43 String get name => 'ConstantHandler'; |
44 | 44 |
45 void registerCompileTimeConstant(Constant constant) { | 45 void registerCompileTimeConstant(Constant constant) { |
46 registerInstantiatedClass(constant.computeType(compiler).element); | 46 registerInstantiatedClass(constant.computeType(compiler).element, |
| 47 compiler.globalDependencies); |
47 if (constant.isFunction()) { | 48 if (constant.isFunction()) { |
48 FunctionConstant function = constant; | 49 FunctionConstant function = constant; |
49 registerGetOfStaticFunction(function.element); | 50 registerGetOfStaticFunction(function.element); |
50 } | 51 } |
51 compiledConstants.add(constant); | 52 compiledConstants.add(constant); |
52 } | 53 } |
53 | 54 |
54 void registerInstantiatedClass(ClassElement element) { | 55 void registerInstantiatedClass(ClassElement element, TreeElements elements) { |
55 if (isMetadata) return; | 56 if (isMetadata) return; |
56 compiler.enqueuer.codegen.registerInstantiatedClass(element); | 57 compiler.enqueuer.codegen.registerInstantiatedClass(element, elements); |
57 } | 58 } |
58 | 59 |
59 void registerStaticUse(Element element) { | 60 void registerStaticUse(Element element) { |
60 if (isMetadata) return; | 61 if (isMetadata) return; |
61 compiler.enqueuer.codegen.registerStaticUse(element); | 62 compiler.enqueuer.codegen.registerStaticUse(element); |
62 } | 63 } |
63 | 64 |
64 void registerGetOfStaticFunction(FunctionElement element) { | 65 void registerGetOfStaticFunction(FunctionElement element) { |
65 if (isMetadata) return; | 66 if (isMetadata) return; |
66 compiler.enqueuer.codegen.registerGetOfStaticFunction(element); | 67 compiler.enqueuer.codegen.registerGetOfStaticFunction(element); |
67 } | 68 } |
68 | 69 |
69 void registerStringInstance() { | 70 void registerStringInstance() { |
70 registerInstantiatedClass(compiler.stringClass); | 71 registerInstantiatedClass(compiler.stringClass, |
| 72 compiler.globalDependencies); |
71 } | 73 } |
72 | 74 |
73 void registerCreateRuntimeTypeFunction() { | 75 void registerCreateRuntimeTypeFunction() { |
74 if (createRuntimeTypeFunction != null) return; | 76 if (createRuntimeTypeFunction != null) return; |
75 SourceString helperName = const SourceString('createRuntimeType'); | 77 SourceString helperName = const SourceString('createRuntimeType'); |
76 createRuntimeTypeFunction = compiler.findHelper(helperName); | 78 createRuntimeTypeFunction = compiler.findHelper(helperName); |
77 registerStaticUse(createRuntimeTypeFunction); | 79 registerStaticUse(createRuntimeTypeFunction); |
78 } | 80 } |
79 | 81 |
80 /** | 82 /** |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 isEvaluatingConstant = oldIsEvaluatingConstant; | 307 isEvaluatingConstant = oldIsEvaluatingConstant; |
306 assert(result != null); | 308 assert(result != null); |
307 return result; | 309 return result; |
308 } | 310 } |
309 | 311 |
310 Constant visitNode(Node node) { | 312 Constant visitNode(Node node) { |
311 return signalNotCompileTimeConstant(node); | 313 return signalNotCompileTimeConstant(node); |
312 } | 314 } |
313 | 315 |
314 Constant visitLiteralBool(LiteralBool node) { | 316 Constant visitLiteralBool(LiteralBool node) { |
315 handler.registerInstantiatedClass(compiler.boolClass); | 317 handler.registerInstantiatedClass(compiler.boolClass, elements); |
316 return constantSystem.createBool(node.value); | 318 return constantSystem.createBool(node.value); |
317 } | 319 } |
318 | 320 |
319 Constant visitLiteralDouble(LiteralDouble node) { | 321 Constant visitLiteralDouble(LiteralDouble node) { |
320 handler.registerInstantiatedClass(compiler.doubleClass); | 322 handler.registerInstantiatedClass(compiler.doubleClass, elements); |
321 return constantSystem.createDouble(node.value); | 323 return constantSystem.createDouble(node.value); |
322 } | 324 } |
323 | 325 |
324 Constant visitLiteralInt(LiteralInt node) { | 326 Constant visitLiteralInt(LiteralInt node) { |
325 handler.registerInstantiatedClass(compiler.intClass); | 327 handler.registerInstantiatedClass(compiler.intClass, elements); |
326 return constantSystem.createInt(node.value); | 328 return constantSystem.createInt(node.value); |
327 } | 329 } |
328 | 330 |
329 Constant visitLiteralList(LiteralList node) { | 331 Constant visitLiteralList(LiteralList node) { |
330 if (!node.isConst()) { | 332 if (!node.isConst()) { |
331 return signalNotCompileTimeConstant(node); | 333 return signalNotCompileTimeConstant(node); |
332 } | 334 } |
333 List<Constant> arguments = <Constant>[]; | 335 List<Constant> arguments = <Constant>[]; |
334 for (Link<Node> link = node.elements.nodes; | 336 for (Link<Node> link = node.elements.nodes; |
335 !link.isEmpty; | 337 !link.isEmpty; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 DartType keysType = compiler.listClass.rawType; | 380 DartType keysType = compiler.listClass.rawType; |
379 ListConstant keysList = new ListConstant(keysType, keys); | 381 ListConstant keysList = new ListConstant(keysType, keys); |
380 handler.registerCompileTimeConstant(keysList); | 382 handler.registerCompileTimeConstant(keysList); |
381 SourceString className = hasProtoKey | 383 SourceString className = hasProtoKey |
382 ? MapConstant.DART_PROTO_CLASS | 384 ? MapConstant.DART_PROTO_CLASS |
383 : MapConstant.DART_CLASS; | 385 : MapConstant.DART_CLASS; |
384 ClassElement classElement = compiler.jsHelperLibrary.find(className); | 386 ClassElement classElement = compiler.jsHelperLibrary.find(className); |
385 classElement.ensureResolved(compiler); | 387 classElement.ensureResolved(compiler); |
386 // TODO(floitsch): copy over the generic type. | 388 // TODO(floitsch): copy over the generic type. |
387 DartType type = classElement.rawType; | 389 DartType type = classElement.rawType; |
388 handler.registerInstantiatedClass(classElement); | 390 handler.registerInstantiatedClass(classElement, elements); |
389 Constant constant = new MapConstant(type, keysList, values, protoValue); | 391 Constant constant = new MapConstant(type, keysList, values, protoValue); |
390 handler.registerCompileTimeConstant(constant); | 392 handler.registerCompileTimeConstant(constant); |
391 return constant; | 393 return constant; |
392 } | 394 } |
393 | 395 |
394 Constant visitLiteralNull(LiteralNull node) { | 396 Constant visitLiteralNull(LiteralNull node) { |
395 return constantSystem.createNull(); | 397 return constantSystem.createNull(); |
396 } | 398 } |
397 | 399 |
398 Constant visitLiteralString(LiteralString node) { | 400 Constant visitLiteralString(LiteralString node) { |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
653 assert(invariant(node, constructor.isImplementation)); | 655 assert(invariant(node, constructor.isImplementation)); |
654 | 656 |
655 Selector selector = elements.getSelector(send); | 657 Selector selector = elements.getSelector(send); |
656 List<Constant> arguments = evaluateArgumentsToConstructor( | 658 List<Constant> arguments = evaluateArgumentsToConstructor( |
657 node, selector, send.arguments, constructor); | 659 node, selector, send.arguments, constructor); |
658 ConstructorEvaluator evaluator = | 660 ConstructorEvaluator evaluator = |
659 new ConstructorEvaluator(node, constructor, handler, compiler); | 661 new ConstructorEvaluator(node, constructor, handler, compiler); |
660 evaluator.evaluateConstructorFieldValues(arguments); | 662 evaluator.evaluateConstructorFieldValues(arguments); |
661 List<Constant> jsNewArguments = evaluator.buildJsNewArguments(classElement); | 663 List<Constant> jsNewArguments = evaluator.buildJsNewArguments(classElement); |
662 | 664 |
663 handler.registerInstantiatedClass(classElement); | 665 handler.registerInstantiatedClass(classElement, elements); |
664 // TODO(floitsch): take generic types into account. | 666 // TODO(floitsch): take generic types into account. |
665 classElement.computeType(compiler); | 667 classElement.computeType(compiler); |
666 DartType type = classElement.rawType; | 668 DartType type = classElement.rawType; |
667 Constant constant = new ConstructedConstant(type, jsNewArguments); | 669 Constant constant = new ConstructedConstant(type, jsNewArguments); |
668 handler.registerCompileTimeConstant(constant); | 670 handler.registerCompileTimeConstant(constant); |
669 return constant; | 671 return constant; |
670 } | 672 } |
671 | 673 |
672 Constant visitParenthesizedExpression(ParenthesizedExpression node) { | 674 Constant visitParenthesizedExpression(ParenthesizedExpression node) { |
673 return node.expression.accept(this); | 675 return node.expression.accept(this); |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
882 // Use the default value. | 884 // Use the default value. |
883 fieldValue = handler.compileConstant(field); | 885 fieldValue = handler.compileConstant(field); |
884 } | 886 } |
885 jsNewArguments.add(fieldValue); | 887 jsNewArguments.add(fieldValue); |
886 }, | 888 }, |
887 includeBackendMembers: true, | 889 includeBackendMembers: true, |
888 includeSuperMembers: true); | 890 includeSuperMembers: true); |
889 return jsNewArguments; | 891 return jsNewArguments; |
890 } | 892 } |
891 } | 893 } |
OLD | NEW |