From 34cc6405dd50d61f272dbaf2b8d93457c9161ad5 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 19 Aug 2022 00:13:47 +0900 Subject: [PATCH] internal/shader: add type checks for the builtin function clamp Updates #2184 --- internal/shader/expr.go | 25 +++++++++++++++++ internal/shader/syntax_test.go | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/internal/shader/expr.go b/internal/shader/expr.go index 7611def3e..f28fb9fe4 100644 --- a/internal/shader/expr.go +++ b/internal/shader/expr.go @@ -453,6 +453,31 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar }) return nil, nil, stmts, true + case shaderir.Clamp: + // 3 arguments + if len(args) != 3 { + cs.addError(e.Pos(), fmt.Sprintf("number of %s's arguments must be 3 but %d", callee.BuiltinFunc, len(args))) + return nil, nil, nil, false + } + for i := range args { + // If the argument is a non-typed constant value, treat this as a float value (#1874). + if args[i].Type == shaderir.NumberExpr && args[i].ConstType == shaderir.ConstTypeNone && gconstant.ToFloat(args[i].Const).Kind() != gconstant.Unknown { + args[i].Const = gconstant.ToFloat(args[i].Const) + args[i].ConstType = shaderir.ConstTypeFloat + argts[i] = shaderir.Type{Main: shaderir.Float} + } + if argts[i].Main != shaderir.Float && argts[i].Main != shaderir.Vec2 && argts[i].Main != shaderir.Vec3 && argts[i].Main != shaderir.Vec4 { + cs.addError(e.Pos(), fmt.Sprintf("cannot use %s as float, vec2, vec3, or vec4 value in argument to %s", argts[i].String(), callee.BuiltinFunc)) + return nil, nil, nil, false + } + } + if (!argts[0].Equal(&argts[1]) || !argts[0].Equal(&argts[2])) && (argts[1].Main != shaderir.Float || argts[2].Main != shaderir.Float) { + cs.addError(e.Pos(), fmt.Sprintf("the second and the third arguments for %s must equal to the first argument %s or float but %s and %s", callee.BuiltinFunc, argts[0].String(), argts[1].String(), argts[2].String())) + return nil, nil, nil, false + } + + t = argts[0] + case shaderir.Atan2, shaderir.Mod, shaderir.Min, shaderir.Max, shaderir.Distance, shaderir.Dot, shaderir.Cross, shaderir.Reflect: // 2 arguments if len(args) != 2 { diff --git a/internal/shader/syntax_test.go b/internal/shader/syntax_test.go index 204d422bc..b3593483f 100644 --- a/internal/shader/syntax_test.go +++ b/internal/shader/syntax_test.go @@ -1793,6 +1793,56 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { } } +// Issue #2184 +func TestSyntaxBuiltinFuncClampType(t *testing.T) { + cases := []struct { + stmt string + err bool + }{ + {stmt: "a := clamp(); _ = a", err: true}, + {stmt: "a := clamp(1); _ = a", err: true}, + {stmt: "a := clamp(false, false); _ = a", err: true}, + {stmt: "a := clamp(1, 1); _ = a", err: true}, + {stmt: "a := clamp(false, false, false); _ = a", err: true}, + {stmt: "a := clamp(1, 1, 1); _ = a", err: false}, + {stmt: "a := clamp(1.0, 1, 1); _ = a", err: false}, + {stmt: "a := clamp(1, 1.0, 1); _ = a", err: false}, + {stmt: "a := clamp(1, 1, 1.0); _ = a", err: false}, + {stmt: "a := clamp(1, vec2(1), 1); _ = a", err: true}, + {stmt: "a := clamp(1, 1, vec2(1)); _ = a", err: true}, + {stmt: "a := clamp(1, vec2(1), vec2(1)); _ = a", err: true}, + {stmt: "a := clamp(vec2(1), 1, 1); _ = a", err: false}, + {stmt: "a := clamp(vec2(1), 1, vec2(1)); _ = a", err: true}, + {stmt: "a := clamp(vec2(1), vec2(1), 1); _ = a", err: true}, + {stmt: "a := clamp(vec2(1), vec2(1), vec2(1)); _ = a", err: false}, + {stmt: "a := clamp(vec2(1), vec2(1), vec3(1)); _ = a", err: true}, + {stmt: "a := clamp(vec3(1), 1, 1); _ = a", err: false}, + {stmt: "a := clamp(vec3(1), 1, vec3(1)); _ = a", err: true}, + {stmt: "a := clamp(vec3(1), vec3(1), 1); _ = a", err: true}, + {stmt: "a := clamp(vec3(1), vec3(1), vec3(1)); _ = a", err: false}, + {stmt: "a := clamp(vec4(1), 1, 1); _ = a", err: false}, + {stmt: "a := clamp(vec4(1), 1, vec4(1)); _ = a", err: true}, + {stmt: "a := clamp(vec4(1), vec4(1), 1); _ = a", err: true}, + {stmt: "a := clamp(vec4(1), vec4(1), vec4(1)); _ = a", err: false}, + } + + for _, c := range cases { + stmt := c.stmt + src := fmt.Sprintf(`package main + +func Fragment(position vec4, texCoord vec2, color vec4) vec4 { + %s + return position +}`, stmt) + _, err := compileToIR([]byte(src)) + if err == nil && c.err { + t.Errorf("%s must return an error but does not", stmt) + } else if err != nil && !c.err { + t.Errorf("%s must not return nil but returned %v", stmt, err) + } + } +} + // Issue #2184 func TestSyntaxBuiltinFuncCrossType(t *testing.T) { cases := []struct {