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_PARSER_H_ | 5 #ifndef V8_PARSER_H_ |
6 #define V8_PARSER_H_ | 6 #define V8_PARSER_H_ |
7 | 7 |
8 #include "src/allocation.h" | 8 #include "src/allocation.h" |
9 #include "src/ast.h" | 9 #include "src/ast.h" |
10 #include "src/compiler.h" // TODO(titzer): remove this include dependency | 10 #include "src/compiler.h" // TODO(titzer): remove this include dependency |
(...skipping 858 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
869 V8_INLINE ZoneList<v8::internal::Expression*>* PrepareSpreadArguments( | 869 V8_INLINE ZoneList<v8::internal::Expression*>* PrepareSpreadArguments( |
870 ZoneList<v8::internal::Expression*>* list); | 870 ZoneList<v8::internal::Expression*>* list); |
871 V8_INLINE void MaterializeUnspreadArgumentsLiterals(int count) {} | 871 V8_INLINE void MaterializeUnspreadArgumentsLiterals(int count) {} |
872 V8_INLINE Expression* SpreadCall(Expression* function, | 872 V8_INLINE Expression* SpreadCall(Expression* function, |
873 ZoneList<v8::internal::Expression*>* args, | 873 ZoneList<v8::internal::Expression*>* args, |
874 int pos); | 874 int pos); |
875 V8_INLINE Expression* SpreadCallNew(Expression* function, | 875 V8_INLINE Expression* SpreadCallNew(Expression* function, |
876 ZoneList<v8::internal::Expression*>* args, | 876 ZoneList<v8::internal::Expression*>* args, |
877 int pos); | 877 int pos); |
878 | 878 |
879 // Rewrite all DestructuringAssignments in the current FunctionState. | |
880 V8_INLINE void RewriteDestructuringAssignments(); | |
881 | |
882 V8_INLINE Expression* RewriteDestructuringAssignmentExpression( | |
883 Expression* expression); | |
884 | |
885 V8_INLINE void ShouldRewriteDestructuringAssignment(Expression* assignment); | |
886 | |
879 private: | 887 private: |
880 Parser* parser_; | 888 Parser* parser_; |
881 }; | 889 }; |
882 | 890 |
883 | 891 |
884 class Parser : public ParserBase<ParserTraits> { | 892 class Parser : public ParserBase<ParserTraits> { |
885 public: | 893 public: |
886 explicit Parser(ParseInfo* info); | 894 explicit Parser(ParseInfo* info); |
887 ~Parser() { | 895 ~Parser() { |
888 delete reusable_preparser_; | 896 delete reusable_preparser_; |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1017 Scanner::Location bindings_loc; | 1025 Scanner::Location bindings_loc; |
1018 }; | 1026 }; |
1019 | 1027 |
1020 class PatternRewriter : private AstVisitor { | 1028 class PatternRewriter : private AstVisitor { |
1021 public: | 1029 public: |
1022 static void DeclareAndInitializeVariables( | 1030 static void DeclareAndInitializeVariables( |
1023 Block* block, const DeclarationDescriptor* declaration_descriptor, | 1031 Block* block, const DeclarationDescriptor* declaration_descriptor, |
1024 const DeclarationParsingResult::Declaration* declaration, | 1032 const DeclarationParsingResult::Declaration* declaration, |
1025 ZoneList<const AstRawString*>* names, bool* ok); | 1033 ZoneList<const AstRawString*>* names, bool* ok); |
1026 | 1034 |
1035 static void RewriteDestructuringAssignment(Parser* parser, | |
1036 Assignment* assignment, | |
1037 Scope* Scope, bool* ok); | |
1038 | |
1027 void set_initializer_position(int pos) { initializer_position_ = pos; } | 1039 void set_initializer_position(int pos) { initializer_position_ = pos; } |
1028 | 1040 |
1029 private: | 1041 private: |
1030 PatternRewriter() {} | 1042 PatternRewriter() {} |
1031 | 1043 |
1032 #define DECLARE_VISIT(type) void Visit##type(v8::internal::type* node) override; | 1044 #define DECLARE_VISIT(type) void Visit##type(v8::internal::type* node) override; |
1033 // Visiting functions for AST nodes make this an AstVisitor. | 1045 // Visiting functions for AST nodes make this an AstVisitor. |
1034 AST_NODE_LIST(DECLARE_VISIT) | 1046 AST_NODE_LIST(DECLARE_VISIT) |
1035 #undef DECLARE_VISIT | 1047 #undef DECLARE_VISIT |
1036 void Visit(AstNode* node) override; | 1048 void Visit(AstNode* node) override; |
1037 | 1049 |
1050 enum PatternContext { BINDING, INITIALIZER, ASSIGNMENT }; | |
1051 | |
1052 PatternContext context() const { return context_; } | |
1053 void set_context(PatternContext context) { context_ = context; } | |
1054 | |
1038 void RecurseIntoSubpattern(AstNode* pattern, Expression* value) { | 1055 void RecurseIntoSubpattern(AstNode* pattern, Expression* value) { |
1039 Expression* old_value = current_value_; | 1056 Expression* old_value = current_value_; |
1040 current_value_ = value; | 1057 current_value_ = value; |
1041 pattern->Accept(this); | 1058 pattern->Accept(this); |
1042 current_value_ = old_value; | 1059 current_value_ = old_value; |
1043 } | 1060 } |
1044 | 1061 |
1062 void VisitObjectLiteral(ObjectLiteral* node, Variable** temp_var); | |
1063 void VisitArrayLiteral(ArrayLiteral* node, Variable** temp_var); | |
1064 | |
1045 Variable* CreateTempVar(Expression* value = nullptr); | 1065 Variable* CreateTempVar(Expression* value = nullptr); |
1046 | 1066 |
1047 AstNodeFactory* factory() const { return descriptor_->parser->factory(); } | 1067 AstNodeFactory* factory() const { return parser_->factory(); } |
1048 AstValueFactory* ast_value_factory() const { | 1068 AstValueFactory* ast_value_factory() const { |
1049 return descriptor_->parser->ast_value_factory(); | 1069 return parser_->ast_value_factory(); |
1050 } | 1070 } |
1051 Zone* zone() const { return descriptor_->parser->zone(); } | 1071 Zone* zone() const { return parser_->zone(); } |
1072 Scope* scope() const { return scope_; } | |
1052 | 1073 |
1074 Scope* scope_; | |
1075 Parser* parser_; | |
1076 PatternContext context_; | |
1053 Expression* pattern_; | 1077 Expression* pattern_; |
1054 int initializer_position_; | 1078 int initializer_position_; |
1055 Block* block_; | 1079 Block* block_; |
1056 const DeclarationDescriptor* descriptor_; | 1080 const DeclarationDescriptor* descriptor_; |
1057 ZoneList<const AstRawString*>* names_; | 1081 ZoneList<const AstRawString*>* names_; |
1058 Expression* current_value_; | 1082 Expression* current_value_; |
1059 bool* ok_; | 1083 bool* ok_; |
1060 }; | 1084 }; |
1061 | 1085 |
1062 | 1086 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1192 ZoneList<v8::internal::Expression*>* PrepareSpreadArguments( | 1216 ZoneList<v8::internal::Expression*>* PrepareSpreadArguments( |
1193 ZoneList<v8::internal::Expression*>* list); | 1217 ZoneList<v8::internal::Expression*>* list); |
1194 Expression* SpreadCall(Expression* function, | 1218 Expression* SpreadCall(Expression* function, |
1195 ZoneList<v8::internal::Expression*>* args, int pos); | 1219 ZoneList<v8::internal::Expression*>* args, int pos); |
1196 Expression* SpreadCallNew(Expression* function, | 1220 Expression* SpreadCallNew(Expression* function, |
1197 ZoneList<v8::internal::Expression*>* args, int pos); | 1221 ZoneList<v8::internal::Expression*>* args, int pos); |
1198 | 1222 |
1199 void SetLanguageMode(Scope* scope, LanguageMode mode); | 1223 void SetLanguageMode(Scope* scope, LanguageMode mode); |
1200 void RaiseLanguageMode(LanguageMode mode); | 1224 void RaiseLanguageMode(LanguageMode mode); |
1201 | 1225 |
1226 V8_INLINE void RewriteDestructuringAssignments(); | |
1227 | |
1228 friend class InitializerRewriter; | |
1229 void RewriteParameterInitializer(Expression* expr, Scope* scope); | |
1230 | |
1231 Expression* RewriteDestructuringAssignmentExpression(Expression* expression); | |
1232 | |
1202 Scanner scanner_; | 1233 Scanner scanner_; |
1203 PreParser* reusable_preparser_; | 1234 PreParser* reusable_preparser_; |
1204 Scope* original_scope_; // for ES5 function declarations in sloppy eval | 1235 Scope* original_scope_; // for ES5 function declarations in sloppy eval |
1205 Target* target_stack_; // for break, continue statements | 1236 Target* target_stack_; // for break, continue statements |
1206 ScriptCompiler::CompileOptions compile_options_; | 1237 ScriptCompiler::CompileOptions compile_options_; |
1207 ParseData* cached_parse_data_; | 1238 ParseData* cached_parse_data_; |
1208 | 1239 |
1209 PendingCompilationErrorHandler pending_error_handler_; | 1240 PendingCompilationErrorHandler pending_error_handler_; |
1210 | 1241 |
1211 // Other information which will be stored in Parser and moved to Isolate after | 1242 // Other information which will be stored in Parser and moved to Isolate after |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1380 } | 1411 } |
1381 } | 1412 } |
1382 } | 1413 } |
1383 | 1414 |
1384 | 1415 |
1385 DoExpression* ParserTraits::ParseDoExpression(bool* ok) { | 1416 DoExpression* ParserTraits::ParseDoExpression(bool* ok) { |
1386 return parser_->ParseDoExpression(ok); | 1417 return parser_->ParseDoExpression(ok); |
1387 } | 1418 } |
1388 | 1419 |
1389 | 1420 |
1421 void ParserTraits::RewriteDestructuringAssignments() { | |
1422 parser_->RewriteDestructuringAssignments(); | |
1423 } | |
1424 | |
1425 | |
1426 void Parser::RewriteDestructuringAssignments() { | |
1427 FunctionState* func = function_state_; | |
1428 typedef typename ParserBase<ParserTraits>::DestructuringAssignment Pair; | |
1429 const List<Pair>& assignments = func->destructuring_assignments_to_rewrite(); | |
1430 for (int i = assignments.length() - 1; i >= 0; --i) { | |
adamk
2015/11/20 22:42:58
Any particular reason for iterating backwards here
| |
1431 Pair pair = assignments.at(i); | |
1432 Assignment* to_rewrite = pair.assignment->AsAssignment(); | |
1433 Scope* scope = pair.scope; | |
1434 AssignmentPattern* pattern = to_rewrite->target()->AsAssignmentPattern(); | |
1435 if (pattern && !pattern->is_rewritten()) { | |
1436 bool ok = true; | |
1437 PatternRewriter::RewriteDestructuringAssignment(this, to_rewrite, scope, | |
1438 &ok); | |
1439 DCHECK(ok); | |
1440 } | |
1441 } | |
1442 } | |
1443 | |
1444 | |
1445 Expression* ParserTraits::RewriteDestructuringAssignmentExpression( | |
1446 Expression* expression) { | |
1447 return parser_->RewriteDestructuringAssignmentExpression(expression); | |
1448 } | |
1449 | |
1450 | |
1451 void ParserTraits::ShouldRewriteDestructuringAssignment(Expression* expr) { | |
adamk
2015/11/20 22:42:58
The name of this function doesn't make sense to me
| |
1452 DCHECK(expr->IsAssignment()); | |
adamk
2015/11/20 22:42:58
Why can't this method just take an Assignment* to
| |
1453 Assignment* assignment = expr->AsAssignment(); | |
1454 DCHECK(assignment->target()->IsAssignmentPattern()); | |
1455 DCHECK_EQ(Token::ASSIGN, assignment->op()); | |
1456 parser_->function_state_->AddDestructuringAssignment( | |
1457 Parser::DestructuringAssignment(assignment, parser_->scope_)); | |
1458 } | |
1459 | |
1460 inline void Parser::PatternRewriter::VisitObjectLiteral(ObjectLiteral* node) { | |
adamk
2015/11/20 22:42:58
Can these methods move to the .cc file? Seems like
| |
1461 Variable* temp_var = nullptr; | |
1462 VisitObjectLiteral(node, &temp_var); | |
1463 } | |
1464 | |
1465 inline void Parser::PatternRewriter::VisitArrayLiteral(ArrayLiteral* node) { | |
1466 Variable* temp_var = nullptr; | |
1467 VisitArrayLiteral(node, &temp_var); | |
1468 } | |
1469 | |
1390 } // namespace internal | 1470 } // namespace internal |
1391 } // namespace v8 | 1471 } // namespace v8 |
1392 | 1472 |
1393 #endif // V8_PARSER_H_ | 1473 #endif // V8_PARSER_H_ |
OLD | NEW |