summaryrefslogtreecommitdiff
path: root/js/src/frontend/ParseNode.cpp
blob: 7ad470865fdf58f030e834384a599f8514fbd7ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "frontend/ParseNode-inl.h"

#include "frontend/Parser.h"

#include "jscntxtinlines.h"

using namespace js;
using namespace js::frontend;

using mozilla::ArrayLength;
using mozilla::IsFinite;

#ifdef DEBUG
void
ListNode::checkConsistency() const
{
    ParseNode* const* tailNode;
    uint32_t actualCount = 0;
    if (const ParseNode* last = head()) {
        const ParseNode* pn = last;
        while (pn) {
            last = pn;
            pn = pn->pn_next;
            actualCount++;
        }

        tailNode = &last->pn_next;
    } else {
        tailNode = &pn_u.list.head;
    }
    MOZ_ASSERT(tail() == tailNode);
    MOZ_ASSERT(count() == actualCount);
}
#endif

/* Add |node| to |parser|'s free node list. */
void
ParseNodeAllocator::freeNode(ParseNode* pn)
{
    /* Catch back-to-back dup recycles. */
    MOZ_ASSERT(pn != freelist);

#ifdef DEBUG
    /* Poison the node, to catch attempts to use it without initializing it. */
    memset(pn, 0xab, sizeof(*pn));
#endif

    pn->pn_next = freelist;
    freelist = pn;
}

namespace {

/*
 * A work pool of ParseNodes. The work pool is a stack, chained together
 * by nodes' pn_next fields. We use this to avoid creating deep C++ stacks
 * when recycling deep parse trees.
 *
 * Since parse nodes are probably allocated in something close to the order
 * they appear in a depth-first traversal of the tree, making the work pool
 * a stack should give us pretty good locality.
 */
class NodeStack {
  public:
    NodeStack() : top(nullptr) { }
    bool empty() { return top == nullptr; }
    void push(ParseNode* pn) {
        pn->pn_next = top;
        top = pn;
    }
    /* Push the children of the PN_LIST node |pn| on the stack. */
    void pushList(ListNode* pn) {
        /* This clobbers pn->pn_head if the list is empty; should be okay. */
        pn->unsafeSetTail(top);
        top = pn->head();
    }
    ParseNode* pop() {
        MOZ_ASSERT(!empty());
        ParseNode* hold = top; /* my kingdom for a prog1 */
        top = top->pn_next;
        return hold;
    }
  private:
    ParseNode* top;
};

} /* anonymous namespace */

enum class PushResult { Recyclable, CleanUpLater };

static PushResult
PushFunctionNodeChildren(FunctionNode* node, NodeStack* stack)
{
    /*
     * Function nodes are linked into the function box tree, and may appear
     * on method lists. Both of those lists are singly-linked, so trying to
     * update them now could result in quadratic behavior when recycling
     * trees containing many functions; and the lists can be very long. So
     * we put off cleaning the lists up until just before function
     * analysis, when we call CleanFunctionList.
     *
     * In fact, we can't recycle the parse node yet, either: it may appear
     * on a method list, and reusing the node would corrupt that. Instead,
     * we clear its funbox pointer to mark it as deleted;
     * CleanFunctionList recycles it as well.
     *
     * We do recycle the nodes around it, though, so we must clear pointers
     * to them to avoid leaving dangling references where someone can find
     * them.
     */
    node->setFunbox(nullptr);
    if (node->body())
        stack->push(node->body());
    node->setBody(nullptr);

    return PushResult::CleanUpLater;
}

static PushResult
PushModuleNodeChildren(ModuleNode* node, NodeStack* stack)
{
    stack->push(node->body());
    node->setBody(nullptr);

    return PushResult::CleanUpLater;
}

static PushResult
PushNameNodeChildren(NameNode* node, NodeStack* stack)
{
    if (node->initializer())
        stack->push(node->initializer());
    node->setInitializer(nullptr);
    return PushResult::Recyclable;
}

static PushResult
PushScopeNodeChildren(LexicalScopeNode* node, NodeStack* stack)
{
    if (node->scopeBody())
        stack->push(node->scopeBody());
    node->setScopeBody(nullptr);
    return PushResult::Recyclable;
}

