]> git.saurik.com Git - apple/javascriptcore.git/blame - parser/Nodes.cpp
JavaScriptCore-1218.33.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
A
38#include "Lexer.h"
39#include "Operations.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{
6fe7ccc8 57 m_lineNumber = firstLine;
f9bf01c6 58 m_lastLine = lastLine;
93a37866
A
59 m_startOffset = startOffset;
60 m_lineStartOffset = lineStartOffset;
61 ASSERT(m_startOffset >= m_lineStartOffset);
9dae56ea
A
62}
63
f9bf01c6 64// ------------------------------ SourceElements --------------------------------
9dae56ea 65
f9bf01c6 66void SourceElements::append(StatementNode* statement)
9dae56ea 67{
f9bf01c6
A
68 if (statement->isEmptyStatement())
69 return;
70 m_statements.append(statement);
9dae56ea
A
71}
72
14957cd0 73StatementNode* SourceElements::singleStatement() const
9dae56ea 74{
f9bf01c6
A
75 size_t size = m_statements.size();
76 return size == 1 ? m_statements[0] : 0;
9dae56ea
A
77}
78
9dae56ea
A
79// ------------------------------ ScopeNode -----------------------------
80
93a37866
A
81ScopeNode::ScopeNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, bool inStrictContext)
82 : StatementNode(endLocation)
83 , ParserArenaRefCounted(vm)
84 , m_startLineNumber(startLocation.line)
85 , m_startStartOffset(startLocation.startOffset)
86 , m_startLineStartOffset(startLocation.lineStartOffset)
14957cd0 87 , m_features(inStrictContext ? StrictModeFeature : NoFeatures)
6fe7ccc8
A
88 , m_numConstants(0)
89 , m_statements(0)
9dae56ea 90{
9dae56ea
A
91}
92
93a37866
A
93ScopeNode::ScopeNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, const SourceCode& source, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, CodeFeatures features, int numConstants)
94 : StatementNode(endLocation)
95 , ParserArenaRefCounted(vm)
96 , m_startLineNumber(startLocation.line)
97 , m_startStartOffset(startLocation.startOffset)
98 , m_startLineStartOffset(startLocation.lineStartOffset)
9dae56ea
A
99 , m_features(features)
100 , m_source(source)
6fe7ccc8
A
101 , m_numConstants(numConstants)
102 , m_statements(children)
9dae56ea 103{
93a37866 104 m_arena.swap(*vm->parserArena);
6fe7ccc8
A
105 if (varStack)
106 m_varStack.swap(*varStack);
107 if (funcStack)
108 m_functionStack.swap(*funcStack);
109 m_capturedVariables.swap(capturedVariables);
f9bf01c6
A
110}
111
112StatementNode* ScopeNode::singleStatement() const
113{
6fe7ccc8 114 return m_statements ? m_statements->singleStatement() : 0;
9dae56ea
A
115}
116
9dae56ea
A
117// ------------------------------ ProgramNode -----------------------------
118
93a37866
A
119inline ProgramNode::ProgramNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& source, CodeFeatures features, int numConstants)
120 : ScopeNode(vm, startLocation, endLocation, source, children, varStack, funcStack, capturedVariables, features, numConstants)
121 , m_startColumn(startColumn)
9dae56ea
A
122{
123}
124
93a37866 125PassRefPtr<ProgramNode> ProgramNode::create(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& source, CodeFeatures features, int numConstants)
9dae56ea 126{
93a37866 127 RefPtr<ProgramNode> node = new ProgramNode(vm, startLocation, endLocation, startColumn, children, varStack, funcStack, capturedVariables, source, features, numConstants);
ba379fdc 128
6fe7ccc8
A
129 ASSERT(node->m_arena.last() == node);
130 node->m_arena.removeLast();
131 ASSERT(!node->m_arena.contains(node.get()));
ba379fdc
A
132
133 return node.release();
9dae56ea
A
134}
135
9dae56ea
A
136// ------------------------------ EvalNode -----------------------------
137
93a37866
A
138inline EvalNode::EvalNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& source, CodeFeatures features, int numConstants)
139 : ScopeNode(vm, startLocation, endLocation, source, children, varStack, funcStack, capturedVariables, features, numConstants)
9dae56ea
A
140{
141}
142
93a37866 143PassRefPtr<EvalNode> EvalNode::create(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& source, CodeFeatures features, int numConstants)
9dae56ea 144{
93a37866 145 RefPtr<EvalNode> node = new EvalNode(vm, startLocation, endLocation, children, varStack, funcStack, capturedVariables, source, features, numConstants);
ba379fdc 146
6fe7ccc8
A
147 ASSERT(node->m_arena.last() == node);
148 node->m_arena.removeLast();
149 ASSERT(!node->m_arena.contains(node.get()));
ba379fdc
A
150
151 return node.release();
9dae56ea
A
152}
153
f9bf01c6 154// ------------------------------ FunctionBodyNode -----------------------------
9dae56ea 155
93a37866 156PassRefPtr<FunctionParameters> FunctionParameters::create(ParameterNode* firstParameter)
ba379fdc 157{
93a37866 158 unsigned parameterCount = 0;
f9bf01c6 159 for (ParameterNode* parameter = firstParameter; parameter; parameter = parameter->nextParam())
93a37866
A
160 ++parameterCount;
161
162 size_t objectSize = sizeof(FunctionParameters) - sizeof(void*) + sizeof(StringImpl*) * parameterCount;
163 void* slot = fastMalloc(objectSize);
164 return adoptRef(new (slot) FunctionParameters(firstParameter, parameterCount));
165}
166
167FunctionParameters::FunctionParameters(ParameterNode* firstParameter, unsigned size)
168 : m_size(size)
169{
170 unsigned i = 0;
171 for (ParameterNode* parameter = firstParameter; parameter; parameter = parameter->nextParam())
172 new (&identifiers()[i++]) Identifier(parameter->ident());
173}
174
175FunctionParameters::~FunctionParameters()
176{
177 for (unsigned i = 0; i < m_size; ++i)
178 identifiers()[i].~Identifier();
ba379fdc 179}
9dae56ea 180
93a37866
A
181inline FunctionBodyNode::FunctionBodyNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, bool inStrictContext)
182 : ScopeNode(vm, startLocation, endLocation, inStrictContext)
183 , m_startColumn(startColumn)
9dae56ea
A
184{
185}
186
93a37866
A
187inline FunctionBodyNode::FunctionBodyNode(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& sourceCode, CodeFeatures features, int numConstants)
188 : ScopeNode(vm, startLocation, endLocation, sourceCode, children, varStack, funcStack, capturedVariables, features, numConstants)
189 , m_startColumn(startColumn)
9dae56ea 190{
9dae56ea
A
191}
192
93a37866 193void FunctionBodyNode::finishParsing(const SourceCode& source, ParameterNode* firstParameter, const Identifier& ident, FunctionNameIsInScopeToggle functionNameIsInScopeToggle)
9dae56ea 194{
9dae56ea 195 setSource(source);
93a37866 196 finishParsing(FunctionParameters::create(firstParameter), ident, functionNameIsInScopeToggle);
9dae56ea
A
197}
198
93a37866 199void FunctionBodyNode::finishParsing(PassRefPtr<FunctionParameters> parameters, const Identifier& ident, FunctionNameIsInScopeToggle functionNameIsInScopeToggle)
9dae56ea
A
200{
201 ASSERT(!source().isNull());
202 m_parameters = parameters;
f9bf01c6 203 m_ident = ident;
93a37866 204 m_functionNameIsInScopeToggle = functionNameIsInScopeToggle;
ba379fdc
A
205}
206
93a37866 207FunctionBodyNode* FunctionBodyNode::create(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, bool inStrictContext)
9dae56ea 208{
93a37866 209 return new FunctionBodyNode(vm, startLocation, endLocation, startColumn, inStrictContext);
9dae56ea
A
210}
211
93a37866 212PassRefPtr<FunctionBodyNode> FunctionBodyNode::create(VM* vm, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, IdentifierSet& capturedVariables, const SourceCode& sourceCode, CodeFeatures features, int numConstants)
9dae56ea 213{
93a37866 214 RefPtr<FunctionBodyNode> node = new FunctionBodyNode(vm, startLocation, endLocation, startColumn, children, varStack, funcStack, capturedVariables, sourceCode, features, numConstants);
ba379fdc 215
6fe7ccc8
A
216 ASSERT(node->m_arena.last() == node);
217 node->m_arena.removeLast();
218 ASSERT(!node->m_arena.contains(node.get()));
ba379fdc
A
219
220 return node.release();
9dae56ea
A
221}
222
9dae56ea 223} // namespace JSC