From adf26310ac54a765ad77e236138528ebbd670ed9 Mon Sep 17 00:00:00 2001 From: Vincent Isambart Date: Sat, 5 Mar 2022 06:42:11 +0900 Subject: [PATCH] Not operator depending on expression type --- src/back/hlsl/writer.rs | 15 +++++++++++++-- src/back/msl/writer.rs | 10 ++++++++-- tests/out/hlsl/operators.hlsl | 2 +- tests/out/msl/operators.msl | 2 +- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/back/hlsl/writer.rs b/src/back/hlsl/writer.rs index 35b76f3fc4..fce74ee91f 100644 --- a/src/back/hlsl/writer.rs +++ b/src/back/hlsl/writer.rs @@ -1751,10 +1751,21 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { } } Expression::Unary { op, expr } => { + use crate::{ScalarKind as Sk, UnaryOperator as Uo}; // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-operators#unary-operators let op_str = match op { - crate::UnaryOperator::Negate => "-", - crate::UnaryOperator::Not => "!", + Uo::Negate => "-", + Uo::Not => match *func_ctx.info[expr].ty.inner_with(&module.types) { + TypeInner::Scalar { kind: Sk::Sint, .. } => "~", + TypeInner::Scalar { kind: Sk::Uint, .. } => "~", + TypeInner::Scalar { kind: Sk::Bool, .. } => "!", + ref other => { + return Err(Error::Custom(format!( + "Cannot apply not to type {:?}", + other + ))) + } + }, }; write!(self.out, "{}", op_str)?; self.write_expr(module, expr, func_ctx)?; diff --git a/src/back/msl/writer.rs b/src/back/msl/writer.rs index 4fb233a3c2..cd86d98834 100644 --- a/src/back/msl/writer.rs +++ b/src/back/msl/writer.rs @@ -1406,9 +1406,15 @@ impl Writer { } }, crate::Expression::Unary { op, expr } => { + use crate::{ScalarKind as Sk, UnaryOperator as Uo}; let op_str = match op { - crate::UnaryOperator::Negate => "-", - crate::UnaryOperator::Not => "!", + Uo::Negate => "-", + Uo::Not => match *context.resolve_type(expr) { + crate::TypeInner::Scalar { kind: Sk::Sint, .. } => "~", + crate::TypeInner::Scalar { kind: Sk::Uint, .. } => "~", + crate::TypeInner::Scalar { kind: Sk::Bool, .. } => "!", + _ => return Err(Error::Validation), + }, }; write!(self.out, "{}", op_str)?; self.put_expression(expr, context, false)?; diff --git a/tests/out/hlsl/operators.hlsl b/tests/out/hlsl/operators.hlsl index 8f060d85bd..3204dd3b1a 100644 --- a/tests/out/hlsl/operators.hlsl +++ b/tests/out/hlsl/operators.hlsl @@ -33,7 +33,7 @@ int unary() if (!true) { return 1; } else { - return !1; + return ~1; } } diff --git a/tests/out/msl/operators.msl b/tests/out/msl/operators.msl index 1d76848a2d..09871e5286 100644 --- a/tests/out/msl/operators.msl +++ b/tests/out/msl/operators.msl @@ -38,7 +38,7 @@ int unary( if (!true) { return 1; } else { - return !1; + return ~1; } }