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 4231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 bool is_computed_name = false; // Classes do not care about computed | 4249 bool is_computed_name = false; // Classes do not care about computed |
4250 // property names here. | 4250 // property names here. |
4251 ExpressionClassifier property_classifier(this); | 4251 ExpressionClassifier property_classifier(this); |
4252 const AstRawString* property_name = nullptr; | 4252 ClassLiteral::Property* property = |
4253 ClassLiteral::Property* property = ParseClassPropertyDefinition( | 4253 ParseClassPropertyDefinition(&checker, has_extends, &is_computed_name, |
4254 &checker, has_extends, &is_computed_name, &has_seen_constructor, | 4254 &has_seen_constructor, CHECK_OK); |
4255 &property_name, CHECK_OK); | |
4256 RewriteNonPattern(CHECK_OK); | 4255 RewriteNonPattern(CHECK_OK); |
4257 impl()->AccumulateFormalParameterContainmentErrors(); | 4256 impl()->AccumulateFormalParameterContainmentErrors(); |
4258 | 4257 |
4259 if (has_seen_constructor && constructor == nullptr) { | 4258 if (has_seen_constructor && constructor == nullptr) { |
4260 constructor = GetPropertyValue(property)->AsFunctionLiteral(); | 4259 constructor = GetPropertyValue(property)->AsFunctionLiteral(); |
4261 DCHECK_NOT_NULL(constructor); | 4260 DCHECK_NOT_NULL(constructor); |
4262 constructor->set_raw_name( | 4261 constructor->set_raw_name( |
4263 name != nullptr ? name : ast_value_factory()->empty_string()); | 4262 name != nullptr ? name : ast_value_factory()->empty_string()); |
4264 } else { | 4263 } else { |
4265 properties->Add(property, zone()); | 4264 properties->Add(property, zone()); |
4266 } | 4265 } |
4267 | 4266 |
4268 DCHECK_NOT_NULL(fni_); | 4267 DCHECK_NOT_NULL(fni_); |
4269 fni_->Infer(); | 4268 fni_->Infer(); |
4270 | |
4271 if (property_name != ast_value_factory()->constructor_string()) { | |
4272 SetFunctionNameFromPropertyName(property, property_name); | |
4273 } | |
4274 } | 4269 } |
4275 | 4270 |
4276 Expect(Token::RBRACE, CHECK_OK); | 4271 Expect(Token::RBRACE, CHECK_OK); |
4277 int end_pos = scanner()->location().end_pos; | 4272 int end_pos = scanner()->location().end_pos; |
4278 | 4273 |
4279 if (constructor == nullptr) { | 4274 if (constructor == nullptr) { |
4280 constructor = DefaultConstructor(name, has_extends, pos, end_pos, | 4275 constructor = DefaultConstructor(name, has_extends, pos, end_pos, |
4281 block_state.language_mode()); | 4276 block_state.language_mode()); |
4282 } | 4277 } |
4283 | 4278 |
(...skipping 918 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5202 DCHECK(expr->IsRewritableExpression()); | 5197 DCHECK(expr->IsRewritableExpression()); |
5203 function_state_->AddDestructuringAssignment( | 5198 function_state_->AddDestructuringAssignment( |
5204 DestructuringAssignment(expr, scope())); | 5199 DestructuringAssignment(expr, scope())); |
5205 } | 5200 } |
5206 | 5201 |
5207 void Parser::QueueNonPatternForRewriting(Expression* expr, bool* ok) { | 5202 void Parser::QueueNonPatternForRewriting(Expression* expr, bool* ok) { |
5208 DCHECK(expr->IsRewritableExpression()); | 5203 DCHECK(expr->IsRewritableExpression()); |
5209 function_state_->AddNonPatternForRewriting(expr, ok); | 5204 function_state_->AddNonPatternForRewriting(expr, ok); |
5210 } | 5205 } |
5211 | 5206 |
| 5207 void Parser::AddAccessorPrefixToFunctionName(bool is_get, |
| 5208 FunctionLiteral* function, |
| 5209 const AstRawString* name) { |
| 5210 DCHECK_NOT_NULL(name); |
| 5211 const AstRawString* prefix = is_get ? ast_value_factory()->get_space_string() |
| 5212 : ast_value_factory()->set_space_string(); |
| 5213 function->set_raw_name(ast_value_factory()->NewConsString(prefix, name)); |
| 5214 } |
| 5215 |
5212 void Parser::SetFunctionNameFromPropertyName(ObjectLiteralProperty* property, | 5216 void Parser::SetFunctionNameFromPropertyName(ObjectLiteralProperty* property, |
5213 const AstRawString* name) { | 5217 const AstRawString* name) { |
5214 Expression* value = property->value(); | 5218 DCHECK(property->kind() != ObjectLiteralProperty::GETTER); |
| 5219 DCHECK(property->kind() != ObjectLiteralProperty::SETTER); |
5215 | 5220 |
5216 // Computed name setting must happen at runtime. | 5221 // Computed name setting must happen at runtime. |
5217 if (property->is_computed_name()) return; | 5222 DCHECK(!property->is_computed_name()); |
5218 | |
5219 // Getter and setter names are handled here because their names | |
5220 // change in ES2015, even though they are not anonymous. | |
5221 auto function = value->AsFunctionLiteral(); | |
5222 if (function != nullptr) { | |
5223 bool is_getter = property->kind() == ObjectLiteralProperty::GETTER; | |
5224 bool is_setter = property->kind() == ObjectLiteralProperty::SETTER; | |
5225 if (is_getter || is_setter) { | |
5226 DCHECK_NOT_NULL(name); | |
5227 const AstRawString* prefix = | |
5228 is_getter ? ast_value_factory()->get_space_string() | |
5229 : ast_value_factory()->set_space_string(); | |
5230 function->set_raw_name(ast_value_factory()->NewConsString(prefix, name)); | |
5231 return; | |
5232 } | |
5233 } | |
5234 | 5223 |
5235 // Ignore "__proto__" as a name when it's being used to set the [[Prototype]] | 5224 // Ignore "__proto__" as a name when it's being used to set the [[Prototype]] |
5236 // of an object literal. | 5225 // of an object literal. |
5237 if (property->kind() == ObjectLiteralProperty::PROTOTYPE) return; | 5226 if (property->kind() == ObjectLiteralProperty::PROTOTYPE) return; |
5238 | 5227 |
| 5228 Expression* value = property->value(); |
| 5229 |
5239 DCHECK(!value->IsAnonymousFunctionDefinition() || | 5230 DCHECK(!value->IsAnonymousFunctionDefinition() || |
5240 property->kind() == ObjectLiteralProperty::COMPUTED); | 5231 property->kind() == ObjectLiteralProperty::COMPUTED); |
5241 SetFunctionName(value, name); | 5232 SetFunctionName(value, name); |
5242 } | 5233 } |
5243 | 5234 |
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 | |
5272 void Parser::SetFunctionNameFromIdentifierRef(Expression* value, | 5235 void Parser::SetFunctionNameFromIdentifierRef(Expression* value, |
5273 Expression* identifier) { | 5236 Expression* identifier) { |
5274 if (!identifier->IsVariableProxy()) return; | 5237 if (!identifier->IsVariableProxy()) return; |
5275 SetFunctionName(value, identifier->AsVariableProxy()->raw_name()); | 5238 SetFunctionName(value, identifier->AsVariableProxy()->raw_name()); |
5276 } | 5239 } |
5277 | 5240 |
5278 void Parser::SetFunctionName(Expression* value, const AstRawString* name) { | 5241 void Parser::SetFunctionName(Expression* value, const AstRawString* name) { |
5279 DCHECK_NOT_NULL(name); | 5242 DCHECK_NOT_NULL(name); |
5280 if (!value->IsAnonymousFunctionDefinition()) return; | 5243 if (!value->IsAnonymousFunctionDefinition()) return; |
5281 auto function = value->AsFunctionLiteral(); | 5244 auto function = value->AsFunctionLiteral(); |
(...skipping 936 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6218 node->Print(Isolate::Current()); | 6181 node->Print(Isolate::Current()); |
6219 } | 6182 } |
6220 #endif // DEBUG | 6183 #endif // DEBUG |
6221 | 6184 |
6222 #undef CHECK_OK | 6185 #undef CHECK_OK |
6223 #undef CHECK_OK_VOID | 6186 #undef CHECK_OK_VOID |
6224 #undef CHECK_FAILED | 6187 #undef CHECK_FAILED |
6225 | 6188 |
6226 } // namespace internal | 6189 } // namespace internal |
6227 } // namespace v8 | 6190 } // namespace v8 |
OLD | NEW |