]> git.saurik.com Git - apple/javascriptcore.git/blame - parser/Nodes.cpp
JavaScriptCore-7600.1.4.15.12.tar.gz
[apple/javascriptcore.git] / parser / Nodes.cpp
CommitLineData
9dae56ea
A
1/*
2* Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
3* Copyright (C) 2001 Peter Kelly (pmk@post.com)
93a37866 4* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013 Apple Inc. All rights reserved.
9dae56ea
A
5* Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
6* Copyright (C) 2007 Maks Orlovich
7* Copyright (C) 2007 Eric Seidel <eric@webkit.org>
8*
9* This library is free software; you can redistribute it and/or
10* modify it under the terms of the GNU Library General Public
11* License as published by the Free Software Foundation; either
12* version 2 of the License, or (at your option) any later version.
13*
14* This library is distributed in the hope that it will be useful,
15* but WITHOUT ANY WARRANTY; without even the implied warranty of
16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17* Library General Public License for more details.
18*
19* You should have received a copy of the GNU Library General Public License
20* along with this library; see the file COPYING.LIB. If not, write to
21* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22* Boston, MA 02110-1301, USA.
23*
24*/
25
26#include "config.h"
27#include "Nodes.h"
ba379fdc 28#include "NodeConstructors.h"
9dae56ea
A
29
30#include "BytecodeGenerator.h"
31#include "CallFrame.h"
ba379fdc
A
32#include "Debugger.h"
33#include "JIT.h"
34#include "JSFunction.h"
9dae56ea 35#include "JSGlobalObject.h"
93a37866 36#include "JSNameScope.h"
9dae56ea 37#include "LabelScope.h"
ba379fdc 38#include "Lexer.h"
81345200 39#include "JSCInlines.h"
9dae56ea
A
40#include "Parser.h"
41#include "PropertyNameArray.h"
42#include "RegExpObject.h"
43#include "SamplingTool.h"
9dae56ea 44#include <wtf/Assertions.h>
9dae56ea
A
45#include <wtf/RefCountedLeakCounter.h>
46#include <wtf/Threading.h>
47
48using namespace WTF;
49
50namespace JSC {
51
9dae56ea 52
f9bf01c6 53// ------------------------------ StatementNode --------------------------------
9dae56ea 54
93a37866 55void StatementNode::setLoc(unsigned firstLine, unsigned lastLine, int startOffset, int lineStartOffset)
9dae56ea 56{
f9bf01c6 57 m_lastLine = lastLine;
81345200
A
58 m_position = JSTextPosition(firstLine, startOffset, lineStartOffset);
59 ASSERT(m_position.offset >= m_position.lineStartOffset);
9dae56ea
A
60}
61
f9bf01c6 62// ------------------------------ SourceElements --------------------------------
9dae56ea 63
f9bf01c6 64void SourceElements::append(StatementNode* statement)
9dae56ea 65{
f9bf01c6
A
66 if (statement->isEmptyStatement())
67 return;
68 m_statements.append(statement);
9dae56ea
A
69}
70
14957cd0 71StatementNode* SourceElements::singleStatement() const
9dae56ea 72{
f9bf01c6
A
73 size_t size = m_statements.size();
74 return size == 1 ? m_statements[0] : 0;
9dae56ea
A
75}
76
9dae56ea
A
77// ------------------------------ ScopeNode -----------------------------
78
93a37866
A
79ScopeNode::ScopeNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, bool inStrictContext)
80 : StatementNode(endLocation)
81 , ParserArenaRefCounted(vm)
82 , m_startLineNumber(startLocation.line)
83 , m_startStartOffset(startLocation.startOffset)
84 , m_startLineStartOffset(startLocation.lineStartOffset)
14957cd0 85 , m_features(inStrictContext ? StrictModeFeature : NoFeatures)
6fe7ccc8
A
86 , m_numConstants(0)
87 , m_statements(0)
9dae56ea 88{
9dae56ea
A
89}
90
93a37866
A
91ScopeNode::ScopeNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, const SourceCode& source, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, CodeFeatures features, int numConstants)
92 : StatementNode(endLocation)
93 , ParserArenaRefCounted(vm)
94 , m_startLineNumber(startLocation.line)
95 , m_startStartOffset(startLocation.startOffset)
96 , m_startLineStartOffset(startLocation.lineStartOffset)
9dae56ea
A
97 , m_features(features)
98 , m_source(source)
6fe7ccc8
A
99 , m_numConstants(numConstants)
100 , m_statements(children)
9dae56ea 101{
93a37866 102 m_arena.swap(*vm->parserArena);
6fe7ccc8
A
103 if (varStack)
104 m_varStack.swap(*varStack);
105 if (funcStack)
106 m_functionStack.swap(*funcStack);
107 m_capturedVariables.swap(capturedVariables);
f9bf01c6
A
108}
109
110StatementNode* ScopeNode::singleStatement() const
111{
6fe7ccc8 112 return m_statements ? m_statements->singleStatement() : 0;
9dae56ea
A
113}
114
9dae56ea
A
115// ------------------------------ ProgramNode -----------------------------
116
81345200 117inline ProgramNode::ProgramNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& source, CodeFeatures features, int numConstants)
93a37866
A
118 : ScopeNode(vm, startLocation, endLocation, source, children, varStack, funcStack, capturedVariables, features, numConstants)
119 , m_startColumn(startColumn)
81345200 120 , m_endColumn(endColumn)
9dae56ea
A
121{
122}
123
81345200 124PassRefPtr<ProgramNode> ProgramNode::create(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& source, CodeFeatures features, int numConstants)
9dae56ea 125{
81345200 126 RefPtr<ProgramNode> node = new ProgramNode(vm, startLocation, endLocation, startColumn, endColumn, children, varStack, funcStack, capturedVariables, source, features, numConstants);
ba379fdc 127
6fe7ccc8
A
128 ASSERT(node->m_arena.last() == node);
129 node->m_arena.removeLast();
130 ASSERT(!node->m_arena.contains(node.get()));
ba379fdc
A
131
132 return node.release();
9dae56ea
A
133}
134
81345200
A
135
136void ProgramNode::setClosedVariables(Vector<RefPtr<StringImpl>>&& closedVariables)
137{
138 m_closedVariables = WTF::move(closedVariables);
139}
140
9dae56ea
A
141// ------------------------------ EvalNode -----------------------------
142
81345200 143inline EvalNode::EvalNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned endColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& source, CodeFeatures features, int numConstants)
93a37866 144 : ScopeNode(vm, startLocation, endLocation, source, children, varStack, funcStack, capturedVariables, features, numConstants)
81345200 145 , m_endColumn(endColumn)
9dae56ea
A
146{
147}
148
81345200 149PassRefPtr<EvalNode> EvalNode::create(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned, unsigned endColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& source, CodeFeatures features, int numConstants)
9dae56ea 150{
81345200 151 RefPtr<EvalNode> node = new EvalNode(vm, startLocation, endLocation, endColumn, children, varStack, funcStack, capturedVariables, source, features, numConstants);
ba379fdc 152
6fe7ccc8
A
153 ASSERT(node->m_arena.last() == node);
154 node->m_arena.removeLast();
155 ASSERT(!node->m_arena.contains(node.get()));
ba379fdc
A
156
157 return node.release();
9dae56ea
A
158}
159
f9bf01c6 160// ------------------------------ FunctionBodyNode -----------------------------
9dae56ea 161
93a37866 162PassRefPtr<FunctionParameters> FunctionParameters::create(ParameterNode* firstParameter)
ba379fdc 163{
93a37866 164 unsigned parameterCount = 0;
f9bf01c6 165 for (ParameterNode* parameter = firstParameter; parameter; parameter = parameter->nextParam())
93a37866
A
166 ++parameterCount;
167
81345200 168 size_t objectSize = sizeof(FunctionParameters) - sizeof(void*) + sizeof(DeconstructionPatternNode*) * parameterCount;
93a37866
A
169 void* slot = fastMalloc(objectSize);
170 return adoptRef(new (slot) FunctionParameters(firstParameter, parameterCount));
171}
172
173FunctionParameters::FunctionParameters(ParameterNode* firstParameter, unsigned size)
174 : m_size(size)
175{
176 unsigned i = 0;
81345200
A
177 for (ParameterNode* parameter = firstParameter; parameter; parameter = parameter->nextParam()) {
178 auto pattern = parameter->pattern();
179 pattern->ref();
180 patterns()[i++] = pattern;
181 }
93a37866
A
182}
183
184FunctionParameters::~FunctionParameters()
185{
186 for (unsigned i = 0; i < m_size; ++i)
81345200 187 patterns()[i]->deref();
ba379fdc 188}
9dae56ea 189
81345200 190inline FunctionBodyNode::FunctionBodyNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, bool inStrictContext)
93a37866
A
191 : ScopeNode(vm, startLocation, endLocation, inStrictContext)
192 , m_startColumn(startColumn)
81345200 193 , m_endColumn(endColumn)
9dae56ea
A
194{
195}
196
81345200 197inline FunctionBodyNode::FunctionBodyNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& sourceCode, CodeFeatures features, int numConstants)
93a37866
A
198 : ScopeNode(vm, startLocation, endLocation, sourceCode, children, varStack, funcStack, capturedVariables, features, numConstants)
199 , m_startColumn(startColumn)
81345200 200 , m_endColumn(endColumn)
9dae56ea 201{
9dae56ea
A
202}
203
81345200 204void FunctionBodyNode::finishParsing(const SourceCode& source, ParameterNode* firstParameter, const Identifier& ident, enum FunctionMode functionMode)
9dae56ea 205{
9dae56ea 206 setSource(source);
81345200 207 finishParsing(FunctionParameters::create(firstParameter), ident, functionMode);
9dae56ea
A
208}
209
81345200 210void FunctionBodyNode::finishParsing(PassRefPtr<FunctionParameters> parameters, const Identifier& ident, enum FunctionMode functionMode)
9dae56ea
A
211{
212 ASSERT(!source().isNull());
213 m_parameters = parameters;
f9bf01c6 214 m_ident = ident;
81345200 215 m_functionMode = functionMode;
ba379fdc
A
216}
217
81345200 218FunctionBodyNode* FunctionBodyNode::create(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, bool inStrictContext)
9dae56ea 219{
81345200 220 return new FunctionBodyNode(vm, startLocation, endLocation, startColumn, endColumn, inStrictContext);
9dae56ea
A
221}
222
81345200 223PassRefPtr<FunctionBodyNode> FunctionBodyNode::create(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& sourceCode, CodeFeatures features, int numConstants)
9dae56ea 224{
81345200 225 RefPtr<FunctionBodyNode> node = new FunctionBodyNode(vm, startLocation, endLocation, startColumn, endColumn , children, varStack, funcStack, capturedVariables, sourceCode, features, numConstants);
ba379fdc 226
6fe7ccc8
A
227 ASSERT(node->m_arena.last() == node);
228 node->m_arena.removeLast();
229 ASSERT(!node->m_arena.contains(node.get()));
ba379fdc
A
230
231 return node.release();
9dae56ea
A
232}
233
81345200
A
234void FunctionBodyNode::setEndPosition(JSTextPosition position)
235{
236 m_lastLine = position.line;
237 m_endColumn = position.offset - position.lineStartOffset;
238}
239
9dae56ea 240} // namespace JSC