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

Side by Side Diff: src/parsing/parser-base.h

Issue 2209573002: Separate Scope into DeclarationScope and Scope (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Addressed comments Created 4 years, 4 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
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 #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
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 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 return this->scope()->FinalizeBlockScope(); 318 return this->scope()->FinalizeBlockScope();
319 } 319 }
320 LanguageMode language_mode() const { 320 LanguageMode language_mode() const {
321 return this->scope()->language_mode(); 321 return this->scope()->language_mode();
322 } 322 }
323 323
324 private: 324 private:
325 Scope* NewScope(ScopeState* outer_state) { 325 Scope* NewScope(ScopeState* outer_state) {
326 Scope* parent = outer_state->scope(); 326 Scope* parent = outer_state->scope();
327 Zone* zone = outer_state->zone(); 327 Zone* zone = outer_state->zone();
328 return new (zone) Scope(zone, parent, BLOCK_SCOPE, kNormalFunction); 328 return new (zone) Scope(zone, parent, BLOCK_SCOPE);
329 } 329 }
330 }; 330 };
331 331
332 struct DestructuringAssignment { 332 struct DestructuringAssignment {
333 public: 333 public:
334 DestructuringAssignment(ExpressionT expression, Scope* scope) 334 DestructuringAssignment(ExpressionT expression, Scope* scope)
335 : assignment(expression), scope(scope) {} 335 : assignment(expression), scope(scope) {}
336 336
337 ExpressionT assignment; 337 ExpressionT assignment;
338 Scope* scope; 338 Scope* scope;
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 } 612 }
613 ~ParsingModeScope() { 613 ~ParsingModeScope() {
614 parser_->mode_ = old_mode_; 614 parser_->mode_ = old_mode_;
615 } 615 }
616 616
617 private: 617 private:
618 ParserBase* parser_; 618 ParserBase* parser_;
619 Mode old_mode_; 619 Mode old_mode_;
620 }; 620 };
621 621
622 Scope* NewScriptScope() { 622 DeclarationScope* NewScriptScope() {
623 return new (zone()) Scope(zone(), nullptr, SCRIPT_SCOPE, kNormalFunction); 623 return new (zone()) DeclarationScope(zone(), nullptr, SCRIPT_SCOPE);
624 }
625
626 DeclarationScope* NewVarblockScope() {
627 return new (zone()) DeclarationScope(zone(), scope(), BLOCK_SCOPE);
628 }
629
630 DeclarationScope* NewModuleScope(Scope* parent) {
631 DeclarationScope* result =
632 new (zone()) DeclarationScope(zone(), parent, MODULE_SCOPE);
633 // TODO(verwaest): Move into the DeclarationScope constructor.
634 result->DeclareThis(ast_value_factory());
635 return result;
636 }
637
638 DeclarationScope* NewEvalScope(Scope* parent) {
639 return new (zone()) DeclarationScope(zone(), parent, EVAL_SCOPE);
624 } 640 }
625 641
626 Scope* NewScope(ScopeType scope_type) { 642 Scope* NewScope(ScopeType scope_type) {
627 return NewScopeWithParent(scope(), scope_type); 643 return NewScopeWithParent(scope(), scope_type);
628 } 644 }
629 645
630 // This constructor should only be used when absolutely necessary. Most scopes 646 // This constructor should only be used when absolutely necessary. Most scopes
631 // should automatically use scope() as parent, and be fine with 647 // should automatically use scope() as parent, and be fine with
632 // NewScope(ScopeType) above. 648 // NewScope(ScopeType) above.
633 Scope* NewScopeWithParent(Scope* parent, ScopeType scope_type) { 649 Scope* NewScopeWithParent(Scope* parent, ScopeType scope_type) {
634 // Must always use the specific constructors for the blacklisted scope 650 // Must always use the specific constructors for the blacklisted scope
635 // types. 651 // types.
636 DCHECK_NE(FUNCTION_SCOPE, scope_type); 652 DCHECK_NE(FUNCTION_SCOPE, scope_type);
637 DCHECK_NE(SCRIPT_SCOPE, scope_type); 653 DCHECK_NE(SCRIPT_SCOPE, scope_type);
654 DCHECK_NE(MODULE_SCOPE, scope_type);
638 DCHECK_NOT_NULL(parent); 655 DCHECK_NOT_NULL(parent);
639 Scope* result = 656 return new (zone()) Scope(zone(), parent, scope_type);
640 new (zone()) Scope(zone(), parent, scope_type, kNormalFunction);
641 if (scope_type == MODULE_SCOPE) result->DeclareThis(ast_value_factory());
642 return result;
643 } 657 }
644 658
645 Scope* NewFunctionScope(FunctionKind kind) { 659 DeclarationScope* NewFunctionScope(FunctionKind kind) {
646 DCHECK(ast_value_factory()); 660 DCHECK(ast_value_factory());
647 Scope* result = new (zone()) Scope(zone(), scope(), FUNCTION_SCOPE, kind); 661 DeclarationScope* result =
662 new (zone()) DeclarationScope(zone(), scope(), FUNCTION_SCOPE, kind);
663 // TODO(verwaest): Move into the DeclarationScope constructor.
648 if (!IsArrowFunction(kind)) { 664 if (!IsArrowFunction(kind)) {
649 result->DeclareThis(ast_value_factory()); 665 result->DeclareThis(ast_value_factory());
650 result->DeclareDefaultFunctionVariables(ast_value_factory()); 666 result->DeclareDefaultFunctionVariables(ast_value_factory());
651 } 667 }
652 return result; 668 return result;
653 } 669 }
654 670
655 Scanner* scanner() const { return scanner_; } 671 Scanner* scanner() const { return scanner_; }
656 AstValueFactory* ast_value_factory() const { return ast_value_factory_; } 672 AstValueFactory* ast_value_factory() const { return ast_value_factory_; }
657 int position() { return scanner_->location().beg_pos; } 673 int position() { return scanner_->location().beg_pos; }
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after
1148 // Keep track of eval() calls since they disable all local variable 1164 // Keep track of eval() calls since they disable all local variable
1149 // optimizations. This checks if expression is an eval call, and if yes, 1165 // optimizations. This checks if expression is an eval call, and if yes,
1150 // forwards the information to scope. 1166 // forwards the information to scope.
1151 void CheckPossibleEvalCall(ExpressionT expression, Scope* scope) { 1167 void CheckPossibleEvalCall(ExpressionT expression, Scope* scope) {
1152 if (Traits::IsIdentifier(expression) && 1168 if (Traits::IsIdentifier(expression) &&
1153 Traits::IsEval(Traits::AsIdentifier(expression))) { 1169 Traits::IsEval(Traits::AsIdentifier(expression))) {
1154 scope->RecordEvalCall(); 1170 scope->RecordEvalCall();
1155 if (is_sloppy(scope->language_mode())) { 1171 if (is_sloppy(scope->language_mode())) {
1156 // For sloppy scopes we also have to record the call at function level, 1172 // For sloppy scopes we also have to record the call at function level,
1157 // in case it includes declarations that will be hoisted. 1173 // in case it includes declarations that will be hoisted.
1158 scope->DeclarationScope()->RecordEvalCall(); 1174 scope->GetDeclarationScope()->RecordEvalCall();
1159 } 1175 }
1160 } 1176 }
1161 } 1177 }
1162 1178
1163 // Used to validate property names in object literals and class literals 1179 // Used to validate property names in object literals and class literals
1164 enum PropertyKind { 1180 enum PropertyKind {
1165 kAccessorProperty, 1181 kAccessorProperty,
1166 kValueProperty, 1182 kValueProperty,
1167 kMethodProperty 1183 kMethodProperty
1168 }; 1184 };
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1212 bool IsConstructor() { 1228 bool IsConstructor() {
1213 return this->scanner()->LiteralMatches("constructor", 11); 1229 return this->scanner()->LiteralMatches("constructor", 11);
1214 } 1230 }
1215 bool IsPrototype() { 1231 bool IsPrototype() {
1216 return this->scanner()->LiteralMatches("prototype", 9); 1232 return this->scanner()->LiteralMatches("prototype", 9);
1217 } 1233 }
1218 1234
1219 bool has_seen_constructor_; 1235 bool has_seen_constructor_;
1220 }; 1236 };
1221 1237
1222 ModuleDescriptor* module() const { return scope()->module(); } 1238 ModuleDescriptor* module() const {
1239 return scope()->AsDeclarationScope()->module();
1240 }
1223 Scope* scope() const { return scope_state_->scope(); } 1241 Scope* scope() const { return scope_state_->scope(); }
1224 1242
1225 ScopeState* scope_state_; // Scope stack. 1243 ScopeState* scope_state_; // Scope stack.
1226 FunctionState* function_state_; // Function state stack. 1244 FunctionState* function_state_; // Function state stack.
1227 v8::Extension* extension_; 1245 v8::Extension* extension_;
1228 FuncNameInferrer* fni_; 1246 FuncNameInferrer* fni_;
1229 AstValueFactory* ast_value_factory_; // Not owned. 1247 AstValueFactory* ast_value_factory_; // Not owned.
1230 typename Traits::Type::Factory ast_node_factory_; 1248 typename Traits::Type::Factory ast_node_factory_;
1231 ParserRecorder* log_; 1249 ParserRecorder* log_;
1232 Mode mode_; 1250 Mode mode_;
(...skipping 1081 matching lines...) Expand 10 before | Expand all | Expand 10 after
2314 parenthesized_formals, is_async, CHECK_OK); 2332 parenthesized_formals, is_async, CHECK_OK);
2315 // This reads strangely, but is correct: it checks whether any 2333 // This reads strangely, but is correct: it checks whether any
2316 // sub-expression of the parameter list failed to be a valid formal 2334 // sub-expression of the parameter list failed to be a valid formal
2317 // parameter initializer. Since YieldExpressions are banned anywhere 2335 // parameter initializer. Since YieldExpressions are banned anywhere
2318 // in an arrow parameter list, this is correct. 2336 // in an arrow parameter list, this is correct.
2319 // TODO(adamk): Rename "FormalParameterInitializerError" to refer to 2337 // TODO(adamk): Rename "FormalParameterInitializerError" to refer to
2320 // "YieldExpression", which is its only use. 2338 // "YieldExpression", which is its only use.
2321 ValidateFormalParameterInitializer(&arrow_formals_classifier, ok); 2339 ValidateFormalParameterInitializer(&arrow_formals_classifier, ok);
2322 2340
2323 Scanner::Location loc(lhs_beg_pos, scanner()->location().end_pos); 2341 Scanner::Location loc(lhs_beg_pos, scanner()->location().end_pos);
2324 Scope* scope = 2342 DeclarationScope* scope =
2325 this->NewFunctionScope(is_async ? FunctionKind::kAsyncArrowFunction 2343 this->NewFunctionScope(is_async ? FunctionKind::kAsyncArrowFunction
2326 : FunctionKind::kArrowFunction); 2344 : FunctionKind::kArrowFunction);
2327 // Because the arrow's parameters were parsed in the outer scope, any 2345 // Because the arrow's parameters were parsed in the outer scope, any
2328 // usage flags that might have been triggered there need to be copied 2346 // usage flags that might have been triggered there need to be copied
2329 // to the arrow scope. 2347 // to the arrow scope.
2330 this->scope()->PropagateUsageFlagsToScope(scope); 2348 this->scope()->PropagateUsageFlagsToScope(scope);
2331 FormalParametersT parameters(scope); 2349 FormalParametersT parameters(scope);
2332 if (!arrow_formals_classifier.is_simple_parameter_list()) { 2350 if (!arrow_formals_classifier.is_simple_parameter_list()) {
2333 scope->SetHasNonSimpleParameters(); 2351 scope->SetHasNonSimpleParameters();
2334 parameters.is_simple = false; 2352 parameters.is_simple = false;
(...skipping 744 matching lines...) Expand 10 before | Expand all | Expand 10 after
3079 ParseMemberExpressionContinuation(result, is_async, classifier, CHECK_OK); 3097 ParseMemberExpressionContinuation(result, is_async, classifier, CHECK_OK);
3080 return result; 3098 return result;
3081 } 3099 }
3082 3100
3083 template <class Traits> 3101 template <class Traits>
3084 typename ParserBase<Traits>::ExpressionT 3102 typename ParserBase<Traits>::ExpressionT
3085 ParserBase<Traits>::ParseSuperExpression(bool is_new, bool* ok) { 3103 ParserBase<Traits>::ParseSuperExpression(bool is_new, bool* ok) {
3086 Expect(Token::SUPER, CHECK_OK); 3104 Expect(Token::SUPER, CHECK_OK);
3087 int pos = position(); 3105 int pos = position();
3088 3106
3089 Scope* scope = this->scope()->ReceiverScope(); 3107 DeclarationScope* scope = this->scope()->GetReceiverScope();
3090 FunctionKind kind = scope->function_kind(); 3108 FunctionKind kind = scope->function_kind();
3091 if (IsConciseMethod(kind) || IsAccessorFunction(kind) || 3109 if (IsConciseMethod(kind) || IsAccessorFunction(kind) ||
3092 IsClassConstructor(kind)) { 3110 IsClassConstructor(kind)) {
3093 if (peek() == Token::PERIOD || peek() == Token::LBRACK) { 3111 if (peek() == Token::PERIOD || peek() == Token::LBRACK) {
3094 scope->RecordSuperPropertyUsage(); 3112 scope->RecordSuperPropertyUsage();
3095 return this->NewSuperPropertyReference(this->scope(), factory(), pos); 3113 return this->NewSuperPropertyReference(this->scope(), factory(), pos);
3096 } 3114 }
3097 // new super() is never allowed. 3115 // new super() is never allowed.
3098 // super() is only allowed in derived constructor 3116 // super() is only allowed in derived constructor
3099 if (!is_new && peek() == Token::LPAREN && IsSubclassConstructor(kind)) { 3117 if (!is_new && peek() == Token::LPAREN && IsSubclassConstructor(kind)) {
(...skipping 21 matching lines...) Expand all
3121 *ok = false; 3139 *ok = false;
3122 } 3140 }
3123 } 3141 }
3124 3142
3125 template <class Traits> 3143 template <class Traits>
3126 typename ParserBase<Traits>::ExpressionT 3144 typename ParserBase<Traits>::ExpressionT
3127 ParserBase<Traits>::ParseNewTargetExpression(bool* ok) { 3145 ParserBase<Traits>::ParseNewTargetExpression(bool* ok) {
3128 int pos = position(); 3146 int pos = position();
3129 ExpectMetaProperty(CStrVector("target"), "new.target", pos, CHECK_OK); 3147 ExpectMetaProperty(CStrVector("target"), "new.target", pos, CHECK_OK);
3130 3148
3131 if (!scope()->ReceiverScope()->is_function_scope()) { 3149 if (!scope()->GetReceiverScope()->is_function_scope()) {
3132 ReportMessageAt(scanner()->location(), 3150 ReportMessageAt(scanner()->location(),
3133 MessageTemplate::kUnexpectedNewTarget); 3151 MessageTemplate::kUnexpectedNewTarget);
3134 *ok = false; 3152 *ok = false;
3135 return this->EmptyExpression(); 3153 return this->EmptyExpression();
3136 } 3154 }
3137 3155
3138 return this->NewTargetExpression(scope(), factory(), pos); 3156 return this->NewTargetExpression(scope(), factory(), pos);
3139 } 3157 }
3140 3158
3141 template <class Traits> 3159 template <class Traits>
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after
3683 has_seen_constructor_ = true; 3701 has_seen_constructor_ = true;
3684 return; 3702 return;
3685 } 3703 }
3686 } 3704 }
3687 3705
3688 3706
3689 } // namespace internal 3707 } // namespace internal
3690 } // namespace v8 3708 } // namespace v8
3691 3709
3692 #endif // V8_PARSING_PARSER_BASE_H 3710 #endif // V8_PARSING_PARSER_BASE_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698