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.h" | 10 #include "src/ast/ast.h" |
(...skipping 1505 matching lines...) Loading... | |
1516 parsing_module_)) { | 1516 parsing_module_)) { |
1517 *ok = false; | 1517 *ok = false; |
1518 ReportMessage(MessageTemplate::kUnexpectedReserved); | 1518 ReportMessage(MessageTemplate::kUnexpectedReserved); |
1519 return nullptr; | 1519 return nullptr; |
1520 } else if (IsEvalOrArguments(local_name)) { | 1520 } else if (IsEvalOrArguments(local_name)) { |
1521 *ok = false; | 1521 *ok = false; |
1522 ReportMessage(MessageTemplate::kStrictEvalArguments); | 1522 ReportMessage(MessageTemplate::kStrictEvalArguments); |
1523 return nullptr; | 1523 return nullptr; |
1524 } | 1524 } |
1525 | 1525 |
1526 DeclareConstVariable(local_name, kNeedsInitialization, position(), | 1526 DeclareVariable(local_name, CONST, kNeedsInitialization, position(), |
1527 CHECK_OK); | 1527 CHECK_OK); |
1528 | 1528 |
1529 NamedImport* import = new (zone()) NamedImport( | 1529 NamedImport* import = new (zone()) NamedImport( |
1530 import_name, local_name, scanner()->location()); | 1530 import_name, local_name, scanner()->location()); |
1531 result->Add(import, zone()); | 1531 result->Add(import, zone()); |
1532 | 1532 |
1533 if (peek() == Token::RBRACE) break; | 1533 if (peek() == Token::RBRACE) break; |
1534 Expect(Token::COMMA, CHECK_OK); | 1534 Expect(Token::COMMA, CHECK_OK); |
1535 } | 1535 } |
1536 | 1536 |
1537 Expect(Token::RBRACE, CHECK_OK); | 1537 Expect(Token::RBRACE, CHECK_OK); |
(...skipping 29 matching lines...) Loading... | |
1567 return; | 1567 return; |
1568 } | 1568 } |
1569 | 1569 |
1570 // Parse ImportedDefaultBinding if present. | 1570 // Parse ImportedDefaultBinding if present. |
1571 const AstRawString* import_default_binding = nullptr; | 1571 const AstRawString* import_default_binding = nullptr; |
1572 Scanner::Location import_default_binding_loc; | 1572 Scanner::Location import_default_binding_loc; |
1573 if (tok != Token::MUL && tok != Token::LBRACE) { | 1573 if (tok != Token::MUL && tok != Token::LBRACE) { |
1574 import_default_binding = | 1574 import_default_binding = |
1575 ParseIdentifier(kDontAllowRestrictedIdentifiers, CHECK_OK_VOID); | 1575 ParseIdentifier(kDontAllowRestrictedIdentifiers, CHECK_OK_VOID); |
1576 import_default_binding_loc = scanner()->location(); | 1576 import_default_binding_loc = scanner()->location(); |
1577 DeclareConstVariable(import_default_binding, kNeedsInitialization, pos, | 1577 DeclareVariable(import_default_binding, CONST, kNeedsInitialization, pos, |
1578 CHECK_OK_VOID); | 1578 CHECK_OK_VOID); |
1579 } | 1579 } |
1580 | 1580 |
1581 // Parse NameSpaceImport or NamedImports if present. | 1581 // Parse NameSpaceImport or NamedImports if present. |
1582 const AstRawString* module_namespace_binding = nullptr; | 1582 const AstRawString* module_namespace_binding = nullptr; |
1583 Scanner::Location module_namespace_binding_loc; | 1583 Scanner::Location module_namespace_binding_loc; |
1584 const ZoneList<const NamedImport*>* named_imports = nullptr; | 1584 const ZoneList<const NamedImport*>* named_imports = nullptr; |
1585 if (import_default_binding == nullptr || Check(Token::COMMA)) { | 1585 if (import_default_binding == nullptr || Check(Token::COMMA)) { |
1586 switch (peek()) { | 1586 switch (peek()) { |
1587 case Token::MUL: { | 1587 case Token::MUL: { |
1588 Consume(Token::MUL); | 1588 Consume(Token::MUL); |
1589 ExpectContextualKeyword(CStrVector("as"), CHECK_OK_VOID); | 1589 ExpectContextualKeyword(CStrVector("as"), CHECK_OK_VOID); |
1590 module_namespace_binding = | 1590 module_namespace_binding = |
1591 ParseIdentifier(kDontAllowRestrictedIdentifiers, CHECK_OK_VOID); | 1591 ParseIdentifier(kDontAllowRestrictedIdentifiers, CHECK_OK_VOID); |
1592 module_namespace_binding_loc = scanner()->location(); | 1592 module_namespace_binding_loc = scanner()->location(); |
1593 DeclareConstVariable(module_namespace_binding, kCreatedInitialized, pos, | 1593 DeclareVariable(module_namespace_binding, CONST, kCreatedInitialized, |
1594 CHECK_OK_VOID); | 1594 pos, CHECK_OK_VOID); |
1595 break; | 1595 break; |
1596 } | 1596 } |
1597 | 1597 |
1598 case Token::LBRACE: | 1598 case Token::LBRACE: |
1599 named_imports = ParseNamedImports(pos, CHECK_OK_VOID); | 1599 named_imports = ParseNamedImports(pos, CHECK_OK_VOID); |
1600 break; | 1600 break; |
1601 | 1601 |
1602 default: | 1602 default: |
1603 *ok = false; | 1603 *ok = false; |
1604 ReportUnexpectedToken(scanner()->current_token()); | 1604 ReportUnexpectedToken(scanner()->current_token()); |
1605 return; | 1605 return; |
1606 } | 1606 } |
1607 } | 1607 } |
1608 | 1608 |
1609 ExpectContextualKeyword(CStrVector("from"), CHECK_OK_VOID); | 1609 ExpectContextualKeyword(CStrVector("from"), CHECK_OK_VOID); |
1610 const AstRawString* module_specifier = ParseModuleSpecifier(CHECK_OK_VOID); | 1610 const AstRawString* module_specifier = ParseModuleSpecifier(CHECK_OK_VOID); |
1611 ExpectSemicolon(CHECK_OK_VOID); | 1611 ExpectSemicolon(CHECK_OK_VOID); |
1612 | 1612 |
1613 // Now that we have all the information, we can make the appropriate | 1613 // Now that we have all the information, we can make the appropriate |
1614 // declarations. | 1614 // declarations. |
1615 | 1615 |
1616 // TODO(neis): Would prefer to call DeclareConstVariable for each case below | 1616 // TODO(neis): Would prefer to call DeclareVariable for each case below rather |
1617 // rather than above and in ParseNamedImports, but then a possible error | 1617 // than above and in ParseNamedImports, but then a possible error message |
1618 // message would point to the wrong location. Maybe have a DeclareAt version | 1618 // would point to the wrong location. Maybe have a DeclareAt version of |
1619 // of Declare that takes a location? | 1619 // Declare that takes a location? |
1620 | 1620 |
1621 if (module_namespace_binding != nullptr) { | 1621 if (module_namespace_binding != nullptr) { |
1622 module()->AddStarImport(module_namespace_binding, module_specifier, | 1622 module()->AddStarImport(module_namespace_binding, module_specifier, |
1623 module_namespace_binding_loc, zone()); | 1623 module_namespace_binding_loc, zone()); |
1624 } | 1624 } |
1625 | 1625 |
1626 if (import_default_binding != nullptr) { | 1626 if (import_default_binding != nullptr) { |
1627 module()->AddImport(ast_value_factory()->default_string(), | 1627 module()->AddImport(ast_value_factory()->default_string(), |
1628 import_default_binding, module_specifier, | 1628 import_default_binding, module_specifier, |
1629 import_default_binding_loc, zone()); | 1629 import_default_binding_loc, zone()); |
(...skipping 50 matching lines...) Loading... | |
1680 ParseAssignmentExpression(true, &classifier, CHECK_OK); | 1680 ParseAssignmentExpression(true, &classifier, CHECK_OK); |
1681 RewriteNonPattern(&classifier, CHECK_OK); | 1681 RewriteNonPattern(&classifier, CHECK_OK); |
1682 SetFunctionName(value, ast_value_factory()->default_string()); | 1682 SetFunctionName(value, ast_value_factory()->default_string()); |
1683 | 1683 |
1684 const AstRawString* local_name = | 1684 const AstRawString* local_name = |
1685 ast_value_factory()->star_default_star_string(); | 1685 ast_value_factory()->star_default_star_string(); |
1686 local_names.Add(local_name, zone()); | 1686 local_names.Add(local_name, zone()); |
1687 | 1687 |
1688 // It's fine to declare this as CONST because the user has no way of | 1688 // It's fine to declare this as CONST because the user has no way of |
1689 // writing to it. | 1689 // writing to it. |
1690 VariableProxy* proxy = NewUnresolved(local_name, CONST); | 1690 Declaration* decl = DeclareVariable(local_name, CONST, pos, CHECK_OK); |
1691 Declaration* declaration = | 1691 decl->proxy()->var()->set_initializer_position(position()); |
1692 factory()->NewVariableDeclaration(proxy, CONST, scope(), pos); | |
1693 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); | |
1694 proxy->var()->set_initializer_position(position()); | |
1695 | 1692 |
1696 Assignment* assignment = factory()->NewAssignment( | 1693 Assignment* assignment = factory()->NewAssignment( |
1697 Token::INIT, proxy, value, kNoSourcePosition); | 1694 Token::INIT, decl->proxy(), value, kNoSourcePosition); |
1698 result = factory()->NewExpressionStatement(assignment, kNoSourcePosition); | 1695 result = factory()->NewExpressionStatement(assignment, kNoSourcePosition); |
1699 | 1696 |
1700 ExpectSemicolon(CHECK_OK); | 1697 ExpectSemicolon(CHECK_OK); |
1701 break; | 1698 break; |
1702 } | 1699 } |
1703 } | 1700 } |
1704 | 1701 |
1705 DCHECK_EQ(local_names.length(), 1); | 1702 DCHECK_EQ(local_names.length(), 1); |
1706 module()->AddExport(local_names.first(), | 1703 module()->AddExport(local_names.first(), |
1707 ast_value_factory()->default_string(), default_loc, | 1704 ast_value_factory()->default_string(), default_loc, |
(...skipping 259 matching lines...) Loading... | |
1967 // scope. | 1964 // scope. |
1968 // Let/const variables are always added to the immediately enclosing scope. | 1965 // Let/const variables are always added to the immediately enclosing scope. |
1969 Scope* scope = IsLexicalVariableMode(mode) | 1966 Scope* scope = IsLexicalVariableMode(mode) |
1970 ? this->scope() | 1967 ? this->scope() |
1971 : this->scope()->GetDeclarationScope(); | 1968 : this->scope()->GetDeclarationScope(); |
1972 return scope->NewUnresolved(factory(), name, Variable::NORMAL, | 1969 return scope->NewUnresolved(factory(), name, Variable::NORMAL, |
1973 scanner()->location().beg_pos, | 1970 scanner()->location().beg_pos, |
1974 scanner()->location().end_pos); | 1971 scanner()->location().end_pos); |
1975 } | 1972 } |
1976 | 1973 |
1977 void Parser::DeclareConstVariable(const AstRawString* name, | 1974 InitializationFlag Parser::DefaultInitializationFlag(VariableMode mode) { |
adamk
2016/08/08 17:32:28
What about just putting this with the Initializati
neis
2016/08/09 08:17:52
Oops, this was of course supposed to be a static m
adamk
2016/08/09 16:27:09
Ah, forgot PatternRewriter is an inner class, this
| |
1978 InitializationFlag init, int pos, bool* ok) { | 1975 return mode == VAR ? kCreatedInitialized : kNeedsInitialization; |
adamk
2016/08/08 17:32:28
Maybe add a DCHECK(IsDeclaredVariableMode(mode)) a
neis
2016/08/09 08:17:52
Done.
| |
1979 DCHECK_NOT_NULL(name); | |
1980 VariableProxy* proxy = NewUnresolved(name, CONST); | |
1981 Declaration* declaration = | |
1982 factory()->NewVariableDeclaration(proxy, CONST, scope(), init, pos); | |
1983 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK_VOID); | |
1984 } | 1976 } |
1985 | 1977 |
1978 Declaration* Parser::DeclareVariable(const AstRawString* name, | |
1979 VariableMode mode, int pos, bool* ok) { | |
1980 return DeclareVariable(name, mode, DefaultInitializationFlag(mode), pos, ok); | |
1981 } | |
1982 | |
1983 Declaration* Parser::DeclareVariable(const AstRawString* name, | |
1984 VariableMode mode, InitializationFlag init, | |
1985 int pos, bool* ok) { | |
1986 DCHECK_NOT_NULL(name); | |
1987 VariableProxy* proxy = NewUnresolved(name, mode); | |
1988 Declaration* declaration = | |
1989 factory()->NewVariableDeclaration(proxy, mode, scope(), pos); | |
1990 Declare(declaration, DeclarationDescriptor::NORMAL, init, true, CHECK_OK); | |
1991 return declaration; | |
1992 } | |
1986 | 1993 |
1987 Variable* Parser::Declare(Declaration* declaration, | 1994 Variable* Parser::Declare(Declaration* declaration, |
1988 DeclarationDescriptor::Kind declaration_kind, | 1995 DeclarationDescriptor::Kind declaration_kind, |
1989 bool resolve, bool* ok, Scope* scope) { | 1996 InitializationFlag init, bool resolve, bool* ok, |
1997 Scope* scope) { | |
1990 VariableProxy* proxy = declaration->proxy(); | 1998 VariableProxy* proxy = declaration->proxy(); |
1991 DCHECK(proxy->raw_name() != NULL); | 1999 DCHECK(proxy->raw_name() != NULL); |
1992 const AstRawString* name = proxy->raw_name(); | 2000 const AstRawString* name = proxy->raw_name(); |
1993 VariableMode mode = declaration->mode(); | 2001 VariableMode mode = declaration->mode(); |
1994 DCHECK(IsDeclaredVariableMode(mode) && mode != CONST_LEGACY); | 2002 DCHECK(IsDeclaredVariableMode(mode) && mode != CONST_LEGACY); |
1995 bool is_function_declaration = declaration->IsFunctionDeclaration(); | 2003 bool is_function_declaration = declaration->IsFunctionDeclaration(); |
1996 if (scope == nullptr) scope = this->scope(); | 2004 if (scope == nullptr) scope = this->scope(); |
1997 Scope* declaration_scope = | 2005 Scope* declaration_scope = |
1998 IsLexicalVariableMode(mode) ? scope : scope->GetDeclarationScope(); | 2006 IsLexicalVariableMode(mode) ? scope : scope->GetDeclarationScope(); |
1999 Variable* var = NULL; | 2007 Variable* var = NULL; |
(...skipping 14 matching lines...) Loading... | |
2014 (is_strict(declaration_scope->language_mode()) || | 2022 (is_strict(declaration_scope->language_mode()) || |
2015 IsLexicalVariableMode(mode)))) { | 2023 IsLexicalVariableMode(mode)))) { |
2016 // Declare the variable in the declaration scope. | 2024 // Declare the variable in the declaration scope. |
2017 var = declaration_scope->LookupLocal(name); | 2025 var = declaration_scope->LookupLocal(name); |
2018 if (var == NULL) { | 2026 if (var == NULL) { |
2019 // Declare the name. | 2027 // Declare the name. |
2020 Variable::Kind kind = Variable::NORMAL; | 2028 Variable::Kind kind = Variable::NORMAL; |
2021 if (is_function_declaration) { | 2029 if (is_function_declaration) { |
2022 kind = Variable::FUNCTION; | 2030 kind = Variable::FUNCTION; |
2023 } | 2031 } |
2024 var = declaration_scope->DeclareLocal( | 2032 var = |
2025 name, mode, declaration->initialization(), kind, kNotAssigned); | 2033 declaration_scope->DeclareLocal(name, mode, init, kind, kNotAssigned); |
2026 } else if (IsLexicalVariableMode(mode) || | 2034 } else if (IsLexicalVariableMode(mode) || |
2027 IsLexicalVariableMode(var->mode())) { | 2035 IsLexicalVariableMode(var->mode())) { |
2028 // Allow duplicate function decls for web compat, see bug 4693. | 2036 // Allow duplicate function decls for web compat, see bug 4693. |
2029 bool duplicate_allowed = false; | 2037 bool duplicate_allowed = false; |
2030 if (is_sloppy(language_mode()) && is_function_declaration && | 2038 if (is_sloppy(language_mode()) && is_function_declaration && |
2031 var->is_function()) { | 2039 var->is_function()) { |
2032 DCHECK(IsLexicalVariableMode(mode) && | 2040 DCHECK(IsLexicalVariableMode(mode) && |
2033 IsLexicalVariableMode(var->mode())); | 2041 IsLexicalVariableMode(var->mode())); |
2034 // If the duplication is allowed, then the var will show up | 2042 // If the duplication is allowed, then the var will show up |
2035 // in the SloppyBlockFunctionMap and the new FunctionKind | 2043 // in the SloppyBlockFunctionMap and the new FunctionKind |
(...skipping 39 matching lines...) Loading... | |
2075 } | 2083 } |
2076 } else if (declaration_scope->is_eval_scope() && | 2084 } else if (declaration_scope->is_eval_scope() && |
2077 is_sloppy(declaration_scope->language_mode()) && | 2085 is_sloppy(declaration_scope->language_mode()) && |
2078 !IsLexicalVariableMode(mode)) { | 2086 !IsLexicalVariableMode(mode)) { |
2079 // In a var binding in a sloppy direct eval, pollute the enclosing scope | 2087 // In a var binding in a sloppy direct eval, pollute the enclosing scope |
2080 // with this new binding by doing the following: | 2088 // with this new binding by doing the following: |
2081 // The proxy is bound to a lookup variable to force a dynamic declaration | 2089 // The proxy is bound to a lookup variable to force a dynamic declaration |
2082 // using the DeclareEvalVar or DeclareEvalFunction runtime functions. | 2090 // using the DeclareEvalVar or DeclareEvalFunction runtime functions. |
2083 Variable::Kind kind = Variable::NORMAL; | 2091 Variable::Kind kind = Variable::NORMAL; |
2084 // TODO(sigurds) figure out if kNotAssigned is OK here | 2092 // TODO(sigurds) figure out if kNotAssigned is OK here |
2085 var = new (zone()) Variable(declaration_scope, name, mode, kind, | 2093 var = new (zone()) |
2086 declaration->initialization(), kNotAssigned); | 2094 Variable(declaration_scope, name, mode, kind, init, kNotAssigned); |
2087 var->AllocateTo(VariableLocation::LOOKUP, -1); | 2095 var->AllocateTo(VariableLocation::LOOKUP, -1); |
2088 resolve = true; | 2096 resolve = true; |
2089 } | 2097 } |
2090 | 2098 |
2091 | 2099 |
2092 // We add a declaration node for every declaration. The compiler | 2100 // We add a declaration node for every declaration. The compiler |
2093 // will only generate code if necessary. In particular, declarations | 2101 // will only generate code if necessary. In particular, declarations |
2094 // for inner local variables that do not represent functions won't | 2102 // for inner local variables that do not represent functions won't |
2095 // result in any generated code. | 2103 // result in any generated code. |
2096 // | 2104 // |
(...skipping 66 matching lines...) Loading... | |
2163 // Make sure that the function containing the native declaration | 2171 // Make sure that the function containing the native declaration |
2164 // isn't lazily compiled. The extension structures are only | 2172 // isn't lazily compiled. The extension structures are only |
2165 // accessible while parsing the first time not when reparsing | 2173 // accessible while parsing the first time not when reparsing |
2166 // because of lazy compilation. | 2174 // because of lazy compilation. |
2167 // TODO(adamk): Should this be GetClosureScope()? | 2175 // TODO(adamk): Should this be GetClosureScope()? |
2168 scope()->GetDeclarationScope()->ForceEagerCompilation(); | 2176 scope()->GetDeclarationScope()->ForceEagerCompilation(); |
2169 | 2177 |
2170 // TODO(1240846): It's weird that native function declarations are | 2178 // TODO(1240846): It's weird that native function declarations are |
2171 // introduced dynamically when we meet their declarations, whereas | 2179 // introduced dynamically when we meet their declarations, whereas |
2172 // other functions are set up when entering the surrounding scope. | 2180 // other functions are set up when entering the surrounding scope. |
2173 VariableProxy* proxy = NewUnresolved(name, VAR); | 2181 Declaration* decl = DeclareVariable(name, VAR, pos, CHECK_OK); |
2174 Declaration* declaration = | |
2175 factory()->NewVariableDeclaration(proxy, VAR, scope(), pos); | |
2176 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); | |
2177 NativeFunctionLiteral* lit = | 2182 NativeFunctionLiteral* lit = |
2178 factory()->NewNativeFunctionLiteral(name, extension_, kNoSourcePosition); | 2183 factory()->NewNativeFunctionLiteral(name, extension_, kNoSourcePosition); |
2179 return factory()->NewExpressionStatement( | 2184 return factory()->NewExpressionStatement( |
2180 factory()->NewAssignment(Token::INIT, proxy, lit, kNoSourcePosition), | 2185 factory()->NewAssignment(Token::INIT, decl->proxy(), lit, |
2186 kNoSourcePosition), | |
2181 pos); | 2187 pos); |
2182 } | 2188 } |
2183 | 2189 |
2184 Statement* Parser::ParseHoistableDeclaration( | 2190 Statement* Parser::ParseHoistableDeclaration( |
2185 ZoneList<const AstRawString*>* names, bool default_export, bool* ok) { | 2191 ZoneList<const AstRawString*>* names, bool default_export, bool* ok) { |
2186 Expect(Token::FUNCTION, CHECK_OK); | 2192 Expect(Token::FUNCTION, CHECK_OK); |
2187 int pos = position(); | 2193 int pos = position(); |
2188 ParseFunctionFlags flags = ParseFunctionFlags::kIsNormal; | 2194 ParseFunctionFlags flags = ParseFunctionFlags::kIsNormal; |
2189 if (Check(Token::MUL)) { | 2195 if (Check(Token::MUL)) { |
2190 flags |= ParseFunctionFlags::kIsGenerator; | 2196 flags |= ParseFunctionFlags::kIsGenerator; |
(...skipping 65 matching lines...) Loading... | |
2256 pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK); | 2262 pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK); |
2257 | 2263 |
2258 // In ES6, a function behaves as a lexical binding, except in | 2264 // In ES6, a function behaves as a lexical binding, except in |
2259 // a script scope, or the initial scope of eval or another function. | 2265 // a script scope, or the initial scope of eval or another function. |
2260 VariableMode mode = | 2266 VariableMode mode = |
2261 (!scope()->is_declaration_scope() || scope()->is_module_scope()) ? LET | 2267 (!scope()->is_declaration_scope() || scope()->is_module_scope()) ? LET |
2262 : VAR; | 2268 : VAR; |
2263 VariableProxy* proxy = NewUnresolved(variable_name, mode); | 2269 VariableProxy* proxy = NewUnresolved(variable_name, mode); |
2264 Declaration* declaration = | 2270 Declaration* declaration = |
2265 factory()->NewFunctionDeclaration(proxy, mode, fun, scope(), pos); | 2271 factory()->NewFunctionDeclaration(proxy, mode, fun, scope(), pos); |
2266 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); | 2272 Declare(declaration, DeclarationDescriptor::NORMAL, kCreatedInitialized, true, |
2273 CHECK_OK); | |
2267 if (names) names->Add(variable_name, zone()); | 2274 if (names) names->Add(variable_name, zone()); |
2268 EmptyStatement* empty = factory()->NewEmptyStatement(kNoSourcePosition); | 2275 EmptyStatement* empty = factory()->NewEmptyStatement(kNoSourcePosition); |
2269 // Async functions don't undergo sloppy mode block scoped hoisting, and don't | 2276 // Async functions don't undergo sloppy mode block scoped hoisting, and don't |
2270 // allow duplicates in a block. Both are represented by the | 2277 // allow duplicates in a block. Both are represented by the |
2271 // sloppy_block_function_map. Don't add them to the map for async functions. | 2278 // sloppy_block_function_map. Don't add them to the map for async functions. |
2272 // Generators are also supposed to be prohibited; currently doing this behind | 2279 // Generators are also supposed to be prohibited; currently doing this behind |
2273 // a flag and UseCounting violations to assess web compatibility. | 2280 // a flag and UseCounting violations to assess web compatibility. |
2274 if (is_sloppy(language_mode()) && !scope()->is_declaration_scope() && | 2281 if (is_sloppy(language_mode()) && !scope()->is_declaration_scope() && |
2275 !is_async && !(allow_harmony_restrictive_generators() && is_generator)) { | 2282 !is_async && !(allow_harmony_restrictive_generators() && is_generator)) { |
2276 SloppyBlockFunctionStatement* delegate = | 2283 SloppyBlockFunctionStatement* delegate = |
(...skipping 35 matching lines...) Loading... | |
2312 is_strict_reserved = false; | 2319 is_strict_reserved = false; |
2313 variable_name = ast_value_factory()->star_default_star_string(); | 2320 variable_name = ast_value_factory()->star_default_star_string(); |
2314 } else { | 2321 } else { |
2315 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK); | 2322 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK); |
2316 variable_name = name; | 2323 variable_name = name; |
2317 } | 2324 } |
2318 | 2325 |
2319 Expression* value = ParseClassLiteral(nullptr, name, scanner()->location(), | 2326 Expression* value = ParseClassLiteral(nullptr, name, scanner()->location(), |
2320 is_strict_reserved, pos, CHECK_OK); | 2327 is_strict_reserved, pos, CHECK_OK); |
2321 | 2328 |
2322 VariableProxy* proxy = NewUnresolved(variable_name, LET); | 2329 Declaration* decl = DeclareVariable(variable_name, LET, pos, CHECK_OK); |
2323 Declaration* declaration = | 2330 decl->proxy()->var()->set_initializer_position(position()); |
2324 factory()->NewVariableDeclaration(proxy, LET, scope(), pos); | |
2325 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); | |
2326 proxy->var()->set_initializer_position(position()); | |
2327 Assignment* assignment = | 2331 Assignment* assignment = |
2328 factory()->NewAssignment(Token::INIT, proxy, value, pos); | 2332 factory()->NewAssignment(Token::INIT, decl->proxy(), value, pos); |
2329 Statement* assignment_statement = | 2333 Statement* assignment_statement = |
2330 factory()->NewExpressionStatement(assignment, kNoSourcePosition); | 2334 factory()->NewExpressionStatement(assignment, kNoSourcePosition); |
2331 if (names) names->Add(variable_name, zone()); | 2335 if (names) names->Add(variable_name, zone()); |
2332 return assignment_statement; | 2336 return assignment_statement; |
2333 } | 2337 } |
2334 | 2338 |
2335 Block* Parser::ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok) { | 2339 Block* Parser::ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok) { |
2336 // The harmony mode uses block elements instead of statements. | 2340 // The harmony mode uses block elements instead of statements. |
2337 // | 2341 // |
2338 // Block :: | 2342 // Block :: |
(...skipping 1206 matching lines...) Loading... | |
3545 Block* inner_block = factory()->NewBlock(NULL, 3, false, kNoSourcePosition); | 3549 Block* inner_block = factory()->NewBlock(NULL, 3, false, kNoSourcePosition); |
3546 { | 3550 { |
3547 BlockState block_state(&scope_state_, inner_scope); | 3551 BlockState block_state(&scope_state_, inner_scope); |
3548 | 3552 |
3549 Block* ignore_completion_block = | 3553 Block* ignore_completion_block = |
3550 factory()->NewBlock(NULL, names->length() + 3, true, kNoSourcePosition); | 3554 factory()->NewBlock(NULL, names->length() + 3, true, kNoSourcePosition); |
3551 ZoneList<Variable*> inner_vars(names->length(), zone()); | 3555 ZoneList<Variable*> inner_vars(names->length(), zone()); |
3552 // For each let variable x: | 3556 // For each let variable x: |
3553 // make statement: let/const x = temp_x. | 3557 // make statement: let/const x = temp_x. |
3554 for (int i = 0; i < names->length(); i++) { | 3558 for (int i = 0; i < names->length(); i++) { |
3555 VariableProxy* proxy = NewUnresolved(names->at(i), mode); | 3559 Declaration* decl = |
3556 Declaration* declaration = factory()->NewVariableDeclaration( | 3560 DeclareVariable(names->at(i), mode, kNoSourcePosition, CHECK_OK); |
3557 proxy, mode, scope(), kNoSourcePosition); | 3561 inner_vars.Add(decl->proxy()->var(), zone()); |
3558 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); | |
3559 inner_vars.Add(declaration->proxy()->var(), zone()); | |
3560 VariableProxy* temp_proxy = factory()->NewVariableProxy(temps.at(i)); | 3562 VariableProxy* temp_proxy = factory()->NewVariableProxy(temps.at(i)); |
3561 Assignment* assignment = factory()->NewAssignment( | 3563 Assignment* assignment = factory()->NewAssignment( |
3562 Token::INIT, proxy, temp_proxy, kNoSourcePosition); | 3564 Token::INIT, decl->proxy(), temp_proxy, kNoSourcePosition); |
3563 Statement* assignment_statement = | 3565 Statement* assignment_statement = |
3564 factory()->NewExpressionStatement(assignment, kNoSourcePosition); | 3566 factory()->NewExpressionStatement(assignment, kNoSourcePosition); |
3565 DCHECK(init->position() != kNoSourcePosition); | 3567 DCHECK(init->position() != kNoSourcePosition); |
3566 proxy->var()->set_initializer_position(init->position()); | 3568 decl->proxy()->var()->set_initializer_position(init->position()); |
3567 ignore_completion_block->statements()->Add(assignment_statement, zone()); | 3569 ignore_completion_block->statements()->Add(assignment_statement, zone()); |
3568 } | 3570 } |
3569 | 3571 |
3570 // Make statement: if (first == 1) { first = 0; } else { next; } | 3572 // Make statement: if (first == 1) { first = 0; } else { next; } |
3571 if (next) { | 3573 if (next) { |
3572 DCHECK(first); | 3574 DCHECK(first); |
3573 Expression* compare = NULL; | 3575 Expression* compare = NULL; |
3574 // Make compare expression: first == 1. | 3576 // Make compare expression: first == 1. |
3575 { | 3577 { |
3576 Expression* const1 = factory()->NewSmiLiteral(1, kNoSourcePosition); | 3578 Expression* const1 = factory()->NewSmiLiteral(1, kNoSourcePosition); |
(...skipping 319 matching lines...) Loading... | |
3896 if (bound_names_are_lexical) { | 3898 if (bound_names_are_lexical) { |
3897 DCHECK_NULL(init_block); | 3899 DCHECK_NULL(init_block); |
3898 | 3900 |
3899 init_block = | 3901 init_block = |
3900 factory()->NewBlock(nullptr, 1, false, kNoSourcePosition); | 3902 factory()->NewBlock(nullptr, 1, false, kNoSourcePosition); |
3901 | 3903 |
3902 for (int i = 0; i < bound_names.length(); ++i) { | 3904 for (int i = 0; i < bound_names.length(); ++i) { |
3903 // TODO(adamk): This needs to be some sort of special | 3905 // TODO(adamk): This needs to be some sort of special |
3904 // INTERNAL variable that's invisible to the debugger | 3906 // INTERNAL variable that's invisible to the debugger |
3905 // but visible to everything else. | 3907 // but visible to everything else. |
3906 VariableProxy* tdz_proxy = NewUnresolved(bound_names[i], LET); | 3908 Declaration* tdz_decl = DeclareVariable( |
3907 Declaration* tdz_decl = factory()->NewVariableDeclaration( | 3909 bound_names[i], LET, kNoSourcePosition, CHECK_OK); |
3908 tdz_proxy, LET, scope(), kNoSourcePosition); | 3910 tdz_decl->proxy()->var()->set_initializer_position(position()); |
3909 Variable* tdz_var = Declare( | |
3910 tdz_decl, DeclarationDescriptor::NORMAL, true, CHECK_OK); | |
3911 tdz_var->set_initializer_position(position()); | |
3912 } | 3911 } |
3913 } | 3912 } |
3914 | 3913 |
3915 for_state.set_end_position(scanner()->location().end_pos); | 3914 for_state.set_end_position(scanner()->location().end_pos); |
3916 Scope* for_scope = for_state.FinalizedBlockScope(); | 3915 Scope* for_scope = for_state.FinalizedBlockScope(); |
3917 // Parsed for-in loop w/ variable declarations. | 3916 // Parsed for-in loop w/ variable declarations. |
3918 if (init_block != nullptr) { | 3917 if (init_block != nullptr) { |
3919 init_block->statements()->Add(final_loop, zone()); | 3918 init_block->statements()->Add(final_loop, zone()); |
3920 init_block->set_scope(for_scope); | 3919 init_block->set_scope(for_scope); |
3921 return init_block; | 3920 return init_block; |
(...skipping 1159 matching lines...) Loading... | |
5081 #ifdef DEBUG | 5080 #ifdef DEBUG |
5082 scope()->SetScopeName(name); | 5081 scope()->SetScopeName(name); |
5083 #endif | 5082 #endif |
5084 | 5083 |
5085 VariableProxy* proxy = nullptr; | 5084 VariableProxy* proxy = nullptr; |
5086 if (name != nullptr) { | 5085 if (name != nullptr) { |
5087 proxy = NewUnresolved(name, CONST); | 5086 proxy = NewUnresolved(name, CONST); |
5088 // TODO(verwaest): declare via block_state. | 5087 // TODO(verwaest): declare via block_state. |
5089 Declaration* declaration = factory()->NewVariableDeclaration( | 5088 Declaration* declaration = factory()->NewVariableDeclaration( |
5090 proxy, CONST, block_state.scope(), pos); | 5089 proxy, CONST, block_state.scope(), pos); |
5091 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); | 5090 Declare(declaration, DeclarationDescriptor::NORMAL, |
5091 DefaultInitializationFlag(CONST), true, CHECK_OK); | |
5092 } | 5092 } |
5093 | 5093 |
5094 Expression* extends = nullptr; | 5094 Expression* extends = nullptr; |
5095 if (Check(Token::EXTENDS)) { | 5095 if (Check(Token::EXTENDS)) { |
5096 block_state.set_start_position(scanner()->location().end_pos); | 5096 block_state.set_start_position(scanner()->location().end_pos); |
5097 ExpressionClassifier extends_classifier(this); | 5097 ExpressionClassifier extends_classifier(this); |
5098 extends = ParseLeftHandSideExpression(&extends_classifier, CHECK_OK); | 5098 extends = ParseLeftHandSideExpression(&extends_classifier, CHECK_OK); |
5099 CheckNoTailCallExpressions(&extends_classifier, CHECK_OK); | 5099 CheckNoTailCallExpressions(&extends_classifier, CHECK_OK); |
5100 RewriteNonPattern(&extends_classifier, CHECK_OK); | 5100 RewriteNonPattern(&extends_classifier, CHECK_OK); |
5101 if (classifier != nullptr) { | 5101 if (classifier != nullptr) { |
(...skipping 257 matching lines...) Loading... | |
5359 } while (query_scope != outer_scope); | 5359 } while (query_scope != outer_scope); |
5360 | 5360 |
5361 if (!should_hoist) continue; | 5361 if (!should_hoist) continue; |
5362 | 5362 |
5363 // Declare a var-style binding for the function in the outer scope | 5363 // Declare a var-style binding for the function in the outer scope |
5364 if (!var_created) { | 5364 if (!var_created) { |
5365 var_created = true; | 5365 var_created = true; |
5366 VariableProxy* proxy = scope->NewUnresolved(factory(), name); | 5366 VariableProxy* proxy = scope->NewUnresolved(factory(), name); |
5367 Declaration* declaration = factory()->NewVariableDeclaration( | 5367 Declaration* declaration = factory()->NewVariableDeclaration( |
5368 proxy, VAR, scope, kNoSourcePosition); | 5368 proxy, VAR, scope, kNoSourcePosition); |
5369 Declare(declaration, DeclarationDescriptor::NORMAL, true, ok, scope); | 5369 Declare(declaration, DeclarationDescriptor::NORMAL, |
5370 DefaultInitializationFlag(VAR), true, ok, scope); | |
5370 DCHECK(ok); // Based on the preceding check, this should not fail | 5371 DCHECK(ok); // Based on the preceding check, this should not fail |
5371 if (!ok) return; | 5372 if (!ok) return; |
5372 } | 5373 } |
5373 | 5374 |
5374 // Read from the local lexical scope and write to the function scope | 5375 // Read from the local lexical scope and write to the function scope |
5375 VariableProxy* to = scope->NewUnresolved(factory(), name); | 5376 VariableProxy* to = scope->NewUnresolved(factory(), name); |
5376 VariableProxy* from = delegate->scope()->NewUnresolved(factory(), name); | 5377 VariableProxy* from = delegate->scope()->NewUnresolved(factory(), name); |
5377 Expression* assignment = | 5378 Expression* assignment = |
5378 factory()->NewAssignment(Token::ASSIGN, to, from, kNoSourcePosition); | 5379 factory()->NewAssignment(Token::ASSIGN, to, from, kNoSourcePosition); |
5379 Statement* statement = | 5380 Statement* statement = |
(...skipping 1785 matching lines...) Loading... | |
7165 node->Print(Isolate::Current()); | 7166 node->Print(Isolate::Current()); |
7166 } | 7167 } |
7167 #endif // DEBUG | 7168 #endif // DEBUG |
7168 | 7169 |
7169 #undef CHECK_OK | 7170 #undef CHECK_OK |
7170 #undef CHECK_OK_VOID | 7171 #undef CHECK_OK_VOID |
7171 #undef CHECK_FAILED | 7172 #undef CHECK_FAILED |
7172 | 7173 |
7173 } // namespace internal | 7174 } // namespace internal |
7174 } // namespace v8 | 7175 } // namespace v8 |
OLD | NEW |