Constify Type* constructor args for ast/sem classes

To avoid breaking things, functions that return the type cast away
the constness for now. This, however, makes it easier to use typ::Type
with these classes, as typ::Type stores pointers to const types. This
also brings us one step closer to constifying types everywhere.

Bug: tint:724
Change-Id: Ia3f4b76f375184dd09b8041c1f60bf1afaefe629
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/48740
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
Antonio Maiorano 2021-04-22 15:34:13 +00:00 committed by Commit Bot service account
parent e204f27f86
commit 26fa9927e8
35 changed files with 100 additions and 63 deletions

View File

@ -24,7 +24,7 @@ namespace ast {
AccessControl::AccessControl(ProgramID program_id,
const Source& source,
Access access,
Type* subtype)
const Type* subtype)
: Base(program_id, source), access_(access), subtype_(subtype) {
TINT_ASSERT(subtype_);
TINT_ASSERT(!subtype_->Is<AccessControl>());

View File

@ -44,7 +44,7 @@ class AccessControl : public Castable<AccessControl, Type> {
AccessControl(ProgramID program_id,
const Source& source,
Access access,
Type* subtype);
const Type* subtype);
/// Move constructor
AccessControl(AccessControl&&);
~AccessControl() override;
@ -59,7 +59,7 @@ class AccessControl : public Castable<AccessControl, Type> {
/// @returns the access control value
Access access_control() const { return access_; }
/// @returns the subtype type
Type* type() const { return subtype_; }
Type* type() const { return const_cast<Type*>(subtype_); }
/// @returns the name for this type
std::string type_name() const override;
@ -76,7 +76,7 @@ class AccessControl : public Castable<AccessControl, Type> {
private:
Access const access_;
Type* const subtype_;
const Type* const subtype_;
};
/// @param out the std::ostream to write to

View File

@ -23,7 +23,7 @@ namespace ast {
BitcastExpression::BitcastExpression(ProgramID program_id,
const Source& source,
sem::Type* type,
const sem::Type* type,
Expression* expr)
: Base(program_id, source), type_(type), expr_(expr) {
TINT_ASSERT(type_);
@ -37,7 +37,7 @@ BitcastExpression::~BitcastExpression() = default;
BitcastExpression* BitcastExpression::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto src = ctx->Clone(source());
auto* ty = ctx->Clone(type_);
auto* ty = ctx->Clone(type());
auto* e = ctx->Clone(expr_);
return ctx->dst->create<BitcastExpression>(src, ty, e);
}

View File

@ -30,14 +30,14 @@ class BitcastExpression : public Castable<BitcastExpression, Expression> {
/// @param expr the expr
BitcastExpression(ProgramID program_id,
const Source& source,
sem::Type* type,
const sem::Type* type,
Expression* expr);
/// Move constructor
BitcastExpression(BitcastExpression&&);
~BitcastExpression() override;
/// @returns the left side expression
sem::Type* type() const { return type_; }
sem::Type* type() const { return const_cast<sem::Type*>(type_); }
/// @returns the expression
Expression* expr() const { return expr_; }
@ -58,7 +58,7 @@ class BitcastExpression : public Castable<BitcastExpression, Expression> {
private:
BitcastExpression(const BitcastExpression&) = delete;
sem::Type* const type_;
const sem::Type* const type_;
Expression* const expr_;
};

View File

@ -23,7 +23,7 @@ namespace ast {
BoolLiteral::BoolLiteral(ProgramID program_id,
const Source& source,
sem::Type* type,
const sem::Type* type,
bool value)
: Base(program_id, source, type), value_(value) {}

View File

@ -32,7 +32,7 @@ class BoolLiteral : public Castable<BoolLiteral, Literal> {
/// @param value the bool literals value
BoolLiteral(ProgramID program_id,
const Source& source,
sem::Type* type,
const sem::Type* type,
bool value);
~BoolLiteral() override;

View File

@ -25,7 +25,7 @@ namespace ast {
FloatLiteral::FloatLiteral(ProgramID program_id,
const Source& source,
sem::Type* type,
const sem::Type* type,
float value)
: Base(program_id, source, type), value_(value) {}

View File

@ -32,7 +32,7 @@ class FloatLiteral : public Castable<FloatLiteral, Literal> {
/// @param value the float literals value
FloatLiteral(ProgramID program_id,
const Source& source,
sem::Type* type,
const sem::Type* type,
float value);
~FloatLiteral() override;

View File

@ -21,7 +21,7 @@ namespace ast {
IntLiteral::IntLiteral(ProgramID program_id,
const Source& source,
sem::Type* type,
const sem::Type* type,
uint32_t value)
: Base(program_id, source, type), value_(value) {}

View File

@ -36,7 +36,7 @@ class IntLiteral : public Castable<IntLiteral, Literal> {
/// @param value value of the literal
IntLiteral(ProgramID program_id,
const Source& source,
sem::Type* type,
const sem::Type* type,
uint32_t value);
private:

View File

@ -19,7 +19,9 @@ TINT_INSTANTIATE_TYPEINFO(tint::ast::Literal);
namespace tint {
namespace ast {
Literal::Literal(ProgramID program_id, const Source& source, sem::Type* type)
Literal::Literal(ProgramID program_id,
const Source& source,
const sem::Type* type)
: Base(program_id, source), type_(type) {}
Literal::~Literal() = default;

View File

@ -28,7 +28,7 @@ class Literal : public Castable<Literal, Node> {
~Literal() override;
/// @returns the type of the literal
sem::Type* type() const { return type_; }
sem::Type* type() const { return const_cast<sem::Type*>(type_); }
/// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
@ -50,10 +50,12 @@ class Literal : public Castable<Literal, Node> {
/// @param program_id the identifier of the program that owns this node
/// @param source the input source
/// @param type the type of the literal
explicit Literal(ProgramID program_id, const Source& source, sem::Type* type);
explicit Literal(ProgramID program_id,
const Source& source,
const sem::Type* type);
private:
sem::Type* const type_;
const sem::Type* const type_;
};
} // namespace ast

View File

@ -23,7 +23,7 @@ namespace ast {
SintLiteral::SintLiteral(ProgramID program_id,
const Source& source,
sem::Type* type,
const sem::Type* type,
int32_t value)
: Base(program_id, source, type, static_cast<uint32_t>(value)) {}

View File

@ -32,7 +32,7 @@ class SintLiteral : public Castable<SintLiteral, IntLiteral> {
/// @param value the signed int literals value
SintLiteral(ProgramID program_id,
const Source& source,
sem::Type* type,
const sem::Type* type,
int32_t value);
~SintLiteral() override;

View File

@ -23,7 +23,7 @@ namespace ast {
TypeConstructorExpression::TypeConstructorExpression(ProgramID program_id,
const Source& source,
sem::Type* type,
const sem::Type* type,
ExpressionList values)
: Base(program_id, source), type_(type), values_(std::move(values)) {
TINT_ASSERT(type_);

View File

@ -33,14 +33,15 @@ class TypeConstructorExpression
/// @param values the constructor values
TypeConstructorExpression(ProgramID program_id,
const Source& source,
sem::Type* type,
const sem::Type* type,
ExpressionList values);
/// Move constructor
TypeConstructorExpression(TypeConstructorExpression&&);
~TypeConstructorExpression() override;
/// @returns the type
sem::Type* type() const { return type_; }
sem::Type* type() const { return const_cast<sem::Type*>(type_); }
/// @returns the values
const ExpressionList& values() const { return values_; }
@ -61,7 +62,7 @@ class TypeConstructorExpression
private:
TypeConstructorExpression(const TypeConstructorExpression&) = delete;
sem::Type* const type_;
const sem::Type* const type_;
ExpressionList const values_;
};

View File

@ -23,7 +23,7 @@ namespace ast {
UintLiteral::UintLiteral(ProgramID program_id,
const Source& source,
sem::Type* type,
const sem::Type* type,
uint32_t value)
: Base(program_id, source, type, value) {}

View File

@ -32,7 +32,7 @@ class UintLiteral : public Castable<UintLiteral, IntLiteral> {
/// @param value the uint literals value
UintLiteral(ProgramID program_id,
const Source& source,
sem::Type* type,
const sem::Type* type,
uint32_t value);
~UintLiteral() override;

View File

@ -27,7 +27,7 @@ Variable::Variable(ProgramID program_id,
const Source& source,
const Symbol& sym,
StorageClass declared_storage_class,
sem::Type* declared_type,
const sem::Type* declared_type,
bool is_const,
Expression* constructor,
DecorationList decorations)

View File

@ -103,7 +103,7 @@ class Variable : public Castable<Variable, Node> {
const Source& source,
const Symbol& sym,
StorageClass declared_storage_class,
sem::Type* declared_type,
const sem::Type* declared_type,
bool is_const,
Expression* constructor,
DecorationList decorations);
@ -116,7 +116,9 @@ class Variable : public Castable<Variable, Node> {
const Symbol& symbol() const { return symbol_; }
/// @returns the declared type
sem::Type* declared_type() const { return declared_type_; }
sem::Type* declared_type() const {
return const_cast<sem::Type*>(declared_type_);
}
/// @returns the declared storage class
StorageClass declared_storage_class() const {
@ -175,7 +177,7 @@ class Variable : public Castable<Variable, Node> {
Symbol const symbol_;
// The value type if a const or formal paramter, and the store type if a var
sem::Type* const declared_type_;
const sem::Type* const declared_type_;
bool const is_const_;
Expression* const constructor_;
DecorationList const decorations_;

View File

@ -21,7 +21,8 @@ TINT_INSTANTIATE_TYPEINFO(tint::sem::AccessControl);
namespace tint {
namespace sem {
AccessControl::AccessControl(ast::AccessControl::Access access, Type* subtype)
AccessControl::AccessControl(ast::AccessControl::Access access,
const Type* subtype)
: access_(access), subtype_(subtype) {
TINT_ASSERT(subtype_);
TINT_ASSERT(!subtype_->Is<AccessControl>());

View File

@ -29,7 +29,7 @@ class AccessControl : public Castable<AccessControl, Type> {
/// Constructor
/// @param access the access control setting
/// @param subtype the access controlled type
AccessControl(ast::AccessControl::Access access, Type* subtype);
AccessControl(ast::AccessControl::Access access, const Type* subtype);
/// Move constructor
AccessControl(AccessControl&&);
~AccessControl() override;
@ -44,7 +44,7 @@ class AccessControl : public Castable<AccessControl, Type> {
/// @returns the access control value
ast::AccessControl::Access access_control() const { return access_; }
/// @returns the subtype type
Type* type() const { return subtype_; }
Type* type() const { return const_cast<Type*>(subtype_); }
/// @returns the name for this type
std::string type_name() const override;
@ -61,7 +61,7 @@ class AccessControl : public Castable<AccessControl, Type> {
private:
ast::AccessControl::Access const access_;
Type* const subtype_;
const Type* const subtype_;
};
} // namespace sem

View File

@ -21,7 +21,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::sem::Alias);
namespace tint {
namespace sem {
Alias::Alias(const Symbol& sym, Type* subtype)
Alias::Alias(const Symbol& sym, const Type* subtype)
: symbol_(sym), subtype_(subtype) {
TINT_ASSERT(subtype_);
}

View File

@ -28,7 +28,7 @@ class Alias : public Castable<Alias, Type> {
/// Constructor
/// @param sym the symbol for the alias
/// @param subtype the alias'd type
Alias(const Symbol& sym, Type* subtype);
Alias(const Symbol& sym, const Type* subtype);
/// Move constructor
Alias(Alias&&);
/// Destructor
@ -37,7 +37,7 @@ class Alias : public Castable<Alias, Type> {
/// @returns the alias symbol
Symbol symbol() const { return symbol_; }
/// @returns the alias type
Type* type() const { return subtype_; }
Type* type() const { return const_cast<Type*>(subtype_); }
/// @returns the type_name for this type
std::string type_name() const override;
@ -54,7 +54,7 @@ class Alias : public Castable<Alias, Type> {
private:
Symbol const symbol_;
Type* const subtype_;
const Type* const subtype_;
};
} // namespace sem

View File

@ -20,7 +20,7 @@ namespace tint {
namespace sem {
Expression::Expression(ast::Expression* declaration,
sem::Type* type,
const sem::Type* type,
Statement* statement)
: declaration_(declaration),
type_(type->UnwrapIfNeeded()),

View File

@ -32,11 +32,11 @@ class Expression : public Castable<Expression, Node> {
/// @param type the resolved type of the expression
/// @param statement the statement that owns this expression
Expression(ast::Expression* declaration,
sem::Type* type,
const sem::Type* type,
Statement* statement);
/// @return the resolved type of the expression
sem::Type* Type() const { return type_; }
sem::Type* Type() const { return const_cast<sem::Type*>(type_); }
/// @return the statement that owns this expression
Statement* Stmt() const { return statement_; }
@ -46,7 +46,7 @@ class Expression : public Castable<Expression, Node> {
private:
ast::Expression* declaration_;
sem::Type* const type_;
const sem::Type* const type_;
Statement* const statement_;
};

View File

@ -26,7 +26,7 @@ namespace sem {
MemberAccessorExpression::MemberAccessorExpression(
ast::MemberAccessorExpression* declaration,
sem::Type* type,
const sem::Type* type,
Statement* statement)
: Base(declaration, type, statement) {}
@ -34,7 +34,7 @@ MemberAccessorExpression::~MemberAccessorExpression() = default;
StructMemberAccess::StructMemberAccess(
ast::MemberAccessorExpression* declaration,
sem::Type* type,
const sem::Type* type,
Statement* statement,
const StructMember* member)
: Base(declaration, type, statement), member_(member) {}
@ -42,7 +42,7 @@ StructMemberAccess::StructMemberAccess(
StructMemberAccess::~StructMemberAccess() = default;
Swizzle::Swizzle(ast::MemberAccessorExpression* declaration,
sem::Type* type,
const sem::Type* type,
Statement* statement,
std::vector<uint32_t> indices)
: Base(declaration, type, statement), indices_(std::move(indices)) {}

View File

@ -42,7 +42,7 @@ class MemberAccessorExpression
/// @param type the resolved type of the expression
/// @param statement the statement that owns this expression
MemberAccessorExpression(ast::MemberAccessorExpression* declaration,
sem::Type* type,
const sem::Type* type,
Statement* statement);
/// Destructor
@ -61,7 +61,7 @@ class StructMemberAccess
/// @param statement the statement that owns this expression
/// @param member the structure member
StructMemberAccess(ast::MemberAccessorExpression* declaration,
sem::Type* type,
const sem::Type* type,
Statement* statement,
const StructMember* member);
@ -85,7 +85,7 @@ class Swizzle : public Castable<Swizzle, MemberAccessorExpression> {
/// @param statement the statement that
/// @param indices the swizzle indices
Swizzle(ast::MemberAccessorExpression* declaration,
sem::Type* type,
const sem::Type* type,
Statement* statement,
std::vector<uint32_t> indices);

View File

@ -21,7 +21,8 @@ TINT_INSTANTIATE_TYPEINFO(tint::sem::MultisampledTexture);
namespace tint {
namespace sem {
MultisampledTexture::MultisampledTexture(ast::TextureDimension dim, Type* type)
MultisampledTexture::MultisampledTexture(ast::TextureDimension dim,
const Type* type)
: Base(dim), type_(type) {
TINT_ASSERT(type_);
}

View File

@ -28,13 +28,13 @@ class MultisampledTexture : public Castable<MultisampledTexture, Texture> {
/// Constructor
/// @param dim the dimensionality of the texture
/// @param type the data type of the multisampled texture
MultisampledTexture(ast::TextureDimension dim, Type* type);
MultisampledTexture(ast::TextureDimension dim, const Type* type);
/// Move constructor
MultisampledTexture(MultisampledTexture&&);
~MultisampledTexture() override;
/// @returns the subtype of the sampled texture
Type* type() const { return type_; }
Type* type() const { return const_cast<Type*>(type_); }
/// @returns the name for this type
std::string type_name() const override;
@ -50,7 +50,7 @@ class MultisampledTexture : public Castable<MultisampledTexture, Texture> {
MultisampledTexture* Clone(CloneContext* ctx) const override;
private:
Type* const type_;
const Type* const type_;
};
} // namespace sem

View File

@ -21,7 +21,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::sem::SampledTexture);
namespace tint {
namespace sem {
SampledTexture::SampledTexture(ast::TextureDimension dim, Type* type)
SampledTexture::SampledTexture(ast::TextureDimension dim, const Type* type)
: Base(dim), type_(type) {
TINT_ASSERT(type_);
}

View File

@ -28,13 +28,13 @@ class SampledTexture : public Castable<SampledTexture, Texture> {
/// Constructor
/// @param dim the dimensionality of the texture
/// @param type the data type of the sampled texture
SampledTexture(ast::TextureDimension dim, Type* type);
SampledTexture(ast::TextureDimension dim, const Type* type);
/// Move constructor
SampledTexture(SampledTexture&&);
~SampledTexture() override;
/// @returns the subtype of the sampled texture
Type* type() const { return type_; }
Type* type() const { return const_cast<Type*>(type_); }
/// @returns the name for this type
std::string type_name() const override;
@ -50,7 +50,7 @@ class SampledTexture : public Castable<SampledTexture, Texture> {
SampledTexture* Clone(CloneContext* ctx) const override;
private:
Type* const type_;
const Type* const type_;
};
} // namespace sem

View File

@ -48,10 +48,21 @@ class Type : public Castable<Type, ShareableCloneable> {
/// @returns the pointee type if this is a pointer, `this` otherwise
Type* UnwrapPtrIfNeeded();
/// @returns the pointee type if this is a pointer, `this` otherwise
const Type* UnwrapPtrIfNeeded() const {
return const_cast<Type*>(this)->UnwrapPtrIfNeeded();
}
/// @returns the most deeply nested aliased type if this is an alias, `this`
/// otherwise
Type* UnwrapAliasIfNeeded();
/// @returns the most deeply nested aliased type if this is an alias, `this`
/// otherwise
const Type* UnwrapAliasIfNeeded() const {
return const_cast<Type*>(this)->UnwrapAliasIfNeeded();
}
/// Removes all levels of aliasing and access control.
/// This is just enough to assist with WGSL translation
/// in that you want see through one level of pointer to get from an
@ -60,6 +71,16 @@ class Type : public Castable<Type, ShareableCloneable> {
/// @returns the completely unaliased type.
Type* UnwrapIfNeeded();
/// Removes all levels of aliasing and access control.
/// This is just enough to assist with WGSL translation
/// in that you want see through one level of pointer to get from an
/// identifier-like expression as an l-value to its corresponding r-value,
/// plus see through the wrappers on either side.
/// @returns the completely unaliased type.
const Type* UnwrapIfNeeded() const {
return const_cast<Type*>(this)->UnwrapIfNeeded();
}
/// Returns the type found after:
/// - removing all layers of aliasing and access control if they exist, then
/// - removing the pointer, if it exists, then
@ -67,6 +88,13 @@ class Type : public Castable<Type, ShareableCloneable> {
/// @returns the unwrapped type
Type* UnwrapAll();
/// Returns the type found after:
/// - removing all layers of aliasing and access control if they exist, then
/// - removing the pointer, if it exists, then
/// - removing all further layers of aliasing or access control, if they exist
/// @returns the unwrapped type
const Type* UnwrapAll() const { return const_cast<Type*>(this)->UnwrapAll(); }
/// @returns true if this type is a scalar
bool is_scalar() const;
/// @returns true if this type is a numeric scalar

View File

@ -24,7 +24,7 @@ namespace tint {
namespace sem {
Variable::Variable(const ast::Variable* declaration,
sem::Type* type,
const sem::Type* type,
ast::StorageClass storage_class)
: declaration_(declaration), type_(type), storage_class_(storage_class) {}
@ -35,7 +35,7 @@ sem::Type* Variable::DeclaredType() const {
}
VariableUser::VariableUser(ast::IdentifierExpression* declaration,
sem::Type* type,
const sem::Type* type,
Statement* statement,
sem::Variable* variable)
: Base(declaration, type, statement), variable_(variable) {}

View File

@ -42,7 +42,7 @@ class Variable : public Castable<Variable, Node> {
/// @param type the variable type
/// @param storage_class the variable storage class
Variable(const ast::Variable* declaration,
sem::Type* type,
const sem::Type* type,
ast::StorageClass storage_class);
/// Destructor
@ -52,7 +52,7 @@ class Variable : public Castable<Variable, Node> {
const ast::Variable* Declaration() const { return declaration_; }
/// @returns the canonical type for the variable
sem::Type* Type() const { return type_; }
sem::Type* Type() const { return const_cast<sem::Type*>(type_); }
/// @returns the AST node's type. May be nullptr.
sem::Type* DeclaredType() const;
@ -68,7 +68,7 @@ class Variable : public Castable<Variable, Node> {
private:
const ast::Variable* const declaration_;
sem::Type* const type_;
const sem::Type* const type_;
ast::StorageClass const storage_class_;
std::vector<const VariableUser*> users_;
};
@ -83,7 +83,7 @@ class VariableUser : public Castable<VariableUser, Expression> {
/// @param statement the statement that owns this expression
/// @param variable the semantic variable
VariableUser(ast::IdentifierExpression* declaration,
sem::Type* type,
const sem::Type* type,
Statement* statement,
sem::Variable* variable);