2008-09-09 20:08:45 +00:00
|
|
|
// Copyright 2006-2008 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "ast.h"
|
2009-10-28 13:25:40 +00:00
|
|
|
#include "parser.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "scopes.h"
|
2008-11-25 11:07:48 +00:00
|
|
|
#include "string-stream.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
VariableProxySentinel VariableProxySentinel::this_proxy_(true);
|
|
|
|
VariableProxySentinel VariableProxySentinel::identifier_proxy_(false);
|
|
|
|
ValidLeftHandSideSentinel ValidLeftHandSideSentinel::instance_;
|
|
|
|
Property Property::this_property_(VariableProxySentinel::this_proxy(), NULL, 0);
|
2008-11-27 13:55:06 +00:00
|
|
|
Call Call::sentinel_(NULL, NULL, 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// All the Accept member functions for each syntax tree node type.
|
|
|
|
|
|
|
|
#define DECL_ACCEPT(type) \
|
2008-11-27 13:55:06 +00:00
|
|
|
void type::Accept(AstVisitor* v) { \
|
2008-07-03 15:10:15 +00:00
|
|
|
if (v->CheckStackOverflow()) return; \
|
|
|
|
v->Visit##type(this); \
|
|
|
|
}
|
2009-07-30 12:09:05 +00:00
|
|
|
AST_NODE_LIST(DECL_ACCEPT)
|
2008-07-03 15:10:15 +00:00
|
|
|
#undef DECL_ACCEPT
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Implementation of other node functionality.
|
|
|
|
|
2010-03-11 10:28:40 +00:00
|
|
|
Assignment* ExpressionStatement::StatementAsSimpleAssignment() {
|
|
|
|
return (expression()->AsAssignment() != NULL &&
|
|
|
|
!expression()->AsAssignment()->is_compound())
|
|
|
|
? expression()->AsAssignment()
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CountOperation* ExpressionStatement::StatementAsCountOperation() {
|
|
|
|
return expression()->AsCountOperation();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
VariableProxy::VariableProxy(Handle<String> name,
|
|
|
|
bool is_this,
|
|
|
|
bool inside_with)
|
|
|
|
: name_(name),
|
|
|
|
var_(NULL),
|
|
|
|
is_this_(is_this),
|
2010-03-11 10:28:40 +00:00
|
|
|
inside_with_(inside_with),
|
2010-03-16 10:54:02 +00:00
|
|
|
is_trivial_(false),
|
|
|
|
reaching_definitions_(NULL) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// names must be canonicalized for fast equality checks
|
|
|
|
ASSERT(name->IsSymbol());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VariableProxy::VariableProxy(bool is_this)
|
2010-03-16 10:54:02 +00:00
|
|
|
: is_this_(is_this),
|
|
|
|
reaching_definitions_(NULL) {
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VariableProxy::BindTo(Variable* var) {
|
|
|
|
ASSERT(var_ == NULL); // must be bound only once
|
|
|
|
ASSERT(var != NULL); // must bind
|
|
|
|
ASSERT((is_this() && var->is_this()) || name_.is_identical_to(var->name()));
|
|
|
|
// Ideally CONST-ness should match. However, this is very hard to achieve
|
|
|
|
// because we don't know the exact semantics of conflicting (const and
|
|
|
|
// non-const) multiple variable declarations, const vars introduced via
|
|
|
|
// eval() etc. Const-ness and variable declarations are a complete mess
|
|
|
|
// in JS. Sigh...
|
|
|
|
var_ = var;
|
2010-03-08 13:01:24 +00:00
|
|
|
var->set_is_used(true);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Token::Value Assignment::binary_op() const {
|
|
|
|
switch (op_) {
|
|
|
|
case Token::ASSIGN_BIT_OR: return Token::BIT_OR;
|
|
|
|
case Token::ASSIGN_BIT_XOR: return Token::BIT_XOR;
|
|
|
|
case Token::ASSIGN_BIT_AND: return Token::BIT_AND;
|
|
|
|
case Token::ASSIGN_SHL: return Token::SHL;
|
|
|
|
case Token::ASSIGN_SAR: return Token::SAR;
|
|
|
|
case Token::ASSIGN_SHR: return Token::SHR;
|
|
|
|
case Token::ASSIGN_ADD: return Token::ADD;
|
|
|
|
case Token::ASSIGN_SUB: return Token::SUB;
|
|
|
|
case Token::ASSIGN_MUL: return Token::MUL;
|
|
|
|
case Token::ASSIGN_DIV: return Token::DIV;
|
|
|
|
case Token::ASSIGN_MOD: return Token::MOD;
|
|
|
|
default: UNREACHABLE();
|
|
|
|
}
|
|
|
|
return Token::ILLEGAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FunctionLiteral::AllowsLazyCompilation() {
|
|
|
|
return scope()->AllowsLazyCompilation();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ObjectLiteral::Property::Property(Literal* key, Expression* value) {
|
|
|
|
key_ = key;
|
|
|
|
value_ = value;
|
|
|
|
Object* k = *key->handle();
|
|
|
|
if (k->IsSymbol() && Heap::Proto_symbol()->Equals(String::cast(k))) {
|
|
|
|
kind_ = PROTOTYPE;
|
2009-03-23 07:27:47 +00:00
|
|
|
} else if (value_->AsMaterializedLiteral() != NULL) {
|
|
|
|
kind_ = MATERIALIZED_LITERAL;
|
|
|
|
} else if (value_->AsLiteral() != NULL) {
|
|
|
|
kind_ = CONSTANT;
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2009-03-23 07:27:47 +00:00
|
|
|
kind_ = COMPUTED;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ObjectLiteral::Property::Property(bool is_getter, FunctionLiteral* value) {
|
|
|
|
key_ = new Literal(value->name());
|
|
|
|
value_ = value;
|
|
|
|
kind_ = is_getter ? GETTER : SETTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-28 13:25:40 +00:00
|
|
|
bool ObjectLiteral::Property::IsCompileTimeValue() {
|
|
|
|
return kind_ == CONSTANT ||
|
|
|
|
(kind_ == MATERIALIZED_LITERAL &&
|
|
|
|
CompileTimeValue::IsCompileTimeValue(value_));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-10 12:11:56 +00:00
|
|
|
void TargetCollector::AddTarget(BreakTarget* target) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Add the label to the collector, but discard duplicates.
|
2009-02-27 13:00:32 +00:00
|
|
|
int length = targets_->length();
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
2009-02-27 13:00:32 +00:00
|
|
|
if (targets_->at(i) == target) return;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2009-02-27 13:00:32 +00:00
|
|
|
targets_->Add(target);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2008-11-27 13:55:06 +00:00
|
|
|
// Implementation of AstVisitor
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2009-10-12 15:06:28 +00:00
|
|
|
void AstVisitor::VisitDeclarations(ZoneList<Declaration*>* declarations) {
|
|
|
|
for (int i = 0; i < declarations->length(); i++) {
|
|
|
|
Visit(declarations->at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-27 13:55:06 +00:00
|
|
|
void AstVisitor::VisitStatements(ZoneList<Statement*>* statements) {
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < statements->length(); i++) {
|
|
|
|
Visit(statements->at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-27 13:55:06 +00:00
|
|
|
void AstVisitor::VisitExpressions(ZoneList<Expression*>* expressions) {
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < expressions->length(); i++) {
|
|
|
|
// The variable statement visiting code may pass NULL expressions
|
|
|
|
// to this code. Maybe this should be handled by introducing an
|
|
|
|
// undefined expression or literal? Revisit this code if this
|
|
|
|
// changes
|
|
|
|
Expression* expression = expressions->at(i);
|
|
|
|
if (expression != NULL) Visit(expression);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Regular expressions
|
|
|
|
|
|
|
|
#define MAKE_ACCEPT(Name) \
|
|
|
|
void* RegExp##Name::Accept(RegExpVisitor* visitor, void* data) { \
|
|
|
|
return visitor->Visit##Name(this, data); \
|
|
|
|
}
|
|
|
|
FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ACCEPT)
|
|
|
|
#undef MAKE_ACCEPT
|
|
|
|
|
|
|
|
#define MAKE_TYPE_CASE(Name) \
|
|
|
|
RegExp##Name* RegExpTree::As##Name() { \
|
|
|
|
return NULL; \
|
|
|
|
} \
|
|
|
|
bool RegExpTree::Is##Name() { return false; }
|
2008-11-26 11:29:26 +00:00
|
|
|
FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE)
|
2008-11-25 11:07:48 +00:00
|
|
|
#undef MAKE_TYPE_CASE
|
|
|
|
|
|
|
|
#define MAKE_TYPE_CASE(Name) \
|
|
|
|
RegExp##Name* RegExp##Name::As##Name() { \
|
|
|
|
return this; \
|
|
|
|
} \
|
|
|
|
bool RegExp##Name::Is##Name() { return true; }
|
|
|
|
FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE)
|
|
|
|
#undef MAKE_TYPE_CASE
|
|
|
|
|
|
|
|
RegExpEmpty RegExpEmpty::kInstance;
|
|
|
|
|
|
|
|
|
2009-01-14 11:32:23 +00:00
|
|
|
static Interval ListCaptureRegisters(ZoneList<RegExpTree*>* children) {
|
|
|
|
Interval result = Interval::Empty();
|
|
|
|
for (int i = 0; i < children->length(); i++)
|
|
|
|
result = result.Union(children->at(i)->CaptureRegisters());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Interval RegExpAlternative::CaptureRegisters() {
|
|
|
|
return ListCaptureRegisters(nodes());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Interval RegExpDisjunction::CaptureRegisters() {
|
|
|
|
return ListCaptureRegisters(alternatives());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Interval RegExpLookahead::CaptureRegisters() {
|
|
|
|
return body()->CaptureRegisters();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Interval RegExpCapture::CaptureRegisters() {
|
|
|
|
Interval self(StartRegister(index()), EndRegister(index()));
|
|
|
|
return self.Union(body()->CaptureRegisters());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Interval RegExpQuantifier::CaptureRegisters() {
|
|
|
|
return body()->CaptureRegisters();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-23 07:46:44 +00:00
|
|
|
bool RegExpAssertion::IsAnchored() {
|
|
|
|
return type() == RegExpAssertion::START_OF_INPUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RegExpAlternative::IsAnchored() {
|
2009-02-03 11:43:55 +00:00
|
|
|
ZoneList<RegExpTree*>* nodes = this->nodes();
|
|
|
|
for (int i = 0; i < nodes->length(); i++) {
|
|
|
|
RegExpTree* node = nodes->at(i);
|
|
|
|
if (node->IsAnchored()) { return true; }
|
|
|
|
if (node->max_match() > 0) { return false; }
|
|
|
|
}
|
|
|
|
return false;
|
2009-01-23 07:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RegExpDisjunction::IsAnchored() {
|
|
|
|
ZoneList<RegExpTree*>* alternatives = this->alternatives();
|
|
|
|
for (int i = 0; i < alternatives->length(); i++) {
|
|
|
|
if (!alternatives->at(i)->IsAnchored())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RegExpLookahead::IsAnchored() {
|
|
|
|
return is_positive() && body()->IsAnchored();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RegExpCapture::IsAnchored() {
|
|
|
|
return body()->IsAnchored();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
// Convert regular expression trees to a simple sexp representation.
|
|
|
|
// This representation should be different from the input grammar
|
|
|
|
// in as many cases as possible, to make it more difficult for incorrect
|
|
|
|
// parses to look as correct ones which is likely if the input and
|
|
|
|
// output formats are alike.
|
|
|
|
class RegExpUnparser: public RegExpVisitor {
|
|
|
|
public:
|
|
|
|
RegExpUnparser();
|
|
|
|
void VisitCharacterRange(CharacterRange that);
|
|
|
|
SmartPointer<const char> ToString() { return stream_.ToCString(); }
|
|
|
|
#define MAKE_CASE(Name) virtual void* Visit##Name(RegExp##Name*, void* data);
|
|
|
|
FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CASE)
|
|
|
|
#undef MAKE_CASE
|
|
|
|
private:
|
|
|
|
StringStream* stream() { return &stream_; }
|
|
|
|
HeapStringAllocator alloc_;
|
|
|
|
StringStream stream_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
RegExpUnparser::RegExpUnparser() : stream_(&alloc_) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitDisjunction(RegExpDisjunction* that, void* data) {
|
|
|
|
stream()->Add("(|");
|
|
|
|
for (int i = 0; i < that->alternatives()->length(); i++) {
|
|
|
|
stream()->Add(" ");
|
|
|
|
that->alternatives()->at(i)->Accept(this, data);
|
|
|
|
}
|
|
|
|
stream()->Add(")");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitAlternative(RegExpAlternative* that, void* data) {
|
|
|
|
stream()->Add("(:");
|
|
|
|
for (int i = 0; i < that->nodes()->length(); i++) {
|
|
|
|
stream()->Add(" ");
|
|
|
|
that->nodes()->at(i)->Accept(this, data);
|
|
|
|
}
|
|
|
|
stream()->Add(")");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RegExpUnparser::VisitCharacterRange(CharacterRange that) {
|
|
|
|
stream()->Add("%k", that.from());
|
|
|
|
if (!that.IsSingleton()) {
|
|
|
|
stream()->Add("-%k", that.to());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitCharacterClass(RegExpCharacterClass* that,
|
|
|
|
void* data) {
|
|
|
|
if (that->is_negated())
|
|
|
|
stream()->Add("^");
|
|
|
|
stream()->Add("[");
|
|
|
|
for (int i = 0; i < that->ranges()->length(); i++) {
|
|
|
|
if (i > 0) stream()->Add(" ");
|
|
|
|
VisitCharacterRange(that->ranges()->at(i));
|
|
|
|
}
|
|
|
|
stream()->Add("]");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitAssertion(RegExpAssertion* that, void* data) {
|
|
|
|
switch (that->type()) {
|
|
|
|
case RegExpAssertion::START_OF_INPUT:
|
|
|
|
stream()->Add("@^i");
|
|
|
|
break;
|
|
|
|
case RegExpAssertion::END_OF_INPUT:
|
|
|
|
stream()->Add("@$i");
|
|
|
|
break;
|
|
|
|
case RegExpAssertion::START_OF_LINE:
|
|
|
|
stream()->Add("@^l");
|
|
|
|
break;
|
|
|
|
case RegExpAssertion::END_OF_LINE:
|
|
|
|
stream()->Add("@$l");
|
|
|
|
break;
|
|
|
|
case RegExpAssertion::BOUNDARY:
|
|
|
|
stream()->Add("@b");
|
|
|
|
break;
|
|
|
|
case RegExpAssertion::NON_BOUNDARY:
|
|
|
|
stream()->Add("@B");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitAtom(RegExpAtom* that, void* data) {
|
|
|
|
stream()->Add("'");
|
|
|
|
Vector<const uc16> chardata = that->data();
|
|
|
|
for (int i = 0; i < chardata.length(); i++) {
|
|
|
|
stream()->Add("%k", chardata[i]);
|
|
|
|
}
|
|
|
|
stream()->Add("'");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitText(RegExpText* that, void* data) {
|
|
|
|
if (that->elements()->length() == 1) {
|
|
|
|
that->elements()->at(0).data.u_atom->Accept(this, data);
|
|
|
|
} else {
|
|
|
|
stream()->Add("(!");
|
|
|
|
for (int i = 0; i < that->elements()->length(); i++) {
|
|
|
|
stream()->Add(" ");
|
|
|
|
that->elements()->at(i).data.u_atom->Accept(this, data);
|
|
|
|
}
|
|
|
|
stream()->Add(")");
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitQuantifier(RegExpQuantifier* that, void* data) {
|
|
|
|
stream()->Add("(# %i ", that->min());
|
2008-12-17 10:59:14 +00:00
|
|
|
if (that->max() == RegExpTree::kInfinity) {
|
2008-11-25 11:07:48 +00:00
|
|
|
stream()->Add("- ");
|
|
|
|
} else {
|
|
|
|
stream()->Add("%i ", that->max());
|
|
|
|
}
|
2010-01-07 19:01:23 +00:00
|
|
|
stream()->Add(that->is_greedy() ? "g " : that->is_possessive() ? "p " : "n ");
|
2008-11-25 11:07:48 +00:00
|
|
|
that->body()->Accept(this, data);
|
|
|
|
stream()->Add(")");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitCapture(RegExpCapture* that, void* data) {
|
|
|
|
stream()->Add("(^ ");
|
|
|
|
that->body()->Accept(this, data);
|
|
|
|
stream()->Add(")");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitLookahead(RegExpLookahead* that, void* data) {
|
|
|
|
stream()->Add("(-> ");
|
|
|
|
stream()->Add(that->is_positive() ? "+ " : "- ");
|
|
|
|
that->body()->Accept(this, data);
|
|
|
|
stream()->Add(")");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitBackReference(RegExpBackReference* that,
|
|
|
|
void* data) {
|
|
|
|
stream()->Add("(<- %i)", that->index());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* RegExpUnparser::VisitEmpty(RegExpEmpty* that, void* data) {
|
|
|
|
stream()->Put('%');
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SmartPointer<const char> RegExpTree::ToString() {
|
|
|
|
RegExpUnparser unparser;
|
|
|
|
Accept(&unparser, NULL);
|
|
|
|
return unparser.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-17 10:59:14 +00:00
|
|
|
RegExpDisjunction::RegExpDisjunction(ZoneList<RegExpTree*>* alternatives)
|
|
|
|
: alternatives_(alternatives) {
|
2009-01-23 07:46:44 +00:00
|
|
|
ASSERT(alternatives->length() > 1);
|
2008-12-17 10:59:14 +00:00
|
|
|
RegExpTree* first_alternative = alternatives->at(0);
|
|
|
|
min_match_ = first_alternative->min_match();
|
|
|
|
max_match_ = first_alternative->max_match();
|
|
|
|
for (int i = 1; i < alternatives->length(); i++) {
|
|
|
|
RegExpTree* alternative = alternatives->at(i);
|
|
|
|
min_match_ = Min(min_match_, alternative->min_match());
|
|
|
|
max_match_ = Max(max_match_, alternative->max_match());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RegExpAlternative::RegExpAlternative(ZoneList<RegExpTree*>* nodes)
|
|
|
|
: nodes_(nodes) {
|
2009-01-23 07:46:44 +00:00
|
|
|
ASSERT(nodes->length() > 1);
|
2008-12-17 10:59:14 +00:00
|
|
|
min_match_ = 0;
|
|
|
|
max_match_ = 0;
|
|
|
|
for (int i = 0; i < nodes->length(); i++) {
|
|
|
|
RegExpTree* node = nodes->at(i);
|
|
|
|
min_match_ += node->min_match();
|
|
|
|
int node_max_match = node->max_match();
|
|
|
|
if (kInfinity - max_match_ < node_max_match) {
|
|
|
|
max_match_ = kInfinity;
|
|
|
|
} else {
|
|
|
|
max_match_ += node->max_match();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add a predicate IsPrimitive to AST Expression nodes.
IsPrimitive reflects that an expression's value is known statically to
be one of the ECMA-262-3 JS types other than Object (e.g., Undefined,
Null, Boolean, String, or Number).
The type conversions ToPrimitive, ToNumber, ToInteger, ToInt32,
ToUInt32, ToUint16, ToString, or ToObject cannot invoke user code for
primitive input values. ToObject throws a TypeError if its input is
Undefined or Null.
Review URL: http://codereview.chromium.org/912002
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4116 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2010-03-12 13:10:42 +00:00
|
|
|
// IsPrimitive implementation. IsPrimitive is true if the value of an
|
|
|
|
// expression is known at compile-time to be any JS type other than Object
|
|
|
|
// (e.g, it is Undefined, Null, Boolean, String, or Number).
|
|
|
|
|
|
|
|
// The following expression types are never primitive because they express
|
|
|
|
// Object values.
|
|
|
|
bool FunctionLiteral::IsPrimitive() { return false; }
|
|
|
|
bool FunctionBoilerplateLiteral::IsPrimitive() { return false; }
|
|
|
|
bool RegExpLiteral::IsPrimitive() { return false; }
|
|
|
|
bool ObjectLiteral::IsPrimitive() { return false; }
|
|
|
|
bool ArrayLiteral::IsPrimitive() { return false; }
|
|
|
|
bool CatchExtensionObject::IsPrimitive() { return false; }
|
|
|
|
bool CallNew::IsPrimitive() { return false; }
|
|
|
|
bool ThisFunction::IsPrimitive() { return false; }
|
|
|
|
|
|
|
|
|
|
|
|
// The following expression types are not always primitive because we do not
|
|
|
|
// have enough information to conclude that they are.
|
|
|
|
bool VariableProxy::IsPrimitive() { return false; }
|
|
|
|
bool Property::IsPrimitive() { return false; }
|
|
|
|
bool Call::IsPrimitive() { return false; }
|
|
|
|
bool CallRuntime::IsPrimitive() { return false; }
|
|
|
|
|
|
|
|
|
|
|
|
// The value of a conditional is the value of one of the alternatives. It's
|
|
|
|
// always primitive if both alternatives are always primitive.
|
|
|
|
bool Conditional::IsPrimitive() {
|
|
|
|
return then_expression()->IsPrimitive() && else_expression()->IsPrimitive();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// A literal is primitive when it is not a JSObject.
|
|
|
|
bool Literal::IsPrimitive() { return !handle()->IsJSObject(); }
|
|
|
|
|
|
|
|
|
|
|
|
// The value of an assignment is the value of its right-hand side.
|
|
|
|
bool Assignment::IsPrimitive() {
|
|
|
|
switch (op()) {
|
|
|
|
case Token::INIT_VAR:
|
|
|
|
case Token::INIT_CONST:
|
|
|
|
case Token::ASSIGN:
|
|
|
|
return value()->IsPrimitive();
|
|
|
|
|
|
|
|
default:
|
|
|
|
// {|=, ^=, &=, <<=, >>=, >>>=, +=, -=, *=, /=, %=}
|
|
|
|
// Arithmetic operations are always primitive. They express Numbers
|
|
|
|
// with the exception of +, which expresses a Number or a String.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Throw does not express a value, so it's trivially always primitive.
|
|
|
|
bool Throw::IsPrimitive() { return true; }
|
|
|
|
|
|
|
|
|
|
|
|
// Unary operations always express primitive values. delete and ! express
|
|
|
|
// Booleans, void Undefined, typeof String, +, -, and ~ Numbers.
|
|
|
|
bool UnaryOperation::IsPrimitive() { return true; }
|
|
|
|
|
|
|
|
|
|
|
|
// Count operations (pre- and post-fix increment and decrement) always
|
|
|
|
// express primitive values (Numbers). See ECMA-262-3, 11.3.1, 11.3.2,
|
|
|
|
// 11.4.4, ane 11.4.5.
|
|
|
|
bool CountOperation::IsPrimitive() { return true; }
|
|
|
|
|
|
|
|
|
|
|
|
// Binary operations depend on the operator.
|
|
|
|
bool BinaryOperation::IsPrimitive() {
|
|
|
|
switch (op()) {
|
|
|
|
case Token::COMMA:
|
|
|
|
// Value is the value of the right subexpression.
|
|
|
|
return right()->IsPrimitive();
|
|
|
|
|
|
|
|
case Token::OR:
|
|
|
|
case Token::AND:
|
|
|
|
// Value is the value one of the subexpressions.
|
|
|
|
return left()->IsPrimitive() && right()->IsPrimitive();
|
|
|
|
|
|
|
|
default:
|
|
|
|
// {|, ^, &, <<, >>, >>>, +, -, *, /, %}
|
|
|
|
// Arithmetic operations are always primitive. They express Numbers
|
|
|
|
// with the exception of +, which expresses a Number or a String.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Compare operations always express Boolean values.
|
|
|
|
bool CompareOperation::IsPrimitive() { return true; }
|
|
|
|
|
2008-12-17 10:59:14 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|