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