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

Side by Side Diff: src/parser.cc

Issue 1416753009: [parser] early error when declaration Pattern missing Initializer (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: comments addressed Created 5 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 unified diff | Download patch
« no previous file with comments | « src/messages.h ('k') | src/preparser.h » ('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 2513 matching lines...) Expand 10 before | Expand all | Expand 10 after
2524 bool first_declaration = true; 2524 bool first_declaration = true;
2525 int bindings_start = peek_position(); 2525 int bindings_start = peek_position();
2526 bool is_for_iteration_variable; 2526 bool is_for_iteration_variable;
2527 do { 2527 do {
2528 if (fni_ != NULL) fni_->Enter(); 2528 if (fni_ != NULL) fni_->Enter();
2529 2529
2530 // Parse name. 2530 // Parse name.
2531 if (!first_declaration) Consume(Token::COMMA); 2531 if (!first_declaration) Consume(Token::COMMA);
2532 2532
2533 Expression* pattern; 2533 Expression* pattern;
2534 int decl_pos = peek_position();
2534 { 2535 {
2535 ExpressionClassifier pattern_classifier; 2536 ExpressionClassifier pattern_classifier;
2536 Token::Value next = peek(); 2537 Token::Value next = peek();
2537 pattern = ParsePrimaryExpression(&pattern_classifier, ok); 2538 pattern = ParsePrimaryExpression(&pattern_classifier, ok);
2538 if (!*ok) return; 2539 if (!*ok) return;
2539 ValidateBindingPattern(&pattern_classifier, ok); 2540 ValidateBindingPattern(&pattern_classifier, ok);
2540 if (!*ok) return; 2541 if (!*ok) return;
2541 if (IsLexicalVariableMode(parsing_result->descriptor.mode)) { 2542 if (IsLexicalVariableMode(parsing_result->descriptor.mode)) {
2542 ValidateLetPattern(&pattern_classifier, ok); 2543 ValidateLetPattern(&pattern_classifier, ok);
2543 if (!*ok) return; 2544 if (!*ok) return;
2544 } 2545 }
2545 if (!allow_harmony_destructuring() && !pattern->IsVariableProxy()) { 2546 if (!allow_harmony_destructuring() && !pattern->IsVariableProxy()) {
2546 ReportUnexpectedToken(next); 2547 ReportUnexpectedToken(next);
2547 *ok = false; 2548 *ok = false;
2548 return; 2549 return;
2549 } 2550 }
2550 } 2551 }
2551 2552
2553 bool is_pattern = pattern->IsObjectLiteral() || pattern->IsArrayLiteral();
2554
2552 Scanner::Location variable_loc = scanner()->location(); 2555 Scanner::Location variable_loc = scanner()->location();
2553 const AstRawString* single_name = 2556 const AstRawString* single_name =
2554 pattern->IsVariableProxy() ? pattern->AsVariableProxy()->raw_name() 2557 pattern->IsVariableProxy() ? pattern->AsVariableProxy()->raw_name()
2555 : nullptr; 2558 : nullptr;
2556 if (single_name != nullptr) { 2559 if (single_name != nullptr) {
2557 if (fni_ != NULL) fni_->PushVariableName(single_name); 2560 if (fni_ != NULL) fni_->PushVariableName(single_name);
2558 } 2561 }
2559 2562
2560 is_for_iteration_variable = 2563 is_for_iteration_variable =
2561 var_context == kForStatement && 2564 var_context == kForStatement &&
2562 (peek() == Token::IN || PeekContextualKeyword(CStrVector("of"))); 2565 (peek() == Token::IN || PeekContextualKeyword(CStrVector("of")));
2563 if (is_for_iteration_variable && 2566 if (is_for_iteration_variable &&
2564 (parsing_result->descriptor.mode == CONST || 2567 (parsing_result->descriptor.mode == CONST ||
2565 parsing_result->descriptor.mode == CONST_LEGACY)) { 2568 parsing_result->descriptor.mode == CONST_LEGACY)) {
2566 parsing_result->descriptor.needs_init = false; 2569 parsing_result->descriptor.needs_init = false;
2567 } 2570 }
2568 2571
2569 Expression* value = NULL; 2572 Expression* value = NULL;
2570 // Harmony consts have non-optional initializers. 2573 // Harmony consts have non-optional initializers.
2571 int initializer_position = RelocInfo::kNoPosition; 2574 int initializer_position = RelocInfo::kNoPosition;
2572 if (peek() == Token::ASSIGN || (parsing_result->descriptor.mode == CONST && 2575 if (Check(Token::ASSIGN)) {
2573 !is_for_iteration_variable)) {
2574 Expect(Token::ASSIGN, ok);
2575 if (!*ok) return;
2576 ExpressionClassifier classifier; 2576 ExpressionClassifier classifier;
2577 value = ParseAssignmentExpression(var_context != kForStatement, 2577 value = ParseAssignmentExpression(var_context != kForStatement,
2578 &classifier, ok); 2578 &classifier, ok);
2579 if (!*ok) return; 2579 if (!*ok) return;
2580 ValidateExpression(&classifier, ok); 2580 ValidateExpression(&classifier, ok);
2581 if (!*ok) return; 2581 if (!*ok) return;
2582 variable_loc.end_pos = scanner()->location().end_pos; 2582 variable_loc.end_pos = scanner()->location().end_pos;
2583 2583
2584 if (!parsing_result->first_initializer_loc.IsValid()) { 2584 if (!parsing_result->first_initializer_loc.IsValid()) {
2585 parsing_result->first_initializer_loc = variable_loc; 2585 parsing_result->first_initializer_loc = variable_loc;
2586 } 2586 }
2587 2587
2588 // Don't infer if it is "a = function(){...}();"-like expression. 2588 // Don't infer if it is "a = function(){...}();"-like expression.
2589 if (single_name) { 2589 if (single_name) {
2590 if (fni_ != NULL && value->AsCall() == NULL && 2590 if (fni_ != NULL && value->AsCall() == NULL &&
2591 value->AsCallNew() == NULL) { 2591 value->AsCallNew() == NULL) {
2592 fni_->Infer(); 2592 fni_->Infer();
2593 } else { 2593 } else {
2594 fni_->RemoveLastFunction(); 2594 fni_->RemoveLastFunction();
2595 } 2595 }
2596 } 2596 }
2597 // End position of the initializer is after the assignment expression. 2597 // End position of the initializer is after the assignment expression.
2598 initializer_position = scanner()->location().end_pos; 2598 initializer_position = scanner()->location().end_pos;
2599 } else { 2599 } else {
2600 if ((parsing_result->descriptor.mode == CONST || is_pattern) &&
2601 !is_for_iteration_variable) {
2602 ParserTraits::ReportMessageAt(
2603 Scanner::Location(decl_pos, scanner()->location().end_pos),
2604 MessageTemplate::kDeclarationMissingInitializer,
2605 is_pattern ? "destructuring" : "const");
2606 *ok = false;
2607 return;
2608 }
2600 // End position of the initializer is after the variable. 2609 // End position of the initializer is after the variable.
2601 initializer_position = position(); 2610 initializer_position = position();
2602 } 2611 }
2603 2612
2604 // Make sure that 'const x' and 'let x' initialize 'x' to undefined. 2613 // Make sure that 'const x' and 'let x' initialize 'x' to undefined.
2605 if (value == NULL && parsing_result->descriptor.needs_init) { 2614 if (value == NULL && parsing_result->descriptor.needs_init) {
2606 value = GetLiteralUndefined(position()); 2615 value = GetLiteralUndefined(position());
2607 } 2616 }
2608 2617
2609 if (single_name && fni_ != NULL) fni_->Leave(); 2618 if (single_name && fni_ != NULL) fni_->Leave();
(...skipping 3771 matching lines...) Expand 10 before | Expand all | Expand 10 after
6381 6390
6382 Expression* Parser::SpreadCallNew(Expression* function, 6391 Expression* Parser::SpreadCallNew(Expression* function,
6383 ZoneList<v8::internal::Expression*>* args, 6392 ZoneList<v8::internal::Expression*>* args,
6384 int pos) { 6393 int pos) {
6385 args->InsertAt(0, function, zone()); 6394 args->InsertAt(0, function, zone());
6386 6395
6387 return factory()->NewCallRuntime(Context::REFLECT_CONSTRUCT_INDEX, args, pos); 6396 return factory()->NewCallRuntime(Context::REFLECT_CONSTRUCT_INDEX, args, pos);
6388 } 6397 }
6389 } // namespace internal 6398 } // namespace internal
6390 } // namespace v8 6399 } // namespace v8
OLDNEW
« no previous file with comments | « src/messages.h ('k') | src/preparser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698