static PushResult
PushListNodeChildren(ListNode* node, NodeStack* stack)
{
    node->checkConsistency();

    stack->pushList(node);

    return PushResult::Recyclable;
}

static PushResult
PushUnaryNodeChild(UnaryNode* node, NodeStack* stack)
{
    stack->push(node->kid());

    return PushResult::Recyclable;
}

/*
 * Push the children of |pn| on |stack|. Return true if |pn| itself could be
 * safely recycled, or false if it must be cleaned later (pn_used and pn_defn
 * nodes, and all function nodes; see comments for CleanFunctionList in
 * SemanticAnalysis.cpp). Some callers want to free |pn|; others
 * (js::ParseNodeAllocator::prepareNodeForMutation) don't care about |pn|, and
 * just need to take care of its children.
 */
static PushResult
PushNodeChildren(ParseNode* pn, NodeStack* stack)
{
    switch (pn->getKind()) {
      // Trivial nodes that refer to no nodes, are referred to by nothing
      // but their parents, are never used, and are never a definition.
      case PNK_NOP:
      case PNK_TRUE:
      case PNK_FALSE:
      case PNK_NULL:
      case PNK_RAW_UNDEFINED:
      case PNK_ELISION:
      case PNK_GENERATOR:
      case PNK_EXPORT_BATCH_SPEC:
      case PNK_POSHOLDER:
        MOZ_ASSERT(pn->is<NullaryNode>());
        return PushResult::Recyclable;

      case PNK_DEBUGGER:
          MOZ_ASSERT(pn->is<DebuggerStatement>());
          return PushResult::Recyclable;

      case PNK_BREAK:
          MOZ_ASSERT(pn->is<BreakStatement>());
          return PushResult::Recyclable;

      case PNK_CONTINUE:
          MOZ_ASSERT(pn->is<ContinueStatement>());
          return PushResult::Recyclable;

      case PNK_OBJECT_PROPERTY_NAME:
      case PNK_STRING:
      case PNK_TEMPLATE_STRING:
        MOZ_ASSERT(pn->is<NameNode>());
        return PushResult::Recyclable;

      case PNK_REGEXP:
        MOZ_ASSERT(pn->is<RegExpLiteral>());
        return PushResult::Recyclable;

      case PNK_NUMBER:
        MOZ_ASSERT(pn->is<NumericLiteral>());
        return PushResult::Recyclable;

      // Nodes with a single non-null child.
      case PNK_TYPEOFNAME:
      case PNK_TYPEOFEXPR:
      case PNK_VOID:
      case PNK_NOT:
      case PNK_BITNOT:
      case PNK_THROW:
      case PNK_DELETENAME:
      case PNK_DELETEPROP:
      case PNK_DELETEELEM:
      case PNK_DELETEEXPR:
      case PNK_POS:
      case PNK_NEG:
      case PNK_PREINCREMENT:
      case PNK_POSTINCREMENT:
      case PNK_PREDECREMENT:
      case PNK_POSTDECREMENT:
      case PNK_COMPUTED_NAME:
      case PNK_STATICCLASSBLOCK:
      case PNK_ARRAYPUSH:
      case PNK_SPREAD:
      case PNK_MUTATEPROTO:
      case PNK_EXPORT:
      case PNK_SUPERBASE:
        return PushUnaryNodeChild(&pn->as<UnaryNode>(), stack);

      // Nodes with a single nullable child.
      case PNK_OPTCHAIN:
      case PNK_DELETEOPTCHAIN:
      case PNK_THIS:
      case PNK_SEMI: {
        UnaryNode* un = &pn->as<UnaryNode>();
        if (un->kid())
            stack->push(un->kid());
        return PushResult::Recyclable;
      }

      // Binary nodes with two non-null children.

      // All assignment and compound assignment nodes qualify.
      case PNK_INITPROP:
      case PNK_ASSIGN:
      case PNK_ADDASSIGN:
      case PNK_SUBASSIGN:
      case PNK_COALESCEASSIGN:
      case PNK_ORASSIGN:
      case PNK_ANDASSIGN:
      case PNK_BITORASSIGN:
      case PNK_BITXORASSIGN:
      case PNK_BITANDASSIGN:
      case PNK_LSHASSIGN:
      case PNK_RSHASSIGN:
      case PNK_URSHASSIGN:
      case PNK_MULASSIGN:
      case PNK_DIVASSIGN:
      case PNK_MODASSIGN:
      case PNK_POWASSIGN:
      // ...and a few others.
      case PNK_OPTELEM:
      case PNK_ELEM:
      case PNK_IMPORT_SPEC:
      case PNK_EXPORT_SPEC:
      case PNK_COLON:
      case PNK_SHORTHAND:
      case PNK_DOWHILE:
      case PNK_WHILE:
      case PNK_SWITCH:
      case PNK_NEW:
      case PNK_OPTDOT:
      case PNK_DOT:
      case PNK_OPTCALL:
      case PNK_CALL:
      case PNK_SUPERCALL:
      case PNK_TAGGED_TEMPLATE:
      case PNK_GENEXP:
      case PNK_CLASSMETHOD:
      case PNK_NEWTARGET:
      case PNK_SETTHIS:
      case PNK_FOR:
      case PNK_COMPREHENSIONFOR:
      case PNK_IMPORT_META:
      case PNK_CALL_IMPORT:
      case PNK_WITH: {
        BinaryNode* bn = &pn->as<BinaryNode>();
        stack->push(bn->left());
        stack->push(bn->right());
        return PushResult::Recyclable;
      }

      // Default clauses are PNK_CASE but do not have case expressions.
      // Named class expressions do not have outer binding nodes.
      // So both are binary nodes with a possibly-null pn_left.
      case PNK_CASE:
      case PNK_CLASSNAMES: {
        BinaryNode* bn = &pn->as<BinaryNode>();
        if (bn->left())
            stack->push(bn->left());
        stack->push(bn->right());
        return PushResult::Recyclable;
      }

      // The child is an assignment of a PNK_GENERATOR node to the
      // '.generator' local, for a synthesized, prepended initial yield.
      case PNK_INITIALYIELD: {
        UnaryNode* un = &pn->as<UnaryNode>();
#ifdef DEBUG
        MOZ_ASSERT(un->kid()->isKind(PNK_ASSIGN));
        BinaryNode* bn = &un->kid()->as<BinaryNode>();
        MOZ_ASSERT(bn->left()->isKind(PNK_NAME) &&
                   bn->right()->isKind(PNK_GENERATOR));
#endif
        stack->push(un->kid());
        return PushResult::Recyclable;
      }

      // The child is the expression being yielded.
      case PNK_YIELD_STAR:
      case PNK_YIELD:
      case PNK_AWAIT: {
        UnaryNode* un = &pn->as<UnaryNode>();
        if (un->kid())
            stack->push(un->kid());
        return PushResult::Recyclable;
      }

      // A return node's child is what you'd expect: the return expression,
      // if any.
      case PNK_RETURN: {
        UnaryNode* un = &pn->as<UnaryNode>();
        if (un->kid())
            stack->push(un->kid());
        return PushResult::Recyclable;
      }

      // Import and export-from nodes have a list of specifiers on the left
      // and a module string on the right.
      case PNK_IMPORT:
      case PNK_EXPORT_FROM: {
        BinaryNode* bn = &pn->as<BinaryNode>();
        MOZ_ASSERT_IF(pn->isKind(PNK_IMPORT), bn->left()->isKind(PNK_IMPORT_SPEC_LIST));
        MOZ_ASSERT_IF(pn->isKind(PNK_EXPORT_FROM), bn->left()->isKind(PNK_EXPORT_SPEC_LIST));
        MOZ_ASSERT(bn->left()->isArity(PN_LIST));
        MOZ_ASSERT(bn->right()->isKind(PNK_STRING));
        stack->pushList(&bn->left()->as<ListNode>());
        stack->push(bn->right());
        return PushResult::Recyclable;
      }

      case PNK_EXPORT_DEFAULT: {
        BinaryNode* bn = &pn->as<BinaryNode>();
        MOZ_ASSERT_IF(bn->right(), bn->right()->isKind(PNK_NAME));
        stack->push(bn->left());
        if (bn->right())
            stack->push(bn->right());
        return PushResult::Recyclable;
      }

      case PNK_CLASSFIELD: {
          BinaryNode* bn = &pn->as<BinaryNode>();
          stack->push(bn->left());
          if (bn->right())
              stack->push(bn->right());
          return PushResult::Recyclable;
      }

      // Ternary nodes with all children non-null.
      case PNK_CONDITIONAL: {
        TernaryNode* tn = &pn->as<TernaryNode>();
        stack->push(tn->kid1());
        stack->push(tn->kid2());
        stack->push(tn->kid3());
        return PushResult::Recyclable;
      }

      // For for-in and for-of, the first child is the left-hand side of the
      // 'in' or 'of' (a declaration or an assignment target). The second
      // child is always null, and the third child is the expression looped
      // over.  For example, in |for (var p in obj)|, the first child is |var
      // p|, the second child is null, and the third child is |obj|.
      case PNK_FORIN:
      case PNK_FOROF: {
        TernaryNode* tn = &pn->as<TernaryNode>();
        MOZ_ASSERT(!tn->kid2());
        stack->push(tn->kid1());
        stack->push(tn->kid3());
        return PushResult::Recyclable;
      }

      // for (;;) nodes have one child per optional component of the loop head.
      case PNK_FORHEAD: {
        TernaryNode* tn = &pn->as<TernaryNode>();
        if (tn->kid1())
            stack->push(tn->kid1());
        if (tn->kid2())
            stack->push(tn->kid2());
        if (tn->kid3())
            stack->push(tn->kid3());
        return PushResult::Recyclable;
      }

      // classes might have an optional node for the heritage, as well as the names
      case PNK_CLASS: {
        TernaryNode* tn = &pn->as<TernaryNode>();
        if (tn->kid1())
            stack->push(tn->kid1());
        if (tn->kid2())
            stack->push(tn->kid2());
        stack->push(tn->kid3());
        return PushResult::Recyclable;
      }

      // if-statement nodes have condition and consequent children and a
      // possibly-null alternative.
      case PNK_IF: {
        TernaryNode* tn = &pn->as<TernaryNode>();
        stack->push(tn->kid1());
        stack->push(tn->kid2());
        if (tn->kid3())
            stack->push(tn->kid3());
        return PushResult::Recyclable;
      }

      // try-statements have statements to execute, and one or both of a
      // catch-list and a finally-block.
      case PNK_TRY: {
        TernaryNode* tn = &pn->as<TernaryNode>();
        MOZ_ASSERT(tn->kid2() || tn->kid3());
        stack->push(tn->kid1());
        if (tn->kid2())
            stack->push(tn->kid2());
        if (tn->kid3())
            stack->push(tn->kid3());
        return PushResult::Recyclable;
      }

      // A catch node has an (optional) first kid as catch-variable pattern,
      // the second kid as (optional) catch condition (which, records the
      // |<cond>| in SpiderMonkey's |catch (e if <cond>)| extension), and
      // third kid as the statements in the catch block.
      case PNK_CATCH: {
        TernaryNode* tn = &pn->as<TernaryNode>();
        if (tn->kid1())
            stack->push(tn->kid1());
        if (tn->kid2())
            stack->push(tn->kid2());
        stack->push(tn->kid3());
        return PushResult::Recyclable;
      }

      // List nodes with all non-null children.
      case PNK_COALESCE:
      case PNK_OR:
      case PNK_AND:
      case PNK_BITOR:
      case PNK_BITXOR:
      case PNK_BITAND:
      case PNK_STRICTEQ:
      case PNK_EQ:
      case PNK_STRICTNE:
      case PNK_NE:
      case PNK_LT:
      case PNK_LE:
      case PNK_GT:
      case PNK_GE:
      case PNK_INSTANCEOF:
      case PNK_IN:
      case PNK_LSH:
      case PNK_RSH:
      case PNK_URSH:
      case PNK_ADD:
      case PNK_SUB:
      case PNK_STAR:
      case PNK_DIV:
      case PNK_MOD:
      case PNK_POW:
      case PNK_COMMA:
      case PNK_ARRAY:
      case PNK_OBJECT:
      case PNK_TEMPLATE_STRING_LIST:
      case PNK_CALLSITEOBJ:
      case PNK_VAR:
      case PNK_CONST:
      case PNK_LET:
      case PNK_ARGUMENTS:
      case PNK_CATCHLIST:
      case PNK_STATEMENTLIST:
      case PNK_IMPORT_SPEC_LIST:
      case PNK_EXPORT_SPEC_LIST:
      case PNK_PARAMSBODY:
      case PNK_CLASSMEMBERLIST:
        return PushListNodeChildren(&pn->as<ListNode>(), stack);

      // Array comprehension nodes are lists with a single child:
      // PNK_COMPREHENSIONFOR for comprehensions, PNK_LEXICALSCOPE for legacy
      // comprehensions.  Probably this should be a non-list eventually.
      case PNK_ARRAYCOMP: {
        ListNode* literal = &pn->as<ListNode>();
        MOZ_ASSERT(literal->count() == 1);
        MOZ_ASSERT(literal->head()->isKind(PNK_LEXICALSCOPE) ||
                   literal->head()->isKind(PNK_COMPREHENSIONFOR));
        return PushListNodeChildren(literal, stack);
      }

      case PNK_LABEL:
      case PNK_NAME:
      case PNK_PROPERTYNAME:
        return PushNameNodeChildren(&pn->as<NameNode>(), stack);

      case PNK_LEXICALSCOPE:
        return PushScopeNodeChildren(&pn->as<LexicalScopeNode>(), stack);

      case PNK_FUNCTION:
        return PushFunctionNodeChildren(&pn->as<FunctionNode>(), stack);
      case PNK_MODULE:
        return PushModuleNodeChildren(&pn->as<ModuleNode>(), stack);

      case PNK_LIMIT: // invalid sentinel value
        MOZ_CRASH("invalid node kind");
    }

    MOZ_CRASH("bad ParseNodeKind");
    return PushResult::CleanUpLater;
}

