Commit 6e8a2d27 authored by neis's avatar neis Committed by Commit bot

[ast][parser] Remove redundant Declaration::mode_.

BUG=

Review-Url: https://codereview.chromium.org/2226223002
Cr-Commit-Position: refs/heads/master@{#38519}
parent acf0fd3c
......@@ -495,18 +495,13 @@ class DoExpression final : public Expression {
class Declaration : public AstNode {
public:
VariableProxy* proxy() const { return proxy_; }
VariableMode mode() const { return mode_; }
Scope* scope() const { return scope_; }
protected:
Declaration(VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
NodeType type)
: AstNode(pos, type), mode_(mode), proxy_(proxy), scope_(scope) {
DCHECK(IsDeclaredVariableMode(mode));
}
Declaration(VariableProxy* proxy, Scope* scope, int pos, NodeType type)
: AstNode(pos, type), proxy_(proxy), scope_(scope) {}
private:
VariableMode mode_;
VariableProxy* proxy_;
// Nested scope from which the declaration originated.
......@@ -518,9 +513,8 @@ class VariableDeclaration final : public Declaration {
private:
friend class AstNodeFactory;
VariableDeclaration(VariableProxy* proxy, VariableMode mode, Scope* scope,
int pos)
: Declaration(proxy, mode, scope, pos, kVariableDeclaration) {}
VariableDeclaration(VariableProxy* proxy, Scope* scope, int pos)
: Declaration(proxy, scope, pos, kVariableDeclaration) {}
};
......@@ -532,10 +526,9 @@ class FunctionDeclaration final : public Declaration {
private:
friend class AstNodeFactory;
FunctionDeclaration(VariableProxy* proxy, VariableMode mode,
FunctionLiteral* fun, Scope* scope, int pos)
: Declaration(proxy, mode, scope, pos, kFunctionDeclaration), fun_(fun) {
DCHECK(mode == VAR || mode == LET || mode == CONST);
FunctionDeclaration(VariableProxy* proxy, FunctionLiteral* fun, Scope* scope,
int pos)
: Declaration(proxy, scope, pos, kFunctionDeclaration), fun_(fun) {
DCHECK(fun != NULL);
}
......@@ -3002,17 +2995,14 @@ class AstNodeFactory final BASE_EMBEDDED {
}
VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy,
VariableMode mode, Scope* scope,
int pos) {
return new (zone_) VariableDeclaration(proxy, mode, scope, pos);
Scope* scope, int pos) {
return new (zone_) VariableDeclaration(proxy, scope, pos);
}
FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
VariableMode mode,
FunctionLiteral* fun,
Scope* scope,
int pos) {
return new (zone_) FunctionDeclaration(proxy, mode, fun, scope, pos);
Scope* scope, int pos) {
return new (zone_) FunctionDeclaration(proxy, fun, scope, pos);
}
Block* NewBlock(ZoneList<const AstRawString*>* labels, int capacity,
......
......@@ -722,8 +722,7 @@ void AstPrinter::VisitBlock(Block* node) {
// TODO(svenpanne) Start with IndentedScope.
void AstPrinter::VisitVariableDeclaration(VariableDeclaration* node) {
PrintLiteralWithModeIndented(Variable::Mode2String(node->mode()),
node->proxy()->var(),
PrintLiteralWithModeIndented("VARIABLE", node->proxy()->var(),
node->proxy()->name());
}
......
......@@ -343,7 +343,7 @@ void Scope::DeserializeScopeInfo(Isolate* isolate,
Variable(this, name, mode, Variable::NORMAL, kCreatedInitialized);
VariableProxy* proxy = factory.NewVariableProxy(result);
VariableDeclaration* declaration =
factory.NewVariableDeclaration(proxy, mode, this, kNoSourcePosition);
factory.NewVariableDeclaration(proxy, this, kNoSourcePosition);
AsDeclarationScope()->DeclareFunctionVar(declaration);
result->AllocateTo(VariableLocation::CONTEXT, index);
}
......@@ -611,7 +611,7 @@ Variable* DeclarationScope::LookupFunctionVar(const AstRawString* name,
DCHECK_NOT_NULL(factory);
VariableProxy* proxy = factory->NewVariableProxy(var);
VariableDeclaration* declaration =
factory->NewVariableDeclaration(proxy, mode, this, kNoSourcePosition);
factory->NewVariableDeclaration(proxy, this, kNoSourcePosition);
DCHECK_EQ(factory->zone(), zone());
DeclareFunctionVar(declaration);
var->AllocateTo(VariableLocation::CONTEXT, index);
......@@ -744,13 +744,13 @@ Declaration* Scope::CheckConflictingVarDeclarations() {
int length = decls_.length();
for (int i = 0; i < length; i++) {
Declaration* decl = decls_[i];
VariableMode mode = decl->proxy()->var()->mode();
// We don't create a separate scope to hold the function name of a function
// expression, so we have to make sure not to consider it when checking for
// conflicts (since it's conceptually "outside" the declaration scope).
if (is_function_scope() && decl == AsDeclarationScope()->function())
continue;
if (IsLexicalVariableMode(decl->mode()) && !is_block_scope()) continue;
const AstRawString* name = decl->proxy()->raw_name();
if (IsLexicalVariableMode(mode) && !is_block_scope()) continue;
// Iterate through all scopes until and including the declaration scope.
Scope* previous = NULL;
......@@ -759,10 +759,11 @@ Declaration* Scope::CheckConflictingVarDeclarations() {
// captured in Parser::Declare. The only conflicts we still need to check
// are lexical vs VAR, or any declarations within a declaration block scope
// vs lexical declarations in its surrounding (function) scope.
if (IsLexicalVariableMode(decl->mode())) current = current->outer_scope_;
if (IsLexicalVariableMode(mode)) current = current->outer_scope_;
do {
// There is a conflict if there exists a non-VAR binding.
Variable* other_var = current->variables_.Lookup(name);
Variable* other_var =
current->variables_.Lookup(decl->proxy()->raw_name());
if (other_var != NULL && IsLexicalVariableMode(other_var->mode())) {
return decl;
}
......
......@@ -1979,28 +1979,30 @@ Declaration* Parser::DeclareVariable(const AstRawString* name,
DCHECK_NOT_NULL(name);
VariableProxy* proxy = NewUnresolved(name, mode);
Declaration* declaration =
factory()->NewVariableDeclaration(proxy, mode, scope(), pos);
Declare(declaration, DeclarationDescriptor::NORMAL, init, CHECK_OK);
factory()->NewVariableDeclaration(proxy, scope(), pos);
Declare(declaration, DeclarationDescriptor::NORMAL, mode, init, CHECK_OK);
return declaration;
}
Variable* Parser::Declare(Declaration* declaration,
DeclarationDescriptor::Kind declaration_kind,
InitializationFlag init, bool* ok, Scope* scope) {
VariableMode mode, InitializationFlag init, bool* ok,
Scope* scope) {
DCHECK(IsDeclaredVariableMode(mode) && mode != CONST_LEGACY);
VariableProxy* proxy = declaration->proxy();
DCHECK(proxy->raw_name() != NULL);
const AstRawString* name = proxy->raw_name();
VariableMode mode = declaration->mode();
DCHECK(IsDeclaredVariableMode(mode) && mode != CONST_LEGACY);
bool is_function_declaration = declaration->IsFunctionDeclaration();
if (scope == nullptr) scope = this->scope();
if (mode == VAR) scope = scope->GetDeclarationScope();
DCHECK(!scope->is_catch_scope());
DCHECK(!scope->is_with_scope());
DCHECK(scope->is_declaration_scope() ||
(IsLexicalVariableMode(mode) && scope->is_block_scope()));
bool is_function_declaration = declaration->IsFunctionDeclaration();
Variable* var = NULL;
if (scope->is_eval_scope() && is_sloppy(scope->language_mode()) &&
mode == VAR) {
......@@ -2212,8 +2214,8 @@ Statement* Parser::ParseHoistableDeclaration(
: VAR;
VariableProxy* proxy = NewUnresolved(variable_name, mode);
Declaration* declaration =
factory()->NewFunctionDeclaration(proxy, mode, fun, scope(), pos);
Declare(declaration, DeclarationDescriptor::NORMAL, kCreatedInitialized,
factory()->NewFunctionDeclaration(proxy, fun, scope(), pos);
Declare(declaration, DeclarationDescriptor::NORMAL, mode, kCreatedInitialized,
CHECK_OK);
if (names) names->Add(variable_name, zone());
EmptyStatement* empty = factory()->NewEmptyStatement(kNoSourcePosition);
......@@ -4939,8 +4941,8 @@ ZoneList<Statement*>* Parser::ParseEagerFunctionBody(
Variable(scope(), function_name, fvar_mode, Variable::NORMAL,
kCreatedInitialized, kNotAssigned);
VariableProxy* proxy = factory()->NewVariableProxy(fvar);
VariableDeclaration* fvar_declaration = factory()->NewVariableDeclaration(
proxy, fvar_mode, scope(), kNoSourcePosition);
VariableDeclaration* fvar_declaration =
factory()->NewVariableDeclaration(proxy, scope(), kNoSourcePosition);
function_scope->DeclareFunctionVar(fvar_declaration);
VariableProxy* fproxy = factory()->NewVariableProxy(fvar);
......@@ -5020,9 +5022,9 @@ Expression* Parser::ParseClassLiteral(ExpressionClassifier* classifier,
if (name != nullptr) {
proxy = NewUnresolved(name, CONST);
// TODO(verwaest): declare via block_state.
Declaration* declaration = factory()->NewVariableDeclaration(
proxy, CONST, block_state.scope(), pos);
Declare(declaration, DeclarationDescriptor::NORMAL,
Declaration* declaration =
factory()->NewVariableDeclaration(proxy, block_state.scope(), pos);
Declare(declaration, DeclarationDescriptor::NORMAL, CONST,
DefaultInitializationFlag(CONST), CHECK_OK);
}
......@@ -5216,7 +5218,9 @@ void Parser::InsertShadowingVarBindingInitializers(Block* inner_block) {
BlockState block_state(&scope_state_, inner_scope);
for (int i = 0; i < decls->length(); ++i) {
Declaration* decl = decls->at(i);
if (decl->mode() != VAR || !decl->IsVariableDeclaration()) continue;
if (decl->proxy()->var()->mode() != VAR || !decl->IsVariableDeclaration()) {
continue;
}
const AstRawString* name = decl->proxy()->raw_name();
Variable* parameter = function_scope->LookupLocal(name);
if (parameter == nullptr) continue;
......@@ -5298,9 +5302,9 @@ void Parser::InsertSloppyBlockFunctionVarBindings(DeclarationScope* scope,
if (!var_created) {
var_created = true;
VariableProxy* proxy = scope->NewUnresolved(factory(), name);
Declaration* declaration = factory()->NewVariableDeclaration(
proxy, VAR, scope, kNoSourcePosition);
Declare(declaration, DeclarationDescriptor::NORMAL,
Declaration* declaration =
factory()->NewVariableDeclaration(proxy, scope, kNoSourcePosition);
Declare(declaration, DeclarationDescriptor::NORMAL, VAR,
DefaultInitializationFlag(VAR), ok, scope);
DCHECK(ok); // Based on the preceding check, this should not fail
if (!ok) return;
......
......@@ -1089,7 +1089,7 @@ class Parser : public ParserBase<ParserTraits> {
VariableProxy* NewUnresolved(const AstRawString* name, VariableMode mode);
Variable* Declare(Declaration* declaration,
DeclarationDescriptor::Kind declaration_kind,
InitializationFlag init, bool* ok,
VariableMode mode, InitializationFlag init, bool* ok,
Scope* declaration_scope = nullptr);
Declaration* DeclareVariable(const AstRawString* name, VariableMode mode,
int pos, bool* ok);
......
......@@ -149,14 +149,14 @@ void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) {
const AstRawString* name = pattern->raw_name();
VariableProxy* proxy = parser_->NewUnresolved(name, descriptor_->mode);
Declaration* declaration = factory()->NewVariableDeclaration(
proxy, descriptor_->mode, descriptor_->scope,
descriptor_->declaration_pos);
proxy, descriptor_->scope, descriptor_->declaration_pos);
Variable* var = parser_->Declare(declaration, descriptor_->declaration_kind,
descriptor_->mode,
DefaultInitializationFlag(descriptor_->mode),
ok_, descriptor_->hoist_scope);
if (!*ok_) return;
DCHECK_NOT_NULL(var);
DCHECK(!proxy->is_resolved() || proxy->var() == var);
DCHECK(proxy->is_resolved());
DCHECK(initializer_position_ != kNoSourcePosition);
var->set_initializer_position(initializer_position_);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment