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_SCALAR_CAST
9 #define SKSL_CONSTRUCTOR_SCALAR_CAST
10 
11 #include "include/private/SkSLDefines.h"
12 #include "src/sksl/SkSLContext.h"
13 #include "src/sksl/ir/SkSLConstructor.h"
14 #include "src/sksl/ir/SkSLExpression.h"
15 
16 #include <memory>
17 
18 namespace SkSL {
19 
20 /**
21  * Represents the construction of a scalar cast, such as `float(intVariable)`.
22  *
23  * These always contain exactly 1 scalar of a differing type, and are never constant.
24  */
25 class ConstructorScalarCast final : public SingleArgumentConstructor {
26 public:
27     static constexpr Kind kExpressionKind = Kind::kConstructorScalarCast;
28 
ConstructorScalarCast(int offset,const Type & type,std::unique_ptr<Expression> arg)29     ConstructorScalarCast(int offset, const Type& type, std::unique_ptr<Expression> arg)
30         : INHERITED(offset, kExpressionKind, &type, std::move(arg)) {}
31 
32     // ConstructorScalarCast::Convert will typecheck and create scalar-constructor expressions.
33     // Reports errors via the ErrorReporter; returns null on error.
34     static std::unique_ptr<Expression> Convert(const Context& context,
35                                                int offset,
36                                                const Type& rawType,
37                                                ExpressionArray args);
38 
39     // ConstructorScalarCast::Make casts a scalar expression. Casts that can be evaluated at
40     // compile-time will do so (e.g. `int(4.1)` --> `IntLiteral(4)`). Errors reported via SkASSERT.
41     static std::unique_ptr<Expression> Make(const Context& context,
42                                             int offset,
43                                             const Type& type,
44                                             std::unique_ptr<Expression> arg);
45 
clone()46     std::unique_ptr<Expression> clone() const override {
47         return std::make_unique<ConstructorScalarCast>(fOffset, this->type(), argument()->clone());
48     }
49 
isCompileTimeConstant()50     bool isCompileTimeConstant() const override {
51         // If this were a compile-time constant, we would have created a literal instead.
52         return false;
53     }
54 
55 private:
56     using INHERITED = SingleArgumentConstructor;
57 };
58 
59 }  // namespace SkSL
60 
61 #endif
62