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 13821884 : PreParserIdentifier() : type_(kUnknownIdentifier) {}
30 680460 : static PreParserIdentifier Default() {
31 680460 : return PreParserIdentifier(kUnknownIdentifier);
32 : }
33 3142757 : static PreParserIdentifier Null() {
34 3142757 : 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 974570 : bool IsNull() const { return type_ == kNullIdentifier; }
58 3861135 : bool IsEval() const { return type_ == kEvalIdentifier; }
59 : bool IsAsync() const { return type_ == kAsyncIdentifier; }
60 40041729 : bool IsArguments() const { return type_ == kArgumentsIdentifier; }
61 9885299 : bool IsEvalOrArguments() const {
62 : STATIC_ASSERT(kEvalIdentifier + 1 == kArgumentsIdentifier);
63 19770598 : return IsInRange(type_, kEvalIdentifier, kArgumentsIdentifier);
64 : }
65 34835 : bool IsConstructor() const { return type_ == kConstructorIdentifier; }
66 : bool IsAwait() const { return type_ == kAwaitIdentifier; }
67 43917 : 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 59975748 : PreParserExpression() : code_(TypeField::encode(kNull)) {}
95 :
96 14050244 : static PreParserExpression Null() { return PreParserExpression(); }
97 634403 : static PreParserExpression Failure() {
98 634403 : return PreParserExpression(TypeField::encode(kFailure));
99 : }
100 :
101 12501774 : static PreParserExpression Default() {
102 12501774 : 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 39982141 : 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 4100 : static PreParserExpression NewTargetExpression() {
127 4100 : 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 5850534 : static PreParserExpression StringLiteral() {
139 5850534 : return PreParserExpression(TypeField::encode(kStringLiteralExpression));
140 : }
141 :
142 3041557 : static PreParserExpression This() {
143 : return PreParserExpression(TypeField::encode(kExpression) |
144 3041557 : 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 1052 : return ExpressionTypeField::decode(code_) == kCallTaggedTemplateExpression;
191 : }
192 :
193 3074 : static PreParserExpression SuperCallReference() {
194 : return PreParserExpression(
195 : TypeField::encode(kExpression) |
196 3074 : ExpressionTypeField::encode(kSuperCallReference));
197 : }
198 :
199 17741768 : bool IsNull() const { return TypeField::decode(code_) == kNull; }
200 : bool IsFailureExpression() const {
201 : return TypeField::decode(code_) == kFailure;
202 : }
203 :
204 64075086 : bool IsIdentifier() const {
205 139400589 : return TypeField::decode(code_) == kIdentifierExpression;
206 : }
207 :
208 12133994 : PreParserIdentifier AsIdentifier() const {
209 : DCHECK(IsIdentifier());
210 24267988 : return PreParserIdentifier(IdentifierTypeField::decode(code_));
211 : }
212 :
213 : bool IsAssignment() const {
214 38400983 : 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 26553741 : return TypeField::decode(code_) == kExpression &&
238 : ExpressionTypeField::decode(code_) == kThisExpression;
239 : }
240 :
241 5512819 : bool IsThisProperty() const {
242 15171782 : return TypeField::decode(code_) == kExpression &&
243 1803409 : (ExpressionTypeField::decode(code_) == kThisPropertyExpression ||
244 : ExpressionTypeField::decode(code_) ==
245 5512819 : kThisPropertyExpressionWithPrivateFieldKey);
246 : }
247 :
248 : bool IsProperty() const {
249 14808895 : return TypeField::decode(code_) == kExpression &&
250 3995199 : (ExpressionTypeField::decode(code_) == kPropertyExpression ||
251 1634569 : ExpressionTypeField::decode(code_) == kThisPropertyExpression ||
252 : ExpressionTypeField::decode(code_) ==
253 1634515 : kPropertyExpressionWithPrivateFieldKey ||
254 : ExpressionTypeField::decode(code_) ==
255 : kThisPropertyExpressionWithPrivateFieldKey);
256 : }
257 :
258 10024 : bool IsPropertyWithPrivateFieldKey() const {
259 28006 : return TypeField::decode(code_) == kExpression &&
260 : (ExpressionTypeField::decode(code_) ==
261 6759 : kPropertyExpressionWithPrivateFieldKey ||
262 : ExpressionTypeField::decode(code_) ==
263 10024 : kThisPropertyExpressionWithPrivateFieldKey);
264 : }
265 :
266 : bool IsCall() const {
267 8318083 : return TypeField::decode(code_) == kExpression &&
268 3116033 : (ExpressionTypeField::decode(code_) == kCallExpression ||
269 3111768 : ExpressionTypeField::decode(code_) == kCallEvalExpression ||
270 : ExpressionTypeField::decode(code_) ==
271 : kCallTaggedTemplateExpression);
272 : }
273 : PreParserExpression* AsCall() {
274 1052 : 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 24579743 : bool is_parenthesized() const { return IsParenthesizedField::decode(code_); }
296 :
297 : void mark_parenthesized() {
298 : code_ = IsParenthesizedField::update(code_, true);
299 : }
300 :
301 5221453 : void clear_parenthesized() {
302 10442906 : code_ = IsParenthesizedField::update(code_, false);
303 5221453 : }
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 : typedef BitField<Type, 0, 3> TypeField;
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 : typedef BitField<bool, TypeField::kNext, 1> IsParenthesizedField;
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 : typedef BitField<ExpressionType, IsParenthesizedField::kNext, 4>
361 : ExpressionTypeField;
362 : typedef BitField<PreParserIdentifier::Type, IsParenthesizedField::kNext, 8>
363 : IdentifierTypeField;
364 : typedef BitField<bool, IsParenthesizedField::kNext, 1>
365 : HasCoverInitializedNameField;
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 28441045 : explicit PreParserExpressionList(std::vector<void*>* buffer) : length_(0) {}
402 :
403 28439500 : int length() const { return length_; }
404 :
405 : void Add(const PreParserExpression& expression) {
406 33488862 : ++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 5357164 : static PreParserStatement Default() {
419 5357164 : return PreParserStatement(kUnknownStatement);
420 : }
421 :
422 7208409 : static PreParserStatement Null() {
423 7208409 : return PreParserStatement(kNullStatement);
424 : }
425 :
426 : static PreParserStatement Empty() {
427 : return PreParserStatement(kEmptyStatement);
428 : }
429 :
430 263573 : static PreParserStatement Jump() {
431 263573 : 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 9702166 : if (expression.IsStringLiteral()) {
443 : return PreParserStatement(kStringLiteralExpressionStatement);
444 : }
445 : return Default();
446 : }
447 :
448 206663 : bool IsStringLiteral() { return code_ == kStringLiteralExpressionStatement; }
449 :
450 : bool IsJumpStatement() {
451 : return code_ == kJumpStatement;
452 : }
453 :
454 20401849 : bool IsNull() { return code_ == kNullStatement; }
455 :
456 14002895 : bool IsEmptyStatement() {
457 : DCHECK(!IsNull());
458 19467308 : return code_ == kEmptyStatement;
459 : }
460 :
461 : // Dummy implementation for making statement->somefunc() work in both Parser
462 : // and PreParser.
463 14002850 : 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 273624 : void set_scope(Scope* scope) { scope_ = scope; }
498 273632 : Scope* scope() const { return scope_; }
499 88598 : static PreParserBlock Default() {
500 88598 : return PreParserBlock(PreParserStatement::kUnknownStatement);
501 : }
502 889753 : static PreParserBlock Null() {
503 889753 : return PreParserBlock(PreParserStatement::kNullStatement);
504 : }
505 : // Dummy implementation for making block->somefunc() work in both Parser and
506 : // PreParser.
507 547238 : 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 915874 : : ast_node_factory_(ast_value_factory, zone), zone_(zone) {}
519 :
520 12847 : 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 11250417 : PreParserExpression NewProperty(const PreParserExpression& obj,
573 : const PreParserExpression& key, int pos) {
574 13157229 : if (key.IsIdentifier() && key.AsIdentifier().IsPrivateName()) {
575 3291 : if (obj.IsThis()) {
576 : return PreParserExpression::ThisPropertyWithPrivateFieldKey();
577 : }
578 : return PreParserExpression::PropertyWithPrivateFieldKey();
579 : }
580 :
581 11247126 : 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 569919 : PreParserExpression NewCountOperation(Token::Value op, bool is_prefix,
628 : const PreParserExpression& expression,
629 : int pos) {
630 569919 : return PreParserExpression::Default();
631 : }
632 4965 : PreParserExpression NewCall(
633 : PreParserExpression expression, const PreParserExpressionList& arguments,
634 : int pos, Call::PossiblyEval possibly_eval = Call::NOT_EVAL) {
635 6714052 : 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 136 : PreParserExpression NewCallNew(const PreParserExpression& expression,
647 : const PreParserExpressionList& arguments,
648 : int pos) {
649 136 : 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 273625 : PreParserBlock NewBlock(bool ignore_completion_value,
697 : const PreParserScopedStatementList& list) {
698 273625 : 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 2775783 : 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 3471101 : : FormalParametersBase(scope) {}
803 :
804 3914 : 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 545771 : 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 : typedef ParserBase<PreParser> Base;
877 : typedef PreParser Impl;
878 :
879 : // Return types for traversing functions.
880 : typedef PreParserExpression ClassLiteralProperty;
881 : typedef PreParserExpression Expression;
882 : typedef PreParserExpression FunctionLiteral;
883 : typedef PreParserExpression ObjectLiteralProperty;
884 : typedef PreParserExpression Suspend;
885 : typedef PreParserExpressionList ExpressionList;
886 : typedef PreParserExpressionList ObjectPropertyList;
887 : typedef PreParserFormalParameters FormalParameters;
888 : typedef PreParserIdentifier Identifier;
889 : typedef PreParserPropertyList ClassPropertyList;
890 : typedef PreParserScopedStatementList StatementList;
891 : typedef PreParserBlock Block;
892 : typedef PreParserStatement BreakableStatement;
893 : typedef PreParserStatement ForStatement;
894 : typedef PreParserStatement IterationStatement;
895 : typedef PreParserStatement Statement;
896 :
897 : // For constructing objects returned by the traversing functions.
898 : typedef PreParserFactory Factory;
899 :
900 : // Other implementation-specific tasks.
901 : typedef PreParserFuncNameInferrer FuncNameInferrer;
902 : typedef PreParserSourceRange SourceRange;
903 : typedef PreParserSourceRangeScope SourceRangeScope;
904 : typedef PreParserTarget Target;
905 : typedef PreParserTargetScope TargetScope;
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 1831816 : class PreParser : public ParserBase<PreParser> {
922 : friend class ParserBase<PreParser>;
923 :
924 : public:
925 : typedef PreParserIdentifier Identifier;
926 : typedef PreParserExpression Expression;
927 : typedef PreParserStatement Statement;
928 :
929 : enum PreParseResult {
930 : kPreParseStackOverflow,
931 : kPreParseNotIdentifiableError,
932 : kPreParseSuccess
933 : };
934 :
935 915732 : 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 1831586 : preparse_data_builder_buffer_() {
948 915854 : preparse_data_builder_buffer_.reserve(16);
949 915882 : }
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 : 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 5253354 : preparse_data_builder_ = preparse_data_builder;
981 : }
982 :
983 : std::vector<void*>* preparse_data_builder_buffer() {
984 : 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 1637780 : PendingCompilationErrorHandler* pending_error_handler() {
1009 1637780 : 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,
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 273629 : 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 30781 : return PreParserExpression::Default();
1047 : }
1048 : V8_INLINE bool IsPropertyWithPrivateFieldKey(
1049 : const PreParserExpression& expression) {
1050 10025 : return expression.IsPropertyWithPrivateFieldKey();
1051 : }
1052 : V8_INLINE void SetLanguageMode(Scope* scope, LanguageMode mode) {
1053 504927 : 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 1363 : 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 9496222 : return DeclareVariableName(name, mode, scope, was_added, position, kind);
1096 : }
1097 :
1098 422575 : 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 845150 : kind);
1103 : // Don't bother actually binding the proxy.
1104 422589 : }
1105 :
1106 10393017 : Variable* DeclareVariableName(const AstRawString* name, VariableMode mode,
1107 : Scope* scope, bool* was_added,
1108 : int position = kNoSourcePosition,
1109 : VariableKind kind = NORMAL_VARIABLE) {
1110 21346274 : Variable* var = scope->DeclareVariableName(name, mode, was_added, kind);
1111 10393601 : if (var == nullptr) {
1112 : ReportUnidentifiableError();
1113 10392995 : } else if (var->scope() != scope) {
1114 : DCHECK_NE(kNoSourcePosition, position);
1115 : DCHECK_EQ(VariableMode::kVar, mode);
1116 : Declaration* nested_declaration =
1117 : factory()->ast_node_factory()->NewNestedVariableDeclaration(scope,
1118 560258 : position);
1119 : nested_declaration->set_var(var);
1120 : var->scope()->declarations()->Add(nested_declaration);
1121 : }
1122 10393605 : return var;
1123 : }
1124 :
1125 : V8_INLINE PreParserBlock RewriteCatchPattern(CatchInfo* catch_info) {
1126 2209 : return PreParserBlock::Default();
1127 : }
1128 :
1129 : V8_INLINE void ReportVarRedeclarationIn(const AstRawString* name,
1130 : Scope* scope) {
1131 : ReportUnidentifiableError();
1132 : }
1133 :
1134 : V8_INLINE PreParserStatement RewriteTryStatement(
1135 : PreParserStatement try_block, PreParserStatement catch_block,
1136 : const SourceRange& catch_range, PreParserStatement finally_block,
1137 : const SourceRange& finally_range, const CatchInfo& catch_info, int pos) {
1138 307340 : return PreParserStatement::Default();
1139 : }
1140 :
1141 : V8_INLINE void ReportUnexpectedTokenAt(
1142 : Scanner::Location location, Token::Value token,
1143 : MessageTemplate message = MessageTemplate::kUnexpectedToken) {
1144 : ReportUnidentifiableError();
1145 : }
1146 : V8_INLINE void ParseAndRewriteGeneratorFunctionBody(
1147 : int pos, FunctionKind kind, PreParserScopedStatementList* body) {
1148 31439 : ParseStatementList(body, Token::RBRACE);
1149 : }
1150 : V8_INLINE void ParseAndRewriteAsyncGeneratorFunctionBody(
1151 : int pos, FunctionKind kind, PreParserScopedStatementList* body) {
1152 37397 : ParseStatementList(body, Token::RBRACE);
1153 : }
1154 : V8_INLINE void DeclareFunctionNameVar(
1155 : const AstRawString* function_name,
1156 : FunctionLiteral::FunctionType function_type,
1157 : DeclarationScope* function_scope) {
1158 4224479 : if (function_type == FunctionLiteral::kNamedExpression &&
1159 873488 : function_scope->LookupLocal(function_name) == nullptr) {
1160 : DCHECK_EQ(function_scope, scope());
1161 871654 : function_scope->DeclareFunctionVar(function_name);
1162 : }
1163 : }
1164 :
1165 : V8_INLINE void DeclareFunctionNameVar(
1166 : const PreParserIdentifier& function_name,
1167 : FunctionLiteral::FunctionType function_type,
1168 : DeclarationScope* function_scope) {
1169 : DeclareFunctionNameVar(function_name.string_, function_type,
1170 : function_scope);
1171 : }
1172 :
1173 : bool IdentifierEquals(const PreParserIdentifier& identifier,
1174 : const AstRawString* other);
1175 :
1176 : // TODO(nikolaos): The preparser currently does not keep track of labels
1177 : // and targets.
1178 : V8_INLINE PreParserStatement
1179 : LookupBreakTarget(const PreParserIdentifier& label) {
1180 151402 : return PreParserStatement::Default();
1181 : }
1182 : V8_INLINE PreParserStatement
1183 : LookupContinueTarget(const PreParserIdentifier& label) {
1184 40101 : return PreParserStatement::Default();
1185 : }
1186 :
1187 : V8_INLINE PreParserStatement DeclareFunction(
1188 : const PreParserIdentifier& variable_name,
1189 : const PreParserExpression& function, VariableMode mode, VariableKind kind,
1190 : int beg_pos, int end_pos, ZonePtrList<const AstRawString>* names) {
1191 : DCHECK_NULL(names);
1192 234752 : if (variable_name.string_ != nullptr) {
1193 : bool was_added;
1194 : Variable* var = DeclareVariableName(variable_name.string_, mode, scope(),
1195 232654 : &was_added, beg_pos, kind);
1196 232676 : if (kind == SLOPPY_BLOCK_FUNCTION_VARIABLE) {
1197 : Token::Value init =
1198 2467 : loop_nesting_depth() > 0 ? Token::ASSIGN : Token::INIT;
1199 : SloppyBlockFunctionStatement* statement =
1200 : factory()->ast_node_factory()->NewSloppyBlockFunctionStatement(
1201 2467 : end_pos, var, init);
1202 2467 : GetDeclarationScope()->DeclareSloppyBlockFunction(statement);
1203 : }
1204 : }
1205 234774 : return Statement::Default();
1206 : }
1207 :
1208 : V8_INLINE PreParserStatement DeclareClass(
1209 : const PreParserIdentifier& variable_name,
1210 : const PreParserExpression& value, ZonePtrList<const AstRawString>* names,
1211 : int class_token_pos, int end_pos) {
1212 : // Preparser shouldn't be used in contexts where we need to track the names.
1213 : DCHECK_NULL(names);
1214 69131 : if (variable_name.string_ != nullptr) {
1215 : bool was_added;
1216 : DeclareVariableName(variable_name.string_, VariableMode::kLet, scope(),
1217 68625 : &was_added);
1218 : }
1219 69131 : return PreParserStatement::Default();
1220 : }
1221 : V8_INLINE void DeclareClassVariable(const PreParserIdentifier& name,
1222 : ClassInfo* class_info,
1223 : int class_token_pos) {
1224 127438 : if (name.string_ != nullptr) {
1225 : bool was_added;
1226 : DeclareVariableName(name.string_, VariableMode::kConst, scope(),
1227 69348 : &was_added);
1228 : }
1229 : }
1230 : V8_INLINE void DeclareClassProperty(const PreParserIdentifier& class_name,
1231 : const PreParserExpression& property,
1232 : bool is_constructor,
1233 : ClassInfo* class_info) {}
1234 :
1235 : V8_INLINE void DeclareClassField(const PreParserExpression& property,
1236 : const PreParserIdentifier& property_name,
1237 : bool is_static, bool is_computed_name,
1238 : bool is_private, ClassInfo* class_info) {
1239 : DCHECK_IMPLIES(is_computed_name, !is_private);
1240 29592 : if (is_computed_name) {
1241 : bool was_added;
1242 : DeclareVariableName(
1243 : ClassFieldVariableName(ast_value_factory(),
1244 : class_info->computed_field_count),
1245 4714 : VariableMode::kConst, scope(), &was_added);
1246 24878 : } else if (is_private && property_name.string_ != nullptr) {
1247 : bool was_added;
1248 : DeclareVariableName(property_name.string_, VariableMode::kConst, scope(),
1249 9275 : &was_added);
1250 : }
1251 : }
1252 :
1253 : V8_INLINE PreParserExpression
1254 : RewriteClassLiteral(Scope* scope, const PreParserIdentifier& name,
1255 : ClassInfo* class_info, int pos, int end_pos) {
1256 53939 : bool has_default_constructor = !class_info->has_seen_constructor;
1257 : // Account for the default constructor.
1258 53939 : if (has_default_constructor) {
1259 : // Creating and disposing of a FunctionState makes tracking of
1260 : // next_function_is_likely_called match what Parser does. TODO(marja):
1261 : // Make the lazy function + next_function_is_likely_called + default ctor
1262 : // logic less surprising. Default ctors shouldn't affect the laziness of
1263 : // functions.
1264 52948 : bool has_extends = class_info->extends.IsNull();
1265 : FunctionKind kind = has_extends ? FunctionKind::kDefaultDerivedConstructor
1266 52948 : : FunctionKind::kDefaultBaseConstructor;
1267 52948 : DeclarationScope* function_scope = NewFunctionScope(kind);
1268 : SetLanguageMode(function_scope, LanguageMode::kStrict);
1269 52948 : function_scope->set_start_position(pos);
1270 52948 : function_scope->set_end_position(pos);
1271 52948 : FunctionState function_state(&function_state_, &scope_, function_scope);
1272 52948 : GetNextFunctionLiteralId();
1273 : }
1274 53939 : if (class_info->has_static_class_fields) {
1275 7875 : GetNextFunctionLiteralId();
1276 : }
1277 53939 : if (class_info->has_instance_members) {
1278 17597 : GetNextFunctionLiteralId();
1279 : }
1280 53939 : return PreParserExpression::Default();
1281 : }
1282 :
1283 : V8_INLINE PreParserStatement DeclareNative(const PreParserIdentifier& name,
1284 : int pos) {
1285 : return PreParserStatement::Default();
1286 : }
1287 :
1288 : V8_INLINE void QueueDestructuringAssignmentForRewriting(
1289 : PreParserExpression assignment) {}
1290 :
1291 : // Helper functions for recursive descent.
1292 : V8_INLINE bool IsEval(const PreParserIdentifier& identifier) const {
1293 3861121 : return identifier.IsEval();
1294 : }
1295 :
1296 : V8_INLINE bool IsAsync(const PreParserIdentifier& identifier) const {
1297 : return identifier.IsAsync();
1298 : }
1299 :
1300 : V8_INLINE bool IsArguments(const PreParserIdentifier& identifier) const {
1301 40046880 : return identifier.IsArguments();
1302 : }
1303 :
1304 : V8_INLINE bool IsEvalOrArguments(
1305 8021946 : const PreParserIdentifier& identifier) const {
1306 9885740 : return identifier.IsEvalOrArguments();
1307 : }
1308 :
1309 : V8_INLINE bool IsAwait(const PreParserIdentifier& identifier) const {
1310 : return identifier.IsAwait();
1311 : }
1312 :
1313 : // Returns true if the expression is of type "this.foo".
1314 : V8_INLINE static bool IsThisProperty(const PreParserExpression& expression) {
1315 5512896 : return expression.IsThisProperty();
1316 : }
1317 :
1318 : V8_INLINE static bool IsIdentifier(const PreParserExpression& expression) {
1319 64096661 : return expression.IsIdentifier();
1320 : }
1321 :
1322 : V8_INLINE static PreParserIdentifier AsIdentifier(
1323 : const PreParserExpression& expression) {
1324 12134878 : return expression.AsIdentifier();
1325 : }
1326 :
1327 : V8_INLINE static PreParserExpression AsIdentifierExpression(
1328 : const PreParserExpression& expression) {
1329 : return expression;
1330 : }
1331 :
1332 34835 : V8_INLINE bool IsConstructor(const PreParserIdentifier& identifier) const {
1333 34835 : return identifier.IsConstructor();
1334 : }
1335 :
1336 43917 : V8_INLINE bool IsName(const PreParserIdentifier& identifier) const {
1337 43917 : return identifier.IsName();
1338 : }
1339 :
1340 : V8_INLINE static bool IsBoilerplateProperty(
1341 : const PreParserExpression& property) {
1342 : // PreParser doesn't count boilerplate properties.
1343 : return false;
1344 : }
1345 :
1346 : V8_INLINE bool IsNative(const PreParserExpression& expr) const {
1347 : // Preparsing is disabled for extensions (because the extension
1348 : // details aren't passed to lazily compiled functions), so we
1349 : // don't accept "native function" in the preparser and there is
1350 : // no need to keep track of "native".
1351 : return false;
1352 : }
1353 :
1354 : V8_INLINE static bool IsArrayIndex(const PreParserIdentifier& string,
1355 : uint32_t* index) {
1356 : return false;
1357 : }
1358 :
1359 : V8_INLINE bool IsStringLiteral(PreParserStatement statement) const {
1360 206664 : return statement.IsStringLiteral();
1361 : }
1362 :
1363 : V8_INLINE static void GetDefaultStrings(
1364 : PreParserIdentifier* default_string,
1365 : PreParserIdentifier* star_default_star_string) {}
1366 :
1367 : // Functions for encapsulating the differences between parsing and preparsing;
1368 : // operations interleaved with the recursive descent.
1369 : V8_INLINE static void PushLiteralName(const PreParserIdentifier& id) {}
1370 : V8_INLINE static void PushVariableName(const PreParserIdentifier& id) {}
1371 : V8_INLINE void PushPropertyName(const PreParserExpression& expression) {}
1372 : V8_INLINE void PushEnclosingName(const PreParserIdentifier& name) {}
1373 : V8_INLINE static void AddFunctionForNameInference(
1374 : const PreParserExpression& expression) {}
1375 : V8_INLINE static void InferFunctionName() {}
1376 :
1377 : V8_INLINE static void CheckAssigningFunctionLiteralToProperty(
1378 : const PreParserExpression& left, const PreParserExpression& right) {}
1379 :
1380 : V8_INLINE bool ShortcutNumericLiteralBinaryExpression(
1381 : PreParserExpression* x, const PreParserExpression& y, Token::Value op,
1382 : int pos) {
1383 : return false;
1384 : }
1385 :
1386 : V8_INLINE NaryOperation* CollapseNaryExpression(PreParserExpression* x,
1387 : PreParserExpression y,
1388 : Token::Value op, int pos,
1389 : const SourceRange& range) {
1390 5221375 : x->clear_parenthesized();
1391 : return nullptr;
1392 : }
1393 :
1394 : V8_INLINE PreParserExpression BuildUnaryExpression(
1395 : const PreParserExpression& expression, Token::Value op, int pos) {
1396 1253855 : return PreParserExpression::Default();
1397 : }
1398 :
1399 : V8_INLINE PreParserStatement
1400 : BuildInitializationBlock(DeclarationParsingResult* parsing_result) {
1401 4553149 : return PreParserStatement::Default();
1402 : }
1403 :
1404 : V8_INLINE PreParserBlock RewriteForVarInLegacy(const ForInfo& for_info) {
1405 66194 : return PreParserBlock::Null();
1406 : }
1407 :
1408 : V8_INLINE void DesugarBindingInForEachStatement(
1409 : ForInfo* for_info, PreParserStatement* body_block,
1410 : PreParserExpression* each_variable) {
1411 : }
1412 :
1413 : V8_INLINE PreParserBlock CreateForEachStatementTDZ(PreParserBlock init_block,
1414 : const ForInfo& for_info) {
1415 109779 : if (IsLexicalVariableMode(for_info.parsing_result.descriptor.mode)) {
1416 175525 : for (auto name : for_info.bound_names) {
1417 : bool was_added;
1418 89748 : DeclareVariableName(name, VariableMode::kLet, scope(), &was_added);
1419 : }
1420 85777 : return PreParserBlock::Default();
1421 : }
1422 : return init_block;
1423 : }
1424 :
1425 : V8_INLINE StatementT DesugarLexicalBindingsInForStatement(
1426 : PreParserStatement loop, PreParserStatement init,
1427 : const PreParserExpression& cond, PreParserStatement next,
1428 : PreParserStatement body, Scope* inner_scope, const ForInfo& for_info) {
1429 : // See Parser::DesugarLexicalBindingsInForStatement.
1430 688 : for (auto name : for_info.bound_names) {
1431 : bool was_added;
1432 : DeclareVariableName(name, for_info.parsing_result.descriptor.mode,
1433 359 : inner_scope, &was_added);
1434 : }
1435 : return loop;
1436 : }
1437 :
1438 : PreParserBlock BuildParameterInitializationBlock(
1439 : const PreParserFormalParameters& parameters);
1440 :
1441 : V8_INLINE PreParserBlock
1442 : BuildRejectPromiseOnException(PreParserStatement init_block) {
1443 612 : return PreParserBlock::Default();
1444 : }
1445 :
1446 : V8_INLINE void InsertSloppyBlockFunctionVarBindings(DeclarationScope* scope) {
1447 508495 : scope->HoistSloppyBlockFunctions(nullptr);
1448 : }
1449 :
1450 : V8_INLINE void InsertShadowingVarBindingInitializers(
1451 : PreParserStatement block) {}
1452 :
1453 : V8_INLINE PreParserExpression NewThrowReferenceError(MessageTemplate message,
1454 : int pos) {
1455 1052 : return PreParserExpression::Default();
1456 : }
1457 :
1458 : V8_INLINE PreParserExpression NewThrowSyntaxError(
1459 : MessageTemplate message, const PreParserIdentifier& arg, int pos) {
1460 : return PreParserExpression::Default();
1461 : }
1462 :
1463 : V8_INLINE PreParserExpression NewThrowTypeError(
1464 : MessageTemplate message, const PreParserIdentifier& arg, int pos) {
1465 : return PreParserExpression::Default();
1466 : }
1467 :
1468 : // Reporting errors.
1469 200554 : void ReportMessageAt(Scanner::Location source_location,
1470 : MessageTemplate message, const char* arg = nullptr,
1471 200554 : ParseErrorType error_type = kSyntaxError) {
1472 : pending_error_handler()->ReportMessageAt(source_location.beg_pos,
1473 : source_location.end_pos, message,
1474 401108 : arg, error_type);
1475 200554 : scanner()->set_parser_error();
1476 200554 : }
1477 :
1478 1637780 : V8_INLINE void ReportUnidentifiableError() {
1479 1637780 : pending_error_handler()->set_unidentifiable_error();
1480 1637780 : scanner()->set_parser_error();
1481 : }
1482 :
1483 : V8_INLINE void ReportMessageAt(Scanner::Location source_location,
1484 : MessageTemplate message,
1485 : const PreParserIdentifier& arg,
1486 : ParseErrorType error_type = kSyntaxError) {
1487 0 : UNREACHABLE();
1488 : }
1489 :
1490 0 : void ReportMessageAt(Scanner::Location source_location,
1491 : MessageTemplate message, const AstRawString* arg,
1492 0 : ParseErrorType error_type = kSyntaxError) {
1493 : pending_error_handler()->ReportMessageAt(source_location.beg_pos,
1494 : source_location.end_pos, message,
1495 0 : arg, error_type);
1496 0 : scanner()->set_parser_error();
1497 0 : }
1498 :
1499 : // "null" return type creators.
1500 : V8_INLINE static PreParserIdentifier NullIdentifier() {
1501 3142846 : return PreParserIdentifier::Null();
1502 : }
1503 : V8_INLINE static PreParserExpression NullExpression() {
1504 13974334 : return PreParserExpression::Null();
1505 : }
1506 : V8_INLINE static PreParserExpression FailureExpression() {
1507 634403 : return PreParserExpression::Failure();
1508 : }
1509 : V8_INLINE static PreParserExpression NullLiteralProperty() {
1510 78640 : return PreParserExpression::Null();
1511 : }
1512 : V8_INLINE static PreParserStatementList NullStatementList() {
1513 : return PreParserStatementList::Null();
1514 : }
1515 : V8_INLINE static PreParserStatement NullStatement() {
1516 7208973 : return PreParserStatement::Null();
1517 : }
1518 823591 : V8_INLINE static PreParserBlock NullBlock() { return PreParserBlock::Null(); }
1519 :
1520 : template <typename T>
1521 : V8_INLINE static bool IsNull(T subject) {
1522 26277942 : return subject.IsNull();
1523 : }
1524 :
1525 : V8_INLINE PreParserIdentifier EmptyIdentifierString() const {
1526 647013 : return PreParserIdentifier::Default();
1527 : }
1528 :
1529 : // Producing data during the recursive descent.
1530 : PreParserIdentifier GetSymbol() const;
1531 :
1532 : V8_INLINE PreParserIdentifier GetNextSymbol() const {
1533 29923 : return PreParserIdentifier::Default();
1534 : }
1535 :
1536 : V8_INLINE PreParserIdentifier GetNumberAsSymbol() const {
1537 3527 : return PreParserIdentifier::Default();
1538 : }
1539 :
1540 : V8_INLINE PreParserExpression ThisExpression() {
1541 : UseThis();
1542 3041577 : return PreParserExpression::This();
1543 : }
1544 :
1545 : V8_INLINE PreParserExpression NewSuperPropertyReference(int pos) {
1546 : scope()->NewUnresolved(factory()->ast_node_factory(),
1547 : ast_value_factory()->this_function_string(), pos,
1548 4236 : NORMAL_VARIABLE);
1549 4236 : return PreParserExpression::Default();
1550 : }
1551 :
1552 : V8_INLINE PreParserExpression NewSuperCallReference(int pos) {
1553 : scope()->NewUnresolved(factory()->ast_node_factory(),
1554 : ast_value_factory()->this_function_string(), pos,
1555 3072 : NORMAL_VARIABLE);
1556 : scope()->NewUnresolved(factory()->ast_node_factory(),
1557 : ast_value_factory()->new_target_string(), pos,
1558 3073 : NORMAL_VARIABLE);
1559 3074 : return PreParserExpression::SuperCallReference();
1560 : }
1561 :
1562 : V8_INLINE PreParserExpression NewTargetExpression(int pos) {
1563 4101 : return PreParserExpression::NewTargetExpression();
1564 : }
1565 :
1566 : V8_INLINE PreParserExpression ImportMetaExpression(int pos) {
1567 7925 : return PreParserExpression::Default();
1568 : }
1569 :
1570 : V8_INLINE PreParserExpression ExpressionFromLiteral(Token::Value token,
1571 : int pos) {
1572 16851785 : if (token != Token::STRING) return PreParserExpression::Default();
1573 5850427 : return PreParserExpression::StringLiteral();
1574 : }
1575 :
1576 39986401 : PreParserExpression ExpressionFromIdentifier(
1577 39982141 : const PreParserIdentifier& name, int start_position,
1578 : InferName infer = InferName::kYes) {
1579 39986401 : if (name.string_ != nullptr) {
1580 39968261 : expression_scope()->NewVariable(name.string_, start_position);
1581 : }
1582 39982141 : return PreParserExpression::FromIdentifier(name);
1583 : }
1584 :
1585 : V8_INLINE void DeclareIdentifier(const PreParserIdentifier& name,
1586 : int start_position) {
1587 238682 : if (name.string_ != nullptr) {
1588 235604 : expression_scope()->Declare(name.string_, start_position);
1589 : }
1590 : }
1591 :
1592 : V8_INLINE Variable* DeclareCatchVariableName(
1593 : Scope* scope, const PreParserIdentifier& identifier) {
1594 271361 : return scope->DeclareCatchVariableName(identifier.string_);
1595 : }
1596 :
1597 : V8_INLINE PreParserPropertyList NewClassPropertyList(int size) const {
1598 : return PreParserPropertyList();
1599 : }
1600 :
1601 : V8_INLINE PreParserStatementList NewStatementList(int size) const {
1602 : return PreParserStatementList();
1603 : }
1604 :
1605 : V8_INLINE PreParserExpression
1606 : NewV8Intrinsic(const PreParserIdentifier& name,
1607 : const PreParserExpressionList& arguments, int pos) {
1608 72867 : return PreParserExpression::Default();
1609 : }
1610 :
1611 : V8_INLINE PreParserStatement
1612 : NewThrowStatement(const PreParserExpression& exception, int pos) {
1613 263577 : return PreParserStatement::Jump();
1614 : }
1615 :
1616 : V8_INLINE void AddFormalParameter(PreParserFormalParameters* parameters,
1617 : PreParserExpression& pattern,
1618 : const PreParserExpression& initializer,
1619 : int initializer_end_position,
1620 : bool is_rest) {
1621 3918555 : DeclarationScope* scope = parameters->scope;
1622 3918555 : scope->RecordParameter(is_rest);
1623 3918554 : parameters->UpdateArityAndFunctionLength(!initializer.IsNull(), is_rest);
1624 : }
1625 :
1626 : V8_INLINE void DeclareFormalParameters(
1627 : const PreParserFormalParameters* parameters) {
1628 2899755 : if (!parameters->is_simple) parameters->scope->SetHasNonSimpleParameters();
1629 : }
1630 :
1631 : V8_INLINE void DeclareArrowFunctionFormalParameters(
1632 : PreParserFormalParameters* parameters, const PreParserExpression& params,
1633 : const Scanner::Location& params_loc) {
1634 : }
1635 :
1636 : V8_INLINE PreParserExpression
1637 : ExpressionListToExpression(const PreParserExpressionList& args) {
1638 76082 : return PreParserExpression::Default();
1639 : }
1640 :
1641 : V8_INLINE void SetFunctionNameFromPropertyName(
1642 : const PreParserExpression& property, const PreParserIdentifier& name,
1643 : const AstRawString* prefix = nullptr) {}
1644 : V8_INLINE void SetFunctionNameFromIdentifierRef(
1645 : const PreParserExpression& value, const PreParserExpression& identifier) {
1646 : }
1647 :
1648 : V8_INLINE void CountUsage(v8::Isolate::UseCounterFeature feature) {
1649 1052 : if (use_counts_ != nullptr) ++use_counts_[feature];
1650 : }
1651 :
1652 : V8_INLINE bool ParsingDynamicFunctionDeclaration() const { return false; }
1653 :
1654 : // Generate empty functions here as the preparser does not collect source
1655 : // ranges for block coverage.
1656 : #define DEFINE_RECORD_SOURCE_RANGE(Name) \
1657 : template <typename... Ts> \
1658 : V8_INLINE void Record##Name##SourceRange(Ts... args) {}
1659 : AST_SOURCE_RANGE_LIST(DEFINE_RECORD_SOURCE_RANGE)
1660 : #undef DEFINE_RECORD_SOURCE_RANGE
1661 :
1662 : // Preparser's private field members.
1663 :
1664 : int* use_counts_;
1665 : PreParserLogger log_;
1666 :
1667 : PreparseDataBuilder* preparse_data_builder_;
1668 : std::vector<void*> preparse_data_builder_buffer_;
1669 : };
1670 :
1671 : PreParserExpression PreParser::SpreadCall(const PreParserExpression& function,
1672 : const PreParserExpressionList& args,
1673 : int pos,
1674 : Call::PossiblyEval possibly_eval) {
1675 4965 : return factory()->NewCall(function, args, pos, possibly_eval);
1676 : }
1677 :
1678 : PreParserExpression PreParser::SpreadCallNew(
1679 : const PreParserExpression& function, const PreParserExpressionList& args,
1680 : int pos) {
1681 136 : return factory()->NewCallNew(function, args, pos);
1682 : }
1683 :
1684 : } // namespace internal
1685 : } // namespace v8
1686 :
1687 : #endif // V8_PARSING_PREPARSER_H_
|