Skip to content

Commit

Permalink
refactor: factor out new Validator::global_var_ty for expr. validat…
Browse files Browse the repository at this point in the history
…ion (#2086)
  • Loading branch information
ErichDonGubler authored Oct 25, 2022
1 parent ddcd5d3 commit 2a11d83
Showing 1 changed file with 31 additions and 100 deletions.
131 changes: 31 additions & 100 deletions src/valid/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,57 +346,9 @@ impl super::Validator {
depth_ref,
} => {
// check the validity of expressions
let image_ty = match function.expressions[image] {
crate::Expression::GlobalVariable(var_handle) => {
module.global_variables[var_handle].ty
}
crate::Expression::FunctionArgument(i) => function.arguments[i as usize].ty,
crate::Expression::Access { base, .. }
| crate::Expression::AccessIndex { base, .. } => {
match function.expressions[base] {
crate::Expression::GlobalVariable(var_handle) => {
let array_ty = module.global_variables[var_handle].ty;

match module.types[array_ty].inner {
Ti::BindingArray { base, .. } => base,
_ => {
return Err(ExpressionError::ExpectedBindingArrayType(
array_ty,
))
}
}
}
_ => return Err(ExpressionError::ExpectedGlobalVariable),
}
}
_ => return Err(ExpressionError::ExpectedGlobalVariable),
};
let image_ty = Self::global_var_ty(module, function, image)?;
let sampler_ty = Self::global_var_ty(module, function, sampler)?;

let sampler_ty = match function.expressions[sampler] {
crate::Expression::GlobalVariable(var_handle) => {
module.global_variables[var_handle].ty
}
crate::Expression::FunctionArgument(i) => function.arguments[i as usize].ty,
crate::Expression::Access { base, .. }
| crate::Expression::AccessIndex { base, .. } => {
match function.expressions[base] {
crate::Expression::GlobalVariable(var_handle) => {
let array_ty = module.global_variables[var_handle].ty;

match module.types[array_ty].inner {
Ti::BindingArray { base, .. } => base,
_ => {
return Err(ExpressionError::ExpectedBindingArrayType(
array_ty,
))
}
}
}
_ => return Err(ExpressionError::ExpectedGlobalVariable),
}
}
_ => return Err(ExpressionError::ExpectedGlobalVariable),
};
let comparison = match module.types[sampler_ty].inner {
Ti::Sampler { comparison } => comparison,
_ => return Err(ExpressionError::ExpectedSamplerType(sampler_ty)),
Expand Down Expand Up @@ -579,31 +531,7 @@ impl super::Validator {
sample,
level,
} => {
let ty = match function.expressions[image] {
crate::Expression::GlobalVariable(var_handle) => {
module.global_variables[var_handle].ty
}
crate::Expression::FunctionArgument(i) => function.arguments[i as usize].ty,
crate::Expression::Access { base, .. }
| crate::Expression::AccessIndex { base, .. } => {
match function.expressions[base] {
crate::Expression::GlobalVariable(var_handle) => {
let array_ty = module.global_variables[var_handle].ty;

match module.types[array_ty].inner {
Ti::BindingArray { base, .. } => base,
_ => {
return Err(ExpressionError::ExpectedBindingArrayType(
array_ty,
))
}
}
}
_ => return Err(ExpressionError::ExpectedGlobalVariable),
}
}
_ => return Err(ExpressionError::ExpectedGlobalVariable),
};
let ty = Self::global_var_ty(module, function, image)?;
match module.types[ty].inner {
Ti::Image {
class,
Expand Down Expand Up @@ -662,31 +590,7 @@ impl super::Validator {
ShaderStages::all()
}
E::ImageQuery { image, query } => {
let ty = match function.expressions[image] {
crate::Expression::GlobalVariable(var_handle) => {
module.global_variables[var_handle].ty
}
crate::Expression::FunctionArgument(i) => function.arguments[i as usize].ty,
crate::Expression::Access { base, .. }
| crate::Expression::AccessIndex { base, .. } => {
match function.expressions[base] {
crate::Expression::GlobalVariable(var_handle) => {
let array_ty = module.global_variables[var_handle].ty;

match module.types[array_ty].inner {
Ti::BindingArray { base, .. } => base,
_ => {
return Err(ExpressionError::ExpectedBindingArrayType(
array_ty,
))
}
}
}
_ => return Err(ExpressionError::ExpectedGlobalVariable),
}
}
_ => return Err(ExpressionError::ExpectedGlobalVariable),
};
let ty = Self::global_var_ty(module, function, image)?;
match module.types[ty].inner {
Ti::Image { class, arrayed, .. } => {
let good = match query {
Expand Down Expand Up @@ -1518,4 +1422,31 @@ impl super::Validator {
};
Ok(stages)
}

fn global_var_ty(
module: &crate::Module,
function: &crate::Function,
expr: Handle<crate::Expression>,
) -> Result<Handle<crate::Type>, ExpressionError> {
use crate::Expression as Ex;

match function.expressions[expr] {
Ex::GlobalVariable(var_handle) => Ok(module.global_variables[var_handle].ty),
Ex::FunctionArgument(i) => Ok(function.arguments[i as usize].ty),
Ex::Access { base, .. } | Ex::AccessIndex { base, .. } => {
match function.expressions[base] {
Ex::GlobalVariable(var_handle) => {
let array_ty = module.global_variables[var_handle].ty;

match module.types[array_ty].inner {
crate::TypeInner::BindingArray { base, .. } => Ok(base),
_ => Err(ExpressionError::ExpectedBindingArrayType(array_ty)),
}
}
_ => Err(ExpressionError::ExpectedGlobalVariable),
}
}
_ => Err(ExpressionError::ExpectedGlobalVariable),
}
}
}

0 comments on commit 2a11d83

Please sign in to comment.