/*
 * Prepare |pn| to be mutated in place into a new kind of node. Recycle all
 * |pn|'s recyclable children (but not |pn| itself!), and disconnect it from
 * metadata structures (the function box tree).
 */
void
ParseNodeAllocator::prepareNodeForMutation(ParseNode* pn)
{
    // Nothing to do for nullary nodes.
    if (pn->isArity(PN_NULLARY))
        return;

    // Put |pn|'s children (but not |pn| itself) on a work stack.
    NodeStack stack;
    PushNodeChildren(pn, &stack);

    // For each node on the work stack, push its children on the work stack,
    // and free the node if we can.
    while (!stack.empty()) {
        pn = stack.pop();
        if (PushNodeChildren(pn, &stack) == PushResult::Recyclable)
            freeNode(pn);
    }
}

/*
 * Return the nodes in the subtree |pn| to the parser's free node list, for
 * reallocation.
 */
ParseNode*
ParseNodeAllocator::freeTree(ParseNode* pn)
{
    if (!pn)
        return nullptr;

    ParseNode* savedNext = pn->pn_next;

    NodeStack stack;
    for (;;) {
        if (PushNodeChildren(pn, &stack) == PushResult::Recyclable)
            freeNode(pn);
        if (stack.empty())
            break;
        pn = stack.pop();
    }

    return savedNext;
}

