Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_PARSING_PARSER_H_ |
| 6 | #define V8_PARSING_PARSER_H_ |
| 7 | |
| 8 | #include "src/allocation.h" |
| 9 | #include "src/ast/ast.h" |
| 10 | #include "src/ast/scopes.h" |
| 11 | #include "src/compiler.h" // TODO(titzer): remove this include dependency |
| 12 | #include "src/parsing/parser-base.h" |
| 13 | #include "src/parsing/preparse-data.h" |
| 14 | #include "src/parsing/preparse-data-format.h" |
| 15 | #include "src/parsing/preparser.h" |
| 16 | #include "src/pending-compilation-error-handler.h" |
| 17 | |
| 18 | namespace v8 { |
| 19 | |
| 20 | class ScriptCompiler; |
| 21 | |
| 22 | namespace internal { |
| 23 | |
| 24 | class Target; |
| 25 | |
| 26 | // A container for the inputs, configuration options, and outputs of parsing. |
| 27 | class ParseInfo { |
| 28 | public: |
| 29 | explicit ParseInfo(Zone* zone); |
| 30 | ParseInfo(Zone* zone, Handle<JSFunction> function); |
| 31 | ParseInfo(Zone* zone, Handle<Script> script); |
| 32 | // TODO(all) Only used via Debug::FindSharedFunctionInfoInScript, remove? |
| 33 | ParseInfo(Zone* zone, Handle<SharedFunctionInfo> shared); |
| 34 | |
| 35 | ~ParseInfo() { |
| 36 | if (ast_value_factory_owned()) { |
| 37 | delete ast_value_factory_; |
| 38 | set_ast_value_factory_owned(false); |
| 39 | } |
| 40 | ast_value_factory_ = nullptr; |
| 41 | } |
| 42 | |
| 43 | Zone* zone() { return zone_; } |
| 44 | |
| 45 | // Convenience accessor methods for flags. |
| 46 | #define FLAG_ACCESSOR(flag, getter, setter) \ |
| 47 | bool getter() const { return GetFlag(flag); } \ |
| 48 | void setter() { SetFlag(flag); } \ |
| 49 | void setter(bool val) { SetFlag(flag, val); } |
| 50 | |
| 51 | FLAG_ACCESSOR(kToplevel, is_toplevel, set_toplevel) |
| 52 | FLAG_ACCESSOR(kLazy, is_lazy, set_lazy) |
| 53 | FLAG_ACCESSOR(kEval, is_eval, set_eval) |
| 54 | FLAG_ACCESSOR(kGlobal, is_global, set_global) |
| 55 | FLAG_ACCESSOR(kStrictMode, is_strict_mode, set_strict_mode) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 56 | FLAG_ACCESSOR(kNative, is_native, set_native) |
| 57 | FLAG_ACCESSOR(kModule, is_module, set_module) |
| 58 | FLAG_ACCESSOR(kAllowLazyParsing, allow_lazy_parsing, set_allow_lazy_parsing) |
| 59 | FLAG_ACCESSOR(kAstValueFactoryOwned, ast_value_factory_owned, |
| 60 | set_ast_value_factory_owned) |
| 61 | |
| 62 | #undef FLAG_ACCESSOR |
| 63 | |
| 64 | void set_parse_restriction(ParseRestriction restriction) { |
| 65 | SetFlag(kParseRestriction, restriction != NO_PARSE_RESTRICTION); |
| 66 | } |
| 67 | |
| 68 | ParseRestriction parse_restriction() const { |
| 69 | return GetFlag(kParseRestriction) ? ONLY_SINGLE_FUNCTION_LITERAL |
| 70 | : NO_PARSE_RESTRICTION; |
| 71 | } |
| 72 | |
| 73 | ScriptCompiler::ExternalSourceStream* source_stream() { |
| 74 | return source_stream_; |
| 75 | } |
| 76 | void set_source_stream(ScriptCompiler::ExternalSourceStream* source_stream) { |
| 77 | source_stream_ = source_stream; |
| 78 | } |
| 79 | |
| 80 | ScriptCompiler::StreamedSource::Encoding source_stream_encoding() { |
| 81 | return source_stream_encoding_; |
| 82 | } |
| 83 | void set_source_stream_encoding( |
| 84 | ScriptCompiler::StreamedSource::Encoding source_stream_encoding) { |
| 85 | source_stream_encoding_ = source_stream_encoding; |
| 86 | } |
| 87 | |
| 88 | v8::Extension* extension() { return extension_; } |
| 89 | void set_extension(v8::Extension* extension) { extension_ = extension; } |
| 90 | |
| 91 | ScriptData** cached_data() { return cached_data_; } |
| 92 | void set_cached_data(ScriptData** cached_data) { cached_data_ = cached_data; } |
| 93 | |
| 94 | ScriptCompiler::CompileOptions compile_options() { return compile_options_; } |
| 95 | void set_compile_options(ScriptCompiler::CompileOptions compile_options) { |
| 96 | compile_options_ = compile_options; |
| 97 | } |
| 98 | |
| 99 | Scope* script_scope() { return script_scope_; } |
| 100 | void set_script_scope(Scope* script_scope) { script_scope_ = script_scope; } |
| 101 | |
| 102 | AstValueFactory* ast_value_factory() { return ast_value_factory_; } |
| 103 | void set_ast_value_factory(AstValueFactory* ast_value_factory) { |
| 104 | ast_value_factory_ = ast_value_factory; |
| 105 | } |
| 106 | |
| 107 | FunctionLiteral* literal() { return literal_; } |
| 108 | void set_literal(FunctionLiteral* literal) { literal_ = literal; } |
| 109 | |
| 110 | Scope* scope() { return scope_; } |
| 111 | void set_scope(Scope* scope) { scope_ = scope; } |
| 112 | |
| 113 | UnicodeCache* unicode_cache() { return unicode_cache_; } |
| 114 | void set_unicode_cache(UnicodeCache* unicode_cache) { |
| 115 | unicode_cache_ = unicode_cache; |
| 116 | } |
| 117 | |
| 118 | uintptr_t stack_limit() { return stack_limit_; } |
| 119 | void set_stack_limit(uintptr_t stack_limit) { stack_limit_ = stack_limit; } |
| 120 | |
| 121 | uint32_t hash_seed() { return hash_seed_; } |
| 122 | void set_hash_seed(uint32_t hash_seed) { hash_seed_ = hash_seed; } |
| 123 | |
| 124 | //-------------------------------------------------------------------------- |
| 125 | // TODO(titzer): these should not be part of ParseInfo. |
| 126 | //-------------------------------------------------------------------------- |
| 127 | Isolate* isolate() { return isolate_; } |
| 128 | Handle<JSFunction> closure() { return closure_; } |
| 129 | Handle<SharedFunctionInfo> shared_info() { return shared_; } |
| 130 | Handle<Script> script() { return script_; } |
| 131 | Handle<Context> context() { return context_; } |
| 132 | void clear_script() { script_ = Handle<Script>::null(); } |
| 133 | void set_isolate(Isolate* isolate) { isolate_ = isolate; } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 134 | void set_shared_info(Handle<SharedFunctionInfo> shared) { shared_ = shared; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 135 | void set_context(Handle<Context> context) { context_ = context; } |
| 136 | void set_script(Handle<Script> script) { script_ = script; } |
| 137 | //-------------------------------------------------------------------------- |
| 138 | |
| 139 | LanguageMode language_mode() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 140 | return construct_language_mode(is_strict_mode()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 141 | } |
| 142 | void set_language_mode(LanguageMode language_mode) { |
| 143 | STATIC_ASSERT(LANGUAGE_END == 3); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 144 | set_strict_mode(is_strict(language_mode)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void ReopenHandlesInNewHandleScope() { |
| 148 | closure_ = Handle<JSFunction>(*closure_); |
| 149 | shared_ = Handle<SharedFunctionInfo>(*shared_); |
| 150 | script_ = Handle<Script>(*script_); |
| 151 | context_ = Handle<Context>(*context_); |
| 152 | } |
| 153 | |
| 154 | #ifdef DEBUG |
| 155 | bool script_is_native() { return script_->type() == Script::TYPE_NATIVE; } |
| 156 | #endif // DEBUG |
| 157 | |
| 158 | private: |
| 159 | // Various configuration flags for parsing. |
| 160 | enum Flag { |
| 161 | // ---------- Input flags --------------------------- |
| 162 | kToplevel = 1 << 0, |
| 163 | kLazy = 1 << 1, |
| 164 | kEval = 1 << 2, |
| 165 | kGlobal = 1 << 3, |
| 166 | kStrictMode = 1 << 4, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 167 | kNative = 1 << 5, |
| 168 | kParseRestriction = 1 << 6, |
| 169 | kModule = 1 << 7, |
| 170 | kAllowLazyParsing = 1 << 8, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 171 | // ---------- Output flags -------------------------- |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 172 | kAstValueFactoryOwned = 1 << 9 |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | //------------- Inputs to parsing and scope analysis ----------------------- |
| 176 | Zone* zone_; |
| 177 | unsigned flags_; |
| 178 | ScriptCompiler::ExternalSourceStream* source_stream_; |
| 179 | ScriptCompiler::StreamedSource::Encoding source_stream_encoding_; |
| 180 | v8::Extension* extension_; |
| 181 | ScriptCompiler::CompileOptions compile_options_; |
| 182 | Scope* script_scope_; |
| 183 | UnicodeCache* unicode_cache_; |
| 184 | uintptr_t stack_limit_; |
| 185 | uint32_t hash_seed_; |
| 186 | |
| 187 | // TODO(titzer): Move handles and isolate out of ParseInfo. |
| 188 | Isolate* isolate_; |
| 189 | Handle<JSFunction> closure_; |
| 190 | Handle<SharedFunctionInfo> shared_; |
| 191 | Handle<Script> script_; |
| 192 | Handle<Context> context_; |
| 193 | |
| 194 | //----------- Inputs+Outputs of parsing and scope analysis ----------------- |
| 195 | ScriptData** cached_data_; // used if available, populated if requested. |
| 196 | AstValueFactory* ast_value_factory_; // used if available, otherwise new. |
| 197 | |
| 198 | //----------- Outputs of parsing and scope analysis ------------------------ |
| 199 | FunctionLiteral* literal_; // produced by full parser. |
| 200 | Scope* scope_; // produced by scope analysis. |
| 201 | |
| 202 | void SetFlag(Flag f) { flags_ |= f; } |
| 203 | void SetFlag(Flag f, bool v) { flags_ = v ? flags_ | f : flags_ & ~f; } |
| 204 | bool GetFlag(Flag f) const { return (flags_ & f) != 0; } |
| 205 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 206 | void set_closure(Handle<JSFunction> closure) { closure_ = closure; } |
| 207 | }; |
| 208 | |
| 209 | class FunctionEntry BASE_EMBEDDED { |
| 210 | public: |
| 211 | enum { |
| 212 | kStartPositionIndex, |
| 213 | kEndPositionIndex, |
| 214 | kLiteralCountIndex, |
| 215 | kPropertyCountIndex, |
| 216 | kLanguageModeIndex, |
| 217 | kUsesSuperPropertyIndex, |
| 218 | kCallsEvalIndex, |
| 219 | kSize |
| 220 | }; |
| 221 | |
| 222 | explicit FunctionEntry(Vector<unsigned> backing) |
| 223 | : backing_(backing) { } |
| 224 | |
| 225 | FunctionEntry() : backing_() { } |
| 226 | |
| 227 | int start_pos() { return backing_[kStartPositionIndex]; } |
| 228 | int end_pos() { return backing_[kEndPositionIndex]; } |
| 229 | int literal_count() { return backing_[kLiteralCountIndex]; } |
| 230 | int property_count() { return backing_[kPropertyCountIndex]; } |
| 231 | LanguageMode language_mode() { |
| 232 | DCHECK(is_valid_language_mode(backing_[kLanguageModeIndex])); |
| 233 | return static_cast<LanguageMode>(backing_[kLanguageModeIndex]); |
| 234 | } |
| 235 | bool uses_super_property() { return backing_[kUsesSuperPropertyIndex]; } |
| 236 | bool calls_eval() { return backing_[kCallsEvalIndex]; } |
| 237 | |
| 238 | bool is_valid() { return !backing_.is_empty(); } |
| 239 | |
| 240 | private: |
| 241 | Vector<unsigned> backing_; |
| 242 | }; |
| 243 | |
| 244 | |
| 245 | // Wrapper around ScriptData to provide parser-specific functionality. |
| 246 | class ParseData { |
| 247 | public: |
| 248 | static ParseData* FromCachedData(ScriptData* cached_data) { |
| 249 | ParseData* pd = new ParseData(cached_data); |
| 250 | if (pd->IsSane()) return pd; |
| 251 | cached_data->Reject(); |
| 252 | delete pd; |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | void Initialize(); |
| 257 | FunctionEntry GetFunctionEntry(int start); |
| 258 | int FunctionCount(); |
| 259 | |
| 260 | bool HasError(); |
| 261 | |
| 262 | unsigned* Data() { // Writable data as unsigned int array. |
| 263 | return reinterpret_cast<unsigned*>(const_cast<byte*>(script_data_->data())); |
| 264 | } |
| 265 | |
| 266 | void Reject() { script_data_->Reject(); } |
| 267 | |
| 268 | bool rejected() const { return script_data_->rejected(); } |
| 269 | |
| 270 | private: |
| 271 | explicit ParseData(ScriptData* script_data) : script_data_(script_data) {} |
| 272 | |
| 273 | bool IsSane(); |
| 274 | unsigned Magic(); |
| 275 | unsigned Version(); |
| 276 | int FunctionsSize(); |
| 277 | int Length() const { |
| 278 | // Script data length is already checked to be a multiple of unsigned size. |
| 279 | return script_data_->length() / sizeof(unsigned); |
| 280 | } |
| 281 | |
| 282 | ScriptData* script_data_; |
| 283 | int function_index_; |
| 284 | |
| 285 | DISALLOW_COPY_AND_ASSIGN(ParseData); |
| 286 | }; |
| 287 | |
| 288 | // ---------------------------------------------------------------------------- |
| 289 | // JAVASCRIPT PARSING |
| 290 | |
| 291 | class Parser; |
| 292 | class SingletonLogger; |
| 293 | |
| 294 | |
| 295 | struct ParserFormalParameters : FormalParametersBase { |
| 296 | struct Parameter { |
| 297 | Parameter(const AstRawString* name, Expression* pattern, |
| 298 | Expression* initializer, int initializer_end_position, |
| 299 | bool is_rest) |
| 300 | : name(name), |
| 301 | pattern(pattern), |
| 302 | initializer(initializer), |
| 303 | initializer_end_position(initializer_end_position), |
| 304 | is_rest(is_rest) {} |
| 305 | const AstRawString* name; |
| 306 | Expression* pattern; |
| 307 | Expression* initializer; |
| 308 | int initializer_end_position; |
| 309 | bool is_rest; |
| 310 | bool is_simple() const { |
| 311 | return pattern->IsVariableProxy() && initializer == nullptr && !is_rest; |
| 312 | } |
| 313 | }; |
| 314 | |
| 315 | explicit ParserFormalParameters(Scope* scope) |
| 316 | : FormalParametersBase(scope), params(4, scope->zone()) {} |
| 317 | ZoneList<Parameter> params; |
| 318 | |
| 319 | int Arity() const { return params.length(); } |
| 320 | const Parameter& at(int i) const { return params[i]; } |
| 321 | }; |
| 322 | |
| 323 | |
| 324 | class ParserTraits { |
| 325 | public: |
| 326 | struct Type { |
| 327 | // TODO(marja): To be removed. The Traits object should contain all the data |
| 328 | // it needs. |
| 329 | typedef v8::internal::Parser* Parser; |
| 330 | |
| 331 | typedef Variable GeneratorVariable; |
| 332 | |
| 333 | typedef v8::internal::AstProperties AstProperties; |
| 334 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 335 | typedef v8::internal::ExpressionClassifier<ParserTraits> |
| 336 | ExpressionClassifier; |
| 337 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 338 | // Return types for traversing functions. |
| 339 | typedef const AstRawString* Identifier; |
| 340 | typedef v8::internal::Expression* Expression; |
| 341 | typedef Yield* YieldExpression; |
| 342 | typedef v8::internal::FunctionLiteral* FunctionLiteral; |
| 343 | typedef v8::internal::ClassLiteral* ClassLiteral; |
| 344 | typedef v8::internal::Literal* Literal; |
| 345 | typedef ObjectLiteral::Property* ObjectLiteralProperty; |
| 346 | typedef ZoneList<v8::internal::Expression*>* ExpressionList; |
| 347 | typedef ZoneList<ObjectLiteral::Property*>* PropertyList; |
| 348 | typedef ParserFormalParameters::Parameter FormalParameter; |
| 349 | typedef ParserFormalParameters FormalParameters; |
| 350 | typedef ZoneList<v8::internal::Statement*>* StatementList; |
| 351 | |
| 352 | // For constructing objects returned by the traversing functions. |
| 353 | typedef AstNodeFactory Factory; |
| 354 | }; |
| 355 | |
| 356 | explicit ParserTraits(Parser* parser) : parser_(parser) {} |
| 357 | |
| 358 | // Helper functions for recursive descent. |
| 359 | bool IsEval(const AstRawString* identifier) const; |
| 360 | bool IsArguments(const AstRawString* identifier) const; |
| 361 | bool IsEvalOrArguments(const AstRawString* identifier) const; |
| 362 | bool IsUndefined(const AstRawString* identifier) const; |
| 363 | V8_INLINE bool IsFutureStrictReserved(const AstRawString* identifier) const; |
| 364 | |
| 365 | // Returns true if the expression is of type "this.foo". |
| 366 | static bool IsThisProperty(Expression* expression); |
| 367 | |
| 368 | static bool IsIdentifier(Expression* expression); |
| 369 | |
| 370 | bool IsPrototype(const AstRawString* identifier) const; |
| 371 | |
| 372 | bool IsConstructor(const AstRawString* identifier) const; |
| 373 | |
| 374 | static const AstRawString* AsIdentifier(Expression* expression) { |
| 375 | DCHECK(IsIdentifier(expression)); |
| 376 | return expression->AsVariableProxy()->raw_name(); |
| 377 | } |
| 378 | |
| 379 | static bool IsBoilerplateProperty(ObjectLiteral::Property* property) { |
| 380 | return ObjectLiteral::IsBoilerplateProperty(property); |
| 381 | } |
| 382 | |
| 383 | static bool IsArrayIndex(const AstRawString* string, uint32_t* index) { |
| 384 | return string->AsArrayIndex(index); |
| 385 | } |
| 386 | |
| 387 | static Expression* GetPropertyValue(ObjectLiteral::Property* property) { |
| 388 | return property->value(); |
| 389 | } |
| 390 | |
| 391 | // Functions for encapsulating the differences between parsing and preparsing; |
| 392 | // operations interleaved with the recursive descent. |
| 393 | static void PushLiteralName(FuncNameInferrer* fni, const AstRawString* id) { |
| 394 | fni->PushLiteralName(id); |
| 395 | } |
| 396 | |
| 397 | void PushPropertyName(FuncNameInferrer* fni, Expression* expression); |
| 398 | |
| 399 | static void InferFunctionName(FuncNameInferrer* fni, |
| 400 | FunctionLiteral* func_to_infer) { |
| 401 | fni->AddFunction(func_to_infer); |
| 402 | } |
| 403 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 404 | // If we assign a function literal to a property we pretenure the |
| 405 | // literal so it can be added as a constant function property. |
| 406 | static void CheckAssigningFunctionLiteralToProperty(Expression* left, |
| 407 | Expression* right); |
| 408 | |
| 409 | // Determine if the expression is a variable proxy and mark it as being used |
| 410 | // in an assignment or with a increment/decrement operator. |
| 411 | static Expression* MarkExpressionAsAssigned(Expression* expression); |
| 412 | |
| 413 | // Returns true if we have a binary expression between two numeric |
| 414 | // literals. In that case, *x will be changed to an expression which is the |
| 415 | // computed value. |
| 416 | bool ShortcutNumericLiteralBinaryExpression(Expression** x, Expression* y, |
| 417 | Token::Value op, int pos, |
| 418 | AstNodeFactory* factory); |
| 419 | |
| 420 | // Rewrites the following types of unary expressions: |
| 421 | // not <literal> -> true / false |
| 422 | // + <numeric literal> -> <numeric literal> |
| 423 | // - <numeric literal> -> <numeric literal with value negated> |
| 424 | // ! <literal> -> true / false |
| 425 | // The following rewriting rules enable the collection of type feedback |
| 426 | // without any special stub and the multiplication is removed later in |
| 427 | // Crankshaft's canonicalization pass. |
| 428 | // + foo -> foo * 1 |
| 429 | // - foo -> foo * (-1) |
| 430 | // ~ foo -> foo ^(~0) |
| 431 | Expression* BuildUnaryExpression(Expression* expression, Token::Value op, |
| 432 | int pos, AstNodeFactory* factory); |
| 433 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 434 | Expression* BuildIteratorResult(Expression* value, bool done); |
| 435 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 436 | // Generate AST node that throws a ReferenceError with the given type. |
| 437 | Expression* NewThrowReferenceError(MessageTemplate::Template message, |
| 438 | int pos); |
| 439 | |
| 440 | // Generate AST node that throws a SyntaxError with the given |
| 441 | // type. The first argument may be null (in the handle sense) in |
| 442 | // which case no arguments are passed to the constructor. |
| 443 | Expression* NewThrowSyntaxError(MessageTemplate::Template message, |
| 444 | const AstRawString* arg, int pos); |
| 445 | |
| 446 | // Generate AST node that throws a TypeError with the given |
| 447 | // type. Both arguments must be non-null (in the handle sense). |
| 448 | Expression* NewThrowTypeError(MessageTemplate::Template message, |
| 449 | const AstRawString* arg, int pos); |
| 450 | |
| 451 | // Generic AST generator for throwing errors from compiled code. |
| 452 | Expression* NewThrowError(Runtime::FunctionId function_id, |
| 453 | MessageTemplate::Template message, |
| 454 | const AstRawString* arg, int pos); |
| 455 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 456 | void FinalizeIteratorUse(Variable* completion, Expression* condition, |
| 457 | Variable* iter, Block* iterator_use, Block* result); |
| 458 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 459 | Statement* FinalizeForOfStatement(ForOfStatement* loop, int pos); |
| 460 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 461 | // Reporting errors. |
| 462 | void ReportMessageAt(Scanner::Location source_location, |
| 463 | MessageTemplate::Template message, |
| 464 | const char* arg = NULL, |
| 465 | ParseErrorType error_type = kSyntaxError); |
| 466 | void ReportMessage(MessageTemplate::Template message, const char* arg = NULL, |
| 467 | ParseErrorType error_type = kSyntaxError); |
| 468 | void ReportMessage(MessageTemplate::Template message, const AstRawString* arg, |
| 469 | ParseErrorType error_type = kSyntaxError); |
| 470 | void ReportMessageAt(Scanner::Location source_location, |
| 471 | MessageTemplate::Template message, |
| 472 | const AstRawString* arg, |
| 473 | ParseErrorType error_type = kSyntaxError); |
| 474 | |
| 475 | // "null" return type creators. |
| 476 | static const AstRawString* EmptyIdentifier() { |
| 477 | return NULL; |
| 478 | } |
| 479 | static Expression* EmptyExpression() { |
| 480 | return NULL; |
| 481 | } |
| 482 | static Literal* EmptyLiteral() { |
| 483 | return NULL; |
| 484 | } |
| 485 | static ObjectLiteralProperty* EmptyObjectLiteralProperty() { return NULL; } |
| 486 | static FunctionLiteral* EmptyFunctionLiteral() { return NULL; } |
| 487 | |
| 488 | // Used in error return values. |
| 489 | static ZoneList<Expression*>* NullExpressionList() { |
| 490 | return NULL; |
| 491 | } |
| 492 | static const AstRawString* EmptyFormalParameter() { return NULL; } |
| 493 | |
| 494 | // Non-NULL empty string. |
| 495 | V8_INLINE const AstRawString* EmptyIdentifierString(); |
| 496 | |
| 497 | // Odd-ball literal creators. |
| 498 | Literal* GetLiteralTheHole(int position, AstNodeFactory* factory); |
| 499 | |
| 500 | // Producing data during the recursive descent. |
| 501 | const AstRawString* GetSymbol(Scanner* scanner); |
| 502 | const AstRawString* GetNextSymbol(Scanner* scanner); |
| 503 | const AstRawString* GetNumberAsSymbol(Scanner* scanner); |
| 504 | |
| 505 | Expression* ThisExpression(Scope* scope, AstNodeFactory* factory, |
| 506 | int pos = RelocInfo::kNoPosition); |
| 507 | Expression* SuperPropertyReference(Scope* scope, AstNodeFactory* factory, |
| 508 | int pos); |
| 509 | Expression* SuperCallReference(Scope* scope, AstNodeFactory* factory, |
| 510 | int pos); |
| 511 | Expression* NewTargetExpression(Scope* scope, AstNodeFactory* factory, |
| 512 | int pos); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 513 | Expression* FunctionSentExpression(Scope* scope, AstNodeFactory* factory, |
| 514 | int pos); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 515 | Literal* ExpressionFromLiteral(Token::Value token, int pos, Scanner* scanner, |
| 516 | AstNodeFactory* factory); |
| 517 | Expression* ExpressionFromIdentifier(const AstRawString* name, |
| 518 | int start_position, int end_position, |
| 519 | Scope* scope, AstNodeFactory* factory); |
| 520 | Expression* ExpressionFromString(int pos, Scanner* scanner, |
| 521 | AstNodeFactory* factory); |
| 522 | Expression* GetIterator(Expression* iterable, AstNodeFactory* factory, |
| 523 | int pos); |
| 524 | ZoneList<v8::internal::Expression*>* NewExpressionList(int size, Zone* zone) { |
| 525 | return new(zone) ZoneList<v8::internal::Expression*>(size, zone); |
| 526 | } |
| 527 | ZoneList<ObjectLiteral::Property*>* NewPropertyList(int size, Zone* zone) { |
| 528 | return new(zone) ZoneList<ObjectLiteral::Property*>(size, zone); |
| 529 | } |
| 530 | ZoneList<v8::internal::Statement*>* NewStatementList(int size, Zone* zone) { |
| 531 | return new(zone) ZoneList<v8::internal::Statement*>(size, zone); |
| 532 | } |
| 533 | |
| 534 | V8_INLINE void AddParameterInitializationBlock( |
| 535 | const ParserFormalParameters& parameters, |
| 536 | ZoneList<v8::internal::Statement*>* body, bool* ok); |
| 537 | |
| 538 | V8_INLINE Scope* NewScope(Scope* parent_scope, ScopeType scope_type, |
| 539 | FunctionKind kind = kNormalFunction); |
| 540 | |
| 541 | V8_INLINE void AddFormalParameter(ParserFormalParameters* parameters, |
| 542 | Expression* pattern, |
| 543 | Expression* initializer, |
| 544 | int initializer_end_position, bool is_rest); |
| 545 | V8_INLINE void DeclareFormalParameter( |
| 546 | Scope* scope, const ParserFormalParameters::Parameter& parameter, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 547 | Type::ExpressionClassifier* classifier); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 548 | void ParseArrowFunctionFormalParameters(ParserFormalParameters* parameters, |
| 549 | Expression* params, |
| 550 | const Scanner::Location& params_loc, |
| 551 | bool* ok); |
| 552 | void ParseArrowFunctionFormalParameterList( |
| 553 | ParserFormalParameters* parameters, Expression* params, |
| 554 | const Scanner::Location& params_loc, |
| 555 | Scanner::Location* duplicate_loc, bool* ok); |
| 556 | |
| 557 | V8_INLINE DoExpression* ParseDoExpression(bool* ok); |
| 558 | |
| 559 | void ReindexLiterals(const ParserFormalParameters& parameters); |
| 560 | |
| 561 | // Temporary glue; these functions will move to ParserBase. |
| 562 | Expression* ParseV8Intrinsic(bool* ok); |
| 563 | FunctionLiteral* ParseFunctionLiteral( |
| 564 | const AstRawString* name, Scanner::Location function_name_location, |
| 565 | FunctionNameValidity function_name_validity, FunctionKind kind, |
| 566 | int function_token_position, FunctionLiteral::FunctionType type, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 567 | LanguageMode language_mode, bool* ok); |
| 568 | V8_INLINE void SkipLazyFunctionBody( |
| 569 | int* materialized_literal_count, int* expected_property_count, bool* ok, |
| 570 | Scanner::BookmarkScope* bookmark = nullptr); |
| 571 | V8_INLINE ZoneList<Statement*>* ParseEagerFunctionBody( |
| 572 | const AstRawString* name, int pos, |
| 573 | const ParserFormalParameters& parameters, FunctionKind kind, |
| 574 | FunctionLiteral::FunctionType function_type, bool* ok); |
| 575 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 576 | ClassLiteral* ParseClassLiteral(Type::ExpressionClassifier* classifier, |
| 577 | const AstRawString* name, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 578 | Scanner::Location class_name_location, |
| 579 | bool name_is_strict_reserved, int pos, |
| 580 | bool* ok); |
| 581 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 582 | V8_INLINE void MarkTailPosition(Expression* expression); |
| 583 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 584 | V8_INLINE void CheckConflictingVarDeclarations(v8::internal::Scope* scope, |
| 585 | bool* ok); |
| 586 | |
| 587 | class TemplateLiteral : public ZoneObject { |
| 588 | public: |
| 589 | TemplateLiteral(Zone* zone, int pos) |
| 590 | : cooked_(8, zone), raw_(8, zone), expressions_(8, zone), pos_(pos) {} |
| 591 | |
| 592 | const ZoneList<Expression*>* cooked() const { return &cooked_; } |
| 593 | const ZoneList<Expression*>* raw() const { return &raw_; } |
| 594 | const ZoneList<Expression*>* expressions() const { return &expressions_; } |
| 595 | int position() const { return pos_; } |
| 596 | |
| 597 | void AddTemplateSpan(Literal* cooked, Literal* raw, int end, Zone* zone) { |
| 598 | DCHECK_NOT_NULL(cooked); |
| 599 | DCHECK_NOT_NULL(raw); |
| 600 | cooked_.Add(cooked, zone); |
| 601 | raw_.Add(raw, zone); |
| 602 | } |
| 603 | |
| 604 | void AddExpression(Expression* expression, Zone* zone) { |
| 605 | DCHECK_NOT_NULL(expression); |
| 606 | expressions_.Add(expression, zone); |
| 607 | } |
| 608 | |
| 609 | private: |
| 610 | ZoneList<Expression*> cooked_; |
| 611 | ZoneList<Expression*> raw_; |
| 612 | ZoneList<Expression*> expressions_; |
| 613 | int pos_; |
| 614 | }; |
| 615 | |
| 616 | typedef TemplateLiteral* TemplateLiteralState; |
| 617 | |
| 618 | V8_INLINE TemplateLiteralState OpenTemplateLiteral(int pos); |
| 619 | V8_INLINE void AddTemplateSpan(TemplateLiteralState* state, bool tail); |
| 620 | V8_INLINE void AddTemplateExpression(TemplateLiteralState* state, |
| 621 | Expression* expression); |
| 622 | V8_INLINE Expression* CloseTemplateLiteral(TemplateLiteralState* state, |
| 623 | int start, Expression* tag); |
| 624 | V8_INLINE Expression* NoTemplateTag() { return NULL; } |
| 625 | V8_INLINE static bool IsTaggedTemplate(const Expression* tag) { |
| 626 | return tag != NULL; |
| 627 | } |
| 628 | |
| 629 | V8_INLINE ZoneList<v8::internal::Expression*>* PrepareSpreadArguments( |
| 630 | ZoneList<v8::internal::Expression*>* list); |
| 631 | V8_INLINE void MaterializeUnspreadArgumentsLiterals(int count) {} |
| 632 | V8_INLINE Expression* SpreadCall(Expression* function, |
| 633 | ZoneList<v8::internal::Expression*>* args, |
| 634 | int pos); |
| 635 | V8_INLINE Expression* SpreadCallNew(Expression* function, |
| 636 | ZoneList<v8::internal::Expression*>* args, |
| 637 | int pos); |
| 638 | |
| 639 | // Rewrite all DestructuringAssignments in the current FunctionState. |
| 640 | V8_INLINE void RewriteDestructuringAssignments(); |
| 641 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 642 | V8_INLINE Expression* RewriteExponentiation(Expression* left, |
| 643 | Expression* right, int pos); |
| 644 | V8_INLINE Expression* RewriteAssignExponentiation(Expression* left, |
| 645 | Expression* right, int pos); |
| 646 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 647 | V8_INLINE void QueueDestructuringAssignmentForRewriting( |
| 648 | Expression* assignment); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 649 | V8_INLINE void QueueNonPatternForRewriting(Expression* expr); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 650 | |
| 651 | void SetFunctionNameFromPropertyName(ObjectLiteralProperty* property, |
| 652 | const AstRawString* name); |
| 653 | |
| 654 | void SetFunctionNameFromIdentifierRef(Expression* value, |
| 655 | Expression* identifier); |
| 656 | |
| 657 | // Rewrite expressions that are not used as patterns |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 658 | V8_INLINE void RewriteNonPattern(Type::ExpressionClassifier* classifier, |
| 659 | bool* ok); |
| 660 | |
| 661 | V8_INLINE Zone* zone() const; |
| 662 | |
| 663 | V8_INLINE ZoneList<Expression*>* GetNonPatternList() const; |
| 664 | |
| 665 | Expression* RewriteYieldStar( |
| 666 | Expression* generator, Expression* expression, int pos); |
| 667 | |
| 668 | Expression* RewriteInstanceof(Expression* lhs, Expression* rhs, int pos); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 669 | |
| 670 | private: |
| 671 | Parser* parser_; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 672 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 673 | void BuildIteratorClose(ZoneList<Statement*>* statements, Variable* iterator, |
| 674 | Maybe<Variable*> input, Variable* output); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 675 | void BuildIteratorCloseForCompletion( |
| 676 | ZoneList<Statement*>* statements, Variable* iterator, |
| 677 | Variable* body_threw); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 678 | Statement* CheckCallable(Variable* var, Expression* error, int pos); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 679 | }; |
| 680 | |
| 681 | |
| 682 | class Parser : public ParserBase<ParserTraits> { |
| 683 | public: |
| 684 | explicit Parser(ParseInfo* info); |
| 685 | ~Parser() { |
| 686 | delete reusable_preparser_; |
| 687 | reusable_preparser_ = NULL; |
| 688 | delete cached_parse_data_; |
| 689 | cached_parse_data_ = NULL; |
| 690 | } |
| 691 | |
| 692 | // Parses the source code represented by the compilation info and sets its |
| 693 | // function literal. Returns false (and deallocates any allocated AST |
| 694 | // nodes) if parsing failed. |
| 695 | static bool ParseStatic(ParseInfo* info); |
| 696 | bool Parse(ParseInfo* info); |
| 697 | void ParseOnBackground(ParseInfo* info); |
| 698 | |
| 699 | // Handle errors detected during parsing, move statistics to Isolate, |
| 700 | // internalize strings (move them to the heap). |
| 701 | void Internalize(Isolate* isolate, Handle<Script> script, bool error); |
| 702 | void HandleSourceURLComments(Isolate* isolate, Handle<Script> script); |
| 703 | |
| 704 | private: |
| 705 | friend class ParserTraits; |
| 706 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 707 | // Runtime encoding of different completion modes. |
| 708 | enum CompletionKind { |
| 709 | kNormalCompletion, |
| 710 | kThrowCompletion, |
| 711 | kAbruptCompletion |
| 712 | }; |
| 713 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 714 | // Limit the allowed number of local variables in a function. The hard limit |
| 715 | // is that offsets computed by FullCodeGenerator::StackOperand and similar |
| 716 | // functions are ints, and they should not overflow. In addition, accessing |
| 717 | // local variables creates user-controlled constants in the generated code, |
| 718 | // and we don't want too much user-controlled memory inside the code (this was |
| 719 | // the reason why this limit was introduced in the first place; see |
| 720 | // https://codereview.chromium.org/7003030/ ). |
| 721 | static const int kMaxNumFunctionLocals = 4194303; // 2^22-1 |
| 722 | |
| 723 | // Returns NULL if parsing failed. |
| 724 | FunctionLiteral* ParseProgram(Isolate* isolate, ParseInfo* info); |
| 725 | |
| 726 | FunctionLiteral* ParseLazy(Isolate* isolate, ParseInfo* info); |
| 727 | FunctionLiteral* ParseLazy(Isolate* isolate, ParseInfo* info, |
| 728 | Utf16CharacterStream* source); |
| 729 | |
| 730 | // Called by ParseProgram after setting up the scanner. |
| 731 | FunctionLiteral* DoParseProgram(ParseInfo* info); |
| 732 | |
| 733 | void SetCachedData(ParseInfo* info); |
| 734 | |
| 735 | ScriptCompiler::CompileOptions compile_options() const { |
| 736 | return compile_options_; |
| 737 | } |
| 738 | bool consume_cached_parse_data() const { |
| 739 | return compile_options_ == ScriptCompiler::kConsumeParserCache && |
| 740 | cached_parse_data_ != NULL; |
| 741 | } |
| 742 | bool produce_cached_parse_data() const { |
| 743 | return compile_options_ == ScriptCompiler::kProduceParserCache; |
| 744 | } |
| 745 | |
| 746 | // All ParseXXX functions take as the last argument an *ok parameter |
| 747 | // which is set to false if parsing failed; it is unchanged otherwise. |
| 748 | // By making the 'exception handling' explicit, we are forced to check |
| 749 | // for failure at the call sites. |
| 750 | void* ParseStatementList(ZoneList<Statement*>* body, int end_token, bool* ok); |
| 751 | Statement* ParseStatementListItem(bool* ok); |
| 752 | void* ParseModuleItemList(ZoneList<Statement*>* body, bool* ok); |
| 753 | Statement* ParseModuleItem(bool* ok); |
| 754 | const AstRawString* ParseModuleSpecifier(bool* ok); |
| 755 | Statement* ParseImportDeclaration(bool* ok); |
| 756 | Statement* ParseExportDeclaration(bool* ok); |
| 757 | Statement* ParseExportDefault(bool* ok); |
| 758 | void* ParseExportClause(ZoneList<const AstRawString*>* export_names, |
| 759 | ZoneList<Scanner::Location>* export_locations, |
| 760 | ZoneList<const AstRawString*>* local_names, |
| 761 | Scanner::Location* reserved_loc, bool* ok); |
| 762 | ZoneList<ImportDeclaration*>* ParseNamedImports(int pos, bool* ok); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 763 | Statement* ParseStatement(ZoneList<const AstRawString*>* labels, |
| 764 | AllowLabelledFunctionStatement allow_function, |
| 765 | bool* ok); |
| 766 | Statement* ParseSubStatement(ZoneList<const AstRawString*>* labels, |
| 767 | AllowLabelledFunctionStatement allow_function, |
| 768 | bool* ok); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 769 | Statement* ParseStatementAsUnlabelled(ZoneList<const AstRawString*>* labels, |
| 770 | bool* ok); |
| 771 | Statement* ParseFunctionDeclaration(ZoneList<const AstRawString*>* names, |
| 772 | bool* ok); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 773 | Statement* ParseFunctionDeclaration(int pos, bool is_generator, |
| 774 | ZoneList<const AstRawString*>* names, |
| 775 | bool* ok); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 776 | Statement* ParseClassDeclaration(ZoneList<const AstRawString*>* names, |
| 777 | bool* ok); |
| 778 | Statement* ParseNativeDeclaration(bool* ok); |
| 779 | Block* ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok); |
| 780 | Block* ParseBlock(ZoneList<const AstRawString*>* labels, |
| 781 | bool finalize_block_scope, bool* ok); |
| 782 | Block* ParseVariableStatement(VariableDeclarationContext var_context, |
| 783 | ZoneList<const AstRawString*>* names, |
| 784 | bool* ok); |
| 785 | DoExpression* ParseDoExpression(bool* ok); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 786 | Expression* ParseYieldStarExpression(bool* ok); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 787 | |
| 788 | struct DeclarationDescriptor { |
| 789 | enum Kind { NORMAL, PARAMETER }; |
| 790 | Parser* parser; |
| 791 | Scope* scope; |
| 792 | Scope* hoist_scope; |
| 793 | VariableMode mode; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 794 | int declaration_pos; |
| 795 | int initialization_pos; |
| 796 | Kind declaration_kind; |
| 797 | }; |
| 798 | |
| 799 | struct DeclarationParsingResult { |
| 800 | struct Declaration { |
| 801 | Declaration(Expression* pattern, int initializer_position, |
| 802 | Expression* initializer) |
| 803 | : pattern(pattern), |
| 804 | initializer_position(initializer_position), |
| 805 | initializer(initializer) {} |
| 806 | |
| 807 | Expression* pattern; |
| 808 | int initializer_position; |
| 809 | Expression* initializer; |
| 810 | }; |
| 811 | |
| 812 | DeclarationParsingResult() |
| 813 | : declarations(4), |
| 814 | first_initializer_loc(Scanner::Location::invalid()), |
| 815 | bindings_loc(Scanner::Location::invalid()) {} |
| 816 | |
| 817 | Block* BuildInitializationBlock(ZoneList<const AstRawString*>* names, |
| 818 | bool* ok); |
| 819 | |
| 820 | DeclarationDescriptor descriptor; |
| 821 | List<Declaration> declarations; |
| 822 | Scanner::Location first_initializer_loc; |
| 823 | Scanner::Location bindings_loc; |
| 824 | }; |
| 825 | |
| 826 | class PatternRewriter : private AstVisitor { |
| 827 | public: |
| 828 | static void DeclareAndInitializeVariables( |
| 829 | Block* block, const DeclarationDescriptor* declaration_descriptor, |
| 830 | const DeclarationParsingResult::Declaration* declaration, |
| 831 | ZoneList<const AstRawString*>* names, bool* ok); |
| 832 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 833 | static void RewriteDestructuringAssignment(Parser* parser, |
| 834 | RewritableExpression* expr, |
| 835 | Scope* Scope); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 836 | |
| 837 | static Expression* RewriteDestructuringAssignment(Parser* parser, |
| 838 | Assignment* assignment, |
| 839 | Scope* scope); |
| 840 | |
| 841 | void set_initializer_position(int pos) { initializer_position_ = pos; } |
| 842 | |
| 843 | private: |
| 844 | PatternRewriter() {} |
| 845 | |
| 846 | #define DECLARE_VISIT(type) void Visit##type(v8::internal::type* node) override; |
| 847 | // Visiting functions for AST nodes make this an AstVisitor. |
| 848 | AST_NODE_LIST(DECLARE_VISIT) |
| 849 | #undef DECLARE_VISIT |
| 850 | void Visit(AstNode* node) override; |
| 851 | |
| 852 | enum PatternContext { |
| 853 | BINDING, |
| 854 | INITIALIZER, |
| 855 | ASSIGNMENT, |
| 856 | ASSIGNMENT_INITIALIZER |
| 857 | }; |
| 858 | |
| 859 | PatternContext context() const { return context_; } |
| 860 | void set_context(PatternContext context) { context_ = context; } |
| 861 | |
| 862 | void RecurseIntoSubpattern(AstNode* pattern, Expression* value) { |
| 863 | Expression* old_value = current_value_; |
| 864 | current_value_ = value; |
| 865 | recursion_level_++; |
| 866 | pattern->Accept(this); |
| 867 | recursion_level_--; |
| 868 | current_value_ = old_value; |
| 869 | } |
| 870 | |
| 871 | void VisitObjectLiteral(ObjectLiteral* node, Variable** temp_var); |
| 872 | void VisitArrayLiteral(ArrayLiteral* node, Variable** temp_var); |
| 873 | |
| 874 | bool IsBindingContext() const { return IsBindingContext(context_); } |
| 875 | bool IsInitializerContext() const { return context_ != ASSIGNMENT; } |
| 876 | bool IsAssignmentContext() const { return IsAssignmentContext(context_); } |
| 877 | bool IsAssignmentContext(PatternContext c) const; |
| 878 | bool IsBindingContext(PatternContext c) const; |
| 879 | bool IsSubPattern() const { return recursion_level_ > 1; } |
| 880 | PatternContext SetAssignmentContextIfNeeded(Expression* node); |
| 881 | PatternContext SetInitializerContextIfNeeded(Expression* node); |
| 882 | |
| 883 | Variable* CreateTempVar(Expression* value = nullptr); |
| 884 | |
| 885 | AstNodeFactory* factory() const { return parser_->factory(); } |
| 886 | AstValueFactory* ast_value_factory() const { |
| 887 | return parser_->ast_value_factory(); |
| 888 | } |
| 889 | Zone* zone() const { return parser_->zone(); } |
| 890 | Scope* scope() const { return scope_; } |
| 891 | |
| 892 | Scope* scope_; |
| 893 | Parser* parser_; |
| 894 | PatternContext context_; |
| 895 | Expression* pattern_; |
| 896 | int initializer_position_; |
| 897 | Block* block_; |
| 898 | const DeclarationDescriptor* descriptor_; |
| 899 | ZoneList<const AstRawString*>* names_; |
| 900 | Expression* current_value_; |
| 901 | int recursion_level_; |
| 902 | bool* ok_; |
| 903 | }; |
| 904 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 905 | Block* ParseVariableDeclarations(VariableDeclarationContext var_context, |
| 906 | DeclarationParsingResult* parsing_result, |
| 907 | ZoneList<const AstRawString*>* names, |
| 908 | bool* ok); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 909 | Statement* ParseExpressionOrLabelledStatement( |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 910 | ZoneList<const AstRawString*>* labels, |
| 911 | AllowLabelledFunctionStatement allow_function, bool* ok); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 912 | IfStatement* ParseIfStatement(ZoneList<const AstRawString*>* labels, |
| 913 | bool* ok); |
| 914 | Statement* ParseContinueStatement(bool* ok); |
| 915 | Statement* ParseBreakStatement(ZoneList<const AstRawString*>* labels, |
| 916 | bool* ok); |
| 917 | Statement* ParseReturnStatement(bool* ok); |
| 918 | Statement* ParseWithStatement(ZoneList<const AstRawString*>* labels, |
| 919 | bool* ok); |
| 920 | CaseClause* ParseCaseClause(bool* default_seen_ptr, bool* ok); |
| 921 | Statement* ParseSwitchStatement(ZoneList<const AstRawString*>* labels, |
| 922 | bool* ok); |
| 923 | DoWhileStatement* ParseDoWhileStatement(ZoneList<const AstRawString*>* labels, |
| 924 | bool* ok); |
| 925 | WhileStatement* ParseWhileStatement(ZoneList<const AstRawString*>* labels, |
| 926 | bool* ok); |
| 927 | Statement* ParseForStatement(ZoneList<const AstRawString*>* labels, bool* ok); |
| 928 | Statement* ParseThrowStatement(bool* ok); |
| 929 | Expression* MakeCatchContext(Handle<String> id, VariableProxy* value); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 930 | class DontCollectExpressionsInTailPositionScope; |
| 931 | class CollectExpressionsInTailPositionToListScope; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 932 | TryStatement* ParseTryStatement(bool* ok); |
| 933 | DebuggerStatement* ParseDebuggerStatement(bool* ok); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 934 | // Parse a SubStatement in strict mode, or with an extra block scope in |
| 935 | // sloppy mode to handle |
| 936 | // ES#sec-functiondeclarations-in-ifstatement-statement-clauses |
| 937 | // The legacy parameter indicates whether function declarations are |
| 938 | // banned by the ES2015 specification in this location, and they are being |
| 939 | // permitted here to match previous V8 behavior. |
| 940 | Statement* ParseScopedStatement(ZoneList<const AstRawString*>* labels, |
| 941 | bool legacy, bool* ok); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 942 | |
| 943 | // !%_IsJSReceiver(result = iterator.next()) && |
| 944 | // %ThrowIteratorResultNotAnObject(result) |
| 945 | Expression* BuildIteratorNextResult(Expression* iterator, Variable* result, |
| 946 | int pos); |
| 947 | |
| 948 | |
| 949 | // Initialize the components of a for-in / for-of statement. |
| 950 | void InitializeForEachStatement(ForEachStatement* stmt, Expression* each, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 951 | Expression* subject, Statement* body); |
| 952 | void InitializeForOfStatement(ForOfStatement* stmt, Expression* each, |
| 953 | Expression* iterable, Statement* body, |
| 954 | int iterable_pos); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 955 | Statement* DesugarLexicalBindingsInForStatement( |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 956 | Scope* inner_scope, VariableMode mode, |
| 957 | ZoneList<const AstRawString*>* names, ForStatement* loop, Statement* init, |
| 958 | Expression* cond, Statement* next, Statement* body, bool* ok); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 959 | |
| 960 | void RewriteDoExpression(Expression* expr, bool* ok); |
| 961 | |
| 962 | FunctionLiteral* ParseFunctionLiteral( |
| 963 | const AstRawString* name, Scanner::Location function_name_location, |
| 964 | FunctionNameValidity function_name_validity, FunctionKind kind, |
| 965 | int function_token_position, FunctionLiteral::FunctionType type, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 966 | LanguageMode language_mode, bool* ok); |
| 967 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 968 | ClassLiteral* ParseClassLiteral(ExpressionClassifier* classifier, |
| 969 | const AstRawString* name, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 970 | Scanner::Location class_name_location, |
| 971 | bool name_is_strict_reserved, int pos, |
| 972 | bool* ok); |
| 973 | |
| 974 | // Magical syntax support. |
| 975 | Expression* ParseV8Intrinsic(bool* ok); |
| 976 | |
| 977 | // Get odd-ball literals. |
| 978 | Literal* GetLiteralUndefined(int position); |
| 979 | |
| 980 | // Check if the scope has conflicting var/let declarations from different |
| 981 | // scopes. This covers for example |
| 982 | // |
| 983 | // function f() { { { var x; } let x; } } |
| 984 | // function g() { { var x; let x; } } |
| 985 | // |
| 986 | // The var declarations are hoisted to the function scope, but originate from |
| 987 | // a scope where the name has also been let bound or the var declaration is |
| 988 | // hoisted over such a scope. |
| 989 | void CheckConflictingVarDeclarations(Scope* scope, bool* ok); |
| 990 | |
| 991 | // Insert initializer statements for var-bindings shadowing parameter bindings |
| 992 | // from a non-simple parameter list. |
| 993 | void InsertShadowingVarBindingInitializers(Block* block); |
| 994 | |
| 995 | // Implement sloppy block-scoped functions, ES2015 Annex B 3.3 |
| 996 | void InsertSloppyBlockFunctionVarBindings(Scope* scope, bool* ok); |
| 997 | |
| 998 | // Parser support |
| 999 | VariableProxy* NewUnresolved(const AstRawString* name, VariableMode mode); |
| 1000 | Variable* Declare(Declaration* declaration, |
| 1001 | DeclarationDescriptor::Kind declaration_kind, bool resolve, |
| 1002 | bool* ok, Scope* declaration_scope = nullptr); |
| 1003 | |
| 1004 | bool TargetStackContainsLabel(const AstRawString* label); |
| 1005 | BreakableStatement* LookupBreakTarget(const AstRawString* label, bool* ok); |
| 1006 | IterationStatement* LookupContinueTarget(const AstRawString* label, bool* ok); |
| 1007 | |
| 1008 | Statement* BuildAssertIsCoercible(Variable* var); |
| 1009 | |
| 1010 | // Factory methods. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1011 | FunctionLiteral* DefaultConstructor(const AstRawString* name, bool call_super, |
| 1012 | Scope* scope, int pos, int end_pos, |
| 1013 | LanguageMode language_mode); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1014 | |
| 1015 | // Skip over a lazy function, either using cached data if we have it, or |
| 1016 | // by parsing the function with PreParser. Consumes the ending }. |
| 1017 | // |
| 1018 | // If bookmark is set, the (pre-)parser may decide to abort skipping |
| 1019 | // in order to force the function to be eagerly parsed, after all. |
| 1020 | // In this case, it'll reset the scanner using the bookmark. |
| 1021 | void SkipLazyFunctionBody(int* materialized_literal_count, |
| 1022 | int* expected_property_count, bool* ok, |
| 1023 | Scanner::BookmarkScope* bookmark = nullptr); |
| 1024 | |
| 1025 | PreParser::PreParseResult ParseLazyFunctionBodyWithPreParser( |
| 1026 | SingletonLogger* logger, Scanner::BookmarkScope* bookmark = nullptr); |
| 1027 | |
| 1028 | Block* BuildParameterInitializationBlock( |
| 1029 | const ParserFormalParameters& parameters, bool* ok); |
| 1030 | |
| 1031 | // Consumes the ending }. |
| 1032 | ZoneList<Statement*>* ParseEagerFunctionBody( |
| 1033 | const AstRawString* function_name, int pos, |
| 1034 | const ParserFormalParameters& parameters, FunctionKind kind, |
| 1035 | FunctionLiteral::FunctionType function_type, bool* ok); |
| 1036 | |
| 1037 | void ThrowPendingError(Isolate* isolate, Handle<Script> script); |
| 1038 | |
| 1039 | TemplateLiteralState OpenTemplateLiteral(int pos); |
| 1040 | void AddTemplateSpan(TemplateLiteralState* state, bool tail); |
| 1041 | void AddTemplateExpression(TemplateLiteralState* state, |
| 1042 | Expression* expression); |
| 1043 | Expression* CloseTemplateLiteral(TemplateLiteralState* state, int start, |
| 1044 | Expression* tag); |
| 1045 | uint32_t ComputeTemplateLiteralHash(const TemplateLiteral* lit); |
| 1046 | |
| 1047 | ZoneList<v8::internal::Expression*>* PrepareSpreadArguments( |
| 1048 | ZoneList<v8::internal::Expression*>* list); |
| 1049 | Expression* SpreadCall(Expression* function, |
| 1050 | ZoneList<v8::internal::Expression*>* args, int pos); |
| 1051 | Expression* SpreadCallNew(Expression* function, |
| 1052 | ZoneList<v8::internal::Expression*>* args, int pos); |
| 1053 | |
| 1054 | void SetLanguageMode(Scope* scope, LanguageMode mode); |
| 1055 | void RaiseLanguageMode(LanguageMode mode); |
| 1056 | |
| 1057 | V8_INLINE void RewriteDestructuringAssignments(); |
| 1058 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 1059 | V8_INLINE Expression* RewriteExponentiation(Expression* left, |
| 1060 | Expression* right, int pos); |
| 1061 | V8_INLINE Expression* RewriteAssignExponentiation(Expression* left, |
| 1062 | Expression* right, int pos); |
| 1063 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1064 | friend class NonPatternRewriter; |
| 1065 | V8_INLINE Expression* RewriteSpreads(ArrayLiteral* lit); |
| 1066 | |
| 1067 | V8_INLINE void RewriteNonPattern(ExpressionClassifier* classifier, bool* ok); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1068 | |
| 1069 | friend class InitializerRewriter; |
| 1070 | void RewriteParameterInitializer(Expression* expr, Scope* scope); |
| 1071 | |
| 1072 | Scanner scanner_; |
| 1073 | PreParser* reusable_preparser_; |
| 1074 | Scope* original_scope_; // for ES5 function declarations in sloppy eval |
| 1075 | Target* target_stack_; // for break, continue statements |
| 1076 | ScriptCompiler::CompileOptions compile_options_; |
| 1077 | ParseData* cached_parse_data_; |
| 1078 | |
| 1079 | PendingCompilationErrorHandler pending_error_handler_; |
| 1080 | |
| 1081 | // Other information which will be stored in Parser and moved to Isolate after |
| 1082 | // parsing. |
| 1083 | int use_counts_[v8::Isolate::kUseCounterFeatureCount]; |
| 1084 | int total_preparse_skipped_; |
| 1085 | HistogramTimer* pre_parse_timer_; |
| 1086 | |
| 1087 | bool parsing_on_main_thread_; |
| 1088 | }; |
| 1089 | |
| 1090 | |
| 1091 | bool ParserTraits::IsFutureStrictReserved( |
| 1092 | const AstRawString* identifier) const { |
| 1093 | return parser_->scanner()->IdentifierIsFutureStrictReserved(identifier); |
| 1094 | } |
| 1095 | |
| 1096 | |
| 1097 | Scope* ParserTraits::NewScope(Scope* parent_scope, ScopeType scope_type, |
| 1098 | FunctionKind kind) { |
| 1099 | return parser_->NewScope(parent_scope, scope_type, kind); |
| 1100 | } |
| 1101 | |
| 1102 | |
| 1103 | const AstRawString* ParserTraits::EmptyIdentifierString() { |
| 1104 | return parser_->ast_value_factory()->empty_string(); |
| 1105 | } |
| 1106 | |
| 1107 | |
| 1108 | void ParserTraits::SkipLazyFunctionBody(int* materialized_literal_count, |
| 1109 | int* expected_property_count, bool* ok, |
| 1110 | Scanner::BookmarkScope* bookmark) { |
| 1111 | return parser_->SkipLazyFunctionBody(materialized_literal_count, |
| 1112 | expected_property_count, ok, bookmark); |
| 1113 | } |
| 1114 | |
| 1115 | |
| 1116 | ZoneList<Statement*>* ParserTraits::ParseEagerFunctionBody( |
| 1117 | const AstRawString* name, int pos, const ParserFormalParameters& parameters, |
| 1118 | FunctionKind kind, FunctionLiteral::FunctionType function_type, bool* ok) { |
| 1119 | return parser_->ParseEagerFunctionBody(name, pos, parameters, kind, |
| 1120 | function_type, ok); |
| 1121 | } |
| 1122 | |
| 1123 | |
| 1124 | void ParserTraits::CheckConflictingVarDeclarations(v8::internal::Scope* scope, |
| 1125 | bool* ok) { |
| 1126 | parser_->CheckConflictingVarDeclarations(scope, ok); |
| 1127 | } |
| 1128 | |
| 1129 | |
| 1130 | // Support for handling complex values (array and object literals) that |
| 1131 | // can be fully handled at compile time. |
| 1132 | class CompileTimeValue: public AllStatic { |
| 1133 | public: |
| 1134 | enum LiteralType { |
| 1135 | OBJECT_LITERAL_FAST_ELEMENTS, |
| 1136 | OBJECT_LITERAL_SLOW_ELEMENTS, |
| 1137 | ARRAY_LITERAL |
| 1138 | }; |
| 1139 | |
| 1140 | static bool IsCompileTimeValue(Expression* expression); |
| 1141 | |
| 1142 | // Get the value as a compile time value. |
| 1143 | static Handle<FixedArray> GetValue(Isolate* isolate, Expression* expression); |
| 1144 | |
| 1145 | // Get the type of a compile time value returned by GetValue(). |
| 1146 | static LiteralType GetLiteralType(Handle<FixedArray> value); |
| 1147 | |
| 1148 | // Get the elements array of a compile time value returned by GetValue(). |
| 1149 | static Handle<FixedArray> GetElements(Handle<FixedArray> value); |
| 1150 | |
| 1151 | private: |
| 1152 | static const int kLiteralTypeSlot = 0; |
| 1153 | static const int kElementsSlot = 1; |
| 1154 | |
| 1155 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); |
| 1156 | }; |
| 1157 | |
| 1158 | |
| 1159 | ParserTraits::TemplateLiteralState ParserTraits::OpenTemplateLiteral(int pos) { |
| 1160 | return parser_->OpenTemplateLiteral(pos); |
| 1161 | } |
| 1162 | |
| 1163 | |
| 1164 | void ParserTraits::AddTemplateSpan(TemplateLiteralState* state, bool tail) { |
| 1165 | parser_->AddTemplateSpan(state, tail); |
| 1166 | } |
| 1167 | |
| 1168 | |
| 1169 | void ParserTraits::AddTemplateExpression(TemplateLiteralState* state, |
| 1170 | Expression* expression) { |
| 1171 | parser_->AddTemplateExpression(state, expression); |
| 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | Expression* ParserTraits::CloseTemplateLiteral(TemplateLiteralState* state, |
| 1176 | int start, Expression* tag) { |
| 1177 | return parser_->CloseTemplateLiteral(state, start, tag); |
| 1178 | } |
| 1179 | |
| 1180 | |
| 1181 | ZoneList<v8::internal::Expression*>* ParserTraits::PrepareSpreadArguments( |
| 1182 | ZoneList<v8::internal::Expression*>* list) { |
| 1183 | return parser_->PrepareSpreadArguments(list); |
| 1184 | } |
| 1185 | |
| 1186 | |
| 1187 | Expression* ParserTraits::SpreadCall(Expression* function, |
| 1188 | ZoneList<v8::internal::Expression*>* args, |
| 1189 | int pos) { |
| 1190 | return parser_->SpreadCall(function, args, pos); |
| 1191 | } |
| 1192 | |
| 1193 | |
| 1194 | Expression* ParserTraits::SpreadCallNew( |
| 1195 | Expression* function, ZoneList<v8::internal::Expression*>* args, int pos) { |
| 1196 | return parser_->SpreadCallNew(function, args, pos); |
| 1197 | } |
| 1198 | |
| 1199 | |
| 1200 | void ParserTraits::AddFormalParameter(ParserFormalParameters* parameters, |
| 1201 | Expression* pattern, |
| 1202 | Expression* initializer, |
| 1203 | int initializer_end_position, |
| 1204 | bool is_rest) { |
| 1205 | bool is_simple = pattern->IsVariableProxy() && initializer == nullptr; |
| 1206 | const AstRawString* name = is_simple |
| 1207 | ? pattern->AsVariableProxy()->raw_name() |
| 1208 | : parser_->ast_value_factory()->empty_string(); |
| 1209 | parameters->params.Add( |
| 1210 | ParserFormalParameters::Parameter(name, pattern, initializer, |
| 1211 | initializer_end_position, is_rest), |
| 1212 | parameters->scope->zone()); |
| 1213 | } |
| 1214 | |
| 1215 | |
| 1216 | void ParserTraits::DeclareFormalParameter( |
| 1217 | Scope* scope, const ParserFormalParameters::Parameter& parameter, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1218 | Type::ExpressionClassifier* classifier) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1219 | bool is_duplicate = false; |
| 1220 | bool is_simple = classifier->is_simple_parameter_list(); |
| 1221 | auto name = is_simple || parameter.is_rest |
| 1222 | ? parameter.name |
| 1223 | : parser_->ast_value_factory()->empty_string(); |
| 1224 | auto mode = is_simple || parameter.is_rest ? VAR : TEMPORARY; |
| 1225 | if (!is_simple) scope->SetHasNonSimpleParameters(); |
| 1226 | bool is_optional = parameter.initializer != nullptr; |
| 1227 | Variable* var = scope->DeclareParameter( |
| 1228 | name, mode, is_optional, parameter.is_rest, &is_duplicate); |
| 1229 | if (is_duplicate) { |
| 1230 | classifier->RecordDuplicateFormalParameterError( |
| 1231 | parser_->scanner()->location()); |
| 1232 | } |
| 1233 | if (is_sloppy(scope->language_mode())) { |
| 1234 | // TODO(sigurds) Mark every parameter as maybe assigned. This is a |
| 1235 | // conservative approximation necessary to account for parameters |
| 1236 | // that are assigned via the arguments array. |
| 1237 | var->set_maybe_assigned(); |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | |
| 1242 | void ParserTraits::AddParameterInitializationBlock( |
| 1243 | const ParserFormalParameters& parameters, |
| 1244 | ZoneList<v8::internal::Statement*>* body, bool* ok) { |
| 1245 | if (!parameters.is_simple) { |
| 1246 | auto* init_block = |
| 1247 | parser_->BuildParameterInitializationBlock(parameters, ok); |
| 1248 | if (!*ok) return; |
| 1249 | if (init_block != nullptr) { |
| 1250 | body->Add(init_block, parser_->zone()); |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | |
| 1256 | DoExpression* ParserTraits::ParseDoExpression(bool* ok) { |
| 1257 | return parser_->ParseDoExpression(ok); |
| 1258 | } |
| 1259 | |
| 1260 | |
| 1261 | } // namespace internal |
| 1262 | } // namespace v8 |
| 1263 | |
| 1264 | #endif // V8_PARSING_PARSER_H_ |