Skip to content

Commit

Permalink
add new type Assigned to AST
Browse files Browse the repository at this point in the history
  • Loading branch information
Constellation committed Dec 3, 2011
1 parent c18054d commit 60eb2b7
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 49 deletions.
32 changes: 24 additions & 8 deletions iv/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,16 @@ INHERIT(Declaration);
template<typename Factory>
class Declaration : public DeclarationBase<Factory> {
public:
Declaration(const SymbolHolder& name,
Declaration(Assigned<Factory>* name,
Maybe<Expression<Factory> > expr)
: name_(name),
expr_(expr) {
}
inline const SymbolHolder& name() const { return name_; }
inline Assigned<Factory>* name() const { return name_; }
inline Maybe<Expression<Factory> > expr() const { return expr_; }
DECLARE_DERIVED_NODE_TYPE(Declaration)
private:
SymbolHolder name_;
Assigned<Factory>* name_;
Maybe<Expression<Factory> > expr_;
};

Expand Down Expand Up @@ -642,7 +642,7 @@ class BreakStatement : public BreakStatementBase<Factory> {
// test: break test;
// } while (0);
// if above example, target is NULL
assert(target_ || label_);
assert(target_ || !label_.IsDummy());
}
inline const SymbolHolder& label() const { return label_; }
inline BreakableStatement<Factory>* target() const {
Expand Down Expand Up @@ -1039,7 +1039,6 @@ INHERIT(Identifier);
template<typename Factory>
class Identifier : public IdentifierBase<Factory> {
public:
typedef typename SpaceUString<Factory>::type value_type;
Identifier(Symbol sym) : sym_(sym) { }
Symbol symbol() const { return sym_; }
inline bool IsValidLeftHandSide() const { return true; }
Expand All @@ -1049,6 +1048,23 @@ class Identifier : public IdentifierBase<Factory> {
Symbol sym_;
};

// Assigned
template<typename Factory>
class Inherit<Factory, kAssigned> : public AstNode<Factory> {
};
INHERIT(Assigned);

template<typename Factory>
class Assigned : public AssignedBase<Factory> {
public:
Assigned(Symbol sym) : sym_(sym) { }
Symbol symbol() const { return sym_; }
DECLARE_DERIVED_NODE_TYPE(Assigned)
ACCEPT_EXPRESSION_VISITOR
protected:
Symbol sym_;
};

// ThisLiteral
template<typename Factory>
class Inherit<Factory, kThisLiteral>
Expand Down Expand Up @@ -1226,7 +1242,7 @@ class FunctionLiteral : public FunctionLiteralBase<Factory> {

// name maybe dummy
FunctionLiteral(DeclType type,
const SymbolHolder& name,
Maybe<Assigned<Factory> > name,
Symbols* params,
Statements* body,
Scope<Factory>* scope,
Expand All @@ -1243,7 +1259,7 @@ class FunctionLiteral : public FunctionLiteralBase<Factory> {
block_end_position_(end_position) {
}

inline const SymbolHolder& name() const { return name_; }
inline Maybe<Assigned<Factory> > name() const { return name_; }
inline DeclType type() const { return type_; }
inline const Symbols& params() const { return *params_; }
inline const Statements& body() const { return *body_; }
Expand All @@ -1259,7 +1275,7 @@ class FunctionLiteral : public FunctionLiteralBase<Factory> {
DECLARE_DERIVED_NODE_TYPE(FunctionLiteral)
ACCEPT_EXPRESSION_VISITOR
private:
SymbolHolder name_;
Maybe<Assigned<Factory> > name_;
DeclType type_;
Symbols* params_;
Statements* body_;
Expand Down
21 changes: 13 additions & 8 deletions iv/ast_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class BasicAstFactory {
begin, end);
}

Assigned* NewAssigned(const SymbolHolder& holder) {
return Location(
new(static_cast<Factory*>(this)) Assigned(holder),
holder.begin_position(), holder.end_position());
}

NumberLiteral* NewReducedNumberLiteral(const double& val) {
return Location(
new(static_cast<Factory*>(this)) NumberLiteral(val), 0, 0);
Expand Down Expand Up @@ -72,7 +78,7 @@ class BasicAstFactory {
}

FunctionLiteral* NewFunctionLiteral(typename FunctionLiteral::DeclType type,
const SymbolHolder& name,
Maybe<Assigned> name,
Symbols* params,
Statements* body,
Scope* scope,
Expand Down Expand Up @@ -101,10 +107,9 @@ class BasicAstFactory {
begin, end);
}

ObjectLiteral*
NewObjectLiteral(typename ObjectLiteral::Properties* properties,
std::size_t begin,
std::size_t end) {
ObjectLiteral* NewObjectLiteral(
typename ObjectLiteral::Properties* properties,
std::size_t begin, std::size_t end) {
return Location(
new(static_cast<Factory*>(this)) ObjectLiteral(properties),
begin, end);
Expand Down Expand Up @@ -203,12 +208,12 @@ class BasicAstFactory {
begin, end);
}

Declaration* NewDeclaration(const SymbolHolder& name,
Declaration* NewDeclaration(Assigned* name,
Maybe<Expression> expr) {
return Location(
new(static_cast<Factory*>(this)) Declaration(name, expr),
name.begin_position(),
(expr) ? (*expr).end_position() : name.end_position());
name->begin_position(),
(expr) ? (*expr).end_position() : name->end_position());
}

IfStatement* NewIfStatement(Expression* cond,
Expand Down
1 change: 1 addition & 0 deletions iv/ast_fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ V(Call)\
IV_EXPRESSION_DERIVED_NODE_LIST(V)

#define IV_OTHER_DERIVED_NODE_LIST(V)\
V(Assigned)\
V(Declaration)

#define IV_OTHER_NODE_LIST(V)\
Expand Down
8 changes: 5 additions & 3 deletions iv/ast_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class AstSerializer: public AstVisitor<Factory>::const_type {
while (it != end) {
const Declaration& decl = **it;
builder_.Append("{\"type\":\"decl\",\"name\":");
Write(decl.name());
Write(decl.name()->symbol());
builder_.Append(",\"exp\":");
if (const Maybe<const Expression> expr = decl.expr()) {
(*expr).Accept(this);
Expand Down Expand Up @@ -324,6 +324,8 @@ class AstSerializer: public AstVisitor<Factory>::const_type {
builder_.Append("\"}");
}

void Visit(const Assigned* assigned) { }

void Visit(const Identifier* literal) {
builder_.Append("{\"type\":\"identifier\",\"value\":\"");
builder_.Append(symbol::GetSymbolString(literal->symbol()));
Expand Down Expand Up @@ -415,8 +417,8 @@ class AstSerializer: public AstVisitor<Factory>::const_type {

void Visit(const FunctionLiteral* literal) {
builder_.Append("{\"type\":\"function\",\"name\":");
if (literal->name() != symbol::kDummySymbol) {
Write(literal->name());
if (literal->name()) {
Write(literal->name().Address()->symbol());
}
builder_.Append(",\"params\":[");
typename Symbols::const_iterator it = literal->params().begin();
Expand Down
2 changes: 2 additions & 0 deletions iv/ast_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class BasicAstVisitor
virtual void Visit(typename add<ConditionalExpression<Factory> >::type cond) = 0; //NOLINT
virtual void Visit(typename add<UnaryOperation<Factory> >::type unary) = 0; //NOLINT
virtual void Visit(typename add<PostfixExpression<Factory> >::type postfix) = 0; //NOLINT
virtual void Visit(typename add<Assigned<Factory> >::type assigned) = 0; //NOLINT

virtual void Visit(typename add<StringLiteral<Factory> >::type literal) = 0; //NOLINT
virtual void Visit(typename add<NumberLiteral<Factory> >::type literal) = 0; //NOLINT
Expand Down Expand Up @@ -134,6 +135,7 @@ class BasicExpressionVisitor
virtual void Visit(typename add<UnaryOperation<Factory> >::type unary) = 0; //NOLINT
virtual void Visit(typename add<PostfixExpression<Factory> >::type postfix) = 0; //NOLINT

virtual void Visit(typename add<Assigned<Factory> >::type assigned) = 0; //NOLINT
virtual void Visit(typename add<StringLiteral<Factory> >::type literal) = 0; //NOLINT
virtual void Visit(typename add<NumberLiteral<Factory> >::type literal) = 0; //NOLINT
virtual void Visit(typename add<Identifier<Factory> >::type literal) = 0; //NOLINT
Expand Down
4 changes: 2 additions & 2 deletions iv/lv5/railgun/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Code : public radio::HeapObject<radio::POINTER> {
has_eval_(false),
has_arguments_(false),
has_arguments_assign_(false),
has_name_(func.name() != symbol::kDummySymbol),
has_name_(func.name()),
has_declarative_env_(true),
arguments_hiding_(false),
scope_nest_count_(0),
Expand All @@ -78,7 +78,7 @@ class Code : public radio::HeapObject<radio::POINTER> {
exception_table_(),
construct_map_(NULL) {
if (has_name_) {
name_ = func.name();
name_ = func.name().Address()->symbol();
}
Names::iterator target = params_.begin();
for (Symbols::const_iterator it = func.params().begin(),
Expand Down
12 changes: 7 additions & 5 deletions iv/lv5/railgun/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ class Compiler
void Visit(const FunctionStatement* stmt) {
const FunctionLiteral& func = *stmt->function();
assert(func.name()); // FunctionStatement must have name
const uint32_t index = SymbolToNameIndex(func.name());
const uint32_t index = SymbolToNameIndex(func.name().Address()->symbol());
Visit(&func);
EmitStoreName(index);
Emit<OP::POP_TOP>();
Expand Down Expand Up @@ -699,7 +699,7 @@ class Compiler
Visit(decl);
// Identifier
lhs = NULL;
for_decl = decl->name();
for_decl = decl->name()->symbol();
} else {
// LeftHandSideExpression
assert(stmt->each()->AsExpressionStatement());
Expand Down Expand Up @@ -823,7 +823,7 @@ class Compiler
}

void Visit(const BreakStatement* stmt) {
if (!stmt->target() && stmt->label() != symbol::kDummySymbol) {
if (!stmt->target() && !stmt->label().IsDummy()) {
// through
} else {
const JumpEntry& entry = jump_table_[stmt->target()];
Expand Down Expand Up @@ -1675,6 +1675,8 @@ class Compiler
point.LevelCheck(1);
}

void Visit(const Assigned* lit) { }

void Visit(const Identifier* lit) {
// directlly extract value and set to top version
DepthPoint point(&stack_depth_);
Expand Down Expand Up @@ -1840,7 +1842,7 @@ class Compiler

void Visit(const Declaration* decl) {
DepthPoint point(&stack_depth_);
const uint32_t index = SymbolToNameIndex(decl->name());
const uint32_t index = SymbolToNameIndex(decl->name()->symbol());
if (const core::Maybe<const Expression> expr = decl->expr()) {
expr.Address()->Accept(this);
EmitStoreName(index);
Expand Down Expand Up @@ -2012,7 +2014,7 @@ class Compiler
last = functions.end(); it != last; ++it) {
const FunctionLiteral* const func = *it;
Visit(func);
const Symbol sym = func->name();
const Symbol sym = func->name().Address()->symbol();
if (sym == symbol::arguments()) {
// arguments hiding optimization
// example:
Expand Down
2 changes: 1 addition & 1 deletion iv/lv5/railgun/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class FunctionScope : public VariableScope {
const Functions& functions = scope.function_declarations();
for (Functions::const_iterator it = functions.begin(),
last = functions.end(); it != last; ++it) {
const Symbol sym = (*it)->name();
const Symbol sym = (*it)->name().Address()->symbol();
map_[sym] = std::make_tuple(STACK, 0, false);
}

Expand Down
12 changes: 6 additions & 6 deletions iv/lv5/teleporter/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void Interpreter::Invoke(JSCodeFunction* code,
last = scope.function_declarations().end();
it != last; ++it) {
const FunctionLiteral* f = *it;
const Symbol fn = f->name();
const Symbol fn = f->name().Address()->symbol();
EVAL_IN_STMT(f);
const JSVal fo = ctx_->ret();
if (!env->HasBinding(ctx_, fn)) {
Expand Down Expand Up @@ -207,7 +207,7 @@ void Interpreter::Run(const FunctionLiteral* global, bool is_eval) {
last = scope.function_declarations().end();
it != last; ++it) {
const FunctionLiteral* f = *it;
const Symbol fn = f->name();
const Symbol fn = f->name().Address()->symbol();
EVAL_IN_STMT(f);
JSVal fo = ctx_->ret();
if (!env->HasBinding(ctx_, fn)) {
Expand Down Expand Up @@ -309,8 +309,8 @@ void Interpreter::Visit(const Block* block) {
void Interpreter::Visit(const FunctionStatement* stmt) {
const FunctionLiteral* const func = stmt->function();
// FunctionStatement must have name
assert(func->name() != symbol::kDummySymbol);
Resolve(func->name());
assert(func->name());
Resolve(func->name().Address()->symbol());
const JSVal lhs = ctx_->ret();
Visit(func);
const JSVal val = GetValue(ctx_->ret(), CHECK_IN_STMT);
Expand All @@ -323,7 +323,7 @@ void Interpreter::Visit(const VariableStatement* var) {
for (Declarations::const_iterator it = var->decls().begin(),
last = var->decls().end(); it != last; ++it) {
const Declaration* decl = *it;
Resolve(decl->name());
Resolve(decl->name()->symbol());
const JSVal lhs = ctx_->ret();
if (const core::Maybe<const Expression> expr = decl->expr()) {
EVAL_IN_STMT(expr.Address());
Expand Down Expand Up @@ -461,7 +461,7 @@ void Interpreter::Visit(const ForInStatement* stmt) {
if (stmt->each()->AsVariableStatement()) {
const Declaration* decl =
stmt->each()->AsVariableStatement()->decls().front();
for_decl = decl->name();
for_decl = decl->name()->symbol();
Resolve(for_decl);
if (ctx_->IsError()) {
RETURN_STMT(Context::THROW, JSEmpty, NULL);
Expand Down
1 change: 1 addition & 0 deletions iv/lv5/teleporter/interpreter_fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Interpreter : private core::Noncopyable<Interpreter>, public AstVisitor {
inline void Visit(const UnaryOperation* unary);
inline void Visit(const PostfixExpression* postfix);
inline void Visit(const StringLiteral* literal);
inline void Visit(const Assigned* assigned) { };
inline void Visit(const NumberLiteral* literal);
inline void Visit(const Identifier* literal);
inline void Visit(const ThisLiteral* literal);
Expand Down
12 changes: 6 additions & 6 deletions iv/lv5/teleporter/jsfunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class JSCodeFunction : public JSFunction {

core::UString GetName() const {
if (HasName()) {
return symbol::GetSymbolString(function_->name());
return symbol::GetSymbolString(name());
} else {
return core::UStringPiece();
}
Expand All @@ -98,11 +98,11 @@ class JSCodeFunction : public JSFunction {
}

bool HasName() const {
return (function_->name() != symbol::kDummySymbol);
return function_->name();
}

Symbol name() const {
return function_->name();
return function_->name().Address()->symbol();
}

private:
Expand Down Expand Up @@ -138,11 +138,11 @@ class JSCodeFunction : public JSFunction {
ATTR::WRITABLE),
false, &e);
if (HasName()) {
const core::UString name(symbol::GetSymbolString(function_->name()));
if (!name.empty()) {
const core::UString str(symbol::GetSymbolString(name()));
if (!str.empty()) {
DefineOwnProperty(
ctx, context::Intern(ctx, "name"),
DataDescriptor(JSString::New(ctx, name),
DataDescriptor(JSString::New(ctx, str),
ATTR::NONE),
false, &e);
} else {
Expand Down
Loading

0 comments on commit 60eb2b7

Please sign in to comment.