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_SPLAT
9 #define SKSL_CONSTRUCTOR_SPLAT
10 
11 #include "src/sksl/SkSLContext.h"
12 #include "src/sksl/ir/SkSLConstructor.h"
13 #include "src/sksl/ir/SkSLExpression.h"
14 
15 #include <memory>
16 
17 namespace SkSL {
18 
19 /**
20  * Represents the construction of a vector splat, such as `half3(n)`.
21  *
22  * These always contain exactly 1 scalar.
23  */
24 class ConstructorSplat final : public SingleArgumentConstructor {
25 public:
26     static constexpr Kind kExpressionKind = Kind::kConstructorSplat;
27 
ConstructorSplat(int offset,const Type & type,std::unique_ptr<Expression> arg)28     ConstructorSplat(int offset, const Type& type, std::unique_ptr<Expression> arg)
29         : INHERITED(offset, kExpressionKind, &type, std::move(arg)) {}
30 
31     // The input argument must be scalar. A "splat" to a scalar type will be optimized into a no-op.
32     static std::unique_ptr<Expression> Make(const Context& context,
33                                             int offset,
34                                             const Type& type,
35                                             std::unique_ptr<Expression> arg);
36 
clone()37     std::unique_ptr<Expression> clone() const override {
38         return std::make_unique<ConstructorSplat>(fOffset, this->type(), argument()->clone());
39     }
40 
getConstantSubexpression(int n)41     const Expression* getConstantSubexpression(int n) const override {
42         SkASSERT(n >= 0 && n < this->type().columns());
43         return this->argument()->getConstantSubexpression(0);
44     }
45 
46 private:
47     using INHERITED = SingleArgumentConstructor;
48 };
49 
50 }  // namespace SkSL
51 
52 #endif
53