Chromium Code Reviews| 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, | 6 * The [CompileTimeConstantHandler] 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 CompileTimeConstantHandler extends CompilerTask { | 10 class CompileTimeConstantHandler extends CompilerTask { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 * other. | 83 * other. |
| 84 */ | 84 */ |
| 85 List<VariableElement> getStaticFinalFieldsForEmission() { | 85 List<VariableElement> getStaticFinalFieldsForEmission() { |
| 86 return initialVariableValues.getKeys().filter((element) { | 86 return initialVariableValues.getKeys().filter((element) { |
| 87 return element.kind == ElementKind.FIELD | 87 return element.kind == ElementKind.FIELD |
| 88 && !element.isInstanceMember() | 88 && !element.isInstanceMember() |
| 89 && element.modifiers.isFinal(); | 89 && element.modifiers.isFinal(); |
| 90 }); | 90 }); |
| 91 } | 91 } |
| 92 | 92 |
| 93 String getJsCodeForVariable(VariableElement element) { | 93 StringBuffer writeJsCodeForVariable(StringBuffer buffer, |
| 94 VariableElement element) { | |
| 94 var value = initialVariableValues[element]; | 95 var value = initialVariableValues[element]; |
| 95 if (value === null) return "(void 0)"; | 96 if (value === null) { |
| 96 if (value is num) return "$value"; | 97 buffer.add("(void 0)"); |
| 97 if (value === true) return "true"; | 98 } else if (value is num) { |
| 98 if (value === false) return "false"; | 99 buffer.add("$value"); |
|
floitsch
2012/02/14 16:08:02
not your code, but could you please add parenthesi
karlklose
2012/02/15 10:37:30
Done.
| |
| 100 } else if (value === true) { | |
| 101 buffer.add("true"); | |
| 102 } else if (value === false) { | |
| 103 buffer.add("false"); | |
| 104 } else if (value is DartString) { | |
| 105 buffer.add("'"); | |
| 106 writeEscapedString(value, buffer, (reason) { | |
| 107 compiler.cancel("failed to write escaped string: $value"); | |
| 108 }); | |
| 109 buffer.add("'"); | |
| 110 } else { | |
| 111 // TODO(floitsch): support more values. | |
| 112 compiler.unimplemented("CompileTimeConstantHandler" + | |
| 113 "writeJsCodeForVariable", | |
| 114 node: element.parseNode(compiler)); | |
| 115 } | |
| 116 return buffer; | |
| 117 } | |
| 99 | 118 |
| 100 // TODO(floitsch): support more values. | 119 /** |
| 101 compiler.unimplemented("CompileTimeConstantHandler.getJsCodeForVariable", | 120 * Write the contents of the quoted string to a [StringBuffer] in |
| 102 node: element.parseNode(compiler)); | 121 * a form that is valid as JavaScript string literal content. |
| 122 * The string is assumed quoted by single quote characters. | |
| 123 */ | |
| 124 static void writeEscapedString(DartString string, | |
| 125 StringBuffer buffer, | |
| 126 void cancel(String reason)) { | |
| 127 Iterator<int> iterator = string.iterator(); | |
| 128 while (iterator.hasNext()) { | |
| 129 int code = iterator.next(); | |
| 130 if (code === $SQ) { | |
| 131 buffer.add(@"\'"); | |
| 132 } else if (code === $LF) { | |
| 133 buffer.add(@'\n'); | |
| 134 } else if (code === $CR) { | |
| 135 buffer.add(@'\r'); | |
| 136 } else if (code === $LS) { | |
| 137 // This Unicode line terminator and $PS are invalid in JS string | |
| 138 // literals. | |
| 139 buffer.add(@'\u2028'); | |
| 140 } else if (code === $PS) { | |
| 141 buffer.add(@'\u2029'); | |
| 142 } else if (code === $BACKSLASH) { | |
| 143 buffer.add(@'\\'); | |
| 144 } else { | |
| 145 if (code > 0xffff) { | |
| 146 cancel("Unhandled non-BMP character: U+" + code.toRadixString(16)); | |
| 147 } | |
| 148 // TODO(lrn): Consider whether all codes above 0x7f really need to | |
| 149 // be escaped. We build a Dart string here, so it should be a literal | |
| 150 // stage that converts it to, e.g., UTF-8 for a JS interpreter. | |
| 151 if (code < 0x20) { | |
| 152 buffer.add(@'\x'); | |
| 153 if (code < 0x10) buffer.add('0'); | |
| 154 buffer.add(code.toRadixString(16)); | |
| 155 } else if (code >= 0x80) { | |
| 156 if (code < 0x100) { | |
| 157 buffer.add(@'\x'); | |
| 158 buffer.add(code.toRadixString(16)); | |
| 159 } else { | |
| 160 buffer.add(@'\u'); | |
| 161 if (code < 0x1000) { | |
| 162 buffer.add('0'); | |
| 163 } | |
| 164 buffer.add(code.toRadixString(16)); | |
| 165 } | |
| 166 } else { | |
| 167 buffer.add(new String.fromCharCodes(<int>[code])); | |
| 168 } | |
| 169 } | |
| 170 } | |
| 103 } | 171 } |
| 104 } | 172 } |
| 105 | 173 |
| 106 class CompileTimeConstantEvaluator extends AbstractVisitor { | 174 class CompileTimeConstantEvaluator extends AbstractVisitor { |
| 107 final CompileTimeConstantHandler constantHandler; | 175 final CompileTimeConstantHandler constantHandler; |
| 108 final TreeElements definitions; | 176 final TreeElements definitions; |
| 109 final Compiler compiler; | 177 final Compiler compiler; |
| 110 | 178 |
| 111 CompileTimeConstantEvaluator(this.constantHandler, | 179 CompileTimeConstantEvaluator(this.constantHandler, |
| 112 this.definitions, | 180 this.definitions, |
| 113 this.compiler); | 181 this.compiler); |
| 114 | 182 |
| 115 evaluate(Node node) { | 183 evaluate(Node node) { |
| 116 return node.accept(this); | 184 return node.accept(this); |
| 117 } | 185 } |
| 118 | 186 |
| 119 visitNode(Node node) { | 187 visitNode(Node node) { |
| 120 compiler.unimplemented("CompileTimeConstantEvaluator", node: node); | 188 compiler.unimplemented("CompileTimeConstantEvaluator", node: node); |
| 121 } | 189 } |
| 122 | 190 |
| 123 visitLiteral(Literal literal) { | 191 visitLiteral(Literal literal) { |
| 192 if (literal is LiteralString) { | |
| 193 assert(literal.asLiteralString().isValidated()); | |
| 194 return literal.asLiteralString().dartString; | |
| 195 } | |
| 124 return literal.value; | 196 return literal.value; |
| 125 } | 197 } |
| 126 | 198 |
| 127 visitSend(Send send) { | 199 visitSend(Send send) { |
| 128 Element element = definitions[send]; | 200 Element element = definitions[send]; |
| 129 if (element !== null && element.kind == ElementKind.FIELD) { | 201 if (element !== null && element.kind == ElementKind.FIELD) { |
| 130 if (element.isInstanceMember() || | 202 if (element.isInstanceMember() || |
| 131 element.modifiers === null || | 203 element.modifiers === null || |
| 132 !element.modifiers.isFinal()) { | 204 !element.modifiers.isFinal()) { |
| 133 error(element); | 205 error(element); |
| 134 } | 206 } |
| 135 return constantHandler.compileVariable(element); | 207 return constantHandler.compileVariable(element); |
| 136 } | 208 } |
| 137 return super.visitSend(send); | 209 return super.visitSend(send); |
| 138 } | 210 } |
| 139 | 211 |
| 140 error(Element element) { | 212 error(Element element) { |
| 141 // TODO(floitsch): get the list of constants that are currently compiled | 213 // TODO(floitsch): get the list of constants that are currently compiled |
| 142 // and present some kind of stack-trace. | 214 // and present some kind of stack-trace. |
| 143 MessageKind kind = MessageKind.NOT_A_COMPILE_TIME_CONSTANT; | 215 MessageKind kind = MessageKind.NOT_A_COMPILE_TIME_CONSTANT; |
| 144 List arguments = [element.name]; | 216 List arguments = [element.name]; |
| 145 Node node = element.parseNode(compiler); | 217 Node node = element.parseNode(compiler); |
| 146 compiler.reportError(node, new CompileTimeConstantError(kind, arguments)); | 218 compiler.reportError(node, new CompileTimeConstantError(kind, arguments)); |
| 147 } | 219 } |
| 148 } | 220 } |
| OLD | NEW |