1/*
2 * Copyright (C) 2010, 2013, 2016 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "BuiltinNames.h"
29#include "BytecodeIntrinsicRegistry.h"
30#include "MathCommon.h"
31#include "NodeConstructors.h"
32#include "SyntaxChecker.h"
33#include "VariableEnvironment.h"
34#include <utility>
35
36namespace JSC {
37
38class ASTBuilder {
39 struct BinaryOpInfo {
40 BinaryOpInfo() {}
41 BinaryOpInfo(const JSTextPosition& otherStart, const JSTextPosition& otherDivot, const JSTextPosition& otherEnd, bool rhsHasAssignment)
42 : start(otherStart)
43 , divot(otherDivot)
44 , end(otherEnd)
45 , hasAssignment(rhsHasAssignment)
46 {
47 }
48 BinaryOpInfo(const BinaryOpInfo& lhs, const BinaryOpInfo& rhs)
49 : start(lhs.start)
50 , divot(rhs.start)
51 , end(rhs.end)
52 , hasAssignment(lhs.hasAssignment || rhs.hasAssignment)
53 {
54 }
55 JSTextPosition start;
56 JSTextPosition divot;
57 JSTextPosition end;
58 bool hasAssignment;
59 };
60
61
62 struct AssignmentInfo {
63 AssignmentInfo() {}
64 AssignmentInfo(ExpressionNode* node, const JSTextPosition& start, const JSTextPosition& divot, int initAssignments, Operator op)
65 : m_node(node)
66 , m_start(start)
67 , m_divot(divot)
68 , m_initAssignments(initAssignments)
69 , m_op(op)
70 {
71 ASSERT(m_divot.offset >= m_divot.lineStartOffset);
72 ASSERT(m_start.offset >= m_start.lineStartOffset);
73 }
74 ExpressionNode* m_node;
75 JSTextPosition m_start;
76 JSTextPosition m_divot;
77 int m_initAssignments;
78 Operator m_op;
79 };
80public:
81 ASTBuilder(VM* vm, ParserArena& parserArena, SourceCode* sourceCode)
82 : m_vm(vm)
83 , m_parserArena(parserArena)
84 , m_sourceCode(sourceCode)
85 , m_evalCount(0)
86 {
87 }
88
89 struct BinaryExprContext {
90 BinaryExprContext(ASTBuilder&) {}
91 };
92 struct UnaryExprContext {
93 UnaryExprContext(ASTBuilder&) {}
94 };
95
96 typedef ExpressionNode* Expression;
97 typedef JSC::SourceElements* SourceElements;
98 typedef ArgumentsNode* Arguments;
99 typedef CommaNode* Comma;
100 typedef PropertyNode* Property;
101 typedef PropertyListNode* PropertyList;
102 typedef ElementNode* ElementList;
103 typedef ArgumentListNode* ArgumentsList;
104 typedef TemplateExpressionListNode* TemplateExpressionList;
105 typedef TemplateStringNode* TemplateString;
106 typedef TemplateStringListNode* TemplateStringList;
107 typedef TemplateLiteralNode* TemplateLiteral;
108 typedef FunctionParameters* FormalParameterList;
109 typedef FunctionMetadataNode* FunctionBody;
110 typedef ClassExprNode* ClassExpression;
111 typedef ModuleNameNode* ModuleName;
112 typedef ImportSpecifierNode* ImportSpecifier;
113 typedef ImportSpecifierListNode* ImportSpecifierList;
114 typedef ExportSpecifierNode* ExportSpecifier;
115 typedef ExportSpecifierListNode* ExportSpecifierList;
116 typedef StatementNode* Statement;
117 typedef ClauseListNode* ClauseList;
118 typedef CaseClauseNode* Clause;
119 typedef std::pair<ExpressionNode*, BinaryOpInfo> BinaryOperand;
120 typedef DestructuringPatternNode* DestructuringPattern;
121 typedef ArrayPatternNode* ArrayPattern;
122 typedef ObjectPatternNode* ObjectPattern;
123 typedef BindingNode* BindingPattern;
124 typedef AssignmentElementNode* AssignmentElement;
125 static const bool CreatesAST = true;
126 static const bool NeedsFreeVariableInfo = true;
127 static const bool CanUseFunctionCache = true;
128 static const int DontBuildKeywords = 0;
129 static const int DontBuildStrings = 0;
130
131 ExpressionNode* makeBinaryNode(const JSTokenLocation&, int token, std::pair<ExpressionNode*, BinaryOpInfo>, std::pair<ExpressionNode*, BinaryOpInfo>);
132 ExpressionNode* makeFunctionCallNode(const JSTokenLocation&, ExpressionNode* func, bool previousBaseWasSuper, ArgumentsNode* args, const JSTextPosition& divotStart, const JSTextPosition& divot, const JSTextPosition& divotEnd, size_t callOrApplyChildDepth);
133
134 JSC::SourceElements* createSourceElements() { return new (m_parserArena) JSC::SourceElements(); }
135
136 int features() const { return m_scope.m_features; }
137 int numConstants() const { return m_scope.m_numConstants; }
138
139 ExpressionNode* makeAssignNode(const JSTokenLocation&, ExpressionNode* left, Operator, ExpressionNode* right, bool leftHasAssignments, bool rightHasAssignments, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end);
140 ExpressionNode* makePrefixNode(const JSTokenLocation&, ExpressionNode*, Operator, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end);
141 ExpressionNode* makePostfixNode(const JSTokenLocation&, ExpressionNode*, Operator, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end);
142 ExpressionNode* makeTypeOfNode(const JSTokenLocation&, ExpressionNode*);
143 ExpressionNode* makeDeleteNode(const JSTokenLocation&, ExpressionNode*, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end);
144 ExpressionNode* makeNegateNode(const JSTokenLocation&, ExpressionNode*);
145 ExpressionNode* makeBitwiseNotNode(const JSTokenLocation&, ExpressionNode*);
146 ExpressionNode* makePowNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
147 ExpressionNode* makeMultNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
148 ExpressionNode* makeDivNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
149 ExpressionNode* makeModNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
150 ExpressionNode* makeAddNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
151 ExpressionNode* makeSubNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
152 ExpressionNode* makeBitXOrNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
153 ExpressionNode* makeBitAndNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
154 ExpressionNode* makeBitOrNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
155 ExpressionNode* makeLeftShiftNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
156 ExpressionNode* makeRightShiftNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
157 ExpressionNode* makeURightShiftNode(const JSTokenLocation&, ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
158
159 ExpressionNode* createLogicalNot(const JSTokenLocation& location, ExpressionNode* expr)
160 {
161 if (expr->isNumber())
162 return createBoolean(location, isZeroOrUnordered(static_cast<NumberNode*>(expr)->value()));
163
164 return new (m_parserArena) LogicalNotNode(location, expr);
165 }
166 ExpressionNode* createUnaryPlus(const JSTokenLocation& location, ExpressionNode* expr) { return new (m_parserArena) UnaryPlusNode(location, expr); }
167 ExpressionNode* createVoid(const JSTokenLocation& location, ExpressionNode* expr)
168 {
169 incConstants();
170 return new (m_parserArena) VoidNode(location, expr);
171 }
172 ExpressionNode* createThisExpr(const JSTokenLocation& location)
173 {
174 usesThis();
175 return new (m_parserArena) ThisNode(location);
176 }
177 ExpressionNode* createSuperExpr(const JSTokenLocation& location)
178 {
179 return new (m_parserArena) SuperNode(location);
180 }
181 ExpressionNode* createImportExpr(const JSTokenLocation& location, ExpressionNode* expr, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end)
182 {
183 auto* node = new (m_parserArena) ImportNode(location, expr);
184 setExceptionLocation(node, start, divot, end);
185 return node;
186 }
187 ExpressionNode* createNewTargetExpr(const JSTokenLocation location)
188 {
189 usesNewTarget();
190 return new (m_parserArena) NewTargetNode(location);
191 }
192 ExpressionNode* createImportMetaExpr(const JSTokenLocation& location, ExpressionNode* expr) { return new (m_parserArena) ImportMetaNode(location, expr); }
193 bool isMetaProperty(ExpressionNode* node) { return node->isMetaProperty(); }
194 bool isNewTarget(ExpressionNode* node) { return node->isNewTarget(); }
195 bool isImportMeta(ExpressionNode* node) { return node->isImportMeta(); }
196 ExpressionNode* createResolve(const JSTokenLocation& location, const Identifier& ident, const JSTextPosition& start, const JSTextPosition& end)
197 {
198 if (m_vm->propertyNames->arguments == ident)
199 usesArguments();
200
201 if (ident.isSymbol()) {
202 if (BytecodeIntrinsicNode::EmitterType emitter = m_vm->bytecodeIntrinsicRegistry().lookup(ident))
203 return new (m_parserArena) BytecodeIntrinsicNode(BytecodeIntrinsicNode::Type::Constant, location, emitter, ident, nullptr, start, start, end);
204 }
205
206 return new (m_parserArena) ResolveNode(location, ident, start);
207 }
208 ExpressionNode* createObjectLiteral(const JSTokenLocation& location) { return new (m_parserArena) ObjectLiteralNode(location); }
209 ExpressionNode* createObjectLiteral(const JSTokenLocation& location, PropertyListNode* properties) { return new (m_parserArena) ObjectLiteralNode(location, properties); }
210
211 ExpressionNode* createArray(const JSTokenLocation& location, int elisions)
212 {
213 if (elisions)
214 incConstants();
215 return new (m_parserArena) ArrayNode(location, elisions);
216 }
217
218 ExpressionNode* createArray(const JSTokenLocation& location, ElementNode* elems) { return new (m_parserArena) ArrayNode(location, elems); }
219 ExpressionNode* createArray(const JSTokenLocation& location, int elisions, ElementNode* elems)
220 {
221 if (elisions)
222 incConstants();
223 return new (m_parserArena) ArrayNode(location, elisions, elems);
224 }
225 ExpressionNode* createDoubleExpr(const JSTokenLocation& location, double d)
226 {
227 incConstants();
228 return new (m_parserArena) DoubleNode(location, d);
229 }
230 ExpressionNode* createIntegerExpr(const JSTokenLocation& location, double d)
231 {
232 incConstants();
233 return new (m_parserArena) IntegerNode(location, d);
234 }
235
236 ExpressionNode* createBigInt(const JSTokenLocation& location, const Identifier* bigInt, uint8_t radix)
237 {
238 incConstants();
239 return new (m_parserArena) BigIntNode(location, *bigInt, radix);
240 }
241
242 ExpressionNode* createString(const JSTokenLocation& location, const Identifier* string)
243 {
244 ASSERT(string);
245 incConstants();
246 return new (m_parserArena) StringNode(location, *string);
247 }
248
249 ExpressionNode* createBoolean(const JSTokenLocation& location, bool b)
250 {
251 incConstants();
252 return new (m_parserArena) BooleanNode(location, b);
253 }
254
255 ExpressionNode* createNull(const JSTokenLocation& location)
256 {
257 incConstants();
258 return new (m_parserArena) NullNode(location);
259 }
260
261 ExpressionNode* createBracketAccess(const JSTokenLocation& location, ExpressionNode* base, ExpressionNode* property, bool propertyHasAssignments, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end)
262 {
263 if (base->isSuperNode())
264 usesSuperProperty();
265
266 BracketAccessorNode* node = new (m_parserArena) BracketAccessorNode(location, base, property, propertyHasAssignments);
267 setExceptionLocation(node, start, divot, end);
268 return node;
269 }
270
271 ExpressionNode* createDotAccess(const JSTokenLocation& location, ExpressionNode* base, const Identifier* property, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end)
272 {
273 if (base->isSuperNode())
274 usesSuperProperty();
275
276 DotAccessorNode* node = new (m_parserArena) DotAccessorNode(location, base, *property);
277 setExceptionLocation(node, start, divot, end);
278 return node;
279 }
280
281 ExpressionNode* createSpreadExpression(const JSTokenLocation& location, ExpressionNode* expression, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end)
282 {
283 auto node = new (m_parserArena) SpreadExpressionNode(location, expression);
284 setExceptionLocation(node, start, divot, end);
285 return node;
286 }
287
288 ExpressionNode* createObjectSpreadExpression(const JSTokenLocation& location, ExpressionNode* expression, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end)
289 {
290 auto node = new (m_parserArena) ObjectSpreadExpressionNode(location, expression);
291 setExceptionLocation(node, start, divot, end);
292 return node;
293 }
294
295 TemplateStringNode* createTemplateString(const JSTokenLocation& location, const Identifier* cooked, const Identifier* raw)
296 {
297 return new (m_parserArena) TemplateStringNode(location, cooked, raw);
298 }
299
300 TemplateStringListNode* createTemplateStringList(TemplateStringNode* templateString)
301 {
302 return new (m_parserArena) TemplateStringListNode(templateString);
303 }
304
305 TemplateStringListNode* createTemplateStringList(TemplateStringListNode* templateStringList, TemplateStringNode* templateString)
306 {
307 return new (m_parserArena) TemplateStringListNode(templateStringList, templateString);
308 }
309
310 TemplateExpressionListNode* createTemplateExpressionList(ExpressionNode* expression)
311 {
312 return new (m_parserArena) TemplateExpressionListNode(expression);
313 }
314
315 TemplateExpressionListNode* createTemplateExpressionList(TemplateExpressionListNode* templateExpressionListNode, ExpressionNode* expression)
316 {
317 return new (m_parserArena) TemplateExpressionListNode(templateExpressionListNode, expression);
318 }
319
320 TemplateLiteralNode* createTemplateLiteral(const JSTokenLocation& location, TemplateStringListNode* templateStringList)
321 {
322 return new (m_parserArena) TemplateLiteralNode(location, templateStringList);
323 }
324
325 TemplateLiteralNode* createTemplateLiteral(const JSTokenLocation& location, TemplateStringListNode* templateStringList, TemplateExpressionListNode* templateExpressionList)
326 {
327 return new (m_parserArena) TemplateLiteralNode(location, templateStringList, templateExpressionList);
328 }
329
330 ExpressionNode* createTaggedTemplate(const JSTokenLocation& location, ExpressionNode* base, TemplateLiteralNode* templateLiteral, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end)
331 {
332 auto node = new (m_parserArena) TaggedTemplateNode(location, base, templateLiteral);
333 setExceptionLocation(node, start, divot, end);
334 setEndOffset(node, end.offset);
335 return node;
336 }
337
338 ExpressionNode* createRegExp(const JSTokenLocation& location, const Identifier& pattern, const Identifier& flags, const JSTextPosition& start)
339 {
340 if (Yarr::hasError(Yarr::checkSyntax(pattern.string(), flags.string())))
341 return 0;
342 RegExpNode* node = new (m_parserArena) RegExpNode(location, pattern, flags);
343 int size = pattern.length() + 2; // + 2 for the two /'s
344 JSTextPosition end = start + size;
345 setExceptionLocation(node, start, end, end);
346 return node;
347 }
348
349 ExpressionNode* createNewExpr(const JSTokenLocation& location, ExpressionNode* expr, ArgumentsNode* arguments, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end)
350 {
351 NewExprNode* node = new (m_parserArena) NewExprNode(location, expr, arguments);
352 setExceptionLocation(node, start, divot, end);
353 return node;
354 }
355
356 ExpressionNode* createNewExpr(const JSTokenLocation& location, ExpressionNode* expr, const JSTextPosition& start, const JSTextPosition& end)
357 {
358 NewExprNode* node = new (m_parserArena) NewExprNode(location, expr);
359 setExceptionLocation(node, start, end, end);
360 return node;
361 }
362
363 ExpressionNode* createConditionalExpr(const JSTokenLocation& location, ExpressionNode* condition, ExpressionNode* lhs, ExpressionNode* rhs)
364 {
365 return new (m_parserArena) ConditionalNode(location, condition, lhs, rhs);
366 }
367
368 ExpressionNode* createAssignResolve(const JSTokenLocation& location, const Identifier& ident, ExpressionNode* rhs, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end, AssignmentContext assignmentContext)
369 {
370 if (rhs->isBaseFuncExprNode()) {
371 auto metadata = static_cast<BaseFuncExprNode*>(rhs)->metadata();
372 metadata->setEcmaName(ident);
373 } else if (rhs->isClassExprNode())
374 static_cast<ClassExprNode*>(rhs)->setEcmaName(ident);
375 AssignResolveNode* node = new (m_parserArena) AssignResolveNode(location, ident, rhs, assignmentContext);
376 setExceptionLocation(node, start, divot, end);
377 return node;
378 }
379
380 YieldExprNode* createYield(const JSTokenLocation& location)
381 {
382 return new (m_parserArena) YieldExprNode(location, nullptr, /* delegate */ false);
383 }
384
385 YieldExprNode* createYield(const JSTokenLocation& location, ExpressionNode* argument, bool delegate, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end)
386 {
387 YieldExprNode* node = new (m_parserArena) YieldExprNode(location, argument, delegate);
388 setExceptionLocation(node, start, divot, end);
389 return node;
390 }
391
392 AwaitExprNode* createAwait(const JSTokenLocation& location, ExpressionNode* argument, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end)
393 {
394 ASSERT(argument);
395 AwaitExprNode* node = new (m_parserArena) AwaitExprNode(location, argument);
396 setExceptionLocation(node, start, divot, end);
397 return node;
398 }
399
400 ClassExprNode* createClassExpr(const JSTokenLocation& location, const ParserClassInfo<ASTBuilder>& classInfo, VariableEnvironment& classEnvironment, ExpressionNode* constructor,
401 ExpressionNode* parentClass, PropertyListNode* classElements)
402 {
403 SourceCode source = m_sourceCode->subExpression(classInfo.startOffset, classInfo.endOffset, classInfo.startLine, classInfo.startColumn);
404 return new (m_parserArena) ClassExprNode(location, *classInfo.className, source, classEnvironment, constructor, parentClass, classElements);
405 }
406
407 ExpressionNode* createFunctionExpr(const JSTokenLocation& location, const ParserFunctionInfo<ASTBuilder>& functionInfo)
408 {
409 FuncExprNode* result = new (m_parserArena) FuncExprNode(location, *functionInfo.name, functionInfo.body,
410 m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.parametersStartColumn));
411 functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset);
412 return result;
413 }
414
415 ExpressionNode* createGeneratorFunctionBody(const JSTokenLocation& location, const ParserFunctionInfo<ASTBuilder>& functionInfo, const Identifier& name)
416 {
417 FuncExprNode* result = static_cast<FuncExprNode*>(createFunctionExpr(location, functionInfo));
418 if (!name.isNull())
419 result->metadata()->setEcmaName(name);
420 return result;
421 }
422
423 ExpressionNode* createAsyncFunctionBody(const JSTokenLocation& location, const ParserFunctionInfo<ASTBuilder>& functionInfo, SourceParseMode parseMode)
424 {
425 if (parseMode == SourceParseMode::AsyncArrowFunctionBodyMode) {
426 SourceCode source = m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.body->isArrowFunctionBodyExpression() ? functionInfo.endOffset - 1 : functionInfo.endOffset, functionInfo.startLine, functionInfo.parametersStartColumn);
427 FuncExprNode* result = new (m_parserArena) FuncExprNode(location, *functionInfo.name, functionInfo.body, source);
428 functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset);
429 return result;
430 }
431 return createFunctionExpr(location, functionInfo);
432 }
433
434 ExpressionNode* createMethodDefinition(const JSTokenLocation& location, const ParserFunctionInfo<ASTBuilder>& functionInfo)
435 {
436 MethodDefinitionNode* result = new (m_parserArena) MethodDefinitionNode(location, *functionInfo.name, functionInfo.body,
437 m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.parametersStartColumn));
438 functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset);
439 return result;
440 }
441
442 FunctionMetadataNode* createFunctionMetadata(
443 const JSTokenLocation& startLocation, const JSTokenLocation& endLocation,
444 unsigned startColumn, unsigned endColumn, int functionKeywordStart,
445 int functionNameStart, int parametersStart, bool inStrictContext,
446 ConstructorKind constructorKind, SuperBinding superBinding,
447 unsigned parameterCount,
448 SourceParseMode mode, bool isArrowFunctionBodyExpression)
449 {
450 return new (m_parserArena) FunctionMetadataNode(
451 m_parserArena, startLocation, endLocation, startColumn, endColumn,
452 functionKeywordStart, functionNameStart, parametersStart,
453 inStrictContext, constructorKind, superBinding,
454 parameterCount, mode, isArrowFunctionBodyExpression);
455 }
456
457 ExpressionNode* createArrowFunctionExpr(const JSTokenLocation& location, const ParserFunctionInfo<ASTBuilder>& functionInfo)
458 {
459 usesArrowFunction();
460 SourceCode source = m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.body->isArrowFunctionBodyExpression() ? functionInfo.endOffset - 1 : functionInfo.endOffset, functionInfo.startLine, functionInfo.parametersStartColumn);
461 ArrowFuncExprNode* result = new (m_parserArena) ArrowFuncExprNode(location, *functionInfo.name, functionInfo.body, source);
462 functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset);
463 return result;
464 }
465
466 ArgumentsNode* createArguments() { return new (m_parserArena) ArgumentsNode(); }
467 ArgumentsNode* createArguments(ArgumentListNode* args) { return new (m_parserArena) ArgumentsNode(args); }
468 ArgumentListNode* createArgumentsList(const JSTokenLocation& location, ExpressionNode* arg) { return new (m_parserArena) ArgumentListNode(location, arg); }
469 ArgumentListNode* createArgumentsList(const JSTokenLocation& location, ArgumentListNode* args, ExpressionNode* arg) { return new (m_parserArena) ArgumentListNode(location, args, arg); }
470
471 NEVER_INLINE PropertyNode* createGetterOrSetterProperty(const JSTokenLocation& location, PropertyNode::Type type, bool,
472 const Identifier* name, const ParserFunctionInfo<ASTBuilder>& functionInfo, ClassElementTag tag)
473 {
474 ASSERT(name);
475 functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset);
476 functionInfo.body->setEcmaName(*name);
477 SourceCode source = m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.parametersStartColumn);
478 MethodDefinitionNode* methodDef = new (m_parserArena) MethodDefinitionNode(location, m_vm->propertyNames->nullIdentifier, functionInfo.body, source);
479 return new (m_parserArena) PropertyNode(*name, methodDef, type, PropertyNode::Unknown, SuperBinding::Needed, tag);
480 }
481
482 NEVER_INLINE PropertyNode* createGetterOrSetterProperty(const JSTokenLocation& location, PropertyNode::Type type, bool,
483 ExpressionNode* name, const ParserFunctionInfo<ASTBuilder>& functionInfo, ClassElementTag tag)
484 {
485 ASSERT(name);
486 functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset);
487 SourceCode source = m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.parametersStartColumn);
488 MethodDefinitionNode* methodDef = new (m_parserArena) MethodDefinitionNode(location, m_vm->propertyNames->nullIdentifier, functionInfo.body, source);
489 return new (m_parserArena) PropertyNode(name, methodDef, type, PropertyNode::Unknown, SuperBinding::Needed, tag);
490 }
491
492 NEVER_INLINE PropertyNode* createGetterOrSetterProperty(VM* vm, ParserArena& parserArena, const JSTokenLocation& location, PropertyNode::Type type, bool,
493 double name, const ParserFunctionInfo<ASTBuilder>& functionInfo, ClassElementTag tag)
494 {
495 functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset);
496 const Identifier& ident = parserArena.identifierArena().makeNumericIdentifier(vm, name);
497 SourceCode source = m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.parametersStartColumn);
498 MethodDefinitionNode* methodDef = new (m_parserArena) MethodDefinitionNode(location, vm->propertyNames->nullIdentifier, functionInfo.body, source);
499 return new (m_parserArena) PropertyNode(ident, methodDef, type, PropertyNode::Unknown, SuperBinding::Needed, tag);
500 }
501
502 PropertyNode* createProperty(const Identifier* propertyName, ExpressionNode* node, PropertyNode::Type type, PropertyNode::PutType putType, bool, SuperBinding superBinding, InferName inferName, ClassElementTag tag)
503 {
504 if (inferName == InferName::Allowed) {
505 if (node->isBaseFuncExprNode()) {
506 auto metadata = static_cast<BaseFuncExprNode*>(node)->metadata();
507 metadata->setEcmaName(*propertyName);
508 } else if (node->isClassExprNode())
509 static_cast<ClassExprNode*>(node)->setEcmaName(*propertyName);
510 }
511 return new (m_parserArena) PropertyNode(*propertyName, node, type, putType, superBinding, tag);
512 }
513 PropertyNode* createProperty(ExpressionNode* node, PropertyNode::Type type, PropertyNode::PutType putType, bool, SuperBinding superBinding, ClassElementTag tag)
514 {
515 return new (m_parserArena) PropertyNode(node, type, putType, superBinding, tag);
516 }
517 PropertyNode* createProperty(VM* vm, ParserArena& parserArena, double propertyName, ExpressionNode* node, PropertyNode::Type type, PropertyNode::PutType putType, bool, SuperBinding superBinding, ClassElementTag tag)
518 {
519 return new (m_parserArena) PropertyNode(parserArena.identifierArena().makeNumericIdentifier(vm, propertyName), node, type, putType, superBinding, tag);
520 }
521 PropertyNode* createProperty(ExpressionNode* propertyName, ExpressionNode* node, PropertyNode::Type type, PropertyNode::PutType putType, bool, SuperBinding superBinding, ClassElementTag tag) { return new (m_parserArena) PropertyNode(propertyName, node, type, putType, superBinding, tag); }
522 PropertyListNode* createPropertyList(const JSTokenLocation& location, PropertyNode* property) { return new (m_parserArena) PropertyListNode(location, property); }
523 PropertyListNode* createPropertyList(const JSTokenLocation& location, PropertyNode* property, PropertyListNode* tail) { return new (m_parserArena) PropertyListNode(location, property, tail); }
524
525 ElementNode* createElementList(int elisions, ExpressionNode* expr) { return new (m_parserArena) ElementNode(elisions, expr); }
526 ElementNode* createElementList(ElementNode* elems, int elisions, ExpressionNode* expr) { return new (m_parserArena) ElementNode(elems, elisions, expr); }
527 ElementNode* createElementList(ArgumentListNode* elems)
528 {
529 ElementNode* head = new (m_parserArena) ElementNode(0, elems->m_expr);
530 ElementNode* tail = head;
531 elems = elems->m_next;
532 while (elems) {
533 tail = new (m_parserArena) ElementNode(tail, 0, elems->m_expr);
534 elems = elems->m_next;
535 }
536 return head;
537 }
538
539 FormalParameterList createFormalParameterList() { return new (m_parserArena) FunctionParameters(); }
540 void appendParameter(FormalParameterList list, DestructuringPattern pattern, ExpressionNode* defaultValue)
541 {
542 list->append(pattern, defaultValue);
543 tryInferNameInPattern(pattern, defaultValue);
544 }
545
546 CaseClauseNode* createClause(ExpressionNode* expr, JSC::SourceElements* statements) { return new (m_parserArena) CaseClauseNode(expr, statements); }
547 ClauseListNode* createClauseList(CaseClauseNode* clause) { return new (m_parserArena) ClauseListNode(clause); }
548 ClauseListNode* createClauseList(ClauseListNode* tail, CaseClauseNode* clause) { return new (m_parserArena) ClauseListNode(tail, clause); }
549
550 StatementNode* createFuncDeclStatement(const JSTokenLocation& location, const ParserFunctionInfo<ASTBuilder>& functionInfo)
551 {
552 FuncDeclNode* decl = new (m_parserArena) FuncDeclNode(location, *functionInfo.name, functionInfo.body,
553 m_sourceCode->subExpression(functionInfo.startOffset, functionInfo.endOffset, functionInfo.startLine, functionInfo.parametersStartColumn));
554 if (*functionInfo.name == m_vm->propertyNames->arguments)
555 usesArguments();
556 functionInfo.body->setLoc(functionInfo.startLine, functionInfo.endLine, location.startOffset, location.lineStartOffset);
557 return decl;
558 }
559
560 StatementNode* createClassDeclStatement(const JSTokenLocation& location, ClassExprNode* classExpression,
561 const JSTextPosition& classStart, const JSTextPosition& classEnd, unsigned startLine, unsigned endLine)
562 {
563 ExpressionNode* assign = createAssignResolve(location, classExpression->name(), classExpression, classStart, classStart + 1, classEnd, AssignmentContext::DeclarationStatement);
564 ClassDeclNode* decl = new (m_parserArena) ClassDeclNode(location, assign);
565 decl->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset);
566 return decl;
567 }
568
569 StatementNode* createBlockStatement(const JSTokenLocation& location, JSC::SourceElements* elements, int startLine, int endLine, VariableEnvironment& lexicalVariables, DeclarationStacks::FunctionStack&& functionStack)
570 {
571 BlockNode* block = new (m_parserArena) BlockNode(location, elements, lexicalVariables, WTFMove(functionStack));
572 block->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset);
573 return block;
574 }
575
576 StatementNode* createExprStatement(const JSTokenLocation& location, ExpressionNode* expr, const JSTextPosition& start, int end)
577 {
578 ExprStatementNode* result = new (m_parserArena) ExprStatementNode(location, expr);
579 result->setLoc(start.line, end, start.offset, start.lineStartOffset);
580 return result;
581 }
582
583 StatementNode* createIfStatement(const JSTokenLocation& location, ExpressionNode* condition, StatementNode* trueBlock, StatementNode* falseBlock, int start, int end)
584 {
585 IfElseNode* result = new (m_parserArena) IfElseNode(location, condition, trueBlock, falseBlock);
586 result->setLoc(start, end, location.startOffset, location.lineStartOffset);
587 return result;
588 }
589
590 StatementNode* createForLoop(const JSTokenLocation& location, ExpressionNode* initializer, ExpressionNode* condition, ExpressionNode* iter, StatementNode* statements, int start, int end, VariableEnvironment& lexicalVariables)
591 {
592 ForNode* result = new (m_parserArena) ForNode(location, initializer, condition, iter, statements, lexicalVariables);
593 result->setLoc(start, end, location.startOffset, location.lineStartOffset);
594 return result;
595 }
596
597 StatementNode* createForInLoop(const JSTokenLocation& location, ExpressionNode* lhs, ExpressionNode* iter, StatementNode* statements, const JSTokenLocation&, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end, VariableEnvironment& lexicalVariables)
598 {
599 ForInNode* result = new (m_parserArena) ForInNode(location, lhs, iter, statements, lexicalVariables);
600 result->setLoc(start, end, location.startOffset, location.lineStartOffset);
601 setExceptionLocation(result, eStart, eDivot, eEnd);
602 return result;
603 }
604
605 StatementNode* createForInLoop(const JSTokenLocation& location, DestructuringPatternNode* pattern, ExpressionNode* iter, StatementNode* statements, const JSTokenLocation& declLocation, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end, VariableEnvironment& lexicalVariables)
606 {
607 auto lexpr = new (m_parserArena) DestructuringAssignmentNode(declLocation, pattern, nullptr);
608 return createForInLoop(location, lexpr, iter, statements, declLocation, eStart, eDivot, eEnd, start, end, lexicalVariables);
609 }
610
611 StatementNode* createForOfLoop(bool isForAwait, const JSTokenLocation& location, ExpressionNode* lhs, ExpressionNode* iter, StatementNode* statements, const JSTokenLocation&, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end, VariableEnvironment& lexicalVariables)
612 {
613 ForOfNode* result = new (m_parserArena) ForOfNode(isForAwait, location, lhs, iter, statements, lexicalVariables);
614 result->setLoc(start, end, location.startOffset, location.lineStartOffset);
615 setExceptionLocation(result, eStart, eDivot, eEnd);
616 return result;
617 }
618
619 StatementNode* createForOfLoop(bool isForAwait, const JSTokenLocation& location, DestructuringPatternNode* pattern, ExpressionNode* iter, StatementNode* statements, const JSTokenLocation& declLocation, const JSTextPosition& eStart, const JSTextPosition& eDivot, const JSTextPosition& eEnd, int start, int end, VariableEnvironment& lexicalVariables)
620 {
621 auto lexpr = new (m_parserArena) DestructuringAssignmentNode(declLocation, pattern, nullptr);
622 return createForOfLoop(isForAwait, location, lexpr, iter, statements, declLocation, eStart, eDivot, eEnd, start, end, lexicalVariables);
623 }
624
625 bool isBindingNode(const DestructuringPattern& pattern)
626 {
627 return pattern->isBindingNode();
628 }
629
630 bool isLocation(const Expression& node)
631 {
632 return node->isLocation();
633 }
634
635 bool isAssignmentLocation(const Expression& node)
636 {
637 return node->isAssignmentLocation();
638 }
639
640 bool isObjectLiteral(const Expression& node)
641 {
642 return node->isObjectLiteral();
643 }
644
645 bool isArrayLiteral(const Expression& node)
646 {
647 return node->isArrayLiteral();
648 }
649
650 bool isObjectOrArrayLiteral(const Expression& node)
651 {
652 return isObjectLiteral(node) || isArrayLiteral(node);
653 }
654
655 bool isFunctionCall(const Expression& node)
656 {
657 return node->isFunctionCall();
658 }
659
660 bool shouldSkipPauseLocation(StatementNode* statement) const
661 {
662 return !statement || statement->isLabel();
663 }
664
665 StatementNode* createEmptyStatement(const JSTokenLocation& location) { return new (m_parserArena) EmptyStatementNode(location); }
666
667 StatementNode* createDeclarationStatement(const JSTokenLocation& location, ExpressionNode* expr, int start, int end)
668 {
669 StatementNode* result;
670 result = new (m_parserArena) DeclarationStatement(location, expr);
671 result->setLoc(start, end, location.startOffset, location.lineStartOffset);
672 return result;
673 }
674
675 ExpressionNode* createEmptyVarExpression(const JSTokenLocation& location, const Identifier& identifier)
676 {
677 return new (m_parserArena) EmptyVarExpression(location, identifier);
678 }
679
680 ExpressionNode* createEmptyLetExpression(const JSTokenLocation& location, const Identifier& identifier)
681 {
682 return new (m_parserArena) EmptyLetExpression(location, identifier);
683 }
684
685 StatementNode* createReturnStatement(const JSTokenLocation& location, ExpressionNode* expression, const JSTextPosition& start, const JSTextPosition& end)
686 {
687 ReturnNode* result = new (m_parserArena) ReturnNode(location, expression);
688 setExceptionLocation(result, start, end, end);
689 result->setLoc(start.line, end.line, start.offset, start.lineStartOffset);
690 return result;
691 }
692
693 StatementNode* createBreakStatement(const JSTokenLocation& location, const Identifier* ident, const JSTextPosition& start, const JSTextPosition& end)
694 {
695 BreakNode* result = new (m_parserArena) BreakNode(location, *ident);
696 setExceptionLocation(result, start, end, end);
697 result->setLoc(start.line, end.line, start.offset, start.lineStartOffset);
698 return result;
699 }
700
701 StatementNode* createContinueStatement(const JSTokenLocation& location, const Identifier* ident, const JSTextPosition& start, const JSTextPosition& end)
702 {
703 ContinueNode* result = new (m_parserArena) ContinueNode(location, *ident);
704 setExceptionLocation(result, start, end, end);
705 result->setLoc(start.line, end.line, start.offset, start.lineStartOffset);
706 return result;
707 }
708
709 StatementNode* createTryStatement(const JSTokenLocation& location, StatementNode* tryBlock, DestructuringPatternNode* catchPattern, StatementNode* catchBlock, StatementNode* finallyBlock, int startLine, int endLine, VariableEnvironment& catchEnvironment)
710 {
711 TryNode* result = new (m_parserArena) TryNode(location, tryBlock, catchPattern, catchBlock, catchEnvironment, finallyBlock);
712 result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset);
713 return result;
714 }
715
716 StatementNode* createSwitchStatement(const JSTokenLocation& location, ExpressionNode* expr, ClauseListNode* firstClauses, CaseClauseNode* defaultClause, ClauseListNode* secondClauses, int startLine, int endLine, VariableEnvironment& lexicalVariables, DeclarationStacks::FunctionStack&& functionStack)
717 {
718 CaseBlockNode* cases = new (m_parserArena) CaseBlockNode(firstClauses, defaultClause, secondClauses);
719 SwitchNode* result = new (m_parserArena) SwitchNode(location, expr, cases, lexicalVariables, WTFMove(functionStack));
720 result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset);
721 return result;
722 }
723
724 StatementNode* createWhileStatement(const JSTokenLocation& location, ExpressionNode* expr, StatementNode* statement, int startLine, int endLine)
725 {
726 WhileNode* result = new (m_parserArena) WhileNode(location, expr, statement);
727 result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset);
728 return result;
729 }
730
731 StatementNode* createDoWhileStatement(const JSTokenLocation& location, StatementNode* statement, ExpressionNode* expr, int startLine, int endLine)
732 {
733 DoWhileNode* result = new (m_parserArena) DoWhileNode(location, statement, expr);
734 result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset);
735 return result;
736 }
737
738 StatementNode* createLabelStatement(const JSTokenLocation& location, const Identifier* ident, StatementNode* statement, const JSTextPosition& start, const JSTextPosition& end)
739 {
740 LabelNode* result = new (m_parserArena) LabelNode(location, *ident, statement);
741 setExceptionLocation(result, start, end, end);
742 return result;
743 }
744
745 StatementNode* createWithStatement(const JSTokenLocation& location, ExpressionNode* expr, StatementNode* statement, unsigned start, const JSTextPosition& end, unsigned startLine, unsigned endLine)
746 {
747 usesWith();
748 WithNode* result = new (m_parserArena) WithNode(location, expr, statement, end, end - start);
749 result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset);
750 return result;
751 }
752
753 StatementNode* createThrowStatement(const JSTokenLocation& location, ExpressionNode* expr, const JSTextPosition& start, const JSTextPosition& end)
754 {
755 ThrowNode* result = new (m_parserArena) ThrowNode(location, expr);
756 result->setLoc(start.line, end.line, start.offset, start.lineStartOffset);
757 setExceptionLocation(result, start, end, end);
758 return result;
759 }
760
761 StatementNode* createDebugger(const JSTokenLocation& location, int startLine, int endLine)
762 {
763 DebuggerStatementNode* result = new (m_parserArena) DebuggerStatementNode(location);
764 result->setLoc(startLine, endLine, location.startOffset, location.lineStartOffset);
765 return result;
766 }
767
768 ModuleNameNode* createModuleName(const JSTokenLocation& location, const Identifier& moduleName)
769 {
770 return new (m_parserArena) ModuleNameNode(location, moduleName);
771 }
772
773 ImportSpecifierNode* createImportSpecifier(const JSTokenLocation& location, const Identifier& importedName, const Identifier& localName)
774 {
775 return new (m_parserArena) ImportSpecifierNode(location, importedName, localName);
776 }
777
778 ImportSpecifierListNode* createImportSpecifierList()
779 {
780 return new (m_parserArena) ImportSpecifierListNode();
781 }
782
783 void appendImportSpecifier(ImportSpecifierListNode* specifierList, ImportSpecifierNode* specifier)
784 {
785 specifierList->append(specifier);
786 }
787
788 StatementNode* createImportDeclaration(const JSTokenLocation& location, ImportSpecifierListNode* importSpecifierList, ModuleNameNode* moduleName)
789 {
790 return new (m_parserArena) ImportDeclarationNode(location, importSpecifierList, moduleName);
791 }
792
793 StatementNode* createExportAllDeclaration(const JSTokenLocation& location, ModuleNameNode* moduleName)
794 {
795 return new (m_parserArena) ExportAllDeclarationNode(location, moduleName);
796 }
797
798 StatementNode* createExportDefaultDeclaration(const JSTokenLocation& location, StatementNode* declaration, const Identifier& localName)
799 {
800 return new (m_parserArena) ExportDefaultDeclarationNode(location, declaration, localName);
801 }
802
803 StatementNode* createExportLocalDeclaration(const JSTokenLocation& location, StatementNode* declaration)
804 {
805 return new (m_parserArena) ExportLocalDeclarationNode(location, declaration);
806 }
807
808 StatementNode* createExportNamedDeclaration(const JSTokenLocation& location, ExportSpecifierListNode* exportSpecifierList, ModuleNameNode* moduleName)
809 {
810 return new (m_parserArena) ExportNamedDeclarationNode(location, exportSpecifierList, moduleName);
811 }
812
813 ExportSpecifierNode* createExportSpecifier(const JSTokenLocation& location, const Identifier& localName, const Identifier& exportedName)
814 {
815 return new (m_parserArena) ExportSpecifierNode(location, localName, exportedName);
816 }
817
818 ExportSpecifierListNode* createExportSpecifierList()
819 {
820 return new (m_parserArena) ExportSpecifierListNode();
821 }
822
823 void appendExportSpecifier(ExportSpecifierListNode* specifierList, ExportSpecifierNode* specifier)
824 {
825 specifierList->append(specifier);
826 }
827
828 void appendStatement(JSC::SourceElements* elements, JSC::StatementNode* statement)
829 {
830 elements->append(statement);
831 }
832
833 CommaNode* createCommaExpr(const JSTokenLocation& location, ExpressionNode* node)
834 {
835 return new (m_parserArena) CommaNode(location, node);
836 }
837
838 CommaNode* appendToCommaExpr(const JSTokenLocation& location, ExpressionNode*, ExpressionNode* tail, ExpressionNode* next)
839 {
840 ASSERT(tail->isCommaNode());
841 ASSERT(next);
842 CommaNode* newTail = new (m_parserArena) CommaNode(location, next);
843 static_cast<CommaNode*>(tail)->setNext(newTail);
844 return newTail;
845 }
846
847 int evalCount() const { return m_evalCount; }
848
849 void appendBinaryExpressionInfo(int& operandStackDepth, ExpressionNode* current, const JSTextPosition& exprStart, const JSTextPosition& lhs, const JSTextPosition& rhs, bool hasAssignments)
850 {
851 operandStackDepth++;
852 m_binaryOperandStack.append(std::make_pair(current, BinaryOpInfo(exprStart, lhs, rhs, hasAssignments)));
853 }
854
855 // Logic to handle datastructures used during parsing of binary expressions
856 void operatorStackPop(int& operatorStackDepth)
857 {
858 operatorStackDepth--;
859 m_binaryOperatorStack.removeLast();
860 }
861 bool operatorStackShouldReduce(int precedence)
862 {
863 // If the current precedence of the operator stack is the same to the one of the given operator,
864 // it depends on the associative whether we reduce the stack.
865 // If the operator is right associative, we should not reduce the stack right now.
866 if (precedence == m_binaryOperatorStack.last().second)
867 return !(m_binaryOperatorStack.last().first & RightAssociativeBinaryOpTokenFlag);
868 return precedence < m_binaryOperatorStack.last().second;
869 }
870 const BinaryOperand& getFromOperandStack(int i) { return m_binaryOperandStack[m_binaryOperandStack.size() + i]; }
871 void shrinkOperandStackBy(int& operandStackDepth, int amount)
872 {
873 operandStackDepth -= amount;
874 ASSERT(operandStackDepth >= 0);
875 m_binaryOperandStack.shrink(m_binaryOperandStack.size() - amount);
876 }
877 void appendBinaryOperation(const JSTokenLocation& location, int& operandStackDepth, int&, const BinaryOperand& lhs, const BinaryOperand& rhs)
878 {
879 operandStackDepth++;
880 m_binaryOperandStack.append(std::make_pair(makeBinaryNode(location, m_binaryOperatorStack.last().first, lhs, rhs), BinaryOpInfo(lhs.second, rhs.second)));
881 }
882 void operatorStackAppend(int& operatorStackDepth, int op, int precedence)
883 {
884 operatorStackDepth++;
885 m_binaryOperatorStack.append(std::make_pair(op, precedence));
886 }
887 ExpressionNode* popOperandStack(int&)
888 {
889 ExpressionNode* result = m_binaryOperandStack.last().first;
890 m_binaryOperandStack.removeLast();
891 return result;
892 }
893
894 void appendUnaryToken(int& tokenStackDepth, int type, const JSTextPosition& start)
895 {
896 tokenStackDepth++;
897 m_unaryTokenStack.append(std::make_pair(type, start));
898 }
899
900 int unaryTokenStackLastType(int&)
901 {
902 return m_unaryTokenStack.last().first;
903 }
904
905 const JSTextPosition& unaryTokenStackLastStart(int&)
906 {
907 return m_unaryTokenStack.last().second;
908 }
909
910 void unaryTokenStackRemoveLast(int& tokenStackDepth)
911 {
912 tokenStackDepth--;
913 m_unaryTokenStack.removeLast();
914 }
915
916 void assignmentStackAppend(int& assignmentStackDepth, ExpressionNode* node, const JSTextPosition& start, const JSTextPosition& divot, int assignmentCount, Operator op)
917 {
918 assignmentStackDepth++;
919 ASSERT(start.offset >= start.lineStartOffset);
920 ASSERT(divot.offset >= divot.lineStartOffset);
921 m_assignmentInfoStack.append(AssignmentInfo(node, start, divot, assignmentCount, op));
922 }
923
924 ExpressionNode* createAssignment(const JSTokenLocation& location, int& assignmentStackDepth, ExpressionNode* rhs, int initialAssignmentCount, int currentAssignmentCount, const JSTextPosition& lastTokenEnd)
925 {
926 AssignmentInfo& info = m_assignmentInfoStack.last();
927 ExpressionNode* result = makeAssignNode(location, info.m_node, info.m_op, rhs, info.m_initAssignments != initialAssignmentCount, info.m_initAssignments != currentAssignmentCount, info.m_start, info.m_divot + 1, lastTokenEnd);
928 m_assignmentInfoStack.removeLast();
929 assignmentStackDepth--;
930 return result;
931 }
932
933 const Identifier* getName(const Property& property) const { return property->name(); }
934 PropertyNode::Type getType(const Property& property) const { return property->type(); }
935
936 bool isResolve(ExpressionNode* expr) const { return expr->isResolveNode(); }
937
938 ExpressionNode* createDestructuringAssignment(const JSTokenLocation& location, DestructuringPattern pattern, ExpressionNode* initializer)
939 {
940 return new (m_parserArena) DestructuringAssignmentNode(location, pattern, initializer);
941 }
942
943 ArrayPattern createArrayPattern(const JSTokenLocation&)
944 {
945 return new (m_parserArena) ArrayPatternNode();
946 }
947
948 void appendArrayPatternSkipEntry(ArrayPattern node, const JSTokenLocation& location)
949 {
950 node->appendIndex(ArrayPatternNode::BindingType::Elision, location, 0, nullptr);
951 }
952
953 void appendArrayPatternEntry(ArrayPattern node, const JSTokenLocation& location, DestructuringPattern pattern, ExpressionNode* defaultValue)
954 {
955 node->appendIndex(ArrayPatternNode::BindingType::Element, location, pattern, defaultValue);
956 tryInferNameInPattern(pattern, defaultValue);
957 }
958
959 void appendArrayPatternRestEntry(ArrayPattern node, const JSTokenLocation& location, DestructuringPattern pattern)
960 {
961 node->appendIndex(ArrayPatternNode::BindingType::RestElement, location, pattern, nullptr);
962 }
963
964 void finishArrayPattern(ArrayPattern node, const JSTextPosition& divotStart, const JSTextPosition& divot, const JSTextPosition& divotEnd)
965 {
966 setExceptionLocation(node, divotStart, divot, divotEnd);
967 }
968
969 ObjectPattern createObjectPattern(const JSTokenLocation&)
970 {
971 return new (m_parserArena) ObjectPatternNode();
972 }
973
974 void appendObjectPatternEntry(ObjectPattern node, const JSTokenLocation& location, bool wasString, const Identifier& identifier, DestructuringPattern pattern, ExpressionNode* defaultValue)
975 {
976 node->appendEntry(location, identifier, wasString, pattern, defaultValue, ObjectPatternNode::BindingType::Element);
977 tryInferNameInPattern(pattern, defaultValue);
978 }
979
980 void appendObjectPatternEntry(VM& vm, ObjectPattern node, const JSTokenLocation& location, ExpressionNode* propertyExpression, DestructuringPattern pattern, ExpressionNode* defaultValue)
981 {
982 node->appendEntry(vm, location, propertyExpression, pattern, defaultValue, ObjectPatternNode::BindingType::Element);
983 tryInferNameInPattern(pattern, defaultValue);
984 }
985
986 void appendObjectPatternRestEntry(VM& vm, ObjectPattern node, const JSTokenLocation& location, DestructuringPattern pattern)
987 {
988 node->appendEntry(vm, location, nullptr, pattern, nullptr, ObjectPatternNode::BindingType::RestElement);
989 }
990
991 void setContainsObjectRestElement(ObjectPattern node, bool containsRestElement)
992 {
993 node->setContainsRestElement(containsRestElement);
994 }
995
996 void setContainsComputedProperty(ObjectPattern node, bool containsComputedProperty)
997 {
998 node->setContainsComputedProperty(containsComputedProperty);
999 }
1000
1001 BindingPattern createBindingLocation(const JSTokenLocation&, const Identifier& boundProperty, const JSTextPosition& start, const JSTextPosition& end, AssignmentContext context)
1002 {
1003 return new (m_parserArena) BindingNode(boundProperty, start, end, context);
1004 }
1005
1006 RestParameterNode* createRestParameter(DestructuringPatternNode* pattern, size_t numParametersToSkip)
1007 {
1008 return new (m_parserArena) RestParameterNode(pattern, numParametersToSkip);
1009 }
1010
1011 AssignmentElement createAssignmentElement(const Expression& assignmentTarget, const JSTextPosition& start, const JSTextPosition& end)
1012 {
1013 return new (m_parserArena) AssignmentElementNode(assignmentTarget, start, end);
1014 }
1015
1016 void setEndOffset(Node* node, int offset)
1017 {
1018 node->setEndOffset(offset);
1019 }
1020
1021 int endOffset(Node* node)
1022 {
1023 return node->endOffset();
1024 }
1025
1026 void setStartOffset(CaseClauseNode* node, int offset)
1027 {
1028 node->setStartOffset(offset);
1029 }
1030
1031 void setStartOffset(Node* node, int offset)
1032 {
1033 node->setStartOffset(offset);
1034 }
1035
1036 JSTextPosition breakpointLocation(Node* node)
1037 {
1038 node->setNeedsDebugHook();
1039 return node->position();
1040 }
1041
1042 void propagateArgumentsUse() { usesArguments(); }
1043
1044private:
1045 struct Scope {
1046 Scope()
1047 : m_features(0)
1048 , m_numConstants(0)
1049 {
1050 }
1051 int m_features;
1052 int m_numConstants;
1053 };
1054
1055 static void setExceptionLocation(ThrowableExpressionData* node, const JSTextPosition& divotStart, const JSTextPosition& divot, const JSTextPosition& divotEnd)
1056 {
1057 ASSERT(divot.offset >= divot.lineStartOffset);
1058 node->setExceptionSourceCode(divot, divotStart, divotEnd);
1059 }
1060
1061 void incConstants() { m_scope.m_numConstants++; }
1062 void usesThis() { m_scope.m_features |= ThisFeature; }
1063 void usesArrowFunction() { m_scope.m_features |= ArrowFunctionFeature; }
1064 void usesArguments() { m_scope.m_features |= ArgumentsFeature; }
1065 void usesWith() { m_scope.m_features |= WithFeature; }
1066 void usesSuperCall() { m_scope.m_features |= SuperCallFeature; }
1067 void usesSuperProperty() { m_scope.m_features |= SuperPropertyFeature; }
1068 void usesEval()
1069 {
1070 m_evalCount++;
1071 m_scope.m_features |= EvalFeature;
1072 }
1073 void usesNewTarget() { m_scope.m_features |= NewTargetFeature; }
1074 ExpressionNode* createIntegerLikeNumber(const JSTokenLocation& location, double d)
1075 {
1076 return new (m_parserArena) IntegerNode(location, d);
1077 }
1078 ExpressionNode* createDoubleLikeNumber(const JSTokenLocation& location, double d)
1079 {
1080 return new (m_parserArena) DoubleNode(location, d);
1081 }
1082 ExpressionNode* createBigIntWithSign(const JSTokenLocation& location, const Identifier& bigInt, uint8_t radix, bool sign)
1083 {
1084 return new (m_parserArena) BigIntNode(location, bigInt, radix, sign);
1085 }
1086 ExpressionNode* createNumberFromBinaryOperation(const JSTokenLocation& location, double value, const NumberNode& originalNodeA, const NumberNode& originalNodeB)
1087 {
1088 if (originalNodeA.isIntegerNode() && originalNodeB.isIntegerNode())
1089 return createIntegerLikeNumber(location, value);
1090 return createDoubleLikeNumber(location, value);
1091 }
1092 ExpressionNode* createNumberFromUnaryOperation(const JSTokenLocation& location, double value, const NumberNode& originalNode)
1093 {
1094 if (originalNode.isIntegerNode())
1095 return createIntegerLikeNumber(location, value);
1096 return createDoubleLikeNumber(location, value);
1097 }
1098 ExpressionNode* createBigIntFromUnaryOperation(const JSTokenLocation& location, bool sign, const BigIntNode& originalNode)
1099 {
1100 return createBigIntWithSign(location, originalNode.identifier(), originalNode.radix(), sign);
1101 }
1102
1103 void tryInferNameInPattern(DestructuringPattern pattern, ExpressionNode* defaultValue)
1104 {
1105 if (!defaultValue)
1106 return;
1107
1108 if (pattern->isBindingNode()) {
1109 const Identifier& ident = static_cast<BindingNode*>(pattern)->boundProperty();
1110 tryInferNameInPatternWithIdentifier(ident, defaultValue);
1111 } else if (pattern->isAssignmentElementNode()) {
1112 const ExpressionNode* assignmentTarget = static_cast<AssignmentElementNode*>(pattern)->assignmentTarget();
1113 if (assignmentTarget->isResolveNode()) {
1114 const Identifier& ident = static_cast<const ResolveNode*>(assignmentTarget)->identifier();
1115 tryInferNameInPatternWithIdentifier(ident, defaultValue);
1116 }
1117 }
1118 }
1119
1120 void tryInferNameInPatternWithIdentifier(const Identifier& ident, ExpressionNode* defaultValue)
1121 {
1122 if (defaultValue->isBaseFuncExprNode()) {
1123 auto metadata = static_cast<BaseFuncExprNode*>(defaultValue)->metadata();
1124 metadata->setEcmaName(ident);
1125 } else if (defaultValue->isClassExprNode())
1126 static_cast<ClassExprNode*>(defaultValue)->setEcmaName(ident);
1127 }
1128
1129 VM* m_vm;
1130 ParserArena& m_parserArena;
1131 SourceCode* m_sourceCode;
1132 Scope m_scope;
1133 Vector<BinaryOperand, 10, UnsafeVectorOverflow> m_binaryOperandStack;
1134 Vector<AssignmentInfo, 10, UnsafeVectorOverflow> m_assignmentInfoStack;
1135 Vector<std::pair<int, int>, 10, UnsafeVectorOverflow> m_binaryOperatorStack;
1136 Vector<std::pair<int, JSTextPosition>, 10, UnsafeVectorOverflow> m_unaryTokenStack;
1137 int m_evalCount;
1138};
1139
1140ExpressionNode* ASTBuilder::makeTypeOfNode(const JSTokenLocation& location, ExpressionNode* expr)
1141{
1142 if (expr->isResolveNode()) {
1143 ResolveNode* resolve = static_cast<ResolveNode*>(expr);
1144 return new (m_parserArena) TypeOfResolveNode(location, resolve->identifier());
1145 }
1146 return new (m_parserArena) TypeOfValueNode(location, expr);
1147}
1148
1149ExpressionNode* ASTBuilder::makeDeleteNode(const JSTokenLocation& location, ExpressionNode* expr, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end)
1150{
1151 if (!expr->isLocation())
1152 return new (m_parserArena) DeleteValueNode(location, expr);
1153 if (expr->isResolveNode()) {
1154 ResolveNode* resolve = static_cast<ResolveNode*>(expr);
1155 return new (m_parserArena) DeleteResolveNode(location, resolve->identifier(), divot, start, end);
1156 }
1157 if (expr->isBracketAccessorNode()) {
1158 BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(expr);
1159 return new (m_parserArena) DeleteBracketNode(location, bracket->base(), bracket->subscript(), divot, start, end);
1160 }
1161 ASSERT(expr->isDotAccessorNode());
1162 DotAccessorNode* dot = static_cast<DotAccessorNode*>(expr);
1163 return new (m_parserArena) DeleteDotNode(location, dot->base(), dot->identifier(), divot, start, end);
1164}
1165
1166ExpressionNode* ASTBuilder::makeNegateNode(const JSTokenLocation& location, ExpressionNode* n)
1167{
1168 if (n->isNumber()) {
1169 const NumberNode& numberNode = static_cast<const NumberNode&>(*n);
1170 return createNumberFromUnaryOperation(location, -numberNode.value(), numberNode);
1171 }
1172
1173 if (n->isBigInt()) {
1174 const BigIntNode& bigIntNode = static_cast<const BigIntNode&>(*n);
1175 return createBigIntFromUnaryOperation(location, !bigIntNode.sign(), bigIntNode);
1176 }
1177
1178 return new (m_parserArena) NegateNode(location, n);
1179}
1180
1181ExpressionNode* ASTBuilder::makeBitwiseNotNode(const JSTokenLocation& location, ExpressionNode* expr)
1182{
1183 if (expr->isNumber())
1184 return createIntegerLikeNumber(location, ~toInt32(static_cast<NumberNode*>(expr)->value()));
1185 return new (m_parserArena) BitwiseNotNode(location, expr);
1186}
1187
1188ExpressionNode* ASTBuilder::makePowNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
1189{
1190 auto* strippedExpr1 = expr1->stripUnaryPlus();
1191 auto* strippedExpr2 = expr2->stripUnaryPlus();
1192
1193 if (strippedExpr1->isNumber() && strippedExpr2->isNumber()) {
1194 const NumberNode& numberExpr1 = static_cast<NumberNode&>(*strippedExpr1);
1195 const NumberNode& numberExpr2 = static_cast<NumberNode&>(*strippedExpr2);
1196 return createNumberFromBinaryOperation(location, operationMathPow(numberExpr1.value(), numberExpr2.value()), numberExpr1, numberExpr2);
1197 }
1198
1199 if (strippedExpr1->isNumber())
1200 expr1 = strippedExpr1;
1201 if (strippedExpr2->isNumber())
1202 expr2 = strippedExpr2;
1203
1204 return new (m_parserArena) PowNode(location, expr1, expr2, rightHasAssignments);
1205}
1206
1207ExpressionNode* ASTBuilder::makeMultNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
1208{
1209 // FIXME: Unary + change the evaluation order.
1210 // https://bugs.webkit.org/show_bug.cgi?id=159968
1211 expr1 = expr1->stripUnaryPlus();
1212 expr2 = expr2->stripUnaryPlus();
1213
1214 if (expr1->isNumber() && expr2->isNumber()) {
1215 const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1);
1216 const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2);
1217 return createNumberFromBinaryOperation(location, numberExpr1.value() * numberExpr2.value(), numberExpr1, numberExpr2);
1218 }
1219
1220 if (expr1->isNumber() && static_cast<NumberNode*>(expr1)->value() == 1)
1221 return new (m_parserArena) UnaryPlusNode(location, expr2);
1222
1223 if (expr2->isNumber() && static_cast<NumberNode*>(expr2)->value() == 1)
1224 return new (m_parserArena) UnaryPlusNode(location, expr1);
1225
1226 return new (m_parserArena) MultNode(location, expr1, expr2, rightHasAssignments);
1227}
1228
1229ExpressionNode* ASTBuilder::makeDivNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
1230{
1231 // FIXME: Unary + change the evaluation order.
1232 // https://bugs.webkit.org/show_bug.cgi?id=159968
1233 expr1 = expr1->stripUnaryPlus();
1234 expr2 = expr2->stripUnaryPlus();
1235
1236 if (expr1->isNumber() && expr2->isNumber()) {
1237 const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1);
1238 const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2);
1239 double result = numberExpr1.value() / numberExpr2.value();
1240 if (static_cast<int64_t>(result) == result)
1241 return createNumberFromBinaryOperation(location, result, numberExpr1, numberExpr2);
1242 return createDoubleLikeNumber(location, result);
1243 }
1244 return new (m_parserArena) DivNode(location, expr1, expr2, rightHasAssignments);
1245}
1246
1247ExpressionNode* ASTBuilder::makeModNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
1248{
1249 // FIXME: Unary + change the evaluation order.
1250 // https://bugs.webkit.org/show_bug.cgi?id=159968
1251 expr1 = expr1->stripUnaryPlus();
1252 expr2 = expr2->stripUnaryPlus();
1253
1254 if (expr1->isNumber() && expr2->isNumber()) {
1255 const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1);
1256 const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2);
1257 return createIntegerLikeNumber(location, fmod(numberExpr1.value(), numberExpr2.value()));
1258 }
1259 return new (m_parserArena) ModNode(location, expr1, expr2, rightHasAssignments);
1260}
1261
1262ExpressionNode* ASTBuilder::makeAddNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
1263{
1264
1265 if (expr1->isNumber() && expr2->isNumber()) {
1266 const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1);
1267 const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2);
1268 return createNumberFromBinaryOperation(location, numberExpr1.value() + numberExpr2.value(), numberExpr1, numberExpr2);
1269 }
1270 return new (m_parserArena) AddNode(location, expr1, expr2, rightHasAssignments);
1271}
1272
1273ExpressionNode* ASTBuilder::makeSubNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
1274{
1275 // FIXME: Unary + change the evaluation order.
1276 // https://bugs.webkit.org/show_bug.cgi?id=159968
1277 expr1 = expr1->stripUnaryPlus();
1278 expr2 = expr2->stripUnaryPlus();
1279
1280 if (expr1->isNumber() && expr2->isNumber()) {
1281 const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1);
1282 const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2);
1283 return createNumberFromBinaryOperation(location, numberExpr1.value() - numberExpr2.value(), numberExpr1, numberExpr2);
1284 }
1285 return new (m_parserArena) SubNode(location, expr1, expr2, rightHasAssignments);
1286}
1287
1288ExpressionNode* ASTBuilder::makeLeftShiftNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
1289{
1290 if (expr1->isNumber() && expr2->isNumber()) {
1291 const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1);
1292 const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2);
1293 return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) << (toUInt32(numberExpr2.value()) & 0x1f));
1294 }
1295 return new (m_parserArena) LeftShiftNode(location, expr1, expr2, rightHasAssignments);
1296}
1297
1298ExpressionNode* ASTBuilder::makeRightShiftNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
1299{
1300 if (expr1->isNumber() && expr2->isNumber()) {
1301 const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1);
1302 const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2);
1303 return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) >> (toUInt32(numberExpr2.value()) & 0x1f));
1304 }
1305 return new (m_parserArena) RightShiftNode(location, expr1, expr2, rightHasAssignments);
1306}
1307
1308ExpressionNode* ASTBuilder::makeURightShiftNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
1309{
1310 if (expr1->isNumber() && expr2->isNumber()) {
1311 const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1);
1312 const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2);
1313 return createIntegerLikeNumber(location, toUInt32(numberExpr1.value()) >> (toUInt32(numberExpr2.value()) & 0x1f));
1314 }
1315 return new (m_parserArena) UnsignedRightShiftNode(location, expr1, expr2, rightHasAssignments);
1316}
1317
1318ExpressionNode* ASTBuilder::makeBitOrNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
1319{
1320 if (expr1->isNumber() && expr2->isNumber()) {
1321 const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1);
1322 const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2);
1323 return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) | toInt32(numberExpr2.value()));
1324 }
1325 return new (m_parserArena) BitOrNode(location, expr1, expr2, rightHasAssignments);
1326}
1327
1328ExpressionNode* ASTBuilder::makeBitAndNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
1329{
1330 if (expr1->isNumber() && expr2->isNumber()) {
1331 const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1);
1332 const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2);
1333 return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) & toInt32(numberExpr2.value()));
1334 }
1335 return new (m_parserArena) BitAndNode(location, expr1, expr2, rightHasAssignments);
1336}
1337
1338ExpressionNode* ASTBuilder::makeBitXOrNode(const JSTokenLocation& location, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
1339{
1340 if (expr1->isNumber() && expr2->isNumber()) {
1341 const NumberNode& numberExpr1 = static_cast<NumberNode&>(*expr1);
1342 const NumberNode& numberExpr2 = static_cast<NumberNode&>(*expr2);
1343 return createIntegerLikeNumber(location, toInt32(numberExpr1.value()) ^ toInt32(numberExpr2.value()));
1344 }
1345 return new (m_parserArena) BitXOrNode(location, expr1, expr2, rightHasAssignments);
1346}
1347
1348ExpressionNode* ASTBuilder::makeFunctionCallNode(const JSTokenLocation& location, ExpressionNode* func, bool previousBaseWasSuper, ArgumentsNode* args, const JSTextPosition& divotStart, const JSTextPosition& divot, const JSTextPosition& divotEnd, size_t callOrApplyChildDepth)
1349{
1350 ASSERT(divot.offset >= divot.lineStartOffset);
1351 if (func->isSuperNode())
1352 usesSuperCall();
1353
1354 if (func->isBytecodeIntrinsicNode()) {
1355 BytecodeIntrinsicNode* intrinsic = static_cast<BytecodeIntrinsicNode*>(func);
1356 if (intrinsic->type() == BytecodeIntrinsicNode::Type::Constant)
1357 return new (m_parserArena) BytecodeIntrinsicNode(BytecodeIntrinsicNode::Type::Function, location, intrinsic->emitter(), intrinsic->identifier(), args, divot, divotStart, divotEnd);
1358 }
1359 if (!func->isLocation())
1360 return new (m_parserArena) FunctionCallValueNode(location, func, args, divot, divotStart, divotEnd);
1361 if (func->isResolveNode()) {
1362 ResolveNode* resolve = static_cast<ResolveNode*>(func);
1363 const Identifier& identifier = resolve->identifier();
1364 if (identifier == m_vm->propertyNames->eval) {
1365 usesEval();
1366 return new (m_parserArena) EvalFunctionCallNode(location, args, divot, divotStart, divotEnd);
1367 }
1368 return new (m_parserArena) FunctionCallResolveNode(location, identifier, args, divot, divotStart, divotEnd);
1369 }
1370 if (func->isBracketAccessorNode()) {
1371 BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(func);
1372 FunctionCallBracketNode* node = new (m_parserArena) FunctionCallBracketNode(location, bracket->base(), bracket->subscript(), bracket->subscriptHasAssignments(), args, divot, divotStart, divotEnd);
1373 node->setSubexpressionInfo(bracket->divot(), bracket->divotEnd().offset);
1374 return node;
1375 }
1376 ASSERT(func->isDotAccessorNode());
1377 DotAccessorNode* dot = static_cast<DotAccessorNode*>(func);
1378 FunctionCallDotNode* node = nullptr;
1379 if (!previousBaseWasSuper && (dot->identifier() == m_vm->propertyNames->builtinNames().callPublicName() || dot->identifier() == m_vm->propertyNames->builtinNames().callPrivateName()))
1380 node = new (m_parserArena) CallFunctionCallDotNode(location, dot->base(), dot->identifier(), args, divot, divotStart, divotEnd, callOrApplyChildDepth);
1381 else if (!previousBaseWasSuper && (dot->identifier() == m_vm->propertyNames->builtinNames().applyPublicName() || dot->identifier() == m_vm->propertyNames->builtinNames().applyPrivateName())) {
1382 // FIXME: This check is only needed because we haven't taught the bytecode generator to inline
1383 // Reflect.apply yet. See https://bugs.webkit.org/show_bug.cgi?id=190668.
1384 if (!dot->base()->isResolveNode() || static_cast<ResolveNode*>(dot->base())->identifier() != "Reflect")
1385 node = new (m_parserArena) ApplyFunctionCallDotNode(location, dot->base(), dot->identifier(), args, divot, divotStart, divotEnd, callOrApplyChildDepth);
1386 }
1387 if (!node)
1388 node = new (m_parserArena) FunctionCallDotNode(location, dot->base(), dot->identifier(), args, divot, divotStart, divotEnd);
1389 node->setSubexpressionInfo(dot->divot(), dot->divotEnd().offset);
1390 return node;
1391}
1392
1393ExpressionNode* ASTBuilder::makeBinaryNode(const JSTokenLocation& location, int token, std::pair<ExpressionNode*, BinaryOpInfo> lhs, std::pair<ExpressionNode*, BinaryOpInfo> rhs)
1394{
1395 switch (token) {
1396 case OR:
1397 return new (m_parserArena) LogicalOpNode(location, lhs.first, rhs.first, OpLogicalOr);
1398
1399 case AND:
1400 return new (m_parserArena) LogicalOpNode(location, lhs.first, rhs.first, OpLogicalAnd);
1401
1402 case BITOR:
1403 return makeBitOrNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1404
1405 case BITXOR:
1406 return makeBitXOrNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1407
1408 case BITAND:
1409 return makeBitAndNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1410
1411 case EQEQ:
1412 return new (m_parserArena) EqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1413
1414 case NE:
1415 return new (m_parserArena) NotEqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1416
1417 case STREQ:
1418 return new (m_parserArena) StrictEqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1419
1420 case STRNEQ:
1421 return new (m_parserArena) NotStrictEqualNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1422
1423 case LT:
1424 return new (m_parserArena) LessNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1425
1426 case GT:
1427 return new (m_parserArena) GreaterNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1428
1429 case LE:
1430 return new (m_parserArena) LessEqNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1431
1432 case GE:
1433 return new (m_parserArena) GreaterEqNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1434
1435 case INSTANCEOF: {
1436 InstanceOfNode* node = new (m_parserArena) InstanceOfNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1437 setExceptionLocation(node, lhs.second.start, rhs.second.start, rhs.second.end);
1438 return node;
1439 }
1440
1441 case INTOKEN: {
1442 InNode* node = new (m_parserArena) InNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1443 setExceptionLocation(node, lhs.second.start, rhs.second.start, rhs.second.end);
1444 return node;
1445 }
1446
1447 case LSHIFT:
1448 return makeLeftShiftNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1449
1450 case RSHIFT:
1451 return makeRightShiftNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1452
1453 case URSHIFT:
1454 return makeURightShiftNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1455
1456 case PLUS:
1457 return makeAddNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1458
1459 case MINUS:
1460 return makeSubNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1461
1462 case TIMES:
1463 return makeMultNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1464
1465 case DIVIDE:
1466 return makeDivNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1467
1468 case MOD:
1469 return makeModNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1470
1471 case POW:
1472 return makePowNode(location, lhs.first, rhs.first, rhs.second.hasAssignment);
1473 }
1474 CRASH();
1475 return 0;
1476}
1477
1478ExpressionNode* ASTBuilder::makeAssignNode(const JSTokenLocation& location, ExpressionNode* loc, Operator op, ExpressionNode* expr, bool locHasAssignments, bool exprHasAssignments, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end)
1479{
1480 if (!loc->isLocation()) {
1481 ASSERT(loc->isFunctionCall());
1482 return new (m_parserArena) AssignErrorNode(location, divot, start, end);
1483 }
1484
1485 if (loc->isResolveNode()) {
1486 ResolveNode* resolve = static_cast<ResolveNode*>(loc);
1487 if (op == OpEqual) {
1488 if (expr->isBaseFuncExprNode()) {
1489 auto metadata = static_cast<BaseFuncExprNode*>(expr)->metadata();
1490 metadata->setEcmaName(resolve->identifier());
1491 } else if (expr->isClassExprNode())
1492 static_cast<ClassExprNode*>(expr)->setEcmaName(resolve->identifier());
1493 AssignResolveNode* node = new (m_parserArena) AssignResolveNode(location, resolve->identifier(), expr, AssignmentContext::AssignmentExpression);
1494 setExceptionLocation(node, start, divot, end);
1495 return node;
1496 }
1497 return new (m_parserArena) ReadModifyResolveNode(location, resolve->identifier(), op, expr, exprHasAssignments, divot, start, end);
1498 }
1499 if (loc->isBracketAccessorNode()) {
1500 BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(loc);
1501 if (op == OpEqual)
1502 return new (m_parserArena) AssignBracketNode(location, bracket->base(), bracket->subscript(), expr, locHasAssignments, exprHasAssignments, bracket->divot(), start, end);
1503 ReadModifyBracketNode* node = new (m_parserArena) ReadModifyBracketNode(location, bracket->base(), bracket->subscript(), op, expr, locHasAssignments, exprHasAssignments, divot, start, end);
1504 node->setSubexpressionInfo(bracket->divot(), bracket->divotEnd().offset);
1505 return node;
1506 }
1507 ASSERT(loc->isDotAccessorNode());
1508 DotAccessorNode* dot = static_cast<DotAccessorNode*>(loc);
1509 if (op == OpEqual)
1510 return new (m_parserArena) AssignDotNode(location, dot->base(), dot->identifier(), expr, exprHasAssignments, dot->divot(), start, end);
1511
1512 ReadModifyDotNode* node = new (m_parserArena) ReadModifyDotNode(location, dot->base(), dot->identifier(), op, expr, exprHasAssignments, divot, start, end);
1513 node->setSubexpressionInfo(dot->divot(), dot->divotEnd().offset);
1514 return node;
1515}
1516
1517ExpressionNode* ASTBuilder::makePrefixNode(const JSTokenLocation& location, ExpressionNode* expr, Operator op, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end)
1518{
1519 return new (m_parserArena) PrefixNode(location, expr, op, divot, start, end);
1520}
1521
1522ExpressionNode* ASTBuilder::makePostfixNode(const JSTokenLocation& location, ExpressionNode* expr, Operator op, const JSTextPosition& start, const JSTextPosition& divot, const JSTextPosition& end)
1523{
1524 return new (m_parserArena) PostfixNode(location, expr, op, divot, start, end);
1525}
1526
1527}
1528