/*
 * Allocate a ParseNode from parser's node freelist or, failing that, from
 * cx's temporary arena.
 */
void*
ParseNodeAllocator::allocNode()
{
    if (ParseNode* pn = freelist) {
        freelist = pn->pn_next;
        return pn;
    }

    LifoAlloc::AutoFallibleScope fallibleAllocator(&alloc);
    void* p = alloc.alloc(sizeof (ParseNode));
    if (!p)
        ReportOutOfMemory(cx);
    return p;
}

ParseNode*
ParseNode::appendOrCreateList(ParseNodeKind kind, JSOp op, ParseNode* left, ParseNode* right,
                              FullParseHandler* handler, ParseContext* pc)
{
    // The asm.js specification is written in ECMAScript grammar terms that
    // specify *only* a binary tree.  It's a royal pain to implement the asm.js
    // spec to act upon n-ary lists as created below.  So for asm.js, form a
    // binary tree of lists exactly as ECMAScript would by skipping the
    // following optimization.
    if (!pc->useAsmOrInsideUseAsm()) {
        // Left-associative trees of a given operator (e.g. |a + b + c|) are
        // binary trees in the spec: (+ (+ a b) c) in Lisp terms.  Recursively
        // processing such a tree, exactly implemented that way, would blow the
        // the stack.  We use a list node that uses O(1) stack to represent
        // such operations: (+ a b c).
        //
        // (**) is right-associative; per spec |a ** b ** c| parses as
        // (** a (** b c)). But we treat this the same way, creating a list
        // node: (** a b c). All consumers must understand that this must be
        // processed with a right fold, whereas the list (+ a b c) must be
        // processed with a left fold because (+) is left-associative.
        //
        if (left->isKind(kind) &&
            left->isOp(op) &&
            (CodeSpec[op].format & JOF_LEFTASSOC ||
             (kind == PNK_POW && !left->pn_parens)))
        {
            ListNode* list = &left->as<ListNode>();

            list->append(right);
            list->pn_pos.end = right->pn_pos.end;

            return list;
        }
    }

    ListNode* list = handler->new_<ListNode>(kind, op, left);
    if (!list)
        return nullptr;

    list->append(right);
    return list;
}

