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