OLD | NEW |
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 Loading... |
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 Statement* Parser::ParseSwitchStatement(ZoneList<const AstRawString*>* labels, | 2967 SwitchStatement* Parser::ParseSwitchStatement( |
2968 bool* ok) { | 2968 ZoneList<const AstRawString*>* labels, 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 // } | |
2980 | 2971 |
2981 Block* switch_block = | 2972 SwitchStatement* statement = |
2982 factory()->NewBlock(NULL, 2, true, RelocInfo::kNoPosition); | 2973 factory()->NewSwitchStatement(labels, peek_position()); |
2983 int switch_pos = peek_position(); | 2974 Target target(&this->target_stack_, statement); |
2984 | 2975 |
2985 Expect(Token::SWITCH, CHECK_OK); | 2976 Expect(Token::SWITCH, CHECK_OK); |
2986 Expect(Token::LPAREN, CHECK_OK); | 2977 Expect(Token::LPAREN, CHECK_OK); |
2987 Expression* tag = ParseExpression(true, CHECK_OK); | 2978 Expression* tag = ParseExpression(true, CHECK_OK); |
2988 Expect(Token::RPAREN, CHECK_OK); | 2979 Expect(Token::RPAREN, CHECK_OK); |
2989 | 2980 |
2990 Variable* tag_variable = | 2981 bool default_seen = false; |
2991 scope_->NewTemporary(ast_value_factory()->dot_switch_tag_string()); | 2982 ZoneList<CaseClause*>* cases = new(zone()) ZoneList<CaseClause*>(4, zone()); |
2992 Assignment* tag_assign = factory()->NewAssignment( | 2983 Expect(Token::LBRACE, CHECK_OK); |
2993 Token::ASSIGN, factory()->NewVariableProxy(tag_variable), tag, | 2984 while (peek() != Token::RBRACE) { |
2994 tag->position()); | 2985 CaseClause* clause = ParseCaseClause(&default_seen, CHECK_OK); |
2995 Statement* tag_statement = | 2986 cases->Add(clause, zone()); |
2996 factory()->NewExpressionStatement(tag_assign, RelocInfo::kNoPosition); | |
2997 switch_block->AddStatement(tag_statement, zone()); | |
2998 | |
2999 Block* cases_block = | |
3000 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); | |
3001 Scope* cases_scope = NewScope(scope_, BLOCK_SCOPE); | |
3002 | |
3003 SwitchStatement* switch_statement = | |
3004 factory()->NewSwitchStatement(labels, switch_pos); | |
3005 | |
3006 cases_scope->set_start_position(scanner()->location().beg_pos); | |
3007 { | |
3008 BlockState cases_block_state(&scope_, cases_scope); | |
3009 Target target(&this->target_stack_, switch_statement); | |
3010 | |
3011 Expression* tag_read = factory()->NewVariableProxy(tag_variable); | |
3012 | |
3013 bool default_seen = false; | |
3014 ZoneList<CaseClause*>* cases = | |
3015 new (zone()) ZoneList<CaseClause*>(4, zone()); | |
3016 Expect(Token::LBRACE, CHECK_OK); | |
3017 while (peek() != Token::RBRACE) { | |
3018 CaseClause* clause = ParseCaseClause(&default_seen, CHECK_OK); | |
3019 cases->Add(clause, zone()); | |
3020 } | |
3021 switch_statement->Initialize(tag_read, cases); | |
3022 cases_block->AddStatement(switch_statement, zone()); | |
3023 } | 2987 } |
3024 Expect(Token::RBRACE, CHECK_OK); | 2988 Expect(Token::RBRACE, CHECK_OK); |
3025 | 2989 |
3026 cases_scope->set_end_position(scanner()->location().end_pos); | 2990 if (statement) statement->Initialize(tag, cases); |
3027 cases_scope = cases_scope->FinalizeBlockScope(); | 2991 return statement; |
3028 cases_block->set_scope(cases_scope); | |
3029 | |
3030 switch_block->AddStatement(cases_block, zone()); | |
3031 | |
3032 return switch_block; | |
3033 } | 2992 } |
3034 | 2993 |
3035 | 2994 |
3036 Statement* Parser::ParseThrowStatement(bool* ok) { | 2995 Statement* Parser::ParseThrowStatement(bool* ok) { |
3037 // ThrowStatement :: | 2996 // ThrowStatement :: |
3038 // 'throw' Expression ';' | 2997 // 'throw' Expression ';' |
3039 | 2998 |
3040 Expect(Token::THROW, CHECK_OK); | 2999 Expect(Token::THROW, CHECK_OK); |
3041 int pos = position(); | 3000 int pos = position(); |
3042 if (scanner()->HasAnyLineTerminatorBeforeNext()) { | 3001 if (scanner()->HasAnyLineTerminatorBeforeNext()) { |
(...skipping 3040 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6083 Expression* Parser::SpreadCallNew(Expression* function, | 6042 Expression* Parser::SpreadCallNew(Expression* function, |
6084 ZoneList<v8::internal::Expression*>* args, | 6043 ZoneList<v8::internal::Expression*>* args, |
6085 int pos) { | 6044 int pos) { |
6086 args->InsertAt(0, function, zone()); | 6045 args->InsertAt(0, function, zone()); |
6087 | 6046 |
6088 return factory()->NewCallRuntime( | 6047 return factory()->NewCallRuntime( |
6089 ast_value_factory()->reflect_construct_string(), NULL, args, pos); | 6048 ast_value_factory()->reflect_construct_string(), NULL, args, pos); |
6090 } | 6049 } |
6091 } // namespace internal | 6050 } // namespace internal |
6092 } // namespace v8 | 6051 } // namespace v8 |
OLD | NEW |