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/preparse-data.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 PreParserIdentifier {
26 : public:
27 : PreParserIdentifier() : type_(kUnknownIdentifier) {}
28 924890 : static PreParserIdentifier Default() {
29 924890 : return PreParserIdentifier(kUnknownIdentifier);
30 : }
31 2211516 : static PreParserIdentifier Empty() {
32 2211516 : return PreParserIdentifier(kEmptyIdentifier);
33 : }
34 : static PreParserIdentifier Eval() {
35 : return PreParserIdentifier(kEvalIdentifier);
36 : }
37 : static PreParserIdentifier Arguments() {
38 : return PreParserIdentifier(kArgumentsIdentifier);
39 : }
40 : static PreParserIdentifier Undefined() {
41 : return PreParserIdentifier(kUndefinedIdentifier);
42 : }
43 : static PreParserIdentifier FutureReserved() {
44 : return PreParserIdentifier(kFutureReservedIdentifier);
45 : }
46 : static PreParserIdentifier FutureStrictReserved() {
47 : return PreParserIdentifier(kFutureStrictReservedIdentifier);
48 : }
49 : static PreParserIdentifier Let() {
50 : return PreParserIdentifier(kLetIdentifier);
51 : }
52 : static PreParserIdentifier Static() {
53 : return PreParserIdentifier(kStaticIdentifier);
54 : }
55 : static PreParserIdentifier Yield() {
56 : return PreParserIdentifier(kYieldIdentifier);
57 : }
58 : static PreParserIdentifier Prototype() {
59 : return PreParserIdentifier(kPrototypeIdentifier);
60 : }
61 : static PreParserIdentifier Constructor() {
62 : return PreParserIdentifier(kConstructorIdentifier);
63 : }
64 : static PreParserIdentifier Enum() {
65 : return PreParserIdentifier(kEnumIdentifier);
66 : }
67 : static PreParserIdentifier Await() {
68 : return PreParserIdentifier(kAwaitIdentifier);
69 : }
70 : static PreParserIdentifier Async() {
71 : return PreParserIdentifier(kAsyncIdentifier);
72 : }
73 : static PreParserIdentifier Name() {
74 : return PreParserIdentifier(kNameIdentifier);
75 : }
76 0 : bool IsEmpty() const { return type_ == kEmptyIdentifier; }
77 1773439 : bool IsEval() const { return type_ == kEvalIdentifier; }
78 : bool IsArguments() const { return type_ == kArgumentsIdentifier; }
79 30317349 : bool IsEvalOrArguments() const { return IsEval() || IsArguments(); }
80 : bool IsUndefined() const { return type_ == kUndefinedIdentifier; }
81 : bool IsLet() const { return type_ == kLetIdentifier; }
82 : bool IsStatic() const { return type_ == kStaticIdentifier; }
83 : bool IsYield() const { return type_ == kYieldIdentifier; }
84 : bool IsPrototype() const { return type_ == kPrototypeIdentifier; }
85 14424 : bool IsConstructor() const { return type_ == kConstructorIdentifier; }
86 : bool IsEnum() const { return type_ == kEnumIdentifier; }
87 : bool IsAwait() const { return type_ == kAwaitIdentifier; }
88 10887 : bool IsName() const { return type_ == kNameIdentifier; }
89 :
90 : // Allow identifier->name()[->length()] to work. The preparser
91 : // does not need the actual positions/lengths of the identifiers.
92 : const PreParserIdentifier* operator->() const { return this; }
93 : const PreParserIdentifier raw_name() const { return *this; }
94 :
95 : int position() const { return 0; }
96 : int length() const { return 0; }
97 :
98 : private:
99 : enum Type {
100 : kEmptyIdentifier,
101 : kUnknownIdentifier,
102 : kFutureReservedIdentifier,
103 : kFutureStrictReservedIdentifier,
104 : kLetIdentifier,
105 : kStaticIdentifier,
106 : kYieldIdentifier,
107 : kEvalIdentifier,
108 : kArgumentsIdentifier,
109 : kUndefinedIdentifier,
110 : kPrototypeIdentifier,
111 : kConstructorIdentifier,
112 : kEnumIdentifier,
113 : kAwaitIdentifier,
114 : kAsyncIdentifier,
115 : kNameIdentifier
116 : };
117 :
118 : explicit PreParserIdentifier(Type type) : type_(type), string_(nullptr) {}
119 : Type type_;
120 : // Only non-nullptr when PreParser.track_unresolved_variables_ is true.
121 : const AstRawString* string_;
122 : friend class PreParserExpression;
123 : friend class PreParser;
124 : friend class PreParserFactory;
125 : };
126 :
127 :
128 : class PreParserExpression {
129 : public:
130 : PreParserExpression()
131 3450389 : : code_(TypeField::encode(kEmpty)), variables_(nullptr) {}
132 :
133 32039765 : static PreParserExpression Empty() { return PreParserExpression(); }
134 :
135 12141781 : static PreParserExpression Default(
136 : ZoneList<VariableProxy*>* variables = nullptr) {
137 12141781 : return PreParserExpression(TypeField::encode(kExpression), variables);
138 : }
139 :
140 : static PreParserExpression Spread(PreParserExpression expression) {
141 : return PreParserExpression(TypeField::encode(kSpreadExpression),
142 : expression.variables_);
143 : }
144 :
145 28362985 : static PreParserExpression FromIdentifier(PreParserIdentifier id,
146 : VariableProxy* variable,
147 : Zone* zone) {
148 : PreParserExpression expression(TypeField::encode(kIdentifierExpression) |
149 56725970 : IdentifierTypeField::encode(id.type_));
150 28362985 : expression.AddVariable(variable, zone);
151 28362977 : return expression;
152 : }
153 :
154 3736173 : static PreParserExpression BinaryOperation(PreParserExpression left,
155 : Token::Value op,
156 : PreParserExpression right,
157 : Zone* zone) {
158 3736173 : if (op == Token::COMMA) {
159 : // Possibly an arrow function parameter list.
160 202419 : if (left.variables_ == nullptr) {
161 : return PreParserExpression(TypeField::encode(kExpression),
162 193063 : right.variables_);
163 : }
164 9356 : if (right.variables_ != nullptr) {
165 19697 : for (auto variable : *right.variables_) {
166 : left.variables_->Add(variable, zone);
167 : }
168 : }
169 : return PreParserExpression(TypeField::encode(kExpression),
170 9356 : left.variables_);
171 : }
172 3533754 : return PreParserExpression(TypeField::encode(kExpression));
173 : }
174 :
175 : static PreParserExpression Assignment(ZoneList<VariableProxy*>* variables) {
176 : return PreParserExpression(TypeField::encode(kExpression) |
177 : ExpressionTypeField::encode(kAssignment),
178 : variables);
179 : }
180 :
181 4268 : static PreParserExpression NewTargetExpression() {
182 : return PreParserExpression(TypeField::encode(kExpression) |
183 4268 : ExpressionTypeField::encode(kNewTarget));
184 : }
185 :
186 : static PreParserExpression ObjectLiteral(
187 : ZoneList<VariableProxy*>* variables) {
188 : return PreParserExpression(TypeField::encode(kObjectLiteralExpression),
189 : variables);
190 : }
191 :
192 : static PreParserExpression ArrayLiteral(ZoneList<VariableProxy*>* variables) {
193 : return PreParserExpression(TypeField::encode(kArrayLiteralExpression),
194 : variables);
195 : }
196 :
197 3090024 : static PreParserExpression StringLiteral() {
198 3090024 : return PreParserExpression(TypeField::encode(kStringLiteralExpression));
199 : }
200 :
201 233029 : static PreParserExpression UseStrictStringLiteral() {
202 : return PreParserExpression(TypeField::encode(kStringLiteralExpression) |
203 233029 : IsUseStrictField::encode(true));
204 : }
205 :
206 : static PreParserExpression UseAsmStringLiteral() {
207 : return PreParserExpression(TypeField::encode(kStringLiteralExpression) |
208 : IsUseAsmField::encode(true));
209 : }
210 :
211 175561 : static PreParserExpression This(ZoneList<VariableProxy*>* variables) {
212 : return PreParserExpression(TypeField::encode(kExpression) |
213 : ExpressionTypeField::encode(kThisExpression),
214 175561 : variables);
215 : }
216 :
217 : static PreParserExpression ThisProperty() {
218 : return PreParserExpression(
219 : TypeField::encode(kExpression) |
220 : ExpressionTypeField::encode(kThisPropertyExpression));
221 : }
222 :
223 : static PreParserExpression Property() {
224 : return PreParserExpression(
225 : TypeField::encode(kExpression) |
226 : ExpressionTypeField::encode(kPropertyExpression));
227 : }
228 :
229 : static PreParserExpression Call() {
230 : return PreParserExpression(TypeField::encode(kExpression) |
231 : ExpressionTypeField::encode(kCallExpression));
232 : }
233 :
234 : static PreParserExpression CallEval() {
235 : return PreParserExpression(
236 : TypeField::encode(kExpression) |
237 : ExpressionTypeField::encode(kCallEvalExpression));
238 : }
239 :
240 146 : static PreParserExpression SuperCallReference() {
241 : return PreParserExpression(
242 : TypeField::encode(kExpression) |
243 146 : ExpressionTypeField::encode(kSuperCallReference));
244 : }
245 :
246 32357 : static PreParserExpression NoTemplateTag() {
247 : return PreParserExpression(
248 : TypeField::encode(kExpression) |
249 32357 : ExpressionTypeField::encode(kNoTemplateTagExpression));
250 : }
251 :
252 33396132 : bool IsEmpty() const { return TypeField::decode(code_) == kEmpty; }
253 :
254 65102101 : bool IsIdentifier() const {
255 130204202 : return TypeField::decode(code_) == kIdentifierExpression;
256 : }
257 :
258 7972711 : PreParserIdentifier AsIdentifier() const {
259 : DCHECK(IsIdentifier());
260 15945422 : return PreParserIdentifier(IdentifierTypeField::decode(code_));
261 : }
262 :
263 : bool IsAssignment() const {
264 5706574 : return TypeField::decode(code_) == kExpression &&
265 : ExpressionTypeField::decode(code_) == kAssignment;
266 : }
267 :
268 : bool IsObjectLiteral() const {
269 : return TypeField::decode(code_) == kObjectLiteralExpression;
270 : }
271 :
272 : bool IsArrayLiteral() const {
273 : return TypeField::decode(code_) == kArrayLiteralExpression;
274 : }
275 :
276 : bool IsStringLiteral() const {
277 : return TypeField::decode(code_) == kStringLiteralExpression;
278 : }
279 :
280 : bool IsUseStrictLiteral() const {
281 6078730 : return TypeField::decode(code_) == kStringLiteralExpression &&
282 : IsUseStrictField::decode(code_);
283 : }
284 :
285 : bool IsUseAsmLiteral() const {
286 5613056 : return TypeField::decode(code_) == kStringLiteralExpression &&
287 : IsUseAsmField::decode(code_);
288 : }
289 :
290 : bool IsThis() const {
291 8033442 : return TypeField::decode(code_) == kExpression &&
292 : ExpressionTypeField::decode(code_) == kThisExpression;
293 : }
294 :
295 4102747 : bool IsThisProperty() const {
296 11024177 : return TypeField::decode(code_) == kExpression &&
297 4102747 : ExpressionTypeField::decode(code_) == kThisPropertyExpression;
298 : }
299 :
300 : bool IsProperty() const {
301 49446976 : return TypeField::decode(code_) == kExpression &&
302 16240267 : (ExpressionTypeField::decode(code_) == kPropertyExpression ||
303 : ExpressionTypeField::decode(code_) == kThisPropertyExpression);
304 : }
305 :
306 : bool IsCall() const {
307 22438 : return TypeField::decode(code_) == kExpression &&
308 9434 : (ExpressionTypeField::decode(code_) == kCallExpression ||
309 : ExpressionTypeField::decode(code_) == kCallEvalExpression);
310 : }
311 :
312 : bool IsSuperCallReference() const {
313 5015375 : return TypeField::decode(code_) == kExpression &&
314 : ExpressionTypeField::decode(code_) == kSuperCallReference;
315 : }
316 :
317 : bool IsValidReferenceExpression() const {
318 7427776 : return IsIdentifier() || IsProperty();
319 : }
320 :
321 : // At the moment PreParser doesn't track these expression types.
322 : bool IsFunctionLiteral() const { return false; }
323 : bool IsCallNew() const { return false; }
324 :
325 : bool IsNoTemplateTag() const {
326 : return TypeField::decode(code_) == kExpression &&
327 : ExpressionTypeField::decode(code_) == kNoTemplateTagExpression;
328 : }
329 :
330 : bool IsSpread() const {
331 : return TypeField::decode(code_) == kSpreadExpression;
332 : }
333 :
334 : PreParserExpression AsFunctionLiteral() { return *this; }
335 :
336 : // Dummy implementation for making expression->somefunc() work in both Parser
337 : // and PreParser.
338 : PreParserExpression* operator->() { return this; }
339 :
340 : // More dummy implementations of things PreParser doesn't need to track:
341 : void SetShouldEagerCompile() {}
342 : void set_should_be_used_once_hint() {}
343 :
344 : int position() const { return kNoSourcePosition; }
345 : void set_function_token_position(int position) {}
346 :
347 : private:
348 : enum Type {
349 : kEmpty,
350 : kExpression,
351 : kIdentifierExpression,
352 : kStringLiteralExpression,
353 : kSpreadExpression,
354 : kObjectLiteralExpression,
355 : kArrayLiteralExpression
356 : };
357 :
358 : enum ExpressionType {
359 : kThisExpression,
360 : kThisPropertyExpression,
361 : kPropertyExpression,
362 : kCallExpression,
363 : kCallEvalExpression,
364 : kSuperCallReference,
365 : kNoTemplateTagExpression,
366 : kAssignment,
367 : kNewTarget
368 : };
369 :
370 : explicit PreParserExpression(uint32_t expression_code,
371 : ZoneList<VariableProxy*>* variables = nullptr)
372 28362985 : : code_(expression_code), variables_(variables) {}
373 :
374 29956344 : void AddVariable(VariableProxy* variable, Zone* zone) {
375 29956344 : if (variable == nullptr) {
376 29956334 : return;
377 : }
378 10717820 : if (variables_ == nullptr) {
379 10717814 : variables_ = new (zone) ZoneList<VariableProxy*>(1, zone);
380 : }
381 10717812 : variables_->Add(variable, zone);
382 : }
383 :
384 : // The first three bits are for the Type.
385 : typedef BitField<Type, 0, 3> TypeField;
386 :
387 : // The high order bit applies only to nodes which would inherit from the
388 : // Expression ASTNode --- This is by necessity, due to the fact that
389 : // Expression nodes may be represented as multiple Types, not exclusively
390 : // through kExpression.
391 : // TODO(caitp, adamk): clean up PreParserExpression bitfields.
392 : typedef BitField<bool, 31, 1> ParenthesizedField;
393 :
394 : // The rest of the bits are interpreted depending on the value
395 : // of the Type field, so they can share the storage.
396 : typedef BitField<ExpressionType, TypeField::kNext, 4> ExpressionTypeField;
397 : typedef BitField<bool, TypeField::kNext, 1> IsUseStrictField;
398 : typedef BitField<bool, IsUseStrictField::kNext, 1> IsUseAsmField;
399 : typedef BitField<PreParserIdentifier::Type, TypeField::kNext, 10>
400 : IdentifierTypeField;
401 : typedef BitField<bool, TypeField::kNext, 1> HasCoverInitializedNameField;
402 :
403 : uint32_t code_;
404 : // If the PreParser is used in the variable tracking mode, PreParserExpression
405 : // accumulates variables in that expression.
406 : ZoneList<VariableProxy*>* variables_;
407 :
408 : friend class PreParser;
409 : friend class PreParserFactory;
410 : template <typename T>
411 : friend class PreParserList;
412 : };
413 :
414 :
415 : // The pre-parser doesn't need to build lists of expressions, identifiers, or
416 : // the like. If the PreParser is used in variable tracking mode, it needs to
417 : // build lists of variables though.
418 : template <typename T>
419 : class PreParserList {
420 : public:
421 : // These functions make list->Add(some_expression) work (and do nothing).
422 7517531 : PreParserList() : length_(0), variables_(nullptr) {}
423 : PreParserList* operator->() { return this; }
424 : void Add(const T& element, Zone* zone);
425 : int length() const { return length_; }
426 84590 : static PreParserList Null() { return PreParserList(-1); }
427 : bool IsNull() const { return length_ == -1; }
428 : void Set(int index, const T& element) {}
429 :
430 : private:
431 : explicit PreParserList(int n) : length_(n), variables_(nullptr) {}
432 : int length_;
433 : ZoneList<VariableProxy*>* variables_;
434 :
435 : friend class PreParser;
436 : friend class PreParserFactory;
437 : };
438 :
439 : template <>
440 9854080 : inline void PreParserList<PreParserExpression>::Add(
441 : const PreParserExpression& expression, Zone* zone) {
442 9854080 : if (expression.variables_ != nullptr) {
443 : DCHECK(FLAG_lazy_inner_functions);
444 : DCHECK(zone != nullptr);
445 1411458 : if (variables_ == nullptr) {
446 1174320 : variables_ = new (zone) ZoneList<VariableProxy*>(1, zone);
447 : }
448 4254710 : for (auto identifier : (*expression.variables_)) {
449 1421626 : variables_->Add(identifier, zone);
450 : }
451 : }
452 9854080 : ++length_;
453 9854080 : }
454 :
455 : template <typename T>
456 : void PreParserList<T>::Add(const T& element, Zone* zone) {
457 22794 : ++length_;
458 : }
459 :
460 : typedef PreParserList<PreParserExpression> PreParserExpressionList;
461 :
462 : class PreParserStatement;
463 : typedef PreParserList<PreParserStatement> PreParserStatementList;
464 :
465 : class PreParserStatement {
466 : public:
467 973329 : static PreParserStatement Default() {
468 973329 : return PreParserStatement(kUnknownStatement);
469 : }
470 :
471 8223937 : static PreParserStatement Null() {
472 8223937 : return PreParserStatement(kNullStatement);
473 : }
474 :
475 : static PreParserStatement Empty() {
476 : return PreParserStatement(kEmptyStatement);
477 : }
478 :
479 65586 : static PreParserStatement Jump() {
480 65586 : return PreParserStatement(kJumpStatement);
481 : }
482 :
483 : // Creates expression statement from expression.
484 : // Preserves being an unparenthesized string literal, possibly
485 : // "use strict".
486 5841693 : static PreParserStatement ExpressionStatement(
487 : PreParserExpression expression) {
488 5841693 : if (expression.IsUseStrictLiteral()) {
489 232837 : return PreParserStatement(kUseStrictExpressionStatement);
490 : }
491 5608856 : if (expression.IsUseAsmLiteral()) {
492 0 : return PreParserStatement(kUseAsmExpressionStatement);
493 : }
494 5608856 : if (expression.IsStringLiteral()) {
495 4200 : return PreParserStatement(kStringLiteralExpressionStatement);
496 : }
497 : return Default();
498 : }
499 :
500 4455 : bool IsStringLiteral() {
501 4455 : return code_ == kStringLiteralExpressionStatement || IsUseStrictLiteral() ||
502 4455 : IsUseAsmLiteral();
503 : }
504 :
505 237115 : bool IsUseStrictLiteral() {
506 237115 : return code_ == kUseStrictExpressionStatement;
507 : }
508 :
509 4455 : bool IsUseAsmLiteral() { return code_ == kUseAsmExpressionStatement; }
510 :
511 : bool IsJumpStatement() {
512 : return code_ == kJumpStatement;
513 : }
514 :
515 12831267 : bool IsNullStatement() { return code_ == kNullStatement; }
516 :
517 12499892 : bool IsEmptyStatement() { return code_ == kEmptyStatement; }
518 :
519 : // Dummy implementation for making statement->somefunc() work in both Parser
520 : // and PreParser.
521 : PreParserStatement* operator->() { return this; }
522 :
523 : PreParserStatementList statements() { return PreParserStatementList(); }
524 : void set_scope(Scope* scope) {}
525 : void Initialize(PreParserExpression cond, PreParserStatement body) {}
526 : void Initialize(PreParserStatement init, PreParserExpression cond,
527 : PreParserStatement next, PreParserStatement body) {}
528 :
529 : private:
530 : enum Type {
531 : kNullStatement,
532 : kEmptyStatement,
533 : kUnknownStatement,
534 : kJumpStatement,
535 : kStringLiteralExpressionStatement,
536 : kUseStrictExpressionStatement,
537 : kUseAsmExpressionStatement,
538 : };
539 :
540 : explicit PreParserStatement(Type code) : code_(code) {}
541 : Type code_;
542 : };
543 :
544 :
545 : class PreParserFactory {
546 : public:
547 405607 : explicit PreParserFactory(AstValueFactory* ast_value_factory)
548 : : ast_value_factory_(ast_value_factory),
549 811214 : zone_(ast_value_factory->zone()) {}
550 :
551 3790454 : void set_zone(Zone* zone) { zone_ = zone; }
552 :
553 5241332 : PreParserExpression NewStringLiteral(PreParserIdentifier identifier,
554 : int pos) {
555 : // This is needed for object literal property names. Property names are
556 : // normalized to string literals during object literal parsing.
557 5241332 : PreParserExpression expression = PreParserExpression::Default();
558 5241332 : if (identifier.string_ != nullptr) {
559 : DCHECK(FLAG_lazy_inner_functions);
560 : AstNodeFactory factory(ast_value_factory_);
561 1593362 : factory.set_zone(zone_);
562 : VariableProxy* variable =
563 : factory.NewVariableProxy(identifier.string_, NORMAL_VARIABLE);
564 1593362 : expression.AddVariable(variable, zone_);
565 : }
566 5241331 : return expression;
567 : }
568 : PreParserExpression NewNumberLiteral(double number,
569 : int pos) {
570 : return PreParserExpression::Default();
571 : }
572 : PreParserExpression NewUndefinedLiteral(int pos) {
573 : return PreParserExpression::Default();
574 : }
575 : PreParserExpression NewRegExpLiteral(PreParserIdentifier js_pattern,
576 : int js_flags, int pos) {
577 : return PreParserExpression::Default();
578 : }
579 : PreParserExpression NewArrayLiteral(PreParserExpressionList values,
580 : int first_spread_index, int pos) {
581 : return PreParserExpression::ArrayLiteral(values.variables_);
582 : }
583 : PreParserExpression NewClassLiteralProperty(PreParserExpression key,
584 : PreParserExpression value,
585 : ClassLiteralProperty::Kind kind,
586 : bool is_static,
587 : bool is_computed_name) {
588 : return PreParserExpression::Default();
589 : }
590 : PreParserExpression NewObjectLiteralProperty(PreParserExpression key,
591 : PreParserExpression value,
592 : ObjectLiteralProperty::Kind kind,
593 : bool is_computed_name) {
594 : return PreParserExpression::Default(value.variables_);
595 : }
596 : PreParserExpression NewObjectLiteralProperty(PreParserExpression key,
597 : PreParserExpression value,
598 : bool is_computed_name) {
599 : return PreParserExpression::Default(value.variables_);
600 : }
601 : PreParserExpression NewObjectLiteral(PreParserExpressionList properties,
602 : int boilerplate_properties, int pos,
603 : bool has_rest_property) {
604 : return PreParserExpression::ObjectLiteral(properties.variables_);
605 : }
606 : PreParserExpression NewVariableProxy(void* variable) {
607 : return PreParserExpression::Default();
608 : }
609 : PreParserExpression NewProperty(PreParserExpression obj,
610 : PreParserExpression key,
611 : int pos) {
612 6960348 : if (obj.IsThis()) {
613 : return PreParserExpression::ThisProperty();
614 : }
615 : return PreParserExpression::Property();
616 : }
617 : PreParserExpression NewUnaryOperation(Token::Value op,
618 : PreParserExpression expression,
619 : int pos) {
620 : return PreParserExpression::Default();
621 : }
622 : PreParserExpression NewBinaryOperation(Token::Value op,
623 : PreParserExpression left,
624 : PreParserExpression right, int pos) {
625 3736173 : return PreParserExpression::BinaryOperation(left, op, right, zone_);
626 : }
627 : PreParserExpression NewCompareOperation(Token::Value op,
628 : PreParserExpression left,
629 : PreParserExpression right, int pos) {
630 : return PreParserExpression::Default();
631 : }
632 : PreParserExpression NewRewritableExpression(PreParserExpression expression) {
633 : return expression;
634 : }
635 : PreParserExpression NewAssignment(Token::Value op,
636 : PreParserExpression left,
637 : PreParserExpression right,
638 : int pos) {
639 : // Identifiers need to be tracked since this might be a parameter with a
640 : // default value inside an arrow function parameter list.
641 : return PreParserExpression::Assignment(left.variables_);
642 : }
643 : PreParserExpression NewSuspend(PreParserExpression generator_object,
644 : PreParserExpression expression, int pos,
645 : Suspend::OnException on_exception,
646 : SuspendFlags flags) {
647 : return PreParserExpression::Default();
648 : }
649 : PreParserExpression NewConditional(PreParserExpression condition,
650 : PreParserExpression then_expression,
651 : PreParserExpression else_expression,
652 : int pos) {
653 : return PreParserExpression::Default();
654 : }
655 : PreParserExpression NewCountOperation(Token::Value op,
656 : bool is_prefix,
657 : PreParserExpression expression,
658 : int pos) {
659 : return PreParserExpression::Default();
660 : }
661 1272 : PreParserExpression NewCall(
662 : PreParserExpression expression, PreParserExpressionList arguments,
663 : int pos, Call::PossiblyEval possibly_eval = Call::NOT_EVAL) {
664 3394453 : if (possibly_eval == Call::IS_POSSIBLY_EVAL) {
665 : DCHECK(expression.IsIdentifier() && expression.AsIdentifier().IsEval());
666 : return PreParserExpression::CallEval();
667 : }
668 : return PreParserExpression::Call();
669 : }
670 21 : PreParserExpression NewCallNew(PreParserExpression expression,
671 : PreParserExpressionList arguments,
672 : int pos) {
673 21 : return PreParserExpression::Default();
674 : }
675 : PreParserStatement NewReturnStatement(PreParserExpression expression,
676 : int pos) {
677 : return PreParserStatement::Jump();
678 : }
679 : PreParserStatement NewAsyncReturnStatement(PreParserExpression expression,
680 : int pos) {
681 : return PreParserStatement::Jump();
682 : }
683 : PreParserExpression NewFunctionLiteral(
684 : PreParserIdentifier name, Scope* scope, PreParserStatementList body,
685 : int expected_property_count, int parameter_count, int function_length,
686 : FunctionLiteral::ParameterFlag has_duplicate_parameters,
687 : FunctionLiteral::FunctionType function_type,
688 : FunctionLiteral::EagerCompileHint eager_compile_hint, int position,
689 : bool has_braces, int function_literal_id) {
690 : return PreParserExpression::Default();
691 : }
692 :
693 : PreParserExpression NewSpread(PreParserExpression expression, int pos,
694 : int expr_pos) {
695 : return PreParserExpression::Spread(expression);
696 : }
697 :
698 : PreParserExpression NewEmptyParentheses(int pos) {
699 : return PreParserExpression::Default();
700 : }
701 :
702 : PreParserStatement NewEmptyStatement(int pos) {
703 : return PreParserStatement::Default();
704 : }
705 :
706 : PreParserStatement NewBlock(ZoneList<const AstRawString*>* labels,
707 : int capacity, bool ignore_completion_value,
708 : int pos) {
709 : return PreParserStatement::Default();
710 : }
711 :
712 : PreParserStatement NewDebuggerStatement(int pos) {
713 : return PreParserStatement::Default();
714 : }
715 :
716 : PreParserStatement NewExpressionStatement(PreParserExpression expr, int pos) {
717 5841693 : return PreParserStatement::ExpressionStatement(expr);
718 : }
719 :
720 : PreParserStatement NewIfStatement(PreParserExpression condition,
721 : PreParserStatement then_statement,
722 : PreParserStatement else_statement,
723 : int pos) {
724 : // This must return a jump statement iff both clauses are jump statements.
725 1386637 : return else_statement.IsJumpStatement() ? then_statement : else_statement;
726 : }
727 :
728 : PreParserStatement NewBreakStatement(PreParserStatement target, int pos) {
729 : return PreParserStatement::Jump();
730 : }
731 :
732 : PreParserStatement NewContinueStatement(PreParserStatement target, int pos) {
733 : return PreParserStatement::Jump();
734 : }
735 :
736 : PreParserStatement NewWithStatement(Scope* scope,
737 : PreParserExpression expression,
738 : PreParserStatement statement, int pos) {
739 : return PreParserStatement::Default();
740 : }
741 :
742 : PreParserStatement NewDoWhileStatement(ZoneList<const AstRawString*>* labels,
743 : int pos) {
744 : return PreParserStatement::Default();
745 : }
746 :
747 : PreParserStatement NewWhileStatement(ZoneList<const AstRawString*>* labels,
748 : int pos) {
749 : return PreParserStatement::Default();
750 : }
751 :
752 : PreParserStatement NewSwitchStatement(ZoneList<const AstRawString*>* labels,
753 : int pos) {
754 : return PreParserStatement::Default();
755 : }
756 :
757 : PreParserStatement NewCaseClause(PreParserExpression label,
758 : PreParserStatementList statements, int pos) {
759 : return PreParserStatement::Default();
760 : }
761 :
762 : PreParserStatement NewForStatement(ZoneList<const AstRawString*>* labels,
763 : int pos) {
764 : return PreParserStatement::Default();
765 : }
766 :
767 : PreParserStatement NewForEachStatement(ForEachStatement::VisitMode visit_mode,
768 : ZoneList<const AstRawString*>* labels,
769 : int pos) {
770 : return PreParserStatement::Default();
771 : }
772 :
773 : PreParserStatement NewForOfStatement(ZoneList<const AstRawString*>* labels,
774 : int pos) {
775 : return PreParserStatement::Default();
776 : }
777 :
778 : PreParserExpression NewCallRuntime(Runtime::FunctionId id,
779 : ZoneList<PreParserExpression>* arguments,
780 : int pos) {
781 : return PreParserExpression::Default();
782 : }
783 :
784 : PreParserExpression NewImportCallExpression(PreParserExpression args,
785 : int pos) {
786 : return PreParserExpression::Default();
787 : }
788 :
789 : // Return the object itself as AstVisitor and implement the needed
790 : // dummy method right in this class.
791 : PreParserFactory* visitor() { return this; }
792 : int* ast_properties() {
793 : static int dummy = 42;
794 : return &dummy;
795 : }
796 :
797 : private:
798 : AstValueFactory* ast_value_factory_;
799 : Zone* zone_;
800 : };
801 :
802 :
803 : struct PreParserFormalParameters : FormalParametersBase {
804 : struct Parameter : public ZoneObject {
805 645097 : Parameter(ZoneList<VariableProxy*>* variables, bool is_rest)
806 645097 : : variables_(variables), is_rest(is_rest) {}
807 : Parameter** next() { return &next_parameter; }
808 : Parameter* const* next() const { return &next_parameter; }
809 :
810 : ZoneList<VariableProxy*>* variables_;
811 : Parameter* next_parameter = nullptr;
812 : bool is_rest : 1;
813 : };
814 : explicit PreParserFormalParameters(DeclarationScope* scope)
815 : : FormalParametersBase(scope) {}
816 :
817 : ThreadedList<Parameter> params;
818 : };
819 :
820 :
821 : class PreParser;
822 :
823 : class PreParserTarget {
824 : public:
825 : PreParserTarget(ParserBase<PreParser>* preparser,
826 : PreParserStatement statement) {}
827 : };
828 :
829 : class PreParserTargetScope {
830 : public:
831 : explicit PreParserTargetScope(ParserBase<PreParser>* preparser) {}
832 : };
833 :
834 : template <>
835 : struct ParserTypes<PreParser> {
836 : typedef ParserBase<PreParser> Base;
837 : typedef PreParser Impl;
838 :
839 : // PreParser doesn't need to store generator variables.
840 : typedef void Variable;
841 :
842 : // Return types for traversing functions.
843 : typedef PreParserIdentifier Identifier;
844 : typedef PreParserExpression Expression;
845 : typedef PreParserExpression FunctionLiteral;
846 : typedef PreParserExpression ObjectLiteralProperty;
847 : typedef PreParserExpression ClassLiteralProperty;
848 : typedef PreParserExpression Suspend;
849 : typedef PreParserExpressionList ExpressionList;
850 : typedef PreParserExpressionList ObjectPropertyList;
851 : typedef PreParserExpressionList ClassPropertyList;
852 : typedef PreParserFormalParameters FormalParameters;
853 : typedef PreParserStatement Statement;
854 : typedef PreParserStatementList StatementList;
855 : typedef PreParserStatement Block;
856 : typedef PreParserStatement BreakableStatement;
857 : typedef PreParserStatement IterationStatement;
858 :
859 : // For constructing objects returned by the traversing functions.
860 : typedef PreParserFactory Factory;
861 :
862 : typedef PreParserTarget Target;
863 : typedef PreParserTargetScope TargetScope;
864 : };
865 :
866 :
867 : // Preparsing checks a JavaScript program and emits preparse-data that helps
868 : // a later parsing to be faster.
869 : // See preparse-data-format.h for the data format.
870 :
871 : // The PreParser checks that the syntax follows the grammar for JavaScript,
872 : // and collects some information about the program along the way.
873 : // The grammar check is only performed in order to understand the program
874 : // sufficiently to deduce some information about it, that can be used
875 : // to speed up later parsing. Finding errors is not the goal of pre-parsing,
876 : // rather it is to speed up properly written and correct programs.
877 : // That means that contextual checks (like a label being declared where
878 : // it is used) are generally omitted.
879 : class PreParser : public ParserBase<PreParser> {
880 : friend class ParserBase<PreParser>;
881 : friend class v8::internal::ExpressionClassifier<ParserTypes<PreParser>>;
882 :
883 : public:
884 : typedef PreParserIdentifier Identifier;
885 : typedef PreParserExpression Expression;
886 : typedef PreParserStatement Statement;
887 :
888 : enum PreParseResult {
889 : kPreParseStackOverflow,
890 : kPreParseAbort,
891 : kPreParseSuccess
892 : };
893 :
894 405607 : PreParser(Zone* zone, Scanner* scanner, uintptr_t stack_limit,
895 : AstValueFactory* ast_value_factory,
896 : PendingCompilationErrorHandler* pending_error_handler,
897 : RuntimeCallStats* runtime_call_stats,
898 : PreParsedScopeData* preparsed_scope_data = nullptr,
899 : bool parsing_on_main_thread = true)
900 : : ParserBase<PreParser>(zone, scanner, stack_limit, nullptr,
901 : ast_value_factory, runtime_call_stats,
902 : preparsed_scope_data, parsing_on_main_thread),
903 : use_counts_(nullptr),
904 : preparse_data_(FLAG_use_parse_tasks ? new PreParseData() : nullptr),
905 : track_unresolved_variables_(false),
906 1216821 : pending_error_handler_(pending_error_handler) {}
907 :
908 : static bool IsPreParser() { return true; }
909 :
910 : PreParserLogger* logger() { return &log_; }
911 :
912 : // Pre-parse the program from the character stream; returns true on
913 : // success (even if parsing failed, the pre-parse data successfully
914 : // captured the syntax error), and false if a stack-overflow happened
915 : // during parsing.
916 : PreParseResult PreParseProgram(bool is_module = false,
917 : int* use_counts = nullptr);
918 :
919 : // Parses a single function literal, from the opening parentheses before
920 : // parameters to the closing brace after the body.
921 : // Returns a FunctionEntry describing the body of the function in enough
922 : // detail that it can be lazily compiled.
923 : // The scanner is expected to have matched the "function" or "function*"
924 : // keyword and parameters, and have consumed the initial '{'.
925 : // At return, unless an error occurred, the scanner is positioned before the
926 : // the final '}'.
927 : PreParseResult PreParseFunction(FunctionKind kind,
928 : DeclarationScope* function_scope,
929 : bool parsing_module,
930 : bool track_unresolved_variables,
931 : bool may_abort, int* use_counts);
932 :
933 : const PreParseData* preparse_data() const { return preparse_data_.get(); }
934 :
935 : private:
936 : // These types form an algebra over syntactic categories that is just
937 : // rich enough to let us recognize and propagate the constructs that
938 : // are either being counted in the preparser data, or is important
939 : // to throw the correct syntax error exceptions.
940 :
941 : // All ParseXXX functions take as the last argument an *ok parameter
942 : // which is set to false if parsing failed; it is unchanged otherwise.
943 : // By making the 'exception handling' explicit, we are forced to check
944 : // for failure at the call sites.
945 :
946 : // Indicates that we won't switch from the preparser to the preparser; we'll
947 : // just stay where we are.
948 : bool AllowsLazyParsingWithoutUnresolvedVariables() const { return false; }
949 : bool parse_lazily() const { return false; }
950 :
951 : V8_INLINE LazyParsingResult SkipFunction(FunctionKind kind,
952 : DeclarationScope* function_scope,
953 : int* num_parameters,
954 : bool is_inner_function,
955 : bool may_abort, bool* ok) {
956 : UNREACHABLE();
957 : return kLazyParsingComplete;
958 : }
959 : Expression ParseFunctionLiteral(
960 : Identifier name, Scanner::Location function_name_location,
961 : FunctionNameValidity function_name_validity, FunctionKind kind,
962 : int function_token_pos, FunctionLiteral::FunctionType function_type,
963 : LanguageMode language_mode, bool* ok);
964 : LazyParsingResult ParseStatementListAndLogFunction(
965 : PreParserFormalParameters* formals, bool maybe_abort, bool* ok);
966 :
967 : struct TemplateLiteralState {};
968 :
969 : V8_INLINE TemplateLiteralState OpenTemplateLiteral(int pos) {
970 : return TemplateLiteralState();
971 : }
972 : V8_INLINE void AddTemplateExpression(TemplateLiteralState* state,
973 : PreParserExpression expression) {}
974 : V8_INLINE void AddTemplateSpan(TemplateLiteralState* state, bool should_cook,
975 : bool tail) {}
976 : V8_INLINE PreParserExpression CloseTemplateLiteral(
977 : TemplateLiteralState* state, int start, PreParserExpression tag);
978 : V8_INLINE void CheckConflictingVarDeclarations(Scope* scope, bool* ok) {}
979 :
980 : V8_INLINE void SetLanguageMode(Scope* scope, LanguageMode mode) {
981 9545573 : scope->SetLanguageMode(mode);
982 : }
983 : V8_INLINE void SetAsmModule() {}
984 :
985 : V8_INLINE void MarkCollectedTailCallExpressions() {}
986 : V8_INLINE void MarkTailPosition(PreParserExpression expression) {}
987 :
988 : V8_INLINE PreParserExpression SpreadCall(PreParserExpression function,
989 : PreParserExpressionList args,
990 : int pos,
991 : Call::PossiblyEval possibly_eval);
992 : V8_INLINE PreParserExpression SpreadCallNew(PreParserExpression function,
993 : PreParserExpressionList args,
994 : int pos);
995 :
996 : V8_INLINE void RewriteDestructuringAssignments() {}
997 :
998 : V8_INLINE PreParserExpression RewriteExponentiation(PreParserExpression left,
999 : PreParserExpression right,
1000 : int pos) {
1001 : return left;
1002 : }
1003 : V8_INLINE PreParserExpression RewriteAssignExponentiation(
1004 : PreParserExpression left, PreParserExpression right, int pos) {
1005 : return left;
1006 : }
1007 :
1008 : V8_INLINE PreParserExpression
1009 : RewriteAwaitExpression(PreParserExpression value, int pos) {
1010 : return value;
1011 : }
1012 : V8_INLINE void PrepareAsyncFunctionBody(PreParserStatementList body,
1013 : FunctionKind kind, int pos) {}
1014 : V8_INLINE void RewriteAsyncFunctionBody(PreParserStatementList body,
1015 : PreParserStatement block,
1016 : PreParserExpression return_value,
1017 : bool* ok) {}
1018 : V8_INLINE PreParserExpression RewriteYieldStar(PreParserExpression generator,
1019 : PreParserExpression expression,
1020 : int pos) {
1021 3687 : return PreParserExpression::Default();
1022 : }
1023 49555962 : V8_INLINE void RewriteNonPattern(bool* ok) { ValidateExpression(ok); }
1024 :
1025 : void DeclareAndInitializeVariables(
1026 : PreParserStatement block,
1027 : const DeclarationDescriptor* declaration_descriptor,
1028 : const DeclarationParsingResult::Declaration* declaration,
1029 : ZoneList<const AstRawString*>* names, bool* ok);
1030 :
1031 : V8_INLINE ZoneList<const AstRawString*>* DeclareLabel(
1032 : ZoneList<const AstRawString*>* labels, PreParserExpression expr,
1033 : bool* ok) {
1034 : DCHECK(!expr.AsIdentifier().IsEnum());
1035 : DCHECK(!parsing_module_ || !expr.AsIdentifier().IsAwait());
1036 : DCHECK(IsIdentifier(expr));
1037 : return labels;
1038 : }
1039 :
1040 : // TODO(nikolaos): The preparser currently does not keep track of labels.
1041 : V8_INLINE bool ContainsLabel(ZoneList<const AstRawString*>* labels,
1042 : PreParserIdentifier label) {
1043 : return false;
1044 : }
1045 :
1046 : V8_INLINE PreParserExpression RewriteReturn(PreParserExpression return_value,
1047 : int pos) {
1048 : return return_value;
1049 : }
1050 : V8_INLINE PreParserStatement RewriteSwitchStatement(
1051 : PreParserExpression tag, PreParserStatement switch_statement,
1052 : PreParserStatementList cases, Scope* scope) {
1053 94305 : return PreParserStatement::Default();
1054 : }
1055 :
1056 : V8_INLINE void RewriteCatchPattern(CatchInfo* catch_info, bool* ok) {
1057 107652 : if (track_unresolved_variables_) {
1058 22139 : if (catch_info->name.string_ != nullptr) {
1059 : // Unlike in the parser, we need to declare the catch variable as LET
1060 : // variable, so that it won't get hoisted out of the scope.
1061 22115 : catch_info->scope->DeclareVariableName(catch_info->name.string_, LET);
1062 : }
1063 22139 : if (catch_info->pattern.variables_ != nullptr) {
1064 66 : for (auto variable : *catch_info->pattern.variables_) {
1065 42 : scope()->DeclareVariableName(variable->raw_name(), LET);
1066 : }
1067 : }
1068 : }
1069 : }
1070 :
1071 : V8_INLINE void ValidateCatchBlock(const CatchInfo& catch_info, bool* ok) {}
1072 : V8_INLINE PreParserStatement RewriteTryStatement(
1073 : PreParserStatement try_block, PreParserStatement catch_block,
1074 : PreParserStatement finally_block, const CatchInfo& catch_info, int pos) {
1075 114482 : return PreParserStatement::Default();
1076 : }
1077 :
1078 : V8_INLINE void ParseAndRewriteGeneratorFunctionBody(
1079 : int pos, FunctionKind kind, PreParserStatementList body, bool* ok) {
1080 68375 : ParseStatementList(body, Token::RBRACE, ok);
1081 : }
1082 : V8_INLINE void CreateFunctionNameAssignment(
1083 : PreParserIdentifier function_name, int pos,
1084 : FunctionLiteral::FunctionType function_type,
1085 : DeclarationScope* function_scope, PreParserStatementList result,
1086 : int index) {}
1087 :
1088 : V8_INLINE PreParserExpression RewriteDoExpression(PreParserStatement body,
1089 : int pos, bool* ok) {
1090 495 : return PreParserExpression::Default();
1091 : }
1092 :
1093 : // TODO(nikolaos): The preparser currently does not keep track of labels
1094 : // and targets.
1095 : V8_INLINE PreParserStatement LookupBreakTarget(PreParserIdentifier label,
1096 : bool* ok) {
1097 98017 : return PreParserStatement::Default();
1098 : }
1099 : V8_INLINE PreParserStatement LookupContinueTarget(PreParserIdentifier label,
1100 : bool* ok) {
1101 36764 : return PreParserStatement::Default();
1102 : }
1103 :
1104 : V8_INLINE PreParserStatement DeclareFunction(
1105 : PreParserIdentifier variable_name, PreParserExpression function,
1106 : VariableMode mode, int pos, bool is_sloppy_block_function,
1107 : ZoneList<const AstRawString*>* names, bool* ok) {
1108 : DCHECK_NULL(names);
1109 175014 : if (variable_name.string_ != nullptr) {
1110 : DCHECK(track_unresolved_variables_);
1111 20020 : scope()->DeclareVariableName(variable_name.string_, mode);
1112 18576 : if (is_sloppy_block_function) {
1113 : GetDeclarationScope()->DeclareSloppyBlockFunction(variable_name.string_,
1114 2888 : scope());
1115 : }
1116 : }
1117 175014 : return Statement::Default();
1118 : }
1119 :
1120 : V8_INLINE PreParserStatement
1121 : DeclareClass(PreParserIdentifier variable_name, PreParserExpression value,
1122 : ZoneList<const AstRawString*>* names, int class_token_pos,
1123 : int end_pos, bool* ok) {
1124 : // Preparser shouldn't be used in contexts where we need to track the names.
1125 : DCHECK_NULL(names);
1126 15702 : if (variable_name.string_ != nullptr) {
1127 : DCHECK(track_unresolved_variables_);
1128 70 : scope()->DeclareVariableName(variable_name.string_, LET);
1129 : }
1130 15702 : return PreParserStatement::Default();
1131 : }
1132 : V8_INLINE void DeclareClassVariable(PreParserIdentifier name,
1133 : ClassInfo* class_info,
1134 : int class_token_pos, bool* ok) {}
1135 : V8_INLINE void DeclareClassProperty(PreParserIdentifier class_name,
1136 : PreParserExpression property,
1137 : ClassLiteralProperty::Kind kind,
1138 : bool is_static, bool is_constructor,
1139 : ClassInfo* class_info, bool* ok) {
1140 : }
1141 : V8_INLINE PreParserExpression RewriteClassLiteral(PreParserIdentifier name,
1142 : ClassInfo* class_info,
1143 : int pos, bool* ok) {
1144 : bool has_default_constructor = !class_info->has_seen_constructor;
1145 : // Account for the default constructor.
1146 32337 : if (has_default_constructor) GetNextFunctionLiteralId();
1147 32337 : return PreParserExpression::Default();
1148 : }
1149 :
1150 : V8_INLINE PreParserStatement DeclareNative(PreParserIdentifier name, int pos,
1151 : bool* ok) {
1152 : return PreParserStatement::Default();
1153 : }
1154 :
1155 : V8_INLINE void QueueDestructuringAssignmentForRewriting(
1156 : PreParserExpression assignment) {}
1157 : V8_INLINE void QueueNonPatternForRewriting(PreParserExpression expr,
1158 : bool* ok) {}
1159 :
1160 : // Helper functions for recursive descent.
1161 : V8_INLINE bool IsEval(PreParserIdentifier identifier) const {
1162 1773439 : return identifier.IsEval();
1163 : }
1164 :
1165 : V8_INLINE bool IsArguments(PreParserIdentifier identifier) const {
1166 : return identifier.IsArguments();
1167 : }
1168 :
1169 : V8_INLINE bool IsEvalOrArguments(PreParserIdentifier identifier) const {
1170 30317349 : return identifier.IsEvalOrArguments();
1171 : }
1172 :
1173 : V8_INLINE bool IsUndefined(PreParserIdentifier identifier) const {
1174 : return identifier.IsUndefined();
1175 : }
1176 :
1177 : V8_INLINE bool IsAwait(PreParserIdentifier identifier) const {
1178 : return identifier.IsAwait();
1179 : }
1180 :
1181 : // Returns true if the expression is of type "this.foo".
1182 : V8_INLINE static bool IsThisProperty(PreParserExpression expression) {
1183 4102747 : return expression.IsThisProperty();
1184 : }
1185 :
1186 : V8_INLINE static bool IsIdentifier(PreParserExpression expression) {
1187 65102108 : return expression.IsIdentifier();
1188 : }
1189 :
1190 : V8_INLINE static PreParserIdentifier AsIdentifier(
1191 : PreParserExpression expression) {
1192 7972711 : return expression.AsIdentifier();
1193 : }
1194 :
1195 : V8_INLINE static PreParserExpression AsIdentifierExpression(
1196 : PreParserExpression expression) {
1197 : return expression;
1198 : }
1199 :
1200 : V8_INLINE bool IsPrototype(PreParserIdentifier identifier) const {
1201 : return identifier.IsPrototype();
1202 : }
1203 :
1204 : V8_INLINE bool IsConstructor(PreParserIdentifier identifier) const {
1205 14424 : return identifier.IsConstructor();
1206 : }
1207 :
1208 : V8_INLINE bool IsName(PreParserIdentifier identifier) const {
1209 10887 : return identifier.IsName();
1210 : }
1211 :
1212 : V8_INLINE static bool IsBoilerplateProperty(PreParserExpression property) {
1213 : // PreParser doesn't count boilerplate properties.
1214 : return false;
1215 : }
1216 :
1217 : V8_INLINE bool IsNative(PreParserExpression expr) const {
1218 : // Preparsing is disabled for extensions (because the extension
1219 : // details aren't passed to lazily compiled functions), so we
1220 : // don't accept "native function" in the preparser and there is
1221 : // no need to keep track of "native".
1222 : return false;
1223 : }
1224 :
1225 : V8_INLINE static bool IsArrayIndex(PreParserIdentifier string,
1226 : uint32_t* index) {
1227 : return false;
1228 : }
1229 :
1230 : V8_INLINE bool IsUseStrictDirective(PreParserStatement statement) const {
1231 237115 : return statement.IsUseStrictLiteral();
1232 : }
1233 :
1234 : V8_INLINE bool IsUseAsmDirective(PreParserStatement statement) const {
1235 4455 : return statement.IsUseAsmLiteral();
1236 : }
1237 :
1238 : V8_INLINE bool IsStringLiteral(PreParserStatement statement) const {
1239 4455 : return statement.IsStringLiteral();
1240 : }
1241 :
1242 : V8_INLINE static PreParserExpression GetPropertyValue(
1243 : PreParserExpression property) {
1244 : return PreParserExpression::Default();
1245 : }
1246 :
1247 : V8_INLINE static void GetDefaultStrings(
1248 : PreParserIdentifier* default_string,
1249 : PreParserIdentifier* star_default_star_string) {}
1250 :
1251 : // Functions for encapsulating the differences between parsing and preparsing;
1252 : // operations interleaved with the recursive descent.
1253 : V8_INLINE static void PushLiteralName(PreParserIdentifier id) {}
1254 : V8_INLINE static void PushVariableName(PreParserIdentifier id) {}
1255 : V8_INLINE void PushPropertyName(PreParserExpression expression) {}
1256 : V8_INLINE void PushEnclosingName(PreParserIdentifier name) {}
1257 : V8_INLINE static void AddFunctionForNameInference(
1258 : PreParserExpression expression) {}
1259 : V8_INLINE static void InferFunctionName() {}
1260 :
1261 : V8_INLINE static void CheckAssigningFunctionLiteralToProperty(
1262 : PreParserExpression left, PreParserExpression right) {}
1263 :
1264 : V8_INLINE void MarkExpressionAsAssigned(PreParserExpression expression) {
1265 : // TODO(marja): To be able to produce the same errors, the preparser needs
1266 : // to start tracking which expressions are variables and which are assigned.
1267 4768166 : if (expression.variables_ != nullptr) {
1268 : DCHECK(FLAG_lazy_inner_functions);
1269 : DCHECK(track_unresolved_variables_);
1270 1373949 : for (auto variable : *expression.variables_) {
1271 687138 : variable->set_is_assigned();
1272 : }
1273 : }
1274 : }
1275 :
1276 : V8_INLINE bool ShortcutNumericLiteralBinaryExpression(PreParserExpression* x,
1277 : PreParserExpression y,
1278 : Token::Value op,
1279 : int pos) {
1280 : return false;
1281 : }
1282 :
1283 : V8_INLINE PreParserExpression BuildUnaryExpression(
1284 : PreParserExpression expression, Token::Value op, int pos) {
1285 849705 : return PreParserExpression::Default();
1286 : }
1287 :
1288 : V8_INLINE PreParserExpression BuildIteratorResult(PreParserExpression value,
1289 : bool done) {
1290 21918 : return PreParserExpression::Default();
1291 : }
1292 :
1293 : V8_INLINE PreParserStatement
1294 : BuildInitializationBlock(DeclarationParsingResult* parsing_result,
1295 : ZoneList<const AstRawString*>* names, bool* ok) {
1296 390417 : for (auto declaration : parsing_result->declarations) {
1297 : DeclareAndInitializeVariables(PreParserStatement::Default(),
1298 : &(parsing_result->descriptor), &declaration,
1299 199940 : names, ok);
1300 : }
1301 190477 : return PreParserStatement::Default();
1302 : }
1303 :
1304 : V8_INLINE PreParserStatement
1305 : InitializeForEachStatement(PreParserStatement stmt, PreParserExpression each,
1306 : PreParserExpression subject,
1307 : PreParserStatement body, int each_keyword_pos) {
1308 : MarkExpressionAsAssigned(each);
1309 : return stmt;
1310 : }
1311 :
1312 : V8_INLINE PreParserStatement InitializeForOfStatement(
1313 : PreParserStatement stmt, PreParserExpression each,
1314 : PreParserExpression iterable, PreParserStatement body, bool finalize,
1315 : IteratorType type, int next_result_pos = kNoSourcePosition) {
1316 : MarkExpressionAsAssigned(each);
1317 : return stmt;
1318 : }
1319 :
1320 : V8_INLINE PreParserStatement RewriteForVarInLegacy(const ForInfo& for_info) {
1321 30834 : return PreParserStatement::Null();
1322 : }
1323 :
1324 : V8_INLINE void DesugarBindingInForEachStatement(
1325 : ForInfo* for_info, PreParserStatement* body_block,
1326 : PreParserExpression* each_variable, bool* ok) {
1327 81329 : if (track_unresolved_variables_) {
1328 : DCHECK(for_info->parsing_result.declarations.length() == 1);
1329 : bool is_for_var_of =
1330 9409 : for_info->mode == ForEachStatement::ITERATE &&
1331 1813 : for_info->parsing_result.descriptor.mode == VariableMode::VAR;
1332 : bool collect_names =
1333 7596 : IsLexicalVariableMode(for_info->parsing_result.descriptor.mode) ||
1334 : is_for_var_of;
1335 :
1336 : DeclareAndInitializeVariables(
1337 : PreParserStatement::Default(), &for_info->parsing_result.descriptor,
1338 7596 : &for_info->parsing_result.declarations[0],
1339 15192 : collect_names ? &for_info->bound_names : nullptr, ok);
1340 : }
1341 : }
1342 :
1343 : V8_INLINE PreParserStatement CreateForEachStatementTDZ(
1344 : PreParserStatement init_block, const ForInfo& for_info, bool* ok) {
1345 81329 : if (track_unresolved_variables_) {
1346 7596 : if (IsLexicalVariableMode(for_info.parsing_result.descriptor.mode)) {
1347 4590 : for (auto name : for_info.bound_names) {
1348 2532 : scope()->DeclareVariableName(name, LET);
1349 : }
1350 2058 : return PreParserStatement::Default();
1351 : }
1352 : }
1353 : return init_block;
1354 : }
1355 :
1356 : V8_INLINE StatementT DesugarLexicalBindingsInForStatement(
1357 : PreParserStatement loop, PreParserStatement init,
1358 : PreParserExpression cond, PreParserStatement next,
1359 : PreParserStatement body, Scope* inner_scope, const ForInfo& for_info,
1360 : bool* ok) {
1361 : // See Parser::DesugarLexicalBindingsInForStatement.
1362 149 : if (track_unresolved_variables_) {
1363 298 : for (auto name : for_info.bound_names) {
1364 : inner_scope->DeclareVariableName(
1365 149 : name, for_info.parsing_result.descriptor.mode);
1366 : }
1367 : }
1368 : return loop;
1369 : }
1370 :
1371 : V8_INLINE PreParserStatement BuildParameterInitializationBlock(
1372 : const PreParserFormalParameters& parameters, bool* ok) {
1373 38974 : return PreParserStatement::Default();
1374 : }
1375 :
1376 : V8_INLINE PreParserStatement
1377 : BuildRejectPromiseOnException(PreParserStatement init_block) {
1378 0 : return PreParserStatement::Default();
1379 : }
1380 :
1381 : V8_INLINE void InsertSloppyBlockFunctionVarBindings(DeclarationScope* scope) {
1382 232576 : scope->HoistSloppyBlockFunctions(nullptr);
1383 : }
1384 :
1385 : V8_INLINE void InsertShadowingVarBindingInitializers(
1386 : PreParserStatement block) {}
1387 :
1388 : V8_INLINE PreParserExpression
1389 : NewThrowReferenceError(MessageTemplate::Template message, int pos) {
1390 1278 : return PreParserExpression::Default();
1391 : }
1392 :
1393 : V8_INLINE PreParserExpression NewThrowSyntaxError(
1394 : MessageTemplate::Template message, PreParserIdentifier arg, int pos) {
1395 : return PreParserExpression::Default();
1396 : }
1397 :
1398 : V8_INLINE PreParserExpression NewThrowTypeError(
1399 : MessageTemplate::Template message, PreParserIdentifier arg, int pos) {
1400 : return PreParserExpression::Default();
1401 : }
1402 :
1403 : // Reporting errors.
1404 : V8_INLINE void ReportMessageAt(Scanner::Location source_location,
1405 : MessageTemplate::Template message,
1406 : const char* arg = NULL,
1407 : ParseErrorType error_type = kSyntaxError) {
1408 : pending_error_handler_->ReportMessageAt(source_location.beg_pos,
1409 : source_location.end_pos, message,
1410 501290 : arg, error_type);
1411 : }
1412 :
1413 : V8_INLINE void ReportMessageAt(Scanner::Location source_location,
1414 : MessageTemplate::Template message,
1415 : PreParserIdentifier arg,
1416 : ParseErrorType error_type = kSyntaxError) {
1417 0 : UNREACHABLE();
1418 : }
1419 :
1420 : // "null" return type creators.
1421 : V8_INLINE static PreParserIdentifier EmptyIdentifier() {
1422 2211516 : return PreParserIdentifier::Empty();
1423 : }
1424 : V8_INLINE static bool IsEmptyIdentifier(PreParserIdentifier name) {
1425 0 : return name.IsEmpty();
1426 : }
1427 : V8_INLINE static PreParserExpression EmptyExpression() {
1428 32039766 : return PreParserExpression::Empty();
1429 : }
1430 : V8_INLINE static PreParserExpression EmptyLiteral() {
1431 : return PreParserExpression::Default();
1432 : }
1433 : V8_INLINE static PreParserExpression EmptyObjectLiteralProperty() {
1434 46162 : return PreParserExpression::Default();
1435 : }
1436 : V8_INLINE static PreParserExpression EmptyClassLiteralProperty() {
1437 11256 : return PreParserExpression::Default();
1438 : }
1439 : V8_INLINE static PreParserExpression EmptyFunctionLiteral() {
1440 45046 : return PreParserExpression::Default();
1441 : }
1442 :
1443 : V8_INLINE static bool IsEmptyExpression(PreParserExpression expr) {
1444 15113140 : return expr.IsEmpty();
1445 : }
1446 :
1447 : V8_INLINE static PreParserExpressionList NullExpressionList() {
1448 4120 : return PreParserExpressionList::Null();
1449 : }
1450 :
1451 : V8_INLINE static bool IsNullExpressionList(PreParserExpressionList exprs) {
1452 : return exprs.IsNull();
1453 : }
1454 :
1455 : V8_INLINE static PreParserStatementList NullStatementList() {
1456 80470 : return PreParserStatementList::Null();
1457 : }
1458 :
1459 : V8_INLINE static bool IsNullStatementList(PreParserStatementList stmts) {
1460 : return stmts.IsNull();
1461 : }
1462 :
1463 : V8_INLINE static PreParserStatement NullStatement() {
1464 2471690 : return PreParserStatement::Null();
1465 : }
1466 :
1467 : V8_INLINE bool IsNullStatement(PreParserStatement stmt) {
1468 12831269 : return stmt.IsNullStatement();
1469 : }
1470 :
1471 : V8_INLINE bool IsEmptyStatement(PreParserStatement stmt) {
1472 12499894 : return stmt.IsEmptyStatement();
1473 : }
1474 :
1475 : V8_INLINE static PreParserStatement NullBlock() {
1476 5508126 : return PreParserStatement::Null();
1477 : }
1478 :
1479 : V8_INLINE PreParserIdentifier EmptyIdentifierString() const {
1480 81250 : return PreParserIdentifier::Default();
1481 : }
1482 :
1483 : // Odd-ball literal creators.
1484 : V8_INLINE PreParserExpression GetLiteralTheHole(int position) {
1485 1433435 : return PreParserExpression::Default();
1486 : }
1487 :
1488 : V8_INLINE PreParserExpression GetLiteralUndefined(int position) {
1489 183770 : return PreParserExpression::Default();
1490 : }
1491 :
1492 : // Producing data during the recursive descent.
1493 : PreParserIdentifier GetSymbol() const;
1494 :
1495 : V8_INLINE PreParserIdentifier GetNextSymbol() const {
1496 30906 : return PreParserIdentifier::Default();
1497 : }
1498 :
1499 : V8_INLINE PreParserIdentifier GetNumberAsSymbol() const {
1500 812734 : return PreParserIdentifier::Default();
1501 : }
1502 :
1503 : V8_INLINE PreParserExpression ThisExpression(int pos = kNoSourcePosition) {
1504 : ZoneList<VariableProxy*>* variables = nullptr;
1505 175561 : if (track_unresolved_variables_) {
1506 12794 : AstNodeFactory factory(ast_value_factory());
1507 : // Setting the Zone is necessary because zone_ might be the temp Zone, and
1508 : // AstValueFactory doesn't know about it.
1509 12794 : factory.set_zone(zone());
1510 : VariableProxy* proxy = scope()->NewUnresolved(
1511 12794 : &factory, ast_value_factory()->this_string(), pos, THIS_VARIABLE);
1512 :
1513 12794 : variables = new (zone()) ZoneList<VariableProxy*>(1, zone());
1514 12794 : variables->Add(proxy, zone());
1515 : }
1516 175561 : return PreParserExpression::This(variables);
1517 : }
1518 :
1519 : V8_INLINE PreParserExpression NewSuperPropertyReference(int pos) {
1520 1733 : return PreParserExpression::Default();
1521 : }
1522 :
1523 : V8_INLINE PreParserExpression NewSuperCallReference(int pos) {
1524 146 : return PreParserExpression::SuperCallReference();
1525 : }
1526 :
1527 : V8_INLINE PreParserExpression NewTargetExpression(int pos) {
1528 4268 : return PreParserExpression::NewTargetExpression();
1529 : }
1530 :
1531 : V8_INLINE PreParserExpression FunctionSentExpression(int pos) {
1532 58 : return PreParserExpression::Default();
1533 : }
1534 :
1535 : V8_INLINE PreParserExpression ExpressionFromLiteral(Token::Value token,
1536 : int pos) {
1537 9492855 : return PreParserExpression::Default();
1538 : }
1539 :
1540 : PreParserExpression ExpressionFromIdentifier(
1541 : PreParserIdentifier name, int start_position,
1542 : InferName infer = InferName::kYes);
1543 :
1544 : V8_INLINE PreParserExpression ExpressionFromString(int pos) {
1545 3323053 : if (scanner()->IsUseStrict()) {
1546 233029 : return PreParserExpression::UseStrictStringLiteral();
1547 : }
1548 3090024 : return PreParserExpression::StringLiteral();
1549 : }
1550 :
1551 : V8_INLINE PreParserExpressionList NewExpressionList(int size) const {
1552 3980431 : return PreParserExpressionList();
1553 : }
1554 :
1555 : V8_INLINE PreParserExpressionList NewObjectPropertyList(int size) const {
1556 707192 : return PreParserExpressionList();
1557 : }
1558 :
1559 : V8_INLINE PreParserExpressionList NewClassPropertyList(int size) const {
1560 44854 : return PreParserExpressionList();
1561 : }
1562 :
1563 : V8_INLINE PreParserStatementList NewStatementList(int size) const {
1564 1006419 : return PreParserStatementList();
1565 : }
1566 :
1567 : PreParserStatementList NewCaseClauseList(int size) {
1568 : return PreParserStatementList();
1569 : }
1570 :
1571 : V8_INLINE PreParserExpression
1572 : NewV8Intrinsic(PreParserIdentifier name, PreParserExpressionList arguments,
1573 : int pos, bool* ok) {
1574 17846 : return PreParserExpression::Default();
1575 : }
1576 :
1577 : V8_INLINE PreParserStatement NewThrowStatement(PreParserExpression exception,
1578 : int pos) {
1579 65586 : return PreParserStatement::Jump();
1580 : }
1581 :
1582 : V8_INLINE void AddParameterInitializationBlock(
1583 : const PreParserFormalParameters& parameters, PreParserStatementList body,
1584 : bool is_async, bool* ok) {}
1585 :
1586 : V8_INLINE void AddFormalParameter(PreParserFormalParameters* parameters,
1587 : PreParserExpression pattern,
1588 : PreParserExpression initializer,
1589 : int initializer_end_position,
1590 : bool is_rest) {
1591 1584926 : if (track_unresolved_variables_) {
1592 : DCHECK(FLAG_lazy_inner_functions);
1593 : parameters->params.Add(new (zone()) PreParserFormalParameters::Parameter(
1594 645097 : pattern.variables_, is_rest));
1595 : }
1596 1584926 : parameters->UpdateArityAndFunctionLength(!initializer.IsEmpty(), is_rest);
1597 : }
1598 :
1599 : V8_INLINE void DeclareFormalParameters(
1600 : DeclarationScope* scope,
1601 : const ThreadedList<PreParserFormalParameters::Parameter>& parameters) {
1602 2402874 : bool is_simple = classifier()->is_simple_parameter_list();
1603 1751746 : if (!is_simple) scope->SetHasNonSimpleParameters();
1604 1751746 : if (track_unresolved_variables_) {
1605 : DCHECK(FLAG_lazy_inner_functions);
1606 1199193 : for (auto parameter : parameters) {
1607 644413 : if (parameter->variables_ != nullptr) {
1608 1295401 : for (auto variable : (*parameter->variables_)) {
1609 : scope->DeclareParameterName(
1610 651128 : variable->raw_name(), parameter->is_rest, ast_value_factory());
1611 : }
1612 : }
1613 : }
1614 : }
1615 : }
1616 :
1617 : V8_INLINE void DeclareArrowFunctionFormalParameters(
1618 : PreParserFormalParameters* parameters, PreParserExpression params,
1619 : const Scanner::Location& params_loc, Scanner::Location* duplicate_loc,
1620 : bool* ok) {
1621 : // TODO(wingo): Detect duplicated identifiers in paramlists. Detect
1622 : // parameter lists that are too long.
1623 83542 : if (track_unresolved_variables_) {
1624 : DCHECK(FLAG_lazy_inner_functions);
1625 12252 : if (params.variables_ != nullptr) {
1626 16391 : for (auto variable : *params.variables_) {
1627 10817 : parameters->scope->DeclareVariableName(variable->raw_name(), VAR);
1628 : }
1629 : }
1630 : }
1631 : }
1632 :
1633 : V8_INLINE PreParserExpression NoTemplateTag() {
1634 32357 : return PreParserExpression::NoTemplateTag();
1635 : }
1636 :
1637 : V8_INLINE static bool IsTaggedTemplate(const PreParserExpression tag) {
1638 : return !tag.IsNoTemplateTag();
1639 : }
1640 :
1641 : V8_INLINE PreParserExpression
1642 : ExpressionListToExpression(PreParserExpressionList args) {
1643 200 : return PreParserExpression::Default(args.variables_);
1644 : }
1645 :
1646 : V8_INLINE void AddAccessorPrefixToFunctionName(bool is_get,
1647 : PreParserExpression function,
1648 : PreParserIdentifier name) {}
1649 : V8_INLINE void SetFunctionNameFromPropertyName(PreParserExpression property,
1650 : PreParserIdentifier name) {}
1651 : V8_INLINE void SetFunctionNameFromIdentifierRef(
1652 : PreParserExpression value, PreParserExpression identifier) {}
1653 :
1654 : V8_INLINE ZoneList<typename ExpressionClassifier::Error>*
1655 : GetReportedErrorList() const {
1656 73521557 : return function_state_->GetReportedErrorList();
1657 : }
1658 :
1659 : V8_INLINE ZoneList<PreParserExpression>* GetNonPatternList() const {
1660 73521558 : return function_state_->non_patterns_to_rewrite();
1661 : }
1662 :
1663 : V8_INLINE void CountUsage(v8::Isolate::UseCounterFeature feature) {
1664 1278 : if (use_counts_ != nullptr) ++use_counts_[feature];
1665 : }
1666 :
1667 : V8_INLINE bool ParsingDynamicFunctionDeclaration() const { return false; }
1668 :
1669 : // Preparser's private field members.
1670 :
1671 : int* use_counts_;
1672 : std::unique_ptr<PreParseData> preparse_data_;
1673 : bool track_unresolved_variables_;
1674 : PreParserLogger log_;
1675 : PendingCompilationErrorHandler* pending_error_handler_;
1676 : };
1677 :
1678 : PreParserExpression PreParser::SpreadCall(PreParserExpression function,
1679 : PreParserExpressionList args, int pos,
1680 : Call::PossiblyEval possibly_eval) {
1681 1272 : return factory()->NewCall(function, args, pos, possibly_eval);
1682 : }
1683 :
1684 : PreParserExpression PreParser::SpreadCallNew(PreParserExpression function,
1685 : PreParserExpressionList args,
1686 : int pos) {
1687 21 : return factory()->NewCallNew(function, args, pos);
1688 : }
1689 :
1690 : PreParserExpression PreParser::CloseTemplateLiteral(TemplateLiteralState* state,
1691 : int start,
1692 : PreParserExpression tag) {
1693 : return EmptyExpression();
1694 : }
1695 :
1696 : } // namespace internal
1697 : } // namespace v8
1698 :
1699 : #endif // V8_PARSING_PREPARSER_H
|