blob: 52bac8efbe1a3ad63f8988f4187c5d9218abbc81 [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
340 // Expression type bounds
341 Bounds bounds() const { return bounds_; }
342 void set_bounds(Bounds bounds) { bounds_ = bounds; }
343
344 // Type feedback information for assignments and properties.
345 virtual bool IsMonomorphic() {
346 UNREACHABLE();
347 return false;
348 }
349 virtual SmallMapList* GetReceiverTypes() {
350 UNREACHABLE();
351 return NULL;
352 }
353 virtual KeyedAccessStoreMode GetStoreMode() const {
354 UNREACHABLE();
355 return STANDARD_STORE;
356 }
357 virtual IcCheckType GetKeyType() const {
358 UNREACHABLE();
359 return ELEMENT;
360 }
361
362 // TODO(rossberg): this should move to its own AST node eventually.
363 virtual void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle);
364 uint16_t to_boolean_types() const {
365 return ToBooleanTypesField::decode(bit_field_);
366 }
367
368 void set_base_id(int id) { base_id_ = id; }
369 static int num_ids() { return parent_num_ids() + 2; }
370 BailoutId id() const { return BailoutId(local_id(0)); }
371 TypeFeedbackId test_id() const { return TypeFeedbackId(local_id(1)); }
372
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000373 protected:
374 Expression(Zone* zone, int pos)
375 : AstNode(pos),
376 base_id_(BailoutId::None().ToInt()),
377 bounds_(Bounds::Unbounded()),
378 bit_field_(0) {}
379 static int parent_num_ids() { return 0; }
380 void set_to_boolean_types(uint16_t types) {
381 bit_field_ = ToBooleanTypesField::update(bit_field_, types);
382 }
383
384 int base_id() const {
385 DCHECK(!BailoutId(base_id_).IsNone());
386 return base_id_;
387 }
388
389 private:
390 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
391
392 int base_id_;
393 Bounds bounds_;
394 class ToBooleanTypesField : public BitField16<uint16_t, 0, 9> {};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000395 uint16_t bit_field_;
396 // Ends with 16-bit field; deriving classes in turn begin with
397 // 16-bit fields for optimum packing efficiency.
398};
399
400
401class BreakableStatement : public Statement {
402 public:
403 enum BreakableType {
404 TARGET_FOR_ANONYMOUS,
405 TARGET_FOR_NAMED_ONLY
406 };
407
408 // The labels associated with this statement. May be NULL;
409 // if it is != NULL, guaranteed to contain at least one entry.
410 ZoneList<const AstRawString*>* labels() const { return labels_; }
411
412 // Type testing & conversion.
413 BreakableStatement* AsBreakableStatement() final { return this; }
414
415 // Code generation
416 Label* break_target() { return &break_target_; }
417
418 // Testers.
419 bool is_target_for_anonymous() const {
420 return breakable_type_ == TARGET_FOR_ANONYMOUS;
421 }
422
423 void set_base_id(int id) { base_id_ = id; }
424 static int num_ids() { return parent_num_ids() + 2; }
425 BailoutId EntryId() const { return BailoutId(local_id(0)); }
426 BailoutId ExitId() const { return BailoutId(local_id(1)); }
427
428 protected:
429 BreakableStatement(Zone* zone, ZoneList<const AstRawString*>* labels,
430 BreakableType breakable_type, int position)
431 : Statement(zone, position),
432 labels_(labels),
433 breakable_type_(breakable_type),
434 base_id_(BailoutId::None().ToInt()) {
435 DCHECK(labels == NULL || labels->length() > 0);
436 }
437 static int parent_num_ids() { return 0; }
438
439 int base_id() const {
440 DCHECK(!BailoutId(base_id_).IsNone());
441 return base_id_;
442 }
443
444 private:
445 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
446
447 ZoneList<const AstRawString*>* labels_;
448 BreakableType breakable_type_;
449 Label break_target_;
450 int base_id_;
451};
452
453
454class Block final : public BreakableStatement {
455 public:
456 DECLARE_NODE_TYPE(Block)
457
458 ZoneList<Statement*>* statements() { return &statements_; }
459 bool ignore_completion_value() const { return ignore_completion_value_; }
460
461 static int num_ids() { return parent_num_ids() + 1; }
462 BailoutId DeclsId() const { return BailoutId(local_id(0)); }
463
464 bool IsJump() const override {
465 return !statements_.is_empty() && statements_.last()->IsJump()
466 && labels() == NULL; // Good enough as an approximation...
467 }
468
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000469 Scope* scope() const { return scope_; }
470 void set_scope(Scope* scope) { scope_ = scope; }
471
472 protected:
473 Block(Zone* zone, ZoneList<const AstRawString*>* labels, int capacity,
474 bool ignore_completion_value, int pos)
475 : BreakableStatement(zone, labels, TARGET_FOR_NAMED_ONLY, pos),
476 statements_(capacity, zone),
477 ignore_completion_value_(ignore_completion_value),
478 scope_(NULL) {}
479 static int parent_num_ids() { return BreakableStatement::num_ids(); }
480
481 private:
482 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
483
484 ZoneList<Statement*> statements_;
485 bool ignore_completion_value_;
486 Scope* scope_;
487};
488
489
490class DoExpression final : public Expression {
491 public:
492 DECLARE_NODE_TYPE(DoExpression)
493
494 Block* block() { return block_; }
495 void set_block(Block* b) { block_ = b; }
496 VariableProxy* result() { return result_; }
497 void set_result(VariableProxy* v) { result_ = v; }
498
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000499 protected:
500 DoExpression(Zone* zone, Block* block, VariableProxy* result, int pos)
501 : Expression(zone, pos), block_(block), result_(result) {
502 DCHECK_NOT_NULL(block_);
503 DCHECK_NOT_NULL(result_);
504 }
505 static int parent_num_ids() { return Expression::num_ids(); }
506
507 private:
508 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
509
510 Block* block_;
511 VariableProxy* result_;
512};
513
514
515class Declaration : public AstNode {
516 public:
517 VariableProxy* proxy() const { return proxy_; }
518 VariableMode mode() const { return mode_; }
519 Scope* scope() const { return scope_; }
520 virtual InitializationFlag initialization() const = 0;
521 virtual bool IsInlineable() const;
522
523 protected:
524 Declaration(Zone* zone, VariableProxy* proxy, VariableMode mode, Scope* scope,
525 int pos)
526 : AstNode(pos), mode_(mode), proxy_(proxy), scope_(scope) {
527 DCHECK(IsDeclaredVariableMode(mode));
528 }
529
530 private:
531 VariableMode mode_;
532 VariableProxy* proxy_;
533
534 // Nested scope from which the declaration originated.
535 Scope* scope_;
536};
537
538
539class VariableDeclaration final : public Declaration {
540 public:
541 DECLARE_NODE_TYPE(VariableDeclaration)
542
543 InitializationFlag initialization() const override {
544 return mode() == VAR ? kCreatedInitialized : kNeedsInitialization;
545 }
546
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000547 protected:
548 VariableDeclaration(Zone* zone, VariableProxy* proxy, VariableMode mode,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100549 Scope* scope, int pos)
550 : Declaration(zone, proxy, mode, scope, pos) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000551};
552
553
554class FunctionDeclaration final : public Declaration {
555 public:
556 DECLARE_NODE_TYPE(FunctionDeclaration)
557
558 FunctionLiteral* fun() const { return fun_; }
559 void set_fun(FunctionLiteral* f) { fun_ = f; }
560 InitializationFlag initialization() const override {
561 return kCreatedInitialized;
562 }
563 bool IsInlineable() const override;
564
565 protected:
566 FunctionDeclaration(Zone* zone,
567 VariableProxy* proxy,
568 VariableMode mode,
569 FunctionLiteral* fun,
570 Scope* scope,
571 int pos)
572 : Declaration(zone, proxy, mode, scope, pos),
573 fun_(fun) {
574 DCHECK(mode == VAR || mode == LET || mode == CONST);
575 DCHECK(fun != NULL);
576 }
577
578 private:
579 FunctionLiteral* fun_;
580};
581
582
583class ImportDeclaration final : public Declaration {
584 public:
585 DECLARE_NODE_TYPE(ImportDeclaration)
586
587 const AstRawString* import_name() const { return import_name_; }
588 const AstRawString* module_specifier() const { return module_specifier_; }
589 void set_module_specifier(const AstRawString* module_specifier) {
590 DCHECK(module_specifier_ == NULL);
591 module_specifier_ = module_specifier;
592 }
593 InitializationFlag initialization() const override {
594 return kNeedsInitialization;
595 }
596
597 protected:
598 ImportDeclaration(Zone* zone, VariableProxy* proxy,
599 const AstRawString* import_name,
600 const AstRawString* module_specifier, Scope* scope, int pos)
601 : Declaration(zone, proxy, IMPORT, scope, pos),
602 import_name_(import_name),
603 module_specifier_(module_specifier) {}
604
605 private:
606 const AstRawString* import_name_;
607 const AstRawString* module_specifier_;
608};
609
610
611class ExportDeclaration final : public Declaration {
612 public:
613 DECLARE_NODE_TYPE(ExportDeclaration)
614
615 InitializationFlag initialization() const override {
616 return kCreatedInitialized;
617 }
618
619 protected:
620 ExportDeclaration(Zone* zone, VariableProxy* proxy, Scope* scope, int pos)
621 : Declaration(zone, proxy, LET, scope, pos) {}
622};
623
624
625class Module : public AstNode {
626 public:
627 ModuleDescriptor* descriptor() const { return descriptor_; }
628 Block* body() const { return body_; }
629
630 protected:
631 Module(Zone* zone, int pos)
632 : AstNode(pos), descriptor_(ModuleDescriptor::New(zone)), body_(NULL) {}
633 Module(Zone* zone, ModuleDescriptor* descriptor, int pos, Block* body = NULL)
634 : AstNode(pos), descriptor_(descriptor), body_(body) {}
635
636 private:
637 ModuleDescriptor* descriptor_;
638 Block* body_;
639};
640
641
642class IterationStatement : public BreakableStatement {
643 public:
644 // Type testing & conversion.
645 IterationStatement* AsIterationStatement() final { return this; }
646
647 Statement* body() const { return body_; }
648 void set_body(Statement* s) { body_ = s; }
649
650 static int num_ids() { return parent_num_ids() + 1; }
651 BailoutId OsrEntryId() const { return BailoutId(local_id(0)); }
652 virtual BailoutId ContinueId() const = 0;
653 virtual BailoutId StackCheckId() const = 0;
654
655 // Code generation
656 Label* continue_target() { return &continue_target_; }
657
658 protected:
659 IterationStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
660 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
661 body_(NULL) {}
662 static int parent_num_ids() { return BreakableStatement::num_ids(); }
663 void Initialize(Statement* body) { body_ = body; }
664
665 private:
666 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
667
668 Statement* body_;
669 Label continue_target_;
670};
671
672
673class DoWhileStatement final : public IterationStatement {
674 public:
675 DECLARE_NODE_TYPE(DoWhileStatement)
676
677 void Initialize(Expression* cond, Statement* body) {
678 IterationStatement::Initialize(body);
679 cond_ = cond;
680 }
681
682 Expression* cond() const { return cond_; }
683 void set_cond(Expression* e) { cond_ = e; }
684
685 static int num_ids() { return parent_num_ids() + 2; }
686 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
687 BailoutId StackCheckId() const override { return BackEdgeId(); }
688 BailoutId BackEdgeId() const { return BailoutId(local_id(1)); }
689
690 protected:
691 DoWhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
692 : IterationStatement(zone, labels, pos), cond_(NULL) {}
693 static int parent_num_ids() { return IterationStatement::num_ids(); }
694
695 private:
696 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
697
698 Expression* cond_;
699};
700
701
702class WhileStatement final : public IterationStatement {
703 public:
704 DECLARE_NODE_TYPE(WhileStatement)
705
706 void Initialize(Expression* cond, Statement* body) {
707 IterationStatement::Initialize(body);
708 cond_ = cond;
709 }
710
711 Expression* cond() const { return cond_; }
712 void set_cond(Expression* e) { cond_ = e; }
713
714 static int num_ids() { return parent_num_ids() + 1; }
715 BailoutId ContinueId() const override { return EntryId(); }
716 BailoutId StackCheckId() const override { return BodyId(); }
717 BailoutId BodyId() const { return BailoutId(local_id(0)); }
718
719 protected:
720 WhileStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
721 : IterationStatement(zone, labels, pos), cond_(NULL) {}
722 static int parent_num_ids() { return IterationStatement::num_ids(); }
723
724 private:
725 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
726
727 Expression* cond_;
728};
729
730
731class ForStatement final : public IterationStatement {
732 public:
733 DECLARE_NODE_TYPE(ForStatement)
734
735 void Initialize(Statement* init,
736 Expression* cond,
737 Statement* next,
738 Statement* body) {
739 IterationStatement::Initialize(body);
740 init_ = init;
741 cond_ = cond;
742 next_ = next;
743 }
744
745 Statement* init() const { return init_; }
746 Expression* cond() const { return cond_; }
747 Statement* next() const { return next_; }
748
749 void set_init(Statement* s) { init_ = s; }
750 void set_cond(Expression* e) { cond_ = e; }
751 void set_next(Statement* s) { next_ = s; }
752
753 static int num_ids() { return parent_num_ids() + 2; }
754 BailoutId ContinueId() const override { return BailoutId(local_id(0)); }
755 BailoutId StackCheckId() const override { return BodyId(); }
756 BailoutId BodyId() const { return BailoutId(local_id(1)); }
757
758 protected:
759 ForStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
760 : IterationStatement(zone, labels, pos),
761 init_(NULL),
762 cond_(NULL),
763 next_(NULL) {}
764 static int parent_num_ids() { return IterationStatement::num_ids(); }
765
766 private:
767 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
768
769 Statement* init_;
770 Expression* cond_;
771 Statement* next_;
772};
773
774
775class ForEachStatement : public IterationStatement {
776 public:
777 enum VisitMode {
778 ENUMERATE, // for (each in subject) body;
779 ITERATE // for (each of subject) body;
780 };
781
782 void Initialize(Expression* each, Expression* subject, Statement* body) {
783 IterationStatement::Initialize(body);
784 each_ = each;
785 subject_ = subject;
786 }
787
788 Expression* each() const { return each_; }
789 Expression* subject() const { return subject_; }
790
791 void set_each(Expression* e) { each_ = e; }
792 void set_subject(Expression* e) { subject_ = e; }
793
Ben Murdoch097c5b22016-05-18 11:27:45 +0100794 static const char* VisitModeString(VisitMode mode) {
795 return mode == ITERATE ? "for-of" : "for-in";
796 }
797
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000798 protected:
799 ForEachStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
800 : IterationStatement(zone, labels, pos), each_(NULL), subject_(NULL) {}
801
802 private:
803 Expression* each_;
804 Expression* subject_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000805};
806
807
808class ForInStatement final : public ForEachStatement {
809 public:
810 DECLARE_NODE_TYPE(ForInStatement)
811
812 Expression* enumerable() const {
813 return subject();
814 }
815
816 // Type feedback information.
817 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
Ben Murdochda12d292016-06-02 14:46:10 +0100818 FeedbackVectorSlotCache* cache) override;
819 FeedbackVectorSlot EachFeedbackSlot() const { return each_slot_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000820 FeedbackVectorSlot ForInFeedbackSlot() {
821 DCHECK(!for_in_feedback_slot_.IsInvalid());
822 return for_in_feedback_slot_;
823 }
824
825 enum ForInType { FAST_FOR_IN, SLOW_FOR_IN };
826 ForInType for_in_type() const { return for_in_type_; }
827 void set_for_in_type(ForInType type) { for_in_type_ = type; }
828
829 static int num_ids() { return parent_num_ids() + 6; }
830 BailoutId BodyId() const { return BailoutId(local_id(0)); }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100831 BailoutId EnumId() const { return BailoutId(local_id(1)); }
832 BailoutId ToObjectId() const { return BailoutId(local_id(2)); }
833 BailoutId PrepareId() const { return BailoutId(local_id(3)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000834 BailoutId FilterId() const { return BailoutId(local_id(4)); }
835 BailoutId AssignmentId() const { return BailoutId(local_id(5)); }
836 BailoutId ContinueId() const override { return EntryId(); }
837 BailoutId StackCheckId() const override { return BodyId(); }
838
839 protected:
840 ForInStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
841 : ForEachStatement(zone, labels, pos), for_in_type_(SLOW_FOR_IN) {}
842 static int parent_num_ids() { return ForEachStatement::num_ids(); }
843
844 private:
845 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
846
847 ForInType for_in_type_;
Ben Murdochda12d292016-06-02 14:46:10 +0100848 FeedbackVectorSlot each_slot_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000849 FeedbackVectorSlot for_in_feedback_slot_;
850};
851
852
853class ForOfStatement final : public ForEachStatement {
854 public:
855 DECLARE_NODE_TYPE(ForOfStatement)
856
857 void Initialize(Expression* each,
858 Expression* subject,
859 Statement* body,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100860 Variable* iterator,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000861 Expression* assign_iterator,
862 Expression* next_result,
863 Expression* result_done,
864 Expression* assign_each) {
865 ForEachStatement::Initialize(each, subject, body);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100866 iterator_ = iterator;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000867 assign_iterator_ = assign_iterator;
868 next_result_ = next_result;
869 result_done_ = result_done;
870 assign_each_ = assign_each;
871 }
872
873 Expression* iterable() const {
874 return subject();
875 }
876
Ben Murdoch097c5b22016-05-18 11:27:45 +0100877 Variable* iterator() const {
878 return iterator_;
879 }
880
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000881 // iterator = subject[Symbol.iterator]()
882 Expression* assign_iterator() const {
883 return assign_iterator_;
884 }
885
886 // result = iterator.next() // with type check
887 Expression* next_result() const {
888 return next_result_;
889 }
890
891 // result.done
892 Expression* result_done() const {
893 return result_done_;
894 }
895
896 // each = result.value
897 Expression* assign_each() const {
898 return assign_each_;
899 }
900
901 void set_assign_iterator(Expression* e) { assign_iterator_ = e; }
902 void set_next_result(Expression* e) { next_result_ = e; }
903 void set_result_done(Expression* e) { result_done_ = e; }
904 void set_assign_each(Expression* e) { assign_each_ = e; }
905
906 BailoutId ContinueId() const override { return EntryId(); }
907 BailoutId StackCheckId() const override { return BackEdgeId(); }
908
909 static int num_ids() { return parent_num_ids() + 1; }
910 BailoutId BackEdgeId() const { return BailoutId(local_id(0)); }
911
912 protected:
913 ForOfStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
914 : ForEachStatement(zone, labels, pos),
Ben Murdoch097c5b22016-05-18 11:27:45 +0100915 iterator_(NULL),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000916 assign_iterator_(NULL),
917 next_result_(NULL),
918 result_done_(NULL),
919 assign_each_(NULL) {}
920 static int parent_num_ids() { return ForEachStatement::num_ids(); }
921
922 private:
923 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
924
Ben Murdoch097c5b22016-05-18 11:27:45 +0100925 Variable* iterator_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000926 Expression* assign_iterator_;
927 Expression* next_result_;
928 Expression* result_done_;
929 Expression* assign_each_;
930};
931
932
933class ExpressionStatement final : public Statement {
934 public:
935 DECLARE_NODE_TYPE(ExpressionStatement)
936
937 void set_expression(Expression* e) { expression_ = e; }
938 Expression* expression() const { return expression_; }
939 bool IsJump() const override { return expression_->IsThrow(); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000940
941 protected:
942 ExpressionStatement(Zone* zone, Expression* expression, int pos)
943 : Statement(zone, pos), expression_(expression) { }
944
945 private:
946 Expression* expression_;
947};
948
949
950class JumpStatement : public Statement {
951 public:
952 bool IsJump() const final { return true; }
953
954 protected:
955 explicit JumpStatement(Zone* zone, int pos) : Statement(zone, pos) {}
956};
957
958
959class ContinueStatement final : public JumpStatement {
960 public:
961 DECLARE_NODE_TYPE(ContinueStatement)
962
963 IterationStatement* target() const { return target_; }
964
965 protected:
966 explicit ContinueStatement(Zone* zone, IterationStatement* target, int pos)
967 : JumpStatement(zone, pos), target_(target) { }
968
969 private:
970 IterationStatement* target_;
971};
972
973
974class BreakStatement final : public JumpStatement {
975 public:
976 DECLARE_NODE_TYPE(BreakStatement)
977
978 BreakableStatement* target() const { return target_; }
979
980 protected:
981 explicit BreakStatement(Zone* zone, BreakableStatement* target, int pos)
982 : JumpStatement(zone, pos), target_(target) { }
983
984 private:
985 BreakableStatement* target_;
986};
987
988
989class ReturnStatement final : public JumpStatement {
990 public:
991 DECLARE_NODE_TYPE(ReturnStatement)
992
993 Expression* expression() const { return expression_; }
994
995 void set_expression(Expression* e) { expression_ = e; }
996
997 protected:
998 explicit ReturnStatement(Zone* zone, Expression* expression, int pos)
999 : JumpStatement(zone, pos), expression_(expression) { }
1000
1001 private:
1002 Expression* expression_;
1003};
1004
1005
1006class WithStatement final : public Statement {
1007 public:
1008 DECLARE_NODE_TYPE(WithStatement)
1009
1010 Scope* scope() { return scope_; }
1011 Expression* expression() const { return expression_; }
1012 void set_expression(Expression* e) { expression_ = e; }
1013 Statement* statement() const { return statement_; }
1014 void set_statement(Statement* s) { statement_ = s; }
1015
1016 void set_base_id(int id) { base_id_ = id; }
1017 static int num_ids() { return parent_num_ids() + 2; }
1018 BailoutId ToObjectId() const { return BailoutId(local_id(0)); }
1019 BailoutId EntryId() const { return BailoutId(local_id(1)); }
1020
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001021 protected:
1022 WithStatement(Zone* zone, Scope* scope, Expression* expression,
1023 Statement* statement, int pos)
1024 : Statement(zone, pos),
1025 scope_(scope),
1026 expression_(expression),
1027 statement_(statement),
1028 base_id_(BailoutId::None().ToInt()) {}
1029 static int parent_num_ids() { return 0; }
1030
1031 int base_id() const {
1032 DCHECK(!BailoutId(base_id_).IsNone());
1033 return base_id_;
1034 }
1035
1036 private:
1037 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1038
1039 Scope* scope_;
1040 Expression* expression_;
1041 Statement* statement_;
1042 int base_id_;
1043};
1044
1045
1046class CaseClause final : public Expression {
1047 public:
1048 DECLARE_NODE_TYPE(CaseClause)
1049
1050 bool is_default() const { return label_ == NULL; }
1051 Expression* label() const {
1052 CHECK(!is_default());
1053 return label_;
1054 }
1055 void set_label(Expression* e) { label_ = e; }
1056 Label* body_target() { return &body_target_; }
1057 ZoneList<Statement*>* statements() const { return statements_; }
1058
1059 static int num_ids() { return parent_num_ids() + 2; }
1060 BailoutId EntryId() const { return BailoutId(local_id(0)); }
1061 TypeFeedbackId CompareId() { return TypeFeedbackId(local_id(1)); }
1062
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001063 Type* compare_type() { return compare_type_; }
1064 void set_compare_type(Type* type) { compare_type_ = type; }
1065
1066 protected:
1067 static int parent_num_ids() { return Expression::num_ids(); }
1068
1069 private:
1070 CaseClause(Zone* zone, Expression* label, ZoneList<Statement*>* statements,
1071 int pos);
1072 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1073
1074 Expression* label_;
1075 Label body_target_;
1076 ZoneList<Statement*>* statements_;
1077 Type* compare_type_;
1078};
1079
1080
1081class SwitchStatement final : public BreakableStatement {
1082 public:
1083 DECLARE_NODE_TYPE(SwitchStatement)
1084
1085 void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) {
1086 tag_ = tag;
1087 cases_ = cases;
1088 }
1089
1090 Expression* tag() const { return tag_; }
1091 ZoneList<CaseClause*>* cases() const { return cases_; }
1092
1093 void set_tag(Expression* t) { tag_ = t; }
1094
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001095 protected:
1096 SwitchStatement(Zone* zone, ZoneList<const AstRawString*>* labels, int pos)
1097 : BreakableStatement(zone, labels, TARGET_FOR_ANONYMOUS, pos),
1098 tag_(NULL),
1099 cases_(NULL) {}
1100
1101 private:
1102 Expression* tag_;
1103 ZoneList<CaseClause*>* cases_;
1104};
1105
1106
1107// If-statements always have non-null references to their then- and
1108// else-parts. When parsing if-statements with no explicit else-part,
1109// the parser implicitly creates an empty statement. Use the
1110// HasThenStatement() and HasElseStatement() functions to check if a
1111// given if-statement has a then- or an else-part containing code.
1112class IfStatement final : public Statement {
1113 public:
1114 DECLARE_NODE_TYPE(IfStatement)
1115
1116 bool HasThenStatement() const { return !then_statement()->IsEmpty(); }
1117 bool HasElseStatement() const { return !else_statement()->IsEmpty(); }
1118
1119 Expression* condition() const { return condition_; }
1120 Statement* then_statement() const { return then_statement_; }
1121 Statement* else_statement() const { return else_statement_; }
1122
1123 void set_condition(Expression* e) { condition_ = e; }
1124 void set_then_statement(Statement* s) { then_statement_ = s; }
1125 void set_else_statement(Statement* s) { else_statement_ = s; }
1126
1127 bool IsJump() const override {
1128 return HasThenStatement() && then_statement()->IsJump()
1129 && HasElseStatement() && else_statement()->IsJump();
1130 }
1131
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001132 void set_base_id(int id) { base_id_ = id; }
1133 static int num_ids() { return parent_num_ids() + 3; }
1134 BailoutId IfId() const { return BailoutId(local_id(0)); }
1135 BailoutId ThenId() const { return BailoutId(local_id(1)); }
1136 BailoutId ElseId() const { return BailoutId(local_id(2)); }
1137
1138 protected:
1139 IfStatement(Zone* zone, Expression* condition, Statement* then_statement,
1140 Statement* else_statement, int pos)
1141 : Statement(zone, pos),
1142 condition_(condition),
1143 then_statement_(then_statement),
1144 else_statement_(else_statement),
1145 base_id_(BailoutId::None().ToInt()) {}
1146 static int parent_num_ids() { return 0; }
1147
1148 int base_id() const {
1149 DCHECK(!BailoutId(base_id_).IsNone());
1150 return base_id_;
1151 }
1152
1153 private:
1154 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1155
1156 Expression* condition_;
1157 Statement* then_statement_;
1158 Statement* else_statement_;
1159 int base_id_;
1160};
1161
1162
1163class TryStatement : public Statement {
1164 public:
1165 Block* try_block() const { return try_block_; }
1166 void set_try_block(Block* b) { try_block_ = b; }
1167
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001168 protected:
1169 TryStatement(Zone* zone, Block* try_block, int pos)
Ben Murdoch097c5b22016-05-18 11:27:45 +01001170 : Statement(zone, pos), try_block_(try_block) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001171
1172 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001173 Block* try_block_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001174};
1175
1176
1177class TryCatchStatement final : public TryStatement {
1178 public:
1179 DECLARE_NODE_TYPE(TryCatchStatement)
1180
1181 Scope* scope() { return scope_; }
1182 Variable* variable() { return variable_; }
1183 Block* catch_block() const { return catch_block_; }
1184 void set_catch_block(Block* b) { catch_block_ = b; }
1185
Ben Murdochda12d292016-06-02 14:46:10 +01001186 // The clear_pending_message flag indicates whether or not to clear the
1187 // isolate's pending exception message before executing the catch_block. In
1188 // the normal use case, this flag is always on because the message object
1189 // is not needed anymore when entering the catch block and should not be kept
1190 // alive.
1191 // The use case where the flag is off is when the catch block is guaranteed to
1192 // rethrow the caught exception (using %ReThrow), which reuses the pending
1193 // message instead of generating a new one.
1194 // (When the catch block doesn't rethrow but is guaranteed to perform an
1195 // ordinary throw, not clearing the old message is safe but not very useful.)
1196 bool clear_pending_message() { return clear_pending_message_; }
1197
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001198 protected:
1199 TryCatchStatement(Zone* zone, Block* try_block, Scope* scope,
Ben Murdochda12d292016-06-02 14:46:10 +01001200 Variable* variable, Block* catch_block,
1201 bool clear_pending_message, int pos)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001202 : TryStatement(zone, try_block, pos),
1203 scope_(scope),
1204 variable_(variable),
Ben Murdochda12d292016-06-02 14:46:10 +01001205 catch_block_(catch_block),
1206 clear_pending_message_(clear_pending_message) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001207
1208 private:
1209 Scope* scope_;
1210 Variable* variable_;
1211 Block* catch_block_;
Ben Murdochda12d292016-06-02 14:46:10 +01001212 bool clear_pending_message_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001213};
1214
1215
1216class TryFinallyStatement final : public TryStatement {
1217 public:
1218 DECLARE_NODE_TYPE(TryFinallyStatement)
1219
1220 Block* finally_block() const { return finally_block_; }
1221 void set_finally_block(Block* b) { finally_block_ = b; }
1222
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001223 protected:
1224 TryFinallyStatement(Zone* zone, Block* try_block, Block* finally_block,
1225 int pos)
1226 : TryStatement(zone, try_block, pos), finally_block_(finally_block) {}
1227
1228 private:
1229 Block* finally_block_;
1230};
1231
1232
1233class DebuggerStatement final : public Statement {
1234 public:
1235 DECLARE_NODE_TYPE(DebuggerStatement)
1236
1237 void set_base_id(int id) { base_id_ = id; }
1238 static int num_ids() { return parent_num_ids() + 1; }
1239 BailoutId DebugBreakId() const { return BailoutId(local_id(0)); }
1240
1241 protected:
1242 explicit DebuggerStatement(Zone* zone, int pos)
1243 : Statement(zone, pos), base_id_(BailoutId::None().ToInt()) {}
1244 static int parent_num_ids() { return 0; }
1245
1246 int base_id() const {
1247 DCHECK(!BailoutId(base_id_).IsNone());
1248 return base_id_;
1249 }
1250
1251 private:
1252 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1253
1254 int base_id_;
1255};
1256
1257
1258class EmptyStatement final : public Statement {
1259 public:
1260 DECLARE_NODE_TYPE(EmptyStatement)
1261
1262 protected:
1263 explicit EmptyStatement(Zone* zone, int pos): Statement(zone, pos) {}
1264};
1265
1266
1267// Delegates to another statement, which may be overwritten.
1268// This was introduced to implement ES2015 Annex B3.3 for conditionally making
1269// sloppy-mode block-scoped functions have a var binding, which is changed
1270// from one statement to another during parsing.
1271class SloppyBlockFunctionStatement final : public Statement {
1272 public:
1273 DECLARE_NODE_TYPE(SloppyBlockFunctionStatement)
1274
1275 Statement* statement() const { return statement_; }
1276 void set_statement(Statement* statement) { statement_ = statement; }
1277 Scope* scope() const { return scope_; }
1278
1279 private:
1280 SloppyBlockFunctionStatement(Zone* zone, Statement* statement, Scope* scope)
1281 : Statement(zone, RelocInfo::kNoPosition),
1282 statement_(statement),
1283 scope_(scope) {}
1284
1285 Statement* statement_;
1286 Scope* const scope_;
1287};
1288
1289
1290class Literal final : public Expression {
1291 public:
1292 DECLARE_NODE_TYPE(Literal)
1293
1294 bool IsPropertyName() const override { return value_->IsPropertyName(); }
1295
1296 Handle<String> AsPropertyName() {
1297 DCHECK(IsPropertyName());
1298 return Handle<String>::cast(value());
1299 }
1300
1301 const AstRawString* AsRawPropertyName() {
1302 DCHECK(IsPropertyName());
1303 return value_->AsString();
1304 }
1305
1306 bool ToBooleanIsTrue() const override { return value()->BooleanValue(); }
1307 bool ToBooleanIsFalse() const override { return !value()->BooleanValue(); }
1308
1309 Handle<Object> value() const { return value_->value(); }
1310 const AstValue* raw_value() const { return value_; }
1311
1312 // Support for using Literal as a HashMap key. NOTE: Currently, this works
1313 // only for string and number literals!
1314 uint32_t Hash();
1315 static bool Match(void* literal1, void* literal2);
1316
1317 static int num_ids() { return parent_num_ids() + 1; }
1318 TypeFeedbackId LiteralFeedbackId() const {
1319 return TypeFeedbackId(local_id(0));
1320 }
1321
1322 protected:
1323 Literal(Zone* zone, const AstValue* value, int position)
1324 : Expression(zone, position), value_(value) {}
1325 static int parent_num_ids() { return Expression::num_ids(); }
1326
1327 private:
1328 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1329
1330 const AstValue* value_;
1331};
1332
1333
1334class AstLiteralReindexer;
1335
1336// Base class for literals that needs space in the corresponding JSFunction.
1337class MaterializedLiteral : public Expression {
1338 public:
1339 MaterializedLiteral* AsMaterializedLiteral() final { return this; }
1340
1341 int literal_index() { return literal_index_; }
1342
1343 int depth() const {
1344 // only callable after initialization.
1345 DCHECK(depth_ >= 1);
1346 return depth_;
1347 }
1348
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001349 protected:
Ben Murdochda12d292016-06-02 14:46:10 +01001350 MaterializedLiteral(Zone* zone, int literal_index, int pos)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001351 : Expression(zone, pos),
1352 literal_index_(literal_index),
1353 is_simple_(false),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001354 depth_(0) {}
1355
1356 // A materialized literal is simple if the values consist of only
1357 // constants and simple object and array literals.
1358 bool is_simple() const { return is_simple_; }
1359 void set_is_simple(bool is_simple) { is_simple_ = is_simple; }
1360 friend class CompileTimeValue;
1361
1362 void set_depth(int depth) {
1363 DCHECK(depth >= 1);
1364 depth_ = depth;
1365 }
1366
1367 // Populate the constant properties/elements fixed array.
1368 void BuildConstants(Isolate* isolate);
1369 friend class ArrayLiteral;
1370 friend class ObjectLiteral;
1371
1372 // If the expression is a literal, return the literal value;
1373 // if the expression is a materialized literal and is simple return a
1374 // compile time value as encoded by CompileTimeValue::GetValue().
1375 // Otherwise, return undefined literal as the placeholder
1376 // in the object literal boilerplate.
1377 Handle<Object> GetBoilerplateValue(Expression* expression, Isolate* isolate);
1378
1379 private:
1380 int literal_index_;
1381 bool is_simple_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001382 int depth_;
1383
1384 friend class AstLiteralReindexer;
1385};
1386
1387
1388// Property is used for passing information
1389// about an object literal's properties from the parser
1390// to the code generator.
1391class ObjectLiteralProperty final : public ZoneObject {
1392 public:
1393 enum Kind {
1394 CONSTANT, // Property with constant value (compile time).
1395 COMPUTED, // Property with computed value (execution time).
1396 MATERIALIZED_LITERAL, // Property value is a materialized literal.
1397 GETTER, SETTER, // Property is an accessor function.
1398 PROTOTYPE // Property is __proto__.
1399 };
1400
1401 Expression* key() { return key_; }
1402 Expression* value() { return value_; }
1403 Kind kind() { return kind_; }
1404
1405 void set_key(Expression* e) { key_ = e; }
1406 void set_value(Expression* e) { value_ = e; }
1407
1408 // Type feedback information.
1409 bool IsMonomorphic() { return !receiver_type_.is_null(); }
1410 Handle<Map> GetReceiverType() { return receiver_type_; }
1411
1412 bool IsCompileTimeValue();
1413
1414 void set_emit_store(bool emit_store);
1415 bool emit_store();
1416
1417 bool is_static() const { return is_static_; }
1418 bool is_computed_name() const { return is_computed_name_; }
1419
1420 FeedbackVectorSlot GetSlot(int offset = 0) const {
1421 DCHECK_LT(offset, static_cast<int>(arraysize(slots_)));
1422 return slots_[offset];
1423 }
1424 void SetSlot(FeedbackVectorSlot slot, int offset = 0) {
1425 DCHECK_LT(offset, static_cast<int>(arraysize(slots_)));
1426 slots_[offset] = slot;
1427 }
1428
1429 void set_receiver_type(Handle<Map> map) { receiver_type_ = map; }
1430
Ben Murdoch097c5b22016-05-18 11:27:45 +01001431 bool NeedsSetFunctionName() const;
1432
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001433 protected:
1434 friend class AstNodeFactory;
1435
1436 ObjectLiteralProperty(Expression* key, Expression* value, Kind kind,
1437 bool is_static, bool is_computed_name);
1438 ObjectLiteralProperty(AstValueFactory* ast_value_factory, Expression* key,
1439 Expression* value, bool is_static,
1440 bool is_computed_name);
1441
1442 private:
1443 Expression* key_;
1444 Expression* value_;
1445 FeedbackVectorSlot slots_[2];
1446 Kind kind_;
1447 bool emit_store_;
1448 bool is_static_;
1449 bool is_computed_name_;
1450 Handle<Map> receiver_type_;
1451};
1452
1453
1454// An object literal has a boilerplate object that is used
1455// for minimizing the work when constructing it at runtime.
1456class ObjectLiteral final : public MaterializedLiteral {
1457 public:
1458 typedef ObjectLiteralProperty Property;
1459
1460 DECLARE_NODE_TYPE(ObjectLiteral)
1461
1462 Handle<FixedArray> constant_properties() const {
1463 return constant_properties_;
1464 }
1465 int properties_count() const { return constant_properties_->length() / 2; }
1466 ZoneList<Property*>* properties() const { return properties_; }
1467 bool fast_elements() const { return fast_elements_; }
1468 bool may_store_doubles() const { return may_store_doubles_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001469 bool has_elements() const { return has_elements_; }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001470 bool has_shallow_properties() const {
1471 return depth() == 1 && !has_elements() && !may_store_doubles();
1472 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001473
1474 // Decide if a property should be in the object boilerplate.
1475 static bool IsBoilerplateProperty(Property* property);
1476
1477 // Populate the constant properties fixed array.
1478 void BuildConstantProperties(Isolate* isolate);
1479
1480 // Mark all computed expressions that are bound to a key that
1481 // is shadowed by a later occurrence of the same key. For the
1482 // marked expressions, no store code is emitted.
1483 void CalculateEmitStore(Zone* zone);
1484
1485 // Assemble bitfield of flags for the CreateObjectLiteral helper.
1486 int ComputeFlags(bool disable_mementos = false) const {
1487 int flags = fast_elements() ? kFastElements : kNoFlags;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001488 if (has_shallow_properties()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001489 flags |= kShallowProperties;
1490 }
1491 if (disable_mementos) {
1492 flags |= kDisableMementos;
1493 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001494 return flags;
1495 }
1496
1497 enum Flags {
1498 kNoFlags = 0,
1499 kFastElements = 1,
Ben Murdochda12d292016-06-02 14:46:10 +01001500 kShallowProperties = 1 << 1,
1501 kDisableMementos = 1 << 2
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001502 };
1503
1504 struct Accessors: public ZoneObject {
1505 Accessors() : getter(NULL), setter(NULL) {}
1506 ObjectLiteralProperty* getter;
1507 ObjectLiteralProperty* setter;
1508 };
1509
1510 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1511
1512 // Return an AST id for a property that is used in simulate instructions.
1513 BailoutId GetIdForPropertyName(int i) {
1514 return BailoutId(local_id(2 * i + 1));
1515 }
1516 BailoutId GetIdForPropertySet(int i) {
1517 return BailoutId(local_id(2 * i + 2));
1518 }
1519
1520 // Unlike other AST nodes, this number of bailout IDs allocated for an
1521 // ObjectLiteral can vary, so num_ids() is not a static method.
1522 int num_ids() const {
1523 return parent_num_ids() + 1 + 2 * properties()->length();
1524 }
1525
1526 // Object literals need one feedback slot for each non-trivial value, as well
1527 // as some slots for home objects.
1528 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1529 FeedbackVectorSlotCache* cache) override;
1530
1531 protected:
1532 ObjectLiteral(Zone* zone, ZoneList<Property*>* properties, int literal_index,
Ben Murdochda12d292016-06-02 14:46:10 +01001533 int boilerplate_properties, int pos)
1534 : MaterializedLiteral(zone, literal_index, pos),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001535 properties_(properties),
1536 boilerplate_properties_(boilerplate_properties),
1537 fast_elements_(false),
1538 has_elements_(false),
Ben Murdochda12d292016-06-02 14:46:10 +01001539 may_store_doubles_(false) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001540 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1541
1542 private:
1543 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1544 Handle<FixedArray> constant_properties_;
1545 ZoneList<Property*>* properties_;
1546 int boilerplate_properties_;
1547 bool fast_elements_;
1548 bool has_elements_;
1549 bool may_store_doubles_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001550 FeedbackVectorSlot slot_;
1551};
1552
1553
1554// A map from property names to getter/setter pairs allocated in the zone.
1555class AccessorTable : public TemplateHashMap<Literal, ObjectLiteral::Accessors,
1556 ZoneAllocationPolicy> {
1557 public:
1558 explicit AccessorTable(Zone* zone)
1559 : TemplateHashMap<Literal, ObjectLiteral::Accessors,
1560 ZoneAllocationPolicy>(Literal::Match,
1561 ZoneAllocationPolicy(zone)),
1562 zone_(zone) {}
1563
1564 Iterator lookup(Literal* literal) {
1565 Iterator it = find(literal, true, ZoneAllocationPolicy(zone_));
1566 if (it->second == NULL) it->second = new (zone_) ObjectLiteral::Accessors();
1567 return it;
1568 }
1569
1570 private:
1571 Zone* zone_;
1572};
1573
1574
1575// Node for capturing a regexp literal.
1576class RegExpLiteral final : public MaterializedLiteral {
1577 public:
1578 DECLARE_NODE_TYPE(RegExpLiteral)
1579
1580 Handle<String> pattern() const { return pattern_->string(); }
1581 int flags() const { return flags_; }
1582
1583 protected:
1584 RegExpLiteral(Zone* zone, const AstRawString* pattern, int flags,
Ben Murdochda12d292016-06-02 14:46:10 +01001585 int literal_index, int pos)
1586 : MaterializedLiteral(zone, literal_index, pos),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001587 pattern_(pattern),
1588 flags_(flags) {
1589 set_depth(1);
1590 }
1591
1592 private:
1593 const AstRawString* const pattern_;
1594 int const flags_;
1595};
1596
1597
1598// An array literal has a literals object that is used
1599// for minimizing the work when constructing it at runtime.
1600class ArrayLiteral final : public MaterializedLiteral {
1601 public:
1602 DECLARE_NODE_TYPE(ArrayLiteral)
1603
1604 Handle<FixedArray> constant_elements() const { return constant_elements_; }
1605 ElementsKind constant_elements_kind() const {
1606 DCHECK_EQ(2, constant_elements_->length());
1607 return static_cast<ElementsKind>(
1608 Smi::cast(constant_elements_->get(0))->value());
1609 }
1610
1611 ZoneList<Expression*>* values() const { return values_; }
1612
1613 BailoutId CreateLiteralId() const { return BailoutId(local_id(0)); }
1614
1615 // Return an AST id for an element that is used in simulate instructions.
1616 BailoutId GetIdForElement(int i) { return BailoutId(local_id(i + 1)); }
1617
1618 // Unlike other AST nodes, this number of bailout IDs allocated for an
1619 // ArrayLiteral can vary, so num_ids() is not a static method.
1620 int num_ids() const { return parent_num_ids() + 1 + values()->length(); }
1621
1622 // Populate the constant elements fixed array.
1623 void BuildConstantElements(Isolate* isolate);
1624
1625 // Assemble bitfield of flags for the CreateArrayLiteral helper.
1626 int ComputeFlags(bool disable_mementos = false) const {
1627 int flags = depth() == 1 ? kShallowElements : kNoFlags;
1628 if (disable_mementos) {
1629 flags |= kDisableMementos;
1630 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001631 return flags;
1632 }
1633
Ben Murdoch097c5b22016-05-18 11:27:45 +01001634 // Provide a mechanism for iterating through values to rewrite spreads.
1635 ZoneList<Expression*>::iterator FirstSpread() const {
1636 return (first_spread_index_ >= 0) ? values_->begin() + first_spread_index_
1637 : values_->end();
1638 }
1639 ZoneList<Expression*>::iterator EndValue() const { return values_->end(); }
1640
1641 // Rewind an array literal omitting everything from the first spread on.
1642 void RewindSpreads() {
1643 values_->Rewind(first_spread_index_);
1644 first_spread_index_ = -1;
1645 }
1646
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001647 enum Flags {
1648 kNoFlags = 0,
1649 kShallowElements = 1,
Ben Murdochda12d292016-06-02 14:46:10 +01001650 kDisableMementos = 1 << 1
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001651 };
1652
1653 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1654 FeedbackVectorSlotCache* cache) override;
1655 FeedbackVectorSlot LiteralFeedbackSlot() const { return literal_slot_; }
1656
1657 protected:
1658 ArrayLiteral(Zone* zone, ZoneList<Expression*>* values,
Ben Murdochda12d292016-06-02 14:46:10 +01001659 int first_spread_index, int literal_index, int pos)
1660 : MaterializedLiteral(zone, literal_index, pos),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001661 values_(values),
1662 first_spread_index_(first_spread_index) {}
1663 static int parent_num_ids() { return MaterializedLiteral::num_ids(); }
1664
1665 private:
1666 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1667
1668 Handle<FixedArray> constant_elements_;
1669 ZoneList<Expression*>* values_;
1670 int first_spread_index_;
1671 FeedbackVectorSlot literal_slot_;
1672};
1673
1674
1675class VariableProxy final : public Expression {
1676 public:
1677 DECLARE_NODE_TYPE(VariableProxy)
1678
1679 bool IsValidReferenceExpression() const override {
1680 return !is_this() && !is_new_target();
1681 }
1682
1683 bool IsArguments() const { return is_resolved() && var()->is_arguments(); }
1684
1685 Handle<String> name() const { return raw_name()->string(); }
1686 const AstRawString* raw_name() const {
1687 return is_resolved() ? var_->raw_name() : raw_name_;
1688 }
1689
1690 Variable* var() const {
1691 DCHECK(is_resolved());
1692 return var_;
1693 }
1694 void set_var(Variable* v) {
1695 DCHECK(!is_resolved());
1696 DCHECK_NOT_NULL(v);
1697 var_ = v;
1698 }
1699
1700 bool is_this() const { return IsThisField::decode(bit_field_); }
1701
1702 bool is_assigned() const { return IsAssignedField::decode(bit_field_); }
1703 void set_is_assigned() {
1704 bit_field_ = IsAssignedField::update(bit_field_, true);
1705 }
1706
1707 bool is_resolved() const { return IsResolvedField::decode(bit_field_); }
1708 void set_is_resolved() {
1709 bit_field_ = IsResolvedField::update(bit_field_, true);
1710 }
1711
1712 bool is_new_target() const { return IsNewTargetField::decode(bit_field_); }
1713 void set_is_new_target() {
1714 bit_field_ = IsNewTargetField::update(bit_field_, true);
1715 }
1716
1717 int end_position() const { return end_position_; }
1718
1719 // Bind this proxy to the variable var.
1720 void BindTo(Variable* var);
1721
1722 bool UsesVariableFeedbackSlot() const {
1723 return var()->IsUnallocated() || var()->IsLookupSlot();
1724 }
1725
1726 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1727 FeedbackVectorSlotCache* cache) override;
1728
1729 FeedbackVectorSlot VariableFeedbackSlot() { return variable_feedback_slot_; }
1730
1731 static int num_ids() { return parent_num_ids() + 1; }
1732 BailoutId BeforeId() const { return BailoutId(local_id(0)); }
1733
1734 protected:
1735 VariableProxy(Zone* zone, Variable* var, int start_position,
1736 int end_position);
1737
1738 VariableProxy(Zone* zone, const AstRawString* name,
1739 Variable::Kind variable_kind, int start_position,
1740 int end_position);
1741 static int parent_num_ids() { return Expression::num_ids(); }
1742 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1743
1744 class IsThisField : public BitField8<bool, 0, 1> {};
1745 class IsAssignedField : public BitField8<bool, 1, 1> {};
1746 class IsResolvedField : public BitField8<bool, 2, 1> {};
1747 class IsNewTargetField : public BitField8<bool, 3, 1> {};
1748
1749 // Start with 16-bit (or smaller) field, which should get packed together
1750 // with Expression's trailing 16-bit field.
1751 uint8_t bit_field_;
1752 FeedbackVectorSlot variable_feedback_slot_;
1753 union {
1754 const AstRawString* raw_name_; // if !is_resolved_
1755 Variable* var_; // if is_resolved_
1756 };
1757 // Position is stored in the AstNode superclass, but VariableProxy needs to
1758 // know its end position too (for error messages). It cannot be inferred from
1759 // the variable name length because it can contain escapes.
1760 int end_position_;
1761};
1762
1763
1764// Left-hand side can only be a property, a global or a (parameter or local)
1765// slot.
1766enum LhsKind {
1767 VARIABLE,
1768 NAMED_PROPERTY,
1769 KEYED_PROPERTY,
1770 NAMED_SUPER_PROPERTY,
1771 KEYED_SUPER_PROPERTY
1772};
1773
1774
1775class Property final : public Expression {
1776 public:
1777 DECLARE_NODE_TYPE(Property)
1778
1779 bool IsValidReferenceExpression() const override { return true; }
1780
1781 Expression* obj() const { return obj_; }
1782 Expression* key() const { return key_; }
1783
1784 void set_obj(Expression* e) { obj_ = e; }
1785 void set_key(Expression* e) { key_ = e; }
1786
1787 static int num_ids() { return parent_num_ids() + 1; }
1788 BailoutId LoadId() const { return BailoutId(local_id(0)); }
1789
1790 bool IsStringAccess() const {
1791 return IsStringAccessField::decode(bit_field_);
1792 }
1793
1794 // Type feedback information.
1795 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
1796 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
1797 KeyedAccessStoreMode GetStoreMode() const override { return STANDARD_STORE; }
1798 IcCheckType GetKeyType() const override {
1799 return KeyTypeField::decode(bit_field_);
1800 }
1801 bool IsUninitialized() const {
1802 return !is_for_call() && HasNoTypeInformation();
1803 }
1804 bool HasNoTypeInformation() const {
1805 return GetInlineCacheState() == UNINITIALIZED;
1806 }
1807 InlineCacheState GetInlineCacheState() const {
1808 return InlineCacheStateField::decode(bit_field_);
1809 }
1810 void set_is_string_access(bool b) {
1811 bit_field_ = IsStringAccessField::update(bit_field_, b);
1812 }
1813 void set_key_type(IcCheckType key_type) {
1814 bit_field_ = KeyTypeField::update(bit_field_, key_type);
1815 }
1816 void set_inline_cache_state(InlineCacheState state) {
1817 bit_field_ = InlineCacheStateField::update(bit_field_, state);
1818 }
1819 void mark_for_call() {
1820 bit_field_ = IsForCallField::update(bit_field_, true);
1821 }
1822 bool is_for_call() const { return IsForCallField::decode(bit_field_); }
1823
1824 bool IsSuperAccess() { return obj()->IsSuperPropertyReference(); }
1825
1826 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1827 FeedbackVectorSlotCache* cache) override {
1828 FeedbackVectorSlotKind kind = key()->IsPropertyName()
1829 ? FeedbackVectorSlotKind::LOAD_IC
1830 : FeedbackVectorSlotKind::KEYED_LOAD_IC;
1831 property_feedback_slot_ = spec->AddSlot(kind);
1832 }
1833
1834 FeedbackVectorSlot PropertyFeedbackSlot() const {
1835 return property_feedback_slot_;
1836 }
1837
1838 static LhsKind GetAssignType(Property* property) {
1839 if (property == NULL) return VARIABLE;
1840 bool super_access = property->IsSuperAccess();
1841 return (property->key()->IsPropertyName())
1842 ? (super_access ? NAMED_SUPER_PROPERTY : NAMED_PROPERTY)
1843 : (super_access ? KEYED_SUPER_PROPERTY : KEYED_PROPERTY);
1844 }
1845
1846 protected:
1847 Property(Zone* zone, Expression* obj, Expression* key, int pos)
1848 : Expression(zone, pos),
1849 bit_field_(IsForCallField::encode(false) |
1850 IsStringAccessField::encode(false) |
1851 InlineCacheStateField::encode(UNINITIALIZED)),
1852 obj_(obj),
1853 key_(key) {}
1854 static int parent_num_ids() { return Expression::num_ids(); }
1855
1856 private:
1857 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1858
1859 class IsForCallField : public BitField8<bool, 0, 1> {};
1860 class IsStringAccessField : public BitField8<bool, 1, 1> {};
1861 class KeyTypeField : public BitField8<IcCheckType, 2, 1> {};
1862 class InlineCacheStateField : public BitField8<InlineCacheState, 3, 4> {};
1863 uint8_t bit_field_;
1864 FeedbackVectorSlot property_feedback_slot_;
1865 Expression* obj_;
1866 Expression* key_;
1867 SmallMapList receiver_types_;
1868};
1869
1870
1871class Call final : public Expression {
1872 public:
1873 DECLARE_NODE_TYPE(Call)
1874
1875 Expression* expression() const { return expression_; }
1876 ZoneList<Expression*>* arguments() const { return arguments_; }
1877
1878 void set_expression(Expression* e) { expression_ = e; }
1879
1880 // Type feedback information.
1881 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
1882 FeedbackVectorSlotCache* cache) override;
1883
1884 FeedbackVectorSlot CallFeedbackSlot() const { return stub_slot_; }
1885
1886 FeedbackVectorSlot CallFeedbackICSlot() const { return ic_slot_; }
1887
1888 SmallMapList* GetReceiverTypes() override {
1889 if (expression()->IsProperty()) {
1890 return expression()->AsProperty()->GetReceiverTypes();
1891 }
1892 return NULL;
1893 }
1894
1895 bool IsMonomorphic() override {
1896 if (expression()->IsProperty()) {
1897 return expression()->AsProperty()->IsMonomorphic();
1898 }
1899 return !target_.is_null();
1900 }
1901
1902 bool global_call() const {
1903 VariableProxy* proxy = expression_->AsVariableProxy();
1904 return proxy != NULL && proxy->var()->IsUnallocatedOrGlobalSlot();
1905 }
1906
1907 bool known_global_function() const {
1908 return global_call() && !target_.is_null();
1909 }
1910
1911 Handle<JSFunction> target() { return target_; }
1912
1913 Handle<AllocationSite> allocation_site() { return allocation_site_; }
1914
1915 void SetKnownGlobalTarget(Handle<JSFunction> target) {
1916 target_ = target;
1917 set_is_uninitialized(false);
1918 }
1919 void set_target(Handle<JSFunction> target) { target_ = target; }
1920 void set_allocation_site(Handle<AllocationSite> site) {
1921 allocation_site_ = site;
1922 }
1923
1924 static int num_ids() { return parent_num_ids() + 4; }
1925 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
1926 BailoutId EvalId() const { return BailoutId(local_id(1)); }
1927 BailoutId LookupId() const { return BailoutId(local_id(2)); }
1928 BailoutId CallId() const { return BailoutId(local_id(3)); }
1929
1930 bool is_uninitialized() const {
1931 return IsUninitializedField::decode(bit_field_);
1932 }
1933 void set_is_uninitialized(bool b) {
1934 bit_field_ = IsUninitializedField::update(bit_field_, b);
1935 }
1936
Ben Murdoch097c5b22016-05-18 11:27:45 +01001937 TailCallMode tail_call_mode() const {
1938 return IsTailField::decode(bit_field_) ? TailCallMode::kAllow
1939 : TailCallMode::kDisallow;
1940 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001941 void MarkTail() override {
1942 bit_field_ = IsTailField::update(bit_field_, true);
1943 }
1944
1945 enum CallType {
1946 POSSIBLY_EVAL_CALL,
1947 GLOBAL_CALL,
1948 LOOKUP_SLOT_CALL,
1949 NAMED_PROPERTY_CALL,
1950 KEYED_PROPERTY_CALL,
1951 NAMED_SUPER_PROPERTY_CALL,
1952 KEYED_SUPER_PROPERTY_CALL,
1953 SUPER_CALL,
1954 OTHER_CALL
1955 };
1956
1957 // Helpers to determine how to handle the call.
1958 CallType GetCallType(Isolate* isolate) const;
1959 bool IsUsingCallFeedbackSlot(Isolate* isolate) const;
1960 bool IsUsingCallFeedbackICSlot(Isolate* isolate) const;
1961
1962#ifdef DEBUG
1963 // Used to assert that the FullCodeGenerator records the return site.
1964 bool return_is_recorded_;
1965#endif
1966
1967 protected:
1968 Call(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
1969 int pos)
1970 : Expression(zone, pos),
1971 expression_(expression),
1972 arguments_(arguments),
1973 bit_field_(IsUninitializedField::encode(false)) {
1974 if (expression->IsProperty()) {
1975 expression->AsProperty()->mark_for_call();
1976 }
1977 }
1978 static int parent_num_ids() { return Expression::num_ids(); }
1979
1980 private:
1981 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
1982
1983 FeedbackVectorSlot ic_slot_;
1984 FeedbackVectorSlot stub_slot_;
1985 Expression* expression_;
1986 ZoneList<Expression*>* arguments_;
1987 Handle<JSFunction> target_;
1988 Handle<AllocationSite> allocation_site_;
1989 class IsUninitializedField : public BitField8<bool, 0, 1> {};
1990 class IsTailField : public BitField8<bool, 1, 1> {};
1991 uint8_t bit_field_;
1992};
1993
1994
1995class CallNew final : public Expression {
1996 public:
1997 DECLARE_NODE_TYPE(CallNew)
1998
1999 Expression* expression() const { return expression_; }
2000 ZoneList<Expression*>* arguments() const { return arguments_; }
2001
2002 void set_expression(Expression* e) { expression_ = e; }
2003
2004 // Type feedback information.
2005 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
2006 FeedbackVectorSlotCache* cache) override {
2007 callnew_feedback_slot_ = spec->AddGeneralSlot();
2008 }
2009
2010 FeedbackVectorSlot CallNewFeedbackSlot() {
2011 DCHECK(!callnew_feedback_slot_.IsInvalid());
2012 return callnew_feedback_slot_;
2013 }
2014
2015 bool IsMonomorphic() override { return is_monomorphic_; }
2016 Handle<JSFunction> target() const { return target_; }
2017 Handle<AllocationSite> allocation_site() const {
2018 return allocation_site_;
2019 }
2020
2021 static int num_ids() { return parent_num_ids() + 1; }
2022 static int feedback_slots() { return 1; }
2023 BailoutId ReturnId() const { return BailoutId(local_id(0)); }
2024
2025 void set_allocation_site(Handle<AllocationSite> site) {
2026 allocation_site_ = site;
2027 }
2028 void set_is_monomorphic(bool monomorphic) { is_monomorphic_ = monomorphic; }
2029 void set_target(Handle<JSFunction> target) { target_ = target; }
2030 void SetKnownGlobalTarget(Handle<JSFunction> target) {
2031 target_ = target;
2032 is_monomorphic_ = true;
2033 }
2034
2035 protected:
2036 CallNew(Zone* zone, Expression* expression, ZoneList<Expression*>* arguments,
2037 int pos)
2038 : Expression(zone, pos),
2039 expression_(expression),
2040 arguments_(arguments),
2041 is_monomorphic_(false) {}
2042
2043 static int parent_num_ids() { return Expression::num_ids(); }
2044
2045 private:
2046 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2047
2048 Expression* expression_;
2049 ZoneList<Expression*>* arguments_;
2050 bool is_monomorphic_;
2051 Handle<JSFunction> target_;
2052 Handle<AllocationSite> allocation_site_;
2053 FeedbackVectorSlot callnew_feedback_slot_;
2054};
2055
2056
2057// The CallRuntime class does not represent any official JavaScript
2058// language construct. Instead it is used to call a C or JS function
2059// with a set of arguments. This is used from the builtins that are
2060// implemented in JavaScript (see "v8natives.js").
2061class CallRuntime final : public Expression {
2062 public:
2063 DECLARE_NODE_TYPE(CallRuntime)
2064
2065 ZoneList<Expression*>* arguments() const { return arguments_; }
2066 bool is_jsruntime() const { return function_ == NULL; }
2067
2068 int context_index() const {
2069 DCHECK(is_jsruntime());
2070 return context_index_;
2071 }
2072 const Runtime::Function* function() const {
2073 DCHECK(!is_jsruntime());
2074 return function_;
2075 }
2076
2077 static int num_ids() { return parent_num_ids() + 1; }
2078 BailoutId CallId() { return BailoutId(local_id(0)); }
2079
2080 const char* debug_name() {
2081 return is_jsruntime() ? "(context function)" : function_->name;
2082 }
2083
2084 protected:
2085 CallRuntime(Zone* zone, const Runtime::Function* function,
2086 ZoneList<Expression*>* arguments, int pos)
2087 : Expression(zone, pos), function_(function), arguments_(arguments) {}
2088
2089 CallRuntime(Zone* zone, int context_index, ZoneList<Expression*>* arguments,
2090 int pos)
2091 : Expression(zone, pos),
2092 function_(NULL),
2093 context_index_(context_index),
2094 arguments_(arguments) {}
2095
2096 static int parent_num_ids() { return Expression::num_ids(); }
2097
2098 private:
2099 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2100
2101 const Runtime::Function* function_;
2102 int context_index_;
2103 ZoneList<Expression*>* arguments_;
2104};
2105
2106
2107class UnaryOperation final : public Expression {
2108 public:
2109 DECLARE_NODE_TYPE(UnaryOperation)
2110
2111 Token::Value op() const { return op_; }
2112 Expression* expression() const { return expression_; }
2113 void set_expression(Expression* e) { expression_ = e; }
2114
2115 // For unary not (Token::NOT), the AST ids where true and false will
2116 // actually be materialized, respectively.
2117 static int num_ids() { return parent_num_ids() + 2; }
2118 BailoutId MaterializeTrueId() const { return BailoutId(local_id(0)); }
2119 BailoutId MaterializeFalseId() const { return BailoutId(local_id(1)); }
2120
2121 void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2122
2123 protected:
2124 UnaryOperation(Zone* zone, Token::Value op, Expression* expression, int pos)
2125 : Expression(zone, pos), op_(op), expression_(expression) {
2126 DCHECK(Token::IsUnaryOp(op));
2127 }
2128 static int parent_num_ids() { return Expression::num_ids(); }
2129
2130 private:
2131 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2132
2133 Token::Value op_;
2134 Expression* expression_;
2135};
2136
2137
2138class BinaryOperation final : public Expression {
2139 public:
2140 DECLARE_NODE_TYPE(BinaryOperation)
2141
2142 Token::Value op() const { return static_cast<Token::Value>(op_); }
2143 Expression* left() const { return left_; }
2144 void set_left(Expression* e) { left_ = e; }
2145 Expression* right() const { return right_; }
2146 void set_right(Expression* e) { right_ = e; }
2147 Handle<AllocationSite> allocation_site() const { return allocation_site_; }
2148 void set_allocation_site(Handle<AllocationSite> allocation_site) {
2149 allocation_site_ = allocation_site;
2150 }
2151
2152 void MarkTail() override {
2153 switch (op()) {
2154 case Token::COMMA:
2155 case Token::AND:
2156 case Token::OR:
2157 right_->MarkTail();
2158 default:
2159 break;
2160 }
2161 }
2162
2163 // The short-circuit logical operations need an AST ID for their
2164 // right-hand subexpression.
2165 static int num_ids() { return parent_num_ids() + 2; }
2166 BailoutId RightId() const { return BailoutId(local_id(0)); }
2167
2168 TypeFeedbackId BinaryOperationFeedbackId() const {
2169 return TypeFeedbackId(local_id(1));
2170 }
2171 Maybe<int> fixed_right_arg() const {
2172 return has_fixed_right_arg_ ? Just(fixed_right_arg_value_) : Nothing<int>();
2173 }
2174 void set_fixed_right_arg(Maybe<int> arg) {
2175 has_fixed_right_arg_ = arg.IsJust();
2176 if (arg.IsJust()) fixed_right_arg_value_ = arg.FromJust();
2177 }
2178
2179 void RecordToBooleanTypeFeedback(TypeFeedbackOracle* oracle) override;
2180
2181 protected:
2182 BinaryOperation(Zone* zone, Token::Value op, Expression* left,
2183 Expression* right, int pos)
2184 : Expression(zone, pos),
2185 op_(static_cast<byte>(op)),
2186 has_fixed_right_arg_(false),
2187 fixed_right_arg_value_(0),
2188 left_(left),
2189 right_(right) {
2190 DCHECK(Token::IsBinaryOp(op));
2191 }
2192 static int parent_num_ids() { return Expression::num_ids(); }
2193
2194 private:
2195 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2196
2197 const byte op_; // actually Token::Value
2198 // TODO(rossberg): the fixed arg should probably be represented as a Constant
2199 // type for the RHS. Currenty it's actually a Maybe<int>
2200 bool has_fixed_right_arg_;
2201 int fixed_right_arg_value_;
2202 Expression* left_;
2203 Expression* right_;
2204 Handle<AllocationSite> allocation_site_;
2205};
2206
2207
2208class CountOperation final : public Expression {
2209 public:
2210 DECLARE_NODE_TYPE(CountOperation)
2211
2212 bool is_prefix() const { return IsPrefixField::decode(bit_field_); }
2213 bool is_postfix() const { return !is_prefix(); }
2214
2215 Token::Value op() const { return TokenField::decode(bit_field_); }
2216 Token::Value binary_op() {
2217 return (op() == Token::INC) ? Token::ADD : Token::SUB;
2218 }
2219
2220 Expression* expression() const { return expression_; }
2221 void set_expression(Expression* e) { expression_ = e; }
2222
2223 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2224 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2225 IcCheckType GetKeyType() const override {
2226 return KeyTypeField::decode(bit_field_);
2227 }
2228 KeyedAccessStoreMode GetStoreMode() const override {
2229 return StoreModeField::decode(bit_field_);
2230 }
2231 Type* type() const { return type_; }
2232 void set_key_type(IcCheckType type) {
2233 bit_field_ = KeyTypeField::update(bit_field_, type);
2234 }
2235 void set_store_mode(KeyedAccessStoreMode mode) {
2236 bit_field_ = StoreModeField::update(bit_field_, mode);
2237 }
2238 void set_type(Type* type) { type_ = type; }
2239
2240 static int num_ids() { return parent_num_ids() + 4; }
2241 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2242 BailoutId ToNumberId() const { return BailoutId(local_id(1)); }
2243 TypeFeedbackId CountBinOpFeedbackId() const {
2244 return TypeFeedbackId(local_id(2));
2245 }
2246 TypeFeedbackId CountStoreFeedbackId() const {
2247 return TypeFeedbackId(local_id(3));
2248 }
2249
2250 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
2251 FeedbackVectorSlotCache* cache) override;
2252 FeedbackVectorSlot CountSlot() const { return slot_; }
2253
2254 protected:
2255 CountOperation(Zone* zone, Token::Value op, bool is_prefix, Expression* expr,
2256 int pos)
2257 : Expression(zone, pos),
2258 bit_field_(
2259 IsPrefixField::encode(is_prefix) | KeyTypeField::encode(ELEMENT) |
2260 StoreModeField::encode(STANDARD_STORE) | TokenField::encode(op)),
2261 type_(NULL),
2262 expression_(expr) {}
2263 static int parent_num_ids() { return Expression::num_ids(); }
2264
2265 private:
2266 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2267
2268 class IsPrefixField : public BitField16<bool, 0, 1> {};
2269 class KeyTypeField : public BitField16<IcCheckType, 1, 1> {};
2270 class StoreModeField : public BitField16<KeyedAccessStoreMode, 2, 3> {};
2271 class TokenField : public BitField16<Token::Value, 5, 8> {};
2272
2273 // Starts with 16-bit field, which should get packed together with
2274 // Expression's trailing 16-bit field.
2275 uint16_t bit_field_;
2276 Type* type_;
2277 Expression* expression_;
2278 SmallMapList receiver_types_;
2279 FeedbackVectorSlot slot_;
2280};
2281
2282
2283class CompareOperation final : public Expression {
2284 public:
2285 DECLARE_NODE_TYPE(CompareOperation)
2286
2287 Token::Value op() const { return op_; }
2288 Expression* left() const { return left_; }
2289 Expression* right() const { return right_; }
2290
2291 void set_left(Expression* e) { left_ = e; }
2292 void set_right(Expression* e) { right_ = e; }
2293
2294 // Type feedback information.
2295 static int num_ids() { return parent_num_ids() + 1; }
2296 TypeFeedbackId CompareOperationFeedbackId() const {
2297 return TypeFeedbackId(local_id(0));
2298 }
2299 Type* combined_type() const { return combined_type_; }
2300 void set_combined_type(Type* type) { combined_type_ = type; }
2301
2302 // Match special cases.
2303 bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check);
Ben Murdochda12d292016-06-02 14:46:10 +01002304 bool IsLiteralCompareUndefined(Expression** expr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002305 bool IsLiteralCompareNull(Expression** expr);
2306
2307 protected:
2308 CompareOperation(Zone* zone, Token::Value op, Expression* left,
2309 Expression* right, int pos)
2310 : Expression(zone, pos),
2311 op_(op),
2312 left_(left),
2313 right_(right),
Ben Murdoch097c5b22016-05-18 11:27:45 +01002314 combined_type_(Type::None()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002315 DCHECK(Token::IsCompareOp(op));
2316 }
2317 static int parent_num_ids() { return Expression::num_ids(); }
2318
2319 private:
2320 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2321
2322 Token::Value op_;
2323 Expression* left_;
2324 Expression* right_;
2325
2326 Type* combined_type_;
2327};
2328
2329
2330class Spread final : public Expression {
2331 public:
2332 DECLARE_NODE_TYPE(Spread)
2333
2334 Expression* expression() const { return expression_; }
2335 void set_expression(Expression* e) { expression_ = e; }
2336
Ben Murdoch097c5b22016-05-18 11:27:45 +01002337 int expression_position() const { return expr_pos_; }
2338
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002339 static int num_ids() { return parent_num_ids(); }
2340
2341 protected:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002342 Spread(Zone* zone, Expression* expression, int pos, int expr_pos)
2343 : Expression(zone, pos), expression_(expression), expr_pos_(expr_pos) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002344 static int parent_num_ids() { return Expression::num_ids(); }
2345
2346 private:
2347 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2348
2349 Expression* expression_;
Ben Murdoch097c5b22016-05-18 11:27:45 +01002350 int expr_pos_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002351};
2352
2353
2354class Conditional final : public Expression {
2355 public:
2356 DECLARE_NODE_TYPE(Conditional)
2357
2358 Expression* condition() const { return condition_; }
2359 Expression* then_expression() const { return then_expression_; }
2360 Expression* else_expression() const { return else_expression_; }
2361
2362 void set_condition(Expression* e) { condition_ = e; }
2363 void set_then_expression(Expression* e) { then_expression_ = e; }
2364 void set_else_expression(Expression* e) { else_expression_ = e; }
2365
2366 void MarkTail() override {
2367 then_expression_->MarkTail();
2368 else_expression_->MarkTail();
2369 }
2370
2371 static int num_ids() { return parent_num_ids() + 2; }
2372 BailoutId ThenId() const { return BailoutId(local_id(0)); }
2373 BailoutId ElseId() const { return BailoutId(local_id(1)); }
2374
2375 protected:
2376 Conditional(Zone* zone, Expression* condition, Expression* then_expression,
2377 Expression* else_expression, int position)
2378 : Expression(zone, position),
2379 condition_(condition),
2380 then_expression_(then_expression),
2381 else_expression_(else_expression) {}
2382 static int parent_num_ids() { return Expression::num_ids(); }
2383
2384 private:
2385 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2386
2387 Expression* condition_;
2388 Expression* then_expression_;
2389 Expression* else_expression_;
2390};
2391
2392
2393class Assignment final : public Expression {
2394 public:
2395 DECLARE_NODE_TYPE(Assignment)
2396
2397 Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; }
2398
2399 Token::Value binary_op() const;
2400
2401 Token::Value op() const { return TokenField::decode(bit_field_); }
2402 Expression* target() const { return target_; }
2403 Expression* value() const { return value_; }
2404
2405 void set_target(Expression* e) { target_ = e; }
2406 void set_value(Expression* e) { value_ = e; }
2407
2408 BinaryOperation* binary_operation() const { return binary_operation_; }
2409
2410 // This check relies on the definition order of token in token.h.
2411 bool is_compound() const { return op() > Token::ASSIGN; }
2412
2413 static int num_ids() { return parent_num_ids() + 2; }
2414 BailoutId AssignmentId() const { return BailoutId(local_id(0)); }
2415
2416 // Type feedback information.
2417 TypeFeedbackId AssignmentFeedbackId() { return TypeFeedbackId(local_id(1)); }
2418 bool IsMonomorphic() override { return receiver_types_.length() == 1; }
2419 bool IsUninitialized() const {
2420 return IsUninitializedField::decode(bit_field_);
2421 }
2422 bool HasNoTypeInformation() {
2423 return IsUninitializedField::decode(bit_field_);
2424 }
2425 SmallMapList* GetReceiverTypes() override { return &receiver_types_; }
2426 IcCheckType GetKeyType() const override {
2427 return KeyTypeField::decode(bit_field_);
2428 }
2429 KeyedAccessStoreMode GetStoreMode() const override {
2430 return StoreModeField::decode(bit_field_);
2431 }
2432 void set_is_uninitialized(bool b) {
2433 bit_field_ = IsUninitializedField::update(bit_field_, b);
2434 }
2435 void set_key_type(IcCheckType key_type) {
2436 bit_field_ = KeyTypeField::update(bit_field_, key_type);
2437 }
2438 void set_store_mode(KeyedAccessStoreMode mode) {
2439 bit_field_ = StoreModeField::update(bit_field_, mode);
2440 }
2441
2442 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
2443 FeedbackVectorSlotCache* cache) override;
2444 FeedbackVectorSlot AssignmentSlot() const { return slot_; }
2445
2446 protected:
2447 Assignment(Zone* zone, Token::Value op, Expression* target, Expression* value,
2448 int pos);
2449 static int parent_num_ids() { return Expression::num_ids(); }
2450
2451 private:
2452 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2453
2454 class IsUninitializedField : public BitField16<bool, 0, 1> {};
2455 class KeyTypeField
2456 : public BitField16<IcCheckType, IsUninitializedField::kNext, 1> {};
2457 class StoreModeField
2458 : public BitField16<KeyedAccessStoreMode, KeyTypeField::kNext, 3> {};
2459 class TokenField : public BitField16<Token::Value, StoreModeField::kNext, 8> {
2460 };
2461
2462 // Starts with 16-bit field, which should get packed together with
2463 // Expression's trailing 16-bit field.
2464 uint16_t bit_field_;
2465 Expression* target_;
2466 Expression* value_;
2467 BinaryOperation* binary_operation_;
2468 SmallMapList receiver_types_;
2469 FeedbackVectorSlot slot_;
2470};
2471
2472
Ben Murdoch097c5b22016-05-18 11:27:45 +01002473// The RewritableExpression class is a wrapper for AST nodes that wait
2474// for some potential rewriting. However, even if such nodes are indeed
2475// rewritten, the RewritableExpression wrapper nodes will survive in the
2476// final AST and should be just ignored, i.e., they should be treated as
2477// equivalent to the wrapped nodes. For this reason and to simplify later
2478// phases, RewritableExpressions are considered as exceptions of AST nodes
2479// in the following sense:
2480//
2481// 1. IsRewritableExpression and AsRewritableExpression behave as usual.
2482// 2. All other Is* and As* methods are practically delegated to the
2483// wrapped node, i.e. IsArrayLiteral() will return true iff the
2484// wrapped node is an array literal.
2485//
2486// Furthermore, an invariant that should be respected is that the wrapped
2487// node is not a RewritableExpression.
2488class RewritableExpression : public Expression {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002489 public:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002490 DECLARE_NODE_TYPE(RewritableExpression)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002491
Ben Murdoch097c5b22016-05-18 11:27:45 +01002492 Expression* expression() const { return expr_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002493 bool is_rewritten() const { return is_rewritten_; }
2494
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002495 void Rewrite(Expression* new_expression) {
2496 DCHECK(!is_rewritten());
2497 DCHECK_NOT_NULL(new_expression);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002498 DCHECK(!new_expression->IsRewritableExpression());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002499 expr_ = new_expression;
2500 is_rewritten_ = true;
2501 }
2502
2503 static int num_ids() { return parent_num_ids(); }
2504
2505 protected:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002506 RewritableExpression(Zone* zone, Expression* expression)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002507 : Expression(zone, expression->position()),
2508 is_rewritten_(false),
Ben Murdoch097c5b22016-05-18 11:27:45 +01002509 expr_(expression) {
2510 DCHECK(!expression->IsRewritableExpression());
2511 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002512
2513 private:
2514 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2515
2516 bool is_rewritten_;
2517 Expression* expr_;
2518};
2519
Ben Murdochda12d292016-06-02 14:46:10 +01002520// Our Yield is different from the JS yield in that it "returns" its argument as
2521// is, without wrapping it in an iterator result object. Such wrapping, if
2522// desired, must be done beforehand (see the parser).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002523class Yield final : public Expression {
2524 public:
2525 DECLARE_NODE_TYPE(Yield)
2526
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002527 Expression* generator_object() const { return generator_object_; }
2528 Expression* expression() const { return expression_; }
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; }
2532
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002533 protected:
2534 Yield(Zone* zone, Expression* generator_object, Expression* expression,
Ben Murdochda12d292016-06-02 14:46:10 +01002535 int pos)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002536 : Expression(zone, pos),
2537 generator_object_(generator_object),
Ben Murdochda12d292016-06-02 14:46:10 +01002538 expression_(expression) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002539
2540 private:
2541 Expression* generator_object_;
2542 Expression* expression_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002543};
2544
2545
2546class Throw final : public Expression {
2547 public:
2548 DECLARE_NODE_TYPE(Throw)
2549
2550 Expression* exception() const { return exception_; }
2551 void set_exception(Expression* e) { exception_ = e; }
2552
2553 protected:
2554 Throw(Zone* zone, Expression* exception, int pos)
2555 : Expression(zone, pos), exception_(exception) {}
2556
2557 private:
2558 Expression* exception_;
2559};
2560
2561
2562class FunctionLiteral final : public Expression {
2563 public:
2564 enum FunctionType {
2565 kAnonymousExpression,
2566 kNamedExpression,
2567 kDeclaration,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002568 kAccessorOrMethod
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002569 };
2570
2571 enum ParameterFlag { kNoDuplicateParameters, kHasDuplicateParameters };
2572
2573 enum EagerCompileHint { kShouldEagerCompile, kShouldLazyCompile };
2574
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002575 DECLARE_NODE_TYPE(FunctionLiteral)
2576
2577 Handle<String> name() const { return raw_name_->string(); }
2578 const AstString* raw_name() const { return raw_name_; }
2579 void set_raw_name(const AstString* name) { raw_name_ = name; }
2580 Scope* scope() const { return scope_; }
2581 ZoneList<Statement*>* body() const { return body_; }
2582 void set_function_token_position(int pos) { function_token_position_ = pos; }
2583 int function_token_position() const { return function_token_position_; }
2584 int start_position() const;
2585 int end_position() const;
2586 int SourceSize() const { return end_position() - start_position(); }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002587 bool is_declaration() const { return IsDeclaration::decode(bitfield_); }
2588 bool is_named_expression() const {
2589 return IsNamedExpression::decode(bitfield_);
2590 }
2591 bool is_anonymous_expression() const {
2592 return IsAnonymousExpression::decode(bitfield_);
2593 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002594 LanguageMode language_mode() const;
2595
2596 static bool NeedsHomeObject(Expression* expr);
2597
2598 int materialized_literal_count() { return materialized_literal_count_; }
2599 int expected_property_count() { return expected_property_count_; }
2600 int parameter_count() { return parameter_count_; }
2601
2602 bool AllowsLazyCompilation();
2603 bool AllowsLazyCompilationWithoutContext();
2604
2605 Handle<String> debug_name() const {
2606 if (raw_name_ != NULL && !raw_name_->IsEmpty()) {
2607 return raw_name_->string();
2608 }
2609 return inferred_name();
2610 }
2611
2612 Handle<String> inferred_name() const {
2613 if (!inferred_name_.is_null()) {
2614 DCHECK(raw_inferred_name_ == NULL);
2615 return inferred_name_;
2616 }
2617 if (raw_inferred_name_ != NULL) {
2618 return raw_inferred_name_->string();
2619 }
2620 UNREACHABLE();
2621 return Handle<String>();
2622 }
2623
2624 // Only one of {set_inferred_name, set_raw_inferred_name} should be called.
2625 void set_inferred_name(Handle<String> inferred_name) {
2626 DCHECK(!inferred_name.is_null());
2627 inferred_name_ = inferred_name;
2628 DCHECK(raw_inferred_name_== NULL || raw_inferred_name_->IsEmpty());
2629 raw_inferred_name_ = NULL;
2630 }
2631
2632 void set_raw_inferred_name(const AstString* raw_inferred_name) {
2633 DCHECK(raw_inferred_name != NULL);
2634 raw_inferred_name_ = raw_inferred_name;
2635 DCHECK(inferred_name_.is_null());
2636 inferred_name_ = Handle<String>();
2637 }
2638
2639 bool pretenure() const { return Pretenure::decode(bitfield_); }
2640 void set_pretenure() { bitfield_ = Pretenure::update(bitfield_, true); }
2641
2642 bool has_duplicate_parameters() const {
2643 return HasDuplicateParameters::decode(bitfield_);
2644 }
2645
2646 bool is_function() const { return IsFunction::decode(bitfield_); }
2647
2648 // This is used as a heuristic on when to eagerly compile a function
2649 // literal. We consider the following constructs as hints that the
2650 // function will be called immediately:
2651 // - (function() { ... })();
2652 // - var x = function() { ... }();
2653 bool should_eager_compile() const {
2654 return ShouldEagerCompile::decode(bitfield_);
2655 }
2656 void set_should_eager_compile() {
2657 bitfield_ = ShouldEagerCompile::update(bitfield_, true);
2658 }
2659
2660 // A hint that we expect this function to be called (exactly) once,
2661 // i.e. we suspect it's an initialization function.
2662 bool should_be_used_once_hint() const {
2663 return ShouldBeUsedOnceHint::decode(bitfield_);
2664 }
2665 void set_should_be_used_once_hint() {
2666 bitfield_ = ShouldBeUsedOnceHint::update(bitfield_, true);
2667 }
2668
2669 FunctionKind kind() const { return FunctionKindBits::decode(bitfield_); }
2670
2671 int ast_node_count() { return ast_properties_.node_count(); }
2672 AstProperties::Flags flags() const { return ast_properties_.flags(); }
2673 void set_ast_properties(AstProperties* ast_properties) {
2674 ast_properties_ = *ast_properties;
2675 }
2676 const FeedbackVectorSpec* feedback_vector_spec() const {
2677 return ast_properties_.get_spec();
2678 }
2679 bool dont_optimize() { return dont_optimize_reason_ != kNoReason; }
2680 BailoutReason dont_optimize_reason() { return dont_optimize_reason_; }
2681 void set_dont_optimize_reason(BailoutReason reason) {
2682 dont_optimize_reason_ = reason;
2683 }
2684
Ben Murdoch097c5b22016-05-18 11:27:45 +01002685 bool IsAnonymousFunctionDefinition() const final {
2686 return is_anonymous_expression();
2687 }
2688
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002689 protected:
2690 FunctionLiteral(Zone* zone, const AstString* name,
2691 AstValueFactory* ast_value_factory, Scope* scope,
2692 ZoneList<Statement*>* body, int materialized_literal_count,
2693 int expected_property_count, int parameter_count,
2694 FunctionType function_type,
2695 ParameterFlag has_duplicate_parameters,
2696 EagerCompileHint eager_compile_hint, FunctionKind kind,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002697 int position, bool is_function)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002698 : Expression(zone, position),
2699 raw_name_(name),
2700 scope_(scope),
2701 body_(body),
2702 raw_inferred_name_(ast_value_factory->empty_string()),
2703 ast_properties_(zone),
2704 dont_optimize_reason_(kNoReason),
2705 materialized_literal_count_(materialized_literal_count),
2706 expected_property_count_(expected_property_count),
2707 parameter_count_(parameter_count),
2708 function_token_position_(RelocInfo::kNoPosition) {
2709 bitfield_ =
Ben Murdoch097c5b22016-05-18 11:27:45 +01002710 IsDeclaration::encode(function_type == kDeclaration) |
2711 IsNamedExpression::encode(function_type == kNamedExpression) |
2712 IsAnonymousExpression::encode(function_type == kAnonymousExpression) |
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002713 Pretenure::encode(false) |
2714 HasDuplicateParameters::encode(has_duplicate_parameters ==
2715 kHasDuplicateParameters) |
Ben Murdoch097c5b22016-05-18 11:27:45 +01002716 IsFunction::encode(is_function) |
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002717 ShouldEagerCompile::encode(eager_compile_hint == kShouldEagerCompile) |
2718 FunctionKindBits::encode(kind) | ShouldBeUsedOnceHint::encode(false);
2719 DCHECK(IsValidFunctionKind(kind));
2720 }
2721
2722 private:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002723 class IsDeclaration : public BitField16<bool, 0, 1> {};
2724 class IsNamedExpression : public BitField16<bool, 1, 1> {};
2725 class IsAnonymousExpression : public BitField16<bool, 2, 1> {};
2726 class Pretenure : public BitField16<bool, 3, 1> {};
2727 class HasDuplicateParameters : public BitField16<bool, 4, 1> {};
2728 class IsFunction : public BitField16<bool, 5, 1> {};
2729 class ShouldEagerCompile : public BitField16<bool, 6, 1> {};
2730 class ShouldBeUsedOnceHint : public BitField16<bool, 7, 1> {};
2731 class FunctionKindBits : public BitField16<FunctionKind, 8, 8> {};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002732
2733 // Start with 16-bit field, which should get packed together
2734 // with Expression's trailing 16-bit field.
2735 uint16_t bitfield_;
2736
2737 const AstString* raw_name_;
2738 Scope* scope_;
2739 ZoneList<Statement*>* body_;
2740 const AstString* raw_inferred_name_;
2741 Handle<String> inferred_name_;
2742 AstProperties ast_properties_;
2743 BailoutReason dont_optimize_reason_;
2744
2745 int materialized_literal_count_;
2746 int expected_property_count_;
2747 int parameter_count_;
2748 int function_token_position_;
2749};
2750
2751
2752class ClassLiteral final : public Expression {
2753 public:
2754 typedef ObjectLiteralProperty Property;
2755
2756 DECLARE_NODE_TYPE(ClassLiteral)
2757
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002758 Scope* scope() const { return scope_; }
2759 VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
2760 Expression* extends() const { return extends_; }
2761 void set_extends(Expression* e) { extends_ = e; }
2762 FunctionLiteral* constructor() const { return constructor_; }
2763 void set_constructor(FunctionLiteral* f) { constructor_ = f; }
2764 ZoneList<Property*>* properties() const { return properties_; }
2765 int start_position() const { return position(); }
2766 int end_position() const { return end_position_; }
2767
2768 BailoutId EntryId() const { return BailoutId(local_id(0)); }
2769 BailoutId DeclsId() const { return BailoutId(local_id(1)); }
2770 BailoutId ExitId() { return BailoutId(local_id(2)); }
2771 BailoutId CreateLiteralId() const { return BailoutId(local_id(3)); }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002772 BailoutId PrototypeId() { return BailoutId(local_id(4)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002773
2774 // Return an AST id for a property that is used in simulate instructions.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002775 BailoutId GetIdForProperty(int i) { return BailoutId(local_id(i + 5)); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002776
2777 // Unlike other AST nodes, this number of bailout IDs allocated for an
2778 // ClassLiteral can vary, so num_ids() is not a static method.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002779 int num_ids() const { return parent_num_ids() + 5 + properties()->length(); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002780
2781 // Object literals need one feedback slot for each non-trivial value, as well
2782 // as some slots for home objects.
2783 void AssignFeedbackVectorSlots(Isolate* isolate, FeedbackVectorSpec* spec,
2784 FeedbackVectorSlotCache* cache) override;
2785
2786 bool NeedsProxySlot() const {
2787 return class_variable_proxy() != nullptr &&
2788 class_variable_proxy()->var()->IsUnallocated();
2789 }
2790
Ben Murdoch097c5b22016-05-18 11:27:45 +01002791 FeedbackVectorSlot PrototypeSlot() const { return prototype_slot_; }
2792 FeedbackVectorSlot ProxySlot() const { return proxy_slot_; }
2793
2794 bool IsAnonymousFunctionDefinition() const final {
2795 return constructor()->raw_name()->length() == 0;
2796 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002797
2798 protected:
Ben Murdoch097c5b22016-05-18 11:27:45 +01002799 ClassLiteral(Zone* zone, Scope* scope, VariableProxy* class_variable_proxy,
2800 Expression* extends, FunctionLiteral* constructor,
2801 ZoneList<Property*>* properties, int start_position,
2802 int end_position)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002803 : Expression(zone, start_position),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002804 scope_(scope),
2805 class_variable_proxy_(class_variable_proxy),
2806 extends_(extends),
2807 constructor_(constructor),
2808 properties_(properties),
2809 end_position_(end_position) {}
2810
2811 static int parent_num_ids() { return Expression::num_ids(); }
2812
2813 private:
2814 int local_id(int n) const { return base_id() + parent_num_ids() + n; }
2815
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002816 Scope* scope_;
2817 VariableProxy* class_variable_proxy_;
2818 Expression* extends_;
2819 FunctionLiteral* constructor_;
2820 ZoneList<Property*>* properties_;
2821 int end_position_;
Ben Murdoch097c5b22016-05-18 11:27:45 +01002822 FeedbackVectorSlot prototype_slot_;
2823 FeedbackVectorSlot proxy_slot_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002824};
2825
2826
2827class NativeFunctionLiteral final : public Expression {
2828 public:
2829 DECLARE_NODE_TYPE(NativeFunctionLiteral)
2830
2831 Handle<String> name() const { return name_->string(); }
2832 v8::Extension* extension() const { return extension_; }
2833
2834 protected:
2835 NativeFunctionLiteral(Zone* zone, const AstRawString* name,
2836 v8::Extension* extension, int pos)
2837 : Expression(zone, pos), name_(name), extension_(extension) {}
2838
2839 private:
2840 const AstRawString* name_;
2841 v8::Extension* extension_;
2842};
2843
2844
2845class ThisFunction final : public Expression {
2846 public:
2847 DECLARE_NODE_TYPE(ThisFunction)
2848
2849 protected:
2850 ThisFunction(Zone* zone, int pos) : Expression(zone, pos) {}
2851};
2852
2853
2854class SuperPropertyReference final : public Expression {
2855 public:
2856 DECLARE_NODE_TYPE(SuperPropertyReference)
2857
2858 VariableProxy* this_var() const { return this_var_; }
2859 void set_this_var(VariableProxy* v) { this_var_ = v; }
2860 Expression* home_object() const { return home_object_; }
2861 void set_home_object(Expression* e) { home_object_ = e; }
2862
2863 protected:
2864 SuperPropertyReference(Zone* zone, VariableProxy* this_var,
2865 Expression* home_object, int pos)
2866 : Expression(zone, pos), this_var_(this_var), home_object_(home_object) {
2867 DCHECK(this_var->is_this());
2868 DCHECK(home_object->IsProperty());
2869 }
2870
2871 private:
2872 VariableProxy* this_var_;
2873 Expression* home_object_;
2874};
2875
2876
2877class SuperCallReference final : public Expression {
2878 public:
2879 DECLARE_NODE_TYPE(SuperCallReference)
2880
2881 VariableProxy* this_var() const { return this_var_; }
2882 void set_this_var(VariableProxy* v) { this_var_ = v; }
2883 VariableProxy* new_target_var() const { return new_target_var_; }
2884 void set_new_target_var(VariableProxy* v) { new_target_var_ = v; }
2885 VariableProxy* this_function_var() const { return this_function_var_; }
2886 void set_this_function_var(VariableProxy* v) { this_function_var_ = v; }
2887
2888 protected:
2889 SuperCallReference(Zone* zone, VariableProxy* this_var,
2890 VariableProxy* new_target_var,
2891 VariableProxy* this_function_var, int pos)
2892 : Expression(zone, pos),
2893 this_var_(this_var),
2894 new_target_var_(new_target_var),
2895 this_function_var_(this_function_var) {
2896 DCHECK(this_var->is_this());
2897 DCHECK(new_target_var->raw_name()->IsOneByteEqualTo(".new.target"));
2898 DCHECK(this_function_var->raw_name()->IsOneByteEqualTo(".this_function"));
2899 }
2900
2901 private:
2902 VariableProxy* this_var_;
2903 VariableProxy* new_target_var_;
2904 VariableProxy* this_function_var_;
2905};
2906
2907
2908// This class is produced when parsing the () in arrow functions without any
2909// arguments and is not actually a valid expression.
2910class EmptyParentheses final : public Expression {
2911 public:
2912 DECLARE_NODE_TYPE(EmptyParentheses)
2913
2914 private:
2915 EmptyParentheses(Zone* zone, int pos) : Expression(zone, pos) {}
2916};
2917
2918
2919#undef DECLARE_NODE_TYPE
2920
2921
2922// ----------------------------------------------------------------------------
2923// Basic visitor
2924// - leaf node visitors are abstract.
2925
2926class AstVisitor BASE_EMBEDDED {
2927 public:
2928 AstVisitor() {}
2929 virtual ~AstVisitor() {}
2930
2931 // Stack overflow check and dynamic dispatch.
2932 virtual void Visit(AstNode* node) = 0;
2933
2934 // Iteration left-to-right.
2935 virtual void VisitDeclarations(ZoneList<Declaration*>* declarations);
2936 virtual void VisitStatements(ZoneList<Statement*>* statements);
2937 virtual void VisitExpressions(ZoneList<Expression*>* expressions);
2938
2939 // Individual AST nodes.
2940#define DEF_VISIT(type) \
2941 virtual void Visit##type(type* node) = 0;
2942 AST_NODE_LIST(DEF_VISIT)
2943#undef DEF_VISIT
2944};
2945
2946#define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
2947 public: \
2948 void Visit(AstNode* node) final { \
2949 if (!CheckStackOverflow()) node->Accept(this); \
2950 } \
2951 \
2952 void SetStackOverflow() { stack_overflow_ = true; } \
2953 void ClearStackOverflow() { stack_overflow_ = false; } \
2954 bool HasStackOverflow() const { return stack_overflow_; } \
2955 \
2956 bool CheckStackOverflow() { \
2957 if (stack_overflow_) return true; \
2958 if (GetCurrentStackPosition() < stack_limit_) { \
2959 stack_overflow_ = true; \
2960 return true; \
2961 } \
2962 return false; \
2963 } \
2964 \
2965 private: \
2966 void InitializeAstVisitor(Isolate* isolate) { \
2967 stack_limit_ = isolate->stack_guard()->real_climit(); \
2968 stack_overflow_ = false; \
2969 } \
2970 \
2971 void InitializeAstVisitor(uintptr_t stack_limit) { \
2972 stack_limit_ = stack_limit; \
2973 stack_overflow_ = false; \
2974 } \
2975 \
2976 uintptr_t stack_limit_; \
2977 bool stack_overflow_
2978
2979#define DEFINE_AST_REWRITER_SUBCLASS_MEMBERS() \
2980 public: \
2981 AstNode* Rewrite(AstNode* node) { \
2982 DCHECK_NULL(replacement_); \
2983 DCHECK_NOT_NULL(node); \
2984 Visit(node); \
2985 if (HasStackOverflow()) return node; \
2986 if (replacement_ == nullptr) return node; \
2987 AstNode* result = replacement_; \
2988 replacement_ = nullptr; \
2989 return result; \
2990 } \
2991 \
2992 private: \
2993 void InitializeAstRewriter(Isolate* isolate) { \
2994 InitializeAstVisitor(isolate); \
2995 replacement_ = nullptr; \
2996 } \
2997 \
2998 void InitializeAstRewriter(uintptr_t stack_limit) { \
2999 InitializeAstVisitor(stack_limit); \
3000 replacement_ = nullptr; \
3001 } \
3002 \
3003 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); \
3004 \
3005 protected: \
3006 AstNode* replacement_
3007
3008// Generic macro for rewriting things; `GET` is the expression to be
3009// rewritten; `SET` is a command that should do the rewriting, i.e.
3010// something sensible with the variable called `replacement`.
3011#define AST_REWRITE(Type, GET, SET) \
3012 do { \
3013 DCHECK(!HasStackOverflow()); \
3014 DCHECK_NULL(replacement_); \
3015 Visit(GET); \
3016 if (HasStackOverflow()) return; \
3017 if (replacement_ == nullptr) break; \
3018 Type* replacement = reinterpret_cast<Type*>(replacement_); \
3019 do { \
3020 SET; \
3021 } while (false); \
3022 replacement_ = nullptr; \
3023 } while (false)
3024
3025// Macro for rewriting object properties; it assumes that `object` has
3026// `property` with a public getter and setter.
3027#define AST_REWRITE_PROPERTY(Type, object, property) \
3028 do { \
3029 auto _obj = (object); \
3030 AST_REWRITE(Type, _obj->property(), _obj->set_##property(replacement)); \
3031 } while (false)
3032
3033// Macro for rewriting list elements; it assumes that `list` has methods
3034// `at` and `Set`.
3035#define AST_REWRITE_LIST_ELEMENT(Type, list, index) \
3036 do { \
3037 auto _list = (list); \
3038 auto _index = (index); \
3039 AST_REWRITE(Type, _list->at(_index), _list->Set(_index, replacement)); \
3040 } while (false)
3041
3042
3043// ----------------------------------------------------------------------------
3044// AstNode factory
3045
3046class AstNodeFactory final BASE_EMBEDDED {
3047 public:
3048 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3049 : local_zone_(ast_value_factory->zone()),
3050 parser_zone_(ast_value_factory->zone()),
3051 ast_value_factory_(ast_value_factory) {}
3052
3053 AstValueFactory* ast_value_factory() const { return ast_value_factory_; }
3054
Ben Murdoch097c5b22016-05-18 11:27:45 +01003055 VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy,
3056 VariableMode mode, Scope* scope,
3057 int pos) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003058 return new (parser_zone_)
Ben Murdoch097c5b22016-05-18 11:27:45 +01003059 VariableDeclaration(parser_zone_, proxy, mode, scope, pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003060 }
3061
3062 FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
3063 VariableMode mode,
3064 FunctionLiteral* fun,
3065 Scope* scope,
3066 int pos) {
3067 return new (parser_zone_)
3068 FunctionDeclaration(parser_zone_, proxy, mode, fun, scope, pos);
3069 }
3070
3071 ImportDeclaration* NewImportDeclaration(VariableProxy* proxy,
3072 const AstRawString* import_name,
3073 const AstRawString* module_specifier,
3074 Scope* scope, int pos) {
3075 return new (parser_zone_) ImportDeclaration(
3076 parser_zone_, proxy, import_name, module_specifier, scope, pos);
3077 }
3078
3079 ExportDeclaration* NewExportDeclaration(VariableProxy* proxy,
3080 Scope* scope,
3081 int pos) {
3082 return new (parser_zone_)
3083 ExportDeclaration(parser_zone_, proxy, scope, pos);
3084 }
3085
3086 Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
3087 bool ignore_completion_value, int pos) {
3088 return new (local_zone_)
3089 Block(local_zone_, labels, capacity, ignore_completion_value, pos);
3090 }
3091
3092#define STATEMENT_WITH_LABELS(NodeType) \
3093 NodeType* New##NodeType(ZoneList<const AstRawString*>* labels, int pos) { \
3094 return new (local_zone_) NodeType(local_zone_, labels, pos); \
3095 }
3096 STATEMENT_WITH_LABELS(DoWhileStatement)
3097 STATEMENT_WITH_LABELS(WhileStatement)
3098 STATEMENT_WITH_LABELS(ForStatement)
3099 STATEMENT_WITH_LABELS(SwitchStatement)
3100#undef STATEMENT_WITH_LABELS
3101
3102 ForEachStatement* NewForEachStatement(ForEachStatement::VisitMode visit_mode,
3103 ZoneList<const AstRawString*>* labels,
3104 int pos) {
3105 switch (visit_mode) {
3106 case ForEachStatement::ENUMERATE: {
3107 return new (local_zone_) ForInStatement(local_zone_, labels, pos);
3108 }
3109 case ForEachStatement::ITERATE: {
3110 return new (local_zone_) ForOfStatement(local_zone_, labels, pos);
3111 }
3112 }
3113 UNREACHABLE();
3114 return NULL;
3115 }
3116
3117 ExpressionStatement* NewExpressionStatement(Expression* expression, int pos) {
3118 return new (local_zone_) ExpressionStatement(local_zone_, expression, pos);
3119 }
3120
3121 ContinueStatement* NewContinueStatement(IterationStatement* target, int pos) {
3122 return new (local_zone_) ContinueStatement(local_zone_, target, pos);
3123 }
3124
3125 BreakStatement* NewBreakStatement(BreakableStatement* target, int pos) {
3126 return new (local_zone_) BreakStatement(local_zone_, target, pos);
3127 }
3128
3129 ReturnStatement* NewReturnStatement(Expression* expression, int pos) {
3130 return new (local_zone_) ReturnStatement(local_zone_, expression, pos);
3131 }
3132
3133 WithStatement* NewWithStatement(Scope* scope,
3134 Expression* expression,
3135 Statement* statement,
3136 int pos) {
3137 return new (local_zone_)
3138 WithStatement(local_zone_, scope, expression, statement, pos);
3139 }
3140
3141 IfStatement* NewIfStatement(Expression* condition,
3142 Statement* then_statement,
3143 Statement* else_statement,
3144 int pos) {
3145 return new (local_zone_) IfStatement(local_zone_, condition, then_statement,
3146 else_statement, pos);
3147 }
3148
3149 TryCatchStatement* NewTryCatchStatement(Block* try_block, Scope* scope,
3150 Variable* variable,
3151 Block* catch_block, int pos) {
Ben Murdochda12d292016-06-02 14:46:10 +01003152 return new (local_zone_) TryCatchStatement(
3153 local_zone_, try_block, scope, variable, catch_block, true, pos);
3154 }
3155
3156 TryCatchStatement* NewTryCatchStatementForReThrow(Block* try_block,
3157 Scope* scope,
3158 Variable* variable,
3159 Block* catch_block,
3160 int pos) {
3161 return new (local_zone_) TryCatchStatement(
3162 local_zone_, try_block, scope, variable, catch_block, false, pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003163 }
3164
3165 TryFinallyStatement* NewTryFinallyStatement(Block* try_block,
3166 Block* finally_block, int pos) {
3167 return new (local_zone_)
3168 TryFinallyStatement(local_zone_, try_block, finally_block, pos);
3169 }
3170
3171 DebuggerStatement* NewDebuggerStatement(int pos) {
3172 return new (local_zone_) DebuggerStatement(local_zone_, pos);
3173 }
3174
3175 EmptyStatement* NewEmptyStatement(int pos) {
3176 return new (local_zone_) EmptyStatement(local_zone_, pos);
3177 }
3178
3179 SloppyBlockFunctionStatement* NewSloppyBlockFunctionStatement(
3180 Statement* statement, Scope* scope) {
3181 return new (parser_zone_)
3182 SloppyBlockFunctionStatement(parser_zone_, statement, scope);
3183 }
3184
3185 CaseClause* NewCaseClause(
3186 Expression* label, ZoneList<Statement*>* statements, int pos) {
3187 return new (local_zone_) CaseClause(local_zone_, label, statements, pos);
3188 }
3189
3190 Literal* NewStringLiteral(const AstRawString* string, int pos) {
3191 return new (local_zone_)
3192 Literal(local_zone_, ast_value_factory_->NewString(string), pos);
3193 }
3194
3195 // A JavaScript symbol (ECMA-262 edition 6).
3196 Literal* NewSymbolLiteral(const char* name, int pos) {
3197 return new (local_zone_)
3198 Literal(local_zone_, ast_value_factory_->NewSymbol(name), pos);
3199 }
3200
3201 Literal* NewNumberLiteral(double number, int pos, bool with_dot = false) {
3202 return new (local_zone_) Literal(
3203 local_zone_, ast_value_factory_->NewNumber(number, with_dot), pos);
3204 }
3205
3206 Literal* NewSmiLiteral(int number, int pos) {
3207 return new (local_zone_)
3208 Literal(local_zone_, ast_value_factory_->NewSmi(number), pos);
3209 }
3210
3211 Literal* NewBooleanLiteral(bool b, int pos) {
3212 return new (local_zone_)
3213 Literal(local_zone_, ast_value_factory_->NewBoolean(b), pos);
3214 }
3215
3216 Literal* NewNullLiteral(int pos) {
3217 return new (local_zone_)
3218 Literal(local_zone_, ast_value_factory_->NewNull(), pos);
3219 }
3220
3221 Literal* NewUndefinedLiteral(int pos) {
3222 return new (local_zone_)
3223 Literal(local_zone_, ast_value_factory_->NewUndefined(), pos);
3224 }
3225
3226 Literal* NewTheHoleLiteral(int pos) {
3227 return new (local_zone_)
3228 Literal(local_zone_, ast_value_factory_->NewTheHole(), pos);
3229 }
3230
3231 ObjectLiteral* NewObjectLiteral(
3232 ZoneList<ObjectLiteral::Property*>* properties,
3233 int literal_index,
3234 int boilerplate_properties,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003235 int pos) {
Ben Murdochda12d292016-06-02 14:46:10 +01003236 return new (local_zone_) ObjectLiteral(
3237 local_zone_, properties, literal_index, boilerplate_properties, pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003238 }
3239
3240 ObjectLiteral::Property* NewObjectLiteralProperty(
3241 Expression* key, Expression* value, ObjectLiteralProperty::Kind kind,
3242 bool is_static, bool is_computed_name) {
3243 return new (local_zone_)
3244 ObjectLiteral::Property(key, value, kind, is_static, is_computed_name);
3245 }
3246
3247 ObjectLiteral::Property* NewObjectLiteralProperty(Expression* key,
3248 Expression* value,
3249 bool is_static,
3250 bool is_computed_name) {
3251 return new (local_zone_) ObjectLiteral::Property(
3252 ast_value_factory_, key, value, is_static, is_computed_name);
3253 }
3254
3255 RegExpLiteral* NewRegExpLiteral(const AstRawString* pattern, int flags,
Ben Murdochda12d292016-06-02 14:46:10 +01003256 int literal_index, int pos) {
3257 return new (local_zone_)
3258 RegExpLiteral(local_zone_, pattern, flags, literal_index, pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003259 }
3260
3261 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3262 int literal_index,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003263 int pos) {
3264 return new (local_zone_)
Ben Murdochda12d292016-06-02 14:46:10 +01003265 ArrayLiteral(local_zone_, values, -1, literal_index, pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003266 }
3267
3268 ArrayLiteral* NewArrayLiteral(ZoneList<Expression*>* values,
3269 int first_spread_index, int literal_index,
Ben Murdochda12d292016-06-02 14:46:10 +01003270 int pos) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003271 return new (local_zone_) ArrayLiteral(
Ben Murdochda12d292016-06-02 14:46:10 +01003272 local_zone_, values, first_spread_index, literal_index, pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003273 }
3274
3275 VariableProxy* NewVariableProxy(Variable* var,
3276 int start_position = RelocInfo::kNoPosition,
3277 int end_position = RelocInfo::kNoPosition) {
3278 return new (parser_zone_)
3279 VariableProxy(parser_zone_, var, start_position, end_position);
3280 }
3281
3282 VariableProxy* NewVariableProxy(const AstRawString* name,
3283 Variable::Kind variable_kind,
3284 int start_position = RelocInfo::kNoPosition,
3285 int end_position = RelocInfo::kNoPosition) {
3286 DCHECK_NOT_NULL(name);
3287 return new (parser_zone_) VariableProxy(parser_zone_, name, variable_kind,
3288 start_position, end_position);
3289 }
3290
3291 Property* NewProperty(Expression* obj, Expression* key, int pos) {
3292 return new (local_zone_) Property(local_zone_, obj, key, pos);
3293 }
3294
3295 Call* NewCall(Expression* expression,
3296 ZoneList<Expression*>* arguments,
3297 int pos) {
3298 return new (local_zone_) Call(local_zone_, expression, arguments, pos);
3299 }
3300
3301 CallNew* NewCallNew(Expression* expression,
3302 ZoneList<Expression*>* arguments,
3303 int pos) {
3304 return new (local_zone_) CallNew(local_zone_, expression, arguments, pos);
3305 }
3306
3307 CallRuntime* NewCallRuntime(Runtime::FunctionId id,
3308 ZoneList<Expression*>* arguments, int pos) {
3309 return new (local_zone_)
3310 CallRuntime(local_zone_, Runtime::FunctionForId(id), arguments, pos);
3311 }
3312
3313 CallRuntime* NewCallRuntime(const Runtime::Function* function,
3314 ZoneList<Expression*>* arguments, int pos) {
3315 return new (local_zone_) CallRuntime(local_zone_, function, arguments, pos);
3316 }
3317
3318 CallRuntime* NewCallRuntime(int context_index,
3319 ZoneList<Expression*>* arguments, int pos) {
3320 return new (local_zone_)
3321 CallRuntime(local_zone_, context_index, arguments, pos);
3322 }
3323
3324 UnaryOperation* NewUnaryOperation(Token::Value op,
3325 Expression* expression,
3326 int pos) {
3327 return new (local_zone_) UnaryOperation(local_zone_, op, expression, pos);
3328 }
3329
3330 BinaryOperation* NewBinaryOperation(Token::Value op,
3331 Expression* left,
3332 Expression* right,
3333 int pos) {
3334 return new (local_zone_) BinaryOperation(local_zone_, op, left, right, pos);
3335 }
3336
3337 CountOperation* NewCountOperation(Token::Value op,
3338 bool is_prefix,
3339 Expression* expr,
3340 int pos) {
3341 return new (local_zone_)
3342 CountOperation(local_zone_, op, is_prefix, expr, pos);
3343 }
3344
3345 CompareOperation* NewCompareOperation(Token::Value op,
3346 Expression* left,
3347 Expression* right,
3348 int pos) {
3349 return new (local_zone_)
3350 CompareOperation(local_zone_, op, left, right, pos);
3351 }
3352
Ben Murdoch097c5b22016-05-18 11:27:45 +01003353 Spread* NewSpread(Expression* expression, int pos, int expr_pos) {
3354 return new (local_zone_) Spread(local_zone_, expression, pos, expr_pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003355 }
3356
3357 Conditional* NewConditional(Expression* condition,
3358 Expression* then_expression,
3359 Expression* else_expression,
3360 int position) {
3361 return new (local_zone_) Conditional(
3362 local_zone_, condition, then_expression, else_expression, position);
3363 }
3364
Ben Murdoch097c5b22016-05-18 11:27:45 +01003365 RewritableExpression* NewRewritableExpression(Expression* expression) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003366 DCHECK_NOT_NULL(expression);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003367 return new (local_zone_) RewritableExpression(local_zone_, expression);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003368 }
3369
3370 Assignment* NewAssignment(Token::Value op,
3371 Expression* target,
3372 Expression* value,
3373 int pos) {
3374 DCHECK(Token::IsAssignmentOp(op));
3375 Assignment* assign =
3376 new (local_zone_) Assignment(local_zone_, op, target, value, pos);
3377 if (assign->is_compound()) {
3378 DCHECK(Token::IsAssignmentOp(op));
3379 assign->binary_operation_ =
3380 NewBinaryOperation(assign->binary_op(), target, value, pos + 1);
3381 }
3382 return assign;
3383 }
3384
3385 Yield* NewYield(Expression *generator_object,
3386 Expression* expression,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003387 int pos) {
3388 if (!expression) expression = NewUndefinedLiteral(pos);
3389 return new (local_zone_)
Ben Murdochda12d292016-06-02 14:46:10 +01003390 Yield(local_zone_, generator_object, expression, pos);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003391 }
3392
3393 Throw* NewThrow(Expression* exception, int pos) {
3394 return new (local_zone_) Throw(local_zone_, exception, pos);
3395 }
3396
3397 FunctionLiteral* NewFunctionLiteral(
3398 const AstRawString* name, Scope* scope, ZoneList<Statement*>* body,
3399 int materialized_literal_count, int expected_property_count,
3400 int parameter_count,
3401 FunctionLiteral::ParameterFlag has_duplicate_parameters,
3402 FunctionLiteral::FunctionType function_type,
3403 FunctionLiteral::EagerCompileHint eager_compile_hint, FunctionKind kind,
3404 int position) {
3405 return new (parser_zone_) FunctionLiteral(
3406 parser_zone_, name, ast_value_factory_, scope, body,
3407 materialized_literal_count, expected_property_count, parameter_count,
3408 function_type, has_duplicate_parameters, eager_compile_hint, kind,
Ben Murdoch097c5b22016-05-18 11:27:45 +01003409 position, true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003410 }
3411
Ben Murdoch097c5b22016-05-18 11:27:45 +01003412 // Creates a FunctionLiteral representing a top-level script, the
3413 // result of an eval (top-level or otherwise), or the result of calling
3414 // the Function constructor.
3415 FunctionLiteral* NewScriptOrEvalFunctionLiteral(
3416 Scope* scope, ZoneList<Statement*>* body, int materialized_literal_count,
3417 int expected_property_count) {
3418 return new (parser_zone_) FunctionLiteral(
3419 parser_zone_, ast_value_factory_->empty_string(), ast_value_factory_,
3420 scope, body, materialized_literal_count, expected_property_count, 0,
3421 FunctionLiteral::kAnonymousExpression,
3422 FunctionLiteral::kNoDuplicateParameters,
3423 FunctionLiteral::kShouldLazyCompile, FunctionKind::kNormalFunction, 0,
3424 false);
3425 }
3426
3427 ClassLiteral* NewClassLiteral(Scope* scope, VariableProxy* proxy,
3428 Expression* extends,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003429 FunctionLiteral* constructor,
3430 ZoneList<ObjectLiteral::Property*>* properties,
3431 int start_position, int end_position) {
3432 return new (parser_zone_)
Ben Murdoch097c5b22016-05-18 11:27:45 +01003433 ClassLiteral(parser_zone_, scope, proxy, extends, constructor,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003434 properties, start_position, end_position);
3435 }
3436
3437 NativeFunctionLiteral* NewNativeFunctionLiteral(const AstRawString* name,
3438 v8::Extension* extension,
3439 int pos) {
3440 return new (parser_zone_)
3441 NativeFunctionLiteral(parser_zone_, name, extension, pos);
3442 }
3443
3444 DoExpression* NewDoExpression(Block* block, Variable* result_var, int pos) {
3445 VariableProxy* result = NewVariableProxy(result_var, pos);
3446 return new (parser_zone_) DoExpression(parser_zone_, block, result, pos);
3447 }
3448
3449 ThisFunction* NewThisFunction(int pos) {
3450 return new (local_zone_) ThisFunction(local_zone_, pos);
3451 }
3452
3453 SuperPropertyReference* NewSuperPropertyReference(VariableProxy* this_var,
3454 Expression* home_object,
3455 int pos) {
3456 return new (parser_zone_)
3457 SuperPropertyReference(parser_zone_, this_var, home_object, pos);
3458 }
3459
3460 SuperCallReference* NewSuperCallReference(VariableProxy* this_var,
3461 VariableProxy* new_target_var,
3462 VariableProxy* this_function_var,
3463 int pos) {
3464 return new (parser_zone_) SuperCallReference(
3465 parser_zone_, this_var, new_target_var, this_function_var, pos);
3466 }
3467
3468 EmptyParentheses* NewEmptyParentheses(int pos) {
3469 return new (local_zone_) EmptyParentheses(local_zone_, pos);
3470 }
3471
3472 Zone* zone() const { return local_zone_; }
3473
3474 // Handles use of temporary zones when parsing inner function bodies.
3475 class BodyScope {
3476 public:
3477 BodyScope(AstNodeFactory* factory, Zone* temp_zone, bool use_temp_zone)
3478 : factory_(factory), prev_zone_(factory->local_zone_) {
3479 if (use_temp_zone) {
3480 factory->local_zone_ = temp_zone;
3481 }
3482 }
3483
3484 ~BodyScope() { factory_->local_zone_ = prev_zone_; }
3485
3486 private:
3487 AstNodeFactory* factory_;
3488 Zone* prev_zone_;
3489 };
3490
3491 private:
3492 // This zone may be deallocated upon returning from parsing a function body
3493 // which we can guarantee is not going to be compiled or have its AST
3494 // inspected.
3495 // See ParseFunctionLiteral in parser.cc for preconditions.
3496 Zone* local_zone_;
3497 // ZoneObjects which need to persist until scope analysis must be allocated in
3498 // the parser-level zone.
3499 Zone* parser_zone_;
3500 AstValueFactory* ast_value_factory_;
3501};
3502
3503
Ben Murdoch097c5b22016-05-18 11:27:45 +01003504// Type testing & conversion functions overridden by concrete subclasses.
3505// Inline functions for AstNode.
3506
3507#define DECLARE_NODE_FUNCTIONS(type) \
3508 bool AstNode::Is##type() const { \
3509 NodeType mine = node_type(); \
3510 if (mine == AstNode::kRewritableExpression && \
3511 AstNode::k##type != AstNode::kRewritableExpression) \
3512 mine = reinterpret_cast<const RewritableExpression*>(this) \
3513 ->expression() \
3514 ->node_type(); \
3515 return mine == AstNode::k##type; \
3516 } \
3517 type* AstNode::As##type() { \
3518 NodeType mine = node_type(); \
3519 AstNode* result = this; \
3520 if (mine == AstNode::kRewritableExpression && \
3521 AstNode::k##type != AstNode::kRewritableExpression) { \
3522 result = \
3523 reinterpret_cast<const RewritableExpression*>(this)->expression(); \
3524 mine = result->node_type(); \
3525 } \
3526 return mine == AstNode::k##type ? reinterpret_cast<type*>(result) : NULL; \
3527 } \
3528 const type* AstNode::As##type() const { \
3529 NodeType mine = node_type(); \
3530 const AstNode* result = this; \
3531 if (mine == AstNode::kRewritableExpression && \
3532 AstNode::k##type != AstNode::kRewritableExpression) { \
3533 result = \
3534 reinterpret_cast<const RewritableExpression*>(this)->expression(); \
3535 mine = result->node_type(); \
3536 } \
3537 return mine == AstNode::k##type ? reinterpret_cast<const type*>(result) \
3538 : NULL; \
3539 }
3540AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
3541#undef DECLARE_NODE_FUNCTIONS
3542
3543
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003544} // namespace internal
3545} // namespace v8
3546
3547#endif // V8_AST_AST_H_