#ifdef DEBUG

static const char * const parseNodeNames[] = {
#define STRINGIFY(name) #name,
    FOR_EACH_PARSE_NODE_KIND(STRINGIFY)
#undef STRINGIFY
};

void
frontend::DumpParseTree(ParseNode* pn, int indent)
{
    if (pn == nullptr)
        fprintf(stderr, "#NULL");
    else
        pn->dump(indent);
}

static void
IndentNewLine(int indent)
{
    fputc('\n', stderr);
    for (int i = 0; i < indent; ++i)
        fputc(' ', stderr);
}

void
ParseNode::dump()
{
    dump(0);
    fputc('\n', stderr);
}

void
ParseNode::dump(int indent)
{
    switch (pn_arity) {
      case PN_NULLARY:
        as<NullaryNode>().dump();
        return;
      case PN_UNARY:
        as<UnaryNode>().dump(indent);
        return;
      case PN_BINARY:
        as<BinaryNode>().dump(indent);
        return;
      case PN_TERNARY:
        as<TernaryNode>().dump(indent);
        return;
      case PN_FUNCTION:
        as<FunctionNode>().dump(indent);
        return;
      case PN_MODULE:
        as<ModuleNode>().dump(indent);
      case PN_LIST:
        as<ListNode>().dump(indent);
        return;
      case PN_NAME:
        as<NameNode>().dump(indent);
        return;
      case PN_NUMBER:
        as<NumericLiteral>().dump(indent);
        return;
      case PN_REGEXP:
        as<RegExpLiteral>().dump(indent);
        return;
      case PN_LOOP:
        as<LoopControlStatement>().dump(indent);
        return;
      case PN_SCOPE:
        as<LexicalScopeNode>().dump(indent);
        return;
    }
    fprintf(stderr, "#<BAD NODE %p, kind=%u, arity=%u>",
            (void*) this, unsigned(getKind()), unsigned(pn_arity));
}

