blob: bee0bab47e5640cbcf43c89e3739993d8fdfac73 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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 "src/assembler.h"
9#include "src/ast/ast-value-factory.h"
10#include "src/ast/modules.h"
11#include "src/ast/variables.h"
12#include "src/bailout-reason.h"
13#include "src/base/flags.h"
14#include "src/base/smart-pointers.h"
15#include "src/factory.h"
16#include "src/isolate.h"
17#include "src/list.h"
18#include "src/parsing/token.h"
19#include "src/runtime/runtime.h"
20#include "src/small-pointer-list.h"
21#include "src/types.h"
22#include "src/utils.h"
23
24namespace v8 {
25namespace internal {
26
27// The abstract syntax tree is an intermediate, light-weight
28// representation of the parsed JavaScript code suitable for
29// compilation to native code.
30
31// Nodes are allocated in a separate zone, which allows faster
32// allocation and constant-time deallocation of the entire syntax
33// tree.
34
35
36// ----------------------------------------------------------------------------
37// Nodes of the abstract syntax tree. Only concrete classes are
38// enumerated here.
39
40#define DECLARATION_NODE_LIST(V) \
41 V(VariableDeclaration) \
42 V(FunctionDeclaration) \
43 V(ImportDeclaration) \
44 V(ExportDeclaration)
45
46#define STATEMENT_NODE_LIST(V) \
47 V(Block) \
48 V(ExpressionStatement) \
49 V(EmptyStatement) \
50 V(SloppyBlockFunctionStatement) \
51 V(IfStatement) \
52 V(ContinueStatement) \
53 V(BreakStatement) \
54 V(ReturnStatement) \
55 V(WithStatement) \
56 V(SwitchStatement) \
57 V(DoWhileStatement) \
58 V(WhileStatement) \
59 V(ForStatement) \
60 V(ForInStatement) \
61 V(ForOfStatement) \
62 V(TryCatchStatement) \
63 V(TryFinallyStatement) \
64 V(DebuggerStatement)
65
66#define EXPRESSION_NODE_LIST(V) \
67 V(FunctionLiteral) \
68 V(ClassLiteral) \
69 V(NativeFunctionLiteral) \
70 V(Conditional) \
71 V(VariableProxy) \
72 V(Literal) \
73 V(RegExpLiteral) \
74 V(ObjectLiteral) \
75 V(ArrayLiteral) \
76 V(Assignment) \
77 V(Yield) \
78 V(Throw) \
79 V(Property) \
80 V(Call) \
81 V(CallNew) \
82 V(CallRuntime) \
83 V(UnaryOperation) \
84 V(CountOperation) \
85 V(BinaryOperation) \
86 V(CompareOperation) \
87 V(Spread) \
88 V(ThisFunction) \
89 V(SuperPropertyReference) \
90 V(SuperCallReference) \
91 V(CaseClause) \
92 V(EmptyParentheses) \
93 V(DoExpression) \
Ben Murdoch097c5b22016-05-18 11:27:45 +010094 V(RewritableExpression)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000095
96#define AST_NODE_LIST(V) \
97 DECLARATION_NODE_LIST(V) \
98 STATEMENT_NODE_LIST(V) \
99 EXPRESSION_NODE_LIST(V)
100
101// Forward declarations
102class AstNodeFactory;
103class AstVisitor;
104class Declaration;
105class Module;
106class BreakableStatement;
107class Expression;
108class IterationStatement;
109class MaterializedLiteral;
110class Statement;
111class TypeFeedbackOracle;
112
113#define DEF_FORWARD_DECLARATION(type) class type;
114AST_NODE_LIST(DEF_FORWARD_DECLARATION)
115#undef DEF_FORWARD_DECLARATION
116
117
118// Typedef only introduced to avoid unreadable code.
119typedef ZoneList<Handle<String>> ZoneStringList;
120typedef ZoneList<Handle<Object>> ZoneObjectList;
121
122
123#define DECLARE_NODE_TYPE(type) \
124 void Accept(AstVisitor* v) override; \
125 AstNode::NodeType node_type() const final { return AstNode::k##type; } \
126 friend class AstNodeFactory;
127
128
129class FeedbackVectorSlotCache {
130 public:
131 explicit FeedbackVectorSlotCache(Zone* zone)
132 : zone_(zone),
133 hash_map_(HashMap::PointersMatch, ZoneHashMap::kDefaultHashMapCapacity,
134 ZoneAllocationPolicy(zone)) {}
135
136 void Put(Variable* variable, FeedbackVectorSlot slot) {
137 ZoneHashMap::Entry* entry = hash_map_.LookupOrInsert(
138 variable, ComputePointerHash(variable), ZoneAllocationPolicy(zone_));
139 entry->value = reinterpret_cast<void*>(slot.ToInt());
140 }
141
142 ZoneHashMap::Entry* Get(Variable* variable) const {
143 return hash_map_.Lookup(variable, ComputePointerHash(variable));
144 }
145
146 private:
147 Zone* zone_;
148 ZoneHashMap hash_map_;
149};
150
151
152class AstProperties final BASE_EMBEDDED {
153 public:
154 enum Flag {
155 kNoFlags = 0,
156 kDontSelfOptimize = 1 << 0,
157 kDontCrankshaft = 1 << 1
158 };
159
160 typedef base::Flags<Flag> Flags;
161
162 explicit AstProperties(Zone* zone) : node_count_(0), spec_(zone) {}
163
164 Flags& flags() { return flags_; }
165 Flags flags() const { return flags_; }
166 int node_count() { return node_count_; }
167 void add_node_count(int count) { node_count_ += count; }
168
169 const FeedbackVectorSpec* get_spec() const { return &spec_; }
170 FeedbackVectorSpec* get_spec() { return &spec_; }
171
172 private:
173 Flags flags_;
174 int node_count_;
175 FeedbackVectorSpec spec_;
176};
177
178DEFINE_OPERATORS_FOR_FLAGS(AstProperties::Flags)
179
180
181class AstNode: public ZoneObject {
182 public:
183#define DECLARE_TYPE_ENUM(type) k##type,
184 enum NodeType {
185 AST_NODE_LIST(DECLARE_TYPE_ENUM)
186 kInvalid = -1
187 };
188#undef DECLARE_TYPE_ENUM
189
190 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
191
192 explicit AstNode(int position): position_(position) {}
193 virtual ~AstNode() {}
194
195 virtual void Accept(AstVisitor* v) = 0;
196 virtual NodeType node_type() const = 0;
197 int position() const { return position_; }
198
Ben Murdoch097c5b22016-05-18 11:27:45 +0100199#ifdef DEBUG
200 void PrettyPrint(Isolate* isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100201 void Print(Isolate* isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100202#endif // DEBUG
203
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000204 // Type testing & conversion functions overridden by concrete subclasses.
205#define DECLARE_NODE_FUNCTIONS(type) \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100206 V8_INLINE bool Is##type() const; \
207 V8_INLINE type* As##type(); \
208 V8_INLINE const type* As##type() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000209 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
210#undef DECLARE_NODE_FUNCTIONS
211
212 virtual BreakableStatement* AsBreakableStatement() { return NULL; }
213 virtual IterationStatement* AsIterationStatement() { return NULL; }
214 virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; }
215
216 // The interface for feedback slots, with default no-op implementations for
217 // node types which don't actually have this. Note that this is conceptually
218 // not really nice, but multiple inheritance would introduce yet another
219 // vtable entry per node, something we don't want for space reasons.
220 virtual void AssignFeedbackVectorSlots(Isolate* isolate,
221 FeedbackVectorSpec* spec,
222 FeedbackVectorSlotCache* cache) {}
223
224 private:
225 // Hidden to prevent accidental usage. It would have to load the
226 // current zone from the TLS.
227 void* operator new(size_t size);
228
229 friend class CaseClause; // Generates AST IDs.
230
231 int position_;
232};
233
234
235class Statement : public AstNode {
236 public:
237 explicit Statement(Zone* zone, int position) : AstNode(position) {}
238
239 bool IsEmpty() { return AsEmptyStatement() != NULL; }
240 virtual bool IsJump() const { return false; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000241};
242
243
244class SmallMapList final {
245 public:
246 SmallMapList() {}
247 SmallMapList(int capacity, Zone* zone) : list_(capacity, zone) {}
248
249 void Reserve(int capacity, Zone* zone) { list_.Reserve(capacity, zone); }
250 void Clear() { list_.Clear(); }
251 void Sort() { list_.Sort(); }
252
253 bool is_empty() const { return list_.is_empty(); }
254 int length() const { return list_.length(); }
255
256 void AddMapIfMissing(Handle<Map> map, Zone* zone) {
257 if (!Map::TryUpdate(map).ToHandle(&map)) return;
258 for (int i = 0; i < length(); ++i) {
259 if (at(i).is_identical_to(map)) return;
260 }
261 Add(map, zone);
262 }
263
264 void FilterForPossibleTransitions(Map* root_map) {
265 for (int i = list_.length() - 1; i >= 0; i--) {
266 if (at(i)->FindRootMap() != root_map) {
267 list_.RemoveElement(list_.at(i));
268 }
269 }
270 }
271
272 void Add(Handle<Map> handle, Zone* zone) {
273 list_.Add(handle.location(), zone);
274 }
275
276 Handle<Map> at(int i) const {
277 return Handle<Map>(list_.at(i));
278 }
279
280 Handle<Map> first() const { return at(0); }
281 Handle<Map> last() const { return at(length() - 1); }
282
283 private:
284 // The list stores pointers to Map*, that is Map**, so it's GC safe.
285 SmallPointerList<Map*> list_;
286
287 DISALLOW_COPY_AND_ASSIGN(SmallMapList);
288};
289
290
291class Expression : public AstNode {
292 public:
293 enum Context {
294 // Not assigned a context yet, or else will not be visited during
295 // code generation.
296 kUninitialized,
297 // Evaluated for its side effects.
298 kEffect,
299 // Evaluated for its value (and side effects).
300 kValue,
301 // Evaluated for control flow (and side effects).
302 kTest
303 };
304
305 // Mark this expression as being in tail position.
306 virtual void MarkTail() {}
307
308 // True iff the expression is a valid reference expression.
309 virtual bool IsValidReferenceExpression() const { return false; }
310
311 // Helpers for ToBoolean conversion.
312 virtual bool ToBooleanIsTrue() const { return false; }
313 virtual bool ToBooleanIsFalse() const { return false; }
314
315 // Symbols that cannot be parsed as array indices are considered property
316 // names. We do not treat symbols that can be array indexes as property
317 // names because [] for string objects is handled only by keyed ICs.
318 virtual bool IsPropertyName() const { return false; }
319
Ben Murdoch097c5b22016-05-18 11:27:45 +0100320 // True iff the expression is a class or function expression without
321 // a syntactic name.
322 virtual bool IsAnonymousFunctionDefinition() const { return false; }
323
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000324 // True iff the expression is a literal represented as a smi.
325 bool IsSmiLiteral() const;
326
327 // True iff the expression is a string literal.
328 bool IsStringLiteral() const;
329
330 // True iff the expression is the null literal.
331 bool IsNullLiteral() const;
332
Ben Murdochda12d292016-06-02 14:46:10 +0100333 // True if we can prove that the expression is the undefined literal. Note
334 // that this also checks for loads of the global "undefined" variable.
335 bool IsUndefinedLiteral() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000336
337 // True iff the expression is a valid target for an assignment.
338 bool IsValidReferenceExpressionOrThis() const;
339
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000340 // Type feedback information for assignments and properties.
341 virtual bool IsMonomorphic() {
342 UNREACHABLE();
343 return false;
344 }
345 virtual SmallMapList* GetReceiverTypes() {
346 UNREACHABLE();
347 return NULL;
348 }
349 virtual KeyedAccessStoreMode GetStoreMode() const {
350 UNREACHABLE();
351 return STANDARD_STORE;
352 }
353 virtual IcCheckType GetKeyType() const {
354 UNREACHABLE();
355 return ELEMENT;
356 }
357
358 // TODO(rossberg): this should move to its own AST node eventually.
359 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
360 uint16_t to_boolean_types() const {
361 return ToBooleanTypesField::decode(bit_field_);
362 }
363
364 void set_base_id(int id) { base_id_ = id; }
365 static int num_ids() { return parent_num_ids() + 2; }
366 BailoutId id() const { return BailoutId(local_id(0)); }
367 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
368
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000369 protected:
370 Expression(Zone* zone, int pos)
371 : AstNode(pos),
372 base_id_(BailoutId::None().ToInt()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000373 bit_field_(0) {}
374 static int parent_num_ids() { return 0; }
375 void set_to_boolean_types(uint16_t types) {
376 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
377 }
378
379 int base_id() const {
380 DCHECK(!BailoutId(base_id_).IsNone());
381 return base_id_;
382 }
383
384 private:
385 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
386
387 int base_id_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000388 class ToBooleanTypesField : public BitField16<uint16_t, 0, 9> {};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000389 uint16_t bit_field_;
390 // Ends with 16-bit field; deriving classes in turn begin with
391 // 16-bit fields for optimum packing efficiency.
392};
393
394
395class BreakableStatement : public Statement {
396 public:
397 enum BreakableType {
398 TARGET_FOR_ANONYMOUS,
399 TARGET_FOR_NAMED_ONLY
400 };
401
402 // The labels associated with this statement. May be NULL;
403 // if it is != NULL, guaranteed to contain at least one entry.
404 ZoneList<const AstRawString*>* labels() const { return labels_; }
405
406 // Type testing & conversion.
407 BreakableStatement* AsBreakableStatement() final { return this; }
408
409 // Code generation
410 Label* break_target() { return &break_target_; }
411
412 // Testers.
413 bool is_target_for_anonymous() const {
414 return breakable_type_ == TARGET_FOR_ANONYMOUS;
415 }
416
417 void set_base_id(int id) { base_id_ = id; }
418 static int num_ids() { return parent_num_ids() + 2; }
419 BailoutId EntryId() const { return BailoutId(local_id(0)); }
420 BailoutId ExitId() const { return BailoutId(local_id(1)); }
421
422 protected:
423 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
424 BreakableType breakable_type, int position)
425 : Statement(zone, position),
426 labels_(labels),
427 breakable_type_(breakable_type),
428 base_id_(BailoutId::None().ToInt()) {
429 DCHECK(labels == NULL || labels->length() > 0);
430 }
431 static int parent_num_ids() { return 0; }
432
433 int base_id() const {
434 DCHECK(!BailoutId(base_id_).IsNone());
435 return base_id_;
436 }
437
438 private:
439 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
440
441 ZoneList<const AstRawString*>* labels_;
442 BreakableType breakable_type_;
443 Label break_target_;
444 int base_id_;
445};
446
447
448class Block final : public BreakableStatement {
449 public:
450 DECLARE_NODE_TYPE(Block)
451
452 ZoneList<Statement*>* statements() { return &statements_; }
453 bool ignore_completion_value() const { return ignore_completion_value_; }
454
455 static int num_ids() { return parent_num_ids() + 1; }
456 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
457
458 bool IsJump() const override {
459 return !statements_.is_empty() && statements_.last()->IsJump()
460 && labels() == NULL; // Good enough as an approximation...
461 }
462
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000463 Scope* scope() const { return scope_; }
464 void set_scope(Scope* scope) { scope_ = scope; }
465
466 protected:
467 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
468 bool ignore_completion_value, int pos)
469 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
470 statements_(capacity, zone),
471 ignore_completion_value_(ignore_completion_value),
472 scope_(NULL) {}
473 static int parent_num_ids() { return BreakableStatement::num_ids(); }
474
475 private:
476 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
477
478 ZoneList<Statement*> statements_;
479 bool ignore_completion_value_;
480 Scope* scope_;
481};
482
483
484class DoExpression final : public Expression {
485 public:
486 DECLARE_NODE_TYPE(DoExpression)
487
488 Block* block() { return block_; }
489 void set_block(Block* b) { block_ = b; }
490 VariableProxy* result() { return result_; }
491 void set_result(VariableProxy* v) { result_ = v; }
492
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000493 protected:
494 DoExpression(Zone* zone, Block* block, VariableProxy* result, int pos)
495 : Expression(zone, pos), block_(block), result_(result) {
496 DCHECK_NOT_NULL(block_);
497 DCHECK_NOT_NULL(result_);
498 }
499 static int parent_num_ids() { return Expression::num_ids(); }
500
501 private:
502 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
503
504 Block* block_;
505 VariableProxy* result_;
506};
507
508
509class Declaration : public AstNode {
510 public:
511 VariableProxy* proxy() const { return proxy_; }
512 VariableMode mode() const { return mode_; }
513 Scope* scope() const { return scope_; }
514 virtual InitializationFlag initialization() const = 0;
515 virtual bool IsInlineable() const;
516
517 protected:
518 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
519 int pos)
520 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
521 DCHECK(IsDeclaredVariableMode(mode));
522 }
523
524 private:
525 VariableMode mode_;
526 VariableProxy* proxy_;
527
528 // Nested scope from which the declaration originated.
529 Scope* scope_;
530};
531
532
533class VariableDeclaration final : public Declaration {
534 public:
535 DECLARE_NODE_TYPE(VariableDeclaration)
536
537 InitializationFlag initialization() const override {
538 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
539 }
540
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000541 protected:
542 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100543 Scope* scope, int pos)
544 : Declaration(zone, proxy, mode, scope, pos) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000545};
546
547
548class FunctionDeclaration final : public Declaration {
549 public:
550 DECLARE_NODE_TYPE(FunctionDeclaration)
551
552 FunctionLiteral* fun() const { return fun_; }
553 void set_fun(FunctionLiteral* f) { fun_ = f; }
554 InitializationFlag initialization() const override {
555 return kCreatedInitialized;
556 }
557 bool IsInlineable() const override;
558
559 protected:
560 FunctionDeclaration(Zone* zone,
561 VariableProxy* proxy,
562 VariableMode mode,
563 FunctionLiteral* fun,
564 Scope* scope,
565 int pos)
566 : Declaration(zone, proxy, mode, scope, pos),
567 fun_(fun) {
568 DCHECK(mode == VAR || mode == LET || mode == CONST);
569 DCHECK(fun != NULL);
570 }
571
572 private:
573 FunctionLiteral* fun_;
574};
575
576
577class ImportDeclaration final : public Declaration {
578 public:
579 DECLARE_NODE_TYPE(ImportDeclaration)
580
581 const AstRawString* import_name() const { return import_name_; }
582 const AstRawString* module_specifier() const { return module_specifier_; }
583 void set_module_specifier(const AstRawString* module_specifier) {
584 DCHECK(module_specifier_ == NULL);
585 module_specifier_ = module_specifier;
586 }
587 InitializationFlag initialization() const override {
588 return kNeedsInitialization;
589 }
590
591 protected:
592 ImportDeclaration(Zone* zone, VariableProxy* proxy,
593 const AstRawString* import_name,
594 const AstRawString* module_specifier, Scope* scope, int pos)
Ben Murdochc5610432016-08-08 18:44:38 +0100595 : Declaration(zone, proxy, CONST, scope, pos),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000596 import_name_(import_name),
597 module_specifier_(module_specifier) {}
598
599 private:
600 const AstRawString* import_name_;
601 const AstRawString* module_specifier_;
602};
603
604
605class ExportDeclaration final : public Declaration {
606 public:
607 DECLARE_NODE_TYPE(ExportDeclaration)
608
609 InitializationFlag initialization() const override {
610 return kCreatedInitialized;
611 }
612
613 protected:
614 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
615 : Declaration(zone, proxy, LET, scope, pos) {}
616};
617
618
619class Module : public AstNode {
620 public:
621 ModuleDescriptor* descriptor() const { return descriptor_; }
622 Block* body() const { return body_; }
623
624 protected:
625 Module(Zone* zone, int pos)
626 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
627 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
628 : AstNode(pos), descriptor_(descriptor), body_(body) {}
629
630 private:
631 ModuleDescriptor* descriptor_;
632 Block* body_;
633};
634
635
636class IterationStatement : public BreakableStatement {
637 public:
638 // Type testing & conversion.
639 IterationStatement* AsIterationStatement() final { return this; }
640
641 Statement* body() const { return body_; }
642 void set_body(Statement* s) { body_ = s; }
643
Ben Murdochc5610432016-08-08 18:44:38 +0100644 int yield_count() const { return yield_count_; }
645 int first_yield_id() const { return first_yield_id_; }
646 void set_yield_count(int yield_count) { yield_count_ = yield_count; }
647 void set_first_yield_id(int first_yield_id) {
648 first_yield_id_ = first_yield_id;
649 }
650
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000651 static int num_ids() { return parent_num_ids() + 1; }
652 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
653 virtual BailoutId ContinueId() const = 0;
654 virtual BailoutId StackCheckId() const = 0;
655
656 // Code generation
657 Label* continue_target() { return &continue_target_; }
658
659 protected:
660 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
661 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
Ben Murdochc5610432016-08-08 18:44:38 +0100662 body_(NULL),
663 yield_count_(0),
664 first_yield_id_(0) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000665 static int parent_num_ids() { return BreakableStatement::num_ids(); }
666 void Initialize(Statement* body) { body_ = body; }
667
668 private:
669 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
670
671 Statement* body_;
672 Label continue_target_;
Ben Murdochc5610432016-08-08 18:44:38 +0100673 int yield_count_;
674 int first_yield_id_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000675};
676
677
678class DoWhileStatement final : public IterationStatement {
679 public:
680 DECLARE_NODE_TYPE(DoWhileStatement)
681
682 void Initialize(Expression* cond, Statement* body) {
683 IterationStatement::Initialize(body);
684 cond_ = cond;
685 }
686
687 Expression* cond() const { return cond_; }
688 void set_cond(Expression* e) { cond_ = e; }
689
690 static int num_ids() { return parent_num_ids() + 2; }
691 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
692 BailoutId StackCheckId() const override { return BackEdgeId(); }
693 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
694
695 protected:
696 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
697 : IterationStatement(zone, labels, pos), cond_(NULL) {}
698 static int parent_num_ids() { return IterationStatement::num_ids(); }
699
700 private:
701 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
702
703 Expression* cond_;
704};
705
706
707class WhileStatement final : public IterationStatement {
708 public:
709 DECLARE_NODE_TYPE(WhileStatement)
710
711 void Initialize(Expression* cond, Statement* body) {
712 IterationStatement::Initialize(body);
713 cond_ = cond;
714 }
715
716 Expression* cond() const { return cond_; }
717 void set_cond(Expression* e) { cond_ = e; }
718
719 static int num_ids() { return parent_num_ids() + 1; }
720 BailoutId ContinueId() const override { return EntryId(); }
721 BailoutId StackCheckId() const override { return BodyId(); }
722 BailoutId BodyId() const { return BailoutId(local_id(0)); }
723
724 protected:
725 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
726 : IterationStatement(zone, labels, pos), cond_(NULL) {}
727 static int parent_num_ids() { return IterationStatement::num_ids(); }
728
729 private:
730 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
731
732 Expression* cond_;
733};
734
735
736class ForStatement final : public IterationStatement {
737 public:
738 DECLARE_NODE_TYPE(ForStatement)
739
740 void Initialize(Statement* init,
741 Expression* cond,
742 Statement* next,
743 Statement* body) {
744 IterationStatement::Initialize(body);
745 init_ = init;
746 cond_ = cond;
747 next_ = next;
748 }
749
750 Statement* init() const { return init_; }
751 Expression* cond() const { return cond_; }
752 Statement* next() const { return next_; }
753
754 void set_init(Statement* s) { init_ = s; }
755 void set_cond(Expression* e) { cond_ = e; }
756 void set_next(Statement* s) { next_ = s; }
757
758 static int num_ids() { return parent_num_ids() + 2; }
759 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
760 BailoutId StackCheckId() const override { return BodyId(); }
761 BailoutId BodyId() const { return BailoutId(local_id(1)); }
762
763 protected:
764 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
765 : IterationStatement(zone, labels, pos),
766 init_(NULL),
767 cond_(NULL),
768 next_(NULL) {}
769 static int parent_num_ids() { return IterationStatement::num_ids(); }
770
771 private:
772 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
773
774 Statement* init_;
775 Expression* cond_;
776 Statement* next_;
777};
778
779
780class ForEachStatement : public IterationStatement {
781 public:
782 enum VisitMode {
783 ENUMERATE, // for (each in subject) body;
784 ITERATE // for (each of subject) body;
785 };
786
Ben Murdochc5610432016-08-08 18:44:38 +0100787 using IterationStatement::Initialize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000788
Ben Murdoch097c5b22016-05-18 11:27:45 +0100789 static const char* VisitModeString(VisitMode mode) {
790 return mode == ITERATE ? "for-of" : "for-in";
791 }
792
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000793 protected:
794 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
Ben Murdochc5610432016-08-08 18:44:38 +0100795 : IterationStatement(zone, labels, pos) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000796};
797
798
799class ForInStatement final : public ForEachStatement {
800 public:
801 DECLARE_NODE_TYPE(ForInStatement)
802
Ben Murdochc5610432016-08-08 18:44:38 +0100803 void Initialize(Expression* each, Expression* subject, Statement* body) {
804 ForEachStatement::Initialize(body);
805 each_ = each;
806 subject_ = subject;
807 }
808
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000809 Expression* enumerable() const {
810 return subject();
811 }
812
Ben Murdochc5610432016-08-08 18:44:38 +0100813 Expression* each() const { return each_; }
814 Expression* subject() const { return subject_; }
815
816 void set_each(Expression* e) { each_ = e; }
817 void set_subject(Expression* e) { subject_ = e; }
818
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000819 // Type feedback information.
820 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
Ben Murdochda12d292016-06-02 14:46:10 +0100821 FeedbackVectorSlotCache* cache) override;
822 FeedbackVectorSlot EachFeedbackSlot() const { return each_slot_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000823 FeedbackVectorSlot ForInFeedbackSlot() {
824 DCHECK(!for_in_feedback_slot_.IsInvalid());
825 return for_in_feedback_slot_;
826 }
827
828 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
829 ForInType for_in_type() const { return for_in_type_; }
830 void set_for_in_type(ForInType type) { for_in_type_ = type; }
831
832 static int num_ids() { return parent_num_ids() + 6; }
833 BailoutId BodyId() const { return BailoutId(local_id(0)); }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100834 BailoutId EnumId() const { return BailoutId(local_id(1)); }
835 BailoutId ToObjectId() const { return BailoutId(local_id(2)); }
836 BailoutId PrepareId() const { return BailoutId(local_id(3)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000837 BailoutId FilterId() const { return BailoutId(local_id(4)); }
838 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
839 BailoutId ContinueId() const override { return EntryId(); }
840 BailoutId StackCheckId() const override { return BodyId(); }
841
842 protected:
843 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
Ben Murdochc5610432016-08-08 18:44:38 +0100844 : ForEachStatement(zone, labels, pos),
845 each_(nullptr),
846 subject_(nullptr),
847 for_in_type_(SLOW_FOR_IN) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000848 static int parent_num_ids() { return ForEachStatement::num_ids(); }
849
850 private:
851 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
852
Ben Murdochc5610432016-08-08 18:44:38 +0100853 Expression* each_;
854 Expression* subject_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000855 ForInType for_in_type_;
Ben Murdochda12d292016-06-02 14:46:10 +0100856 FeedbackVectorSlot each_slot_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000857 FeedbackVectorSlot for_in_feedback_slot_;
858};
859
860
861class ForOfStatement final : public ForEachStatement {
862 public:
863 DECLARE_NODE_TYPE(ForOfStatement)
864
Ben Murdochc5610432016-08-08 18:44:38 +0100865 void Initialize(Statement* body, Variable* iterator,
866 Expression* assign_iterator, Expression* next_result,
867 Expression* result_done, Expression* assign_each) {
868 ForEachStatement::Initialize(body);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100869 iterator_ = iterator;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000870 assign_iterator_ = assign_iterator;
871 next_result_ = next_result;
872 result_done_ = result_done;
873 assign_each_ = assign_each;
874 }
875
Ben Murdoch097c5b22016-05-18 11:27:45 +0100876 Variable* iterator() const {
877 return iterator_;
878 }
879
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000880 // iterator = subject[Symbol.iterator]()
881 Expression* assign_iterator() const {
882 return assign_iterator_;
883 }
884
885 // result = iterator.next() // with type check
886 Expression* next_result() const {
887 return next_result_;
888 }
889
890 // result.done
891 Expression* result_done() const {
892 return result_done_;
893 }
894
895 // each = result.value
896 Expression* assign_each() const {
897 return assign_each_;
898 }
899
900 void set_assign_iterator(Expression* e) { assign_iterator_ = e; }
901 void set_next_result(Expression* e) { next_result_ = e; }
902 void set_result_done(Expression* e) { result_done_ = e; }
903 void set_assign_each(Expression* e) { assign_each_ = e; }
904
905 BailoutId ContinueId() const override { return EntryId(); }
906 BailoutId StackCheckId() const override { return BackEdgeId(); }
907
908 static int num_ids() { return parent_num_ids() + 1; }
909 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
910
911 protected:
912 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
913 : ForEachStatement(zone, labels, pos),
Ben Murdoch097c5b22016-05-18 11:27:45 +0100914 iterator_(NULL),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000915 assign_iterator_(NULL),
916 next_result_(NULL),
917 result_done_(NULL),
918 assign_each_(NULL) {}
919 static int parent_num_ids() { return ForEachStatement::num_ids(); }
920
921 private:
922 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
923
Ben Murdoch097c5b22016-05-18 11:27:45 +0100924 Variable* iterator_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000925 Expression* assign_iterator_;
926 Expression* next_result_;
927 Expression* result_done_;
928 Expression* assign_each_;
929};
930
931
932class ExpressionStatement final : public Statement {
933 public:
934 DECLARE_NODE_TYPE(ExpressionStatement)
935
936 void set_expression(Expression* e) { expression_ = e; }
937 Expression* expression() const { return expression_; }
938 bool IsJump() const override { return expression_->IsThrow(); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000939
940 protected:
941 ExpressionStatement(Zone* zone, Expression* expression, int pos)
942 : Statement(zone, pos), expression_(expression) { }
943
944 private:
945 Expression* expression_;
946};
947
948
949class JumpStatement : public Statement {
950 public:
951 bool IsJump() const final { return true; }
952
953 protected:
954 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
955};
956
957
958class ContinueStatement final : public JumpStatement {
959 public:
960 DECLARE_NODE_TYPE(ContinueStatement)
961
962 IterationStatement* target() const { return target_; }
963
964 protected:
965 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
966 : JumpStatement(zone, pos), target_(target) { }
967
968 private:
969 IterationStatement* target_;
970};
971
972
973class BreakStatement final : public JumpStatement {
974 public:
975 DECLARE_NODE_TYPE(BreakStatement)
976
977 BreakableStatement* target() const { return target_; }
978
979 protected:
980 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
981 : JumpStatement(zone, pos), target_(target) { }
982
983 private:
984 BreakableStatement* target_;
985};
986
987
988class ReturnStatement final : public JumpStatement {
989 public:
990 DECLARE_NODE_TYPE(ReturnStatement)
991
992 Expression* expression() const { return expression_; }
993
994 void set_expression(Expression* e) { expression_ = e; }
995
996 protected:
997 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
998 : JumpStatement(zone, pos), expression_(expression) { }
999
1000 private:
1001 Expression* expression_;
1002};
1003
1004
1005class WithStatement final : public Statement {
1006 public:
1007 DECLARE_NODE_TYPE(WithStatement)
1008
1009 Scope* scope() { return scope_; }
1010 Expression* expression() const { return expression_; }
1011 void set_expression(Expression* e) { expression_ = e; }
1012 Statement* statement() const { return statement_; }
1013 void set_statement(Statement* s) { statement_ = s; }
1014
1015 void set_base_id(int id) { base_id_ = id; }
1016 static int num_ids() { return parent_num_ids() + 2; }
1017 BailoutId ToObjectId() const { return BailoutId(local_id(0)); }
1018 BailoutId EntryId() const { return BailoutId(local_id(1)); }
1019
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001020 protected:
1021 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1022 Statement* statement, int pos)
1023 : Statement(zone, pos),
1024 scope_(scope),
1025 expression_(expression),
1026 statement_(statement),
1027 base_id_(BailoutId::None().ToInt()) {}
1028 static int parent_num_ids() { return 0; }
1029
1030 int base_id() const {
1031 DCHECK(!BailoutId(base_id_).IsNone());
1032 return base_id_;
1033 }
1034
1035 private:
1036 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1037
1038 Scope* scope_;
1039 Expression* expression_;
1040 Statement* statement_;
1041 int base_id_;
1042};
1043
1044
1045class CaseClause final : public Expression {
1046 public:
1047 DECLARE_NODE_TYPE(CaseClause)
1048
1049 bool is_default() const { return label_ == NULL; }
1050 Expression* label() const {
1051 CHECK(!is_default());
1052 return label_;
1053 }
1054 void set_label(Expression* e) { label_ = e; }
1055 Label* body_target() { return &body_target_; }
1056 ZoneList<Statement*>* statements() const { return statements_; }
1057
1058 static int num_ids() { return parent_num_ids() + 2; }
1059 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1060 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1061
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001062 Type* compare_type() { return compare_type_; }
1063 void set_compare_type(Type* type) { compare_type_ = type; }
1064
1065 protected:
1066 static int parent_num_ids() { return Expression::num_ids(); }
1067
1068 private:
1069 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1070 int pos);
1071 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1072
1073 Expression* label_;
1074 Label body_target_;
1075 ZoneList<Statement*>* statements_;
1076 Type* compare_type_;
1077};
1078
1079
1080class SwitchStatement final : public BreakableStatement {
1081 public:
1082 DECLARE_NODE_TYPE(SwitchStatement)
1083
1084 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1085 tag_ = tag;
1086 cases_ = cases;
1087 }
1088
1089 Expression* tag() const { return tag_; }
1090 ZoneList<CaseClause*>* cases() const { return cases_; }
1091
1092 void set_tag(Expression* t) { tag_ = t; }
1093
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001094 protected:
1095 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1096 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1097 tag_(NULL),
1098 cases_(NULL) {}
1099
1100 private:
1101 Expression* tag_;
1102 ZoneList<CaseClause*>* cases_;
1103};
1104
1105
1106// If-statements always have non-null references to their then- and
1107// else-parts. When parsing if-statements with no explicit else-part,
1108// the parser implicitly creates an empty statement. Use the
1109// HasThenStatement() and HasElseStatement() functions to check if a
1110// given if-statement has a then- or an else-part containing code.
1111class IfStatement final : public Statement {
1112 public:
1113 DECLARE_NODE_TYPE(IfStatement)
1114
1115 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1116 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1117
1118 Expression* condition() const { return condition_; }
1119 Statement* then_statement() const { return then_statement_; }
1120 Statement* else_statement() const { return else_statement_; }
1121
1122 void set_condition(Expression* e) { condition_ = e; }
1123 void set_then_statement(Statement* s) { then_statement_ = s; }
1124 void set_else_statement(Statement* s) { else_statement_ = s; }
1125
1126 bool IsJump() const override {
1127 return HasThenStatement() && then_statement()->IsJump()
1128 && HasElseStatement() && else_statement()->IsJump();
1129 }
1130
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001131 void set_base_id(int id) { base_id_ = id; }
1132 static int num_ids() { return parent_num_ids() + 3; }
1133 BailoutId IfId() const { return BailoutId(local_id(0)); }
1134 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1135 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1136
1137 protected:
1138 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1139 Statement* else_statement, int pos)
1140 : Statement(zone, pos),
1141 condition_(condition),
1142 then_statement_(then_statement),
1143 else_statement_(else_statement),
1144 base_id_(BailoutId::None().ToInt()) {}
1145 static int parent_num_ids() { return 0; }
1146
1147 int base_id() const {
1148 DCHECK(!BailoutId(base_id_).IsNone());
1149 return base_id_;
1150 }
1151
1152 private:
1153 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1154
1155 Expression* condition_;
1156 Statement* then_statement_;
1157 Statement* else_statement_;
1158 int base_id_;
1159};
1160
1161
1162class TryStatement : public Statement {
1163 public:
1164 Block* try_block() const { return try_block_; }
1165 void set_try_block(Block* b) { try_block_ = b; }
1166
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001167 protected:
1168 TryStatement(Zone* zone, Block* try_block, int pos)
Ben Murdoch097c5b22016-05-18 11:27:45 +01001169 : Statement(zone, pos), try_block_(try_block) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001170
1171 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001172 Block* try_block_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001173};
1174
1175
1176class TryCatchStatement final : public TryStatement {
1177 public:
1178 DECLARE_NODE_TYPE(TryCatchStatement)
1179
1180 Scope* scope() { return scope_; }
1181 Variable* variable() { return variable_; }
1182 Block* catch_block() const { return catch_block_; }
1183 void set_catch_block(Block* b) { catch_block_ = b; }
1184
Ben Murdochda12d292016-06-02 14:46:10 +01001185 // The clear_pending_message flag indicates whether or not to clear the
1186 // isolate's pending exception message before executing the catch_block. In
1187 // the normal use case, this flag is always on because the message object
1188 // is not needed anymore when entering the catch block and should not be kept
1189 // alive.
1190 // The use case where the flag is off is when the catch block is guaranteed to
1191 // rethrow the caught exception (using %ReThrow), which reuses the pending
1192 // message instead of generating a new one.
1193 // (When the catch block doesn't rethrow but is guaranteed to perform an
1194 // ordinary throw, not clearing the old message is safe but not very useful.)
1195 bool clear_pending_message() { return clear_pending_message_; }
1196
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001197 protected:
1198 TryCatchStatement(Zone* zone, Block* try_block, Scope* scope,
Ben Murdochda12d292016-06-02 14:46:10 +01001199 Variable* variable, Block* catch_block,
1200 bool clear_pending_message, int pos)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001201 : TryStatement(zone, try_block, pos),
1202 scope_(scope),
1203 variable_(variable),
Ben Murdochda12d292016-06-02 14:46:10 +01001204 catch_block_(catch_block),
1205 clear_pending_message_(clear_pending_message) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001206
1207 private:
1208 Scope* scope_;
1209 Variable* variable_;
1210 Block* catch_block_;
Ben Murdochda12d292016-06-02 14:46:10 +01001211 bool clear_pending_message_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001212};
1213
1214
1215class TryFinallyStatement final : public TryStatement {
1216 public:
1217 DECLARE_NODE_TYPE(TryFinallyStatement)
1218
1219 Block* finally_block() const { return finally_block_; }
1220 void set_finally_block(Block* b) { finally_block_ = b; }
1221
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001222 protected:
1223 TryFinallyStatement(Zone* zone, Block* try_block, Block* finally_block,
1224 int pos)
1225 : TryStatement(zone, try_block, pos), finally_block_(finally_block) {}
1226
1227 private:
1228 Block* finally_block_;
1229};
1230
1231
1232class DebuggerStatement final : public Statement {
1233 public:
1234 DECLARE_NODE_TYPE(DebuggerStatement)
1235
1236 void set_base_id(int id) { base_id_ = id; }
1237 static int num_ids() { return parent_num_ids() + 1; }
1238 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1239
1240 protected:
1241 explicit DebuggerStatement(Zone* zone, int pos)
1242 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1243 static int parent_num_ids() { return 0; }
1244
1245 int base_id() const {
1246 DCHECK(!BailoutId(base_id_).IsNone());
1247 return base_id_;
1248 }
1249
1250 private:
1251 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1252
1253 int base_id_;
1254};
1255
1256
1257class EmptyStatement final : public Statement {
1258 public:
1259 DECLARE_NODE_TYPE(EmptyStatement)
1260
1261 protected:
1262 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1263};
1264
1265
1266// Delegates to another statement, which may be overwritten.
1267// This was introduced to implement ES2015 Annex B3.3 for conditionally making
1268// sloppy-mode block-scoped functions have a var binding, which is changed
1269// from one statement to another during parsing.
1270class SloppyBlockFunctionStatement final : public Statement {
1271 public:
1272 DECLARE_NODE_TYPE(SloppyBlockFunctionStatement)
1273
1274 Statement* statement() const { return statement_; }
1275 void set_statement(Statement* statement) { statement_ = statement; }
1276 Scope* scope() const { return scope_; }
1277
1278 private:
1279 SloppyBlockFunctionStatement(Zone* zone, Statement* statement, Scope* scope)
1280 : Statement(zone, RelocInfo::kNoPosition),
1281 statement_(statement),
1282 scope_(scope) {}
1283
1284 Statement* statement_;
1285 Scope* const scope_;
1286};
1287
1288
1289class Literal final : public Expression {
1290 public:
1291 DECLARE_NODE_TYPE(Literal)
1292
1293 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1294
1295 Handle<String> AsPropertyName() {
1296 DCHECK(IsPropertyName());
1297 return Handle<String>::cast(value());
1298 }
1299
1300 const AstRawString* AsRawPropertyName() {
1301 DCHECK(IsPropertyName());
1302 return value_->AsString();
1303 }
1304
1305 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1306 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1307
1308 Handle<Object> value() const { return value_->value(); }
1309 const AstValue* raw_value() const { return value_; }
1310
1311 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1312 // only for string and number literals!
1313 uint32_t Hash();
1314 static bool Match(void* literal1, void* literal2);
1315
1316 static int num_ids() { return parent_num_ids() + 1; }
1317 TypeFeedbackId LiteralFeedbackId() const {
1318 return TypeFeedbackId(local_id(0));
1319 }
1320
1321 protected:
1322 Literal(Zone* zone, const AstValue* value, int position)
1323 : Expression(zone, position), value_(value) {}
1324 static int parent_num_ids() { return Expression::num_ids(); }
1325
1326 private:
1327 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1328
1329 const AstValue* value_;
1330};
1331
1332
1333class AstLiteralReindexer;
1334
1335// Base class for literals that needs space in the corresponding JSFunction.
1336class MaterializedLiteral : public Expression {
1337 public:
1338 MaterializedLiteral* AsMaterializedLiteral() final { return this; }
1339
1340 int literal_index() { return literal_index_; }
1341
1342 int depth() const {
1343 // only callable after initialization.
1344 DCHECK(depth_ >= 1);
1345 return depth_;
1346 }
1347
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001348 protected:
Ben Murdochda12d292016-06-02 14:46:10 +01001349 MaterializedLiteral(Zone* zone, int literal_index, int pos)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001350 : Expression(zone, pos),
1351 literal_index_(literal_index),
1352 is_simple_(false),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001353 depth_(0) {}
1354
1355 // A materialized literal is simple if the values consist of only
1356 // constants and simple object and array literals.
1357 bool is_simple() const { return is_simple_; }
1358 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1359 friend class CompileTimeValue;
1360
1361 void set_depth(int depth) {
1362 DCHECK(depth >= 1);
1363 depth_ = depth;
1364 }
1365
1366 // Populate the constant properties/elements fixed array.
1367 void BuildConstants(Isolate* isolate);
1368 friend class ArrayLiteral;
1369 friend class ObjectLiteral;
1370
1371 // If the expression is a literal, return the literal value;
1372 // if the expression is a materialized literal and is simple return a
1373 // compile time value as encoded by CompileTimeValue::GetValue().
1374 // Otherwise, return undefined literal as the placeholder
1375 // in the object literal boilerplate.
1376 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1377
1378 private:
1379 int literal_index_;
1380 bool is_simple_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001381 int depth_;
1382
1383 friend class AstLiteralReindexer;
1384};
1385
1386
1387// Property is used for passing information
1388// about an object literal's properties from the parser
1389// to the code generator.
1390class ObjectLiteralProperty final : public ZoneObject {
1391 public:
1392 enum Kind {
1393 CONSTANT, // Property with constant value (compile time).
1394 COMPUTED, // Property with computed value (execution time).
1395 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1396 GETTER, SETTER, // Property is an accessor function.
1397 PROTOTYPE // Property is __proto__.
1398 };
1399
1400 Expression* key() { return key_; }
1401 Expression* value() { return value_; }
1402 Kind kind() { return kind_; }
1403
1404 void set_key(Expression* e) { key_ = e; }
1405 void set_value(Expression* e) { value_ = e; }
1406
1407 // Type feedback information.
1408 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1409 Handle<Map> GetReceiverType() { return receiver_type_; }
1410
1411 bool IsCompileTimeValue();
1412
1413 void set_emit_store(bool emit_store);
1414 bool emit_store();
1415
1416 bool is_static() const { return is_static_; }
1417 bool is_computed_name() const { return is_computed_name_; }
1418
1419 FeedbackVectorSlot GetSlot(int offset = 0) const {
1420 DCHECK_LT(offset, static_cast<int>(arraysize(slots_)));
1421 return slots_[offset];
1422 }
1423 void SetSlot(FeedbackVectorSlot slot, int offset = 0) {
1424 DCHECK_LT(offset, static_cast<int>(arraysize(slots_)));
1425 slots_[offset] = slot;
1426 }
1427
1428 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1429
Ben Murdoch097c5b22016-05-18 11:27:45 +01001430 bool NeedsSetFunctionName() const;
1431
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001432 protected:
1433 friend class AstNodeFactory;
1434
1435 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1436 bool is_static, bool is_computed_name);
1437 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1438 Expression* value, bool is_static,
1439 bool is_computed_name);
1440
1441 private:
1442 Expression* key_;
1443 Expression* value_;
1444 FeedbackVectorSlot slots_[2];
1445 Kind kind_;
1446 bool emit_store_;
1447 bool is_static_;
1448 bool is_computed_name_;
1449 Handle<Map> receiver_type_;
1450};
1451
1452
1453// An object literal has a boilerplate object that is used
1454// for minimizing the work when constructing it at runtime.
1455class ObjectLiteral final : public MaterializedLiteral {
1456 public:
1457 typedef ObjectLiteralProperty Property;
1458
1459 DECLARE_NODE_TYPE(ObjectLiteral)
1460
1461 Handle<FixedArray> constant_properties() const {
1462 return constant_properties_;
1463 }
1464 int properties_count() const { return constant_properties_->length() / 2; }
1465 ZoneList<Property*>* properties() const { return properties_; }
1466 bool fast_elements() const { return fast_elements_; }
1467 bool may_store_doubles() const { return may_store_doubles_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001468 bool has_elements() const { return has_elements_; }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001469 bool has_shallow_properties() const {
1470 return depth() == 1 && !has_elements() && !may_store_doubles();
1471 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001472
1473 // Decide if a property should be in the object boilerplate.
1474 static bool IsBoilerplateProperty(Property* property);
1475
1476 // Populate the constant properties fixed array.
1477 void BuildConstantProperties(Isolate* isolate);
1478
1479 // Mark all computed expressions that are bound to a key that
1480 // is shadowed by a later occurrence of the same key. For the
1481 // marked expressions, no store code is emitted.
1482 void CalculateEmitStore(Zone* zone);
1483
1484 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1485 int ComputeFlags(bool disable_mementos = false) const {
1486 int flags = fast_elements() ? kFastElements : kNoFlags;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001487 if (has_shallow_properties()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001488 flags |= kShallowProperties;
1489 }
1490 if (disable_mementos) {
1491 flags |= kDisableMementos;
1492 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001493 return flags;
1494 }
1495
1496 enum Flags {
1497 kNoFlags = 0,
1498 kFastElements = 1,
Ben Murdochda12d292016-06-02 14:46:10 +01001499 kShallowProperties = 1 << 1,
1500 kDisableMementos = 1 << 2
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001501 };
1502
1503 struct Accessors: public ZoneObject {
1504 Accessors() : getter(NULL), setter(NULL) {}
1505 ObjectLiteralProperty* getter;
1506 ObjectLiteralProperty* setter;
1507 };
1508
1509 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1510
1511 // Return an AST id for a property that is used in simulate instructions.
1512 BailoutId GetIdForPropertyName(int i) {
1513 return BailoutId(local_id(2 * i + 1));
1514 }
1515 BailoutId GetIdForPropertySet(int i) {
1516 return BailoutId(local_id(2 * i + 2));
1517 }
1518
1519 // Unlike other AST nodes, this number of bailout IDs allocated for an
1520 // ObjectLiteral can vary, so num_ids() is not a static method.
1521 int num_ids() const {
1522 return parent_num_ids() + 1 + 2 * properties()->length();
1523 }
1524
1525 // Object literals need one feedback slot for each non-trivial value, as well
1526 // as some slots for home objects.
1527 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1528 FeedbackVectorSlotCache* cache) override;
1529
1530 protected:
1531 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
Ben Murdochda12d292016-06-02 14:46:10 +01001532 int boilerplate_properties, int pos)
1533 : MaterializedLiteral(zone, literal_index, pos),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001534 properties_(properties),
1535 boilerplate_properties_(boilerplate_properties),
1536 fast_elements_(false),
1537 has_elements_(false),
Ben Murdochda12d292016-06-02 14:46:10 +01001538 may_store_doubles_(false) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001539 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1540
1541 private:
1542 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1543 Handle<FixedArray> constant_properties_;
1544 ZoneList<Property*>* properties_;
1545 int boilerplate_properties_;
1546 bool fast_elements_;
1547 bool has_elements_;
1548 bool may_store_doubles_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001549 FeedbackVectorSlot slot_;
1550};
1551
1552
1553// A map from property names to getter/setter pairs allocated in the zone.
1554class AccessorTable : public TemplateHashMap<Literal, ObjectLiteral::Accessors,
1555 ZoneAllocationPolicy> {
1556 public:
1557 explicit AccessorTable(Zone* zone)
1558 : TemplateHashMap<Literal, ObjectLiteral::Accessors,
1559 ZoneAllocationPolicy>(Literal::Match,
1560 ZoneAllocationPolicy(zone)),
1561 zone_(zone) {}
1562
1563 Iterator lookup(Literal* literal) {
1564 Iterator it = find(literal, true, ZoneAllocationPolicy(zone_));
1565 if (it->second == NULL) it->second = new (zone_) ObjectLiteral::Accessors();
1566 return it;
1567 }
1568
1569 private:
1570 Zone* zone_;
1571};
1572
1573
1574// Node for capturing a regexp literal.
1575class RegExpLiteral final : public MaterializedLiteral {
1576 public:
1577 DECLARE_NODE_TYPE(RegExpLiteral)
1578
1579 Handle<String> pattern() const { return pattern_->string(); }
1580 int flags() const { return flags_; }
1581
1582 protected:
1583 RegExpLiteral(Zone* zone, const AstRawString* pattern, int flags,
Ben Murdochda12d292016-06-02 14:46:10 +01001584 int literal_index, int pos)
1585 : MaterializedLiteral(zone, literal_index, pos),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001586 pattern_(pattern),
1587 flags_(flags) {
1588 set_depth(1);
1589 }
1590
1591 private:
1592 const AstRawString* const pattern_;
1593 int const flags_;
1594};
1595
1596
1597// An array literal has a literals object that is used
1598// for minimizing the work when constructing it at runtime.
1599class ArrayLiteral final : public MaterializedLiteral {
1600 public:
1601 DECLARE_NODE_TYPE(ArrayLiteral)
1602
1603 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1604 ElementsKind constant_elements_kind() const {
1605 DCHECK_EQ(2, constant_elements_->length());
1606 return static_cast<ElementsKind>(
1607 Smi::cast(constant_elements_->get(0))->value());
1608 }
1609
1610 ZoneList<Expression*>* values() const { return values_; }
1611
1612 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1613
1614 // Return an AST id for an element that is used in simulate instructions.
1615 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1616
1617 // Unlike other AST nodes, this number of bailout IDs allocated for an
1618 // ArrayLiteral can vary, so num_ids() is not a static method.
1619 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1620
1621 // Populate the constant elements fixed array.
1622 void BuildConstantElements(Isolate* isolate);
1623
1624 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1625 int ComputeFlags(bool disable_mementos = false) const {
1626 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1627 if (disable_mementos) {
1628 flags |= kDisableMementos;
1629 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001630 return flags;
1631 }
1632
Ben Murdoch097c5b22016-05-18 11:27:45 +01001633 // Provide a mechanism for iterating through values to rewrite spreads.
1634 ZoneList<Expression*>::iterator FirstSpread() const {
1635 return (first_spread_index_ >= 0) ? values_->begin() + first_spread_index_
1636 : values_->end();
1637 }
1638 ZoneList<Expression*>::iterator EndValue() const { return values_->end(); }
1639
1640 // Rewind an array literal omitting everything from the first spread on.
1641 void RewindSpreads() {
1642 values_->Rewind(first_spread_index_);
1643 first_spread_index_ = -1;
1644 }
1645
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001646 enum Flags {
1647 kNoFlags = 0,
1648 kShallowElements = 1,
Ben Murdochda12d292016-06-02 14:46:10 +01001649 kDisableMementos = 1 << 1
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001650 };
1651
1652 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1653 FeedbackVectorSlotCache* cache) override;
1654 FeedbackVectorSlot LiteralFeedbackSlot() const { return literal_slot_; }
1655
1656 protected:
1657 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values,
Ben Murdochda12d292016-06-02 14:46:10 +01001658 int first_spread_index, int literal_index, int pos)
1659 : MaterializedLiteral(zone, literal_index, pos),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001660 values_(values),
1661 first_spread_index_(first_spread_index) {}
1662 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1663
1664 private:
1665 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1666
1667 Handle<FixedArray> constant_elements_;
1668 ZoneList<Expression*>* values_;
1669 int first_spread_index_;
1670 FeedbackVectorSlot literal_slot_;
1671};
1672
1673
1674class VariableProxy final : public Expression {
1675 public:
1676 DECLARE_NODE_TYPE(VariableProxy)
1677
1678 bool IsValidReferenceExpression() const override {
1679 return !is_this() && !is_new_target();
1680 }
1681
1682 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1683
1684 Handle<String> name() const { return raw_name()->string(); }
1685 const AstRawString* raw_name() const {
1686 return is_resolved() ? var_->raw_name() : raw_name_;
1687 }
1688
1689 Variable* var() const {
1690 DCHECK(is_resolved());
1691 return var_;
1692 }
1693 void set_var(Variable* v) {
1694 DCHECK(!is_resolved());
1695 DCHECK_NOT_NULL(v);
1696 var_ = v;
1697 }
1698
1699 bool is_this() const { return IsThisField::decode(bit_field_); }
1700
1701 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1702 void set_is_assigned() {
1703 bit_field_ = IsAssignedField::update(bit_field_, true);
1704 }
1705
1706 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1707 void set_is_resolved() {
1708 bit_field_ = IsResolvedField::update(bit_field_, true);
1709 }
1710
1711 bool is_new_target() const { return IsNewTargetField::decode(bit_field_); }
1712 void set_is_new_target() {
1713 bit_field_ = IsNewTargetField::update(bit_field_, true);
1714 }
1715
1716 int end_position() const { return end_position_; }
1717
1718 // Bind this proxy to the variable var.
1719 void BindTo(Variable* var);
1720
1721 bool UsesVariableFeedbackSlot() const {
1722 return var()->IsUnallocated() || var()->IsLookupSlot();
1723 }
1724
1725 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1726 FeedbackVectorSlotCache* cache) override;
1727
1728 FeedbackVectorSlot VariableFeedbackSlot() { return variable_feedback_slot_; }
1729
1730 static int num_ids() { return parent_num_ids() + 1; }
1731 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1732
1733 protected:
1734 VariableProxy(Zone* zone, Variable* var, int start_position,
1735 int end_position);
1736
1737 VariableProxy(Zone* zone, const AstRawString* name,
1738 Variable::Kind variable_kind, int start_position,
1739 int end_position);
1740 static int parent_num_ids() { return Expression::num_ids(); }
1741 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1742
1743 class IsThisField : public BitField8<bool, 0, 1> {};
1744 class IsAssignedField : public BitField8<bool, 1, 1> {};
1745 class IsResolvedField : public BitField8<bool, 2, 1> {};
1746 class IsNewTargetField : public BitField8<bool, 3, 1> {};
1747
1748 // Start with 16-bit (or smaller) field, which should get packed together
1749 // with Expression's trailing 16-bit field.
1750 uint8_t bit_field_;
1751 FeedbackVectorSlot variable_feedback_slot_;
1752 union {
1753 const AstRawString* raw_name_; // if !is_resolved_
1754 Variable* var_; // if is_resolved_
1755 };
1756 // Position is stored in the AstNode superclass, but VariableProxy needs to
1757 // know its end position too (for error messages). It cannot be inferred from
1758 // the variable name length because it can contain escapes.
1759 int end_position_;
1760};
1761
1762
1763// Left-hand side can only be a property, a global or a (parameter or local)
1764// slot.
1765enum LhsKind {
1766 VARIABLE,
1767 NAMED_PROPERTY,
1768 KEYED_PROPERTY,
1769 NAMED_SUPER_PROPERTY,
1770 KEYED_SUPER_PROPERTY
1771};
1772
1773
1774class Property final : public Expression {
1775 public:
1776 DECLARE_NODE_TYPE(Property)
1777
1778 bool IsValidReferenceExpression() const override { return true; }
1779
1780 Expression* obj() const { return obj_; }
1781 Expression* key() const { return key_; }
1782
1783 void set_obj(Expression* e) { obj_ = e; }
1784 void set_key(Expression* e) { key_ = e; }
1785
1786 static int num_ids() { return parent_num_ids() + 1; }
1787 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1788
1789 bool IsStringAccess() const {
1790 return IsStringAccessField::decode(bit_field_);
1791 }
1792
1793 // Type feedback information.
1794 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1795 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1796 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1797 IcCheckType GetKeyType() const override {
1798 return KeyTypeField::decode(bit_field_);
1799 }
1800 bool IsUninitialized() const {
1801 return !is_for_call() && HasNoTypeInformation();
1802 }
1803 bool HasNoTypeInformation() const {
1804 return GetInlineCacheState() == UNINITIALIZED;
1805 }
1806 InlineCacheState GetInlineCacheState() const {
1807 return InlineCacheStateField::decode(bit_field_);
1808 }
1809 void set_is_string_access(bool b) {
1810 bit_field_ = IsStringAccessField::update(bit_field_, b);
1811 }
1812 void set_key_type(IcCheckType key_type) {
1813 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1814 }
1815 void set_inline_cache_state(InlineCacheState state) {
1816 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1817 }
1818 void mark_for_call() {
1819 bit_field_ = IsForCallField::update(bit_field_, true);
1820 }
1821 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1822
1823 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1824
1825 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1826 FeedbackVectorSlotCache* cache) override {
1827 FeedbackVectorSlotKind kind = key()->IsPropertyName()
1828 ? FeedbackVectorSlotKind::LOAD_IC
1829 : FeedbackVectorSlotKind::KEYED_LOAD_IC;
1830 property_feedback_slot_ = spec->AddSlot(kind);
1831 }
1832
1833 FeedbackVectorSlot PropertyFeedbackSlot() const {
1834 return property_feedback_slot_;
1835 }
1836
1837 static LhsKind GetAssignType(Property* property) {
1838 if (property == NULL) return VARIABLE;
1839 bool super_access = property->IsSuperAccess();
1840 return (property->key()->IsPropertyName())
1841 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1842 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1843 }
1844
1845 protected:
1846 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1847 : Expression(zone, pos),
1848 bit_field_(IsForCallField::encode(false) |
1849 IsStringAccessField::encode(false) |
1850 InlineCacheStateField::encode(UNINITIALIZED)),
1851 obj_(obj),
1852 key_(key) {}
1853 static int parent_num_ids() { return Expression::num_ids(); }
1854
1855 private:
1856 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1857
1858 class IsForCallField : public BitField8<bool, 0, 1> {};
1859 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1860 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1861 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1862 uint8_t bit_field_;
1863 FeedbackVectorSlot property_feedback_slot_;
1864 Expression* obj_;
1865 Expression* key_;
1866 SmallMapList receiver_types_;
1867};
1868
1869
1870class Call final : public Expression {
1871 public:
1872 DECLARE_NODE_TYPE(Call)
1873
1874 Expression* expression() const { return expression_; }
1875 ZoneList<Expression*>* arguments() const { return arguments_; }
1876
1877 void set_expression(Expression* e) { expression_ = e; }
1878
1879 // Type feedback information.
1880 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1881 FeedbackVectorSlotCache* cache) override;
1882
1883 FeedbackVectorSlot CallFeedbackSlot() const { return stub_slot_; }
1884
1885 FeedbackVectorSlot CallFeedbackICSlot() const { return ic_slot_; }
1886
1887 SmallMapList* GetReceiverTypes() override {
1888 if (expression()->IsProperty()) {
1889 return expression()->AsProperty()->GetReceiverTypes();
1890 }
1891 return NULL;
1892 }
1893
1894 bool IsMonomorphic() override {
1895 if (expression()->IsProperty()) {
1896 return expression()->AsProperty()->IsMonomorphic();
1897 }
1898 return !target_.is_null();
1899 }
1900
1901 bool global_call() const {
1902 VariableProxy* proxy = expression_->AsVariableProxy();
1903 return proxy != NULL && proxy->var()->IsUnallocatedOrGlobalSlot();
1904 }
1905
1906 bool known_global_function() const {
1907 return global_call() && !target_.is_null();
1908 }
1909
1910 Handle<JSFunction> target() { return target_; }
1911
1912 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1913
1914 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1915 target_ = target;
1916 set_is_uninitialized(false);
1917 }
1918 void set_target(Handle<JSFunction> target) { target_ = target; }
1919 void set_allocation_site(Handle<AllocationSite> site) {
1920 allocation_site_ = site;
1921 }
1922
1923 static int num_ids() { return parent_num_ids() + 4; }
1924 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1925 BailoutId EvalId() const { return BailoutId(local_id(1)); }
1926 BailoutId LookupId() const { return BailoutId(local_id(2)); }
1927 BailoutId CallId() const { return BailoutId(local_id(3)); }
1928
1929 bool is_uninitialized() const {
1930 return IsUninitializedField::decode(bit_field_);
1931 }
1932 void set_is_uninitialized(bool b) {
1933 bit_field_ = IsUninitializedField::update(bit_field_, b);
1934 }
1935
Ben Murdoch097c5b22016-05-18 11:27:45 +01001936 TailCallMode tail_call_mode() const {
1937 return IsTailField::decode(bit_field_) ? TailCallMode::kAllow
1938 : TailCallMode::kDisallow;
1939 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001940 void MarkTail() override {
1941 bit_field_ = IsTailField::update(bit_field_, true);
1942 }
1943
1944 enum CallType {
1945 POSSIBLY_EVAL_CALL,
1946 GLOBAL_CALL,
1947 LOOKUP_SLOT_CALL,
1948 NAMED_PROPERTY_CALL,
1949 KEYED_PROPERTY_CALL,
1950 NAMED_SUPER_PROPERTY_CALL,
1951 KEYED_SUPER_PROPERTY_CALL,
1952 SUPER_CALL,
1953 OTHER_CALL
1954 };
1955
1956 // Helpers to determine how to handle the call.
1957 CallType GetCallType(Isolate* isolate) const;
1958 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1959 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1960
1961#ifdef DEBUG
1962 // Used to assert that the FullCodeGenerator records the return site.
1963 bool return_is_recorded_;
1964#endif
1965
1966 protected:
1967 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1968 int pos)
1969 : Expression(zone, pos),
1970 expression_(expression),
1971 arguments_(arguments),
1972 bit_field_(IsUninitializedField::encode(false)) {
1973 if (expression->IsProperty()) {
1974 expression->AsProperty()->mark_for_call();
1975 }
1976 }
1977 static int parent_num_ids() { return Expression::num_ids(); }
1978
1979 private:
1980 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1981
1982 FeedbackVectorSlot ic_slot_;
1983 FeedbackVectorSlot stub_slot_;
1984 Expression* expression_;
1985 ZoneList<Expression*>* arguments_;
1986 Handle<JSFunction> target_;
1987 Handle<AllocationSite> allocation_site_;
1988 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1989 class IsTailField : public BitField8<bool, 1, 1> {};
1990 uint8_t bit_field_;
1991};
1992
1993
1994class CallNew final : public Expression {
1995 public:
1996 DECLARE_NODE_TYPE(CallNew)
1997
1998 Expression* expression() const { return expression_; }
1999 ZoneList<Expression*>* arguments() const { return arguments_; }
2000
2001 void set_expression(Expression* e) { expression_ = e; }
2002
2003 // Type feedback information.
2004 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
2005 FeedbackVectorSlotCache* cache) override {
2006 callnew_feedback_slot_ = spec->AddGeneralSlot();
2007 }
2008
2009 FeedbackVectorSlot CallNewFeedbackSlot() {
2010 DCHECK(!callnew_feedback_slot_.IsInvalid());
2011 return callnew_feedback_slot_;
2012 }
2013
2014 bool IsMonomorphic() override { return is_monomorphic_; }
2015 Handle<JSFunction> target() const { return target_; }
2016 Handle<AllocationSite> allocation_site() const {
2017 return allocation_site_;
2018 }
2019
2020 static int num_ids() { return parent_num_ids() + 1; }
2021 static int feedback_slots() { return 1; }
2022 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
2023
2024 void set_allocation_site(Handle<AllocationSite> site) {
2025 allocation_site_ = site;
2026 }
2027 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
2028 void set_target(Handle<JSFunction> target) { target_ = target; }
2029 void SetKnownGlobalTarget(Handle<JSFunction> target) {
2030 target_ = target;
2031 is_monomorphic_ = true;
2032 }
2033
2034 protected:
2035 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
2036 int pos)
2037 : Expression(zone, pos),
2038 expression_(expression),
2039 arguments_(arguments),
2040 is_monomorphic_(false) {}
2041
2042 static int parent_num_ids() { return Expression::num_ids(); }
2043
2044 private:
2045 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2046
2047 Expression* expression_;
2048 ZoneList<Expression*>* arguments_;
2049 bool is_monomorphic_;
2050 Handle<JSFunction> target_;
2051 Handle<AllocationSite> allocation_site_;
2052 FeedbackVectorSlot callnew_feedback_slot_;
2053};
2054
2055
2056// The CallRuntime class does not represent any official JavaScript
2057// language construct. Instead it is used to call a C or JS function
2058// with a set of arguments. This is used from the builtins that are
2059// implemented in JavaScript (see "v8natives.js").
2060class CallRuntime final : public Expression {
2061 public:
2062 DECLARE_NODE_TYPE(CallRuntime)
2063
2064 ZoneList<Expression*>* arguments() const { return arguments_; }
2065 bool is_jsruntime() const { return function_ == NULL; }
2066
2067 int context_index() const {
2068 DCHECK(is_jsruntime());
2069 return context_index_;
2070 }
2071 const Runtime::Function* function() const {
2072 DCHECK(!is_jsruntime());
2073 return function_;
2074 }
2075
2076 static int num_ids() { return parent_num_ids() + 1; }
2077 BailoutId CallId() { return BailoutId(local_id(0)); }
2078
2079 const char* debug_name() {
2080 return is_jsruntime() ? "(context function)" : function_->name;
2081 }
2082
2083 protected:
2084 CallRuntime(Zone* zone, const Runtime::Function* function,
2085 ZoneList<Expression*>* arguments, int pos)
2086 : Expression(zone, pos), function_(function), arguments_(arguments) {}
2087
2088 CallRuntime(Zone* zone, int context_index, ZoneList<Expression*>* arguments,
2089 int pos)
2090 : Expression(zone, pos),
2091 function_(NULL),
2092 context_index_(context_index),
2093 arguments_(arguments) {}
2094
2095 static int parent_num_ids() { return Expression::num_ids(); }
2096
2097 private:
2098 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2099
2100 const Runtime::Function* function_;
2101 int context_index_;
2102 ZoneList<Expression*>* arguments_;
2103};
2104
2105
2106class UnaryOperation final : public Expression {
2107 public:
2108 DECLARE_NODE_TYPE(UnaryOperation)
2109
2110 Token::Value op() const { return op_; }
2111 Expression* expression() const { return expression_; }
2112 void set_expression(Expression* e) { expression_ = e; }
2113
2114 // For unary not (Token::NOT), the AST ids where true and false will
2115 // actually be materialized, respectively.
2116 static int num_ids() { return parent_num_ids() + 2; }
2117 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2118 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2119
2120 void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2121
2122 protected:
2123 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2124 : Expression(zone, pos), op_(op), expression_(expression) {
2125 DCHECK(Token::IsUnaryOp(op));
2126 }
2127 static int parent_num_ids() { return Expression::num_ids(); }
2128
2129 private:
2130 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2131
2132 Token::Value op_;
2133 Expression* expression_;
2134};
2135
2136
2137class BinaryOperation final : public Expression {
2138 public:
2139 DECLARE_NODE_TYPE(BinaryOperation)
2140
2141 Token::Value op() const { return static_cast<Token::Value>(op_); }
2142 Expression* left() const { return left_; }
2143 void set_left(Expression* e) { left_ = e; }
2144 Expression* right() const { return right_; }
2145 void set_right(Expression* e) { right_ = e; }
2146 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2147 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2148 allocation_site_ = allocation_site;
2149 }
2150
2151 void MarkTail() override {
2152 switch (op()) {
2153 case Token::COMMA:
2154 case Token::AND:
2155 case Token::OR:
2156 right_->MarkTail();
2157 default:
2158 break;
2159 }
2160 }
2161
2162 // The short-circuit logical operations need an AST ID for their
2163 // right-hand subexpression.
2164 static int num_ids() { return parent_num_ids() + 2; }
2165 BailoutId RightId() const { return BailoutId(local_id(0)); }
2166
2167 TypeFeedbackId BinaryOperationFeedbackId() const {
2168 return TypeFeedbackId(local_id(1));
2169 }
2170 Maybe<int> fixed_right_arg() const {
2171 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2172 }
2173 void set_fixed_right_arg(Maybe<int> arg) {
2174 has_fixed_right_arg_ = arg.IsJust();
2175 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2176 }
2177
2178 void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2179
2180 protected:
2181 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2182 Expression* right, int pos)
2183 : Expression(zone, pos),
2184 op_(static_cast<byte>(op)),
2185 has_fixed_right_arg_(false),
2186 fixed_right_arg_value_(0),
2187 left_(left),
2188 right_(right) {
2189 DCHECK(Token::IsBinaryOp(op));
2190 }
2191 static int parent_num_ids() { return Expression::num_ids(); }
2192
2193 private:
2194 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2195
2196 const byte op_; // actually Token::Value
2197 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2198 // type for the RHS. Currenty it's actually a Maybe<int>
2199 bool has_fixed_right_arg_;
2200 int fixed_right_arg_value_;
2201 Expression* left_;
2202 Expression* right_;
2203 Handle<AllocationSite> allocation_site_;
2204};
2205
2206
2207class CountOperation final : public Expression {
2208 public:
2209 DECLARE_NODE_TYPE(CountOperation)
2210
2211 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2212 bool is_postfix() const { return !is_prefix(); }
2213
2214 Token::Value op() const { return TokenField::decode(bit_field_); }
2215 Token::Value binary_op() {
2216 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2217 }
2218
2219 Expression* expression() const { return expression_; }
2220 void set_expression(Expression* e) { expression_ = e; }
2221
2222 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2223 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2224 IcCheckType GetKeyType() const override {
2225 return KeyTypeField::decode(bit_field_);
2226 }
2227 KeyedAccessStoreMode GetStoreMode() const override {
2228 return StoreModeField::decode(bit_field_);
2229 }
2230 Type* type() const { return type_; }
2231 void set_key_type(IcCheckType type) {
2232 bit_field_ = KeyTypeField::update(bit_field_, type);
2233 }
2234 void set_store_mode(KeyedAccessStoreMode mode) {
2235 bit_field_ = StoreModeField::update(bit_field_, mode);
2236 }
2237 void set_type(Type* type) { type_ = type; }
2238
2239 static int num_ids() { return parent_num_ids() + 4; }
2240 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2241 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2242 TypeFeedbackId CountBinOpFeedbackId() const {
2243 return TypeFeedbackId(local_id(2));
2244 }
2245 TypeFeedbackId CountStoreFeedbackId() const {
2246 return TypeFeedbackId(local_id(3));
2247 }
2248
2249 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
2250 FeedbackVectorSlotCache* cache) override;
2251 FeedbackVectorSlot CountSlot() const { return slot_; }
2252
2253 protected:
2254 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2255 int pos)
2256 : Expression(zone, pos),
2257 bit_field_(
2258 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2259 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2260 type_(NULL),
2261 expression_(expr) {}
2262 static int parent_num_ids() { return Expression::num_ids(); }
2263
2264 private:
2265 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2266
2267 class IsPrefixField : public BitField16<bool, 0, 1> {};
2268 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2269 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 3> {};
2270 class TokenField : public BitField16<Token::Value, 5, 8> {};
2271
2272 // Starts with 16-bit field, which should get packed together with
2273 // Expression's trailing 16-bit field.
2274 uint16_t bit_field_;
2275 Type* type_;
2276 Expression* expression_;
2277 SmallMapList receiver_types_;
2278 FeedbackVectorSlot slot_;
2279};
2280
2281
2282class CompareOperation final : public Expression {
2283 public:
2284 DECLARE_NODE_TYPE(CompareOperation)
2285
2286 Token::Value op() const { return op_; }
2287 Expression* left() const { return left_; }
2288 Expression* right() const { return right_; }
2289
2290 void set_left(Expression* e) { left_ = e; }
2291 void set_right(Expression* e) { right_ = e; }
2292
2293 // Type feedback information.
2294 static int num_ids() { return parent_num_ids() + 1; }
2295 TypeFeedbackId CompareOperationFeedbackId() const {
2296 return TypeFeedbackId(local_id(0));
2297 }
2298 Type* combined_type() const { return combined_type_; }
2299 void set_combined_type(Type* type) { combined_type_ = type; }
2300
2301 // Match special cases.
2302 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
Ben Murdochda12d292016-06-02 14:46:10 +01002303 bool IsLiteralCompareUndefined(Expression** expr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002304 bool IsLiteralCompareNull(Expression** expr);
2305
2306 protected:
2307 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2308 Expression* right, int pos)
2309 : Expression(zone, pos),
2310 op_(op),
2311 left_(left),
2312 right_(right),
Ben Murdoch097c5b22016-05-18 11:27:45 +01002313 combined_type_(Type::None()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002314 DCHECK(Token::IsCompareOp(op));
2315 }
2316 static int parent_num_ids() { return Expression::num_ids(); }
2317
2318 private:
2319 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2320
2321 Token::Value op_;
2322 Expression* left_;
2323 Expression* right_;
2324
2325 Type* combined_type_;
2326};
2327
2328
2329class Spread final : public Expression {
2330 public:
2331 DECLARE_NODE_TYPE(Spread)
2332
2333 Expression* expression() const { return expression_; }
2334 void set_expression(Expression* e) { expression_ = e; }
2335
Ben Murdoch097c5b22016-05-18 11:27:45 +01002336 int expression_position() const { return expr_pos_; }
2337
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002338 static int num_ids() { return parent_num_ids(); }
2339
2340 protected:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002341 Spread(Zone* zone, Expression* expression, int pos, int expr_pos)
2342 : Expression(zone, pos), expression_(expression), expr_pos_(expr_pos) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002343 static int parent_num_ids() { return Expression::num_ids(); }
2344
2345 private:
2346 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2347
2348 Expression* expression_;
Ben Murdoch097c5b22016-05-18 11:27:45 +01002349 int expr_pos_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002350};
2351
2352
2353class Conditional final : public Expression {
2354 public:
2355 DECLARE_NODE_TYPE(Conditional)
2356
2357 Expression* condition() const { return condition_; }
2358 Expression* then_expression() const { return then_expression_; }
2359 Expression* else_expression() const { return else_expression_; }
2360
2361 void set_condition(Expression* e) { condition_ = e; }
2362 void set_then_expression(Expression* e) { then_expression_ = e; }
2363 void set_else_expression(Expression* e) { else_expression_ = e; }
2364
2365 void MarkTail() override {
2366 then_expression_->MarkTail();
2367 else_expression_->MarkTail();
2368 }
2369
2370 static int num_ids() { return parent_num_ids() + 2; }
2371 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2372 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2373
2374 protected:
2375 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2376 Expression* else_expression, int position)
2377 : Expression(zone, position),
2378 condition_(condition),
2379 then_expression_(then_expression),
2380 else_expression_(else_expression) {}
2381 static int parent_num_ids() { return Expression::num_ids(); }
2382
2383 private:
2384 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2385
2386 Expression* condition_;
2387 Expression* then_expression_;
2388 Expression* else_expression_;
2389};
2390
2391
2392class Assignment final : public Expression {
2393 public:
2394 DECLARE_NODE_TYPE(Assignment)
2395
2396 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2397
2398 Token::Value binary_op() const;
2399
2400 Token::Value op() const { return TokenField::decode(bit_field_); }
2401 Expression* target() const { return target_; }
2402 Expression* value() const { return value_; }
2403
2404 void set_target(Expression* e) { target_ = e; }
2405 void set_value(Expression* e) { value_ = e; }
2406
2407 BinaryOperation* binary_operation() const { return binary_operation_; }
2408
2409 // This check relies on the definition order of token in token.h.
2410 bool is_compound() const { return op() > Token::ASSIGN; }
2411
2412 static int num_ids() { return parent_num_ids() + 2; }
2413 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2414
2415 // Type feedback information.
2416 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2417 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2418 bool IsUninitialized() const {
2419 return IsUninitializedField::decode(bit_field_);
2420 }
2421 bool HasNoTypeInformation() {
2422 return IsUninitializedField::decode(bit_field_);
2423 }
2424 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2425 IcCheckType GetKeyType() const override {
2426 return KeyTypeField::decode(bit_field_);
2427 }
2428 KeyedAccessStoreMode GetStoreMode() const override {
2429 return StoreModeField::decode(bit_field_);
2430 }
2431 void set_is_uninitialized(bool b) {
2432 bit_field_ = IsUninitializedField::update(bit_field_, b);
2433 }
2434 void set_key_type(IcCheckType key_type) {
2435 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2436 }
2437 void set_store_mode(KeyedAccessStoreMode mode) {
2438 bit_field_ = StoreModeField::update(bit_field_, mode);
2439 }
2440
2441 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
2442 FeedbackVectorSlotCache* cache) override;
2443 FeedbackVectorSlot AssignmentSlot() const { return slot_; }
2444
2445 protected:
2446 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2447 int pos);
2448 static int parent_num_ids() { return Expression::num_ids(); }
2449
2450 private:
2451 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2452
2453 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2454 class KeyTypeField
2455 : public BitField16<IcCheckType, IsUninitializedField::kNext, 1> {};
2456 class StoreModeField
2457 : public BitField16<KeyedAccessStoreMode, KeyTypeField::kNext, 3> {};
2458 class TokenField : public BitField16<Token::Value, StoreModeField::kNext, 8> {
2459 };
2460
2461 // Starts with 16-bit field, which should get packed together with
2462 // Expression's trailing 16-bit field.
2463 uint16_t bit_field_;
2464 Expression* target_;
2465 Expression* value_;
2466 BinaryOperation* binary_operation_;
2467 SmallMapList receiver_types_;
2468 FeedbackVectorSlot slot_;
2469};
2470
2471
Ben Murdoch097c5b22016-05-18 11:27:45 +01002472// The RewritableExpression class is a wrapper for AST nodes that wait
2473// for some potential rewriting. However, even if such nodes are indeed
2474// rewritten, the RewritableExpression wrapper nodes will survive in the
2475// final AST and should be just ignored, i.e., they should be treated as
2476// equivalent to the wrapped nodes. For this reason and to simplify later
2477// phases, RewritableExpressions are considered as exceptions of AST nodes
2478// in the following sense:
2479//
2480// 1. IsRewritableExpression and AsRewritableExpression behave as usual.
2481// 2. All other Is* and As* methods are practically delegated to the
2482// wrapped node, i.e. IsArrayLiteral() will return true iff the
2483// wrapped node is an array literal.
2484//
2485// Furthermore, an invariant that should be respected is that the wrapped
2486// node is not a RewritableExpression.
2487class RewritableExpression : public Expression {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002488 public:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002489 DECLARE_NODE_TYPE(RewritableExpression)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002490
Ben Murdoch097c5b22016-05-18 11:27:45 +01002491 Expression* expression() const { return expr_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002492 bool is_rewritten() const { return is_rewritten_; }
2493
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002494 void Rewrite(Expression* new_expression) {
2495 DCHECK(!is_rewritten());
2496 DCHECK_NOT_NULL(new_expression);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002497 DCHECK(!new_expression->IsRewritableExpression());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002498 expr_ = new_expression;
2499 is_rewritten_ = true;
2500 }
2501
2502 static int num_ids() { return parent_num_ids(); }
2503
2504 protected:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002505 RewritableExpression(Zone* zone, Expression* expression)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002506 : Expression(zone, expression->position()),
2507 is_rewritten_(false),
Ben Murdoch097c5b22016-05-18 11:27:45 +01002508 expr_(expression) {
2509 DCHECK(!expression->IsRewritableExpression());
2510 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002511
2512 private:
2513 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2514
2515 bool is_rewritten_;
2516 Expression* expr_;
2517};
2518
Ben Murdochda12d292016-06-02 14:46:10 +01002519// Our Yield is different from the JS yield in that it "returns" its argument as
2520// is, without wrapping it in an iterator result object. Such wrapping, if
2521// desired, must be done beforehand (see the parser).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002522class Yield final : public Expression {
2523 public:
2524 DECLARE_NODE_TYPE(Yield)
2525
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002526 Expression* generator_object() const { return generator_object_; }
2527 Expression* expression() const { return expression_; }
Ben Murdochc5610432016-08-08 18:44:38 +01002528 int yield_id() const { return yield_id_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002529
2530 void set_generator_object(Expression* e) { generator_object_ = e; }
2531 void set_expression(Expression* e) { expression_ = e; }
Ben Murdochc5610432016-08-08 18:44:38 +01002532 void set_yield_id(int yield_id) { yield_id_ = yield_id; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002533
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002534 protected:
2535 Yield(Zone* zone, Expression* generator_object, Expression* expression,
Ben Murdochda12d292016-06-02 14:46:10 +01002536 int pos)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002537 : Expression(zone, pos),
2538 generator_object_(generator_object),
Ben Murdochc5610432016-08-08 18:44:38 +01002539 expression_(expression),
2540 yield_id_(-1) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002541
2542 private:
2543 Expression* generator_object_;
2544 Expression* expression_;
Ben Murdochc5610432016-08-08 18:44:38 +01002545 int yield_id_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002546};
2547
2548
2549class Throw final : public Expression {
2550 public:
2551 DECLARE_NODE_TYPE(Throw)
2552
2553 Expression* exception() const { return exception_; }
2554 void set_exception(Expression* e) { exception_ = e; }
2555
2556 protected:
2557 Throw(Zone* zone, Expression* exception, int pos)
2558 : Expression(zone, pos), exception_(exception) {}
2559
2560 private:
2561 Expression* exception_;
2562};
2563
2564
2565class FunctionLiteral final : public Expression {
2566 public:
2567 enum FunctionType {
2568 kAnonymousExpression,
2569 kNamedExpression,
2570 kDeclaration,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002571 kAccessorOrMethod
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002572 };
2573
2574 enum ParameterFlag { kNoDuplicateParameters, kHasDuplicateParameters };
2575
2576 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2577
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002578 DECLARE_NODE_TYPE(FunctionLiteral)
2579
2580 Handle<String> name() const { return raw_name_->string(); }
2581 const AstString* raw_name() const { return raw_name_; }
2582 void set_raw_name(const AstString* name) { raw_name_ = name; }
2583 Scope* scope() const { return scope_; }
2584 ZoneList<Statement*>* body() const { return body_; }
2585 void set_function_token_position(int pos) { function_token_position_ = pos; }
2586 int function_token_position() const { return function_token_position_; }
2587 int start_position() const;
2588 int end_position() const;
2589 int SourceSize() const { return end_position() - start_position(); }
Ben Murdochc5610432016-08-08 18:44:38 +01002590 bool is_declaration() const { return function_type() == kDeclaration; }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002591 bool is_named_expression() const {
Ben Murdochc5610432016-08-08 18:44:38 +01002592 return function_type() == kNamedExpression;
Ben Murdoch097c5b22016-05-18 11:27:45 +01002593 }
2594 bool is_anonymous_expression() const {
Ben Murdochc5610432016-08-08 18:44:38 +01002595 return function_type() == kAnonymousExpression;
Ben Murdoch097c5b22016-05-18 11:27:45 +01002596 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002597 LanguageMode language_mode() const;
2598
2599 static bool NeedsHomeObject(Expression* expr);
2600
2601 int materialized_literal_count() { return materialized_literal_count_; }
2602 int expected_property_count() { return expected_property_count_; }
2603 int parameter_count() { return parameter_count_; }
2604
2605 bool AllowsLazyCompilation();
2606 bool AllowsLazyCompilationWithoutContext();
2607
2608 Handle<String> debug_name() const {
2609 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2610 return raw_name_->string();
2611 }
2612 return inferred_name();
2613 }
2614
2615 Handle<String> inferred_name() const {
2616 if (!inferred_name_.is_null()) {
2617 DCHECK(raw_inferred_name_ == NULL);
2618 return inferred_name_;
2619 }
2620 if (raw_inferred_name_ != NULL) {
2621 return raw_inferred_name_->string();
2622 }
2623 UNREACHABLE();
2624 return Handle<String>();
2625 }
2626
2627 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2628 void set_inferred_name(Handle<String> inferred_name) {
2629 DCHECK(!inferred_name.is_null());
2630 inferred_name_ = inferred_name;
2631 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2632 raw_inferred_name_ = NULL;
2633 }
2634
2635 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2636 DCHECK(raw_inferred_name != NULL);
2637 raw_inferred_name_ = raw_inferred_name;
2638 DCHECK(inferred_name_.is_null());
2639 inferred_name_ = Handle<String>();
2640 }
2641
2642 bool pretenure() const { return Pretenure::decode(bitfield_); }
2643 void set_pretenure() { bitfield_ = Pretenure::update(bitfield_, true); }
2644
2645 bool has_duplicate_parameters() const {
2646 return HasDuplicateParameters::decode(bitfield_);
2647 }
2648
2649 bool is_function() const { return IsFunction::decode(bitfield_); }
2650
2651 // This is used as a heuristic on when to eagerly compile a function
2652 // literal. We consider the following constructs as hints that the
2653 // function will be called immediately:
2654 // - (function() { ... })();
2655 // - var x = function() { ... }();
2656 bool should_eager_compile() const {
2657 return ShouldEagerCompile::decode(bitfield_);
2658 }
2659 void set_should_eager_compile() {
2660 bitfield_ = ShouldEagerCompile::update(bitfield_, true);
2661 }
2662
2663 // A hint that we expect this function to be called (exactly) once,
2664 // i.e. we suspect it's an initialization function.
2665 bool should_be_used_once_hint() const {
2666 return ShouldBeUsedOnceHint::decode(bitfield_);
2667 }
2668 void set_should_be_used_once_hint() {
2669 bitfield_ = ShouldBeUsedOnceHint::update(bitfield_, true);
2670 }
2671
Ben Murdochc5610432016-08-08 18:44:38 +01002672 FunctionType function_type() const {
2673 return FunctionTypeBits::decode(bitfield_);
2674 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002675 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2676
2677 int ast_node_count() { return ast_properties_.node_count(); }
2678 AstProperties::Flags flags() const { return ast_properties_.flags(); }
2679 void set_ast_properties(AstProperties* ast_properties) {
2680 ast_properties_ = *ast_properties;
2681 }
2682 const FeedbackVectorSpec* feedback_vector_spec() const {
2683 return ast_properties_.get_spec();
2684 }
2685 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2686 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2687 void set_dont_optimize_reason(BailoutReason reason) {
2688 dont_optimize_reason_ = reason;
2689 }
2690
Ben Murdoch097c5b22016-05-18 11:27:45 +01002691 bool IsAnonymousFunctionDefinition() const final {
2692 return is_anonymous_expression();
2693 }
2694
Ben Murdochc5610432016-08-08 18:44:38 +01002695 int yield_count() { return yield_count_; }
2696 void set_yield_count(int yield_count) { yield_count_ = yield_count; }
2697
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002698 protected:
2699 FunctionLiteral(Zone* zone, const AstString* name,
2700 AstValueFactory* ast_value_factory, Scope* scope,
2701 ZoneList<Statement*>* body, int materialized_literal_count,
2702 int expected_property_count, int parameter_count,
2703 FunctionType function_type,
2704 ParameterFlag has_duplicate_parameters,
2705 EagerCompileHint eager_compile_hint, FunctionKind kind,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002706 int position, bool is_function)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002707 : Expression(zone, position),
2708 raw_name_(name),
2709 scope_(scope),
2710 body_(body),
2711 raw_inferred_name_(ast_value_factory->empty_string()),
2712 ast_properties_(zone),
2713 dont_optimize_reason_(kNoReason),
2714 materialized_literal_count_(materialized_literal_count),
2715 expected_property_count_(expected_property_count),
2716 parameter_count_(parameter_count),
Ben Murdochc5610432016-08-08 18:44:38 +01002717 function_token_position_(RelocInfo::kNoPosition),
2718 yield_count_(0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002719 bitfield_ =
Ben Murdochc5610432016-08-08 18:44:38 +01002720 FunctionTypeBits::encode(function_type) | Pretenure::encode(false) |
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002721 HasDuplicateParameters::encode(has_duplicate_parameters ==
2722 kHasDuplicateParameters) |
Ben Murdoch097c5b22016-05-18 11:27:45 +01002723 IsFunction::encode(is_function) |
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002724 ShouldEagerCompile::encode(eager_compile_hint == kShouldEagerCompile) |
2725 FunctionKindBits::encode(kind) | ShouldBeUsedOnceHint::encode(false);
2726 DCHECK(IsValidFunctionKind(kind));
2727 }
2728
2729 private:
Ben Murdochc5610432016-08-08 18:44:38 +01002730 class FunctionTypeBits : public BitField16<FunctionType, 0, 2> {};
2731 class Pretenure : public BitField16<bool, 2, 1> {};
2732 class HasDuplicateParameters : public BitField16<bool, 3, 1> {};
2733 class IsFunction : public BitField16<bool, 4, 1> {};
2734 class ShouldEagerCompile : public BitField16<bool, 5, 1> {};
2735 class ShouldBeUsedOnceHint : public BitField16<bool, 6, 1> {};
2736 class FunctionKindBits : public BitField16<FunctionKind, 7, 9> {};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002737
2738 // Start with 16-bit field, which should get packed together
2739 // with Expression's trailing 16-bit field.
2740 uint16_t bitfield_;
2741
2742 const AstString* raw_name_;
2743 Scope* scope_;
2744 ZoneList<Statement*>* body_;
2745 const AstString* raw_inferred_name_;
2746 Handle<String> inferred_name_;
2747 AstProperties ast_properties_;
2748 BailoutReason dont_optimize_reason_;
2749
2750 int materialized_literal_count_;
2751 int expected_property_count_;
2752 int parameter_count_;
2753 int function_token_position_;
Ben Murdochc5610432016-08-08 18:44:38 +01002754 int yield_count_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002755};
2756
2757
2758class ClassLiteral final : public Expression {
2759 public:
2760 typedef ObjectLiteralProperty Property;
2761
2762 DECLARE_NODE_TYPE(ClassLiteral)
2763
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002764 Scope* scope() const { return scope_; }
2765 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2766 Expression* extends() const { return extends_; }
2767 void set_extends(Expression* e) { extends_ = e; }
2768 FunctionLiteral* constructor() const { return constructor_; }
2769 void set_constructor(FunctionLiteral* f) { constructor_ = f; }
2770 ZoneList<Property*>* properties() const { return properties_; }
2771 int start_position() const { return position(); }
2772 int end_position() const { return end_position_; }
2773
2774 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2775 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2776 BailoutId ExitId() { return BailoutId(local_id(2)); }
2777 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002778 BailoutId PrototypeId() { return BailoutId(local_id(4)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002779
2780 // Return an AST id for a property that is used in simulate instructions.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002781 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 5)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002782
2783 // Unlike other AST nodes, this number of bailout IDs allocated for an
2784 // ClassLiteral can vary, so num_ids() is not a static method.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002785 int num_ids() const { return parent_num_ids() + 5 + properties()->length(); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002786
2787 // Object literals need one feedback slot for each non-trivial value, as well
2788 // as some slots for home objects.
2789 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
2790 FeedbackVectorSlotCache* cache) override;
2791
2792 bool NeedsProxySlot() const {
2793 return class_variable_proxy() != nullptr &&
2794 class_variable_proxy()->var()->IsUnallocated();
2795 }
2796
Ben Murdoch097c5b22016-05-18 11:27:45 +01002797 FeedbackVectorSlot PrototypeSlot() const { return prototype_slot_; }
2798 FeedbackVectorSlot ProxySlot() const { return proxy_slot_; }
2799
2800 bool IsAnonymousFunctionDefinition() const final {
2801 return constructor()->raw_name()->length() == 0;
2802 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002803
2804 protected:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002805 ClassLiteral(Zone* zone, Scope* scope, VariableProxy* class_variable_proxy,
2806 Expression* extends, FunctionLiteral* constructor,
2807 ZoneList<Property*>* properties, int start_position,
2808 int end_position)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002809 : Expression(zone, start_position),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002810 scope_(scope),
2811 class_variable_proxy_(class_variable_proxy),
2812 extends_(extends),
2813 constructor_(constructor),
2814 properties_(properties),
2815 end_position_(end_position) {}
2816
2817 static int parent_num_ids() { return Expression::num_ids(); }
2818
2819 private:
2820 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2821
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002822 Scope* scope_;
2823 VariableProxy* class_variable_proxy_;
2824 Expression* extends_;
2825 FunctionLiteral* constructor_;
2826 ZoneList<Property*>* properties_;
2827 int end_position_;
Ben Murdoch097c5b22016-05-18 11:27:45 +01002828 FeedbackVectorSlot prototype_slot_;
2829 FeedbackVectorSlot proxy_slot_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002830};
2831
2832
2833class NativeFunctionLiteral final : public Expression {
2834 public:
2835 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2836
2837 Handle<String> name() const { return name_->string(); }
2838 v8::Extension* extension() const { return extension_; }
2839
2840 protected:
2841 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2842 v8::Extension* extension, int pos)
2843 : Expression(zone, pos), name_(name), extension_(extension) {}
2844
2845 private:
2846 const AstRawString* name_;
2847 v8::Extension* extension_;
2848};
2849
2850
2851class ThisFunction final : public Expression {
2852 public:
2853 DECLARE_NODE_TYPE(ThisFunction)
2854
2855 protected:
2856 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2857};
2858
2859
2860class SuperPropertyReference final : public Expression {
2861 public:
2862 DECLARE_NODE_TYPE(SuperPropertyReference)
2863
2864 VariableProxy* this_var() const { return this_var_; }
2865 void set_this_var(VariableProxy* v) { this_var_ = v; }
2866 Expression* home_object() const { return home_object_; }
2867 void set_home_object(Expression* e) { home_object_ = e; }
2868
2869 protected:
2870 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2871 Expression* home_object, int pos)
2872 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2873 DCHECK(this_var->is_this());
2874 DCHECK(home_object->IsProperty());
2875 }
2876
2877 private:
2878 VariableProxy* this_var_;
2879 Expression* home_object_;
2880};
2881
2882
2883class SuperCallReference final : public Expression {
2884 public:
2885 DECLARE_NODE_TYPE(SuperCallReference)
2886
2887 VariableProxy* this_var() const { return this_var_; }
2888 void set_this_var(VariableProxy* v) { this_var_ = v; }
2889 VariableProxy* new_target_var() const { return new_target_var_; }
2890 void set_new_target_var(VariableProxy* v) { new_target_var_ = v; }
2891 VariableProxy* this_function_var() const { return this_function_var_; }
2892 void set_this_function_var(VariableProxy* v) { this_function_var_ = v; }
2893
2894 protected:
2895 SuperCallReference(Zone* zone, VariableProxy* this_var,
2896 VariableProxy* new_target_var,
2897 VariableProxy* this_function_var, int pos)
2898 : Expression(zone, pos),
2899 this_var_(this_var),
2900 new_target_var_(new_target_var),
2901 this_function_var_(this_function_var) {
2902 DCHECK(this_var->is_this());
2903 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo(".new.target"));
2904 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2905 }
2906
2907 private:
2908 VariableProxy* this_var_;
2909 VariableProxy* new_target_var_;
2910 VariableProxy* this_function_var_;
2911};
2912
2913
2914// This class is produced when parsing the () in arrow functions without any
2915// arguments and is not actually a valid expression.
2916class EmptyParentheses final : public Expression {
2917 public:
2918 DECLARE_NODE_TYPE(EmptyParentheses)
2919
2920 private:
2921 EmptyParentheses(Zone* zone, int pos) : Expression(zone, pos) {}
2922};
2923
2924
2925#undef DECLARE_NODE_TYPE
2926
2927
2928// ----------------------------------------------------------------------------
2929// Basic visitor
2930// - leaf node visitors are abstract.
2931
2932class AstVisitor BASE_EMBEDDED {
2933 public:
2934 AstVisitor() {}
2935 virtual ~AstVisitor() {}
2936
2937 // Stack overflow check and dynamic dispatch.
2938 virtual void Visit(AstNode* node) = 0;
2939
2940 // Iteration left-to-right.
2941 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
2942 virtual void VisitStatements(ZoneList<Statement*>* statements);
2943 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
2944
2945 // Individual AST nodes.
2946#define DEF_VISIT(type) \
2947 virtual void Visit##type(type* node) = 0;
2948 AST_NODE_LIST(DEF_VISIT)
2949#undef DEF_VISIT
2950};
2951
2952#define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
2953 public: \
2954 void Visit(AstNode* node) final { \
2955 if (!CheckStackOverflow()) node->Accept(this); \
2956 } \
2957 \
2958 void SetStackOverflow() { stack_overflow_ = true; } \
2959 void ClearStackOverflow() { stack_overflow_ = false; } \
2960 bool HasStackOverflow() const { return stack_overflow_; } \
2961 \
2962 bool CheckStackOverflow() { \
2963 if (stack_overflow_) return true; \
2964 if (GetCurrentStackPosition() < stack_limit_) { \
2965 stack_overflow_ = true; \
2966 return true; \
2967 } \
2968 return false; \
2969 } \
2970 \
2971 private: \
2972 void InitializeAstVisitor(Isolate* isolate) { \
2973 stack_limit_ = isolate->stack_guard()->real_climit(); \
2974 stack_overflow_ = false; \
2975 } \
2976 \
2977 void InitializeAstVisitor(uintptr_t stack_limit) { \
2978 stack_limit_ = stack_limit; \
2979 stack_overflow_ = false; \
2980 } \
2981 \
2982 uintptr_t stack_limit_; \
2983 bool stack_overflow_
2984
2985#define DEFINE_AST_REWRITER_SUBCLASS_MEMBERS() \
2986 public: \
2987 AstNode* Rewrite(AstNode* node) { \
2988 DCHECK_NULL(replacement_); \
2989 DCHECK_NOT_NULL(node); \
2990 Visit(node); \
2991 if (HasStackOverflow()) return node; \
2992 if (replacement_ == nullptr) return node; \
2993 AstNode* result = replacement_; \
2994 replacement_ = nullptr; \
2995 return result; \
2996 } \
2997 \
2998 private: \
2999 void InitializeAstRewriter(Isolate* isolate) { \
3000 InitializeAstVisitor(isolate); \
3001 replacement_ = nullptr; \
3002 } \
3003 \
3004 void InitializeAstRewriter(uintptr_t stack_limit) { \
3005 InitializeAstVisitor(stack_limit); \
3006 replacement_ = nullptr; \
3007 } \
3008 \
3009 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); \
3010 \
3011 protected: \
3012 AstNode* replacement_
3013
3014// Generic macro for rewriting things; `GET` is the expression to be
3015// rewritten; `SET` is a command that should do the rewriting, i.e.
3016// something sensible with the variable called `replacement`.
3017#define AST_REWRITE(Type, GET, SET) \
3018 do { \
3019 DCHECK(!HasStackOverflow()); \
3020 DCHECK_NULL(replacement_); \
3021 Visit(GET); \
3022 if (HasStackOverflow()) return; \
3023 if (replacement_ == nullptr) break; \
3024 Type* replacement = reinterpret_cast<Type*>(replacement_); \
3025 do { \
3026 SET; \
3027 } while (false); \
3028 replacement_ = nullptr; \
3029 } while (false)
3030
3031// Macro for rewriting object properties; it assumes that `object` has
3032// `property` with a public getter and setter.
3033#define AST_REWRITE_PROPERTY(Type, object, property) \
3034 do { \
3035 auto _obj = (object); \
3036 AST_REWRITE(Type, _obj->property(), _obj->set_##property(replacement)); \
3037 } while (false)
3038
3039// Macro for rewriting list elements; it assumes that `list` has methods
3040// `at` and `Set`.
3041#define AST_REWRITE_LIST_ELEMENT(Type, list, index) \
3042 do { \
3043 auto _list = (list); \
3044 auto _index = (index); \
3045 AST_REWRITE(Type, _list->at(_index), _list->Set(_index, replacement)); \
3046 } while (false)
3047
3048
3049// ----------------------------------------------------------------------------
Ben Murdochc5610432016-08-08 18:44:38 +01003050// Traversing visitor
3051// - fully traverses the entire AST.
3052
3053class AstTraversalVisitor : public AstVisitor {
3054 public:
3055 explicit AstTraversalVisitor(Isolate* isolate);
3056 virtual ~AstTraversalVisitor() {}
3057
3058 // Iteration left-to-right.
3059 void VisitDeclarations(ZoneList<Declaration*>* declarations) override;
3060 void VisitStatements(ZoneList<Statement*>* statements) override;
3061 void VisitExpressions(ZoneList<Expression*>* expressions) override;
3062
3063// Individual nodes
3064#define DECLARE_VISIT(type) void Visit##type(type* node) override;
3065 AST_NODE_LIST(DECLARE_VISIT)
3066#undef DECLARE_VISIT
3067
3068 private:
3069 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
3070 DISALLOW_COPY_AND_ASSIGN(AstTraversalVisitor);
3071};
3072
3073// ----------------------------------------------------------------------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003074// AstNode factory
3075
3076class AstNodeFactory final BASE_EMBEDDED {
3077 public:
3078 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3079 : local_zone_(ast_value_factory->zone()),
3080 parser_zone_(ast_value_factory->zone()),
3081 ast_value_factory_(ast_value_factory) {}
3082
3083 AstValueFactory* ast_value_factory() const { return ast_value_factory_; }
3084
Ben Murdoch097c5b22016-05-18 11:27:45 +01003085 VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy,
3086 VariableMode mode, Scope* scope,
3087 int pos) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003088 return new (parser_zone_)
Ben Murdoch097c5b22016-05-18 11:27:45 +01003089 VariableDeclaration(parser_zone_, proxy, mode, scope, pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003090 }
3091
3092 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3093 VariableMode mode,
3094 FunctionLiteral* fun,
3095 Scope* scope,
3096 int pos) {
3097 return new (parser_zone_)
3098 FunctionDeclaration(parser_zone_, proxy, mode, fun, scope, pos);
3099 }
3100
3101 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3102 const AstRawString* import_name,
3103 const AstRawString* module_specifier,
3104 Scope* scope, int pos) {
3105 return new (parser_zone_) ImportDeclaration(
3106 parser_zone_, proxy, import_name, module_specifier, scope, pos);
3107 }
3108
3109 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3110 Scope* scope,
3111 int pos) {
3112 return new (parser_zone_)
3113 ExportDeclaration(parser_zone_, proxy, scope, pos);
3114 }
3115
3116 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3117 bool ignore_completion_value, int pos) {
3118 return new (local_zone_)
3119 Block(local_zone_, labels, capacity, ignore_completion_value, pos);
3120 }
3121
3122#define STATEMENT_WITH_LABELS(NodeType) \
3123 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3124 return new (local_zone_) NodeType(local_zone_, labels, pos); \
3125 }
3126 STATEMENT_WITH_LABELS(DoWhileStatement)
3127 STATEMENT_WITH_LABELS(WhileStatement)
3128 STATEMENT_WITH_LABELS(ForStatement)
3129 STATEMENT_WITH_LABELS(SwitchStatement)
3130#undef STATEMENT_WITH_LABELS
3131
3132 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3133 ZoneList<const AstRawString*>* labels,
3134 int pos) {
3135 switch (visit_mode) {
3136 case ForEachStatement::ENUMERATE: {
3137 return new (local_zone_) ForInStatement(local_zone_, labels, pos);
3138 }
3139 case ForEachStatement::ITERATE: {
3140 return new (local_zone_) ForOfStatement(local_zone_, labels, pos);
3141 }
3142 }
3143 UNREACHABLE();
3144 return NULL;
3145 }
3146
3147 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3148 return new (local_zone_) ExpressionStatement(local_zone_, expression, pos);
3149 }
3150
3151 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3152 return new (local_zone_) ContinueStatement(local_zone_, target, pos);
3153 }
3154
3155 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3156 return new (local_zone_) BreakStatement(local_zone_, target, pos);
3157 }
3158
3159 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3160 return new (local_zone_) ReturnStatement(local_zone_, expression, pos);
3161 }
3162
3163 WithStatement* NewWithStatement(Scope* scope,
3164 Expression* expression,
3165 Statement* statement,
3166 int pos) {
3167 return new (local_zone_)
3168 WithStatement(local_zone_, scope, expression, statement, pos);
3169 }
3170
3171 IfStatement* NewIfStatement(Expression* condition,
3172 Statement* then_statement,
3173 Statement* else_statement,
3174 int pos) {
3175 return new (local_zone_) IfStatement(local_zone_, condition, then_statement,
3176 else_statement, pos);
3177 }
3178
3179 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3180 Variable* variable,
3181 Block* catch_block, int pos) {
Ben Murdochda12d292016-06-02 14:46:10 +01003182 return new (local_zone_) TryCatchStatement(
3183 local_zone_, try_block, scope, variable, catch_block, true, pos);
3184 }
3185
3186 TryCatchStatement* NewTryCatchStatementForReThrow(Block* try_block,
3187 Scope* scope,
3188 Variable* variable,
3189 Block* catch_block,
3190 int pos) {
3191 return new (local_zone_) TryCatchStatement(
3192 local_zone_, try_block, scope, variable, catch_block, false, pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003193 }
3194
3195 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3196 Block* finally_block, int pos) {
3197 return new (local_zone_)
3198 TryFinallyStatement(local_zone_, try_block, finally_block, pos);
3199 }
3200
3201 DebuggerStatement* NewDebuggerStatement(int pos) {
3202 return new (local_zone_) DebuggerStatement(local_zone_, pos);
3203 }
3204
3205 EmptyStatement* NewEmptyStatement(int pos) {
3206 return new (local_zone_) EmptyStatement(local_zone_, pos);
3207 }
3208
3209 SloppyBlockFunctionStatement* NewSloppyBlockFunctionStatement(
3210 Statement* statement, Scope* scope) {
3211 return new (parser_zone_)
3212 SloppyBlockFunctionStatement(parser_zone_, statement, scope);
3213 }
3214
3215 CaseClause* NewCaseClause(
3216 Expression* label, ZoneList<Statement*>* statements, int pos) {
3217 return new (local_zone_) CaseClause(local_zone_, label, statements, pos);
3218 }
3219
3220 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3221 return new (local_zone_)
3222 Literal(local_zone_, ast_value_factory_->NewString(string), pos);
3223 }
3224
3225 // A JavaScript symbol (ECMA-262 edition 6).
3226 Literal* NewSymbolLiteral(const char* name, int pos) {
3227 return new (local_zone_)
3228 Literal(local_zone_, ast_value_factory_->NewSymbol(name), pos);
3229 }
3230
3231 Literal* NewNumberLiteral(double number, int pos, bool with_dot = false) {
3232 return new (local_zone_) Literal(
3233 local_zone_, ast_value_factory_->NewNumber(number, with_dot), pos);
3234 }
3235
3236 Literal* NewSmiLiteral(int number, int pos) {
3237 return new (local_zone_)
3238 Literal(local_zone_, ast_value_factory_->NewSmi(number), pos);
3239 }
3240
3241 Literal* NewBooleanLiteral(bool b, int pos) {
3242 return new (local_zone_)
3243 Literal(local_zone_, ast_value_factory_->NewBoolean(b), pos);
3244 }
3245
3246 Literal* NewNullLiteral(int pos) {
3247 return new (local_zone_)
3248 Literal(local_zone_, ast_value_factory_->NewNull(), pos);
3249 }
3250
3251 Literal* NewUndefinedLiteral(int pos) {
3252 return new (local_zone_)
3253 Literal(local_zone_, ast_value_factory_->NewUndefined(), pos);
3254 }
3255
3256 Literal* NewTheHoleLiteral(int pos) {
3257 return new (local_zone_)
3258 Literal(local_zone_, ast_value_factory_->NewTheHole(), pos);
3259 }
3260
3261 ObjectLiteral* NewObjectLiteral(
3262 ZoneList<ObjectLiteral::Property*>* properties,
3263 int literal_index,
3264 int boilerplate_properties,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003265 int pos) {
Ben Murdochda12d292016-06-02 14:46:10 +01003266 return new (local_zone_) ObjectLiteral(
3267 local_zone_, properties, literal_index, boilerplate_properties, pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003268 }
3269
3270 ObjectLiteral::Property* NewObjectLiteralProperty(
3271 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3272 bool is_static, bool is_computed_name) {
3273 return new (local_zone_)
3274 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3275 }
3276
3277 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3278 Expression* value,
3279 bool is_static,
3280 bool is_computed_name) {
3281 return new (local_zone_) ObjectLiteral::Property(
3282 ast_value_factory_, key, value, is_static, is_computed_name);
3283 }
3284
3285 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern, int flags,
Ben Murdochda12d292016-06-02 14:46:10 +01003286 int literal_index, int pos) {
3287 return new (local_zone_)
3288 RegExpLiteral(local_zone_, pattern, flags, literal_index, pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003289 }
3290
3291 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3292 int literal_index,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003293 int pos) {
3294 return new (local_zone_)
Ben Murdochda12d292016-06-02 14:46:10 +01003295 ArrayLiteral(local_zone_, values, -1, literal_index, pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003296 }
3297
3298 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3299 int first_spread_index, int literal_index,
Ben Murdochda12d292016-06-02 14:46:10 +01003300 int pos) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003301 return new (local_zone_) ArrayLiteral(
Ben Murdochda12d292016-06-02 14:46:10 +01003302 local_zone_, values, first_spread_index, literal_index, pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003303 }
3304
3305 VariableProxy* NewVariableProxy(Variable* var,
3306 int start_position = RelocInfo::kNoPosition,
3307 int end_position = RelocInfo::kNoPosition) {
3308 return new (parser_zone_)
3309 VariableProxy(parser_zone_, var, start_position, end_position);
3310 }
3311
3312 VariableProxy* NewVariableProxy(const AstRawString* name,
3313 Variable::Kind variable_kind,
3314 int start_position = RelocInfo::kNoPosition,
3315 int end_position = RelocInfo::kNoPosition) {
3316 DCHECK_NOT_NULL(name);
3317 return new (parser_zone_) VariableProxy(parser_zone_, name, variable_kind,
3318 start_position, end_position);
3319 }
3320
3321 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3322 return new (local_zone_) Property(local_zone_, obj, key, pos);
3323 }
3324
3325 Call* NewCall(Expression* expression,
3326 ZoneList<Expression*>* arguments,
3327 int pos) {
3328 return new (local_zone_) Call(local_zone_, expression, arguments, pos);
3329 }
3330
3331 CallNew* NewCallNew(Expression* expression,
3332 ZoneList<Expression*>* arguments,
3333 int pos) {
3334 return new (local_zone_) CallNew(local_zone_, expression, arguments, pos);
3335 }
3336
3337 CallRuntime* NewCallRuntime(Runtime::FunctionId id,
3338 ZoneList<Expression*>* arguments, int pos) {
3339 return new (local_zone_)
3340 CallRuntime(local_zone_, Runtime::FunctionForId(id), arguments, pos);
3341 }
3342
3343 CallRuntime* NewCallRuntime(const Runtime::Function* function,
3344 ZoneList<Expression*>* arguments, int pos) {
3345 return new (local_zone_) CallRuntime(local_zone_, function, arguments, pos);
3346 }
3347
3348 CallRuntime* NewCallRuntime(int context_index,
3349 ZoneList<Expression*>* arguments, int pos) {
3350 return new (local_zone_)
3351 CallRuntime(local_zone_, context_index, arguments, pos);
3352 }
3353
3354 UnaryOperation* NewUnaryOperation(Token::Value op,
3355 Expression* expression,
3356 int pos) {
3357 return new (local_zone_) UnaryOperation(local_zone_, op, expression, pos);
3358 }
3359
3360 BinaryOperation* NewBinaryOperation(Token::Value op,
3361 Expression* left,
3362 Expression* right,
3363 int pos) {
3364 return new (local_zone_) BinaryOperation(local_zone_, op, left, right, pos);
3365 }
3366
3367 CountOperation* NewCountOperation(Token::Value op,
3368 bool is_prefix,
3369 Expression* expr,
3370 int pos) {
3371 return new (local_zone_)
3372 CountOperation(local_zone_, op, is_prefix, expr, pos);
3373 }
3374
3375 CompareOperation* NewCompareOperation(Token::Value op,
3376 Expression* left,
3377 Expression* right,
3378 int pos) {
3379 return new (local_zone_)
3380 CompareOperation(local_zone_, op, left, right, pos);
3381 }
3382
Ben Murdoch097c5b22016-05-18 11:27:45 +01003383 Spread* NewSpread(Expression* expression, int pos, int expr_pos) {
3384 return new (local_zone_) Spread(local_zone_, expression, pos, expr_pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003385 }
3386
3387 Conditional* NewConditional(Expression* condition,
3388 Expression* then_expression,
3389 Expression* else_expression,
3390 int position) {
3391 return new (local_zone_) Conditional(
3392 local_zone_, condition, then_expression, else_expression, position);
3393 }
3394
Ben Murdoch097c5b22016-05-18 11:27:45 +01003395 RewritableExpression* NewRewritableExpression(Expression* expression) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003396 DCHECK_NOT_NULL(expression);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003397 return new (local_zone_) RewritableExpression(local_zone_, expression);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003398 }
3399
3400 Assignment* NewAssignment(Token::Value op,
3401 Expression* target,
3402 Expression* value,
3403 int pos) {
3404 DCHECK(Token::IsAssignmentOp(op));
3405 Assignment* assign =
3406 new (local_zone_) Assignment(local_zone_, op, target, value, pos);
3407 if (assign->is_compound()) {
3408 DCHECK(Token::IsAssignmentOp(op));
3409 assign->binary_operation_ =
3410 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3411 }
3412 return assign;
3413 }
3414
3415 Yield* NewYield(Expression *generator_object,
3416 Expression* expression,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003417 int pos) {
3418 if (!expression) expression = NewUndefinedLiteral(pos);
3419 return new (local_zone_)
Ben Murdochda12d292016-06-02 14:46:10 +01003420 Yield(local_zone_, generator_object, expression, pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003421 }
3422
3423 Throw* NewThrow(Expression* exception, int pos) {
3424 return new (local_zone_) Throw(local_zone_, exception, pos);
3425 }
3426
3427 FunctionLiteral* NewFunctionLiteral(
3428 const AstRawString* name, Scope* scope, ZoneList<Statement*>* body,
3429 int materialized_literal_count, int expected_property_count,
3430 int parameter_count,
3431 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3432 FunctionLiteral::FunctionType function_type,
3433 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3434 int position) {
3435 return new (parser_zone_) FunctionLiteral(
3436 parser_zone_, name, ast_value_factory_, scope, body,
3437 materialized_literal_count, expected_property_count, parameter_count,
3438 function_type, has_duplicate_parameters, eager_compile_hint, kind,
Ben Murdoch097c5b22016-05-18 11:27:45 +01003439 position, true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003440 }
3441
Ben Murdoch097c5b22016-05-18 11:27:45 +01003442 // Creates a FunctionLiteral representing a top-level script, the
3443 // result of an eval (top-level or otherwise), or the result of calling
3444 // the Function constructor.
3445 FunctionLiteral* NewScriptOrEvalFunctionLiteral(
3446 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3447 int expected_property_count) {
3448 return new (parser_zone_) FunctionLiteral(
3449 parser_zone_, ast_value_factory_->empty_string(), ast_value_factory_,
3450 scope, body, materialized_literal_count, expected_property_count, 0,
3451 FunctionLiteral::kAnonymousExpression,
3452 FunctionLiteral::kNoDuplicateParameters,
3453 FunctionLiteral::kShouldLazyCompile, FunctionKind::kNormalFunction, 0,
3454 false);
3455 }
3456
3457 ClassLiteral* NewClassLiteral(Scope* scope, VariableProxy* proxy,
3458 Expression* extends,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003459 FunctionLiteral* constructor,
3460 ZoneList<ObjectLiteral::Property*>* properties,
3461 int start_position, int end_position) {
3462 return new (parser_zone_)
Ben Murdoch097c5b22016-05-18 11:27:45 +01003463 ClassLiteral(parser_zone_, scope, proxy, extends, constructor,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003464 properties, start_position, end_position);
3465 }
3466
3467 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3468 v8::Extension* extension,
3469 int pos) {
3470 return new (parser_zone_)
3471 NativeFunctionLiteral(parser_zone_, name, extension, pos);
3472 }
3473
3474 DoExpression* NewDoExpression(Block* block, Variable* result_var, int pos) {
3475 VariableProxy* result = NewVariableProxy(result_var, pos);
3476 return new (parser_zone_) DoExpression(parser_zone_, block, result, pos);
3477 }
3478
3479 ThisFunction* NewThisFunction(int pos) {
3480 return new (local_zone_) ThisFunction(local_zone_, pos);
3481 }
3482
3483 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3484 Expression* home_object,
3485 int pos) {
3486 return new (parser_zone_)
3487 SuperPropertyReference(parser_zone_, this_var, home_object, pos);
3488 }
3489
3490 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3491 VariableProxy* new_target_var,
3492 VariableProxy* this_function_var,
3493 int pos) {
3494 return new (parser_zone_) SuperCallReference(
3495 parser_zone_, this_var, new_target_var, this_function_var, pos);
3496 }
3497
3498 EmptyParentheses* NewEmptyParentheses(int pos) {
3499 return new (local_zone_) EmptyParentheses(local_zone_, pos);
3500 }
3501
3502 Zone* zone() const { return local_zone_; }
3503
3504 // Handles use of temporary zones when parsing inner function bodies.
3505 class BodyScope {
3506 public:
3507 BodyScope(AstNodeFactory* factory, Zone* temp_zone, bool use_temp_zone)
3508 : factory_(factory), prev_zone_(factory->local_zone_) {
3509 if (use_temp_zone) {
3510 factory->local_zone_ = temp_zone;
3511 }
3512 }
3513
3514 ~BodyScope() { factory_->local_zone_ = prev_zone_; }
3515
3516 private:
3517 AstNodeFactory* factory_;
3518 Zone* prev_zone_;
3519 };
3520
3521 private:
3522 // This zone may be deallocated upon returning from parsing a function body
3523 // which we can guarantee is not going to be compiled or have its AST
3524 // inspected.
3525 // See ParseFunctionLiteral in parser.cc for preconditions.
3526 Zone* local_zone_;
3527 // ZoneObjects which need to persist until scope analysis must be allocated in
3528 // the parser-level zone.
3529 Zone* parser_zone_;
3530 AstValueFactory* ast_value_factory_;
3531};
3532
3533
Ben Murdoch097c5b22016-05-18 11:27:45 +01003534// Type testing & conversion functions overridden by concrete subclasses.
3535// Inline functions for AstNode.
3536
3537#define DECLARE_NODE_FUNCTIONS(type) \
3538 bool AstNode::Is##type() const { \
3539 NodeType mine = node_type(); \
3540 if (mine == AstNode::kRewritableExpression && \
3541 AstNode::k##type != AstNode::kRewritableExpression) \
3542 mine = reinterpret_cast<const RewritableExpression*>(this) \
3543 ->expression() \
3544 ->node_type(); \
3545 return mine == AstNode::k##type; \
3546 } \
3547 type* AstNode::As##type() { \
3548 NodeType mine = node_type(); \
3549 AstNode* result = this; \
3550 if (mine == AstNode::kRewritableExpression && \
3551 AstNode::k##type != AstNode::kRewritableExpression) { \
3552 result = \
3553 reinterpret_cast<const RewritableExpression*>(this)->expression(); \
3554 mine = result->node_type(); \
3555 } \
3556 return mine == AstNode::k##type ? reinterpret_cast<type*>(result) : NULL; \
3557 } \
3558 const type* AstNode::As##type() const { \
3559 NodeType mine = node_type(); \
3560 const AstNode* result = this; \
3561 if (mine == AstNode::kRewritableExpression && \
3562 AstNode::k##type != AstNode::kRewritableExpression) { \
3563 result = \
3564 reinterpret_cast<const RewritableExpression*>(this)->expression(); \
3565 mine = result->node_type(); \
3566 } \
3567 return mine == AstNode::k##type ? reinterpret_cast<const type*>(result) \
3568 : NULL; \
3569 }
3570AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
3571#undef DECLARE_NODE_FUNCTIONS
3572
3573
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003574} // namespace internal
3575} // namespace v8
3576
3577#endif // V8_AST_AST_H_