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 #ifndef V8_PREPARSER_H | 5 #ifndef V8_PREPARSER_H |
6 #define V8_PREPARSER_H | 6 #define V8_PREPARSER_H |
7 | 7 |
8 #include "src/v8.h" | 8 #include "src/v8.h" |
9 | 9 |
10 #include "src/bailout-reason.h" | 10 #include "src/bailout-reason.h" |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 typename Traits::Type::Scope** scope_stack_; | 163 typename Traits::Type::Scope** scope_stack_; |
164 typename Traits::Type::Scope* outer_scope_; | 164 typename Traits::Type::Scope* outer_scope_; |
165 typename Traits::Type::Scope* scope_; | 165 typename Traits::Type::Scope* scope_; |
166 }; | 166 }; |
167 | 167 |
168 class FunctionState BASE_EMBEDDED { | 168 class FunctionState BASE_EMBEDDED { |
169 public: | 169 public: |
170 FunctionState(FunctionState** function_state_stack, | 170 FunctionState(FunctionState** function_state_stack, |
171 typename Traits::Type::Scope** scope_stack, | 171 typename Traits::Type::Scope** scope_stack, |
172 typename Traits::Type::Scope* scope, | 172 typename Traits::Type::Scope* scope, |
173 typename Traits::Type::Zone* zone = NULL, | 173 typename Traits::Type::Factory* factory); |
174 AstValueFactory* ast_value_factory = NULL, | |
175 AstNode::IdGen* ast_node_id_gen = NULL); | |
176 FunctionState(FunctionState** function_state_stack, | 174 FunctionState(FunctionState** function_state_stack, |
177 typename Traits::Type::Scope** scope_stack, | 175 typename Traits::Type::Scope** scope_stack, |
178 typename Traits::Type::Scope** scope, | 176 typename Traits::Type::Scope** scope, |
179 typename Traits::Type::Zone* zone = NULL, | 177 typename Traits::Type::Factory* factory); |
180 AstValueFactory* ast_value_factory = NULL, | |
181 AstNode::IdGen* ast_node_id_gen = NULL); | |
182 ~FunctionState(); | 178 ~FunctionState(); |
183 | 179 |
184 int NextMaterializedLiteralIndex() { | 180 int NextMaterializedLiteralIndex() { |
185 return next_materialized_literal_index_++; | 181 return next_materialized_literal_index_++; |
186 } | 182 } |
187 int materialized_literal_count() { | 183 int materialized_literal_count() { |
188 return next_materialized_literal_index_ - JSFunction::kLiteralsPrefixSize; | 184 return next_materialized_literal_index_ - JSFunction::kLiteralsPrefixSize; |
189 } | 185 } |
190 | 186 |
191 int NextHandlerIndex() { return next_handler_index_++; } | 187 int NextHandlerIndex() { return next_handler_index_++; } |
(...skipping 10 matching lines...) Expand all Loading... |
202 DCHECK(variable != NULL); | 198 DCHECK(variable != NULL); |
203 DCHECK(!is_generator()); | 199 DCHECK(!is_generator()); |
204 generator_object_variable_ = variable; | 200 generator_object_variable_ = variable; |
205 is_generator_ = true; | 201 is_generator_ = true; |
206 } | 202 } |
207 typename Traits::Type::GeneratorVariable* generator_object_variable() | 203 typename Traits::Type::GeneratorVariable* generator_object_variable() |
208 const { | 204 const { |
209 return generator_object_variable_; | 205 return generator_object_variable_; |
210 } | 206 } |
211 | 207 |
212 typename Traits::Type::Factory* factory() { return &factory_; } | 208 typename Traits::Type::Factory* factory() { return factory_; } |
213 | 209 |
214 private: | 210 private: |
215 // Used to assign an index to each literal that needs materialization in | 211 // Used to assign an index to each literal that needs materialization in |
216 // the function. Includes regexp literals, and boilerplate for object and | 212 // the function. Includes regexp literals, and boilerplate for object and |
217 // array literals. | 213 // array literals. |
218 int next_materialized_literal_index_; | 214 int next_materialized_literal_index_; |
219 | 215 |
220 // Used to assign a per-function index to try and catch handlers. | 216 // Used to assign a per-function index to try and catch handlers. |
221 int next_handler_index_; | 217 int next_handler_index_; |
222 | 218 |
223 // Properties count estimation. | 219 // Properties count estimation. |
224 int expected_property_count_; | 220 int expected_property_count_; |
225 | 221 |
226 // Whether the function is a generator. | 222 // Whether the function is a generator. |
227 bool is_generator_; | 223 bool is_generator_; |
228 // For generators, this variable may hold the generator object. It variable | 224 // For generators, this variable may hold the generator object. It variable |
229 // is used by yield expressions and return statements. It is not necessary | 225 // is used by yield expressions and return statements. It is not necessary |
230 // for generator functions to have this variable set. | 226 // for generator functions to have this variable set. |
231 Variable* generator_object_variable_; | 227 Variable* generator_object_variable_; |
232 | 228 |
233 FunctionState** function_state_stack_; | 229 FunctionState** function_state_stack_; |
234 FunctionState* outer_function_state_; | 230 FunctionState* outer_function_state_; |
235 typename Traits::Type::Scope** scope_stack_; | 231 typename Traits::Type::Scope** scope_stack_; |
236 typename Traits::Type::Scope* outer_scope_; | 232 typename Traits::Type::Scope* outer_scope_; |
237 AstNode::IdGen* ast_node_id_gen_; // Only used by ParserTraits. | |
238 AstNode::IdGen saved_id_gen_; // Ditto. | |
239 typename Traits::Type::Zone* extra_param_; | 233 typename Traits::Type::Zone* extra_param_; |
240 typename Traits::Type::Factory factory_; | 234 typename Traits::Type::Factory* factory_; |
241 | 235 |
242 friend class ParserTraits; | 236 friend class ParserTraits; |
243 friend class Checkpoint; | 237 friend class Checkpoint; |
244 }; | 238 }; |
245 | 239 |
246 // Annoyingly, arrow functions first parse as comma expressions, then when we | 240 // Annoyingly, arrow functions first parse as comma expressions, then when we |
247 // see the => we have to go back and reinterpret the arguments as being formal | 241 // see the => we have to go back and reinterpret the arguments as being formal |
248 // parameters. To do so we need to reset some of the parser state back to | 242 // parameters. To do so we need to reset some of the parser state back to |
249 // what it was before the arguments were first seen. | 243 // what it was before the arguments were first seen. |
250 class Checkpoint BASE_EMBEDDED { | 244 class Checkpoint BASE_EMBEDDED { |
(...skipping 898 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1149 typedef PreParserExpressionList ExpressionList; | 1143 typedef PreParserExpressionList ExpressionList; |
1150 typedef PreParserExpressionList PropertyList; | 1144 typedef PreParserExpressionList PropertyList; |
1151 typedef PreParserStatementList StatementList; | 1145 typedef PreParserStatementList StatementList; |
1152 | 1146 |
1153 // For constructing objects returned by the traversing functions. | 1147 // For constructing objects returned by the traversing functions. |
1154 typedef PreParserFactory Factory; | 1148 typedef PreParserFactory Factory; |
1155 }; | 1149 }; |
1156 | 1150 |
1157 explicit PreParserTraits(PreParser* pre_parser) : pre_parser_(pre_parser) {} | 1151 explicit PreParserTraits(PreParser* pre_parser) : pre_parser_(pre_parser) {} |
1158 | 1152 |
1159 // Custom operations executed when FunctionStates are created and | |
1160 // destructed. (The PreParser doesn't need to do anything.) | |
1161 template <typename FunctionState> | |
1162 static void SetUpFunctionState(FunctionState* function_state) {} | |
1163 template <typename FunctionState> | |
1164 static void TearDownFunctionState(FunctionState* function_state) {} | |
1165 | |
1166 // Helper functions for recursive descent. | 1153 // Helper functions for recursive descent. |
1167 static bool IsEvalOrArguments(PreParserIdentifier identifier) { | 1154 static bool IsEvalOrArguments(PreParserIdentifier identifier) { |
1168 return identifier.IsEvalOrArguments(); | 1155 return identifier.IsEvalOrArguments(); |
1169 } | 1156 } |
1170 | 1157 |
1171 static bool IsPrototype(PreParserIdentifier identifier) { | 1158 static bool IsPrototype(PreParserIdentifier identifier) { |
1172 return identifier.IsPrototype(); | 1159 return identifier.IsPrototype(); |
1173 } | 1160 } |
1174 | 1161 |
1175 static bool IsConstructor(PreParserIdentifier identifier) { | 1162 static bool IsConstructor(PreParserIdentifier identifier) { |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1440 PreParser(Scanner* scanner, ParserRecorder* log, uintptr_t stack_limit) | 1427 PreParser(Scanner* scanner, ParserRecorder* log, uintptr_t stack_limit) |
1441 : ParserBase<PreParserTraits>(scanner, stack_limit, NULL, log, NULL, NULL, | 1428 : ParserBase<PreParserTraits>(scanner, stack_limit, NULL, log, NULL, NULL, |
1442 this) {} | 1429 this) {} |
1443 | 1430 |
1444 // Pre-parse the program from the character stream; returns true on | 1431 // Pre-parse the program from the character stream; returns true on |
1445 // success (even if parsing failed, the pre-parse data successfully | 1432 // success (even if parsing failed, the pre-parse data successfully |
1446 // captured the syntax error), and false if a stack-overflow happened | 1433 // captured the syntax error), and false if a stack-overflow happened |
1447 // during parsing. | 1434 // during parsing. |
1448 PreParseResult PreParseProgram() { | 1435 PreParseResult PreParseProgram() { |
1449 PreParserScope scope(scope_, GLOBAL_SCOPE); | 1436 PreParserScope scope(scope_, GLOBAL_SCOPE); |
1450 FunctionState top_scope(&function_state_, &scope_, &scope); | 1437 PreParserFactory factory(NULL, NULL, NULL); |
| 1438 FunctionState top_scope(&function_state_, &scope_, &scope, &factory); |
1451 bool ok = true; | 1439 bool ok = true; |
1452 int start_position = scanner()->peek_location().beg_pos; | 1440 int start_position = scanner()->peek_location().beg_pos; |
1453 ParseSourceElements(Token::EOS, &ok); | 1441 ParseSourceElements(Token::EOS, &ok); |
1454 if (stack_overflow()) return kPreParseStackOverflow; | 1442 if (stack_overflow()) return kPreParseStackOverflow; |
1455 if (!ok) { | 1443 if (!ok) { |
1456 ReportUnexpectedToken(scanner()->current_token()); | 1444 ReportUnexpectedToken(scanner()->current_token()); |
1457 } else if (scope_->strict_mode() == STRICT) { | 1445 } else if (scope_->strict_mode() == STRICT) { |
1458 CheckOctalLiteral(start_position, scanner()->location().end_pos, &ok); | 1446 CheckOctalLiteral(start_position, scanner()->location().end_pos, &ok); |
1459 } | 1447 } |
1460 return kPreParseSuccess; | 1448 return kPreParseSuccess; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1567 Token::Value fvar_init_op, bool is_generator, bool* ok) { | 1555 Token::Value fvar_init_op, bool is_generator, bool* ok) { |
1568 return pre_parser_->ParseEagerFunctionBody(function_name, pos, fvar, | 1556 return pre_parser_->ParseEagerFunctionBody(function_name, pos, fvar, |
1569 fvar_init_op, is_generator, ok); | 1557 fvar_init_op, is_generator, ok); |
1570 } | 1558 } |
1571 | 1559 |
1572 | 1560 |
1573 template <class Traits> | 1561 template <class Traits> |
1574 ParserBase<Traits>::FunctionState::FunctionState( | 1562 ParserBase<Traits>::FunctionState::FunctionState( |
1575 FunctionState** function_state_stack, | 1563 FunctionState** function_state_stack, |
1576 typename Traits::Type::Scope** scope_stack, | 1564 typename Traits::Type::Scope** scope_stack, |
1577 typename Traits::Type::Scope* scope, typename Traits::Type::Zone* zone, | 1565 typename Traits::Type::Scope* scope, |
1578 AstValueFactory* ast_value_factory, AstNode::IdGen* ast_node_id_gen) | 1566 typename Traits::Type::Factory* factory) |
1579 : next_materialized_literal_index_(JSFunction::kLiteralsPrefixSize), | 1567 : next_materialized_literal_index_(JSFunction::kLiteralsPrefixSize), |
1580 next_handler_index_(0), | 1568 next_handler_index_(0), |
1581 expected_property_count_(0), | 1569 expected_property_count_(0), |
1582 is_generator_(false), | 1570 is_generator_(false), |
1583 generator_object_variable_(NULL), | 1571 generator_object_variable_(NULL), |
1584 function_state_stack_(function_state_stack), | 1572 function_state_stack_(function_state_stack), |
1585 outer_function_state_(*function_state_stack), | 1573 outer_function_state_(*function_state_stack), |
1586 scope_stack_(scope_stack), | 1574 scope_stack_(scope_stack), |
1587 outer_scope_(*scope_stack), | 1575 outer_scope_(*scope_stack), |
1588 ast_node_id_gen_(ast_node_id_gen), | 1576 factory_(factory) { |
1589 factory_(zone, ast_value_factory, ast_node_id_gen) { | |
1590 *scope_stack_ = scope; | 1577 *scope_stack_ = scope; |
1591 *function_state_stack = this; | 1578 *function_state_stack = this; |
1592 Traits::SetUpFunctionState(this); | |
1593 } | 1579 } |
1594 | 1580 |
1595 | 1581 |
1596 template <class Traits> | 1582 template <class Traits> |
1597 ParserBase<Traits>::FunctionState::FunctionState( | 1583 ParserBase<Traits>::FunctionState::FunctionState( |
1598 FunctionState** function_state_stack, | 1584 FunctionState** function_state_stack, |
1599 typename Traits::Type::Scope** scope_stack, | 1585 typename Traits::Type::Scope** scope_stack, |
1600 typename Traits::Type::Scope** scope, typename Traits::Type::Zone* zone, | 1586 typename Traits::Type::Scope** scope, |
1601 AstValueFactory* ast_value_factory, AstNode::IdGen* ast_node_id_gen) | 1587 typename Traits::Type::Factory* factory) |
1602 : next_materialized_literal_index_(JSFunction::kLiteralsPrefixSize), | 1588 : next_materialized_literal_index_(JSFunction::kLiteralsPrefixSize), |
1603 next_handler_index_(0), | 1589 next_handler_index_(0), |
1604 expected_property_count_(0), | 1590 expected_property_count_(0), |
1605 is_generator_(false), | 1591 is_generator_(false), |
1606 generator_object_variable_(NULL), | 1592 generator_object_variable_(NULL), |
1607 function_state_stack_(function_state_stack), | 1593 function_state_stack_(function_state_stack), |
1608 outer_function_state_(*function_state_stack), | 1594 outer_function_state_(*function_state_stack), |
1609 scope_stack_(scope_stack), | 1595 scope_stack_(scope_stack), |
1610 outer_scope_(*scope_stack), | 1596 outer_scope_(*scope_stack), |
1611 ast_node_id_gen_(ast_node_id_gen), | 1597 factory_(factory) { |
1612 factory_(zone, ast_value_factory, ast_node_id_gen) { | |
1613 *scope_stack_ = *scope; | 1598 *scope_stack_ = *scope; |
1614 *function_state_stack = this; | 1599 *function_state_stack = this; |
1615 Traits::SetUpFunctionState(this); | |
1616 } | 1600 } |
1617 | 1601 |
1618 | 1602 |
1619 template <class Traits> | 1603 template <class Traits> |
1620 ParserBase<Traits>::FunctionState::~FunctionState() { | 1604 ParserBase<Traits>::FunctionState::~FunctionState() { |
1621 *scope_stack_ = outer_scope_; | 1605 *scope_stack_ = outer_scope_; |
1622 *function_state_stack_ = outer_function_state_; | 1606 *function_state_stack_ = outer_function_state_; |
1623 Traits::TearDownFunctionState(this); | |
1624 } | 1607 } |
1625 | 1608 |
1626 | 1609 |
1627 template<class Traits> | 1610 template<class Traits> |
1628 void ParserBase<Traits>::ReportUnexpectedToken(Token::Value token) { | 1611 void ParserBase<Traits>::ReportUnexpectedToken(Token::Value token) { |
1629 Scanner::Location source_location = scanner()->location(); | 1612 Scanner::Location source_location = scanner()->location(); |
1630 | 1613 |
1631 // Four of the tokens are treated specially | 1614 // Four of the tokens are treated specially |
1632 switch (token) { | 1615 switch (token) { |
1633 case Token::EOS: | 1616 case Token::EOS: |
(...skipping 1012 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2646 this->NewScope(scope_, FUNCTION_SCOPE); | 2629 this->NewScope(scope_, FUNCTION_SCOPE); |
2647 typename Traits::Type::StatementList body; | 2630 typename Traits::Type::StatementList body; |
2648 typename Traits::Type::AstProperties ast_properties; | 2631 typename Traits::Type::AstProperties ast_properties; |
2649 BailoutReason dont_optimize_reason = kNoReason; | 2632 BailoutReason dont_optimize_reason = kNoReason; |
2650 int num_parameters = -1; | 2633 int num_parameters = -1; |
2651 int materialized_literal_count = -1; | 2634 int materialized_literal_count = -1; |
2652 int expected_property_count = -1; | 2635 int expected_property_count = -1; |
2653 int handler_count = 0; | 2636 int handler_count = 0; |
2654 | 2637 |
2655 { | 2638 { |
2656 FunctionState function_state(&function_state_, &scope_, &scope, zone(), | 2639 typename Traits::Type::Factory function_factory( |
2657 this->ast_value_factory(), ast_node_id_gen_); | 2640 zone(), this->ast_value_factory(), ast_node_id_gen_); |
| 2641 FunctionState function_state(&function_state_, &scope_, &scope, |
| 2642 &function_factory); |
2658 Scanner::Location dupe_error_loc = Scanner::Location::invalid(); | 2643 Scanner::Location dupe_error_loc = Scanner::Location::invalid(); |
2659 num_parameters = Traits::DeclareArrowParametersFromExpression( | 2644 num_parameters = Traits::DeclareArrowParametersFromExpression( |
2660 params_ast, scope_, &dupe_error_loc, ok); | 2645 params_ast, scope_, &dupe_error_loc, ok); |
2661 if (!*ok) { | 2646 if (!*ok) { |
2662 ReportMessageAt( | 2647 ReportMessageAt( |
2663 Scanner::Location(start_pos, scanner()->location().beg_pos), | 2648 Scanner::Location(start_pos, scanner()->location().beg_pos), |
2664 "malformed_arrow_function_parameter_list"); | 2649 "malformed_arrow_function_parameter_list"); |
2665 return this->EmptyExpression(); | 2650 return this->EmptyExpression(); |
2666 } | 2651 } |
2667 | 2652 |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2864 DCHECK(IsAccessorAccessorConflict(old_type, type)); | 2849 DCHECK(IsAccessorAccessorConflict(old_type, type)); |
2865 // Both accessors of the same type. | 2850 // Both accessors of the same type. |
2866 parser()->ReportMessage("accessor_get_set"); | 2851 parser()->ReportMessage("accessor_get_set"); |
2867 } | 2852 } |
2868 *ok = false; | 2853 *ok = false; |
2869 } | 2854 } |
2870 } | 2855 } |
2871 } } // v8::internal | 2856 } } // v8::internal |
2872 | 2857 |
2873 #endif // V8_PREPARSER_H | 2858 #endif // V8_PREPARSER_H |
OLD | NEW |