void
NullaryNode::dump()
{
    switch (getKind()) {
      case PNK_TRUE:  fprintf(stderr, "#true");  break;
      case PNK_FALSE: fprintf(stderr, "#false"); break;
      case PNK_NULL:  fprintf(stderr, "#null");  break;
      case PNK_RAW_UNDEFINED: fprintf(stderr, "#undefined"); break;

      default:
        fprintf(stderr, "(%s)", parseNodeNames[getKind()]);
    }
}

void
NumericLiteral::dump(int indent)
{
    ToCStringBuf cbuf;
    const char* cstr = NumberToCString(nullptr, &cbuf, value());
    if (!IsFinite(value())) {
        fprintf(stderr, "#");
    }
    if (cstr) {
        fprintf(stderr, "%s", cstr);
    } else {
        fprintf(stderr, "%g", value());
    }
}

void
RegExpLiteral::dump(int indent)
{
    fprintf(stderr, "(%s)", parseNodeNames[size_t(getKind())]);
}

void
LoopControlStatement::dump(int indent)
{
    const char* name = parseNodeNames[size_t(getKind())];
    fprintf(stderr, "(%s", name);
    if (label()) {
        fprintf(stderr, " ");
        label()->dumpCharsNoNewline();
    }
    fprintf(stderr, ")");
}

