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

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: Rebase 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 1234 matching lines...) Expand 10 before | Expand all | Expand 10 after
1245 1245
1246 1246
1247 Statement* Parser::ParseStatementListItem(bool* ok) { 1247 Statement* Parser::ParseStatementListItem(bool* ok) {
1248 // (Ecma 262 6th Edition, 13.1): 1248 // (Ecma 262 6th Edition, 13.1):
1249 // StatementListItem: 1249 // StatementListItem:
1250 // Statement 1250 // Statement
1251 // Declaration 1251 // Declaration
1252 1252
1253 switch (peek()) { 1253 switch (peek()) {
1254 case Token::FUNCTION: 1254 case Token::FUNCTION:
1255 return ParseFunctionDeclaration(NULL, ok); 1255 return ParseHoistableDeclaration(NULL, ok);
1256 case Token::CLASS: 1256 case Token::CLASS:
1257 Consume(Token::CLASS); 1257 Consume(Token::CLASS);
1258 return ParseClassDeclaration(NULL, ok); 1258 return ParseClassDeclaration(NULL, ok);
1259 case Token::CONST: 1259 case Token::CONST:
1260 return ParseVariableStatement(kStatementListItem, NULL, ok); 1260 return ParseVariableStatement(kStatementListItem, NULL, ok);
1261 case Token::VAR: 1261 case Token::VAR:
1262 return ParseVariableStatement(kStatementListItem, NULL, ok); 1262 return ParseVariableStatement(kStatementListItem, NULL, ok);
1263 case Token::LET: 1263 case Token::LET:
1264 if (IsNextLetKeyword()) { 1264 if (IsNextLetKeyword()) {
1265 return ParseVariableStatement(kStatementListItem, NULL, ok); 1265 return ParseVariableStatement(kStatementListItem, NULL, ok);
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
1546 // GeneratorDeclaration[+Default] :: 1546 // GeneratorDeclaration[+Default] ::
1547 // 'function' '*' '(' FormalParameters ')' '{' FunctionBody '}' 1547 // 'function' '*' '(' FormalParameters ')' '{' FunctionBody '}'
1548 default_export = ParseFunctionLiteral( 1548 default_export = ParseFunctionLiteral(
1549 default_string, Scanner::Location::invalid(), 1549 default_string, Scanner::Location::invalid(),
1550 kSkipFunctionNameCheck, 1550 kSkipFunctionNameCheck,
1551 is_generator ? FunctionKind::kGeneratorFunction 1551 is_generator ? FunctionKind::kGeneratorFunction
1552 : FunctionKind::kNormalFunction, 1552 : FunctionKind::kNormalFunction,
1553 pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK); 1553 pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK);
1554 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition); 1554 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition);
1555 } else { 1555 } else {
1556 result = ParseFunctionDeclaration(pos, is_generator, &names, CHECK_OK); 1556 result = ParseHoistableDeclaration(pos, is_generator, &names, CHECK_OK);
1557 } 1557 }
1558 break; 1558 break;
1559 } 1559 }
1560 1560
1561 case Token::CLASS: 1561 case Token::CLASS:
1562 Consume(Token::CLASS); 1562 Consume(Token::CLASS);
1563 if (peek() == Token::EXTENDS || peek() == Token::LBRACE) { 1563 if (peek() == Token::EXTENDS || peek() == Token::LBRACE) {
1564 // ClassDeclaration[+Default] :: 1564 // ClassDeclaration[+Default] ::
1565 // 'class' ('extends' LeftHandExpression)? '{' ClassBody '}' 1565 // 'class' ('extends' LeftHandExpression)? '{' ClassBody '}'
1566 default_export = 1566 default_export =
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1675 scope_->module()->AddModuleRequest(indirect_export_module_specifier, 1675 scope_->module()->AddModuleRequest(indirect_export_module_specifier,
1676 zone()); 1676 zone());
1677 for (int i = 0; i < length; ++i) { 1677 for (int i = 0; i < length; ++i) {
1678 // TODO(ES6): scope_->module()->AddIndirectExport(...);( 1678 // TODO(ES6): scope_->module()->AddIndirectExport(...);(
1679 } 1679 }
1680 } 1680 }
1681 return factory()->NewEmptyStatement(pos); 1681 return factory()->NewEmptyStatement(pos);
1682 } 1682 }
1683 1683
1684 case Token::FUNCTION: 1684 case Token::FUNCTION:
1685 result = ParseFunctionDeclaration(&names, CHECK_OK); 1685 result = ParseHoistableDeclaration(&names, CHECK_OK);
1686 break; 1686 break;
1687 1687
1688 case Token::CLASS: 1688 case Token::CLASS:
1689 Consume(Token::CLASS); 1689 Consume(Token::CLASS);
1690 result = ParseClassDeclaration(&names, CHECK_OK); 1690 result = ParseClassDeclaration(&names, CHECK_OK);
1691 break; 1691 break;
1692 1692
1693 case Token::VAR: 1693 case Token::VAR:
1694 case Token::LET: 1694 case Token::LET:
1695 case Token::CONST: 1695 case Token::CONST:
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
2044 factory()->NewVariableDeclaration(proxy, VAR, scope_, pos); 2044 factory()->NewVariableDeclaration(proxy, VAR, scope_, pos);
2045 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); 2045 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK);
2046 NativeFunctionLiteral* lit = factory()->NewNativeFunctionLiteral( 2046 NativeFunctionLiteral* lit = factory()->NewNativeFunctionLiteral(
2047 name, extension_, RelocInfo::kNoPosition); 2047 name, extension_, RelocInfo::kNoPosition);
2048 return factory()->NewExpressionStatement( 2048 return factory()->NewExpressionStatement(
2049 factory()->NewAssignment(Token::INIT, proxy, lit, RelocInfo::kNoPosition), 2049 factory()->NewAssignment(Token::INIT, proxy, lit, RelocInfo::kNoPosition),
2050 pos); 2050 pos);
2051 } 2051 }
2052 2052
2053 2053
2054 Statement* Parser::ParseFunctionDeclaration( 2054 Statement* Parser::ParseHoistableDeclaration(
2055 ZoneList<const AstRawString*>* names, bool* ok) { 2055 ZoneList<const AstRawString*>* names, bool* ok) {
2056 Expect(Token::FUNCTION, CHECK_OK); 2056 Expect(Token::FUNCTION, CHECK_OK);
2057 int pos = position(); 2057 int pos = position();
2058 bool is_generator = Check(Token::MUL); 2058 bool is_generator = Check(Token::MUL);
2059 return ParseFunctionDeclaration(pos, is_generator, names, ok); 2059 return ParseHoistableDeclaration(pos, is_generator, names, ok);
2060 } 2060 }
2061 2061
2062 2062
2063 Statement* Parser::ParseFunctionDeclaration( 2063 Statement* Parser::ParseHoistableDeclaration(
2064 int pos, bool is_generator, ZoneList<const AstRawString*>* names, 2064 int pos, bool is_generator, ZoneList<const AstRawString*>* names,
2065 bool* ok) { 2065 bool* ok) {
2066 // FunctionDeclaration :: 2066 // FunctionDeclaration ::
2067 // 'function' Identifier '(' FormalParameters ')' '{' FunctionBody '}' 2067 // 'function' Identifier '(' FormalParameters ')' '{' FunctionBody '}'
2068 // GeneratorDeclaration :: 2068 // GeneratorDeclaration ::
2069 // 'function' '*' Identifier '(' FormalParameters ')' '{' FunctionBody '}' 2069 // 'function' '*' Identifier '(' FormalParameters ')' '{' FunctionBody '}'
2070 // 2070 //
2071 // 'function' and '*' (if present) have been consumed by the caller. 2071 // 'function' and '*' (if present) have been consumed by the caller.
2072 bool is_strict_reserved = false; 2072 bool is_strict_reserved = false;
2073 const AstRawString* name = ParseIdentifierOrStrictReservedWord( 2073 const AstRawString* name = ParseIdentifierOrStrictReservedWord(
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
2383 if (labels != NULL) { 2383 if (labels != NULL) {
2384 for (int i = labels->length(); i-- > 0; ) { 2384 for (int i = labels->length(); i-- > 0; ) {
2385 if (labels->at(i) == label) { 2385 if (labels->at(i) == label) {
2386 return true; 2386 return true;
2387 } 2387 }
2388 } 2388 }
2389 } 2389 }
2390 return false; 2390 return false;
2391 } 2391 }
2392 2392
2393 Statement* Parser::ParseFunctionDeclaration(bool* ok) {
2394 Consume(Token::FUNCTION);
2395 int pos = position();
2396 bool is_generator = Check(Token::MUL);
2397 if (allow_harmony_restrictive_declarations() && is_generator) {
2398 ParserTraits::ReportMessageAt(
2399 scanner()->location(),
2400 MessageTemplate::kGeneratorInLegacyContext);
2401 *ok = false;
2402 return nullptr;
2403 }
2404 return ParseHoistableDeclaration(pos, is_generator, nullptr, CHECK_OK);
2405 }
2406
2393 Statement* Parser::ParseExpressionOrLabelledStatement( 2407 Statement* Parser::ParseExpressionOrLabelledStatement(
2394 ZoneList<const AstRawString*>* labels, 2408 ZoneList<const AstRawString*>* labels,
2395 AllowLabelledFunctionStatement allow_function, bool* ok) { 2409 AllowLabelledFunctionStatement allow_function, bool* ok) {
2396 // ExpressionStatement | LabelledStatement :: 2410 // ExpressionStatement | LabelledStatement ::
2397 // Expression ';' 2411 // Expression ';'
2398 // Identifier ':' Statement 2412 // Identifier ':' Statement
2399 // 2413 //
2400 // ExpressionStatement[Yield] : 2414 // ExpressionStatement[Yield] :
2401 // [lookahead ∉ {{, function, class, let [}] Expression[In, ?Yield] ; 2415 // [lookahead ∉ {{, function, class, let [}] Expression[In, ?Yield] ;
2402 2416
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2439 } 2453 }
2440 labels->Add(label, zone()); 2454 labels->Add(label, zone());
2441 // Remove the "ghost" variable that turned out to be a label 2455 // Remove the "ghost" variable that turned out to be a label
2442 // from the top scope. This way, we don't try to resolve it 2456 // from the top scope. This way, we don't try to resolve it
2443 // during the scope processing. 2457 // during the scope processing.
2444 scope_->RemoveUnresolved(var); 2458 scope_->RemoveUnresolved(var);
2445 Expect(Token::COLON, CHECK_OK); 2459 Expect(Token::COLON, CHECK_OK);
2446 // ES#sec-labelled-function-declarations Labelled Function Declarations 2460 // ES#sec-labelled-function-declarations Labelled Function Declarations
2447 if (peek() == Token::FUNCTION && is_sloppy(language_mode())) { 2461 if (peek() == Token::FUNCTION && is_sloppy(language_mode())) {
2448 if (allow_function == kAllowLabelledFunctionStatement) { 2462 if (allow_function == kAllowLabelledFunctionStatement) {
2449 return ParseFunctionDeclaration(labels, ok); 2463 return ParseFunctionDeclaration(ok);
2450 } else { 2464 } else {
2451 return ParseScopedStatement(labels, true, ok); 2465 return ParseScopedStatement(labels, true, ok);
2452 } 2466 }
2453 } 2467 }
2454 return ParseStatement(labels, kDisallowLabelledFunctionStatement, ok); 2468 return ParseStatement(labels, kDisallowLabelledFunctionStatement, ok);
2455 } 2469 }
2456 2470
2457 // If we have an extension, we allow a native function declaration. 2471 // If we have an extension, we allow a native function declaration.
2458 // A native function declaration starts with "native function" with 2472 // A native function declaration starts with "native function" with
2459 // no line-terminator between the two words. 2473 // no line-terminator between the two words.
(...skipping 963 matching lines...) Expand 10 before | Expand all | Expand 10 after
3423 } else { 3437 } else {
3424 if (legacy) { 3438 if (legacy) {
3425 ++use_counts_[v8::Isolate::kLegacyFunctionDeclaration]; 3439 ++use_counts_[v8::Isolate::kLegacyFunctionDeclaration];
3426 } 3440 }
3427 // Make a block around the statement for a lexical binding 3441 // Make a block around the statement for a lexical binding
3428 // is introduced by a FunctionDeclaration. 3442 // is introduced by a FunctionDeclaration.
3429 Scope* body_scope = NewScope(scope_, BLOCK_SCOPE); 3443 Scope* body_scope = NewScope(scope_, BLOCK_SCOPE);
3430 body_scope->set_start_position(scanner()->location().beg_pos); 3444 body_scope->set_start_position(scanner()->location().beg_pos);
3431 BlockState block_state(&scope_, body_scope); 3445 BlockState block_state(&scope_, body_scope);
3432 Block* block = factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition); 3446 Block* block = factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition);
3433 Statement* body = ParseFunctionDeclaration(NULL, CHECK_OK); 3447 Statement* body = ParseFunctionDeclaration(CHECK_OK);
3434 block->statements()->Add(body, zone()); 3448 block->statements()->Add(body, zone());
3435 body_scope->set_end_position(scanner()->location().end_pos); 3449 body_scope->set_end_position(scanner()->location().end_pos);
3436 body_scope = body_scope->FinalizeBlockScope(); 3450 body_scope = body_scope->FinalizeBlockScope();
3437 block->set_scope(body_scope); 3451 block->set_scope(body_scope);
3438 return block; 3452 return block;
3439 } 3453 }
3440 } 3454 }
3441 3455
3442 Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels, 3456 Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
3443 bool* ok) { 3457 bool* ok) {
(...skipping 3351 matching lines...) Expand 10 before | Expand all | Expand 10 after
6795 try_block, target); 6809 try_block, target);
6796 final_loop = target; 6810 final_loop = target;
6797 } 6811 }
6798 6812
6799 return final_loop; 6813 return final_loop;
6800 } 6814 }
6801 6815
6802 6816
6803 } // namespace internal 6817 } // namespace internal
6804 } // namespace v8 6818 } // 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