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

Side by Side Diff: src/parser.cc

Issue 1189743003: [destructuring] Implement parameter pattern matching. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 6 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 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast.h" 8 #include "src/ast.h"
9 #include "src/bailout-reason.h" 9 #include "src/bailout-reason.h"
10 #include "src/base/platform/platform.h" 10 #include "src/base/platform/platform.h"
(...skipping 3793 matching lines...) Expand 10 before | Expand all | Expand 10 after
3804 } 3804 }
3805 3805
3806 // Only the right-most expression may be a rest parameter. 3806 // Only the right-most expression may be a rest parameter.
3807 DCHECK(!*has_rest); 3807 DCHECK(!*has_rest);
3808 3808
3809 if (expr->IsSpread()) { 3809 if (expr->IsSpread()) {
3810 *has_rest = true; 3810 *has_rest = true;
3811 expr = expr->AsSpread()->expression(); 3811 expr = expr->AsSpread()->expression();
3812 } 3812 }
3813 3813
3814 if (!expr->IsVariableProxy()) { 3814 if (expr->IsVariableProxy()) {
3815 // TODO(dslomov): support pattern desugaring 3815 // When the formal parameter was originally seen, it was parsed as a
3816 return; 3816 // VariableProxy and recorded as unresolved in the scope. Here we undo that
3817 // parse-time side-effect for parameters that are single-names (not
3818 // patterns).
3819 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy());
3817 } 3820 }
3818 DCHECK(!expr->AsVariableProxy()->is_this());
3819
3820 const AstRawString* raw_name = expr->AsVariableProxy()->raw_name();
3821 Scanner::Location param_location(expr->position(),
3822 expr->position() + raw_name->length());
3823
3824 // When the formal parameter was originally seen, it was parsed as a
3825 // VariableProxy and recorded as unresolved in the scope. Here we undo that
3826 // parse-time side-effect.
3827 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy());
3828 3821
3829 ExpressionClassifier classifier; 3822 ExpressionClassifier classifier;
3830 DeclareFormalParameter(scope, expr, &classifier, *has_rest); 3823 DeclareFormalParameter(scope, expr, &classifier, *has_rest);
3831 if (!duplicate_loc->IsValid()) { 3824 if (!duplicate_loc->IsValid()) {
3832 *duplicate_loc = classifier.duplicate_formal_parameter_error().location; 3825 *duplicate_loc = classifier.duplicate_formal_parameter_error().location;
3833 } 3826 }
3834 } 3827 }
3835 3828
3836 3829
3837 FunctionLiteral* Parser::ParseFunctionLiteral( 3830 FunctionLiteral* Parser::ParseFunctionLiteral(
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
4210 RelocInfo::kNoPosition); 4203 RelocInfo::kNoPosition);
4211 IfStatement* if_statement = factory()->NewIfStatement( 4204 IfStatement* if_statement = factory()->NewIfStatement(
4212 condition, factory()->NewExpressionStatement(throw_type_error, 4205 condition, factory()->NewExpressionStatement(throw_type_error,
4213 RelocInfo::kNoPosition), 4206 RelocInfo::kNoPosition),
4214 factory()->NewEmptyStatement(RelocInfo::kNoPosition), 4207 factory()->NewEmptyStatement(RelocInfo::kNoPosition),
4215 RelocInfo::kNoPosition); 4208 RelocInfo::kNoPosition);
4216 return if_statement; 4209 return if_statement;
4217 } 4210 }
4218 4211
4219 4212
4213 Block* Parser::BuildParameterInitializationBlock(bool* ok) {
4214 DCHECK(scope_->is_function_scope());
4215 Block* init_block = nullptr;
4216 for (int i = 0; i < scope_->num_parameters(); i++) {
4217 auto parameter = scope_->parameter(i);
4218 if (parameter.pattern == nullptr) continue;
4219 if (init_block == nullptr) {
4220 init_block = factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition);
4221 }
4222
4223 DeclarationDescriptor descriptor;
4224 descriptor.parser = this;
4225 descriptor.declaration_scope = scope_;
4226 descriptor.scope = scope_;
4227 descriptor.mode = VAR;
rossberg 2015/06/16 17:02:12 Shouldn't this be LET?
caitp (gmail) 2015/06/16 19:08:09 I think this is VAR unless computed property names
Dmitry Lomov (no reviews) 2015/06/19 15:25:14 Changing to LET - it is always LET for patterns si
4228 descriptor.is_const = false;
4229 descriptor.needs_init = false;
4230 descriptor.declaration_pos = parameter.pattern->position();
4231 descriptor.initialization_pos = parameter.pattern->position();
4232 descriptor.init_op = Token::ASSIGN;
4233 DeclarationParsingResult::Declaration decl(
4234 parameter.pattern, parameter.pattern->position(),
4235 factory()->NewVariableProxy(parameter.var));
4236 PatternRewriter::DeclareAndInitializeVariables(init_block, &descriptor,
4237 &decl, nullptr, CHECK_OK);
4238 }
4239 return init_block;
4240 }
4241
4242
4220 ZoneList<Statement*>* Parser::ParseEagerFunctionBody( 4243 ZoneList<Statement*>* Parser::ParseEagerFunctionBody(
4221 const AstRawString* function_name, int pos, Variable* fvar, 4244 const AstRawString* function_name, int pos, Variable* fvar,
4222 Token::Value fvar_init_op, FunctionKind kind, bool* ok) { 4245 Token::Value fvar_init_op, FunctionKind kind, bool* ok) {
4223 // Everything inside an eagerly parsed function will be parsed eagerly 4246 // Everything inside an eagerly parsed function will be parsed eagerly
4224 // (see comment above). 4247 // (see comment above).
4225 ParsingModeScope parsing_mode(this, PARSE_EAGERLY); 4248 ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
4226 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(8, zone()); 4249 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(8, zone());
4227 if (fvar != NULL) { 4250 if (fvar != NULL) {
4228 VariableProxy* fproxy = scope_->NewUnresolved(factory(), function_name); 4251 VariableProxy* fproxy = scope_->NewUnresolved(factory(), function_name);
4229 fproxy->BindTo(fvar); 4252 fproxy->BindTo(fvar);
4230 body->Add(factory()->NewExpressionStatement( 4253 body->Add(factory()->NewExpressionStatement(
4231 factory()->NewAssignment(fvar_init_op, 4254 factory()->NewAssignment(fvar_init_op,
4232 fproxy, 4255 fproxy,
4233 factory()->NewThisFunction(pos), 4256 factory()->NewThisFunction(pos),
4234 RelocInfo::kNoPosition), 4257 RelocInfo::kNoPosition),
4235 RelocInfo::kNoPosition), zone()); 4258 RelocInfo::kNoPosition), zone());
4236 } 4259 }
4237 4260
4238 4261
4239 // For concise constructors, check that they are constructed, 4262 // For concise constructors, check that they are constructed,
4240 // not called. 4263 // not called.
4241 if (i::IsConstructor(kind)) { 4264 if (i::IsConstructor(kind)) {
4242 AddAssertIsConstruct(body, pos); 4265 AddAssertIsConstruct(body, pos);
4243 } 4266 }
4244 4267
4268
4269 auto init_block = BuildParameterInitializationBlock(CHECK_OK);
4270 if (init_block != nullptr) {
4271 body->Add(init_block, zone());
4272 }
4273
4245 // For generators, allocate and yield an iterator on function entry. 4274 // For generators, allocate and yield an iterator on function entry.
4246 if (IsGeneratorFunction(kind)) { 4275 if (IsGeneratorFunction(kind)) {
4247 ZoneList<Expression*>* arguments = 4276 ZoneList<Expression*>* arguments =
4248 new(zone()) ZoneList<Expression*>(0, zone()); 4277 new(zone()) ZoneList<Expression*>(0, zone());
4249 CallRuntime* allocation = factory()->NewCallRuntime( 4278 CallRuntime* allocation = factory()->NewCallRuntime(
4250 ast_value_factory()->empty_string(), 4279 ast_value_factory()->empty_string(),
4251 Runtime::FunctionForId(Runtime::kCreateJSGeneratorObject), arguments, 4280 Runtime::FunctionForId(Runtime::kCreateJSGeneratorObject), arguments,
4252 pos); 4281 pos);
4253 VariableProxy* init_proxy = factory()->NewVariableProxy( 4282 VariableProxy* init_proxy = factory()->NewVariableProxy(
4254 function_state_->generator_object_variable()); 4283 function_state_->generator_object_variable());
(...skipping 1580 matching lines...) Expand 10 before | Expand all | Expand 10 after
5835 Expression* Parser::SpreadCallNew(Expression* function, 5864 Expression* Parser::SpreadCallNew(Expression* function,
5836 ZoneList<v8::internal::Expression*>* args, 5865 ZoneList<v8::internal::Expression*>* args,
5837 int pos) { 5866 int pos) {
5838 args->InsertAt(0, function, zone()); 5867 args->InsertAt(0, function, zone());
5839 5868
5840 return factory()->NewCallRuntime( 5869 return factory()->NewCallRuntime(
5841 ast_value_factory()->reflect_construct_string(), NULL, args, pos); 5870 ast_value_factory()->reflect_construct_string(), NULL, args, pos);
5842 } 5871 }
5843 } // namespace internal 5872 } // namespace internal
5844 } // namespace v8 5873 } // namespace v8
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/ppc/full-codegen-ppc.cc » ('j') | src/scopes.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698