From 2a11d830bb8a4965758cab787196322de4f24e19 Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Tue, 25 Oct 2022 05:38:44 -1000 Subject: [PATCH] refactor: factor out new `Validator::global_var_ty` for expr. validation (#2086) --- src/valid/expression.rs | 131 ++++++++++------------------------------ 1 file changed, 31 insertions(+), 100 deletions(-) diff --git a/src/valid/expression.rs b/src/valid/expression.rs index 0f173707f3..eb95c0e095 100644 --- a/src/valid/expression.rs +++ b/src/valid/expression.rs @@ -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)), @@ -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, @@ -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 { @@ -1518,4 +1422,31 @@ impl super::Validator { }; Ok(stages) } + + fn global_var_ty( + module: &crate::Module, + function: &crate::Function, + expr: Handle, + ) -> Result, 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), + } + } }