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 "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 ExpressionArray; 23 class Type; 24 25 /** 26 * Represents the construction of a scalar cast, such as `float(intVariable)`. 27 * 28 * These always contain exactly 1 scalar of a differing type, and are never constant. 29 */ 30 class ConstructorScalarCast final : public SingleArgumentConstructor { 31 public: 32 inline static constexpr Kind kIRNodeKind = Kind::kConstructorScalarCast; 33 ConstructorScalarCast(Position pos,const Type & type,std::unique_ptr<Expression> arg)34 ConstructorScalarCast(Position pos, const Type& type, std::unique_ptr<Expression> arg) 35 : INHERITED(pos, kIRNodeKind, &type, std::move(arg)) {} 36 37 // ConstructorScalarCast::Convert will typecheck and create scalar-constructor expressions. 38 // Reports errors via the ErrorReporter; returns null on error. 39 static std::unique_ptr<Expression> Convert(const Context& context, 40 Position pos, 41 const Type& rawType, 42 ExpressionArray args); 43 44 // ConstructorScalarCast::Make casts a scalar expression. Casts that can be evaluated at 45 // compile-time will do so (e.g. `int(4.1)` --> `Literal(int 4)`). Errors reported via SkASSERT. 46 static std::unique_ptr<Expression> Make(const Context& context, 47 Position pos, 48 const Type& type, 49 std::unique_ptr<Expression> arg); 50 clone(Position pos)51 std::unique_ptr<Expression> clone(Position pos) const override { 52 return std::make_unique<ConstructorScalarCast>(pos, this->type(), argument()->clone()); 53 } 54 55 private: 56 using INHERITED = SingleArgumentConstructor; 57 }; 58 59 } // namespace SkSL 60 61 #endif 62