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 #ifndef V8_PARSING_PARSER_BASE_H | 5 #ifndef V8_PARSING_PARSER_BASE_H |
6 #define V8_PARSING_PARSER_BASE_H | 6 #define V8_PARSING_PARSER_BASE_H |
7 | 7 |
8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
9 #include "src/ast/scopes.h" | 9 #include "src/ast/scopes.h" |
10 #include "src/bailout-reason.h" | 10 #include "src/bailout-reason.h" |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 const Impl* impl() const { return static_cast<const Impl*>(this); } | 191 const Impl* impl() const { return static_cast<const Impl*>(this); } |
192 | 192 |
193 ParserBase(Zone* zone, Scanner* scanner, uintptr_t stack_limit, | 193 ParserBase(Zone* zone, Scanner* scanner, uintptr_t stack_limit, |
194 v8::Extension* extension, AstValueFactory* ast_value_factory) | 194 v8::Extension* extension, AstValueFactory* ast_value_factory) |
195 : scope_state_(nullptr), | 195 : scope_state_(nullptr), |
196 function_state_(nullptr), | 196 function_state_(nullptr), |
197 extension_(extension), | 197 extension_(extension), |
198 fni_(nullptr), | 198 fni_(nullptr), |
199 ast_value_factory_(ast_value_factory), | 199 ast_value_factory_(ast_value_factory), |
200 ast_node_factory_(ast_value_factory), | 200 ast_node_factory_(ast_value_factory), |
201 mode_(PARSE_EAGERLY), // Lazy mode must be set explicitly. | |
202 parsing_module_(false), | 201 parsing_module_(false), |
203 stack_limit_(stack_limit), | 202 stack_limit_(stack_limit), |
204 zone_(zone), | 203 zone_(zone), |
205 classifier_(nullptr), | 204 classifier_(nullptr), |
206 scanner_(scanner), | 205 scanner_(scanner), |
207 stack_overflow_(false), | 206 stack_overflow_(false), |
208 default_eager_compile_hint_(FunctionLiteral::kShouldLazyCompile), | 207 default_eager_compile_hint_(FunctionLiteral::kShouldLazyCompile), |
209 allow_lazy_(false), | 208 allow_lazy_(false), |
210 allow_natives_(false), | 209 allow_natives_(false), |
211 allow_tailcalls_(false), | 210 allow_tailcalls_(false), |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 Zone* zone() const { return zone_; } | 247 Zone* zone() const { return zone_; } |
249 | 248 |
250 protected: | 249 protected: |
251 friend class v8::internal::ExpressionClassifier<ParserTypes<Impl>>; | 250 friend class v8::internal::ExpressionClassifier<ParserTypes<Impl>>; |
252 | 251 |
253 enum AllowRestrictedIdentifiers { | 252 enum AllowRestrictedIdentifiers { |
254 kAllowRestrictedIdentifiers, | 253 kAllowRestrictedIdentifiers, |
255 kDontAllowRestrictedIdentifiers | 254 kDontAllowRestrictedIdentifiers |
256 }; | 255 }; |
257 | 256 |
258 enum Mode { PARSE_LAZILY, PARSE_EAGERLY }; | |
259 | |
260 enum LazyParsingResult { kLazyParsingComplete, kLazyParsingAborted }; | 257 enum LazyParsingResult { kLazyParsingComplete, kLazyParsingAborted }; |
261 | 258 |
262 enum VariableDeclarationContext { | 259 enum VariableDeclarationContext { |
263 kStatementListItem, | 260 kStatementListItem, |
264 kStatement, | 261 kStatement, |
265 kForStatement | 262 kForStatement |
266 }; | 263 }; |
267 | 264 |
268 enum class FunctionBodyType { kNormal, kSingleExpression }; | 265 enum class FunctionBodyType { kNormal, kSingleExpression }; |
269 | 266 |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 next_materialized_literal_index_; | 581 next_materialized_literal_index_; |
585 function_state_->expected_property_count_ = expected_property_count_; | 582 function_state_->expected_property_count_ = expected_property_count_; |
586 } | 583 } |
587 | 584 |
588 private: | 585 private: |
589 FunctionState* function_state_; | 586 FunctionState* function_state_; |
590 int next_materialized_literal_index_; | 587 int next_materialized_literal_index_; |
591 int expected_property_count_; | 588 int expected_property_count_; |
592 }; | 589 }; |
593 | 590 |
594 class ParsingModeScope BASE_EMBEDDED { | |
595 public: | |
596 ParsingModeScope(ParserBase* parser, Mode mode) | |
597 : parser_(parser), | |
598 old_mode_(parser->mode()) { | |
599 parser_->mode_ = mode; | |
600 } | |
601 ~ParsingModeScope() { | |
602 parser_->mode_ = old_mode_; | |
603 } | |
604 | |
605 private: | |
606 ParserBase* parser_; | |
607 Mode old_mode_; | |
608 }; | |
609 | |
610 struct DeclarationDescriptor { | 591 struct DeclarationDescriptor { |
611 enum Kind { NORMAL, PARAMETER }; | 592 enum Kind { NORMAL, PARAMETER }; |
612 Scope* scope; | 593 Scope* scope; |
613 Scope* hoist_scope; | 594 Scope* hoist_scope; |
614 VariableMode mode; | 595 VariableMode mode; |
615 int declaration_pos; | 596 int declaration_pos; |
616 int initialization_pos; | 597 int initialization_pos; |
617 Kind declaration_kind; | 598 Kind declaration_kind; |
618 }; | 599 }; |
619 | 600 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
746 V8_INLINE DeclarationScope* GetClosureScope() const { | 727 V8_INLINE DeclarationScope* GetClosureScope() const { |
747 return scope()->GetClosureScope(); | 728 return scope()->GetClosureScope(); |
748 } | 729 } |
749 | 730 |
750 Scanner* scanner() const { return scanner_; } | 731 Scanner* scanner() const { return scanner_; } |
751 AstValueFactory* ast_value_factory() const { return ast_value_factory_; } | 732 AstValueFactory* ast_value_factory() const { return ast_value_factory_; } |
752 int position() const { return scanner_->location().beg_pos; } | 733 int position() const { return scanner_->location().beg_pos; } |
753 int peek_position() const { return scanner_->peek_location().beg_pos; } | 734 int peek_position() const { return scanner_->peek_location().beg_pos; } |
754 bool stack_overflow() const { return stack_overflow_; } | 735 bool stack_overflow() const { return stack_overflow_; } |
755 void set_stack_overflow() { stack_overflow_ = true; } | 736 void set_stack_overflow() { stack_overflow_ = true; } |
756 Mode mode() const { return mode_; } | |
757 | 737 |
758 INLINE(Token::Value peek()) { | 738 INLINE(Token::Value peek()) { |
759 if (stack_overflow_) return Token::ILLEGAL; | 739 if (stack_overflow_) return Token::ILLEGAL; |
760 return scanner()->peek(); | 740 return scanner()->peek(); |
761 } | 741 } |
762 | 742 |
763 INLINE(Token::Value PeekAhead()) { | 743 INLINE(Token::Value PeekAhead()) { |
764 if (stack_overflow_) return Token::ILLEGAL; | 744 if (stack_overflow_) return Token::ILLEGAL; |
765 return scanner()->PeekAhead(); | 745 return scanner()->PeekAhead(); |
766 } | 746 } |
(...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1433 } | 1413 } |
1434 | 1414 |
1435 // Parser base's protected field members. | 1415 // Parser base's protected field members. |
1436 | 1416 |
1437 ScopeState* scope_state_; // Scope stack. | 1417 ScopeState* scope_state_; // Scope stack. |
1438 FunctionState* function_state_; // Function state stack. | 1418 FunctionState* function_state_; // Function state stack. |
1439 v8::Extension* extension_; | 1419 v8::Extension* extension_; |
1440 FuncNameInferrer* fni_; | 1420 FuncNameInferrer* fni_; |
1441 AstValueFactory* ast_value_factory_; // Not owned. | 1421 AstValueFactory* ast_value_factory_; // Not owned. |
1442 typename Types::Factory ast_node_factory_; | 1422 typename Types::Factory ast_node_factory_; |
1443 Mode mode_; | |
1444 bool parsing_module_; | 1423 bool parsing_module_; |
1445 uintptr_t stack_limit_; | 1424 uintptr_t stack_limit_; |
1446 | 1425 |
1447 // Parser base's private field members. | 1426 // Parser base's private field members. |
1448 | 1427 |
1449 private: | 1428 private: |
1450 Zone* zone_; | 1429 Zone* zone_; |
1451 ExpressionClassifier* classifier_; | 1430 ExpressionClassifier* classifier_; |
1452 | 1431 |
1453 Scanner* scanner_; | 1432 Scanner* scanner_; |
(...skipping 2463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3917 return impl()->EmptyExpression(); | 3896 return impl()->EmptyExpression(); |
3918 } | 3897 } |
3919 | 3898 |
3920 StatementListT body = impl()->NullStatementList(); | 3899 StatementListT body = impl()->NullStatementList(); |
3921 int materialized_literal_count = -1; | 3900 int materialized_literal_count = -1; |
3922 int expected_property_count = -1; | 3901 int expected_property_count = -1; |
3923 | 3902 |
3924 FunctionKind kind = formal_parameters.scope->function_kind(); | 3903 FunctionKind kind = formal_parameters.scope->function_kind(); |
3925 FunctionLiteral::EagerCompileHint eager_compile_hint = | 3904 FunctionLiteral::EagerCompileHint eager_compile_hint = |
3926 default_eager_compile_hint_; | 3905 default_eager_compile_hint_; |
3927 bool can_preparse = mode() == PARSE_LAZILY && | 3906 bool can_preparse = impl()->parse_lazily() && |
3928 eager_compile_hint == FunctionLiteral::kShouldLazyCompile; | 3907 eager_compile_hint == FunctionLiteral::kShouldLazyCompile; |
3929 // TODO(marja): consider lazy-parsing inner arrow functions too. is_this | 3908 // TODO(marja): consider lazy-parsing inner arrow functions too. is_this |
3930 // handling in Scope::ResolveVariable needs to change. | 3909 // handling in Scope::ResolveVariable needs to change. |
3931 bool is_lazy_top_level_function = | 3910 bool is_lazy_top_level_function = |
3932 can_preparse && impl()->AllowsLazyParsingWithoutUnresolvedVariables(); | 3911 can_preparse && impl()->AllowsLazyParsingWithoutUnresolvedVariables(); |
3933 bool should_be_used_once_hint = false; | 3912 bool should_be_used_once_hint = false; |
3934 { | 3913 { |
3935 FunctionState function_state(&function_state_, &scope_state_, | 3914 FunctionState function_state(&function_state_, &scope_state_, |
3936 formal_parameters.scope); | 3915 formal_parameters.scope); |
3937 | 3916 |
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4398 if (!scope()->HasSimpleParameters()) { | 4377 if (!scope()->HasSimpleParameters()) { |
4399 // TC39 deemed "use strict" directives to be an error when occurring | 4378 // TC39 deemed "use strict" directives to be an error when occurring |
4400 // in the body of a function with non-simple parameter list, on | 4379 // in the body of a function with non-simple parameter list, on |
4401 // 29/7/2015. https://goo.gl/ueA7Ln | 4380 // 29/7/2015. https://goo.gl/ueA7Ln |
4402 impl()->ReportMessageAt( | 4381 impl()->ReportMessageAt( |
4403 token_loc, MessageTemplate::kIllegalLanguageModeDirective, | 4382 token_loc, MessageTemplate::kIllegalLanguageModeDirective, |
4404 "use strict"); | 4383 "use strict"); |
4405 *ok = false; | 4384 *ok = false; |
4406 return kLazyParsingComplete; | 4385 return kLazyParsingComplete; |
4407 } | 4386 } |
4408 // Because declarations in strict eval code don't leak into the scope | |
4409 // of the eval call, it is likely that functions declared in strict | |
4410 // eval code will be used within the eval code, so lazy parsing is | |
4411 // probably not a win. | |
4412 if (scope()->is_eval_scope()) mode_ = PARSE_EAGERLY; | |
4413 } else if (impl()->IsUseAsmDirective(stat) && | 4387 } else if (impl()->IsUseAsmDirective(stat) && |
4414 token_loc.end_pos - token_loc.beg_pos == | 4388 token_loc.end_pos - token_loc.beg_pos == |
4415 sizeof("use asm") + 1) { | 4389 sizeof("use asm") + 1) { |
4416 // Directive "use asm". | 4390 // Directive "use asm". |
4417 impl()->SetAsmModule(); | 4391 impl()->SetAsmModule(); |
4418 } else if (impl()->IsStringLiteral(stat)) { | 4392 } else if (impl()->IsStringLiteral(stat)) { |
4419 // Possibly an unknown directive. | 4393 // Possibly an unknown directive. |
4420 // Should not change mode, but will increment usage counters | 4394 // Should not change mode, but will increment usage counters |
4421 // as appropriate. Ditto usages below. | 4395 // as appropriate. Ditto usages below. |
4422 RaiseLanguageMode(SLOPPY); | 4396 RaiseLanguageMode(SLOPPY); |
(...skipping 1039 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5462 has_seen_constructor_ = true; | 5436 has_seen_constructor_ = true; |
5463 return; | 5437 return; |
5464 } | 5438 } |
5465 } | 5439 } |
5466 | 5440 |
5467 | 5441 |
5468 } // namespace internal | 5442 } // namespace internal |
5469 } // namespace v8 | 5443 } // namespace v8 |
5470 | 5444 |
5471 #endif // V8_PARSING_PARSER_BASE_H | 5445 #endif // V8_PARSING_PARSER_BASE_H |
OLD | NEW |