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/scopes.h" | 8 #include "src/ast/scopes.h" |
9 #include "src/bailout-reason.h" | 9 #include "src/bailout-reason.h" |
10 #include "src/base/hashmap.h" | 10 #include "src/base/hashmap.h" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 return kind & MethodKind::Static; | 104 return kind & MethodKind::Static; |
105 } | 105 } |
106 | 106 |
107 inline bool IsGeneratorMethod(MethodKind kind) { | 107 inline bool IsGeneratorMethod(MethodKind kind) { |
108 return kind & MethodKind::Generator; | 108 return kind & MethodKind::Generator; |
109 } | 109 } |
110 | 110 |
111 inline bool IsAsyncMethod(MethodKind kind) { return kind & MethodKind::Async; } | 111 inline bool IsAsyncMethod(MethodKind kind) { return kind & MethodKind::Async; } |
112 | 112 |
113 struct FormalParametersBase { | 113 struct FormalParametersBase { |
114 explicit FormalParametersBase(DeclarationScope* scope) : scope(scope) {} | 114 explicit FormalParametersBase(Scope* scope) : scope(scope) {} |
115 DeclarationScope* scope; | 115 Scope* scope; |
116 bool has_rest = false; | 116 bool has_rest = false; |
117 bool is_simple = true; | 117 bool is_simple = true; |
118 int materialized_literals_count = 0; | 118 int materialized_literals_count = 0; |
119 }; | 119 }; |
120 | 120 |
121 | 121 |
122 // ---------------------------------------------------------------------------- | 122 // ---------------------------------------------------------------------------- |
123 // The CHECK_OK macro is a convenient macro to enforce error | 123 // The CHECK_OK macro is a convenient macro to enforce error |
124 // handling for functions that may fail (by returning !*ok). | 124 // handling for functions that may fail (by returning !*ok). |
125 // | 125 // |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 protected: | 280 protected: |
281 ScopeState(ScopeState** scope_stack, Scope* scope) | 281 ScopeState(ScopeState** scope_stack, Scope* scope) |
282 : scope_stack_(scope_stack), outer_scope_(*scope_stack), scope_(scope) { | 282 : scope_stack_(scope_stack), outer_scope_(*scope_stack), scope_(scope) { |
283 *scope_stack = this; | 283 *scope_stack = this; |
284 } | 284 } |
285 ~ScopeState() { *scope_stack_ = outer_scope_; } | 285 ~ScopeState() { *scope_stack_ = outer_scope_; } |
286 | 286 |
287 private: | 287 private: |
288 ScopeState** const scope_stack_; | 288 ScopeState** const scope_stack_; |
289 ScopeState* const outer_scope_; | 289 ScopeState* const outer_scope_; |
290 Scope* const scope_; | 290 Scope* scope_; |
291 }; | 291 }; |
292 | 292 |
293 class BlockState final : public ScopeState { | 293 class BlockState final : public ScopeState { |
294 public: | 294 public: |
295 BlockState(ScopeState** scope_stack, Scope* scope) | 295 BlockState(ScopeState** scope_stack, Scope* scope) |
296 : ScopeState(scope_stack, scope) {} | 296 : ScopeState(scope_stack, scope) {} |
297 | 297 |
298 // BlockState(ScopeState**) automatically manages Scope(BLOCK_SCOPE) | 298 // BlockState(ScopeState**) automatically manages Scope(BLOCK_SCOPE) |
299 // allocation. | 299 // allocation. |
300 // TODO(verwaest): Move to LazyBlockState class that only allocates the | 300 // TODO(verwaest): Move to LazyBlockState class that only allocates the |
301 // scope when needed. | 301 // scope when needed. |
302 explicit BlockState(ScopeState** scope_stack) | 302 explicit BlockState(ScopeState** scope_stack) |
303 : ScopeState(scope_stack, NewScope(*scope_stack)) {} | 303 : ScopeState(scope_stack, NewScope(*scope_stack)) {} |
304 | 304 |
305 void SetNonlinear() { this->scope()->SetNonlinear(); } | 305 void SetNonlinear() { this->scope()->SetNonlinear(); } |
306 void set_start_position(int pos) { this->scope()->set_start_position(pos); } | 306 void set_start_position(int pos) { this->scope()->set_start_position(pos); } |
307 void set_end_position(int pos) { this->scope()->set_end_position(pos); } | 307 void set_end_position(int pos) { this->scope()->set_end_position(pos); } |
308 void set_is_hidden() { this->scope()->set_is_hidden(); } | 308 void set_is_hidden() { this->scope()->set_is_hidden(); } |
309 Scope* FinalizedBlockScope() const { | 309 Scope* FinalizedBlockScope() const { |
310 return this->scope()->FinalizeBlockScope(); | 310 return this->scope()->FinalizeBlockScope(); |
311 } | 311 } |
312 LanguageMode language_mode() const { | 312 LanguageMode language_mode() const { |
313 return this->scope()->language_mode(); | 313 return this->scope()->language_mode(); |
314 } | 314 } |
315 | 315 |
316 private: | 316 private: |
317 Scope* NewScope(ScopeState* outer_state) { | 317 Scope* NewScope(ScopeState* outer_state) { |
318 Scope* parent = outer_state->scope(); | 318 Scope* parent = outer_state->scope(); |
319 Zone* zone = outer_state->zone(); | 319 Zone* zone = outer_state->zone(); |
320 return new (zone) Scope(zone, parent, BLOCK_SCOPE); | 320 return new (zone) Scope(zone, parent, BLOCK_SCOPE, kNormalFunction); |
321 } | 321 } |
322 }; | 322 }; |
323 | 323 |
324 struct DestructuringAssignment { | 324 struct DestructuringAssignment { |
325 public: | 325 public: |
326 DestructuringAssignment(ExpressionT expression, Scope* scope) | 326 DestructuringAssignment(ExpressionT expression, Scope* scope) |
327 : assignment(expression), scope(scope) {} | 327 : assignment(expression), scope(scope) {} |
328 | 328 |
329 ExpressionT assignment; | 329 ExpressionT assignment; |
330 Scope* scope; | 330 Scope* scope; |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 } | 604 } |
605 ~ParsingModeScope() { | 605 ~ParsingModeScope() { |
606 parser_->mode_ = old_mode_; | 606 parser_->mode_ = old_mode_; |
607 } | 607 } |
608 | 608 |
609 private: | 609 private: |
610 ParserBase* parser_; | 610 ParserBase* parser_; |
611 Mode old_mode_; | 611 Mode old_mode_; |
612 }; | 612 }; |
613 | 613 |
614 DeclarationScope* NewScriptScope() { | 614 Scope* NewScriptScope() { |
615 return new (zone()) DeclarationScope(zone(), nullptr, SCRIPT_SCOPE); | 615 return new (zone()) Scope(zone(), nullptr, SCRIPT_SCOPE, kNormalFunction); |
616 } | |
617 | |
618 DeclarationScope* NewVarblockScope() { | |
619 return new (zone()) DeclarationScope(zone(), scope(), BLOCK_SCOPE); | |
620 } | |
621 | |
622 DeclarationScope* NewModuleScope(Scope* parent) { | |
623 DeclarationScope* result = | |
624 new (zone()) DeclarationScope(zone(), parent, MODULE_SCOPE); | |
625 // TODO(verwaest): Move into the DeclarationScope constructor. | |
626 result->DeclareThis(ast_value_factory()); | |
627 return result; | |
628 } | |
629 | |
630 DeclarationScope* NewEvalScope(Scope* parent) { | |
631 return new (zone()) DeclarationScope(zone(), parent, EVAL_SCOPE); | |
632 } | 616 } |
633 | 617 |
634 Scope* NewScope(ScopeType scope_type) { | 618 Scope* NewScope(ScopeType scope_type) { |
635 return NewScopeWithParent(scope(), scope_type); | 619 return NewScopeWithParent(scope(), scope_type); |
636 } | 620 } |
637 | 621 |
638 // This constructor should only be used when absolutely necessary. Most scopes | 622 // This constructor should only be used when absolutely necessary. Most scopes |
639 // should automatically use scope() as parent, and be fine with | 623 // should automatically use scope() as parent, and be fine with |
640 // NewScope(ScopeType) above. | 624 // NewScope(ScopeType) above. |
641 Scope* NewScopeWithParent(Scope* parent, ScopeType scope_type) { | 625 Scope* NewScopeWithParent(Scope* parent, ScopeType scope_type) { |
642 // Must always use the specific constructors for the blacklisted scope | 626 // Must always use the specific constructors for the blacklisted scope |
643 // types. | 627 // types. |
644 DCHECK_NE(FUNCTION_SCOPE, scope_type); | 628 DCHECK_NE(FUNCTION_SCOPE, scope_type); |
645 DCHECK_NE(SCRIPT_SCOPE, scope_type); | 629 DCHECK_NE(SCRIPT_SCOPE, scope_type); |
646 DCHECK_NE(MODULE_SCOPE, scope_type); | |
647 DCHECK_NOT_NULL(parent); | 630 DCHECK_NOT_NULL(parent); |
648 return new (zone()) Scope(zone(), parent, scope_type); | 631 Scope* result = |
| 632 new (zone()) Scope(zone(), parent, scope_type, kNormalFunction); |
| 633 if (scope_type == MODULE_SCOPE) result->DeclareThis(ast_value_factory()); |
| 634 return result; |
649 } | 635 } |
650 | 636 |
651 DeclarationScope* NewFunctionScope(FunctionKind kind) { | 637 Scope* NewFunctionScope(FunctionKind kind) { |
652 DCHECK(ast_value_factory()); | 638 DCHECK(ast_value_factory()); |
653 DeclarationScope* result = | 639 Scope* result = new (zone()) Scope(zone(), scope(), FUNCTION_SCOPE, kind); |
654 new (zone()) DeclarationScope(zone(), scope(), FUNCTION_SCOPE, kind); | |
655 // TODO(verwaest): Move into the DeclarationScope constructor. | |
656 if (!IsArrowFunction(kind)) { | 640 if (!IsArrowFunction(kind)) { |
657 result->DeclareThis(ast_value_factory()); | 641 result->DeclareThis(ast_value_factory()); |
658 result->DeclareDefaultFunctionVariables(ast_value_factory()); | 642 result->DeclareDefaultFunctionVariables(ast_value_factory()); |
659 } | 643 } |
660 return result; | 644 return result; |
661 } | 645 } |
662 | 646 |
663 Scanner* scanner() const { return scanner_; } | 647 Scanner* scanner() const { return scanner_; } |
664 AstValueFactory* ast_value_factory() const { return ast_value_factory_; } | 648 AstValueFactory* ast_value_factory() const { return ast_value_factory_; } |
665 int position() { return scanner_->location().beg_pos; } | 649 int position() { return scanner_->location().beg_pos; } |
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1156 // Keep track of eval() calls since they disable all local variable | 1140 // Keep track of eval() calls since they disable all local variable |
1157 // optimizations. This checks if expression is an eval call, and if yes, | 1141 // optimizations. This checks if expression is an eval call, and if yes, |
1158 // forwards the information to scope. | 1142 // forwards the information to scope. |
1159 void CheckPossibleEvalCall(ExpressionT expression, Scope* scope) { | 1143 void CheckPossibleEvalCall(ExpressionT expression, Scope* scope) { |
1160 if (Traits::IsIdentifier(expression) && | 1144 if (Traits::IsIdentifier(expression) && |
1161 Traits::IsEval(Traits::AsIdentifier(expression))) { | 1145 Traits::IsEval(Traits::AsIdentifier(expression))) { |
1162 scope->RecordEvalCall(); | 1146 scope->RecordEvalCall(); |
1163 if (is_sloppy(scope->language_mode())) { | 1147 if (is_sloppy(scope->language_mode())) { |
1164 // For sloppy scopes we also have to record the call at function level, | 1148 // For sloppy scopes we also have to record the call at function level, |
1165 // in case it includes declarations that will be hoisted. | 1149 // in case it includes declarations that will be hoisted. |
1166 scope->GetDeclarationScope()->RecordEvalCall(); | 1150 scope->DeclarationScope()->RecordEvalCall(); |
1167 } | 1151 } |
1168 } | 1152 } |
1169 } | 1153 } |
1170 | 1154 |
1171 // Used to validate property names in object literals and class literals | 1155 // Used to validate property names in object literals and class literals |
1172 enum PropertyKind { | 1156 enum PropertyKind { |
1173 kAccessorProperty, | 1157 kAccessorProperty, |
1174 kValueProperty, | 1158 kValueProperty, |
1175 kMethodProperty | 1159 kMethodProperty |
1176 }; | 1160 }; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1220 bool IsConstructor() { | 1204 bool IsConstructor() { |
1221 return this->scanner()->LiteralMatches("constructor", 11); | 1205 return this->scanner()->LiteralMatches("constructor", 11); |
1222 } | 1206 } |
1223 bool IsPrototype() { | 1207 bool IsPrototype() { |
1224 return this->scanner()->LiteralMatches("prototype", 9); | 1208 return this->scanner()->LiteralMatches("prototype", 9); |
1225 } | 1209 } |
1226 | 1210 |
1227 bool has_seen_constructor_; | 1211 bool has_seen_constructor_; |
1228 }; | 1212 }; |
1229 | 1213 |
1230 ModuleDescriptor* module() const { | 1214 ModuleDescriptor* module() const { return scope()->module(); } |
1231 return scope()->AsDeclarationScope()->module(); | |
1232 } | |
1233 Scope* scope() const { return scope_state_->scope(); } | 1215 Scope* scope() const { return scope_state_->scope(); } |
1234 | 1216 |
1235 ScopeState* scope_state_; // Scope stack. | 1217 ScopeState* scope_state_; // Scope stack. |
1236 FunctionState* function_state_; // Function state stack. | 1218 FunctionState* function_state_; // Function state stack. |
1237 v8::Extension* extension_; | 1219 v8::Extension* extension_; |
1238 FuncNameInferrer* fni_; | 1220 FuncNameInferrer* fni_; |
1239 AstValueFactory* ast_value_factory_; // Not owned. | 1221 AstValueFactory* ast_value_factory_; // Not owned. |
1240 typename Traits::Type::Factory ast_node_factory_; | 1222 typename Traits::Type::Factory ast_node_factory_; |
1241 ParserRecorder* log_; | 1223 ParserRecorder* log_; |
1242 Mode mode_; | 1224 Mode mode_; |
(...skipping 1081 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2324 parenthesized_formals, is_async, CHECK_OK); | 2306 parenthesized_formals, is_async, CHECK_OK); |
2325 // This reads strangely, but is correct: it checks whether any | 2307 // This reads strangely, but is correct: it checks whether any |
2326 // sub-expression of the parameter list failed to be a valid formal | 2308 // sub-expression of the parameter list failed to be a valid formal |
2327 // parameter initializer. Since YieldExpressions are banned anywhere | 2309 // parameter initializer. Since YieldExpressions are banned anywhere |
2328 // in an arrow parameter list, this is correct. | 2310 // in an arrow parameter list, this is correct. |
2329 // TODO(adamk): Rename "FormalParameterInitializerError" to refer to | 2311 // TODO(adamk): Rename "FormalParameterInitializerError" to refer to |
2330 // "YieldExpression", which is its only use. | 2312 // "YieldExpression", which is its only use. |
2331 ValidateFormalParameterInitializer(&arrow_formals_classifier, ok); | 2313 ValidateFormalParameterInitializer(&arrow_formals_classifier, ok); |
2332 | 2314 |
2333 Scanner::Location loc(lhs_beg_pos, scanner()->location().end_pos); | 2315 Scanner::Location loc(lhs_beg_pos, scanner()->location().end_pos); |
2334 DeclarationScope* scope = | 2316 Scope* scope = |
2335 this->NewFunctionScope(is_async ? FunctionKind::kAsyncArrowFunction | 2317 this->NewFunctionScope(is_async ? FunctionKind::kAsyncArrowFunction |
2336 : FunctionKind::kArrowFunction); | 2318 : FunctionKind::kArrowFunction); |
2337 // Because the arrow's parameters were parsed in the outer scope, any | 2319 // Because the arrow's parameters were parsed in the outer scope, any |
2338 // usage flags that might have been triggered there need to be copied | 2320 // usage flags that might have been triggered there need to be copied |
2339 // to the arrow scope. | 2321 // to the arrow scope. |
2340 this->scope()->PropagateUsageFlagsToScope(scope); | 2322 this->scope()->PropagateUsageFlagsToScope(scope); |
2341 FormalParametersT parameters(scope); | 2323 FormalParametersT parameters(scope); |
2342 if (!arrow_formals_classifier.is_simple_parameter_list()) { | 2324 if (!arrow_formals_classifier.is_simple_parameter_list()) { |
2343 scope->SetHasNonSimpleParameters(); | 2325 scope->SetHasNonSimpleParameters(); |
2344 parameters.is_simple = false; | 2326 parameters.is_simple = false; |
(...skipping 744 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3089 ParseMemberExpressionContinuation(result, is_async, classifier, CHECK_OK); | 3071 ParseMemberExpressionContinuation(result, is_async, classifier, CHECK_OK); |
3090 return result; | 3072 return result; |
3091 } | 3073 } |
3092 | 3074 |
3093 template <class Traits> | 3075 template <class Traits> |
3094 typename ParserBase<Traits>::ExpressionT | 3076 typename ParserBase<Traits>::ExpressionT |
3095 ParserBase<Traits>::ParseSuperExpression(bool is_new, bool* ok) { | 3077 ParserBase<Traits>::ParseSuperExpression(bool is_new, bool* ok) { |
3096 Expect(Token::SUPER, CHECK_OK); | 3078 Expect(Token::SUPER, CHECK_OK); |
3097 int pos = position(); | 3079 int pos = position(); |
3098 | 3080 |
3099 DeclarationScope* scope = this->scope()->GetReceiverScope(); | 3081 Scope* scope = this->scope()->ReceiverScope(); |
3100 FunctionKind kind = scope->function_kind(); | 3082 FunctionKind kind = scope->function_kind(); |
3101 if (IsConciseMethod(kind) || IsAccessorFunction(kind) || | 3083 if (IsConciseMethod(kind) || IsAccessorFunction(kind) || |
3102 IsClassConstructor(kind)) { | 3084 IsClassConstructor(kind)) { |
3103 if (peek() == Token::PERIOD || peek() == Token::LBRACK) { | 3085 if (peek() == Token::PERIOD || peek() == Token::LBRACK) { |
3104 scope->RecordSuperPropertyUsage(); | 3086 scope->RecordSuperPropertyUsage(); |
3105 return this->NewSuperPropertyReference(this->scope(), factory(), pos); | 3087 return this->NewSuperPropertyReference(this->scope(), factory(), pos); |
3106 } | 3088 } |
3107 // new super() is never allowed. | 3089 // new super() is never allowed. |
3108 // super() is only allowed in derived constructor | 3090 // super() is only allowed in derived constructor |
3109 if (!is_new && peek() == Token::LPAREN && IsSubclassConstructor(kind)) { | 3091 if (!is_new && peek() == Token::LPAREN && IsSubclassConstructor(kind)) { |
(...skipping 21 matching lines...) Expand all Loading... |
3131 *ok = false; | 3113 *ok = false; |
3132 } | 3114 } |
3133 } | 3115 } |
3134 | 3116 |
3135 template <class Traits> | 3117 template <class Traits> |
3136 typename ParserBase<Traits>::ExpressionT | 3118 typename ParserBase<Traits>::ExpressionT |
3137 ParserBase<Traits>::ParseNewTargetExpression(bool* ok) { | 3119 ParserBase<Traits>::ParseNewTargetExpression(bool* ok) { |
3138 int pos = position(); | 3120 int pos = position(); |
3139 ExpectMetaProperty(CStrVector("target"), "new.target", pos, CHECK_OK); | 3121 ExpectMetaProperty(CStrVector("target"), "new.target", pos, CHECK_OK); |
3140 | 3122 |
3141 if (!scope()->GetReceiverScope()->is_function_scope()) { | 3123 if (!scope()->ReceiverScope()->is_function_scope()) { |
3142 ReportMessageAt(scanner()->location(), | 3124 ReportMessageAt(scanner()->location(), |
3143 MessageTemplate::kUnexpectedNewTarget); | 3125 MessageTemplate::kUnexpectedNewTarget); |
3144 *ok = false; | 3126 *ok = false; |
3145 return this->EmptyExpression(); | 3127 return this->EmptyExpression(); |
3146 } | 3128 } |
3147 | 3129 |
3148 return this->NewTargetExpression(scope(), factory(), pos); | 3130 return this->NewTargetExpression(scope(), factory(), pos); |
3149 } | 3131 } |
3150 | 3132 |
3151 template <class Traits> | 3133 template <class Traits> |
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3693 has_seen_constructor_ = true; | 3675 has_seen_constructor_ = true; |
3694 return; | 3676 return; |
3695 } | 3677 } |
3696 } | 3678 } |
3697 | 3679 |
3698 | 3680 |
3699 } // namespace internal | 3681 } // namespace internal |
3700 } // namespace v8 | 3682 } // namespace v8 |
3701 | 3683 |
3702 #endif // V8_PARSING_PARSER_BASE_H | 3684 #endif // V8_PARSING_PARSER_BASE_H |
OLD | NEW |