| 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/parsing/parser.h" | 5 #include "src/parsing/parser.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "src/api.h" | 9 #include "src/api.h" |
| 10 #include "src/ast/ast.h" | 10 #include "src/ast/ast.h" |
| (...skipping 1936 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1947 } | 1947 } |
| 1948 } | 1948 } |
| 1949 | 1949 |
| 1950 | 1950 |
| 1951 VariableProxy* Parser::NewUnresolved(const AstRawString* name, | 1951 VariableProxy* Parser::NewUnresolved(const AstRawString* name, |
| 1952 VariableMode mode) { | 1952 VariableMode mode) { |
| 1953 // If we are inside a function, a declaration of a 'var' variable is a | 1953 // If we are inside a function, a declaration of a 'var' variable is a |
| 1954 // truly local variable, and the scope of the variable is always the function | 1954 // truly local variable, and the scope of the variable is always the function |
| 1955 // scope. | 1955 // scope. |
| 1956 // Let/const variables are always added to the immediately enclosing scope. | 1956 // Let/const variables are always added to the immediately enclosing scope. |
| 1957 Scope* scope = IsLexicalVariableMode(mode) | 1957 Scope* scope = |
| 1958 ? this->scope() | 1958 IsLexicalVariableMode(mode) ? this->scope() : GetDeclarationScope(); |
| 1959 : this->scope()->GetDeclarationScope(); | |
| 1960 return scope->NewUnresolved(factory(), name, Variable::NORMAL, | 1959 return scope->NewUnresolved(factory(), name, Variable::NORMAL, |
| 1961 scanner()->location().beg_pos, | 1960 scanner()->location().beg_pos, |
| 1962 scanner()->location().end_pos); | 1961 scanner()->location().end_pos); |
| 1963 } | 1962 } |
| 1964 | 1963 |
| 1965 InitializationFlag Parser::DefaultInitializationFlag(VariableMode mode) { | 1964 InitializationFlag Parser::DefaultInitializationFlag(VariableMode mode) { |
| 1966 DCHECK(IsDeclaredVariableMode(mode)); | 1965 DCHECK(IsDeclaredVariableMode(mode)); |
| 1967 return mode == VAR ? kCreatedInitialized : kNeedsInitialization; | 1966 return mode == VAR ? kCreatedInitialized : kNeedsInitialization; |
| 1968 } | 1967 } |
| 1969 | 1968 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2109 } | 2108 } |
| 2110 } | 2109 } |
| 2111 Expect(Token::RPAREN, CHECK_OK); | 2110 Expect(Token::RPAREN, CHECK_OK); |
| 2112 Expect(Token::SEMICOLON, CHECK_OK); | 2111 Expect(Token::SEMICOLON, CHECK_OK); |
| 2113 | 2112 |
| 2114 // Make sure that the function containing the native declaration | 2113 // Make sure that the function containing the native declaration |
| 2115 // isn't lazily compiled. The extension structures are only | 2114 // isn't lazily compiled. The extension structures are only |
| 2116 // accessible while parsing the first time not when reparsing | 2115 // accessible while parsing the first time not when reparsing |
| 2117 // because of lazy compilation. | 2116 // because of lazy compilation. |
| 2118 // TODO(adamk): Should this be GetClosureScope()? | 2117 // TODO(adamk): Should this be GetClosureScope()? |
| 2119 scope()->GetDeclarationScope()->ForceEagerCompilation(); | 2118 GetDeclarationScope()->ForceEagerCompilation(); |
| 2120 | 2119 |
| 2121 // TODO(1240846): It's weird that native function declarations are | 2120 // TODO(1240846): It's weird that native function declarations are |
| 2122 // introduced dynamically when we meet their declarations, whereas | 2121 // introduced dynamically when we meet their declarations, whereas |
| 2123 // other functions are set up when entering the surrounding scope. | 2122 // other functions are set up when entering the surrounding scope. |
| 2124 Declaration* decl = DeclareVariable(name, VAR, pos, CHECK_OK); | 2123 Declaration* decl = DeclareVariable(name, VAR, pos, CHECK_OK); |
| 2125 NativeFunctionLiteral* lit = | 2124 NativeFunctionLiteral* lit = |
| 2126 factory()->NewNativeFunctionLiteral(name, extension_, kNoSourcePosition); | 2125 factory()->NewNativeFunctionLiteral(name, extension_, kNoSourcePosition); |
| 2127 return factory()->NewExpressionStatement( | 2126 return factory()->NewExpressionStatement( |
| 2128 factory()->NewAssignment(Token::INIT, decl->proxy(), lit, | 2127 factory()->NewAssignment(Token::INIT, decl->proxy(), lit, |
| 2129 kNoSourcePosition), | 2128 kNoSourcePosition), |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2218 EmptyStatement* empty = factory()->NewEmptyStatement(kNoSourcePosition); | 2217 EmptyStatement* empty = factory()->NewEmptyStatement(kNoSourcePosition); |
| 2219 // Async functions don't undergo sloppy mode block scoped hoisting, and don't | 2218 // Async functions don't undergo sloppy mode block scoped hoisting, and don't |
| 2220 // allow duplicates in a block. Both are represented by the | 2219 // allow duplicates in a block. Both are represented by the |
| 2221 // sloppy_block_function_map. Don't add them to the map for async functions. | 2220 // sloppy_block_function_map. Don't add them to the map for async functions. |
| 2222 // Generators are also supposed to be prohibited; currently doing this behind | 2221 // Generators are also supposed to be prohibited; currently doing this behind |
| 2223 // a flag and UseCounting violations to assess web compatibility. | 2222 // a flag and UseCounting violations to assess web compatibility. |
| 2224 if (is_sloppy(language_mode()) && !scope()->is_declaration_scope() && | 2223 if (is_sloppy(language_mode()) && !scope()->is_declaration_scope() && |
| 2225 !is_async && !(allow_harmony_restrictive_generators() && is_generator)) { | 2224 !is_async && !(allow_harmony_restrictive_generators() && is_generator)) { |
| 2226 SloppyBlockFunctionStatement* delegate = | 2225 SloppyBlockFunctionStatement* delegate = |
| 2227 factory()->NewSloppyBlockFunctionStatement(empty, scope()); | 2226 factory()->NewSloppyBlockFunctionStatement(empty, scope()); |
| 2228 DeclarationScope* target_scope = scope()->GetDeclarationScope(); | 2227 DeclarationScope* target_scope = GetDeclarationScope(); |
| 2229 target_scope->DeclareSloppyBlockFunction(variable_name, delegate); | 2228 target_scope->DeclareSloppyBlockFunction(variable_name, delegate); |
| 2230 return delegate; | 2229 return delegate; |
| 2231 } | 2230 } |
| 2232 return empty; | 2231 return empty; |
| 2233 } | 2232 } |
| 2234 | 2233 |
| 2235 Statement* Parser::ParseClassDeclaration(ZoneList<const AstRawString*>* names, | 2234 Statement* Parser::ParseClassDeclaration(ZoneList<const AstRawString*>* names, |
| 2236 bool default_export, bool* ok) { | 2235 bool default_export, bool* ok) { |
| 2237 // ClassDeclaration :: | 2236 // ClassDeclaration :: |
| 2238 // 'class' Identifier ('extends' LeftHandExpression)? '{' ClassBody '}' | 2237 // 'class' Identifier ('extends' LeftHandExpression)? '{' ClassBody '}' |
| (...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2782 ExpectSemicolon(CHECK_OK); | 2781 ExpectSemicolon(CHECK_OK); |
| 2783 | 2782 |
| 2784 if (is_generator()) { | 2783 if (is_generator()) { |
| 2785 return_value = BuildIteratorResult(return_value, true); | 2784 return_value = BuildIteratorResult(return_value, true); |
| 2786 } else if (is_async_function()) { | 2785 } else if (is_async_function()) { |
| 2787 return_value = BuildPromiseResolve(return_value, return_value->position()); | 2786 return_value = BuildPromiseResolve(return_value, return_value->position()); |
| 2788 } | 2787 } |
| 2789 | 2788 |
| 2790 result = factory()->NewReturnStatement(return_value, loc.beg_pos); | 2789 result = factory()->NewReturnStatement(return_value, loc.beg_pos); |
| 2791 | 2790 |
| 2792 DeclarationScope* decl_scope = scope()->GetDeclarationScope(); | 2791 DeclarationScope* decl_scope = GetDeclarationScope(); |
| 2793 if (decl_scope->is_script_scope() || decl_scope->is_eval_scope()) { | 2792 if (decl_scope->is_script_scope() || decl_scope->is_eval_scope()) { |
| 2794 ReportMessageAt(loc, MessageTemplate::kIllegalReturn); | 2793 ReportMessageAt(loc, MessageTemplate::kIllegalReturn); |
| 2795 *ok = false; | 2794 *ok = false; |
| 2796 return NULL; | 2795 return NULL; |
| 2797 } | 2796 } |
| 2798 return result; | 2797 return result; |
| 2799 } | 2798 } |
| 2800 | 2799 |
| 2801 | 2800 |
| 2802 Statement* Parser::ParseWithStatement(ZoneList<const AstRawString*>* labels, | 2801 Statement* Parser::ParseWithStatement(ZoneList<const AstRawString*>* labels, |
| (...skipping 1373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4176 DoExpression* Parser::ParseDoExpression(bool* ok) { | 4175 DoExpression* Parser::ParseDoExpression(bool* ok) { |
| 4177 // AssignmentExpression :: | 4176 // AssignmentExpression :: |
| 4178 // do '{' StatementList '}' | 4177 // do '{' StatementList '}' |
| 4179 int pos = peek_position(); | 4178 int pos = peek_position(); |
| 4180 | 4179 |
| 4181 Expect(Token::DO, CHECK_OK); | 4180 Expect(Token::DO, CHECK_OK); |
| 4182 Variable* result = | 4181 Variable* result = |
| 4183 scope()->NewTemporary(ast_value_factory()->dot_result_string()); | 4182 scope()->NewTemporary(ast_value_factory()->dot_result_string()); |
| 4184 Block* block = ParseBlock(nullptr, CHECK_OK); | 4183 Block* block = ParseBlock(nullptr, CHECK_OK); |
| 4185 DoExpression* expr = factory()->NewDoExpression(block, result, pos); | 4184 DoExpression* expr = factory()->NewDoExpression(block, result, pos); |
| 4186 if (!Rewriter::Rewrite(this, scope()->GetClosureScope(), expr, | 4185 if (!Rewriter::Rewrite(this, GetClosureScope(), expr, ast_value_factory())) { |
| 4187 ast_value_factory())) { | |
| 4188 *ok = false; | 4186 *ok = false; |
| 4189 return nullptr; | 4187 return nullptr; |
| 4190 } | 4188 } |
| 4191 return expr; | 4189 return expr; |
| 4192 } | 4190 } |
| 4193 | 4191 |
| 4194 void ParserTraits::ParseArrowFunctionFormalParameterList( | 4192 void ParserTraits::ParseArrowFunctionFormalParameterList( |
| 4195 ParserFormalParameters* parameters, Expression* expr, | 4193 ParserFormalParameters* parameters, Expression* expr, |
| 4196 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, | 4194 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, |
| 4197 const Scope::Snapshot& scope_snapshot, bool* ok) { | 4195 const Scope::Snapshot& scope_snapshot, bool* ok) { |
| (...skipping 913 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5111 scope()->NewTemporary(ast_value_factory()->empty_string()); | 5109 scope()->NewTemporary(ast_value_factory()->empty_string()); |
| 5112 do_block->set_scope(block_state.FinalizedBlockScope()); | 5110 do_block->set_scope(block_state.FinalizedBlockScope()); |
| 5113 DoExpression* do_expr = factory()->NewDoExpression(do_block, result_var, pos); | 5111 DoExpression* do_expr = factory()->NewDoExpression(do_block, result_var, pos); |
| 5114 | 5112 |
| 5115 ClassLiteral* class_literal = factory()->NewClassLiteral( | 5113 ClassLiteral* class_literal = factory()->NewClassLiteral( |
| 5116 proxy, extends, constructor, properties, pos, end_pos); | 5114 proxy, extends, constructor, properties, pos, end_pos); |
| 5117 | 5115 |
| 5118 do_block->statements()->Add( | 5116 do_block->statements()->Add( |
| 5119 factory()->NewExpressionStatement(class_literal, pos), zone()); | 5117 factory()->NewExpressionStatement(class_literal, pos), zone()); |
| 5120 do_expr->set_represented_function(constructor); | 5118 do_expr->set_represented_function(constructor); |
| 5121 Rewriter::Rewrite(this, scope()->GetClosureScope(), do_expr, | 5119 Rewriter::Rewrite(this, GetClosureScope(), do_expr, ast_value_factory()); |
| 5122 ast_value_factory()); | |
| 5123 | 5120 |
| 5124 return do_expr; | 5121 return do_expr; |
| 5125 } | 5122 } |
| 5126 | 5123 |
| 5127 | 5124 |
| 5128 Expression* Parser::ParseV8Intrinsic(bool* ok) { | 5125 Expression* Parser::ParseV8Intrinsic(bool* ok) { |
| 5129 // CallRuntime :: | 5126 // CallRuntime :: |
| 5130 // '%' Identifier Arguments | 5127 // '%' Identifier Arguments |
| 5131 | 5128 |
| 5132 int pos = peek_position(); | 5129 int pos = peek_position(); |
| 5133 Expect(Token::MOD, CHECK_OK); | 5130 Expect(Token::MOD, CHECK_OK); |
| 5134 // Allow "eval" or "arguments" for backward compatibility. | 5131 // Allow "eval" or "arguments" for backward compatibility. |
| 5135 const AstRawString* name = ParseIdentifier(kAllowRestrictedIdentifiers, | 5132 const AstRawString* name = ParseIdentifier(kAllowRestrictedIdentifiers, |
| 5136 CHECK_OK); | 5133 CHECK_OK); |
| 5137 Scanner::Location spread_pos; | 5134 Scanner::Location spread_pos; |
| 5138 ExpressionClassifier classifier(this); | 5135 ExpressionClassifier classifier(this); |
| 5139 ZoneList<Expression*>* args = | 5136 ZoneList<Expression*>* args = |
| 5140 ParseArguments(&spread_pos, &classifier, CHECK_OK); | 5137 ParseArguments(&spread_pos, &classifier, CHECK_OK); |
| 5141 | 5138 |
| 5142 DCHECK(!spread_pos.IsValid()); | 5139 DCHECK(!spread_pos.IsValid()); |
| 5143 | 5140 |
| 5144 if (extension_ != NULL) { | 5141 if (extension_ != NULL) { |
| 5145 // The extension structures are only accessible while parsing the | 5142 // The extension structures are only accessible while parsing the |
| 5146 // very first time not when reparsing because of lazy compilation. | 5143 // very first time not when reparsing because of lazy compilation. |
| 5147 scope()->GetDeclarationScope()->ForceEagerCompilation(); | 5144 GetDeclarationScope()->ForceEagerCompilation(); |
| 5148 } | 5145 } |
| 5149 | 5146 |
| 5150 const Runtime::Function* function = Runtime::FunctionForName(name->string()); | 5147 const Runtime::Function* function = Runtime::FunctionForName(name->string()); |
| 5151 | 5148 |
| 5152 if (function != NULL) { | 5149 if (function != NULL) { |
| 5153 // Check for possible name clash. | 5150 // Check for possible name clash. |
| 5154 DCHECK_EQ(Context::kNotFound, | 5151 DCHECK_EQ(Context::kNotFound, |
| 5155 Context::IntrinsicIndexForName(name->string())); | 5152 Context::IntrinsicIndexForName(name->string())); |
| 5156 // Check for built-in IS_VAR macro. | 5153 // Check for built-in IS_VAR macro. |
| 5157 if (function->function_id == Runtime::kIS_VAR) { | 5154 if (function->function_id == Runtime::kIS_VAR) { |
| (...skipping 1948 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7106 node->Print(Isolate::Current()); | 7103 node->Print(Isolate::Current()); |
| 7107 } | 7104 } |
| 7108 #endif // DEBUG | 7105 #endif // DEBUG |
| 7109 | 7106 |
| 7110 #undef CHECK_OK | 7107 #undef CHECK_OK |
| 7111 #undef CHECK_OK_VOID | 7108 #undef CHECK_OK_VOID |
| 7112 #undef CHECK_FAILED | 7109 #undef CHECK_FAILED |
| 7113 | 7110 |
| 7114 } // namespace internal | 7111 } // namespace internal |
| 7115 } // namespace v8 | 7112 } // namespace v8 |
| OLD | NEW |