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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "frontend/ParseNode.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/Try.h" // MOZ_TRY*
#include "jsnum.h"
#include "frontend/CompilationStencil.h" // ExtensibleCompilationStencil
#include "frontend/FullParseHandler.h"
#include "frontend/ParseContext.h"
#include "frontend/Parser.h" // ParserBase
#include "frontend/ParserAtom.h" // ParserAtomsTable, TaggedParserAtomIndex
#include "frontend/SharedContext.h"
#include "js/Printer.h"
#include "vm/Scope.h" // GetScopeDataTrailingNames
using namespace js;
using namespace js::frontend;
#ifdef DEBUG
void ListNode::checkConsistency() const {
ParseNode* const* tailNode;
uint32_t actualCount = 0;
if (const ParseNode* last = head()) {
const ParseNode* pn = last;
while (pn) {
last = pn;
pn = pn->pn_next;
actualCount++;
}
tailNode = &last->pn_next;
} else {
tailNode = &head_;
}
MOZ_ASSERT(tail() == tailNode);
MOZ_ASSERT(count() == actualCount);
}
#endif
/*
* Allocate a ParseNode from parser's node freelist or, failing that, from
* cx's temporary arena.
*/
void* ParseNodeAllocator::allocNode(size_t size) {
LifoAlloc::AutoFallibleScope fallibleAllocator(&alloc);
void* p = alloc.alloc(size);
if (!p) {
ReportOutOfMemory(fc);
}
return p;
}
ParseNodeResult ParseNode::appendOrCreateList(ParseNodeKind kind,
ParseNode* left, ParseNode* right,
FullParseHandler* handler,
ParseContext* pc) {
// The asm.js specification is written in ECMAScript grammar terms that
// specify *only* a binary tree. It's a royal pain to implement the asm.js
// spec to act upon n-ary lists as created below. So for asm.js, form a
// binary tree of lists exactly as ECMAScript would by skipping the
// following optimization.
if (!pc->useAsmOrInsideUseAsm()) {
// Left-associative trees of a given operator (e.g. |a + b + c|) are
// binary trees in the spec: (+ (+ a b) c) in Lisp terms. Recursively
// processing such a tree, exactly implemented that way, would blow the
// the stack. We use a list node that uses O(1) stack to represent
// such operations: (+ a b c).
//
// (**) is right-associative; per spec |a ** b ** c| parses as
// (** a (** b c)). But we treat this the same way, creating a list
// node: (** a b c). All consumers must understand that this must be
// processed with a right fold, whereas the list (+ a b c) must be
// processed with a left fold because (+) is left-associative.
//
if (left->isKind(kind) &&
(kind == ParseNodeKind::PowExpr ? !left->isInParens()
: left->isBinaryOperation())) {
ListNode* list = &left->as<ListNode>();
list->append(right);
list->pn_pos.end = right->pn_pos.end;
return list;
}
}
ListNode* list;
MOZ_TRY_VAR(list, handler->newResult<ListNode>(kind, left));
list->append(right);
return list;
}
const ParseNode::TypeCode ParseNode::typeCodeTable[] = {
#define TYPE_CODE(_name, type) type::classTypeCode(),
FOR_EACH_PARSE_NODE_KIND(TYPE_CODE)
#undef TYPE_CODE
};
#ifdef DEBUG
const size_t ParseNode::sizeTable[] = {
# define NODE_SIZE(_name, type) sizeof(type),
FOR_EACH_PARSE_NODE_KIND(NODE_SIZE)
# undef NODE_SIZE
};
static const char* const parseNodeNames[] = {
# define STRINGIFY(name, _type) #name,
FOR_EACH_PARSE_NODE_KIND(STRINGIFY)
# undef STRINGIFY
};
static void DumpParseTree(const ParserAtomsTable* parserAtoms, ParseNode* pn,
GenericPrinter& out, int indent) {
if (pn == nullptr) {
out.put("#NULL");
} else {
pn->dump(parserAtoms, out, indent);
}
}
void frontend::DumpParseTree(ParserBase* parser, ParseNode* pn,
GenericPrinter& out, int indent) {
ParserAtomsTable* parserAtoms = parser ? &parser->parserAtoms() : nullptr;
::DumpParseTree(parserAtoms, pn, out, indent);
}
static void IndentNewLine(GenericPrinter& out, int indent) {
out.putChar('\n');
for (int i = 0; i < indent; ++i) {
out.putChar(' ');
}
}
void ParseNode::dump() { dump(nullptr); }
void ParseNode::dump(const ParserAtomsTable* parserAtoms) {
js::Fprinter out(stderr);
dump(parserAtoms, out);
}
void ParseNode::dump(const ParserAtomsTable* parserAtoms, GenericPrinter& out) {
dump(parserAtoms, out, 0);
out.putChar('\n');
}
void ParseNode::dump(const ParserAtomsTable* parserAtoms, GenericPrinter& out,
int indent) {
switch (getKind()) {
# define DUMP(K, T) \
case ParseNodeKind::K: \
as<T>().dumpImpl(parserAtoms, out, indent); \
break;
FOR_EACH_PARSE_NODE_KIND(DUMP)
# undef DUMP
default:
out.printf("#<BAD NODE %p, kind=%u>", (void*)this, unsigned(getKind()));
}
}
void NullaryNode::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
switch (getKind()) {
case ParseNodeKind::TrueExpr:
out.put("#true");
break;
case ParseNodeKind::FalseExpr:
out.put("#false");
break;
case ParseNodeKind::NullExpr:
out.put("#null");
break;
case ParseNodeKind::RawUndefinedExpr:
out.put("#undefined");
break;
default:
out.printf("(%s)", parseNodeNames[getKindAsIndex()]);
}
}
void NumericLiteral::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
ToCStringBuf cbuf;
const char* cstr = NumberToCString(&cbuf, value());
MOZ_ASSERT(cstr);
if (!std::isfinite(value())) {
out.put("#");
}
out.printf("%s", cstr);
}
void BigIntLiteral::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
out.printf("(%s)", parseNodeNames[getKindAsIndex()]);
}
void RegExpLiteral::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
out.printf("(%s)", parseNodeNames[getKindAsIndex()]);
}
static void DumpCharsNoNewline(const ParserAtomsTable* parserAtoms,
TaggedParserAtomIndex index,
GenericPrinter& out) {
out.put("\"");
if (parserAtoms) {
parserAtoms->dumpCharsNoQuote(out, index);
} else {
DumpTaggedParserAtomIndexNoQuote(out, index, nullptr);
}
out.put("\"");
}
void LoopControlStatement::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
const char* name = parseNodeNames[getKindAsIndex()];
out.printf("(%s", name);
if (label_) {
out.printf(" ");
DumpCharsNoNewline(parserAtoms, label_, out);
}
out.printf(")");
}
void UnaryNode::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
const char* name = parseNodeNames[getKindAsIndex()];
out.printf("(%s ", name);
indent += strlen(name) + 2;
::DumpParseTree(parserAtoms, kid(), out, indent);
out.printf(")");
}
void BinaryNode::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
if (isKind(ParseNodeKind::DotExpr)) {
out.put("(.");
::DumpParseTree(parserAtoms, right(), out, indent + 2);
out.putChar(' ');
if (as<PropertyAccess>().isSuper()) {
out.put("super");
} else {
::DumpParseTree(parserAtoms, left(), out, indent + 2);
}
out.printf(")");
return;
}
const char* name = parseNodeNames[getKindAsIndex()];
out.printf("(%s ", name);
indent += strlen(name) + 2;
::DumpParseTree(parserAtoms, left(), out, indent);
IndentNewLine(out, indent);
::DumpParseTree(parserAtoms, right(), out, indent);
out.printf(")");
}
void TernaryNode::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
const char* name = parseNodeNames[getKindAsIndex()];
out.printf("(%s ", name);
indent += strlen(name) + 2;
::DumpParseTree(parserAtoms, kid1(), out, indent);
IndentNewLine(out, indent);
::DumpParseTree(parserAtoms, kid2(), out, indent);
IndentNewLine(out, indent);
::DumpParseTree(parserAtoms, kid3(), out, indent);
out.printf(")");
}
void FunctionNode::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
const char* name = parseNodeNames[getKindAsIndex()];
out.printf("(%s ", name);
indent += strlen(name) + 2;
::DumpParseTree(parserAtoms, body(), out, indent);
out.printf(")");
}
void ModuleNode::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
const char* name = parseNodeNames[getKindAsIndex()];
out.printf("(%s ", name);
indent += strlen(name) + 2;
::DumpParseTree(parserAtoms, body(), out, indent);
out.printf(")");
}
void ListNode::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
const char* name = parseNodeNames[getKindAsIndex()];
out.printf("(%s [", name);
if (ParseNode* listHead = head()) {
indent += strlen(name) + 3;
::DumpParseTree(parserAtoms, listHead, out, indent);
for (ParseNode* item : contentsFrom(listHead->pn_next)) {
IndentNewLine(out, indent);
::DumpParseTree(parserAtoms, item, out, indent);
}
}
out.printf("])");
}
void NameNode::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
switch (getKind()) {
case ParseNodeKind::StringExpr:
case ParseNodeKind::TemplateStringExpr:
case ParseNodeKind::ObjectPropertyName:
DumpCharsNoNewline(parserAtoms, atom_, out);
return;
case ParseNodeKind::Name:
case ParseNodeKind::PrivateName: // atom() already includes the '#', no
// need to specially include it.
case ParseNodeKind::PropertyNameExpr:
if (!atom_) {
out.put("#<null name>");
} else if (parserAtoms) {
if (atom_ == TaggedParserAtomIndex::WellKnown::empty()) {
out.put("#<zero-length name>");
} else {
parserAtoms->dumpCharsNoQuote(out, atom_);
}
} else {
DumpTaggedParserAtomIndexNoQuote(out, atom_, nullptr);
}
return;
case ParseNodeKind::LabelStmt: {
this->as<LabeledStatement>().dumpImpl(parserAtoms, out, indent);
return;
}
default: {
const char* name = parseNodeNames[getKindAsIndex()];
out.printf("(%s)", name);
return;
}
}
}
void LabeledStatement::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
const char* name = parseNodeNames[getKindAsIndex()];
out.printf("(%s ", name);
DumpCharsNoNewline(parserAtoms, label(), out);
indent += strlen(name) + 2;
IndentNewLine(out, indent);
::DumpParseTree(parserAtoms, statement(), out, indent);
out.printf(")");
}
template <ParseNodeKind Kind, typename ScopeType>
void BaseScopeNode<Kind, ScopeType>::dumpImpl(
const ParserAtomsTable* parserAtoms, GenericPrinter& out, int indent) {
const char* name = parseNodeNames[getKindAsIndex()];
out.printf("(%s [", name);
int nameIndent = indent + strlen(name) + 3;
if (!isEmptyScope()) {
typename ScopeType::ParserData* bindings = scopeBindings();
auto names = GetScopeDataTrailingNames(bindings);
for (uint32_t i = 0; i < names.size(); i++) {
auto index = names[i].name();
if (parserAtoms) {
if (index == TaggedParserAtomIndex::WellKnown::empty()) {
out.put("#<zero-length name>");
} else {
parserAtoms->dumpCharsNoQuote(out, index);
}
} else {
DumpTaggedParserAtomIndexNoQuote(out, index, nullptr);
}
if (i < names.size() - 1) {
IndentNewLine(out, nameIndent);
}
}
}
out.putChar(']');
indent += 2;
IndentNewLine(out, indent);
::DumpParseTree(parserAtoms, scopeBody(), out, indent);
out.printf(")");
}
# ifdef ENABLE_DECORATORS
void ClassMethod::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
if (decorators_) {
decorators_->dumpImpl(parserAtoms, out, indent);
}
Base::dumpImpl(parserAtoms, out, indent);
}
void ClassField::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
if (decorators_) {
decorators_->dumpImpl(parserAtoms, out, indent);
out.putChar(' ');
}
Base::dumpImpl(parserAtoms, out, indent);
IndentNewLine(out, indent + 2);
if (accessorGetterNode_) {
out.printf("getter: ");
accessorGetterNode_->dumpImpl(parserAtoms, out, indent);
}
IndentNewLine(out, indent + 2);
if (accessorSetterNode_) {
out.printf("setter: ");
accessorSetterNode_->dumpImpl(parserAtoms, out, indent);
}
}
void ClassNode::dumpImpl(const ParserAtomsTable* parserAtoms,
GenericPrinter& out, int indent) {
if (decorators_) {
decorators_->dumpImpl(parserAtoms, out, indent);
}
Base::dumpImpl(parserAtoms, out, indent);
}
# endif
#endif
TaggedParserAtomIndex NumericLiteral::toAtom(
FrontendContext* fc, ParserAtomsTable& parserAtoms) const {
return NumberToParserAtom(fc, parserAtoms, value());
}
RegExpObject* RegExpLiteral::create(
JSContext* cx, FrontendContext* fc, ParserAtomsTable& parserAtoms,
CompilationAtomCache& atomCache,
ExtensibleCompilationStencil& stencil) const {
return stencil.regExpData[index_].createRegExpAndEnsureAtom(
cx, fc, parserAtoms, atomCache);
}
bool js::frontend::IsAnonymousFunctionDefinition(ParseNode* pn) {
// ES 2017 draft
// 12.15.2 (ArrowFunction, AsyncArrowFunction).
// 14.1.12 (FunctionExpression).
// 14.4.8 (Generatoression).
// 14.6.8 (AsyncFunctionExpression)
if (pn->is<FunctionNode>() &&
!pn->as<FunctionNode>().funbox()->explicitName()) {
return true;
}
// 14.5.8 (ClassExpression)
if (pn->is<ClassNode>() && !pn->as<ClassNode>().names()) {
return true;
}
return false;
}
|