Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_AST_H_ |
| 29 | #define V8_AST_H_ |
| 30 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 31 | #include "allocation.h" |
| 32 | #include "execution.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | #include "factory.h" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 34 | #include "jsregexp.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | #include "runtime.h" |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 36 | #include "small-pointer-list.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | #include "token.h" |
| 38 | #include "variables.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | |
| 40 | namespace v8 { |
| 41 | namespace internal { |
| 42 | |
| 43 | // The abstract syntax tree is an intermediate, light-weight |
| 44 | // representation of the parsed JavaScript code suitable for |
| 45 | // compilation to native code. |
| 46 | |
| 47 | // Nodes are allocated in a separate zone, which allows faster |
| 48 | // allocation and constant-time deallocation of the entire syntax |
| 49 | // tree. |
| 50 | |
| 51 | |
| 52 | // ---------------------------------------------------------------------------- |
| 53 | // Nodes of the abstract syntax tree. Only concrete classes are |
| 54 | // enumerated here. |
| 55 | |
| 56 | #define STATEMENT_NODE_LIST(V) \ |
| 57 | V(Block) \ |
| 58 | V(ExpressionStatement) \ |
| 59 | V(EmptyStatement) \ |
| 60 | V(IfStatement) \ |
| 61 | V(ContinueStatement) \ |
| 62 | V(BreakStatement) \ |
| 63 | V(ReturnStatement) \ |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 64 | V(WithStatement) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 65 | V(SwitchStatement) \ |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 66 | V(DoWhileStatement) \ |
| 67 | V(WhileStatement) \ |
| 68 | V(ForStatement) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 69 | V(ForInStatement) \ |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 70 | V(TryCatchStatement) \ |
| 71 | V(TryFinallyStatement) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 72 | V(DebuggerStatement) |
| 73 | |
| 74 | #define EXPRESSION_NODE_LIST(V) \ |
| 75 | V(FunctionLiteral) \ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 76 | V(SharedFunctionInfoLiteral) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 77 | V(Conditional) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 78 | V(VariableProxy) \ |
| 79 | V(Literal) \ |
| 80 | V(RegExpLiteral) \ |
| 81 | V(ObjectLiteral) \ |
| 82 | V(ArrayLiteral) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 83 | V(Assignment) \ |
| 84 | V(Throw) \ |
| 85 | V(Property) \ |
| 86 | V(Call) \ |
| 87 | V(CallNew) \ |
| 88 | V(CallRuntime) \ |
| 89 | V(UnaryOperation) \ |
| 90 | V(CountOperation) \ |
| 91 | V(BinaryOperation) \ |
| 92 | V(CompareOperation) \ |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 93 | V(CompareToNull) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | V(ThisFunction) |
| 95 | |
| 96 | #define AST_NODE_LIST(V) \ |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 97 | V(Declaration) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | STATEMENT_NODE_LIST(V) \ |
| 99 | EXPRESSION_NODE_LIST(V) |
| 100 | |
| 101 | // Forward declarations |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 102 | class BitVector; |
| 103 | class DefinitionInfo; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 104 | class MaterializedLiteral; |
| 105 | class TargetCollector; |
| 106 | class TypeFeedbackOracle; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | |
| 108 | #define DEF_FORWARD_DECLARATION(type) class type; |
| 109 | AST_NODE_LIST(DEF_FORWARD_DECLARATION) |
| 110 | #undef DEF_FORWARD_DECLARATION |
| 111 | |
| 112 | |
| 113 | // Typedef only introduced to avoid unreadable code. |
| 114 | // Please do appreciate the required space in "> >". |
| 115 | typedef ZoneList<Handle<String> > ZoneStringList; |
| 116 | typedef ZoneList<Handle<Object> > ZoneObjectList; |
| 117 | |
| 118 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 119 | #define DECLARE_NODE_TYPE(type) \ |
| 120 | virtual void Accept(AstVisitor* v); \ |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 121 | virtual AstNode::Type node_type() const { return AstNode::k##type; } \ |
| 122 | virtual type* As##type() { return this; } |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 123 | |
| 124 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 125 | class AstNode: public ZoneObject { |
| 126 | public: |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 127 | #define DECLARE_TYPE_ENUM(type) k##type, |
| 128 | enum Type { |
| 129 | AST_NODE_LIST(DECLARE_TYPE_ENUM) |
| 130 | kInvalid = -1 |
| 131 | }; |
| 132 | #undef DECLARE_TYPE_ENUM |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 133 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 134 | static const int kNoNumber = -1; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 135 | static const int kFunctionEntryId = 2; // Using 0 could disguise errors. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 136 | // This AST id identifies the point after the declarations have been |
| 137 | // visited. We need it to capture the environment effects of declarations |
| 138 | // that emit code (function declarations). |
| 139 | static const int kDeclarationsId = 3; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 140 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 141 | // Override ZoneObject's new to count allocated AST nodes. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 142 | void* operator new(size_t size, Zone* zone) { |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 143 | Isolate* isolate = zone->isolate(); |
| 144 | isolate->set_ast_node_count(isolate->ast_node_count() + 1); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 145 | return zone->New(static_cast<int>(size)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 146 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 147 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 148 | AstNode() {} |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 149 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 150 | virtual ~AstNode() { } |
| 151 | |
| 152 | virtual void Accept(AstVisitor* v) = 0; |
| 153 | virtual Type node_type() const { return kInvalid; } |
| 154 | |
| 155 | // Type testing & conversion functions overridden by concrete subclasses. |
| 156 | #define DECLARE_NODE_FUNCTIONS(type) \ |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 157 | virtual type* As##type() { return NULL; } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 158 | AST_NODE_LIST(DECLARE_NODE_FUNCTIONS) |
| 159 | #undef DECLARE_NODE_FUNCTIONS |
| 160 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | virtual Statement* AsStatement() { return NULL; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 162 | virtual Expression* AsExpression() { return NULL; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | virtual TargetCollector* AsTargetCollector() { return NULL; } |
| 164 | virtual BreakableStatement* AsBreakableStatement() { return NULL; } |
| 165 | virtual IterationStatement* AsIterationStatement() { return NULL; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 166 | virtual MaterializedLiteral* AsMaterializedLiteral() { return NULL; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 167 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 168 | // True if the node is simple enough for us to inline calls containing it. |
| 169 | virtual bool IsInlineable() const = 0; |
| 170 | |
| 171 | static int Count() { return Isolate::Current()->ast_node_count(); } |
| 172 | static void ResetIds() { Isolate::Current()->set_ast_node_id(0); } |
| 173 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 174 | protected: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 175 | static unsigned GetNextId(Isolate* isolate) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 176 | return ReserveIdRange(isolate, 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 177 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 178 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 179 | static unsigned ReserveIdRange(Isolate* isolate, int n) { |
| 180 | unsigned tmp = isolate->ast_node_id(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 181 | isolate->set_ast_node_id(tmp + n); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 182 | return tmp; |
| 183 | } |
| 184 | |
| 185 | private: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 186 | // Hidden to prevent accidental usage. It would have to load the |
| 187 | // current zone from the TLS. |
| 188 | void* operator new(size_t size); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 189 | |
| 190 | friend class CaseClause; // Generates AST IDs. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | |
| 194 | class Statement: public AstNode { |
| 195 | public: |
| 196 | Statement() : statement_pos_(RelocInfo::kNoPosition) {} |
| 197 | |
| 198 | virtual Statement* AsStatement() { return this; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 199 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 200 | virtual Assignment* StatementAsSimpleAssignment() { return NULL; } |
| 201 | virtual CountOperation* StatementAsCountOperation() { return NULL; } |
| 202 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | bool IsEmpty() { return AsEmptyStatement() != NULL; } |
| 204 | |
| 205 | void set_statement_pos(int statement_pos) { statement_pos_ = statement_pos; } |
| 206 | int statement_pos() const { return statement_pos_; } |
| 207 | |
| 208 | private: |
| 209 | int statement_pos_; |
| 210 | }; |
| 211 | |
| 212 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 213 | class SmallMapList { |
| 214 | public: |
| 215 | SmallMapList() {} |
| 216 | explicit SmallMapList(int capacity) : list_(capacity) {} |
| 217 | |
| 218 | void Reserve(int capacity) { list_.Reserve(capacity); } |
| 219 | void Clear() { list_.Clear(); } |
| 220 | |
| 221 | bool is_empty() const { return list_.is_empty(); } |
| 222 | int length() const { return list_.length(); } |
| 223 | |
| 224 | void Add(Handle<Map> handle) { |
| 225 | list_.Add(handle.location()); |
| 226 | } |
| 227 | |
| 228 | Handle<Map> at(int i) const { |
| 229 | return Handle<Map>(list_.at(i)); |
| 230 | } |
| 231 | |
| 232 | Handle<Map> first() const { return at(0); } |
| 233 | Handle<Map> last() const { return at(length() - 1); } |
| 234 | |
| 235 | private: |
| 236 | // The list stores pointers to Map*, that is Map**, so it's GC safe. |
| 237 | SmallPointerList<Map*> list_; |
| 238 | |
| 239 | DISALLOW_COPY_AND_ASSIGN(SmallMapList); |
| 240 | }; |
| 241 | |
| 242 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 243 | class Expression: public AstNode { |
| 244 | public: |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 245 | enum Context { |
| 246 | // Not assigned a context yet, or else will not be visited during |
| 247 | // code generation. |
| 248 | kUninitialized, |
| 249 | // Evaluated for its side effects. |
| 250 | kEffect, |
| 251 | // Evaluated for its value (and side effects). |
| 252 | kValue, |
| 253 | // Evaluated for control flow (and side effects). |
| 254 | kTest |
| 255 | }; |
| 256 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 257 | explicit Expression(Isolate* isolate) |
| 258 | : id_(GetNextId(isolate)), |
| 259 | test_id_(GetNextId(isolate)) {} |
| 260 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 261 | virtual int position() const { |
| 262 | UNREACHABLE(); |
| 263 | return 0; |
| 264 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 265 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 266 | virtual Expression* AsExpression() { return this; } |
| 267 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 268 | virtual bool IsTrivial() { return false; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 269 | virtual bool IsValidLeftHandSide() { return false; } |
| 270 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 271 | // Helpers for ToBoolean conversion. |
| 272 | virtual bool ToBooleanIsTrue() { return false; } |
| 273 | virtual bool ToBooleanIsFalse() { return false; } |
| 274 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 275 | // Symbols that cannot be parsed as array indices are considered property |
| 276 | // names. We do not treat symbols that can be array indexes as property |
| 277 | // names because [] for string objects is handled only by keyed ICs. |
| 278 | virtual bool IsPropertyName() { return false; } |
| 279 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 280 | // Mark the expression as being compiled as an expression |
| 281 | // statement. This is used to transform postfix increments to |
| 282 | // (faster) prefix increments. |
| 283 | virtual void MarkAsStatement() { /* do nothing */ } |
| 284 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 285 | // True iff the result can be safely overwritten (to avoid allocation). |
| 286 | // False for operations that can return one of their operands. |
| 287 | virtual bool ResultOverwriteAllowed() { return false; } |
| 288 | |
| 289 | // True iff the expression is a literal represented as a smi. |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 290 | virtual bool IsSmiLiteral() { return false; } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 291 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 292 | // Type feedback information for assignments and properties. |
| 293 | virtual bool IsMonomorphic() { |
| 294 | UNREACHABLE(); |
| 295 | return false; |
| 296 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 297 | virtual bool IsArrayLength() { |
| 298 | UNREACHABLE(); |
| 299 | return false; |
| 300 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 301 | virtual SmallMapList* GetReceiverTypes() { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 302 | UNREACHABLE(); |
| 303 | return NULL; |
| 304 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 305 | Handle<Map> GetMonomorphicReceiverType() { |
| 306 | ASSERT(IsMonomorphic()); |
| 307 | SmallMapList* types = GetReceiverTypes(); |
| 308 | ASSERT(types != NULL && types->length() == 1); |
| 309 | return types->at(0); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 312 | unsigned id() const { return id_; } |
| 313 | unsigned test_id() const { return test_id_; } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 314 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 315 | private: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 316 | unsigned id_; |
| 317 | unsigned test_id_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 321 | class BreakableStatement: public Statement { |
| 322 | public: |
| 323 | enum Type { |
| 324 | TARGET_FOR_ANONYMOUS, |
| 325 | TARGET_FOR_NAMED_ONLY |
| 326 | }; |
| 327 | |
| 328 | // The labels associated with this statement. May be NULL; |
| 329 | // if it is != NULL, guaranteed to contain at least one entry. |
| 330 | ZoneStringList* labels() const { return labels_; } |
| 331 | |
| 332 | // Type testing & conversion. |
| 333 | virtual BreakableStatement* AsBreakableStatement() { return this; } |
| 334 | |
| 335 | // Code generation |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 336 | Label* break_target() { return &break_target_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 337 | |
| 338 | // Testers. |
| 339 | bool is_target_for_anonymous() const { return type_ == TARGET_FOR_ANONYMOUS; } |
| 340 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 341 | // Bailout support. |
| 342 | int EntryId() const { return entry_id_; } |
| 343 | int ExitId() const { return exit_id_; } |
| 344 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 345 | protected: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 346 | BreakableStatement(Isolate* isolate, ZoneStringList* labels, Type type); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 347 | |
| 348 | private: |
| 349 | ZoneStringList* labels_; |
| 350 | Type type_; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 351 | Label break_target_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 352 | int entry_id_; |
| 353 | int exit_id_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 354 | }; |
| 355 | |
| 356 | |
| 357 | class Block: public BreakableStatement { |
| 358 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 359 | inline Block(Isolate* isolate, |
| 360 | ZoneStringList* labels, |
| 361 | int capacity, |
| 362 | bool is_initializer_block); |
| 363 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 364 | DECLARE_NODE_TYPE(Block) |
| 365 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 366 | virtual Assignment* StatementAsSimpleAssignment() { |
| 367 | if (statements_.length() != 1) return NULL; |
| 368 | return statements_[0]->StatementAsSimpleAssignment(); |
| 369 | } |
| 370 | |
| 371 | virtual CountOperation* StatementAsCountOperation() { |
| 372 | if (statements_.length() != 1) return NULL; |
| 373 | return statements_[0]->StatementAsCountOperation(); |
| 374 | } |
| 375 | |
| 376 | virtual bool IsInlineable() const; |
| 377 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 378 | void AddStatement(Statement* statement) { statements_.Add(statement); } |
| 379 | |
| 380 | ZoneList<Statement*>* statements() { return &statements_; } |
| 381 | bool is_initializer_block() const { return is_initializer_block_; } |
| 382 | |
| 383 | Scope* block_scope() const { return block_scope_; } |
| 384 | void set_block_scope(Scope* block_scope) { block_scope_ = block_scope; } |
| 385 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 386 | private: |
| 387 | ZoneList<Statement*> statements_; |
| 388 | bool is_initializer_block_; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 389 | Scope* block_scope_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 390 | }; |
| 391 | |
| 392 | |
| 393 | class Declaration: public AstNode { |
| 394 | public: |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 395 | Declaration(VariableProxy* proxy, |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 396 | Variable::Mode mode, |
| 397 | FunctionLiteral* fun, |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 398 | Scope* scope) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 399 | : proxy_(proxy), |
| 400 | mode_(mode), |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 401 | fun_(fun), |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 402 | scope_(scope) { |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 403 | ASSERT(mode == Variable::VAR || |
| 404 | mode == Variable::CONST || |
| 405 | mode == Variable::LET); |
| 406 | // At the moment there are no "const functions"'s in JavaScript... |
| 407 | ASSERT(fun == NULL || mode == Variable::VAR || mode == Variable::LET); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 410 | DECLARE_NODE_TYPE(Declaration) |
| 411 | |
| 412 | VariableProxy* proxy() const { return proxy_; } |
| 413 | Variable::Mode mode() const { return mode_; } |
| 414 | FunctionLiteral* fun() const { return fun_; } // may be NULL |
| 415 | virtual bool IsInlineable() const; |
| 416 | Scope* scope() const { return scope_; } |
| 417 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 418 | private: |
| 419 | VariableProxy* proxy_; |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 420 | Variable::Mode mode_; |
| 421 | FunctionLiteral* fun_; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 422 | |
| 423 | // Nested scope from which the declaration originated. |
| 424 | Scope* scope_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 425 | }; |
| 426 | |
| 427 | |
| 428 | class IterationStatement: public BreakableStatement { |
| 429 | public: |
| 430 | // Type testing & conversion. |
| 431 | virtual IterationStatement* AsIterationStatement() { return this; } |
| 432 | |
| 433 | Statement* body() const { return body_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 434 | |
| 435 | // Bailout support. |
| 436 | int OsrEntryId() const { return osr_entry_id_; } |
| 437 | virtual int ContinueId() const = 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 438 | virtual int StackCheckId() const = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 439 | |
| 440 | // Code generation |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 441 | Label* continue_target() { return &continue_target_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 442 | |
| 443 | protected: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 444 | inline IterationStatement(Isolate* isolate, ZoneStringList* labels); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 445 | |
| 446 | void Initialize(Statement* body) { |
| 447 | body_ = body; |
| 448 | } |
| 449 | |
| 450 | private: |
| 451 | Statement* body_; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 452 | Label continue_target_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 453 | int osr_entry_id_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 457 | class DoWhileStatement: public IterationStatement { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 458 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 459 | inline DoWhileStatement(Isolate* isolate, ZoneStringList* labels); |
| 460 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 461 | DECLARE_NODE_TYPE(DoWhileStatement) |
| 462 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 463 | void Initialize(Expression* cond, Statement* body) { |
| 464 | IterationStatement::Initialize(body); |
| 465 | cond_ = cond; |
| 466 | } |
| 467 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 468 | Expression* cond() const { return cond_; } |
| 469 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 470 | // Position where condition expression starts. We need it to make |
| 471 | // the loop's condition a breakable location. |
| 472 | int condition_position() { return condition_position_; } |
| 473 | void set_condition_position(int pos) { condition_position_ = pos; } |
| 474 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 475 | // Bailout support. |
| 476 | virtual int ContinueId() const { return continue_id_; } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 477 | virtual int StackCheckId() const { return back_edge_id_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 478 | int BackEdgeId() const { return back_edge_id_; } |
| 479 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 480 | virtual bool IsInlineable() const; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 481 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 482 | private: |
| 483 | Expression* cond_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 484 | int condition_position_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 485 | int continue_id_; |
| 486 | int back_edge_id_; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | |
| 490 | class WhileStatement: public IterationStatement { |
| 491 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 492 | inline WhileStatement(Isolate* isolate, ZoneStringList* labels); |
| 493 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 494 | DECLARE_NODE_TYPE(WhileStatement) |
| 495 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 496 | void Initialize(Expression* cond, Statement* body) { |
| 497 | IterationStatement::Initialize(body); |
| 498 | cond_ = cond; |
| 499 | } |
| 500 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 501 | Expression* cond() const { return cond_; } |
| 502 | bool may_have_function_literal() const { |
| 503 | return may_have_function_literal_; |
| 504 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 505 | void set_may_have_function_literal(bool value) { |
| 506 | may_have_function_literal_ = value; |
| 507 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 508 | virtual bool IsInlineable() const; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 509 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 510 | // Bailout support. |
| 511 | virtual int ContinueId() const { return EntryId(); } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 512 | virtual int StackCheckId() const { return body_id_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 513 | int BodyId() const { return body_id_; } |
| 514 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 515 | private: |
| 516 | Expression* cond_; |
| 517 | // True if there is a function literal subexpression in the condition. |
| 518 | bool may_have_function_literal_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 519 | int body_id_; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 520 | }; |
| 521 | |
| 522 | |
| 523 | class ForStatement: public IterationStatement { |
| 524 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 525 | inline ForStatement(Isolate* isolate, ZoneStringList* labels); |
| 526 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 527 | DECLARE_NODE_TYPE(ForStatement) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 528 | |
| 529 | void Initialize(Statement* init, |
| 530 | Expression* cond, |
| 531 | Statement* next, |
| 532 | Statement* body) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 533 | IterationStatement::Initialize(body); |
| 534 | init_ = init; |
| 535 | cond_ = cond; |
| 536 | next_ = next; |
| 537 | } |
| 538 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 539 | Statement* init() const { return init_; } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 540 | Expression* cond() const { return cond_; } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 541 | Statement* next() const { return next_; } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 542 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 543 | bool may_have_function_literal() const { |
| 544 | return may_have_function_literal_; |
| 545 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 546 | void set_may_have_function_literal(bool value) { |
| 547 | may_have_function_literal_ = value; |
| 548 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 549 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 550 | // Bailout support. |
| 551 | virtual int ContinueId() const { return continue_id_; } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 552 | virtual int StackCheckId() const { return body_id_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 553 | int BodyId() const { return body_id_; } |
| 554 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 555 | bool is_fast_smi_loop() { return loop_variable_ != NULL; } |
| 556 | Variable* loop_variable() { return loop_variable_; } |
| 557 | void set_loop_variable(Variable* var) { loop_variable_ = var; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 558 | virtual bool IsInlineable() const; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 559 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 560 | private: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 561 | Statement* init_; |
| 562 | Expression* cond_; |
| 563 | Statement* next_; |
| 564 | // True if there is a function literal subexpression in the condition. |
| 565 | bool may_have_function_literal_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 566 | Variable* loop_variable_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 567 | int continue_id_; |
| 568 | int body_id_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 569 | }; |
| 570 | |
| 571 | |
| 572 | class ForInStatement: public IterationStatement { |
| 573 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 574 | inline ForInStatement(Isolate* isolate, ZoneStringList* labels); |
| 575 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 576 | DECLARE_NODE_TYPE(ForInStatement) |
| 577 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 578 | void Initialize(Expression* each, Expression* enumerable, Statement* body) { |
| 579 | IterationStatement::Initialize(body); |
| 580 | each_ = each; |
| 581 | enumerable_ = enumerable; |
| 582 | } |
| 583 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 584 | Expression* each() const { return each_; } |
| 585 | Expression* enumerable() const { return enumerable_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 586 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 587 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 588 | // Bailout support. |
| 589 | int AssignmentId() const { return assignment_id_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 590 | virtual int ContinueId() const { return EntryId(); } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 591 | virtual int StackCheckId() const { return EntryId(); } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 592 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 593 | private: |
| 594 | Expression* each_; |
| 595 | Expression* enumerable_; |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 596 | int assignment_id_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 597 | }; |
| 598 | |
| 599 | |
| 600 | class ExpressionStatement: public Statement { |
| 601 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 602 | explicit ExpressionStatement(Expression* expression) |
| 603 | : expression_(expression) { } |
| 604 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 605 | DECLARE_NODE_TYPE(ExpressionStatement) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 606 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 607 | virtual bool IsInlineable() const; |
| 608 | |
| 609 | virtual Assignment* StatementAsSimpleAssignment(); |
| 610 | virtual CountOperation* StatementAsCountOperation(); |
| 611 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 612 | void set_expression(Expression* e) { expression_ = e; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 613 | Expression* expression() const { return expression_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 614 | |
| 615 | private: |
| 616 | Expression* expression_; |
| 617 | }; |
| 618 | |
| 619 | |
| 620 | class ContinueStatement: public Statement { |
| 621 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 622 | explicit ContinueStatement(IterationStatement* target) |
| 623 | : target_(target) { } |
| 624 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 625 | DECLARE_NODE_TYPE(ContinueStatement) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 626 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 627 | IterationStatement* target() const { return target_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 628 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 629 | |
| 630 | private: |
| 631 | IterationStatement* target_; |
| 632 | }; |
| 633 | |
| 634 | |
| 635 | class BreakStatement: public Statement { |
| 636 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 637 | explicit BreakStatement(BreakableStatement* target) |
| 638 | : target_(target) { } |
| 639 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 640 | DECLARE_NODE_TYPE(BreakStatement) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 641 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 642 | BreakableStatement* target() const { return target_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 643 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 644 | |
| 645 | private: |
| 646 | BreakableStatement* target_; |
| 647 | }; |
| 648 | |
| 649 | |
| 650 | class ReturnStatement: public Statement { |
| 651 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 652 | explicit ReturnStatement(Expression* expression) |
| 653 | : expression_(expression) { } |
| 654 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 655 | DECLARE_NODE_TYPE(ReturnStatement) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 656 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 657 | Expression* expression() const { return expression_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 658 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 659 | |
| 660 | private: |
| 661 | Expression* expression_; |
| 662 | }; |
| 663 | |
| 664 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 665 | class WithStatement: public Statement { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 666 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 667 | WithStatement(Expression* expression, Statement* statement) |
| 668 | : expression_(expression), statement_(statement) { } |
| 669 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 670 | DECLARE_NODE_TYPE(WithStatement) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 671 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 672 | Expression* expression() const { return expression_; } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 673 | Statement* statement() const { return statement_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 674 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 675 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 676 | |
| 677 | private: |
| 678 | Expression* expression_; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 679 | Statement* statement_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 680 | }; |
| 681 | |
| 682 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 683 | class CaseClause: public ZoneObject { |
| 684 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 685 | CaseClause(Isolate* isolate, |
| 686 | Expression* label, |
| 687 | ZoneList<Statement*>* statements, |
| 688 | int pos); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 689 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 690 | bool is_default() const { return label_ == NULL; } |
| 691 | Expression* label() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 692 | CHECK(!is_default()); |
| 693 | return label_; |
| 694 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 695 | Label* body_target() { return &body_target_; } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 696 | ZoneList<Statement*>* statements() const { return statements_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 697 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 698 | int position() const { return position_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 699 | void set_position(int pos) { position_ = pos; } |
| 700 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 701 | int EntryId() { return entry_id_; } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 702 | int CompareId() { return compare_id_; } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 703 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 704 | // Type feedback information. |
| 705 | void RecordTypeFeedback(TypeFeedbackOracle* oracle); |
| 706 | bool IsSmiCompare() { return compare_type_ == SMI_ONLY; } |
| 707 | bool IsObjectCompare() { return compare_type_ == OBJECT_ONLY; } |
| 708 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 709 | private: |
| 710 | Expression* label_; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 711 | Label body_target_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 712 | ZoneList<Statement*>* statements_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 713 | int position_; |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 714 | enum CompareTypeFeedback { NONE, SMI_ONLY, OBJECT_ONLY }; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 715 | CompareTypeFeedback compare_type_; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 716 | int compare_id_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 717 | int entry_id_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 718 | }; |
| 719 | |
| 720 | |
| 721 | class SwitchStatement: public BreakableStatement { |
| 722 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 723 | inline SwitchStatement(Isolate* isolate, ZoneStringList* labels); |
| 724 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 725 | DECLARE_NODE_TYPE(SwitchStatement) |
| 726 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 727 | void Initialize(Expression* tag, ZoneList<CaseClause*>* cases) { |
| 728 | tag_ = tag; |
| 729 | cases_ = cases; |
| 730 | } |
| 731 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 732 | Expression* tag() const { return tag_; } |
| 733 | ZoneList<CaseClause*>* cases() const { return cases_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 734 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 735 | |
| 736 | private: |
| 737 | Expression* tag_; |
| 738 | ZoneList<CaseClause*>* cases_; |
| 739 | }; |
| 740 | |
| 741 | |
| 742 | // If-statements always have non-null references to their then- and |
| 743 | // else-parts. When parsing if-statements with no explicit else-part, |
| 744 | // the parser implicitly creates an empty statement. Use the |
| 745 | // HasThenStatement() and HasElseStatement() functions to check if a |
| 746 | // given if-statement has a then- or an else-part containing code. |
| 747 | class IfStatement: public Statement { |
| 748 | public: |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 749 | IfStatement(Isolate* isolate, |
| 750 | Expression* condition, |
| 751 | Statement* then_statement, |
| 752 | Statement* else_statement) |
| 753 | : condition_(condition), |
| 754 | then_statement_(then_statement), |
| 755 | else_statement_(else_statement), |
| 756 | if_id_(GetNextId(isolate)), |
| 757 | then_id_(GetNextId(isolate)), |
| 758 | else_id_(GetNextId(isolate)) { |
| 759 | } |
| 760 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 761 | DECLARE_NODE_TYPE(IfStatement) |
| 762 | |
| 763 | virtual bool IsInlineable() const; |
| 764 | |
| 765 | bool HasThenStatement() const { return !then_statement()->IsEmpty(); } |
| 766 | bool HasElseStatement() const { return !else_statement()->IsEmpty(); } |
| 767 | |
| 768 | Expression* condition() const { return condition_; } |
| 769 | Statement* then_statement() const { return then_statement_; } |
| 770 | Statement* else_statement() const { return else_statement_; } |
| 771 | |
| 772 | int IfId() const { return if_id_; } |
| 773 | int ThenId() const { return then_id_; } |
| 774 | int ElseId() const { return else_id_; } |
| 775 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 776 | private: |
| 777 | Expression* condition_; |
| 778 | Statement* then_statement_; |
| 779 | Statement* else_statement_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 780 | int if_id_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 781 | int then_id_; |
| 782 | int else_id_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 783 | }; |
| 784 | |
| 785 | |
| 786 | // NOTE: TargetCollectors are represented as nodes to fit in the target |
| 787 | // stack in the compiler; this should probably be reworked. |
| 788 | class TargetCollector: public AstNode { |
| 789 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 790 | TargetCollector(): targets_(0) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 791 | |
| 792 | // Adds a jump target to the collector. The collector stores a pointer not |
| 793 | // a copy of the target to make binding work, so make sure not to pass in |
| 794 | // references to something on the stack. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 795 | void AddTarget(Label* target); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 796 | |
| 797 | // Virtual behaviour. TargetCollectors are never part of the AST. |
| 798 | virtual void Accept(AstVisitor* v) { UNREACHABLE(); } |
| 799 | virtual TargetCollector* AsTargetCollector() { return this; } |
| 800 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 801 | ZoneList<Label*>* targets() { return &targets_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 802 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 803 | |
| 804 | private: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 805 | ZoneList<Label*> targets_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 806 | }; |
| 807 | |
| 808 | |
| 809 | class TryStatement: public Statement { |
| 810 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 811 | explicit TryStatement(Block* try_block) |
| 812 | : try_block_(try_block), escaping_targets_(NULL) { } |
| 813 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 814 | void set_escaping_targets(ZoneList<Label*>* targets) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 815 | escaping_targets_ = targets; |
| 816 | } |
| 817 | |
| 818 | Block* try_block() const { return try_block_; } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 819 | ZoneList<Label*>* escaping_targets() const { return escaping_targets_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 820 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 821 | |
| 822 | private: |
| 823 | Block* try_block_; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 824 | ZoneList<Label*>* escaping_targets_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 825 | }; |
| 826 | |
| 827 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 828 | class TryCatchStatement: public TryStatement { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 829 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 830 | TryCatchStatement(Block* try_block, |
| 831 | Scope* scope, |
| 832 | Variable* variable, |
| 833 | Block* catch_block) |
| 834 | : TryStatement(try_block), |
| 835 | scope_(scope), |
| 836 | variable_(variable), |
| 837 | catch_block_(catch_block) { |
| 838 | } |
| 839 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 840 | DECLARE_NODE_TYPE(TryCatchStatement) |
| 841 | |
| 842 | Scope* scope() { return scope_; } |
| 843 | Variable* variable() { return variable_; } |
| 844 | Block* catch_block() const { return catch_block_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 845 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 846 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 847 | private: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 848 | Scope* scope_; |
| 849 | Variable* variable_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 850 | Block* catch_block_; |
| 851 | }; |
| 852 | |
| 853 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 854 | class TryFinallyStatement: public TryStatement { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 855 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 856 | TryFinallyStatement(Block* try_block, Block* finally_block) |
| 857 | : TryStatement(try_block), |
| 858 | finally_block_(finally_block) { } |
| 859 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 860 | DECLARE_NODE_TYPE(TryFinallyStatement) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 861 | |
| 862 | Block* finally_block() const { return finally_block_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 863 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 864 | |
| 865 | private: |
| 866 | Block* finally_block_; |
| 867 | }; |
| 868 | |
| 869 | |
| 870 | class DebuggerStatement: public Statement { |
| 871 | public: |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 872 | DECLARE_NODE_TYPE(DebuggerStatement) |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 873 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 874 | }; |
| 875 | |
| 876 | |
| 877 | class EmptyStatement: public Statement { |
| 878 | public: |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 879 | DECLARE_NODE_TYPE(EmptyStatement) |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 880 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 881 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 882 | }; |
| 883 | |
| 884 | |
| 885 | class Literal: public Expression { |
| 886 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 887 | Literal(Isolate* isolate, Handle<Object> handle) |
| 888 | : Expression(isolate), handle_(handle) { } |
| 889 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 890 | DECLARE_NODE_TYPE(Literal) |
| 891 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 892 | virtual bool IsTrivial() { return true; } |
| 893 | virtual bool IsSmiLiteral() { return handle_->IsSmi(); } |
| 894 | |
| 895 | // Check if this literal is identical to the other literal. |
| 896 | bool IsIdenticalTo(const Literal* other) const { |
| 897 | return handle_.is_identical_to(other->handle_); |
| 898 | } |
| 899 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 900 | virtual bool IsPropertyName() { |
| 901 | if (handle_->IsSymbol()) { |
| 902 | uint32_t ignored; |
| 903 | return !String::cast(*handle_)->AsArrayIndex(&ignored); |
| 904 | } |
| 905 | return false; |
| 906 | } |
| 907 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 908 | Handle<String> AsPropertyName() { |
| 909 | ASSERT(IsPropertyName()); |
| 910 | return Handle<String>::cast(handle_); |
| 911 | } |
| 912 | |
| 913 | virtual bool ToBooleanIsTrue() { return handle_->ToBoolean()->IsTrue(); } |
| 914 | virtual bool ToBooleanIsFalse() { return handle_->ToBoolean()->IsFalse(); } |
| 915 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 916 | // Identity testers. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 917 | bool IsNull() const { |
| 918 | ASSERT(!handle_.is_null()); |
| 919 | return handle_->IsNull(); |
| 920 | } |
| 921 | bool IsTrue() const { |
| 922 | ASSERT(!handle_.is_null()); |
| 923 | return handle_->IsTrue(); |
| 924 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 925 | bool IsFalse() const { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 926 | ASSERT(!handle_.is_null()); |
| 927 | return handle_->IsFalse(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | Handle<Object> handle() const { return handle_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 931 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 932 | |
| 933 | private: |
| 934 | Handle<Object> handle_; |
| 935 | }; |
| 936 | |
| 937 | |
| 938 | // Base class for literals that needs space in the corresponding JSFunction. |
| 939 | class MaterializedLiteral: public Expression { |
| 940 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 941 | MaterializedLiteral(Isolate* isolate, |
| 942 | int literal_index, |
| 943 | bool is_simple, |
| 944 | int depth) |
| 945 | : Expression(isolate), |
| 946 | literal_index_(literal_index), |
| 947 | is_simple_(is_simple), |
| 948 | depth_(depth) {} |
| 949 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 950 | virtual MaterializedLiteral* AsMaterializedLiteral() { return this; } |
| 951 | |
| 952 | int literal_index() { return literal_index_; } |
| 953 | |
| 954 | // A materialized literal is simple if the values consist of only |
| 955 | // constants and simple object and array literals. |
| 956 | bool is_simple() const { return is_simple_; } |
| 957 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 958 | int depth() const { return depth_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 959 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 960 | |
| 961 | private: |
| 962 | int literal_index_; |
| 963 | bool is_simple_; |
| 964 | int depth_; |
| 965 | }; |
| 966 | |
| 967 | |
| 968 | // An object literal has a boilerplate object that is used |
| 969 | // for minimizing the work when constructing it at runtime. |
| 970 | class ObjectLiteral: public MaterializedLiteral { |
| 971 | public: |
| 972 | // Property is used for passing information |
| 973 | // about an object literal's properties from the parser |
| 974 | // to the code generator. |
| 975 | class Property: public ZoneObject { |
| 976 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 977 | enum Kind { |
| 978 | CONSTANT, // Property with constant value (compile time). |
| 979 | COMPUTED, // Property with computed value (execution time). |
| 980 | MATERIALIZED_LITERAL, // Property value is a materialized literal. |
| 981 | GETTER, SETTER, // Property is an accessor function. |
| 982 | PROTOTYPE // Property is __proto__. |
| 983 | }; |
| 984 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 985 | Property(Literal* key, Expression* value); |
| 986 | Property(bool is_getter, FunctionLiteral* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 987 | |
| 988 | Literal* key() { return key_; } |
| 989 | Expression* value() { return value_; } |
| 990 | Kind kind() { return kind_; } |
| 991 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 992 | bool IsCompileTimeValue(); |
| 993 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 994 | void set_emit_store(bool emit_store); |
| 995 | bool emit_store(); |
| 996 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 997 | private: |
| 998 | Literal* key_; |
| 999 | Expression* value_; |
| 1000 | Kind kind_; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1001 | bool emit_store_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1002 | }; |
| 1003 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1004 | ObjectLiteral(Isolate* isolate, |
| 1005 | Handle<FixedArray> constant_properties, |
| 1006 | ZoneList<Property*>* properties, |
| 1007 | int literal_index, |
| 1008 | bool is_simple, |
| 1009 | bool fast_elements, |
| 1010 | int depth, |
| 1011 | bool has_function) |
| 1012 | : MaterializedLiteral(isolate, literal_index, is_simple, depth), |
| 1013 | constant_properties_(constant_properties), |
| 1014 | properties_(properties), |
| 1015 | fast_elements_(fast_elements), |
| 1016 | has_function_(has_function) {} |
| 1017 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1018 | DECLARE_NODE_TYPE(ObjectLiteral) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1019 | |
| 1020 | Handle<FixedArray> constant_properties() const { |
| 1021 | return constant_properties_; |
| 1022 | } |
| 1023 | ZoneList<Property*>* properties() const { return properties_; } |
| 1024 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1025 | bool fast_elements() const { return fast_elements_; } |
| 1026 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1027 | bool has_function() { return has_function_; } |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1028 | |
| 1029 | // Mark all computed expressions that are bound to a key that |
| 1030 | // is shadowed by a later occurrence of the same key. For the |
| 1031 | // marked expressions, no store code is emitted. |
| 1032 | void CalculateEmitStore(); |
| 1033 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1034 | enum Flags { |
| 1035 | kNoFlags = 0, |
| 1036 | kFastElements = 1, |
| 1037 | kHasFunction = 1 << 1 |
| 1038 | }; |
| 1039 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1040 | private: |
| 1041 | Handle<FixedArray> constant_properties_; |
| 1042 | ZoneList<Property*>* properties_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1043 | bool fast_elements_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1044 | bool has_function_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1045 | }; |
| 1046 | |
| 1047 | |
| 1048 | // Node for capturing a regexp literal. |
| 1049 | class RegExpLiteral: public MaterializedLiteral { |
| 1050 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1051 | RegExpLiteral(Isolate* isolate, |
| 1052 | Handle<String> pattern, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1053 | Handle<String> flags, |
| 1054 | int literal_index) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1055 | : MaterializedLiteral(isolate, literal_index, false, 1), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1056 | pattern_(pattern), |
| 1057 | flags_(flags) {} |
| 1058 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1059 | DECLARE_NODE_TYPE(RegExpLiteral) |
| 1060 | |
| 1061 | Handle<String> pattern() const { return pattern_; } |
| 1062 | Handle<String> flags() const { return flags_; } |
| 1063 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1064 | private: |
| 1065 | Handle<String> pattern_; |
| 1066 | Handle<String> flags_; |
| 1067 | }; |
| 1068 | |
| 1069 | // An array literal has a literals object that is used |
| 1070 | // for minimizing the work when constructing it at runtime. |
| 1071 | class ArrayLiteral: public MaterializedLiteral { |
| 1072 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1073 | ArrayLiteral(Isolate* isolate, |
| 1074 | Handle<FixedArray> constant_elements, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1075 | ZoneList<Expression*>* values, |
| 1076 | int literal_index, |
| 1077 | bool is_simple, |
| 1078 | int depth) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1079 | : MaterializedLiteral(isolate, literal_index, is_simple, depth), |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1080 | constant_elements_(constant_elements), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1081 | values_(values), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1082 | first_element_id_(ReserveIdRange(isolate, values->length())) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1083 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1084 | DECLARE_NODE_TYPE(ArrayLiteral) |
| 1085 | |
| 1086 | Handle<FixedArray> constant_elements() const { return constant_elements_; } |
| 1087 | ZoneList<Expression*>* values() const { return values_; } |
| 1088 | |
| 1089 | // Return an AST id for an element that is used in simulate instructions. |
| 1090 | int GetIdForElement(int i) { return first_element_id_ + i; } |
| 1091 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1092 | private: |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1093 | Handle<FixedArray> constant_elements_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1094 | ZoneList<Expression*>* values_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1095 | int first_element_id_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1096 | }; |
| 1097 | |
| 1098 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1099 | class VariableProxy: public Expression { |
| 1100 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1101 | VariableProxy(Isolate* isolate, Variable* var); |
| 1102 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1103 | DECLARE_NODE_TYPE(VariableProxy) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1104 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1105 | virtual bool IsValidLeftHandSide() { |
| 1106 | return var_ == NULL ? true : var_->IsValidLeftHandSide(); |
| 1107 | } |
| 1108 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1109 | virtual bool IsTrivial() { |
| 1110 | // Reading from a mutable variable is a side effect, but the |
| 1111 | // variable for 'this' is immutable. |
| 1112 | return is_this_ || is_trivial_; |
| 1113 | } |
| 1114 | |
| 1115 | virtual bool IsInlineable() const; |
| 1116 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1117 | bool IsVariable(Handle<String> n) { |
| 1118 | return !is_this() && name().is_identical_to(n); |
| 1119 | } |
| 1120 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1121 | bool IsArguments() { return var_ != NULL && var_->is_arguments(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1122 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1123 | Handle<String> name() const { return name_; } |
| 1124 | Variable* var() const { return var_; } |
| 1125 | bool is_this() const { return is_this_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1126 | bool inside_with() const { return inside_with_; } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1127 | int position() const { return position_; } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1128 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1129 | void MarkAsTrivial() { is_trivial_ = true; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1130 | |
| 1131 | // Bind this proxy to the variable var. |
| 1132 | void BindTo(Variable* var); |
| 1133 | |
| 1134 | protected: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1135 | Handle<String> name_; |
| 1136 | Variable* var_; // resolved variable, or NULL |
| 1137 | bool is_this_; |
| 1138 | bool inside_with_; |
| 1139 | bool is_trivial_; |
| 1140 | int position_; |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 1141 | |
| 1142 | VariableProxy(Isolate* isolate, |
| 1143 | Handle<String> name, |
| 1144 | bool is_this, |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1145 | bool inside_with, |
| 1146 | int position = RelocInfo::kNoPosition); |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 1147 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1148 | friend class Scope; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1149 | }; |
| 1150 | |
| 1151 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1152 | class Property: public Expression { |
| 1153 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1154 | Property(Isolate* isolate, |
| 1155 | Expression* obj, |
| 1156 | Expression* key, |
| 1157 | int pos) |
| 1158 | : Expression(isolate), |
| 1159 | obj_(obj), |
| 1160 | key_(key), |
| 1161 | pos_(pos), |
| 1162 | is_monomorphic_(false), |
| 1163 | is_array_length_(false), |
| 1164 | is_string_length_(false), |
| 1165 | is_string_access_(false), |
| 1166 | is_function_prototype_(false) { } |
| 1167 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1168 | DECLARE_NODE_TYPE(Property) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1169 | |
| 1170 | virtual bool IsValidLeftHandSide() { return true; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1171 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1172 | |
| 1173 | Expression* obj() const { return obj_; } |
| 1174 | Expression* key() const { return key_; } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1175 | virtual int position() const { return pos_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1176 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1177 | bool IsStringLength() const { return is_string_length_; } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1178 | bool IsStringAccess() const { return is_string_access_; } |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 1179 | bool IsFunctionPrototype() const { return is_function_prototype_; } |
| 1180 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1181 | // Type feedback information. |
| 1182 | void RecordTypeFeedback(TypeFeedbackOracle* oracle); |
| 1183 | virtual bool IsMonomorphic() { return is_monomorphic_; } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1184 | virtual SmallMapList* GetReceiverTypes() { return &receiver_types_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1185 | virtual bool IsArrayLength() { return is_array_length_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1186 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1187 | private: |
| 1188 | Expression* obj_; |
| 1189 | Expression* key_; |
| 1190 | int pos_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1191 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1192 | SmallMapList receiver_types_; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1193 | bool is_monomorphic_ : 1; |
| 1194 | bool is_array_length_ : 1; |
| 1195 | bool is_string_length_ : 1; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1196 | bool is_string_access_ : 1; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1197 | bool is_function_prototype_ : 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1198 | }; |
| 1199 | |
| 1200 | |
| 1201 | class Call: public Expression { |
| 1202 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1203 | Call(Isolate* isolate, |
| 1204 | Expression* expression, |
| 1205 | ZoneList<Expression*>* arguments, |
| 1206 | int pos) |
| 1207 | : Expression(isolate), |
| 1208 | expression_(expression), |
| 1209 | arguments_(arguments), |
| 1210 | pos_(pos), |
| 1211 | is_monomorphic_(false), |
| 1212 | check_type_(RECEIVER_MAP_CHECK), |
| 1213 | return_id_(GetNextId(isolate)) { |
| 1214 | } |
| 1215 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1216 | DECLARE_NODE_TYPE(Call) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1217 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1218 | virtual bool IsInlineable() const; |
| 1219 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1220 | Expression* expression() const { return expression_; } |
| 1221 | ZoneList<Expression*>* arguments() const { return arguments_; } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1222 | virtual int position() const { return pos_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1223 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1224 | void RecordTypeFeedback(TypeFeedbackOracle* oracle, |
| 1225 | CallKind call_kind); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1226 | virtual SmallMapList* GetReceiverTypes() { return &receiver_types_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1227 | virtual bool IsMonomorphic() { return is_monomorphic_; } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 1228 | CheckType check_type() const { return check_type_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1229 | Handle<JSFunction> target() { return target_; } |
| 1230 | Handle<JSObject> holder() { return holder_; } |
| 1231 | Handle<JSGlobalPropertyCell> cell() { return cell_; } |
| 1232 | |
| 1233 | bool ComputeTarget(Handle<Map> type, Handle<String> name); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1234 | bool ComputeGlobalTarget(Handle<GlobalObject> global, LookupResult* lookup); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1235 | |
| 1236 | // Bailout support. |
| 1237 | int ReturnId() const { return return_id_; } |
| 1238 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1239 | #ifdef DEBUG |
| 1240 | // Used to assert that the FullCodeGenerator records the return site. |
| 1241 | bool return_is_recorded_; |
| 1242 | #endif |
| 1243 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1244 | private: |
| 1245 | Expression* expression_; |
| 1246 | ZoneList<Expression*>* arguments_; |
| 1247 | int pos_; |
| 1248 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1249 | bool is_monomorphic_; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 1250 | CheckType check_type_; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1251 | SmallMapList receiver_types_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1252 | Handle<JSFunction> target_; |
| 1253 | Handle<JSObject> holder_; |
| 1254 | Handle<JSGlobalPropertyCell> cell_; |
| 1255 | |
| 1256 | int return_id_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1257 | }; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1258 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1259 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1260 | class CallNew: public Expression { |
| 1261 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1262 | CallNew(Isolate* isolate, |
| 1263 | Expression* expression, |
| 1264 | ZoneList<Expression*>* arguments, |
| 1265 | int pos) |
| 1266 | : Expression(isolate), |
| 1267 | expression_(expression), |
| 1268 | arguments_(arguments), |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1269 | pos_(pos) { } |
| 1270 | |
| 1271 | DECLARE_NODE_TYPE(CallNew) |
| 1272 | |
| 1273 | virtual bool IsInlineable() const; |
| 1274 | |
| 1275 | Expression* expression() const { return expression_; } |
| 1276 | ZoneList<Expression*>* arguments() const { return arguments_; } |
| 1277 | virtual int position() const { return pos_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1278 | |
| 1279 | private: |
| 1280 | Expression* expression_; |
| 1281 | ZoneList<Expression*>* arguments_; |
| 1282 | int pos_; |
| 1283 | }; |
| 1284 | |
| 1285 | |
| 1286 | // The CallRuntime class does not represent any official JavaScript |
| 1287 | // language construct. Instead it is used to call a C or JS function |
| 1288 | // with a set of arguments. This is used from the builtins that are |
| 1289 | // implemented in JavaScript (see "v8natives.js"). |
| 1290 | class CallRuntime: public Expression { |
| 1291 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1292 | CallRuntime(Isolate* isolate, |
| 1293 | Handle<String> name, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1294 | const Runtime::Function* function, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1295 | ZoneList<Expression*>* arguments) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1296 | : Expression(isolate), |
| 1297 | name_(name), |
| 1298 | function_(function), |
| 1299 | arguments_(arguments) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1300 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1301 | DECLARE_NODE_TYPE(CallRuntime) |
| 1302 | |
| 1303 | virtual bool IsInlineable() const; |
| 1304 | |
| 1305 | Handle<String> name() const { return name_; } |
| 1306 | const Runtime::Function* function() const { return function_; } |
| 1307 | ZoneList<Expression*>* arguments() const { return arguments_; } |
| 1308 | bool is_jsruntime() const { return function_ == NULL; } |
| 1309 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1310 | private: |
| 1311 | Handle<String> name_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1312 | const Runtime::Function* function_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1313 | ZoneList<Expression*>* arguments_; |
| 1314 | }; |
| 1315 | |
| 1316 | |
| 1317 | class UnaryOperation: public Expression { |
| 1318 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1319 | UnaryOperation(Isolate* isolate, |
| 1320 | Token::Value op, |
| 1321 | Expression* expression, |
| 1322 | int pos) |
| 1323 | : Expression(isolate), op_(op), expression_(expression), pos_(pos) { |
| 1324 | ASSERT(Token::IsUnaryOp(op)); |
| 1325 | } |
| 1326 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 1327 | DECLARE_NODE_TYPE(UnaryOperation) |
| 1328 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1329 | virtual bool IsInlineable() const; |
| 1330 | |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 1331 | virtual bool ResultOverwriteAllowed(); |
| 1332 | |
| 1333 | Token::Value op() const { return op_; } |
| 1334 | Expression* expression() const { return expression_; } |
| 1335 | virtual int position() const { return pos_; } |
| 1336 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1337 | private: |
| 1338 | Token::Value op_; |
| 1339 | Expression* expression_; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1340 | int pos_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1341 | }; |
| 1342 | |
| 1343 | |
| 1344 | class BinaryOperation: public Expression { |
| 1345 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1346 | BinaryOperation(Isolate* isolate, |
| 1347 | Token::Value op, |
| 1348 | Expression* left, |
| 1349 | Expression* right, |
| 1350 | int pos) |
| 1351 | : Expression(isolate), op_(op), left_(left), right_(right), pos_(pos) { |
| 1352 | ASSERT(Token::IsBinaryOp(op)); |
| 1353 | right_id_ = (op == Token::AND || op == Token::OR) |
| 1354 | ? static_cast<int>(GetNextId(isolate)) |
| 1355 | : AstNode::kNoNumber; |
| 1356 | } |
| 1357 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1358 | DECLARE_NODE_TYPE(BinaryOperation) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1359 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1360 | virtual bool IsInlineable() const; |
| 1361 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1362 | virtual bool ResultOverwriteAllowed(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1363 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1364 | Token::Value op() const { return op_; } |
| 1365 | Expression* left() const { return left_; } |
| 1366 | Expression* right() const { return right_; } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1367 | virtual int position() const { return pos_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1368 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1369 | // Bailout support. |
| 1370 | int RightId() const { return right_id_; } |
| 1371 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1372 | private: |
| 1373 | Token::Value op_; |
| 1374 | Expression* left_; |
| 1375 | Expression* right_; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1376 | int pos_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1377 | // The short-circuit logical operations have an AST ID for their |
| 1378 | // right-hand subexpression. |
| 1379 | int right_id_; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1380 | }; |
| 1381 | |
| 1382 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1383 | class CountOperation: public Expression { |
| 1384 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1385 | CountOperation(Isolate* isolate, |
| 1386 | Token::Value op, |
| 1387 | bool is_prefix, |
| 1388 | Expression* expr, |
| 1389 | int pos) |
| 1390 | : Expression(isolate), |
| 1391 | op_(op), |
| 1392 | is_prefix_(is_prefix), |
| 1393 | expression_(expr), |
| 1394 | pos_(pos), |
| 1395 | assignment_id_(GetNextId(isolate)), |
| 1396 | count_id_(GetNextId(isolate)) {} |
| 1397 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1398 | DECLARE_NODE_TYPE(CountOperation) |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1399 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1400 | bool is_prefix() const { return is_prefix_; } |
| 1401 | bool is_postfix() const { return !is_prefix_; } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1402 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1403 | Token::Value op() const { return op_; } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1404 | Token::Value binary_op() { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1405 | return (op() == Token::INC) ? Token::ADD : Token::SUB; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1406 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1407 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1408 | Expression* expression() const { return expression_; } |
| 1409 | virtual int position() const { return pos_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1410 | |
| 1411 | virtual void MarkAsStatement() { is_prefix_ = true; } |
| 1412 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1413 | virtual bool IsInlineable() const; |
| 1414 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1415 | void RecordTypeFeedback(TypeFeedbackOracle* oracle); |
| 1416 | virtual bool IsMonomorphic() { return is_monomorphic_; } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1417 | virtual SmallMapList* GetReceiverTypes() { return &receiver_types_; } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1418 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1419 | // Bailout support. |
| 1420 | int AssignmentId() const { return assignment_id_; } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1421 | int CountId() const { return count_id_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1422 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1423 | private: |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1424 | Token::Value op_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1425 | bool is_prefix_; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1426 | bool is_monomorphic_; |
| 1427 | Expression* expression_; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1428 | int pos_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1429 | int assignment_id_; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1430 | int count_id_; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1431 | SmallMapList receiver_types_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1432 | }; |
| 1433 | |
| 1434 | |
| 1435 | class CompareOperation: public Expression { |
| 1436 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1437 | CompareOperation(Isolate* isolate, |
| 1438 | Token::Value op, |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1439 | Expression* left, |
| 1440 | Expression* right, |
| 1441 | int pos) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1442 | : Expression(isolate), |
| 1443 | op_(op), |
| 1444 | left_(left), |
| 1445 | right_(right), |
| 1446 | pos_(pos), |
| 1447 | compare_type_(NONE) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1448 | ASSERT(Token::IsCompareOp(op)); |
| 1449 | } |
| 1450 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1451 | DECLARE_NODE_TYPE(CompareOperation) |
| 1452 | |
| 1453 | Token::Value op() const { return op_; } |
| 1454 | Expression* left() const { return left_; } |
| 1455 | Expression* right() const { return right_; } |
| 1456 | virtual int position() const { return pos_; } |
| 1457 | |
| 1458 | virtual bool IsInlineable() const; |
| 1459 | |
| 1460 | // Type feedback information. |
| 1461 | void RecordTypeFeedback(TypeFeedbackOracle* oracle); |
| 1462 | bool IsSmiCompare() { return compare_type_ == SMI_ONLY; } |
| 1463 | bool IsObjectCompare() { return compare_type_ == OBJECT_ONLY; } |
| 1464 | |
| 1465 | // Match special cases. |
| 1466 | bool IsLiteralCompareTypeof(Expression** expr, Handle<String>* check); |
| 1467 | bool IsLiteralCompareUndefined(Expression** expr); |
| 1468 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1469 | private: |
| 1470 | Token::Value op_; |
| 1471 | Expression* left_; |
| 1472 | Expression* right_; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1473 | int pos_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1474 | |
| 1475 | enum CompareTypeFeedback { NONE, SMI_ONLY, OBJECT_ONLY }; |
| 1476 | CompareTypeFeedback compare_type_; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1477 | }; |
| 1478 | |
| 1479 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1480 | class CompareToNull: public Expression { |
| 1481 | public: |
| 1482 | CompareToNull(Isolate* isolate, bool is_strict, Expression* expression) |
| 1483 | : Expression(isolate), is_strict_(is_strict), expression_(expression) { } |
| 1484 | |
| 1485 | DECLARE_NODE_TYPE(CompareToNull) |
| 1486 | |
| 1487 | virtual bool IsInlineable() const; |
| 1488 | |
| 1489 | bool is_strict() const { return is_strict_; } |
| 1490 | Token::Value op() const { return is_strict_ ? Token::EQ_STRICT : Token::EQ; } |
| 1491 | Expression* expression() const { return expression_; } |
| 1492 | |
| 1493 | private: |
| 1494 | bool is_strict_; |
| 1495 | Expression* expression_; |
| 1496 | }; |
| 1497 | |
| 1498 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1499 | class Conditional: public Expression { |
| 1500 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1501 | Conditional(Isolate* isolate, |
| 1502 | Expression* condition, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1503 | Expression* then_expression, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1504 | Expression* else_expression, |
| 1505 | int then_expression_position, |
| 1506 | int else_expression_position) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1507 | : Expression(isolate), |
| 1508 | condition_(condition), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1509 | then_expression_(then_expression), |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1510 | else_expression_(else_expression), |
| 1511 | then_expression_position_(then_expression_position), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1512 | else_expression_position_(else_expression_position), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1513 | then_id_(GetNextId(isolate)), |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1514 | else_id_(GetNextId(isolate)) { |
| 1515 | } |
| 1516 | |
| 1517 | DECLARE_NODE_TYPE(Conditional) |
| 1518 | |
| 1519 | virtual bool IsInlineable() const; |
| 1520 | |
| 1521 | Expression* condition() const { return condition_; } |
| 1522 | Expression* then_expression() const { return then_expression_; } |
| 1523 | Expression* else_expression() const { return else_expression_; } |
| 1524 | |
| 1525 | int then_expression_position() const { return then_expression_position_; } |
| 1526 | int else_expression_position() const { return else_expression_position_; } |
| 1527 | |
| 1528 | int ThenId() const { return then_id_; } |
| 1529 | int ElseId() const { return else_id_; } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1530 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1531 | private: |
| 1532 | Expression* condition_; |
| 1533 | Expression* then_expression_; |
| 1534 | Expression* else_expression_; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1535 | int then_expression_position_; |
| 1536 | int else_expression_position_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1537 | int then_id_; |
| 1538 | int else_id_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1539 | }; |
| 1540 | |
| 1541 | |
| 1542 | class Assignment: public Expression { |
| 1543 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1544 | Assignment(Isolate* isolate, |
| 1545 | Token::Value op, |
| 1546 | Expression* target, |
| 1547 | Expression* value, |
| 1548 | int pos); |
| 1549 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1550 | DECLARE_NODE_TYPE(Assignment) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1551 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1552 | virtual bool IsInlineable() const; |
| 1553 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1554 | Assignment* AsSimpleAssignment() { return !is_compound() ? this : NULL; } |
| 1555 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1556 | Token::Value binary_op() const; |
| 1557 | |
| 1558 | Token::Value op() const { return op_; } |
| 1559 | Expression* target() const { return target_; } |
| 1560 | Expression* value() const { return value_; } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1561 | virtual int position() const { return pos_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1562 | BinaryOperation* binary_operation() const { return binary_operation_; } |
| 1563 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1564 | // This check relies on the definition order of token in token.h. |
| 1565 | bool is_compound() const { return op() > Token::ASSIGN; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1566 | |
| 1567 | // An initialization block is a series of statments of the form |
| 1568 | // x.y.z.a = ...; x.y.z.b = ...; etc. The parser marks the beginning and |
| 1569 | // ending of these blocks to allow for optimizations of initialization |
| 1570 | // blocks. |
| 1571 | bool starts_initialization_block() { return block_start_; } |
| 1572 | bool ends_initialization_block() { return block_end_; } |
| 1573 | void mark_block_start() { block_start_ = true; } |
| 1574 | void mark_block_end() { block_end_ = true; } |
| 1575 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1576 | // Type feedback information. |
| 1577 | void RecordTypeFeedback(TypeFeedbackOracle* oracle); |
| 1578 | virtual bool IsMonomorphic() { return is_monomorphic_; } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1579 | virtual SmallMapList* GetReceiverTypes() { return &receiver_types_; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1580 | |
| 1581 | // Bailout support. |
| 1582 | int CompoundLoadId() const { return compound_load_id_; } |
| 1583 | int AssignmentId() const { return assignment_id_; } |
| 1584 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1585 | private: |
| 1586 | Token::Value op_; |
| 1587 | Expression* target_; |
| 1588 | Expression* value_; |
| 1589 | int pos_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1590 | BinaryOperation* binary_operation_; |
| 1591 | int compound_load_id_; |
| 1592 | int assignment_id_; |
| 1593 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1594 | bool block_start_; |
| 1595 | bool block_end_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1596 | |
| 1597 | bool is_monomorphic_; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1598 | SmallMapList receiver_types_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1599 | }; |
| 1600 | |
| 1601 | |
| 1602 | class Throw: public Expression { |
| 1603 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1604 | Throw(Isolate* isolate, Expression* exception, int pos) |
| 1605 | : Expression(isolate), exception_(exception), pos_(pos) {} |
| 1606 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1607 | DECLARE_NODE_TYPE(Throw) |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1608 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1609 | Expression* exception() const { return exception_; } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1610 | virtual int position() const { return pos_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1611 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1612 | |
| 1613 | private: |
| 1614 | Expression* exception_; |
| 1615 | int pos_; |
| 1616 | }; |
| 1617 | |
| 1618 | |
| 1619 | class FunctionLiteral: public Expression { |
| 1620 | public: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1621 | enum Type { |
| 1622 | ANONYMOUS_EXPRESSION, |
| 1623 | NAMED_EXPRESSION, |
| 1624 | DECLARATION |
| 1625 | }; |
| 1626 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1627 | FunctionLiteral(Isolate* isolate, |
| 1628 | Handle<String> name, |
| 1629 | Scope* scope, |
| 1630 | ZoneList<Statement*>* body, |
| 1631 | int materialized_literal_count, |
| 1632 | int expected_property_count, |
| 1633 | bool has_only_simple_this_property_assignments, |
| 1634 | Handle<FixedArray> this_property_assignments, |
| 1635 | int num_parameters, |
| 1636 | int start_position, |
| 1637 | int end_position, |
| 1638 | Type type, |
| 1639 | bool has_duplicate_parameters) |
| 1640 | : Expression(isolate), |
| 1641 | name_(name), |
| 1642 | scope_(scope), |
| 1643 | body_(body), |
| 1644 | materialized_literal_count_(materialized_literal_count), |
| 1645 | expected_property_count_(expected_property_count), |
| 1646 | has_only_simple_this_property_assignments_( |
| 1647 | has_only_simple_this_property_assignments), |
| 1648 | this_property_assignments_(this_property_assignments), |
| 1649 | num_parameters_(num_parameters), |
| 1650 | start_position_(start_position), |
| 1651 | end_position_(end_position), |
| 1652 | function_token_position_(RelocInfo::kNoPosition), |
| 1653 | inferred_name_(HEAP->empty_string()), |
| 1654 | is_expression_(type != DECLARATION), |
| 1655 | is_anonymous_(type == ANONYMOUS_EXPRESSION), |
| 1656 | pretenure_(false), |
| 1657 | has_duplicate_parameters_(has_duplicate_parameters) { |
| 1658 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1659 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1660 | DECLARE_NODE_TYPE(FunctionLiteral) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1661 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1662 | Handle<String> name() const { return name_; } |
| 1663 | Scope* scope() const { return scope_; } |
| 1664 | ZoneList<Statement*>* body() const { return body_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1665 | void set_function_token_position(int pos) { function_token_position_ = pos; } |
| 1666 | int function_token_position() const { return function_token_position_; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1667 | int start_position() const { return start_position_; } |
| 1668 | int end_position() const { return end_position_; } |
| 1669 | bool is_expression() const { return is_expression_; } |
| 1670 | bool is_anonymous() const { return is_anonymous_; } |
| 1671 | bool strict_mode() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1672 | |
| 1673 | int materialized_literal_count() { return materialized_literal_count_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1674 | int expected_property_count() { return expected_property_count_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1675 | bool has_only_simple_this_property_assignments() { |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1676 | return has_only_simple_this_property_assignments_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1677 | } |
| 1678 | Handle<FixedArray> this_property_assignments() { |
| 1679 | return this_property_assignments_; |
| 1680 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1681 | int num_parameters() { return num_parameters_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1682 | |
| 1683 | bool AllowsLazyCompilation(); |
| 1684 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1685 | Handle<String> debug_name() const { |
| 1686 | if (name_->length() > 0) return name_; |
| 1687 | return inferred_name(); |
| 1688 | } |
| 1689 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1690 | Handle<String> inferred_name() const { return inferred_name_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1691 | void set_inferred_name(Handle<String> inferred_name) { |
| 1692 | inferred_name_ = inferred_name; |
| 1693 | } |
| 1694 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1695 | bool pretenure() { return pretenure_; } |
| 1696 | void set_pretenure(bool value) { pretenure_ = value; } |
| 1697 | virtual bool IsInlineable() const; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1698 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1699 | bool has_duplicate_parameters() { return has_duplicate_parameters_; } |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 1700 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1701 | private: |
| 1702 | Handle<String> name_; |
| 1703 | Scope* scope_; |
| 1704 | ZoneList<Statement*>* body_; |
| 1705 | int materialized_literal_count_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1706 | int expected_property_count_; |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1707 | bool has_only_simple_this_property_assignments_; |
| 1708 | Handle<FixedArray> this_property_assignments_; |
| 1709 | int num_parameters_; |
| 1710 | int start_position_; |
| 1711 | int end_position_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1712 | int function_token_position_; |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1713 | Handle<String> inferred_name_; |
| 1714 | bool is_expression_; |
| 1715 | bool is_anonymous_; |
| 1716 | bool pretenure_; |
| 1717 | bool has_duplicate_parameters_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1718 | }; |
| 1719 | |
| 1720 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1721 | class SharedFunctionInfoLiteral: public Expression { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1722 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1723 | SharedFunctionInfoLiteral( |
| 1724 | Isolate* isolate, |
| 1725 | Handle<SharedFunctionInfo> shared_function_info) |
| 1726 | : Expression(isolate), shared_function_info_(shared_function_info) { } |
| 1727 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1728 | DECLARE_NODE_TYPE(SharedFunctionInfoLiteral) |
| 1729 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1730 | Handle<SharedFunctionInfo> shared_function_info() const { |
| 1731 | return shared_function_info_; |
| 1732 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1733 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1734 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1735 | private: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1736 | Handle<SharedFunctionInfo> shared_function_info_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1737 | }; |
| 1738 | |
| 1739 | |
| 1740 | class ThisFunction: public Expression { |
| 1741 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1742 | explicit ThisFunction(Isolate* isolate) : Expression(isolate) {} |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1743 | DECLARE_NODE_TYPE(ThisFunction) |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1744 | virtual bool IsInlineable() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1745 | }; |
| 1746 | |
| 1747 | |
| 1748 | // ---------------------------------------------------------------------------- |
| 1749 | // Regular expressions |
| 1750 | |
| 1751 | |
| 1752 | class RegExpVisitor BASE_EMBEDDED { |
| 1753 | public: |
| 1754 | virtual ~RegExpVisitor() { } |
| 1755 | #define MAKE_CASE(Name) \ |
| 1756 | virtual void* Visit##Name(RegExp##Name*, void* data) = 0; |
| 1757 | FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE) |
| 1758 | #undef MAKE_CASE |
| 1759 | }; |
| 1760 | |
| 1761 | |
| 1762 | class RegExpTree: public ZoneObject { |
| 1763 | public: |
| 1764 | static const int kInfinity = kMaxInt; |
| 1765 | virtual ~RegExpTree() { } |
| 1766 | virtual void* Accept(RegExpVisitor* visitor, void* data) = 0; |
| 1767 | virtual RegExpNode* ToNode(RegExpCompiler* compiler, |
| 1768 | RegExpNode* on_success) = 0; |
| 1769 | virtual bool IsTextElement() { return false; } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1770 | virtual bool IsAnchoredAtStart() { return false; } |
| 1771 | virtual bool IsAnchoredAtEnd() { return false; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1772 | virtual int min_match() = 0; |
| 1773 | virtual int max_match() = 0; |
| 1774 | // Returns the interval of registers used for captures within this |
| 1775 | // expression. |
| 1776 | virtual Interval CaptureRegisters() { return Interval::Empty(); } |
| 1777 | virtual void AppendToText(RegExpText* text); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1778 | SmartArrayPointer<const char> ToString(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1779 | #define MAKE_ASTYPE(Name) \ |
| 1780 | virtual RegExp##Name* As##Name(); \ |
| 1781 | virtual bool Is##Name(); |
| 1782 | FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ASTYPE) |
| 1783 | #undef MAKE_ASTYPE |
| 1784 | }; |
| 1785 | |
| 1786 | |
| 1787 | class RegExpDisjunction: public RegExpTree { |
| 1788 | public: |
| 1789 | explicit RegExpDisjunction(ZoneList<RegExpTree*>* alternatives); |
| 1790 | virtual void* Accept(RegExpVisitor* visitor, void* data); |
| 1791 | virtual RegExpNode* ToNode(RegExpCompiler* compiler, |
| 1792 | RegExpNode* on_success); |
| 1793 | virtual RegExpDisjunction* AsDisjunction(); |
| 1794 | virtual Interval CaptureRegisters(); |
| 1795 | virtual bool IsDisjunction(); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1796 | virtual bool IsAnchoredAtStart(); |
| 1797 | virtual bool IsAnchoredAtEnd(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1798 | virtual int min_match() { return min_match_; } |
| 1799 | virtual int max_match() { return max_match_; } |
| 1800 | ZoneList<RegExpTree*>* alternatives() { return alternatives_; } |
| 1801 | private: |
| 1802 | ZoneList<RegExpTree*>* alternatives_; |
| 1803 | int min_match_; |
| 1804 | int max_match_; |
| 1805 | }; |
| 1806 | |
| 1807 | |
| 1808 | class RegExpAlternative: public RegExpTree { |
| 1809 | public: |
| 1810 | explicit RegExpAlternative(ZoneList<RegExpTree*>* nodes); |
| 1811 | virtual void* Accept(RegExpVisitor* visitor, void* data); |
| 1812 | virtual RegExpNode* ToNode(RegExpCompiler* compiler, |
| 1813 | RegExpNode* on_success); |
| 1814 | virtual RegExpAlternative* AsAlternative(); |
| 1815 | virtual Interval CaptureRegisters(); |
| 1816 | virtual bool IsAlternative(); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1817 | virtual bool IsAnchoredAtStart(); |
| 1818 | virtual bool IsAnchoredAtEnd(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1819 | virtual int min_match() { return min_match_; } |
| 1820 | virtual int max_match() { return max_match_; } |
| 1821 | ZoneList<RegExpTree*>* nodes() { return nodes_; } |
| 1822 | private: |
| 1823 | ZoneList<RegExpTree*>* nodes_; |
| 1824 | int min_match_; |
| 1825 | int max_match_; |
| 1826 | }; |
| 1827 | |
| 1828 | |
| 1829 | class RegExpAssertion: public RegExpTree { |
| 1830 | public: |
| 1831 | enum Type { |
| 1832 | START_OF_LINE, |
| 1833 | START_OF_INPUT, |
| 1834 | END_OF_LINE, |
| 1835 | END_OF_INPUT, |
| 1836 | BOUNDARY, |
| 1837 | NON_BOUNDARY |
| 1838 | }; |
| 1839 | explicit RegExpAssertion(Type type) : type_(type) { } |
| 1840 | virtual void* Accept(RegExpVisitor* visitor, void* data); |
| 1841 | virtual RegExpNode* ToNode(RegExpCompiler* compiler, |
| 1842 | RegExpNode* on_success); |
| 1843 | virtual RegExpAssertion* AsAssertion(); |
| 1844 | virtual bool IsAssertion(); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1845 | virtual bool IsAnchoredAtStart(); |
| 1846 | virtual bool IsAnchoredAtEnd(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1847 | virtual int min_match() { return 0; } |
| 1848 | virtual int max_match() { return 0; } |
| 1849 | Type type() { return type_; } |
| 1850 | private: |
| 1851 | Type type_; |
| 1852 | }; |
| 1853 | |
| 1854 | |
| 1855 | class CharacterSet BASE_EMBEDDED { |
| 1856 | public: |
| 1857 | explicit CharacterSet(uc16 standard_set_type) |
| 1858 | : ranges_(NULL), |
| 1859 | standard_set_type_(standard_set_type) {} |
| 1860 | explicit CharacterSet(ZoneList<CharacterRange>* ranges) |
| 1861 | : ranges_(ranges), |
| 1862 | standard_set_type_(0) {} |
| 1863 | ZoneList<CharacterRange>* ranges(); |
| 1864 | uc16 standard_set_type() { return standard_set_type_; } |
| 1865 | void set_standard_set_type(uc16 special_set_type) { |
| 1866 | standard_set_type_ = special_set_type; |
| 1867 | } |
| 1868 | bool is_standard() { return standard_set_type_ != 0; } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1869 | void Canonicalize(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1870 | private: |
| 1871 | ZoneList<CharacterRange>* ranges_; |
| 1872 | // If non-zero, the value represents a standard set (e.g., all whitespace |
| 1873 | // characters) without having to expand the ranges. |
| 1874 | uc16 standard_set_type_; |
| 1875 | }; |
| 1876 | |
| 1877 | |
| 1878 | class RegExpCharacterClass: public RegExpTree { |
| 1879 | public: |
| 1880 | RegExpCharacterClass(ZoneList<CharacterRange>* ranges, bool is_negated) |
| 1881 | : set_(ranges), |
| 1882 | is_negated_(is_negated) { } |
| 1883 | explicit RegExpCharacterClass(uc16 type) |
| 1884 | : set_(type), |
| 1885 | is_negated_(false) { } |
| 1886 | virtual void* Accept(RegExpVisitor* visitor, void* data); |
| 1887 | virtual RegExpNode* ToNode(RegExpCompiler* compiler, |
| 1888 | RegExpNode* on_success); |
| 1889 | virtual RegExpCharacterClass* AsCharacterClass(); |
| 1890 | virtual bool IsCharacterClass(); |
| 1891 | virtual bool IsTextElement() { return true; } |
| 1892 | virtual int min_match() { return 1; } |
| 1893 | virtual int max_match() { return 1; } |
| 1894 | virtual void AppendToText(RegExpText* text); |
| 1895 | CharacterSet character_set() { return set_; } |
| 1896 | // TODO(lrn): Remove need for complex version if is_standard that |
| 1897 | // recognizes a mangled standard set and just do { return set_.is_special(); } |
| 1898 | bool is_standard(); |
| 1899 | // Returns a value representing the standard character set if is_standard() |
| 1900 | // returns true. |
| 1901 | // Currently used values are: |
| 1902 | // s : unicode whitespace |
| 1903 | // S : unicode non-whitespace |
| 1904 | // w : ASCII word character (digit, letter, underscore) |
| 1905 | // W : non-ASCII word character |
| 1906 | // d : ASCII digit |
| 1907 | // D : non-ASCII digit |
| 1908 | // . : non-unicode non-newline |
| 1909 | // * : All characters |
| 1910 | uc16 standard_type() { return set_.standard_set_type(); } |
| 1911 | ZoneList<CharacterRange>* ranges() { return set_.ranges(); } |
| 1912 | bool is_negated() { return is_negated_; } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1913 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1914 | private: |
| 1915 | CharacterSet set_; |
| 1916 | bool is_negated_; |
| 1917 | }; |
| 1918 | |
| 1919 | |
| 1920 | class RegExpAtom: public RegExpTree { |
| 1921 | public: |
| 1922 | explicit RegExpAtom(Vector<const uc16> data) : data_(data) { } |
| 1923 | virtual void* Accept(RegExpVisitor* visitor, void* data); |
| 1924 | virtual RegExpNode* ToNode(RegExpCompiler* compiler, |
| 1925 | RegExpNode* on_success); |
| 1926 | virtual RegExpAtom* AsAtom(); |
| 1927 | virtual bool IsAtom(); |
| 1928 | virtual bool IsTextElement() { return true; } |
| 1929 | virtual int min_match() { return data_.length(); } |
| 1930 | virtual int max_match() { return data_.length(); } |
| 1931 | virtual void AppendToText(RegExpText* text); |
| 1932 | Vector<const uc16> data() { return data_; } |
| 1933 | int length() { return data_.length(); } |
| 1934 | private: |
| 1935 | Vector<const uc16> data_; |
| 1936 | }; |
| 1937 | |
| 1938 | |
| 1939 | class RegExpText: public RegExpTree { |
| 1940 | public: |
| 1941 | RegExpText() : elements_(2), length_(0) {} |
| 1942 | virtual void* Accept(RegExpVisitor* visitor, void* data); |
| 1943 | virtual RegExpNode* ToNode(RegExpCompiler* compiler, |
| 1944 | RegExpNode* on_success); |
| 1945 | virtual RegExpText* AsText(); |
| 1946 | virtual bool IsText(); |
| 1947 | virtual bool IsTextElement() { return true; } |
| 1948 | virtual int min_match() { return length_; } |
| 1949 | virtual int max_match() { return length_; } |
| 1950 | virtual void AppendToText(RegExpText* text); |
| 1951 | void AddElement(TextElement elm) { |
| 1952 | elements_.Add(elm); |
| 1953 | length_ += elm.length(); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 1954 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1955 | ZoneList<TextElement>* elements() { return &elements_; } |
| 1956 | private: |
| 1957 | ZoneList<TextElement> elements_; |
| 1958 | int length_; |
| 1959 | }; |
| 1960 | |
| 1961 | |
| 1962 | class RegExpQuantifier: public RegExpTree { |
| 1963 | public: |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1964 | enum Type { GREEDY, NON_GREEDY, POSSESSIVE }; |
| 1965 | RegExpQuantifier(int min, int max, Type type, RegExpTree* body) |
| 1966 | : body_(body), |
| 1967 | min_(min), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1968 | max_(max), |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1969 | min_match_(min * body->min_match()), |
| 1970 | type_(type) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1971 | if (max > 0 && body->max_match() > kInfinity / max) { |
| 1972 | max_match_ = kInfinity; |
| 1973 | } else { |
| 1974 | max_match_ = max * body->max_match(); |
| 1975 | } |
| 1976 | } |
| 1977 | virtual void* Accept(RegExpVisitor* visitor, void* data); |
| 1978 | virtual RegExpNode* ToNode(RegExpCompiler* compiler, |
| 1979 | RegExpNode* on_success); |
| 1980 | static RegExpNode* ToNode(int min, |
| 1981 | int max, |
| 1982 | bool is_greedy, |
| 1983 | RegExpTree* body, |
| 1984 | RegExpCompiler* compiler, |
| 1985 | RegExpNode* on_success, |
| 1986 | bool not_at_start = false); |
| 1987 | virtual RegExpQuantifier* AsQuantifier(); |
| 1988 | virtual Interval CaptureRegisters(); |
| 1989 | virtual bool IsQuantifier(); |
| 1990 | virtual int min_match() { return min_match_; } |
| 1991 | virtual int max_match() { return max_match_; } |
| 1992 | int min() { return min_; } |
| 1993 | int max() { return max_; } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1994 | bool is_possessive() { return type_ == POSSESSIVE; } |
| 1995 | bool is_non_greedy() { return type_ == NON_GREEDY; } |
| 1996 | bool is_greedy() { return type_ == GREEDY; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1997 | RegExpTree* body() { return body_; } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1998 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1999 | private: |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2000 | RegExpTree* body_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2001 | int min_; |
| 2002 | int max_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2003 | int min_match_; |
| 2004 | int max_match_; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2005 | Type type_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2006 | }; |
| 2007 | |
| 2008 | |
| 2009 | class RegExpCapture: public RegExpTree { |
| 2010 | public: |
| 2011 | explicit RegExpCapture(RegExpTree* body, int index) |
| 2012 | : body_(body), index_(index) { } |
| 2013 | virtual void* Accept(RegExpVisitor* visitor, void* data); |
| 2014 | virtual RegExpNode* ToNode(RegExpCompiler* compiler, |
| 2015 | RegExpNode* on_success); |
| 2016 | static RegExpNode* ToNode(RegExpTree* body, |
| 2017 | int index, |
| 2018 | RegExpCompiler* compiler, |
| 2019 | RegExpNode* on_success); |
| 2020 | virtual RegExpCapture* AsCapture(); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 2021 | virtual bool IsAnchoredAtStart(); |
| 2022 | virtual bool IsAnchoredAtEnd(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2023 | virtual Interval CaptureRegisters(); |
| 2024 | virtual bool IsCapture(); |
| 2025 | virtual int min_match() { return body_->min_match(); } |
| 2026 | virtual int max_match() { return body_->max_match(); } |
| 2027 | RegExpTree* body() { return body_; } |
| 2028 | int index() { return index_; } |
| 2029 | static int StartRegister(int index) { return index * 2; } |
| 2030 | static int EndRegister(int index) { return index * 2 + 1; } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2031 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2032 | private: |
| 2033 | RegExpTree* body_; |
| 2034 | int index_; |
| 2035 | }; |
| 2036 | |
| 2037 | |
| 2038 | class RegExpLookahead: public RegExpTree { |
| 2039 | public: |
| 2040 | RegExpLookahead(RegExpTree* body, |
| 2041 | bool is_positive, |
| 2042 | int capture_count, |
| 2043 | int capture_from) |
| 2044 | : body_(body), |
| 2045 | is_positive_(is_positive), |
| 2046 | capture_count_(capture_count), |
| 2047 | capture_from_(capture_from) { } |
| 2048 | |
| 2049 | virtual void* Accept(RegExpVisitor* visitor, void* data); |
| 2050 | virtual RegExpNode* ToNode(RegExpCompiler* compiler, |
| 2051 | RegExpNode* on_success); |
| 2052 | virtual RegExpLookahead* AsLookahead(); |
| 2053 | virtual Interval CaptureRegisters(); |
| 2054 | virtual bool IsLookahead(); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 2055 | virtual bool IsAnchoredAtStart(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2056 | virtual int min_match() { return 0; } |
| 2057 | virtual int max_match() { return 0; } |
| 2058 | RegExpTree* body() { return body_; } |
| 2059 | bool is_positive() { return is_positive_; } |
| 2060 | int capture_count() { return capture_count_; } |
| 2061 | int capture_from() { return capture_from_; } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2062 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2063 | private: |
| 2064 | RegExpTree* body_; |
| 2065 | bool is_positive_; |
| 2066 | int capture_count_; |
| 2067 | int capture_from_; |
| 2068 | }; |
| 2069 | |
| 2070 | |
| 2071 | class RegExpBackReference: public RegExpTree { |
| 2072 | public: |
| 2073 | explicit RegExpBackReference(RegExpCapture* capture) |
| 2074 | : capture_(capture) { } |
| 2075 | virtual void* Accept(RegExpVisitor* visitor, void* data); |
| 2076 | virtual RegExpNode* ToNode(RegExpCompiler* compiler, |
| 2077 | RegExpNode* on_success); |
| 2078 | virtual RegExpBackReference* AsBackReference(); |
| 2079 | virtual bool IsBackReference(); |
| 2080 | virtual int min_match() { return 0; } |
| 2081 | virtual int max_match() { return capture_->max_match(); } |
| 2082 | int index() { return capture_->index(); } |
| 2083 | RegExpCapture* capture() { return capture_; } |
| 2084 | private: |
| 2085 | RegExpCapture* capture_; |
| 2086 | }; |
| 2087 | |
| 2088 | |
| 2089 | class RegExpEmpty: public RegExpTree { |
| 2090 | public: |
| 2091 | RegExpEmpty() { } |
| 2092 | virtual void* Accept(RegExpVisitor* visitor, void* data); |
| 2093 | virtual RegExpNode* ToNode(RegExpCompiler* compiler, |
| 2094 | RegExpNode* on_success); |
| 2095 | virtual RegExpEmpty* AsEmpty(); |
| 2096 | virtual bool IsEmpty(); |
| 2097 | virtual int min_match() { return 0; } |
| 2098 | virtual int max_match() { return 0; } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 2099 | static RegExpEmpty* GetInstance() { return &kInstance; } |
| 2100 | private: |
| 2101 | static RegExpEmpty kInstance; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2102 | }; |
| 2103 | |
| 2104 | |
| 2105 | // ---------------------------------------------------------------------------- |
| 2106 | // Basic visitor |
| 2107 | // - leaf node visitors are abstract. |
| 2108 | |
| 2109 | class AstVisitor BASE_EMBEDDED { |
| 2110 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2111 | AstVisitor() : isolate_(Isolate::Current()), stack_overflow_(false) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2112 | virtual ~AstVisitor() { } |
| 2113 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2114 | // Stack overflow check and dynamic dispatch. |
| 2115 | void Visit(AstNode* node) { if (!CheckStackOverflow()) node->Accept(this); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2116 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2117 | // Iteration left-to-right. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2118 | virtual void VisitDeclarations(ZoneList<Declaration*>* declarations); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2119 | virtual void VisitStatements(ZoneList<Statement*>* statements); |
| 2120 | virtual void VisitExpressions(ZoneList<Expression*>* expressions); |
| 2121 | |
| 2122 | // Stack overflow tracking support. |
| 2123 | bool HasStackOverflow() const { return stack_overflow_; } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2124 | bool CheckStackOverflow(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2125 | |
| 2126 | // If a stack-overflow exception is encountered when visiting a |
| 2127 | // node, calling SetStackOverflow will make sure that the visitor |
| 2128 | // bails out without visiting more nodes. |
| 2129 | void SetStackOverflow() { stack_overflow_ = true; } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2130 | void ClearStackOverflow() { stack_overflow_ = false; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2131 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2132 | // Individual AST nodes. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2133 | #define DEF_VISIT(type) \ |
| 2134 | virtual void Visit##type(type* node) = 0; |
| 2135 | AST_NODE_LIST(DEF_VISIT) |
| 2136 | #undef DEF_VISIT |
| 2137 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2138 | protected: |
| 2139 | Isolate* isolate() { return isolate_; } |
| 2140 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2141 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2142 | Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2143 | bool stack_overflow_; |
| 2144 | }; |
| 2145 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2146 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2147 | } } // namespace v8::internal |
| 2148 | |
| 2149 | #endif // V8_AST_H_ |