Line data Source code
1 : // Copyright 2012 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_PARSING_PREPARSER_H_
6 : #define V8_PARSING_PREPARSER_H_
7 :
8 : #include "src/ast/ast.h"
9 : #include "src/ast/scopes.h"
10 : #include "src/parsing/parser-base.h"
11 : #include "src/parsing/preparser-logger.h"
12 : #include "src/pending-compilation-error-handler.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 :
17 : // Whereas the Parser generates AST during the recursive descent,
18 : // the PreParser doesn't create a tree. Instead, it passes around minimal
19 : // data objects (PreParserExpression, PreParserIdentifier etc.) which contain
20 : // just enough data for the upper layer functions. PreParserFactory is
21 : // responsible for creating these dummy objects. It provides a similar kind of
22 : // interface as AstNodeFactory, so ParserBase doesn't need to care which one is
23 : // used.
24 :
25 : class PreparseDataBuilder;
26 :
27 : class PreParserIdentifier {
28 : public:
29 9229106 : PreParserIdentifier() : type_(kUnknownIdentifier) {}
30 : static PreParserIdentifier Default() {
31 : return PreParserIdentifier(kUnknownIdentifier);
32 : }
33 : static PreParserIdentifier Null() {
34 : return PreParserIdentifier(kNullIdentifier);
35 : }
36 : static PreParserIdentifier Eval() {
37 : return PreParserIdentifier(kEvalIdentifier);
38 : }
39 : static PreParserIdentifier Arguments() {
40 : return PreParserIdentifier(kArgumentsIdentifier);
41 : }
42 : static PreParserIdentifier Constructor() {
43 : return PreParserIdentifier(kConstructorIdentifier);
44 : }
45 : static PreParserIdentifier Await() {
46 : return PreParserIdentifier(kAwaitIdentifier);
47 : }
48 : static PreParserIdentifier Async() {
49 : return PreParserIdentifier(kAsyncIdentifier);
50 : }
51 : static PreParserIdentifier Name() {
52 : return PreParserIdentifier(kNameIdentifier);
53 : }
54 : static PreParserIdentifier PrivateName() {
55 : return PreParserIdentifier(kPrivateNameIdentifier);
56 : }
57 130571 : bool IsNull() const { return type_ == kNullIdentifier; }
58 : bool IsEval() const { return type_ == kEvalIdentifier; }
59 : bool IsAsync() const { return type_ == kAsyncIdentifier; }
60 40782406 : bool IsArguments() const { return type_ == kArgumentsIdentifier; }
61 : bool IsEvalOrArguments() const {
62 : STATIC_ASSERT(kEvalIdentifier + 1 == kArgumentsIdentifier);
63 : return IsInRange(type_, kEvalIdentifier, kArgumentsIdentifier);
64 : }
65 : bool IsConstructor() const { return type_ == kConstructorIdentifier; }
66 : bool IsAwait() const { return type_ == kAwaitIdentifier; }
67 : bool IsName() const { return type_ == kNameIdentifier; }
68 : bool IsPrivateName() const { return type_ == kPrivateNameIdentifier; }
69 :
70 : private:
71 : enum Type : uint8_t {
72 : kNullIdentifier,
73 : kUnknownIdentifier,
74 : kEvalIdentifier,
75 : kArgumentsIdentifier,
76 : kConstructorIdentifier,
77 : kAwaitIdentifier,
78 : kAsyncIdentifier,
79 : kNameIdentifier,
80 : kPrivateNameIdentifier
81 : };
82 :
83 : explicit PreParserIdentifier(Type type) : string_(nullptr), type_(type) {}
84 : const AstRawString* string_;
85 :
86 : Type type_;
87 : friend class PreParserExpression;
88 : friend class PreParser;
89 : friend class PreParserFactory;
90 : };
91 :
92 : class PreParserExpression {
93 : public:
94 : PreParserExpression() : code_(TypeField::encode(kNull)) {}
95 :
96 : static PreParserExpression Null() { return PreParserExpression(); }
97 : static PreParserExpression Failure() {
98 : return PreParserExpression(TypeField::encode(kFailure));
99 : }
100 :
101 : static PreParserExpression Default() {
102 : return PreParserExpression(TypeField::encode(kExpression));
103 : }
104 :
105 : static PreParserExpression Spread(const PreParserExpression& expression) {
106 : return PreParserExpression(TypeField::encode(kSpreadExpression));
107 : }
108 :
109 : static PreParserExpression FromIdentifier(const PreParserIdentifier& id) {
110 : return PreParserExpression(TypeField::encode(kIdentifierExpression) |
111 40726170 : IdentifierTypeField::encode(id.type_));
112 : }
113 :
114 : static PreParserExpression BinaryOperation(const PreParserExpression& left,
115 : Token::Value op,
116 : const PreParserExpression& right,
117 : Zone* zone) {
118 : return PreParserExpression(TypeField::encode(kExpression));
119 : }
120 :
121 : static PreParserExpression Assignment() {
122 : return PreParserExpression(TypeField::encode(kExpression) |
123 : ExpressionTypeField::encode(kAssignment));
124 : }
125 :
126 : static PreParserExpression NewTargetExpression() {
127 : return PreParserExpression::Default();
128 : }
129 :
130 : static PreParserExpression ObjectLiteral() {
131 : return PreParserExpression(TypeField::encode(kObjectLiteralExpression));
132 : }
133 :
134 : static PreParserExpression ArrayLiteral() {
135 : return PreParserExpression(TypeField::encode(kArrayLiteralExpression));
136 : }
137 :
138 : static PreParserExpression StringLiteral() {
139 : return PreParserExpression(TypeField::encode(kStringLiteralExpression));
140 : }
141 :
142 : static PreParserExpression This() {
143 : return PreParserExpression(TypeField::encode(kExpression) |
144 : ExpressionTypeField::encode(kThisExpression));
145 : }
146 :
147 : static PreParserExpression ThisPropertyWithPrivateFieldKey() {
148 : return PreParserExpression(TypeField::encode(kExpression) |
149 : ExpressionTypeField::encode(
150 : kThisPropertyExpressionWithPrivateFieldKey));
151 : }
152 :
153 : static PreParserExpression ThisProperty() {
154 : return PreParserExpression(
155 : TypeField::encode(kExpression) |
156 : ExpressionTypeField::encode(kThisPropertyExpression));
157 : }
158 :
159 : static PreParserExpression Property() {
160 : return PreParserExpression(
161 : TypeField::encode(kExpression) |
162 : ExpressionTypeField::encode(kPropertyExpression));
163 : }
164 :
165 : static PreParserExpression PropertyWithPrivateFieldKey() {
166 : return PreParserExpression(
167 : TypeField::encode(kExpression) |
168 : ExpressionTypeField::encode(kPropertyExpressionWithPrivateFieldKey));
169 : }
170 :
171 : static PreParserExpression Call() {
172 : return PreParserExpression(TypeField::encode(kExpression) |
173 : ExpressionTypeField::encode(kCallExpression));
174 : }
175 :
176 : static PreParserExpression CallEval() {
177 : return PreParserExpression(
178 : TypeField::encode(kExpression) |
179 : ExpressionTypeField::encode(kCallEvalExpression));
180 : }
181 :
182 : static PreParserExpression CallTaggedTemplate() {
183 : return PreParserExpression(
184 : TypeField::encode(kExpression) |
185 : ExpressionTypeField::encode(kCallTaggedTemplateExpression));
186 : }
187 :
188 : bool is_tagged_template() const {
189 : DCHECK(IsCall());
190 1058 : return ExpressionTypeField::decode(code_) == kCallTaggedTemplateExpression;
191 : }
192 :
193 : static PreParserExpression SuperCallReference() {
194 : return PreParserExpression(
195 : TypeField::encode(kExpression) |
196 : ExpressionTypeField::encode(kSuperCallReference));
197 : }
198 :
199 53460 : bool IsNull() const { return TypeField::decode(code_) == kNull; }
200 : bool IsFailureExpression() const {
201 : return TypeField::decode(code_) == kFailure;
202 : }
203 :
204 : bool IsIdentifier() const {
205 25641371 : return TypeField::decode(code_) == kIdentifierExpression;
206 : }
207 :
208 : PreParserIdentifier AsIdentifier() const {
209 : DCHECK(IsIdentifier());
210 : return PreParserIdentifier(IdentifierTypeField::decode(code_));
211 : }
212 :
213 : bool IsAssignment() const {
214 51060167 : return TypeField::decode(code_) == kExpression &&
215 : ExpressionTypeField::decode(code_) == kAssignment;
216 : }
217 :
218 : bool IsObjectLiteral() const {
219 : return TypeField::decode(code_) == kObjectLiteralExpression;
220 : }
221 :
222 : bool IsArrayLiteral() const {
223 : return TypeField::decode(code_) == kArrayLiteralExpression;
224 : }
225 :
226 : bool IsPattern() const {
227 : STATIC_ASSERT(kObjectLiteralExpression + 1 == kArrayLiteralExpression);
228 : return IsInRange(TypeField::decode(code_), kObjectLiteralExpression,
229 : kArrayLiteralExpression);
230 : }
231 :
232 : bool IsStringLiteral() const {
233 : return TypeField::decode(code_) == kStringLiteralExpression;
234 : }
235 :
236 : bool IsThis() const {
237 27336268 : return TypeField::decode(code_) == kExpression &&
238 : ExpressionTypeField::decode(code_) == kThisExpression;
239 : }
240 :
241 : bool IsThisProperty() const {
242 9744796 : return TypeField::decode(code_) == kExpression &&
243 1822333 : (ExpressionTypeField::decode(code_) == kThisPropertyExpression ||
244 : ExpressionTypeField::decode(code_) ==
245 : kThisPropertyExpressionWithPrivateFieldKey);
246 : }
247 :
248 : bool IsProperty() const {
249 12668239 : return TypeField::decode(code_) == kExpression &&
250 4032429 : (ExpressionTypeField::decode(code_) == kPropertyExpression ||
251 1636758 : ExpressionTypeField::decode(code_) == kThisPropertyExpression ||
252 : ExpressionTypeField::decode(code_) ==
253 1636699 : kPropertyExpressionWithPrivateFieldKey ||
254 : ExpressionTypeField::decode(code_) ==
255 : kThisPropertyExpressionWithPrivateFieldKey);
256 : }
257 :
258 : bool IsPropertyWithPrivateFieldKey() const {
259 18076 : return TypeField::decode(code_) == kExpression &&
260 : (ExpressionTypeField::decode(code_) ==
261 6805 : kPropertyExpressionWithPrivateFieldKey ||
262 : ExpressionTypeField::decode(code_) ==
263 : kThisPropertyExpressionWithPrivateFieldKey);
264 : }
265 :
266 : bool IsCall() const {
267 22160 : return TypeField::decode(code_) == kExpression &&
268 7934 : (ExpressionTypeField::decode(code_) == kCallExpression ||
269 7934 : ExpressionTypeField::decode(code_) == kCallEvalExpression ||
270 : ExpressionTypeField::decode(code_) ==
271 : kCallTaggedTemplateExpression);
272 : }
273 : PreParserExpression* AsCall() {
274 1058 : if (IsCall()) return this;
275 : return nullptr;
276 : }
277 :
278 : bool IsSuperCallReference() const {
279 : return TypeField::decode(code_) == kExpression &&
280 : ExpressionTypeField::decode(code_) == kSuperCallReference;
281 : }
282 :
283 : bool IsValidReferenceExpression() const {
284 : return IsIdentifier() || IsProperty();
285 : }
286 :
287 : // At the moment PreParser doesn't track these expression types.
288 : bool IsFunctionLiteral() const { return false; }
289 : bool IsCallNew() const { return false; }
290 :
291 : bool IsSpread() const {
292 : return TypeField::decode(code_) == kSpreadExpression;
293 : }
294 :
295 119617 : bool is_parenthesized() const { return IsParenthesizedField::decode(code_); }
296 :
297 : void mark_parenthesized() {
298 : code_ = IsParenthesizedField::update(code_, true);
299 : }
300 :
301 : void clear_parenthesized() {
302 : code_ = IsParenthesizedField::update(code_, false);
303 : }
304 :
305 : PreParserExpression AsFunctionLiteral() { return *this; }
306 :
307 : // Dummy implementation for making expression->somefunc() work in both Parser
308 : // and PreParser.
309 : PreParserExpression* operator->() { return this; }
310 :
311 : // More dummy implementations of things PreParser doesn't need to track:
312 : void SetShouldEagerCompile() {}
313 : void mark_as_oneshot_iife() {}
314 :
315 : int position() const { return kNoSourcePosition; }
316 : void set_function_token_position(int position) {}
317 : void set_scope(Scope* scope) {}
318 : void set_suspend_count(int suspend_count) {}
319 :
320 : private:
321 : enum Type {
322 : kNull,
323 : kFailure,
324 : kExpression,
325 : kIdentifierExpression,
326 : kStringLiteralExpression,
327 : kSpreadExpression,
328 : kObjectLiteralExpression,
329 : kArrayLiteralExpression
330 : };
331 :
332 : enum ExpressionType {
333 : kThisExpression,
334 : kThisPropertyExpression,
335 : kThisPropertyExpressionWithPrivateFieldKey,
336 : kPropertyExpression,
337 : kPropertyExpressionWithPrivateFieldKey,
338 : kCallExpression,
339 : kCallEvalExpression,
340 : kCallTaggedTemplateExpression,
341 : kSuperCallReference,
342 : kAssignment
343 : };
344 :
345 : explicit PreParserExpression(uint32_t expression_code)
346 : : code_(expression_code) {}
347 :
348 : // The first three bits are for the Type.
349 : using TypeField = BitField<Type, 0, 3>;
350 :
351 : // The high order bit applies only to nodes which would inherit from the
352 : // Expression ASTNode --- This is by necessity, due to the fact that
353 : // Expression nodes may be represented as multiple Types, not exclusively
354 : // through kExpression.
355 : // TODO(caitp, adamk): clean up PreParserExpression bitfields.
356 : using IsParenthesizedField = BitField<bool, TypeField::kNext, 1>;
357 :
358 : // The rest of the bits are interpreted depending on the value
359 : // of the Type field, so they can share the storage.
360 : using ExpressionTypeField =
361 : BitField<ExpressionType, IsParenthesizedField::kNext, 4>;
362 : using IdentifierTypeField =
363 : BitField<PreParserIdentifier::Type, IsParenthesizedField::kNext, 8>;
364 : using HasCoverInitializedNameField =
365 : BitField<bool, IsParenthesizedField::kNext, 1>;
366 :
367 : uint32_t code_;
368 : friend class PreParser;
369 : friend class PreParserFactory;
370 : friend class PreParserExpressionList;
371 : };
372 :
373 : class PreParserStatement;
374 : class PreParserStatementList {
375 : public:
376 : PreParserStatementList() : PreParserStatementList(false) {}
377 : PreParserStatementList* operator->() { return this; }
378 : void Add(const PreParserStatement& element, Zone* zone) {}
379 : static PreParserStatementList Null() { return PreParserStatementList(true); }
380 : bool IsNull() const { return is_null_; }
381 :
382 : private:
383 : explicit PreParserStatementList(bool is_null) : is_null_(is_null) {}
384 : bool is_null_;
385 : };
386 :
387 : class PreParserScopedStatementList {
388 : public:
389 : explicit PreParserScopedStatementList(std::vector<void*>* buffer) {}
390 : void Rewind() {}
391 : void MergeInto(const PreParserScopedStatementList* other) {}
392 : void Add(const PreParserStatement& element) {}
393 : int length() { return 0; }
394 : };
395 :
396 : // The pre-parser doesn't need to build lists of expressions, identifiers, or
397 : // the like. If the PreParser is used in variable tracking mode, it needs to
398 : // build lists of variables though.
399 : class PreParserExpressionList {
400 : public:
401 28992097 : explicit PreParserExpressionList(std::vector<void*>* buffer) : length_(0) {}
402 :
403 28990834 : int length() const { return length_; }
404 :
405 : void Add(const PreParserExpression& expression) {
406 40105290 : ++length_;
407 : }
408 :
409 : private:
410 : int length_;
411 :
412 : friend class PreParser;
413 : friend class PreParserFactory;
414 : };
415 :
416 : class PreParserStatement {
417 : public:
418 : static PreParserStatement Default() {
419 : return PreParserStatement(kUnknownStatement);
420 : }
421 :
422 : static PreParserStatement Null() {
423 : return PreParserStatement(kNullStatement);
424 : }
425 :
426 : static PreParserStatement Empty() {
427 : return PreParserStatement(kEmptyStatement);
428 : }
429 :
430 : static PreParserStatement Jump() {
431 : return PreParserStatement(kJumpStatement);
432 : }
433 :
434 : void InitializeStatements(const PreParserScopedStatementList& statements,
435 : Zone* zone) {}
436 :
437 : // Creates expression statement from expression.
438 : // Preserves being an unparenthesized string literal, possibly
439 : // "use strict".
440 : static PreParserStatement ExpressionStatement(
441 : const PreParserExpression& expression) {
442 9860524 : if (expression.IsStringLiteral()) {
443 : return PreParserStatement(kStringLiteralExpressionStatement);
444 : }
445 : return Default();
446 : }
447 :
448 : bool IsStringLiteral() { return code_ == kStringLiteralExpressionStatement; }
449 :
450 : bool IsJumpStatement() {
451 : return code_ == kJumpStatement;
452 : }
453 :
454 : bool IsNull() { return code_ == kNullStatement; }
455 :
456 : bool IsEmptyStatement() {
457 : DCHECK(!IsNull());
458 : return code_ == kEmptyStatement;
459 : }
460 :
461 : // Dummy implementation for making statement->somefunc() work in both Parser
462 : // and PreParser.
463 : PreParserStatement* operator->() { return this; }
464 :
465 : PreParserStatementList statements() { return PreParserStatementList(); }
466 : PreParserStatementList cases() { return PreParserStatementList(); }
467 :
468 : void set_scope(Scope* scope) {}
469 : void Initialize(const PreParserExpression& cond, PreParserStatement body,
470 : const SourceRange& body_range = {}) {}
471 : void Initialize(PreParserStatement init, const PreParserExpression& cond,
472 : PreParserStatement next, PreParserStatement body,
473 : const SourceRange& body_range = {}) {}
474 : void Initialize(PreParserExpression each, const PreParserExpression& subject,
475 : PreParserStatement body, const SourceRange& body_range = {}) {
476 : }
477 :
478 : protected:
479 : enum Type {
480 : kNullStatement,
481 : kEmptyStatement,
482 : kUnknownStatement,
483 : kJumpStatement,
484 : kStringLiteralExpressionStatement,
485 : };
486 :
487 : explicit PreParserStatement(Type code) : code_(code) {}
488 :
489 : private:
490 : Type code_;
491 : };
492 :
493 : // A PreParserBlock extends statement with a place to store the scope.
494 : // The scope is dropped as the block is returned as a statement.
495 : class PreParserBlock : public PreParserStatement {
496 : public:
497 : void set_scope(Scope* scope) { scope_ = scope; }
498 : Scope* scope() const { return scope_; }
499 : static PreParserBlock Default() {
500 : return PreParserBlock(PreParserStatement::kUnknownStatement);
501 : }
502 : static PreParserBlock Null() {
503 : return PreParserBlock(PreParserStatement::kNullStatement);
504 : }
505 : // Dummy implementation for making block->somefunc() work in both Parser and
506 : // PreParser.
507 : PreParserBlock* operator->() { return this; }
508 :
509 : private:
510 : explicit PreParserBlock(PreParserStatement::Type type)
511 : : PreParserStatement(type), scope_(nullptr) {}
512 : Scope* scope_;
513 : };
514 :
515 : class PreParserFactory {
516 : public:
517 : explicit PreParserFactory(AstValueFactory* ast_value_factory, Zone* zone)
518 924357 : : ast_node_factory_(ast_value_factory, zone), zone_(zone) {}
519 :
520 7362 : AstNodeFactory* ast_node_factory() { return &ast_node_factory_; }
521 :
522 : PreParserExpression NewStringLiteral(const PreParserIdentifier& identifier,
523 : int pos) {
524 : return PreParserExpression::Default();
525 : }
526 : PreParserExpression NewNumberLiteral(double number,
527 : int pos) {
528 : return PreParserExpression::Default();
529 : }
530 : PreParserExpression NewUndefinedLiteral(int pos) {
531 : return PreParserExpression::Default();
532 : }
533 : PreParserExpression NewTheHoleLiteral() {
534 : return PreParserExpression::Default();
535 : }
536 : PreParserExpression NewRegExpLiteral(const PreParserIdentifier& js_pattern,
537 : int js_flags, int pos) {
538 : return PreParserExpression::Default();
539 : }
540 : PreParserExpression NewArrayLiteral(const PreParserExpressionList& values,
541 : int first_spread_index, int pos) {
542 : return PreParserExpression::ArrayLiteral();
543 : }
544 : PreParserExpression NewClassLiteralProperty(const PreParserExpression& key,
545 : const PreParserExpression& value,
546 : ClassLiteralProperty::Kind kind,
547 : bool is_static,
548 : bool is_computed_name,
549 : bool is_private) {
550 : return PreParserExpression::Default();
551 : }
552 : PreParserExpression NewObjectLiteralProperty(const PreParserExpression& key,
553 : const PreParserExpression& value,
554 : ObjectLiteralProperty::Kind kind,
555 : bool is_computed_name) {
556 : return PreParserExpression::Default();
557 : }
558 : PreParserExpression NewObjectLiteralProperty(const PreParserExpression& key,
559 : const PreParserExpression& value,
560 : bool is_computed_name) {
561 : return PreParserExpression::Default();
562 : }
563 : PreParserExpression NewObjectLiteral(
564 : const PreParserExpressionList& properties, int boilerplate_properties,
565 : int pos, bool has_rest_property) {
566 : return PreParserExpression::ObjectLiteral();
567 : }
568 : PreParserExpression NewVariableProxy(void* variable) {
569 : return PreParserExpression::Default();
570 : }
571 :
572 11573200 : PreParserExpression NewProperty(const PreParserExpression& obj,
573 : const PreParserExpression& key, int pos) {
574 13485062 : if (key.IsIdentifier() && key.AsIdentifier().IsPrivateName()) {
575 3450 : if (obj.IsThis()) {
576 : return PreParserExpression::ThisPropertyWithPrivateFieldKey();
577 : }
578 : return PreParserExpression::PropertyWithPrivateFieldKey();
579 : }
580 :
581 11569750 : if (obj.IsThis()) {
582 : return PreParserExpression::ThisProperty();
583 : }
584 : return PreParserExpression::Property();
585 : }
586 : PreParserExpression NewUnaryOperation(Token::Value op,
587 : const PreParserExpression& expression,
588 : int pos) {
589 : return PreParserExpression::Default();
590 : }
591 : PreParserExpression NewBinaryOperation(Token::Value op,
592 : const PreParserExpression& left,
593 : const PreParserExpression& right,
594 : int pos) {
595 : return PreParserExpression::BinaryOperation(left, op, right, zone_);
596 : }
597 : PreParserExpression NewCompareOperation(Token::Value op,
598 : const PreParserExpression& left,
599 : const PreParserExpression& right,
600 : int pos) {
601 : return PreParserExpression::Default();
602 : }
603 : PreParserExpression NewAssignment(Token::Value op,
604 : const PreParserExpression& left,
605 : const PreParserExpression& right, int pos) {
606 : // Identifiers need to be tracked since this might be a parameter with a
607 : // default value inside an arrow function parameter list.
608 : return PreParserExpression::Assignment();
609 : }
610 : PreParserExpression NewYield(const PreParserExpression& expression, int pos,
611 : Suspend::OnAbruptResume on_abrupt_resume) {
612 : return PreParserExpression::Default();
613 : }
614 : PreParserExpression NewAwait(const PreParserExpression& expression, int pos) {
615 : return PreParserExpression::Default();
616 : }
617 : PreParserExpression NewYieldStar(const PreParserExpression& iterable,
618 : int pos) {
619 : return PreParserExpression::Default();
620 : }
621 : PreParserExpression NewConditional(const PreParserExpression& condition,
622 : const PreParserExpression& then_expression,
623 : const PreParserExpression& else_expression,
624 : int pos) {
625 : return PreParserExpression::Default();
626 : }
627 : PreParserExpression NewCountOperation(Token::Value op, bool is_prefix,
628 : const PreParserExpression& expression,
629 : int pos) {
630 : return PreParserExpression::Default();
631 : }
632 : PreParserExpression NewCall(
633 : PreParserExpression expression, const PreParserExpressionList& arguments,
634 : int pos, Call::PossiblyEval possibly_eval = Call::NOT_EVAL) {
635 6885713 : if (possibly_eval == Call::IS_POSSIBLY_EVAL) {
636 : DCHECK(expression.IsIdentifier() && expression.AsIdentifier().IsEval());
637 : return PreParserExpression::CallEval();
638 : }
639 : return PreParserExpression::Call();
640 : }
641 : PreParserExpression NewTaggedTemplate(
642 : PreParserExpression expression, const PreParserExpressionList& arguments,
643 : int pos) {
644 : return PreParserExpression::CallTaggedTemplate();
645 : }
646 : PreParserExpression NewCallNew(const PreParserExpression& expression,
647 : const PreParserExpressionList& arguments,
648 : int pos) {
649 : return PreParserExpression::Default();
650 : }
651 : PreParserStatement NewReturnStatement(
652 : const PreParserExpression& expression, int pos,
653 : int continuation_pos = kNoSourcePosition) {
654 : return PreParserStatement::Jump();
655 : }
656 : PreParserStatement NewAsyncReturnStatement(
657 : const PreParserExpression& expression, int pos,
658 : int continuation_pos = kNoSourcePosition) {
659 : return PreParserStatement::Jump();
660 : }
661 : PreParserExpression NewFunctionLiteral(
662 : const PreParserIdentifier& name, Scope* scope,
663 : const PreParserScopedStatementList& body, int expected_property_count,
664 : int parameter_count, int function_length,
665 : FunctionLiteral::ParameterFlag has_duplicate_parameters,
666 : FunctionLiteral::FunctionType function_type,
667 : FunctionLiteral::EagerCompileHint eager_compile_hint, int position,
668 : bool has_braces, int function_literal_id,
669 : ProducedPreparseData* produced_preparse_data = nullptr) {
670 : DCHECK_NULL(produced_preparse_data);
671 : return PreParserExpression::Default();
672 : }
673 :
674 : PreParserExpression NewSpread(const PreParserExpression& expression, int pos,
675 : int expr_pos) {
676 : return PreParserExpression::Spread(expression);
677 : }
678 :
679 : PreParserExpression NewEmptyParentheses(int pos) {
680 : PreParserExpression result = PreParserExpression::Default();
681 : result.mark_parenthesized();
682 : return result;
683 : }
684 :
685 : PreParserStatement EmptyStatement() { return PreParserStatement::Default(); }
686 :
687 : PreParserBlock NewBlock(int capacity, bool ignore_completion_value) {
688 : return PreParserBlock::Default();
689 : }
690 :
691 : PreParserBlock NewBlock(bool ignore_completion_value,
692 : ZonePtrList<const AstRawString>* labels) {
693 : return PreParserBlock::Default();
694 : }
695 :
696 : PreParserBlock NewBlock(bool ignore_completion_value,
697 : const PreParserScopedStatementList& list) {
698 : return PreParserBlock::Default();
699 : }
700 :
701 : PreParserStatement NewDebuggerStatement(int pos) {
702 : return PreParserStatement::Default();
703 : }
704 :
705 : PreParserStatement NewExpressionStatement(const PreParserExpression& expr,
706 : int pos) {
707 : return PreParserStatement::ExpressionStatement(expr);
708 : }
709 :
710 : PreParserStatement NewIfStatement(const PreParserExpression& condition,
711 : PreParserStatement then_statement,
712 : PreParserStatement else_statement, int pos,
713 : SourceRange then_range = {},
714 : SourceRange else_range = {}) {
715 : // This must return a jump statement iff both clauses are jump statements.
716 2844104 : return else_statement.IsJumpStatement() ? then_statement : else_statement;
717 : }
718 :
719 : PreParserStatement NewBreakStatement(
720 : PreParserStatement target, int pos,
721 : int continuation_pos = kNoSourcePosition) {
722 : return PreParserStatement::Jump();
723 : }
724 :
725 : PreParserStatement NewContinueStatement(
726 : PreParserStatement target, int pos,
727 : int continuation_pos = kNoSourcePosition) {
728 : return PreParserStatement::Jump();
729 : }
730 :
731 : PreParserStatement NewWithStatement(Scope* scope,
732 : const PreParserExpression& expression,
733 : PreParserStatement statement, int pos) {
734 : return PreParserStatement::Default();
735 : }
736 :
737 : PreParserStatement NewDoWhileStatement(
738 : ZonePtrList<const AstRawString>* labels,
739 : ZonePtrList<const AstRawString>* own_labels, int pos) {
740 : return PreParserStatement::Default();
741 : }
742 :
743 : PreParserStatement NewWhileStatement(
744 : ZonePtrList<const AstRawString>* labels,
745 : ZonePtrList<const AstRawString>* own_labels, int pos) {
746 : return PreParserStatement::Default();
747 : }
748 :
749 : PreParserStatement NewSwitchStatement(ZonePtrList<const AstRawString>* labels,
750 : const PreParserExpression& tag,
751 : int pos) {
752 : return PreParserStatement::Default();
753 : }
754 :
755 : PreParserStatement NewCaseClause(
756 : const PreParserExpression& label,
757 : const PreParserScopedStatementList& statements) {
758 : return PreParserStatement::Default();
759 : }
760 :
761 : PreParserStatement NewForStatement(
762 : ZonePtrList<const AstRawString>* labels,
763 : ZonePtrList<const AstRawString>* own_labels, int pos) {
764 : return PreParserStatement::Default();
765 : }
766 :
767 : PreParserStatement NewForEachStatement(
768 : ForEachStatement::VisitMode visit_mode,
769 : ZonePtrList<const AstRawString>* labels,
770 : ZonePtrList<const AstRawString>* own_labels, int pos) {
771 : return PreParserStatement::Default();
772 : }
773 :
774 : PreParserStatement NewForOfStatement(
775 : ZonePtrList<const AstRawString>* labels,
776 : ZonePtrList<const AstRawString>* own_labels, int pos, IteratorType type) {
777 : return PreParserStatement::Default();
778 : }
779 :
780 : PreParserExpression NewCallRuntime(
781 : Runtime::FunctionId id, ZoneChunkList<PreParserExpression>* arguments,
782 : int pos) {
783 : return PreParserExpression::Default();
784 : }
785 :
786 : PreParserExpression NewImportCallExpression(const PreParserExpression& args,
787 : int pos) {
788 : return PreParserExpression::Default();
789 : }
790 :
791 : private:
792 : // For creating VariableProxy objects to track unresolved variables.
793 : AstNodeFactory ast_node_factory_;
794 : Zone* zone_;
795 : };
796 :
797 : class PreParser;
798 :
799 : class PreParserFormalParameters : public FormalParametersBase {
800 : public:
801 : explicit PreParserFormalParameters(DeclarationScope* scope)
802 3542347 : : FormalParametersBase(scope) {}
803 :
804 3915 : void set_has_duplicate() { has_duplicate_ = true; }
805 : bool has_duplicate() { return has_duplicate_; }
806 : void ValidateDuplicate(PreParser* preparser) const;
807 :
808 : void set_strict_parameter_error(const Scanner::Location& loc,
809 : MessageTemplate message) {
810 555931 : strict_parameter_error_ = loc.IsValid();
811 : }
812 : void ValidateStrictMode(PreParser* preparser) const;
813 :
814 : private:
815 : bool has_duplicate_ = false;
816 : bool strict_parameter_error_ = false;
817 : };
818 :
819 : class PreParserTarget {
820 : public:
821 : PreParserTarget(ParserBase<PreParser>* preparser,
822 : PreParserStatement statement) {}
823 : };
824 :
825 : class PreParserTargetScope {
826 : public:
827 : explicit PreParserTargetScope(ParserBase<PreParser>* preparser) {}
828 : };
829 :
830 : class PreParserFuncNameInferrer {
831 : public:
832 : explicit PreParserFuncNameInferrer(AstValueFactory* avf) {}
833 : void RemoveAsyncKeywordFromEnd() const {}
834 : void Infer() const {}
835 : void RemoveLastFunction() const {}
836 :
837 : class State {
838 : public:
839 : explicit State(PreParserFuncNameInferrer* fni) {}
840 :
841 : private:
842 : DISALLOW_COPY_AND_ASSIGN(State);
843 : };
844 :
845 : private:
846 : DISALLOW_COPY_AND_ASSIGN(PreParserFuncNameInferrer);
847 : };
848 :
849 : class PreParserSourceRange {
850 : public:
851 : PreParserSourceRange() {}
852 : PreParserSourceRange(int start, int end) {}
853 : static PreParserSourceRange Empty() { return PreParserSourceRange(); }
854 : static PreParserSourceRange OpenEnded(int32_t start) { return Empty(); }
855 : static const PreParserSourceRange& ContinuationOf(
856 : const PreParserSourceRange& that, int end) {
857 : return that;
858 : }
859 : };
860 :
861 : class PreParserSourceRangeScope {
862 : public:
863 : PreParserSourceRangeScope(Scanner* scanner, PreParserSourceRange* range) {}
864 : const PreParserSourceRange& Finalize() const { return range_; }
865 :
866 : private:
867 : PreParserSourceRange range_;
868 :
869 : DISALLOW_IMPLICIT_CONSTRUCTORS(PreParserSourceRangeScope);
870 : };
871 :
872 : class PreParserPropertyList {};
873 :
874 : template <>
875 : struct ParserTypes<PreParser> {
876 : using Base = ParserBase<PreParser>;
877 : using Impl = PreParser;
878 :
879 : // Return types for traversing functions.
880 : using ClassLiteralProperty = PreParserExpression;
881 : using Expression = PreParserExpression;
882 : using FunctionLiteral = PreParserExpression;
883 : using ObjectLiteralProperty = PreParserExpression;
884 : using Suspend = PreParserExpression;
885 : using ExpressionList = PreParserExpressionList;
886 : using ObjectPropertyList = PreParserExpressionList;
887 : using FormalParameters = PreParserFormalParameters;
888 : using Identifier = PreParserIdentifier;
889 : using ClassPropertyList = PreParserPropertyList;
890 : using StatementList = PreParserScopedStatementList;
891 : using Block = PreParserBlock;
892 : using BreakableStatement = PreParserStatement;
893 : using ForStatement = PreParserStatement;
894 : using IterationStatement = PreParserStatement;
895 : using Statement = PreParserStatement;
896 :
897 : // For constructing objects returned by the traversing functions.
898 : using Factory = PreParserFactory;
899 :
900 : // Other implementation-specific tasks.
901 : using FuncNameInferrer = PreParserFuncNameInferrer;
902 : using SourceRange = PreParserSourceRange;
903 : using SourceRangeScope = PreParserSourceRangeScope;
904 : using Target = PreParserTarget;
905 : using TargetScope = PreParserTargetScope;
906 : };
907 :
908 :
909 : // Preparsing checks a JavaScript program and emits preparse-data that helps
910 : // a later parsing to be faster.
911 : // See preparse-data-format.h for the data format.
912 :
913 : // The PreParser checks that the syntax follows the grammar for JavaScript,
914 : // and collects some information about the program along the way.
915 : // The grammar check is only performed in order to understand the program
916 : // sufficiently to deduce some information about it, that can be used
917 : // to speed up later parsing. Finding errors is not the goal of pre-parsing,
918 : // rather it is to speed up properly written and correct programs.
919 : // That means that contextual checks (like a label being declared where
920 : // it is used) are generally omitted.
921 1848742 : class PreParser : public ParserBase<PreParser> {
922 : friend class ParserBase<PreParser>;
923 :
924 : public:
925 : using Identifier = PreParserIdentifier;
926 : using Expression = PreParserExpression;
927 : using Statement = PreParserStatement;
928 :
929 : enum PreParseResult {
930 : kPreParseStackOverflow,
931 : kPreParseNotIdentifiableError,
932 : kPreParseSuccess
933 : };
934 :
935 924358 : PreParser(Zone* zone, Scanner* scanner, uintptr_t stack_limit,
936 : AstValueFactory* ast_value_factory,
937 : PendingCompilationErrorHandler* pending_error_handler,
938 : RuntimeCallStats* runtime_call_stats, Logger* logger,
939 : int script_id = -1, bool parsing_module = false,
940 : bool parsing_on_main_thread = true)
941 : : ParserBase<PreParser>(zone, scanner, stack_limit, nullptr,
942 : ast_value_factory, pending_error_handler,
943 : runtime_call_stats, logger, script_id,
944 : parsing_module, parsing_on_main_thread),
945 : use_counts_(nullptr),
946 : preparse_data_builder_(nullptr),
947 1848723 : preparse_data_builder_buffer_() {
948 924365 : preparse_data_builder_buffer_.reserve(16);
949 924367 : }
950 :
951 : static bool IsPreParser() { return true; }
952 :
953 : PreParserLogger* logger() { return &log_; }
954 :
955 : // Pre-parse the program from the character stream; returns true on
956 : // success (even if parsing failed, the pre-parse data successfully
957 : // captured the syntax error), and false if a stack-overflow happened
958 : // during parsing.
959 : V8_EXPORT_PRIVATE PreParseResult PreParseProgram();
960 :
961 : // Parses a single function literal, from the opening parentheses before
962 : // parameters to the closing brace after the body.
963 : // Returns a FunctionEntry describing the body of the function in enough
964 : // detail that it can be lazily compiled.
965 : // The scanner is expected to have matched the "function" or "function*"
966 : // keyword and parameters, and have consumed the initial '{'.
967 : // At return, unless an error occurred, the scanner is positioned before the
968 : // the final '}'.
969 : PreParseResult PreParseFunction(
970 : const AstRawString* function_name, FunctionKind kind,
971 : FunctionLiteral::FunctionType function_type,
972 : DeclarationScope* function_scope, int* use_counts,
973 : ProducedPreparseData** produced_preparser_scope_data, int script_id);
974 :
975 : PreparseDataBuilder* preparse_data_builder() const {
976 : return preparse_data_builder_;
977 : }
978 :
979 : void set_preparse_data_builder(PreparseDataBuilder* preparse_data_builder) {
980 5364578 : preparse_data_builder_ = preparse_data_builder;
981 : }
982 :
983 : std::vector<void*>* preparse_data_builder_buffer() {
984 2682309 : return &preparse_data_builder_buffer_;
985 : }
986 :
987 : private:
988 : friend class i::ExpressionScope<ParserTypes<PreParser>>;
989 : friend class i::VariableDeclarationParsingScope<ParserTypes<PreParser>>;
990 : friend class i::ParameterDeclarationParsingScope<ParserTypes<PreParser>>;
991 : friend class i::ArrowHeadParsingScope<ParserTypes<PreParser>>;
992 : friend class PreParserFormalParameters;
993 : // These types form an algebra over syntactic categories that is just
994 : // rich enough to let us recognize and propagate the constructs that
995 : // are either being counted in the preparser data, or is important
996 : // to throw the correct syntax error exceptions.
997 :
998 : // All ParseXXX functions take as the last argument an *ok parameter
999 : // which is set to false if parsing failed; it is unchanged otherwise.
1000 : // By making the 'exception handling' explicit, we are forced to check
1001 : // for failure at the call sites.
1002 :
1003 : // Indicates that we won't switch from the preparser to the preparser; we'll
1004 : // just stay where we are.
1005 : bool AllowsLazyParsingWithoutUnresolvedVariables() const { return false; }
1006 : bool parse_lazily() const { return false; }
1007 :
1008 : PendingCompilationErrorHandler* pending_error_handler() {
1009 : return pending_error_handler_;
1010 : }
1011 :
1012 : V8_INLINE bool SkipFunction(const AstRawString* name, FunctionKind kind,
1013 : FunctionLiteral::FunctionType function_type,
1014 : DeclarationScope* function_scope,
1015 : int* num_parameters, int* function_length,
1016 : ProducedPreparseData** produced_preparse_data) {
1017 : UNREACHABLE();
1018 : }
1019 :
1020 : Expression ParseFunctionLiteral(
1021 : Identifier name, Scanner::Location function_name_location,
1022 : FunctionNameValidity function_name_validity, FunctionKind kind,
1023 : int function_token_pos, FunctionLiteral::FunctionType function_type,
1024 : LanguageMode language_mode,
1025 : ZonePtrList<const AstRawString>* arguments_for_wrapped_function);
1026 :
1027 : PreParserExpression InitializeObjectLiteral(PreParserExpression literal) {
1028 : return literal;
1029 : }
1030 :
1031 : bool HasCheckedSyntax() { return false; }
1032 :
1033 : void ParseStatementListAndLogFunction(PreParserFormalParameters* formals);
1034 :
1035 : struct TemplateLiteralState {};
1036 :
1037 : V8_INLINE TemplateLiteralState OpenTemplateLiteral(int pos) {
1038 : return TemplateLiteralState();
1039 : }
1040 : V8_INLINE void AddTemplateExpression(TemplateLiteralState* state,
1041 : const PreParserExpression& expression) {}
1042 : V8_INLINE void AddTemplateSpan(TemplateLiteralState* state, bool should_cook,
1043 : bool tail) {}
1044 : V8_INLINE PreParserExpression CloseTemplateLiteral(
1045 : TemplateLiteralState* state, int start, const PreParserExpression& tag) {
1046 : return PreParserExpression::Default();
1047 : }
1048 : V8_INLINE bool IsPropertyWithPrivateFieldKey(
1049 : const PreParserExpression& expression) {
1050 : return expression.IsPropertyWithPrivateFieldKey();
1051 : }
1052 : V8_INLINE void SetLanguageMode(Scope* scope, LanguageMode mode) {
1053 : scope->SetLanguageMode(mode);
1054 : }
1055 : V8_INLINE void SetAsmModule() {}
1056 :
1057 : V8_INLINE PreParserExpression SpreadCall(const PreParserExpression& function,
1058 : const PreParserExpressionList& args,
1059 : int pos,
1060 : Call::PossiblyEval possibly_eval);
1061 : V8_INLINE PreParserExpression
1062 : SpreadCallNew(const PreParserExpression& function,
1063 : const PreParserExpressionList& args, int pos);
1064 :
1065 : V8_INLINE void PrepareGeneratorVariables() {}
1066 : V8_INLINE void RewriteAsyncFunctionBody(
1067 : const PreParserScopedStatementList* body, PreParserStatement block,
1068 : const PreParserExpression& return_value) {}
1069 :
1070 : V8_INLINE void DeclareLabel(ZonePtrList<const AstRawString>** labels,
1071 : ZonePtrList<const AstRawString>** own_labels,
1072 : const PreParserExpression& expr) {
1073 : DCHECK(!parsing_module_ || !expr.AsIdentifier().IsAwait());
1074 : DCHECK(IsIdentifier(expr));
1075 : }
1076 :
1077 : // TODO(nikolaos): The preparser currently does not keep track of labels.
1078 : V8_INLINE bool ContainsLabel(ZonePtrList<const AstRawString>* labels,
1079 : const PreParserIdentifier& label) {
1080 : return false;
1081 : }
1082 :
1083 : V8_INLINE PreParserExpression
1084 : RewriteReturn(const PreParserExpression& return_value, int pos) {
1085 : return return_value;
1086 : }
1087 : V8_INLINE PreParserStatement
1088 : RewriteSwitchStatement(PreParserStatement switch_statement, Scope* scope) {
1089 : return PreParserStatement::Default();
1090 : }
1091 :
1092 : Variable* DeclareVariable(const AstRawString* name, VariableKind kind,
1093 : VariableMode mode, InitializationFlag init,
1094 : Scope* scope, bool* was_added, int position) {
1095 9652502 : return DeclareVariableName(name, mode, scope, was_added, position, kind);
1096 : }
1097 :
1098 : void DeclareAndBindVariable(const VariableProxy* proxy, VariableKind kind,
1099 : VariableMode mode, InitializationFlag init,
1100 : Scope* scope, bool* was_added, int position) {
1101 : DeclareVariableName(proxy->raw_name(), mode, scope, was_added, position,
1102 433011 : kind);
1103 : // Don't bother actually binding the proxy.
1104 : }
1105 :
1106 : Variable* DeclarePrivateVariableName(const AstRawString* name,
1107 : ClassScope* scope, bool* was_added) {
1108 9545 : return scope->DeclarePrivateName(name, was_added);
1109 : }
1110 :
1111 10564596 : Variable* DeclareVariableName(const AstRawString* name, VariableMode mode,
1112 : Scope* scope, bool* was_added,
1113 : int position = kNoSourcePosition,
1114 : VariableKind kind = NORMAL_VARIABLE) {
1115 10564596 : Variable* var = scope->DeclareVariableName(name, mode, was_added, kind);
1116 10564654 : if (var == nullptr) {
1117 : ReportUnidentifiableError();
1118 215 : if (!IsLexicalVariableMode(mode)) scope = scope->GetDeclarationScope();
1119 : var = scope->LookupLocal(name);
1120 10564439 : } else if (var->scope() != scope) {
1121 : DCHECK_NE(kNoSourcePosition, position);
1122 : DCHECK_EQ(VariableMode::kVar, mode);
1123 : Declaration* nested_declaration =
1124 : factory()->ast_node_factory()->NewNestedVariableDeclaration(scope,
1125 : position);
1126 : nested_declaration->set_var(var);
1127 : var->scope()->declarations()->Add(nested_declaration);
1128 : }
1129 10564654 : return var;
1130 : }
1131 :
1132 : V8_INLINE PreParserBlock RewriteCatchPattern(CatchInfo* catch_info) {
1133 : return PreParserBlock::Default();
1134 : }
1135 :
1136 : V8_INLINE void ReportVarRedeclarationIn(const AstRawString* name,
1137 : Scope* scope) {
1138 : ReportUnidentifiableError();
1139 : }
1140 :
1141 : V8_INLINE PreParserStatement RewriteTryStatement(
1142 : PreParserStatement try_block, PreParserStatement catch_block,
1143 : const SourceRange& catch_range, PreParserStatement finally_block,
1144 : const SourceRange& finally_range, const CatchInfo& catch_info, int pos) {
1145 : return PreParserStatement::Default();
1146 : }
1147 :
1148 : V8_INLINE void ReportUnexpectedTokenAt(
1149 : Scanner::Location location, Token::Value token,
1150 : MessageTemplate message = MessageTemplate::kUnexpectedToken) {
1151 : ReportUnidentifiableError();
1152 : }
1153 : V8_INLINE void ParseAndRewriteGeneratorFunctionBody(
1154 : int pos, FunctionKind kind, PreParserScopedStatementList* body) {
1155 31448 : ParseStatementList(body, Token::RBRACE);
1156 : }
1157 : V8_INLINE void ParseAndRewriteAsyncGeneratorFunctionBody(
1158 : int pos, FunctionKind kind, PreParserScopedStatementList* body) {
1159 37397 : ParseStatementList(body, Token::RBRACE);
1160 : }
1161 : V8_INLINE void DeclareFunctionNameVar(
1162 : const AstRawString* function_name,
1163 : FunctionLiteral::FunctionType function_type,
1164 : DeclarationScope* function_scope) {
1165 4311209 : if (function_type == FunctionLiteral::kNamedExpression &&
1166 : function_scope->LookupLocal(function_name) == nullptr) {
1167 : DCHECK_EQ(function_scope, scope());
1168 889298 : function_scope->DeclareFunctionVar(function_name);
1169 : }
1170 : }
1171 :
1172 : V8_INLINE void DeclareFunctionNameVar(
1173 : const PreParserIdentifier& function_name,
1174 : FunctionLiteral::FunctionType function_type,
1175 : DeclarationScope* function_scope) {
1176 940256 : DeclareFunctionNameVar(function_name.string_, function_type,
1177 : function_scope);
1178 : }
1179 :
1180 : bool IdentifierEquals(const PreParserIdentifier& identifier,
1181 : const AstRawString* other);
1182 :
1183 : // TODO(nikolaos): The preparser currently does not keep track of labels
1184 : // and targets.
1185 : V8_INLINE PreParserStatement
1186 : LookupBreakTarget(const PreParserIdentifier& label) {
1187 : return PreParserStatement::Default();
1188 : }
1189 : V8_INLINE PreParserStatement
1190 : LookupContinueTarget(const PreParserIdentifier& label) {
1191 : return PreParserStatement::Default();
1192 : }
1193 :
1194 : V8_INLINE PreParserStatement DeclareFunction(
1195 : const PreParserIdentifier& variable_name,
1196 : const PreParserExpression& function, VariableMode mode, VariableKind kind,
1197 : int beg_pos, int end_pos, ZonePtrList<const AstRawString>* names) {
1198 : DCHECK_NULL(names);
1199 : bool was_added;
1200 237512 : Variable* var = DeclareVariableName(variable_name.string_, mode, scope(),
1201 237512 : &was_added, beg_pos, kind);
1202 237514 : if (kind == SLOPPY_BLOCK_FUNCTION_VARIABLE) {
1203 : Token::Value init =
1204 2482 : loop_nesting_depth() > 0 ? Token::ASSIGN : Token::INIT;
1205 : SloppyBlockFunctionStatement* statement =
1206 : factory()->ast_node_factory()->NewSloppyBlockFunctionStatement(
1207 : end_pos, var, init);
1208 2482 : GetDeclarationScope()->DeclareSloppyBlockFunction(statement);
1209 : }
1210 : return Statement::Default();
1211 : }
1212 :
1213 : V8_INLINE PreParserStatement DeclareClass(
1214 : const PreParserIdentifier& variable_name,
1215 : const PreParserExpression& value, ZonePtrList<const AstRawString>* names,
1216 : int class_token_pos, int end_pos) {
1217 : // Preparser shouldn't be used in contexts where we need to track the names.
1218 : DCHECK_NULL(names);
1219 : bool was_added;
1220 : DeclareVariableName(variable_name.string_, VariableMode::kLet, scope(),
1221 70962 : &was_added);
1222 : return PreParserStatement::Default();
1223 : }
1224 : V8_INLINE void DeclareClassVariable(const PreParserIdentifier& name,
1225 : ClassInfo* class_info,
1226 : int class_token_pos) {
1227 129871 : if (!IsNull(name)) {
1228 : bool was_added;
1229 : DeclareVariableName(name.string_, VariableMode::kConst, scope(),
1230 71946 : &was_added);
1231 : }
1232 : }
1233 : V8_INLINE void DeclareClassProperty(ClassScope* scope,
1234 : const PreParserIdentifier& class_name,
1235 : const PreParserExpression& property,
1236 : bool is_constructor,
1237 : ClassInfo* class_info) {}
1238 :
1239 : V8_INLINE void DeclareClassField(ClassScope* scope,
1240 : const PreParserExpression& property,
1241 : const PreParserIdentifier& property_name,
1242 : bool is_static, bool is_computed_name,
1243 : bool is_private, ClassInfo* class_info) {
1244 : DCHECK_IMPLIES(is_computed_name, !is_private);
1245 29862 : if (is_computed_name) {
1246 : bool was_added;
1247 4714 : DeclareVariableName(
1248 : ClassFieldVariableName(ast_value_factory(),
1249 : class_info->computed_field_count),
1250 4714 : VariableMode::kConst, scope, &was_added);
1251 25148 : } else if (is_private) {
1252 : bool was_added;
1253 9545 : DeclarePrivateVariableName(property_name.string_, scope, &was_added);
1254 9545 : if (!was_added) {
1255 : Scanner::Location loc(property.position(), property.position() + 1);
1256 : ReportMessageAt(loc, MessageTemplate::kVarRedeclaration,
1257 400 : property_name.string_);
1258 : }
1259 : }
1260 : }
1261 :
1262 : V8_INLINE PreParserExpression
1263 : RewriteClassLiteral(ClassScope* scope, const PreParserIdentifier& name,
1264 : ClassInfo* class_info, int pos, int end_pos) {
1265 54451 : bool has_default_constructor = !class_info->has_seen_constructor;
1266 : // Account for the default constructor.
1267 54451 : if (has_default_constructor) {
1268 : // Creating and disposing of a FunctionState makes tracking of
1269 : // next_function_is_likely_called match what Parser does. TODO(marja):
1270 : // Make the lazy function + next_function_is_likely_called + default ctor
1271 : // logic less surprising. Default ctors shouldn't affect the laziness of
1272 : // functions.
1273 : bool has_extends = class_info->extends.IsNull();
1274 : FunctionKind kind = has_extends ? FunctionKind::kDefaultDerivedConstructor
1275 53460 : : FunctionKind::kDefaultBaseConstructor;
1276 53460 : DeclarationScope* function_scope = NewFunctionScope(kind);
1277 : SetLanguageMode(function_scope, LanguageMode::kStrict);
1278 : function_scope->set_start_position(pos);
1279 : function_scope->set_end_position(pos);
1280 53460 : FunctionState function_state(&function_state_, &scope_, function_scope);
1281 : GetNextFunctionLiteralId();
1282 : }
1283 54451 : if (class_info->has_static_class_fields) {
1284 : GetNextFunctionLiteralId();
1285 : }
1286 54451 : if (class_info->has_instance_members) {
1287 : GetNextFunctionLiteralId();
1288 : }
1289 : return PreParserExpression::Default();
1290 : }
1291 :
1292 : V8_INLINE PreParserStatement DeclareNative(const PreParserIdentifier& name,
1293 : int pos) {
1294 : return PreParserStatement::Default();
1295 : }
1296 :
1297 : V8_INLINE void QueueDestructuringAssignmentForRewriting(
1298 : PreParserExpression assignment) {}
1299 :
1300 : // Helper functions for recursive descent.
1301 : V8_INLINE bool IsEval(const PreParserIdentifier& identifier) const {
1302 : return identifier.IsEval();
1303 : }
1304 :
1305 : V8_INLINE bool IsAsync(const PreParserIdentifier& identifier) const {
1306 : return identifier.IsAsync();
1307 : }
1308 :
1309 : V8_INLINE bool IsArguments(const PreParserIdentifier& identifier) const {
1310 : return identifier.IsArguments();
1311 : }
1312 :
1313 : V8_INLINE bool IsEvalOrArguments(
1314 : const PreParserIdentifier& identifier) const {
1315 : return identifier.IsEvalOrArguments();
1316 : }
1317 :
1318 : V8_INLINE bool IsAwait(const PreParserIdentifier& identifier) const {
1319 : return identifier.IsAwait();
1320 : }
1321 :
1322 : // Returns true if the expression is of type "this.foo".
1323 : V8_INLINE static bool IsThisProperty(const PreParserExpression& expression) {
1324 : return expression.IsThisProperty();
1325 : }
1326 :
1327 : V8_INLINE static bool IsIdentifier(const PreParserExpression& expression) {
1328 : return expression.IsIdentifier();
1329 : }
1330 :
1331 : V8_INLINE static PreParserIdentifier AsIdentifier(
1332 : const PreParserExpression& expression) {
1333 : return expression.AsIdentifier();
1334 : }
1335 :
1336 : V8_INLINE static PreParserExpression AsIdentifierExpression(
1337 : const PreParserExpression& expression) {
1338 : return expression;
1339 : }
1340 :
1341 : V8_INLINE bool IsConstructor(const PreParserIdentifier& identifier) const {
1342 : return identifier.IsConstructor();
1343 : }
1344 :
1345 : V8_INLINE bool IsName(const PreParserIdentifier& identifier) const {
1346 : return identifier.IsName();
1347 : }
1348 :
1349 : V8_INLINE static bool IsBoilerplateProperty(
1350 : const PreParserExpression& property) {
1351 : // PreParser doesn't count boilerplate properties.
1352 : return false;
1353 : }
1354 :
1355 : V8_INLINE bool IsNative(const PreParserExpression& expr) const {
1356 : // Preparsing is disabled for extensions (because the extension
1357 : // details aren't passed to lazily compiled functions), so we
1358 : // don't accept "native function" in the preparser and there is
1359 : // no need to keep track of "native".
1360 : return false;
1361 : }
1362 :
1363 : V8_INLINE static bool IsArrayIndex(const PreParserIdentifier& string,
1364 : uint32_t* index) {
1365 : return false;
1366 : }
1367 :
1368 : V8_INLINE bool IsStringLiteral(PreParserStatement statement) const {
1369 : return statement.IsStringLiteral();
1370 : }
1371 :
1372 : V8_INLINE static void GetDefaultStrings(
1373 : PreParserIdentifier* default_string,
1374 : PreParserIdentifier* dot_default_string) {}
1375 :
1376 : // Functions for encapsulating the differences between parsing and preparsing;
1377 : // operations interleaved with the recursive descent.
1378 : V8_INLINE static void PushLiteralName(const PreParserIdentifier& id) {}
1379 : V8_INLINE static void PushVariableName(const PreParserIdentifier& id) {}
1380 : V8_INLINE void PushPropertyName(const PreParserExpression& expression) {}
1381 : V8_INLINE void PushEnclosingName(const PreParserIdentifier& name) {}
1382 : V8_INLINE static void AddFunctionForNameInference(
1383 : const PreParserExpression& expression) {}
1384 : V8_INLINE static void InferFunctionName() {}
1385 :
1386 : V8_INLINE static void CheckAssigningFunctionLiteralToProperty(
1387 : const PreParserExpression& left, const PreParserExpression& right) {}
1388 :
1389 : V8_INLINE bool ShortcutNumericLiteralBinaryExpression(
1390 : PreParserExpression* x, const PreParserExpression& y, Token::Value op,
1391 : int pos) {
1392 : return false;
1393 : }
1394 :
1395 : V8_INLINE NaryOperation* CollapseNaryExpression(PreParserExpression* x,
1396 : PreParserExpression y,
1397 : Token::Value op, int pos,
1398 : const SourceRange& range) {
1399 : x->clear_parenthesized();
1400 : return nullptr;
1401 : }
1402 :
1403 : V8_INLINE PreParserExpression BuildUnaryExpression(
1404 : const PreParserExpression& expression, Token::Value op, int pos) {
1405 : return PreParserExpression::Default();
1406 : }
1407 :
1408 : V8_INLINE PreParserStatement
1409 : BuildInitializationBlock(DeclarationParsingResult* parsing_result) {
1410 : return PreParserStatement::Default();
1411 : }
1412 :
1413 : V8_INLINE PreParserBlock RewriteForVarInLegacy(const ForInfo& for_info) {
1414 : return PreParserBlock::Null();
1415 : }
1416 :
1417 : V8_INLINE void DesugarBindingInForEachStatement(
1418 : ForInfo* for_info, PreParserStatement* body_block,
1419 : PreParserExpression* each_variable) {
1420 : }
1421 :
1422 : V8_INLINE PreParserBlock CreateForEachStatementTDZ(PreParserBlock init_block,
1423 : const ForInfo& for_info) {
1424 112648 : if (IsLexicalVariableMode(for_info.parsing_result.descriptor.mode)) {
1425 277561 : for (auto name : for_info.bound_names) {
1426 : bool was_added;
1427 93846 : DeclareVariableName(name, VariableMode::kLet, scope(), &was_added);
1428 : }
1429 : return PreParserBlock::Default();
1430 : }
1431 : return init_block;
1432 : }
1433 :
1434 : V8_INLINE StatementT DesugarLexicalBindingsInForStatement(
1435 : PreParserStatement loop, PreParserStatement init,
1436 : const PreParserExpression& cond, PreParserStatement next,
1437 : PreParserStatement body, Scope* inner_scope, const ForInfo& for_info) {
1438 : // See Parser::DesugarLexicalBindingsInForStatement.
1439 1014 : for (auto name : for_info.bound_names) {
1440 : bool was_added;
1441 348 : DeclareVariableName(name, for_info.parsing_result.descriptor.mode,
1442 348 : inner_scope, &was_added);
1443 : }
1444 : return loop;
1445 : }
1446 :
1447 : PreParserBlock BuildParameterInitializationBlock(
1448 : const PreParserFormalParameters& parameters);
1449 :
1450 : V8_INLINE PreParserBlock
1451 : BuildRejectPromiseOnException(PreParserStatement init_block) {
1452 : return PreParserBlock::Default();
1453 : }
1454 :
1455 : V8_INLINE void InsertSloppyBlockFunctionVarBindings(DeclarationScope* scope) {
1456 518220 : scope->HoistSloppyBlockFunctions(nullptr);
1457 : }
1458 :
1459 : V8_INLINE void InsertShadowingVarBindingInitializers(
1460 : PreParserStatement block) {}
1461 :
1462 : V8_INLINE PreParserExpression NewThrowReferenceError(MessageTemplate message,
1463 : int pos) {
1464 : return PreParserExpression::Default();
1465 : }
1466 :
1467 : V8_INLINE PreParserExpression NewThrowSyntaxError(
1468 : MessageTemplate message, const PreParserIdentifier& arg, int pos) {
1469 : return PreParserExpression::Default();
1470 : }
1471 :
1472 : V8_INLINE PreParserExpression NewThrowTypeError(
1473 : MessageTemplate message, const PreParserIdentifier& arg, int pos) {
1474 : return PreParserExpression::Default();
1475 : }
1476 :
1477 : // Reporting errors.
1478 202555 : void ReportMessageAt(Scanner::Location source_location,
1479 : MessageTemplate message, const char* arg = nullptr,
1480 : ParseErrorType error_type = kSyntaxError) {
1481 202555 : pending_error_handler()->ReportMessageAt(source_location.beg_pos,
1482 : source_location.end_pos, message,
1483 202555 : arg, error_type);
1484 : scanner()->set_parser_error();
1485 202555 : }
1486 :
1487 : V8_INLINE void ReportUnidentifiableError() {
1488 : pending_error_handler()->set_unidentifiable_error();
1489 5621 : scanner()->set_parser_error();
1490 : }
1491 :
1492 : V8_INLINE void ReportMessageAt(Scanner::Location source_location,
1493 : MessageTemplate message,
1494 : const PreParserIdentifier& arg,
1495 : ParseErrorType error_type = kSyntaxError) {
1496 : UNREACHABLE();
1497 : }
1498 :
1499 1260 : void ReportMessageAt(Scanner::Location source_location,
1500 : MessageTemplate message, const AstRawString* arg,
1501 : ParseErrorType error_type = kSyntaxError) {
1502 1260 : pending_error_handler()->ReportMessageAt(source_location.beg_pos,
1503 : source_location.end_pos, message,
1504 1260 : arg, error_type);
1505 : scanner()->set_parser_error();
1506 1260 : }
1507 :
1508 : // "null" return type creators.
1509 : V8_INLINE static PreParserIdentifier NullIdentifier() {
1510 : return PreParserIdentifier::Null();
1511 : }
1512 : V8_INLINE static PreParserExpression NullExpression() {
1513 : return PreParserExpression::Null();
1514 : }
1515 : V8_INLINE static PreParserExpression FailureExpression() {
1516 : return PreParserExpression::Failure();
1517 : }
1518 : V8_INLINE static PreParserExpression NullLiteralProperty() {
1519 : return PreParserExpression::Null();
1520 : }
1521 : V8_INLINE static PreParserStatementList NullStatementList() {
1522 : return PreParserStatementList::Null();
1523 : }
1524 : V8_INLINE static PreParserStatement NullStatement() {
1525 : return PreParserStatement::Null();
1526 : }
1527 : V8_INLINE static PreParserBlock NullBlock() { return PreParserBlock::Null(); }
1528 :
1529 : template <typename T>
1530 : V8_INLINE static bool IsNull(T subject) {
1531 : return subject.IsNull();
1532 : }
1533 :
1534 : V8_INLINE PreParserIdentifier EmptyIdentifierString() const {
1535 : PreParserIdentifier result = PreParserIdentifier::Default();
1536 : result.string_ = ast_value_factory()->empty_string();
1537 : return result;
1538 : }
1539 :
1540 : // Producing data during the recursive descent.
1541 : PreParserIdentifier GetSymbol() const {
1542 : return PreParserIdentifier::Default();
1543 : }
1544 :
1545 : PreParserIdentifier GetIdentifier() const;
1546 :
1547 : V8_INLINE PreParserIdentifier GetNextSymbol() const {
1548 : return PreParserIdentifier::Default();
1549 : }
1550 :
1551 : V8_INLINE PreParserIdentifier GetNumberAsSymbol() const {
1552 : return PreParserIdentifier::Default();
1553 : }
1554 :
1555 : V8_INLINE PreParserExpression ThisExpression() {
1556 : UseThis();
1557 : return PreParserExpression::This();
1558 : }
1559 :
1560 : V8_INLINE PreParserExpression NewSuperPropertyReference(int pos) {
1561 : scope()->NewUnresolved(factory()->ast_node_factory(),
1562 : ast_value_factory()->this_function_string(), pos,
1563 4248 : NORMAL_VARIABLE);
1564 : return PreParserExpression::Default();
1565 : }
1566 :
1567 : V8_INLINE PreParserExpression NewSuperCallReference(int pos) {
1568 : scope()->NewUnresolved(factory()->ast_node_factory(),
1569 : ast_value_factory()->this_function_string(), pos,
1570 3114 : NORMAL_VARIABLE);
1571 : scope()->NewUnresolved(factory()->ast_node_factory(),
1572 : ast_value_factory()->new_target_string(), pos,
1573 3114 : NORMAL_VARIABLE);
1574 : return PreParserExpression::SuperCallReference();
1575 : }
1576 :
1577 : V8_INLINE PreParserExpression NewTargetExpression(int pos) {
1578 : return PreParserExpression::NewTargetExpression();
1579 : }
1580 :
1581 : V8_INLINE PreParserExpression ImportMetaExpression(int pos) {
1582 : return PreParserExpression::Default();
1583 : }
1584 :
1585 : V8_INLINE PreParserExpression ExpressionFromLiteral(Token::Value token,
1586 : int pos) {
1587 23028518 : if (token != Token::STRING) return PreParserExpression::Default();
1588 : return PreParserExpression::StringLiteral();
1589 : }
1590 :
1591 3450 : PreParserExpression ExpressionFromPrivateName(ClassScope* class_scope,
1592 : const PreParserIdentifier& name,
1593 : int start_position) {
1594 : VariableProxy* proxy = factory()->ast_node_factory()->NewVariableProxy(
1595 3450 : name.string_, NORMAL_VARIABLE, start_position);
1596 3450 : class_scope->AddUnresolvedPrivateName(proxy);
1597 3450 : return PreParserExpression::FromIdentifier(name);
1598 : }
1599 :
1600 : PreParserExpression ExpressionFromIdentifier(
1601 : const PreParserIdentifier& name, int start_position,
1602 : InferName infer = InferName::kYes) {
1603 40723435 : expression_scope()->NewVariable(name.string_, start_position);
1604 : return PreParserExpression::FromIdentifier(name);
1605 : }
1606 :
1607 : V8_INLINE void DeclareIdentifier(const PreParserIdentifier& name,
1608 : int start_position) {
1609 241166 : expression_scope()->Declare(name.string_, start_position);
1610 : }
1611 :
1612 : V8_INLINE Variable* DeclareCatchVariableName(
1613 : Scope* scope, const PreParserIdentifier& identifier) {
1614 280445 : return scope->DeclareCatchVariableName(identifier.string_);
1615 : }
1616 :
1617 : V8_INLINE PreParserPropertyList NewClassPropertyList(int size) const {
1618 : return PreParserPropertyList();
1619 : }
1620 :
1621 : V8_INLINE PreParserStatementList NewStatementList(int size) const {
1622 : return PreParserStatementList();
1623 : }
1624 :
1625 : V8_INLINE PreParserExpression
1626 : NewV8Intrinsic(const PreParserIdentifier& name,
1627 : const PreParserExpressionList& arguments, int pos) {
1628 : return PreParserExpression::Default();
1629 : }
1630 :
1631 : V8_INLINE PreParserStatement
1632 : NewThrowStatement(const PreParserExpression& exception, int pos) {
1633 : return PreParserStatement::Jump();
1634 : }
1635 :
1636 : V8_INLINE void AddFormalParameter(PreParserFormalParameters* parameters,
1637 : PreParserExpression& pattern,
1638 : const PreParserExpression& initializer,
1639 : int initializer_end_position,
1640 : bool is_rest) {
1641 4014489 : DeclarationScope* scope = parameters->scope;
1642 4014489 : scope->RecordParameter(is_rest);
1643 4014474 : parameters->UpdateArityAndFunctionLength(!initializer.IsNull(), is_rest);
1644 : }
1645 :
1646 : V8_INLINE void DeclareFormalParameters(
1647 : const PreParserFormalParameters* parameters) {
1648 2959113 : if (!parameters->is_simple) parameters->scope->SetHasNonSimpleParameters();
1649 : }
1650 :
1651 : V8_INLINE void DeclareArrowFunctionFormalParameters(
1652 : PreParserFormalParameters* parameters, const PreParserExpression& params,
1653 : const Scanner::Location& params_loc) {
1654 : }
1655 :
1656 : V8_INLINE PreParserExpression
1657 : ExpressionListToExpression(const PreParserExpressionList& args) {
1658 : return PreParserExpression::Default();
1659 : }
1660 :
1661 : V8_INLINE void SetFunctionNameFromPropertyName(
1662 : const PreParserExpression& property, const PreParserIdentifier& name,
1663 : const AstRawString* prefix = nullptr) {}
1664 : V8_INLINE void SetFunctionNameFromIdentifierRef(
1665 : const PreParserExpression& value, const PreParserExpression& identifier) {
1666 : }
1667 :
1668 : V8_INLINE void CountUsage(v8::Isolate::UseCounterFeature feature) {
1669 1058 : if (use_counts_ != nullptr) ++use_counts_[feature];
1670 : }
1671 :
1672 : V8_INLINE bool ParsingDynamicFunctionDeclaration() const { return false; }
1673 :
1674 : // Generate empty functions here as the preparser does not collect source
1675 : // ranges for block coverage.
1676 : #define DEFINE_RECORD_SOURCE_RANGE(Name) \
1677 : template <typename... Ts> \
1678 : V8_INLINE void Record##Name##SourceRange(Ts... args) {}
1679 : AST_SOURCE_RANGE_LIST(DEFINE_RECORD_SOURCE_RANGE)
1680 : #undef DEFINE_RECORD_SOURCE_RANGE
1681 :
1682 : // Preparser's private field members.
1683 :
1684 : int* use_counts_;
1685 : PreParserLogger log_;
1686 :
1687 : PreparseDataBuilder* preparse_data_builder_;
1688 : std::vector<void*> preparse_data_builder_buffer_;
1689 : };
1690 :
1691 : PreParserExpression PreParser::SpreadCall(const PreParserExpression& function,
1692 : const PreParserExpressionList& args,
1693 : int pos,
1694 : Call::PossiblyEval possibly_eval) {
1695 : return factory()->NewCall(function, args, pos, possibly_eval);
1696 : }
1697 :
1698 : PreParserExpression PreParser::SpreadCallNew(
1699 : const PreParserExpression& function, const PreParserExpressionList& args,
1700 : int pos) {
1701 : return factory()->NewCallNew(function, args, pos);
1702 : }
1703 :
1704 : } // namespace internal
1705 : } // namespace v8
1706 :
1707 : #endif // V8_PARSING_PREPARSER_H_
|