Debian Codegraph @

js/src/frontend/ParseNode.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef frontend_ParseNode_h
8 #define frontend_ParseNode_h
9
10 #include "mozilla/Assertions.h"
11
12 #include <iterator>
13 #include <stddef.h>
14 #include <stdint.h>
15
16 #include "jstypes.h" // js::Bit
17
18 #include "frontend/FunctionSyntaxKind.h" // FunctionSyntaxKind
19 #include "frontend/NameAnalysisTypes.h" // PrivateNameKind
20 #include "frontend/ParserAtom.h" // TaggedParserAtomIndex
21 #include "frontend/Stencil.h" // BigIntStencil
22 #include "frontend/Token.h"
23 #include "js/TypeDecls.h"
24 #include "vm/Opcodes.h"
25 #include "vm/Scope.h"
26 #include "vm/ScopeKind.h"
27
28 // [SMDOC] ParseNode tree lifetime information
29 //
30 // - All the `ParseNode` instances MUST BE explicitly allocated in the context's
31 // `LifoAlloc`. This is typically implemented by the `FullParseHandler` or it
32 // can be reimplemented with a custom `new_`.
33 //
34 // - The tree is bulk-deallocated when the parser is deallocated. Consequently,
35 // references to a subtree MUST NOT exist once the parser has been
36 // deallocated.
37 //
38 // - This bulk-deallocation DOES NOT run destructors.
39 //
40 // - Instances of `LexicalScope::ParserData` and `ClassBodyScope::ParserData`
41 // MUST BE allocated as instances of `ParseNode`, in the same `LifoAlloc`.
42 // They are bulk-deallocated alongside the rest of the tree.
43
44 struct JSContext;
45
46 namespace js {
47
48 class JS_PUBLIC_API GenericPrinter;
49 class LifoAlloc;
50 class RegExpObject;
51
52 namespace frontend {
53
54 class ParserAtomsTable;
55 class ParserBase;
56 class ParseContext;
57 struct ExtensibleCompilationStencil;
58 class ParserSharedBase;
59 class FullParseHandler;
60
61 class FunctionBox;
62
63 #define FOR_EACH_PARSE_NODE_KIND(F) \
64 F(EmptyStmt, NullaryNode) \
65 F(ExpressionStmt, UnaryNode) \
66 F(CommaExpr, ListNode) \
67 F(ConditionalExpr, ConditionalExpression) \
68 F(PropertyDefinition, PropertyDefinition) \
69 F(Shorthand, BinaryNode) \
70 F(PosExpr, UnaryNode) \
71 F(NegExpr, UnaryNode) \
72 F(PreIncrementExpr, UnaryNode) \
73 F(PostIncrementExpr, UnaryNode) \
74 F(PreDecrementExpr, UnaryNode) \
75 F(PostDecrementExpr, UnaryNode) \
76 F(PropertyNameExpr, NameNode) \
77 F(DotExpr, PropertyAccess) \
78 F(ArgumentsLength, ArgumentsLength) \
79 F(ElemExpr, PropertyByValue) \
80 F(PrivateMemberExpr, PrivateMemberAccess) \
81 F(OptionalDotExpr, OptionalPropertyAccess) \
82 F(OptionalChain, UnaryNode) \
83 F(OptionalElemExpr, OptionalPropertyByValue) \
84 F(OptionalPrivateMemberExpr, OptionalPrivateMemberAccess) \
85 F(OptionalCallExpr, CallNode) \
86 F(ArrayExpr, ListNode) \
87 F(Elision, NullaryNode) \
88 F(StatementList, ListNode) \
89 F(LabelStmt, LabeledStatement) \
90 F(ObjectExpr, ListNode) \
91 F(CallExpr, CallNode) \
92 F(Arguments, ListNode) \
93 F(Name, NameNode) \
94 F(ObjectPropertyName, NameNode) \
95 F(PrivateName, NameNode) \
96 F(ComputedName, UnaryNode) \
97 F(NumberExpr, NumericLiteral) \
98 F(BigIntExpr, BigIntLiteral) \
99 F(StringExpr, NameNode) \
100 F(TemplateStringListExpr, ListNode) \
101 F(TemplateStringExpr, NameNode) \
102 F(TaggedTemplateExpr, CallNode) \
103 F(CallSiteObj, CallSiteNode) \
104 F(RegExpExpr, RegExpLiteral) \
105 F(TrueExpr, BooleanLiteral) \
106 F(FalseExpr, BooleanLiteral) \
107 F(NullExpr, NullLiteral) \
108 F(RawUndefinedExpr, RawUndefinedLiteral) \
109 F(ThisExpr, UnaryNode) \
110 F(Function, FunctionNode) \
111 F(Module, ModuleNode) \
112 F(IfStmt, TernaryNode) \
113 F(SwitchStmt, SwitchStatement) \
114 F(Case, CaseClause) \
115 F(WhileStmt, BinaryNode) \
116 F(DoWhileStmt, BinaryNode) \
117 F(ForStmt, ForNode) \
118 F(BreakStmt, BreakStatement) \
119 F(ContinueStmt, ContinueStatement) \
120 F(VarStmt, DeclarationListNode) \
121 F(ConstDecl, DeclarationListNode) \
122 IF_EXPLICIT_RESOURCE_MANAGEMENT(F(UsingDecl, DeclarationListNode)) \
123 IF_EXPLICIT_RESOURCE_MANAGEMENT(F(AwaitUsingDecl, DeclarationListNode)) \
124 F(WithStmt, BinaryNode) \
125 F(ReturnStmt, UnaryNode) \
126 F(NewExpr, CallNode) \
127 IF_DECORATORS(F(DecoratorList, ListNode)) \
128 /* Delete operations. These must be sequential. */ \
129 F(DeleteNameExpr, UnaryNode) \
130 F(DeletePropExpr, UnaryNode) \
131 F(DeleteElemExpr, UnaryNode) \
132 F(DeleteOptionalChainExpr, UnaryNode) \
133 F(DeleteExpr, UnaryNode) \
134 F(TryStmt, TernaryNode) \
135 F(Catch, BinaryNode) \
136 F(ThrowStmt, UnaryNode) \
137 F(DebuggerStmt, DebuggerStatement) \
138 F(Generator, NullaryNode) \
139 F(InitialYield, UnaryNode) \
140 F(YieldExpr, UnaryNode) \
141 F(YieldStarExpr, UnaryNode) \
142 F(LexicalScope, LexicalScopeNode) \
143 F(LetDecl, DeclarationListNode) \
144 F(ImportDecl, BinaryNode) \
145 F(ImportSpecList, ListNode) \
146 F(ImportSpec, BinaryNode) \
147 F(ImportNamespaceSpec, UnaryNode) \
148 F(ImportAttributeList, ListNode) \
149 F(ImportAttribute, BinaryNode) \
150 F(ImportModuleRequest, BinaryNode) \
151 F(ExportStmt, UnaryNode) \
152 F(ExportFromStmt, BinaryNode) \
153 F(ExportDefaultStmt, BinaryNode) \
154 F(ExportSpecList, ListNode) \
155 F(ExportSpec, BinaryNode) \
156 F(ExportNamespaceSpec, UnaryNode) \
157 F(ExportBatchSpecStmt, NullaryNode) \
158 F(ForIn, TernaryNode) \
159 F(ForOf, TernaryNode) \
160 F(ForHead, TernaryNode) \
161 F(ParamsBody, ParamsBodyNode) \
162 F(Spread, UnaryNode) \
163 F(MutateProto, UnaryNode) \
164 F(ClassDecl, ClassNode) \
165 F(DefaultConstructor, ClassMethod) \
166 F(ClassBodyScope, ClassBodyScopeNode) \
167 F(ClassMethod, ClassMethod) \
168 F(StaticClassBlock, StaticClassBlock) \
169 F(ClassField, ClassField) \
170 F(ClassMemberList, ListNode) \
171 F(ClassNames, ClassNames) \
172 F(NewTargetExpr, NewTargetNode) \
173 F(PosHolder, NullaryNode) \
174 F(SuperBase, UnaryNode) \
175 F(SuperCallExpr, CallNode) \
176 F(SetThis, BinaryNode) \
177 F(ImportMetaExpr, BinaryNode) \
178 F(CallImportExpr, BinaryNode) \
179 F(CallImportSpec, BinaryNode) \
180 F(InitExpr, BinaryNode) \
181 \
182 /* Unary operators. */ \
183 F(TypeOfNameExpr, UnaryNode) \
184 F(TypeOfExpr, UnaryNode) \
185 F(VoidExpr, UnaryNode) \
186 F(NotExpr, UnaryNode) \
187 F(BitNotExpr, UnaryNode) \
188 F(AwaitExpr, UnaryNode) \
189 \
190 /* \
191 * Binary operators. \
192 * This list must be kept in the same order in several places: \
193 * - The binary operators in ParseNode.h \
194 * - the binary operators in TokenKind.h \
195 * - the precedence list in Parser.cpp \
196 * - the JSOp code list in BytecodeEmitter.cpp \
197 */ \
198 F(CoalesceExpr, ListNode) \
199 F(OrExpr, ListNode) \
200 F(AndExpr, ListNode) \
201 F(BitOrExpr, ListNode) \
202 F(BitXorExpr, ListNode) \
203 F(BitAndExpr, ListNode) \
204 F(StrictEqExpr, ListNode) \
205 F(EqExpr, ListNode) \
206 F(StrictNeExpr, ListNode) \
207 F(NeExpr, ListNode) \
208 F(LtExpr, ListNode) \
209 F(LeExpr, ListNode) \
210 F(GtExpr, ListNode) \
211 F(GeExpr, ListNode) \
212 F(InstanceOfExpr, ListNode) \
213 F(InExpr, ListNode) \
214 F(PrivateInExpr, ListNode) \
215 F(LshExpr, ListNode) \
216 F(RshExpr, ListNode) \
217 F(UrshExpr, ListNode) \
218 F(AddExpr, ListNode) \
219 F(SubExpr, ListNode) \
220 F(MulExpr, ListNode) \
221 F(DivExpr, ListNode) \
222 F(ModExpr, ListNode) \
223 F(PowExpr, ListNode) \
224 \
225 /* Assignment operators (= += -= etc.). */ \
226 /* AssignmentNode::test assumes all these are consecutive. */ \
227 F(AssignExpr, AssignmentNode) \
228 F(AddAssignExpr, AssignmentNode) \
229 F(SubAssignExpr, AssignmentNode) \
230 F(CoalesceAssignExpr, AssignmentNode) \
231 F(OrAssignExpr, AssignmentNode) \
232 F(AndAssignExpr, AssignmentNode) \
233 F(BitOrAssignExpr, AssignmentNode) \
234 F(BitXorAssignExpr, AssignmentNode) \
235 F(BitAndAssignExpr, AssignmentNode) \
236 F(LshAssignExpr, AssignmentNode) \
237 F(RshAssignExpr, AssignmentNode) \
238 F(UrshAssignExpr, AssignmentNode) \
239 F(MulAssignExpr, AssignmentNode) \
240 F(DivAssignExpr, AssignmentNode) \
241 F(ModAssignExpr, AssignmentNode) \
242 F(PowAssignExpr, AssignmentNode)
243
244 /*
245 * Parsing builds a tree of nodes that directs code generation. This tree is
246 * not a concrete syntax tree in all respects (for example, || and && are left
247 * associative, but (A && B && C) translates into the right-associated tree
248 * <A && <B && C>> so that code generation can emit a left-associative branch
249 * around <B && C> when A is false). Nodes are labeled by kind.
250 *
251 * The long comment after this enum block describes the kinds in detail.
252 */
253 enum class ParseNodeKind : uint16_t {
254 // These constants start at 1001, the better to catch
255 LastUnused = 1000,
256 #define EMIT_ENUM(name, _type) name,
257 FOR_EACH_PARSE_NODE_KIND(EMIT_ENUM)
258 #undef EMIT_ENUM
259 Limit,
260 Start = LastUnused + 1,
261 BinOpFirst = ParseNodeKind::CoalesceExpr,
262 BinOpLast = ParseNodeKind::PowExpr,
263 AssignmentStart = ParseNodeKind::AssignExpr,
264 AssignmentLast = ParseNodeKind::PowAssignExpr,
265 };
266
267 inline bool IsDeleteKind(ParseNodeKind kind) {
268 return ParseNodeKind::DeleteNameExpr <= kind &&
269 kind <= ParseNodeKind::DeleteExpr;
270 }
271
272 inline bool IsTypeofKind(ParseNodeKind kind) {
273 return ParseNodeKind::TypeOfNameExpr <= kind &&
274 kind <= ParseNodeKind::TypeOfExpr;
275 }
276
277 /*
278 * <Definitions>
279 * Function (FunctionNode)
280 * funbox: ptr to js::FunctionBox
281 * body: ParamsBody or null for lazily-parsed function
282 * syntaxKind: the syntax of the function
283 * ParamsBody (ListNode)
284 * head: list of formal parameters with
285 * * Name node with non-empty name for SingleNameBinding without
286 * Initializer
287 * * AssignExpr node for SingleNameBinding with Initializer
288 * * Name node with empty name for destructuring
289 * expr: Array or Object for BindingPattern without
290 * Initializer, Assign for BindingPattern with
291 * Initializer
292 * followed by:
293 * * LexicalScopeNode
294 * count: number of formal parameters + 1
295 * Spread (UnaryNode)
296 * kid: expression being spread
297 * ClassDecl (ClassNode)
298 * kid1: ClassNames for class name. can be null for anonymous class.
299 * kid2: expression after `extends`. null if no expression
300 * kid3: either of
301 * * ClassMemberList, if anonymous class
302 * * LexicalScopeNode which contains ClassMemberList as scopeBody,
303 * if named class
304 * ClassNames (ClassNames)
305 * left: Name node for outer binding, or null if the class is an expression
306 * that doesn't create an outer binding
307 * right: Name node for inner binding
308 * ClassMemberList (ListNode)
309 * head: list of N ClassMethod, ClassField or StaticClassBlock nodes
310 * count: N >= 0
311 * DefaultConstructor (ClassMethod)
312 * name: propertyName
313 * method: methodDefinition
314 * ClassMethod (ClassMethod)
315 * name: propertyName
316 * method: methodDefinition
317 * initializerIfPrivate: initializer to stamp private method onto instance
318 * Module (ModuleNode)
319 * body: statement list of the module
320 *
321 * <Statements>
322 * StatementList (ListNode)
323 * head: list of N statements
324 * count: N >= 0
325 * IfStmt (TernaryNode)
326 * kid1: cond
327 * kid2: then
328 * kid3: else or null
329 * SwitchStmt (SwitchStatement)
330 * left: discriminant
331 * right: LexicalScope node that contains the list of Case nodes, with at
332 * most one default node.
333 * hasDefault: true if there's a default case
334 * Case (CaseClause)
335 * left: case-expression if CaseClause, or null if DefaultClause
336 * right: StatementList node for this case's statements
337 * WhileStmt (BinaryNode)
338 * left: cond
339 * right: body
340 * DoWhileStmt (BinaryNode)
341 * left: body
342 * right: cond
343 * ForStmt (ForNode)
344 * left: one of
345 * * ForIn: for (x in y) ...
346 * * ForOf: for (x of x) ...
347 * * ForHead: for (;;) ...
348 * right: body
349 * ForIn (TernaryNode)
350 * kid1: declaration or expression to left of 'in'
351 * kid2: null
352 * kid3: object expr to right of 'in'
353 * ForOf (TernaryNode)
354 * kid1: declaration or expression to left of 'of'
355 * kid2: null
356 * kid3: expr to right of 'of'
357 * ForHead (TernaryNode)
358 * kid1: init expr before first ';' or nullptr
359 * kid2: cond expr before second ';' or nullptr
360 * kid3: update expr after second ';' or nullptr
361 * ThrowStmt (UnaryNode)
362 * kid: thrown exception
363 * TryStmt (TernaryNode)
364 * kid1: try block
365 * kid2: null or LexicalScope for catch-block with scopeBody pointing to a
366 * Catch node
367 * kid3: null or finally block
368 * Catch (BinaryNode)
369 * left: Name, Array, or Object catch var node
370 * (Array or Object if destructuring),
371 * or null if optional catch binding
372 * right: catch block statements
373 * BreakStmt (BreakStatement)
374 * label: label or null
375 * ContinueStmt (ContinueStatement)
376 * label: label or null
377 * WithStmt (BinaryNode)
378 * left: head expr
379 * right: body
380 * VarStmt, LetDecl, ConstDecl (DeclarationListNode)
381 * head: list of N Name or AssignExpr nodes
382 * each name node has either
383 * atom: variable name
384 * expr: initializer or null
385 * or
386 * atom: variable name
387 * each assignment node has
388 * left: pattern
389 * right: initializer
390 * count: N > 0
391 * ReturnStmt (UnaryNode)
392 * kid: returned expression, or null if none
393 * ExpressionStmt (UnaryNode)
394 * kid: expr
395 * EmptyStmt (NullaryNode)
396 * (no fields)
397 * LabelStmt (LabeledStatement)
398 * atom: label
399 * expr: labeled statement
400 * ImportDecl (BinaryNode)
401 * left: ImportSpecList import specifiers
402 * right: String module specifier
403 * ImportSpecList (ListNode)
404 * head: list of N ImportSpec nodes
405 * count: N >= 0 (N = 0 for `import {} from ...`)
406 * ImportSpec (BinaryNode)
407 * left: import name
408 * right: local binding name
409 * ImportNamespaceSpec (UnaryNode)
410 * kid: local binding name
411 * ExportStmt (UnaryNode)
412 * kid: declaration expression
413 * ExportFromStmt (BinaryNode)
414 * left: ExportSpecList export specifiers
415 * right: String module specifier
416 * ExportSpecList (ListNode)
417 * head: list of N ExportSpec nodes
418 * count: N >= 0 (N = 0 for `export {}`)
419 * ExportSpec (BinaryNode)
420 * left: local binding name
421 * right: export name
422 * ExportNamespaceSpec (UnaryNode)
423 * kid: export name
424 * ExportDefaultStmt (BinaryNode)
425 * left: export default declaration or expression
426 * right: Name node for assignment
427 *
428 * <Expressions>
429 * The `Expr` suffix is used for nodes that can appear anywhere an expression
430 * could appear. It is not used on a few weird kinds like Arguments and
431 * CallSiteObj that are always the child node of an expression node, but which
432 * can't stand alone.
433 *
434 * All left-associated binary trees of the same type are optimized into lists
435 * to avoid recursion when processing expression chains.
436 *
437 * CommaExpr (ListNode)
438 * head: list of N comma-separated exprs
439 * count: N >= 2
440 * AssignExpr (BinaryNode)
441 * left: target of assignment
442 * right: value to assign
443 * AddAssignExpr, SubAssignExpr, CoalesceAssignExpr, OrAssignExpr,
444 * AndAssignExpr, BitOrAssignExpr, BitXorAssignExpr, BitAndAssignExpr,
445 * LshAssignExpr, RshAssignExpr, UrshAssignExpr, MulAssignExpr, DivAssignExpr,
446 * ModAssignExpr, PowAssignExpr (AssignmentNode)
447 * left: target of assignment
448 * right: value to assign
449 * ConditionalExpr (ConditionalExpression)
450 * (cond ? thenExpr : elseExpr)
451 * kid1: cond
452 * kid2: thenExpr
453 * kid3: elseExpr
454 * CoalesceExpr, OrExpr, AndExpr, BitOrExpr, BitXorExpr,
455 * BitAndExpr, StrictEqExpr, EqExpr, StrictNeExpr, NeExpr, LtExpr, LeExpr,
456 * GtExpr, GeExpr, InstanceOfExpr, InExpr, LshExpr, RshExpr, UrshExpr, AddExpr,
457 * SubExpr, MulExpr, DivExpr, ModExpr, PowExpr (ListNode)
458 * head: list of N subexpressions
459 * All of these operators are left-associative except Pow which is
460 * right-associative, but still forms a list (see comments in
461 * ParseNode::appendOrCreateList).
462 * count: N >= 2
463 * PosExpr, NegExpr, VoidExpr, NotExpr, BitNotExpr, TypeOfNameExpr,
464 * TypeOfExpr (UnaryNode)
465 * kid: unary expr
466 * PreIncrementExpr, PostIncrementExpr, PreDecrementExpr,
467 * PostDecrementExpr (UnaryNode)
468 * kid: member expr
469 * NewExpr (BinaryNode)
470 * left: ctor expression on the left of the '('
471 * right: Arguments
472 * DecoratorList (ListNode)
473 * head: list of N nodes, each item is one of:
474 * * NameNode (DecoratorMemberExpression)
475 * * CallNode (DecoratorCallExpression)
476 * * Node (DecoratorParenthesizedExpression)
477 * count: N > 0
478 * DeleteNameExpr, DeletePropExpr, DeleteElemExpr, DeleteExpr (UnaryNode)
479 * kid: expression that's evaluated, then the overall delete evaluates to
480 * true; can't be a kind for a more-specific ParseNodeKind::Delete*
481 * unless constant folding (or a similar parse tree manipulation) has
482 * occurred
483 * * DeleteNameExpr: Name expr
484 * * DeletePropExpr: Dot expr
485 * * DeleteElemExpr: Elem expr
486 * * DeleteOptionalChainExpr: Member expr
487 * * DeleteExpr: Member expr
488 * DeleteOptionalChainExpr (UnaryNode)
489 * kid: expression that's evaluated, then the overall delete evaluates to
490 * true; If constant folding occurs, Elem expr may become Dot expr.
491 * OptionalElemExpr does not get folded into OptionalDot.
492 * OptionalChain (UnaryNode)
493 * kid: expression that is evaluated as a chain. An Optional chain contains
494 * one or more optional nodes. It's first node (kid) is always an
495 * optional node, for example: an OptionalElemExpr, OptionalDotExpr, or
496 * OptionalCall. An OptionalChain will shortcircuit and return
497 * Undefined without evaluating the rest of the expression if any of the
498 * optional nodes it contains are nullish. An optionalChain also can
499 * contain nodes such as DotExpr, ElemExpr, NameExpr CallExpr, etc.
500 * These are evaluated normally.
501 * * OptionalDotExpr: Dot expr with jump
502 * * OptionalElemExpr: Elem expr with jump
503 * * OptionalCallExpr: Call expr with jump
504 * * DotExpr: Dot expr without jump
505 * * ElemExpr: Elem expr without jump
506 * * CallExpr: Call expr without jump
507 * PropertyNameExpr (NameNode)
508 * atom: property name being accessed
509 * privateNameKind: kind of the name if private
510 * DotExpr (PropertyAccess)
511 * left: Member expr to left of '.'
512 * right: PropertyName to right of '.'
513 * OptionalDotExpr (OptionalPropertyAccess)
514 * left: Member expr to left of '.', short circuits back to OptionalChain
515 * if nullish.
516 * right: PropertyName to right of '.'
517 * ElemExpr (PropertyByValue)
518 * left: Member expr to left of '['
519 * right: expr between '[' and ']'
520 * OptionalElemExpr (OptionalPropertyByValue)
521 * left: Member expr to left of '[', short circuits back to OptionalChain
522 * if nullish.
523 * right: expr between '[' and ']'
524 * CallExpr (BinaryNode)
525 * left: callee expression on the left of the '('
526 * right: Arguments
527 * OptionalCallExpr (BinaryNode)
528 * left: callee expression on the left of the '(', short circuits back to
529 * OptionalChain if nullish.
530 * right: Arguments
531 * Arguments (ListNode)
532 * head: list of arg1, arg2, ... argN
533 * count: N >= 0
534 * ArrayExpr (ListNode)
535 * head: list of N array element expressions
536 * holes ([,,]) are represented by Elision nodes,
537 * spread elements ([...X]) are represented by Spread nodes
538 * count: N >= 0
539 * ObjectExpr (ListNode)
540 * head: list of N nodes, each item is one of:
541 * * MutateProto
542 * * PropertyDefinition
543 * * Shorthand
544 * * Spread
545 * count: N >= 0
546 * PropertyDefinition (PropertyDefinition)
547 * key-value pair in object initializer or destructuring lhs
548 * left: property id
549 * right: value
550 * Shorthand (BinaryNode)
551 * Same fields as PropertyDefinition. This is used for object literal
552 * properties using shorthand ({x}).
553 * ComputedName (UnaryNode)
554 * ES6 ComputedPropertyName.
555 * kid: the AssignmentExpression inside the square brackets
556 * Name (NameNode)
557 * atom: name, or object atom
558 * StringExpr (NameNode)
559 * atom: string
560 * TemplateStringListExpr (ListNode)
561 * head: list of alternating expr and template strings
562 * TemplateString [, expression, TemplateString]+
563 * there's at least one expression. If the template literal contains
564 * no ${}-delimited expression, it's parsed as a single TemplateString
565 * TemplateStringExpr (NameNode)
566 * atom: template string atom
567 * TaggedTemplateExpr (BinaryNode)
568 * left: tag expression
569 * right: Arguments, with the first being the call site object, then
570 * arg1, arg2, ... argN
571 * CallSiteObj (CallSiteNode)
572 * head: an Array of raw TemplateString, then corresponding cooked
573 * TemplateString nodes
574 * Array [, cooked TemplateString]+
575 * where the Array is
576 * [raw TemplateString]+
577 * RegExpExpr (RegExpLiteral)
578 * regexp: RegExp model object
579 * NumberExpr (NumericLiteral)
580 * value: double value of numeric literal
581 * BigIntExpr (BigIntLiteral)
582 * stencil: script compilation struct that has |bigIntData| vector
583 * index: index into the script compilation's |bigIntData| vector
584 * TrueExpr, FalseExpr (BooleanLiteral)
585 * NullExpr (NullLiteral)
586 * RawUndefinedExpr (RawUndefinedLiteral)
587 *
588 * ThisExpr (UnaryNode)
589 * kid: '.this' Name if function `this`, else nullptr
590 * SuperBase (UnaryNode)
591 * kid: '.this' Name
592 * SuperCallExpr (BinaryNode)
593 * left: SuperBase
594 * right: Arguments
595 * SetThis (BinaryNode)
596 * left: '.this' Name
597 * right: SuperCall
598 *
599 * LexicalScope (LexicalScopeNode)
600 * scopeBindings: scope bindings
601 * scopeBody: scope body
602 * Generator (NullaryNode)
603 * InitialYield (UnaryNode)
604 * kid: generator object
605 * YieldExpr, YieldStarExpr, AwaitExpr (UnaryNode)
606 * kid: expr or null
607 */
608
609 #define FOR_EACH_PARSENODE_SUBCLASS(MACRO) \
610 MACRO(BinaryNode) \
611 MACRO(AssignmentNode) \
612 MACRO(CaseClause) \
613 MACRO(ClassMethod) \
614 MACRO(ClassField) \
615 MACRO(StaticClassBlock) \
616 MACRO(PropertyDefinition) \
617 MACRO(ClassNames) \
618 MACRO(ForNode) \
619 MACRO(PropertyAccess) \
620 MACRO(ArgumentsLength) \
621 MACRO(OptionalPropertyAccess) \
622 MACRO(PropertyByValue) \
623 MACRO(OptionalPropertyByValue) \
624 MACRO(PrivateMemberAccess) \
625 MACRO(OptionalPrivateMemberAccess) \
626 MACRO(NewTargetNode) \
627 MACRO(SwitchStatement) \
628 MACRO(DeclarationListNode) \
629 \
630 MACRO(ParamsBodyNode) \
631 MACRO(FunctionNode) \
632 MACRO(ModuleNode) \
633 \
634 MACRO(LexicalScopeNode) \
635 MACRO(ClassBodyScopeNode) \
636 \
637 MACRO(ListNode) \
638 MACRO(CallSiteNode) \
639 MACRO(CallNode) \
640 \
641 MACRO(LoopControlStatement) \
642 MACRO(BreakStatement) \
643 MACRO(ContinueStatement) \
644 \
645 MACRO(NameNode) \
646 MACRO(LabeledStatement) \
647 \
648 MACRO(NullaryNode) \
649 MACRO(BooleanLiteral) \
650 MACRO(DebuggerStatement) \
651 MACRO(NullLiteral) \
652 MACRO(RawUndefinedLiteral) \
653 \
654 MACRO(NumericLiteral) \
655 MACRO(BigIntLiteral) \
656 \
657 MACRO(RegExpLiteral) \
658 \
659 MACRO(TernaryNode) \
660 MACRO(ClassNode) \
661 MACRO(ConditionalExpression) \
662 MACRO(TryNode) \
663 \
664 MACRO(UnaryNode) \
665 MACRO(ThisLiteral)
666
667 #define DECLARE_CLASS(typeName) class typeName;
668 FOR_EACH_PARSENODE_SUBCLASS(DECLARE_CLASS)
669 #undef DECLARE_CLASS
670
671 enum class AccessorType { None, Getter, Setter };
672
673 static inline bool IsConstructorKind(FunctionSyntaxKind kind) {
674 return kind == FunctionSyntaxKind::ClassConstructor ||
675 kind == FunctionSyntaxKind::DerivedClassConstructor;
676 }
677
678 static inline bool IsMethodDefinitionKind(FunctionSyntaxKind kind) {
679 return IsConstructorKind(kind) || kind == FunctionSyntaxKind::Method ||
680 kind == FunctionSyntaxKind::FieldInitializer ||
681 kind == FunctionSyntaxKind::Getter ||
682 kind == FunctionSyntaxKind::Setter;
683 }
684
685 // To help diagnose sporadic crashes in the frontend, a few assertions are
686 // enabled in early beta builds. (Most are not; those still use MOZ_ASSERT.)
687 // See bug 1547561.
688 #if defined(EARLY_BETA_OR_EARLIER)
689 # define JS_PARSE_NODE_ASSERT MOZ_RELEASE_ASSERT
690 #else
691 # define JS_PARSE_NODE_ASSERT MOZ_ASSERT
692 #endif
693
694 class ParseNode;
695 struct ParseNodeError {};
696 using ParseNodeResult = mozilla::Result<ParseNode*, ParseNodeError>;
697
698 class ParseNode {
699 const ParseNodeKind pn_type;
700
701 bool pn_parens : 1; /* this expr was enclosed in parens */
702 bool pn_rhs_anon_fun : 1; /* this expr is anonymous function or class that
703 * is a direct RHS of ParseNodeKind::Assign or
704 * ParseNodeKind::PropertyDefinition of property,
705 * that needs SetFunctionName. */
706
707 protected:
708 // Used by ComputedName to indicate if the ComputedName is a
709 // a synthetic construct. This allows us to avoid needing to
710 // compute ToString on uncommon property values such as BigInt.
711 // Instead we parse as though they were computed names.
712 //
713 // We need this bit to distinguish a synthetic computed name like
714 // this however to undo this transformation in Reflect.parse and
715 // name guessing.
716 bool pn_synthetic_computed : 1;
717
718 ParseNode(const ParseNode& other) = delete;
719 void operator=(const ParseNode& other) = delete;
720
721 public:
722 explicit ParseNode(ParseNodeKind kind)
723 : pn_type(kind),
724 pn_parens(false),
725 pn_rhs_anon_fun(false),
726 pn_synthetic_computed(false),
727 pn_pos(0, 0),
728 pn_next(nullptr) {
729 JS_PARSE_NODE_ASSERT(ParseNodeKind::Start <= kind);
730 JS_PARSE_NODE_ASSERT(kind < ParseNodeKind::Limit);
731 }
732
733 ParseNode(ParseNodeKind kind, const TokenPos& pos)
734 : pn_type(kind),
735 pn_parens(false),
736 pn_rhs_anon_fun(false),
737 pn_synthetic_computed(false),
738 pn_pos(pos),
739 pn_next(nullptr) {
740 JS_PARSE_NODE_ASSERT(ParseNodeKind::Start <= kind);
741 JS_PARSE_NODE_ASSERT(kind < ParseNodeKind::Limit);
742 }
743
744 ParseNodeKind getKind() const {
745 JS_PARSE_NODE_ASSERT(ParseNodeKind::Start <= pn_type);
746 JS_PARSE_NODE_ASSERT(pn_type < ParseNodeKind::Limit);
747 return pn_type;
748 }
749 bool isKind(ParseNodeKind kind) const { return getKind() == kind; }
750
751 protected:
752 size_t getKindAsIndex() const {
753 return size_t(getKind()) - size_t(ParseNodeKind::Start);
754 }
755
756 // Used to implement test() on a few ParseNodes efficiently.
757 // (This enum doesn't fully reflect the ParseNode class hierarchy,
758 // so don't use it for anything else.)
759 enum class TypeCode : uint8_t {
760 Nullary,
761 Unary,
762 Binary,
763 Ternary,
764 List,
765 Name,
766 Other
767 };
768
769 // typeCodeTable[getKindAsIndex()] is the type code of a ParseNode of kind
770 // pnk.
771 static const TypeCode typeCodeTable[];
772
773 private:
774 #ifdef DEBUG
775 static const size_t sizeTable[];
776 #endif
777
778 public:
779 TypeCode typeCode() const { return typeCodeTable[getKindAsIndex()]; }
780
781 bool isBinaryOperation() const {
782 ParseNodeKind kind = getKind();
783 return ParseNodeKind::BinOpFirst <= kind &&
784 kind <= ParseNodeKind::BinOpLast;
785 }
786 inline bool isName(TaggedParserAtomIndex name) const;
787
788 /* Boolean attributes. */
789 bool isInParens() const { return pn_parens; }
790 bool isLikelyIIFE() const { return isInParens(); }
791 void setInParens(bool enabled) { pn_parens = enabled; }
792
793 bool isDirectRHSAnonFunction() const { return pn_rhs_anon_fun; }
794 void setDirectRHSAnonFunction(bool enabled) { pn_rhs_anon_fun = enabled; }
795
796 TokenPos pn_pos; /* two 16-bit pairs here, for 64 bits */
797 ParseNode* pn_next; /* intrinsic link in parent ListNode */
798
799 public:
800 /*
801 * If |left| is a list of the given kind/left-associative op, append
802 * |right| to it and return |left|. Otherwise return a [left, right] list.
803 */
804 static ParseNodeResult appendOrCreateList(ParseNodeKind kind, ParseNode* left,
805 ParseNode* right,
806 FullParseHandler* handler,
807 ParseContext* pc);
808
809 /* True if pn is a parsenode representing a literal constant. */
810 bool isLiteral() const {
811 return isKind(ParseNodeKind::NumberExpr) ||
812 isKind(ParseNodeKind::BigIntExpr) ||
813 isKind(ParseNodeKind::StringExpr) ||
814 isKind(ParseNodeKind::TrueExpr) ||
815 isKind(ParseNodeKind::FalseExpr) ||
816 isKind(ParseNodeKind::NullExpr) ||
817 isKind(ParseNodeKind::RawUndefinedExpr);
818 }
819
820 inline bool isConstant();
821
822 template <class NodeType>
823 inline bool is() const {
824 return NodeType::test(*this);
825 }
826
827 /* Casting operations. */
828 template <class NodeType>
829 inline NodeType& as() {
830 MOZ_ASSERT(NodeType::test(*this));
831 return *static_cast<NodeType*>(this);
832 }
833
834 template <class NodeType>
835 inline const NodeType& as() const {
836 MOZ_ASSERT(NodeType::test(*this));
837 return *static_cast<const NodeType*>(this);
838 }
839
840 #ifdef DEBUG
841 // Debugger-friendly stderr printer.
842 void dump();
843 void dump(const ParserAtomsTable* parserAtoms);
844 void dump(const ParserAtomsTable* parserAtoms, GenericPrinter& out);
845 void dump(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
846 int indent);
847
848 // The size of this node, in bytes.
849 size_t size() const { return sizeTable[getKindAsIndex()]; }
850 #endif
851 };
852
853 // Remove a ParseNode, **pnp, from a parse tree, putting another ParseNode,
854 // *pn, in its place.
855 //
856 // pnp points to a ParseNode pointer. This must be the only pointer that points
857 // to the parse node being replaced. The replacement, *pn, is unchanged except
858 // for its pn_next pointer; updating that is necessary if *pn's new parent is a
859 // list node.
860 inline void ReplaceNode(ParseNode** pnp, ParseNode* pn) {
861 pn->pn_next = (*pnp)->pn_next;
862 *pnp = pn;
863 }
864
865 class NullaryNode : public ParseNode {
866 public:
867 NullaryNode(ParseNodeKind kind, const TokenPos& pos) : ParseNode(kind, pos) {
868 MOZ_ASSERT(is<NullaryNode>());
869 }
870
871 static bool test(const ParseNode& node) {
872 return node.typeCode() == TypeCode::Nullary;
873 }
874
875 static constexpr TypeCode classTypeCode() { return TypeCode::Nullary; }
876
877 template <typename Visitor>
878 bool accept(Visitor& visitor) {
879 return true;
880 }
881
882 #ifdef DEBUG
883 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
884 int indent);
885 #endif
886 };
887
888 class NameNode : public ParseNode {
889 TaggedParserAtomIndex atom_; /* lexical name or label atom */
890 PrivateNameKind privateNameKind_ = PrivateNameKind::None;
891
892 public:
893 NameNode(ParseNodeKind kind, TaggedParserAtomIndex atom, const TokenPos& pos)
894 : ParseNode(kind, pos), atom_(atom) {
895 MOZ_ASSERT(atom);
896 MOZ_ASSERT(is<NameNode>());
897 }
898
899 static bool test(const ParseNode& node) {
900 return node.typeCode() == TypeCode::Name;
901 }
902
903 static constexpr TypeCode classTypeCode() { return TypeCode::Name; }
904
905 template <typename Visitor>
906 bool accept(Visitor& visitor) {
907 return true;
908 }
909
910 #ifdef DEBUG
911 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
912 int indent);
913 #endif
914
915 TaggedParserAtomIndex atom() const { return atom_; }
916
917 TaggedParserAtomIndex name() const {
918 MOZ_ASSERT(isKind(ParseNodeKind::Name) ||
919 isKind(ParseNodeKind::PrivateName));
920 return atom_;
921 }
922
923 void setAtom(TaggedParserAtomIndex atom) { atom_ = atom; }
924
925 void setPrivateNameKind(PrivateNameKind privateNameKind) {
926 privateNameKind_ = privateNameKind;
927 }
928
929 PrivateNameKind privateNameKind() { return privateNameKind_; }
930 };
931
932 inline bool ParseNode::isName(TaggedParserAtomIndex name) const {
933 return getKind() == ParseNodeKind::Name && as<NameNode>().name() == name;
934 }
935
936 class UnaryNode : public ParseNode {
937 ParseNode* kid_;
938
939 public:
940 UnaryNode(ParseNodeKind kind, const TokenPos& pos, ParseNode* kid)
941 : ParseNode(kind, pos), kid_(kid) {
942 MOZ_ASSERT(is<UnaryNode>());
943 }
944
945 static bool test(const ParseNode& node) {
946 return node.typeCode() == TypeCode::Unary;
947 }
948
949 static constexpr TypeCode classTypeCode() { return TypeCode::Unary; }
950
951 template <typename Visitor>
952 bool accept(Visitor& visitor) {
953 if (kid_) {
954 if (!visitor.visit(kid_)) {
955 return false;
956 }
957 }
958 return true;
959 }
960
961 #ifdef DEBUG
962 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
963 int indent);
964 #endif
965
966 ParseNode* kid() const { return kid_; }
967
968 /*
969 * Non-null if this is a statement node which could be a member of a
970 * Directive Prologue: an expression statement consisting of a single
971 * string literal.
972 *
973 * This considers only the node and its children, not its context. After
974 * parsing, check the node's prologue flag to see if it is indeed part of
975 * a directive prologue.
976 *
977 * Note that a Directive Prologue can contain statements that cannot
978 * themselves be directives (string literals that include escape sequences
979 * or escaped newlines, say). This member function returns true for such
980 * nodes; we use it to determine the extent of the prologue.
981 */
982 TaggedParserAtomIndex isStringExprStatement() const {
983 if (isKind(ParseNodeKind::ExpressionStmt)) {
984 if (kid()->isKind(ParseNodeKind::StringExpr) && !kid()->isInParens()) {
985 return kid()->as<NameNode>().atom();
986 }
987 }
988 return TaggedParserAtomIndex::null();
989 }
990
991 // Methods used by FoldConstants.cpp.
992 ParseNode** unsafeKidReference() { return &kid_; }
993
994 void setSyntheticComputedName() { pn_synthetic_computed = true; }
995 bool isSyntheticComputedName() {
996 MOZ_ASSERT(isKind(ParseNodeKind::ComputedName));
997 return pn_synthetic_computed;
998 }
999 };
1000
1001 class BinaryNode : public ParseNode {
1002 ParseNode* left_;
1003 ParseNode* right_;
1004
1005 public:
1006 BinaryNode(ParseNodeKind kind, const TokenPos& pos, ParseNode* left,
1007 ParseNode* right)
1008 : ParseNode(kind, pos), left_(left), right_(right) {
1009 MOZ_ASSERT(is<BinaryNode>());
1010 }
1011
1012 BinaryNode(ParseNodeKind kind, ParseNode* left, ParseNode* right)
1013 : ParseNode(kind, TokenPos::box(left->pn_pos, right->pn_pos)),
1014 left_(left),
1015 right_(right) {
1016 MOZ_ASSERT(is<BinaryNode>());
1017 }
1018
1019 static bool test(const ParseNode& node) {
1020 return node.typeCode() == TypeCode::Binary;
1021 }
1022
1023 static constexpr TypeCode classTypeCode() { return TypeCode::Binary; }
1024
1025 template <typename Visitor>
1026 bool accept(Visitor& visitor) {
1027 if (left_) {
1028 if (!visitor.visit(left_)) {
1029 return false;
1030 }
1031 }
1032 if (right_) {
1033 if (!visitor.visit(right_)) {
1034 return false;
1035 }
1036 }
1037 return true;
1038 }
1039
1040 #ifdef DEBUG
1041 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
1042 int indent);
1043 #endif
1044
1045 ParseNode* left() const { return left_; }
1046
1047 ParseNode* right() const { return right_; }
1048
1049 // Methods used by FoldConstants.cpp.
1050 // callers are responsible for keeping the list consistent.
1051 ParseNode** unsafeLeftReference() { return &left_; }
1052
1053 ParseNode** unsafeRightReference() { return &right_; }
1054 };
1055
1056 class AssignmentNode : public BinaryNode {
1057 public:
1058 AssignmentNode(ParseNodeKind kind, ParseNode* left, ParseNode* right)
1059 : BinaryNode(kind, TokenPos(left->pn_pos.begin, right->pn_pos.end), left,
1060 right) {
1061 MOZ_ASSERT(is<AssignmentNode>());
1062 }
1063
1064 static bool test(const ParseNode& node) {
1065 ParseNodeKind kind = node.getKind();
1066 bool match = ParseNodeKind::AssignmentStart <= kind &&
1067 kind <= ParseNodeKind::AssignmentLast;
1068 MOZ_ASSERT_IF(match, node.is<BinaryNode>());
1069 return match;
1070 }
1071 };
1072
1073 class ForNode : public BinaryNode {
1074 unsigned iflags_; /* JSITER_* flags */
1075
1076 public:
1077 ForNode(const TokenPos& pos, ParseNode* forHead, ParseNode* body,
1078 unsigned iflags)
1079 : BinaryNode(ParseNodeKind::ForStmt, pos, forHead, body),
1080 iflags_(iflags) {
1081 MOZ_ASSERT(forHead->isKind(ParseNodeKind::ForIn) ||
1082 forHead->isKind(ParseNodeKind::ForOf) ||
1083 forHead->isKind(ParseNodeKind::ForHead));
1084 }
1085
1086 static bool test(const ParseNode& node) {
1087 bool match = node.isKind(ParseNodeKind::ForStmt);
1088 MOZ_ASSERT_IF(match, node.is<BinaryNode>());
1089 return match;
1090 }
1091
1092 TernaryNode* head() const { return &left()->as<TernaryNode>(); }
1093
1094 ParseNode* body() const { return right(); }
1095
1096 unsigned iflags() const { return iflags_; }
1097 };
1098
1099 class TernaryNode : public ParseNode {
1100 ParseNode* kid1_; /* condition, discriminant, etc. */
1101 ParseNode* kid2_; /* then-part, case list, etc. */
1102 ParseNode* kid3_; /* else-part, default case, etc. */
1103
1104 public:
1105 TernaryNode(ParseNodeKind kind, ParseNode* kid1, ParseNode* kid2,
1106 ParseNode* kid3)
1107 : TernaryNode(kind, kid1, kid2, kid3,
1108 TokenPos((kid1 ? kid1
1109 : kid2 ? kid2
1110 : kid3)
1111 ->pn_pos.begin,
1112 (kid3 ? kid3
1113 : kid2 ? kid2
1114 : kid1)
1115 ->pn_pos.end)) {}
1116
1117 TernaryNode(ParseNodeKind kind, ParseNode* kid1, ParseNode* kid2,
1118 ParseNode* kid3, const TokenPos& pos)
1119 : ParseNode(kind, pos), kid1_(kid1), kid2_(kid2), kid3_(kid3) {
1120 MOZ_ASSERT(is<TernaryNode>());
1121 }
1122
1123 static bool test(const ParseNode& node) {
1124 return node.typeCode() == TypeCode::Ternary;
1125 }
1126
1127 static constexpr TypeCode classTypeCode() { return TypeCode::Ternary; }
1128
1129 template <typename Visitor>
1130 bool accept(Visitor& visitor) {
1131 if (kid1_) {
1132 if (!visitor.visit(kid1_)) {
1133 return false;
1134 }
1135 }
1136 if (kid2_) {
1137 if (!visitor.visit(kid2_)) {
1138 return false;
1139 }
1140 }
1141 if (kid3_) {
1142 if (!visitor.visit(kid3_)) {
1143 return false;
1144 }
1145 }
1146 return true;
1147 }
1148
1149 #ifdef DEBUG
1150 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
1151 int indent);
1152 #endif
1153
1154 ParseNode* kid1() const { return kid1_; }
1155
1156 ParseNode* kid2() const { return kid2_; }
1157
1158 ParseNode* kid3() const { return kid3_; }
1159
1160 // Methods used by FoldConstants.cpp.
1161 ParseNode** unsafeKid1Reference() { return &kid1_; }
1162
1163 ParseNode** unsafeKid2Reference() { return &kid2_; }
1164
1165 ParseNode** unsafeKid3Reference() { return &kid3_; }
1166 };
1167
1168 class ListNode : public ParseNode {
1169 ParseNode* head_; /* first node in list */
1170 ParseNode** tail_; /* ptr to last node's pn_next in list */
1171 uint32_t count_; /* number of nodes in list */
1172 uint32_t xflags;
1173
1174 private:
1175 // xflags bits.
1176
1177 // Statement list has top-level function statements.
1178 static constexpr uint32_t hasTopLevelFunctionDeclarationsBit = Bit(0);
1179
1180 // Array/Object/Class initializer has non-constants.
1181 // * array has holes
1182 // * array has spread node
1183 // * array has element which is known not to be constant
1184 // * array has no element
1185 // * object/class has __proto__
1186 // * object/class has property which is known not to be constant
1187 // * object/class shorthand property
1188 // * object/class spread property
1189 // * object/class has method
1190 // * object/class has computed property
1191 static constexpr uint32_t hasNonConstInitializerBit = Bit(1);
1192
1193 // Flag set by the emitter after emitting top-level function statements.
1194 static constexpr uint32_t emittedTopLevelFunctionDeclarationsBit = Bit(2);
1195
1196 public:
1197 ListNode(ParseNodeKind kind, const TokenPos& pos)
1198 : ParseNode(kind, pos),
1199 head_(nullptr),
1200 tail_(&head_),
1201 count_(0),
1202 xflags(0) {
1203 MOZ_ASSERT(is<ListNode>());
1204 }
1205
1206 ListNode(ParseNodeKind kind, ParseNode* kid)
1207 : ParseNode(kind, kid->pn_pos),
1208 head_(kid),
1209 tail_(&kid->pn_next),
1210 count_(1),
1211 xflags(0) {
1212 if (kid->pn_pos.begin < pn_pos.begin) {
1213 pn_pos.begin = kid->pn_pos.begin;
1214 }
1215 pn_pos.end = kid->pn_pos.end;
1216
1217 MOZ_ASSERT(is<ListNode>());
1218 }
1219
1220 static bool test(const ParseNode& node) {
1221 return node.typeCode() == TypeCode::List;
1222 }
1223
1224 static constexpr TypeCode classTypeCode() { return TypeCode::List; }
1225
1226 template <typename Visitor>
1227 bool accept(Visitor& visitor) {
1228 ParseNode** listp = &head_;
1229 for (; *listp; listp = &(*listp)->pn_next) {
1230 // Don't use reference because we want to check if it changed, so we can
1231 // use ReplaceNode
1232 ParseNode* pn = *listp;
1233 if (!visitor.visit(pn)) {
1234 return false;
1235 }
1236 if (pn != *listp) {
1237 ReplaceNode(listp, pn);
1238 }
1239 }
1240 unsafeReplaceTail(listp);
1241 return true;
1242 }
1243
1244 #ifdef DEBUG
1245 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
1246 int indent);
1247 #endif
1248
1249 ParseNode* head() const { return head_; }
1250
1251 ParseNode** tail() const { return tail_; }
1252
1253 uint32_t count() const { return count_; }
1254
1255 bool empty() const { return count() == 0; }
1256
1257 void checkConsistency() const
1258 #ifndef DEBUG
1259 {}
1260 #endif
1261 ;
1262
1263 [[nodiscard]] bool hasTopLevelFunctionDeclarations() const {
1264 MOZ_ASSERT(isKind(ParseNodeKind::StatementList));
1265 return xflags & hasTopLevelFunctionDeclarationsBit;
1266 }
1267
1268 [[nodiscard]] bool emittedTopLevelFunctionDeclarations() const {
1269 MOZ_ASSERT(isKind(ParseNodeKind::StatementList));
1270 MOZ_ASSERT(hasTopLevelFunctionDeclarations());
1271 return xflags & emittedTopLevelFunctionDeclarationsBit;
1272 }
1273
1274 [[nodiscard]] bool hasNonConstInitializer() const {
1275 MOZ_ASSERT(isKind(ParseNodeKind::ArrayExpr) ||
1276 isKind(ParseNodeKind::ObjectExpr));
1277 return xflags & hasNonConstInitializerBit;
1278 }
1279
1280 void setHasTopLevelFunctionDeclarations() {
1281 MOZ_ASSERT(isKind(ParseNodeKind::StatementList));
1282 xflags |= hasTopLevelFunctionDeclarationsBit;
1283 }
1284
1285 void setEmittedTopLevelFunctionDeclarations() {
1286 MOZ_ASSERT(isKind(ParseNodeKind::StatementList));
1287 MOZ_ASSERT(hasTopLevelFunctionDeclarations());
1288 xflags |= emittedTopLevelFunctionDeclarationsBit;
1289 }
1290
1291 void setHasNonConstInitializer() {
1292 MOZ_ASSERT(isKind(ParseNodeKind::ArrayExpr) ||
1293 isKind(ParseNodeKind::ObjectExpr));
1294 xflags |= hasNonConstInitializerBit;
1295 }
1296
1297 void unsetHasNonConstInitializer() {
1298 MOZ_ASSERT(isKind(ParseNodeKind::ArrayExpr) ||
1299 isKind(ParseNodeKind::ObjectExpr));
1300 xflags &= ~hasNonConstInitializerBit;
1301 }
1302
1303 /*
1304 * Compute a pointer to the last element in a singly-linked list. NB: list
1305 * must be non-empty -- this is asserted!
1306 */
1307 ParseNode* last() const {
1308 MOZ_ASSERT(!empty());
1309 //
1310 // ParseNode ParseNode
1311 // +-----+---------+-----+ +-----+---------+-----+
1312 // | ... | pn_next | ... | +-...->| ... | pn_next | ... |
1313 // +-----+---------+-----+ | +-----+---------+-----+
1314 // ^ | | ^ ^
1315 // | +---------------+ | |
1316 // | | tail()
1317 // | |
1318 // head() last()
1319 //
1320 return (ParseNode*)(uintptr_t(tail()) - offsetof(ParseNode, pn_next));
1321 }
1322
1323 void replaceLast(ParseNode* node) {
1324 MOZ_ASSERT(!empty());
1325 pn_pos.end = node->pn_pos.end;
1326
1327 ParseNode* item = head();
1328 ParseNode* lastNode = last();
1329 MOZ_ASSERT(item);
1330 if (item == lastNode) {
1331 head_ = node;
1332 } else {
1333 while (item->pn_next != lastNode) {
1334 MOZ_ASSERT(item->pn_next);
1335 item = item->pn_next;
1336 }
1337 item->pn_next = node;
1338 }
1339 tail_ = &node->pn_next;
1340 }
1341
1342 void append(ParseNode* item) {
1343 MOZ_ASSERT(item->pn_pos.begin >= pn_pos.begin);
1344 pn_pos.end = item->pn_pos.end;
1345 *tail_ = item;
1346 tail_ = &item->pn_next;
1347 count_++;
1348 }
1349
1350 void prepend(ParseNode* item) {
1351 item->pn_next = head_;
1352 head_ = item;
1353 if (tail_ == &head_) {
1354 tail_ = &item->pn_next;
1355 }
1356 count_++;
1357 }
1358
1359 // Methods used by FoldConstants.cpp.
1360 // Caller is responsible for keeping the list consistent.
1361 ParseNode** unsafeHeadReference() { return &head_; }
1362
1363 void unsafeReplaceTail(ParseNode** newTail) {
1364 tail_ = newTail;
1365 checkConsistency();
1366 }
1367
1368 void unsafeDecrementCount() {
1369 MOZ_ASSERT(count() > 1);
1370 count_--;
1371 }
1372
1373 private:
1374 // Classes to iterate over ListNode contents:
1375 //
1376 // Usage:
1377 // ListNode* list;
1378 // for (ParseNode* item : list->contents()) {
1379 // // item is ParseNode* typed.
1380 // }
1381 class iterator {
1382 private:
1383 ParseNode* node_;
1384
1385 friend class ListNode;
1386 explicit iterator(ParseNode* node) : node_(node) {}
1387
1388 public:
1389 // Implement std::iterator_traits.
1390 using iterator_category = std::input_iterator_tag;
1391 using value_type = ParseNode*;
1392 using difference_type = ptrdiff_t;
1393 using pointer = ParseNode**;
1394 using reference = ParseNode*&;
1395
1396 bool operator==(const iterator& other) const {
1397 return node_ == other.node_;
1398 }
1399
1400 bool operator!=(const iterator& other) const { return !(*this == other); }
1401
1402 iterator& operator++() {
1403 node_ = node_->pn_next;
1404 return *this;
1405 }
1406
1407 ParseNode* operator*() { return node_; }
1408
1409 const ParseNode* operator*() const { return node_; }
1410 };
1411
1412 class range {
1413 private:
1414 ParseNode* begin_;
1415 ParseNode* end_;
1416
1417 friend class ListNode;
1418 range(ParseNode* begin, ParseNode* end) : begin_(begin), end_(end) {}
1419
1420 public:
1421 iterator begin() { return iterator(begin_); }
1422
1423 iterator end() { return iterator(end_); }
1424
1425 const iterator begin() const { return iterator(begin_); }
1426
1427 const iterator end() const { return iterator(end_); }
1428
1429 const iterator cbegin() const { return begin(); }
1430
1431 const iterator cend() const { return end(); }
1432 };
1433
1434 #ifdef DEBUG
1435 [[nodiscard]] bool contains(ParseNode* target) const {
1436 MOZ_ASSERT(target);
1437 for (ParseNode* node : contents()) {
1438 if (target == node) {
1439 return true;
1440 }
1441 }
1442 return false;
1443 }
1444 #endif
1445
1446 public:
1447 range contents() { return range(head(), nullptr); }
1448
1449 const range contents() const { return range(head(), nullptr); }
1450
1451 range contentsFrom(ParseNode* begin) {
1452 MOZ_ASSERT_IF(begin, contains(begin));
1453 return range(begin, nullptr);
1454 }
1455
1456 const range contentsFrom(ParseNode* begin) const {
1457 MOZ_ASSERT_IF(begin, contains(begin));
1458 return range(begin, nullptr);
1459 }
1460
1461 range contentsTo(ParseNode* end) {
1462 MOZ_ASSERT_IF(end, contains(end));
1463 return range(head(), end);
1464 }
1465
1466 const range contentsTo(ParseNode* end) const {
1467 MOZ_ASSERT_IF(end, contains(end));
1468 return range(head(), end);
1469 }
1470 };
1471
1472 class DeclarationListNode : public ListNode {
1473 public:
1474 DeclarationListNode(ParseNodeKind kind, const TokenPos& pos)
1475 : ListNode(kind, pos) {
1476 MOZ_ASSERT(is<DeclarationListNode>());
1477 }
1478
1479 static bool test(const ParseNode& node) {
1480 bool match = node.isKind(ParseNodeKind::VarStmt) ||
1481 node.isKind(ParseNodeKind::LetDecl) ||
1482 #ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT
1483 node.isKind(ParseNodeKind::UsingDecl) ||
1484 node.isKind(ParseNodeKind::AwaitUsingDecl) ||
1485 #endif
1486 node.isKind(ParseNodeKind::ConstDecl);
1487 MOZ_ASSERT_IF(match, node.is<ListNode>());
1488 return match;
1489 }
1490
1491 auto* singleBinding() const {
1492 MOZ_ASSERT(count() == 1);
1493 return head();
1494 }
1495 };
1496
1497 class ParamsBodyNode : public ListNode {
1498 public:
1499 explicit ParamsBodyNode(const TokenPos& pos)
1500 : ListNode(ParseNodeKind::ParamsBody, pos) {
1501 MOZ_ASSERT(is<ParamsBodyNode>());
1502 }
1503
1504 static bool test(const ParseNode& node) {
1505 bool match = node.isKind(ParseNodeKind::ParamsBody);
1506 MOZ_ASSERT_IF(match, node.is<ListNode>());
1507 return match;
1508 }
1509
1510 auto parameters() const {
1511 MOZ_ASSERT(last()->is<LexicalScopeNode>());
1512 return contentsTo(last());
1513 }
1514
1515 auto* body() const {
1516 MOZ_ASSERT(last()->is<LexicalScopeNode>());
1517 return &last()->as<LexicalScopeNode>();
1518 }
1519 };
1520
1521 class FunctionNode : public ParseNode {
1522 FunctionBox* funbox_;
1523 ParseNode* body_;
1524 FunctionSyntaxKind syntaxKind_;
1525
1526 public:
1527 FunctionNode(FunctionSyntaxKind syntaxKind, const TokenPos& pos)
1528 : ParseNode(ParseNodeKind::Function, pos),
1529 funbox_(nullptr),
1530 body_(nullptr),
1531 syntaxKind_(syntaxKind) {
1532 MOZ_ASSERT(!body_);
1533 MOZ_ASSERT(!funbox_);
1534 MOZ_ASSERT(is<FunctionNode>());
1535 }
1536
1537 static bool test(const ParseNode& node) {
1538 return node.isKind(ParseNodeKind::Function);
1539 }
1540
1541 static constexpr TypeCode classTypeCode() { return TypeCode::Other; }
1542
1543 template <typename Visitor>
1544 bool accept(Visitor& visitor) {
1545 // Note: body is null for lazily-parsed functions.
1546 if (body_) {
1547 if (!visitor.visit(body_)) {
1548 return false;
1549 }
1550 MOZ_ASSERT(body_->is<ParamsBodyNode>());
1551 }
1552 return true;
1553 }
1554
1555 #ifdef DEBUG
1556 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
1557 int indent);
1558 #endif
1559
1560 FunctionBox* funbox() const { return funbox_; }
1561
1562 ParamsBodyNode* body() const {
1563 return body_ ? &body_->as<ParamsBodyNode>() : nullptr;
1564 }
1565
1566 void setFunbox(FunctionBox* funbox) { funbox_ = funbox; }
1567
1568 void setBody(ParamsBodyNode* body) { body_ = body; }
1569
1570 FunctionSyntaxKind syntaxKind() const { return syntaxKind_; }
1571
1572 bool functionIsHoisted() const {
1573 return syntaxKind() == FunctionSyntaxKind::Statement;
1574 }
1575 };
1576
1577 class ModuleNode : public ParseNode {
1578 ParseNode* body_;
1579
1580 public:
1581 explicit ModuleNode(const TokenPos& pos)
1582 : ParseNode(ParseNodeKind::Module, pos), body_(nullptr) {
1583 MOZ_ASSERT(!body_);
1584 MOZ_ASSERT(is<ModuleNode>());
1585 }
1586
1587 static bool test(const ParseNode& node) {
1588 return node.isKind(ParseNodeKind::Module);
1589 }
1590
1591 static constexpr TypeCode classTypeCode() { return TypeCode::Other; }
1592
1593 template <typename Visitor>
1594 bool accept(Visitor& visitor) {
1595 return visitor.visit(body_);
1596 }
1597
1598 #ifdef DEBUG
1599 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
1600 int indent);
1601 #endif
1602
1603 ListNode* body() const { return &body_->as<ListNode>(); }
1604
1605 void setBody(ListNode* body) { body_ = body; }
1606 };
1607
1608 class NumericLiteral : public ParseNode {
1609 double value_; /* aligned numeric literal value */
1610 DecimalPoint decimalPoint_; /* Whether the number has a decimal point */
1611
1612 public:
1613 NumericLiteral(double value, DecimalPoint decimalPoint, const TokenPos& pos)
1614 : ParseNode(ParseNodeKind::NumberExpr, pos),
1615 value_(value),
1616 decimalPoint_(decimalPoint) {}
1617
1618 static bool test(const ParseNode& node) {
1619 return node.isKind(ParseNodeKind::NumberExpr);
1620 }
1621
1622 static constexpr TypeCode classTypeCode() { return TypeCode::Other; }
1623
1624 template <typename Visitor>
1625 bool accept(Visitor& visitor) {
1626 return true;
1627 }
1628
1629 #ifdef DEBUG
1630 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
1631 int indent);
1632 #endif
1633
1634 double value() const { return value_; }
1635
1636 DecimalPoint decimalPoint() const { return decimalPoint_; }
1637
1638 // Return the decimal string representation of this numeric literal.
1639 TaggedParserAtomIndex toAtom(FrontendContext* fc,
1640 ParserAtomsTable& parserAtoms) const;
1641 };
1642
1643 class BigIntLiteral : public ParseNode {
1644 BigIntIndex index_;
1645
1646 public:
1647 BigIntLiteral(BigIntIndex index, const TokenPos& pos)
1648 : ParseNode(ParseNodeKind::BigIntExpr, pos), index_(index) {}
1649
1650 static bool test(const ParseNode& node) {
1651 return node.isKind(ParseNodeKind::BigIntExpr);
1652 }
1653
1654 static constexpr TypeCode classTypeCode() { return TypeCode::Other; }
1655
1656 template <typename Visitor>
1657 bool accept(Visitor& visitor) {
1658 return true;
1659 }
1660
1661 #ifdef DEBUG
1662 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
1663 int indent);
1664 #endif
1665
1666 BigIntIndex index() { return index_; }
1667 };
1668
1669 template <ParseNodeKind NodeKind, typename ScopeType>
1670 class BaseScopeNode : public ParseNode {
1671 using ParserData = typename ScopeType::ParserData;
1672 ParserData* bindings;
1673 ParseNode* body;
1674 ScopeKind kind_;
1675
1676 public:
1677 BaseScopeNode(ParserData* bindings, ParseNode* body,
1678 ScopeKind kind = ScopeKind::Lexical)
1679 : ParseNode(NodeKind, body->pn_pos),
1680 bindings(bindings),
1681 body(body),
1682 kind_(kind) {}
1683
1684 static bool test(const ParseNode& node) { return node.isKind(NodeKind); }
1685
1686 static constexpr TypeCode classTypeCode() { return TypeCode::Other; }
1687
1688 template <typename Visitor>
1689 bool accept(Visitor& visitor) {
1690 return visitor.visit(body);
1691 }
1692
1693 #ifdef DEBUG
1694 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
1695 int indent);
1696 #endif
1697
1698 ParserData* scopeBindings() const {
1699 MOZ_ASSERT(!isEmptyScope());
1700 return bindings;
1701 }
1702
1703 ParseNode* scopeBody() const { return body; }
1704
1705 void setScopeBody(ParseNode* body) { this->body = body; }
1706
1707 bool isEmptyScope() const { return !bindings; }
1708
1709 ScopeKind kind() const { return kind_; }
1710 };
1711
1712 class LexicalScopeNode
1713 : public BaseScopeNode<ParseNodeKind::LexicalScope, LexicalScope> {
1714 public:
1715 LexicalScopeNode(LexicalScope::ParserData* bindings, ParseNode* body,
1716 ScopeKind kind = ScopeKind::Lexical)
1717 : BaseScopeNode(bindings, body, kind) {}
1718 };
1719
1720 class ClassBodyScopeNode
1721 : public BaseScopeNode<ParseNodeKind::ClassBodyScope, ClassBodyScope> {
1722 public:
1723 ClassBodyScopeNode(ClassBodyScope::ParserData* bindings, ListNode* memberList)
1724 : BaseScopeNode(bindings, memberList, ScopeKind::ClassBody) {
1725 MOZ_ASSERT(memberList->isKind(ParseNodeKind::ClassMemberList));
1726 }
1727
1728 ListNode* memberList() const {
1729 ListNode* list = &scopeBody()->as<ListNode>();
1730 MOZ_ASSERT(list->isKind(ParseNodeKind::ClassMemberList));
1731 return list;
1732 }
1733 };
1734
1735 class LabeledStatement : public NameNode {
1736 ParseNode* statement_;
1737
1738 public:
1739 LabeledStatement(TaggedParserAtomIndex label, ParseNode* stmt, uint32_t begin)
1740 : NameNode(ParseNodeKind::LabelStmt, label,
1741 TokenPos(begin, stmt->pn_pos.end)),
1742 statement_(stmt) {}
1743
1744 TaggedParserAtomIndex label() const { return atom(); }
1745
1746 ParseNode* statement() const { return statement_; }
1747
1748 static bool test(const ParseNode& node) {
1749 return node.isKind(ParseNodeKind::LabelStmt);
1750 }
1751
1752 template <typename Visitor>
1753 bool accept(Visitor& visitor) {
1754 if (statement_) {
1755 if (!visitor.visit(statement_)) {
1756 return false;
1757 }
1758 }
1759 return true;
1760 }
1761
1762 #ifdef DEBUG
1763 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
1764 int indent);
1765 #endif
1766 };
1767
1768 // Inside a switch statement, a CaseClause is a case-label and the subsequent
1769 // statements. The same node type is used for DefaultClauses. The only
1770 // difference is that their caseExpression() is null.
1771 class CaseClause : public BinaryNode {
1772 public:
1773 CaseClause(ParseNode* expr, ParseNode* stmts, uint32_t begin)
1774 : BinaryNode(ParseNodeKind::Case, TokenPos(begin, stmts->pn_pos.end),
1775 expr, stmts) {}
1776
1777 ParseNode* caseExpression() const { return left(); }
1778
1779 bool isDefault() const { return !caseExpression(); }
1780
1781 ListNode* statementList() const { return &right()->as<ListNode>(); }
1782
1783 static bool test(const ParseNode& node) {
1784 bool match = node.isKind(ParseNodeKind::Case);
1785 MOZ_ASSERT_IF(match, node.is<BinaryNode>());
1786 return match;
1787 }
1788 };
1789
1790 class LoopControlStatement : public ParseNode {
1791 TaggedParserAtomIndex label_; /* target of break/continue statement */
1792
1793 protected:
1794 LoopControlStatement(ParseNodeKind kind, TaggedParserAtomIndex label,
1795 const TokenPos& pos)
1796 : ParseNode(kind, pos), label_(label) {
1797 MOZ_ASSERT(kind == ParseNodeKind::BreakStmt ||
1798 kind == ParseNodeKind::ContinueStmt);
1799 MOZ_ASSERT(is<LoopControlStatement>());
1800 }
1801
1802 public:
1803 /* Label associated with this break/continue statement, if any. */
1804 TaggedParserAtomIndex label() const { return label_; }
1805
1806 #ifdef DEBUG
1807 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
1808 int indent);
1809 #endif
1810
1811 static bool test(const ParseNode& node) {
1812 return node.isKind(ParseNodeKind::BreakStmt) ||
1813 node.isKind(ParseNodeKind::ContinueStmt);
1814 }
1815
1816 static constexpr TypeCode classTypeCode() { return TypeCode::Other; }
1817
1818 template <typename Visitor>
1819 bool accept(Visitor& visitor) {
1820 return true;
1821 }
1822 };
1823
1824 class BreakStatement : public LoopControlStatement {
1825 public:
1826 BreakStatement(TaggedParserAtomIndex label, const TokenPos& pos)
1827 : LoopControlStatement(ParseNodeKind::BreakStmt, label, pos) {}
1828
1829 static bool test(const ParseNode& node) {
1830 bool match = node.isKind(ParseNodeKind::BreakStmt);
1831 MOZ_ASSERT_IF(match, node.is<LoopControlStatement>());
1832 return match;
1833 }
1834 };
1835
1836 class ContinueStatement : public LoopControlStatement {
1837 public:
1838 ContinueStatement(TaggedParserAtomIndex label, const TokenPos& pos)
1839 : LoopControlStatement(ParseNodeKind::ContinueStmt, label, pos) {}
1840
1841 static bool test(const ParseNode& node) {
1842 bool match = node.isKind(ParseNodeKind::ContinueStmt);
1843 MOZ_ASSERT_IF(match, node.is<LoopControlStatement>());
1844 return match;
1845 }
1846 };
1847
1848 class DebuggerStatement : public NullaryNode {
1849 public:
1850 explicit DebuggerStatement(const TokenPos& pos)
1851 : NullaryNode(ParseNodeKind::DebuggerStmt, pos) {}
1852
1853 static bool test(const ParseNode& node) {
1854 bool match = node.isKind(ParseNodeKind::DebuggerStmt);
1855 MOZ_ASSERT_IF(match, node.is<NullaryNode>());
1856 return match;
1857 }
1858 };
1859
1860 class ConditionalExpression : public TernaryNode {
1861 public:
1862 ConditionalExpression(ParseNode* condition, ParseNode* thenExpr,
1863 ParseNode* elseExpr)
1864 : TernaryNode(ParseNodeKind::ConditionalExpr, condition, thenExpr,
1865 elseExpr,
1866 TokenPos(condition->pn_pos.begin, elseExpr->pn_pos.end)) {
1867 MOZ_ASSERT(condition);
1868 MOZ_ASSERT(thenExpr);
1869 MOZ_ASSERT(elseExpr);
1870 }
1871
1872 ParseNode& condition() const { return *kid1(); }
1873
1874 ParseNode& thenExpression() const { return *kid2(); }
1875
1876 ParseNode& elseExpression() const { return *kid3(); }
1877
1878 static bool test(const ParseNode& node) {
1879 bool match = node.isKind(ParseNodeKind::ConditionalExpr);
1880 MOZ_ASSERT_IF(match, node.is<TernaryNode>());
1881 return match;
1882 }
1883 };
1884
1885 class TryNode : public TernaryNode {
1886 public:
1887 TryNode(uint32_t begin, ParseNode* body, LexicalScopeNode* catchScope,
1888 ParseNode* finallyBlock)
1889 : TernaryNode(
1890 ParseNodeKind::TryStmt, body, catchScope, finallyBlock,
1891 TokenPos(begin,
1892 (finallyBlock ? finallyBlock : catchScope)->pn_pos.end)) {
1893 MOZ_ASSERT(body);
1894 MOZ_ASSERT(catchScope || finallyBlock);
1895 }
1896
1897 static bool test(const ParseNode& node) {
1898 bool match = node.isKind(ParseNodeKind::TryStmt);
1899 MOZ_ASSERT_IF(match, node.is<TernaryNode>());
1900 return match;
1901 }
1902
1903 ParseNode* body() const { return kid1(); }
1904
1905 LexicalScopeNode* catchScope() const {
1906 return kid2() ? &kid2()->as<LexicalScopeNode>() : nullptr;
1907 }
1908
1909 ParseNode* finallyBlock() const { return kid3(); }
1910 };
1911
1912 class ThisLiteral : public UnaryNode {
1913 public:
1914 ThisLiteral(const TokenPos& pos, ParseNode* thisName)
1915 : UnaryNode(ParseNodeKind::ThisExpr, pos, thisName) {}
1916
1917 static bool test(const ParseNode& node) {
1918 bool match = node.isKind(ParseNodeKind::ThisExpr);
1919 MOZ_ASSERT_IF(match, node.is<UnaryNode>());
1920 return match;
1921 }
1922 };
1923
1924 class NullLiteral : public NullaryNode {
1925 public:
1926 explicit NullLiteral(const TokenPos& pos)
1927 : NullaryNode(ParseNodeKind::NullExpr, pos) {}
1928
1929 static bool test(const ParseNode& node) {
1930 bool match = node.isKind(ParseNodeKind::NullExpr);
1931 MOZ_ASSERT_IF(match, node.is<NullaryNode>());
1932 return match;
1933 }
1934 };
1935
1936 // This is only used internally, currently just for tagged templates and the
1937 // initial value of fields without initializers. It represents the value
1938 // 'undefined' (aka `void 0`), like NullLiteral represents the value 'null'.
1939 class RawUndefinedLiteral : public NullaryNode {
1940 public:
1941 explicit RawUndefinedLiteral(const TokenPos& pos)
1942 : NullaryNode(ParseNodeKind::RawUndefinedExpr, pos) {}
1943
1944 static bool test(const ParseNode& node) {
1945 bool match = node.isKind(ParseNodeKind::RawUndefinedExpr);
1946 MOZ_ASSERT_IF(match, node.is<NullaryNode>());
1947 return match;
1948 }
1949 };
1950
1951 class BooleanLiteral : public NullaryNode {
1952 public:
1953 BooleanLiteral(bool b, const TokenPos& pos)
1954 : NullaryNode(b ? ParseNodeKind::TrueExpr : ParseNodeKind::FalseExpr,
1955 pos) {}
1956
1957 static bool test(const ParseNode& node) {
1958 bool match = node.isKind(ParseNodeKind::TrueExpr) ||
1959 node.isKind(ParseNodeKind::FalseExpr);
1960 MOZ_ASSERT_IF(match, node.is<NullaryNode>());
1961 return match;
1962 }
1963 };
1964
1965 class RegExpLiteral : public ParseNode {
1966 RegExpIndex index_;
1967
1968 public:
1969 RegExpLiteral(RegExpIndex dataIndex, const TokenPos& pos)
1970 : ParseNode(ParseNodeKind::RegExpExpr, pos), index_(dataIndex) {}
1971
1972 // Create a RegExp object of this RegExp literal.
1973 RegExpObject* create(JSContext* cx, FrontendContext* fc,
1974 ParserAtomsTable& parserAtoms,
1975 CompilationAtomCache& atomCache,
1976 ExtensibleCompilationStencil& stencil) const;
1977
1978 #ifdef DEBUG
1979 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
1980 int indent);
1981 #endif
1982
1983 static bool test(const ParseNode& node) {
1984 return node.isKind(ParseNodeKind::RegExpExpr);
1985 }
1986
1987 static constexpr TypeCode classTypeCode() { return TypeCode::Other; }
1988
1989 template <typename Visitor>
1990 bool accept(Visitor& visitor) {
1991 return true;
1992 }
1993
1994 RegExpIndex index() { return index_; }
1995 };
1996
1997 class PropertyAccessBase : public BinaryNode {
1998 public:
1999 /*
2000 * PropertyAccess nodes can have any expression/'super' as left-hand
2001 * side, but the name must be a ParseNodeKind::PropertyName node.
2002 */
2003 PropertyAccessBase(ParseNodeKind kind, ParseNode* lhs, NameNode* name,
2004 uint32_t begin, uint32_t end)
2005 : BinaryNode(kind, TokenPos(begin, end), lhs, name) {
2006 MOZ_ASSERT(lhs);
2007 MOZ_ASSERT(name);
2008 }
2009
2010 ParseNode& expression() const { return *left(); }
2011
2012 static bool test(const ParseNode& node) {
2013 bool match = node.isKind(ParseNodeKind::DotExpr) ||
2014 node.isKind(ParseNodeKind::OptionalDotExpr) ||
2015 node.isKind(ParseNodeKind::ArgumentsLength);
2016 MOZ_ASSERT_IF(match, node.is<BinaryNode>());
2017 MOZ_ASSERT_IF(match, node.as<BinaryNode>().right()->isKind(
2018 ParseNodeKind::PropertyNameExpr));
2019 return match;
2020 }
2021
2022 NameNode& key() const { return right()->as<NameNode>(); }
2023
2024 // Method used by BytecodeEmitter::emitPropLHS for optimization.
2025 // Those methods allow expression to temporarily be nullptr for
2026 // optimization purpose.
2027 ParseNode* maybeExpression() const { return left(); }
2028
2029 void setExpression(ParseNode* pn) { *unsafeLeftReference() = pn; }
2030
2031 TaggedParserAtomIndex name() const { return right()->as<NameNode>().atom(); }
2032 };
2033
2034 class PropertyAccess : public PropertyAccessBase {
2035 public:
2036 PropertyAccess(ParseNode* lhs, NameNode* name, uint32_t begin, uint32_t end)
2037 : PropertyAccessBase(ParseNodeKind::DotExpr, lhs, name, begin, end) {
2038 MOZ_ASSERT(lhs);
2039 MOZ_ASSERT(name);
2040 }
2041
2042 static bool test(const ParseNode& node) {
2043 bool match = node.isKind(ParseNodeKind::DotExpr) ||
2044 node.isKind(ParseNodeKind::ArgumentsLength);
2045 MOZ_ASSERT_IF(match, node.is<PropertyAccessBase>());
2046 return match;
2047 }
2048
2049 bool isSuper() const {
2050 // ParseNodeKind::SuperBase cannot result from any expression syntax.
2051 return expression().isKind(ParseNodeKind::SuperBase);
2052 }
2053
2054 protected:
2055 using PropertyAccessBase::PropertyAccessBase;
2056 };
2057
2058 class ArgumentsLength : public PropertyAccess {
2059 public:
2060 ArgumentsLength(ParseNode* lhs, NameNode* name, uint32_t begin, uint32_t end)
2061 : PropertyAccess(ParseNodeKind::ArgumentsLength, lhs, name, begin, end) {
2062 MOZ_ASSERT(lhs);
2063 MOZ_ASSERT(name);
2064 }
2065
2066 static bool test(const ParseNode& node) {
2067 bool match = node.isKind(ParseNodeKind::ArgumentsLength);
2068 MOZ_ASSERT_IF(match, node.is<PropertyAccessBase>());
2069 return match;
2070 }
2071
2072 bool isSuper() const { return false; }
2073 };
2074
2075 class OptionalPropertyAccess : public PropertyAccessBase {
2076 public:
2077 OptionalPropertyAccess(ParseNode* lhs, NameNode* name, uint32_t begin,
2078 uint32_t end)
2079 : PropertyAccessBase(ParseNodeKind::OptionalDotExpr, lhs, name, begin,
2080 end) {
2081 MOZ_ASSERT(lhs);
2082 MOZ_ASSERT(name);
2083 }
2084
2085 static bool test(const ParseNode& node) {
2086 bool match = node.isKind(ParseNodeKind::OptionalDotExpr);
2087 MOZ_ASSERT_IF(match, node.is<PropertyAccessBase>());
2088 return match;
2089 }
2090 };
2091
2092 class PropertyByValueBase : public BinaryNode {
2093 public:
2094 PropertyByValueBase(ParseNodeKind kind, ParseNode* lhs, ParseNode* propExpr,
2095 uint32_t begin, uint32_t end)
2096 : BinaryNode(kind, TokenPos(begin, end), lhs, propExpr) {}
2097
2098 static bool test(const ParseNode& node) {
2099 bool match = node.isKind(ParseNodeKind::ElemExpr) ||
2100 node.isKind(ParseNodeKind::OptionalElemExpr);
2101 MOZ_ASSERT_IF(match, node.is<BinaryNode>());
2102 return match;
2103 }
2104
2105 ParseNode& expression() const { return *left(); }
2106
2107 ParseNode& key() const { return *right(); }
2108 };
2109
2110 class PropertyByValue : public PropertyByValueBase {
2111 public:
2112 PropertyByValue(ParseNode* lhs, ParseNode* propExpr, uint32_t begin,
2113 uint32_t end)
2114 : PropertyByValueBase(ParseNodeKind::ElemExpr, lhs, propExpr, begin,
2115 end) {}
2116
2117 static bool test(const ParseNode& node) {
2118 bool match = node.isKind(ParseNodeKind::ElemExpr);
2119 MOZ_ASSERT_IF(match, node.is<PropertyByValueBase>());
2120 return match;
2121 }
2122
2123 bool isSuper() const { return left()->isKind(ParseNodeKind::SuperBase); }
2124 };
2125
2126 class OptionalPropertyByValue : public PropertyByValueBase {
2127 public:
2128 OptionalPropertyByValue(ParseNode* lhs, ParseNode* propExpr, uint32_t begin,
2129 uint32_t end)
2130 : PropertyByValueBase(ParseNodeKind::OptionalElemExpr, lhs, propExpr,
2131 begin, end) {}
2132
2133 static bool test(const ParseNode& node) {
2134 bool match = node.isKind(ParseNodeKind::OptionalElemExpr);
2135 MOZ_ASSERT_IF(match, node.is<PropertyByValueBase>());
2136 return match;
2137 }
2138 };
2139
2140 class PrivateMemberAccessBase : public BinaryNode {
2141 public:
2142 PrivateMemberAccessBase(ParseNodeKind kind, ParseNode* lhs, NameNode* name,
2143 uint32_t begin, uint32_t end)
2144 : BinaryNode(kind, TokenPos(begin, end), lhs, name) {
2145 MOZ_ASSERT(name->isKind(ParseNodeKind::PrivateName));
2146 }
2147
2148 ParseNode& expression() const { return *left(); }
2149
2150 NameNode& privateName() const {
2151 NameNode& name = right()->as<NameNode>();
2152 MOZ_ASSERT(name.isKind(ParseNodeKind::PrivateName));
2153 return name;
2154 }
2155
2156 static bool test(const ParseNode& node) {
2157 bool match = node.isKind(ParseNodeKind::PrivateMemberExpr) ||
2158 node.isKind(ParseNodeKind::OptionalPrivateMemberExpr);
2159 MOZ_ASSERT_IF(match, node.is<BinaryNode>());
2160 MOZ_ASSERT_IF(match, node.as<BinaryNode>().right()->isKind(
2161 ParseNodeKind::PrivateName));
2162 return match;
2163 }
2164 };
2165
2166 class PrivateMemberAccess : public PrivateMemberAccessBase {
2167 public:
2168 PrivateMemberAccess(ParseNode* lhs, NameNode* name, uint32_t begin,
2169 uint32_t end)
2170 : PrivateMemberAccessBase(ParseNodeKind::PrivateMemberExpr, lhs, name,
2171 begin, end) {}
2172
2173 static bool test(const ParseNode& node) {
2174 return node.isKind(ParseNodeKind::PrivateMemberExpr);
2175 }
2176 };
2177
2178 class OptionalPrivateMemberAccess : public PrivateMemberAccessBase {
2179 public:
2180 OptionalPrivateMemberAccess(ParseNode* lhs, NameNode* name, uint32_t begin,
2181 uint32_t end)
2182 : PrivateMemberAccessBase(ParseNodeKind::OptionalPrivateMemberExpr, lhs,
2183 name, begin, end) {}
2184
2185 static bool test(const ParseNode& node) {
2186 return node.isKind(ParseNodeKind::OptionalPrivateMemberExpr);
2187 }
2188 };
2189
2190 class NewTargetNode : public TernaryNode {
2191 public:
2192 NewTargetNode(NullaryNode* newHolder, NullaryNode* targetHolder,
2193 NameNode* newTargetName)
2194 : TernaryNode(ParseNodeKind::NewTargetExpr, newHolder, targetHolder,
2195 newTargetName) {}
2196
2197 static bool test(const ParseNode& node) {
2198 bool match = node.isKind(ParseNodeKind::NewTargetExpr);
2199 MOZ_ASSERT_IF(match, node.is<TernaryNode>());
2200 return match;
2201 }
2202
2203 auto* newHolder() const { return &kid1()->as<NullaryNode>(); }
2204 auto* targetHolder() const { return &kid2()->as<NullaryNode>(); }
2205 auto* newTargetName() const { return &kid3()->as<NameNode>(); }
2206 };
2207
2208 /*
2209 * A CallSiteNode represents the implicit call site object argument in a
2210 * TaggedTemplate.
2211 */
2212 class CallSiteNode : public ListNode {
2213 public:
2214 explicit CallSiteNode(uint32_t begin)
2215 : ListNode(ParseNodeKind::CallSiteObj, TokenPos(begin, begin + 1)) {}
2216
2217 static bool test(const ParseNode& node) {
2218 bool match = node.isKind(ParseNodeKind::CallSiteObj);
2219 MOZ_ASSERT_IF(match, node.is<ListNode>());
2220 return match;
2221 }
2222
2223 ListNode* rawNodes() const {
2224 MOZ_ASSERT(head());
2225 return &head()->as<ListNode>();
2226 }
2227 };
2228
2229 class CallNode : public BinaryNode {
2230 const JSOp callOp_;
2231
2232 public:
2233 CallNode(ParseNodeKind kind, JSOp callOp, ParseNode* left, ListNode* right)
2234 : CallNode(kind, callOp, TokenPos(left->pn_pos.begin, right->pn_pos.end),
2235 left, right) {}
2236
2237 CallNode(ParseNodeKind kind, JSOp callOp, TokenPos pos, ParseNode* left,
2238 ListNode* right)
2239 : BinaryNode(kind, pos, left, right), callOp_(callOp) {
2240 MOZ_ASSERT(is<CallNode>());
2241 }
2242
2243 static bool test(const ParseNode& node) {
2244 bool match = node.isKind(ParseNodeKind::CallExpr) ||
2245 node.isKind(ParseNodeKind::SuperCallExpr) ||
2246 node.isKind(ParseNodeKind::OptionalCallExpr) ||
2247 node.isKind(ParseNodeKind::TaggedTemplateExpr) ||
2248 node.isKind(ParseNodeKind::NewExpr);
2249 MOZ_ASSERT_IF(match, node.is<BinaryNode>());
2250 return match;
2251 }
2252
2253 JSOp callOp() const { return callOp_; }
2254 auto* callee() const { return left(); }
2255 auto* args() const { return &right()->as<ListNode>(); }
2256 };
2257
2258 class ClassMethod : public BinaryNode {
2259 using Base = BinaryNode;
2260
2261 bool isStatic_;
2262 AccessorType accessorType_;
2263 FunctionNode* initializerIfPrivate_;
2264
2265 #ifdef ENABLE_DECORATORS
2266 ListNode* decorators_;
2267 #endif
2268
2269 public:
2270 /*
2271 * Method definitions often keep a name and function body that overlap,
2272 * so explicitly define the beginning and end here.
2273 */
2274 ClassMethod(ParseNodeKind kind, ParseNode* name, ParseNode* body,
2275 AccessorType accessorType, bool isStatic,
2276 FunctionNode* initializerIfPrivate
2277 #ifdef ENABLE_DECORATORS
2278 ,
2279 ListNode* decorators
2280 #endif
2281 )
2282 : BinaryNode(kind, TokenPos(name->pn_pos.begin, body->pn_pos.end), name,
2283 body),
2284 isStatic_(isStatic),
2285 accessorType_(accessorType),
2286 initializerIfPrivate_(initializerIfPrivate)
2287 #ifdef ENABLE_DECORATORS
2288 ,
2289 decorators_(decorators)
2290 #endif
2291 {
2292 MOZ_ASSERT(kind == ParseNodeKind::DefaultConstructor ||
2293 kind == ParseNodeKind::ClassMethod);
2294 }
2295
2296 static bool test(const ParseNode& node) {
2297 bool match = node.isKind(ParseNodeKind::DefaultConstructor) ||
2298 node.isKind(ParseNodeKind::ClassMethod);
2299 MOZ_ASSERT_IF(match, node.is<BinaryNode>());
2300 return match;
2301 }
2302
2303 ParseNode& name() const { return *left(); }
2304
2305 FunctionNode& method() const { return right()->as<FunctionNode>(); }
2306
2307 bool isStatic() const { return isStatic_; }
2308
2309 AccessorType accessorType() const { return accessorType_; }
2310
2311 FunctionNode* initializerIfPrivate() const { return initializerIfPrivate_; }
2312
2313 #ifdef ENABLE_DECORATORS
2314 ListNode* decorators() const { return decorators_; }
2315
2316 # ifdef DEBUG
2317 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
2318 int indent);
2319 # endif
2320 #endif
2321 };
2322
2323 class ClassField : public BinaryNode {
2324 using Base = BinaryNode;
2325
2326 bool isStatic_;
2327 #ifdef ENABLE_DECORATORS
2328 // The accessorGetterNode_ and accessorSetterNode_ are used to store the
2329 // getter and setter synthesized by the `accessor` keyword when they are
2330 // decorated. Otherwise, they are null.
2331 //
2332 // In most cases, the accessors are not added to the class members, and the
2333 // code generation occurs immediately prior to the decorator running. For
2334 // non-static private methods, the accessors are added to the class members
2335 // which causes them to be stored in lexical variables. The references here
2336 // are used to store the names of the accessors to look up the values of these
2337 // variables during bytecode generation.
2338 ClassMethod* accessorGetterNode_;
2339 ClassMethod* accessorSetterNode_;
2340 ListNode* decorators_;
2341 #endif
2342
2343 public:
2344 ClassField(ParseNode* name, ParseNode* initializer, bool isStatic
2345 #ifdef ENABLE_DECORATORS
2346 ,
2347 ListNode* decorators, ClassMethod* accessorGetterNode,
2348 ClassMethod* accessorSetterNode
2349 #endif
2350 )
2351 : BinaryNode(ParseNodeKind::ClassField, initializer->pn_pos, name,
2352 initializer),
2353 isStatic_(isStatic)
2354 #ifdef ENABLE_DECORATORS
2355 ,
2356 accessorGetterNode_(accessorGetterNode),
2357 accessorSetterNode_(accessorSetterNode),
2358 decorators_(decorators)
2359 #endif
2360 {
2361 #ifdef ENABLE_DECORATORS
2362 MOZ_ASSERT((accessorGetterNode_ == nullptr) ==
2363 (accessorSetterNode_ == nullptr));
2364 #endif
2365 }
2366
2367 static bool test(const ParseNode& node) {
2368 bool match = node.isKind(ParseNodeKind::ClassField);
2369 MOZ_ASSERT_IF(match, node.is<BinaryNode>());
2370 return match;
2371 }
2372
2373 ParseNode& name() const { return *left(); }
2374
2375 FunctionNode* initializer() const { return &right()->as<FunctionNode>(); }
2376
2377 bool isStatic() const { return isStatic_; }
2378
2379 #ifdef ENABLE_DECORATORS
2380 ListNode* decorators() const { return decorators_; }
2381 bool hasAccessor() const {
2382 return accessorGetterNode_ != nullptr && accessorSetterNode_ != nullptr;
2383 }
2384 ClassMethod* accessorGetterNode() { return accessorGetterNode_; }
2385 ClassMethod* accessorSetterNode() { return accessorSetterNode_; }
2386
2387 # ifdef DEBUG
2388 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
2389 int indent);
2390 # endif
2391 #endif
2392 };
2393
2394 // Hold onto the function generated for a class static block like
2395 //
2396 // class A {
2397 // static { /* this static block */ }
2398 // }
2399 //
2400 class StaticClassBlock : public UnaryNode {
2401 public:
2402 explicit StaticClassBlock(FunctionNode* function)
2403 : UnaryNode(ParseNodeKind::StaticClassBlock, function->pn_pos, function) {
2404 }
2405
2406 static bool test(const ParseNode& node) {
2407 bool match = node.isKind(ParseNodeKind::StaticClassBlock);
2408 MOZ_ASSERT_IF(match, node.is<UnaryNode>());
2409 return match;
2410 }
2411 FunctionNode* function() const { return &kid()->as<FunctionNode>(); }
2412 };
2413
2414 class PropertyDefinition : public BinaryNode {
2415 AccessorType accessorType_;
2416
2417 public:
2418 PropertyDefinition(ParseNode* name, ParseNode* value,
2419 AccessorType accessorType)
2420 : BinaryNode(ParseNodeKind::PropertyDefinition,
2421 TokenPos(name->pn_pos.begin, value->pn_pos.end), name,
2422 value),
2423 accessorType_(accessorType) {}
2424
2425 static bool test(const ParseNode& node) {
2426 bool match = node.isKind(ParseNodeKind::PropertyDefinition);
2427 MOZ_ASSERT_IF(match, node.is<BinaryNode>());
2428 return match;
2429 }
2430
2431 AccessorType accessorType() { return accessorType_; }
2432 };
2433
2434 class SwitchStatement : public BinaryNode {
2435 bool hasDefault_; /* only for ParseNodeKind::Switch */
2436
2437 public:
2438 SwitchStatement(uint32_t begin, ParseNode* discriminant,
2439 LexicalScopeNode* lexicalForCaseList, bool hasDefault)
2440 : BinaryNode(ParseNodeKind::SwitchStmt,
2441 TokenPos(begin, lexicalForCaseList->pn_pos.end),
2442 discriminant, lexicalForCaseList),
2443 hasDefault_(hasDefault) {
2444 #ifdef DEBUG
2445 ListNode* cases = &lexicalForCaseList->scopeBody()->as<ListNode>();
2446 MOZ_ASSERT(cases->isKind(ParseNodeKind::StatementList));
2447 bool found = false;
2448 for (ParseNode* item : cases->contents()) {
2449 CaseClause* caseNode = &item->as<CaseClause>();
2450 if (caseNode->isDefault()) {
2451 found = true;
2452 break;
2453 }
2454 }
2455 MOZ_ASSERT(found == hasDefault);
2456 #endif
2457 }
2458
2459 static bool test(const ParseNode& node) {
2460 bool match = node.isKind(ParseNodeKind::SwitchStmt);
2461 MOZ_ASSERT_IF(match, node.is<BinaryNode>());
2462 return match;
2463 }
2464
2465 ParseNode& discriminant() const { return *left(); }
2466
2467 LexicalScopeNode& lexicalForCaseList() const {
2468 return right()->as<LexicalScopeNode>();
2469 }
2470
2471 bool hasDefault() const { return hasDefault_; }
2472 };
2473
2474 class ClassNames : public BinaryNode {
2475 public:
2476 ClassNames(ParseNode* outerBinding, ParseNode* innerBinding,
2477 const TokenPos& pos)
2478 : BinaryNode(ParseNodeKind::ClassNames, pos, outerBinding, innerBinding) {
2479 MOZ_ASSERT_IF(outerBinding, outerBinding->isKind(ParseNodeKind::Name));
2480 MOZ_ASSERT(innerBinding->isKind(ParseNodeKind::Name));
2481 MOZ_ASSERT_IF(outerBinding, innerBinding->as<NameNode>().atom() ==
2482 outerBinding->as<NameNode>().atom());
2483 }
2484
2485 static bool test(const ParseNode& node) {
2486 bool match = node.isKind(ParseNodeKind::ClassNames);
2487 MOZ_ASSERT_IF(match, node.is<BinaryNode>());
2488 return match;
2489 }
2490
2491 /*
2492 * Classes require two definitions: The first "outer" binding binds the
2493 * class into the scope in which it was declared. the outer binding is a
2494 * mutable lexial binding. The second "inner" binding binds the class by
2495 * name inside a block in which the methods are evaulated. It is immutable,
2496 * giving the methods access to the static members of the class even if
2497 * the outer binding has been overwritten.
2498 */
2499 NameNode* outerBinding() const {
2500 if (ParseNode* binding = left()) {
2501 return &binding->as<NameNode>();
2502 }
2503 return nullptr;
2504 }
2505
2506 NameNode* innerBinding() const { return &right()->as<NameNode>(); }
2507 };
2508
2509 class ClassNode : public TernaryNode {
2510 using Base = TernaryNode;
2511
2512 private:
2513 LexicalScopeNode* innerScope() const {
2514 return &kid3()->as<LexicalScopeNode>();
2515 }
2516
2517 ClassBodyScopeNode* bodyScope() const {
2518 return &innerScope()->scopeBody()->as<ClassBodyScopeNode>();
2519 }
2520
2521 #ifdef ENABLE_DECORATORS
2522 ListNode* decorators_;
2523 FunctionNode* addInitializerFunction_;
2524 #endif
2525
2526 public:
2527 ClassNode(ParseNode* names, ParseNode* heritage,
2528 LexicalScopeNode* memberBlock,
2529 #ifdef ENABLE_DECORATORS
2530 ListNode* decorators, FunctionNode* addInitializerFunction,
2531 #endif
2532 const TokenPos& pos)
2533 : TernaryNode(ParseNodeKind::ClassDecl, names, heritage, memberBlock, pos)
2534 #ifdef ENABLE_DECORATORS
2535 ,
2536 decorators_(decorators),
2537 addInitializerFunction_(addInitializerFunction)
2538 #endif
2539 {
2540 MOZ_ASSERT(innerScope()->scopeBody()->is<ClassBodyScopeNode>());
2541 MOZ_ASSERT_IF(names, names->is<ClassNames>());
2542 }
2543
2544 static bool test(const ParseNode& node) {
2545 bool match = node.isKind(ParseNodeKind::ClassDecl);
2546 MOZ_ASSERT_IF(match, node.is<TernaryNode>());
2547 return match;
2548 }
2549
2550 ClassNames* names() const {
2551 return kid1() ? &kid1()->as<ClassNames>() : nullptr;
2552 }
2553
2554 ParseNode* heritage() const { return kid2(); }
2555
2556 ListNode* memberList() const { return bodyScope()->memberList(); }
2557
2558 LexicalScopeNode* scopeBindings() const {
2559 LexicalScopeNode* scope = innerScope();
2560 return scope->isEmptyScope() ? nullptr : scope;
2561 }
2562
2563 ClassBodyScopeNode* bodyScopeBindings() const {
2564 ClassBodyScopeNode* scope = bodyScope();
2565 return scope->isEmptyScope() ? nullptr : scope;
2566 }
2567 #ifdef ENABLE_DECORATORS
2568 ListNode* decorators() const { return decorators_; }
2569
2570 FunctionNode* addInitializerFunction() const {
2571 return addInitializerFunction_;
2572 }
2573 # ifdef DEBUG
2574 void dumpImpl(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
2575 int indent);
2576 # endif
2577 #endif
2578 };
2579
2580 #ifdef DEBUG
2581 void DumpParseTree(ParserBase* parser, ParseNode* pn, GenericPrinter& out,
2582 int indent = 0);
2583 #endif
2584
2585 class ParseNodeAllocator {
2586 public:
2587 explicit ParseNodeAllocator(FrontendContext* fc, LifoAlloc& alloc)
2588 : fc(fc), alloc(alloc) {}
2589
2590 void* allocNode(size_t size);
2591
2592 private:
2593 FrontendContext* fc;
2594 LifoAlloc& alloc;
2595 };
2596
2597 inline bool ParseNode::isConstant() {
2598 switch (pn_type) {
2599 case ParseNodeKind::NumberExpr:
2600 case ParseNodeKind::StringExpr:
2601 case ParseNodeKind::TemplateStringExpr:
2602 case ParseNodeKind::NullExpr:
2603 case ParseNodeKind::RawUndefinedExpr:
2604 case ParseNodeKind::FalseExpr:
2605 case ParseNodeKind::TrueExpr:
2606 return true;
2607 case ParseNodeKind::ArrayExpr:
2608 case ParseNodeKind::ObjectExpr:
2609 return !as<ListNode>().hasNonConstInitializer();
2610 default:
2611 return false;
2612 }
2613 }
2614
2615 bool IsAnonymousFunctionDefinition(ParseNode* pn);
2616
2617 } /* namespace frontend */
2618 } /* namespace js */
2619
2620 #endif /* frontend_ParseNode_h */