Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(296)

Unified Diff: frog/leg/compile_time_constants.dart

Issue 9211005: Support static/global field initializations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Added test. Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | frog/leg/resolver.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: frog/leg/compile_time_constants.dart
diff --git a/frog/leg/compile_time_constants.dart b/frog/leg/compile_time_constants.dart
index 09463d45264f880d814a60fb0fffa9c462730266..c2226ef7a712f72baed42370e510f5486627d6da 100644
--- a/frog/leg/compile_time_constants.dart
+++ b/frog/leg/compile_time_constants.dart
@@ -35,6 +35,8 @@ class CompileTimeConstantHandler extends CompilerTask {
if (initialFieldValues.containsKey(element)) {
return initialFieldValues[element];
}
+ // TODO(floitsch): keep track of currently compiling elements so that we
+ // don't end up in an infinite loop: final x = y; final y = x;
TreeElements definitions = compiler.analyzeElement(element);
return compileFieldWithDefinitions(element, definitions);
}
@@ -50,8 +52,10 @@ class CompileTimeConstantHandler extends CompilerTask {
// No initial value.
value = null;
} else {
- compiler.unimplemented("CTC for static initialized fields.",
- node: node);
+ Node right = node.arguments.head;
+ CompileTimeConstantEvaluator evaluator =
+ new CompileTimeConstantEvaluator(this, definitions, compiler);
kasperl 2012/01/16 10:44:04 4 space indent.
floitsch 2012/01/16 15:07:18 Done.
+ value = evaluator.evaluate(right);
}
initialFieldValues[element] = value;
return value;
@@ -82,8 +86,51 @@ class CompileTimeConstantHandler extends CompilerTask {
void emitJsCodeForField(VariableElement element, StringBuffer buffer) {
var value = initialFieldValues[element];
- // TODO(floitsch): support more values.
- assert(value === null);
- buffer.add("(void 0)");
+ if (value === null) {
+ buffer.add("(void 0)");
+ } else if (value === true) {
+ buffer.add("true");
+ } else if (value === false) {
+ buffer.add("false");
+ } else if (value is num) {
+ buffer.add("$value");
+ } else {
+ // TODO(floitsch): support more values.
+ compiler.unimplemented("CompileTimeConstantHandler.emitJsCodeForField",
+ node: element.parseNode(compiler, compiler));
+ }
+ }
+}
+
+class CompileTimeConstantEvaluator extends AbstractVisitor {
+ final CompileTimeConstantHandler compileTimeConstantHandler;
kasperl 2012/01/16 10:44:04 Maybe shorten the name to constantHandler? The con
floitsch 2012/01/16 15:07:18 Done.
+ final TreeElements definitions;
+ final Compiler compiler;
+
+ CompileTimeConstantEvaluator(this.compileTimeConstantHandler,
+ this.definitions,
+ this.compiler);
+
+ evaluate(Node node) {
+ return node.accept(this);
+ }
+
+ visitNode(Node node) {
+ compiler.unimplemented("CompileTimeConstantEvaluator", node: node);
+ }
+
+ visitLiteral(Literal literal) {
+ return literal.value;
+ }
+
+ visitSend(Send send) {
+ Element element = definitions[send];
+ if (element !== null &&
+ !element.isInstanceMember() &&
+ element.kind == ElementKind.FIELD) {
+ // TODO(floitsch): make sure the field is final.
+ return compileTimeConstantHandler.compileField(element);
+ }
+ return super.visitSend(send);
}
}
« no previous file with comments | « no previous file | frog/leg/resolver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698