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_AST_AST_H_
6 : #define V8_AST_AST_H_
7 :
8 : #include <memory>
9 :
10 : #include "src/ast/ast-value-factory.h"
11 : #include "src/ast/modules.h"
12 : #include "src/ast/variables.h"
13 : #include "src/bailout-reason.h"
14 : #include "src/globals.h"
15 : #include "src/heap/factory.h"
16 : #include "src/isolate.h"
17 : #include "src/label.h"
18 : #include "src/objects/literal-objects.h"
19 : #include "src/objects/smi.h"
20 : #include "src/parsing/token.h"
21 : #include "src/runtime/runtime.h"
22 :
23 : namespace v8 {
24 : namespace internal {
25 :
26 : // The abstract syntax tree is an intermediate, light-weight
27 : // representation of the parsed JavaScript code suitable for
28 : // compilation to native code.
29 :
30 : // Nodes are allocated in a separate zone, which allows faster
31 : // allocation and constant-time deallocation of the entire syntax
32 : // tree.
33 :
34 :
35 : // ----------------------------------------------------------------------------
36 : // Nodes of the abstract syntax tree. Only concrete classes are
37 : // enumerated here.
38 :
39 : #define DECLARATION_NODE_LIST(V) \
40 : V(VariableDeclaration) \
41 : V(FunctionDeclaration)
42 :
43 : #define ITERATION_NODE_LIST(V) \
44 : V(DoWhileStatement) \
45 : V(WhileStatement) \
46 : V(ForStatement) \
47 : V(ForInStatement) \
48 : V(ForOfStatement)
49 :
50 : #define BREAKABLE_NODE_LIST(V) \
51 : V(Block) \
52 : V(SwitchStatement)
53 :
54 : #define STATEMENT_NODE_LIST(V) \
55 : ITERATION_NODE_LIST(V) \
56 : BREAKABLE_NODE_LIST(V) \
57 : V(ExpressionStatement) \
58 : V(EmptyStatement) \
59 : V(SloppyBlockFunctionStatement) \
60 : V(IfStatement) \
61 : V(ContinueStatement) \
62 : V(BreakStatement) \
63 : V(ReturnStatement) \
64 : V(WithStatement) \
65 : V(TryCatchStatement) \
66 : V(TryFinallyStatement) \
67 : V(DebuggerStatement) \
68 : V(InitializeClassMembersStatement)
69 :
70 : #define LITERAL_NODE_LIST(V) \
71 : V(RegExpLiteral) \
72 : V(ObjectLiteral) \
73 : V(ArrayLiteral)
74 :
75 : #define EXPRESSION_NODE_LIST(V) \
76 : LITERAL_NODE_LIST(V) \
77 : V(Assignment) \
78 : V(Await) \
79 : V(BinaryOperation) \
80 : V(NaryOperation) \
81 : V(Call) \
82 : V(CallNew) \
83 : V(CallRuntime) \
84 : V(ClassLiteral) \
85 : V(CompareOperation) \
86 : V(CompoundAssignment) \
87 : V(Conditional) \
88 : V(CountOperation) \
89 : V(DoExpression) \
90 : V(EmptyParentheses) \
91 : V(FunctionLiteral) \
92 : V(GetTemplateObject) \
93 : V(ImportCallExpression) \
94 : V(Literal) \
95 : V(NativeFunctionLiteral) \
96 : V(Property) \
97 : V(ResolvedProperty) \
98 : V(Spread) \
99 : V(StoreInArrayLiteral) \
100 : V(SuperCallReference) \
101 : V(SuperPropertyReference) \
102 : V(TemplateLiteral) \
103 : V(ThisFunction) \
104 : V(Throw) \
105 : V(UnaryOperation) \
106 : V(VariableProxy) \
107 : V(Yield) \
108 : V(YieldStar)
109 :
110 : #define FAILURE_NODE_LIST(V) V(FailureExpression)
111 :
112 : #define AST_NODE_LIST(V) \
113 : DECLARATION_NODE_LIST(V) \
114 : STATEMENT_NODE_LIST(V) \
115 : EXPRESSION_NODE_LIST(V)
116 :
117 : // Forward declarations
118 : class AstNode;
119 : class AstNodeFactory;
120 : class Declaration;
121 : class BreakableStatement;
122 : class Expression;
123 : class IterationStatement;
124 : class MaterializedLiteral;
125 : class NestedVariableDeclaration;
126 : class ProducedPreparseData;
127 : class Statement;
128 :
129 : #define DEF_FORWARD_DECLARATION(type) class type;
130 : AST_NODE_LIST(DEF_FORWARD_DECLARATION)
131 : FAILURE_NODE_LIST(DEF_FORWARD_DECLARATION)
132 : #undef DEF_FORWARD_DECLARATION
133 :
134 : class AstNode: public ZoneObject {
135 : public:
136 : #define DECLARE_TYPE_ENUM(type) k##type,
137 : enum NodeType : uint8_t {
138 : AST_NODE_LIST(DECLARE_TYPE_ENUM) /* , */
139 : FAILURE_NODE_LIST(DECLARE_TYPE_ENUM)
140 : };
141 : #undef DECLARE_TYPE_ENUM
142 :
143 218956879 : void* operator new(size_t size, Zone* zone) { return zone->New(size); }
144 :
145 701376386 : NodeType node_type() const { return NodeTypeField::decode(bit_field_); }
146 81 : int position() const { return position_; }
147 :
148 : #ifdef DEBUG
149 : void Print();
150 : void Print(Isolate* isolate);
151 : #endif // DEBUG
152 :
153 : // Type testing & conversion functions overridden by concrete subclasses.
154 : #define DECLARE_NODE_FUNCTIONS(type) \
155 : V8_INLINE bool Is##type() const; \
156 : V8_INLINE type* As##type(); \
157 : V8_INLINE const type* As##type() const;
158 : AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
159 : FAILURE_NODE_LIST(DECLARE_NODE_FUNCTIONS)
160 : #undef DECLARE_NODE_FUNCTIONS
161 :
162 : IterationStatement* AsIterationStatement();
163 : MaterializedLiteral* AsMaterializedLiteral();
164 :
165 : private:
166 : // Hidden to prevent accidental usage. It would have to load the
167 : // current zone from the TLS.
168 : void* operator new(size_t size);
169 :
170 : int position_;
171 : class NodeTypeField : public BitField<NodeType, 0, 6> {};
172 :
173 : protected:
174 : uint32_t bit_field_;
175 : static const uint8_t kNextBitFieldIndex = NodeTypeField::kNext;
176 :
177 : AstNode(int position, NodeType type)
178 233305299 : : position_(position), bit_field_(NodeTypeField::encode(type)) {}
179 : };
180 :
181 :
182 : class Statement : public AstNode {
183 : public:
184 : bool IsJump() const;
185 :
186 : protected:
187 : Statement(int position, NodeType type) : AstNode(position, type) {}
188 :
189 : static const uint8_t kNextBitFieldIndex = AstNode::kNextBitFieldIndex;
190 : };
191 :
192 :
193 : class Expression : public AstNode {
194 : public:
195 : enum Context {
196 : // Not assigned a context yet, or else will not be visited during
197 : // code generation.
198 : kUninitialized,
199 : // Evaluated for its side effects.
200 : kEffect,
201 : // Evaluated for its value (and side effects).
202 : kValue,
203 : // Evaluated for control flow (and side effects).
204 : kTest
205 : };
206 :
207 : // True iff the expression is a valid reference expression.
208 : bool IsValidReferenceExpression() const;
209 :
210 : // Helpers for ToBoolean conversion.
211 : bool ToBooleanIsTrue() const;
212 : bool ToBooleanIsFalse() const;
213 :
214 : // Symbols that cannot be parsed as array indices are considered property
215 : // names. We do not treat symbols that can be array indexes as property
216 : // names because [] for string objects is handled only by keyed ICs.
217 : bool IsPropertyName() const;
218 :
219 : // True iff the expression is a class or function expression without
220 : // a syntactic name.
221 : bool IsAnonymousFunctionDefinition() const;
222 :
223 : // True iff the expression is a concise method definition.
224 : bool IsConciseMethodDefinition() const;
225 :
226 : // True iff the expression is an accessor function definition.
227 : bool IsAccessorFunctionDefinition() const;
228 :
229 : // True iff the expression is a literal represented as a smi.
230 : bool IsSmiLiteral() const;
231 :
232 : // True iff the expression is a literal represented as a number.
233 : bool IsNumberLiteral() const;
234 :
235 : // True iff the expression is a string literal.
236 : bool IsStringLiteral() const;
237 :
238 : // True iff the expression is the null literal.
239 : bool IsNullLiteral() const;
240 :
241 : // True iff the expression is the hole literal.
242 : bool IsTheHoleLiteral() const;
243 :
244 : // True if we can prove that the expression is the undefined literal. Note
245 : // that this also checks for loads of the global "undefined" variable.
246 : bool IsUndefinedLiteral() const;
247 :
248 : bool IsCompileTimeValue();
249 :
250 : bool IsPattern() {
251 : STATIC_ASSERT(kObjectLiteral + 1 == kArrayLiteral);
252 95200 : return IsInRange(node_type(), kObjectLiteral, kArrayLiteral);
253 : }
254 :
255 : bool is_parenthesized() const {
256 : return IsParenthesizedField::decode(bit_field_);
257 : }
258 :
259 : void mark_parenthesized() {
260 2593018 : bit_field_ = IsParenthesizedField::update(bit_field_, true);
261 : }
262 :
263 : void clear_parenthesized() {
264 635518 : bit_field_ = IsParenthesizedField::update(bit_field_, false);
265 : }
266 :
267 : private:
268 : class IsParenthesizedField
269 : : public BitField<bool, AstNode::kNextBitFieldIndex, 1> {};
270 :
271 : protected:
272 : Expression(int pos, NodeType type) : AstNode(pos, type) {
273 : DCHECK(!is_parenthesized());
274 : }
275 :
276 : static const uint8_t kNextBitFieldIndex = IsParenthesizedField::kNext;
277 : };
278 :
279 : class FailureExpression : public Expression {
280 : private:
281 : friend class AstNodeFactory;
282 : FailureExpression() : Expression(kNoSourcePosition, kFailureExpression) {}
283 : };
284 :
285 : // V8's notion of BreakableStatement does not correspond to the notion of
286 : // BreakableStatement in ECMAScript. In V8, the idea is that a
287 : // BreakableStatement is a statement that can be the target of a break
288 : // statement. The BreakableStatement AST node carries a list of labels, any of
289 : // which can be used as an argument to the break statement in order to target
290 : // it.
291 : //
292 : // Since we don't want to attach a list of labels to all kinds of statements, we
293 : // only declare switchs, loops, and blocks as BreakableStatements. This means
294 : // that we implement breaks targeting other statement forms as breaks targeting
295 : // a substatement thereof. For instance, in "foo: if (b) { f(); break foo; }" we
296 : // pretend that foo is the label of the inner block. That's okay because one
297 : // can't observe the difference.
298 : //
299 : // This optimization makes it harder to detect invalid continue labels, see the
300 : // need for own_labels in IterationStatement.
301 : //
302 : class BreakableStatement : public Statement {
303 : public:
304 : enum BreakableType {
305 : TARGET_FOR_ANONYMOUS,
306 : TARGET_FOR_NAMED_ONLY
307 : };
308 :
309 : // A list of all labels declared on the path up to the previous
310 : // BreakableStatement (if any).
311 : //
312 : // Example: "l1: for (;;) l2: l3: { l4: if (b) l5: { s } }"
313 : // labels() of the ForStatement will be l1.
314 : // labels() of the Block { l4: ... } will be l2, l3.
315 : // labels() of the Block { s } will be l4, l5.
316 : ZonePtrList<const AstRawString>* labels() const;
317 :
318 : // Testers.
319 : bool is_target_for_anonymous() const {
320 : return BreakableTypeField::decode(bit_field_) == TARGET_FOR_ANONYMOUS;
321 : }
322 :
323 : private:
324 : class BreakableTypeField
325 : : public BitField<BreakableType, Statement::kNextBitFieldIndex, 1> {};
326 :
327 : protected:
328 : BreakableStatement(BreakableType breakable_type, int position, NodeType type)
329 : : Statement(position, type) {
330 10851411 : bit_field_ |= BreakableTypeField::encode(breakable_type);
331 : }
332 :
333 : static const uint8_t kNextBitFieldIndex = BreakableTypeField::kNext;
334 : };
335 :
336 : class Block : public BreakableStatement {
337 : public:
338 : ZonePtrList<Statement>* statements() { return &statements_; }
339 : bool ignore_completion_value() const {
340 : return IgnoreCompletionField::decode(bit_field_);
341 : }
342 :
343 : inline ZonePtrList<const AstRawString>* labels() const;
344 :
345 3386 : bool IsJump() const {
346 7049 : return !statements_.is_empty() && statements_.last()->IsJump() &&
347 3386 : labels() == nullptr; // Good enough as an approximation...
348 : }
349 :
350 89328 : Scope* scope() const { return scope_; }
351 1302228 : void set_scope(Scope* scope) { scope_ = scope; }
352 :
353 : void InitializeStatements(const ScopedPtrList<Statement>& statements,
354 : Zone* zone) {
355 : DCHECK_EQ(0, statements_.length());
356 10229818 : statements.CopyTo(&statements_, zone);
357 : }
358 :
359 : private:
360 : friend class AstNodeFactory;
361 :
362 : ZonePtrList<Statement> statements_;
363 : Scope* scope_;
364 :
365 : class IgnoreCompletionField
366 : : public BitField<bool, BreakableStatement::kNextBitFieldIndex, 1> {};
367 : class IsLabeledField
368 : : public BitField<bool, IgnoreCompletionField::kNext, 1> {};
369 :
370 : protected:
371 10851411 : Block(Zone* zone, ZonePtrList<const AstRawString>* labels, int capacity,
372 : bool ignore_completion_value)
373 : : BreakableStatement(TARGET_FOR_NAMED_ONLY, kNoSourcePosition, kBlock),
374 : statements_(capacity, zone),
375 10851411 : scope_(nullptr) {
376 : bit_field_ |= IgnoreCompletionField::encode(ignore_completion_value) |
377 21702758 : IsLabeledField::encode(labels != nullptr);
378 10851379 : }
379 :
380 : Block(ZonePtrList<const AstRawString>* labels, bool ignore_completion_value)
381 10249680 : : Block(nullptr, labels, 0, ignore_completion_value) {}
382 : };
383 :
384 : class LabeledBlock final : public Block {
385 : private:
386 : friend class AstNodeFactory;
387 : friend class Block;
388 :
389 : LabeledBlock(Zone* zone, ZonePtrList<const AstRawString>* labels,
390 : int capacity, bool ignore_completion_value)
391 : : Block(zone, labels, capacity, ignore_completion_value),
392 6690 : labels_(labels) {
393 : DCHECK_NOT_NULL(labels);
394 : DCHECK_GT(labels->length(), 0);
395 : }
396 :
397 : LabeledBlock(ZonePtrList<const AstRawString>* labels,
398 : bool ignore_completion_value)
399 : : LabeledBlock(nullptr, labels, 0, ignore_completion_value) {}
400 :
401 : ZonePtrList<const AstRawString>* labels_;
402 : };
403 :
404 : inline ZonePtrList<const AstRawString>* Block::labels() const {
405 153112 : if (IsLabeledField::decode(bit_field_)) {
406 7036 : return static_cast<const LabeledBlock*>(this)->labels_;
407 : }
408 : return nullptr;
409 : }
410 :
411 : class DoExpression final : public Expression {
412 : public:
413 : Block* block() { return block_; }
414 : VariableProxy* result() { return result_; }
415 :
416 : private:
417 : friend class AstNodeFactory;
418 :
419 : DoExpression(Block* block, VariableProxy* result, int pos)
420 : : Expression(pos, kDoExpression), block_(block), result_(result) {
421 : DCHECK_NOT_NULL(block_);
422 : DCHECK_NOT_NULL(result_);
423 : }
424 :
425 : Block* block_;
426 : VariableProxy* result_;
427 : };
428 :
429 :
430 : class Declaration : public AstNode {
431 : public:
432 : typedef base::ThreadedList<Declaration> List;
433 :
434 : Variable* var() const { return var_; }
435 10645186 : void set_var(Variable* var) { var_ = var; }
436 :
437 : protected:
438 10645259 : Declaration(int pos, NodeType type) : AstNode(pos, type), next_(nullptr) {}
439 :
440 : private:
441 : Variable* var_;
442 : // Declarations list threaded through the declarations.
443 : Declaration** next() { return &next_; }
444 : Declaration* next_;
445 : friend List;
446 : friend base::ThreadedListTraits<Declaration>;
447 : };
448 :
449 : class VariableDeclaration : public Declaration {
450 : public:
451 : inline NestedVariableDeclaration* AsNested();
452 :
453 : private:
454 : friend class AstNodeFactory;
455 :
456 : class IsNestedField
457 : : public BitField<bool, Declaration::kNextBitFieldIndex, 1> {};
458 :
459 : protected:
460 : explicit VariableDeclaration(int pos, bool is_nested = false)
461 : : Declaration(pos, kVariableDeclaration) {
462 410961 : bit_field_ = IsNestedField::update(bit_field_, is_nested);
463 : }
464 :
465 : static const uint8_t kNextBitFieldIndex = IsNestedField::kNext;
466 : };
467 :
468 : // For var declarations that appear in a block scope.
469 : // Only distinguished from VariableDeclaration during Scope analysis,
470 : // so it doesn't get its own NodeType.
471 : class NestedVariableDeclaration final : public VariableDeclaration {
472 : public:
473 : Scope* scope() const { return scope_; }
474 :
475 : private:
476 : friend class AstNodeFactory;
477 :
478 : NestedVariableDeclaration(Scope* scope, int pos)
479 410961 : : VariableDeclaration(pos, true), scope_(scope) {}
480 :
481 : // Nested scope from which the declaration originated.
482 : Scope* scope_;
483 : };
484 :
485 : inline NestedVariableDeclaration* VariableDeclaration::AsNested() {
486 9313937 : return IsNestedField::decode(bit_field_)
487 : ? static_cast<NestedVariableDeclaration*>(this)
488 9313937 : : nullptr;
489 : }
490 :
491 : class FunctionDeclaration final : public Declaration {
492 : public:
493 : FunctionLiteral* fun() const { return fun_; }
494 : bool declares_sloppy_block_function() const {
495 : return DeclaresSloppyBlockFunction::decode(bit_field_);
496 : }
497 :
498 : private:
499 : friend class AstNodeFactory;
500 :
501 : class DeclaresSloppyBlockFunction
502 : : public BitField<bool, Declaration::kNextBitFieldIndex, 1> {};
503 :
504 : FunctionDeclaration(FunctionLiteral* fun, bool declares_sloppy_block_function,
505 : int pos)
506 926014 : : Declaration(pos, kFunctionDeclaration), fun_(fun) {
507 : bit_field_ = DeclaresSloppyBlockFunction::update(
508 926014 : bit_field_, declares_sloppy_block_function);
509 : }
510 :
511 : FunctionLiteral* fun_;
512 :
513 : static const uint8_t kNextBitFieldIndex = DeclaresSloppyBlockFunction::kNext;
514 : };
515 :
516 :
517 : class IterationStatement : public BreakableStatement {
518 : public:
519 : Statement* body() const { return body_; }
520 60238 : void set_body(Statement* s) { body_ = s; }
521 :
522 : ZonePtrList<const AstRawString>* labels() const { return labels_; }
523 :
524 : // A list of all labels that the iteration statement is directly prefixed
525 : // with, i.e. all the labels that a continue statement in the body can use to
526 : // continue this iteration statement. This is always a subset of {labels}.
527 : //
528 : // Example: "l1: { l2: if (b) l3: l4: for (;;) s }"
529 : // labels() of the Block will be l1.
530 : // labels() of the ForStatement will be l2, l3, l4.
531 : // own_labels() of the ForStatement will be l3, l4.
532 : ZonePtrList<const AstRawString>* own_labels() const { return own_labels_; }
533 :
534 : protected:
535 : IterationStatement(ZonePtrList<const AstRawString>* labels,
536 : ZonePtrList<const AstRawString>* own_labels, int pos,
537 : NodeType type)
538 : : BreakableStatement(TARGET_FOR_ANONYMOUS, pos, type),
539 : labels_(labels),
540 : own_labels_(own_labels),
541 514570 : body_(nullptr) {}
542 489797 : void Initialize(Statement* body) { body_ = body; }
543 :
544 : static const uint8_t kNextBitFieldIndex =
545 : BreakableStatement::kNextBitFieldIndex;
546 :
547 : private:
548 : ZonePtrList<const AstRawString>* labels_;
549 : ZonePtrList<const AstRawString>* own_labels_;
550 : Statement* body_;
551 : };
552 :
553 :
554 : class DoWhileStatement final : public IterationStatement {
555 : public:
556 : void Initialize(Expression* cond, Statement* body) {
557 : IterationStatement::Initialize(body);
558 4113 : cond_ = cond;
559 : }
560 :
561 : Expression* cond() const { return cond_; }
562 :
563 : private:
564 : friend class AstNodeFactory;
565 :
566 : DoWhileStatement(ZonePtrList<const AstRawString>* labels,
567 : ZonePtrList<const AstRawString>* own_labels, int pos)
568 : : IterationStatement(labels, own_labels, pos, kDoWhileStatement),
569 4113 : cond_(nullptr) {}
570 :
571 : Expression* cond_;
572 : };
573 :
574 :
575 : class WhileStatement final : public IterationStatement {
576 : public:
577 : void Initialize(Expression* cond, Statement* body) {
578 : IterationStatement::Initialize(body);
579 17487 : cond_ = cond;
580 : }
581 :
582 : Expression* cond() const { return cond_; }
583 :
584 : private:
585 : friend class AstNodeFactory;
586 :
587 : WhileStatement(ZonePtrList<const AstRawString>* labels,
588 : ZonePtrList<const AstRawString>* own_labels, int pos)
589 : : IterationStatement(labels, own_labels, pos, kWhileStatement),
590 17487 : cond_(nullptr) {}
591 :
592 : Expression* cond_;
593 : };
594 :
595 :
596 : class ForStatement final : public IterationStatement {
597 : public:
598 : void Initialize(Statement* init, Expression* cond, Statement* next,
599 : Statement* body) {
600 : IterationStatement::Initialize(body);
601 298204 : init_ = init;
602 298204 : cond_ = cond;
603 298204 : next_ = next;
604 : }
605 :
606 : Statement* init() const { return init_; }
607 : Expression* cond() const { return cond_; }
608 : Statement* next() const { return next_; }
609 :
610 : private:
611 : friend class AstNodeFactory;
612 :
613 : ForStatement(ZonePtrList<const AstRawString>* labels,
614 : ZonePtrList<const AstRawString>* own_labels, int pos)
615 : : IterationStatement(labels, own_labels, pos, kForStatement),
616 : init_(nullptr),
617 : cond_(nullptr),
618 304866 : next_(nullptr) {}
619 :
620 : Statement* init_;
621 : Expression* cond_;
622 : Statement* next_;
623 : };
624 :
625 : // Shared class for for-in and for-of statements.
626 : class ForEachStatement : public IterationStatement {
627 : public:
628 : enum VisitMode {
629 : ENUMERATE, // for (each in subject) body;
630 : ITERATE // for (each of subject) body;
631 : };
632 :
633 : using IterationStatement::Initialize;
634 :
635 : static const char* VisitModeString(VisitMode mode) {
636 6549 : return mode == ITERATE ? "for-of" : "for-in";
637 : }
638 :
639 : void Initialize(Expression* each, Expression* subject, Statement* body) {
640 : IterationStatement::Initialize(body);
641 169993 : each_ = each;
642 169993 : subject_ = subject;
643 : }
644 :
645 : Expression* each() const { return each_; }
646 : Expression* subject() const { return subject_; }
647 :
648 : protected:
649 : friend class AstNodeFactory;
650 :
651 : ForEachStatement(ZonePtrList<const AstRawString>* labels,
652 : ZonePtrList<const AstRawString>* own_labels, int pos,
653 : NodeType type)
654 : : IterationStatement(labels, own_labels, pos, type),
655 : each_(nullptr),
656 188104 : subject_(nullptr) {}
657 :
658 : Expression* each_;
659 : Expression* subject_;
660 : };
661 :
662 : class ForInStatement final : public ForEachStatement {
663 : private:
664 : friend class AstNodeFactory;
665 :
666 : ForInStatement(ZonePtrList<const AstRawString>* labels,
667 : ZonePtrList<const AstRawString>* own_labels, int pos)
668 : : ForEachStatement(labels, own_labels, pos, kForInStatement) {}
669 : };
670 :
671 : enum class IteratorType { kNormal, kAsync };
672 : class ForOfStatement final : public ForEachStatement {
673 : public:
674 : IteratorType type() const { return type_; }
675 :
676 : private:
677 : friend class AstNodeFactory;
678 :
679 : ForOfStatement(ZonePtrList<const AstRawString>* labels,
680 : ZonePtrList<const AstRawString>* own_labels, int pos,
681 : IteratorType type)
682 : : ForEachStatement(labels, own_labels, pos, kForOfStatement),
683 140605 : type_(type) {}
684 :
685 : IteratorType type_;
686 : };
687 :
688 : class ExpressionStatement final : public Statement {
689 : public:
690 1029820 : void set_expression(Expression* e) { expression_ = e; }
691 1314132 : Expression* expression() const { return expression_; }
692 : bool IsJump() const { return expression_->IsThrow(); }
693 :
694 : private:
695 : friend class AstNodeFactory;
696 :
697 : ExpressionStatement(Expression* expression, int pos)
698 18009563 : : Statement(pos, kExpressionStatement), expression_(expression) {}
699 :
700 : Expression* expression_;
701 : };
702 :
703 :
704 : class JumpStatement : public Statement {
705 : public:
706 : bool IsJump() const { return true; }
707 :
708 : protected:
709 : JumpStatement(int pos, NodeType type) : Statement(pos, type) {}
710 : };
711 :
712 :
713 : class ContinueStatement final : public JumpStatement {
714 : public:
715 : IterationStatement* target() const { return target_; }
716 :
717 : private:
718 : friend class AstNodeFactory;
719 :
720 : ContinueStatement(IterationStatement* target, int pos)
721 13847 : : JumpStatement(pos, kContinueStatement), target_(target) {}
722 :
723 : IterationStatement* target_;
724 : };
725 :
726 :
727 : class BreakStatement final : public JumpStatement {
728 : public:
729 : BreakableStatement* target() const { return target_; }
730 :
731 : private:
732 : friend class AstNodeFactory;
733 :
734 : BreakStatement(BreakableStatement* target, int pos)
735 74027 : : JumpStatement(pos, kBreakStatement), target_(target) {}
736 :
737 : BreakableStatement* target_;
738 : };
739 :
740 :
741 : class ReturnStatement final : public JumpStatement {
742 : public:
743 : enum Type { kNormal, kAsyncReturn };
744 : Expression* expression() const { return expression_; }
745 :
746 : Type type() const { return TypeField::decode(bit_field_); }
747 2060949 : bool is_async_return() const { return type() == kAsyncReturn; }
748 :
749 : int end_position() const { return end_position_; }
750 :
751 : private:
752 : friend class AstNodeFactory;
753 :
754 : ReturnStatement(Expression* expression, Type type, int pos, int end_position)
755 : : JumpStatement(pos, kReturnStatement),
756 : expression_(expression),
757 2618113 : end_position_(end_position) {
758 71541 : bit_field_ |= TypeField::encode(type);
759 : }
760 :
761 : Expression* expression_;
762 : int end_position_;
763 :
764 : class TypeField
765 : : public BitField<Type, JumpStatement::kNextBitFieldIndex, 1> {};
766 : };
767 :
768 :
769 : class WithStatement final : public Statement {
770 : public:
771 : Scope* scope() { return scope_; }
772 : Expression* expression() const { return expression_; }
773 : Statement* statement() const { return statement_; }
774 3486 : void set_statement(Statement* s) { statement_ = s; }
775 :
776 : private:
777 : friend class AstNodeFactory;
778 :
779 : WithStatement(Scope* scope, Expression* expression, Statement* statement,
780 : int pos)
781 : : Statement(pos, kWithStatement),
782 : scope_(scope),
783 : expression_(expression),
784 38382 : statement_(statement) {}
785 :
786 : Scope* scope_;
787 : Expression* expression_;
788 : Statement* statement_;
789 : };
790 :
791 : class CaseClause final : public ZoneObject {
792 : public:
793 : bool is_default() const { return label_ == nullptr; }
794 : Expression* label() const {
795 : DCHECK(!is_default());
796 : return label_;
797 : }
798 : ZonePtrList<Statement>* statements() { return &statements_; }
799 :
800 : private:
801 : friend class AstNodeFactory;
802 :
803 : CaseClause(Zone* zone, Expression* label,
804 : const ScopedPtrList<Statement>& statements);
805 :
806 : Expression* label_;
807 : ZonePtrList<Statement> statements_;
808 : };
809 :
810 :
811 : class SwitchStatement final : public BreakableStatement {
812 : public:
813 : ZonePtrList<const AstRawString>* labels() const { return labels_; }
814 :
815 : Expression* tag() const { return tag_; }
816 924 : void set_tag(Expression* t) { tag_ = t; }
817 :
818 : ZonePtrList<CaseClause>* cases() { return &cases_; }
819 :
820 : private:
821 : friend class AstNodeFactory;
822 :
823 : SwitchStatement(Zone* zone, ZonePtrList<const AstRawString>* labels,
824 : Expression* tag, int pos)
825 : : BreakableStatement(TARGET_FOR_ANONYMOUS, pos, kSwitchStatement),
826 : labels_(labels),
827 : tag_(tag),
828 14294 : cases_(4, zone) {}
829 :
830 : ZonePtrList<const AstRawString>* labels_;
831 : Expression* tag_;
832 : ZonePtrList<CaseClause> cases_;
833 : };
834 :
835 :
836 : // If-statements always have non-null references to their then- and
837 : // else-parts. When parsing if-statements with no explicit else-part,
838 : // the parser implicitly creates an empty statement. Use the
839 : // HasThenStatement() and HasElseStatement() functions to check if a
840 : // given if-statement has a then- or an else-part containing code.
841 : class IfStatement final : public Statement {
842 : public:
843 : bool HasThenStatement() const { return !then_statement_->IsEmptyStatement(); }
844 : bool HasElseStatement() const { return !else_statement_->IsEmptyStatement(); }
845 :
846 : Expression* condition() const { return condition_; }
847 : Statement* then_statement() const { return then_statement_; }
848 : Statement* else_statement() const { return else_statement_; }
849 :
850 2702 : void set_then_statement(Statement* s) { then_statement_ = s; }
851 2702 : void set_else_statement(Statement* s) { else_statement_ = s; }
852 :
853 1973 : bool IsJump() const {
854 1121 : return HasThenStatement() && then_statement()->IsJump()
855 1973 : && HasElseStatement() && else_statement()->IsJump();
856 : }
857 :
858 : private:
859 : friend class AstNodeFactory;
860 :
861 : IfStatement(Expression* condition, Statement* then_statement,
862 : Statement* else_statement, int pos)
863 : : Statement(pos, kIfStatement),
864 : condition_(condition),
865 : then_statement_(then_statement),
866 642658 : else_statement_(else_statement) {}
867 :
868 : Expression* condition_;
869 : Statement* then_statement_;
870 : Statement* else_statement_;
871 : };
872 :
873 :
874 : class TryStatement : public Statement {
875 : public:
876 : Block* try_block() const { return try_block_; }
877 4049 : void set_try_block(Block* b) { try_block_ = b; }
878 :
879 : protected:
880 : TryStatement(Block* try_block, int pos, NodeType type)
881 186306 : : Statement(pos, type), try_block_(try_block) {}
882 :
883 : private:
884 : Block* try_block_;
885 : };
886 :
887 :
888 : class TryCatchStatement final : public TryStatement {
889 : public:
890 : Scope* scope() { return scope_; }
891 : Block* catch_block() const { return catch_block_; }
892 3794 : void set_catch_block(Block* b) { catch_block_ = b; }
893 :
894 : // Prediction of whether exceptions thrown into the handler for this try block
895 : // will be caught.
896 : //
897 : // BytecodeGenerator tracks the state of catch prediction, which can change
898 : // with each TryCatchStatement encountered. The tracked catch prediction is
899 : // later compiled into the code's handler table. The runtime uses this
900 : // information to implement a feature that notifies the debugger when an
901 : // uncaught exception is thrown, _before_ the exception propagates to the top.
902 : //
903 : // If this try/catch statement is meant to rethrow (HandlerTable::UNCAUGHT),
904 : // the catch prediction value is set to the same value as the surrounding
905 : // catch prediction.
906 : //
907 : // Since it's generally undecidable whether an exception will be caught, our
908 : // prediction is only an approximation.
909 : // ---------------------------------------------------------------------------
910 : inline HandlerTable::CatchPrediction GetCatchPrediction(
911 : HandlerTable::CatchPrediction outer_catch_prediction) const {
912 69680 : if (catch_prediction_ == HandlerTable::UNCAUGHT) {
913 : return outer_catch_prediction;
914 : }
915 : return catch_prediction_;
916 : }
917 :
918 : // Indicates whether or not code should be generated to clear the pending
919 : // exception. The pending exception is cleared for cases where the exception
920 : // is not guaranteed to be rethrown, indicated by the value
921 : // HandlerTable::UNCAUGHT. If both the current and surrounding catch handler's
922 : // are predicted uncaught, the exception is not cleared.
923 : //
924 : // If this handler is not going to simply rethrow the exception, this method
925 : // indicates that the isolate's pending exception message should be cleared
926 : // before executing the catch_block.
927 : // In the normal use case, this flag is always on because the message object
928 : // is not needed anymore when entering the catch block and should not be
929 : // kept alive.
930 : // The use case where the flag is off is when the catch block is guaranteed
931 : // to rethrow the caught exception (using %ReThrow), which reuses the
932 : // pending message instead of generating a new one.
933 : // (When the catch block doesn't rethrow but is guaranteed to perform an
934 : // ordinary throw, not clearing the old message is safe but not very
935 : // useful.)
936 : inline bool ShouldClearPendingException(
937 : HandlerTable::CatchPrediction outer_catch_prediction) const {
938 69684 : return catch_prediction_ != HandlerTable::UNCAUGHT ||
939 : outer_catch_prediction != HandlerTable::UNCAUGHT;
940 : }
941 :
942 : private:
943 : friend class AstNodeFactory;
944 :
945 : TryCatchStatement(Block* try_block, Scope* scope, Block* catch_block,
946 : HandlerTable::CatchPrediction catch_prediction, int pos)
947 : : TryStatement(try_block, pos, kTryCatchStatement),
948 : scope_(scope),
949 : catch_block_(catch_block),
950 156993 : catch_prediction_(catch_prediction) {}
951 :
952 : Scope* scope_;
953 : Block* catch_block_;
954 : HandlerTable::CatchPrediction catch_prediction_;
955 : };
956 :
957 :
958 : class TryFinallyStatement final : public TryStatement {
959 : public:
960 : Block* finally_block() const { return finally_block_; }
961 102 : void set_finally_block(Block* b) { finally_block_ = b; }
962 :
963 : private:
964 : friend class AstNodeFactory;
965 :
966 : TryFinallyStatement(Block* try_block, Block* finally_block, int pos)
967 : : TryStatement(try_block, pos, kTryFinallyStatement),
968 29313 : finally_block_(finally_block) {}
969 :
970 : Block* finally_block_;
971 : };
972 :
973 :
974 : class DebuggerStatement final : public Statement {
975 : private:
976 : friend class AstNodeFactory;
977 :
978 : explicit DebuggerStatement(int pos) : Statement(pos, kDebuggerStatement) {}
979 : };
980 :
981 :
982 : class EmptyStatement final : public Statement {
983 : private:
984 : friend class AstNodeFactory;
985 : EmptyStatement() : Statement(kNoSourcePosition, kEmptyStatement) {}
986 : };
987 :
988 :
989 : // Delegates to another statement, which may be overwritten.
990 : // This was introduced to implement ES2015 Annex B3.3 for conditionally making
991 : // sloppy-mode block-scoped functions have a var binding, which is changed
992 : // from one statement to another during parsing.
993 : class SloppyBlockFunctionStatement final : public Statement {
994 : public:
995 : Statement* statement() const { return statement_; }
996 9214 : void set_statement(Statement* statement) { statement_ = statement; }
997 :
998 : private:
999 : friend class AstNodeFactory;
1000 :
1001 : SloppyBlockFunctionStatement(int pos, Statement* statement)
1002 9100 : : Statement(pos, kSloppyBlockFunctionStatement), statement_(statement) {}
1003 :
1004 : Statement* statement_;
1005 : };
1006 :
1007 :
1008 : class Literal final : public Expression {
1009 : public:
1010 : enum Type {
1011 : kSmi,
1012 : kHeapNumber,
1013 : kBigInt,
1014 : kString,
1015 : kSymbol,
1016 : kBoolean,
1017 : kUndefined,
1018 : kNull,
1019 : kTheHole,
1020 : };
1021 :
1022 : Type type() const { return TypeField::decode(bit_field_); }
1023 :
1024 : // Returns true if literal represents a property name (i.e. cannot be parsed
1025 : // as array indices).
1026 : bool IsPropertyName() const;
1027 :
1028 : // Returns true if literal represents an array index.
1029 : // Note, that in general the following statement is not true:
1030 : // key->IsPropertyName() != key->AsArrayIndex(...)
1031 : // but for non-computed LiteralProperty properties the following is true:
1032 : // property->key()->IsPropertyName() != property->key()->AsArrayIndex(...)
1033 : bool AsArrayIndex(uint32_t* index) const;
1034 :
1035 20822 : const AstRawString* AsRawPropertyName() {
1036 : DCHECK(IsPropertyName());
1037 20822 : return string_;
1038 : }
1039 :
1040 : Smi AsSmiLiteral() const {
1041 : DCHECK_EQ(kSmi, type());
1042 : return Smi::FromInt(smi_);
1043 : }
1044 :
1045 : // Returns true if literal represents a Number.
1046 3629651 : bool IsNumber() const { return type() == kHeapNumber || type() == kSmi; }
1047 3352243 : double AsNumber() const {
1048 : DCHECK(IsNumber());
1049 3352243 : switch (type()) {
1050 : case kSmi:
1051 2971624 : return smi_;
1052 : case kHeapNumber:
1053 380619 : return number_;
1054 : default:
1055 0 : UNREACHABLE();
1056 : }
1057 : }
1058 :
1059 : AstBigInt AsBigInt() const {
1060 : DCHECK_EQ(type(), kBigInt);
1061 : return bigint_;
1062 : }
1063 :
1064 8651562 : bool IsString() const { return type() == kString; }
1065 : const AstRawString* AsRawString() {
1066 : DCHECK_EQ(type(), kString);
1067 : return string_;
1068 : }
1069 :
1070 : AstSymbol AsSymbol() {
1071 : DCHECK_EQ(type(), kSymbol);
1072 : return symbol_;
1073 : }
1074 :
1075 : V8_EXPORT_PRIVATE bool ToBooleanIsTrue() const;
1076 33880 : bool ToBooleanIsFalse() const { return !ToBooleanIsTrue(); }
1077 :
1078 : bool ToUint32(uint32_t* value) const;
1079 :
1080 : // Returns an appropriate Object representing this Literal, allocating
1081 : // a heap object if needed.
1082 : Handle<Object> BuildValue(Isolate* isolate) const;
1083 :
1084 : // Support for using Literal as a HashMap key. NOTE: Currently, this works
1085 : // only for string and number literals!
1086 : uint32_t Hash();
1087 : static bool Match(void* literal1, void* literal2);
1088 :
1089 : private:
1090 : friend class AstNodeFactory;
1091 :
1092 : class TypeField : public BitField<Type, Expression::kNextBitFieldIndex, 4> {};
1093 :
1094 22778410 : Literal(int smi, int position) : Expression(position, kLiteral), smi_(smi) {
1095 : bit_field_ = TypeField::update(bit_field_, kSmi);
1096 : }
1097 :
1098 : Literal(double number, int position)
1099 795579 : : Expression(position, kLiteral), number_(number) {
1100 795579 : bit_field_ = TypeField::update(bit_field_, kHeapNumber);
1101 : }
1102 :
1103 : Literal(AstBigInt bigint, int position)
1104 12251 : : Expression(position, kLiteral), bigint_(bigint) {
1105 12251 : bit_field_ = TypeField::update(bit_field_, kBigInt);
1106 : }
1107 :
1108 : Literal(const AstRawString* string, int position)
1109 12153180 : : Expression(position, kLiteral), string_(string) {
1110 12153180 : bit_field_ = TypeField::update(bit_field_, kString);
1111 : }
1112 :
1113 : Literal(AstSymbol symbol, int position)
1114 2758 : : Expression(position, kLiteral), symbol_(symbol) {
1115 2758 : bit_field_ = TypeField::update(bit_field_, kSymbol);
1116 : }
1117 :
1118 : Literal(bool boolean, int position)
1119 445344 : : Expression(position, kLiteral), boolean_(boolean) {
1120 445344 : bit_field_ = TypeField::update(bit_field_, kBoolean);
1121 : }
1122 :
1123 : Literal(Type type, int position) : Expression(position, kLiteral) {
1124 : DCHECK(type == kNull || type == kUndefined || type == kTheHole);
1125 1603570 : bit_field_ = TypeField::update(bit_field_, type);
1126 : }
1127 :
1128 : union {
1129 : const AstRawString* string_;
1130 : int smi_;
1131 : double number_;
1132 : AstSymbol symbol_;
1133 : AstBigInt bigint_;
1134 : bool boolean_;
1135 : };
1136 : };
1137 :
1138 : // Base class for literals that need space in the type feedback vector.
1139 : class MaterializedLiteral : public Expression {
1140 : public:
1141 : // A Materializedliteral is simple if the values consist of only
1142 : // constants and simple object and array literals.
1143 : bool IsSimple() const;
1144 :
1145 : protected:
1146 : MaterializedLiteral(int pos, NodeType type) : Expression(pos, type) {}
1147 :
1148 : friend class CompileTimeValue;
1149 : friend class ArrayLiteral;
1150 : friend class ObjectLiteral;
1151 :
1152 : // Populate the depth field and any flags the literal has, returns the depth.
1153 : int InitDepthAndFlags();
1154 :
1155 : bool NeedsInitialAllocationSite();
1156 :
1157 : // Populate the constant properties/elements fixed array.
1158 : void BuildConstants(Isolate* isolate);
1159 :
1160 : // If the expression is a literal, return the literal value;
1161 : // if the expression is a materialized literal and is_simple
1162 : // then return an Array or Object Boilerplate Description
1163 : // Otherwise, return undefined literal as the placeholder
1164 : // in the object literal boilerplate.
1165 : Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1166 : };
1167 :
1168 : // Node for capturing a regexp literal.
1169 : class RegExpLiteral final : public MaterializedLiteral {
1170 : public:
1171 252 : Handle<String> pattern() const { return pattern_->string(); }
1172 : const AstRawString* raw_pattern() const { return pattern_; }
1173 : int flags() const { return flags_; }
1174 :
1175 : private:
1176 : friend class AstNodeFactory;
1177 :
1178 : RegExpLiteral(const AstRawString* pattern, int flags, int pos)
1179 : : MaterializedLiteral(pos, kRegExpLiteral),
1180 : flags_(flags),
1181 48735 : pattern_(pattern) {}
1182 :
1183 : int const flags_;
1184 : const AstRawString* const pattern_;
1185 : };
1186 :
1187 : // Base class for Array and Object literals, providing common code for handling
1188 : // nested subliterals.
1189 : class AggregateLiteral : public MaterializedLiteral {
1190 : public:
1191 : enum Flags {
1192 : kNoFlags = 0,
1193 : kIsShallow = 1,
1194 : kDisableMementos = 1 << 1,
1195 : kNeedsInitialAllocationSite = 1 << 2,
1196 : };
1197 :
1198 677892 : bool is_initialized() const { return 0 < depth_; }
1199 : int depth() const {
1200 : DCHECK(is_initialized());
1201 1259055 : return depth_;
1202 : }
1203 :
1204 : bool is_shallow() const { return depth() == 1; }
1205 : bool needs_initial_allocation_site() const {
1206 : return NeedsInitialAllocationSiteField::decode(bit_field_);
1207 : }
1208 :
1209 579363 : int ComputeFlags(bool disable_mementos = false) const {
1210 : int flags = kNoFlags;
1211 579363 : if (is_shallow()) flags |= kIsShallow;
1212 201895 : if (disable_mementos) flags |= kDisableMementos;
1213 579363 : if (needs_initial_allocation_site()) flags |= kNeedsInitialAllocationSite;
1214 : return flags;
1215 : }
1216 :
1217 : // An AggregateLiteral is simple if the values consist of only
1218 : // constants and simple object and array literals.
1219 : bool is_simple() const { return IsSimpleField::decode(bit_field_); }
1220 :
1221 : private:
1222 : int depth_ : 31;
1223 : class NeedsInitialAllocationSiteField
1224 : : public BitField<bool, MaterializedLiteral::kNextBitFieldIndex, 1> {};
1225 : class IsSimpleField
1226 : : public BitField<bool, NeedsInitialAllocationSiteField::kNext, 1> {};
1227 :
1228 : protected:
1229 : friend class AstNodeFactory;
1230 : AggregateLiteral(int pos, NodeType type)
1231 1355818 : : MaterializedLiteral(pos, type), depth_(0) {
1232 : bit_field_ |= NeedsInitialAllocationSiteField::encode(false) |
1233 : IsSimpleField::encode(false);
1234 : }
1235 :
1236 : void set_is_simple(bool is_simple) {
1237 1309458 : bit_field_ = IsSimpleField::update(bit_field_, is_simple);
1238 : }
1239 :
1240 : void set_depth(int depth) {
1241 : DCHECK(!is_initialized());
1242 654729 : depth_ = depth;
1243 : }
1244 :
1245 : void set_needs_initial_allocation_site(bool required) {
1246 654729 : bit_field_ = NeedsInitialAllocationSiteField::update(bit_field_, required);
1247 : }
1248 :
1249 : static const uint8_t kNextBitFieldIndex = IsSimpleField::kNext;
1250 : };
1251 :
1252 : // Common supertype for ObjectLiteralProperty and ClassLiteralProperty
1253 : class LiteralProperty : public ZoneObject {
1254 : public:
1255 234 : Expression* key() const { return key_and_is_computed_name_.GetPointer(); }
1256 : Expression* value() const { return value_; }
1257 :
1258 : bool is_computed_name() const {
1259 : return key_and_is_computed_name_.GetPayload();
1260 : }
1261 : bool NeedsSetFunctionName() const;
1262 :
1263 : protected:
1264 : LiteralProperty(Expression* key, Expression* value, bool is_computed_name)
1265 3476154 : : key_and_is_computed_name_(key, is_computed_name), value_(value) {}
1266 :
1267 : PointerWithPayload<Expression, bool, 1> key_and_is_computed_name_;
1268 : Expression* value_;
1269 : };
1270 :
1271 : // Property is used for passing information
1272 : // about an object literal's properties from the parser
1273 : // to the code generator.
1274 : class ObjectLiteralProperty final : public LiteralProperty {
1275 : public:
1276 : enum Kind : uint8_t {
1277 : CONSTANT, // Property with constant value (compile time).
1278 : COMPUTED, // Property with computed value (execution time).
1279 : MATERIALIZED_LITERAL, // Property value is a materialized literal.
1280 : GETTER,
1281 : SETTER, // Property is an accessor function.
1282 : PROTOTYPE, // Property is __proto__.
1283 : SPREAD
1284 : };
1285 :
1286 : Kind kind() const { return kind_; }
1287 :
1288 : bool IsCompileTimeValue() const;
1289 :
1290 : void set_emit_store(bool emit_store);
1291 : bool emit_store() const;
1292 :
1293 : bool IsNullPrototype() const {
1294 11296 : return IsPrototype() && value()->IsNullLiteral();
1295 : }
1296 15054018 : bool IsPrototype() const { return kind() == PROTOTYPE; }
1297 :
1298 : private:
1299 : friend class AstNodeFactory;
1300 :
1301 : ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1302 : bool is_computed_name);
1303 : ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1304 : Expression* value, bool is_computed_name);
1305 :
1306 : Kind kind_;
1307 : bool emit_store_;
1308 : };
1309 :
1310 :
1311 : // An object literal has a boilerplate object that is used
1312 : // for minimizing the work when constructing it at runtime.
1313 : class ObjectLiteral final : public AggregateLiteral {
1314 : public:
1315 : typedef ObjectLiteralProperty Property;
1316 :
1317 : Handle<ObjectBoilerplateDescription> boilerplate_description() const {
1318 : DCHECK(!boilerplate_description_.is_null());
1319 : return boilerplate_description_;
1320 : }
1321 370642 : int properties_count() const { return boilerplate_properties_; }
1322 207 : const ZonePtrList<Property>* properties() const { return &properties_; }
1323 : bool has_elements() const { return HasElementsField::decode(bit_field_); }
1324 : bool has_rest_property() const {
1325 : return HasRestPropertyField::decode(bit_field_);
1326 : }
1327 : bool fast_elements() const { return FastElementsField::decode(bit_field_); }
1328 : bool has_null_prototype() const {
1329 : return HasNullPrototypeField::decode(bit_field_);
1330 : }
1331 :
1332 457325 : bool is_empty() const {
1333 : DCHECK(is_initialized());
1334 487991 : return !has_elements() && properties_count() == 0 &&
1335 30666 : properties()->length() == 0;
1336 : }
1337 :
1338 229964 : bool IsEmptyObjectLiteral() const {
1339 258124 : return is_empty() && !has_null_prototype();
1340 : }
1341 :
1342 : // Populate the depth field and flags, returns the depth.
1343 : int InitDepthAndFlags();
1344 :
1345 : // Get the boilerplate description, populating it if necessary.
1346 : Handle<ObjectBoilerplateDescription> GetOrBuildBoilerplateDescription(
1347 : Isolate* isolate) {
1348 187753 : if (boilerplate_description_.is_null()) {
1349 172412 : BuildBoilerplateDescription(isolate);
1350 : }
1351 : return boilerplate_description();
1352 : }
1353 :
1354 : // Populate the boilerplate description.
1355 : void BuildBoilerplateDescription(Isolate* isolate);
1356 :
1357 : // Mark all computed expressions that are bound to a key that
1358 : // is shadowed by a later occurrence of the same key. For the
1359 : // marked expressions, no store code is emitted.
1360 : void CalculateEmitStore(Zone* zone);
1361 :
1362 : // Determines whether the {CreateShallowObjectLiteratal} builtin can be used.
1363 : bool IsFastCloningSupported() const;
1364 :
1365 : // Assemble bitfield of flags for the CreateObjectLiteral helper.
1366 201895 : int ComputeFlags(bool disable_mementos = false) const {
1367 : int flags = AggregateLiteral::ComputeFlags(disable_mementos);
1368 201895 : if (fast_elements()) flags |= kFastElements;
1369 201895 : if (has_null_prototype()) flags |= kHasNullPrototype;
1370 201895 : return flags;
1371 : }
1372 :
1373 205236 : int EncodeLiteralType() {
1374 : int flags = kNoFlags;
1375 205236 : if (fast_elements()) flags |= kFastElements;
1376 205236 : if (has_null_prototype()) flags |= kHasNullPrototype;
1377 : return flags;
1378 : }
1379 :
1380 : enum Flags {
1381 : kFastElements = 1 << 3,
1382 : kHasNullPrototype = 1 << 4,
1383 : };
1384 : STATIC_ASSERT(
1385 : static_cast<int>(AggregateLiteral::kNeedsInitialAllocationSite) <
1386 : static_cast<int>(kFastElements));
1387 :
1388 : struct Accessors: public ZoneObject {
1389 4629 : Accessors() : getter(nullptr), setter(nullptr) {}
1390 : ObjectLiteralProperty* getter;
1391 : ObjectLiteralProperty* setter;
1392 : };
1393 :
1394 : private:
1395 : friend class AstNodeFactory;
1396 :
1397 580407 : ObjectLiteral(Zone* zone, const ScopedPtrList<Property>& properties,
1398 : uint32_t boilerplate_properties, int pos,
1399 : bool has_rest_property)
1400 : : AggregateLiteral(pos, kObjectLiteral),
1401 : boilerplate_properties_(boilerplate_properties),
1402 1160814 : properties_(0, nullptr) {
1403 : bit_field_ |= HasElementsField::encode(false) |
1404 : HasRestPropertyField::encode(has_rest_property) |
1405 : FastElementsField::encode(false) |
1406 580407 : HasNullPrototypeField::encode(false);
1407 580407 : properties.CopyTo(&properties_, zone);
1408 580419 : }
1409 :
1410 : void InitFlagsForPendingNullPrototype(int i);
1411 :
1412 : void set_has_elements(bool has_elements) {
1413 248401 : bit_field_ = HasElementsField::update(bit_field_, has_elements);
1414 : }
1415 : void set_fast_elements(bool fast_elements) {
1416 248401 : bit_field_ = FastElementsField::update(bit_field_, fast_elements);
1417 : }
1418 : void set_has_null_protoype(bool has_null_prototype) {
1419 5682 : bit_field_ = HasNullPrototypeField::update(bit_field_, has_null_prototype);
1420 : }
1421 :
1422 : uint32_t boilerplate_properties_;
1423 : Handle<ObjectBoilerplateDescription> boilerplate_description_;
1424 : ZoneList<Property*> properties_;
1425 :
1426 : class HasElementsField
1427 : : public BitField<bool, AggregateLiteral::kNextBitFieldIndex, 1> {};
1428 : class HasRestPropertyField
1429 : : public BitField<bool, HasElementsField::kNext, 1> {};
1430 : class FastElementsField
1431 : : public BitField<bool, HasRestPropertyField::kNext, 1> {};
1432 : class HasNullPrototypeField
1433 : : public BitField<bool, FastElementsField::kNext, 1> {};
1434 : };
1435 :
1436 :
1437 : // A map from property names to getter/setter pairs allocated in the zone.
1438 : class AccessorTable
1439 : : public base::TemplateHashMap<Literal, ObjectLiteral::Accessors,
1440 : bool (*)(void*, void*),
1441 : ZoneAllocationPolicy> {
1442 : public:
1443 : explicit AccessorTable(Zone* zone)
1444 : : base::TemplateHashMap<Literal, ObjectLiteral::Accessors,
1445 : bool (*)(void*, void*), ZoneAllocationPolicy>(
1446 : Literal::Match, ZoneAllocationPolicy(zone)),
1447 201797 : zone_(zone) {}
1448 :
1449 5340 : Iterator lookup(Literal* literal) {
1450 10680 : Iterator it = find(literal, true, ZoneAllocationPolicy(zone_));
1451 5341 : if (it->second == nullptr) {
1452 9258 : it->second = new (zone_) ObjectLiteral::Accessors();
1453 : }
1454 5341 : return it;
1455 : }
1456 :
1457 : private:
1458 : Zone* zone_;
1459 : };
1460 :
1461 :
1462 : // An array literal has a literals object that is used
1463 : // for minimizing the work when constructing it at runtime.
1464 : class ArrayLiteral final : public AggregateLiteral {
1465 : public:
1466 : Handle<ArrayBoilerplateDescription> boilerplate_description() const {
1467 : return boilerplate_description_;
1468 : }
1469 :
1470 : const ZonePtrList<Expression>* values() const { return &values_; }
1471 :
1472 : int first_spread_index() const { return first_spread_index_; }
1473 :
1474 : // Populate the depth field and flags, returns the depth.
1475 : int InitDepthAndFlags();
1476 :
1477 : // Get the boilerplate description, populating it if necessary.
1478 : Handle<ArrayBoilerplateDescription> GetOrBuildBoilerplateDescription(
1479 : Isolate* isolate) {
1480 154411 : if (boilerplate_description_.is_null()) {
1481 150015 : BuildBoilerplateDescription(isolate);
1482 : }
1483 : return boilerplate_description();
1484 : }
1485 :
1486 : // Populate the boilerplate description.
1487 : void BuildBoilerplateDescription(Isolate* isolate);
1488 :
1489 : // Determines whether the {CreateShallowArrayLiteral} builtin can be used.
1490 : bool IsFastCloningSupported() const;
1491 :
1492 : // Assemble bitfield of flags for the CreateArrayLiteral helper.
1493 : int ComputeFlags(bool disable_mementos = false) const {
1494 : return AggregateLiteral::ComputeFlags(disable_mementos);
1495 : }
1496 :
1497 : private:
1498 : friend class AstNodeFactory;
1499 :
1500 775411 : ArrayLiteral(Zone* zone, const ScopedPtrList<Expression>& values,
1501 : int first_spread_index, int pos)
1502 : : AggregateLiteral(pos, kArrayLiteral),
1503 : first_spread_index_(first_spread_index),
1504 1550822 : values_(0, nullptr) {
1505 775394 : values.CopyTo(&values_, zone);
1506 775406 : }
1507 :
1508 : int first_spread_index_;
1509 : Handle<ArrayBoilerplateDescription> boilerplate_description_;
1510 : ZonePtrList<Expression> values_;
1511 : };
1512 :
1513 : enum class HoleCheckMode { kRequired, kElided };
1514 :
1515 : class VariableProxy final : public Expression {
1516 : public:
1517 0 : bool IsValidReferenceExpression() const {
1518 0 : return !is_this() && !is_new_target();
1519 : }
1520 :
1521 73019 : Handle<String> name() const { return raw_name()->string(); }
1522 130562179 : const AstRawString* raw_name() const {
1523 133307872 : return is_resolved() ? var_->raw_name() : raw_name_;
1524 : }
1525 :
1526 : Variable* var() const {
1527 : DCHECK(is_resolved());
1528 : return var_;
1529 : }
1530 : void set_var(Variable* v) {
1531 : DCHECK(!is_resolved());
1532 : DCHECK_NOT_NULL(v);
1533 34794943 : var_ = v;
1534 : }
1535 :
1536 75670280 : bool is_this() const { return IsThisField::decode(bit_field_); }
1537 :
1538 : bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1539 1183128 : void set_is_assigned() {
1540 19241183 : bit_field_ = IsAssignedField::update(bit_field_, true);
1541 11114896 : if (is_resolved()) {
1542 : var()->set_maybe_assigned();
1543 : }
1544 : }
1545 :
1546 : bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1547 : void set_is_resolved() {
1548 66705297 : bit_field_ = IsResolvedField::update(bit_field_, true);
1549 : }
1550 :
1551 67842476 : bool is_new_target() const { return IsNewTargetField::decode(bit_field_); }
1552 : void set_is_new_target() {
1553 4006 : bit_field_ = IsNewTargetField::update(bit_field_, true);
1554 : }
1555 :
1556 : HoleCheckMode hole_check_mode() const {
1557 : HoleCheckMode mode = HoleCheckModeField::decode(bit_field_);
1558 : DCHECK_IMPLIES(mode == HoleCheckMode::kRequired,
1559 : var()->binding_needs_init() ||
1560 : var()->local_if_not_shadowed()->binding_needs_init());
1561 : return mode;
1562 : }
1563 : void set_needs_hole_check() {
1564 : bit_field_ =
1565 5469612 : HoleCheckModeField::update(bit_field_, HoleCheckMode::kRequired);
1566 : }
1567 :
1568 2745693 : bool IsPrivateName() const {
1569 5491386 : return raw_name()->length() > 0 && raw_name()->FirstCharacter() == '#';
1570 : }
1571 :
1572 : // Bind this proxy to the variable var.
1573 : void BindTo(Variable* var);
1574 :
1575 : V8_INLINE VariableProxy* next_unresolved() { return next_unresolved_; }
1576 : V8_INLINE bool is_removed_from_unresolved() const {
1577 26962745 : return IsRemovedFromUnresolvedField::decode(bit_field_);
1578 : }
1579 :
1580 : void mark_removed_from_unresolved() {
1581 31566 : bit_field_ = IsRemovedFromUnresolvedField::update(bit_field_, true);
1582 : }
1583 :
1584 : // Provides an access type for the ThreadedList used by the PreParsers
1585 : // expressions, lists, and formal parameters.
1586 : struct PreParserNext {
1587 : static VariableProxy** next(VariableProxy* t) {
1588 : return t->pre_parser_expr_next();
1589 : }
1590 :
1591 : static VariableProxy** start(VariableProxy** head) { return head; }
1592 : };
1593 :
1594 : // Provides an access type for the ThreadedList used by the PreParsers
1595 : // expressions, lists, and formal parameters.
1596 : struct UnresolvedNext {
1597 91987884 : static VariableProxy** filter(VariableProxy** t) {
1598 : VariableProxy** n = t;
1599 : // Skip over possibly removed values.
1600 210943597 : while (*n != nullptr && (*n)->is_removed_from_unresolved()) {
1601 26970215 : n = (*n)->next();
1602 : }
1603 91985498 : return n;
1604 : }
1605 :
1606 5605021 : static VariableProxy** start(VariableProxy** head) { return filter(head); }
1607 :
1608 86403406 : static VariableProxy** next(VariableProxy* t) { return filter(t->next()); }
1609 : };
1610 :
1611 : private:
1612 : friend class AstNodeFactory;
1613 :
1614 : VariableProxy(Variable* var, int start_position);
1615 :
1616 : VariableProxy(const AstRawString* name, VariableKind variable_kind,
1617 : int start_position)
1618 : : Expression(start_position, kVariableProxy),
1619 : raw_name_(name),
1620 : next_unresolved_(nullptr),
1621 78855793 : pre_parser_expr_next_(nullptr) {
1622 78855793 : bit_field_ |= IsThisField::encode(variable_kind == THIS_VARIABLE) |
1623 : IsAssignedField::encode(false) |
1624 : IsResolvedField::encode(false) |
1625 : IsRemovedFromUnresolvedField::encode(false) |
1626 78855793 : HoleCheckModeField::encode(HoleCheckMode::kElided);
1627 : }
1628 :
1629 : explicit VariableProxy(const VariableProxy* copy_from);
1630 :
1631 : class IsThisField : public BitField<bool, Expression::kNextBitFieldIndex, 1> {
1632 : };
1633 : class IsAssignedField : public BitField<bool, IsThisField::kNext, 1> {};
1634 : class IsResolvedField : public BitField<bool, IsAssignedField::kNext, 1> {};
1635 : class IsRemovedFromUnresolvedField
1636 : : public BitField<bool, IsResolvedField::kNext, 1> {};
1637 : class IsNewTargetField
1638 : : public BitField<bool, IsRemovedFromUnresolvedField::kNext, 1> {};
1639 : class HoleCheckModeField
1640 : : public BitField<HoleCheckMode, IsNewTargetField::kNext, 1> {};
1641 :
1642 : union {
1643 : const AstRawString* raw_name_; // if !is_resolved_
1644 : Variable* var_; // if is_resolved_
1645 : };
1646 :
1647 : V8_INLINE VariableProxy** next() { return &next_unresolved_; }
1648 : VariableProxy* next_unresolved_;
1649 :
1650 : VariableProxy** pre_parser_expr_next() { return &pre_parser_expr_next_; }
1651 : VariableProxy* pre_parser_expr_next_;
1652 :
1653 : friend base::ThreadedListTraits<VariableProxy>;
1654 : };
1655 :
1656 : // Assignments to a property will use one of several types of property access.
1657 : // Otherwise, the assignment is to a non-property (a global, a local slot, a
1658 : // parameter slot, or a destructuring pattern).
1659 : enum AssignType {
1660 : NON_PROPERTY,
1661 : NAMED_PROPERTY,
1662 : KEYED_PROPERTY,
1663 : NAMED_SUPER_PROPERTY,
1664 : KEYED_SUPER_PROPERTY
1665 : };
1666 :
1667 : class Property final : public Expression {
1668 : public:
1669 : bool IsValidReferenceExpression() const { return true; }
1670 :
1671 3953268 : Expression* obj() const { return obj_; }
1672 : Expression* key() const { return key_; }
1673 :
1674 7442384 : bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1675 :
1676 : // Returns the properties assign type.
1677 18689364 : static AssignType GetAssignType(Property* property) {
1678 12053053 : if (property == nullptr) return NON_PROPERTY;
1679 : bool super_access = property->IsSuperAccess();
1680 6636311 : return (property->key()->IsPropertyName())
1681 : ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1682 6636187 : : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1683 : }
1684 :
1685 : private:
1686 : friend class AstNodeFactory;
1687 :
1688 : Property(Expression* obj, Expression* key, int pos)
1689 7381282 : : Expression(pos, kProperty), obj_(obj), key_(key) {
1690 : }
1691 :
1692 : Expression* obj_;
1693 : Expression* key_;
1694 : };
1695 :
1696 : // ResolvedProperty pairs a receiver field with a value field. It allows Call
1697 : // to support arbitrary receivers while still taking advantage of TypeFeedback.
1698 : class ResolvedProperty final : public Expression {
1699 : public:
1700 : VariableProxy* object() const { return object_; }
1701 : VariableProxy* property() const { return property_; }
1702 :
1703 : void set_object(VariableProxy* e) { object_ = e; }
1704 : void set_property(VariableProxy* e) { property_ = e; }
1705 :
1706 : private:
1707 : friend class AstNodeFactory;
1708 :
1709 : ResolvedProperty(VariableProxy* obj, VariableProxy* property, int pos)
1710 : : Expression(pos, kResolvedProperty), object_(obj), property_(property) {}
1711 :
1712 : VariableProxy* object_;
1713 : VariableProxy* property_;
1714 : };
1715 :
1716 : class Call final : public Expression {
1717 : public:
1718 : Expression* expression() const { return expression_; }
1719 : const ZonePtrList<Expression>* arguments() const { return &arguments_; }
1720 :
1721 : bool is_possibly_eval() const {
1722 : return IsPossiblyEvalField::decode(bit_field_);
1723 : }
1724 :
1725 : bool is_tagged_template() const {
1726 : return IsTaggedTemplateField::decode(bit_field_);
1727 : }
1728 :
1729 5161895 : bool only_last_arg_is_spread() {
1730 10188416 : return !arguments_.is_empty() && arguments_.last()->IsSpread();
1731 : }
1732 :
1733 : enum CallType {
1734 : GLOBAL_CALL,
1735 : WITH_CALL,
1736 : NAMED_PROPERTY_CALL,
1737 : KEYED_PROPERTY_CALL,
1738 : NAMED_SUPER_PROPERTY_CALL,
1739 : KEYED_SUPER_PROPERTY_CALL,
1740 : SUPER_CALL,
1741 : RESOLVED_PROPERTY_CALL,
1742 : OTHER_CALL
1743 : };
1744 :
1745 : enum PossiblyEval {
1746 : IS_POSSIBLY_EVAL,
1747 : NOT_EVAL,
1748 : };
1749 :
1750 : // Helpers to determine how to handle the call.
1751 : CallType GetCallType() const;
1752 :
1753 : enum class TaggedTemplateTag { kTrue };
1754 :
1755 : private:
1756 : friend class AstNodeFactory;
1757 :
1758 6545175 : Call(Zone* zone, Expression* expression,
1759 : const ScopedPtrList<Expression>& arguments, int pos,
1760 : PossiblyEval possibly_eval)
1761 : : Expression(pos, kCall),
1762 : expression_(expression),
1763 6545175 : arguments_(0, nullptr) {
1764 : bit_field_ |=
1765 6544989 : IsPossiblyEvalField::encode(possibly_eval == IS_POSSIBLY_EVAL) |
1766 6544989 : IsTaggedTemplateField::encode(false);
1767 6544989 : arguments.CopyTo(&arguments_, zone);
1768 6545017 : }
1769 :
1770 7314 : Call(Zone* zone, Expression* expression,
1771 : const ScopedPtrList<Expression>& arguments, int pos,
1772 : TaggedTemplateTag tag)
1773 : : Expression(pos, kCall),
1774 : expression_(expression),
1775 7314 : arguments_(0, nullptr) {
1776 : bit_field_ |= IsPossiblyEvalField::encode(false) |
1777 7313 : IsTaggedTemplateField::encode(true);
1778 7313 : arguments.CopyTo(&arguments_, zone);
1779 7313 : }
1780 :
1781 : class IsPossiblyEvalField
1782 : : public BitField<bool, Expression::kNextBitFieldIndex, 1> {};
1783 : class IsTaggedTemplateField
1784 : : public BitField<bool, IsPossiblyEvalField::kNext, 1> {};
1785 :
1786 : Expression* expression_;
1787 : ZonePtrList<Expression> arguments_;
1788 : };
1789 :
1790 :
1791 : class CallNew final : public Expression {
1792 : public:
1793 : Expression* expression() const { return expression_; }
1794 : const ZonePtrList<Expression>* arguments() const { return &arguments_; }
1795 :
1796 142260 : bool only_last_arg_is_spread() {
1797 294618 : return !arguments_.is_empty() && arguments_.last()->IsSpread();
1798 : }
1799 :
1800 : private:
1801 : friend class AstNodeFactory;
1802 :
1803 172880 : CallNew(Zone* zone, Expression* expression,
1804 : const ScopedPtrList<Expression>& arguments, int pos)
1805 : : Expression(pos, kCallNew),
1806 : expression_(expression),
1807 172880 : arguments_(0, nullptr) {
1808 172874 : arguments.CopyTo(&arguments_, zone);
1809 172899 : }
1810 :
1811 : Expression* expression_;
1812 : ZonePtrList<Expression> arguments_;
1813 : };
1814 :
1815 : // The CallRuntime class does not represent any official JavaScript
1816 : // language construct. Instead it is used to call a C or JS function
1817 : // with a set of arguments. This is used from the builtins that are
1818 : // implemented in JavaScript.
1819 : class CallRuntime final : public Expression {
1820 : public:
1821 : const ZonePtrList<Expression>* arguments() const { return &arguments_; }
1822 : bool is_jsruntime() const { return function_ == nullptr; }
1823 :
1824 : int context_index() const {
1825 : DCHECK(is_jsruntime());
1826 : return context_index_;
1827 : }
1828 : const Runtime::Function* function() const {
1829 : DCHECK(!is_jsruntime());
1830 : return function_;
1831 : }
1832 :
1833 : const char* debug_name();
1834 :
1835 : private:
1836 : friend class AstNodeFactory;
1837 :
1838 151265 : CallRuntime(Zone* zone, const Runtime::Function* function,
1839 : const ScopedPtrList<Expression>& arguments, int pos)
1840 : : Expression(pos, kCallRuntime),
1841 : function_(function),
1842 151265 : arguments_(0, nullptr) {
1843 151268 : arguments.CopyTo(&arguments_, zone);
1844 151286 : }
1845 722 : CallRuntime(Zone* zone, int context_index,
1846 : const ScopedPtrList<Expression>& arguments, int pos)
1847 : : Expression(pos, kCallRuntime),
1848 : context_index_(context_index),
1849 : function_(nullptr),
1850 722 : arguments_(0, nullptr) {
1851 722 : arguments.CopyTo(&arguments_, zone);
1852 723 : }
1853 :
1854 : int context_index_;
1855 : const Runtime::Function* function_;
1856 : ZonePtrList<Expression> arguments_;
1857 : };
1858 :
1859 :
1860 : class UnaryOperation final : public Expression {
1861 : public:
1862 : Token::Value op() const { return OperatorField::decode(bit_field_); }
1863 : Expression* expression() const { return expression_; }
1864 :
1865 : private:
1866 : friend class AstNodeFactory;
1867 :
1868 : UnaryOperation(Token::Value op, Expression* expression, int pos)
1869 490368 : : Expression(pos, kUnaryOperation), expression_(expression) {
1870 490368 : bit_field_ |= OperatorField::encode(op);
1871 : DCHECK(Token::IsUnaryOp(op));
1872 : }
1873 :
1874 : Expression* expression_;
1875 :
1876 : class OperatorField
1877 : : public BitField<Token::Value, Expression::kNextBitFieldIndex, 7> {};
1878 : };
1879 :
1880 :
1881 : class BinaryOperation final : public Expression {
1882 : public:
1883 : Token::Value op() const { return OperatorField::decode(bit_field_); }
1884 : Expression* left() const { return left_; }
1885 : Expression* right() const { return right_; }
1886 :
1887 : // Returns true if one side is a Smi literal, returning the other side's
1888 : // sub-expression in |subexpr| and the literal Smi in |literal|.
1889 : bool IsSmiLiteralOperation(Expression** subexpr, Smi* literal);
1890 :
1891 : private:
1892 : friend class AstNodeFactory;
1893 :
1894 : BinaryOperation(Token::Value op, Expression* left, Expression* right, int pos)
1895 873991 : : Expression(pos, kBinaryOperation), left_(left), right_(right) {
1896 873991 : bit_field_ |= OperatorField::encode(op);
1897 : DCHECK(Token::IsBinaryOp(op));
1898 : }
1899 :
1900 : Expression* left_;
1901 : Expression* right_;
1902 :
1903 : class OperatorField
1904 : : public BitField<Token::Value, Expression::kNextBitFieldIndex, 7> {};
1905 : };
1906 :
1907 : class NaryOperation final : public Expression {
1908 : public:
1909 : Token::Value op() const { return OperatorField::decode(bit_field_); }
1910 : Expression* first() const { return first_; }
1911 : Expression* subsequent(size_t index) const {
1912 3621435 : return subsequent_[index].expression;
1913 : }
1914 :
1915 3091232 : size_t subsequent_length() const { return subsequent_.size(); }
1916 : int subsequent_op_position(size_t index) const {
1917 619638 : return subsequent_[index].op_position;
1918 : }
1919 :
1920 : void AddSubsequent(Expression* expr, int pos) {
1921 3058169 : subsequent_.emplace_back(expr, pos);
1922 : }
1923 :
1924 : private:
1925 : friend class AstNodeFactory;
1926 :
1927 : NaryOperation(Zone* zone, Token::Value op, Expression* first,
1928 : size_t initial_subsequent_size)
1929 : : Expression(first->position(), kNaryOperation),
1930 : first_(first),
1931 246916 : subsequent_(zone) {
1932 123458 : bit_field_ |= OperatorField::encode(op);
1933 : DCHECK(Token::IsBinaryOp(op));
1934 : DCHECK_NE(op, Token::EXP);
1935 123458 : subsequent_.reserve(initial_subsequent_size);
1936 : }
1937 :
1938 : // Nary operations store the first (lhs) child expression inline, and the
1939 : // child expressions (rhs of each op) are stored out-of-line, along with
1940 : // their operation's position. Note that the Nary operation expression's
1941 : // position has no meaning.
1942 : //
1943 : // So an nary add:
1944 : //
1945 : // expr + expr + expr + ...
1946 : //
1947 : // is stored as:
1948 : //
1949 : // (expr) [(+ expr), (+ expr), ...]
1950 : // '-.--' '-----------.-----------'
1951 : // first subsequent entry list
1952 :
1953 : Expression* first_;
1954 :
1955 : struct NaryOperationEntry {
1956 : Expression* expression;
1957 : int op_position;
1958 : NaryOperationEntry(Expression* e, int pos)
1959 3058156 : : expression(e), op_position(pos) {}
1960 : };
1961 : ZoneVector<NaryOperationEntry> subsequent_;
1962 :
1963 : class OperatorField
1964 : : public BitField<Token::Value, Expression::kNextBitFieldIndex, 7> {};
1965 : };
1966 :
1967 : class CountOperation final : public Expression {
1968 : public:
1969 : bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
1970 241291 : bool is_postfix() const { return !is_prefix(); }
1971 :
1972 : Token::Value op() const { return TokenField::decode(bit_field_); }
1973 :
1974 : Expression* expression() const { return expression_; }
1975 :
1976 : private:
1977 : friend class AstNodeFactory;
1978 :
1979 : CountOperation(Token::Value op, bool is_prefix, Expression* expr, int pos)
1980 316757 : : Expression(pos, kCountOperation), expression_(expr) {
1981 316757 : bit_field_ |= IsPrefixField::encode(is_prefix) | TokenField::encode(op);
1982 : }
1983 :
1984 : class IsPrefixField
1985 : : public BitField<bool, Expression::kNextBitFieldIndex, 1> {};
1986 : class TokenField : public BitField<Token::Value, IsPrefixField::kNext, 7> {};
1987 :
1988 : Expression* expression_;
1989 : };
1990 :
1991 :
1992 : class CompareOperation final : public Expression {
1993 : public:
1994 : Token::Value op() const { return OperatorField::decode(bit_field_); }
1995 : Expression* left() const { return left_; }
1996 : Expression* right() const { return right_; }
1997 :
1998 : // Match special cases.
1999 : bool IsLiteralCompareTypeof(Expression** expr, Literal** literal);
2000 : bool IsLiteralCompareUndefined(Expression** expr);
2001 : bool IsLiteralCompareNull(Expression** expr);
2002 :
2003 : private:
2004 : friend class AstNodeFactory;
2005 :
2006 : CompareOperation(Token::Value op, Expression* left, Expression* right,
2007 : int pos)
2008 983377 : : Expression(pos, kCompareOperation), left_(left), right_(right) {
2009 983377 : bit_field_ |= OperatorField::encode(op);
2010 : DCHECK(Token::IsCompareOp(op));
2011 : }
2012 :
2013 : Expression* left_;
2014 : Expression* right_;
2015 :
2016 : class OperatorField
2017 : : public BitField<Token::Value, Expression::kNextBitFieldIndex, 7> {};
2018 : };
2019 :
2020 :
2021 : class Spread final : public Expression {
2022 : public:
2023 : Expression* expression() const { return expression_; }
2024 :
2025 : int expression_position() const { return expr_pos_; }
2026 :
2027 : private:
2028 : friend class AstNodeFactory;
2029 :
2030 : Spread(Expression* expression, int pos, int expr_pos)
2031 : : Expression(pos, kSpread),
2032 : expr_pos_(expr_pos),
2033 56782 : expression_(expression) {}
2034 :
2035 : int expr_pos_;
2036 : Expression* expression_;
2037 : };
2038 :
2039 : // The StoreInArrayLiteral node corresponds to the StaInArrayLiteral bytecode.
2040 : // It is used in the rewriting of destructuring assignments that contain an
2041 : // array rest pattern.
2042 : class StoreInArrayLiteral final : public Expression {
2043 : public:
2044 : Expression* array() const { return array_; }
2045 : Expression* index() const { return index_; }
2046 : Expression* value() const { return value_; }
2047 :
2048 : private:
2049 : friend class AstNodeFactory;
2050 :
2051 : StoreInArrayLiteral(Expression* array, Expression* index, Expression* value,
2052 : int position)
2053 : : Expression(position, kStoreInArrayLiteral),
2054 : array_(array),
2055 : index_(index),
2056 : value_(value) {}
2057 :
2058 : Expression* array_;
2059 : Expression* index_;
2060 : Expression* value_;
2061 : };
2062 :
2063 : class Conditional final : public Expression {
2064 : public:
2065 : Expression* condition() const { return condition_; }
2066 : Expression* then_expression() const { return then_expression_; }
2067 : Expression* else_expression() const { return else_expression_; }
2068 :
2069 : private:
2070 : friend class AstNodeFactory;
2071 :
2072 : Conditional(Expression* condition, Expression* then_expression,
2073 : Expression* else_expression, int position)
2074 : : Expression(position, kConditional),
2075 : condition_(condition),
2076 : then_expression_(then_expression),
2077 64905 : else_expression_(else_expression) {}
2078 :
2079 : Expression* condition_;
2080 : Expression* then_expression_;
2081 : Expression* else_expression_;
2082 : };
2083 :
2084 : class Assignment : public Expression {
2085 : public:
2086 : Token::Value op() const { return TokenField::decode(bit_field_); }
2087 : Expression* target() const { return target_; }
2088 : Expression* value() const { return value_; }
2089 :
2090 : // The assignment was generated as part of block-scoped sloppy-mode
2091 : // function hoisting, see
2092 : // ES#sec-block-level-function-declarations-web-legacy-compatibility-semantics
2093 : LookupHoistingMode lookup_hoisting_mode() const {
2094 : return static_cast<LookupHoistingMode>(
2095 7541970 : LookupHoistingModeField::decode(bit_field_));
2096 : }
2097 : void set_lookup_hoisting_mode(LookupHoistingMode mode) {
2098 : bit_field_ =
2099 14944 : LookupHoistingModeField::update(bit_field_, static_cast<bool>(mode));
2100 : }
2101 :
2102 : protected:
2103 : Assignment(NodeType type, Token::Value op, Expression* target,
2104 : Expression* value, int pos);
2105 :
2106 : private:
2107 : friend class AstNodeFactory;
2108 :
2109 : class TokenField
2110 : : public BitField<Token::Value, Expression::kNextBitFieldIndex, 7> {};
2111 : class LookupHoistingModeField : public BitField<bool, TokenField::kNext, 1> {
2112 : };
2113 :
2114 : Expression* target_;
2115 : Expression* value_;
2116 : };
2117 :
2118 : class CompoundAssignment final : public Assignment {
2119 : public:
2120 : BinaryOperation* binary_operation() const { return binary_operation_; }
2121 :
2122 : private:
2123 : friend class AstNodeFactory;
2124 :
2125 : CompoundAssignment(Token::Value op, Expression* target, Expression* value,
2126 : int pos, BinaryOperation* binary_operation)
2127 : : Assignment(kCompoundAssignment, op, target, value, pos),
2128 98005 : binary_operation_(binary_operation) {}
2129 :
2130 : BinaryOperation* binary_operation_;
2131 : };
2132 :
2133 : // There are several types of Suspend node:
2134 : //
2135 : // Yield
2136 : // YieldStar
2137 : // Await
2138 : //
2139 : // Our Yield is different from the JS yield in that it "returns" its argument as
2140 : // is, without wrapping it in an iterator result object. Such wrapping, if
2141 : // desired, must be done beforehand (see the parser).
2142 : class Suspend : public Expression {
2143 : public:
2144 : // With {kNoControl}, the {Suspend} behaves like yield, except that it never
2145 : // throws and never causes the current generator to return. This is used to
2146 : // desugar yield*.
2147 : // TODO(caitp): remove once yield* desugaring for async generators is handled
2148 : // in BytecodeGenerator.
2149 : enum OnAbruptResume { kOnExceptionThrow, kNoControl };
2150 :
2151 : Expression* expression() const { return expression_; }
2152 : OnAbruptResume on_abrupt_resume() const {
2153 : return OnAbruptResumeField::decode(bit_field_);
2154 : }
2155 :
2156 : private:
2157 : friend class AstNodeFactory;
2158 : friend class Yield;
2159 : friend class YieldStar;
2160 : friend class Await;
2161 :
2162 : Suspend(NodeType node_type, Expression* expression, int pos,
2163 : OnAbruptResume on_abrupt_resume)
2164 156523 : : Expression(pos, node_type), expression_(expression) {
2165 104419 : bit_field_ |= OnAbruptResumeField::encode(on_abrupt_resume);
2166 : }
2167 :
2168 : Expression* expression_;
2169 :
2170 : class OnAbruptResumeField
2171 : : public BitField<OnAbruptResume, Expression::kNextBitFieldIndex, 1> {};
2172 : };
2173 :
2174 : class Yield final : public Suspend {
2175 : private:
2176 : friend class AstNodeFactory;
2177 : Yield(Expression* expression, int pos, OnAbruptResume on_abrupt_resume)
2178 : : Suspend(kYield, expression, pos, on_abrupt_resume) {}
2179 : };
2180 :
2181 : class YieldStar final : public Suspend {
2182 : private:
2183 : friend class AstNodeFactory;
2184 : YieldStar(Expression* expression, int pos)
2185 : : Suspend(kYieldStar, expression, pos,
2186 : Suspend::OnAbruptResume::kNoControl) {}
2187 : };
2188 :
2189 : class Await final : public Suspend {
2190 : private:
2191 : friend class AstNodeFactory;
2192 :
2193 : Await(Expression* expression, int pos)
2194 : : Suspend(kAwait, expression, pos, Suspend::kOnExceptionThrow) {}
2195 : };
2196 :
2197 : class Throw final : public Expression {
2198 : public:
2199 : Expression* exception() const { return exception_; }
2200 :
2201 : private:
2202 : friend class AstNodeFactory;
2203 :
2204 : Throw(Expression* exception, int pos)
2205 41969 : : Expression(pos, kThrow), exception_(exception) {}
2206 :
2207 : Expression* exception_;
2208 : };
2209 :
2210 :
2211 : class FunctionLiteral final : public Expression {
2212 : public:
2213 : enum FunctionType {
2214 : kAnonymousExpression,
2215 : kNamedExpression,
2216 : kDeclaration,
2217 : kAccessorOrMethod,
2218 : kWrapped,
2219 : };
2220 :
2221 : enum IdType { kIdTypeInvalid = -1, kIdTypeTopLevel = 0 };
2222 :
2223 : enum ParameterFlag : uint8_t {
2224 : kNoDuplicateParameters,
2225 : kHasDuplicateParameters
2226 : };
2227 : enum EagerCompileHint : uint8_t { kShouldEagerCompile, kShouldLazyCompile };
2228 :
2229 : // Empty handle means that the function does not have a shared name (i.e.
2230 : // the name will be set dynamically after creation of the function closure).
2231 : MaybeHandle<String> name() const {
2232 3564329 : return raw_name_ ? raw_name_->string() : MaybeHandle<String>();
2233 : }
2234 : Handle<String> name(Isolate* isolate) const;
2235 : bool has_shared_name() const { return raw_name_ != nullptr; }
2236 : const AstConsString* raw_name() const { return raw_name_; }
2237 761418 : void set_raw_name(const AstConsString* name) { raw_name_ = name; }
2238 : DeclarationScope* scope() const { return scope_; }
2239 : ZonePtrList<Statement>* body() { return &body_; }
2240 4283575 : void set_function_token_position(int pos) { function_token_position_ = pos; }
2241 : int function_token_position() const { return function_token_position_; }
2242 : int start_position() const;
2243 : int end_position() const;
2244 7128774 : bool is_declaration() const { return function_type() == kDeclaration; }
2245 3564425 : bool is_named_expression() const {
2246 3564345 : return function_type() == kNamedExpression;
2247 : }
2248 5161359 : bool is_anonymous_expression() const {
2249 3564343 : return function_type() == kAnonymousExpression;
2250 : }
2251 :
2252 218085 : void mark_as_iife() { bit_field_ = IIFEBit::update(bit_field_, true); }
2253 : bool is_iife() const { return IIFEBit::decode(bit_field_); }
2254 10125465 : bool is_toplevel() const {
2255 : return function_literal_id() == FunctionLiteral::kIdTypeTopLevel;
2256 : }
2257 7128778 : bool is_wrapped() const { return function_type() == kWrapped; }
2258 : LanguageMode language_mode() const;
2259 :
2260 : static bool NeedsHomeObject(Expression* expr);
2261 :
2262 : int expected_property_count() {
2263 : // Not valid for lazy functions.
2264 : DCHECK(ShouldEagerCompile());
2265 : return expected_property_count_;
2266 : }
2267 : int parameter_count() { return parameter_count_; }
2268 : int function_length() { return function_length_; }
2269 :
2270 : bool AllowsLazyCompilation();
2271 :
2272 2115864 : bool CanSuspend() {
2273 2115864 : if (suspend_count() > 0) {
2274 : DCHECK(IsResumableFunction(kind()));
2275 : return true;
2276 : }
2277 : return false;
2278 : }
2279 :
2280 : // Returns either name or inferred name as a cstring.
2281 : std::unique_ptr<char[]> GetDebugName() const;
2282 :
2283 2043568 : Handle<String> inferred_name() const {
2284 2043568 : if (!inferred_name_.is_null()) {
2285 : DCHECK_NULL(raw_inferred_name_);
2286 0 : return inferred_name_;
2287 : }
2288 2043568 : if (raw_inferred_name_ != nullptr) {
2289 : return raw_inferred_name_->string();
2290 : }
2291 0 : UNREACHABLE();
2292 : }
2293 : const AstConsString* raw_inferred_name() { return raw_inferred_name_; }
2294 :
2295 : // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2296 : void set_inferred_name(Handle<String> inferred_name);
2297 : void set_raw_inferred_name(const AstConsString* raw_inferred_name);
2298 :
2299 : bool pretenure() const { return Pretenure::decode(bit_field_); }
2300 167484 : void set_pretenure() { bit_field_ = Pretenure::update(bit_field_, true); }
2301 :
2302 : bool has_duplicate_parameters() const {
2303 : // Not valid for lazy functions.
2304 : DCHECK(ShouldEagerCompile());
2305 : return HasDuplicateParameters::decode(bit_field_);
2306 : }
2307 :
2308 : // This is used as a heuristic on when to eagerly compile a function
2309 : // literal. We consider the following constructs as hints that the
2310 : // function will be called immediately:
2311 : // - (function() { ... })();
2312 : // - var x = function() { ... }();
2313 : bool ShouldEagerCompile() const;
2314 : V8_EXPORT_PRIVATE void SetShouldEagerCompile();
2315 :
2316 : FunctionType function_type() const {
2317 : return FunctionTypeBits::decode(bit_field_);
2318 : }
2319 : FunctionKind kind() const;
2320 :
2321 : bool dont_optimize() {
2322 : return dont_optimize_reason() != BailoutReason::kNoReason;
2323 : }
2324 : BailoutReason dont_optimize_reason() {
2325 : return DontOptimizeReasonField::decode(bit_field_);
2326 : }
2327 : void set_dont_optimize_reason(BailoutReason reason) {
2328 : bit_field_ = DontOptimizeReasonField::update(bit_field_, reason);
2329 : }
2330 :
2331 : bool IsAnonymousFunctionDefinition() const {
2332 : return is_anonymous_expression();
2333 : }
2334 :
2335 : int suspend_count() { return suspend_count_; }
2336 6007093 : void set_suspend_count(int suspend_count) { suspend_count_ = suspend_count; }
2337 :
2338 3073436 : int return_position() {
2339 : return std::max(
2340 3073573 : start_position(),
2341 12294405 : end_position() - (HasBracesField::decode(bit_field_) ? 1 : 0));
2342 : }
2343 :
2344 : int function_literal_id() const { return function_literal_id_; }
2345 : void set_function_literal_id(int function_literal_id) {
2346 305 : function_literal_id_ = function_literal_id;
2347 : }
2348 :
2349 : void set_requires_instance_members_initializer(bool value) {
2350 1461572 : bit_field_ = RequiresInstanceMembersInitializer::update(bit_field_, value);
2351 : }
2352 : bool requires_instance_members_initializer() const {
2353 : return RequiresInstanceMembersInitializer::decode(bit_field_);
2354 : }
2355 :
2356 : ProducedPreparseData* produced_preparse_data() const {
2357 : return produced_preparse_data_;
2358 : }
2359 :
2360 : private:
2361 : friend class AstNodeFactory;
2362 :
2363 6133600 : FunctionLiteral(Zone* zone, const AstRawString* name,
2364 6133658 : AstValueFactory* ast_value_factory, DeclarationScope* scope,
2365 : const ScopedPtrList<Statement>& body,
2366 : int expected_property_count, int parameter_count,
2367 : int function_length, FunctionType function_type,
2368 : ParameterFlag has_duplicate_parameters,
2369 : EagerCompileHint eager_compile_hint, int position,
2370 : bool has_braces, int function_literal_id,
2371 : ProducedPreparseData* produced_preparse_data = nullptr)
2372 : : Expression(position, kFunctionLiteral),
2373 : expected_property_count_(expected_property_count),
2374 : parameter_count_(parameter_count),
2375 : function_length_(function_length),
2376 : function_token_position_(kNoSourcePosition),
2377 : suspend_count_(0),
2378 : function_literal_id_(function_literal_id),
2379 : raw_name_(name ? ast_value_factory->NewConsString(name) : nullptr),
2380 : scope_(scope),
2381 : body_(0, nullptr),
2382 : raw_inferred_name_(ast_value_factory->empty_cons_string()),
2383 18400916 : produced_preparse_data_(produced_preparse_data) {
2384 : bit_field_ |= FunctionTypeBits::encode(function_type) |
2385 6133658 : Pretenure::encode(false) |
2386 : HasDuplicateParameters::encode(has_duplicate_parameters ==
2387 6133658 : kHasDuplicateParameters) |
2388 : DontOptimizeReasonField::encode(BailoutReason::kNoReason) |
2389 6133658 : RequiresInstanceMembersInitializer::encode(false) |
2390 6133658 : HasBracesField::encode(has_braces) | IIFEBit::encode(false);
2391 6133658 : if (eager_compile_hint == kShouldEagerCompile) SetShouldEagerCompile();
2392 6133659 : body.CopyTo(&body_, zone);
2393 6133718 : }
2394 :
2395 : class FunctionTypeBits
2396 : : public BitField<FunctionType, Expression::kNextBitFieldIndex, 3> {};
2397 : class Pretenure : public BitField<bool, FunctionTypeBits::kNext, 1> {};
2398 : class HasDuplicateParameters : public BitField<bool, Pretenure::kNext, 1> {};
2399 : class DontOptimizeReasonField
2400 : : public BitField<BailoutReason, HasDuplicateParameters::kNext, 8> {};
2401 : class RequiresInstanceMembersInitializer
2402 : : public BitField<bool, DontOptimizeReasonField::kNext, 1> {};
2403 : class HasBracesField
2404 : : public BitField<bool, RequiresInstanceMembersInitializer::kNext, 1> {};
2405 : class IIFEBit : public BitField<bool, HasBracesField::kNext, 1> {};
2406 :
2407 : int expected_property_count_;
2408 : int parameter_count_;
2409 : int function_length_;
2410 : int function_token_position_;
2411 : int suspend_count_;
2412 : int function_literal_id_;
2413 :
2414 : const AstConsString* raw_name_;
2415 : DeclarationScope* scope_;
2416 : ZonePtrList<Statement> body_;
2417 : const AstConsString* raw_inferred_name_;
2418 : Handle<String> inferred_name_;
2419 : ProducedPreparseData* produced_preparse_data_;
2420 : };
2421 :
2422 : // Property is used for passing information
2423 : // about a class literal's properties from the parser to the code generator.
2424 : class ClassLiteralProperty final : public LiteralProperty {
2425 : public:
2426 : enum Kind : uint8_t { METHOD, GETTER, SETTER, FIELD };
2427 :
2428 : Kind kind() const { return kind_; }
2429 :
2430 : bool is_static() const { return is_static_; }
2431 :
2432 : bool is_private() const { return is_private_; }
2433 :
2434 : void set_computed_name_var(Variable* var) {
2435 : DCHECK_EQ(FIELD, kind());
2436 : DCHECK(!is_private());
2437 5210 : private_or_computed_name_var_ = var;
2438 : }
2439 :
2440 : Variable* computed_name_var() const {
2441 : DCHECK_EQ(FIELD, kind());
2442 : DCHECK(!is_private());
2443 : return private_or_computed_name_var_;
2444 : }
2445 :
2446 : void set_private_name_var(Variable* var) {
2447 : DCHECK_EQ(FIELD, kind());
2448 : DCHECK(is_private());
2449 9758 : private_or_computed_name_var_ = var;
2450 : }
2451 : Variable* private_name_var() const {
2452 : DCHECK_EQ(FIELD, kind());
2453 : DCHECK(is_private());
2454 : return private_or_computed_name_var_;
2455 : }
2456 :
2457 : private:
2458 : friend class AstNodeFactory;
2459 :
2460 : ClassLiteralProperty(Expression* key, Expression* value, Kind kind,
2461 : bool is_static, bool is_computed_name, bool is_private);
2462 :
2463 : Kind kind_;
2464 : bool is_static_;
2465 : bool is_private_;
2466 : Variable* private_or_computed_name_var_;
2467 : };
2468 :
2469 : class InitializeClassMembersStatement final : public Statement {
2470 : public:
2471 : typedef ClassLiteralProperty Property;
2472 :
2473 : ZonePtrList<Property>* fields() const { return fields_; }
2474 :
2475 : private:
2476 : friend class AstNodeFactory;
2477 :
2478 : InitializeClassMembersStatement(ZonePtrList<Property>* fields, int pos)
2479 26902 : : Statement(pos, kInitializeClassMembersStatement), fields_(fields) {}
2480 :
2481 : ZonePtrList<Property>* fields_;
2482 : };
2483 :
2484 : class ClassLiteral final : public Expression {
2485 : public:
2486 : typedef ClassLiteralProperty Property;
2487 :
2488 : Scope* scope() const { return scope_; }
2489 : Variable* class_variable() const { return class_variable_; }
2490 : Expression* extends() const { return extends_; }
2491 : FunctionLiteral* constructor() const { return constructor_; }
2492 : ZonePtrList<Property>* properties() const { return properties_; }
2493 38836 : int start_position() const { return position(); }
2494 : int end_position() const { return end_position_; }
2495 : bool has_name_static_property() const {
2496 : return HasNameStaticProperty::decode(bit_field_);
2497 : }
2498 : bool has_static_computed_names() const {
2499 : return HasStaticComputedNames::decode(bit_field_);
2500 : }
2501 :
2502 : bool is_anonymous_expression() const {
2503 : return IsAnonymousExpression::decode(bit_field_);
2504 : }
2505 3673 : bool IsAnonymousFunctionDefinition() const {
2506 : return is_anonymous_expression();
2507 : }
2508 :
2509 : FunctionLiteral* static_fields_initializer() const {
2510 : return static_fields_initializer_;
2511 : }
2512 :
2513 : FunctionLiteral* instance_members_initializer_function() const {
2514 : return instance_members_initializer_function_;
2515 : }
2516 :
2517 : private:
2518 : friend class AstNodeFactory;
2519 :
2520 : ClassLiteral(Scope* scope, Variable* class_variable, Expression* extends,
2521 : FunctionLiteral* constructor, ZonePtrList<Property>* properties,
2522 : FunctionLiteral* static_fields_initializer,
2523 : FunctionLiteral* instance_members_initializer_function,
2524 : int start_position, int end_position,
2525 : bool has_name_static_property, bool has_static_computed_names,
2526 : bool is_anonymous)
2527 : : Expression(start_position, kClassLiteral),
2528 : end_position_(end_position),
2529 : scope_(scope),
2530 : class_variable_(class_variable),
2531 : extends_(extends),
2532 : constructor_(constructor),
2533 : properties_(properties),
2534 : static_fields_initializer_(static_fields_initializer),
2535 : instance_members_initializer_function_(
2536 108050 : instance_members_initializer_function) {
2537 108050 : bit_field_ |= HasNameStaticProperty::encode(has_name_static_property) |
2538 : HasStaticComputedNames::encode(has_static_computed_names) |
2539 108050 : IsAnonymousExpression::encode(is_anonymous);
2540 : }
2541 :
2542 : int end_position_;
2543 : Scope* scope_;
2544 : Variable* class_variable_;
2545 : Expression* extends_;
2546 : FunctionLiteral* constructor_;
2547 : ZonePtrList<Property>* properties_;
2548 : FunctionLiteral* static_fields_initializer_;
2549 : FunctionLiteral* instance_members_initializer_function_;
2550 : class HasNameStaticProperty
2551 : : public BitField<bool, Expression::kNextBitFieldIndex, 1> {};
2552 : class HasStaticComputedNames
2553 : : public BitField<bool, HasNameStaticProperty::kNext, 1> {};
2554 : class IsAnonymousExpression
2555 : : public BitField<bool, HasStaticComputedNames::kNext, 1> {};
2556 : };
2557 :
2558 :
2559 : class NativeFunctionLiteral final : public Expression {
2560 : public:
2561 3522 : Handle<String> name() const { return name_->string(); }
2562 : const AstRawString* raw_name() const { return name_; }
2563 : v8::Extension* extension() const { return extension_; }
2564 :
2565 : private:
2566 : friend class AstNodeFactory;
2567 :
2568 : NativeFunctionLiteral(const AstRawString* name, v8::Extension* extension,
2569 : int pos)
2570 : : Expression(pos, kNativeFunctionLiteral),
2571 : name_(name),
2572 1761 : extension_(extension) {}
2573 :
2574 : const AstRawString* name_;
2575 : v8::Extension* extension_;
2576 : };
2577 :
2578 :
2579 : class ThisFunction final : public Expression {
2580 : private:
2581 : friend class AstNodeFactory;
2582 : explicit ThisFunction(int pos) : Expression(pos, kThisFunction) {}
2583 : };
2584 :
2585 :
2586 : class SuperPropertyReference final : public Expression {
2587 : public:
2588 : VariableProxy* this_var() const { return this_var_; }
2589 : Expression* home_object() const { return home_object_; }
2590 :
2591 : private:
2592 : friend class AstNodeFactory;
2593 :
2594 : SuperPropertyReference(VariableProxy* this_var, Expression* home_object,
2595 : int pos)
2596 : : Expression(pos, kSuperPropertyReference),
2597 : this_var_(this_var),
2598 2758 : home_object_(home_object) {
2599 : DCHECK(this_var->is_this());
2600 : DCHECK(home_object->IsProperty());
2601 : }
2602 :
2603 : VariableProxy* this_var_;
2604 : Expression* home_object_;
2605 : };
2606 :
2607 :
2608 : class SuperCallReference final : public Expression {
2609 : public:
2610 : VariableProxy* this_var() const { return this_var_; }
2611 : VariableProxy* new_target_var() const { return new_target_var_; }
2612 : VariableProxy* this_function_var() const { return this_function_var_; }
2613 :
2614 : private:
2615 : friend class AstNodeFactory;
2616 :
2617 : SuperCallReference(VariableProxy* this_var, VariableProxy* new_target_var,
2618 : VariableProxy* this_function_var, int pos)
2619 : : Expression(pos, kSuperCallReference),
2620 : this_var_(this_var),
2621 : new_target_var_(new_target_var),
2622 30619 : this_function_var_(this_function_var) {
2623 : DCHECK(this_var->is_this());
2624 : DCHECK(new_target_var->raw_name()->IsOneByteEqualTo(".new.target"));
2625 : DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2626 : }
2627 :
2628 : VariableProxy* this_var_;
2629 : VariableProxy* new_target_var_;
2630 : VariableProxy* this_function_var_;
2631 : };
2632 :
2633 : // This AST Node is used to represent a dynamic import call --
2634 : // import(argument).
2635 : class ImportCallExpression final : public Expression {
2636 : public:
2637 : Expression* argument() const { return argument_; }
2638 :
2639 : private:
2640 : friend class AstNodeFactory;
2641 :
2642 : ImportCallExpression(Expression* argument, int pos)
2643 3598 : : Expression(pos, kImportCallExpression), argument_(argument) {}
2644 :
2645 : Expression* argument_;
2646 : };
2647 :
2648 : // This class is produced when parsing the () in arrow functions without any
2649 : // arguments and is not actually a valid expression.
2650 : class EmptyParentheses final : public Expression {
2651 : private:
2652 : friend class AstNodeFactory;
2653 :
2654 : explicit EmptyParentheses(int pos) : Expression(pos, kEmptyParentheses) {
2655 : mark_parenthesized();
2656 : }
2657 : };
2658 :
2659 : // Represents the spec operation `GetTemplateObject(templateLiteral)`
2660 : // (defined at https://tc39.github.io/ecma262/#sec-gettemplateobject).
2661 : class GetTemplateObject final : public Expression {
2662 : public:
2663 : const ZonePtrList<const AstRawString>* cooked_strings() const {
2664 : return cooked_strings_;
2665 : }
2666 : const ZonePtrList<const AstRawString>* raw_strings() const {
2667 : return raw_strings_;
2668 : }
2669 :
2670 : Handle<TemplateObjectDescription> GetOrBuildDescription(Isolate* isolate);
2671 :
2672 : private:
2673 : friend class AstNodeFactory;
2674 :
2675 : GetTemplateObject(const ZonePtrList<const AstRawString>* cooked_strings,
2676 : const ZonePtrList<const AstRawString>* raw_strings, int pos)
2677 : : Expression(pos, kGetTemplateObject),
2678 : cooked_strings_(cooked_strings),
2679 7314 : raw_strings_(raw_strings) {}
2680 :
2681 : const ZonePtrList<const AstRawString>* cooked_strings_;
2682 : const ZonePtrList<const AstRawString>* raw_strings_;
2683 : };
2684 :
2685 : class TemplateLiteral final : public Expression {
2686 : public:
2687 : const ZonePtrList<const AstRawString>* string_parts() const {
2688 : return string_parts_;
2689 : }
2690 : const ZonePtrList<Expression>* substitutions() const {
2691 : return substitutions_;
2692 : }
2693 :
2694 : private:
2695 : friend class AstNodeFactory;
2696 : TemplateLiteral(const ZonePtrList<const AstRawString>* parts,
2697 : const ZonePtrList<Expression>* substitutions, int pos)
2698 : : Expression(pos, kTemplateLiteral),
2699 : string_parts_(parts),
2700 21117 : substitutions_(substitutions) {}
2701 :
2702 : const ZonePtrList<const AstRawString>* string_parts_;
2703 : const ZonePtrList<Expression>* substitutions_;
2704 : };
2705 :
2706 : // ----------------------------------------------------------------------------
2707 : // Basic visitor
2708 : // Sub-class should parametrize AstVisitor with itself, e.g.:
2709 : // class SpecificVisitor : public AstVisitor<SpecificVisitor> { ... }
2710 :
2711 : template <class Subclass>
2712 : class AstVisitor {
2713 : public:
2714 : void Visit(AstNode* node) { impl()->Visit(node); }
2715 :
2716 : void VisitDeclarations(Declaration::List* declarations) {
2717 : for (Declaration* decl : *declarations) Visit(decl);
2718 : }
2719 :
2720 : void VisitStatements(const ZonePtrList<Statement>* statements) {
2721 : for (int i = 0; i < statements->length(); i++) {
2722 : Statement* stmt = statements->at(i);
2723 : Visit(stmt);
2724 : if (stmt->IsJump()) break;
2725 : }
2726 : }
2727 :
2728 : void VisitExpressions(const ZonePtrList<Expression>* expressions) {
2729 : for (int i = 0; i < expressions->length(); i++) {
2730 : // The variable statement visiting code may pass null expressions
2731 : // to this code. Maybe this should be handled by introducing an
2732 : // undefined expression or literal? Revisit this code if this
2733 : // changes.
2734 : Expression* expression = expressions->at(i);
2735 : if (expression != nullptr) Visit(expression);
2736 : }
2737 : }
2738 :
2739 : protected:
2740 : Subclass* impl() { return static_cast<Subclass*>(this); }
2741 : };
2742 :
2743 : #define GENERATE_VISIT_CASE(NodeType) \
2744 : case AstNode::k##NodeType: \
2745 : return this->impl()->Visit##NodeType(static_cast<NodeType*>(node));
2746 :
2747 : #define GENERATE_FAILURE_CASE(NodeType) \
2748 : case AstNode::k##NodeType: \
2749 : UNREACHABLE();
2750 :
2751 : #define GENERATE_AST_VISITOR_SWITCH() \
2752 : switch (node->node_type()) { \
2753 : AST_NODE_LIST(GENERATE_VISIT_CASE) \
2754 : FAILURE_NODE_LIST(GENERATE_FAILURE_CASE) \
2755 : }
2756 :
2757 : #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
2758 : public: \
2759 : void VisitNoStackOverflowCheck(AstNode* node) { \
2760 : GENERATE_AST_VISITOR_SWITCH() \
2761 : } \
2762 : \
2763 : void Visit(AstNode* node) { \
2764 : if (CheckStackOverflow()) return; \
2765 : VisitNoStackOverflowCheck(node); \
2766 : } \
2767 : \
2768 : void SetStackOverflow() { stack_overflow_ = true; } \
2769 : void ClearStackOverflow() { stack_overflow_ = false; } \
2770 : bool HasStackOverflow() const { return stack_overflow_; } \
2771 : \
2772 : bool CheckStackOverflow() { \
2773 : if (stack_overflow_) return true; \
2774 : if (GetCurrentStackPosition() < stack_limit_) { \
2775 : stack_overflow_ = true; \
2776 : return true; \
2777 : } \
2778 : return false; \
2779 : } \
2780 : \
2781 : private: \
2782 : void InitializeAstVisitor(Isolate* isolate) { \
2783 : stack_limit_ = isolate->stack_guard()->real_climit(); \
2784 : stack_overflow_ = false; \
2785 : } \
2786 : \
2787 : void InitializeAstVisitor(uintptr_t stack_limit) { \
2788 : stack_limit_ = stack_limit; \
2789 : stack_overflow_ = false; \
2790 : } \
2791 : \
2792 : uintptr_t stack_limit_; \
2793 : bool stack_overflow_
2794 :
2795 : #define DEFINE_AST_VISITOR_MEMBERS_WITHOUT_STACKOVERFLOW() \
2796 : public: \
2797 : void Visit(AstNode* node) { GENERATE_AST_VISITOR_SWITCH() } \
2798 : \
2799 : private:
2800 :
2801 : // ----------------------------------------------------------------------------
2802 : // AstNode factory
2803 :
2804 : class AstNodeFactory final {
2805 : public:
2806 5167881 : AstNodeFactory(AstValueFactory* ast_value_factory, Zone* zone)
2807 : : zone_(zone),
2808 : ast_value_factory_(ast_value_factory),
2809 : empty_statement_(new (zone) class EmptyStatement()),
2810 15503620 : failure_expression_(new (zone) class FailureExpression()) {}
2811 :
2812 : AstNodeFactory* ast_node_factory() { return this; }
2813 : AstValueFactory* ast_value_factory() const { return ast_value_factory_; }
2814 :
2815 : VariableDeclaration* NewVariableDeclaration(int pos) {
2816 : return new (zone_) VariableDeclaration(pos);
2817 : }
2818 :
2819 : NestedVariableDeclaration* NewNestedVariableDeclaration(Scope* scope,
2820 : int pos) {
2821 : return new (zone_) NestedVariableDeclaration(scope, pos);
2822 : }
2823 :
2824 : FunctionDeclaration* NewFunctionDeclaration(FunctionLiteral* fun,
2825 : bool is_sloppy_block_function,
2826 : int pos) {
2827 : return new (zone_) FunctionDeclaration(fun, is_sloppy_block_function, pos);
2828 : }
2829 :
2830 595040 : Block* NewBlock(int capacity, bool ignore_completion_value) {
2831 1190087 : return new (zone_) Block(zone_, nullptr, capacity, ignore_completion_value);
2832 : }
2833 :
2834 10256459 : Block* NewBlock(bool ignore_completion_value,
2835 : ZonePtrList<const AstRawString>* labels) {
2836 : return labels != nullptr
2837 6690 : ? new (zone_) LabeledBlock(labels, ignore_completion_value)
2838 30762582 : : new (zone_) Block(labels, ignore_completion_value);
2839 : }
2840 :
2841 9285202 : Block* NewBlock(bool ignore_completion_value,
2842 9285202 : const ScopedPtrList<Statement>& statements) {
2843 18570404 : Block* result = NewBlock(ignore_completion_value, nullptr);
2844 9285088 : result->InitializeStatements(statements, zone_);
2845 9285102 : return result;
2846 : }
2847 :
2848 : #define STATEMENT_WITH_LABELS(NodeType) \
2849 : NodeType* New##NodeType(ZonePtrList<const AstRawString>* labels, \
2850 : ZonePtrList<const AstRawString>* own_labels, \
2851 : int pos) { \
2852 : return new (zone_) NodeType(labels, own_labels, pos); \
2853 : }
2854 : STATEMENT_WITH_LABELS(DoWhileStatement)
2855 : STATEMENT_WITH_LABELS(WhileStatement)
2856 609729 : STATEMENT_WITH_LABELS(ForStatement)
2857 : #undef STATEMENT_WITH_LABELS
2858 :
2859 14294 : SwitchStatement* NewSwitchStatement(ZonePtrList<const AstRawString>* labels,
2860 : Expression* tag, int pos) {
2861 42881 : return new (zone_) SwitchStatement(zone_, labels, tag, pos);
2862 : }
2863 :
2864 153456 : ForEachStatement* NewForEachStatement(
2865 : ForEachStatement::VisitMode visit_mode,
2866 : ZonePtrList<const AstRawString>* labels,
2867 : ZonePtrList<const AstRawString>* own_labels, int pos) {
2868 153456 : switch (visit_mode) {
2869 : case ForEachStatement::ENUMERATE: {
2870 47499 : return new (zone_) ForInStatement(labels, own_labels, pos);
2871 : }
2872 : case ForEachStatement::ITERATE: {
2873 : return new (zone_)
2874 105957 : ForOfStatement(labels, own_labels, pos, IteratorType::kNormal);
2875 : }
2876 : }
2877 0 : UNREACHABLE();
2878 : }
2879 :
2880 : ForOfStatement* NewForOfStatement(ZonePtrList<const AstRawString>* labels,
2881 : ZonePtrList<const AstRawString>* own_labels,
2882 : int pos, IteratorType type) {
2883 : return new (zone_) ForOfStatement(labels, own_labels, pos, type);
2884 : }
2885 :
2886 40753 : ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
2887 81508 : return new (zone_) ExpressionStatement(expression, pos);
2888 : }
2889 :
2890 : ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
2891 : return new (zone_) ContinueStatement(target, pos);
2892 : }
2893 :
2894 : BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
2895 : return new (zone_) BreakStatement(target, pos);
2896 : }
2897 :
2898 : ReturnStatement* NewReturnStatement(Expression* expression, int pos,
2899 : int end_position = kNoSourcePosition) {
2900 : return new (zone_) ReturnStatement(expression, ReturnStatement::kNormal,
2901 : pos, end_position);
2902 : }
2903 :
2904 : ReturnStatement* NewAsyncReturnStatement(
2905 : Expression* expression, int pos, int end_position = kNoSourcePosition) {
2906 : return new (zone_) ReturnStatement(
2907 : expression, ReturnStatement::kAsyncReturn, pos, end_position);
2908 : }
2909 :
2910 : WithStatement* NewWithStatement(Scope* scope,
2911 : Expression* expression,
2912 : Statement* statement,
2913 : int pos) {
2914 : return new (zone_) WithStatement(scope, expression, statement, pos);
2915 : }
2916 :
2917 : IfStatement* NewIfStatement(Expression* condition, Statement* then_statement,
2918 : Statement* else_statement, int pos) {
2919 : return new (zone_)
2920 : IfStatement(condition, then_statement, else_statement, pos);
2921 : }
2922 :
2923 : TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
2924 : Block* catch_block, int pos) {
2925 : return new (zone_) TryCatchStatement(try_block, scope, catch_block,
2926 : HandlerTable::CAUGHT, pos);
2927 : }
2928 :
2929 : TryCatchStatement* NewTryCatchStatementForReThrow(Block* try_block,
2930 : Scope* scope,
2931 : Block* catch_block,
2932 : int pos) {
2933 : return new (zone_) TryCatchStatement(try_block, scope, catch_block,
2934 : HandlerTable::UNCAUGHT, pos);
2935 : }
2936 :
2937 : TryCatchStatement* NewTryCatchStatementForDesugaring(Block* try_block,
2938 : Scope* scope,
2939 : Block* catch_block,
2940 : int pos) {
2941 : return new (zone_) TryCatchStatement(try_block, scope, catch_block,
2942 : HandlerTable::DESUGARING, pos);
2943 : }
2944 :
2945 : TryCatchStatement* NewTryCatchStatementForAsyncAwait(Block* try_block,
2946 : Scope* scope,
2947 : Block* catch_block,
2948 : int pos) {
2949 : return new (zone_) TryCatchStatement(try_block, scope, catch_block,
2950 : HandlerTable::ASYNC_AWAIT, pos);
2951 : }
2952 :
2953 : TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
2954 : Block* finally_block, int pos) {
2955 : return new (zone_) TryFinallyStatement(try_block, finally_block, pos);
2956 : }
2957 :
2958 : DebuggerStatement* NewDebuggerStatement(int pos) {
2959 : return new (zone_) DebuggerStatement(pos);
2960 : }
2961 :
2962 : class EmptyStatement* EmptyStatement() {
2963 : return empty_statement_;
2964 : }
2965 :
2966 : class FailureExpression* FailureExpression() {
2967 : return failure_expression_;
2968 : }
2969 :
2970 9100 : SloppyBlockFunctionStatement* NewSloppyBlockFunctionStatement(int pos) {
2971 9100 : return new (zone_) SloppyBlockFunctionStatement(pos, EmptyStatement());
2972 : }
2973 :
2974 88679 : CaseClause* NewCaseClause(Expression* label,
2975 : const ScopedPtrList<Statement>& statements) {
2976 177358 : return new (zone_) CaseClause(zone_, label, statements);
2977 : }
2978 :
2979 : Literal* NewStringLiteral(const AstRawString* string, int pos) {
2980 : DCHECK_NOT_NULL(string);
2981 : return new (zone_) Literal(string, pos);
2982 : }
2983 :
2984 : // A JavaScript symbol (ECMA-262 edition 6).
2985 : Literal* NewSymbolLiteral(AstSymbol symbol, int pos) {
2986 : return new (zone_) Literal(symbol, pos);
2987 : }
2988 :
2989 : Literal* NewNumberLiteral(double number, int pos);
2990 :
2991 : Literal* NewSmiLiteral(int number, int pos) {
2992 : return new (zone_) Literal(number, pos);
2993 : }
2994 :
2995 : Literal* NewBigIntLiteral(AstBigInt bigint, int pos) {
2996 : return new (zone_) Literal(bigint, pos);
2997 : }
2998 :
2999 : Literal* NewBooleanLiteral(bool b, int pos) {
3000 : return new (zone_) Literal(b, pos);
3001 : }
3002 :
3003 : Literal* NewNullLiteral(int pos) {
3004 : return new (zone_) Literal(Literal::kNull, pos);
3005 : }
3006 :
3007 : Literal* NewUndefinedLiteral(int pos) {
3008 : return new (zone_) Literal(Literal::kUndefined, pos);
3009 : }
3010 :
3011 : Literal* NewTheHoleLiteral() {
3012 : return new (zone_) Literal(Literal::kTheHole, kNoSourcePosition);
3013 : }
3014 :
3015 580382 : ObjectLiteral* NewObjectLiteral(
3016 : const ScopedPtrList<ObjectLiteral::Property>& properties,
3017 : uint32_t boilerplate_properties, int pos, bool has_rest_property) {
3018 : return new (zone_) ObjectLiteral(zone_, properties, boilerplate_properties,
3019 1160791 : pos, has_rest_property);
3020 : }
3021 :
3022 : ObjectLiteral::Property* NewObjectLiteralProperty(
3023 : Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3024 : bool is_computed_name) {
3025 : return new (zone_)
3026 130689 : ObjectLiteral::Property(key, value, kind, is_computed_name);
3027 : }
3028 :
3029 2907816 : ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3030 : Expression* value,
3031 : bool is_computed_name) {
3032 : return new (zone_) ObjectLiteral::Property(ast_value_factory_, key, value,
3033 5815646 : is_computed_name);
3034 : }
3035 :
3036 : RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern, int flags,
3037 : int pos) {
3038 : return new (zone_) RegExpLiteral(pattern, flags, pos);
3039 : }
3040 :
3041 : ArrayLiteral* NewArrayLiteral(const ScopedPtrList<Expression>& values,
3042 : int pos) {
3043 : return new (zone_) ArrayLiteral(zone_, values, -1, pos);
3044 : }
3045 :
3046 775412 : ArrayLiteral* NewArrayLiteral(const ScopedPtrList<Expression>& values,
3047 : int first_spread_index, int pos) {
3048 1550830 : return new (zone_) ArrayLiteral(zone_, values, first_spread_index, pos);
3049 : }
3050 :
3051 : VariableProxy* NewVariableProxy(Variable* var,
3052 : int start_position = kNoSourcePosition) {
3053 2884631 : return new (zone_) VariableProxy(var, start_position);
3054 : }
3055 :
3056 78858095 : VariableProxy* NewVariableProxy(const AstRawString* name,
3057 : VariableKind variable_kind,
3058 : int start_position = kNoSourcePosition) {
3059 : DCHECK_NOT_NULL(name);
3060 157713888 : return new (zone_) VariableProxy(name, variable_kind, start_position);
3061 : }
3062 :
3063 : // Recreates the VariableProxy in this Zone.
3064 : VariableProxy* CopyVariableProxy(VariableProxy* proxy) {
3065 5679844 : return new (zone_) VariableProxy(proxy);
3066 : }
3067 :
3068 : Variable* CopyVariable(Variable* variable) {
3069 821013 : return new (zone_) Variable(variable);
3070 : }
3071 :
3072 : Property* NewProperty(Expression* obj, Expression* key, int pos) {
3073 : return new (zone_) Property(obj, key, pos);
3074 : }
3075 :
3076 : ResolvedProperty* NewResolvedProperty(VariableProxy* obj,
3077 : VariableProxy* property,
3078 : int pos = kNoSourcePosition) {
3079 : return new (zone_) ResolvedProperty(obj, property, pos);
3080 : }
3081 :
3082 6545232 : Call* NewCall(Expression* expression,
3083 : const ScopedPtrList<Expression>& arguments, int pos,
3084 : Call::PossiblyEval possibly_eval = Call::NOT_EVAL) {
3085 13090433 : return new (zone_) Call(zone_, expression, arguments, pos, possibly_eval);
3086 : }
3087 :
3088 7314 : Call* NewTaggedTemplate(Expression* expression,
3089 : const ScopedPtrList<Expression>& arguments, int pos) {
3090 : return new (zone_)
3091 14628 : Call(zone_, expression, arguments, pos, Call::TaggedTemplateTag::kTrue);
3092 : }
3093 :
3094 172899 : CallNew* NewCallNew(Expression* expression,
3095 : const ScopedPtrList<Expression>& arguments, int pos) {
3096 345803 : return new (zone_) CallNew(zone_, expression, arguments, pos);
3097 : }
3098 :
3099 96635 : CallRuntime* NewCallRuntime(Runtime::FunctionId id,
3100 : const ScopedPtrList<Expression>& arguments,
3101 : int pos) {
3102 : return new (zone_)
3103 193270 : CallRuntime(zone_, Runtime::FunctionForId(id), arguments, pos);
3104 : }
3105 :
3106 54640 : CallRuntime* NewCallRuntime(const Runtime::Function* function,
3107 : const ScopedPtrList<Expression>& arguments,
3108 : int pos) {
3109 109282 : return new (zone_) CallRuntime(zone_, function, arguments, pos);
3110 : }
3111 :
3112 722 : CallRuntime* NewCallRuntime(int context_index,
3113 : const ScopedPtrList<Expression>& arguments,
3114 : int pos) {
3115 1444 : return new (zone_) CallRuntime(zone_, context_index, arguments, pos);
3116 : }
3117 :
3118 : UnaryOperation* NewUnaryOperation(Token::Value op,
3119 : Expression* expression,
3120 : int pos) {
3121 : return new (zone_) UnaryOperation(op, expression, pos);
3122 : }
3123 :
3124 : BinaryOperation* NewBinaryOperation(Token::Value op,
3125 : Expression* left,
3126 : Expression* right,
3127 : int pos) {
3128 : return new (zone_) BinaryOperation(op, left, right, pos);
3129 : }
3130 :
3131 123456 : NaryOperation* NewNaryOperation(Token::Value op, Expression* first,
3132 : size_t initial_subsequent_size) {
3133 370369 : return new (zone_) NaryOperation(zone_, op, first, initial_subsequent_size);
3134 : }
3135 :
3136 215952 : CountOperation* NewCountOperation(Token::Value op,
3137 : bool is_prefix,
3138 : Expression* expr,
3139 : int pos) {
3140 431904 : return new (zone_) CountOperation(op, is_prefix, expr, pos);
3141 : }
3142 :
3143 : CompareOperation* NewCompareOperation(Token::Value op,
3144 : Expression* left,
3145 : Expression* right,
3146 : int pos) {
3147 : return new (zone_) CompareOperation(op, left, right, pos);
3148 : }
3149 :
3150 : Spread* NewSpread(Expression* expression, int pos, int expr_pos) {
3151 : return new (zone_) Spread(expression, pos, expr_pos);
3152 : }
3153 :
3154 : StoreInArrayLiteral* NewStoreInArrayLiteral(Expression* array,
3155 : Expression* index,
3156 : Expression* value, int pos) {
3157 : return new (zone_) StoreInArrayLiteral(array, index, value, pos);
3158 : }
3159 :
3160 : Conditional* NewConditional(Expression* condition,
3161 : Expression* then_expression,
3162 : Expression* else_expression,
3163 : int position) {
3164 : return new (zone_)
3165 : Conditional(condition, then_expression, else_expression, position);
3166 : }
3167 :
3168 14351375 : Assignment* NewAssignment(Token::Value op,
3169 : Expression* target,
3170 : Expression* value,
3171 98005 : int pos) {
3172 : DCHECK(Token::IsAssignmentOp(op));
3173 :
3174 21209723 : if (op != Token::INIT && target->IsVariableProxy()) {
3175 2802548 : target->AsVariableProxy()->set_is_assigned();
3176 : }
3177 :
3178 14351433 : if (op == Token::ASSIGN || op == Token::INIT) {
3179 : return new (zone_)
3180 28506831 : Assignment(AstNode::kAssignment, op, target, value, pos);
3181 : } else {
3182 : return new (zone_) CompoundAssignment(
3183 : op, target, value, pos,
3184 : NewBinaryOperation(Token::BinaryOpForAssignment(op), target, value,
3185 294016 : pos + 1));
3186 : }
3187 : }
3188 :
3189 102199 : Suspend* NewYield(Expression* expression, int pos,
3190 5419 : Suspend::OnAbruptResume on_abrupt_resume) {
3191 102199 : if (!expression) expression = NewUndefinedLiteral(pos);
3192 204399 : return new (zone_) Yield(expression, pos, on_abrupt_resume);
3193 : }
3194 :
3195 : YieldStar* NewYieldStar(Expression* expression, int pos) {
3196 : return new (zone_) YieldStar(expression, pos);
3197 : }
3198 :
3199 52104 : Await* NewAwait(Expression* expression, int pos) {
3200 52104 : if (!expression) expression = NewUndefinedLiteral(pos);
3201 104208 : return new (zone_) Await(expression, pos);
3202 : }
3203 :
3204 40750 : Throw* NewThrow(Expression* exception, int pos) {
3205 81500 : return new (zone_) Throw(exception, pos);
3206 : }
3207 :
3208 4410225 : FunctionLiteral* NewFunctionLiteral(
3209 : const AstRawString* name, DeclarationScope* scope,
3210 : const ScopedPtrList<Statement>& body, int expected_property_count,
3211 : int parameter_count, int function_length,
3212 : FunctionLiteral::ParameterFlag has_duplicate_parameters,
3213 : FunctionLiteral::FunctionType function_type,
3214 : FunctionLiteral::EagerCompileHint eager_compile_hint, int position,
3215 : bool has_braces, int function_literal_id,
3216 : ProducedPreparseData* produced_preparse_data = nullptr) {
3217 : return new (zone_) FunctionLiteral(
3218 : zone_, name, ast_value_factory_, scope, body, expected_property_count,
3219 : parameter_count, function_length, function_type,
3220 : has_duplicate_parameters, eager_compile_hint, position, has_braces,
3221 8820467 : function_literal_id, produced_preparse_data);
3222 : }
3223 :
3224 : // Creates a FunctionLiteral representing a top-level script, the
3225 : // result of an eval (top-level or otherwise), or the result of calling
3226 : // the Function constructor.
3227 1723331 : FunctionLiteral* NewScriptOrEvalFunctionLiteral(
3228 : DeclarationScope* scope, const ScopedPtrList<Statement>& body,
3229 : int expected_property_count, int parameter_count) {
3230 : return new (zone_) FunctionLiteral(
3231 1723331 : zone_, ast_value_factory_->empty_string(), ast_value_factory_, scope,
3232 : body, expected_property_count, parameter_count, parameter_count,
3233 : FunctionLiteral::kAnonymousExpression,
3234 : FunctionLiteral::kNoDuplicateParameters,
3235 : FunctionLiteral::kShouldLazyCompile, 0, /* has_braces */ false,
3236 3446718 : FunctionLiteral::kIdTypeTopLevel);
3237 : }
3238 :
3239 : ClassLiteral::Property* NewClassLiteralProperty(
3240 : Expression* key, Expression* value, ClassLiteralProperty::Kind kind,
3241 : bool is_static, bool is_computed_name, bool is_private) {
3242 : return new (zone_) ClassLiteral::Property(key, value, kind, is_static,
3243 437653 : is_computed_name, is_private);
3244 : }
3245 :
3246 108051 : ClassLiteral* NewClassLiteral(
3247 : Scope* scope, Variable* variable, Expression* extends,
3248 : FunctionLiteral* constructor,
3249 : ZonePtrList<ClassLiteral::Property>* properties,
3250 : FunctionLiteral* static_fields_initializer,
3251 : FunctionLiteral* instance_members_initializer_function,
3252 : int start_position, int end_position, bool has_name_static_property,
3253 : bool has_static_computed_names, bool is_anonymous) {
3254 : return new (zone_) ClassLiteral(
3255 : scope, variable, extends, constructor, properties,
3256 : static_fields_initializer, instance_members_initializer_function,
3257 : start_position, end_position, has_name_static_property,
3258 216101 : has_static_computed_names, is_anonymous);
3259 : }
3260 :
3261 : NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3262 : v8::Extension* extension,
3263 : int pos) {
3264 : return new (zone_) NativeFunctionLiteral(name, extension, pos);
3265 : }
3266 :
3267 : DoExpression* NewDoExpression(Block* block, Variable* result_var, int pos) {
3268 : VariableProxy* result = NewVariableProxy(result_var, pos);
3269 : return new (zone_) DoExpression(block, result, pos);
3270 : }
3271 :
3272 : ThisFunction* NewThisFunction(int pos) {
3273 : return new (zone_) ThisFunction(pos);
3274 : }
3275 :
3276 : SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3277 : Expression* home_object,
3278 : int pos) {
3279 : return new (zone_) SuperPropertyReference(this_var, home_object, pos);
3280 : }
3281 :
3282 : SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3283 : VariableProxy* new_target_var,
3284 : VariableProxy* this_function_var,
3285 : int pos) {
3286 : return new (zone_)
3287 : SuperCallReference(this_var, new_target_var, this_function_var, pos);
3288 : }
3289 :
3290 : EmptyParentheses* NewEmptyParentheses(int pos) {
3291 : return new (zone_) EmptyParentheses(pos);
3292 : }
3293 :
3294 : GetTemplateObject* NewGetTemplateObject(
3295 : const ZonePtrList<const AstRawString>* cooked_strings,
3296 : const ZonePtrList<const AstRawString>* raw_strings, int pos) {
3297 : return new (zone_) GetTemplateObject(cooked_strings, raw_strings, pos);
3298 : }
3299 :
3300 : TemplateLiteral* NewTemplateLiteral(
3301 : const ZonePtrList<const AstRawString>* string_parts,
3302 : const ZonePtrList<Expression>* substitutions, int pos) {
3303 : return new (zone_) TemplateLiteral(string_parts, substitutions, pos);
3304 : }
3305 :
3306 : ImportCallExpression* NewImportCallExpression(Expression* args, int pos) {
3307 : return new (zone_) ImportCallExpression(args, pos);
3308 : }
3309 :
3310 : InitializeClassMembersStatement* NewInitializeClassMembersStatement(
3311 : ZonePtrList<ClassLiteral::Property>* args, int pos) {
3312 : return new (zone_) InitializeClassMembersStatement(args, pos);
3313 : }
3314 :
3315 : Zone* zone() const { return zone_; }
3316 :
3317 : private:
3318 : // This zone may be deallocated upon returning from parsing a function body
3319 : // which we can guarantee is not going to be compiled or have its AST
3320 : // inspected.
3321 : // See ParseFunctionLiteral in parser.cc for preconditions.
3322 : Zone* zone_;
3323 : AstValueFactory* ast_value_factory_;
3324 : class EmptyStatement* empty_statement_;
3325 : class FailureExpression* failure_expression_;
3326 : };
3327 :
3328 :
3329 : // Type testing & conversion functions overridden by concrete subclasses.
3330 : // Inline functions for AstNode.
3331 :
3332 : #define DECLARE_NODE_FUNCTIONS(type) \
3333 : bool AstNode::Is##type() const { return node_type() == AstNode::k##type; } \
3334 : type* AstNode::As##type() { \
3335 : return node_type() == AstNode::k##type ? reinterpret_cast<type*>(this) \
3336 : : nullptr; \
3337 : } \
3338 : const type* AstNode::As##type() const { \
3339 : return node_type() == AstNode::k##type \
3340 : ? reinterpret_cast<const type*>(this) \
3341 : : nullptr; \
3342 : }
3343 409809063 : AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
3344 17885897 : FAILURE_NODE_LIST(DECLARE_NODE_FUNCTIONS)
3345 : #undef DECLARE_NODE_FUNCTIONS
3346 :
3347 : } // namespace internal
3348 : } // namespace v8
3349 :
3350 : #endif // V8_AST_AST_H_
|