Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Validate Compose, Unary, Binary, Select, and Relational #599

Merged
merged 3 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/front/wgsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2575,7 +2575,7 @@ impl Parser {
lexer.expect(Token::Separator(';'))?;
}
(Token::Word("const"), _) => {
let (name, _ty, _access) = self.parse_variable_ident_decl(
let (name, explicit_ty, _access) = self.parse_variable_ident_decl(
lexer,
&mut module.types,
&mut module.constants,
Expand All @@ -2589,6 +2589,20 @@ impl Parser {
&mut module.types,
&mut module.constants,
)?;
let con = &module.constants[const_handle];
let type_match = match con.inner {
crate::ConstantInner::Scalar { width, value } => {
module.types[explicit_ty].inner
== crate::TypeInner::Scalar {
kind: value.scalar_kind(),
width,
}
}
crate::ConstantInner::Composite { ty, components: _ } => ty == explicit_ty,
};
if !type_match {
return Err(Error::ConstTypeMismatch(name, explicit_ty));
}
//TODO: check `ty` against `const_handle`.
lexer.expect(Token::Separator(';'))?;
lookup_global_expression.insert(name, crate::Expression::Constant(const_handle));
Expand Down
18 changes: 3 additions & 15 deletions src/proc/layouter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,9 @@ impl Layouter {
},
Ti::Array { base, size, stride } => {
let count = match size {
crate::ArraySize::Constant(handle) => match constants[handle].inner {
crate::ConstantInner::Scalar {
width: _,
value: crate::ScalarValue::Uint(value),
} => value as u32,
// Accept a signed integer size to avoid
// requiring an explicit uint
// literal. Type inference should make
// this unnecessary.
crate::ConstantInner::Scalar {
width: _,
value: crate::ScalarValue::Sint(value),
} => value as u32,
ref other => unreachable!("Unexpected array size {:?}", other),
},
crate::ArraySize::Constant(handle) => {
constants[handle].to_array_length().unwrap()
}
// A dynamically-sized array has to have at least one element
crate::ArraySize::Dynamic => 1,
};
Expand Down
19 changes: 19 additions & 0 deletions src/proc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,22 @@ impl crate::SampleLevel {
}
}
}

impl crate::Constant {
pub fn to_array_length(&self) -> Option<u32> {
use std::convert::TryInto;
match self.inner {
crate::ConstantInner::Scalar { value, width: _ } => match value {
crate::ScalarValue::Uint(value) => value.try_into().ok(),
// Accept a signed integer size to avoid
// requiring an explicit uint
// literal. Type inference should make
// this unnecessary.
crate::ScalarValue::Sint(value) => value.try_into().ok(),
_ => None,
},
// caught by type validation
crate::ConstantInner::Composite { .. } => None,
}
}
}
Loading