void
UnaryNode::dump(int indent)
{
    const char* name = parseNodeNames[getKind()];
    fprintf(stderr, "(%s ", name);
    indent += strlen(name) + 2;
    DumpParseTree(kid(), indent);
    fprintf(stderr, ")");
}

void
BinaryNode::dump(int indent)
{
    if (isKind(PNK_DOT)) {
        fprintf(stderr, "(.");

        DumpParseTree(right(), indent + 2);

        fprintf(stderr, " ");
        if (as<PropertyAccess>().isSuper())
            fprintf(stderr, "super");
        else
            DumpParseTree(left(), indent + 2);

        fprintf(stderr, ")");
        return;
    }

    const char* name = parseNodeNames[getKind()];
    fprintf(stderr, "(%s ", name);
    indent += strlen(name) + 2;
    DumpParseTree(left(), indent);
    IndentNewLine(indent);
    DumpParseTree(right(), indent);
    fprintf(stderr, ")");
}

void
TernaryNode::dump(int indent)
{
    const char* name = parseNodeNames[getKind()];
    fprintf(stderr, "(%s ", name);
    indent += strlen(name) + 2;
    DumpParseTree(kid1(), indent);
    IndentNewLine(indent);
    DumpParseTree(kid2(), indent);
    IndentNewLine(indent);
    DumpParseTree(kid3(), indent);
    fprintf(stderr, ")");
}

void
FunctionNode::dump(int indent)
{
    const char* name = parseNodeNames[getKind()];
    fprintf(stderr, "(%s ", name);
    indent += strlen(name) + 2;
    DumpParseTree(body(), indent);
    fprintf(stderr, ")");
}

void
ModuleNode::dump(int indent)
{
    const char* name = parseNodeNames[getKind()];
    fprintf(stderr, "(%s ", name);
    indent += strlen(name) + 2;
    DumpParseTree(body(), indent);
    fprintf(stderr, ")");
}

void
ListNode::dump(int indent)
{
    const char* name = parseNodeNames[getKind()];
    fprintf(stderr, "(%s [", name);
    if (ParseNode* listHead = head()) {
        indent += strlen(name) + 3;
        DumpParseTree(listHead, indent);
        for (ParseNode* item : contentsFrom(listHead->pn_next)) {
            IndentNewLine(indent);
            DumpParseTree(item, indent);
        }
    }
    fprintf(stderr, "])");
}

template <typename CharT>
static void
DumpName(const CharT* s, size_t len)
{
    if (len == 0)
        fprintf(stderr, "#<zero-length name>");

    for (size_t i = 0; i < len; i++) {
        char16_t c = s[i];
        if (c > 32 && c < 127)
            fputc(c, stderr);
        else if (c <= 255)
            fprintf(stderr, "\\x%02x", unsigned(c));
        else
            fprintf(stderr, "\\u%04x", unsigned(c));
    }
}

