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 853 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
864 bool Parse(ParseInfo* info); | 864 bool Parse(ParseInfo* info); |
865 void ParseOnBackground(ParseInfo* info); | 865 void ParseOnBackground(ParseInfo* info); |
866 | 866 |
867 // Handle errors detected during parsing, move statistics to Isolate, | 867 // Handle errors detected during parsing, move statistics to Isolate, |
868 // internalize strings (move them to the heap). | 868 // internalize strings (move them to the heap). |
869 void Internalize(Isolate* isolate, Handle<Script> script, bool error); | 869 void Internalize(Isolate* isolate, Handle<Script> script, bool error); |
870 void HandleSourceURLComments(Isolate* isolate, Handle<Script> script); | 870 void HandleSourceURLComments(Isolate* isolate, Handle<Script> script); |
871 | 871 |
872 private: | 872 private: |
873 friend class ParserTraits; | 873 friend class ParserTraits; |
874 class PatternRewriter; | |
875 | 874 |
876 // Limit the allowed number of local variables in a function. The hard limit | 875 // Limit the allowed number of local variables in a function. The hard limit |
877 // is that offsets computed by FullCodeGenerator::StackOperand and similar | 876 // is that offsets computed by FullCodeGenerator::StackOperand and similar |
878 // functions are ints, and they should not overflow. In addition, accessing | 877 // functions are ints, and they should not overflow. In addition, accessing |
879 // local variables creates user-controlled constants in the generated code, | 878 // local variables creates user-controlled constants in the generated code, |
880 // and we don't want too much user-controlled memory inside the code (this was | 879 // and we don't want too much user-controlled memory inside the code (this was |
881 // the reason why this limit was introduced in the first place; see | 880 // the reason why this limit was introduced in the first place; see |
882 // https://codereview.chromium.org/7003030/ ). | 881 // https://codereview.chromium.org/7003030/ ). |
883 static const int kMaxNumFunctionLocals = 4194303; // 2^22-1 | 882 static const int kMaxNumFunctionLocals = 4194303; // 2^22-1 |
884 | 883 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
933 bool* ok); | 932 bool* ok); |
934 Statement* ParseFunctionDeclaration(ZoneList<const AstRawString*>* names, | 933 Statement* ParseFunctionDeclaration(ZoneList<const AstRawString*>* names, |
935 bool* ok); | 934 bool* ok); |
936 Statement* ParseClassDeclaration(ZoneList<const AstRawString*>* names, | 935 Statement* ParseClassDeclaration(ZoneList<const AstRawString*>* names, |
937 bool* ok); | 936 bool* ok); |
938 Statement* ParseNativeDeclaration(bool* ok); | 937 Statement* ParseNativeDeclaration(bool* ok); |
939 Block* ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok); | 938 Block* ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok); |
940 Block* ParseVariableStatement(VariableDeclarationContext var_context, | 939 Block* ParseVariableStatement(VariableDeclarationContext var_context, |
941 ZoneList<const AstRawString*>* names, | 940 ZoneList<const AstRawString*>* names, |
942 bool* ok); | 941 bool* ok); |
943 Block* ParseVariableDeclarations(VariableDeclarationContext var_context, | 942 |
944 int* num_decl, | 943 struct DeclarationDescriptor { |
945 ZoneList<const AstRawString*>* names, | 944 Parser* parser; |
946 const AstRawString** out, | 945 Scope* declaration_scope; |
947 Scanner::Location* first_initializer_loc, | 946 Scope* scope; |
948 Scanner::Location* bindings_loc, bool* ok); | 947 VariableMode mode; |
| 948 bool is_const; |
| 949 bool needs_init; |
| 950 int pos; |
| 951 Token::Value init_op; |
| 952 }; |
| 953 |
| 954 struct DeclarationParsingResult { |
| 955 struct Declaration { |
| 956 Declaration(Expression* pattern, int initializer_position, |
| 957 Expression* initializer) |
| 958 : pattern(pattern), |
| 959 initializer_position(initializer_position), |
| 960 initializer(initializer) {} |
| 961 |
| 962 Expression* pattern; |
| 963 int initializer_position; |
| 964 Expression* initializer; |
| 965 }; |
| 966 |
| 967 DeclarationParsingResult() |
| 968 : declarations(4), |
| 969 first_initializer_loc(Scanner::Location::invalid()), |
| 970 bindings_loc(Scanner::Location::invalid()) {} |
| 971 |
| 972 Block* BuildInitializationBlock(ZoneList<const AstRawString*>* names, |
| 973 bool* ok); |
| 974 const AstRawString* SingleName() const; |
| 975 |
| 976 DeclarationDescriptor descriptor; |
| 977 List<Declaration> declarations; |
| 978 Scanner::Location first_initializer_loc; |
| 979 Scanner::Location bindings_loc; |
| 980 }; |
| 981 |
| 982 class PatternRewriter : private AstVisitor { |
| 983 public: |
| 984 static void DeclareAndInitializeVariables( |
| 985 Block* block, const DeclarationDescriptor* declaration_descriptor, |
| 986 const DeclarationParsingResult::Declaration* declaration, |
| 987 ZoneList<const AstRawString*>* names, bool* ok); |
| 988 |
| 989 void set_initializer_position(int pos) { initializer_position_ = pos; } |
| 990 |
| 991 private: |
| 992 PatternRewriter() {} |
| 993 |
| 994 #define DECLARE_VISIT(type) void Visit##type(v8::internal::type* node) override; |
| 995 // Visiting functions for AST nodes make this an AstVisitor. |
| 996 AST_NODE_LIST(DECLARE_VISIT) |
| 997 #undef DECLARE_VISIT |
| 998 virtual void Visit(AstNode* node) override; |
| 999 |
| 1000 void RecurseIntoSubpattern(AstNode* pattern, Expression* value) { |
| 1001 Expression* old_value = current_value_; |
| 1002 current_value_ = value; |
| 1003 pattern->Accept(this); |
| 1004 current_value_ = old_value; |
| 1005 } |
| 1006 |
| 1007 AstNodeFactory* factory() const { return descriptor_->parser->factory(); } |
| 1008 AstValueFactory* ast_value_factory() const { |
| 1009 return descriptor_->parser->ast_value_factory(); |
| 1010 } |
| 1011 bool inside_with() const { return descriptor_->parser->inside_with(); } |
| 1012 Zone* zone() const { return descriptor_->parser->zone(); } |
| 1013 |
| 1014 Expression* pattern_; |
| 1015 int initializer_position_; |
| 1016 Block* block_; |
| 1017 const DeclarationDescriptor* descriptor_; |
| 1018 ZoneList<const AstRawString*>* names_; |
| 1019 Expression* current_value_; |
| 1020 bool* ok_; |
| 1021 }; |
| 1022 |
| 1023 |
| 1024 void ParseVariableDeclarations(VariableDeclarationContext var_context, |
| 1025 DeclarationParsingResult* parsing_result, |
| 1026 bool* ok); |
949 Statement* ParseExpressionOrLabelledStatement( | 1027 Statement* ParseExpressionOrLabelledStatement( |
950 ZoneList<const AstRawString*>* labels, bool* ok); | 1028 ZoneList<const AstRawString*>* labels, bool* ok); |
951 IfStatement* ParseIfStatement(ZoneList<const AstRawString*>* labels, | 1029 IfStatement* ParseIfStatement(ZoneList<const AstRawString*>* labels, |
952 bool* ok); | 1030 bool* ok); |
953 Statement* ParseContinueStatement(bool* ok); | 1031 Statement* ParseContinueStatement(bool* ok); |
954 Statement* ParseBreakStatement(ZoneList<const AstRawString*>* labels, | 1032 Statement* ParseBreakStatement(ZoneList<const AstRawString*>* labels, |
955 bool* ok); | 1033 bool* ok); |
956 Statement* ParseReturnStatement(bool* ok); | 1034 Statement* ParseReturnStatement(bool* ok); |
957 Statement* ParseWithStatement(ZoneList<const AstRawString*>* labels, | 1035 Statement* ParseWithStatement(ZoneList<const AstRawString*>* labels, |
958 bool* ok); | 1036 bool* ok); |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1181 } | 1259 } |
1182 | 1260 |
1183 | 1261 |
1184 Expression* ParserTraits::SpreadCallNew( | 1262 Expression* ParserTraits::SpreadCallNew( |
1185 Expression* function, ZoneList<v8::internal::Expression*>* args, int pos) { | 1263 Expression* function, ZoneList<v8::internal::Expression*>* args, int pos) { |
1186 return parser_->SpreadCallNew(function, args, pos); | 1264 return parser_->SpreadCallNew(function, args, pos); |
1187 } | 1265 } |
1188 } } // namespace v8::internal | 1266 } } // namespace v8::internal |
1189 | 1267 |
1190 #endif // V8_PARSER_H_ | 1268 #endif // V8_PARSER_H_ |
OLD | NEW |