summaryrefslogtreecommitdiff
path: root/js/src/frontend/NameFunctions.cpp
blob: 9dbfb441f09e86fb35ec9a4716ab7ee5df580d2a (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
/* -*- 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/NameFunctions.h"

#include "mozilla/Sprintf.h"

#include "jsfun.h"
#include "jsprf.h"

#include "frontend/BytecodeCompiler.h"
#include "frontend/ParseNode.h"
#include "frontend/SharedContext.h"
#include "vm/StringBuffer.h"

using namespace js;
using namespace js::frontend;

namespace {

class NameResolver
{
    static const size_t MaxParents = 100;

    ExclusiveContext* cx;
    size_t nparents;                /* number of parents in the parents array */
    ParseNode* parents[MaxParents]; /* history of ParseNodes we've been looking at */
    StringBuffer* buf;              /* when resolving, buffer to append to */

    /* Test whether a ParseNode represents a function invocation */
    bool call(ParseNode* pn) {
        return pn && pn->isKind(PNK_CALL);
    }

    /*
     * Append a reference to a property named |name| to |buf|. If |name| is
     * a proper identifier name, then we append '.name'; otherwise, we
     * append '["name"]'.
     *
     * Note that we need the IsIdentifier check for atoms from both
     * PNK_NAME nodes and PNK_STRING nodes: given code like a["b c"], the
     * front end will produce a PNK_DOT with a PNK_NAME child whose name
     * contains spaces.
     */
    bool appendPropertyReference(JSAtom* name) {
        if (IsIdentifier(name))
            return buf->append('.') && buf->append(name);

        /* Quote the string as needed. */
        JSString* source = QuoteString(cx, name, '"');
        return source && buf->append('[') && buf->append(source) && buf->append(']');
    }

    /* Append a number to buf. */
    bool appendNumber(double n) {
        char number[30];
        int digits = SprintfLiteral(number, "%g", n);
        return buf->append(number, digits);
    }

    /* Append "[<n>]" to buf, referencing a property named by a numeric literal. */
    bool appendNumericPropertyReference(double n) {
        return buf->append("[") && appendNumber(n) && buf->append(']');
    }

    /*
     * Walk over the given ParseNode, attempting to convert it to a stringified
     * name that respresents where the function is being assigned to.
     *
     * |*foundName| is set to true if a name is found for the expression.
     */
    bool nameExpression(ParseNode* n, bool* foundName) {
        switch (n->getKind()) {
          case PNK_DOT: {
            PropertyAccess* prop = &n->as<PropertyAccess>();
            if (!nameExpression(&prop->expression(), foundName))
                return false;
            if (!*foundName)
                return true;
            return appendPropertyReference(prop->right()->pn_atom);
          }

          case PNK_NAME:
            *foundName = true;
            return buf->append(n->pn_atom);

          case PNK_THIS:
            *foundName = true;
            return buf->append("this");

          case PNK_ELEM: {
            PropertyByValue* elem = &n->as<PropertyByValue>();
            if (!nameExpression(&elem->expression(), foundName))
                return false;
            if (!*foundName)
                return true;
            if (!buf->append('[') || !nameExpression(elem->right(), foundName))
                return false;
            if (!*foundName)
                return true;
            return buf->append(']');
          }

          case PNK_NUMBER:
            *foundName = true;
            return appendNumber(n->pn_dval);

          default:
            /* We're confused as to what to call this function. */
            *foundName = false;
            return true;
        }
    }

    /*
     * When naming an anonymous function, the process works loosely by walking
     * up the AST and then translating that to a string. The stringification
     * happens from some far-up assignment and then going back down the parse
     * tree to the function definition point.
     *
     * This function will walk up the parse tree, gathering relevant nodes used
     * for naming, and return the assignment node if there is one. The provided
     * array and size will be filled in, and the returned node could be nullptr
     * if no assignment is found. The first element of the array will be the
     * innermost node relevant to naming, and the last element will be the
     * outermost node.
     */
    ParseNode* gatherNameable(ParseNode** nameable, size_t* size) {
        *size = 0;

        for (int pos = nparents - 1; pos >= 0; pos--) {
            ParseNode* cur = parents[pos];
            if (cur->is<AssignmentNode>())
                return cur;

            switch (cur->getKind()) {
              case PNK_NAME:     return cur;  /* found the initialized declaration */
              case PNK_THIS:     return cur;  /* Setting a property of 'this'. */
              case PNK_FUNCTION: return nullptr; /* won't find an assignment or declaration */

              case PNK_RETURN:
                /*
                 * Normally the relevant parent of a node is its direct parent, but
                 * sometimes with code like:
                 *
                 *    var foo = (function() { return function() {}; })();
                 *
                 * the outer function is just a helper to create a scope for the
                 * returned function. Hence the name of the returned function should
                 * actually be 'foo'.  This loop sees if the current node is a
                 * PNK_RETURN, and if there is a direct function call we skip to
                 * that.
                 */
                for (int tmp = pos - 1; tmp > 0; tmp--) {
                    if (isDirectCall(tmp, cur)) {
                        pos = tmp;
                        break;
                    } else if (call(cur)) {
                        /* Don't skip too high in the tree */
                        break;
                    }
                    cur = parents[tmp];
                }
                break;

              case PNK_COLON:
              case PNK_SHORTHAND:
                /*
                 * Record the PNK_COLON/SHORTHAND but skip the PNK_OBJECT so we're not
                 * flagged as a contributor.
                 */
                pos--;
                MOZ_FALLTHROUGH;

              default:
                /* Save any other nodes we encounter on the way up. */
                MOZ_ASSERT(*size < MaxParents);
                nameable[(*size)++] = cur;
                break;
            }
        }

        return nullptr;
    }

    /*
     * Resolve the name of a function. If the function already has a name
     * listed, then it is skipped. Otherwise an intelligent name is guessed to
     * assign to the function's displayAtom field.
     */
    bool resolveFun(ParseNode* pn, HandleAtom prefix, MutableHandleAtom retAtom) {
        MOZ_ASSERT(pn != nullptr);
        MOZ_ASSERT(pn->isKind(PNK_FUNCTION));
        MOZ_ASSERT(pn->isArity(PN_CODE));
        RootedFunction fun(cx, pn->pn_funbox->function());

        StringBuffer buf(cx);
        this->buf = &buf;

        retAtom.set(nullptr);

        /* If the function already has a name, use that */
        if (fun->displayAtom() != nullptr) {
            if (prefix == nullptr) {
                retAtom.set(fun->displayAtom());
                return true;
            }
            if (!buf.append(prefix) ||
                !buf.append('/') ||
                !buf.append(fun->displayAtom()))
                return false;
            retAtom.set(buf.finishAtom());
            return !!retAtom;
        }

        /* If a prefix is specified, then it is a form of namespace */
        if (prefix != nullptr && (!buf.append(prefix) || !buf.append('/')))
            return false;

        /* Gather all nodes relevant to naming */
        ParseNode* toName[MaxParents];
        size_t size;
        ParseNode* assignment = gatherNameable(toName, &size);

        /* If the function is assigned to something, then that is very relevant */
        if (assignment) {
            if (assignment->is<AssignmentNode>())
                assignment = assignment->as<AssignmentNode>().left();
            bool foundName = false;
            if (!nameExpression(assignment, &foundName))
                return false;
            if (!foundName)
                return true;
        }

        /*
         * Other than the actual assignment, other relevant nodes to naming are
         * those in object initializers and then particular nodes marking a
         * contribution.
         */
        for (int pos = size - 1; pos >= 0; pos--) {
            ParseNode* node = toName[pos];

            if (node->isKind(PNK_COLON) || node->isKind(PNK_SHORTHAND)) {
                ParseNode* left = node->as<BinaryNode>().left();
                if (left->isKind(PNK_OBJECT_PROPERTY_NAME) || left->isKind(PNK_STRING)) {
                    if (!appendPropertyReference(left->pn_atom))
                        return false;
                } else if (left->isKind(PNK_NUMBER)) {
                    if (!appendNumericPropertyReference(left->pn_dval))
                        return false;
                } else {
                    MOZ_ASSERT(left->isKind(PNK_COMPUTED_NAME));
                }
            } else {
                /*
                 * Don't have consecutive '<' characters, and also don't start
                 * with a '<' character.
                 */
                if (!buf.empty() && buf.getChar(buf.length() - 1) != '<' && !buf.append('<'))
                    return false;
            }
        }

        /*
         * functions which are "genuinely anonymous" but are contained in some
         * other namespace are rather considered as "contributing" to the outer
         * function, so give them a contribution symbol here.
         */
        if (!buf.empty() && buf.getChar(buf.length() - 1) == '/' && !buf.append('<'))
            return false;

        if (buf.empty())
            return true;

        retAtom.set(buf.finishAtom());
        if (!retAtom)
            return false;
        fun->setGuessedAtom(retAtom);
        return true;
    }

    /*
     * Tests whether parents[pos] is a function call whose callee is cur.
     * This is the case for functions which do things like simply create a scope
     * for new variables and then return an anonymous function using this scope.
     */
    bool isDirectCall(int pos, ParseNode* cur) {
        return pos >= 0 && call(parents[pos]) && parents[pos]->as<BinaryNode>().left() == cur;
    }

    bool resolveTemplateLiteral(ListNode* node, HandleAtom prefix) {
        MOZ_ASSERT(node->isKind(PNK_TEMPLATE_STRING_LIST));
        ParseNode* element = node->head();
        while (true) {
            MOZ_ASSERT(element->isKind(PNK_TEMPLATE_STRING));

            element = element->pn_next;
            if (!element)
                return true;

            if (!resolve(element, prefix))
                return false;

            element = element->pn_next;
        }
    }

    bool resolveTaggedTemplate(BinaryNode* taggedTemplate, HandleAtom prefix) {
        MOZ_ASSERT(taggedTemplate->isKind(PNK_TAGGED_TEMPLATE));

        ParseNode* tag = taggedTemplate->left();

        // The leading expression, e.g. |tag| in |tag`foo`|,
        // that might contain functions.
        if (!resolve(tag, prefix))
            return false;

        // The callsite object node is first.  This node only contains
        // internal strings or undefined and an array -- no user-controlled
        // expressions.
        CallSiteNode* element =
            &taggedTemplate->right()->as<ListNode>().head()->as<CallSiteNode>();
#ifdef DEBUG
        {
            ListNode* rawNodes = &element->head()->as<ListNode>();
            MOZ_ASSERT(rawNodes->isKind(PNK_ARRAY));
            for (ParseNode* raw : rawNodes->contents()) {
                MOZ_ASSERT(raw->isKind(PNK_TEMPLATE_STRING));
            }
            for (ParseNode* cooked : element->contentsFrom(rawNodes->pn_next)) {
                MOZ_ASSERT(cooked->isKind(PNK_TEMPLATE_STRING) ||
                           cooked->isKind(PNK_RAW_UNDEFINED));
            }
        }
#endif

        // Next come any interpolated expressions in the tagged template.
        ParseNode* interpolated = element->pn_next;
        for (; interpolated; interpolated = interpolated->pn_next) {
            if (!resolve(interpolated, prefix))
                return false;
        }

        return true;
    }

  public:
    explicit NameResolver(ExclusiveContext* cx) : cx(cx), nparents(0), buf(nullptr) {}

    /*
     * Resolve all names for anonymous functions recursively within the
     * ParseNode instance given. The prefix is for each subsequent name, and
     * should initially be nullptr.
     */
    bool resolve(ParseNode* cur, HandleAtom prefixArg = nullptr) {
        RootedAtom prefix(cx, prefixArg);
        if (cur == nullptr)
            return true;

        MOZ_ASSERT((cur->isKind(PNK_FUNCTION) || cur->isKind(PNK_MODULE)) == cur->isArity(PN_CODE));
        if (cur->isKind(PNK_FUNCTION)) {
            RootedAtom prefix2(cx);
            if (!resolveFun(cur, prefix, &prefix2))
                return false;

            /*
             * If a function looks like (function(){})() where the parent node
             * of the definition of the function is a call, then it shouldn't
             * contribute anything to the namespace, so don't bother updating
             * the prefix to whatever was returned.
             */
            if (!isDirectCall(nparents - 1, cur))
                prefix = prefix2;
        }
        if (nparents >= MaxParents)
            return true;
        parents[nparents++] = cur;

        switch (cur->getKind()) {
          // Nodes with no children that might require name resolution need no
          // further work.
          case PNK_NOP:
          case PNK_STRING:
          case PNK_TEMPLATE_STRING:
          case PNK_REGEXP:
          case PNK_TRUE:
          case PNK_FALSE:
          case PNK_NULL:
          case PNK_RAW_UNDEFINED:
          case PNK_ELISION:
          case PNK_GENERATOR:
          case PNK_NUMBER:
          case PNK_BREAK:
          case PNK_CONTINUE:
          case PNK_DEBUGGER:
          case PNK_EXPORT_BATCH_SPEC:
          case PNK_OBJECT_PROPERTY_NAME:
          case PNK_POSHOLDER:
            MOZ_ASSERT(cur->isArity(PN_NULLARY));
            break;

          case PNK_TYPEOFNAME:
          case PNK_SUPERBASE:
            MOZ_ASSERT(cur->as<UnaryNode>().kid()->isKind(PNK_NAME));
            MOZ_ASSERT(!cur->as<UnaryNode>().kid()->expr());
            break;

          case PNK_NEWTARGET: {
            MOZ_ASSERT(cur->as<BinaryNode>().left()->isKind(PNK_POSHOLDER));
            MOZ_ASSERT(cur->as<BinaryNode>().right()->isKind(PNK_POSHOLDER));
            break;
          }

          // Nodes with a single non-null child requiring name resolution.
          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_NEG:
          case PNK_POS:
          case PNK_PREINCREMENT:
          case PNK_POSTINCREMENT:
          case PNK_PREDECREMENT:
          case PNK_POSTDECREMENT:
          case PNK_COMPUTED_NAME:
          case PNK_ARRAYPUSH:
          case PNK_SPREAD:
          case PNK_MUTATEPROTO:
          case PNK_EXPORT:
            if (!resolve(cur->as<UnaryNode>().kid(), prefix))
                return false;
            break;

          // Nodes with a single nullable child.
          case PNK_SEMI:
          case PNK_THIS:
            if (ParseNode* expr = cur->as<UnaryNode>().kid()) {
                if (!resolve(expr, prefix))
                    return false;
            }
            break;

          // Binary nodes with two non-null children.
          case PNK_ASSIGN:
          case PNK_ADDASSIGN:
          case PNK_SUBASSIGN:
          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:
          case PNK_COLON:
          case PNK_SHORTHAND:
          case PNK_DOWHILE:
          case PNK_WHILE:
          case PNK_SWITCH:
          case PNK_FOR:
          case PNK_COMPREHENSIONFOR:
          case PNK_CLASSMETHOD:
          case PNK_SETTHIS: {
            BinaryNode* node = &cur->as<BinaryNode>();
            if (!resolve(node->left(), prefix)) {
                return false;
            }
            if (!resolve(node->right(), prefix)) {
                return false;
            }
            break;
          }

          case PNK_ELEM: {
            PropertyByValue* elem = &cur->as<PropertyByValue>();
            if (!elem->isSuper() && !resolve(&elem->expression(), prefix))
                return false;
            if (!resolve(&elem->key(), prefix))
                return false;
            break;
          }

          case PNK_WITH: {
            BinaryNode* node = &cur->as<BinaryNode>();
            if (!resolve(node->left(), prefix))
                return false;
            if (!resolve(node->right(), prefix))
                return false;
            break;
          }

          case PNK_CASE: {
            CaseClause* caseClause = &cur->as<CaseClause>();
            if (ParseNode* caseExpr = caseClause->caseExpression()) {
                if (!resolve(caseExpr, prefix))
                    return false;
            }
            if (!resolve(caseClause->statementList(), prefix))
                return false;
            break;
          }

          case PNK_INITIALYIELD: {
#ifdef DEBUG
            AssignmentNode* assignNode = &cur->as<UnaryNode>().kid()->as<AssignmentNode>();
            MOZ_ASSERT(assignNode->left()->isKind(PNK_NAME));
            MOZ_ASSERT(assignNode->right()->isKind(PNK_GENERATOR));
#endif
            break;
          }

          case PNK_YIELD_STAR:
            if (!resolve(cur->as<UnaryNode>().kid(), prefix))
                return false;
            break;

          case PNK_YIELD:
          case PNK_AWAIT:
            if (ParseNode* expr = cur->as<UnaryNode>().kid()) {
                if (!resolve(expr, prefix))
                    return false;
            }
            break;

          case PNK_RETURN:
            if (ParseNode* returnValue = cur->as<UnaryNode>().kid()) {
                if (!resolve(returnValue, prefix))
                    return false;
            }
            break;

          case PNK_IMPORT:
          case PNK_EXPORT_FROM:
          case PNK_EXPORT_DEFAULT: {
            BinaryNode* node = &cur->as<BinaryNode>();
            MOZ_ASSERT(cur->isArity(PN_BINARY));
            // The left halves of these nodes don't contain any unconstrained
            // expressions, but it's very hard to assert this to safely rely on
            // it.  So recur anyway.
            if (!resolve(node->left(), prefix))
                return false;
            MOZ_ASSERT_IF(!node->isKind(PNK_EXPORT_DEFAULT),
                          node->right()->isKind(PNK_STRING));
            break;
          }

          // Ternary nodes with three expression children.
          case PNK_CONDITIONAL: {
            TernaryNode* condNode = &cur->as<TernaryNode>();
            if (!resolve(condNode->kid1(), prefix))
                return false;
            if (!resolve(condNode->kid2(), prefix))
                return false;
            if (!resolve(condNode->kid3(), prefix))
                return false;
            break;
          }

          // The first part of a for-in/of is the declaration in the loop (or
          // null if no declaration).  The latter two parts are the location
          // assigned each loop and the value being looped over; obviously,
          // either might contain functions to name.  Declarations may (through
          // computed property names, and possibly through [deprecated!]
          // initializers) also contain functions to name.
          case PNK_FORIN:
          case PNK_FOROF: {
            TernaryNode* forHead = &cur->as<TernaryNode>();
            if (ParseNode* decl = forHead->kid1()) {
                if (!resolve(decl, prefix))
                    return false;
            }
            MOZ_ASSERT(!forHead->kid2());
            if (!resolve(forHead->kid3(), prefix))
                return false;
            break;
          }

          // Every part of a for(;;) head may contain a function needing name
          // resolution.
          case PNK_FORHEAD: {
            TernaryNode* forHead = &cur->as<TernaryNode>();
            if (ParseNode* init = forHead->kid1()) {
                if (!resolve(init, prefix))
                    return false;
            }
            if (ParseNode* cond = forHead->kid2()) {
                if (!resolve(cond, prefix))
                    return false;
            }
            if (ParseNode* update = forHead->kid3()) {
                if (!resolve(update, prefix))
                    return false;
            }
            break;
          }

          // The first child of a class is a pair of names referring to it,
          // inside and outside the class.  The second is the class's heritage,
          // if any.  The third is the class body.
          case PNK_CLASS: {
            ClassNode* classNode = &cur->as<ClassNode>();
#ifdef DEBUG
            if (classNode->names()) {
                ClassNames* names = classNode->names();
                if (ParseNode* outerBinding = names->outerBinding()) {
                    MOZ_ASSERT(outerBinding->isKind(PNK_NAME));
                    MOZ_ASSERT(!outerBinding->expr());
                }

                ParseNode* innerBinding = names->innerBinding();
                MOZ_ASSERT(innerBinding->isKind(PNK_NAME));
                MOZ_ASSERT(!innerBinding->expr());
            }
#endif
            if (ParseNode* heritage = classNode->heritage()) {
                if (!resolve(heritage, prefix))
                    return false;
            }
            if (!resolve(classNode->methodList(), prefix))
                return false;
            break;
          }

          // The condition and consequent are non-optional, but the alternative
          // might be omitted.
          case PNK_IF: {
            TernaryNode* ifNode = &cur->as<TernaryNode>();
            if (!resolve(ifNode->kid1(), prefix))
                return false;
            if (!resolve(ifNode->kid2(), prefix))
                return false;
            if (ParseNode* alternative = ifNode->kid3()) {
                if (!resolve(alternative, prefix))
                    return false;
            }
            break;
          }

          // The statements in the try-block are mandatory.  The catch-blocks
          // and finally block are optional (but at least one or the other must
          // be present).
          case PNK_TRY: {
            TernaryNode* tryNode = &cur->as<TernaryNode>();
            if (!resolve(tryNode->kid1(), prefix))
                return false;
            MOZ_ASSERT(tryNode->kid2() || tryNode->kid3());
            if (ParseNode* catchList = tryNode->kid2()) {
                MOZ_ASSERT(catchList->isKind(PNK_CATCHLIST));
                if (!resolve(catchList, prefix))
                    return false;
            }
            if (ParseNode* finallyBlock = tryNode->kid3()) {
                if (!resolve(finallyBlock, prefix))
                    return false;
            }
            break;
          }

          // The first child, the catch-pattern, may contain functions via
          // computed property names.  The optional catch-conditions may
          // contain any expression.  The catch statements, of course, may
          // contain arbitrary expressions.
          case PNK_CATCH: {
            TernaryNode* catchNode = &cur->as<TernaryNode>();
            if (ParseNode* patNode = catchNode->kid1()) {
                if (!resolve(patNode, prefix))
                    return false;
            }
            if (ParseNode* condNode = catchNode->kid2()) {
                if (!resolve(condNode, prefix))
                    return false;
            }
            if (!resolve(catchNode->kid3(), prefix))
                return false;
            break;
          }

          // Nodes with arbitrary-expression 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_STATEMENTLIST:
          case PNK_PARAMSBODY:
          // Initializers for individual variables, and computed property names
          // within destructuring patterns, may contain unnamed functions.
          case PNK_VAR:
          case PNK_CONST:
          case PNK_LET:
            for (ParseNode* element : cur->as<ListNode>().contents()) {
                if (!resolve(element, prefix))
                    return false;
            }
            break;

          // 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 = &cur->as<ListNode>();
            MOZ_ASSERT(literal->count() == 1);
            MOZ_ASSERT(literal->head()->isKind(PNK_LEXICALSCOPE) ||
                       literal->head()->isKind(PNK_COMPREHENSIONFOR));
            if (!resolve(literal->head(), prefix))
                return false;
            break;
          }

          case PNK_OBJECT:
          case PNK_CLASSMETHODLIST:
            for (ParseNode* element : cur->as<ListNode>().contents()) {
                if (!resolve(element, prefix))
                    return false;
            }
            break;

          // A template string list's contents alternate raw template string
          // contents with expressions interpolated into the overall literal.
          case PNK_TEMPLATE_STRING_LIST:
            if (!resolveTemplateLiteral(&cur->as<ListNode>(), prefix))
                return false;
            break;

          case PNK_TAGGED_TEMPLATE:
            if (!resolveTaggedTemplate(&cur->as<BinaryNode>(), prefix))
                return false;
            break;

          case PNK_NEW:
          case PNK_CALL:
          case PNK_GENEXP:
          case PNK_SUPERCALL: {
            BinaryNode* callNode = &cur->as<BinaryNode>();
            if (!resolve(callNode->left(), prefix))
                return false;
            if (!resolve(callNode->right(), prefix))
                return false;
            break;
          }

          // Handles the arguments for new/call/supercall, but does _not_ handle
          // the Arguments node used by tagged template literals, since that is
          // special-cased inside of resolveTaggedTemplate.
          case PNK_ARGUMENTS:
            for (ParseNode* element : cur->as<ListNode>().contents()) {
                if (!resolve(element, prefix))
                    return false;
            }
            break;

          // Import/export spec lists contain import/export specs containing
          // only pairs of names. Alternatively, an export spec lists may
          // contain a single export batch specifier.
          case PNK_EXPORT_SPEC_LIST:
          case PNK_IMPORT_SPEC_LIST: {
#ifdef DEBUG
            bool isImport = cur->isKind(PNK_IMPORT_SPEC_LIST);
            ListNode* list = &cur->as<ListNode>();
            ParseNode* item = list->head();
            if (!isImport && item && item->isKind(PNK_EXPORT_BATCH_SPEC)) {
                MOZ_ASSERT(item->isArity(PN_NULLARY));
                break;
            }
            for (ParseNode* item : list->contents()) {
                BinaryNode* spec = &item->as<BinaryNode>();
                MOZ_ASSERT(spec->isKind(isImport ? PNK_IMPORT_SPEC : PNK_EXPORT_SPEC));
                MOZ_ASSERT(spec->left()->isKind(PNK_NAME));
                MOZ_ASSERT(!spec->left()->expr());
                MOZ_ASSERT(spec->right()->isKind(PNK_NAME));
                MOZ_ASSERT(!spec->right()->expr());
            }
#endif
            break;
          }

          case PNK_CATCHLIST: {
            ListNode* catchList = &cur->as<ListNode>();
            for (ParseNode* catchNode : catchList->contents()) {
                MOZ_ASSERT(catchNode->isKind(PNK_LEXICALSCOPE));
                MOZ_ASSERT(catchNode->scopeBody()->isKind(PNK_CATCH));
                MOZ_ASSERT(catchNode->scopeBody()->isArity(PN_TERNARY));
                if (!resolve(catchNode->scopeBody(), prefix))
                    return false;
            }
            break;
          }

          case PNK_DOT: {
            // Super prop nodes do not have a meaningful LHS
            PropertyAccess* prop = &cur->as<PropertyAccess>();
            if (prop->isSuper()) {
                break;
            }
            if (!resolve(&prop->expression(), prefix)) {
                return false;
            }
            break;
          }

          case PNK_LABEL:
            MOZ_ASSERT(cur->isArity(PN_NAME));
            if (!resolve(cur->expr(), prefix))
                return false;
            break;

          case PNK_NAME:
            MOZ_ASSERT(cur->isArity(PN_NAME));
            if (!resolve(cur->expr(), prefix))
                return false;
            break;

          case PNK_LEXICALSCOPE:
            MOZ_ASSERT(cur->isArity(PN_SCOPE));
            if (!resolve(cur->scopeBody(), prefix))
                return false;
            break;

          case PNK_FUNCTION:
          case PNK_MODULE:
            MOZ_ASSERT(cur->isArity(PN_CODE));
            if (!resolve(cur->pn_body, prefix))
                return false;
            break;

          // Kinds that should be handled by parent node resolution.

          case PNK_IMPORT_SPEC: // by PNK_IMPORT_SPEC_LIST
          case PNK_EXPORT_SPEC: // by PNK_EXPORT_SPEC_LIST
          case PNK_CALLSITEOBJ: // by PNK_TAGGED_TEMPLATE
          case PNK_CLASSNAMES:  // by PNK_CLASS
          case PNK_PROPERTYNAME: // by PNK_DOT
            MOZ_CRASH("should have been handled by a parent node");

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

        nparents--;
        return true;
    }
};

} /* anonymous namespace */

bool
frontend::NameFunctions(ExclusiveContext* cx, ParseNode* pn)
{
    NameResolver nr(cx);
    return nr.resolve(pn);
}