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_PARSING_PARSER_H_ | 5 #ifndef V8_PARSING_PARSER_H_ |
6 #define V8_PARSING_PARSER_H_ | 6 #define V8_PARSING_PARSER_H_ |
7 | 7 |
8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
9 #include "src/ast/scopes.h" | 9 #include "src/ast/scopes.h" |
10 #include "src/parsing/parser-base.h" | 10 #include "src/parsing/parser-base.h" |
11 #include "src/parsing/preparse-data.h" | 11 #include "src/parsing/preparse-data.h" |
12 #include "src/parsing/preparse-data-format.h" | 12 #include "src/parsing/preparse-data-format.h" |
13 #include "src/parsing/preparser.h" | 13 #include "src/parsing/preparser.h" |
14 #include "src/pending-compilation-error-handler.h" | 14 #include "src/pending-compilation-error-handler.h" |
15 | 15 |
16 namespace v8 { | 16 namespace v8 { |
17 | 17 |
18 class ScriptCompiler; | 18 class ScriptCompiler; |
19 | 19 |
20 namespace internal { | 20 namespace internal { |
21 | 21 |
22 class ParseInfo; | 22 class ParseInfo; |
23 class ScriptData; | 23 class ScriptData; |
24 class Target; | 24 class ParserTarget; |
| 25 class ParserTargetScope; |
25 | 26 |
26 class FunctionEntry BASE_EMBEDDED { | 27 class FunctionEntry BASE_EMBEDDED { |
27 public: | 28 public: |
28 enum { | 29 enum { |
29 kStartPositionIndex, | 30 kStartPositionIndex, |
30 kEndPositionIndex, | 31 kEndPositionIndex, |
31 kLiteralCountIndex, | 32 kLiteralCountIndex, |
32 kPropertyCountIndex, | 33 kPropertyCountIndex, |
33 kLanguageModeIndex, | 34 kLanguageModeIndex, |
34 kUsesSuperPropertyIndex, | 35 kUsesSuperPropertyIndex, |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 typedef Variable GeneratorVariable; | 146 typedef Variable GeneratorVariable; |
146 | 147 |
147 // Return types for traversing functions. | 148 // Return types for traversing functions. |
148 typedef const AstRawString* Identifier; | 149 typedef const AstRawString* Identifier; |
149 typedef v8::internal::Expression* Expression; | 150 typedef v8::internal::Expression* Expression; |
150 typedef v8::internal::FunctionLiteral* FunctionLiteral; | 151 typedef v8::internal::FunctionLiteral* FunctionLiteral; |
151 typedef ObjectLiteral::Property* ObjectLiteralProperty; | 152 typedef ObjectLiteral::Property* ObjectLiteralProperty; |
152 typedef ZoneList<v8::internal::Expression*>* ExpressionList; | 153 typedef ZoneList<v8::internal::Expression*>* ExpressionList; |
153 typedef ZoneList<ObjectLiteral::Property*>* PropertyList; | 154 typedef ZoneList<ObjectLiteral::Property*>* PropertyList; |
154 typedef ParserFormalParameters FormalParameters; | 155 typedef ParserFormalParameters FormalParameters; |
| 156 typedef v8::internal::Statement* Statement; |
155 typedef ZoneList<v8::internal::Statement*>* StatementList; | 157 typedef ZoneList<v8::internal::Statement*>* StatementList; |
156 typedef v8::internal::Block* Block; | 158 typedef v8::internal::Block* Block; |
157 | 159 |
158 // For constructing objects returned by the traversing functions. | 160 // For constructing objects returned by the traversing functions. |
159 typedef AstNodeFactory Factory; | 161 typedef AstNodeFactory Factory; |
| 162 |
| 163 typedef ParserTarget Target; |
| 164 typedef ParserTargetScope TargetScope; |
160 }; | 165 }; |
161 | 166 |
162 class Parser : public ParserBase<Parser> { | 167 class Parser : public ParserBase<Parser> { |
163 public: | 168 public: |
164 explicit Parser(ParseInfo* info); | 169 explicit Parser(ParseInfo* info); |
165 ~Parser() { | 170 ~Parser() { |
166 delete reusable_preparser_; | 171 delete reusable_preparser_; |
167 reusable_preparser_ = NULL; | 172 reusable_preparser_ = NULL; |
168 delete cached_parse_data_; | 173 delete cached_parse_data_; |
169 cached_parse_data_ = NULL; | 174 cached_parse_data_ = NULL; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 return compile_options_; | 237 return compile_options_; |
233 } | 238 } |
234 bool consume_cached_parse_data() const { | 239 bool consume_cached_parse_data() const { |
235 return compile_options_ == ScriptCompiler::kConsumeParserCache && | 240 return compile_options_ == ScriptCompiler::kConsumeParserCache && |
236 cached_parse_data_ != NULL; | 241 cached_parse_data_ != NULL; |
237 } | 242 } |
238 bool produce_cached_parse_data() const { | 243 bool produce_cached_parse_data() const { |
239 return compile_options_ == ScriptCompiler::kProduceParserCache; | 244 return compile_options_ == ScriptCompiler::kProduceParserCache; |
240 } | 245 } |
241 | 246 |
242 // All ParseXXX functions take as the last argument an *ok parameter | |
243 // which is set to false if parsing failed; it is unchanged otherwise. | |
244 // By making the 'exception handling' explicit, we are forced to check | |
245 // for failure at the call sites. | |
246 void ParseStatementList(ZoneList<Statement*>* body, int end_token, bool* ok); | |
247 Statement* ParseStatementListItem(bool* ok); | |
248 void ParseModuleItemList(ZoneList<Statement*>* body, bool* ok); | 247 void ParseModuleItemList(ZoneList<Statement*>* body, bool* ok); |
249 Statement* ParseModuleItem(bool* ok); | 248 Statement* ParseModuleItem(bool* ok); |
250 const AstRawString* ParseModuleSpecifier(bool* ok); | 249 const AstRawString* ParseModuleSpecifier(bool* ok); |
251 void ParseImportDeclaration(bool* ok); | 250 void ParseImportDeclaration(bool* ok); |
252 Statement* ParseExportDeclaration(bool* ok); | 251 Statement* ParseExportDeclaration(bool* ok); |
253 Statement* ParseExportDefault(bool* ok); | 252 Statement* ParseExportDefault(bool* ok); |
254 void ParseExportClause(ZoneList<const AstRawString*>* export_names, | 253 void ParseExportClause(ZoneList<const AstRawString*>* export_names, |
255 ZoneList<Scanner::Location>* export_locations, | 254 ZoneList<Scanner::Location>* export_locations, |
256 ZoneList<const AstRawString*>* local_names, | 255 ZoneList<const AstRawString*>* local_names, |
257 Scanner::Location* reserved_loc, bool* ok); | 256 Scanner::Location* reserved_loc, bool* ok); |
258 struct NamedImport : public ZoneObject { | 257 struct NamedImport : public ZoneObject { |
259 const AstRawString* import_name; | 258 const AstRawString* import_name; |
260 const AstRawString* local_name; | 259 const AstRawString* local_name; |
261 const Scanner::Location location; | 260 const Scanner::Location location; |
262 NamedImport(const AstRawString* import_name, const AstRawString* local_name, | 261 NamedImport(const AstRawString* import_name, const AstRawString* local_name, |
263 Scanner::Location location) | 262 Scanner::Location location) |
264 : import_name(import_name), | 263 : import_name(import_name), |
265 local_name(local_name), | 264 local_name(local_name), |
266 location(location) {} | 265 location(location) {} |
267 }; | 266 }; |
268 ZoneList<const NamedImport*>* ParseNamedImports(int pos, bool* ok); | 267 ZoneList<const NamedImport*>* ParseNamedImports(int pos, bool* ok); |
269 Statement* ParseStatement(ZoneList<const AstRawString*>* labels, | |
270 AllowLabelledFunctionStatement allow_function, | |
271 bool* ok); | |
272 Statement* ParseSubStatement(ZoneList<const AstRawString*>* labels, | |
273 AllowLabelledFunctionStatement allow_function, | |
274 bool* ok); | |
275 Statement* ParseStatementAsUnlabelled(ZoneList<const AstRawString*>* labels, | |
276 bool* ok); | |
277 Statement* ParseFunctionDeclaration(bool* ok); | 268 Statement* ParseFunctionDeclaration(bool* ok); |
278 Statement* ParseHoistableDeclaration(ZoneList<const AstRawString*>* names, | 269 Statement* ParseHoistableDeclaration(ZoneList<const AstRawString*>* names, |
279 bool default_export, bool* ok); | 270 bool default_export, bool* ok); |
280 Statement* ParseHoistableDeclaration(int pos, ParseFunctionFlags flags, | 271 Statement* ParseHoistableDeclaration(int pos, ParseFunctionFlags flags, |
281 ZoneList<const AstRawString*>* names, | 272 ZoneList<const AstRawString*>* names, |
282 bool default_export, bool* ok); | 273 bool default_export, bool* ok); |
283 Statement* ParseAsyncFunctionDeclaration(ZoneList<const AstRawString*>* names, | 274 Statement* ParseAsyncFunctionDeclaration(ZoneList<const AstRawString*>* names, |
284 bool default_export, bool* ok); | 275 bool default_export, bool* ok); |
285 Expression* ParseAsyncFunctionExpression(bool* ok); | 276 Expression* ParseAsyncFunctionExpression(bool* ok); |
286 Statement* ParseClassDeclaration(ZoneList<const AstRawString*>* names, | 277 Statement* ParseClassDeclaration(ZoneList<const AstRawString*>* names, |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 FunctionBodyType::kSingleExpression, accept_IN, pos, ok); | 564 FunctionBodyType::kSingleExpression, accept_IN, pos, ok); |
574 } | 565 } |
575 | 566 |
576 ZoneList<Expression*>* PrepareSpreadArguments(ZoneList<Expression*>* list); | 567 ZoneList<Expression*>* PrepareSpreadArguments(ZoneList<Expression*>* list); |
577 Expression* SpreadCall(Expression* function, ZoneList<Expression*>* args, | 568 Expression* SpreadCall(Expression* function, ZoneList<Expression*>* args, |
578 int pos); | 569 int pos); |
579 Expression* SpreadCallNew(Expression* function, ZoneList<Expression*>* args, | 570 Expression* SpreadCallNew(Expression* function, ZoneList<Expression*>* args, |
580 int pos); | 571 int pos); |
581 | 572 |
582 void SetLanguageMode(Scope* scope, LanguageMode mode); | 573 void SetLanguageMode(Scope* scope, LanguageMode mode); |
583 void RaiseLanguageMode(LanguageMode mode); | 574 void SetAsmModule(); |
584 | 575 |
585 V8_INLINE void MarkCollectedTailCallExpressions(); | 576 V8_INLINE void MarkCollectedTailCallExpressions(); |
586 V8_INLINE void MarkTailPosition(Expression* expression); | 577 V8_INLINE void MarkTailPosition(Expression* expression); |
587 | 578 |
588 // Rewrite all DestructuringAssignments in the current FunctionState. | 579 // Rewrite all DestructuringAssignments in the current FunctionState. |
589 V8_INLINE void RewriteDestructuringAssignments(); | 580 V8_INLINE void RewriteDestructuringAssignments(); |
590 | 581 |
591 V8_INLINE Expression* RewriteExponentiation(Expression* left, | 582 V8_INLINE Expression* RewriteExponentiation(Expression* left, |
592 Expression* right, int pos); | 583 Expression* right, int pos); |
593 V8_INLINE Expression* RewriteAssignExponentiation(Expression* left, | 584 V8_INLINE Expression* RewriteAssignExponentiation(Expression* left, |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 V8_INLINE static bool IsBoilerplateProperty( | 686 V8_INLINE static bool IsBoilerplateProperty( |
696 ObjectLiteral::Property* property) { | 687 ObjectLiteral::Property* property) { |
697 return ObjectLiteral::IsBoilerplateProperty(property); | 688 return ObjectLiteral::IsBoilerplateProperty(property); |
698 } | 689 } |
699 | 690 |
700 V8_INLINE static bool IsArrayIndex(const AstRawString* string, | 691 V8_INLINE static bool IsArrayIndex(const AstRawString* string, |
701 uint32_t* index) { | 692 uint32_t* index) { |
702 return string->AsArrayIndex(index); | 693 return string->AsArrayIndex(index); |
703 } | 694 } |
704 | 695 |
| 696 V8_INLINE bool IsUseStrictDirective(Statement* statement) const { |
| 697 return IsStringLiteral(statement, ast_value_factory()->use_strict_string()); |
| 698 } |
| 699 |
| 700 V8_INLINE bool IsUseAsmDirective(Statement* statement) const { |
| 701 return IsStringLiteral(statement, ast_value_factory()->use_asm_string()); |
| 702 } |
| 703 |
| 704 // Returns true if the statement is an expression statement containing |
| 705 // a single string literal. If a second argument is given, the literal |
| 706 // is also compared with it and the result is true only if they are equal. |
| 707 V8_INLINE bool IsStringLiteral(Statement* statement, |
| 708 const AstRawString* arg = nullptr) const { |
| 709 ExpressionStatement* e_stat = statement->AsExpressionStatement(); |
| 710 if (e_stat == nullptr) return false; |
| 711 Literal* literal = e_stat->expression()->AsLiteral(); |
| 712 if (literal == nullptr || !literal->raw_value()->IsString()) return false; |
| 713 return arg == nullptr || literal->raw_value()->AsString() == arg; |
| 714 } |
| 715 |
705 V8_INLINE static Expression* GetPropertyValue( | 716 V8_INLINE static Expression* GetPropertyValue( |
706 ObjectLiteral::Property* property) { | 717 ObjectLiteral::Property* property) { |
707 return property->value(); | 718 return property->value(); |
708 } | 719 } |
709 | 720 |
710 // Functions for encapsulating the differences between parsing and preparsing; | 721 // Functions for encapsulating the differences between parsing and preparsing; |
711 // operations interleaved with the recursive descent. | 722 // operations interleaved with the recursive descent. |
712 V8_INLINE static void PushLiteralName(FuncNameInferrer* fni, | 723 V8_INLINE static void PushLiteralName(FuncNameInferrer* fni, |
713 const AstRawString* id) { | 724 const AstRawString* id) { |
714 fni->PushLiteralName(id); | 725 fni->PushLiteralName(id); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
841 V8_INLINE static Block* NullBlock() { return nullptr; } | 852 V8_INLINE static Block* NullBlock() { return nullptr; } |
842 | 853 |
843 V8_INLINE static bool IsEmptyExpression(Expression* expr) { | 854 V8_INLINE static bool IsEmptyExpression(Expression* expr) { |
844 return expr == nullptr; | 855 return expr == nullptr; |
845 } | 856 } |
846 | 857 |
847 // Used in error return values. | 858 // Used in error return values. |
848 V8_INLINE static ZoneList<Expression*>* NullExpressionList() { | 859 V8_INLINE static ZoneList<Expression*>* NullExpressionList() { |
849 return nullptr; | 860 return nullptr; |
850 } | 861 } |
| 862 V8_INLINE static bool IsNullExpressionList(ZoneList<Expression*>* exprs) { |
| 863 return exprs == nullptr; |
| 864 } |
851 V8_INLINE static ZoneList<Statement*>* NullStatementList() { return nullptr; } | 865 V8_INLINE static ZoneList<Statement*>* NullStatementList() { return nullptr; } |
| 866 V8_INLINE static bool IsNullStatementList(ZoneList<Statement*>* stmts) { |
| 867 return stmts == nullptr; |
| 868 } |
| 869 V8_INLINE static Statement* NullStatement() { return nullptr; } |
| 870 V8_INLINE bool IsNullOrEmptyStatement(Statement* stmt) { |
| 871 return stmt == nullptr || stmt->IsEmpty(); |
| 872 } |
852 | 873 |
853 // Non-NULL empty string. | 874 // Non-NULL empty string. |
854 V8_INLINE const AstRawString* EmptyIdentifierString() const { | 875 V8_INLINE const AstRawString* EmptyIdentifierString() const { |
855 return ast_value_factory()->empty_string(); | 876 return ast_value_factory()->empty_string(); |
856 } | 877 } |
857 | 878 |
858 // Odd-ball literal creators. | 879 // Odd-ball literal creators. |
859 V8_INLINE Literal* GetLiteralTheHole(int position) { | 880 V8_INLINE Literal* GetLiteralTheHole(int position) { |
860 return factory()->NewTheHoleLiteral(kNoSourcePosition); | 881 return factory()->NewTheHoleLiteral(kNoSourcePosition); |
861 } | 882 } |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1003 | 1024 |
1004 V8_INLINE ZoneList<Expression*>* GetNonPatternList() const { | 1025 V8_INLINE ZoneList<Expression*>* GetNonPatternList() const { |
1005 return function_state_->non_patterns_to_rewrite(); | 1026 return function_state_->non_patterns_to_rewrite(); |
1006 } | 1027 } |
1007 | 1028 |
1008 // Parser's private field members. | 1029 // Parser's private field members. |
1009 | 1030 |
1010 Scanner scanner_; | 1031 Scanner scanner_; |
1011 PreParser* reusable_preparser_; | 1032 PreParser* reusable_preparser_; |
1012 Scope* original_scope_; // for ES5 function declarations in sloppy eval | 1033 Scope* original_scope_; // for ES5 function declarations in sloppy eval |
1013 Target* target_stack_; // for break, continue statements | 1034 |
| 1035 friend class ParserTarget; |
| 1036 friend class ParserTargetScope; |
| 1037 ParserTarget* target_stack_; // for break, continue statements |
| 1038 |
1014 ScriptCompiler::CompileOptions compile_options_; | 1039 ScriptCompiler::CompileOptions compile_options_; |
1015 ParseData* cached_parse_data_; | 1040 ParseData* cached_parse_data_; |
1016 | 1041 |
1017 PendingCompilationErrorHandler pending_error_handler_; | 1042 PendingCompilationErrorHandler pending_error_handler_; |
1018 | 1043 |
1019 // Other information which will be stored in Parser and moved to Isolate after | 1044 // Other information which will be stored in Parser and moved to Isolate after |
1020 // parsing. | 1045 // parsing. |
1021 int use_counts_[v8::Isolate::kUseCounterFeatureCount]; | 1046 int use_counts_[v8::Isolate::kUseCounterFeatureCount]; |
1022 int total_preparse_skipped_; | 1047 int total_preparse_skipped_; |
1023 HistogramTimer* pre_parse_timer_; | 1048 HistogramTimer* pre_parse_timer_; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1055 static const int kLiteralTypeSlot = 0; | 1080 static const int kLiteralTypeSlot = 0; |
1056 static const int kElementsSlot = 1; | 1081 static const int kElementsSlot = 1; |
1057 | 1082 |
1058 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); | 1083 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); |
1059 }; | 1084 }; |
1060 | 1085 |
1061 } // namespace internal | 1086 } // namespace internal |
1062 } // namespace v8 | 1087 } // namespace v8 |
1063 | 1088 |
1064 #endif // V8_PARSING_PARSER_H_ | 1089 #endif // V8_PARSING_PARSER_H_ |
OLD | NEW |