| 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 [CompileTimeConstantHandler] keeps track of compile-time constants, and | 6 * The [CompileTimeConstantHandler] keeps track of compile-time constants, and |
| 7 * initializations of global and static fields. | 7 * initializations of global and static fields. |
| 8 */ | 8 */ |
| 9 class CompileTimeConstantHandler extends CompilerTask { | 9 class CompileTimeConstantHandler extends CompilerTask { |
| 10 // Contains the initial value of fields. Must contain all static and global | 10 // Contains the initial value of fields. Must contain all static and global |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 VariableElement element = work.element; | 28 VariableElement element = work.element; |
| 29 // Shortcut if it has already been compiled. | 29 // Shortcut if it has already been compiled. |
| 30 if (initialFieldValues.containsKey(element)) return; | 30 if (initialFieldValues.containsKey(element)) return; |
| 31 compileFieldWithDefinitions(element, work.resolutionTree); | 31 compileFieldWithDefinitions(element, work.resolutionTree); |
| 32 } | 32 } |
| 33 | 33 |
| 34 compileField(VariableElement element) { | 34 compileField(VariableElement element) { |
| 35 if (initialFieldValues.containsKey(element)) { | 35 if (initialFieldValues.containsKey(element)) { |
| 36 return initialFieldValues[element]; | 36 return initialFieldValues[element]; |
| 37 } | 37 } |
| 38 // TODO(floitsch): keep track of currently compiling elements so that we |
| 39 // don't end up in an infinite loop: final x = y; final y = x; |
| 38 TreeElements definitions = compiler.analyzeElement(element); | 40 TreeElements definitions = compiler.analyzeElement(element); |
| 39 return compileFieldWithDefinitions(element, definitions); | 41 return compileFieldWithDefinitions(element, definitions); |
| 40 } | 42 } |
| 41 | 43 |
| 42 compileFieldWithDefinitions(VariableElement element, | 44 compileFieldWithDefinitions(VariableElement element, |
| 43 TreeElements definitions) { | 45 TreeElements definitions) { |
| 44 return measure(() { | 46 return measure(() { |
| 45 Node node = element.parseNode(compiler, compiler); | 47 Node node = element.parseNode(compiler, compiler); |
| 46 assert(node !== null); | 48 assert(node !== null); |
| 47 SendSet assignment = node.asSendSet(); | 49 SendSet assignment = node.asSendSet(); |
| 48 var value; | 50 var value; |
| 49 if (assignment === null) { | 51 if (assignment === null) { |
| 50 // No initial value. | 52 // No initial value. |
| 51 value = null; | 53 value = null; |
| 52 } else { | 54 } else { |
| 53 compiler.unimplemented("CTC for static initialized fields.", | 55 Node right = node.arguments.head; |
| 54 node: node); | 56 CompileTimeConstantEvaluator evaluator = |
| 57 new CompileTimeConstantEvaluator(this, definitions, compiler); |
| 58 value = evaluator.evaluate(right); |
| 55 } | 59 } |
| 56 initialFieldValues[element] = value; | 60 initialFieldValues[element] = value; |
| 57 return value; | 61 return value; |
| 58 }); | 62 }); |
| 59 } | 63 } |
| 60 | 64 |
| 61 /** | 65 /** |
| 62 * Returns a [List] of static non final fields that need to be initialized. | 66 * Returns a [List] of static non final fields that need to be initialized. |
| 63 * The list must be evaluated in order since the fields might depend on each | 67 * The list must be evaluated in order since the fields might depend on each |
| 64 * other. | 68 * other. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 75 * other. | 79 * other. |
| 76 */ | 80 */ |
| 77 List<VariableElement> getStaticFinalFieldsForEmission() { | 81 List<VariableElement> getStaticFinalFieldsForEmission() { |
| 78 return initialFieldValues.getKeys().filter((element) { | 82 return initialFieldValues.getKeys().filter((element) { |
| 79 return !element.isInstanceMember() && element.modifiers.isFinal(); | 83 return !element.isInstanceMember() && element.modifiers.isFinal(); |
| 80 }); | 84 }); |
| 81 } | 85 } |
| 82 | 86 |
| 83 void emitJsCodeForField(VariableElement element, StringBuffer buffer) { | 87 void emitJsCodeForField(VariableElement element, StringBuffer buffer) { |
| 84 var value = initialFieldValues[element]; | 88 var value = initialFieldValues[element]; |
| 85 // TODO(floitsch): support more values. | 89 if (value === null) { |
| 86 assert(value === null); | 90 buffer.add("(void 0)"); |
| 87 buffer.add("(void 0)"); | 91 } else if (value === true) { |
| 92 buffer.add("true"); |
| 93 } else if (value === false) { |
| 94 buffer.add("false"); |
| 95 } else if (value is num) { |
| 96 buffer.add("$value"); |
| 97 } else { |
| 98 // TODO(floitsch): support more values. |
| 99 compiler.unimplemented("CompileTimeConstantHandler.emitJsCodeForField", |
| 100 node: element.parseNode(compiler, compiler)); |
| 101 } |
| 88 } | 102 } |
| 89 } | 103 } |
| 104 |
| 105 class CompileTimeConstantEvaluator extends AbstractVisitor { |
| 106 final CompileTimeConstantHandler constantHandler; |
| 107 final TreeElements definitions; |
| 108 final Compiler compiler; |
| 109 |
| 110 CompileTimeConstantEvaluator(this.constantHandler, |
| 111 this.definitions, |
| 112 this.compiler); |
| 113 |
| 114 evaluate(Node node) { |
| 115 return node.accept(this); |
| 116 } |
| 117 |
| 118 visitNode(Node node) { |
| 119 compiler.unimplemented("CompileTimeConstantEvaluator", node: node); |
| 120 } |
| 121 |
| 122 visitLiteral(Literal literal) { |
| 123 return literal.value; |
| 124 } |
| 125 |
| 126 visitSend(Send send) { |
| 127 Element element = definitions[send]; |
| 128 if (element !== null && element.kind == ElementKind.FIELD) { |
| 129 if (element.isInstanceMember() || |
| 130 element.modifiers === null || |
| 131 !element.modifiers.isFinal()) { |
| 132 error(element); |
| 133 } |
| 134 return constantHandler.compileField(element); |
| 135 } |
| 136 return super.visitSend(send); |
| 137 } |
| 138 |
| 139 error(Element element) { |
| 140 // TODO(floitsch): get the list of constants that are currently compiled |
| 141 // and present some kind of stack-trace. |
| 142 MessageKind kind = MessageKind.NOT_A_COMPILE_TIME_CONSTANT; |
| 143 List arguments = [element.name]; |
| 144 Node node = element.parseNode(compiler, compiler); |
| 145 compiler.reportError(node, new CompileTimeConstantError(kind, arguments)); |
| 146 } |
| 147 } |
| OLD | NEW |