xref: /aosp_15_r20/external/skia/src/sksl/ir/SkSLConstructorArrayCast.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SKSL_CONSTRUCTOR_ARRAY_CAST
9 #define SKSL_CONSTRUCTOR_ARRAY_CAST
10 
11 #include "src/sksl/SkSLPosition.h"
12 #include "src/sksl/ir/SkSLConstructor.h"
13 #include "src/sksl/ir/SkSLExpression.h"
14 #include "src/sksl/ir/SkSLIRNode.h"
15 
16 #include <memory>
17 #include <utility>
18 
19 namespace SkSL {
20 
21 class Context;
22 class Type;
23 
24 /**
25  * Represents the typecasting of an array. Arrays cannot be directly casted in SkSL (or GLSL), but
26  * type narrowing can cause an array to be implicitly casted. For instance, the expression
27  * `myHalf2Array == float[2](a, b)` should be allowed when narrowing conversions are enabled; this
28  * constructor allows the necessary array-type conversion to be represented in IR.
29  *
30  * These always contain exactly 1 array of matching size, and are never constant.
31  */
32 class ConstructorArrayCast final : public SingleArgumentConstructor {
33 public:
34     inline static constexpr Kind kIRNodeKind = Kind::kConstructorArrayCast;
35 
ConstructorArrayCast(Position pos,const Type & type,std::unique_ptr<Expression> arg)36     ConstructorArrayCast(Position pos, const Type& type, std::unique_ptr<Expression> arg)
37         : INHERITED(pos, kIRNodeKind, &type, std::move(arg)) {}
38 
39     static std::unique_ptr<Expression> Make(const Context& context,
40                                             Position pos,
41                                             const Type& type,
42                                             std::unique_ptr<Expression> arg);
43 
clone(Position pos)44     std::unique_ptr<Expression> clone(Position pos) const override {
45         return std::make_unique<ConstructorArrayCast>(pos, this->type(), argument()->clone());
46     }
47 
48 private:
49     using INHERITED = SingleArgumentConstructor;
50 };
51 
52 }  // namespace SkSL
53 
54 #endif
55