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

Side by Side Diff: src/parser.cc

Issue 1293283002: Add a separate scope for switch (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fix double evaluation of tag expression Created 5 years, 4 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 unified diff | Download patch
« no previous file with comments | « src/parser.h ('k') | test/mjsunit/regress/regress-4377.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/parser.h" 5 #include "src/parser.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast.h" 8 #include "src/ast.h"
9 #include "src/ast-literal-reindexer.h" 9 #include "src/ast-literal-reindexer.h"
10 #include "src/bailout-reason.h" 10 #include "src/bailout-reason.h"
(...skipping 2946 matching lines...) Expand 10 before | Expand all | Expand 10 after
2957 peek() != Token::RBRACE) { 2957 peek() != Token::RBRACE) {
2958 ReportMessageAt(scanner()->location(), 2958 ReportMessageAt(scanner()->location(),
2959 MessageTemplate::kStrongSwitchFallthrough); 2959 MessageTemplate::kStrongSwitchFallthrough);
2960 *ok = false; 2960 *ok = false;
2961 return NULL; 2961 return NULL;
2962 } 2962 }
2963 return factory()->NewCaseClause(label, statements, pos); 2963 return factory()->NewCaseClause(label, statements, pos);
2964 } 2964 }
2965 2965
2966 2966
2967 SwitchStatement* Parser::ParseSwitchStatement( 2967 Statement* Parser::ParseSwitchStatement(ZoneList<const AstRawString*>* labels,
2968 ZoneList<const AstRawString*>* labels, bool* ok) { 2968 bool* ok) {
2969 // SwitchStatement :: 2969 // SwitchStatement ::
2970 // 'switch' '(' Expression ')' '{' CaseClause* '}' 2970 // 'switch' '(' Expression ')' '{' CaseClause* '}'
2971 // In order to get the CaseClauses to execute in their own lexical scope,
2972 // but without requiring downstream code to have special scope handling
2973 // code for switch statements, desugar into blocks as follows:
2974 // { // To group the statements--harmless to evaluate Expression in scope
2975 // .tag_variable = Expression;
2976 // { // To give CaseClauses a scope
2977 // switch (.tag_variable) { CaseClause* }
2978 // }
2979 // }
2971 2980
2972 SwitchStatement* statement = 2981 Block* switch_block =
2973 factory()->NewSwitchStatement(labels, peek_position()); 2982 factory()->NewBlock(labels, 2, true, RelocInfo::kNoPosition);
adamk 2015/08/21 01:19:08 Seems weird that you pass labels both here and to
Dan Ehrenberg 2015/08/21 23:06:14 Done
2974 Target target(&this->target_stack_, statement); 2983 Scope* switch_scope = NewScope(scope_, BLOCK_SCOPE);
adamk 2015/08/21 01:19:08 As discussed, I don't think you need this scope (a
Dan Ehrenberg 2015/08/21 23:06:14 Removed it.
2984 int switch_pos = peek_position();
2975 2985
2976 Expect(Token::SWITCH, CHECK_OK); 2986 {
2977 Expect(Token::LPAREN, CHECK_OK); 2987 BlockState switch_block_state(&scope_, switch_scope);
2978 Expression* tag = ParseExpression(true, CHECK_OK); 2988 // This target exists in case the tag has a do-expression
adamk 2015/08/21 01:19:08 This comment likely goes away if you simplify the
Dan Ehrenberg 2015/08/21 23:06:14 Done
2979 Expect(Token::RPAREN, CHECK_OK); 2989 Target target(&this->target_stack_, switch_block);
2980 2990
2981 bool default_seen = false; 2991 Expect(Token::SWITCH, CHECK_OK);
2982 ZoneList<CaseClause*>* cases = new(zone()) ZoneList<CaseClause*>(4, zone()); 2992 Expect(Token::LPAREN, CHECK_OK);
2983 Expect(Token::LBRACE, CHECK_OK); 2993 Expression* tag = ParseExpression(true, CHECK_OK);
2984 while (peek() != Token::RBRACE) { 2994 Expect(Token::RPAREN, CHECK_OK);
2985 CaseClause* clause = ParseCaseClause(&default_seen, CHECK_OK); 2995
2986 cases->Add(clause, zone()); 2996 Variable* tag_variable =
2997 scope_->NewTemporary(ast_value_factory()->dot_switch_tag_string());
2998 Assignment* tag_assign = factory()->NewAssignment(
2999 Token::ASSIGN, factory()->NewVariableProxy(tag_variable), tag,
3000 tag->position());
3001 Statement* tag_statement =
3002 factory()->NewExpressionStatement(tag_assign, RelocInfo::kNoPosition);
3003 switch_block->AddStatement(tag_statement, zone());
3004
3005 Block* cases_block =
3006 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition);
3007 Scope* cases_scope = NewScope(scope_, BLOCK_SCOPE);
3008
3009 SwitchStatement* switch_statement =
3010 factory()->NewSwitchStatement(labels, switch_pos);
adamk 2015/08/21 01:19:07 See above, I'd drop the labels here (pass NULL).
Dan Ehrenberg 2015/08/21 23:06:14 Passed the labels here, as you suggested above.
3011
3012 ZoneList<CaseClause*>* cases;
3013 cases_scope->set_start_position(scanner()->location().beg_pos);
3014 {
3015 BlockState cases_block_state(&scope_, cases_scope);
3016 Target target(&this->target_stack_, switch_statement);
3017
3018 Expression* tag_read = factory()->NewVariableProxy(tag_variable);
3019
3020 bool default_seen = false;
3021 cases = new (zone()) ZoneList<CaseClause*>(4, zone());
adamk 2015/08/21 01:19:08 Can you move this initialization up to where cases
Dan Ehrenberg 2015/08/21 23:06:14 Moved the declaration down to here.
3022 Expect(Token::LBRACE, CHECK_OK);
3023 while (peek() != Token::RBRACE) {
3024 CaseClause* clause = ParseCaseClause(&default_seen, CHECK_OK);
3025 cases->Add(clause, zone());
3026 }
3027 switch_statement->Initialize(tag_read, cases);
3028 cases_block->AddStatement(switch_statement, zone());
3029 }
3030 cases_scope->set_end_position(scanner()->location().end_pos);
3031 cases_scope = cases_scope->FinalizeBlockScope();
3032 cases_block->set_scope(cases_scope);
3033
3034 Expect(Token::RBRACE, CHECK_OK);
adamk 2015/08/21 01:19:08 I think this should move above the scope stuff jus
Dan Ehrenberg 2015/08/21 23:06:14 Done
3035
3036 switch_block->AddStatement(cases_block, zone());
2987 } 3037 }
2988 Expect(Token::RBRACE, CHECK_OK); 3038 switch_scope->set_end_position(scanner()->location().end_pos);
3039 switch_scope = switch_scope->FinalizeBlockScope();
3040 switch_block->set_scope(switch_scope);
2989 3041
2990 if (statement) statement->Initialize(tag, cases); 3042 return switch_block;
2991 return statement;
2992 } 3043 }
2993 3044
2994 3045
2995 Statement* Parser::ParseThrowStatement(bool* ok) { 3046 Statement* Parser::ParseThrowStatement(bool* ok) {
2996 // ThrowStatement :: 3047 // ThrowStatement ::
2997 // 'throw' Expression ';' 3048 // 'throw' Expression ';'
2998 3049
2999 Expect(Token::THROW, CHECK_OK); 3050 Expect(Token::THROW, CHECK_OK);
3000 int pos = position(); 3051 int pos = position();
3001 if (scanner()->HasAnyLineTerminatorBeforeNext()) { 3052 if (scanner()->HasAnyLineTerminatorBeforeNext()) {
(...skipping 3040 matching lines...) Expand 10 before | Expand all | Expand 10 after
6042 Expression* Parser::SpreadCallNew(Expression* function, 6093 Expression* Parser::SpreadCallNew(Expression* function,
6043 ZoneList<v8::internal::Expression*>* args, 6094 ZoneList<v8::internal::Expression*>* args,
6044 int pos) { 6095 int pos) {
6045 args->InsertAt(0, function, zone()); 6096 args->InsertAt(0, function, zone());
6046 6097
6047 return factory()->NewCallRuntime( 6098 return factory()->NewCallRuntime(
6048 ast_value_factory()->reflect_construct_string(), NULL, args, pos); 6099 ast_value_factory()->reflect_construct_string(), NULL, args, pos);
6049 } 6100 }
6050 } // namespace internal 6101 } // namespace internal
6051 } // namespace v8 6102 } // namespace v8
OLDNEW
« no previous file with comments | « src/parser.h ('k') | test/mjsunit/regress/regress-4377.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698