Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame^] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 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 | #include "v8.h" |
| 29 | |
| 30 | #include "ast.h" |
| 31 | #include "scopes.h" |
| 32 | #include "string-stream.h" |
| 33 | |
| 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
| 37 | |
| 38 | VariableProxySentinel VariableProxySentinel::this_proxy_(true); |
| 39 | VariableProxySentinel VariableProxySentinel::identifier_proxy_(false); |
| 40 | ValidLeftHandSideSentinel ValidLeftHandSideSentinel::instance_; |
| 41 | Property Property::this_property_(VariableProxySentinel::this_proxy(), NULL, 0); |
| 42 | Call Call::sentinel_(NULL, NULL, 0); |
| 43 | |
| 44 | |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | // All the Accept member functions for each syntax tree node type. |
| 47 | |
| 48 | #define DECL_ACCEPT(type) \ |
| 49 | void type::Accept(AstVisitor* v) { \ |
| 50 | if (v->CheckStackOverflow()) return; \ |
| 51 | v->Visit##type(this); \ |
| 52 | } |
| 53 | AST_NODE_LIST(DECL_ACCEPT) |
| 54 | #undef DECL_ACCEPT |
| 55 | |
| 56 | |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | // Implementation of other node functionality. |
| 59 | |
| 60 | VariableProxy::VariableProxy(Handle<String> name, |
| 61 | bool is_this, |
| 62 | bool inside_with) |
| 63 | : name_(name), |
| 64 | var_(NULL), |
| 65 | is_this_(is_this), |
| 66 | inside_with_(inside_with) { |
| 67 | // names must be canonicalized for fast equality checks |
| 68 | ASSERT(name->IsSymbol()); |
| 69 | // at least one access, otherwise no need for a VariableProxy |
| 70 | var_uses_.RecordRead(1); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | VariableProxy::VariableProxy(bool is_this) |
| 75 | : is_this_(is_this) { |
| 76 | } |
| 77 | |
| 78 | |
| 79 | void VariableProxy::BindTo(Variable* var) { |
| 80 | ASSERT(var_ == NULL); // must be bound only once |
| 81 | ASSERT(var != NULL); // must bind |
| 82 | ASSERT((is_this() && var->is_this()) || name_.is_identical_to(var->name())); |
| 83 | // Ideally CONST-ness should match. However, this is very hard to achieve |
| 84 | // because we don't know the exact semantics of conflicting (const and |
| 85 | // non-const) multiple variable declarations, const vars introduced via |
| 86 | // eval() etc. Const-ness and variable declarations are a complete mess |
| 87 | // in JS. Sigh... |
| 88 | var_ = var; |
| 89 | var->var_uses()->RecordUses(&var_uses_); |
| 90 | var->obj_uses()->RecordUses(&obj_uses_); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | #ifdef DEBUG |
| 95 | |
| 96 | const char* LoopStatement::OperatorString() const { |
| 97 | switch (type()) { |
| 98 | case DO_LOOP: return "DO"; |
| 99 | case FOR_LOOP: return "FOR"; |
| 100 | case WHILE_LOOP: return "WHILE"; |
| 101 | } |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | #endif // DEBUG |
| 106 | |
| 107 | |
| 108 | Token::Value Assignment::binary_op() const { |
| 109 | switch (op_) { |
| 110 | case Token::ASSIGN_BIT_OR: return Token::BIT_OR; |
| 111 | case Token::ASSIGN_BIT_XOR: return Token::BIT_XOR; |
| 112 | case Token::ASSIGN_BIT_AND: return Token::BIT_AND; |
| 113 | case Token::ASSIGN_SHL: return Token::SHL; |
| 114 | case Token::ASSIGN_SAR: return Token::SAR; |
| 115 | case Token::ASSIGN_SHR: return Token::SHR; |
| 116 | case Token::ASSIGN_ADD: return Token::ADD; |
| 117 | case Token::ASSIGN_SUB: return Token::SUB; |
| 118 | case Token::ASSIGN_MUL: return Token::MUL; |
| 119 | case Token::ASSIGN_DIV: return Token::DIV; |
| 120 | case Token::ASSIGN_MOD: return Token::MOD; |
| 121 | default: UNREACHABLE(); |
| 122 | } |
| 123 | return Token::ILLEGAL; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | bool FunctionLiteral::AllowsLazyCompilation() { |
| 128 | return scope()->AllowsLazyCompilation(); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | ObjectLiteral::Property::Property(Literal* key, Expression* value) { |
| 133 | key_ = key; |
| 134 | value_ = value; |
| 135 | Object* k = *key->handle(); |
| 136 | if (k->IsSymbol() && Heap::Proto_symbol()->Equals(String::cast(k))) { |
| 137 | kind_ = PROTOTYPE; |
| 138 | } else if (value_->AsMaterializedLiteral() != NULL) { |
| 139 | kind_ = MATERIALIZED_LITERAL; |
| 140 | } else if (value_->AsLiteral() != NULL) { |
| 141 | kind_ = CONSTANT; |
| 142 | } else { |
| 143 | kind_ = COMPUTED; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | |
| 148 | ObjectLiteral::Property::Property(bool is_getter, FunctionLiteral* value) { |
| 149 | key_ = new Literal(value->name()); |
| 150 | value_ = value; |
| 151 | kind_ = is_getter ? GETTER : SETTER; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | bool ObjectLiteral::IsValidJSON() { |
| 156 | int length = properties()->length(); |
| 157 | for (int i = 0; i < length; i++) { |
| 158 | Property* prop = properties()->at(i); |
| 159 | if (!prop->value()->IsValidJSON()) |
| 160 | return false; |
| 161 | } |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | bool ArrayLiteral::IsValidJSON() { |
| 167 | int length = values()->length(); |
| 168 | for (int i = 0; i < length; i++) { |
| 169 | if (!values()->at(i)->IsValidJSON()) |
| 170 | return false; |
| 171 | } |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | void TargetCollector::AddTarget(BreakTarget* target) { |
| 177 | // Add the label to the collector, but discard duplicates. |
| 178 | int length = targets_->length(); |
| 179 | for (int i = 0; i < length; i++) { |
| 180 | if (targets_->at(i) == target) return; |
| 181 | } |
| 182 | targets_->Add(target); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | // ---------------------------------------------------------------------------- |
| 187 | // Implementation of AstVisitor |
| 188 | |
| 189 | |
| 190 | void AstVisitor::VisitStatements(ZoneList<Statement*>* statements) { |
| 191 | for (int i = 0; i < statements->length(); i++) { |
| 192 | Visit(statements->at(i)); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | |
| 197 | void AstVisitor::VisitExpressions(ZoneList<Expression*>* expressions) { |
| 198 | for (int i = 0; i < expressions->length(); i++) { |
| 199 | // The variable statement visiting code may pass NULL expressions |
| 200 | // to this code. Maybe this should be handled by introducing an |
| 201 | // undefined expression or literal? Revisit this code if this |
| 202 | // changes |
| 203 | Expression* expression = expressions->at(i); |
| 204 | if (expression != NULL) Visit(expression); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | |
| 209 | // ---------------------------------------------------------------------------- |
| 210 | // Regular expressions |
| 211 | |
| 212 | #define MAKE_ACCEPT(Name) \ |
| 213 | void* RegExp##Name::Accept(RegExpVisitor* visitor, void* data) { \ |
| 214 | return visitor->Visit##Name(this, data); \ |
| 215 | } |
| 216 | FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ACCEPT) |
| 217 | #undef MAKE_ACCEPT |
| 218 | |
| 219 | #define MAKE_TYPE_CASE(Name) \ |
| 220 | RegExp##Name* RegExpTree::As##Name() { \ |
| 221 | return NULL; \ |
| 222 | } \ |
| 223 | bool RegExpTree::Is##Name() { return false; } |
| 224 | FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE) |
| 225 | #undef MAKE_TYPE_CASE |
| 226 | |
| 227 | #define MAKE_TYPE_CASE(Name) \ |
| 228 | RegExp##Name* RegExp##Name::As##Name() { \ |
| 229 | return this; \ |
| 230 | } \ |
| 231 | bool RegExp##Name::Is##Name() { return true; } |
| 232 | FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE) |
| 233 | #undef MAKE_TYPE_CASE |
| 234 | |
| 235 | RegExpEmpty RegExpEmpty::kInstance; |
| 236 | |
| 237 | |
| 238 | static Interval ListCaptureRegisters(ZoneList<RegExpTree*>* children) { |
| 239 | Interval result = Interval::Empty(); |
| 240 | for (int i = 0; i < children->length(); i++) |
| 241 | result = result.Union(children->at(i)->CaptureRegisters()); |
| 242 | return result; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | Interval RegExpAlternative::CaptureRegisters() { |
| 247 | return ListCaptureRegisters(nodes()); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | Interval RegExpDisjunction::CaptureRegisters() { |
| 252 | return ListCaptureRegisters(alternatives()); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | Interval RegExpLookahead::CaptureRegisters() { |
| 257 | return body()->CaptureRegisters(); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | Interval RegExpCapture::CaptureRegisters() { |
| 262 | Interval self(StartRegister(index()), EndRegister(index())); |
| 263 | return self.Union(body()->CaptureRegisters()); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | Interval RegExpQuantifier::CaptureRegisters() { |
| 268 | return body()->CaptureRegisters(); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | bool RegExpAssertion::IsAnchored() { |
| 273 | return type() == RegExpAssertion::START_OF_INPUT; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | bool RegExpAlternative::IsAnchored() { |
| 278 | ZoneList<RegExpTree*>* nodes = this->nodes(); |
| 279 | for (int i = 0; i < nodes->length(); i++) { |
| 280 | RegExpTree* node = nodes->at(i); |
| 281 | if (node->IsAnchored()) { return true; } |
| 282 | if (node->max_match() > 0) { return false; } |
| 283 | } |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | bool RegExpDisjunction::IsAnchored() { |
| 289 | ZoneList<RegExpTree*>* alternatives = this->alternatives(); |
| 290 | for (int i = 0; i < alternatives->length(); i++) { |
| 291 | if (!alternatives->at(i)->IsAnchored()) |
| 292 | return false; |
| 293 | } |
| 294 | return true; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | bool RegExpLookahead::IsAnchored() { |
| 299 | return is_positive() && body()->IsAnchored(); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | bool RegExpCapture::IsAnchored() { |
| 304 | return body()->IsAnchored(); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | // Convert regular expression trees to a simple sexp representation. |
| 309 | // This representation should be different from the input grammar |
| 310 | // in as many cases as possible, to make it more difficult for incorrect |
| 311 | // parses to look as correct ones which is likely if the input and |
| 312 | // output formats are alike. |
| 313 | class RegExpUnparser: public RegExpVisitor { |
| 314 | public: |
| 315 | RegExpUnparser(); |
| 316 | void VisitCharacterRange(CharacterRange that); |
| 317 | SmartPointer<const char> ToString() { return stream_.ToCString(); } |
| 318 | #define MAKE_CASE(Name) virtual void* Visit##Name(RegExp##Name*, void* data); |
| 319 | FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE) |
| 320 | #undef MAKE_CASE |
| 321 | private: |
| 322 | StringStream* stream() { return &stream_; } |
| 323 | HeapStringAllocator alloc_; |
| 324 | StringStream stream_; |
| 325 | }; |
| 326 | |
| 327 | |
| 328 | RegExpUnparser::RegExpUnparser() : stream_(&alloc_) { |
| 329 | } |
| 330 | |
| 331 | |
| 332 | void* RegExpUnparser::VisitDisjunction(RegExpDisjunction* that, void* data) { |
| 333 | stream()->Add("(|"); |
| 334 | for (int i = 0; i < that->alternatives()->length(); i++) { |
| 335 | stream()->Add(" "); |
| 336 | that->alternatives()->at(i)->Accept(this, data); |
| 337 | } |
| 338 | stream()->Add(")"); |
| 339 | return NULL; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | void* RegExpUnparser::VisitAlternative(RegExpAlternative* that, void* data) { |
| 344 | stream()->Add("(:"); |
| 345 | for (int i = 0; i < that->nodes()->length(); i++) { |
| 346 | stream()->Add(" "); |
| 347 | that->nodes()->at(i)->Accept(this, data); |
| 348 | } |
| 349 | stream()->Add(")"); |
| 350 | return NULL; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | void RegExpUnparser::VisitCharacterRange(CharacterRange that) { |
| 355 | stream()->Add("%k", that.from()); |
| 356 | if (!that.IsSingleton()) { |
| 357 | stream()->Add("-%k", that.to()); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | |
| 362 | |
| 363 | void* RegExpUnparser::VisitCharacterClass(RegExpCharacterClass* that, |
| 364 | void* data) { |
| 365 | if (that->is_negated()) |
| 366 | stream()->Add("^"); |
| 367 | stream()->Add("["); |
| 368 | for (int i = 0; i < that->ranges()->length(); i++) { |
| 369 | if (i > 0) stream()->Add(" "); |
| 370 | VisitCharacterRange(that->ranges()->at(i)); |
| 371 | } |
| 372 | stream()->Add("]"); |
| 373 | return NULL; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | void* RegExpUnparser::VisitAssertion(RegExpAssertion* that, void* data) { |
| 378 | switch (that->type()) { |
| 379 | case RegExpAssertion::START_OF_INPUT: |
| 380 | stream()->Add("@^i"); |
| 381 | break; |
| 382 | case RegExpAssertion::END_OF_INPUT: |
| 383 | stream()->Add("@$i"); |
| 384 | break; |
| 385 | case RegExpAssertion::START_OF_LINE: |
| 386 | stream()->Add("@^l"); |
| 387 | break; |
| 388 | case RegExpAssertion::END_OF_LINE: |
| 389 | stream()->Add("@$l"); |
| 390 | break; |
| 391 | case RegExpAssertion::BOUNDARY: |
| 392 | stream()->Add("@b"); |
| 393 | break; |
| 394 | case RegExpAssertion::NON_BOUNDARY: |
| 395 | stream()->Add("@B"); |
| 396 | break; |
| 397 | } |
| 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | void* RegExpUnparser::VisitAtom(RegExpAtom* that, void* data) { |
| 403 | stream()->Add("'"); |
| 404 | Vector<const uc16> chardata = that->data(); |
| 405 | for (int i = 0; i < chardata.length(); i++) { |
| 406 | stream()->Add("%k", chardata[i]); |
| 407 | } |
| 408 | stream()->Add("'"); |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | void* RegExpUnparser::VisitText(RegExpText* that, void* data) { |
| 414 | if (that->elements()->length() == 1) { |
| 415 | that->elements()->at(0).data.u_atom->Accept(this, data); |
| 416 | } else { |
| 417 | stream()->Add("(!"); |
| 418 | for (int i = 0; i < that->elements()->length(); i++) { |
| 419 | stream()->Add(" "); |
| 420 | that->elements()->at(i).data.u_atom->Accept(this, data); |
| 421 | } |
| 422 | stream()->Add(")"); |
| 423 | } |
| 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | |
| 428 | void* RegExpUnparser::VisitQuantifier(RegExpQuantifier* that, void* data) { |
| 429 | stream()->Add("(# %i ", that->min()); |
| 430 | if (that->max() == RegExpTree::kInfinity) { |
| 431 | stream()->Add("- "); |
| 432 | } else { |
| 433 | stream()->Add("%i ", that->max()); |
| 434 | } |
| 435 | stream()->Add(that->is_greedy() ? "g " : "n "); |
| 436 | that->body()->Accept(this, data); |
| 437 | stream()->Add(")"); |
| 438 | return NULL; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | void* RegExpUnparser::VisitCapture(RegExpCapture* that, void* data) { |
| 443 | stream()->Add("(^ "); |
| 444 | that->body()->Accept(this, data); |
| 445 | stream()->Add(")"); |
| 446 | return NULL; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | void* RegExpUnparser::VisitLookahead(RegExpLookahead* that, void* data) { |
| 451 | stream()->Add("(-> "); |
| 452 | stream()->Add(that->is_positive() ? "+ " : "- "); |
| 453 | that->body()->Accept(this, data); |
| 454 | stream()->Add(")"); |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | |
| 459 | void* RegExpUnparser::VisitBackReference(RegExpBackReference* that, |
| 460 | void* data) { |
| 461 | stream()->Add("(<- %i)", that->index()); |
| 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | void* RegExpUnparser::VisitEmpty(RegExpEmpty* that, void* data) { |
| 467 | stream()->Put('%'); |
| 468 | return NULL; |
| 469 | } |
| 470 | |
| 471 | |
| 472 | SmartPointer<const char> RegExpTree::ToString() { |
| 473 | RegExpUnparser unparser; |
| 474 | Accept(&unparser, NULL); |
| 475 | return unparser.ToString(); |
| 476 | } |
| 477 | |
| 478 | |
| 479 | RegExpDisjunction::RegExpDisjunction(ZoneList<RegExpTree*>* alternatives) |
| 480 | : alternatives_(alternatives) { |
| 481 | ASSERT(alternatives->length() > 1); |
| 482 | RegExpTree* first_alternative = alternatives->at(0); |
| 483 | min_match_ = first_alternative->min_match(); |
| 484 | max_match_ = first_alternative->max_match(); |
| 485 | for (int i = 1; i < alternatives->length(); i++) { |
| 486 | RegExpTree* alternative = alternatives->at(i); |
| 487 | min_match_ = Min(min_match_, alternative->min_match()); |
| 488 | max_match_ = Max(max_match_, alternative->max_match()); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | |
| 493 | RegExpAlternative::RegExpAlternative(ZoneList<RegExpTree*>* nodes) |
| 494 | : nodes_(nodes) { |
| 495 | ASSERT(nodes->length() > 1); |
| 496 | min_match_ = 0; |
| 497 | max_match_ = 0; |
| 498 | for (int i = 0; i < nodes->length(); i++) { |
| 499 | RegExpTree* node = nodes->at(i); |
| 500 | min_match_ += node->min_match(); |
| 501 | int node_max_match = node->max_match(); |
| 502 | if (kInfinity - max_match_ < node_max_match) { |
| 503 | max_match_ = kInfinity; |
| 504 | } else { |
| 505 | max_match_ += node->max_match(); |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | |
| 511 | } } // namespace v8::internal |