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

Side by Side Diff: src/parsing/parser.cc

Issue 1900033003: Disallow generator declarations in certain locations (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fix from review comments Created 4 years, 7 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/parsing/parser.h ('k') | src/parsing/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/parsing/parser.h" 5 #include "src/parsing/parser.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast/ast.h" 8 #include "src/ast/ast.h"
9 #include "src/ast/ast-expression-rewriter.h" 9 #include "src/ast/ast-expression-rewriter.h"
10 #include "src/ast/ast-expression-visitor.h" 10 #include "src/ast/ast-expression-visitor.h"
(...skipping 1230 matching lines...) Expand 10 before | Expand all | Expand 10 after
1241 1241
1242 1242
1243 Statement* Parser::ParseStatementListItem(bool* ok) { 1243 Statement* Parser::ParseStatementListItem(bool* ok) {
1244 // (Ecma 262 6th Edition, 13.1): 1244 // (Ecma 262 6th Edition, 13.1):
1245 // StatementListItem: 1245 // StatementListItem:
1246 // Statement 1246 // Statement
1247 // Declaration 1247 // Declaration
1248 1248
1249 switch (peek()) { 1249 switch (peek()) {
1250 case Token::FUNCTION: 1250 case Token::FUNCTION:
1251 return ParseFunctionDeclaration(NULL, ok); 1251 return ParseHoistableDeclaration(NULL, ok);
1252 case Token::CLASS: 1252 case Token::CLASS:
1253 Consume(Token::CLASS); 1253 Consume(Token::CLASS);
1254 return ParseClassDeclaration(NULL, ok); 1254 return ParseClassDeclaration(NULL, ok);
1255 case Token::CONST: 1255 case Token::CONST:
1256 return ParseVariableStatement(kStatementListItem, NULL, ok); 1256 return ParseVariableStatement(kStatementListItem, NULL, ok);
1257 case Token::VAR: 1257 case Token::VAR:
1258 return ParseVariableStatement(kStatementListItem, NULL, ok); 1258 return ParseVariableStatement(kStatementListItem, NULL, ok);
1259 case Token::LET: 1259 case Token::LET:
1260 if (IsNextLetKeyword()) { 1260 if (IsNextLetKeyword()) {
1261 return ParseVariableStatement(kStatementListItem, NULL, ok); 1261 return ParseVariableStatement(kStatementListItem, NULL, ok);
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
1543 // GeneratorDeclaration[+Default] :: 1543 // GeneratorDeclaration[+Default] ::
1544 // 'function' '*' '(' FormalParameters ')' '{' FunctionBody '}' 1544 // 'function' '*' '(' FormalParameters ')' '{' FunctionBody '}'
1545 default_export = ParseFunctionLiteral( 1545 default_export = ParseFunctionLiteral(
1546 default_string, Scanner::Location::invalid(), 1546 default_string, Scanner::Location::invalid(),
1547 kSkipFunctionNameCheck, 1547 kSkipFunctionNameCheck,
1548 is_generator ? FunctionKind::kGeneratorFunction 1548 is_generator ? FunctionKind::kGeneratorFunction
1549 : FunctionKind::kNormalFunction, 1549 : FunctionKind::kNormalFunction,
1550 pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK); 1550 pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK);
1551 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition); 1551 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition);
1552 } else { 1552 } else {
1553 result = ParseFunctionDeclaration(pos, is_generator, &names, CHECK_OK); 1553 result = ParseHoistableDeclaration(pos, is_generator, &names, CHECK_OK);
1554 } 1554 }
1555 break; 1555 break;
1556 } 1556 }
1557 1557
1558 case Token::CLASS: 1558 case Token::CLASS:
1559 Consume(Token::CLASS); 1559 Consume(Token::CLASS);
1560 if (peek() == Token::EXTENDS || peek() == Token::LBRACE) { 1560 if (peek() == Token::EXTENDS || peek() == Token::LBRACE) {
1561 // ClassDeclaration[+Default] :: 1561 // ClassDeclaration[+Default] ::
1562 // 'class' ('extends' LeftHandExpression)? '{' ClassBody '}' 1562 // 'class' ('extends' LeftHandExpression)? '{' ClassBody '}'
1563 default_export = 1563 default_export =
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1672 scope_->module()->AddModuleRequest(indirect_export_module_specifier, 1672 scope_->module()->AddModuleRequest(indirect_export_module_specifier,
1673 zone()); 1673 zone());
1674 for (int i = 0; i < length; ++i) { 1674 for (int i = 0; i < length; ++i) {
1675 // TODO(ES6): scope_->module()->AddIndirectExport(...);( 1675 // TODO(ES6): scope_->module()->AddIndirectExport(...);(
1676 } 1676 }
1677 } 1677 }
1678 return factory()->NewEmptyStatement(pos); 1678 return factory()->NewEmptyStatement(pos);
1679 } 1679 }
1680 1680
1681 case Token::FUNCTION: 1681 case Token::FUNCTION:
1682 result = ParseFunctionDeclaration(&names, CHECK_OK); 1682 result = ParseHoistableDeclaration(&names, CHECK_OK);
1683 break; 1683 break;
1684 1684
1685 case Token::CLASS: 1685 case Token::CLASS:
1686 Consume(Token::CLASS); 1686 Consume(Token::CLASS);
1687 result = ParseClassDeclaration(&names, CHECK_OK); 1687 result = ParseClassDeclaration(&names, CHECK_OK);
1688 break; 1688 break;
1689 1689
1690 case Token::VAR: 1690 case Token::VAR:
1691 case Token::LET: 1691 case Token::LET:
1692 case Token::CONST: 1692 case Token::CONST:
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
2041 factory()->NewVariableDeclaration(proxy, VAR, scope_, pos); 2041 factory()->NewVariableDeclaration(proxy, VAR, scope_, pos);
2042 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); 2042 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK);
2043 NativeFunctionLiteral* lit = factory()->NewNativeFunctionLiteral( 2043 NativeFunctionLiteral* lit = factory()->NewNativeFunctionLiteral(
2044 name, extension_, RelocInfo::kNoPosition); 2044 name, extension_, RelocInfo::kNoPosition);
2045 return factory()->NewExpressionStatement( 2045 return factory()->NewExpressionStatement(
2046 factory()->NewAssignment(Token::INIT, proxy, lit, RelocInfo::kNoPosition), 2046 factory()->NewAssignment(Token::INIT, proxy, lit, RelocInfo::kNoPosition),
2047 pos); 2047 pos);
2048 } 2048 }
2049 2049
2050 2050
2051 Statement* Parser::ParseFunctionDeclaration( 2051 Statement* Parser::ParseHoistableDeclaration(
2052 ZoneList<const AstRawString*>* names, bool* ok) { 2052 ZoneList<const AstRawString*>* names, bool* ok) {
2053 Expect(Token::FUNCTION, CHECK_OK); 2053 Expect(Token::FUNCTION, CHECK_OK);
2054 int pos = position(); 2054 int pos = position();
2055 bool is_generator = Check(Token::MUL); 2055 bool is_generator = Check(Token::MUL);
2056 return ParseFunctionDeclaration(pos, is_generator, names, ok); 2056 return ParseHoistableDeclaration(pos, is_generator, names, ok);
2057 } 2057 }
2058 2058
2059 2059
2060 Statement* Parser::ParseFunctionDeclaration( 2060 Statement* Parser::ParseHoistableDeclaration(
2061 int pos, bool is_generator, ZoneList<const AstRawString*>* names, 2061 int pos, bool is_generator, ZoneList<const AstRawString*>* names,
2062 bool* ok) { 2062 bool* ok) {
2063 // FunctionDeclaration :: 2063 // FunctionDeclaration ::
2064 // 'function' Identifier '(' FormalParameters ')' '{' FunctionBody '}' 2064 // 'function' Identifier '(' FormalParameters ')' '{' FunctionBody '}'
2065 // GeneratorDeclaration :: 2065 // GeneratorDeclaration ::
2066 // 'function' '*' Identifier '(' FormalParameters ')' '{' FunctionBody '}' 2066 // 'function' '*' Identifier '(' FormalParameters ')' '{' FunctionBody '}'
2067 // 2067 //
2068 // 'function' and '*' (if present) have been consumed by the caller. 2068 // 'function' and '*' (if present) have been consumed by the caller.
2069 bool is_strict_reserved = false; 2069 bool is_strict_reserved = false;
2070 const AstRawString* name = ParseIdentifierOrStrictReservedWord( 2070 const AstRawString* name = ParseIdentifierOrStrictReservedWord(
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
2380 if (labels != NULL) { 2380 if (labels != NULL) {
2381 for (int i = labels->length(); i-- > 0; ) { 2381 for (int i = labels->length(); i-- > 0; ) {
2382 if (labels->at(i) == label) { 2382 if (labels->at(i) == label) {
2383 return true; 2383 return true;
2384 } 2384 }
2385 } 2385 }
2386 } 2386 }
2387 return false; 2387 return false;
2388 } 2388 }
2389 2389
2390 Statement* Parser::ParseHoistableDeclaration(bool* ok) {
adamk 2016/04/26 23:55:57 I was suggesting that this should be called ParseF
Dan Ehrenberg 2016/04/27 00:18:10 No, you were clear; overzealous find/replace. Fixe
2391 Consume(Token::FUNCTION);
2392 int pos = position();
2393 bool is_generator = Check(Token::MUL);
2394 if (allow_harmony_restrictive_declarations() && is_generator) {
2395 ParserTraits::ReportMessageAt(
2396 scanner()->location(),
2397 MessageTemplate::kGeneratorInLegacyContext);
2398 *ok = false;
2399 return nullptr;
2400 }
2401 return ParseHoistableDeclaration(pos, is_generator, nullptr, CHECK_OK);
2402 }
2403
2390 Statement* Parser::ParseExpressionOrLabelledStatement( 2404 Statement* Parser::ParseExpressionOrLabelledStatement(
2391 ZoneList<const AstRawString*>* labels, 2405 ZoneList<const AstRawString*>* labels,
2392 AllowLabelledFunctionStatement allow_function, bool* ok) { 2406 AllowLabelledFunctionStatement allow_function, bool* ok) {
2393 // ExpressionStatement | LabelledStatement :: 2407 // ExpressionStatement | LabelledStatement ::
2394 // Expression ';' 2408 // Expression ';'
2395 // Identifier ':' Statement 2409 // Identifier ':' Statement
2396 // 2410 //
2397 // ExpressionStatement[Yield] : 2411 // ExpressionStatement[Yield] :
2398 // [lookahead ∉ {{, function, class, let [}] Expression[In, ?Yield] ; 2412 // [lookahead ∉ {{, function, class, let [}] Expression[In, ?Yield] ;
2399 2413
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2436 } 2450 }
2437 labels->Add(label, zone()); 2451 labels->Add(label, zone());
2438 // Remove the "ghost" variable that turned out to be a label 2452 // Remove the "ghost" variable that turned out to be a label
2439 // from the top scope. This way, we don't try to resolve it 2453 // from the top scope. This way, we don't try to resolve it
2440 // during the scope processing. 2454 // during the scope processing.
2441 scope_->RemoveUnresolved(var); 2455 scope_->RemoveUnresolved(var);
2442 Expect(Token::COLON, CHECK_OK); 2456 Expect(Token::COLON, CHECK_OK);
2443 // ES#sec-labelled-function-declarations Labelled Function Declarations 2457 // ES#sec-labelled-function-declarations Labelled Function Declarations
2444 if (peek() == Token::FUNCTION && is_sloppy(language_mode())) { 2458 if (peek() == Token::FUNCTION && is_sloppy(language_mode())) {
2445 if (allow_function == kAllowLabelledFunctionStatement) { 2459 if (allow_function == kAllowLabelledFunctionStatement) {
2446 return ParseFunctionDeclaration(labels, ok); 2460 return ParseHoistableDeclaration(ok);
2447 } else { 2461 } else {
2448 return ParseScopedStatement(labels, true, ok); 2462 return ParseScopedStatement(labels, true, ok);
2449 } 2463 }
2450 } 2464 }
2451 return ParseStatement(labels, kDisallowLabelledFunctionStatement, ok); 2465 return ParseStatement(labels, kDisallowLabelledFunctionStatement, ok);
2452 } 2466 }
2453 2467
2454 // If we have an extension, we allow a native function declaration. 2468 // If we have an extension, we allow a native function declaration.
2455 // A native function declaration starts with "native function" with 2469 // A native function declaration starts with "native function" with
2456 // no line-terminator between the two words. 2470 // no line-terminator between the two words.
(...skipping 973 matching lines...) Expand 10 before | Expand all | Expand 10 after
3430 return ParseSubStatement(labels, kDisallowLabelledFunctionStatement, ok); 3444 return ParseSubStatement(labels, kDisallowLabelledFunctionStatement, ok);
3431 } else { 3445 } else {
3432 if (legacy) { 3446 if (legacy) {
3433 ++use_counts_[v8::Isolate::kLegacyFunctionDeclaration]; 3447 ++use_counts_[v8::Isolate::kLegacyFunctionDeclaration];
3434 } 3448 }
3435 // Make a block around the statement for a lexical binding 3449 // Make a block around the statement for a lexical binding
3436 // is introduced by a FunctionDeclaration. 3450 // is introduced by a FunctionDeclaration.
3437 Scope* body_scope = NewScope(scope_, BLOCK_SCOPE); 3451 Scope* body_scope = NewScope(scope_, BLOCK_SCOPE);
3438 BlockState block_state(&scope_, body_scope); 3452 BlockState block_state(&scope_, body_scope);
3439 Block* block = factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition); 3453 Block* block = factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition);
3440 Statement* body = ParseFunctionDeclaration(NULL, CHECK_OK); 3454 Statement* body = ParseHoistableDeclaration(CHECK_OK);
3441 block->statements()->Add(body, zone()); 3455 block->statements()->Add(body, zone());
3442 body_scope->set_end_position(scanner()->location().end_pos); 3456 body_scope->set_end_position(scanner()->location().end_pos);
3443 body_scope = body_scope->FinalizeBlockScope(); 3457 body_scope = body_scope->FinalizeBlockScope();
3444 block->set_scope(body_scope); 3458 block->set_scope(body_scope);
3445 return block; 3459 return block;
3446 } 3460 }
3447 } 3461 }
3448 3462
3449 Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels, 3463 Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
3450 bool* ok) { 3464 bool* ok) {
(...skipping 3332 matching lines...) Expand 10 before | Expand all | Expand 10 after
6783 try_block, target); 6797 try_block, target);
6784 final_loop = target; 6798 final_loop = target;
6785 } 6799 }
6786 6800
6787 return final_loop; 6801 return final_loop;
6788 } 6802 }
6789 6803
6790 6804
6791 } // namespace internal 6805 } // namespace internal
6792 } // namespace v8 6806 } // namespace v8
OLDNEW
« no previous file with comments | « src/parsing/parser.h ('k') | src/parsing/preparser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698