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 1908 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1919 } | 1919 } |
1920 | 1920 |
1921 | 1921 |
1922 Variable* Parser::Declare(Declaration* declaration, | 1922 Variable* Parser::Declare(Declaration* declaration, |
1923 DeclarationDescriptor::Kind declaration_kind, | 1923 DeclarationDescriptor::Kind declaration_kind, |
1924 bool resolve, bool* ok, Scope* scope) { | 1924 bool resolve, bool* ok, Scope* scope) { |
1925 VariableProxy* proxy = declaration->proxy(); | 1925 VariableProxy* proxy = declaration->proxy(); |
1926 DCHECK(proxy->raw_name() != NULL); | 1926 DCHECK(proxy->raw_name() != NULL); |
1927 const AstRawString* name = proxy->raw_name(); | 1927 const AstRawString* name = proxy->raw_name(); |
1928 VariableMode mode = declaration->mode(); | 1928 VariableMode mode = declaration->mode(); |
| 1929 bool is_function_declaration = declaration->IsFunctionDeclaration(); |
1929 if (scope == nullptr) scope = scope_; | 1930 if (scope == nullptr) scope = scope_; |
1930 Scope* declaration_scope = | 1931 Scope* declaration_scope = |
1931 IsLexicalVariableMode(mode) ? scope : scope->DeclarationScope(); | 1932 IsLexicalVariableMode(mode) ? scope : scope->DeclarationScope(); |
1932 Variable* var = NULL; | 1933 Variable* var = NULL; |
1933 | 1934 |
1934 // If a suitable scope exists, then we can statically declare this | 1935 // If a suitable scope exists, then we can statically declare this |
1935 // variable and also set its mode. In any case, a Declaration node | 1936 // variable and also set its mode. In any case, a Declaration node |
1936 // will be added to the scope so that the declaration can be added | 1937 // will be added to the scope so that the declaration can be added |
1937 // to the corresponding activation frame at runtime if necessary. | 1938 // to the corresponding activation frame at runtime if necessary. |
1938 // For instance, var declarations inside a sloppy eval scope need | 1939 // For instance, var declarations inside a sloppy eval scope need |
1939 // to be added to the calling function context. Similarly, strict | 1940 // to be added to the calling function context. Similarly, strict |
1940 // mode eval scope and lexical eval bindings do not leak variable | 1941 // mode eval scope and lexical eval bindings do not leak variable |
1941 // declarations to the caller's scope so we declare all locals, too. | 1942 // declarations to the caller's scope so we declare all locals, too. |
1942 if (declaration_scope->is_function_scope() || | 1943 if (declaration_scope->is_function_scope() || |
1943 declaration_scope->is_block_scope() || | 1944 declaration_scope->is_block_scope() || |
1944 declaration_scope->is_module_scope() || | 1945 declaration_scope->is_module_scope() || |
1945 declaration_scope->is_script_scope() || | 1946 declaration_scope->is_script_scope() || |
1946 (declaration_scope->is_eval_scope() && | 1947 (declaration_scope->is_eval_scope() && |
1947 (is_strict(declaration_scope->language_mode()) || | 1948 (is_strict(declaration_scope->language_mode()) || |
1948 IsLexicalVariableMode(mode)))) { | 1949 IsLexicalVariableMode(mode)))) { |
1949 // Declare the variable in the declaration scope. | 1950 // Declare the variable in the declaration scope. |
1950 var = declaration_scope->LookupLocal(name); | 1951 var = declaration_scope->LookupLocal(name); |
1951 if (var == NULL) { | 1952 if (var == NULL) { |
1952 // Declare the name. | 1953 // Declare the name. |
1953 Variable::Kind kind = Variable::NORMAL; | 1954 Variable::Kind kind = Variable::NORMAL; |
1954 int declaration_group_start = -1; | 1955 int declaration_group_start = -1; |
1955 if (declaration->IsFunctionDeclaration()) { | 1956 if (is_function_declaration) { |
1956 kind = Variable::FUNCTION; | 1957 kind = Variable::FUNCTION; |
1957 } else if (declaration->IsVariableDeclaration() && | 1958 } else if (declaration->IsVariableDeclaration() && |
1958 declaration->AsVariableDeclaration()->is_class_declaration()) { | 1959 declaration->AsVariableDeclaration()->is_class_declaration()) { |
1959 kind = Variable::CLASS; | 1960 kind = Variable::CLASS; |
1960 declaration_group_start = | 1961 declaration_group_start = |
1961 declaration->AsVariableDeclaration()->declaration_group_start(); | 1962 declaration->AsVariableDeclaration()->declaration_group_start(); |
1962 } | 1963 } |
1963 var = declaration_scope->DeclareLocal( | 1964 var = declaration_scope->DeclareLocal( |
1964 name, mode, declaration->initialization(), kind, kNotAssigned, | 1965 name, mode, declaration->initialization(), kind, kNotAssigned, |
1965 declaration_group_start); | 1966 declaration_group_start); |
1966 } else if (IsLexicalVariableMode(mode) || | 1967 } else if (((IsLexicalVariableMode(mode) || |
1967 IsLexicalVariableMode(var->mode()) || | 1968 IsLexicalVariableMode(var->mode())) && |
| 1969 // Allow duplicate function decls for web compat, see bug 4693. |
| 1970 (is_strict(language_mode()) || !is_function_declaration || |
| 1971 !var->is_function())) || |
1968 ((mode == CONST_LEGACY || var->mode() == CONST_LEGACY) && | 1972 ((mode == CONST_LEGACY || var->mode() == CONST_LEGACY) && |
1969 !declaration_scope->is_script_scope())) { | 1973 !declaration_scope->is_script_scope())) { |
1970 // The name was declared in this scope before; check for conflicting | 1974 // The name was declared in this scope before; check for conflicting |
1971 // re-declarations. We have a conflict if either of the declarations is | 1975 // re-declarations. We have a conflict if either of the declarations is |
1972 // not a var (in script scope, we also have to ignore legacy const for | 1976 // not a var (in script scope, we also have to ignore legacy const for |
1973 // compatibility). There is similar code in runtime.cc in the Declare | 1977 // compatibility). There is similar code in runtime.cc in the Declare |
1974 // functions. The function CheckConflictingVarDeclarations checks for | 1978 // functions. The function CheckConflictingVarDeclarations checks for |
1975 // var and let bindings from different scopes whereas this is a check for | 1979 // var and let bindings from different scopes whereas this is a check for |
1976 // conflicting declarations within the same scope. This check also covers | 1980 // conflicting declarations within the same scope. This check also covers |
1977 // the special case | 1981 // the special case |
(...skipping 3751 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5729 auto class_literal = value->AsClassLiteral(); | 5733 auto class_literal = value->AsClassLiteral(); |
5730 if (class_literal->raw_name() == nullptr) { | 5734 if (class_literal->raw_name() == nullptr) { |
5731 class_literal->set_raw_name(name); | 5735 class_literal->set_raw_name(name); |
5732 } | 5736 } |
5733 } | 5737 } |
5734 } | 5738 } |
5735 | 5739 |
5736 | 5740 |
5737 } // namespace internal | 5741 } // namespace internal |
5738 } // namespace v8 | 5742 } // namespace v8 |
OLD | NEW |