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

Unified Diff: runtime/vm/parser.cc

Issue 1493693002: Make ?? a compile-time constant operator. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Fix test, runs in dart2js and analyzer. Created 5 years 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
Index: runtime/vm/parser.cc
diff --git a/runtime/vm/parser.cc b/runtime/vm/parser.cc
index 26f2ad7adb095eb5f22a4e1473c56684e15d4ad5..4df590bfcf42f38e8056d849d672cb839084346e 100644
--- a/runtime/vm/parser.cc
+++ b/runtime/vm/parser.cc
@@ -10552,6 +10552,21 @@ AstNode* Parser::OptimizeBinaryOpNode(intptr_t op_pos,
}
if (binary_op == Token::kIFNULL) {
// Handle a ?? b.
+ if (lhs->IsPotentiallyConst() && lhs->EvalConstExpr() != NULL) {
Ivan Posva 2015/12/03 21:29:04 () ditto: Please keep the style of the file.
Lasse Reichstein Nielsen 2015/12/04 12:11:46 Done.
+ // If we know at compile time whether the lhs is null
+ // and side-effect free, just reduce it to either the lhs or rhs.
+ // This enables ?? to be compile-time constant.
+ Instance& expr_value = Instance::ZoneHandle(Z);
+ intptr_t lhs_pos = lhs->token_pos();
+ if (!GetCachedConstant(lhs_pos, &expr_value)) {
+ expr_value = EvaluateConstExpr(lhs_pos, lhs).raw();
+ CacheConstantValue(lhs_pos, expr_value);
+ }
+ if (expr_value.IsNull()) return rhs;
Ivan Posva 2015/12/03 21:29:04 ditto
Lasse Reichstein Nielsen 2015/12/04 12:11:46 Done.
+ return new(Z) LiteralNode(lhs_pos, expr_value);
+ }
+ // Otherwise create a temporary variable for the lhs so we can both
+ // check it against null and return it if necessary.
LetNode* result = new(Z) LetNode(op_pos);
LocalVariable* left_temp = result->AddInitializer(lhs);
const intptr_t no_pos = Scanner::kNoSourcePos;
« runtime/vm/ast.cc ('K') | « runtime/vm/ast.cc ('k') | tests/language/const_qq_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698