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 #include "src/parsing/parser.h"
6
7 #include <algorithm>
8 #include <memory>
9
10 #include "src/ast/ast-function-literal-id-reindexer.h"
11 #include "src/ast/ast-traversal-visitor.h"
12 #include "src/ast/ast.h"
13 #include "src/ast/source-range-ast-visitor.h"
14 #include "src/base/ieee754.h"
15 #include "src/base/overflowing-math.h"
16 #include "src/base/platform/platform.h"
17 #include "src/codegen/bailout-reason.h"
18 #include "src/common/globals.h"
19 #include "src/common/message-template.h"
20 #include "src/compiler-dispatcher/lazy-compile-dispatcher.h"
21 #include "src/heap/parked-scope.h"
22 #include "src/logging/counters.h"
23 #include "src/logging/log.h"
24 #include "src/logging/runtime-call-stats-scope.h"
25 #include "src/numbers/conversions-inl.h"
26 #include "src/objects/scope-info.h"
27 #include "src/parsing/parse-info.h"
28 #include "src/parsing/rewriter.h"
29 #include "src/runtime/runtime.h"
30 #include "src/strings/char-predicates-inl.h"
31 #include "src/strings/string-stream.h"
32 #include "src/strings/unicode-inl.h"
33 #include "src/tracing/trace-event.h"
34 #include "src/zone/zone-list-inl.h"
35
36 namespace v8 {
37 namespace internal {
38
DefaultConstructor(const AstRawString * name,bool call_super,int pos,int end_pos)39 FunctionLiteral* Parser::DefaultConstructor(const AstRawString* name,
40 bool call_super, int pos,
41 int end_pos) {
42 int expected_property_count = 0;
43 const int parameter_count = 0;
44
45 FunctionKind kind = call_super ? FunctionKind::kDefaultDerivedConstructor
46 : FunctionKind::kDefaultBaseConstructor;
47 DeclarationScope* function_scope = NewFunctionScope(kind);
48 SetLanguageMode(function_scope, LanguageMode::kStrict);
49 // Set start and end position to the same value
50 function_scope->set_start_position(pos);
51 function_scope->set_end_position(pos);
52 ScopedPtrList<Statement> body(pointer_buffer());
53
54 {
55 FunctionState function_state(&function_state_, &scope_, function_scope);
56
57 if (call_super) {
58 // Create a SuperCallReference and handle in BytecodeGenerator.
59 auto constructor_args_name = ast_value_factory()->empty_string();
60 bool is_rest = true;
61 bool is_optional = false;
62 Variable* constructor_args = function_scope->DeclareParameter(
63 constructor_args_name, VariableMode::kTemporary, is_optional, is_rest,
64 ast_value_factory(), pos);
65
66 Expression* call;
67 {
68 ScopedPtrList<Expression> args(pointer_buffer());
69 Spread* spread_args = factory()->NewSpread(
70 factory()->NewVariableProxy(constructor_args), pos, pos);
71
72 args.Add(spread_args);
73 Expression* super_call_ref = NewSuperCallReference(pos);
74 constexpr bool has_spread = true;
75 call = factory()->NewCall(super_call_ref, args, pos, has_spread);
76 }
77 body.Add(factory()->NewReturnStatement(call, pos));
78 }
79
80 expected_property_count = function_state.expected_property_count();
81 }
82
83 FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
84 name, function_scope, body, expected_property_count, parameter_count,
85 parameter_count, FunctionLiteral::kNoDuplicateParameters,
86 FunctionSyntaxKind::kAnonymousExpression, default_eager_compile_hint(),
87 pos, true, GetNextFunctionLiteralId());
88 return function_literal;
89 }
90
ReportUnexpectedTokenAt(Scanner::Location location,Token::Value token,MessageTemplate message)91 void Parser::ReportUnexpectedTokenAt(Scanner::Location location,
92 Token::Value token,
93 MessageTemplate message) {
94 const char* arg = nullptr;
95 switch (token) {
96 case Token::EOS:
97 message = MessageTemplate::kUnexpectedEOS;
98 break;
99 case Token::SMI:
100 case Token::NUMBER:
101 case Token::BIGINT:
102 message = MessageTemplate::kUnexpectedTokenNumber;
103 break;
104 case Token::STRING:
105 message = MessageTemplate::kUnexpectedTokenString;
106 break;
107 case Token::PRIVATE_NAME:
108 case Token::IDENTIFIER:
109 message = MessageTemplate::kUnexpectedTokenIdentifier;
110 break;
111 case Token::AWAIT:
112 case Token::ENUM:
113 message = MessageTemplate::kUnexpectedReserved;
114 break;
115 case Token::LET:
116 case Token::STATIC:
117 case Token::YIELD:
118 case Token::FUTURE_STRICT_RESERVED_WORD:
119 message = is_strict(language_mode())
120 ? MessageTemplate::kUnexpectedStrictReserved
121 : MessageTemplate::kUnexpectedTokenIdentifier;
122 break;
123 case Token::TEMPLATE_SPAN:
124 case Token::TEMPLATE_TAIL:
125 message = MessageTemplate::kUnexpectedTemplateString;
126 break;
127 case Token::ESCAPED_STRICT_RESERVED_WORD:
128 case Token::ESCAPED_KEYWORD:
129 message = MessageTemplate::kInvalidEscapedReservedWord;
130 break;
131 case Token::ILLEGAL:
132 if (scanner()->has_error()) {
133 message = scanner()->error();
134 location = scanner()->error_location();
135 } else {
136 message = MessageTemplate::kInvalidOrUnexpectedToken;
137 }
138 break;
139 case Token::REGEXP_LITERAL:
140 message = MessageTemplate::kUnexpectedTokenRegExp;
141 break;
142 default:
143 const char* name = Token::String(token);
144 DCHECK_NOT_NULL(name);
145 arg = name;
146 break;
147 }
148 ReportMessageAt(location, message, arg);
149 }
150
151 // ----------------------------------------------------------------------------
152 // Implementation of Parser
153
ShortcutNumericLiteralBinaryExpression(Expression ** x,Expression * y,Token::Value op,int pos)154 bool Parser::ShortcutNumericLiteralBinaryExpression(Expression** x,
155 Expression* y,
156 Token::Value op, int pos) {
157 if ((*x)->IsNumberLiteral() && y->IsNumberLiteral()) {
158 double x_val = (*x)->AsLiteral()->AsNumber();
159 double y_val = y->AsLiteral()->AsNumber();
160 switch (op) {
161 case Token::ADD:
162 *x = factory()->NewNumberLiteral(x_val + y_val, pos);
163 return true;
164 case Token::SUB:
165 *x = factory()->NewNumberLiteral(x_val - y_val, pos);
166 return true;
167 case Token::MUL:
168 *x = factory()->NewNumberLiteral(x_val * y_val, pos);
169 return true;
170 case Token::DIV:
171 *x = factory()->NewNumberLiteral(base::Divide(x_val, y_val), pos);
172 return true;
173 case Token::BIT_OR: {
174 int value = DoubleToInt32(x_val) | DoubleToInt32(y_val);
175 *x = factory()->NewNumberLiteral(value, pos);
176 return true;
177 }
178 case Token::BIT_AND: {
179 int value = DoubleToInt32(x_val) & DoubleToInt32(y_val);
180 *x = factory()->NewNumberLiteral(value, pos);
181 return true;
182 }
183 case Token::BIT_XOR: {
184 int value = DoubleToInt32(x_val) ^ DoubleToInt32(y_val);
185 *x = factory()->NewNumberLiteral(value, pos);
186 return true;
187 }
188 case Token::SHL: {
189 int value =
190 base::ShlWithWraparound(DoubleToInt32(x_val), DoubleToInt32(y_val));
191 *x = factory()->NewNumberLiteral(value, pos);
192 return true;
193 }
194 case Token::SHR: {
195 uint32_t shift = DoubleToInt32(y_val) & 0x1F;
196 uint32_t value = DoubleToUint32(x_val) >> shift;
197 *x = factory()->NewNumberLiteral(value, pos);
198 return true;
199 }
200 case Token::SAR: {
201 uint32_t shift = DoubleToInt32(y_val) & 0x1F;
202 int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift);
203 *x = factory()->NewNumberLiteral(value, pos);
204 return true;
205 }
206 case Token::EXP:
207 *x = factory()->NewNumberLiteral(base::ieee754::pow(x_val, y_val), pos);
208 return true;
209 default:
210 break;
211 }
212 }
213 return false;
214 }
215
CollapseNaryExpression(Expression ** x,Expression * y,Token::Value op,int pos,const SourceRange & range)216 bool Parser::CollapseNaryExpression(Expression** x, Expression* y,
217 Token::Value op, int pos,
218 const SourceRange& range) {
219 // Filter out unsupported ops.
220 if (!Token::IsBinaryOp(op) || op == Token::EXP) return false;
221
222 // Convert *x into an nary operation with the given op, returning false if
223 // this is not possible.
224 NaryOperation* nary = nullptr;
225 if ((*x)->IsBinaryOperation()) {
226 BinaryOperation* binop = (*x)->AsBinaryOperation();
227 if (binop->op() != op) return false;
228
229 nary = factory()->NewNaryOperation(op, binop->left(), 2);
230 nary->AddSubsequent(binop->right(), binop->position());
231 ConvertBinaryToNaryOperationSourceRange(binop, nary);
232 *x = nary;
233 } else if ((*x)->IsNaryOperation()) {
234 nary = (*x)->AsNaryOperation();
235 if (nary->op() != op) return false;
236 } else {
237 return false;
238 }
239
240 // Append our current expression to the nary operation.
241 // TODO(leszeks): Do some literal collapsing here if we're appending Smi or
242 // String literals.
243 nary->AddSubsequent(y, pos);
244 nary->clear_parenthesized();
245 AppendNaryOperationSourceRange(nary, range);
246
247 return true;
248 }
249
GetBigIntAsSymbol()250 const AstRawString* Parser::GetBigIntAsSymbol() {
251 base::Vector<const uint8_t> literal = scanner()->BigIntLiteral();
252 if (literal[0] != '0' || literal.length() == 1) {
253 return ast_value_factory()->GetOneByteString(literal);
254 }
255 std::unique_ptr<char[]> decimal =
256 BigIntLiteralToDecimal(local_isolate_, literal);
257 return ast_value_factory()->GetOneByteString(decimal.get());
258 }
259
BuildUnaryExpression(Expression * expression,Token::Value op,int pos)260 Expression* Parser::BuildUnaryExpression(Expression* expression,
261 Token::Value op, int pos) {
262 DCHECK_NOT_NULL(expression);
263 const Literal* literal = expression->AsLiteral();
264 if (literal != nullptr) {
265 if (op == Token::NOT) {
266 // Convert the literal to a boolean condition and negate it.
267 return factory()->NewBooleanLiteral(literal->ToBooleanIsFalse(), pos);
268 } else if (literal->IsNumberLiteral()) {
269 // Compute some expressions involving only number literals.
270 double value = literal->AsNumber();
271 switch (op) {
272 case Token::ADD:
273 return expression;
274 case Token::SUB:
275 return factory()->NewNumberLiteral(-value, pos);
276 case Token::BIT_NOT:
277 return factory()->NewNumberLiteral(~DoubleToInt32(value), pos);
278 default:
279 break;
280 }
281 }
282 }
283 return factory()->NewUnaryOperation(op, expression, pos);
284 }
285
NewThrowError(Runtime::FunctionId id,MessageTemplate message,const AstRawString * arg,int pos)286 Expression* Parser::NewThrowError(Runtime::FunctionId id,
287 MessageTemplate message,
288 const AstRawString* arg, int pos) {
289 ScopedPtrList<Expression> args(pointer_buffer());
290 args.Add(factory()->NewSmiLiteral(static_cast<int>(message), pos));
291 args.Add(factory()->NewStringLiteral(arg, pos));
292 CallRuntime* call_constructor = factory()->NewCallRuntime(id, args, pos);
293 return factory()->NewThrow(call_constructor, pos);
294 }
295
NewSuperPropertyReference(Scope * home_object_scope,int pos)296 Expression* Parser::NewSuperPropertyReference(Scope* home_object_scope,
297 int pos) {
298 const AstRawString* home_object_name;
299 if (IsStatic(scope()->GetReceiverScope()->function_kind())) {
300 home_object_name = ast_value_factory_->dot_static_home_object_string();
301 } else {
302 home_object_name = ast_value_factory_->dot_home_object_string();
303 }
304 return factory()->NewSuperPropertyReference(
305 home_object_scope->NewHomeObjectVariableProxy(factory(), home_object_name,
306 pos),
307 pos);
308 }
309
NewSuperCallReference(int pos)310 Expression* Parser::NewSuperCallReference(int pos) {
311 VariableProxy* new_target_proxy =
312 NewUnresolved(ast_value_factory()->new_target_string(), pos);
313 VariableProxy* this_function_proxy =
314 NewUnresolved(ast_value_factory()->this_function_string(), pos);
315 return factory()->NewSuperCallReference(new_target_proxy, this_function_proxy,
316 pos);
317 }
318
NewTargetExpression(int pos)319 Expression* Parser::NewTargetExpression(int pos) {
320 auto proxy = NewUnresolved(ast_value_factory()->new_target_string(), pos);
321 proxy->set_is_new_target();
322 return proxy;
323 }
324
ImportMetaExpression(int pos)325 Expression* Parser::ImportMetaExpression(int pos) {
326 ScopedPtrList<Expression> args(pointer_buffer());
327 return factory()->NewCallRuntime(Runtime::kInlineGetImportMetaObject, args,
328 pos);
329 }
330
ExpressionFromLiteral(Token::Value token,int pos)331 Expression* Parser::ExpressionFromLiteral(Token::Value token, int pos) {
332 switch (token) {
333 case Token::NULL_LITERAL:
334 return factory()->NewNullLiteral(pos);
335 case Token::TRUE_LITERAL:
336 return factory()->NewBooleanLiteral(true, pos);
337 case Token::FALSE_LITERAL:
338 return factory()->NewBooleanLiteral(false, pos);
339 case Token::SMI: {
340 uint32_t value = scanner()->smi_value();
341 return factory()->NewSmiLiteral(value, pos);
342 }
343 case Token::NUMBER: {
344 double value = scanner()->DoubleValue();
345 return factory()->NewNumberLiteral(value, pos);
346 }
347 case Token::BIGINT:
348 return factory()->NewBigIntLiteral(
349 AstBigInt(scanner()->CurrentLiteralAsCString(zone())), pos);
350 case Token::STRING: {
351 return factory()->NewStringLiteral(GetSymbol(), pos);
352 }
353 default:
354 DCHECK(false);
355 }
356 return FailureExpression();
357 }
358
NewV8Intrinsic(const AstRawString * name,const ScopedPtrList<Expression> & args,int pos)359 Expression* Parser::NewV8Intrinsic(const AstRawString* name,
360 const ScopedPtrList<Expression>& args,
361 int pos) {
362 if (ParsingExtension()) {
363 // The extension structures are only accessible while parsing the
364 // very first time, not when reparsing because of lazy compilation.
365 GetClosureScope()->ForceEagerCompilation();
366 }
367
368 if (!name->is_one_byte()) {
369 // There are no two-byte named intrinsics.
370 ReportMessage(MessageTemplate::kNotDefined, name);
371 return FailureExpression();
372 }
373
374 const Runtime::Function* function =
375 Runtime::FunctionForName(name->raw_data(), name->length());
376
377 // Be more permissive when fuzzing. Intrinsics are not supported.
378 if (FLAG_fuzzing) {
379 return NewV8RuntimeFunctionForFuzzing(function, args, pos);
380 }
381
382 if (function != nullptr) {
383 // Check for possible name clash.
384 DCHECK_EQ(Context::kNotFound,
385 Context::IntrinsicIndexForName(name->raw_data(), name->length()));
386
387 // Check that the expected number of arguments are being passed.
388 if (function->nargs != -1 && function->nargs != args.length()) {
389 ReportMessage(MessageTemplate::kRuntimeWrongNumArgs);
390 return FailureExpression();
391 }
392
393 return factory()->NewCallRuntime(function, args, pos);
394 }
395
396 int context_index =
397 Context::IntrinsicIndexForName(name->raw_data(), name->length());
398
399 // Check that the function is defined.
400 if (context_index == Context::kNotFound) {
401 ReportMessage(MessageTemplate::kNotDefined, name);
402 return FailureExpression();
403 }
404
405 return factory()->NewCallRuntime(context_index, args, pos);
406 }
407
408 // More permissive runtime-function creation on fuzzers.
NewV8RuntimeFunctionForFuzzing(const Runtime::Function * function,const ScopedPtrList<Expression> & args,int pos)409 Expression* Parser::NewV8RuntimeFunctionForFuzzing(
410 const Runtime::Function* function, const ScopedPtrList<Expression>& args,
411 int pos) {
412 CHECK(FLAG_fuzzing);
413
414 // Intrinsics are not supported for fuzzing. Only allow allowlisted runtime
415 // functions. Also prevent later errors due to too few arguments and just
416 // ignore this call.
417 if (function == nullptr ||
418 !Runtime::IsAllowListedForFuzzing(function->function_id) ||
419 function->nargs > args.length()) {
420 return factory()->NewUndefinedLiteral(kNoSourcePosition);
421 }
422
423 // Flexible number of arguments permitted.
424 if (function->nargs == -1) {
425 return factory()->NewCallRuntime(function, args, pos);
426 }
427
428 // Otherwise ignore superfluous arguments.
429 ScopedPtrList<Expression> permissive_args(pointer_buffer());
430 for (int i = 0; i < function->nargs; i++) {
431 permissive_args.Add(args.at(i));
432 }
433 return factory()->NewCallRuntime(function, permissive_args, pos);
434 }
435
Parser(LocalIsolate * local_isolate,ParseInfo * info,Handle<Script> script)436 Parser::Parser(LocalIsolate* local_isolate, ParseInfo* info,
437 Handle<Script> script)
438 : ParserBase<Parser>(
439 info->zone(), &scanner_, info->stack_limit(),
440 info->ast_value_factory(), info->pending_error_handler(),
441 info->runtime_call_stats(), info->logger(), info->flags(), true),
442 local_isolate_(local_isolate),
443 info_(info),
444 script_(script),
445 scanner_(info->character_stream(), flags()),
446 preparser_zone_(info->zone()->allocator(), "pre-parser-zone"),
447 reusable_preparser_(nullptr),
448 mode_(PARSE_EAGERLY), // Lazy mode must be set explicitly.
449 source_range_map_(info->source_range_map()),
450 total_preparse_skipped_(0),
451 consumed_preparse_data_(info->consumed_preparse_data()),
452 preparse_data_buffer_(),
453 parameters_end_pos_(info->parameters_end_pos()) {
454 // Even though we were passed ParseInfo, we should not store it in
455 // Parser - this makes sure that Isolate is not accidentally accessed via
456 // ParseInfo during background parsing.
457 DCHECK_NOT_NULL(info->character_stream());
458 // Determine if functions can be lazily compiled. This is necessary to
459 // allow some of our builtin JS files to be lazily compiled. These
460 // builtins cannot be handled lazily by the parser, since we have to know
461 // if a function uses the special natives syntax, which is something the
462 // parser records.
463 // If the debugger requests compilation for break points, we cannot be
464 // aggressive about lazy compilation, because it might trigger compilation
465 // of functions without an outer context when setting a breakpoint through
466 // Debug::FindSharedFunctionInfoInScript
467 // We also compile eagerly for kProduceExhaustiveCodeCache.
468 bool can_compile_lazily = flags().allow_lazy_compile() && !flags().is_eager();
469
470 set_default_eager_compile_hint(can_compile_lazily
471 ? FunctionLiteral::kShouldLazyCompile
472 : FunctionLiteral::kShouldEagerCompile);
473 allow_lazy_ = flags().allow_lazy_compile() && flags().allow_lazy_parsing() &&
474 info->extension() == nullptr && can_compile_lazily;
475 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
476 ++feature) {
477 use_counts_[feature] = 0;
478 }
479 }
480
InitializeEmptyScopeChain(ParseInfo * info)481 void Parser::InitializeEmptyScopeChain(ParseInfo* info) {
482 DCHECK_NULL(original_scope_);
483 DCHECK_NULL(info->script_scope());
484 DeclarationScope* script_scope =
485 NewScriptScope(flags().is_repl_mode() ? REPLMode::kYes : REPLMode::kNo);
486 info->set_script_scope(script_scope);
487 original_scope_ = script_scope;
488 }
489
490 template <typename IsolateT>
DeserializeScopeChain(IsolateT * isolate,ParseInfo * info,MaybeHandle<ScopeInfo> maybe_outer_scope_info,Scope::DeserializationMode mode)491 void Parser::DeserializeScopeChain(
492 IsolateT* isolate, ParseInfo* info,
493 MaybeHandle<ScopeInfo> maybe_outer_scope_info,
494 Scope::DeserializationMode mode) {
495 InitializeEmptyScopeChain(info);
496 Handle<ScopeInfo> outer_scope_info;
497 if (maybe_outer_scope_info.ToHandle(&outer_scope_info)) {
498 DCHECK_EQ(ThreadId::Current(), isolate->thread_id());
499 original_scope_ = Scope::DeserializeScopeChain(
500 isolate, zone(), *outer_scope_info, info->script_scope(),
501 ast_value_factory(), mode);
502 if (flags().is_eval() || IsArrowFunction(flags().function_kind())) {
503 original_scope_->GetReceiverScope()->DeserializeReceiver(
504 ast_value_factory());
505 }
506 }
507 }
508
509 template void Parser::DeserializeScopeChain(
510 Isolate* isolate, ParseInfo* info,
511 MaybeHandle<ScopeInfo> maybe_outer_scope_info,
512 Scope::DeserializationMode mode);
513 template void Parser::DeserializeScopeChain(
514 LocalIsolate* isolate, ParseInfo* info,
515 MaybeHandle<ScopeInfo> maybe_outer_scope_info,
516 Scope::DeserializationMode mode);
517
518 namespace {
519
MaybeProcessSourceRanges(ParseInfo * parse_info,Expression * root,uintptr_t stack_limit_)520 void MaybeProcessSourceRanges(ParseInfo* parse_info, Expression* root,
521 uintptr_t stack_limit_) {
522 if (root != nullptr && parse_info->source_range_map() != nullptr) {
523 SourceRangeAstVisitor visitor(stack_limit_, root,
524 parse_info->source_range_map());
525 visitor.Run();
526 }
527 }
528
529 } // namespace
530
ParseProgram(Isolate * isolate,Handle<Script> script,ParseInfo * info,MaybeHandle<ScopeInfo> maybe_outer_scope_info)531 void Parser::ParseProgram(Isolate* isolate, Handle<Script> script,
532 ParseInfo* info,
533 MaybeHandle<ScopeInfo> maybe_outer_scope_info) {
534 DCHECK_EQ(script->id(), flags().script_id());
535
536 // It's OK to use the Isolate & counters here, since this function is only
537 // called in the main thread.
538 DCHECK(parsing_on_main_thread_);
539 RCS_SCOPE(runtime_call_stats_, flags().is_eval()
540 ? RuntimeCallCounterId::kParseEval
541 : RuntimeCallCounterId::kParseProgram);
542 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.ParseProgram");
543 base::ElapsedTimer timer;
544 if (V8_UNLIKELY(FLAG_log_function_events)) timer.Start();
545
546 // Initialize parser state.
547 DeserializeScopeChain(isolate, info, maybe_outer_scope_info,
548 Scope::DeserializationMode::kIncludingVariables);
549
550 DCHECK_EQ(script->is_wrapped(), info->is_wrapped_as_function());
551 if (script->is_wrapped()) {
552 maybe_wrapped_arguments_ = handle(script->wrapped_arguments(), isolate);
553 }
554
555 scanner_.Initialize();
556 FunctionLiteral* result = DoParseProgram(isolate, info);
557 MaybeProcessSourceRanges(info, result, stack_limit_);
558 PostProcessParseResult(isolate, info, result);
559
560 HandleSourceURLComments(isolate, script);
561
562 if (V8_UNLIKELY(FLAG_log_function_events) && result != nullptr) {
563 double ms = timer.Elapsed().InMillisecondsF();
564 const char* event_name = "parse-eval";
565 int start = -1;
566 int end = -1;
567 if (!flags().is_eval()) {
568 event_name = "parse-script";
569 start = 0;
570 end = String::cast(script->source()).length();
571 }
572 LOG(isolate,
573 FunctionEvent(event_name, flags().script_id(), ms, start, end, "", 0));
574 }
575 }
576
DoParseProgram(Isolate * isolate,ParseInfo * info)577 FunctionLiteral* Parser::DoParseProgram(Isolate* isolate, ParseInfo* info) {
578 // Note that this function can be called from the main thread or from a
579 // background thread. We should not access anything Isolate / heap dependent
580 // via ParseInfo, and also not pass it forward. If not on the main thread
581 // isolate will be nullptr.
582 DCHECK_EQ(parsing_on_main_thread_, isolate != nullptr);
583 DCHECK_NULL(scope_);
584
585 ParsingModeScope mode(this, allow_lazy_ ? PARSE_LAZILY : PARSE_EAGERLY);
586 ResetFunctionLiteralId();
587
588 FunctionLiteral* result = nullptr;
589 {
590 Scope* outer = original_scope_;
591 DCHECK_NOT_NULL(outer);
592 if (flags().is_eval()) {
593 outer = NewEvalScope(outer);
594 } else if (flags().is_module()) {
595 DCHECK_EQ(outer, info->script_scope());
596 outer = NewModuleScope(info->script_scope());
597 }
598
599 DeclarationScope* scope = outer->AsDeclarationScope();
600 scope->set_start_position(0);
601
602 FunctionState function_state(&function_state_, &scope_, scope);
603 ScopedPtrList<Statement> body(pointer_buffer());
604 int beg_pos = scanner()->location().beg_pos;
605 if (flags().is_module()) {
606 DCHECK(flags().is_module());
607
608 PrepareGeneratorVariables();
609 Expression* initial_yield = BuildInitialYield(
610 kNoSourcePosition, FunctionKind::kGeneratorFunction);
611 body.Add(
612 factory()->NewExpressionStatement(initial_yield, kNoSourcePosition));
613 // First parse statements into a buffer. Then, if there was a
614 // top level await, create an inner block and rewrite the body of the
615 // module as an async function. Otherwise merge the statements back
616 // into the main body.
617 BlockT block = impl()->NullBlock();
618 {
619 StatementListT statements(pointer_buffer());
620 ParseModuleItemList(&statements);
621 // Modules will always have an initial yield. If there are any
622 // additional suspends, i.e. awaits, then we treat the module as an
623 // AsyncModule.
624 if (function_state.suspend_count() > 1) {
625 scope->set_is_async_module();
626 block = factory()->NewBlock(true, statements);
627 } else {
628 statements.MergeInto(&body);
629 }
630 }
631 if (IsAsyncModule(scope->function_kind())) {
632 impl()->RewriteAsyncFunctionBody(
633 &body, block, factory()->NewUndefinedLiteral(kNoSourcePosition));
634 }
635 if (!has_error() &&
636 !module()->Validate(this->scope()->AsModuleScope(),
637 pending_error_handler(), zone())) {
638 scanner()->set_parser_error();
639 }
640 } else if (info->is_wrapped_as_function()) {
641 DCHECK(parsing_on_main_thread_);
642 ParseWrapped(isolate, info, &body, scope, zone());
643 } else if (flags().is_repl_mode()) {
644 ParseREPLProgram(info, &body, scope);
645 } else {
646 // Don't count the mode in the use counters--give the program a chance
647 // to enable script-wide strict mode below.
648 this->scope()->SetLanguageMode(info->language_mode());
649 ParseStatementList(&body, Token::EOS);
650 }
651
652 // The parser will peek but not consume EOS. Our scope logically goes all
653 // the way to the EOS, though.
654 scope->set_end_position(peek_position());
655
656 if (is_strict(language_mode())) {
657 CheckStrictOctalLiteral(beg_pos, end_position());
658 }
659 if (is_sloppy(language_mode())) {
660 // TODO(littledan): Function bindings on the global object that modify
661 // pre-existing bindings should be made writable, enumerable and
662 // nonconfigurable if possible, whereas this code will leave attributes
663 // unchanged if the property already exists.
664 InsertSloppyBlockFunctionVarBindings(scope);
665 }
666 // Internalize the ast strings in the case of eval so we can check for
667 // conflicting var declarations with outer scope-info-backed scopes.
668 if (flags().is_eval()) {
669 DCHECK(parsing_on_main_thread_);
670 DCHECK(!overall_parse_is_parked_);
671 info->ast_value_factory()->Internalize(isolate);
672 }
673 CheckConflictingVarDeclarations(scope);
674
675 if (flags().parse_restriction() == ONLY_SINGLE_FUNCTION_LITERAL) {
676 if (body.length() != 1 || !body.at(0)->IsExpressionStatement() ||
677 !body.at(0)
678 ->AsExpressionStatement()
679 ->expression()
680 ->IsFunctionLiteral()) {
681 ReportMessage(MessageTemplate::kSingleFunctionLiteral);
682 }
683 }
684
685 int parameter_count = 0;
686 result = factory()->NewScriptOrEvalFunctionLiteral(
687 scope, body, function_state.expected_property_count(), parameter_count);
688 result->set_suspend_count(function_state.suspend_count());
689 }
690
691 info->set_max_function_literal_id(GetLastFunctionLiteralId());
692
693 if (has_error()) return nullptr;
694
695 RecordFunctionLiteralSourceRange(result);
696
697 return result;
698 }
699
700 template <typename IsolateT>
PostProcessParseResult(IsolateT * isolate,ParseInfo * info,FunctionLiteral * literal)701 void Parser::PostProcessParseResult(IsolateT* isolate, ParseInfo* info,
702 FunctionLiteral* literal) {
703 if (literal == nullptr) return;
704
705 info->set_literal(literal);
706 info->set_language_mode(literal->language_mode());
707 if (info->flags().is_eval()) {
708 info->set_allow_eval_cache(allow_eval_cache());
709 }
710
711 info->ast_value_factory()->Internalize(isolate);
712
713 {
714 RCS_SCOPE(info->runtime_call_stats(), RuntimeCallCounterId::kCompileAnalyse,
715 RuntimeCallStats::kThreadSpecific);
716 if (!Rewriter::Rewrite(info) || !DeclarationScope::Analyze(info)) {
717 // Null out the literal to indicate that something failed.
718 info->set_literal(nullptr);
719 return;
720 }
721 }
722 }
723
724 template void Parser::PostProcessParseResult(Isolate* isolate, ParseInfo* info,
725 FunctionLiteral* literal);
726 template void Parser::PostProcessParseResult(LocalIsolate* isolate,
727 ParseInfo* info,
728 FunctionLiteral* literal);
729
PrepareWrappedArguments(Isolate * isolate,ParseInfo * info,Zone * zone)730 ZonePtrList<const AstRawString>* Parser::PrepareWrappedArguments(
731 Isolate* isolate, ParseInfo* info, Zone* zone) {
732 DCHECK(parsing_on_main_thread_);
733 DCHECK_NOT_NULL(isolate);
734 Handle<FixedArray> arguments = maybe_wrapped_arguments_.ToHandleChecked();
735 int arguments_length = arguments->length();
736 ZonePtrList<const AstRawString>* arguments_for_wrapped_function =
737 zone->New<ZonePtrList<const AstRawString>>(arguments_length, zone);
738 for (int i = 0; i < arguments_length; i++) {
739 const AstRawString* argument_string = ast_value_factory()->GetString(
740 String::cast(arguments->get(i)),
741 SharedStringAccessGuardIfNeeded(isolate));
742 arguments_for_wrapped_function->Add(argument_string, zone);
743 }
744 return arguments_for_wrapped_function;
745 }
746
ParseWrapped(Isolate * isolate,ParseInfo * info,ScopedPtrList<Statement> * body,DeclarationScope * outer_scope,Zone * zone)747 void Parser::ParseWrapped(Isolate* isolate, ParseInfo* info,
748 ScopedPtrList<Statement>* body,
749 DeclarationScope* outer_scope, Zone* zone) {
750 DCHECK(parsing_on_main_thread_);
751 DCHECK(info->is_wrapped_as_function());
752 ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
753
754 // Set function and block state for the outer eval scope.
755 DCHECK(outer_scope->is_eval_scope());
756 FunctionState function_state(&function_state_, &scope_, outer_scope);
757
758 const AstRawString* function_name = nullptr;
759 Scanner::Location location(0, 0);
760
761 ZonePtrList<const AstRawString>* arguments_for_wrapped_function =
762 PrepareWrappedArguments(isolate, info, zone);
763
764 FunctionLiteral* function_literal =
765 ParseFunctionLiteral(function_name, location, kSkipFunctionNameCheck,
766 FunctionKind::kNormalFunction, kNoSourcePosition,
767 FunctionSyntaxKind::kWrapped, LanguageMode::kSloppy,
768 arguments_for_wrapped_function);
769
770 Statement* return_statement =
771 factory()->NewReturnStatement(function_literal, kNoSourcePosition);
772 body->Add(return_statement);
773 }
774
ParseREPLProgram(ParseInfo * info,ScopedPtrList<Statement> * body,DeclarationScope * scope)775 void Parser::ParseREPLProgram(ParseInfo* info, ScopedPtrList<Statement>* body,
776 DeclarationScope* scope) {
777 // REPL scripts are handled nearly the same way as the body of an async
778 // function. The difference is the value used to resolve the async
779 // promise.
780 // For a REPL script this is the completion value of the
781 // script instead of the expression of some "return" statement. The
782 // completion value of the script is obtained by manually invoking
783 // the {Rewriter} which will return a VariableProxy referencing the
784 // result.
785 DCHECK(flags().is_repl_mode());
786 this->scope()->SetLanguageMode(info->language_mode());
787 PrepareGeneratorVariables();
788
789 BlockT block = impl()->NullBlock();
790 {
791 StatementListT statements(pointer_buffer());
792 ParseStatementList(&statements, Token::EOS);
793 block = factory()->NewBlock(true, statements);
794 }
795
796 if (has_error()) return;
797
798 base::Optional<VariableProxy*> maybe_result =
799 Rewriter::RewriteBody(info, scope, block->statements());
800 Expression* result_value =
801 (maybe_result && *maybe_result)
802 ? static_cast<Expression*>(*maybe_result)
803 : factory()->NewUndefinedLiteral(kNoSourcePosition);
804
805 impl()->RewriteAsyncFunctionBody(body, block, WrapREPLResult(result_value),
806 REPLMode::kYes);
807 }
808
WrapREPLResult(Expression * value)809 Expression* Parser::WrapREPLResult(Expression* value) {
810 // REPL scripts additionally wrap the ".result" variable in an
811 // object literal:
812 //
813 // return %_AsyncFunctionResolve(
814 // .generator_object, {.repl_result: .result});
815 //
816 // Should ".result" be a resolved promise itself, the async return
817 // would chain the promises and return the resolve value instead of
818 // the promise.
819
820 Literal* property_name = factory()->NewStringLiteral(
821 ast_value_factory()->dot_repl_result_string(), kNoSourcePosition);
822 ObjectLiteralProperty* property =
823 factory()->NewObjectLiteralProperty(property_name, value, true);
824
825 ScopedPtrList<ObjectLiteralProperty> properties(pointer_buffer());
826 properties.Add(property);
827 return factory()->NewObjectLiteral(properties, false, kNoSourcePosition,
828 false);
829 }
830
ParseFunction(Isolate * isolate,ParseInfo * info,Handle<SharedFunctionInfo> shared_info)831 void Parser::ParseFunction(Isolate* isolate, ParseInfo* info,
832 Handle<SharedFunctionInfo> shared_info) {
833 // It's OK to use the Isolate & counters here, since this function is only
834 // called in the main thread.
835 DCHECK(parsing_on_main_thread_);
836 RCS_SCOPE(runtime_call_stats_, RuntimeCallCounterId::kParseFunction);
837 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.ParseFunction");
838 base::ElapsedTimer timer;
839 if (V8_UNLIKELY(FLAG_log_function_events)) timer.Start();
840
841 MaybeHandle<ScopeInfo> maybe_outer_scope_info;
842 if (shared_info->HasOuterScopeInfo()) {
843 maybe_outer_scope_info = handle(shared_info->GetOuterScopeInfo(), isolate);
844 }
845 int start_position = shared_info->StartPosition();
846 int end_position = shared_info->EndPosition();
847
848 MaybeHandle<ScopeInfo> deserialize_start_scope = maybe_outer_scope_info;
849 bool needs_script_scope_finalization = false;
850 // If the function is a class member initializer and there isn't a
851 // scope mismatch, we will only deserialize up to the outer scope of
852 // the class scope, and regenerate the class scope during reparsing.
853 if (flags().function_kind() ==
854 FunctionKind::kClassMembersInitializerFunction &&
855 shared_info->HasOuterScopeInfo() &&
856 maybe_outer_scope_info.ToHandleChecked()->scope_type() == CLASS_SCOPE &&
857 maybe_outer_scope_info.ToHandleChecked()->StartPosition() ==
858 start_position) {
859 Handle<ScopeInfo> outer_scope_info =
860 maybe_outer_scope_info.ToHandleChecked();
861 if (outer_scope_info->HasOuterScopeInfo()) {
862 deserialize_start_scope =
863 handle(outer_scope_info->OuterScopeInfo(), isolate);
864 } else {
865 // If the class scope doesn't have an outer scope to deserialize, we need
866 // to finalize the script scope without using
867 // Scope::DeserializeScopeChain().
868 deserialize_start_scope = MaybeHandle<ScopeInfo>();
869 needs_script_scope_finalization = true;
870 }
871 }
872
873 DeserializeScopeChain(isolate, info, deserialize_start_scope,
874 Scope::DeserializationMode::kIncludingVariables);
875 if (needs_script_scope_finalization) {
876 DCHECK_EQ(original_scope_, info->script_scope());
877 Scope::SetScriptScopeInfo(isolate, info->script_scope());
878 }
879 DCHECK_EQ(factory()->zone(), info->zone());
880
881 Handle<Script> script = handle(Script::cast(shared_info->script()), isolate);
882 if (shared_info->is_wrapped()) {
883 maybe_wrapped_arguments_ = handle(script->wrapped_arguments(), isolate);
884 }
885
886 int function_literal_id = shared_info->function_literal_id();
887 if V8_UNLIKELY (script->type() == Script::TYPE_WEB_SNAPSHOT) {
888 // Function literal IDs for inner functions haven't been allocated when
889 // deserializing. Put the inner function SFIs to the end of the list;
890 // they'll be deduplicated later (if the corresponding SFIs exist already)
891 // in Script::FindSharedFunctionInfo. (-1 here because function_literal_id
892 // is the parent's id. The inner function will get ids starting from
893 // function_literal_id + 1.)
894 function_literal_id = script->shared_function_info_count() - 1;
895 }
896
897 // Initialize parser state.
898 info->set_function_name(ast_value_factory()->GetString(
899 shared_info->Name(), SharedStringAccessGuardIfNeeded(isolate)));
900 scanner_.Initialize();
901
902 FunctionLiteral* result;
903 if (V8_UNLIKELY(shared_info->private_name_lookup_skips_outer_class() &&
904 original_scope_->is_class_scope())) {
905 // If the function skips the outer class and the outer scope is a class, the
906 // function is in heritage position. Otherwise the function scope's skip bit
907 // will be correctly inherited from the outer scope.
908 ClassScope::HeritageParsingScope heritage(original_scope_->AsClassScope());
909 result = DoParseDeserializedFunction(
910 isolate, maybe_outer_scope_info, info, start_position, end_position,
911 function_literal_id, info->function_name());
912 } else {
913 result = DoParseDeserializedFunction(
914 isolate, maybe_outer_scope_info, info, start_position, end_position,
915 function_literal_id, info->function_name());
916 }
917 MaybeProcessSourceRanges(info, result, stack_limit_);
918 if (result != nullptr) {
919 Handle<String> inferred_name(shared_info->inferred_name(), isolate);
920 result->set_inferred_name(inferred_name);
921 // Fix the function_literal_id in case we changed it earlier.
922 result->set_function_literal_id(shared_info->function_literal_id());
923 }
924 PostProcessParseResult(isolate, info, result);
925 if (V8_UNLIKELY(FLAG_log_function_events) && result != nullptr) {
926 double ms = timer.Elapsed().InMillisecondsF();
927 // We should already be internalized by now, so the debug name will be
928 // available.
929 DeclarationScope* function_scope = result->scope();
930 std::unique_ptr<char[]> function_name = result->GetDebugName();
931 LOG(isolate,
932 FunctionEvent("parse-function", flags().script_id(), ms,
933 function_scope->start_position(),
934 function_scope->end_position(), function_name.get(),
935 strlen(function_name.get())));
936 }
937 }
938
DoParseFunction(Isolate * isolate,ParseInfo * info,int start_position,int end_position,int function_literal_id,const AstRawString * raw_name)939 FunctionLiteral* Parser::DoParseFunction(Isolate* isolate, ParseInfo* info,
940 int start_position, int end_position,
941 int function_literal_id,
942 const AstRawString* raw_name) {
943 DCHECK_EQ(parsing_on_main_thread_, isolate != nullptr);
944 DCHECK_NOT_NULL(raw_name);
945 DCHECK_NULL(scope_);
946
947 DCHECK(ast_value_factory());
948 fni_.PushEnclosingName(raw_name);
949
950 ResetFunctionLiteralId();
951 DCHECK_LT(0, function_literal_id);
952 SkipFunctionLiterals(function_literal_id - 1);
953
954 ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
955
956 // Place holder for the result.
957 FunctionLiteral* result = nullptr;
958
959 {
960 // Parse the function literal.
961 Scope* outer = original_scope_;
962 DeclarationScope* outer_function = outer->GetClosureScope();
963 DCHECK(outer);
964 FunctionState function_state(&function_state_, &scope_, outer_function);
965 BlockState block_state(&scope_, outer);
966 DCHECK(is_sloppy(outer->language_mode()) ||
967 is_strict(info->language_mode()));
968 FunctionKind kind = flags().function_kind();
969 DCHECK_IMPLIES(IsConciseMethod(kind) || IsAccessorFunction(kind),
970 flags().function_syntax_kind() ==
971 FunctionSyntaxKind::kAccessorOrMethod);
972
973 if (IsArrowFunction(kind)) {
974 if (IsAsyncFunction(kind)) {
975 DCHECK(!scanner()->HasLineTerminatorAfterNext());
976 if (!Check(Token::ASYNC)) {
977 CHECK(stack_overflow());
978 return nullptr;
979 }
980 if (!(peek_any_identifier() || peek() == Token::LPAREN)) {
981 CHECK(stack_overflow());
982 return nullptr;
983 }
984 }
985
986 // TODO(adamk): We should construct this scope from the ScopeInfo.
987 DeclarationScope* scope = NewFunctionScope(kind);
988 scope->set_has_checked_syntax(true);
989
990 // This bit only needs to be explicitly set because we're
991 // not passing the ScopeInfo to the Scope constructor.
992 SetLanguageMode(scope, info->language_mode());
993
994 scope->set_start_position(start_position);
995 ParserFormalParameters formals(scope);
996 {
997 ParameterDeclarationParsingScope formals_scope(this);
998 // Parsing patterns as variable reference expression creates
999 // NewUnresolved references in current scope. Enter arrow function
1000 // scope for formal parameter parsing.
1001 BlockState inner_block_state(&scope_, scope);
1002 if (Check(Token::LPAREN)) {
1003 // '(' StrictFormalParameters ')'
1004 ParseFormalParameterList(&formals);
1005 Expect(Token::RPAREN);
1006 } else {
1007 // BindingIdentifier
1008 ParameterParsingScope parameter_parsing_scope(impl(), &formals);
1009 ParseFormalParameter(&formals);
1010 DeclareFormalParameters(&formals);
1011 }
1012 formals.duplicate_loc = formals_scope.duplicate_location();
1013 }
1014
1015 if (GetLastFunctionLiteralId() != function_literal_id - 1) {
1016 if (has_error()) return nullptr;
1017 // If there were FunctionLiterals in the parameters, we need to
1018 // renumber them to shift down so the next function literal id for
1019 // the arrow function is the one requested.
1020 AstFunctionLiteralIdReindexer reindexer(
1021 stack_limit_,
1022 (function_literal_id - 1) - GetLastFunctionLiteralId());
1023 for (auto p : formals.params) {
1024 if (p->pattern != nullptr) reindexer.Reindex(p->pattern);
1025 if (p->initializer() != nullptr) {
1026 reindexer.Reindex(p->initializer());
1027 }
1028 if (reindexer.HasStackOverflow()) {
1029 set_stack_overflow();
1030 return nullptr;
1031 }
1032 }
1033 ResetFunctionLiteralId();
1034 SkipFunctionLiterals(function_literal_id - 1);
1035 }
1036
1037 Expression* expression = ParseArrowFunctionLiteral(formals);
1038 // Scanning must end at the same position that was recorded
1039 // previously. If not, parsing has been interrupted due to a stack
1040 // overflow, at which point the partially parsed arrow function
1041 // concise body happens to be a valid expression. This is a problem
1042 // only for arrow functions with single expression bodies, since there
1043 // is no end token such as "}" for normal functions.
1044 if (scanner()->location().end_pos == end_position) {
1045 // The pre-parser saw an arrow function here, so the full parser
1046 // must produce a FunctionLiteral.
1047 DCHECK(expression->IsFunctionLiteral());
1048 result = expression->AsFunctionLiteral();
1049 }
1050 } else if (IsDefaultConstructor(kind)) {
1051 DCHECK_EQ(scope(), outer);
1052 result = DefaultConstructor(raw_name, IsDerivedConstructor(kind),
1053 start_position, end_position);
1054 } else {
1055 ZonePtrList<const AstRawString>* arguments_for_wrapped_function =
1056 info->is_wrapped_as_function()
1057 ? PrepareWrappedArguments(isolate, info, zone())
1058 : nullptr;
1059 result = ParseFunctionLiteral(
1060 raw_name, Scanner::Location::invalid(), kSkipFunctionNameCheck, kind,
1061 kNoSourcePosition, flags().function_syntax_kind(),
1062 info->language_mode(), arguments_for_wrapped_function);
1063 }
1064
1065 if (has_error()) return nullptr;
1066 result->set_requires_instance_members_initializer(
1067 flags().requires_instance_members_initializer());
1068 result->set_class_scope_has_private_brand(
1069 flags().class_scope_has_private_brand());
1070 result->set_has_static_private_methods_or_accessors(
1071 flags().has_static_private_methods_or_accessors());
1072 }
1073
1074 DCHECK_IMPLIES(result, function_literal_id == result->function_literal_id());
1075 return result;
1076 }
1077
DoParseDeserializedFunction(Isolate * isolate,MaybeHandle<ScopeInfo> maybe_outer_scope_info,ParseInfo * info,int start_position,int end_position,int function_literal_id,const AstRawString * raw_name)1078 FunctionLiteral* Parser::DoParseDeserializedFunction(
1079 Isolate* isolate, MaybeHandle<ScopeInfo> maybe_outer_scope_info,
1080 ParseInfo* info, int start_position, int end_position,
1081 int function_literal_id, const AstRawString* raw_name) {
1082 if (flags().function_kind() ==
1083 FunctionKind::kClassMembersInitializerFunction) {
1084 return ParseClassForInstanceMemberInitialization(
1085 isolate, maybe_outer_scope_info, start_position, function_literal_id,
1086 end_position);
1087 }
1088
1089 return DoParseFunction(isolate, info, start_position, end_position,
1090 function_literal_id, raw_name);
1091 }
1092
ParseClassForInstanceMemberInitialization(Isolate * isolate,MaybeHandle<ScopeInfo> maybe_class_scope_info,int initializer_pos,int initializer_id,int initializer_end_pos)1093 FunctionLiteral* Parser::ParseClassForInstanceMemberInitialization(
1094 Isolate* isolate, MaybeHandle<ScopeInfo> maybe_class_scope_info,
1095 int initializer_pos, int initializer_id, int initializer_end_pos) {
1096 // When the function is a kClassMembersInitializerFunction, we record the
1097 // source range of the entire class as its positions in its SFI, so at this
1098 // point the scanner should be rewound to the position of the class token.
1099 int class_token_pos = initializer_pos;
1100 DCHECK_EQ(peek_position(), class_token_pos);
1101
1102 // Insert a FunctionState with the closest outer Declaration scope
1103 DeclarationScope* nearest_decl_scope = original_scope_->GetDeclarationScope();
1104 DCHECK_NOT_NULL(nearest_decl_scope);
1105 FunctionState function_state(&function_state_, &scope_, nearest_decl_scope);
1106 // We will reindex the function literals later.
1107 ResetFunctionLiteralId();
1108
1109 // We preparse the class members that are not fields with initializers
1110 // in order to collect the function literal ids.
1111 ParsingModeScope mode(this, PARSE_LAZILY);
1112
1113 ExpressionParsingScope no_expression_scope(impl());
1114
1115 // Reparse the class as an expression to build the instance member
1116 // initializer function.
1117 Expression* expr = ParseClassExpression(original_scope_);
1118
1119 DCHECK(expr->IsClassLiteral());
1120 ClassLiteral* literal = expr->AsClassLiteral();
1121 FunctionLiteral* initializer =
1122 literal->instance_members_initializer_function();
1123
1124 // Reindex so that the function literal ids match.
1125 AstFunctionLiteralIdReindexer reindexer(
1126 stack_limit_, initializer_id - initializer->function_literal_id());
1127 reindexer.Reindex(expr);
1128
1129 no_expression_scope.ValidateExpression();
1130
1131 // If the class scope was not optimized away, we know that it allocated
1132 // some variables and we need to fix up the allocation info for them.
1133 bool needs_allocation_fixup =
1134 !maybe_class_scope_info.is_null() &&
1135 maybe_class_scope_info.ToHandleChecked()->scope_type() == CLASS_SCOPE &&
1136 maybe_class_scope_info.ToHandleChecked()->StartPosition() ==
1137 class_token_pos;
1138
1139 ClassScope* reparsed_scope = literal->scope();
1140 reparsed_scope->FinalizeReparsedClassScope(isolate, maybe_class_scope_info,
1141 ast_value_factory(),
1142 needs_allocation_fixup);
1143 original_scope_ = reparsed_scope;
1144
1145 DCHECK_EQ(initializer->kind(),
1146 FunctionKind::kClassMembersInitializerFunction);
1147 DCHECK_EQ(initializer->function_literal_id(), initializer_id);
1148 DCHECK_EQ(initializer->end_position(), initializer_end_pos);
1149
1150 return initializer;
1151 }
1152
ParseModuleItem()1153 Statement* Parser::ParseModuleItem() {
1154 // ecma262/#prod-ModuleItem
1155 // ModuleItem :
1156 // ImportDeclaration
1157 // ExportDeclaration
1158 // StatementListItem
1159
1160 Token::Value next = peek();
1161
1162 if (next == Token::EXPORT) {
1163 return ParseExportDeclaration();
1164 }
1165
1166 if (next == Token::IMPORT) {
1167 // We must be careful not to parse a dynamic import expression as an import
1168 // declaration. Same for import.meta expressions.
1169 Token::Value peek_ahead = PeekAhead();
1170 if (peek_ahead != Token::LPAREN && peek_ahead != Token::PERIOD) {
1171 ParseImportDeclaration();
1172 return factory()->EmptyStatement();
1173 }
1174 }
1175
1176 return ParseStatementListItem();
1177 }
1178
ParseModuleItemList(ScopedPtrList<Statement> * body)1179 void Parser::ParseModuleItemList(ScopedPtrList<Statement>* body) {
1180 // ecma262/#prod-Module
1181 // Module :
1182 // ModuleBody?
1183 //
1184 // ecma262/#prod-ModuleItemList
1185 // ModuleBody :
1186 // ModuleItem*
1187
1188 DCHECK(scope()->is_module_scope());
1189 while (peek() != Token::EOS) {
1190 Statement* stat = ParseModuleItem();
1191 if (stat == nullptr) return;
1192 if (stat->IsEmptyStatement()) continue;
1193 body->Add(stat);
1194 }
1195 }
1196
ParseModuleSpecifier()1197 const AstRawString* Parser::ParseModuleSpecifier() {
1198 // ModuleSpecifier :
1199 // StringLiteral
1200
1201 Expect(Token::STRING);
1202 return GetSymbol();
1203 }
1204
ParseExportClause(Scanner::Location * reserved_loc,Scanner::Location * string_literal_local_name_loc)1205 ZoneChunkList<Parser::ExportClauseData>* Parser::ParseExportClause(
1206 Scanner::Location* reserved_loc,
1207 Scanner::Location* string_literal_local_name_loc) {
1208 // ExportClause :
1209 // '{' '}'
1210 // '{' ExportsList '}'
1211 // '{' ExportsList ',' '}'
1212 //
1213 // ExportsList :
1214 // ExportSpecifier
1215 // ExportsList ',' ExportSpecifier
1216 //
1217 // ExportSpecifier :
1218 // IdentifierName
1219 // IdentifierName 'as' IdentifierName
1220 // IdentifierName 'as' ModuleExportName
1221 // ModuleExportName
1222 // ModuleExportName 'as' ModuleExportName
1223 //
1224 // ModuleExportName :
1225 // StringLiteral
1226 ZoneChunkList<ExportClauseData>* export_data =
1227 zone()->New<ZoneChunkList<ExportClauseData>>(zone());
1228
1229 Expect(Token::LBRACE);
1230
1231 Token::Value name_tok;
1232 while ((name_tok = peek()) != Token::RBRACE) {
1233 const AstRawString* local_name = ParseExportSpecifierName();
1234 if (!string_literal_local_name_loc->IsValid() &&
1235 name_tok == Token::STRING) {
1236 // Keep track of the first string literal local name exported for error
1237 // reporting. These must be followed by a 'from' clause.
1238 *string_literal_local_name_loc = scanner()->location();
1239 } else if (!reserved_loc->IsValid() &&
1240 !Token::IsValidIdentifier(name_tok, LanguageMode::kStrict, false,
1241 flags().is_module())) {
1242 // Keep track of the first reserved word encountered in case our
1243 // caller needs to report an error.
1244 *reserved_loc = scanner()->location();
1245 }
1246 const AstRawString* export_name;
1247 Scanner::Location location = scanner()->location();
1248 if (CheckContextualKeyword(ast_value_factory()->as_string())) {
1249 export_name = ParseExportSpecifierName();
1250 // Set the location to the whole "a as b" string, so that it makes sense
1251 // both for errors due to "a" and for errors due to "b".
1252 location.end_pos = scanner()->location().end_pos;
1253 } else {
1254 export_name = local_name;
1255 }
1256 export_data->push_back({export_name, local_name, location});
1257 if (peek() == Token::RBRACE) break;
1258 if (V8_UNLIKELY(!Check(Token::COMMA))) {
1259 ReportUnexpectedToken(Next());
1260 break;
1261 }
1262 }
1263
1264 Expect(Token::RBRACE);
1265 return export_data;
1266 }
1267
ParseExportSpecifierName()1268 const AstRawString* Parser::ParseExportSpecifierName() {
1269 Token::Value next = Next();
1270
1271 // IdentifierName
1272 if (V8_LIKELY(Token::IsPropertyName(next))) {
1273 return GetSymbol();
1274 }
1275
1276 // ModuleExportName
1277 if (next == Token::STRING) {
1278 const AstRawString* export_name = GetSymbol();
1279 if (V8_LIKELY(export_name->is_one_byte())) return export_name;
1280 if (!unibrow::Utf16::HasUnpairedSurrogate(
1281 reinterpret_cast<const uint16_t*>(export_name->raw_data()),
1282 export_name->length())) {
1283 return export_name;
1284 }
1285 ReportMessage(MessageTemplate::kInvalidModuleExportName);
1286 return EmptyIdentifierString();
1287 }
1288
1289 ReportUnexpectedToken(next);
1290 return EmptyIdentifierString();
1291 }
1292
ParseNamedImports(int pos)1293 ZonePtrList<const Parser::NamedImport>* Parser::ParseNamedImports(int pos) {
1294 // NamedImports :
1295 // '{' '}'
1296 // '{' ImportsList '}'
1297 // '{' ImportsList ',' '}'
1298 //
1299 // ImportsList :
1300 // ImportSpecifier
1301 // ImportsList ',' ImportSpecifier
1302 //
1303 // ImportSpecifier :
1304 // BindingIdentifier
1305 // IdentifierName 'as' BindingIdentifier
1306 // ModuleExportName 'as' BindingIdentifier
1307
1308 Expect(Token::LBRACE);
1309
1310 auto result = zone()->New<ZonePtrList<const NamedImport>>(1, zone());
1311 while (peek() != Token::RBRACE) {
1312 const AstRawString* import_name = ParseExportSpecifierName();
1313 const AstRawString* local_name = import_name;
1314 Scanner::Location location = scanner()->location();
1315 // In the presence of 'as', the left-side of the 'as' can
1316 // be any IdentifierName. But without 'as', it must be a valid
1317 // BindingIdentifier.
1318 if (CheckContextualKeyword(ast_value_factory()->as_string())) {
1319 local_name = ParsePropertyName();
1320 }
1321 if (!Token::IsValidIdentifier(scanner()->current_token(),
1322 LanguageMode::kStrict, false,
1323 flags().is_module())) {
1324 ReportMessage(MessageTemplate::kUnexpectedReserved);
1325 return nullptr;
1326 } else if (IsEvalOrArguments(local_name)) {
1327 ReportMessage(MessageTemplate::kStrictEvalArguments);
1328 return nullptr;
1329 }
1330
1331 DeclareUnboundVariable(local_name, VariableMode::kConst,
1332 kNeedsInitialization, position());
1333
1334 NamedImport* import =
1335 zone()->New<NamedImport>(import_name, local_name, location);
1336 result->Add(import, zone());
1337
1338 if (peek() == Token::RBRACE) break;
1339 Expect(Token::COMMA);
1340 }
1341
1342 Expect(Token::RBRACE);
1343 return result;
1344 }
1345
ParseImportAssertClause()1346 ImportAssertions* Parser::ParseImportAssertClause() {
1347 // AssertClause :
1348 // assert '{' '}'
1349 // assert '{' AssertEntries '}'
1350
1351 // AssertEntries :
1352 // IdentifierName: AssertionKey
1353 // IdentifierName: AssertionKey , AssertEntries
1354
1355 // AssertionKey :
1356 // IdentifierName
1357 // StringLiteral
1358
1359 auto import_assertions = zone()->New<ImportAssertions>(zone());
1360
1361 if (!FLAG_harmony_import_assertions) {
1362 return import_assertions;
1363 }
1364
1365 // Assert clause is optional, and cannot be preceded by a LineTerminator.
1366 if (scanner()->HasLineTerminatorBeforeNext() ||
1367 !CheckContextualKeyword(ast_value_factory()->assert_string())) {
1368 return import_assertions;
1369 }
1370
1371 Expect(Token::LBRACE);
1372
1373 while (peek() != Token::RBRACE) {
1374 const AstRawString* attribute_key = nullptr;
1375 if (Check(Token::STRING)) {
1376 attribute_key = GetSymbol();
1377 } else {
1378 attribute_key = ParsePropertyName();
1379 }
1380
1381 Scanner::Location location = scanner()->location();
1382
1383 Expect(Token::COLON);
1384 Expect(Token::STRING);
1385
1386 const AstRawString* attribute_value = GetSymbol();
1387
1388 // Set the location to the whole "key: 'value'"" string, so that it makes
1389 // sense both for errors due to the key and errors due to the value.
1390 location.end_pos = scanner()->location().end_pos;
1391
1392 auto result = import_assertions->insert(std::make_pair(
1393 attribute_key, std::make_pair(attribute_value, location)));
1394 if (!result.second) {
1395 // It is a syntax error if two AssertEntries have the same key.
1396 ReportMessageAt(location, MessageTemplate::kImportAssertionDuplicateKey,
1397 attribute_key);
1398 break;
1399 }
1400
1401 if (peek() == Token::RBRACE) break;
1402 if (V8_UNLIKELY(!Check(Token::COMMA))) {
1403 ReportUnexpectedToken(Next());
1404 break;
1405 }
1406 }
1407
1408 Expect(Token::RBRACE);
1409
1410 return import_assertions;
1411 }
1412
ParseImportDeclaration()1413 void Parser::ParseImportDeclaration() {
1414 // ImportDeclaration :
1415 // 'import' ImportClause 'from' ModuleSpecifier ';'
1416 // 'import' ModuleSpecifier ';'
1417 // 'import' ImportClause 'from' ModuleSpecifier [no LineTerminator here]
1418 // AssertClause ';'
1419 // 'import' ModuleSpecifier [no LineTerminator here] AssertClause';'
1420 //
1421 // ImportClause :
1422 // ImportedDefaultBinding
1423 // NameSpaceImport
1424 // NamedImports
1425 // ImportedDefaultBinding ',' NameSpaceImport
1426 // ImportedDefaultBinding ',' NamedImports
1427 //
1428 // NameSpaceImport :
1429 // '*' 'as' ImportedBinding
1430
1431 int pos = peek_position();
1432 Expect(Token::IMPORT);
1433
1434 Token::Value tok = peek();
1435
1436 // 'import' ModuleSpecifier ';'
1437 if (tok == Token::STRING) {
1438 Scanner::Location specifier_loc = scanner()->peek_location();
1439 const AstRawString* module_specifier = ParseModuleSpecifier();
1440 const ImportAssertions* import_assertions = ParseImportAssertClause();
1441 ExpectSemicolon();
1442 module()->AddEmptyImport(module_specifier, import_assertions, specifier_loc,
1443 zone());
1444 return;
1445 }
1446
1447 // Parse ImportedDefaultBinding if present.
1448 const AstRawString* import_default_binding = nullptr;
1449 Scanner::Location import_default_binding_loc;
1450 if (tok != Token::MUL && tok != Token::LBRACE) {
1451 import_default_binding = ParseNonRestrictedIdentifier();
1452 import_default_binding_loc = scanner()->location();
1453 DeclareUnboundVariable(import_default_binding, VariableMode::kConst,
1454 kNeedsInitialization, pos);
1455 }
1456
1457 // Parse NameSpaceImport or NamedImports if present.
1458 const AstRawString* module_namespace_binding = nullptr;
1459 Scanner::Location module_namespace_binding_loc;
1460 const ZonePtrList<const NamedImport>* named_imports = nullptr;
1461 if (import_default_binding == nullptr || Check(Token::COMMA)) {
1462 switch (peek()) {
1463 case Token::MUL: {
1464 Consume(Token::MUL);
1465 ExpectContextualKeyword(ast_value_factory()->as_string());
1466 module_namespace_binding = ParseNonRestrictedIdentifier();
1467 module_namespace_binding_loc = scanner()->location();
1468 DeclareUnboundVariable(module_namespace_binding, VariableMode::kConst,
1469 kCreatedInitialized, pos);
1470 break;
1471 }
1472
1473 case Token::LBRACE:
1474 named_imports = ParseNamedImports(pos);
1475 break;
1476
1477 default:
1478 ReportUnexpectedToken(scanner()->current_token());
1479 return;
1480 }
1481 }
1482
1483 ExpectContextualKeyword(ast_value_factory()->from_string());
1484 Scanner::Location specifier_loc = scanner()->peek_location();
1485 const AstRawString* module_specifier = ParseModuleSpecifier();
1486 const ImportAssertions* import_assertions = ParseImportAssertClause();
1487 ExpectSemicolon();
1488
1489 // Now that we have all the information, we can make the appropriate
1490 // declarations.
1491
1492 // TODO(neis): Would prefer to call DeclareVariable for each case below rather
1493 // than above and in ParseNamedImports, but then a possible error message
1494 // would point to the wrong location. Maybe have a DeclareAt version of
1495 // Declare that takes a location?
1496
1497 if (module_namespace_binding != nullptr) {
1498 module()->AddStarImport(module_namespace_binding, module_specifier,
1499 import_assertions, module_namespace_binding_loc,
1500 specifier_loc, zone());
1501 }
1502
1503 if (import_default_binding != nullptr) {
1504 module()->AddImport(ast_value_factory()->default_string(),
1505 import_default_binding, module_specifier,
1506 import_assertions, import_default_binding_loc,
1507 specifier_loc, zone());
1508 }
1509
1510 if (named_imports != nullptr) {
1511 if (named_imports->length() == 0) {
1512 module()->AddEmptyImport(module_specifier, import_assertions,
1513 specifier_loc, zone());
1514 } else {
1515 for (const NamedImport* import : *named_imports) {
1516 module()->AddImport(import->import_name, import->local_name,
1517 module_specifier, import_assertions,
1518 import->location, specifier_loc, zone());
1519 }
1520 }
1521 }
1522 }
1523
ParseExportDefault()1524 Statement* Parser::ParseExportDefault() {
1525 // Supports the following productions, starting after the 'default' token:
1526 // 'export' 'default' HoistableDeclaration
1527 // 'export' 'default' ClassDeclaration
1528 // 'export' 'default' AssignmentExpression[In] ';'
1529
1530 Expect(Token::DEFAULT);
1531 Scanner::Location default_loc = scanner()->location();
1532
1533 ZonePtrList<const AstRawString> local_names(1, zone());
1534 Statement* result = nullptr;
1535 switch (peek()) {
1536 case Token::FUNCTION:
1537 result = ParseHoistableDeclaration(&local_names, true);
1538 break;
1539
1540 case Token::CLASS:
1541 Consume(Token::CLASS);
1542 result = ParseClassDeclaration(&local_names, true);
1543 break;
1544
1545 case Token::ASYNC:
1546 if (PeekAhead() == Token::FUNCTION &&
1547 !scanner()->HasLineTerminatorAfterNext()) {
1548 Consume(Token::ASYNC);
1549 result = ParseAsyncFunctionDeclaration(&local_names, true);
1550 break;
1551 }
1552 V8_FALLTHROUGH;
1553
1554 default: {
1555 int pos = position();
1556 AcceptINScope scope(this, true);
1557 Expression* value = ParseAssignmentExpression();
1558 SetFunctionName(value, ast_value_factory()->default_string());
1559
1560 const AstRawString* local_name =
1561 ast_value_factory()->dot_default_string();
1562 local_names.Add(local_name, zone());
1563
1564 // It's fine to declare this as VariableMode::kConst because the user has
1565 // no way of writing to it.
1566 VariableProxy* proxy =
1567 DeclareBoundVariable(local_name, VariableMode::kConst, pos);
1568 proxy->var()->set_initializer_position(position());
1569
1570 Assignment* assignment = factory()->NewAssignment(
1571 Token::INIT, proxy, value, kNoSourcePosition);
1572 result = IgnoreCompletion(
1573 factory()->NewExpressionStatement(assignment, kNoSourcePosition));
1574
1575 ExpectSemicolon();
1576 break;
1577 }
1578 }
1579
1580 if (result != nullptr) {
1581 DCHECK_EQ(local_names.length(), 1);
1582 module()->AddExport(local_names.first(),
1583 ast_value_factory()->default_string(), default_loc,
1584 zone());
1585 }
1586
1587 return result;
1588 }
1589
NextInternalNamespaceExportName()1590 const AstRawString* Parser::NextInternalNamespaceExportName() {
1591 const char* prefix = ".ns-export";
1592 std::string s(prefix);
1593 s.append(std::to_string(number_of_named_namespace_exports_++));
1594 return ast_value_factory()->GetOneByteString(s.c_str());
1595 }
1596
ParseExportStar()1597 void Parser::ParseExportStar() {
1598 int pos = position();
1599 Consume(Token::MUL);
1600
1601 if (!PeekContextualKeyword(ast_value_factory()->as_string())) {
1602 // 'export' '*' 'from' ModuleSpecifier ';'
1603 Scanner::Location loc = scanner()->location();
1604 ExpectContextualKeyword(ast_value_factory()->from_string());
1605 Scanner::Location specifier_loc = scanner()->peek_location();
1606 const AstRawString* module_specifier = ParseModuleSpecifier();
1607 const ImportAssertions* import_assertions = ParseImportAssertClause();
1608 ExpectSemicolon();
1609 module()->AddStarExport(module_specifier, import_assertions, loc,
1610 specifier_loc, zone());
1611 return;
1612 }
1613
1614 // 'export' '*' 'as' IdentifierName 'from' ModuleSpecifier ';'
1615 //
1616 // Desugaring:
1617 // export * as x from "...";
1618 // ~>
1619 // import * as .x from "..."; export {.x as x};
1620 //
1621 // Note that the desugared internal namespace export name (.x above) will
1622 // never conflict with a string literal export name, as literal string export
1623 // names in local name positions (i.e. left of 'as' or in a clause without
1624 // 'as') are disallowed without a following 'from' clause.
1625
1626 ExpectContextualKeyword(ast_value_factory()->as_string());
1627 const AstRawString* export_name = ParseExportSpecifierName();
1628 Scanner::Location export_name_loc = scanner()->location();
1629 const AstRawString* local_name = NextInternalNamespaceExportName();
1630 Scanner::Location local_name_loc = Scanner::Location::invalid();
1631 DeclareUnboundVariable(local_name, VariableMode::kConst, kCreatedInitialized,
1632 pos);
1633
1634 ExpectContextualKeyword(ast_value_factory()->from_string());
1635 Scanner::Location specifier_loc = scanner()->peek_location();
1636 const AstRawString* module_specifier = ParseModuleSpecifier();
1637 const ImportAssertions* import_assertions = ParseImportAssertClause();
1638 ExpectSemicolon();
1639
1640 module()->AddStarImport(local_name, module_specifier, import_assertions,
1641 local_name_loc, specifier_loc, zone());
1642 module()->AddExport(local_name, export_name, export_name_loc, zone());
1643 }
1644
ParseExportDeclaration()1645 Statement* Parser::ParseExportDeclaration() {
1646 // ExportDeclaration:
1647 // 'export' '*' 'from' ModuleSpecifier ';'
1648 // 'export' '*' 'from' ModuleSpecifier [no LineTerminator here]
1649 // AssertClause ';'
1650 // 'export' '*' 'as' IdentifierName 'from' ModuleSpecifier ';'
1651 // 'export' '*' 'as' IdentifierName 'from' ModuleSpecifier
1652 // [no LineTerminator here] AssertClause ';'
1653 // 'export' '*' 'as' ModuleExportName 'from' ModuleSpecifier ';'
1654 // 'export' '*' 'as' ModuleExportName 'from' ModuleSpecifier ';'
1655 // [no LineTerminator here] AssertClause ';'
1656 // 'export' ExportClause ('from' ModuleSpecifier)? ';'
1657 // 'export' ExportClause ('from' ModuleSpecifier [no LineTerminator here]
1658 // AssertClause)? ';'
1659 // 'export' VariableStatement
1660 // 'export' Declaration
1661 // 'export' 'default' ... (handled in ParseExportDefault)
1662 //
1663 // ModuleExportName :
1664 // StringLiteral
1665
1666 Expect(Token::EXPORT);
1667 Statement* result = nullptr;
1668 ZonePtrList<const AstRawString> names(1, zone());
1669 Scanner::Location loc = scanner()->peek_location();
1670 switch (peek()) {
1671 case Token::DEFAULT:
1672 return ParseExportDefault();
1673
1674 case Token::MUL:
1675 ParseExportStar();
1676 return factory()->EmptyStatement();
1677
1678 case Token::LBRACE: {
1679 // There are two cases here:
1680 //
1681 // 'export' ExportClause ';'
1682 // and
1683 // 'export' ExportClause FromClause ';'
1684 //
1685 // In the first case, the exported identifiers in ExportClause must
1686 // not be reserved words, while in the latter they may be. We
1687 // pass in a location that gets filled with the first reserved word
1688 // encountered, and then throw a SyntaxError if we are in the
1689 // non-FromClause case.
1690 Scanner::Location reserved_loc = Scanner::Location::invalid();
1691 Scanner::Location string_literal_local_name_loc =
1692 Scanner::Location::invalid();
1693 ZoneChunkList<ExportClauseData>* export_data =
1694 ParseExportClause(&reserved_loc, &string_literal_local_name_loc);
1695 if (CheckContextualKeyword(ast_value_factory()->from_string())) {
1696 Scanner::Location specifier_loc = scanner()->peek_location();
1697 const AstRawString* module_specifier = ParseModuleSpecifier();
1698 const ImportAssertions* import_assertions = ParseImportAssertClause();
1699 ExpectSemicolon();
1700
1701 if (export_data->is_empty()) {
1702 module()->AddEmptyImport(module_specifier, import_assertions,
1703 specifier_loc, zone());
1704 } else {
1705 for (const ExportClauseData& data : *export_data) {
1706 module()->AddExport(data.local_name, data.export_name,
1707 module_specifier, import_assertions,
1708 data.location, specifier_loc, zone());
1709 }
1710 }
1711 } else {
1712 if (reserved_loc.IsValid()) {
1713 // No FromClause, so reserved words are invalid in ExportClause.
1714 ReportMessageAt(reserved_loc, MessageTemplate::kUnexpectedReserved);
1715 return nullptr;
1716 } else if (string_literal_local_name_loc.IsValid()) {
1717 ReportMessageAt(string_literal_local_name_loc,
1718 MessageTemplate::kModuleExportNameWithoutFromClause);
1719 return nullptr;
1720 }
1721
1722 ExpectSemicolon();
1723
1724 for (const ExportClauseData& data : *export_data) {
1725 module()->AddExport(data.local_name, data.export_name, data.location,
1726 zone());
1727 }
1728 }
1729 return factory()->EmptyStatement();
1730 }
1731
1732 case Token::FUNCTION:
1733 result = ParseHoistableDeclaration(&names, false);
1734 break;
1735
1736 case Token::CLASS:
1737 Consume(Token::CLASS);
1738 result = ParseClassDeclaration(&names, false);
1739 break;
1740
1741 case Token::VAR:
1742 case Token::LET:
1743 case Token::CONST:
1744 result = ParseVariableStatement(kStatementListItem, &names);
1745 break;
1746
1747 case Token::ASYNC:
1748 Consume(Token::ASYNC);
1749 if (peek() == Token::FUNCTION &&
1750 !scanner()->HasLineTerminatorBeforeNext()) {
1751 result = ParseAsyncFunctionDeclaration(&names, false);
1752 break;
1753 }
1754 V8_FALLTHROUGH;
1755
1756 default:
1757 ReportUnexpectedToken(scanner()->current_token());
1758 return nullptr;
1759 }
1760 loc.end_pos = scanner()->location().end_pos;
1761
1762 SourceTextModuleDescriptor* descriptor = module();
1763 for (const AstRawString* name : names) {
1764 descriptor->AddExport(name, name, loc, zone());
1765 }
1766
1767 return result;
1768 }
1769
DeclareUnboundVariable(const AstRawString * name,VariableMode mode,InitializationFlag init,int pos)1770 void Parser::DeclareUnboundVariable(const AstRawString* name, VariableMode mode,
1771 InitializationFlag init, int pos) {
1772 bool was_added;
1773 Variable* var = DeclareVariable(name, NORMAL_VARIABLE, mode, init, scope(),
1774 &was_added, pos, end_position());
1775 // The variable will be added to the declarations list, but since we are not
1776 // binding it to anything, we can simply ignore it here.
1777 USE(var);
1778 }
1779
DeclareBoundVariable(const AstRawString * name,VariableMode mode,int pos)1780 VariableProxy* Parser::DeclareBoundVariable(const AstRawString* name,
1781 VariableMode mode, int pos) {
1782 DCHECK_NOT_NULL(name);
1783 VariableProxy* proxy =
1784 factory()->NewVariableProxy(name, NORMAL_VARIABLE, position());
1785 bool was_added;
1786 Variable* var = DeclareVariable(name, NORMAL_VARIABLE, mode,
1787 Variable::DefaultInitializationFlag(mode),
1788 scope(), &was_added, pos, end_position());
1789 proxy->BindTo(var);
1790 return proxy;
1791 }
1792
DeclareAndBindVariable(VariableProxy * proxy,VariableKind kind,VariableMode mode,Scope * scope,bool * was_added,int initializer_position)1793 void Parser::DeclareAndBindVariable(VariableProxy* proxy, VariableKind kind,
1794 VariableMode mode, Scope* scope,
1795 bool* was_added, int initializer_position) {
1796 Variable* var = DeclareVariable(
1797 proxy->raw_name(), kind, mode, Variable::DefaultInitializationFlag(mode),
1798 scope, was_added, proxy->position(), kNoSourcePosition);
1799 var->set_initializer_position(initializer_position);
1800 proxy->BindTo(var);
1801 }
1802
DeclareVariable(const AstRawString * name,VariableKind kind,VariableMode mode,InitializationFlag init,Scope * scope,bool * was_added,int begin,int end)1803 Variable* Parser::DeclareVariable(const AstRawString* name, VariableKind kind,
1804 VariableMode mode, InitializationFlag init,
1805 Scope* scope, bool* was_added, int begin,
1806 int end) {
1807 Declaration* declaration;
1808 if (mode == VariableMode::kVar && !scope->is_declaration_scope()) {
1809 DCHECK(scope->is_block_scope() || scope->is_with_scope());
1810 declaration = factory()->NewNestedVariableDeclaration(scope, begin);
1811 } else {
1812 declaration = factory()->NewVariableDeclaration(begin);
1813 }
1814 Declare(declaration, name, kind, mode, init, scope, was_added, begin, end);
1815 return declaration->var();
1816 }
1817
Declare(Declaration * declaration,const AstRawString * name,VariableKind variable_kind,VariableMode mode,InitializationFlag init,Scope * scope,bool * was_added,int var_begin_pos,int var_end_pos)1818 void Parser::Declare(Declaration* declaration, const AstRawString* name,
1819 VariableKind variable_kind, VariableMode mode,
1820 InitializationFlag init, Scope* scope, bool* was_added,
1821 int var_begin_pos, int var_end_pos) {
1822 bool local_ok = true;
1823 bool sloppy_mode_block_scope_function_redefinition = false;
1824 scope->DeclareVariable(
1825 declaration, name, var_begin_pos, mode, variable_kind, init, was_added,
1826 &sloppy_mode_block_scope_function_redefinition, &local_ok);
1827 if (!local_ok) {
1828 // If we only have the start position of a proxy, we can't highlight the
1829 // whole variable name. Pretend its length is 1 so that we highlight at
1830 // least the first character.
1831 Scanner::Location loc(var_begin_pos, var_end_pos != kNoSourcePosition
1832 ? var_end_pos
1833 : var_begin_pos + 1);
1834 if (variable_kind == PARAMETER_VARIABLE) {
1835 ReportMessageAt(loc, MessageTemplate::kParamDupe);
1836 } else {
1837 ReportMessageAt(loc, MessageTemplate::kVarRedeclaration,
1838 declaration->var()->raw_name());
1839 }
1840 } else if (sloppy_mode_block_scope_function_redefinition) {
1841 ++use_counts_[v8::Isolate::kSloppyModeBlockScopedFunctionRedefinition];
1842 }
1843 }
1844
BuildInitializationBlock(DeclarationParsingResult * parsing_result)1845 Statement* Parser::BuildInitializationBlock(
1846 DeclarationParsingResult* parsing_result) {
1847 ScopedPtrList<Statement> statements(pointer_buffer());
1848 for (const auto& declaration : parsing_result->declarations) {
1849 if (!declaration.initializer) continue;
1850 InitializeVariables(&statements, parsing_result->descriptor.kind,
1851 &declaration);
1852 }
1853 return factory()->NewBlock(true, statements);
1854 }
1855
DeclareFunction(const AstRawString * variable_name,FunctionLiteral * function,VariableMode mode,VariableKind kind,int beg_pos,int end_pos,ZonePtrList<const AstRawString> * names)1856 Statement* Parser::DeclareFunction(const AstRawString* variable_name,
1857 FunctionLiteral* function, VariableMode mode,
1858 VariableKind kind, int beg_pos, int end_pos,
1859 ZonePtrList<const AstRawString>* names) {
1860 Declaration* declaration =
1861 factory()->NewFunctionDeclaration(function, beg_pos);
1862 bool was_added;
1863 Declare(declaration, variable_name, kind, mode, kCreatedInitialized, scope(),
1864 &was_added, beg_pos);
1865 if (info()->flags().coverage_enabled()) {
1866 // Force the function to be allocated when collecting source coverage, so
1867 // that even dead functions get source coverage data.
1868 declaration->var()->set_is_used();
1869 }
1870 if (names) names->Add(variable_name, zone());
1871 if (kind == SLOPPY_BLOCK_FUNCTION_VARIABLE) {
1872 Token::Value init = loop_nesting_depth() > 0 ? Token::ASSIGN : Token::INIT;
1873 SloppyBlockFunctionStatement* statement =
1874 factory()->NewSloppyBlockFunctionStatement(end_pos, declaration->var(),
1875 init);
1876 GetDeclarationScope()->DeclareSloppyBlockFunction(statement);
1877 return statement;
1878 }
1879 return factory()->EmptyStatement();
1880 }
1881
DeclareClass(const AstRawString * variable_name,Expression * value,ZonePtrList<const AstRawString> * names,int class_token_pos,int end_pos)1882 Statement* Parser::DeclareClass(const AstRawString* variable_name,
1883 Expression* value,
1884 ZonePtrList<const AstRawString>* names,
1885 int class_token_pos, int end_pos) {
1886 VariableProxy* proxy =
1887 DeclareBoundVariable(variable_name, VariableMode::kLet, class_token_pos);
1888 proxy->var()->set_initializer_position(end_pos);
1889 if (names) names->Add(variable_name, zone());
1890
1891 Assignment* assignment =
1892 factory()->NewAssignment(Token::INIT, proxy, value, class_token_pos);
1893 return IgnoreCompletion(
1894 factory()->NewExpressionStatement(assignment, kNoSourcePosition));
1895 }
1896
DeclareNative(const AstRawString * name,int pos)1897 Statement* Parser::DeclareNative(const AstRawString* name, int pos) {
1898 // Make sure that the function containing the native declaration
1899 // isn't lazily compiled. The extension structures are only
1900 // accessible while parsing the first time not when reparsing
1901 // because of lazy compilation.
1902 GetClosureScope()->ForceEagerCompilation();
1903
1904 // TODO(1240846): It's weird that native function declarations are
1905 // introduced dynamically when we meet their declarations, whereas
1906 // other functions are set up when entering the surrounding scope.
1907 VariableProxy* proxy = DeclareBoundVariable(name, VariableMode::kVar, pos);
1908 NativeFunctionLiteral* lit =
1909 factory()->NewNativeFunctionLiteral(name, extension(), kNoSourcePosition);
1910 return factory()->NewExpressionStatement(
1911 factory()->NewAssignment(Token::INIT, proxy, lit, kNoSourcePosition),
1912 pos);
1913 }
1914
IgnoreCompletion(Statement * statement)1915 Block* Parser::IgnoreCompletion(Statement* statement) {
1916 Block* block = factory()->NewBlock(1, true);
1917 block->statements()->Add(statement, zone());
1918 return block;
1919 }
1920
RewriteReturn(Expression * return_value,int pos)1921 Expression* Parser::RewriteReturn(Expression* return_value, int pos) {
1922 if (IsDerivedConstructor(function_state_->kind())) {
1923 // For subclass constructors we need to return this in case of undefined;
1924 // other primitive values trigger an exception in the ConstructStub.
1925 //
1926 // return expr;
1927 //
1928 // Is rewritten as:
1929 //
1930 // return (temp = expr) === undefined ? this : temp;
1931
1932 // temp = expr
1933 Variable* temp = NewTemporary(ast_value_factory()->empty_string());
1934 Assignment* assign = factory()->NewAssignment(
1935 Token::ASSIGN, factory()->NewVariableProxy(temp), return_value, pos);
1936
1937 // temp === undefined
1938 Expression* is_undefined = factory()->NewCompareOperation(
1939 Token::EQ_STRICT, assign,
1940 factory()->NewUndefinedLiteral(kNoSourcePosition), pos);
1941
1942 // is_undefined ? this : temp
1943 // We don't need to call UseThis() since it's guaranteed to be called
1944 // for derived constructors after parsing the constructor in
1945 // ParseFunctionBody.
1946 return_value =
1947 factory()->NewConditional(is_undefined, factory()->ThisExpression(),
1948 factory()->NewVariableProxy(temp), pos);
1949 }
1950 return return_value;
1951 }
1952
RewriteSwitchStatement(SwitchStatement * switch_statement,Scope * scope)1953 Statement* Parser::RewriteSwitchStatement(SwitchStatement* switch_statement,
1954 Scope* scope) {
1955 // In order to get the CaseClauses to execute in their own lexical scope,
1956 // but without requiring downstream code to have special scope handling
1957 // code for switch statements, desugar into blocks as follows:
1958 // { // To group the statements--harmless to evaluate Expression in scope
1959 // .tag_variable = Expression;
1960 // { // To give CaseClauses a scope
1961 // switch (.tag_variable) { CaseClause* }
1962 // }
1963 // }
1964 DCHECK_NOT_NULL(scope);
1965 DCHECK(scope->is_block_scope());
1966 DCHECK_GE(switch_statement->position(), scope->start_position());
1967 DCHECK_LT(switch_statement->position(), scope->end_position());
1968
1969 Block* switch_block = factory()->NewBlock(2, false);
1970
1971 Expression* tag = switch_statement->tag();
1972 Variable* tag_variable =
1973 NewTemporary(ast_value_factory()->dot_switch_tag_string());
1974 Assignment* tag_assign = factory()->NewAssignment(
1975 Token::ASSIGN, factory()->NewVariableProxy(tag_variable), tag,
1976 tag->position());
1977 // Wrap with IgnoreCompletion so the tag isn't returned as the completion
1978 // value, in case the switch statements don't have a value.
1979 Statement* tag_statement = IgnoreCompletion(
1980 factory()->NewExpressionStatement(tag_assign, kNoSourcePosition));
1981 switch_block->statements()->Add(tag_statement, zone());
1982
1983 switch_statement->set_tag(factory()->NewVariableProxy(tag_variable));
1984 Block* cases_block = factory()->NewBlock(1, false);
1985 cases_block->statements()->Add(switch_statement, zone());
1986 cases_block->set_scope(scope);
1987 switch_block->statements()->Add(cases_block, zone());
1988 return switch_block;
1989 }
1990
InitializeVariables(ScopedPtrList<Statement> * statements,VariableKind kind,const DeclarationParsingResult::Declaration * declaration)1991 void Parser::InitializeVariables(
1992 ScopedPtrList<Statement>* statements, VariableKind kind,
1993 const DeclarationParsingResult::Declaration* declaration) {
1994 if (has_error()) return;
1995
1996 DCHECK_NOT_NULL(declaration->initializer);
1997
1998 int pos = declaration->value_beg_pos;
1999 if (pos == kNoSourcePosition) {
2000 pos = declaration->initializer->position();
2001 }
2002 Assignment* assignment = factory()->NewAssignment(
2003 Token::INIT, declaration->pattern, declaration->initializer, pos);
2004 statements->Add(factory()->NewExpressionStatement(assignment, pos));
2005 }
2006
RewriteCatchPattern(CatchInfo * catch_info)2007 Block* Parser::RewriteCatchPattern(CatchInfo* catch_info) {
2008 DCHECK_NOT_NULL(catch_info->pattern);
2009
2010 DeclarationParsingResult::Declaration decl(
2011 catch_info->pattern, factory()->NewVariableProxy(catch_info->variable));
2012
2013 ScopedPtrList<Statement> init_statements(pointer_buffer());
2014 InitializeVariables(&init_statements, NORMAL_VARIABLE, &decl);
2015 return factory()->NewBlock(true, init_statements);
2016 }
2017
ReportVarRedeclarationIn(const AstRawString * name,Scope * scope)2018 void Parser::ReportVarRedeclarationIn(const AstRawString* name, Scope* scope) {
2019 for (Declaration* decl : *scope->declarations()) {
2020 if (decl->var()->raw_name() == name) {
2021 int position = decl->position();
2022 Scanner::Location location =
2023 position == kNoSourcePosition
2024 ? Scanner::Location::invalid()
2025 : Scanner::Location(position, position + name->length());
2026 ReportMessageAt(location, MessageTemplate::kVarRedeclaration, name);
2027 return;
2028 }
2029 }
2030 UNREACHABLE();
2031 }
2032
RewriteTryStatement(Block * try_block,Block * catch_block,const SourceRange & catch_range,Block * finally_block,const SourceRange & finally_range,const CatchInfo & catch_info,int pos)2033 Statement* Parser::RewriteTryStatement(Block* try_block, Block* catch_block,
2034 const SourceRange& catch_range,
2035 Block* finally_block,
2036 const SourceRange& finally_range,
2037 const CatchInfo& catch_info, int pos) {
2038 // Simplify the AST nodes by converting:
2039 // 'try B0 catch B1 finally B2'
2040 // to:
2041 // 'try { try B0 catch B1 } finally B2'
2042
2043 if (catch_block != nullptr && finally_block != nullptr) {
2044 // If we have both, create an inner try/catch.
2045 TryCatchStatement* statement;
2046 statement = factory()->NewTryCatchStatement(try_block, catch_info.scope,
2047 catch_block, kNoSourcePosition);
2048 RecordTryCatchStatementSourceRange(statement, catch_range);
2049
2050 try_block = factory()->NewBlock(1, false);
2051 try_block->statements()->Add(statement, zone());
2052 catch_block = nullptr; // Clear to indicate it's been handled.
2053 }
2054
2055 if (catch_block != nullptr) {
2056 DCHECK_NULL(finally_block);
2057 TryCatchStatement* stmt = factory()->NewTryCatchStatement(
2058 try_block, catch_info.scope, catch_block, pos);
2059 RecordTryCatchStatementSourceRange(stmt, catch_range);
2060 return stmt;
2061 } else {
2062 DCHECK_NOT_NULL(finally_block);
2063 TryFinallyStatement* stmt =
2064 factory()->NewTryFinallyStatement(try_block, finally_block, pos);
2065 RecordTryFinallyStatementSourceRange(stmt, finally_range);
2066 return stmt;
2067 }
2068 }
2069
ParseAndRewriteGeneratorFunctionBody(int pos,FunctionKind kind,ScopedPtrList<Statement> * body)2070 void Parser::ParseAndRewriteGeneratorFunctionBody(
2071 int pos, FunctionKind kind, ScopedPtrList<Statement>* body) {
2072 // For ES6 Generators, we just prepend the initial yield.
2073 Expression* initial_yield = BuildInitialYield(pos, kind);
2074 body->Add(
2075 factory()->NewExpressionStatement(initial_yield, kNoSourcePosition));
2076 ParseStatementList(body, Token::RBRACE);
2077 }
2078
ParseAndRewriteAsyncGeneratorFunctionBody(int pos,FunctionKind kind,ScopedPtrList<Statement> * body)2079 void Parser::ParseAndRewriteAsyncGeneratorFunctionBody(
2080 int pos, FunctionKind kind, ScopedPtrList<Statement>* body) {
2081 // For ES2017 Async Generators, we produce:
2082 //
2083 // try {
2084 // InitialYield;
2085 // ...body...;
2086 // // fall through to the implicit return after the try-finally
2087 // } catch (.catch) {
2088 // %AsyncGeneratorReject(generator, .catch);
2089 // } finally {
2090 // %_GeneratorClose(generator);
2091 // }
2092 //
2093 // - InitialYield yields the actual generator object.
2094 // - Any return statement inside the body will have its argument wrapped
2095 // in an iterator result object with a "done" property set to `true`.
2096 // - If the generator terminates for whatever reason, we must close it.
2097 // Hence the finally clause.
2098 // - BytecodeGenerator performs special handling for ReturnStatements in
2099 // async generator functions, resolving the appropriate Promise with an
2100 // "done" iterator result object containing a Promise-unwrapped value.
2101 DCHECK(IsAsyncGeneratorFunction(kind));
2102
2103 Block* try_block;
2104 {
2105 ScopedPtrList<Statement> statements(pointer_buffer());
2106 Expression* initial_yield = BuildInitialYield(pos, kind);
2107 statements.Add(
2108 factory()->NewExpressionStatement(initial_yield, kNoSourcePosition));
2109 ParseStatementList(&statements, Token::RBRACE);
2110 // Since the whole body is wrapped in a try-catch, make the implicit
2111 // end-of-function return explicit to ensure BytecodeGenerator's special
2112 // handling for ReturnStatements in async generators applies.
2113 statements.Add(factory()->NewSyntheticAsyncReturnStatement(
2114 factory()->NewUndefinedLiteral(kNoSourcePosition), kNoSourcePosition));
2115
2116 // Don't create iterator result for async generators, as the resume methods
2117 // will create it.
2118 try_block = factory()->NewBlock(false, statements);
2119 }
2120
2121 // For AsyncGenerators, a top-level catch block will reject the Promise.
2122 Scope* catch_scope = NewHiddenCatchScope();
2123
2124 Block* catch_block;
2125 {
2126 ScopedPtrList<Expression> reject_args(pointer_buffer());
2127 reject_args.Add(factory()->NewVariableProxy(
2128 function_state_->scope()->generator_object_var()));
2129 reject_args.Add(factory()->NewVariableProxy(catch_scope->catch_variable()));
2130
2131 Expression* reject_call = factory()->NewCallRuntime(
2132 Runtime::kInlineAsyncGeneratorReject, reject_args, kNoSourcePosition);
2133 catch_block = IgnoreCompletion(factory()->NewReturnStatement(
2134 reject_call, kNoSourcePosition, kNoSourcePosition));
2135 }
2136
2137 {
2138 ScopedPtrList<Statement> statements(pointer_buffer());
2139 TryStatement* try_catch = factory()->NewTryCatchStatementForAsyncAwait(
2140 try_block, catch_scope, catch_block, kNoSourcePosition);
2141 statements.Add(try_catch);
2142 try_block = factory()->NewBlock(false, statements);
2143 }
2144
2145 Expression* close_call;
2146 {
2147 ScopedPtrList<Expression> close_args(pointer_buffer());
2148 VariableProxy* call_proxy = factory()->NewVariableProxy(
2149 function_state_->scope()->generator_object_var());
2150 close_args.Add(call_proxy);
2151 close_call = factory()->NewCallRuntime(Runtime::kInlineGeneratorClose,
2152 close_args, kNoSourcePosition);
2153 }
2154
2155 Block* finally_block;
2156 {
2157 ScopedPtrList<Statement> statements(pointer_buffer());
2158 statements.Add(
2159 factory()->NewExpressionStatement(close_call, kNoSourcePosition));
2160 finally_block = factory()->NewBlock(false, statements);
2161 }
2162
2163 body->Add(factory()->NewTryFinallyStatement(try_block, finally_block,
2164 kNoSourcePosition));
2165 }
2166
DeclareFunctionNameVar(const AstRawString * function_name,FunctionSyntaxKind function_syntax_kind,DeclarationScope * function_scope)2167 void Parser::DeclareFunctionNameVar(const AstRawString* function_name,
2168 FunctionSyntaxKind function_syntax_kind,
2169 DeclarationScope* function_scope) {
2170 if (function_syntax_kind == FunctionSyntaxKind::kNamedExpression &&
2171 function_scope->LookupLocal(function_name) == nullptr) {
2172 DCHECK_EQ(function_scope, scope());
2173 function_scope->DeclareFunctionVar(function_name);
2174 }
2175 }
2176
2177 // Special case for legacy for
2178 //
2179 // for (var x = initializer in enumerable) body
2180 //
2181 // An initialization block of the form
2182 //
2183 // {
2184 // x = initializer;
2185 // }
2186 //
2187 // is returned in this case. It has reserved space for two statements,
2188 // so that (later on during parsing), the equivalent of
2189 //
2190 // for (x in enumerable) body
2191 //
2192 // is added as a second statement to it.
RewriteForVarInLegacy(const ForInfo & for_info)2193 Block* Parser::RewriteForVarInLegacy(const ForInfo& for_info) {
2194 const DeclarationParsingResult::Declaration& decl =
2195 for_info.parsing_result.declarations[0];
2196 if (!IsLexicalVariableMode(for_info.parsing_result.descriptor.mode) &&
2197 decl.initializer != nullptr && decl.pattern->IsVariableProxy()) {
2198 ++use_counts_[v8::Isolate::kForInInitializer];
2199 const AstRawString* name = decl.pattern->AsVariableProxy()->raw_name();
2200 VariableProxy* single_var = NewUnresolved(name);
2201 Block* init_block = factory()->NewBlock(2, true);
2202 init_block->statements()->Add(
2203 factory()->NewExpressionStatement(
2204 factory()->NewAssignment(Token::ASSIGN, single_var,
2205 decl.initializer, decl.value_beg_pos),
2206 kNoSourcePosition),
2207 zone());
2208 return init_block;
2209 }
2210 return nullptr;
2211 }
2212
2213 // Rewrite a for-in/of statement of the form
2214 //
2215 // for (let/const/var x in/of e) b
2216 //
2217 // into
2218 //
2219 // {
2220 // var temp;
2221 // for (temp in/of e) {
2222 // let/const/var x = temp;
2223 // b;
2224 // }
2225 // let x; // for TDZ
2226 // }
DesugarBindingInForEachStatement(ForInfo * for_info,Block ** body_block,Expression ** each_variable)2227 void Parser::DesugarBindingInForEachStatement(ForInfo* for_info,
2228 Block** body_block,
2229 Expression** each_variable) {
2230 DCHECK_EQ(1, for_info->parsing_result.declarations.size());
2231 DeclarationParsingResult::Declaration& decl =
2232 for_info->parsing_result.declarations[0];
2233 Variable* temp = NewTemporary(ast_value_factory()->dot_for_string());
2234 ScopedPtrList<Statement> each_initialization_statements(pointer_buffer());
2235 DCHECK_IMPLIES(!has_error(), decl.pattern != nullptr);
2236 decl.initializer = factory()->NewVariableProxy(temp, for_info->position);
2237 InitializeVariables(&each_initialization_statements, NORMAL_VARIABLE, &decl);
2238
2239 *body_block = factory()->NewBlock(3, false);
2240 (*body_block)
2241 ->statements()
2242 ->Add(factory()->NewBlock(true, each_initialization_statements), zone());
2243 *each_variable = factory()->NewVariableProxy(temp, for_info->position);
2244 }
2245
2246 // Create a TDZ for any lexically-bound names in for in/of statements.
CreateForEachStatementTDZ(Block * init_block,const ForInfo & for_info)2247 Block* Parser::CreateForEachStatementTDZ(Block* init_block,
2248 const ForInfo& for_info) {
2249 if (IsLexicalVariableMode(for_info.parsing_result.descriptor.mode)) {
2250 DCHECK_NULL(init_block);
2251
2252 init_block = factory()->NewBlock(1, false);
2253
2254 for (const AstRawString* bound_name : for_info.bound_names) {
2255 // TODO(adamk): This needs to be some sort of special
2256 // INTERNAL variable that's invisible to the debugger
2257 // but visible to everything else.
2258 VariableProxy* tdz_proxy = DeclareBoundVariable(
2259 bound_name, VariableMode::kLet, kNoSourcePosition);
2260 tdz_proxy->var()->set_initializer_position(position());
2261 }
2262 }
2263 return init_block;
2264 }
2265
DesugarLexicalBindingsInForStatement(ForStatement * loop,Statement * init,Expression * cond,Statement * next,Statement * body,Scope * inner_scope,const ForInfo & for_info)2266 Statement* Parser::DesugarLexicalBindingsInForStatement(
2267 ForStatement* loop, Statement* init, Expression* cond, Statement* next,
2268 Statement* body, Scope* inner_scope, const ForInfo& for_info) {
2269 // ES6 13.7.4.8 specifies that on each loop iteration the let variables are
2270 // copied into a new environment. Moreover, the "next" statement must be
2271 // evaluated not in the environment of the just completed iteration but in
2272 // that of the upcoming one. We achieve this with the following desugaring.
2273 // Extra care is needed to preserve the completion value of the original loop.
2274 //
2275 // We are given a for statement of the form
2276 //
2277 // labels: for (let/const x = i; cond; next) body
2278 //
2279 // and rewrite it as follows. Here we write {{ ... }} for init-blocks, ie.,
2280 // blocks whose ignore_completion_value_ flag is set.
2281 //
2282 // {
2283 // let/const x = i;
2284 // temp_x = x;
2285 // first = 1;
2286 // undefined;
2287 // outer: for (;;) {
2288 // let/const x = temp_x;
2289 // {{ if (first == 1) {
2290 // first = 0;
2291 // } else {
2292 // next;
2293 // }
2294 // flag = 1;
2295 // if (!cond) break;
2296 // }}
2297 // labels: for (; flag == 1; flag = 0, temp_x = x) {
2298 // body
2299 // }
2300 // {{ if (flag == 1) // Body used break.
2301 // break;
2302 // }}
2303 // }
2304 // }
2305
2306 DCHECK_GT(for_info.bound_names.length(), 0);
2307 ScopedPtrList<Variable> temps(pointer_buffer());
2308
2309 Block* outer_block =
2310 factory()->NewBlock(for_info.bound_names.length() + 4, false);
2311
2312 // Add statement: let/const x = i.
2313 outer_block->statements()->Add(init, zone());
2314
2315 const AstRawString* temp_name = ast_value_factory()->dot_for_string();
2316
2317 // For each lexical variable x:
2318 // make statement: temp_x = x.
2319 for (const AstRawString* bound_name : for_info.bound_names) {
2320 VariableProxy* proxy = NewUnresolved(bound_name);
2321 Variable* temp = NewTemporary(temp_name);
2322 VariableProxy* temp_proxy = factory()->NewVariableProxy(temp);
2323 Assignment* assignment = factory()->NewAssignment(Token::ASSIGN, temp_proxy,
2324 proxy, kNoSourcePosition);
2325 Statement* assignment_statement =
2326 factory()->NewExpressionStatement(assignment, kNoSourcePosition);
2327 outer_block->statements()->Add(assignment_statement, zone());
2328 temps.Add(temp);
2329 }
2330
2331 Variable* first = nullptr;
2332 // Make statement: first = 1.
2333 if (next) {
2334 first = NewTemporary(temp_name);
2335 VariableProxy* first_proxy = factory()->NewVariableProxy(first);
2336 Expression* const1 = factory()->NewSmiLiteral(1, kNoSourcePosition);
2337 Assignment* assignment = factory()->NewAssignment(
2338 Token::ASSIGN, first_proxy, const1, kNoSourcePosition);
2339 Statement* assignment_statement =
2340 factory()->NewExpressionStatement(assignment, kNoSourcePosition);
2341 outer_block->statements()->Add(assignment_statement, zone());
2342 }
2343
2344 // make statement: undefined;
2345 outer_block->statements()->Add(
2346 factory()->NewExpressionStatement(
2347 factory()->NewUndefinedLiteral(kNoSourcePosition), kNoSourcePosition),
2348 zone());
2349
2350 // Make statement: outer: for (;;)
2351 // Note that we don't actually create the label, or set this loop up as an
2352 // explicit break target, instead handing it directly to those nodes that
2353 // need to know about it. This should be safe because we don't run any code
2354 // in this function that looks up break targets.
2355 ForStatement* outer_loop = factory()->NewForStatement(kNoSourcePosition);
2356 outer_block->statements()->Add(outer_loop, zone());
2357 outer_block->set_scope(scope());
2358
2359 Block* inner_block = factory()->NewBlock(3, false);
2360 {
2361 BlockState block_state(&scope_, inner_scope);
2362
2363 Block* ignore_completion_block =
2364 factory()->NewBlock(for_info.bound_names.length() + 3, true);
2365 ScopedPtrList<Variable> inner_vars(pointer_buffer());
2366 // For each let variable x:
2367 // make statement: let/const x = temp_x.
2368 for (int i = 0; i < for_info.bound_names.length(); i++) {
2369 VariableProxy* proxy = DeclareBoundVariable(
2370 for_info.bound_names[i], for_info.parsing_result.descriptor.mode,
2371 kNoSourcePosition);
2372 inner_vars.Add(proxy->var());
2373 VariableProxy* temp_proxy = factory()->NewVariableProxy(temps.at(i));
2374 Assignment* assignment = factory()->NewAssignment(
2375 Token::INIT, proxy, temp_proxy, kNoSourcePosition);
2376 Statement* assignment_statement =
2377 factory()->NewExpressionStatement(assignment, kNoSourcePosition);
2378 int declaration_pos = for_info.parsing_result.descriptor.declaration_pos;
2379 DCHECK_NE(declaration_pos, kNoSourcePosition);
2380 proxy->var()->set_initializer_position(declaration_pos);
2381 ignore_completion_block->statements()->Add(assignment_statement, zone());
2382 }
2383
2384 // Make statement: if (first == 1) { first = 0; } else { next; }
2385 if (next) {
2386 DCHECK(first);
2387 Expression* compare = nullptr;
2388 // Make compare expression: first == 1.
2389 {
2390 Expression* const1 = factory()->NewSmiLiteral(1, kNoSourcePosition);
2391 VariableProxy* first_proxy = factory()->NewVariableProxy(first);
2392 compare = factory()->NewCompareOperation(Token::EQ, first_proxy, const1,
2393 kNoSourcePosition);
2394 }
2395 Statement* clear_first = nullptr;
2396 // Make statement: first = 0.
2397 {
2398 VariableProxy* first_proxy = factory()->NewVariableProxy(first);
2399 Expression* const0 = factory()->NewSmiLiteral(0, kNoSourcePosition);
2400 Assignment* assignment = factory()->NewAssignment(
2401 Token::ASSIGN, first_proxy, const0, kNoSourcePosition);
2402 clear_first =
2403 factory()->NewExpressionStatement(assignment, kNoSourcePosition);
2404 }
2405 Statement* clear_first_or_next = factory()->NewIfStatement(
2406 compare, clear_first, next, kNoSourcePosition);
2407 ignore_completion_block->statements()->Add(clear_first_or_next, zone());
2408 }
2409
2410 Variable* flag = NewTemporary(temp_name);
2411 // Make statement: flag = 1.
2412 {
2413 VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
2414 Expression* const1 = factory()->NewSmiLiteral(1, kNoSourcePosition);
2415 Assignment* assignment = factory()->NewAssignment(
2416 Token::ASSIGN, flag_proxy, const1, kNoSourcePosition);
2417 Statement* assignment_statement =
2418 factory()->NewExpressionStatement(assignment, kNoSourcePosition);
2419 ignore_completion_block->statements()->Add(assignment_statement, zone());
2420 }
2421
2422 // Make statement: if (!cond) break.
2423 if (cond) {
2424 Statement* stop =
2425 factory()->NewBreakStatement(outer_loop, kNoSourcePosition);
2426 Statement* noop = factory()->EmptyStatement();
2427 ignore_completion_block->statements()->Add(
2428 factory()->NewIfStatement(cond, noop, stop, cond->position()),
2429 zone());
2430 }
2431
2432 inner_block->statements()->Add(ignore_completion_block, zone());
2433 // Make cond expression for main loop: flag == 1.
2434 Expression* flag_cond = nullptr;
2435 {
2436 Expression* const1 = factory()->NewSmiLiteral(1, kNoSourcePosition);
2437 VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
2438 flag_cond = factory()->NewCompareOperation(Token::EQ, flag_proxy, const1,
2439 kNoSourcePosition);
2440 }
2441
2442 // Create chain of expressions "flag = 0, temp_x = x, ..."
2443 Statement* compound_next_statement = nullptr;
2444 {
2445 Expression* compound_next = nullptr;
2446 // Make expression: flag = 0.
2447 {
2448 VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
2449 Expression* const0 = factory()->NewSmiLiteral(0, kNoSourcePosition);
2450 compound_next = factory()->NewAssignment(Token::ASSIGN, flag_proxy,
2451 const0, kNoSourcePosition);
2452 }
2453
2454 // Make the comma-separated list of temp_x = x assignments.
2455 int inner_var_proxy_pos = scanner()->location().beg_pos;
2456 for (int i = 0; i < for_info.bound_names.length(); i++) {
2457 VariableProxy* temp_proxy = factory()->NewVariableProxy(temps.at(i));
2458 VariableProxy* proxy =
2459 factory()->NewVariableProxy(inner_vars.at(i), inner_var_proxy_pos);
2460 Assignment* assignment = factory()->NewAssignment(
2461 Token::ASSIGN, temp_proxy, proxy, kNoSourcePosition);
2462 compound_next = factory()->NewBinaryOperation(
2463 Token::COMMA, compound_next, assignment, kNoSourcePosition);
2464 }
2465
2466 compound_next_statement =
2467 factory()->NewExpressionStatement(compound_next, kNoSourcePosition);
2468 }
2469
2470 // Make statement: labels: for (; flag == 1; flag = 0, temp_x = x)
2471 // Note that we re-use the original loop node, which retains its labels
2472 // and ensures that any break or continue statements in body point to
2473 // the right place.
2474 loop->Initialize(nullptr, flag_cond, compound_next_statement, body);
2475 inner_block->statements()->Add(loop, zone());
2476
2477 // Make statement: {{if (flag == 1) break;}}
2478 {
2479 Expression* compare = nullptr;
2480 // Make compare expresion: flag == 1.
2481 {
2482 Expression* const1 = factory()->NewSmiLiteral(1, kNoSourcePosition);
2483 VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
2484 compare = factory()->NewCompareOperation(Token::EQ, flag_proxy, const1,
2485 kNoSourcePosition);
2486 }
2487 Statement* stop =
2488 factory()->NewBreakStatement(outer_loop, kNoSourcePosition);
2489 Statement* empty = factory()->EmptyStatement();
2490 Statement* if_flag_break =
2491 factory()->NewIfStatement(compare, stop, empty, kNoSourcePosition);
2492 inner_block->statements()->Add(IgnoreCompletion(if_flag_break), zone());
2493 }
2494
2495 inner_block->set_scope(inner_scope);
2496 }
2497
2498 outer_loop->Initialize(nullptr, nullptr, nullptr, inner_block);
2499
2500 return outer_block;
2501 }
2502
ValidateDuplicate(Parser * parser) const2503 void ParserFormalParameters::ValidateDuplicate(Parser* parser) const {
2504 if (has_duplicate()) {
2505 parser->ReportMessageAt(duplicate_loc, MessageTemplate::kParamDupe);
2506 }
2507 }
ValidateStrictMode(Parser * parser) const2508 void ParserFormalParameters::ValidateStrictMode(Parser* parser) const {
2509 if (strict_error_loc.IsValid()) {
2510 parser->ReportMessageAt(strict_error_loc, strict_error_message);
2511 }
2512 }
2513
AddArrowFunctionFormalParameters(ParserFormalParameters * parameters,Expression * expr,int end_pos)2514 void Parser::AddArrowFunctionFormalParameters(
2515 ParserFormalParameters* parameters, Expression* expr, int end_pos) {
2516 // ArrowFunctionFormals ::
2517 // Nary(Token::COMMA, VariableProxy*, Tail)
2518 // Binary(Token::COMMA, NonTailArrowFunctionFormals, Tail)
2519 // Tail
2520 // NonTailArrowFunctionFormals ::
2521 // Binary(Token::COMMA, NonTailArrowFunctionFormals, VariableProxy)
2522 // VariableProxy
2523 // Tail ::
2524 // VariableProxy
2525 // Spread(VariableProxy)
2526 //
2527 // We need to visit the parameters in left-to-right order
2528 //
2529
2530 // For the Nary case, we simply visit the parameters in a loop.
2531 if (expr->IsNaryOperation()) {
2532 NaryOperation* nary = expr->AsNaryOperation();
2533 // The classifier has already run, so we know that the expression is a valid
2534 // arrow function formals production.
2535 DCHECK_EQ(nary->op(), Token::COMMA);
2536 // Each op position is the end position of the *previous* expr, with the
2537 // second (i.e. first "subsequent") op position being the end position of
2538 // the first child expression.
2539 Expression* next = nary->first();
2540 for (size_t i = 0; i < nary->subsequent_length(); ++i) {
2541 AddArrowFunctionFormalParameters(parameters, next,
2542 nary->subsequent_op_position(i));
2543 next = nary->subsequent(i);
2544 }
2545 AddArrowFunctionFormalParameters(parameters, next, end_pos);
2546 return;
2547 }
2548
2549 // For the binary case, we recurse on the left-hand side of binary comma
2550 // expressions.
2551 if (expr->IsBinaryOperation()) {
2552 BinaryOperation* binop = expr->AsBinaryOperation();
2553 // The classifier has already run, so we know that the expression is a valid
2554 // arrow function formals production.
2555 DCHECK_EQ(binop->op(), Token::COMMA);
2556 Expression* left = binop->left();
2557 Expression* right = binop->right();
2558 int comma_pos = binop->position();
2559 AddArrowFunctionFormalParameters(parameters, left, comma_pos);
2560 // LHS of comma expression should be unparenthesized.
2561 expr = right;
2562 }
2563
2564 // Only the right-most expression may be a rest parameter.
2565 DCHECK(!parameters->has_rest);
2566
2567 bool is_rest = expr->IsSpread();
2568 if (is_rest) {
2569 expr = expr->AsSpread()->expression();
2570 parameters->has_rest = true;
2571 }
2572 DCHECK_IMPLIES(parameters->is_simple, !is_rest);
2573 DCHECK_IMPLIES(parameters->is_simple, expr->IsVariableProxy());
2574
2575 Expression* initializer = nullptr;
2576 if (expr->IsAssignment()) {
2577 Assignment* assignment = expr->AsAssignment();
2578 DCHECK(!assignment->IsCompoundAssignment());
2579 initializer = assignment->value();
2580 expr = assignment->target();
2581 }
2582
2583 AddFormalParameter(parameters, expr, initializer, end_pos, is_rest);
2584 }
2585
DeclareArrowFunctionFormalParameters(ParserFormalParameters * parameters,Expression * expr,const Scanner::Location & params_loc)2586 void Parser::DeclareArrowFunctionFormalParameters(
2587 ParserFormalParameters* parameters, Expression* expr,
2588 const Scanner::Location& params_loc) {
2589 if (expr->IsEmptyParentheses() || has_error()) return;
2590
2591 AddArrowFunctionFormalParameters(parameters, expr, params_loc.end_pos);
2592
2593 if (parameters->arity > Code::kMaxArguments) {
2594 ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList);
2595 return;
2596 }
2597
2598 DeclareFormalParameters(parameters);
2599 DCHECK_IMPLIES(parameters->is_simple,
2600 parameters->scope->has_simple_parameters());
2601 }
2602
PrepareGeneratorVariables()2603 void Parser::PrepareGeneratorVariables() {
2604 // Calling a generator returns a generator object. That object is stored
2605 // in a temporary variable, a definition that is used by "yield"
2606 // expressions.
2607 function_state_->scope()->DeclareGeneratorObjectVar(
2608 ast_value_factory()->dot_generator_object_string());
2609 }
2610
ParseFunctionLiteral(const AstRawString * function_name,Scanner::Location function_name_location,FunctionNameValidity function_name_validity,FunctionKind kind,int function_token_pos,FunctionSyntaxKind function_syntax_kind,LanguageMode language_mode,ZonePtrList<const AstRawString> * arguments_for_wrapped_function)2611 FunctionLiteral* Parser::ParseFunctionLiteral(
2612 const AstRawString* function_name, Scanner::Location function_name_location,
2613 FunctionNameValidity function_name_validity, FunctionKind kind,
2614 int function_token_pos, FunctionSyntaxKind function_syntax_kind,
2615 LanguageMode language_mode,
2616 ZonePtrList<const AstRawString>* arguments_for_wrapped_function) {
2617 // Function ::
2618 // '(' FormalParameterList? ')' '{' FunctionBody '}'
2619 //
2620 // Getter ::
2621 // '(' ')' '{' FunctionBody '}'
2622 //
2623 // Setter ::
2624 // '(' PropertySetParameterList ')' '{' FunctionBody '}'
2625
2626 bool is_wrapped = function_syntax_kind == FunctionSyntaxKind::kWrapped;
2627 DCHECK_EQ(is_wrapped, arguments_for_wrapped_function != nullptr);
2628
2629 int pos = function_token_pos == kNoSourcePosition ? peek_position()
2630 : function_token_pos;
2631 DCHECK_NE(kNoSourcePosition, pos);
2632
2633 // Anonymous functions were passed either the empty symbol or a null
2634 // handle as the function name. Remember if we were passed a non-empty
2635 // handle to decide whether to invoke function name inference.
2636 bool should_infer_name = function_name == nullptr;
2637
2638 // We want a non-null handle as the function name by default. We will handle
2639 // the "function does not have a shared name" case later.
2640 if (should_infer_name) {
2641 function_name = ast_value_factory()->empty_string();
2642 }
2643
2644 FunctionLiteral::EagerCompileHint eager_compile_hint =
2645 function_state_->next_function_is_likely_called() || is_wrapped
2646 ? FunctionLiteral::kShouldEagerCompile
2647 : default_eager_compile_hint();
2648
2649 // Determine if the function can be parsed lazily. Lazy parsing is
2650 // different from lazy compilation; we need to parse more eagerly than we
2651 // compile.
2652
2653 // We can only parse lazily if we also compile lazily. The heuristics for lazy
2654 // compilation are:
2655 // - It must not have been prohibited by the caller to Parse (some callers
2656 // need a full AST).
2657 // - The outer scope must allow lazy compilation of inner functions.
2658 // - The function mustn't be a function expression with an open parenthesis
2659 // before; we consider that a hint that the function will be called
2660 // immediately, and it would be a waste of time to make it lazily
2661 // compiled.
2662 // These are all things we can know at this point, without looking at the
2663 // function itself.
2664
2665 // We separate between lazy parsing top level functions and lazy parsing inner
2666 // functions, because the latter needs to do more work. In particular, we need
2667 // to track unresolved variables to distinguish between these cases:
2668 // (function foo() {
2669 // bar = function() { return 1; }
2670 // })();
2671 // and
2672 // (function foo() {
2673 // var a = 1;
2674 // bar = function() { return a; }
2675 // })();
2676
2677 // Now foo will be parsed eagerly and compiled eagerly (optimization: assume
2678 // parenthesis before the function means that it will be called
2679 // immediately). bar can be parsed lazily, but we need to parse it in a mode
2680 // that tracks unresolved variables.
2681 DCHECK_IMPLIES(parse_lazily(), info()->flags().allow_lazy_compile());
2682 DCHECK_IMPLIES(parse_lazily(), has_error() || allow_lazy_);
2683 DCHECK_IMPLIES(parse_lazily(), extension() == nullptr);
2684
2685 const bool is_lazy =
2686 eager_compile_hint == FunctionLiteral::kShouldLazyCompile;
2687 const bool is_top_level = AllowsLazyParsingWithoutUnresolvedVariables();
2688 const bool is_eager_top_level_function = !is_lazy && is_top_level;
2689
2690 RCS_SCOPE(runtime_call_stats_, RuntimeCallCounterId::kParseFunctionLiteral,
2691 RuntimeCallStats::kThreadSpecific);
2692 base::ElapsedTimer timer;
2693 if (V8_UNLIKELY(FLAG_log_function_events)) timer.Start();
2694
2695 // Determine whether we can lazy parse the inner function. Lazy compilation
2696 // has to be enabled, which is either forced by overall parse flags or via a
2697 // ParsingModeScope.
2698 const bool can_preparse = parse_lazily();
2699
2700 // Determine whether we can post any parallel compile tasks. Preparsing must
2701 // be possible, there has to be a dispatcher, and the character stream must be
2702 // cloneable.
2703 const bool can_post_parallel_task =
2704 can_preparse && info()->dispatcher() &&
2705 scanner()->stream()->can_be_cloned_for_parallel_access();
2706
2707 // If parallel compile tasks are enabled, enable parallel compile for the
2708 // subset of functions as defined by flags.
2709 bool should_post_parallel_task =
2710 can_post_parallel_task &&
2711 ((is_eager_top_level_function &&
2712 flags().post_parallel_compile_tasks_for_eager_toplevel()) ||
2713 (is_lazy && flags().post_parallel_compile_tasks_for_lazy()));
2714
2715 // Determine whether we should lazy parse the inner function. This will be
2716 // when either the function is lazy by inspection, or when we force it to be
2717 // preparsed now so that we can then post a parallel full parse & compile task
2718 // for it.
2719 const bool should_preparse =
2720 can_preparse && (is_lazy || should_post_parallel_task);
2721
2722 ScopedPtrList<Statement> body(pointer_buffer());
2723 int expected_property_count = 0;
2724 int suspend_count = -1;
2725 int num_parameters = -1;
2726 int function_length = -1;
2727 bool has_duplicate_parameters = false;
2728 int function_literal_id = GetNextFunctionLiteralId();
2729 ProducedPreparseData* produced_preparse_data = nullptr;
2730
2731 // Inner functions will be parsed using a temporary Zone. After parsing, we
2732 // will migrate unresolved variable into a Scope in the main Zone.
2733 Zone* parse_zone = should_preparse ? &preparser_zone_ : zone();
2734 // This Scope lives in the main zone. We'll migrate data into that zone later.
2735 DeclarationScope* scope = NewFunctionScope(kind, parse_zone);
2736 SetLanguageMode(scope, language_mode);
2737 #ifdef DEBUG
2738 scope->SetScopeName(function_name);
2739 #endif
2740
2741 if (!is_wrapped && V8_UNLIKELY(!Check(Token::LPAREN))) {
2742 ReportUnexpectedToken(Next());
2743 return nullptr;
2744 }
2745 scope->set_start_position(position());
2746
2747 // Eager or lazy parse? If is_lazy_top_level_function, we'll parse
2748 // lazily. We'll call SkipFunction, which may decide to
2749 // abort lazy parsing if it suspects that wasn't a good idea. If so (in
2750 // which case the parser is expected to have backtracked), or if we didn't
2751 // try to lazy parse in the first place, we'll have to parse eagerly.
2752 bool did_preparse_successfully =
2753 should_preparse &&
2754 SkipFunction(function_name, kind, function_syntax_kind, scope,
2755 &num_parameters, &function_length, &produced_preparse_data);
2756
2757 if (!did_preparse_successfully) {
2758 // If skipping aborted, it rewound the scanner until before the LPAREN.
2759 // Consume it in that case.
2760 if (should_preparse) Consume(Token::LPAREN);
2761 should_post_parallel_task = false;
2762 ParseFunction(&body, function_name, pos, kind, function_syntax_kind, scope,
2763 &num_parameters, &function_length, &has_duplicate_parameters,
2764 &expected_property_count, &suspend_count,
2765 arguments_for_wrapped_function);
2766 }
2767
2768 if (V8_UNLIKELY(FLAG_log_function_events)) {
2769 double ms = timer.Elapsed().InMillisecondsF();
2770 const char* event_name =
2771 should_preparse
2772 ? (is_top_level ? "preparse-no-resolution" : "preparse-resolution")
2773 : "full-parse";
2774 logger_->FunctionEvent(
2775 event_name, flags().script_id(), ms, scope->start_position(),
2776 scope->end_position(),
2777 reinterpret_cast<const char*>(function_name->raw_data()),
2778 function_name->byte_length(), function_name->is_one_byte());
2779 }
2780 #ifdef V8_RUNTIME_CALL_STATS
2781 if (did_preparse_successfully && runtime_call_stats_ &&
2782 V8_UNLIKELY(TracingFlags::is_runtime_stats_enabled())) {
2783 runtime_call_stats_->CorrectCurrentCounterId(
2784 RuntimeCallCounterId::kPreParseWithVariableResolution,
2785 RuntimeCallStats::kThreadSpecific);
2786 }
2787 #endif // V8_RUNTIME_CALL_STATS
2788
2789 // Validate function name. We can do this only after parsing the function,
2790 // since the function can declare itself strict.
2791 language_mode = scope->language_mode();
2792 CheckFunctionName(language_mode, function_name, function_name_validity,
2793 function_name_location);
2794
2795 if (is_strict(language_mode)) {
2796 CheckStrictOctalLiteral(scope->start_position(), scope->end_position());
2797 }
2798
2799 FunctionLiteral::ParameterFlag duplicate_parameters =
2800 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters
2801 : FunctionLiteral::kNoDuplicateParameters;
2802
2803 // Note that the FunctionLiteral needs to be created in the main Zone again.
2804 FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
2805 function_name, scope, body, expected_property_count, num_parameters,
2806 function_length, duplicate_parameters, function_syntax_kind,
2807 eager_compile_hint, pos, true, function_literal_id,
2808 produced_preparse_data);
2809 function_literal->set_function_token_position(function_token_pos);
2810 function_literal->set_suspend_count(suspend_count);
2811
2812 RecordFunctionLiteralSourceRange(function_literal);
2813
2814 if (should_post_parallel_task && !has_error()) {
2815 function_literal->set_should_parallel_compile();
2816 }
2817
2818 if (should_infer_name) {
2819 fni_.AddFunction(function_literal);
2820 }
2821 return function_literal;
2822 }
2823
SkipFunction(const AstRawString * function_name,FunctionKind kind,FunctionSyntaxKind function_syntax_kind,DeclarationScope * function_scope,int * num_parameters,int * function_length,ProducedPreparseData ** produced_preparse_data)2824 bool Parser::SkipFunction(const AstRawString* function_name, FunctionKind kind,
2825 FunctionSyntaxKind function_syntax_kind,
2826 DeclarationScope* function_scope, int* num_parameters,
2827 int* function_length,
2828 ProducedPreparseData** produced_preparse_data) {
2829 FunctionState function_state(&function_state_, &scope_, function_scope);
2830 function_scope->set_zone(&preparser_zone_);
2831
2832 DCHECK_NE(kNoSourcePosition, function_scope->start_position());
2833 DCHECK_EQ(kNoSourcePosition, parameters_end_pos_);
2834
2835 DCHECK_IMPLIES(IsArrowFunction(kind),
2836 scanner()->current_token() == Token::ARROW);
2837
2838 // FIXME(marja): There are 2 ways to skip functions now. Unify them.
2839 if (consumed_preparse_data_) {
2840 int end_position;
2841 LanguageMode language_mode;
2842 int num_inner_functions;
2843 bool uses_super_property;
2844 if (stack_overflow()) return true;
2845 {
2846 base::Optional<UnparkedScope> unparked_scope;
2847 if (overall_parse_is_parked_) {
2848 unparked_scope.emplace(local_isolate_);
2849 }
2850 *produced_preparse_data =
2851 consumed_preparse_data_->GetDataForSkippableFunction(
2852 main_zone(), function_scope->start_position(), &end_position,
2853 num_parameters, function_length, &num_inner_functions,
2854 &uses_super_property, &language_mode);
2855 }
2856
2857 function_scope->outer_scope()->SetMustUsePreparseData();
2858 function_scope->set_is_skipped_function(true);
2859 function_scope->set_end_position(end_position);
2860 scanner()->SeekForward(end_position - 1);
2861 Expect(Token::RBRACE);
2862 SetLanguageMode(function_scope, language_mode);
2863 if (uses_super_property) {
2864 function_scope->RecordSuperPropertyUsage();
2865 }
2866 SkipFunctionLiterals(num_inner_functions);
2867 function_scope->ResetAfterPreparsing(ast_value_factory_, false);
2868 return true;
2869 }
2870
2871 Scanner::BookmarkScope bookmark(scanner());
2872 bookmark.Set(function_scope->start_position());
2873
2874 UnresolvedList::Iterator unresolved_private_tail;
2875 PrivateNameScopeIterator private_name_scope_iter(function_scope);
2876 if (!private_name_scope_iter.Done()) {
2877 unresolved_private_tail =
2878 private_name_scope_iter.GetScope()->GetUnresolvedPrivateNameTail();
2879 }
2880
2881 // With no cached data, we partially parse the function, without building an
2882 // AST. This gathers the data needed to build a lazy function.
2883 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.PreParse");
2884
2885 PreParser::PreParseResult result = reusable_preparser()->PreParseFunction(
2886 function_name, kind, function_syntax_kind, function_scope, use_counts_,
2887 produced_preparse_data);
2888
2889 if (result == PreParser::kPreParseStackOverflow) {
2890 // Propagate stack overflow.
2891 set_stack_overflow();
2892 } else if (pending_error_handler()->has_error_unidentifiable_by_preparser()) {
2893 // Make sure we don't re-preparse inner functions of the aborted function.
2894 // The error might be in an inner function.
2895 allow_lazy_ = false;
2896 mode_ = PARSE_EAGERLY;
2897 DCHECK(!pending_error_handler()->stack_overflow());
2898 // If we encounter an error that the preparser can not identify we reset to
2899 // the state before preparsing. The caller may then fully parse the function
2900 // to identify the actual error.
2901 bookmark.Apply();
2902 if (!private_name_scope_iter.Done()) {
2903 private_name_scope_iter.GetScope()->ResetUnresolvedPrivateNameTail(
2904 unresolved_private_tail);
2905 }
2906 function_scope->ResetAfterPreparsing(ast_value_factory_, true);
2907 pending_error_handler()->clear_unidentifiable_error();
2908 return false;
2909 } else if (pending_error_handler()->has_pending_error()) {
2910 DCHECK(!pending_error_handler()->stack_overflow());
2911 DCHECK(has_error());
2912 } else {
2913 DCHECK(!pending_error_handler()->stack_overflow());
2914 set_allow_eval_cache(reusable_preparser()->allow_eval_cache());
2915
2916 PreParserLogger* logger = reusable_preparser()->logger();
2917 function_scope->set_end_position(logger->end());
2918 Expect(Token::RBRACE);
2919 total_preparse_skipped_ +=
2920 function_scope->end_position() - function_scope->start_position();
2921 *num_parameters = logger->num_parameters();
2922 *function_length = logger->function_length();
2923 SkipFunctionLiterals(logger->num_inner_functions());
2924 if (!private_name_scope_iter.Done()) {
2925 private_name_scope_iter.GetScope()->MigrateUnresolvedPrivateNameTail(
2926 factory(), unresolved_private_tail);
2927 }
2928 function_scope->AnalyzePartially(this, factory(), MaybeParsingArrowhead());
2929 }
2930
2931 return true;
2932 }
2933
BuildParameterInitializationBlock(const ParserFormalParameters & parameters)2934 Block* Parser::BuildParameterInitializationBlock(
2935 const ParserFormalParameters& parameters) {
2936 DCHECK(!parameters.is_simple);
2937 DCHECK(scope()->is_function_scope());
2938 DCHECK_EQ(scope(), parameters.scope);
2939 ScopedPtrList<Statement> init_statements(pointer_buffer());
2940 int index = 0;
2941 for (auto parameter : parameters.params) {
2942 Expression* initial_value =
2943 factory()->NewVariableProxy(parameters.scope->parameter(index));
2944 if (parameter->initializer() != nullptr) {
2945 // IS_UNDEFINED($param) ? initializer : $param
2946
2947 auto condition = factory()->NewCompareOperation(
2948 Token::EQ_STRICT,
2949 factory()->NewVariableProxy(parameters.scope->parameter(index)),
2950 factory()->NewUndefinedLiteral(kNoSourcePosition), kNoSourcePosition);
2951 initial_value =
2952 factory()->NewConditional(condition, parameter->initializer(),
2953 initial_value, kNoSourcePosition);
2954 }
2955
2956 BlockState block_state(&scope_, scope()->AsDeclarationScope());
2957 DeclarationParsingResult::Declaration decl(parameter->pattern,
2958 initial_value);
2959 InitializeVariables(&init_statements, PARAMETER_VARIABLE, &decl);
2960
2961 ++index;
2962 }
2963 return factory()->NewBlock(true, init_statements);
2964 }
2965
NewHiddenCatchScope()2966 Scope* Parser::NewHiddenCatchScope() {
2967 Scope* catch_scope = NewScopeWithParent(scope(), CATCH_SCOPE);
2968 bool was_added;
2969 catch_scope->DeclareLocal(ast_value_factory()->dot_catch_string(),
2970 VariableMode::kVar, NORMAL_VARIABLE, &was_added);
2971 DCHECK(was_added);
2972 catch_scope->set_is_hidden();
2973 return catch_scope;
2974 }
2975
BuildRejectPromiseOnException(Block * inner_block,REPLMode repl_mode)2976 Block* Parser::BuildRejectPromiseOnException(Block* inner_block,
2977 REPLMode repl_mode) {
2978 // try {
2979 // <inner_block>
2980 // } catch (.catch) {
2981 // return %_AsyncFunctionReject(.generator_object, .catch, can_suspend);
2982 // }
2983 Block* result = factory()->NewBlock(1, true);
2984
2985 // catch (.catch) {
2986 // return %_AsyncFunctionReject(.generator_object, .catch, can_suspend)
2987 // }
2988 Scope* catch_scope = NewHiddenCatchScope();
2989
2990 Expression* reject_promise;
2991 {
2992 ScopedPtrList<Expression> args(pointer_buffer());
2993 args.Add(factory()->NewVariableProxy(
2994 function_state_->scope()->generator_object_var()));
2995 args.Add(factory()->NewVariableProxy(catch_scope->catch_variable()));
2996 reject_promise = factory()->NewCallRuntime(
2997 Runtime::kInlineAsyncFunctionReject, args, kNoSourcePosition);
2998 }
2999 Block* catch_block = IgnoreCompletion(factory()->NewReturnStatement(
3000 reject_promise, kNoSourcePosition, kNoSourcePosition));
3001
3002 // Treat the exception for REPL mode scripts as UNCAUGHT. This will
3003 // keep the corresponding JSMessageObject alive on the Isolate. The
3004 // message object is used by the inspector to provide better error
3005 // messages for REPL inputs that throw.
3006 TryStatement* try_catch_statement =
3007 repl_mode == REPLMode::kYes
3008 ? factory()->NewTryCatchStatementForReplAsyncAwait(
3009 inner_block, catch_scope, catch_block, kNoSourcePosition)
3010 : factory()->NewTryCatchStatementForAsyncAwait(
3011 inner_block, catch_scope, catch_block, kNoSourcePosition);
3012 result->statements()->Add(try_catch_statement, zone());
3013 return result;
3014 }
3015
BuildInitialYield(int pos,FunctionKind kind)3016 Expression* Parser::BuildInitialYield(int pos, FunctionKind kind) {
3017 Expression* yield_result = factory()->NewVariableProxy(
3018 function_state_->scope()->generator_object_var());
3019 // The position of the yield is important for reporting the exception
3020 // caused by calling the .throw method on a generator suspended at the
3021 // initial yield (i.e. right after generator instantiation).
3022 function_state_->AddSuspend();
3023 return factory()->NewYield(yield_result, scope()->start_position(),
3024 Suspend::kOnExceptionThrow);
3025 }
3026
ParseFunction(ScopedPtrList<Statement> * body,const AstRawString * function_name,int pos,FunctionKind kind,FunctionSyntaxKind function_syntax_kind,DeclarationScope * function_scope,int * num_parameters,int * function_length,bool * has_duplicate_parameters,int * expected_property_count,int * suspend_count,ZonePtrList<const AstRawString> * arguments_for_wrapped_function)3027 void Parser::ParseFunction(
3028 ScopedPtrList<Statement>* body, const AstRawString* function_name, int pos,
3029 FunctionKind kind, FunctionSyntaxKind function_syntax_kind,
3030 DeclarationScope* function_scope, int* num_parameters, int* function_length,
3031 bool* has_duplicate_parameters, int* expected_property_count,
3032 int* suspend_count,
3033 ZonePtrList<const AstRawString>* arguments_for_wrapped_function) {
3034 FunctionParsingScope function_parsing_scope(this);
3035 ParsingModeScope mode(this, allow_lazy_ ? PARSE_LAZILY : PARSE_EAGERLY);
3036
3037 FunctionState function_state(&function_state_, &scope_, function_scope);
3038
3039 bool is_wrapped = function_syntax_kind == FunctionSyntaxKind::kWrapped;
3040
3041 int expected_parameters_end_pos = parameters_end_pos_;
3042 if (expected_parameters_end_pos != kNoSourcePosition) {
3043 // This is the first function encountered in a CreateDynamicFunction eval.
3044 parameters_end_pos_ = kNoSourcePosition;
3045 // The function name should have been ignored, giving us the empty string
3046 // here.
3047 DCHECK_EQ(function_name, ast_value_factory()->empty_string());
3048 }
3049
3050 ParserFormalParameters formals(function_scope);
3051
3052 {
3053 ParameterDeclarationParsingScope formals_scope(this);
3054 if (is_wrapped) {
3055 // For a function implicitly wrapped in function header and footer, the
3056 // function arguments are provided separately to the source, and are
3057 // declared directly here.
3058 for (const AstRawString* arg : *arguments_for_wrapped_function) {
3059 const bool is_rest = false;
3060 Expression* argument = ExpressionFromIdentifier(arg, kNoSourcePosition);
3061 AddFormalParameter(&formals, argument, NullExpression(),
3062 kNoSourcePosition, is_rest);
3063 }
3064 DCHECK_EQ(arguments_for_wrapped_function->length(),
3065 formals.num_parameters());
3066 DeclareFormalParameters(&formals);
3067 } else {
3068 // For a regular function, the function arguments are parsed from source.
3069 DCHECK_NULL(arguments_for_wrapped_function);
3070 ParseFormalParameterList(&formals);
3071 if (expected_parameters_end_pos != kNoSourcePosition) {
3072 // Check for '(' or ')' shenanigans in the parameter string for dynamic
3073 // functions.
3074 int position = peek_position();
3075 if (position < expected_parameters_end_pos) {
3076 ReportMessageAt(Scanner::Location(position, position + 1),
3077 MessageTemplate::kArgStringTerminatesParametersEarly);
3078 return;
3079 } else if (position > expected_parameters_end_pos) {
3080 ReportMessageAt(Scanner::Location(expected_parameters_end_pos - 2,
3081 expected_parameters_end_pos),
3082 MessageTemplate::kUnexpectedEndOfArgString);
3083 return;
3084 }
3085 }
3086 Expect(Token::RPAREN);
3087 int formals_end_position = scanner()->location().end_pos;
3088
3089 CheckArityRestrictions(formals.arity, kind, formals.has_rest,
3090 function_scope->start_position(),
3091 formals_end_position);
3092 Expect(Token::LBRACE);
3093 }
3094 formals.duplicate_loc = formals_scope.duplicate_location();
3095 }
3096
3097 *num_parameters = formals.num_parameters();
3098 *function_length = formals.function_length;
3099
3100 AcceptINScope scope(this, true);
3101 ParseFunctionBody(body, function_name, pos, formals, kind,
3102 function_syntax_kind, FunctionBodyType::kBlock);
3103
3104 *has_duplicate_parameters = formals.has_duplicate();
3105
3106 *expected_property_count = function_state.expected_property_count();
3107 *suspend_count = function_state.suspend_count();
3108 }
3109
DeclareClassVariable(ClassScope * scope,const AstRawString * name,ClassInfo * class_info,int class_token_pos)3110 void Parser::DeclareClassVariable(ClassScope* scope, const AstRawString* name,
3111 ClassInfo* class_info, int class_token_pos) {
3112 #ifdef DEBUG
3113 scope->SetScopeName(name);
3114 #endif
3115
3116 DCHECK_IMPLIES(name == nullptr, class_info->is_anonymous);
3117 // Declare a special class variable for anonymous classes with the dot
3118 // if we need to save it for static private method access.
3119 Variable* class_variable =
3120 scope->DeclareClassVariable(ast_value_factory(), name, class_token_pos);
3121 Declaration* declaration = factory()->NewVariableDeclaration(class_token_pos);
3122 scope->declarations()->Add(declaration);
3123 declaration->set_var(class_variable);
3124 }
3125
3126 // TODO(gsathya): Ideally, this should just bypass scope analysis and
3127 // allocate a slot directly on the context. We should just store this
3128 // index in the AST, instead of storing the variable.
CreateSyntheticContextVariable(const AstRawString * name)3129 Variable* Parser::CreateSyntheticContextVariable(const AstRawString* name) {
3130 VariableProxy* proxy =
3131 DeclareBoundVariable(name, VariableMode::kConst, kNoSourcePosition);
3132 proxy->var()->ForceContextAllocation();
3133 return proxy->var();
3134 }
3135
CreatePrivateNameVariable(ClassScope * scope,VariableMode mode,IsStaticFlag is_static_flag,const AstRawString * name)3136 Variable* Parser::CreatePrivateNameVariable(ClassScope* scope,
3137 VariableMode mode,
3138 IsStaticFlag is_static_flag,
3139 const AstRawString* name) {
3140 DCHECK_NOT_NULL(name);
3141 int begin = position();
3142 int end = end_position();
3143 bool was_added = false;
3144 DCHECK(IsConstVariableMode(mode));
3145 Variable* var =
3146 scope->DeclarePrivateName(name, mode, is_static_flag, &was_added);
3147 if (!was_added) {
3148 Scanner::Location loc(begin, end);
3149 ReportMessageAt(loc, MessageTemplate::kVarRedeclaration, var->raw_name());
3150 }
3151 VariableProxy* proxy = factory()->NewVariableProxy(var, begin);
3152 return proxy->var();
3153 }
3154
DeclarePublicClassField(ClassScope * scope,ClassLiteralProperty * property,bool is_static,bool is_computed_name,ClassInfo * class_info)3155 void Parser::DeclarePublicClassField(ClassScope* scope,
3156 ClassLiteralProperty* property,
3157 bool is_static, bool is_computed_name,
3158 ClassInfo* class_info) {
3159 if (is_static) {
3160 class_info->static_elements->Add(
3161 factory()->NewClassLiteralStaticElement(property), zone());
3162 } else {
3163 class_info->instance_fields->Add(property, zone());
3164 }
3165
3166 if (is_computed_name) {
3167 // We create a synthetic variable name here so that scope
3168 // analysis doesn't dedupe the vars.
3169 Variable* computed_name_var =
3170 CreateSyntheticContextVariable(ClassFieldVariableName(
3171 ast_value_factory(), class_info->computed_field_count));
3172 property->set_computed_name_var(computed_name_var);
3173 class_info->public_members->Add(property, zone());
3174 }
3175 }
3176
DeclarePrivateClassMember(ClassScope * scope,const AstRawString * property_name,ClassLiteralProperty * property,ClassLiteralProperty::Kind kind,bool is_static,ClassInfo * class_info)3177 void Parser::DeclarePrivateClassMember(ClassScope* scope,
3178 const AstRawString* property_name,
3179 ClassLiteralProperty* property,
3180 ClassLiteralProperty::Kind kind,
3181 bool is_static, ClassInfo* class_info) {
3182 if (kind == ClassLiteralProperty::Kind::FIELD) {
3183 if (is_static) {
3184 class_info->static_elements->Add(
3185 factory()->NewClassLiteralStaticElement(property), zone());
3186 } else {
3187 class_info->instance_fields->Add(property, zone());
3188 }
3189 }
3190
3191 Variable* private_name_var = CreatePrivateNameVariable(
3192 scope, GetVariableMode(kind),
3193 is_static ? IsStaticFlag::kStatic : IsStaticFlag::kNotStatic,
3194 property_name);
3195 int pos = property->value()->position();
3196 if (pos == kNoSourcePosition) {
3197 pos = property->key()->position();
3198 }
3199 private_name_var->set_initializer_position(pos);
3200 property->set_private_name_var(private_name_var);
3201 class_info->private_members->Add(property, zone());
3202 }
3203
3204 // This method declares a property of the given class. It updates the
3205 // following fields of class_info, as appropriate:
3206 // - constructor
3207 // - properties
DeclarePublicClassMethod(const AstRawString * class_name,ClassLiteralProperty * property,bool is_constructor,ClassInfo * class_info)3208 void Parser::DeclarePublicClassMethod(const AstRawString* class_name,
3209 ClassLiteralProperty* property,
3210 bool is_constructor,
3211 ClassInfo* class_info) {
3212 if (is_constructor) {
3213 DCHECK(!class_info->constructor);
3214 class_info->constructor = property->value()->AsFunctionLiteral();
3215 DCHECK_NOT_NULL(class_info->constructor);
3216 class_info->constructor->set_raw_name(
3217 class_name != nullptr ? ast_value_factory()->NewConsString(class_name)
3218 : nullptr);
3219 return;
3220 }
3221
3222 class_info->public_members->Add(property, zone());
3223 }
3224
AddClassStaticBlock(Block * block,ClassInfo * class_info)3225 void Parser::AddClassStaticBlock(Block* block, ClassInfo* class_info) {
3226 DCHECK(class_info->has_static_elements);
3227 class_info->static_elements->Add(
3228 factory()->NewClassLiteralStaticElement(block), zone());
3229 }
3230
CreateInitializerFunction(const char * name,DeclarationScope * scope,Statement * initializer_stmt)3231 FunctionLiteral* Parser::CreateInitializerFunction(
3232 const char* name, DeclarationScope* scope, Statement* initializer_stmt) {
3233 DCHECK(IsClassMembersInitializerFunction(scope->function_kind()));
3234 // function() { .. class fields initializer .. }
3235 ScopedPtrList<Statement> statements(pointer_buffer());
3236 statements.Add(initializer_stmt);
3237 FunctionLiteral* result = factory()->NewFunctionLiteral(
3238 ast_value_factory()->GetOneByteString(name), scope, statements, 0, 0, 0,
3239 FunctionLiteral::kNoDuplicateParameters,
3240 FunctionSyntaxKind::kAccessorOrMethod,
3241 FunctionLiteral::kShouldEagerCompile, scope->start_position(), false,
3242 GetNextFunctionLiteralId());
3243 #ifdef DEBUG
3244 scope->SetScopeName(ast_value_factory()->GetOneByteString(name));
3245 #endif
3246 RecordFunctionLiteralSourceRange(result);
3247
3248 return result;
3249 }
3250
3251 // This method generates a ClassLiteral AST node.
3252 // It uses the following fields of class_info:
3253 // - constructor (if missing, it updates it with a default constructor)
3254 // - proxy
3255 // - extends
3256 // - properties
3257 // - has_static_computed_names
RewriteClassLiteral(ClassScope * block_scope,const AstRawString * name,ClassInfo * class_info,int pos,int end_pos)3258 Expression* Parser::RewriteClassLiteral(ClassScope* block_scope,
3259 const AstRawString* name,
3260 ClassInfo* class_info, int pos,
3261 int end_pos) {
3262 DCHECK_NOT_NULL(block_scope);
3263 DCHECK_EQ(block_scope->scope_type(), CLASS_SCOPE);
3264 DCHECK_EQ(block_scope->language_mode(), LanguageMode::kStrict);
3265
3266 bool has_extends = class_info->extends != nullptr;
3267 bool has_default_constructor = class_info->constructor == nullptr;
3268 if (has_default_constructor) {
3269 class_info->constructor =
3270 DefaultConstructor(name, has_extends, pos, end_pos);
3271 }
3272
3273 if (name != nullptr) {
3274 DCHECK_NOT_NULL(block_scope->class_variable());
3275 block_scope->class_variable()->set_initializer_position(end_pos);
3276 }
3277
3278 FunctionLiteral* static_initializer = nullptr;
3279 if (class_info->has_static_elements) {
3280 static_initializer = CreateInitializerFunction(
3281 "<static_initializer>", class_info->static_elements_scope,
3282 factory()->NewInitializeClassStaticElementsStatement(
3283 class_info->static_elements, kNoSourcePosition));
3284 }
3285
3286 FunctionLiteral* instance_members_initializer_function = nullptr;
3287 if (class_info->has_instance_members) {
3288 instance_members_initializer_function = CreateInitializerFunction(
3289 "<instance_members_initializer>", class_info->instance_members_scope,
3290 factory()->NewInitializeClassMembersStatement(
3291 class_info->instance_fields, kNoSourcePosition));
3292 class_info->constructor->set_requires_instance_members_initializer(true);
3293 class_info->constructor->add_expected_properties(
3294 class_info->instance_fields->length());
3295 }
3296
3297 if (class_info->requires_brand) {
3298 class_info->constructor->set_class_scope_has_private_brand(true);
3299 }
3300 if (class_info->has_static_private_methods) {
3301 class_info->constructor->set_has_static_private_methods_or_accessors(true);
3302 }
3303 ClassLiteral* class_literal = factory()->NewClassLiteral(
3304 block_scope, class_info->extends, class_info->constructor,
3305 class_info->public_members, class_info->private_members,
3306 static_initializer, instance_members_initializer_function, pos, end_pos,
3307 class_info->has_static_computed_names, class_info->is_anonymous,
3308 class_info->has_private_methods, class_info->home_object_variable,
3309 class_info->static_home_object_variable);
3310
3311 AddFunctionForNameInference(class_info->constructor);
3312 return class_literal;
3313 }
3314
InsertShadowingVarBindingInitializers(Block * inner_block)3315 void Parser::InsertShadowingVarBindingInitializers(Block* inner_block) {
3316 // For each var-binding that shadows a parameter, insert an assignment
3317 // initializing the variable with the parameter.
3318 Scope* inner_scope = inner_block->scope();
3319 DCHECK(inner_scope->is_declaration_scope());
3320 Scope* function_scope = inner_scope->outer_scope();
3321 DCHECK(function_scope->is_function_scope());
3322 BlockState block_state(&scope_, inner_scope);
3323 for (Declaration* decl : *inner_scope->declarations()) {
3324 if (decl->var()->mode() != VariableMode::kVar ||
3325 !decl->IsVariableDeclaration()) {
3326 continue;
3327 }
3328 const AstRawString* name = decl->var()->raw_name();
3329 Variable* parameter = function_scope->LookupLocal(name);
3330 if (parameter == nullptr) continue;
3331 VariableProxy* to = NewUnresolved(name);
3332 VariableProxy* from = factory()->NewVariableProxy(parameter);
3333 Expression* assignment =
3334 factory()->NewAssignment(Token::ASSIGN, to, from, kNoSourcePosition);
3335 Statement* statement =
3336 factory()->NewExpressionStatement(assignment, kNoSourcePosition);
3337 inner_block->statements()->InsertAt(0, statement, zone());
3338 }
3339 }
3340
InsertSloppyBlockFunctionVarBindings(DeclarationScope * scope)3341 void Parser::InsertSloppyBlockFunctionVarBindings(DeclarationScope* scope) {
3342 // For the outermost eval scope, we cannot hoist during parsing: let
3343 // declarations in the surrounding scope may prevent hoisting, but the
3344 // information is unaccessible during parsing. In this case, we hoist later in
3345 // DeclarationScope::Analyze.
3346 if (scope->is_eval_scope() && scope->outer_scope() == original_scope_) {
3347 return;
3348 }
3349 scope->HoistSloppyBlockFunctions(factory());
3350 }
3351
3352 // ----------------------------------------------------------------------------
3353 // Parser support
3354
3355 template <typename IsolateT>
HandleSourceURLComments(IsolateT * isolate,Handle<Script> script)3356 void Parser::HandleSourceURLComments(IsolateT* isolate, Handle<Script> script) {
3357 Handle<String> source_url = scanner_.SourceUrl(isolate);
3358 if (!source_url.is_null()) {
3359 script->set_source_url(*source_url);
3360 }
3361 Handle<String> source_mapping_url = scanner_.SourceMappingUrl(isolate);
3362 if (!source_mapping_url.is_null()) {
3363 script->set_source_mapping_url(*source_mapping_url);
3364 }
3365 }
3366
3367 template void Parser::HandleSourceURLComments(Isolate* isolate,
3368 Handle<Script> script);
3369 template void Parser::HandleSourceURLComments(LocalIsolate* isolate,
3370 Handle<Script> script);
3371
UpdateStatistics(Isolate * isolate,Handle<Script> script)3372 void Parser::UpdateStatistics(Isolate* isolate, Handle<Script> script) {
3373 CHECK_NOT_NULL(isolate);
3374
3375 // Move statistics to Isolate.
3376 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
3377 ++feature) {
3378 if (use_counts_[feature] > 0) {
3379 isolate->CountUsage(v8::Isolate::UseCounterFeature(feature));
3380 }
3381 }
3382 if (scanner_.FoundHtmlComment()) {
3383 isolate->CountUsage(v8::Isolate::kHtmlComment);
3384 if (script->line_offset() == 0 && script->column_offset() == 0) {
3385 isolate->CountUsage(v8::Isolate::kHtmlCommentInExternalScript);
3386 }
3387 }
3388 isolate->counters()->total_preparse_skipped()->Increment(
3389 total_preparse_skipped_);
3390 }
3391
UpdateStatistics(Handle<Script> script,base::SmallVector<v8::Isolate::UseCounterFeature,8> * use_counts,int * preparse_skipped)3392 void Parser::UpdateStatistics(
3393 Handle<Script> script,
3394 base::SmallVector<v8::Isolate::UseCounterFeature, 8>* use_counts,
3395 int* preparse_skipped) {
3396 // Move statistics to Isolate.
3397 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
3398 ++feature) {
3399 if (use_counts_[feature] > 0) {
3400 use_counts->emplace_back(v8::Isolate::UseCounterFeature(feature));
3401 }
3402 }
3403 if (scanner_.FoundHtmlComment()) {
3404 use_counts->emplace_back(v8::Isolate::kHtmlComment);
3405 if (script->line_offset() == 0 && script->column_offset() == 0) {
3406 use_counts->emplace_back(v8::Isolate::kHtmlCommentInExternalScript);
3407 }
3408 }
3409 *preparse_skipped = total_preparse_skipped_;
3410 }
3411
ParseOnBackground(LocalIsolate * isolate,ParseInfo * info,int start_position,int end_position,int function_literal_id)3412 void Parser::ParseOnBackground(LocalIsolate* isolate, ParseInfo* info,
3413 int start_position, int end_position,
3414 int function_literal_id) {
3415 RCS_SCOPE(isolate, RuntimeCallCounterId::kParseProgram,
3416 RuntimeCallStats::CounterMode::kThreadSpecific);
3417 parsing_on_main_thread_ = false;
3418
3419 DCHECK_NULL(info->literal());
3420 FunctionLiteral* result = nullptr;
3421 {
3422 // We can park the isolate while parsing, it doesn't need to allocate or
3423 // access the main thread.
3424 ParkedScope parked_scope(isolate);
3425 overall_parse_is_parked_ = true;
3426
3427 scanner_.Initialize();
3428
3429 DCHECK(original_scope_);
3430
3431 // When streaming, we don't know the length of the source until we have
3432 // parsed it. The raw data can be UTF-8, so we wouldn't know the source
3433 // length until we have decoded it anyway even if we knew the raw data
3434 // length (which we don't). We work around this by storing all the scopes
3435 // which need their end position set at the end of the script (the top scope
3436 // and possible eval scopes) and set their end position after we know the
3437 // script length.
3438 if (flags().is_toplevel()) {
3439 DCHECK_EQ(start_position, 0);
3440 DCHECK_EQ(end_position, 0);
3441 DCHECK_EQ(function_literal_id, kFunctionLiteralIdTopLevel);
3442 result = DoParseProgram(/* isolate = */ nullptr, info);
3443 } else {
3444 base::Optional<ClassScope::HeritageParsingScope> heritage;
3445 if (V8_UNLIKELY(flags().private_name_lookup_skips_outer_class() &&
3446 original_scope_->is_class_scope())) {
3447 // If the function skips the outer class and the outer scope is a class,
3448 // the function is in heritage position. Otherwise the function scope's
3449 // skip bit will be correctly inherited from the outer scope.
3450 heritage.emplace(original_scope_->AsClassScope());
3451 }
3452 result = DoParseFunction(/* isolate = */ nullptr, info, start_position,
3453 end_position, function_literal_id,
3454 info->function_name());
3455 }
3456 MaybeProcessSourceRanges(info, result, stack_limit_);
3457 }
3458 // We need to unpark by now though, to be able to internalize.
3459 PostProcessParseResult(isolate, info, result);
3460 if (flags().is_toplevel()) {
3461 HandleSourceURLComments(isolate, script_);
3462 }
3463 }
3464
OpenTemplateLiteral(int pos)3465 Parser::TemplateLiteralState Parser::OpenTemplateLiteral(int pos) {
3466 return zone()->New<TemplateLiteral>(zone(), pos);
3467 }
3468
AddTemplateSpan(TemplateLiteralState * state,bool should_cook,bool tail)3469 void Parser::AddTemplateSpan(TemplateLiteralState* state, bool should_cook,
3470 bool tail) {
3471 int end = scanner()->location().end_pos - (tail ? 1 : 2);
3472 const AstRawString* raw = scanner()->CurrentRawSymbol(ast_value_factory());
3473 if (should_cook) {
3474 const AstRawString* cooked = scanner()->CurrentSymbol(ast_value_factory());
3475 (*state)->AddTemplateSpan(cooked, raw, end, zone());
3476 } else {
3477 (*state)->AddTemplateSpan(nullptr, raw, end, zone());
3478 }
3479 }
3480
AddTemplateExpression(TemplateLiteralState * state,Expression * expression)3481 void Parser::AddTemplateExpression(TemplateLiteralState* state,
3482 Expression* expression) {
3483 (*state)->AddExpression(expression, zone());
3484 }
3485
CloseTemplateLiteral(TemplateLiteralState * state,int start,Expression * tag)3486 Expression* Parser::CloseTemplateLiteral(TemplateLiteralState* state, int start,
3487 Expression* tag) {
3488 TemplateLiteral* lit = *state;
3489 int pos = lit->position();
3490 const ZonePtrList<const AstRawString>* cooked_strings = lit->cooked();
3491 const ZonePtrList<const AstRawString>* raw_strings = lit->raw();
3492 const ZonePtrList<Expression>* expressions = lit->expressions();
3493 DCHECK_EQ(cooked_strings->length(), raw_strings->length());
3494 DCHECK_EQ(cooked_strings->length(), expressions->length() + 1);
3495
3496 if (!tag) {
3497 if (cooked_strings->length() == 1) {
3498 return factory()->NewStringLiteral(cooked_strings->first(), pos);
3499 }
3500 return factory()->NewTemplateLiteral(cooked_strings, expressions, pos);
3501 } else {
3502 // GetTemplateObject
3503 Expression* template_object =
3504 factory()->NewGetTemplateObject(cooked_strings, raw_strings, pos);
3505
3506 // Call TagFn
3507 ScopedPtrList<Expression> call_args(pointer_buffer());
3508 call_args.Add(template_object);
3509 call_args.AddAll(expressions->ToConstVector());
3510 return factory()->NewTaggedTemplate(tag, call_args, pos);
3511 }
3512 }
3513
ArrayLiteralFromListWithSpread(const ScopedPtrList<Expression> & list)3514 ArrayLiteral* Parser::ArrayLiteralFromListWithSpread(
3515 const ScopedPtrList<Expression>& list) {
3516 // If there's only a single spread argument, a fast path using CallWithSpread
3517 // is taken.
3518 DCHECK_LT(1, list.length());
3519
3520 // The arguments of the spread call become a single ArrayLiteral.
3521 int first_spread = 0;
3522 for (; first_spread < list.length() && !list.at(first_spread)->IsSpread();
3523 ++first_spread) {
3524 }
3525
3526 DCHECK_LT(first_spread, list.length());
3527 return factory()->NewArrayLiteral(list, first_spread, kNoSourcePosition);
3528 }
3529
SetLanguageMode(Scope * scope,LanguageMode mode)3530 void Parser::SetLanguageMode(Scope* scope, LanguageMode mode) {
3531 v8::Isolate::UseCounterFeature feature;
3532 if (is_sloppy(mode))
3533 feature = v8::Isolate::kSloppyMode;
3534 else if (is_strict(mode))
3535 feature = v8::Isolate::kStrictMode;
3536 else
3537 UNREACHABLE();
3538 ++use_counts_[feature];
3539 scope->SetLanguageMode(mode);
3540 }
3541
3542 #if V8_ENABLE_WEBASSEMBLY
SetAsmModule()3543 void Parser::SetAsmModule() {
3544 // Store the usage count; The actual use counter on the isolate is
3545 // incremented after parsing is done.
3546 ++use_counts_[v8::Isolate::kUseAsm];
3547 DCHECK(scope()->is_declaration_scope());
3548 scope()->AsDeclarationScope()->set_is_asm_module();
3549 info_->set_contains_asm_module(true);
3550 }
3551 #endif // V8_ENABLE_WEBASSEMBLY
3552
ExpressionListToExpression(const ScopedPtrList<Expression> & args)3553 Expression* Parser::ExpressionListToExpression(
3554 const ScopedPtrList<Expression>& args) {
3555 Expression* expr = args.at(0);
3556 if (args.length() == 1) return expr;
3557 if (args.length() == 2) {
3558 return factory()->NewBinaryOperation(Token::COMMA, expr, args.at(1),
3559 args.at(1)->position());
3560 }
3561 NaryOperation* result =
3562 factory()->NewNaryOperation(Token::COMMA, expr, args.length() - 1);
3563 for (int i = 1; i < args.length(); i++) {
3564 result->AddSubsequent(args.at(i), args.at(i)->position());
3565 }
3566 return result;
3567 }
3568
3569 // This method completes the desugaring of the body of async_function.
RewriteAsyncFunctionBody(ScopedPtrList<Statement> * body,Block * block,Expression * return_value,REPLMode repl_mode)3570 void Parser::RewriteAsyncFunctionBody(ScopedPtrList<Statement>* body,
3571 Block* block, Expression* return_value,
3572 REPLMode repl_mode) {
3573 // function async_function() {
3574 // .generator_object = %_AsyncFunctionEnter();
3575 // BuildRejectPromiseOnException({
3576 // ... block ...
3577 // return %_AsyncFunctionResolve(.generator_object, expr);
3578 // })
3579 // }
3580
3581 block->statements()->Add(factory()->NewSyntheticAsyncReturnStatement(
3582 return_value, return_value->position()),
3583 zone());
3584 block = BuildRejectPromiseOnException(block, repl_mode);
3585 body->Add(block);
3586 }
3587
SetFunctionNameFromPropertyName(LiteralProperty * property,const AstRawString * name,const AstRawString * prefix)3588 void Parser::SetFunctionNameFromPropertyName(LiteralProperty* property,
3589 const AstRawString* name,
3590 const AstRawString* prefix) {
3591 if (has_error()) return;
3592 // Ensure that the function we are going to create has shared name iff
3593 // we are not going to set it later.
3594 if (property->NeedsSetFunctionName()) {
3595 name = nullptr;
3596 prefix = nullptr;
3597 } else {
3598 // If the property value is an anonymous function or an anonymous class or
3599 // a concise method or an accessor function which doesn't require the name
3600 // to be set then the shared name must be provided.
3601 DCHECK_IMPLIES(property->value()->IsAnonymousFunctionDefinition() ||
3602 property->value()->IsConciseMethodDefinition() ||
3603 property->value()->IsAccessorFunctionDefinition(),
3604 name != nullptr);
3605 }
3606
3607 Expression* value = property->value();
3608 SetFunctionName(value, name, prefix);
3609 }
3610
SetFunctionNameFromPropertyName(ObjectLiteralProperty * property,const AstRawString * name,const AstRawString * prefix)3611 void Parser::SetFunctionNameFromPropertyName(ObjectLiteralProperty* property,
3612 const AstRawString* name,
3613 const AstRawString* prefix) {
3614 // Ignore "__proto__" as a name when it's being used to set the [[Prototype]]
3615 // of an object literal.
3616 // See ES #sec-__proto__-property-names-in-object-initializers.
3617 if (property->IsPrototype() || has_error()) return;
3618
3619 DCHECK(!property->value()->IsAnonymousFunctionDefinition() ||
3620 property->kind() == ObjectLiteralProperty::COMPUTED);
3621
3622 SetFunctionNameFromPropertyName(static_cast<LiteralProperty*>(property), name,
3623 prefix);
3624 }
3625
SetFunctionNameFromIdentifierRef(Expression * value,Expression * identifier)3626 void Parser::SetFunctionNameFromIdentifierRef(Expression* value,
3627 Expression* identifier) {
3628 if (!identifier->IsVariableProxy()) return;
3629 // IsIdentifierRef of parenthesized expressions is false.
3630 if (identifier->is_parenthesized()) return;
3631 SetFunctionName(value, identifier->AsVariableProxy()->raw_name());
3632 }
3633
SetFunctionName(Expression * value,const AstRawString * name,const AstRawString * prefix)3634 void Parser::SetFunctionName(Expression* value, const AstRawString* name,
3635 const AstRawString* prefix) {
3636 if (!value->IsAnonymousFunctionDefinition() &&
3637 !value->IsConciseMethodDefinition() &&
3638 !value->IsAccessorFunctionDefinition()) {
3639 return;
3640 }
3641 auto function = value->AsFunctionLiteral();
3642 if (value->IsClassLiteral()) {
3643 function = value->AsClassLiteral()->constructor();
3644 }
3645 if (function != nullptr) {
3646 AstConsString* cons_name = nullptr;
3647 if (name != nullptr) {
3648 if (prefix != nullptr) {
3649 cons_name = ast_value_factory()->NewConsString(prefix, name);
3650 } else {
3651 cons_name = ast_value_factory()->NewConsString(name);
3652 }
3653 } else {
3654 DCHECK_NULL(prefix);
3655 }
3656 function->set_raw_name(cons_name);
3657 }
3658 }
3659
CheckCallable(Variable * var,Expression * error,int pos)3660 Statement* Parser::CheckCallable(Variable* var, Expression* error, int pos) {
3661 const int nopos = kNoSourcePosition;
3662 Statement* validate_var;
3663 {
3664 Expression* type_of = factory()->NewUnaryOperation(
3665 Token::TYPEOF, factory()->NewVariableProxy(var), nopos);
3666 Expression* function_literal = factory()->NewStringLiteral(
3667 ast_value_factory()->function_string(), nopos);
3668 Expression* condition = factory()->NewCompareOperation(
3669 Token::EQ_STRICT, type_of, function_literal, nopos);
3670
3671 Statement* throw_call = factory()->NewExpressionStatement(error, pos);
3672
3673 validate_var = factory()->NewIfStatement(
3674 condition, factory()->EmptyStatement(), throw_call, nopos);
3675 }
3676 return validate_var;
3677 }
3678
3679 } // namespace internal
3680 } // namespace v8
3681