]> git.saurik.com Git - apple/javascriptcore.git/blame - parser/Nodes.cpp
JavaScriptCore-584.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)
4* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
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
A
35#include "JSGlobalObject.h"
36#include "JSStaticScopeObject.h"
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
f9bf01c6 55void StatementNode::setLoc(int firstLine, int lastLine)
9dae56ea 56{
f9bf01c6
A
57 m_line = firstLine;
58 m_lastLine = lastLine;
9dae56ea
A
59}
60
f9bf01c6 61// ------------------------------ SourceElements --------------------------------
9dae56ea 62
f9bf01c6 63void SourceElements::append(StatementNode* statement)
9dae56ea 64{
f9bf01c6
A
65 if (statement->isEmptyStatement())
66 return;
67 m_statements.append(statement);
9dae56ea
A
68}
69
f9bf01c6 70inline StatementNode* SourceElements::singleStatement() const
9dae56ea 71{
f9bf01c6
A
72 size_t size = m_statements.size();
73 return size == 1 ? m_statements[0] : 0;
9dae56ea
A
74}
75
9dae56ea
A
76// -----------------------------ScopeNodeData ---------------------------
77
f9bf01c6 78ScopeNodeData::ScopeNodeData(ParserArena& arena, SourceElements* statements, VarStack* varStack, FunctionStack* funcStack, int numConstants)
9dae56ea 79 : m_numConstants(numConstants)
f9bf01c6 80 , m_statements(statements)
9dae56ea 81{
ba379fdc 82 m_arena.swap(arena);
9dae56ea 83 if (varStack)
ba379fdc 84 m_varStack.swap(*varStack);
9dae56ea 85 if (funcStack)
ba379fdc 86 m_functionStack.swap(*funcStack);
9dae56ea
A
87}
88
89// ------------------------------ ScopeNode -----------------------------
90
91ScopeNode::ScopeNode(JSGlobalData* globalData)
92 : StatementNode(globalData)
ba379fdc 93 , ParserArenaRefCounted(globalData)
9dae56ea
A
94 , m_features(NoFeatures)
95{
9dae56ea
A
96}
97
98ScopeNode::ScopeNode(JSGlobalData* globalData, const SourceCode& source, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, CodeFeatures features, int numConstants)
99 : StatementNode(globalData)
ba379fdc
A
100 , ParserArenaRefCounted(globalData)
101 , m_data(new ScopeNodeData(globalData->parser->arena(), children, varStack, funcStack, numConstants))
9dae56ea
A
102 , m_features(features)
103 , m_source(source)
104{
f9bf01c6
A
105}
106
107StatementNode* ScopeNode::singleStatement() const
108{
109 return m_data->m_statements ? m_data->m_statements->singleStatement() : 0;
9dae56ea
A
110}
111
9dae56ea
A
112// ------------------------------ ProgramNode -----------------------------
113
ba379fdc 114inline ProgramNode::ProgramNode(JSGlobalData* globalData, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, const SourceCode& source, CodeFeatures features, int numConstants)
9dae56ea
A
115 : ScopeNode(globalData, source, children, varStack, funcStack, features, numConstants)
116{
117}
118
ba379fdc 119PassRefPtr<ProgramNode> ProgramNode::create(JSGlobalData* globalData, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, const SourceCode& source, CodeFeatures features, int numConstants)
9dae56ea 120{
ba379fdc
A
121 RefPtr<ProgramNode> node = new ProgramNode(globalData, children, varStack, funcStack, source, features, numConstants);
122
123 ASSERT(node->data()->m_arena.last() == node);
124 node->data()->m_arena.removeLast();
125 ASSERT(!node->data()->m_arena.contains(node.get()));
126
127 return node.release();
9dae56ea
A
128}
129
9dae56ea
A
130// ------------------------------ EvalNode -----------------------------
131
ba379fdc 132inline EvalNode::EvalNode(JSGlobalData* globalData, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, const SourceCode& source, CodeFeatures features, int numConstants)
9dae56ea
A
133 : ScopeNode(globalData, source, children, varStack, funcStack, features, numConstants)
134{
135}
136
ba379fdc 137PassRefPtr<EvalNode> EvalNode::create(JSGlobalData* globalData, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, const SourceCode& source, CodeFeatures features, int numConstants)
9dae56ea 138{
ba379fdc
A
139 RefPtr<EvalNode> node = new EvalNode(globalData, children, varStack, funcStack, source, features, numConstants);
140
141 ASSERT(node->data()->m_arena.last() == node);
142 node->data()->m_arena.removeLast();
143 ASSERT(!node->data()->m_arena.contains(node.get()));
144
145 return node.release();
9dae56ea
A
146}
147
f9bf01c6 148// ------------------------------ FunctionBodyNode -----------------------------
9dae56ea 149
f9bf01c6 150FunctionParameters::FunctionParameters(ParameterNode* firstParameter)
ba379fdc 151{
f9bf01c6
A
152 for (ParameterNode* parameter = firstParameter; parameter; parameter = parameter->nextParam())
153 append(parameter->ident());
ba379fdc 154}
9dae56ea 155
ba379fdc 156inline FunctionBodyNode::FunctionBodyNode(JSGlobalData* globalData)
9dae56ea 157 : ScopeNode(globalData)
9dae56ea
A
158{
159}
160
ba379fdc 161inline FunctionBodyNode::FunctionBodyNode(JSGlobalData* globalData, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, const SourceCode& sourceCode, CodeFeatures features, int numConstants)
9dae56ea 162 : ScopeNode(globalData, sourceCode, children, varStack, funcStack, features, numConstants)
9dae56ea 163{
9dae56ea
A
164}
165
f9bf01c6 166void FunctionBodyNode::finishParsing(const SourceCode& source, ParameterNode* firstParameter, const Identifier& ident)
9dae56ea 167{
9dae56ea 168 setSource(source);
f9bf01c6 169 finishParsing(FunctionParameters::create(firstParameter), ident);
9dae56ea
A
170}
171
f9bf01c6 172void FunctionBodyNode::finishParsing(PassRefPtr<FunctionParameters> parameters, const Identifier& ident)
9dae56ea
A
173{
174 ASSERT(!source().isNull());
175 m_parameters = parameters;
f9bf01c6 176 m_ident = ident;
ba379fdc
A
177}
178
9dae56ea
A
179FunctionBodyNode* FunctionBodyNode::create(JSGlobalData* globalData)
180{
181 return new FunctionBodyNode(globalData);
182}
183
ba379fdc 184PassRefPtr<FunctionBodyNode> FunctionBodyNode::create(JSGlobalData* globalData, SourceElements* children, VarStack* varStack, FunctionStack* funcStack, const SourceCode& sourceCode, CodeFeatures features, int numConstants)
9dae56ea 185{
ba379fdc
A
186 RefPtr<FunctionBodyNode> node = new FunctionBodyNode(globalData, children, varStack, funcStack, sourceCode, features, numConstants);
187
188 ASSERT(node->data()->m_arena.last() == node);
189 node->data()->m_arena.removeLast();
190 ASSERT(!node->data()->m_arena.contains(node.get()));
191
192 return node.release();
9dae56ea
A
193}
194
9dae56ea 195} // namespace JSC