From 2571039af0607770948b6bf569a724fd985abef2 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Sat, 30 Apr 2022 21:55:25 -0700 Subject: [PATCH] Factor out code for deciding which pointers can be arguments. --- src/valid/type.rs | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/valid/type.rs b/src/valid/type.rs index 9d77342a64..988a8fe5ba 100644 --- a/src/valid/type.rs +++ b/src/valid/type.rs @@ -156,6 +156,22 @@ fn check_member_layout( }; } +/// Determine whether a pointer in `space` can be passed as an argument. +/// +/// If a pointer in `space` is permitted to be passed as an argument to a +/// user-defined function, return `TypeFlags::ARGUMENT`. Otherwise, return +/// `TypeFlags::empty()`. +/// +/// Pointers passed as arguments to user-defined functions must be in the +/// `Function`, `Private`, or `Workgroup` storage space. +const fn ptr_space_argument_flag(space: crate::AddressSpace) -> TypeFlags { + use crate::AddressSpace as As; + match space { + As::Function | As::Private | As::WorkGroup => TypeFlags::ARGUMENT, + As::Uniform | As::Storage { .. } | As::Handle | As::PushConstant => TypeFlags::empty(), + } +} + #[derive(Clone, Debug)] pub(super) struct TypeInfo { pub flags: TypeFlags, @@ -300,20 +316,11 @@ impl super::Validator { } } - // Pointers passed as arguments to user-defined functions must - // be in the `Function`, `Private`, or `Workgroup` storage - // space. We only mark pointers in those spaces as `ARGUMENT`. - // // `Validator::validate_function` actually checks the storage // space of pointer arguments explicitly before checking the // `ARGUMENT` flag, to give better error messages. But it seems // best to set `ARGUMENT` accurately anyway. - let argument_flag = match space { - As::Function | As::Private | As::WorkGroup => TypeFlags::ARGUMENT, - As::Uniform | As::Storage { .. } | As::Handle | As::PushConstant => { - TypeFlags::empty() - } - }; + let argument_flag = ptr_space_argument_flag(space); // Pointers cannot be stored in variables, structure members, or // array elements, so we do not mark them as `DATA`. @@ -336,14 +343,14 @@ impl super::Validator { return Err(TypeError::InvalidWidth(kind, width)); } - use crate::AddressSpace as As; - let argument_flag = match space { - As::Function | As::Private | As::WorkGroup => TypeFlags::ARGUMENT, - As::Uniform | As::Storage { .. } | As::Handle | As::PushConstant => { - TypeFlags::empty() - } - }; + // `Validator::validate_function` actually checks the storage + // space of pointer arguments explicitly before checking the + // `ARGUMENT` flag, to give better error messages. But it seems + // best to set `ARGUMENT` accurately anyway. + let argument_flag = ptr_space_argument_flag(space); + // Pointers cannot be stored in variables, structure members, or + // array elements, so we do not mark them as `DATA`. TypeInfo::new(argument_flag | TypeFlags::SIZED | TypeFlags::COPY, 0) } Ti::Array { base, size, stride } => {