void
NameNode::dump(int indent)
{
    switch (getKind()) {
      case PNK_STRING:
      case PNK_TEMPLATE_STRING:
      case PNK_OBJECT_PROPERTY_NAME: {
        atom()->dumpCharsNoNewline();
        return;
      }
      
      case PNK_NAME:
      case PNK_PRIVATE_NAME: // atom() already includes the '#', no need to specially include it.
      case PNK_PROPERTYNAME: {
        if (!atom()) {
            fprintf(stderr, "#<null name>");
        } else if (getOp() == JSOP_GETARG && atom()->length() == 0) {
            // Dump destructuring parameter.
            static const char ZeroLengthPrefix[] = "(#<zero-length name> ";
            fprintf(stderr, ZeroLengthPrefix);
            DumpParseTree(initializer(), indent + strlen(ZeroLengthPrefix));
            fputc(')', stderr);
        } else {
            JS::AutoCheckCannotGC nogc;
            if (atom()->hasLatin1Chars())
                DumpName(atom()->latin1Chars(nogc), atom()->length());
            else
                DumpName(atom()->twoByteChars(nogc), atom()->length());
        }
        return;
      }

      case PNK_LABEL: {
        const char* name = parseNodeNames[getKind()];
        fprintf(stderr, "(%s ", name);
        atom()->dumpCharsNoNewline();
        indent += strlen(name) + atom()->length() + 2;
        DumpParseTree(initializer(), indent);
        fprintf(stderr, ")");
        return;
      }

      default: {
        const char* name = parseNodeNames[getKind()];
        fprintf(stderr, "(%s ", name);
        indent += strlen(name) + 2;
        DumpParseTree(initializer(), indent);
        fprintf(stderr, ")");
        return;
      }
    }
}

void
LexicalScopeNode::dump(int indent)
{
    const char* name = parseNodeNames[getKind()];
    fprintf(stderr, "(%s [", name);
    int nameIndent = indent + strlen(name) + 3;
    if (!isEmptyScope()) {
        LexicalScope::Data* bindings = scopeBindings();
        for (uint32_t i = 0; i < bindings->length; i++) {
            JSAtom* name = bindings->trailingNames[i].name();
            JS::AutoCheckCannotGC nogc;
            if (name->hasLatin1Chars())
                DumpName(name->latin1Chars(nogc), name->length());
            else
                DumpName(name->twoByteChars(nogc), name->length());
            if (i < bindings->length - 1)
                IndentNewLine(nameIndent);
        }
    }
    fprintf(stderr, "]");
    indent += 2;
    IndentNewLine(indent);
    DumpParseTree(scopeBody(), indent);
    fprintf(stderr, ")");
}
#endif

ObjectBox::ObjectBox(JSObject* object, ObjectBox* traceLink)
  : object(object),
    traceLink(traceLink),
    emitLink(nullptr)
{
    MOZ_ASSERT(!object->is<JSFunction>());
    MOZ_ASSERT(object->isTenured());
}

ObjectBox::ObjectBox(JSFunction* function, ObjectBox* traceLink)
  : object(function),
    traceLink(traceLink),
    emitLink(nullptr)
{
    MOZ_ASSERT(object->is<JSFunction>());
    MOZ_ASSERT(asFunctionBox()->function() == function);
    MOZ_ASSERT(object->isTenured());
}

FunctionBox*
ObjectBox::asFunctionBox()
{
    MOZ_ASSERT(isFunctionBox());
    return static_cast<FunctionBox*>(this);
}

/* static */ void
ObjectBox::TraceList(JSTracer* trc, ObjectBox* listHead)
{
    for (ObjectBox* box = listHead; box; box = box->traceLink)
        box->trace(trc);
}

void
ObjectBox::trace(JSTracer* trc)
{
    TraceRoot(trc, &object, "parser.object");
}

void
FunctionBox::trace(JSTracer* trc)
{
    ObjectBox::trace(trc);
    if (enclosingScope_)
        TraceRoot(trc, &enclosingScope_, "funbox-enclosingScope");
}

bool
js::frontend::IsAnonymousFunctionDefinition(ParseNode* pn)
{
    // ES 2017 draft
    // 12.15.2 (ArrowFunction, AsyncArrowFunction).
    // 14.1.12 (FunctionExpression).
    // 14.4.8 (GeneratorExpression).
    // 14.6.8 (AsyncFunctionExpression)
    if (pn->is<FunctionNode>() &&
        !pn->as<FunctionNode>().funbox()->function()->explicitName())
        return true;

    // 14.5.8 (ClassExpression)
    if (pn->is<ClassNode>() && !pn->as<ClassNode>().names())
        return true;

    return false;
}