| 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" |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 | 131 |
| 132 explicit ParserFormalParameters(DeclarationScope* scope) | 132 explicit ParserFormalParameters(DeclarationScope* scope) |
| 133 : FormalParametersBase(scope), params(4, scope->zone()) {} | 133 : FormalParametersBase(scope), params(4, scope->zone()) {} |
| 134 ZoneList<Parameter> params; | 134 ZoneList<Parameter> params; |
| 135 | 135 |
| 136 int Arity() const { return params.length(); } | 136 int Arity() const { return params.length(); } |
| 137 const Parameter& at(int i) const { return params[i]; } | 137 const Parameter& at(int i) const { return params[i]; } |
| 138 }; | 138 }; |
| 139 | 139 |
| 140 template <> | 140 template <> |
| 141 class ParserBaseTraits<Parser> { | 141 struct ParserTypes<Parser> { |
| 142 public: | 142 typedef ParserBase<Parser> Base; |
| 143 struct Type { | 143 typedef Parser Impl; |
| 144 typedef ParserBase<Parser> Base; | |
| 145 typedef Parser Impl; | |
| 146 | 144 |
| 147 typedef Variable GeneratorVariable; | 145 typedef Variable GeneratorVariable; |
| 148 | 146 |
| 149 typedef v8::internal::AstProperties AstProperties; | 147 typedef v8::internal::AstProperties AstProperties; |
| 150 | 148 |
| 151 typedef v8::internal::ExpressionClassifier<ParserBaseTraits<Parser>> | 149 typedef v8::internal::ExpressionClassifier<ParserTypes<Parser>> |
| 152 ExpressionClassifier; | 150 ExpressionClassifier; |
| 153 | 151 |
| 154 // Return types for traversing functions. | 152 // Return types for traversing functions. |
| 155 typedef const AstRawString* Identifier; | 153 typedef const AstRawString* Identifier; |
| 156 typedef v8::internal::Expression* Expression; | 154 typedef v8::internal::Expression* Expression; |
| 157 typedef Yield* YieldExpression; | 155 typedef Yield* YieldExpression; |
| 158 typedef v8::internal::FunctionLiteral* FunctionLiteral; | 156 typedef v8::internal::FunctionLiteral* FunctionLiteral; |
| 159 typedef v8::internal::ClassLiteral* ClassLiteral; | 157 typedef v8::internal::ClassLiteral* ClassLiteral; |
| 160 typedef v8::internal::Literal* Literal; | 158 typedef v8::internal::Literal* Literal; |
| 161 typedef ObjectLiteral::Property* ObjectLiteralProperty; | 159 typedef ObjectLiteral::Property* ObjectLiteralProperty; |
| 162 typedef ZoneList<v8::internal::Expression*>* ExpressionList; | 160 typedef ZoneList<v8::internal::Expression*>* ExpressionList; |
| 163 typedef ZoneList<ObjectLiteral::Property*>* PropertyList; | 161 typedef ZoneList<ObjectLiteral::Property*>* PropertyList; |
| 164 typedef ParserFormalParameters::Parameter FormalParameter; | 162 typedef ParserFormalParameters::Parameter FormalParameter; |
| 165 typedef ParserFormalParameters FormalParameters; | 163 typedef ParserFormalParameters FormalParameters; |
| 166 typedef ZoneList<v8::internal::Statement*>* StatementList; | 164 typedef ZoneList<v8::internal::Statement*>* StatementList; |
| 167 | 165 |
| 168 // For constructing objects returned by the traversing functions. | 166 // For constructing objects returned by the traversing functions. |
| 169 typedef AstNodeFactory Factory; | 167 typedef AstNodeFactory Factory; |
| 170 }; | |
| 171 }; | 168 }; |
| 172 | 169 |
| 173 class Parser : public ParserBase<Parser> { | 170 class Parser : public ParserBase<Parser> { |
| 174 public: | 171 public: |
| 175 explicit Parser(ParseInfo* info); | 172 explicit Parser(ParseInfo* info); |
| 176 ~Parser() { | 173 ~Parser() { |
| 177 delete reusable_preparser_; | 174 delete reusable_preparser_; |
| 178 reusable_preparser_ = NULL; | 175 reusable_preparser_ = NULL; |
| 179 delete cached_parse_data_; | 176 delete cached_parse_data_; |
| 180 cached_parse_data_ = NULL; | 177 cached_parse_data_ = NULL; |
| 181 } | 178 } |
| 182 | 179 |
| 183 // Parses the source code represented by the compilation info and sets its | 180 // Parses the source code represented by the compilation info and sets its |
| 184 // function literal. Returns false (and deallocates any allocated AST | 181 // function literal. Returns false (and deallocates any allocated AST |
| 185 // nodes) if parsing failed. | 182 // nodes) if parsing failed. |
| 186 static bool ParseStatic(ParseInfo* info); | 183 static bool ParseStatic(ParseInfo* info); |
| 187 bool Parse(ParseInfo* info); | 184 bool Parse(ParseInfo* info); |
| 188 void ParseOnBackground(ParseInfo* info); | 185 void ParseOnBackground(ParseInfo* info); |
| 189 | 186 |
| 190 void DeserializeScopeChain(ParseInfo* info, Handle<Context> context, | 187 void DeserializeScopeChain(ParseInfo* info, Handle<Context> context, |
| 191 Scope::DeserializationMode deserialization_mode); | 188 Scope::DeserializationMode deserialization_mode); |
| 192 | 189 |
| 193 // Handle errors detected during parsing, move statistics to Isolate, | 190 // Handle errors detected during parsing, move statistics to Isolate, |
| 194 // internalize strings (move them to the heap). | 191 // internalize strings (move them to the heap). |
| 195 void Internalize(Isolate* isolate, Handle<Script> script, bool error); | 192 void Internalize(Isolate* isolate, Handle<Script> script, bool error); |
| 196 void HandleSourceURLComments(Isolate* isolate, Handle<Script> script); | 193 void HandleSourceURLComments(Isolate* isolate, Handle<Script> script); |
| 197 | 194 |
| 198 private: | 195 private: |
| 199 friend class ParserBase<Parser>; | 196 friend class ParserBase<Parser>; |
| 200 friend class v8::internal::ExpressionClassifier<ParserBaseTraits<Parser>>; | 197 friend class v8::internal::ExpressionClassifier<ParserTypes<Parser>>; |
| 201 | 198 |
| 202 // Runtime encoding of different completion modes. | 199 // Runtime encoding of different completion modes. |
| 203 enum CompletionKind { | 200 enum CompletionKind { |
| 204 kNormalCompletion, | 201 kNormalCompletion, |
| 205 kThrowCompletion, | 202 kThrowCompletion, |
| 206 kAbruptCompletion | 203 kAbruptCompletion |
| 207 }; | 204 }; |
| 208 | 205 |
| 209 enum class FunctionBodyType { kNormal, kSingleExpression }; | 206 enum class FunctionBodyType { kNormal, kSingleExpression }; |
| 210 | 207 |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 Expression* iterable, Statement* body, | 470 Expression* iterable, Statement* body, |
| 474 bool finalize, | 471 bool finalize, |
| 475 int next_result_pos = kNoSourcePosition); | 472 int next_result_pos = kNoSourcePosition); |
| 476 Statement* DesugarLexicalBindingsInForStatement( | 473 Statement* DesugarLexicalBindingsInForStatement( |
| 477 Scope* inner_scope, VariableMode mode, | 474 Scope* inner_scope, VariableMode mode, |
| 478 ZoneList<const AstRawString*>* names, ForStatement* loop, Statement* init, | 475 ZoneList<const AstRawString*>* names, ForStatement* loop, Statement* init, |
| 479 Expression* cond, Statement* next, Statement* body, bool* ok); | 476 Expression* cond, Statement* next, Statement* body, bool* ok); |
| 480 | 477 |
| 481 void DesugarAsyncFunctionBody(const AstRawString* function_name, Scope* scope, | 478 void DesugarAsyncFunctionBody(const AstRawString* function_name, Scope* scope, |
| 482 ZoneList<Statement*>* body, | 479 ZoneList<Statement*>* body, |
| 483 Type::ExpressionClassifier* classifier, | 480 ExpressionClassifier* classifier, |
| 484 FunctionKind kind, FunctionBodyType type, | 481 FunctionKind kind, FunctionBodyType type, |
| 485 bool accept_IN, int pos, bool* ok); | 482 bool accept_IN, int pos, bool* ok); |
| 486 | 483 |
| 487 void RewriteDoExpression(Expression* expr, bool* ok); | 484 void RewriteDoExpression(Expression* expr, bool* ok); |
| 488 | 485 |
| 489 FunctionLiteral* ParseFunctionLiteral( | 486 FunctionLiteral* ParseFunctionLiteral( |
| 490 const AstRawString* name, Scanner::Location function_name_location, | 487 const AstRawString* name, Scanner::Location function_name_location, |
| 491 FunctionNameValidity function_name_validity, FunctionKind kind, | 488 FunctionNameValidity function_name_validity, FunctionKind kind, |
| 492 int function_token_position, FunctionLiteral::FunctionType type, | 489 int function_token_position, FunctionLiteral::FunctionType type, |
| 493 LanguageMode language_mode, bool* ok); | 490 LanguageMode language_mode, bool* ok); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 void ParseAsyncArrowSingleExpressionBody(ZoneList<Statement*>* body, | 613 void ParseAsyncArrowSingleExpressionBody(ZoneList<Statement*>* body, |
| 617 bool accept_IN, | 614 bool accept_IN, |
| 618 ExpressionClassifier* classifier, | 615 ExpressionClassifier* classifier, |
| 619 int pos, bool* ok) { | 616 int pos, bool* ok) { |
| 620 DesugarAsyncFunctionBody(ast_value_factory()->empty_string(), scope(), body, | 617 DesugarAsyncFunctionBody(ast_value_factory()->empty_string(), scope(), body, |
| 621 classifier, kAsyncArrowFunction, | 618 classifier, kAsyncArrowFunction, |
| 622 FunctionBodyType::kSingleExpression, accept_IN, | 619 FunctionBodyType::kSingleExpression, accept_IN, |
| 623 pos, ok); | 620 pos, ok); |
| 624 } | 621 } |
| 625 | 622 |
| 626 ZoneList<v8::internal::Expression*>* PrepareSpreadArguments( | 623 ZoneList<Expression*>* PrepareSpreadArguments(ZoneList<Expression*>* list); |
| 627 ZoneList<v8::internal::Expression*>* list); | 624 Expression* SpreadCall(Expression* function, ZoneList<Expression*>* args, |
| 628 Expression* SpreadCall(Expression* function, | 625 int pos); |
| 629 ZoneList<v8::internal::Expression*>* args, int pos); | 626 Expression* SpreadCallNew(Expression* function, ZoneList<Expression*>* args, |
| 630 Expression* SpreadCallNew(Expression* function, | 627 int pos); |
| 631 ZoneList<v8::internal::Expression*>* args, int pos); | |
| 632 | 628 |
| 633 void SetLanguageMode(Scope* scope, LanguageMode mode); | 629 void SetLanguageMode(Scope* scope, LanguageMode mode); |
| 634 void RaiseLanguageMode(LanguageMode mode); | 630 void RaiseLanguageMode(LanguageMode mode); |
| 635 | 631 |
| 636 V8_INLINE void MarkCollectedTailCallExpressions(); | 632 V8_INLINE void MarkCollectedTailCallExpressions(); |
| 637 V8_INLINE void MarkTailPosition(Expression* expression); | 633 V8_INLINE void MarkTailPosition(Expression* expression); |
| 638 | 634 |
| 639 // Rewrite all DestructuringAssignments in the current FunctionState. | 635 // Rewrite all DestructuringAssignments in the current FunctionState. |
| 640 V8_INLINE void RewriteDestructuringAssignments(); | 636 V8_INLINE void RewriteDestructuringAssignments(); |
| 641 | 637 |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 936 } | 932 } |
| 937 return NewUnresolved(name, start_position, end_position); | 933 return NewUnresolved(name, start_position, end_position); |
| 938 } | 934 } |
| 939 | 935 |
| 940 V8_INLINE Expression* ExpressionFromString(int pos) { | 936 V8_INLINE Expression* ExpressionFromString(int pos) { |
| 941 const AstRawString* symbol = GetSymbol(); | 937 const AstRawString* symbol = GetSymbol(); |
| 942 if (fni_ != NULL) fni_->PushLiteralName(symbol); | 938 if (fni_ != NULL) fni_->PushLiteralName(symbol); |
| 943 return factory()->NewStringLiteral(symbol, pos); | 939 return factory()->NewStringLiteral(symbol, pos); |
| 944 } | 940 } |
| 945 | 941 |
| 946 V8_INLINE ZoneList<v8::internal::Expression*>* NewExpressionList( | 942 V8_INLINE ZoneList<Expression*>* NewExpressionList(int size) const { |
| 947 int size) const { | 943 return new (zone()) ZoneList<Expression*>(size, zone()); |
| 948 return new (zone()) ZoneList<v8::internal::Expression*>(size, zone()); | |
| 949 } | 944 } |
| 950 V8_INLINE ZoneList<ObjectLiteral::Property*>* NewPropertyList( | 945 V8_INLINE ZoneList<ObjectLiteral::Property*>* NewPropertyList( |
| 951 int size) const { | 946 int size) const { |
| 952 return new (zone()) ZoneList<ObjectLiteral::Property*>(size, zone()); | 947 return new (zone()) ZoneList<ObjectLiteral::Property*>(size, zone()); |
| 953 } | 948 } |
| 954 V8_INLINE ZoneList<v8::internal::Statement*>* NewStatementList( | 949 V8_INLINE ZoneList<Statement*>* NewStatementList(int size) const { |
| 955 int size) const { | 950 return new (zone()) ZoneList<Statement*>(size, zone()); |
| 956 return new (zone()) ZoneList<v8::internal::Statement*>(size, zone()); | |
| 957 } | 951 } |
| 958 | 952 |
| 959 V8_INLINE void AddParameterInitializationBlock( | 953 V8_INLINE void AddParameterInitializationBlock( |
| 960 const ParserFormalParameters& parameters, | 954 const ParserFormalParameters& parameters, ZoneList<Statement*>* body, |
| 961 ZoneList<v8::internal::Statement*>* body, bool is_async, bool* ok) { | 955 bool is_async, bool* ok) { |
| 962 if (parameters.is_simple) return; | 956 if (parameters.is_simple) return; |
| 963 auto* init_block = BuildParameterInitializationBlock(parameters, ok); | 957 auto* init_block = BuildParameterInitializationBlock(parameters, ok); |
| 964 if (!*ok) return; | 958 if (!*ok) return; |
| 965 if (is_async) init_block = BuildRejectPromiseOnException(init_block); | 959 if (is_async) init_block = BuildRejectPromiseOnException(init_block); |
| 966 if (init_block != nullptr) body->Add(init_block, zone()); | 960 if (init_block != nullptr) body->Add(init_block, zone()); |
| 967 } | 961 } |
| 968 | 962 |
| 969 V8_INLINE void AddFormalParameter(ParserFormalParameters* parameters, | 963 V8_INLINE void AddFormalParameter(ParserFormalParameters* parameters, |
| 970 Expression* pattern, | 964 Expression* pattern, |
| 971 Expression* initializer, | 965 Expression* initializer, |
| 972 int initializer_end_position, | 966 int initializer_end_position, |
| 973 bool is_rest) { | 967 bool is_rest) { |
| 974 bool is_simple = pattern->IsVariableProxy() && initializer == nullptr; | 968 bool is_simple = pattern->IsVariableProxy() && initializer == nullptr; |
| 975 const AstRawString* name = is_simple | 969 const AstRawString* name = is_simple |
| 976 ? pattern->AsVariableProxy()->raw_name() | 970 ? pattern->AsVariableProxy()->raw_name() |
| 977 : ast_value_factory()->empty_string(); | 971 : ast_value_factory()->empty_string(); |
| 978 parameters->params.Add( | 972 parameters->params.Add( |
| 979 ParserFormalParameters::Parameter(name, pattern, initializer, | 973 ParserFormalParameters::Parameter(name, pattern, initializer, |
| 980 initializer_end_position, is_rest), | 974 initializer_end_position, is_rest), |
| 981 parameters->scope->zone()); | 975 parameters->scope->zone()); |
| 982 } | 976 } |
| 983 | 977 |
| 984 V8_INLINE void DeclareFormalParameter( | 978 V8_INLINE void DeclareFormalParameter( |
| 985 DeclarationScope* scope, | 979 DeclarationScope* scope, |
| 986 const ParserFormalParameters::Parameter& parameter, | 980 const ParserFormalParameters::Parameter& parameter, |
| 987 Type::ExpressionClassifier* classifier) { | 981 ExpressionClassifier* classifier) { |
| 988 bool is_duplicate = false; | 982 bool is_duplicate = false; |
| 989 bool is_simple = classifier->is_simple_parameter_list(); | 983 bool is_simple = classifier->is_simple_parameter_list(); |
| 990 auto name = is_simple || parameter.is_rest | 984 auto name = is_simple || parameter.is_rest |
| 991 ? parameter.name | 985 ? parameter.name |
| 992 : ast_value_factory()->empty_string(); | 986 : ast_value_factory()->empty_string(); |
| 993 auto mode = is_simple || parameter.is_rest ? VAR : TEMPORARY; | 987 auto mode = is_simple || parameter.is_rest ? VAR : TEMPORARY; |
| 994 if (!is_simple) scope->SetHasNonSimpleParameters(); | 988 if (!is_simple) scope->SetHasNonSimpleParameters(); |
| 995 bool is_optional = parameter.initializer != nullptr; | 989 bool is_optional = parameter.initializer != nullptr; |
| 996 Variable* var = | 990 Variable* var = |
| 997 scope->DeclareParameter(name, mode, is_optional, parameter.is_rest, | 991 scope->DeclareParameter(name, mode, is_optional, parameter.is_rest, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1022 V8_INLINE void MaterializeUnspreadArgumentsLiterals(int count) {} | 1016 V8_INLINE void MaterializeUnspreadArgumentsLiterals(int count) {} |
| 1023 | 1017 |
| 1024 Expression* ExpressionListToExpression(ZoneList<Expression*>* args); | 1018 Expression* ExpressionListToExpression(ZoneList<Expression*>* args); |
| 1025 | 1019 |
| 1026 void SetFunctionNameFromPropertyName(ObjectLiteralProperty* property, | 1020 void SetFunctionNameFromPropertyName(ObjectLiteralProperty* property, |
| 1027 const AstRawString* name); | 1021 const AstRawString* name); |
| 1028 | 1022 |
| 1029 void SetFunctionNameFromIdentifierRef(Expression* value, | 1023 void SetFunctionNameFromIdentifierRef(Expression* value, |
| 1030 Expression* identifier); | 1024 Expression* identifier); |
| 1031 | 1025 |
| 1032 V8_INLINE ZoneList<typename Type::ExpressionClassifier::Error>* | 1026 V8_INLINE ZoneList<typename ExpressionClassifier::Error>* |
| 1033 GetReportedErrorList() const { | 1027 GetReportedErrorList() const { |
| 1034 return function_state_->GetReportedErrorList(); | 1028 return function_state_->GetReportedErrorList(); |
| 1035 } | 1029 } |
| 1036 | 1030 |
| 1037 V8_INLINE ZoneList<Expression*>* GetNonPatternList() const { | 1031 V8_INLINE ZoneList<Expression*>* GetNonPatternList() const { |
| 1038 return function_state_->non_patterns_to_rewrite(); | 1032 return function_state_->non_patterns_to_rewrite(); |
| 1039 } | 1033 } |
| 1040 | 1034 |
| 1041 // Parser's private field members. | 1035 // Parser's private field members. |
| 1042 | 1036 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1088 static const int kLiteralTypeSlot = 0; | 1082 static const int kLiteralTypeSlot = 0; |
| 1089 static const int kElementsSlot = 1; | 1083 static const int kElementsSlot = 1; |
| 1090 | 1084 |
| 1091 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); | 1085 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); |
| 1092 }; | 1086 }; |
| 1093 | 1087 |
| 1094 } // namespace internal | 1088 } // namespace internal |
| 1095 } // namespace v8 | 1089 } // namespace v8 |
| 1096 | 1090 |
| 1097 #endif // V8_PARSING_PARSER_H_ | 1091 #endif // V8_PARSING_PARSER_H_ |
| OLD | NEW |