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-expression-rewriter.h" | 10 #include "src/ast/ast-expression-rewriter.h" |
(...skipping 4218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4229 extends = ParseLeftHandSideExpression(CHECK_OK); | 4229 extends = ParseLeftHandSideExpression(CHECK_OK); |
4230 CheckNoTailCallExpressions(CHECK_OK); | 4230 CheckNoTailCallExpressions(CHECK_OK); |
4231 RewriteNonPattern(CHECK_OK); | 4231 RewriteNonPattern(CHECK_OK); |
4232 impl()->AccumulateFormalParameterContainmentErrors(); | 4232 impl()->AccumulateFormalParameterContainmentErrors(); |
4233 } else { | 4233 } else { |
4234 block_state.set_start_position(scanner()->location().end_pos); | 4234 block_state.set_start_position(scanner()->location().end_pos); |
4235 } | 4235 } |
4236 | 4236 |
4237 | 4237 |
4238 ClassLiteralChecker checker(this); | 4238 ClassLiteralChecker checker(this); |
4239 ZoneList<ObjectLiteral::Property*>* properties = NewPropertyList(4); | 4239 ZoneList<ClassLiteral::Property*>* properties = NewClassPropertyList(4); |
4240 FunctionLiteral* constructor = nullptr; | 4240 FunctionLiteral* constructor = nullptr; |
4241 bool has_seen_constructor = false; | 4241 bool has_seen_constructor = false; |
4242 | 4242 |
4243 Expect(Token::LBRACE, CHECK_OK); | 4243 Expect(Token::LBRACE, CHECK_OK); |
4244 | 4244 |
4245 const bool has_extends = extends != nullptr; | 4245 const bool has_extends = extends != nullptr; |
4246 while (peek() != Token::RBRACE) { | 4246 while (peek() != Token::RBRACE) { |
4247 if (Check(Token::SEMICOLON)) continue; | 4247 if (Check(Token::SEMICOLON)) continue; |
4248 FuncNameInferrer::State fni_state(fni_); | 4248 FuncNameInferrer::State fni_state(fni_); |
4249 const bool in_class = true; | |
4250 bool is_computed_name = false; // Classes do not care about computed | 4249 bool is_computed_name = false; // Classes do not care about computed |
4251 // property names here. | 4250 // property names here. |
4252 ExpressionClassifier property_classifier(this); | 4251 ExpressionClassifier property_classifier(this); |
4253 const AstRawString* property_name = nullptr; | 4252 const AstRawString* property_name = nullptr; |
4254 ObjectLiteral::Property* property = ParsePropertyDefinition( | 4253 ClassLiteral::Property* property = ParseClassPropertyDefinition( |
4255 &checker, in_class, has_extends, &is_computed_name, | 4254 &checker, has_extends, &is_computed_name, &has_seen_constructor, |
4256 &has_seen_constructor, &property_name, CHECK_OK); | 4255 &property_name, CHECK_OK); |
4257 RewriteNonPattern(CHECK_OK); | 4256 RewriteNonPattern(CHECK_OK); |
4258 impl()->AccumulateFormalParameterContainmentErrors(); | 4257 impl()->AccumulateFormalParameterContainmentErrors(); |
4259 | 4258 |
4260 if (has_seen_constructor && constructor == nullptr) { | 4259 if (has_seen_constructor && constructor == nullptr) { |
4261 constructor = GetPropertyValue(property)->AsFunctionLiteral(); | 4260 constructor = GetPropertyValue(property)->AsFunctionLiteral(); |
4262 DCHECK_NOT_NULL(constructor); | 4261 DCHECK_NOT_NULL(constructor); |
4263 constructor->set_raw_name( | 4262 constructor->set_raw_name( |
4264 name != nullptr ? name : ast_value_factory()->empty_string()); | 4263 name != nullptr ? name : ast_value_factory()->empty_string()); |
4265 } else { | 4264 } else { |
4266 properties->Add(property, zone()); | 4265 properties->Add(property, zone()); |
(...skipping 757 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5024 return true; | 5023 return true; |
5025 } | 5024 } |
5026 if (expr->IsBinaryOperation() && | 5025 if (expr->IsBinaryOperation() && |
5027 expr->AsBinaryOperation()->op() == Token::COMMA) { | 5026 expr->AsBinaryOperation()->op() == Token::COMMA) { |
5028 return true; | 5027 return true; |
5029 } | 5028 } |
5030 // Everything else does not need rewriting. | 5029 // Everything else does not need rewriting. |
5031 return false; | 5030 return false; |
5032 } | 5031 } |
5033 | 5032 |
5034 void VisitObjectLiteralProperty(ObjectLiteralProperty* property) override { | 5033 void VisitLiteralProperty(LiteralProperty* property) override { |
5035 if (property == nullptr) return; | 5034 if (property == nullptr) return; |
5036 // Do not rewrite (computed) key expressions | 5035 // Do not rewrite (computed) key expressions |
5037 AST_REWRITE_PROPERTY(Expression, property, value); | 5036 AST_REWRITE_PROPERTY(Expression, property, value); |
5038 } | 5037 } |
5039 | 5038 |
5040 Parser* parser_; | 5039 Parser* parser_; |
5041 }; | 5040 }; |
5042 | 5041 |
5043 void Parser::RewriteNonPattern(bool* ok) { | 5042 void Parser::RewriteNonPattern(bool* ok) { |
5044 ValidateExpression(CHECK_OK_VOID); | 5043 ValidateExpression(CHECK_OK_VOID); |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5235 | 5234 |
5236 // Ignore "__proto__" as a name when it's being used to set the [[Prototype]] | 5235 // Ignore "__proto__" as a name when it's being used to set the [[Prototype]] |
5237 // of an object literal. | 5236 // of an object literal. |
5238 if (property->kind() == ObjectLiteralProperty::PROTOTYPE) return; | 5237 if (property->kind() == ObjectLiteralProperty::PROTOTYPE) return; |
5239 | 5238 |
5240 DCHECK(!value->IsAnonymousFunctionDefinition() || | 5239 DCHECK(!value->IsAnonymousFunctionDefinition() || |
5241 property->kind() == ObjectLiteralProperty::COMPUTED); | 5240 property->kind() == ObjectLiteralProperty::COMPUTED); |
5242 SetFunctionName(value, name); | 5241 SetFunctionName(value, name); |
5243 } | 5242 } |
5244 | 5243 |
| 5244 void Parser::SetFunctionNameFromPropertyName(ClassLiteralProperty* property, |
| 5245 const AstRawString* name) { |
| 5246 // TODO(bakkot) move this logic into Parse{Object,Class}PropertyDefinition and |
| 5247 // clean it up. |
| 5248 Expression* value = property->value(); |
| 5249 |
| 5250 // Computed name setting must happen at runtime. |
| 5251 if (property->is_computed_name()) return; |
| 5252 |
| 5253 // Getter and setter names are handled here because their names |
| 5254 // change in ES2015, even though they are not anonymous. |
| 5255 auto function = value->AsFunctionLiteral(); |
| 5256 DCHECK_NOT_NULL(function); |
| 5257 |
| 5258 bool is_getter = property->kind() == ClassLiteralProperty::GETTER; |
| 5259 bool is_setter = property->kind() == ClassLiteralProperty::SETTER; |
| 5260 if (is_getter || is_setter) { |
| 5261 DCHECK_NOT_NULL(name); |
| 5262 const AstRawString* prefix = is_getter |
| 5263 ? ast_value_factory()->get_space_string() |
| 5264 : ast_value_factory()->set_space_string(); |
| 5265 function->set_raw_name(ast_value_factory()->NewConsString(prefix, name)); |
| 5266 return; |
| 5267 } |
| 5268 |
| 5269 SetFunctionName(value, name); |
| 5270 } |
| 5271 |
5245 void Parser::SetFunctionNameFromIdentifierRef(Expression* value, | 5272 void Parser::SetFunctionNameFromIdentifierRef(Expression* value, |
5246 Expression* identifier) { | 5273 Expression* identifier) { |
5247 if (!identifier->IsVariableProxy()) return; | 5274 if (!identifier->IsVariableProxy()) return; |
5248 SetFunctionName(value, identifier->AsVariableProxy()->raw_name()); | 5275 SetFunctionName(value, identifier->AsVariableProxy()->raw_name()); |
5249 } | 5276 } |
5250 | 5277 |
5251 void Parser::SetFunctionName(Expression* value, const AstRawString* name) { | 5278 void Parser::SetFunctionName(Expression* value, const AstRawString* name) { |
5252 DCHECK_NOT_NULL(name); | 5279 DCHECK_NOT_NULL(name); |
5253 if (!value->IsAnonymousFunctionDefinition()) return; | 5280 if (!value->IsAnonymousFunctionDefinition()) return; |
5254 auto function = value->AsFunctionLiteral(); | 5281 auto function = value->AsFunctionLiteral(); |
(...skipping 936 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6191 node->Print(Isolate::Current()); | 6218 node->Print(Isolate::Current()); |
6192 } | 6219 } |
6193 #endif // DEBUG | 6220 #endif // DEBUG |
6194 | 6221 |
6195 #undef CHECK_OK | 6222 #undef CHECK_OK |
6196 #undef CHECK_OK_VOID | 6223 #undef CHECK_OK_VOID |
6197 #undef CHECK_FAILED | 6224 #undef CHECK_FAILED |
6198 | 6225 |
6199 } // namespace internal | 6226 } // namespace internal |
6200 } // namespace v8 | 6227 } // namespace v8 |
OLD | NEW |