]>
git.saurik.com Git - apple/javascriptcore.git/blob - parser/Nodes.cpp
c32e4c73a24ebeb233cd5e01fffd63a48685c886
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>
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.
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.
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.
28 #include "NodeConstructors.h"
30 #include "BytecodeGenerator.h"
31 #include "CallFrame.h"
34 #include "JSFunction.h"
35 #include "JSGlobalObject.h"
36 #include "JSStaticScopeObject.h"
37 #include "LabelScope.h"
39 #include "Operations.h"
41 #include "PropertyNameArray.h"
42 #include "RegExpObject.h"
43 #include "SamplingTool.h"
44 #include <wtf/Assertions.h>
45 #include <wtf/RefCountedLeakCounter.h>
46 #include <wtf/Threading.h>
53 // ------------------------------ StatementNode --------------------------------
55 void StatementNode::setLoc(int firstLine
, int lastLine
)
57 m_lineNumber
= firstLine
;
58 m_lastLine
= lastLine
;
61 // ------------------------------ SourceElements --------------------------------
63 void SourceElements::append(StatementNode
* statement
)
65 if (statement
->isEmptyStatement())
67 m_statements
.append(statement
);
70 StatementNode
* SourceElements::singleStatement() const
72 size_t size
= m_statements
.size();
73 return size
== 1 ? m_statements
[0] : 0;
76 // ------------------------------ ScopeNode -----------------------------
78 ScopeNode::ScopeNode(JSGlobalData
* globalData
, int lineNumber
, bool inStrictContext
)
79 : StatementNode(lineNumber
)
80 , ParserArenaRefCounted(globalData
)
81 , m_features(inStrictContext
? StrictModeFeature
: NoFeatures
)
87 ScopeNode::ScopeNode(JSGlobalData
* globalData
, int lineNumber
, const SourceCode
& source
, SourceElements
* children
, VarStack
* varStack
, FunctionStack
* funcStack
, IdentifierSet
& capturedVariables
, CodeFeatures features
, int numConstants
)
88 : StatementNode(lineNumber
)
89 , ParserArenaRefCounted(globalData
)
90 , m_features(features
)
92 , m_numConstants(numConstants
)
93 , m_statements(children
)
95 m_arena
.swap(*globalData
->parserArena
);
97 m_varStack
.swap(*varStack
);
99 m_functionStack
.swap(*funcStack
);
100 m_capturedVariables
.swap(capturedVariables
);
103 StatementNode
* ScopeNode::singleStatement() const
105 return m_statements
? m_statements
->singleStatement() : 0;
108 // ------------------------------ ProgramNode -----------------------------
110 inline ProgramNode::ProgramNode(JSGlobalData
* globalData
, int lineNumber
, SourceElements
* children
, VarStack
* varStack
, FunctionStack
* funcStack
, IdentifierSet
& capturedVariables
, const SourceCode
& source
, CodeFeatures features
, int numConstants
)
111 : ScopeNode(globalData
, lineNumber
, source
, children
, varStack
, funcStack
, capturedVariables
, features
, numConstants
)
115 PassRefPtr
<ProgramNode
> ProgramNode::create(JSGlobalData
* globalData
, int lineNumber
, SourceElements
* children
, VarStack
* varStack
, FunctionStack
* funcStack
, IdentifierSet
& capturedVariables
, const SourceCode
& source
, CodeFeatures features
, int numConstants
)
117 RefPtr
<ProgramNode
> node
= new ProgramNode(globalData
, lineNumber
, children
, varStack
, funcStack
, capturedVariables
, source
, features
, numConstants
);
119 ASSERT(node
->m_arena
.last() == node
);
120 node
->m_arena
.removeLast();
121 ASSERT(!node
->m_arena
.contains(node
.get()));
123 return node
.release();
126 // ------------------------------ EvalNode -----------------------------
128 inline EvalNode::EvalNode(JSGlobalData
* globalData
, int lineNumber
, SourceElements
* children
, VarStack
* varStack
, FunctionStack
* funcStack
, IdentifierSet
& capturedVariables
, const SourceCode
& source
, CodeFeatures features
, int numConstants
)
129 : ScopeNode(globalData
, lineNumber
, source
, children
, varStack
, funcStack
, capturedVariables
, features
, numConstants
)
133 PassRefPtr
<EvalNode
> EvalNode::create(JSGlobalData
* globalData
, int lineNumber
, SourceElements
* children
, VarStack
* varStack
, FunctionStack
* funcStack
, IdentifierSet
& capturedVariables
, const SourceCode
& source
, CodeFeatures features
, int numConstants
)
135 RefPtr
<EvalNode
> node
= new EvalNode(globalData
, lineNumber
, children
, varStack
, funcStack
, capturedVariables
, source
, features
, numConstants
);
137 ASSERT(node
->m_arena
.last() == node
);
138 node
->m_arena
.removeLast();
139 ASSERT(!node
->m_arena
.contains(node
.get()));
141 return node
.release();
144 // ------------------------------ FunctionBodyNode -----------------------------
146 FunctionParameters::FunctionParameters(ParameterNode
* firstParameter
)
148 for (ParameterNode
* parameter
= firstParameter
; parameter
; parameter
= parameter
->nextParam())
149 append(parameter
->ident());
152 inline FunctionBodyNode::FunctionBodyNode(JSGlobalData
* globalData
, int lineNumber
, bool inStrictContext
)
153 : ScopeNode(globalData
, lineNumber
, inStrictContext
)
157 inline FunctionBodyNode::FunctionBodyNode(JSGlobalData
* globalData
, int lineNumber
, SourceElements
* children
, VarStack
* varStack
, FunctionStack
* funcStack
, IdentifierSet
& capturedVariables
, const SourceCode
& sourceCode
, CodeFeatures features
, int numConstants
)
158 : ScopeNode(globalData
, lineNumber
, sourceCode
, children
, varStack
, funcStack
, capturedVariables
, features
, numConstants
)
162 void FunctionBodyNode::finishParsing(const SourceCode
& source
, ParameterNode
* firstParameter
, const Identifier
& ident
)
165 finishParsing(FunctionParameters::create(firstParameter
), ident
);
168 void FunctionBodyNode::finishParsing(PassRefPtr
<FunctionParameters
> parameters
, const Identifier
& ident
)
170 ASSERT(!source().isNull());
171 m_parameters
= parameters
;
175 FunctionBodyNode
* FunctionBodyNode::create(JSGlobalData
* globalData
, int lineNumber
, bool inStrictContext
)
177 return new FunctionBodyNode(globalData
, lineNumber
, inStrictContext
);
180 PassRefPtr
<FunctionBodyNode
> FunctionBodyNode::create(JSGlobalData
* globalData
, int lineNumber
, SourceElements
* children
, VarStack
* varStack
, FunctionStack
* funcStack
, IdentifierSet
& capturedVariables
, const SourceCode
& sourceCode
, CodeFeatures features
, int numConstants
)
182 RefPtr
<FunctionBodyNode
> node
= new FunctionBodyNode(globalData
, lineNumber
, children
, varStack
, funcStack
, capturedVariables
, sourceCode
, features
, numConstants
);
184 ASSERT(node
->m_arena
.last() == node
);
185 node
->m_arena
.removeLast();
186 ASSERT(!node
->m_arena
.contains(node
.get()));
188 return node
.release();