OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 1085 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1096 Isolate* isolate_; | 1096 Isolate* isolate_; |
1097 bool only_simple_this_property_assignments_; | 1097 bool only_simple_this_property_assignments_; |
1098 ZoneStringList* names_; | 1098 ZoneStringList* names_; |
1099 ZoneList<int>* assigned_arguments_; | 1099 ZoneList<int>* assigned_arguments_; |
1100 ZoneObjectList* assigned_constants_; | 1100 ZoneObjectList* assigned_constants_; |
1101 }; | 1101 }; |
1102 | 1102 |
1103 | 1103 |
1104 Statement* Parser::ParseSourceElement(ZoneStringList* labels, | 1104 Statement* Parser::ParseSourceElement(ZoneStringList* labels, |
1105 bool* ok) { | 1105 bool* ok) { |
| 1106 // (Ecma 262 5th Edition, clause 14): |
| 1107 // SourceElement: |
| 1108 // Statement |
| 1109 // FunctionDeclaration |
| 1110 // |
| 1111 // In harmony mode we allow additionally the following productions |
| 1112 // SourceElement: |
| 1113 // LetDeclaration |
| 1114 |
1106 if (peek() == Token::FUNCTION) { | 1115 if (peek() == Token::FUNCTION) { |
1107 // FunctionDeclaration is only allowed in the context of SourceElements | |
1108 // (Ecma 262 5th Edition, clause 14): | |
1109 // SourceElement: | |
1110 // Statement | |
1111 // FunctionDeclaration | |
1112 // Common language extension is to allow function declaration in place | |
1113 // of any statement. This language extension is disabled in strict mode. | |
1114 return ParseFunctionDeclaration(ok); | 1116 return ParseFunctionDeclaration(ok); |
1115 } else if (peek() == Token::LET) { | 1117 } else if (peek() == Token::LET) { |
1116 return ParseVariableStatement(kSourceElement, ok); | 1118 return ParseVariableStatement(kSourceElement, ok); |
1117 } else { | 1119 } else { |
1118 return ParseStatement(labels, ok); | 1120 return ParseStatement(labels, ok); |
1119 } | 1121 } |
1120 } | 1122 } |
1121 | 1123 |
1122 | 1124 |
1123 void* Parser::ParseSourceElements(ZoneList<Statement*>* processor, | 1125 void* Parser::ParseSourceElements(ZoneList<Statement*>* processor, |
1124 int end_token, | 1126 int end_token, |
1125 bool* ok) { | 1127 bool* ok) { |
1126 // SourceElements :: | 1128 // SourceElements :: |
1127 // (Statement)* <end_token> | 1129 // (SourceElement)* <end_token> |
1128 | 1130 |
1129 // Allocate a target stack to use for this set of source | 1131 // Allocate a target stack to use for this set of source |
1130 // elements. This way, all scripts and functions get their own | 1132 // elements. This way, all scripts and functions get their own |
1131 // target stack thus avoiding illegal breaks and continues across | 1133 // target stack thus avoiding illegal breaks and continues across |
1132 // functions. | 1134 // functions. |
1133 TargetScope scope(&this->target_stack_); | 1135 TargetScope scope(&this->target_stack_); |
1134 | 1136 |
1135 ASSERT(processor != NULL); | 1137 ASSERT(processor != NULL); |
1136 InitializationBlockFinder block_finder(top_scope_, target_stack_); | 1138 InitializationBlockFinder block_finder(top_scope_, target_stack_); |
1137 ThisNamedPropertyAssigmentFinder this_property_assignment_finder(isolate()); | 1139 ThisNamedPropertyAssigmentFinder this_property_assignment_finder(isolate()); |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1288 Target target(&this->target_stack_, result); | 1290 Target target(&this->target_stack_, result); |
1289 TryStatement* statement = ParseTryStatement(CHECK_OK); | 1291 TryStatement* statement = ParseTryStatement(CHECK_OK); |
1290 if (statement) { | 1292 if (statement) { |
1291 statement->set_statement_pos(statement_pos); | 1293 statement->set_statement_pos(statement_pos); |
1292 } | 1294 } |
1293 if (result) result->AddStatement(statement); | 1295 if (result) result->AddStatement(statement); |
1294 return result; | 1296 return result; |
1295 } | 1297 } |
1296 | 1298 |
1297 case Token::FUNCTION: { | 1299 case Token::FUNCTION: { |
1298 // In strict mode, FunctionDeclaration is only allowed in the context | 1300 // FunctionDeclaration is only allowed in the context of SourceElements |
1299 // of SourceElements. | 1301 // (Ecma 262 5th Edition, clause 14): |
| 1302 // SourceElement: |
| 1303 // Statement |
| 1304 // FunctionDeclaration |
| 1305 // Common language extension is to allow function declaration in place |
| 1306 // of any statement. This language extension is disabled in strict mode. |
1300 if (top_scope_->is_strict_mode()) { | 1307 if (top_scope_->is_strict_mode()) { |
1301 ReportMessageAt(scanner().peek_location(), "strict_function", | 1308 ReportMessageAt(scanner().peek_location(), "strict_function", |
1302 Vector<const char*>::empty()); | 1309 Vector<const char*>::empty()); |
1303 *ok = false; | 1310 *ok = false; |
1304 return NULL; | 1311 return NULL; |
1305 } | 1312 } |
1306 return ParseFunctionDeclaration(ok); | 1313 return ParseFunctionDeclaration(ok); |
1307 } | 1314 } |
1308 | 1315 |
1309 case Token::DEBUGGER: | 1316 case Token::DEBUGGER: |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1548 result->AddStatement(stat); | 1555 result->AddStatement(stat); |
1549 block_finder.Update(stat); | 1556 block_finder.Update(stat); |
1550 } | 1557 } |
1551 } | 1558 } |
1552 Expect(Token::RBRACE, CHECK_OK); | 1559 Expect(Token::RBRACE, CHECK_OK); |
1553 return result; | 1560 return result; |
1554 } | 1561 } |
1555 | 1562 |
1556 | 1563 |
1557 Block* Parser::ParseScopedBlock(ZoneStringList* labels, bool* ok) { | 1564 Block* Parser::ParseScopedBlock(ZoneStringList* labels, bool* ok) { |
| 1565 // The harmony mode uses source elements instead of statements. |
| 1566 // |
| 1567 // Block :: |
| 1568 // '{' SourceElement* '}' |
| 1569 |
1558 // Construct block expecting 16 statements. | 1570 // Construct block expecting 16 statements. |
1559 Block* body = new(zone()) Block(isolate(), labels, 16, false); | 1571 Block* body = new(zone()) Block(isolate(), labels, 16, false); |
1560 Scope* saved_scope = top_scope_; | 1572 Scope* saved_scope = top_scope_; |
1561 Scope* block_scope = NewScope(top_scope_, | 1573 Scope* block_scope = NewScope(top_scope_, |
1562 Scope::BLOCK_SCOPE, | 1574 Scope::BLOCK_SCOPE, |
1563 inside_with()); | 1575 inside_with()); |
1564 if (top_scope_->is_strict_mode()) { | 1576 if (top_scope_->is_strict_mode()) { |
1565 block_scope->EnableStrictMode(); | 1577 block_scope->EnableStrictMode(); |
1566 } | 1578 } |
1567 top_scope_ = block_scope; | 1579 top_scope_ = block_scope; |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1746 int position = -1; | 1758 int position = -1; |
1747 if (peek() == Token::ASSIGN) { | 1759 if (peek() == Token::ASSIGN) { |
1748 Expect(Token::ASSIGN, CHECK_OK); | 1760 Expect(Token::ASSIGN, CHECK_OK); |
1749 position = scanner().location().beg_pos; | 1761 position = scanner().location().beg_pos; |
1750 value = ParseAssignmentExpression(var_context != kForStatement, CHECK_OK); | 1762 value = ParseAssignmentExpression(var_context != kForStatement, CHECK_OK); |
1751 // Don't infer if it is "a = function(){...}();"-like expression. | 1763 // Don't infer if it is "a = function(){...}();"-like expression. |
1752 if (fni_ != NULL && | 1764 if (fni_ != NULL && |
1753 value->AsCall() == NULL && | 1765 value->AsCall() == NULL && |
1754 value->AsCallNew() == NULL) { | 1766 value->AsCallNew() == NULL) { |
1755 fni_->Infer(); | 1767 fni_->Infer(); |
| 1768 } else { |
| 1769 fni_->RemoveLastFunction(); |
1756 } | 1770 } |
1757 } | 1771 } |
1758 | 1772 |
1759 // Make sure that 'const x' and 'let x' initialize 'x' to undefined. | 1773 // Make sure that 'const x' and 'let x' initialize 'x' to undefined. |
1760 if (value == NULL && needs_init) { | 1774 if (value == NULL && needs_init) { |
1761 value = GetLiteralUndefined(); | 1775 value = GetLiteralUndefined(); |
1762 } | 1776 } |
1763 | 1777 |
1764 // Global variable declarations must be compiled in a specific | 1778 // Global variable declarations must be compiled in a specific |
1765 // way. When the script containing the global variable declaration | 1779 // way. When the script containing the global variable declaration |
(...skipping 730 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2496 | 2510 |
2497 if (fni_ != NULL) { | 2511 if (fni_ != NULL) { |
2498 // Check if the right hand side is a call to avoid inferring a | 2512 // Check if the right hand side is a call to avoid inferring a |
2499 // name if we're dealing with "a = function(){...}();"-like | 2513 // name if we're dealing with "a = function(){...}();"-like |
2500 // expression. | 2514 // expression. |
2501 if ((op == Token::INIT_VAR | 2515 if ((op == Token::INIT_VAR |
2502 || op == Token::INIT_CONST | 2516 || op == Token::INIT_CONST |
2503 || op == Token::ASSIGN) | 2517 || op == Token::ASSIGN) |
2504 && (right->AsCall() == NULL && right->AsCallNew() == NULL)) { | 2518 && (right->AsCall() == NULL && right->AsCallNew() == NULL)) { |
2505 fni_->Infer(); | 2519 fni_->Infer(); |
| 2520 } else { |
| 2521 fni_->RemoveLastFunction(); |
2506 } | 2522 } |
2507 fni_->Leave(); | 2523 fni_->Leave(); |
2508 } | 2524 } |
2509 | 2525 |
2510 return new(zone()) Assignment(isolate(), op, expression, right, pos); | 2526 return new(zone()) Assignment(isolate(), op, expression, right, pos); |
2511 } | 2527 } |
2512 | 2528 |
2513 | 2529 |
2514 // Precedence = 3 | 2530 // Precedence = 3 |
2515 Expression* Parser::ParseConditionalExpression(bool accept_IN, bool* ok) { | 2531 Expression* Parser::ParseConditionalExpression(bool accept_IN, bool* ok) { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2607 // operations. (We could combine the two and get rid of this | 2623 // operations. (We could combine the two and get rid of this |
2608 // code and AST node eventually.) | 2624 // code and AST node eventually.) |
2609 if (Token::IsCompareOp(op)) { | 2625 if (Token::IsCompareOp(op)) { |
2610 // We have a comparison. | 2626 // We have a comparison. |
2611 Token::Value cmp = op; | 2627 Token::Value cmp = op; |
2612 switch (op) { | 2628 switch (op) { |
2613 case Token::NE: cmp = Token::EQ; break; | 2629 case Token::NE: cmp = Token::EQ; break; |
2614 case Token::NE_STRICT: cmp = Token::EQ_STRICT; break; | 2630 case Token::NE_STRICT: cmp = Token::EQ_STRICT; break; |
2615 default: break; | 2631 default: break; |
2616 } | 2632 } |
2617 x = NewCompareNode(cmp, x, y, position); | 2633 x = new(zone()) CompareOperation(isolate(), cmp, x, y, position); |
2618 if (cmp != op) { | 2634 if (cmp != op) { |
2619 // The comparison was negated - add a NOT. | 2635 // The comparison was negated - add a NOT. |
2620 x = new(zone()) UnaryOperation(isolate(), Token::NOT, x, position); | 2636 x = new(zone()) UnaryOperation(isolate(), Token::NOT, x, position); |
2621 } | 2637 } |
2622 | 2638 |
2623 } else { | 2639 } else { |
2624 // We have a "normal" binary operation. | 2640 // We have a "normal" binary operation. |
2625 x = new(zone()) BinaryOperation(isolate(), op, x, y, position); | 2641 x = new(zone()) BinaryOperation(isolate(), op, x, y, position); |
2626 } | 2642 } |
2627 } | 2643 } |
2628 } | 2644 } |
2629 return x; | 2645 return x; |
2630 } | 2646 } |
2631 | 2647 |
2632 | 2648 |
2633 Expression* Parser::NewCompareNode(Token::Value op, | |
2634 Expression* x, | |
2635 Expression* y, | |
2636 int position) { | |
2637 ASSERT(op != Token::NE && op != Token::NE_STRICT); | |
2638 if (op == Token::EQ || op == Token::EQ_STRICT) { | |
2639 bool is_strict = (op == Token::EQ_STRICT); | |
2640 Literal* x_literal = x->AsLiteral(); | |
2641 if (x_literal != NULL && x_literal->IsNull()) { | |
2642 return new(zone()) CompareToNull(isolate(), is_strict, y); | |
2643 } | |
2644 | |
2645 Literal* y_literal = y->AsLiteral(); | |
2646 if (y_literal != NULL && y_literal->IsNull()) { | |
2647 return new(zone()) CompareToNull(isolate(), is_strict, x); | |
2648 } | |
2649 } | |
2650 return new(zone()) CompareOperation(isolate(), op, x, y, position); | |
2651 } | |
2652 | |
2653 | |
2654 Expression* Parser::ParseUnaryExpression(bool* ok) { | 2649 Expression* Parser::ParseUnaryExpression(bool* ok) { |
2655 // UnaryExpression :: | 2650 // UnaryExpression :: |
2656 // PostfixExpression | 2651 // PostfixExpression |
2657 // 'delete' UnaryExpression | 2652 // 'delete' UnaryExpression |
2658 // 'void' UnaryExpression | 2653 // 'void' UnaryExpression |
2659 // 'typeof' UnaryExpression | 2654 // 'typeof' UnaryExpression |
2660 // '++' UnaryExpression | 2655 // '++' UnaryExpression |
2661 // '--' UnaryExpression | 2656 // '--' UnaryExpression |
2662 // '+' UnaryExpression | 2657 // '+' UnaryExpression |
2663 // '-' UnaryExpression | 2658 // '-' UnaryExpression |
(...skipping 2559 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5223 result = parser.ParseProgram(source, | 5218 result = parser.ParseProgram(source, |
5224 info->is_global(), | 5219 info->is_global(), |
5225 info->StrictMode()); | 5220 info->StrictMode()); |
5226 } | 5221 } |
5227 } | 5222 } |
5228 info->SetFunction(result); | 5223 info->SetFunction(result); |
5229 return (result != NULL); | 5224 return (result != NULL); |
5230 } | 5225 } |
5231 | 5226 |
5232 } } // namespace v8::internal | 5227 } } // namespace v8::internal |
OLD | NEW |