ast: Move [Set]IsSwizzle() from IdentifierExpression to MemberAccessorExpression

This is more a property of a MemberAccessorExpression than the identifier itself.

Change-Id: Icb17df1fe43a959332d73df026e77ca4e07d23ed
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/40140
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton 2021-02-03 22:26:36 +00:00 committed by Commit Bot service account
parent a568701d61
commit ea9c86c296
5 changed files with 28 additions and 23 deletions

View File

@ -39,12 +39,6 @@ class IdentifierExpression : public Castable<IdentifierExpression, Expression> {
/// @returns the symbol for the identifier /// @returns the symbol for the identifier
Symbol symbol() const { return sym_; } Symbol symbol() const { return sym_; }
/// Sets the identifier as a swizzle
void SetIsSwizzle() { is_swizzle_ = true; }
/// @returns true if this is a swizzle identifier
bool IsSwizzle() const { return is_swizzle_; }
/// Clones this node and all transitive child nodes using the `CloneContext` /// Clones this node and all transitive child nodes using the `CloneContext`
/// `ctx`. /// `ctx`.
/// @note Semantic information such as resolved expression type and intrinsic /// @note Semantic information such as resolved expression type and intrinsic
@ -68,8 +62,6 @@ class IdentifierExpression : public Castable<IdentifierExpression, Expression> {
IdentifierExpression(const IdentifierExpression&) = delete; IdentifierExpression(const IdentifierExpression&) = delete;
Symbol const sym_; Symbol const sym_;
bool is_swizzle_ = false; // Semantic info
}; };
} // namespace ast } // namespace ast

View File

@ -45,6 +45,12 @@ class MemberAccessorExpression
/// @returns the member expression /// @returns the member expression
IdentifierExpression* member() const { return member_; } IdentifierExpression* member() const { return member_; }
/// Sets the identifier as a swizzle
void SetIsSwizzle() { is_swizzle_ = true; }
/// @returns true if this is a swizzle identifier
bool IsSwizzle() const { return is_swizzle_; }
/// Clones this node and all transitive child nodes using the `CloneContext` /// Clones this node and all transitive child nodes using the `CloneContext`
/// `ctx`. /// `ctx`.
/// @note Semantic information such as resolved expression type and intrinsic /// @note Semantic information such as resolved expression type and intrinsic
@ -69,6 +75,8 @@ class MemberAccessorExpression
Expression* const struct_; Expression* const struct_;
IdentifierExpression* const member_; IdentifierExpression* const member_;
bool is_swizzle_ = false; // Semantic info
}; };
} // namespace ast } // namespace ast

View File

@ -1029,7 +1029,7 @@ bool TypeDeterminer::DetermineMemberAccessor(
ret = builder_->create<type::Pointer>(ret, ptr->storage_class()); ret = builder_->create<type::Pointer>(ret, ptr->storage_class());
} }
} else if (auto* vec = data_type->As<type::Vector>()) { } else if (auto* vec = data_type->As<type::Vector>()) {
expr->member()->SetIsSwizzle(); expr->SetIsSwizzle();
auto size = builder_->Symbols().NameFor(expr->member()->symbol()).size(); auto size = builder_->Symbols().NameFor(expr->member()->symbol()).size();
if (size == 1) { if (size == 1) {

View File

@ -1165,12 +1165,7 @@ bool GeneratorImpl::EmitIdentifier(std::ostream&,
} }
} }
// Swizzles output the name directly
if (ident->IsSwizzle()) {
out << builder_.Symbols().NameFor(ident->symbol());
} else {
out << namer_.NameFor(builder_.Symbols().NameFor(ident->symbol())); out << namer_.NameFor(builder_.Symbols().NameFor(ident->symbol()));
}
return true; return true;
} }
@ -2107,7 +2102,15 @@ bool GeneratorImpl::EmitMemberAccessor(std::ostream& pre,
return false; return false;
} }
out << "."; out << ".";
return EmitExpression(pre, out, expr->member());
// Swizzles output the name directly
if (expr->IsSwizzle()) {
out << builder_.Symbols().NameFor(expr->member()->symbol());
} else if (!EmitExpression(pre, out, expr->member())) {
return false;
}
return true;
} }
bool GeneratorImpl::EmitReturn(std::ostream& out, ast::ReturnStatement* stmt) { bool GeneratorImpl::EmitReturn(std::ostream& out, ast::ReturnStatement* stmt) {

View File

@ -1593,12 +1593,7 @@ bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) {
} }
} }
// Swizzles get written out directly
if (ident->IsSwizzle()) {
out_ << program_->Symbols().NameFor(ident->symbol());
} else {
out_ << namer_.NameFor(program_->Symbols().NameFor(ident->symbol())); out_ << namer_.NameFor(program_->Symbols().NameFor(ident->symbol()));
}
return true; return true;
} }
@ -1742,7 +1737,14 @@ bool GeneratorImpl::EmitMemberAccessor(ast::MemberAccessorExpression* expr) {
out_ << "."; out_ << ".";
return EmitExpression(expr->member()); // Swizzles get written out directly
if (expr->IsSwizzle()) {
out_ << program_->Symbols().NameFor(expr->member()->symbol());
} else if (!EmitExpression(expr->member())) {
return false;
}
return true;
} }
bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {