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 "src/api.h" | 7 #include "src/api.h" |
8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
9 #include "src/ast/ast-expression-rewriter.h" | 9 #include "src/ast/ast-expression-rewriter.h" |
10 #include "src/ast/ast-expression-visitor.h" | 10 #include "src/ast/ast-expression-visitor.h" |
(...skipping 1946 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1957 kind = Variable::FUNCTION; | 1957 kind = Variable::FUNCTION; |
1958 } else if (declaration->IsVariableDeclaration() && | 1958 } else if (declaration->IsVariableDeclaration() && |
1959 declaration->AsVariableDeclaration()->is_class_declaration()) { | 1959 declaration->AsVariableDeclaration()->is_class_declaration()) { |
1960 kind = Variable::CLASS; | 1960 kind = Variable::CLASS; |
1961 declaration_group_start = | 1961 declaration_group_start = |
1962 declaration->AsVariableDeclaration()->declaration_group_start(); | 1962 declaration->AsVariableDeclaration()->declaration_group_start(); |
1963 } | 1963 } |
1964 var = declaration_scope->DeclareLocal( | 1964 var = declaration_scope->DeclareLocal( |
1965 name, mode, declaration->initialization(), kind, kNotAssigned, | 1965 name, mode, declaration->initialization(), kind, kNotAssigned, |
1966 declaration_group_start); | 1966 declaration_group_start); |
1967 } else if (((IsLexicalVariableMode(mode) || | 1967 } else if ((mode == CONST_LEGACY || var->mode() == CONST_LEGACY) && |
1968 IsLexicalVariableMode(var->mode())) && | 1968 !declaration_scope->is_script_scope()) { |
1969 // Allow duplicate function decls for web compat, see bug 4693. | 1969 // Duplicate legacy const definitions throw at runtime. |
1970 (is_strict(language_mode()) || !is_function_declaration || | 1970 DCHECK(is_sloppy(language_mode())); |
1971 !var->is_function())) || | 1971 Expression* expression = NewThrowSyntaxError( |
1972 ((mode == CONST_LEGACY || var->mode() == CONST_LEGACY) && | 1972 MessageTemplate::kVarRedeclaration, name, declaration->position()); |
1973 !declaration_scope->is_script_scope())) { | 1973 declaration_scope->SetIllegalRedeclaration(expression); |
1974 // The name was declared in this scope before; check for conflicting | 1974 } else if ((IsLexicalVariableMode(mode) || |
1975 // re-declarations. We have a conflict if either of the declarations is | 1975 IsLexicalVariableMode(var->mode())) && |
1976 // not a var (in script scope, we also have to ignore legacy const for | 1976 // Lexical bindings may appear for some parameters in sloppy |
1977 // compatibility). There is similar code in runtime.cc in the Declare | 1977 // mode even with --harmony-sloppy off. |
1978 // functions. The function CheckConflictingVarDeclarations checks for | 1978 (is_strict(language_mode()) || allow_harmony_sloppy())) { |
1979 // var and let bindings from different scopes whereas this is a check for | 1979 // Allow duplicate function decls for web compat, see bug 4693. |
1980 // conflicting declarations within the same scope. This check also covers | 1980 if (is_sloppy(language_mode()) && is_function_declaration && |
1981 // the special case | 1981 var->is_function()) { |
1982 // | 1982 DCHECK(IsLexicalVariableMode(mode) && |
1983 // function () { let x; { var x; } } | 1983 IsLexicalVariableMode(var->mode())); |
1984 // | 1984 ++use_counts_[v8::Isolate::kSloppyModeBlockScopedFunctionRedefinition]; |
1985 // because the var declaration is hoisted to the function scope where 'x' | 1985 } else { |
1986 // is already bound. | 1986 // The name was declared in this scope before; check for conflicting |
1987 DCHECK(IsDeclaredVariableMode(var->mode())); | 1987 // re-declarations. We have a conflict if either of the declarations |
1988 if (is_strict(language_mode()) || | 1988 // is |
adamk
2016/02/01 18:26:58
Please re-flow this whole comment to avoid lines
Dan Ehrenberg
2016/02/01 22:02:36
Done
Dan Ehrenberg
2016/02/01 22:02:36
Done
| |
1989 (allow_harmony_sloppy() && mode != CONST_LEGACY && | 1989 // not a var (in script scope, we also have to ignore legacy const for |
1990 var->mode() != CONST_LEGACY)) { | 1990 // compatibility). There is similar code in runtime.cc in the Declare |
1991 // functions. The function CheckConflictingVarDeclarations checks for | |
1992 // var and let bindings from different scopes whereas this is a check | |
1993 // for | |
1994 // conflicting declarations within the same scope. This check also | |
1995 // covers | |
1996 // the special case | |
1997 // | |
1998 // function () { let x; { var x; } } | |
1999 // | |
2000 // because the var declaration is hoisted to the function scope where | |
2001 // 'x' | |
2002 // is already bound. | |
2003 DCHECK(IsDeclaredVariableMode(var->mode())); | |
1991 // In harmony we treat re-declarations as early errors. See | 2004 // In harmony we treat re-declarations as early errors. See |
1992 // ES5 16 for a definition of early errors. | 2005 // ES5 16 for a definition of early errors. |
1993 if (declaration_kind == DeclarationDescriptor::NORMAL) { | 2006 if (declaration_kind == DeclarationDescriptor::NORMAL) { |
1994 ParserTraits::ReportMessage(MessageTemplate::kVarRedeclaration, name); | 2007 ParserTraits::ReportMessage(MessageTemplate::kVarRedeclaration, name); |
1995 } else { | 2008 } else { |
1996 ParserTraits::ReportMessage(MessageTemplate::kParamDupe); | 2009 ParserTraits::ReportMessage(MessageTemplate::kParamDupe); |
1997 } | 2010 } |
1998 *ok = false; | 2011 *ok = false; |
1999 return nullptr; | 2012 return nullptr; |
2000 } | 2013 } |
2001 Expression* expression = NewThrowSyntaxError( | |
2002 MessageTemplate::kVarRedeclaration, name, declaration->position()); | |
2003 declaration_scope->SetIllegalRedeclaration(expression); | |
2004 } else if (mode == VAR) { | 2014 } else if (mode == VAR) { |
2005 var->set_maybe_assigned(); | 2015 var->set_maybe_assigned(); |
2006 } | 2016 } |
2007 } else if (declaration_scope->is_eval_scope() && | 2017 } else if (declaration_scope->is_eval_scope() && |
2008 is_sloppy(declaration_scope->language_mode()) && | 2018 is_sloppy(declaration_scope->language_mode()) && |
2009 !IsLexicalVariableMode(mode)) { | 2019 !IsLexicalVariableMode(mode)) { |
2010 // In a var binding in a sloppy direct eval, pollute the enclosing scope | 2020 // In a var binding in a sloppy direct eval, pollute the enclosing scope |
2011 // with this new binding by doing the following: | 2021 // with this new binding by doing the following: |
2012 // The proxy is bound to a lookup variable to force a dynamic declaration | 2022 // The proxy is bound to a lookup variable to force a dynamic declaration |
2013 // using the DeclareLookupSlot runtime function. | 2023 // using the DeclareLookupSlot runtime function. |
(...skipping 1622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3636 } | 3646 } |
3637 *ok = false; | 3647 *ok = false; |
3638 return nullptr; | 3648 return nullptr; |
3639 } | 3649 } |
3640 | 3650 |
3641 Block* init_block = nullptr; | 3651 Block* init_block = nullptr; |
3642 | 3652 |
3643 // special case for legacy for (var/const x =.... in) | 3653 // special case for legacy for (var/const x =.... in) |
3644 if (!IsLexicalVariableMode(parsing_result.descriptor.mode) && | 3654 if (!IsLexicalVariableMode(parsing_result.descriptor.mode) && |
3645 decl.pattern->IsVariableProxy() && decl.initializer != nullptr) { | 3655 decl.pattern->IsVariableProxy() && decl.initializer != nullptr) { |
3656 ++use_counts_[v8::Isolate::kForInInitializer]; | |
3646 const AstRawString* name = | 3657 const AstRawString* name = |
3647 decl.pattern->AsVariableProxy()->raw_name(); | 3658 decl.pattern->AsVariableProxy()->raw_name(); |
3648 VariableProxy* single_var = scope_->NewUnresolved( | 3659 VariableProxy* single_var = scope_->NewUnresolved( |
3649 factory(), name, Variable::NORMAL, each_beg_pos, each_end_pos); | 3660 factory(), name, Variable::NORMAL, each_beg_pos, each_end_pos); |
3650 init_block = factory()->NewBlock( | 3661 init_block = factory()->NewBlock( |
3651 nullptr, 2, true, parsing_result.descriptor.declaration_pos); | 3662 nullptr, 2, true, parsing_result.descriptor.declaration_pos); |
3652 init_block->statements()->Add( | 3663 init_block->statements()->Add( |
3653 factory()->NewExpressionStatement( | 3664 factory()->NewExpressionStatement( |
3654 factory()->NewAssignment(Token::ASSIGN, single_var, | 3665 factory()->NewAssignment(Token::ASSIGN, single_var, |
3655 decl.initializer, | 3666 decl.initializer, |
(...skipping 2077 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5733 auto class_literal = value->AsClassLiteral(); | 5744 auto class_literal = value->AsClassLiteral(); |
5734 if (class_literal->raw_name() == nullptr) { | 5745 if (class_literal->raw_name() == nullptr) { |
5735 class_literal->set_raw_name(name); | 5746 class_literal->set_raw_name(name); |
5736 } | 5747 } |
5737 } | 5748 } |
5738 } | 5749 } |
5739 | 5750 |
5740 | 5751 |
5741 } // namespace internal | 5752 } // namespace internal |
5742 } // namespace v8 | 5753 } // namespace v8 |
OLD | NEW |