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

Unified Diff: sdk/lib/_internal/compiler/implementation/compile_time_constants.dart

Issue 25478009: Handle compile-time constness for conditionals and type literal calls. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated cf. comments Created 7 years, 2 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 | sdk/lib/_internal/compiler/implementation/js_backend/backend.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/compiler/implementation/compile_time_constants.dart
diff --git a/sdk/lib/_internal/compiler/implementation/compile_time_constants.dart b/sdk/lib/_internal/compiler/implementation/compile_time_constants.dart
index 5bd59db5216150fb77a0aa3f6521d83d655e815e..8ae51fadd20f237e8c5e84d0ab72c067bcb4fa50 100644
--- a/sdk/lib/_internal/compiler/implementation/compile_time_constants.dart
+++ b/sdk/lib/_internal/compiler/implementation/compile_time_constants.dart
@@ -91,9 +91,6 @@ class ConstantHandler extends CompilerTask {
TreeElements definitions,
{bool isConst: false}) {
return measure(() {
- // Initializers for parameters must be const.
- isConst = isConst || element.modifiers.isConst()
- || !Elements.isStaticOrTopLevel(element);
if (!isConst && lazyStatics.contains(element)) return null;
Node node = element.parseNode(compiler);
@@ -151,9 +148,17 @@ class ConstantHandler extends CompilerTask {
{bool isConst: false}) {
return measure(() {
assert(node != null);
+ Constant constant = definitions.getConstant(node);
+ if (constant != null) {
+ return constant;
+ }
CompileTimeConstantEvaluator evaluator = new CompileTimeConstantEvaluator(
this, definitions, compiler, isConst: isConst);
- return evaluator.evaluate(node);
+ constant = evaluator.evaluate(node);
+ if (constant != null) {
+ definitions.setConstant(node, constant);
+ }
+ return constant;
});
}
@@ -397,8 +402,6 @@ class CompileTimeConstantEvaluator extends Visitor {
Constant makeTypeConstant(Element element) {
DartType elementType = element.computeType(compiler).asRaw();
- compiler.backend.registerTypeLiteral(
- element, compiler.enqueuer.codegen, elements);
DartType constantType =
compiler.backend.typeImplementation.computeType(compiler);
return new TypeConstant(elementType, constantType);
@@ -419,6 +422,7 @@ class CompileTimeConstantEvaluator extends Visitor {
}
if (result != null) return result;
} else if (Elements.isClass(element) || Elements.isTypedef(element)) {
+ assert(elements.isTypeLiteral(send));
return makeTypeConstant(element);
} else if (send.receiver != null) {
// Fall through to error handling.
@@ -437,7 +441,10 @@ class CompileTimeConstantEvaluator extends Visitor {
Constant result = constantSystem.identity.fold(left, right);
if (result != null) return result;
} else if (Elements.isClass(element) || Elements.isTypedef(element)) {
- return makeTypeConstant(element);
+ // The node itself is not a constant but we register the selector (the
+ // identifier that refers to the class/typedef) as a constant.
+ Constant typeConstant = makeTypeConstant(element);
+ elements.setConstant(send.selector, typeConstant);
}
return signalNotCompileTimeConstant(send);
} else if (send.isPrefix) {
@@ -555,6 +562,25 @@ class CompileTimeConstantEvaluator extends Visitor {
return signalNotCompileTimeConstant(send);
}
+ Constant visitConditional(Conditional node) {
+ Constant condition = evaluate(node.condition);
+ if (condition == null) {
+ return null;
+ } else if (!condition.isBool()) {
+ DartType conditionType = condition.computeType(compiler);
+ if (isEvaluatingConstant) {
+ compiler.reportFatalError(
+ node.condition, MessageKind.NOT_ASSIGNABLE.error,
+ {'fromType': conditionType, 'toType': compiler.boolClass.rawType});
+ }
+ return null;
+ }
+ Constant thenExpression = evaluate(node.thenExpression);
+ Constant elseExpression = evaluate(node.elseExpression);
+ BoolConstant boolCondition = condition;
+ return boolCondition.value ? thenExpression : elseExpression;
+ }
+
Constant visitSendSet(SendSet node) {
return signalNotCompileTimeConstant(node);
}
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/js_backend/backend.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698