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

Unified Diff: runtime/vm/parser.cc

Issue 11265047: Implement const expressions for local variables (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 1 month 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 | « runtime/vm/object.cc ('k') | runtime/vm/raw_object.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/parser.cc
===================================================================
--- runtime/vm/parser.cc (revision 14713)
+++ runtime/vm/parser.cc (working copy)
@@ -4851,6 +4851,10 @@
ConsumeToken();
AstNode* expr = ParseExpr(is_const, kConsumeCascades);
initialization = new StoreLocalNode(assign_pos, variable, expr);
+ if (is_const) {
+ ASSERT(expr->IsLiteralNode());
+ variable->SetConstValue(expr->AsLiteralNode()->literal());
+ }
} else if (is_final || is_const) {
ErrorMsg(ident_pos,
"missing initialization of 'final' or 'const' variable");
@@ -8142,7 +8146,11 @@
LocalVariable* local = LookupLocalScope(ident);
if (local != NULL) {
if (node != NULL) {
- *node = new LoadLocalNode(ident_pos, local);
+ if (local->IsConst()) {
+ *node = new LiteralNode(ident_pos, *local->ConstValue());
+ } else {
+ *node = new LoadLocalNode(ident_pos, local);
+ }
}
return true;
}
@@ -9658,6 +9666,9 @@
const Instance& Parser::EvaluateConstExpr(AstNode* expr) {
if (expr->IsLiteralNode()) {
return expr->AsLiteralNode()->literal();
+ } else if (expr->IsLoadLocalNode() &&
+ expr->AsLoadLocalNode()->local().IsConst()) {
+ return *expr->AsLoadLocalNode()->local().ConstValue();
} else {
ASSERT(expr->EvalConstExpr() != NULL);
ReturnNode* ret = new ReturnNode(expr->token_pos(), expr);
« no previous file with comments | « runtime/vm/object.cc ('k') | runtime/vm/raw_object.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698