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

Unified Diff: dart/frog/leg/compile_time_constants.dart

Issue 9315028: Support default values for optional parameters. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' 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 | dart/frog/leg/compiler.dart » ('j') | dart/frog/leg/compiler.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dart/frog/leg/compile_time_constants.dart
===================================================================
--- dart/frog/leg/compile_time_constants.dart (revision 3786)
+++ dart/frog/leg/compile_time_constants.dart (working copy)
@@ -3,16 +3,17 @@
// BSD-style license that can be found in the LICENSE file.
/**
- * The [CompileTimeConstantHandler] keeps track of compile-time constants, and
- * initializations of global and static fields.
+ * The [CompileTimeConstantHandler] keeps track of compile-time constants,
+ * initializations of global and static fields, and default values of
+ * optional parameters.
*/
class CompileTimeConstantHandler extends CompilerTask {
// Contains the initial value of fields. Must contain all static and global
// initializations of used fields. May contain caches for instance fields.
- final Map<VariableElement, Dynamic> initialFieldValues;
+ final Map<VariableElement, Dynamic> initialVariableValues;
CompileTimeConstantHandler(Compiler compiler)
- : initialFieldValues = new Map<VariableElement, Dynamic>(),
+ : initialVariableValues = new Map<VariableElement, Dynamic>(),
super(compiler);
String get name() => 'CompileTimeConstantHandler';
@@ -24,24 +25,25 @@
* static field.
*/
void compileWorkItem(WorkItem work) {
- assert(work.element.kind == ElementKind.FIELD);
+ assert(work.element.kind == ElementKind.FIELD
+ || work.element.kind == ElementKind.PARAMETER);
VariableElement element = work.element;
// Shortcut if it has already been compiled.
- if (initialFieldValues.containsKey(element)) return;
- compileFieldWithDefinitions(element, work.resolutionTree);
+ if (initialVariableValues.containsKey(element)) return;
+ compileVariableWithDefinitions(element, work.resolutionTree);
}
- compileField(VariableElement element) {
- if (initialFieldValues.containsKey(element)) {
- return initialFieldValues[element];
+ compileVariable(VariableElement element) {
+ if (initialVariableValues.containsKey(element)) {
+ return initialVariableValues[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);
+ return compileVariableWithDefinitions(element, definitions);
}
- compileFieldWithDefinitions(VariableElement element,
+ compileVariableWithDefinitions(VariableElement element,
TreeElements definitions) {
floitsch 2012/02/01 14:35:39 indent.
ngeoffray 2012/02/01 15:54:59 Done.
return measure(() {
Node node = element.parseNode(compiler);
@@ -57,7 +59,7 @@
new CompileTimeConstantEvaluator(this, definitions, compiler);
value = evaluator.evaluate(right);
}
- initialFieldValues[element] = value;
+ initialVariableValues[element] = value;
return value;
});
}
@@ -68,8 +70,10 @@
* other.
*/
List<VariableElement> getStaticNonFinalFieldsForEmission() {
- return initialFieldValues.getKeys().filter((element) {
- return !element.isInstanceMember() && !element.modifiers.isFinal();
+ return initialVariableValues.getKeys().filter((element) {
+ return element.kind == ElementKind.FIELD
+ && !element.isInstanceMember()
+ && !element.modifiers.isFinal();
});
}
@@ -79,26 +83,23 @@
* other.
*/
List<VariableElement> getStaticFinalFieldsForEmission() {
- return initialFieldValues.getKeys().filter((element) {
- return !element.isInstanceMember() && element.modifiers.isFinal();
+ return initialVariableValues.getKeys().filter((element) {
+ return element.kind == ElementKind.FIELD
+ && !element.isInstanceMember()
+ && element.modifiers.isFinal();
});
}
- void emitJsCodeForField(VariableElement element, StringBuffer buffer) {
- var value = initialFieldValues[element];
- if (value === null) {
- buffer.add("(void 0)");
- } else if (value is num) {
- buffer.add("$value");
- } else if (value === true) {
- buffer.add("true");
- } else if (value === false) {
- buffer.add("false");
- } else {
- // TODO(floitsch): support more values.
- compiler.unimplemented("CompileTimeConstantHandler.emitJsCodeForField",
- node: element.parseNode(compiler));
- }
+ String getJsCodeForVariable(VariableElement element) {
+ var value = initialVariableValues[element];
+ if (value === null) return "(void 0)";
+ if (value is num) return "$value";
+ if (value === true) return "true";
+ if (value === false) return "false";
+
+ // TODO(floitsch): support more values.
+ compiler.unimplemented("CompileTimeConstantHandler.getJsCodeForVariable",
+ node: element.parseNode(compiler));
}
}
@@ -131,7 +132,7 @@
!element.modifiers.isFinal()) {
error(element);
}
- return constantHandler.compileField(element);
+ return constantHandler.compileVariable(element);
}
return super.visitSend(send);
}
« no previous file with comments | « no previous file | dart/frog/leg/compiler.dart » ('j') | dart/frog/leg/compiler.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698