Make all ast and sem pointers const

And remove a whole load of const_cast hackery.

Semantic nodes may contain internally mutable fields (although only ever modified during resolving), so these are always passed by `const` pointer.

While all AST nodes are internally immutable, we have decided that pointers to AST nodes should also be marked `const`, for consistency.

There's still a collection of const_cast calls in the Resolver. These will be fixed up in a later change.

Bug: tint:745
Change-Id: I046309b8e586772605fc0fe6b2d27f28806d40ef
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/66606
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton
2021-10-19 18:38:54 +00:00
committed by Tint LUCI CQ
parent 7d0fc07b20
commit 8648120bbe
261 changed files with 2441 additions and 2258 deletions

View File

@@ -99,12 +99,12 @@ class Array : public Castable<Array, Type> {
private:
Type const* const element_;
uint32_t const count_;
uint32_t const align_;
uint32_t const size_;
uint32_t const stride_;
uint32_t const implicit_stride_;
bool const constructible_;
const uint32_t count_;
const uint32_t align_;
const uint32_t size_;
const uint32_t stride_;
const uint32_t implicit_stride_;
const bool constructible_;
};
} // namespace sem

View File

@@ -35,7 +35,7 @@ const ast::BlockStatement* BlockStatement::Declaration() const {
return Base::Declaration()->As<ast::BlockStatement>();
}
void BlockStatement::AddDecl(ast::Variable* var) {
void BlockStatement::AddDecl(const ast::Variable* var) {
decls_.push_back(var);
}

View File

@@ -55,7 +55,7 @@ class BlockStatement : public Castable<BlockStatement, CompoundStatement> {
/// Associates a declaration with this block.
/// @param var a variable declaration to be added to the block
void AddDecl(ast::Variable* var);
void AddDecl(const ast::Variable* var);
private:
std::vector<const ast::Variable*> decls_;

View File

@@ -22,7 +22,8 @@ TINT_INSTANTIATE_TYPEINFO(tint::sem::CallTarget);
namespace tint {
namespace sem {
CallTarget::CallTarget(sem::Type* return_type, const ParameterList& parameters)
CallTarget::CallTarget(const sem::Type* return_type,
const ParameterList& parameters)
: signature_{return_type, parameters} {
TINT_ASSERT(Semantic, return_type);
}
@@ -30,7 +31,7 @@ CallTarget::CallTarget(sem::Type* return_type, const ParameterList& parameters)
CallTarget::CallTarget(const CallTarget&) = default;
CallTarget::~CallTarget() = default;
CallTargetSignature::CallTargetSignature(sem::Type* ret_ty,
CallTargetSignature::CallTargetSignature(const sem::Type* ret_ty,
const ParameterList& params)
: return_type(ret_ty), parameters(params) {}
CallTargetSignature::CallTargetSignature(const CallTargetSignature&) = default;

View File

@@ -32,7 +32,7 @@ struct CallTargetSignature {
/// Constructor
/// @param ret_ty the call target return type
/// @param params the call target parameters
CallTargetSignature(sem::Type* ret_ty, const ParameterList& params);
CallTargetSignature(const sem::Type* ret_ty, const ParameterList& params);
/// Copy constructor
CallTargetSignature(const CallTargetSignature&);
@@ -41,9 +41,9 @@ struct CallTargetSignature {
~CallTargetSignature();
/// The type of the call target return value
sem::Type* const return_type = nullptr;
const sem::Type* const return_type = nullptr;
/// The parameters of the call target
ParameterList const parameters;
const ParameterList parameters;
/// Equality operator
/// @param other the signature to compare this to
@@ -62,7 +62,7 @@ class CallTarget : public Castable<CallTarget, Node> {
/// Constructor
/// @param return_type the return type of the call target
/// @param parameters the parameters for the call target
CallTarget(sem::Type* return_type, const ParameterList& parameters);
CallTarget(const sem::Type* return_type, const ParameterList& parameters);
/// Copy constructor
CallTarget(const CallTarget&);
@@ -71,7 +71,7 @@ class CallTarget : public Castable<CallTarget, Node> {
~CallTarget() override;
/// @return the return type of the call target
sem::Type* ReturnType() const { return signature_.return_type; }
const sem::Type* ReturnType() const { return signature_.return_type; }
/// @return the parameters of the call target
const ParameterList& Parameters() const { return signature_.parameters; }

View File

@@ -23,7 +23,7 @@ namespace sem {
Expression::Expression(const ast::Expression* declaration,
const sem::Type* type,
Statement* statement,
const Statement* statement,
Constant constant)
: declaration_(declaration),
type_(type),

View File

@@ -35,31 +35,29 @@ class Expression : public Castable<Expression, Node> {
/// @param constant the constant value of the expression. May be invalid
Expression(const ast::Expression* declaration,
const sem::Type* type,
Statement* statement,
const Statement* statement,
Constant constant);
/// Destructor
~Expression() override;
/// @return the resolved type of the expression
sem::Type* Type() const { return const_cast<sem::Type*>(type_); }
const sem::Type* Type() const { return type_; }
/// @return the statement that owns this expression
Statement* Stmt() const { return statement_; }
const Statement* Stmt() const { return statement_; }
/// @return the constant value of this expression
const Constant& ConstantValue() const { return constant_; }
/// @returns the AST node
ast::Expression* Declaration() const {
return const_cast<ast::Expression*>(declaration_);
}
const ast::Expression* Declaration() const { return declaration_; }
private:
const ast::Expression* declaration_;
const ast::Expression* const declaration_;
const sem::Type* const type_;
Statement* const statement_;
Constant const constant_;
const Statement* const statement_;
const Constant constant_;
};
} // namespace sem

View File

@@ -28,7 +28,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::sem::Function);
namespace tint {
namespace sem {
Function::Function(ast::Function* declaration,
Function::Function(const ast::Function* declaration,
Type* return_type,
std::vector<Parameter*> parameters,
std::vector<const Variable*> referenced_module_vars,
@@ -52,9 +52,9 @@ Function::Function(ast::Function* declaration,
Function::~Function() = default;
std::vector<std::pair<const Variable*, ast::LocationDecoration*>>
std::vector<std::pair<const Variable*, const ast::LocationDecoration*>>
Function::ReferencedLocationVariables() const {
std::vector<std::pair<const Variable*, ast::LocationDecoration*>> ret;
std::vector<std::pair<const Variable*, const ast::LocationDecoration*>> ret;
for (auto* var : ReferencedModuleVariables()) {
for (auto* deco : var->Declaration()->decorations) {
@@ -97,9 +97,9 @@ Function::VariableBindings Function::ReferencedStorageBufferVariables() const {
return ret;
}
std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>>
std::vector<std::pair<const Variable*, const ast::BuiltinDecoration*>>
Function::ReferencedBuiltinVariables() const {
std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>> ret;
std::vector<std::pair<const Variable*, const ast::BuiltinDecoration*>> ret;
for (auto* var : ReferencedModuleVariables()) {
for (auto* deco : var->Declaration()->decorations) {

View File

@@ -66,7 +66,7 @@ class Function : public Castable<Function, CallTarget> {
/// @param callsites the callsites of the function
/// @param ancestor_entry_points the ancestor entry points
/// @param workgroup_size the workgroup size
Function(ast::Function* declaration,
Function(const ast::Function* declaration,
Type* return_type,
std::vector<Parameter*> parameters,
std::vector<const Variable*> referenced_module_vars,
@@ -80,7 +80,7 @@ class Function : public Castable<Function, CallTarget> {
~Function() override;
/// @returns the ast::Function declaration
ast::Function* Declaration() const { return declaration_; }
const ast::Function* Declaration() const { return declaration_; }
/// Note: If this function calls other functions, the return will also include
/// all of the referenced variables from the callees.
@@ -106,12 +106,12 @@ class Function : public Castable<Function, CallTarget> {
}
/// Retrieves any referenced location variables
/// @returns the <variable, decoration> pair.
std::vector<std::pair<const Variable*, ast::LocationDecoration*>>
std::vector<std::pair<const Variable*, const ast::LocationDecoration*>>
ReferencedLocationVariables() const;
/// Retrieves any referenced builtin variables
/// @returns the <variable, decoration> pair.
std::vector<std::pair<const Variable*, ast::BuiltinDecoration*>>
std::vector<std::pair<const Variable*, const ast::BuiltinDecoration*>>
ReferencedBuiltinVariables() const;
/// Retrieves any referenced uniform variables. Note, the variables must be
@@ -174,7 +174,7 @@ class Function : public Castable<Function, CallTarget> {
VariableBindings ReferencedSampledTextureVariablesImpl(
bool multisampled) const;
ast::Function* const declaration_;
const ast::Function* const declaration_;
std::vector<const Variable*> const referenced_module_vars_;
std::vector<const Variable*> const local_referenced_module_vars_;
std::vector<const ast::ReturnStatement*> const return_statements_;

View File

@@ -102,7 +102,7 @@ bool IsAtomicIntrinsic(IntrinsicType i) {
}
Intrinsic::Intrinsic(IntrinsicType type,
sem::Type* return_type,
const sem::Type* return_type,
std::vector<Parameter*> parameters,
PipelineStageSet supported_stages,
bool is_deprecated)

View File

@@ -88,7 +88,7 @@ class Intrinsic : public Castable<Intrinsic, CallTarget> {
/// @param is_deprecated true if the particular overload is considered
/// deprecated
Intrinsic(IntrinsicType type,
sem::Type* return_type,
const sem::Type* return_type,
std::vector<Parameter*> parameters,
PipelineStageSet supported_stages,
bool is_deprecated);
@@ -140,9 +140,9 @@ class Intrinsic : public Castable<Intrinsic, CallTarget> {
bool IsAtomic() const;
private:
IntrinsicType const type_;
PipelineStageSet const supported_stages_;
bool const is_deprecated_;
const IntrinsicType type_;
const PipelineStageSet supported_stages_;
const bool is_deprecated_;
};
} // namespace sem

View File

@@ -22,7 +22,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::sem::Matrix);
namespace tint {
namespace sem {
Matrix::Matrix(Vector* column_type, uint32_t columns)
Matrix::Matrix(const Vector* column_type, uint32_t columns)
: subtype_(column_type->type()),
column_type_(column_type),
rows_(column_type->Width()),

View File

@@ -31,20 +31,20 @@ class Matrix : public Castable<Matrix, Type> {
/// Constructor
/// @param column_type the type of a column of the matrix
/// @param columns the number of columns in the matrix
Matrix(Vector* column_type, uint32_t columns);
Matrix(const Vector* column_type, uint32_t columns);
/// Move constructor
Matrix(Matrix&&);
~Matrix() override;
/// @returns the type of the matrix
Type* type() const { return subtype_; }
const Type* type() const { return subtype_; }
/// @returns the number of rows in the matrix
uint32_t rows() const { return rows_; }
/// @returns the number of columns in the matrix
uint32_t columns() const { return columns_; }
/// @returns the column-vector type of the matrix
Vector* ColumnType() const { return column_type_; }
const Vector* ColumnType() const { return column_type_; }
/// @returns the name for this type
std::string type_name() const override;
@@ -69,10 +69,10 @@ class Matrix : public Castable<Matrix, Type> {
uint32_t ColumnStride() const;
private:
Type* const subtype_;
Vector* const column_type_;
uint32_t const rows_;
uint32_t const columns_;
const Type* const subtype_;
const Vector* const column_type_;
const uint32_t rows_;
const uint32_t columns_;
};
} // namespace sem

View File

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

View File

@@ -41,9 +41,9 @@ class MemberAccessorExpression
/// @param declaration the AST node
/// @param type the resolved type of the expression
/// @param statement the statement that owns this expression
MemberAccessorExpression(ast::MemberAccessorExpression* declaration,
MemberAccessorExpression(const ast::MemberAccessorExpression* declaration,
const sem::Type* type,
Statement* statement);
const Statement* statement);
/// Destructor
~MemberAccessorExpression() override;
@@ -60,9 +60,9 @@ class StructMemberAccess
/// @param type the resolved type of the expression
/// @param statement the statement that owns this expression
/// @param member the structure member
StructMemberAccess(ast::MemberAccessorExpression* declaration,
StructMemberAccess(const ast::MemberAccessorExpression* declaration,
const sem::Type* type,
Statement* statement,
const Statement* statement,
const StructMember* member);
/// Destructor
@@ -84,9 +84,9 @@ class Swizzle : public Castable<Swizzle, MemberAccessorExpression> {
/// @param type the resolved type of the expression
/// @param statement the statement that
/// @param indices the swizzle indices
Swizzle(ast::MemberAccessorExpression* declaration,
Swizzle(const ast::MemberAccessorExpression* declaration,
const sem::Type* type,
Statement* statement,
const Statement* statement,
std::vector<uint32_t> indices);
/// Destructor

View File

@@ -34,7 +34,7 @@ class MultisampledTexture : public Castable<MultisampledTexture, Texture> {
~MultisampledTexture() override;
/// @returns the subtype of the sampled texture
Type* type() const { return const_cast<Type*>(type_); }
const Type* type() const { return type_; }
/// @returns the name for this type
std::string type_name() const override;

View File

@@ -78,7 +78,7 @@ bool Struct::IsConstructible() const {
return constructible_;
}
StructMember::StructMember(ast::StructMember* declaration,
StructMember::StructMember(const ast::StructMember* declaration,
Symbol name,
sem::Type* type,
uint32_t index,

View File

@@ -161,11 +161,11 @@ class Struct : public Castable<Struct, Type> {
uint64_t LargestMemberBaseAlignment(MemoryLayout mem_layout) const;
ast::Struct const* const declaration_;
Symbol const name_;
StructMemberList const members_;
uint32_t const align_;
uint32_t const size_;
uint32_t const size_no_padding_;
const Symbol name_;
const StructMemberList members_;
const uint32_t align_;
const uint32_t size_;
const uint32_t size_no_padding_;
std::unordered_set<ast::StorageClass> storage_class_usage_;
std::unordered_set<PipelineStageUsage> pipeline_stage_uses_;
bool constructible_;
@@ -182,7 +182,7 @@ class StructMember : public Castable<StructMember, Node> {
/// @param offset the byte offset from the base of the structure
/// @param align the byte alignment of the member
/// @param size the byte size of the member
StructMember(ast::StructMember* declaration,
StructMember(const ast::StructMember* declaration,
Symbol name,
sem::Type* type,
uint32_t index,
@@ -194,7 +194,7 @@ class StructMember : public Castable<StructMember, Node> {
~StructMember() override;
/// @returns the AST declaration node
ast::StructMember* Declaration() const { return declaration_; }
const ast::StructMember* Declaration() const { return declaration_; }
/// @returns the name of the structure
Symbol Name() const { return name_; }
@@ -215,13 +215,13 @@ class StructMember : public Castable<StructMember, Node> {
uint32_t Size() const { return size_; }
private:
ast::StructMember* const declaration_;
Symbol const name_;
const ast::StructMember* const declaration_;
const Symbol name_;
sem::Type* const type_;
uint32_t const index_;
uint32_t const offset_;
uint32_t const align_;
uint32_t const size_;
const uint32_t index_;
const uint32_t offset_;
const uint32_t align_;
const uint32_t size_;
};
} // namespace sem

View File

@@ -80,7 +80,7 @@ Parameter::Parameter(const ast::Variable* declaration,
Parameter::~Parameter() = default;
VariableUser::VariableUser(ast::IdentifierExpression* declaration,
VariableUser::VariableUser(const ast::IdentifierExpression* declaration,
const sem::Type* type,
Statement* statement,
sem::Variable* variable,

View File

@@ -59,7 +59,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 const_cast<sem::Type*>(type_); }
const sem::Type* Type() const { return type_; }
/// @returns the storage class for the variable
ast::StorageClass StorageClass() const { return storage_class_; }
@@ -135,7 +135,7 @@ class GlobalVariable : public Castable<GlobalVariable, Variable> {
private:
sem::BindingPoint binding_point_;
bool const is_pipeline_constant_;
const bool is_pipeline_constant_;
uint16_t const constant_id_ = 0;
};
@@ -172,8 +172,8 @@ class Parameter : public Castable<Parameter, Variable> {
void SetOwner(CallTarget const* owner) { owner_ = owner; }
private:
uint32_t const index_;
ParameterUsage const usage_;
const uint32_t index_;
const ParameterUsage usage_;
CallTarget const* owner_;
};
@@ -190,7 +190,7 @@ class VariableUser : public Castable<VariableUser, Expression> {
/// @param statement the statement that owns this expression
/// @param variable the semantic variable
/// @param constant_value the constant value for the variable. May be invalid
VariableUser(ast::IdentifierExpression* declaration,
VariableUser(const ast::IdentifierExpression* declaration,
const sem::Type* type,
Statement* statement,
sem::Variable* variable,

View File

@@ -34,7 +34,7 @@ class Vector : public Castable<Vector, Type> {
~Vector() override;
/// @returns the type of the vector elements
Type* type() const { return const_cast<Type*>(subtype_); }
const Type* type() const { return subtype_; }
/// @returns the name for th type
std::string type_name() const override;
@@ -68,7 +68,7 @@ class Vector : public Castable<Vector, Type> {
private:
Type const* const subtype_;
uint32_t const width_;
const uint32_t width_;
};
} // namespace sem