1 | /*
|
---|
2 | * Copyright (C) 2010 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 | #ifndef ASTBuilder_h
|
---|
27 | #define ASTBuilder_h
|
---|
28 |
|
---|
29 | #include "NodeConstructors.h"
|
---|
30 | #include "SyntaxChecker.h"
|
---|
31 | #include <utility>
|
---|
32 |
|
---|
33 | namespace JSC {
|
---|
34 |
|
---|
35 | class ASTBuilder {
|
---|
36 | struct BinaryOpInfo {
|
---|
37 | BinaryOpInfo() {}
|
---|
38 | BinaryOpInfo(int s, int d, int e, bool r)
|
---|
39 | : start(s)
|
---|
40 | , divot(d)
|
---|
41 | , end(e)
|
---|
42 | , hasAssignment(r)
|
---|
43 | {
|
---|
44 | }
|
---|
45 | BinaryOpInfo(const BinaryOpInfo& lhs, const BinaryOpInfo& rhs)
|
---|
46 | : start(lhs.start)
|
---|
47 | , divot(rhs.start)
|
---|
48 | , end(rhs.end)
|
---|
49 | , hasAssignment(lhs.hasAssignment || rhs.hasAssignment)
|
---|
50 | {
|
---|
51 | }
|
---|
52 | int start;
|
---|
53 | int divot;
|
---|
54 | int end;
|
---|
55 | bool hasAssignment;
|
---|
56 | };
|
---|
57 |
|
---|
58 |
|
---|
59 | struct AssignmentInfo {
|
---|
60 | AssignmentInfo() {}
|
---|
61 | AssignmentInfo(ExpressionNode* node, int start, int divot, int initAssignments, Operator op)
|
---|
62 | : m_node(node)
|
---|
63 | , m_start(start)
|
---|
64 | , m_divot(divot)
|
---|
65 | , m_initAssignments(initAssignments)
|
---|
66 | , m_op(op)
|
---|
67 | {
|
---|
68 | }
|
---|
69 | ExpressionNode* m_node;
|
---|
70 | int m_start;
|
---|
71 | int m_divot;
|
---|
72 | int m_initAssignments;
|
---|
73 | Operator m_op;
|
---|
74 | };
|
---|
75 | public:
|
---|
76 | ASTBuilder(JSGlobalData* globalData, Lexer* lexer)
|
---|
77 | : m_globalData(globalData)
|
---|
78 | , m_lexer(lexer)
|
---|
79 | , m_evalCount(0)
|
---|
80 | {
|
---|
81 | m_scopes.append(Scope(globalData));
|
---|
82 | }
|
---|
83 |
|
---|
84 | typedef SyntaxChecker FunctionBodyBuilder;
|
---|
85 |
|
---|
86 | typedef ExpressionNode* Expression;
|
---|
87 | typedef JSC::SourceElements* SourceElements;
|
---|
88 | typedef ArgumentsNode* Arguments;
|
---|
89 | typedef CommaNode* Comma;
|
---|
90 | typedef PropertyNode* Property;
|
---|
91 | typedef PropertyListNode* PropertyList;
|
---|
92 | typedef ElementNode* ElementList;
|
---|
93 | typedef ArgumentListNode* ArgumentsList;
|
---|
94 | typedef ParameterNode* FormalParameterList;
|
---|
95 | typedef FunctionBodyNode* FunctionBody;
|
---|
96 | typedef StatementNode* Statement;
|
---|
97 | typedef ClauseListNode* ClauseList;
|
---|
98 | typedef CaseClauseNode* Clause;
|
---|
99 | typedef ConstDeclNode* ConstDeclList;
|
---|
100 | typedef std::pair<ExpressionNode*, BinaryOpInfo> BinaryOperand;
|
---|
101 |
|
---|
102 | static const bool CreatesAST = true;
|
---|
103 |
|
---|
104 | ExpressionNode* makeBinaryNode(int token, std::pair<ExpressionNode*, BinaryOpInfo>, std::pair<ExpressionNode*, BinaryOpInfo>);
|
---|
105 | ExpressionNode* makeFunctionCallNode(ExpressionNode* func, ArgumentsNode* args, int start, int divot, int end);
|
---|
106 |
|
---|
107 | JSC::SourceElements* createSourceElements() { return new (m_globalData) JSC::SourceElements(m_globalData); }
|
---|
108 |
|
---|
109 | ParserArenaData<DeclarationStacks::VarStack>* varDeclarations() { return m_scopes.last().m_varDeclarations; }
|
---|
110 | ParserArenaData<DeclarationStacks::FunctionStack>* funcDeclarations() { return m_scopes.last().m_funcDeclarations; }
|
---|
111 | int features() const { return m_scopes.last().m_features; }
|
---|
112 | int numConstants() const { return m_scopes.last().m_numConstants; }
|
---|
113 |
|
---|
114 | void appendToComma(CommaNode* commaNode, ExpressionNode* expr) { commaNode->append(expr); }
|
---|
115 |
|
---|
116 | CommaNode* createCommaExpr(ExpressionNode* lhs, ExpressionNode* rhs) { return new (m_globalData) CommaNode(m_globalData, lhs, rhs); }
|
---|
117 |
|
---|
118 | ExpressionNode* makeAssignNode(ExpressionNode* left, Operator, ExpressionNode* right, bool leftHasAssignments, bool rightHasAssignments, int start, int divot, int end);
|
---|
119 | ExpressionNode* makePrefixNode(ExpressionNode*, Operator, int start, int divot, int end);
|
---|
120 | ExpressionNode* makePostfixNode(ExpressionNode*, Operator, int start, int divot, int end);
|
---|
121 | ExpressionNode* makeTypeOfNode(ExpressionNode*);
|
---|
122 | ExpressionNode* makeDeleteNode(ExpressionNode*, int start, int divot, int end);
|
---|
123 | ExpressionNode* makeNegateNode(ExpressionNode*);
|
---|
124 | ExpressionNode* makeBitwiseNotNode(ExpressionNode*);
|
---|
125 | ExpressionNode* makeMultNode(ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
|
---|
126 | ExpressionNode* makeDivNode(ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
|
---|
127 | ExpressionNode* makeModNode(ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
|
---|
128 | ExpressionNode* makeAddNode(ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
|
---|
129 | ExpressionNode* makeSubNode(ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
|
---|
130 | ExpressionNode* makeBitXOrNode(ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
|
---|
131 | ExpressionNode* makeBitAndNode(ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
|
---|
132 | ExpressionNode* makeBitOrNode(ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
|
---|
133 | ExpressionNode* makeLeftShiftNode(ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
|
---|
134 | ExpressionNode* makeRightShiftNode(ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
|
---|
135 | ExpressionNode* makeURightShiftNode(ExpressionNode* left, ExpressionNode* right, bool rightHasAssignments);
|
---|
136 |
|
---|
137 | ExpressionNode* createLogicalNot(ExpressionNode* expr) { return new (m_globalData) LogicalNotNode(m_globalData, expr); }
|
---|
138 | ExpressionNode* createUnaryPlus(ExpressionNode* expr) { return new (m_globalData) UnaryPlusNode(m_globalData, expr); }
|
---|
139 | ExpressionNode* createVoid(ExpressionNode* expr)
|
---|
140 | {
|
---|
141 | incConstants();
|
---|
142 | return new (m_globalData) VoidNode(m_globalData, expr);
|
---|
143 | }
|
---|
144 | ExpressionNode* thisExpr()
|
---|
145 | {
|
---|
146 | usesThis();
|
---|
147 | return new (m_globalData) ThisNode(m_globalData);
|
---|
148 | }
|
---|
149 | ExpressionNode* createResolve(const Identifier* ident, int start)
|
---|
150 | {
|
---|
151 | if (m_globalData->propertyNames->arguments == *ident)
|
---|
152 | usesArguments();
|
---|
153 | return new (m_globalData) ResolveNode(m_globalData, *ident, start);
|
---|
154 | }
|
---|
155 | ExpressionNode* createObjectLiteral() { return new (m_globalData) ObjectLiteralNode(m_globalData); }
|
---|
156 | ExpressionNode* createObjectLiteral(PropertyListNode* properties) { return new (m_globalData) ObjectLiteralNode(m_globalData, properties); }
|
---|
157 |
|
---|
158 | ExpressionNode* createArray(int elisions)
|
---|
159 | {
|
---|
160 | if (elisions)
|
---|
161 | incConstants();
|
---|
162 | return new (m_globalData) ArrayNode(m_globalData, elisions);
|
---|
163 | }
|
---|
164 |
|
---|
165 | ExpressionNode* createArray(ElementNode* elems) { return new (m_globalData) ArrayNode(m_globalData, elems); }
|
---|
166 | ExpressionNode* createArray(int elisions, ElementNode* elems)
|
---|
167 | {
|
---|
168 | if (elisions)
|
---|
169 | incConstants();
|
---|
170 | return new (m_globalData) ArrayNode(m_globalData, elisions, elems);
|
---|
171 | }
|
---|
172 | ExpressionNode* createNumberExpr(double d)
|
---|
173 | {
|
---|
174 | incConstants();
|
---|
175 | return new (m_globalData) NumberNode(m_globalData, d);
|
---|
176 | }
|
---|
177 |
|
---|
178 | ExpressionNode* createString(const Identifier* string)
|
---|
179 | {
|
---|
180 | incConstants();
|
---|
181 | return new (m_globalData) StringNode(m_globalData, *string);
|
---|
182 | }
|
---|
183 |
|
---|
184 | ExpressionNode* createBoolean(bool b)
|
---|
185 | {
|
---|
186 | incConstants();
|
---|
187 | return new (m_globalData) BooleanNode(m_globalData, b);
|
---|
188 | }
|
---|
189 |
|
---|
190 | ExpressionNode* createNull()
|
---|
191 | {
|
---|
192 | incConstants();
|
---|
193 | return new (m_globalData) NullNode(m_globalData);
|
---|
194 | }
|
---|
195 |
|
---|
196 | ExpressionNode* createBracketAccess(ExpressionNode* base, ExpressionNode* property, bool propertyHasAssignments, int start, int divot, int end)
|
---|
197 | {
|
---|
198 | BracketAccessorNode* node = new (m_globalData) BracketAccessorNode(m_globalData, base, property, propertyHasAssignments);
|
---|
199 | setExceptionLocation(node, start, divot, end);
|
---|
200 | return node;
|
---|
201 | }
|
---|
202 |
|
---|
203 | ExpressionNode* createDotAccess(ExpressionNode* base, const Identifier& property, int start, int divot, int end)
|
---|
204 | {
|
---|
205 | DotAccessorNode* node = new (m_globalData) DotAccessorNode(m_globalData, base, property);
|
---|
206 | setExceptionLocation(node, start, divot, end);
|
---|
207 | return node;
|
---|
208 | }
|
---|
209 |
|
---|
210 | ExpressionNode* createRegex(const Identifier& pattern, const Identifier& flags, int start)
|
---|
211 | {
|
---|
212 | RegExpNode* node = new (m_globalData) RegExpNode(m_globalData, pattern, flags);
|
---|
213 | int size = pattern.size() + 2; // + 2 for the two /'s
|
---|
214 | setExceptionLocation(node, start, start + size, start + size);
|
---|
215 | return node;
|
---|
216 | }
|
---|
217 |
|
---|
218 | ExpressionNode* createNewExpr(ExpressionNode* expr, ArgumentsNode* arguments, int start, int divot, int end)
|
---|
219 | {
|
---|
220 | NewExprNode* node = new (m_globalData) NewExprNode(m_globalData, expr, arguments);
|
---|
221 | setExceptionLocation(node, start, divot, end);
|
---|
222 | return node;
|
---|
223 | }
|
---|
224 |
|
---|
225 | ExpressionNode* createNewExpr(ExpressionNode* expr, int start, int end)
|
---|
226 | {
|
---|
227 | NewExprNode* node = new (m_globalData) NewExprNode(m_globalData, expr);
|
---|
228 | setExceptionLocation(node, start, end, end);
|
---|
229 | return node;
|
---|
230 | }
|
---|
231 |
|
---|
232 | ExpressionNode* createConditionalExpr(ExpressionNode* condition, ExpressionNode* lhs, ExpressionNode* rhs)
|
---|
233 | {
|
---|
234 | return new (m_globalData) ConditionalNode(m_globalData, condition, lhs, rhs);
|
---|
235 | }
|
---|
236 |
|
---|
237 | ExpressionNode* createAssignResolve(const Identifier& ident, ExpressionNode* rhs, bool rhsHasAssignment, int start, int divot, int end)
|
---|
238 | {
|
---|
239 | AssignResolveNode* node = new (m_globalData) AssignResolveNode(m_globalData, ident, rhs, rhsHasAssignment);
|
---|
240 | setExceptionLocation(node, start, divot, end);
|
---|
241 | return node;
|
---|
242 | }
|
---|
243 |
|
---|
244 | ExpressionNode* createFunctionExpr(const Identifier* name, FunctionBodyNode* body, ParameterNode* parameters, int openBracePos, int closeBracePos, int bodyStartLine, int bodyEndLine)
|
---|
245 | {
|
---|
246 | FuncExprNode* result = new (m_globalData) FuncExprNode(m_globalData, *name, body, m_lexer->sourceCode(openBracePos, closeBracePos, bodyStartLine), parameters);
|
---|
247 | body->setLoc(bodyStartLine, bodyEndLine);
|
---|
248 | return result;
|
---|
249 | }
|
---|
250 |
|
---|
251 | FunctionBodyNode* createFunctionBody()
|
---|
252 | {
|
---|
253 | usesClosures();
|
---|
254 | return FunctionBodyNode::create(m_globalData);
|
---|
255 | }
|
---|
256 |
|
---|
257 | template <bool> PropertyNode* createGetterOrSetterProperty(PropertyNode::Type type, const Identifier* name, ParameterNode* params, FunctionBodyNode* body, int openBracePos, int closeBracePos, int bodyStartLine, int bodyEndLine)
|
---|
258 | {
|
---|
259 | ASSERT(name);
|
---|
260 | body->setLoc(bodyStartLine, bodyEndLine);
|
---|
261 | return new (m_globalData) PropertyNode(m_globalData, *name, new (m_globalData) FuncExprNode(m_globalData, m_globalData->propertyNames->nullIdentifier, body, m_lexer->sourceCode(openBracePos, closeBracePos, bodyStartLine), params), type);
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | ArgumentsNode* createArguments() { return new (m_globalData) ArgumentsNode(m_globalData); }
|
---|
266 | ArgumentsNode* createArguments(ArgumentListNode* args) { return new (m_globalData) ArgumentsNode(m_globalData, args); }
|
---|
267 | ArgumentListNode* createArgumentsList(ExpressionNode* arg) { return new (m_globalData) ArgumentListNode(m_globalData, arg); }
|
---|
268 | ArgumentListNode* createArgumentsList(ArgumentListNode* args, ExpressionNode* arg) { return new (m_globalData) ArgumentListNode(m_globalData, args, arg); }
|
---|
269 |
|
---|
270 | template <bool> PropertyNode* createProperty(const Identifier* propertyName, ExpressionNode* node, PropertyNode::Type type) { return new (m_globalData) PropertyNode(m_globalData, *propertyName, node, type); }
|
---|
271 | template <bool> PropertyNode* createProperty(JSGlobalData*, double propertyName, ExpressionNode* node, PropertyNode::Type type) { return new (m_globalData) PropertyNode(m_globalData, propertyName, node, type); }
|
---|
272 | PropertyListNode* createPropertyList(PropertyNode* property) { return new (m_globalData) PropertyListNode(m_globalData, property); }
|
---|
273 | PropertyListNode* createPropertyList(PropertyNode* property, PropertyListNode* tail) { return new (m_globalData) PropertyListNode(m_globalData, property, tail); }
|
---|
274 |
|
---|
275 | ElementNode* createElementList(int elisions, ExpressionNode* expr) { return new (m_globalData) ElementNode(m_globalData, elisions, expr); }
|
---|
276 | ElementNode* createElementList(ElementNode* elems, int elisions, ExpressionNode* expr) { return new (m_globalData) ElementNode(m_globalData, elems, elisions, expr); }
|
---|
277 |
|
---|
278 | ParameterNode* createFormalParameterList(const Identifier& ident) { return new (m_globalData) ParameterNode(m_globalData, ident); }
|
---|
279 | ParameterNode* createFormalParameterList(ParameterNode* list, const Identifier& ident) { return new (m_globalData) ParameterNode(m_globalData, list, ident); }
|
---|
280 |
|
---|
281 | CaseClauseNode* createClause(ExpressionNode* expr, JSC::SourceElements* statements) { return new (m_globalData) CaseClauseNode(m_globalData, expr, statements); }
|
---|
282 | ClauseListNode* createClauseList(CaseClauseNode* clause) { return new (m_globalData) ClauseListNode(m_globalData, clause); }
|
---|
283 | ClauseListNode* createClauseList(ClauseListNode* tail, CaseClauseNode* clause) { return new (m_globalData) ClauseListNode(m_globalData, tail, clause); }
|
---|
284 |
|
---|
285 | void setUsesArguments(FunctionBodyNode* node) { node->setUsesArguments(); }
|
---|
286 |
|
---|
287 | StatementNode* createFuncDeclStatement(const Identifier* name, FunctionBodyNode* body, ParameterNode* parameters, int openBracePos, int closeBracePos, int bodyStartLine, int bodyEndLine)
|
---|
288 | {
|
---|
289 | FuncDeclNode* decl = new (m_globalData) FuncDeclNode(m_globalData, *name, body, m_lexer->sourceCode(openBracePos, closeBracePos, bodyStartLine), parameters);
|
---|
290 | if (*name == m_globalData->propertyNames->arguments)
|
---|
291 | usesArguments();
|
---|
292 | m_scopes.last().m_funcDeclarations->data.append(decl->body());
|
---|
293 | body->setLoc(bodyStartLine, bodyEndLine);
|
---|
294 | return decl;
|
---|
295 | }
|
---|
296 |
|
---|
297 | StatementNode* createBlockStatement(JSC::SourceElements* elements, int startLine, int endLine)
|
---|
298 | {
|
---|
299 | BlockNode* block = new (m_globalData) BlockNode(m_globalData, elements);
|
---|
300 | block->setLoc(startLine, endLine);
|
---|
301 | return block;
|
---|
302 | }
|
---|
303 |
|
---|
304 | StatementNode* createExprStatement(ExpressionNode* expr, int start, int end)
|
---|
305 | {
|
---|
306 | ExprStatementNode* result = new (m_globalData) ExprStatementNode(m_globalData, expr);
|
---|
307 | result->setLoc(start, end);
|
---|
308 | return result;
|
---|
309 | }
|
---|
310 |
|
---|
311 | StatementNode* createIfStatement(ExpressionNode* condition, StatementNode* trueBlock, int start, int end)
|
---|
312 | {
|
---|
313 | IfNode* result = new (m_globalData) IfNode(m_globalData, condition, trueBlock);
|
---|
314 | result->setLoc(start, end);
|
---|
315 | return result;
|
---|
316 | }
|
---|
317 |
|
---|
318 | StatementNode* createIfStatement(ExpressionNode* condition, StatementNode* trueBlock, StatementNode* falseBlock, int start, int end)
|
---|
319 | {
|
---|
320 | IfNode* result = new (m_globalData) IfElseNode(m_globalData, condition, trueBlock, falseBlock);
|
---|
321 | result->setLoc(start, end);
|
---|
322 | return result;
|
---|
323 | }
|
---|
324 |
|
---|
325 | StatementNode* createForLoop(ExpressionNode* initializer, ExpressionNode* condition, ExpressionNode* iter, StatementNode* statements, bool b, int start, int end)
|
---|
326 | {
|
---|
327 | ForNode* result = new (m_globalData) ForNode(m_globalData, initializer, condition, iter, statements, b);
|
---|
328 | result->setLoc(start, end);
|
---|
329 | return result;
|
---|
330 | }
|
---|
331 |
|
---|
332 | StatementNode* createForInLoop(const Identifier* ident, ExpressionNode* initializer, ExpressionNode* iter, StatementNode* statements, int start, int divot, int end, int initStart, int initEnd, int startLine, int endLine)
|
---|
333 | {
|
---|
334 | ForInNode* result = new (m_globalData) ForInNode(m_globalData, *ident, initializer, iter, statements, initStart, initStart - start, initEnd - initStart);
|
---|
335 | result->setLoc(startLine, endLine);
|
---|
336 | setExceptionLocation(result, start, divot + 1, end);
|
---|
337 | return result;
|
---|
338 | }
|
---|
339 |
|
---|
340 | StatementNode* createForInLoop(ExpressionNode* lhs, ExpressionNode* iter, StatementNode* statements, int eStart, int eDivot, int eEnd, int start, int end)
|
---|
341 | {
|
---|
342 | ForInNode* result = new (m_globalData) ForInNode(m_globalData, lhs, iter, statements);
|
---|
343 | result->setLoc(start, end);
|
---|
344 | setExceptionLocation(result, eStart, eDivot, eEnd);
|
---|
345 | return result;
|
---|
346 | }
|
---|
347 |
|
---|
348 | StatementNode* createEmptyStatement() { return new (m_globalData) EmptyStatementNode(m_globalData); }
|
---|
349 |
|
---|
350 | StatementNode* createVarStatement(ExpressionNode* expr, int start, int end)
|
---|
351 | {
|
---|
352 | StatementNode* result;
|
---|
353 | if (!expr)
|
---|
354 | result = new (m_globalData) EmptyStatementNode(m_globalData);
|
---|
355 | else
|
---|
356 | result = new (m_globalData) VarStatementNode(m_globalData, expr);
|
---|
357 | result->setLoc(start, end);
|
---|
358 | return result;
|
---|
359 | }
|
---|
360 |
|
---|
361 | StatementNode* createReturnStatement(ExpressionNode* expression, int eStart, int eEnd, int startLine, int endLine)
|
---|
362 | {
|
---|
363 | ReturnNode* result = new (m_globalData) ReturnNode(m_globalData, expression);
|
---|
364 | setExceptionLocation(result, eStart, eEnd, eEnd);
|
---|
365 | result->setLoc(startLine, endLine);
|
---|
366 | return result;
|
---|
367 | }
|
---|
368 |
|
---|
369 | StatementNode* createBreakStatement(int eStart, int eEnd, int startLine, int endLine)
|
---|
370 | {
|
---|
371 | BreakNode* result = new (m_globalData) BreakNode(m_globalData);
|
---|
372 | setExceptionLocation(result, eStart, eEnd, eEnd);
|
---|
373 | result->setLoc(startLine, endLine);
|
---|
374 | return result;
|
---|
375 | }
|
---|
376 |
|
---|
377 | StatementNode* createBreakStatement(const Identifier* ident, int eStart, int eEnd, int startLine, int endLine)
|
---|
378 | {
|
---|
379 | BreakNode* result = new (m_globalData) BreakNode(m_globalData, *ident);
|
---|
380 | setExceptionLocation(result, eStart, eEnd, eEnd);
|
---|
381 | result->setLoc(startLine, endLine);
|
---|
382 | return result;
|
---|
383 | }
|
---|
384 |
|
---|
385 | StatementNode* createContinueStatement(int eStart, int eEnd, int startLine, int endLine)
|
---|
386 | {
|
---|
387 | ContinueNode* result = new (m_globalData) ContinueNode(m_globalData);
|
---|
388 | setExceptionLocation(result, eStart, eEnd, eEnd);
|
---|
389 | result->setLoc(startLine, endLine);
|
---|
390 | return result;
|
---|
391 | }
|
---|
392 |
|
---|
393 | StatementNode* createContinueStatement(const Identifier* ident, int eStart, int eEnd, int startLine, int endLine)
|
---|
394 | {
|
---|
395 | ContinueNode* result = new (m_globalData) ContinueNode(m_globalData, *ident);
|
---|
396 | setExceptionLocation(result, eStart, eEnd, eEnd);
|
---|
397 | result->setLoc(startLine, endLine);
|
---|
398 | return result;
|
---|
399 | }
|
---|
400 |
|
---|
401 | StatementNode* createTryStatement(StatementNode* tryBlock, const Identifier* ident, bool catchHasEval, StatementNode* catchBlock, StatementNode* finallyBlock, int startLine, int endLine)
|
---|
402 | {
|
---|
403 | TryNode* result = new (m_globalData) TryNode(m_globalData, tryBlock, *ident, catchHasEval, catchBlock, finallyBlock);
|
---|
404 | if (catchBlock)
|
---|
405 | usesCatch();
|
---|
406 | result->setLoc(startLine, endLine);
|
---|
407 | return result;
|
---|
408 | }
|
---|
409 |
|
---|
410 | StatementNode* createSwitchStatement(ExpressionNode* expr, ClauseListNode* firstClauses, CaseClauseNode* defaultClause, ClauseListNode* secondClauses, int startLine, int endLine)
|
---|
411 | {
|
---|
412 | CaseBlockNode* cases = new (m_globalData) CaseBlockNode(m_globalData, firstClauses, defaultClause, secondClauses);
|
---|
413 | SwitchNode* result = new (m_globalData) SwitchNode(m_globalData, expr, cases);
|
---|
414 | result->setLoc(startLine, endLine);
|
---|
415 | return result;
|
---|
416 | }
|
---|
417 |
|
---|
418 | StatementNode* createWhileStatement(ExpressionNode* expr, StatementNode* statement, int startLine, int endLine)
|
---|
419 | {
|
---|
420 | WhileNode* result = new (m_globalData) WhileNode(m_globalData, expr, statement);
|
---|
421 | result->setLoc(startLine, endLine);
|
---|
422 | return result;
|
---|
423 | }
|
---|
424 |
|
---|
425 | StatementNode* createDoWhileStatement(StatementNode* statement, ExpressionNode* expr, int startLine, int endLine)
|
---|
426 | {
|
---|
427 | DoWhileNode* result = new (m_globalData) DoWhileNode(m_globalData, statement, expr);
|
---|
428 | result->setLoc(startLine, endLine);
|
---|
429 | return result;
|
---|
430 | }
|
---|
431 |
|
---|
432 | StatementNode* createLabelStatement(const Identifier* ident, StatementNode* statement, int start, int end)
|
---|
433 | {
|
---|
434 | LabelNode* result = new (m_globalData) LabelNode(m_globalData, *ident, statement);
|
---|
435 | setExceptionLocation(result, start, end, end);
|
---|
436 | return result;
|
---|
437 | }
|
---|
438 |
|
---|
439 | StatementNode* createWithStatement(ExpressionNode* expr, StatementNode* statement, int start, int end, int startLine, int endLine)
|
---|
440 | {
|
---|
441 | usesWith();
|
---|
442 | WithNode* result = new (m_globalData) WithNode(m_globalData, expr, statement, end, end - start);
|
---|
443 | result->setLoc(startLine, endLine);
|
---|
444 | return result;
|
---|
445 | }
|
---|
446 |
|
---|
447 | StatementNode* createThrowStatement(ExpressionNode* expr, int start, int end, int startLine, int endLine)
|
---|
448 | {
|
---|
449 | ThrowNode* result = new (m_globalData) ThrowNode(m_globalData, expr);
|
---|
450 | result->setLoc(startLine, endLine);
|
---|
451 | setExceptionLocation(result, start, end, end);
|
---|
452 | return result;
|
---|
453 | }
|
---|
454 |
|
---|
455 | StatementNode* createDebugger(int startLine, int endLine)
|
---|
456 | {
|
---|
457 | DebuggerStatementNode* result = new (m_globalData) DebuggerStatementNode(m_globalData);
|
---|
458 | result->setLoc(startLine, endLine);
|
---|
459 | return result;
|
---|
460 | }
|
---|
461 |
|
---|
462 | StatementNode* createConstStatement(ConstDeclNode* decls, int startLine, int endLine)
|
---|
463 | {
|
---|
464 | ConstStatementNode* result = new (m_globalData) ConstStatementNode(m_globalData, decls);
|
---|
465 | result->setLoc(startLine, endLine);
|
---|
466 | return result;
|
---|
467 | }
|
---|
468 |
|
---|
469 | ConstDeclNode* appendConstDecl(ConstDeclNode* tail, const Identifier* name, ExpressionNode* initializer)
|
---|
470 | {
|
---|
471 | ConstDeclNode* result = new (m_globalData) ConstDeclNode(m_globalData, *name, initializer);
|
---|
472 | if (tail)
|
---|
473 | tail->m_next = result;
|
---|
474 | return result;
|
---|
475 | }
|
---|
476 |
|
---|
477 | void appendStatement(JSC::SourceElements* elements, JSC::StatementNode* statement)
|
---|
478 | {
|
---|
479 | elements->append(statement);
|
---|
480 | }
|
---|
481 |
|
---|
482 | void addVar(const Identifier* ident, int attrs)
|
---|
483 | {
|
---|
484 | if (m_globalData->propertyNames->arguments == *ident)
|
---|
485 | usesArguments();
|
---|
486 | m_scopes.last().m_varDeclarations->data.append(std::make_pair(ident, attrs));
|
---|
487 | }
|
---|
488 |
|
---|
489 | ExpressionNode* combineCommaNodes(ExpressionNode* list, ExpressionNode* init)
|
---|
490 | {
|
---|
491 | if (!list)
|
---|
492 | return init;
|
---|
493 | if (list->isCommaNode()) {
|
---|
494 | static_cast<CommaNode*>(list)->append(init);
|
---|
495 | return list;
|
---|
496 | }
|
---|
497 | return new (m_globalData) CommaNode(m_globalData, list, init);
|
---|
498 | }
|
---|
499 |
|
---|
500 | int evalCount() const { return m_evalCount; }
|
---|
501 |
|
---|
502 | void appendBinaryExpressionInfo(int& operandStackDepth, ExpressionNode* current, int exprStart, int lhs, int rhs, bool hasAssignments)
|
---|
503 | {
|
---|
504 | operandStackDepth++;
|
---|
505 | m_binaryOperandStack.append(std::make_pair(current, BinaryOpInfo(exprStart, lhs, rhs, hasAssignments)));
|
---|
506 | }
|
---|
507 |
|
---|
508 | // Logic to handle datastructures used during parsing of binary expressions
|
---|
509 | void operatorStackPop(int& operatorStackDepth)
|
---|
510 | {
|
---|
511 | operatorStackDepth--;
|
---|
512 | m_binaryOperatorStack.removeLast();
|
---|
513 | }
|
---|
514 | bool operatorStackHasHigherPrecedence(int&, int precedence)
|
---|
515 | {
|
---|
516 | return precedence <= m_binaryOperatorStack.last().second;
|
---|
517 | }
|
---|
518 | const BinaryOperand& getFromOperandStack(int i) { return m_binaryOperandStack[m_binaryOperandStack.size() + i]; }
|
---|
519 | void shrinkOperandStackBy(int& operandStackDepth, int amount)
|
---|
520 | {
|
---|
521 | operandStackDepth -= amount;
|
---|
522 | ASSERT(operandStackDepth >= 0);
|
---|
523 | m_binaryOperandStack.resize(m_binaryOperandStack.size() - amount);
|
---|
524 | }
|
---|
525 | void appendBinaryOperation(int& operandStackDepth, int&, const BinaryOperand& lhs, const BinaryOperand& rhs)
|
---|
526 | {
|
---|
527 | operandStackDepth++;
|
---|
528 | m_binaryOperandStack.append(std::make_pair(makeBinaryNode(m_binaryOperatorStack.last().first, lhs, rhs), BinaryOpInfo(lhs.second, rhs.second)));
|
---|
529 | }
|
---|
530 | void operatorStackAppend(int& operatorStackDepth, int op, int precedence)
|
---|
531 | {
|
---|
532 | operatorStackDepth++;
|
---|
533 | m_binaryOperatorStack.append(std::make_pair(op, precedence));
|
---|
534 | }
|
---|
535 | ExpressionNode* popOperandStack(int&)
|
---|
536 | {
|
---|
537 | ExpressionNode* result = m_binaryOperandStack.last().first;
|
---|
538 | m_binaryOperandStack.removeLast();
|
---|
539 | return result;
|
---|
540 | }
|
---|
541 |
|
---|
542 | void appendUnaryToken(int& tokenStackDepth, int type, int start)
|
---|
543 | {
|
---|
544 | tokenStackDepth++;
|
---|
545 | m_unaryTokenStack.append(std::make_pair(type, start));
|
---|
546 | }
|
---|
547 |
|
---|
548 | int unaryTokenStackLastType(int&)
|
---|
549 | {
|
---|
550 | return m_unaryTokenStack.last().first;
|
---|
551 | }
|
---|
552 |
|
---|
553 | int unaryTokenStackLastStart(int&)
|
---|
554 | {
|
---|
555 | return m_unaryTokenStack.last().second;
|
---|
556 | }
|
---|
557 |
|
---|
558 | void unaryTokenStackRemoveLast(int& tokenStackDepth)
|
---|
559 | {
|
---|
560 | tokenStackDepth--;
|
---|
561 | m_unaryTokenStack.removeLast();
|
---|
562 | }
|
---|
563 |
|
---|
564 | void assignmentStackAppend(int& assignmentStackDepth, ExpressionNode* node, int start, int divot, int assignmentCount, Operator op)
|
---|
565 | {
|
---|
566 | assignmentStackDepth++;
|
---|
567 | m_assignmentInfoStack.append(AssignmentInfo(node, start, divot, assignmentCount, op));
|
---|
568 | }
|
---|
569 |
|
---|
570 | ExpressionNode* createAssignment(int& assignmentStackDepth, ExpressionNode* rhs, int initialAssignmentCount, int currentAssignmentCount, int lastTokenEnd)
|
---|
571 | {
|
---|
572 | ExpressionNode* result = makeAssignNode(m_assignmentInfoStack.last().m_node, m_assignmentInfoStack.last().m_op, rhs, m_assignmentInfoStack.last().m_initAssignments != initialAssignmentCount, m_assignmentInfoStack.last().m_initAssignments != currentAssignmentCount, m_assignmentInfoStack.last().m_start, m_assignmentInfoStack.last().m_divot + 1, lastTokenEnd);
|
---|
573 | m_assignmentInfoStack.removeLast();
|
---|
574 | assignmentStackDepth--;
|
---|
575 | return result;
|
---|
576 | }
|
---|
577 |
|
---|
578 | const Identifier& getName(Property property) { return property->name(); }
|
---|
579 | PropertyNode::Type getType(Property property) { return property->type(); }
|
---|
580 | private:
|
---|
581 | struct Scope {
|
---|
582 | Scope(JSGlobalData* globalData)
|
---|
583 | : m_varDeclarations(new (globalData) ParserArenaData<DeclarationStacks::VarStack>)
|
---|
584 | , m_funcDeclarations(new (globalData) ParserArenaData<DeclarationStacks::FunctionStack>)
|
---|
585 | , m_features(0)
|
---|
586 | , m_numConstants(0)
|
---|
587 | {
|
---|
588 | }
|
---|
589 | ParserArenaData<DeclarationStacks::VarStack>* m_varDeclarations;
|
---|
590 | ParserArenaData<DeclarationStacks::FunctionStack>* m_funcDeclarations;
|
---|
591 | int m_features;
|
---|
592 | int m_numConstants;
|
---|
593 | };
|
---|
594 |
|
---|
595 | static void setExceptionLocation(ThrowableExpressionData* node, unsigned start, unsigned divot, unsigned end)
|
---|
596 | {
|
---|
597 | node->setExceptionSourceCode(divot, divot - start, end - divot);
|
---|
598 | }
|
---|
599 |
|
---|
600 | void incConstants() { m_scopes.last().m_numConstants++; }
|
---|
601 | void usesThis() { m_scopes.last().m_features |= ThisFeature; }
|
---|
602 | void usesCatch() { m_scopes.last().m_features |= CatchFeature; }
|
---|
603 | void usesClosures() { m_scopes.last().m_features |= ClosureFeature; }
|
---|
604 | void usesArguments() { m_scopes.last().m_features |= ArgumentsFeature; }
|
---|
605 | void usesAssignment() { m_scopes.last().m_features |= AssignFeature; }
|
---|
606 | void usesWith() { m_scopes.last().m_features |= WithFeature; }
|
---|
607 | void usesEval()
|
---|
608 | {
|
---|
609 | m_evalCount++;
|
---|
610 | m_scopes.last().m_features |= EvalFeature;
|
---|
611 | }
|
---|
612 | ExpressionNode* createNumber(double d)
|
---|
613 | {
|
---|
614 | return new (m_globalData) NumberNode(m_globalData, d);
|
---|
615 | }
|
---|
616 |
|
---|
617 | JSGlobalData* m_globalData;
|
---|
618 | Lexer* m_lexer;
|
---|
619 | Vector<Scope> m_scopes;
|
---|
620 | Vector<BinaryOperand, 10> m_binaryOperandStack;
|
---|
621 | Vector<AssignmentInfo, 10> m_assignmentInfoStack;
|
---|
622 | Vector<pair<int, int>, 10> m_binaryOperatorStack;
|
---|
623 | Vector<pair<int, int>, 10> m_unaryTokenStack;
|
---|
624 | int m_evalCount;
|
---|
625 | };
|
---|
626 |
|
---|
627 | ExpressionNode* ASTBuilder::makeTypeOfNode(ExpressionNode* expr)
|
---|
628 | {
|
---|
629 | if (expr->isResolveNode()) {
|
---|
630 | ResolveNode* resolve = static_cast<ResolveNode*>(expr);
|
---|
631 | return new (m_globalData) TypeOfResolveNode(m_globalData, resolve->identifier());
|
---|
632 | }
|
---|
633 | return new (m_globalData) TypeOfValueNode(m_globalData, expr);
|
---|
634 | }
|
---|
635 |
|
---|
636 | ExpressionNode* ASTBuilder::makeDeleteNode(ExpressionNode* expr, int start, int divot, int end)
|
---|
637 | {
|
---|
638 | if (!expr->isLocation())
|
---|
639 | return new (m_globalData) DeleteValueNode(m_globalData, expr);
|
---|
640 | if (expr->isResolveNode()) {
|
---|
641 | ResolveNode* resolve = static_cast<ResolveNode*>(expr);
|
---|
642 | return new (m_globalData) DeleteResolveNode(m_globalData, resolve->identifier(), divot, divot - start, end - divot);
|
---|
643 | }
|
---|
644 | if (expr->isBracketAccessorNode()) {
|
---|
645 | BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(expr);
|
---|
646 | return new (m_globalData) DeleteBracketNode(m_globalData, bracket->base(), bracket->subscript(), divot, divot - start, end - divot);
|
---|
647 | }
|
---|
648 | ASSERT(expr->isDotAccessorNode());
|
---|
649 | DotAccessorNode* dot = static_cast<DotAccessorNode*>(expr);
|
---|
650 | return new (m_globalData) DeleteDotNode(m_globalData, dot->base(), dot->identifier(), divot, divot - start, end - divot);
|
---|
651 | }
|
---|
652 |
|
---|
653 | ExpressionNode* ASTBuilder::makeNegateNode(ExpressionNode* n)
|
---|
654 | {
|
---|
655 | if (n->isNumber()) {
|
---|
656 | NumberNode* numberNode = static_cast<NumberNode*>(n);
|
---|
657 | numberNode->setValue(-numberNode->value());
|
---|
658 | return numberNode;
|
---|
659 | }
|
---|
660 |
|
---|
661 | return new (m_globalData) NegateNode(m_globalData, n);
|
---|
662 | }
|
---|
663 |
|
---|
664 | ExpressionNode* ASTBuilder::makeBitwiseNotNode(ExpressionNode* expr)
|
---|
665 | {
|
---|
666 | if (expr->isNumber())
|
---|
667 | return createNumber(~toInt32(static_cast<NumberNode*>(expr)->value()));
|
---|
668 | return new (m_globalData) BitwiseNotNode(m_globalData, expr);
|
---|
669 | }
|
---|
670 |
|
---|
671 | ExpressionNode* ASTBuilder::makeMultNode(ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
|
---|
672 | {
|
---|
673 | expr1 = expr1->stripUnaryPlus();
|
---|
674 | expr2 = expr2->stripUnaryPlus();
|
---|
675 |
|
---|
676 | if (expr1->isNumber() && expr2->isNumber())
|
---|
677 | return createNumber(static_cast<NumberNode*>(expr1)->value() * static_cast<NumberNode*>(expr2)->value());
|
---|
678 |
|
---|
679 | if (expr1->isNumber() && static_cast<NumberNode*>(expr1)->value() == 1)
|
---|
680 | return new (m_globalData) UnaryPlusNode(m_globalData, expr2);
|
---|
681 |
|
---|
682 | if (expr2->isNumber() && static_cast<NumberNode*>(expr2)->value() == 1)
|
---|
683 | return new (m_globalData) UnaryPlusNode(m_globalData, expr1);
|
---|
684 |
|
---|
685 | return new (m_globalData) MultNode(m_globalData, expr1, expr2, rightHasAssignments);
|
---|
686 | }
|
---|
687 |
|
---|
688 | ExpressionNode* ASTBuilder::makeDivNode(ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
|
---|
689 | {
|
---|
690 | expr1 = expr1->stripUnaryPlus();
|
---|
691 | expr2 = expr2->stripUnaryPlus();
|
---|
692 |
|
---|
693 | if (expr1->isNumber() && expr2->isNumber())
|
---|
694 | return createNumber(static_cast<NumberNode*>(expr1)->value() / static_cast<NumberNode*>(expr2)->value());
|
---|
695 | return new (m_globalData) DivNode(m_globalData, expr1, expr2, rightHasAssignments);
|
---|
696 | }
|
---|
697 |
|
---|
698 | ExpressionNode* ASTBuilder::makeModNode(ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
|
---|
699 | {
|
---|
700 | expr1 = expr1->stripUnaryPlus();
|
---|
701 | expr2 = expr2->stripUnaryPlus();
|
---|
702 |
|
---|
703 | if (expr1->isNumber() && expr2->isNumber())
|
---|
704 | return createNumber(fmod(static_cast<NumberNode*>(expr1)->value(), static_cast<NumberNode*>(expr2)->value()));
|
---|
705 | return new (m_globalData) ModNode(m_globalData, expr1, expr2, rightHasAssignments);
|
---|
706 | }
|
---|
707 |
|
---|
708 | ExpressionNode* ASTBuilder::makeAddNode(ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
|
---|
709 | {
|
---|
710 | if (expr1->isNumber() && expr2->isNumber())
|
---|
711 | return createNumber(static_cast<NumberNode*>(expr1)->value() + static_cast<NumberNode*>(expr2)->value());
|
---|
712 | return new (m_globalData) AddNode(m_globalData, expr1, expr2, rightHasAssignments);
|
---|
713 | }
|
---|
714 |
|
---|
715 | ExpressionNode* ASTBuilder::makeSubNode(ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
|
---|
716 | {
|
---|
717 | expr1 = expr1->stripUnaryPlus();
|
---|
718 | expr2 = expr2->stripUnaryPlus();
|
---|
719 |
|
---|
720 | if (expr1->isNumber() && expr2->isNumber())
|
---|
721 | return createNumber(static_cast<NumberNode*>(expr1)->value() - static_cast<NumberNode*>(expr2)->value());
|
---|
722 | return new (m_globalData) SubNode(m_globalData, expr1, expr2, rightHasAssignments);
|
---|
723 | }
|
---|
724 |
|
---|
725 | ExpressionNode* ASTBuilder::makeLeftShiftNode(ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
|
---|
726 | {
|
---|
727 | if (expr1->isNumber() && expr2->isNumber())
|
---|
728 | return createNumber(toInt32(static_cast<NumberNode*>(expr1)->value()) << (toUInt32(static_cast<NumberNode*>(expr2)->value()) & 0x1f));
|
---|
729 | return new (m_globalData) LeftShiftNode(m_globalData, expr1, expr2, rightHasAssignments);
|
---|
730 | }
|
---|
731 |
|
---|
732 | ExpressionNode* ASTBuilder::makeRightShiftNode(ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
|
---|
733 | {
|
---|
734 | if (expr1->isNumber() && expr2->isNumber())
|
---|
735 | return createNumber(toInt32(static_cast<NumberNode*>(expr1)->value()) >> (toUInt32(static_cast<NumberNode*>(expr2)->value()) & 0x1f));
|
---|
736 | return new (m_globalData) RightShiftNode(m_globalData, expr1, expr2, rightHasAssignments);
|
---|
737 | }
|
---|
738 |
|
---|
739 | ExpressionNode* ASTBuilder::makeURightShiftNode(ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
|
---|
740 | {
|
---|
741 | if (expr1->isNumber() && expr2->isNumber())
|
---|
742 | return createNumber(toUInt32(static_cast<NumberNode*>(expr1)->value()) >> (toUInt32(static_cast<NumberNode*>(expr2)->value()) & 0x1f));
|
---|
743 | return new (m_globalData) UnsignedRightShiftNode(m_globalData, expr1, expr2, rightHasAssignments);
|
---|
744 | }
|
---|
745 |
|
---|
746 | ExpressionNode* ASTBuilder::makeBitOrNode(ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
|
---|
747 | {
|
---|
748 | if (expr1->isNumber() && expr2->isNumber())
|
---|
749 | return createNumber(toInt32(static_cast<NumberNode*>(expr1)->value()) | toInt32(static_cast<NumberNode*>(expr2)->value()));
|
---|
750 | return new (m_globalData) BitOrNode(m_globalData, expr1, expr2, rightHasAssignments);
|
---|
751 | }
|
---|
752 |
|
---|
753 | ExpressionNode* ASTBuilder::makeBitAndNode(ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
|
---|
754 | {
|
---|
755 | if (expr1->isNumber() && expr2->isNumber())
|
---|
756 | return createNumber(toInt32(static_cast<NumberNode*>(expr1)->value()) & toInt32(static_cast<NumberNode*>(expr2)->value()));
|
---|
757 | return new (m_globalData) BitAndNode(m_globalData, expr1, expr2, rightHasAssignments);
|
---|
758 | }
|
---|
759 |
|
---|
760 | ExpressionNode* ASTBuilder::makeBitXOrNode(ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments)
|
---|
761 | {
|
---|
762 | if (expr1->isNumber() && expr2->isNumber())
|
---|
763 | return createNumber(toInt32(static_cast<NumberNode*>(expr1)->value()) ^ toInt32(static_cast<NumberNode*>(expr2)->value()));
|
---|
764 | return new (m_globalData) BitXOrNode(m_globalData, expr1, expr2, rightHasAssignments);
|
---|
765 | }
|
---|
766 |
|
---|
767 | ExpressionNode* ASTBuilder::makeFunctionCallNode(ExpressionNode* func, ArgumentsNode* args, int start, int divot, int end)
|
---|
768 | {
|
---|
769 | if (!func->isLocation())
|
---|
770 | return new (m_globalData) FunctionCallValueNode(m_globalData, func, args, divot, divot - start, end - divot);
|
---|
771 | if (func->isResolveNode()) {
|
---|
772 | ResolveNode* resolve = static_cast<ResolveNode*>(func);
|
---|
773 | const Identifier& identifier = resolve->identifier();
|
---|
774 | if (identifier == m_globalData->propertyNames->eval) {
|
---|
775 | usesEval();
|
---|
776 | return new (m_globalData) EvalFunctionCallNode(m_globalData, args, divot, divot - start, end - divot);
|
---|
777 | }
|
---|
778 | return new (m_globalData) FunctionCallResolveNode(m_globalData, identifier, args, divot, divot - start, end - divot);
|
---|
779 | }
|
---|
780 | if (func->isBracketAccessorNode()) {
|
---|
781 | BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(func);
|
---|
782 | FunctionCallBracketNode* node = new (m_globalData) FunctionCallBracketNode(m_globalData, bracket->base(), bracket->subscript(), args, divot, divot - start, end - divot);
|
---|
783 | node->setSubexpressionInfo(bracket->divot(), bracket->endOffset());
|
---|
784 | return node;
|
---|
785 | }
|
---|
786 | ASSERT(func->isDotAccessorNode());
|
---|
787 | DotAccessorNode* dot = static_cast<DotAccessorNode*>(func);
|
---|
788 | FunctionCallDotNode* node;
|
---|
789 | if (dot->identifier() == m_globalData->propertyNames->call)
|
---|
790 | node = new (m_globalData) CallFunctionCallDotNode(m_globalData, dot->base(), dot->identifier(), args, divot, divot - start, end - divot);
|
---|
791 | else if (dot->identifier() == m_globalData->propertyNames->apply)
|
---|
792 | node = new (m_globalData) ApplyFunctionCallDotNode(m_globalData, dot->base(), dot->identifier(), args, divot, divot - start, end - divot);
|
---|
793 | else
|
---|
794 | node = new (m_globalData) FunctionCallDotNode(m_globalData, dot->base(), dot->identifier(), args, divot, divot - start, end - divot);
|
---|
795 | node->setSubexpressionInfo(dot->divot(), dot->endOffset());
|
---|
796 | return node;
|
---|
797 | }
|
---|
798 |
|
---|
799 | ExpressionNode* ASTBuilder::makeBinaryNode(int token, pair<ExpressionNode*, BinaryOpInfo> lhs, pair<ExpressionNode*, BinaryOpInfo> rhs)
|
---|
800 | {
|
---|
801 | switch (token) {
|
---|
802 | case OR:
|
---|
803 | return new (m_globalData) LogicalOpNode(m_globalData, lhs.first, rhs.first, OpLogicalOr);
|
---|
804 |
|
---|
805 | case AND:
|
---|
806 | return new (m_globalData) LogicalOpNode(m_globalData, lhs.first, rhs.first, OpLogicalAnd);
|
---|
807 |
|
---|
808 | case BITOR:
|
---|
809 | return makeBitOrNode(lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
810 |
|
---|
811 | case BITXOR:
|
---|
812 | return makeBitXOrNode(lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
813 |
|
---|
814 | case BITAND:
|
---|
815 | return makeBitAndNode(lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
816 |
|
---|
817 | case EQEQ:
|
---|
818 | return new (m_globalData) EqualNode(m_globalData, lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
819 |
|
---|
820 | case NE:
|
---|
821 | return new (m_globalData) NotEqualNode(m_globalData, lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
822 |
|
---|
823 | case STREQ:
|
---|
824 | return new (m_globalData) StrictEqualNode(m_globalData, lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
825 |
|
---|
826 | case STRNEQ:
|
---|
827 | return new (m_globalData) NotStrictEqualNode(m_globalData, lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
828 |
|
---|
829 | case LT:
|
---|
830 | return new (m_globalData) LessNode(m_globalData, lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
831 |
|
---|
832 | case GT:
|
---|
833 | return new (m_globalData) GreaterNode(m_globalData, lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
834 |
|
---|
835 | case LE:
|
---|
836 | return new (m_globalData) LessEqNode(m_globalData, lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
837 |
|
---|
838 | case GE:
|
---|
839 | return new (m_globalData) GreaterEqNode(m_globalData, lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
840 |
|
---|
841 | case INSTANCEOF: {
|
---|
842 | InstanceOfNode* node = new (m_globalData) InstanceOfNode(m_globalData, lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
843 | setExceptionLocation(node, lhs.second.start, rhs.second.start, rhs.second.end);
|
---|
844 | return node;
|
---|
845 | }
|
---|
846 |
|
---|
847 | case INTOKEN: {
|
---|
848 | InNode* node = new (m_globalData) InNode(m_globalData, lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
849 | setExceptionLocation(node, lhs.second.start, rhs.second.start, rhs.second.end);
|
---|
850 | return node;
|
---|
851 | }
|
---|
852 |
|
---|
853 | case LSHIFT:
|
---|
854 | return makeLeftShiftNode(lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
855 |
|
---|
856 | case RSHIFT:
|
---|
857 | return makeRightShiftNode(lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
858 |
|
---|
859 | case URSHIFT:
|
---|
860 | return makeURightShiftNode(lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
861 |
|
---|
862 | case PLUS:
|
---|
863 | return makeAddNode(lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
864 |
|
---|
865 | case MINUS:
|
---|
866 | return makeSubNode(lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
867 |
|
---|
868 | case TIMES:
|
---|
869 | return makeMultNode(lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
870 |
|
---|
871 | case DIVIDE:
|
---|
872 | return makeDivNode(lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
873 |
|
---|
874 | case MOD:
|
---|
875 | return makeModNode(lhs.first, rhs.first, rhs.second.hasAssignment);
|
---|
876 | }
|
---|
877 | CRASH();
|
---|
878 | return 0;
|
---|
879 | }
|
---|
880 |
|
---|
881 | ExpressionNode* ASTBuilder::makeAssignNode(ExpressionNode* loc, Operator op, ExpressionNode* expr, bool locHasAssignments, bool exprHasAssignments, int start, int divot, int end)
|
---|
882 | {
|
---|
883 | usesAssignment();
|
---|
884 | if (!loc->isLocation())
|
---|
885 | return new (m_globalData) AssignErrorNode(m_globalData, loc, op, expr, divot, divot - start, end - divot);
|
---|
886 |
|
---|
887 | if (loc->isResolveNode()) {
|
---|
888 | ResolveNode* resolve = static_cast<ResolveNode*>(loc);
|
---|
889 | if (op == OpEqual) {
|
---|
890 | AssignResolveNode* node = new (m_globalData) AssignResolveNode(m_globalData, resolve->identifier(), expr, exprHasAssignments);
|
---|
891 | setExceptionLocation(node, start, divot, end);
|
---|
892 | return node;
|
---|
893 | }
|
---|
894 | return new (m_globalData) ReadModifyResolveNode(m_globalData, resolve->identifier(), op, expr, exprHasAssignments, divot, divot - start, end - divot);
|
---|
895 | }
|
---|
896 | if (loc->isBracketAccessorNode()) {
|
---|
897 | BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(loc);
|
---|
898 | if (op == OpEqual)
|
---|
899 | return new (m_globalData) AssignBracketNode(m_globalData, bracket->base(), bracket->subscript(), expr, locHasAssignments, exprHasAssignments, bracket->divot(), bracket->divot() - start, end - bracket->divot());
|
---|
900 | ReadModifyBracketNode* node = new (m_globalData) ReadModifyBracketNode(m_globalData, bracket->base(), bracket->subscript(), op, expr, locHasAssignments, exprHasAssignments, divot, divot - start, end - divot);
|
---|
901 | node->setSubexpressionInfo(bracket->divot(), bracket->endOffset());
|
---|
902 | return node;
|
---|
903 | }
|
---|
904 | ASSERT(loc->isDotAccessorNode());
|
---|
905 | DotAccessorNode* dot = static_cast<DotAccessorNode*>(loc);
|
---|
906 | if (op == OpEqual)
|
---|
907 | return new (m_globalData) AssignDotNode(m_globalData, dot->base(), dot->identifier(), expr, exprHasAssignments, dot->divot(), dot->divot() - start, end - dot->divot());
|
---|
908 |
|
---|
909 | ReadModifyDotNode* node = new (m_globalData) ReadModifyDotNode(m_globalData, dot->base(), dot->identifier(), op, expr, exprHasAssignments, divot, divot - start, end - divot);
|
---|
910 | node->setSubexpressionInfo(dot->divot(), dot->endOffset());
|
---|
911 | return node;
|
---|
912 | }
|
---|
913 |
|
---|
914 | ExpressionNode* ASTBuilder::makePrefixNode(ExpressionNode* expr, Operator op, int start, int divot, int end)
|
---|
915 | {
|
---|
916 | usesAssignment();
|
---|
917 | if (!expr->isLocation())
|
---|
918 | return new (m_globalData) PrefixErrorNode(m_globalData, expr, op, divot, divot - start, end - divot);
|
---|
919 |
|
---|
920 | if (expr->isResolveNode()) {
|
---|
921 | ResolveNode* resolve = static_cast<ResolveNode*>(expr);
|
---|
922 | return new (m_globalData) PrefixResolveNode(m_globalData, resolve->identifier(), op, divot, divot - start, end - divot);
|
---|
923 | }
|
---|
924 | if (expr->isBracketAccessorNode()) {
|
---|
925 | BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(expr);
|
---|
926 | PrefixBracketNode* node = new (m_globalData) PrefixBracketNode(m_globalData, bracket->base(), bracket->subscript(), op, divot, divot - start, end - divot);
|
---|
927 | node->setSubexpressionInfo(bracket->divot(), bracket->startOffset());
|
---|
928 | return node;
|
---|
929 | }
|
---|
930 | ASSERT(expr->isDotAccessorNode());
|
---|
931 | DotAccessorNode* dot = static_cast<DotAccessorNode*>(expr);
|
---|
932 | PrefixDotNode* node = new (m_globalData) PrefixDotNode(m_globalData, dot->base(), dot->identifier(), op, divot, divot - start, end - divot);
|
---|
933 | node->setSubexpressionInfo(dot->divot(), dot->startOffset());
|
---|
934 | return node;
|
---|
935 | }
|
---|
936 |
|
---|
937 | ExpressionNode* ASTBuilder::makePostfixNode(ExpressionNode* expr, Operator op, int start, int divot, int end)
|
---|
938 | {
|
---|
939 | usesAssignment();
|
---|
940 | if (!expr->isLocation())
|
---|
941 | return new (m_globalData) PostfixErrorNode(m_globalData, expr, op, divot, divot - start, end - divot);
|
---|
942 |
|
---|
943 | if (expr->isResolveNode()) {
|
---|
944 | ResolveNode* resolve = static_cast<ResolveNode*>(expr);
|
---|
945 | return new (m_globalData) PostfixResolveNode(m_globalData, resolve->identifier(), op, divot, divot - start, end - divot);
|
---|
946 | }
|
---|
947 | if (expr->isBracketAccessorNode()) {
|
---|
948 | BracketAccessorNode* bracket = static_cast<BracketAccessorNode*>(expr);
|
---|
949 | PostfixBracketNode* node = new (m_globalData) PostfixBracketNode(m_globalData, bracket->base(), bracket->subscript(), op, divot, divot - start, end - divot);
|
---|
950 | node->setSubexpressionInfo(bracket->divot(), bracket->endOffset());
|
---|
951 | return node;
|
---|
952 |
|
---|
953 | }
|
---|
954 | ASSERT(expr->isDotAccessorNode());
|
---|
955 | DotAccessorNode* dot = static_cast<DotAccessorNode*>(expr);
|
---|
956 | PostfixDotNode* node = new (m_globalData) PostfixDotNode(m_globalData, dot->base(), dot->identifier(), op, divot, divot - start, end - divot);
|
---|
957 | node->setSubexpressionInfo(dot->divot(), dot->endOffset());
|
---|
958 | return node;
|
---|
959 | }
|
---|
960 |
|
---|
961 | }
|
---|
962 |
|
---|
963 | #endif
|
---|