Commit 54ff89b2 authored by neis's avatar neis Committed by Commit bot

[ast][parsing] Variable declaration cleanups.

- Remove Declaration::initialization(), move logic into parser.
  The backends should only care about the actual initialization flag on the
  variable.
- Introduce DeclareVariable convenience function that covers most cases of
  variable declarations.

R=adamk@chromium.org
BUG=

Review-Url: https://codereview.chromium.org/2223843002
Cr-Commit-Position: refs/heads/master@{#38477}
parent 8f0ff8f8
......@@ -65,19 +65,6 @@ MaterializedLiteral* AstNode::AsMaterializedLiteral() {
#undef RETURN_NODE
InitializationFlag Declaration::initialization() const {
switch (node_type()) {
#define GENERATE_CASE(Node) \
case k##Node: \
return static_cast<const Node*>(this)->initialization();
DECLARATION_NODE_LIST(GENERATE_CASE);
#undef GENERATE_CASE
default:
UNREACHABLE();
return kNeedsInitialization;
}
}
bool Expression::IsSmiLiteral() const {
return IsLiteral() && AsLiteral()->value()->IsSmi();
}
......
......@@ -497,7 +497,6 @@ class Declaration : public AstNode {
VariableProxy* proxy() const { return proxy_; }
VariableMode mode() const { return mode_; }
Scope* scope() const { return scope_; }
InitializationFlag initialization() const;
protected:
Declaration(VariableProxy* proxy, VariableMode mode, Scope* scope, int pos,
......@@ -516,18 +515,12 @@ class Declaration : public AstNode {
class VariableDeclaration final : public Declaration {
public:
InitializationFlag initialization() const { return initialization_; }
private:
friend class AstNodeFactory;
VariableDeclaration(VariableProxy* proxy, VariableMode mode, Scope* scope,
InitializationFlag initialization, int pos)
: Declaration(proxy, mode, scope, pos, kVariableDeclaration),
initialization_(initialization) {}
InitializationFlag initialization_;
int pos)
: Declaration(proxy, mode, scope, pos, kVariableDeclaration) {}
};
......@@ -535,7 +528,6 @@ class FunctionDeclaration final : public Declaration {
public:
FunctionLiteral* fun() const { return fun_; }
void set_fun(FunctionLiteral* f) { fun_ = f; }
InitializationFlag initialization() const { return kCreatedInitialized; }
private:
friend class AstNodeFactory;
......@@ -3012,16 +3004,7 @@ class AstNodeFactory final BASE_EMBEDDED {
VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy,
VariableMode mode, Scope* scope,
int pos) {
return NewVariableDeclaration(
proxy, mode, scope,
mode == VAR ? kCreatedInitialized : kNeedsInitialization, pos);
}
VariableDeclaration* NewVariableDeclaration(VariableProxy* proxy,
VariableMode mode, Scope* scope,
InitializationFlag init,
int pos) {
return new (zone_) VariableDeclaration(proxy, mode, scope, init, pos);
return new (zone_) VariableDeclaration(proxy, mode, scope, pos);
}
FunctionDeclaration* NewFunctionDeclaration(VariableProxy* proxy,
......
This diff is collapsed.
......@@ -1075,13 +1075,16 @@ class Parser : public ParserBase<ParserTraits> {
Scope* complex_params_scope,
bool* ok);
// Parser support
static InitializationFlag DefaultInitializationFlag(VariableMode mode);
VariableProxy* NewUnresolved(const AstRawString* name, VariableMode mode);
Variable* Declare(Declaration* declaration,
DeclarationDescriptor::Kind declaration_kind, bool* ok,
DeclarationDescriptor::Kind declaration_kind,
InitializationFlag init, bool* ok,
Scope* declaration_scope = nullptr);
void DeclareConstVariable(const AstRawString* name, InitializationFlag init,
int pos, bool* ok);
Declaration* DeclareVariable(const AstRawString* name, VariableMode mode,
int pos, bool* ok);
Declaration* DeclareVariable(const AstRawString* name, VariableMode mode,
InitializationFlag init, int pos, bool* ok);
bool TargetStackContainsLabel(const AstRawString* label);
BreakableStatement* LookupBreakTarget(const AstRawString* label, bool* ok);
......
......@@ -152,13 +152,13 @@ void Parser::PatternRewriter::VisitVariableProxy(VariableProxy* pattern) {
proxy, descriptor_->mode, descriptor_->scope,
descriptor_->declaration_pos);
Variable* var = parser_->Declare(declaration, descriptor_->declaration_kind,
DefaultInitializationFlag(descriptor_->mode),
ok_, descriptor_->hoist_scope);
if (!*ok_) return;
DCHECK_NOT_NULL(var);
DCHECK(!proxy->is_resolved() || proxy->var() == var);
var->set_initializer_position(initializer_position_);
DCHECK(initializer_position_ != kNoSourcePosition);
var->set_initializer_position(initializer_position_);
// TODO(adamk): This should probably be checking hoist_scope.
// Move it to Parser::Declare() to make it easier to test
......
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