Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
SkSL::ConstructorScalarCast Class Referencefinal

#include <SkSLConstructorScalarCast.h>

Inheritance diagram for SkSL::ConstructorScalarCast:
SkSL::SingleArgumentConstructor SkSL::AnyConstructor SkSL::Expression SkSL::IRNode SkSL::Poolable

Public Member Functions

 ConstructorScalarCast (Position pos, const Type &type, std::unique_ptr< Expression > arg)
 
std::unique_ptr< Expressionclone (Position pos) const override
 
- Public Member Functions inherited from SkSL::SingleArgumentConstructor
 SingleArgumentConstructor (Position pos, Kind kind, const Type *type, std::unique_ptr< Expression > argument)
 
std::unique_ptr< Expression > & argument ()
 
const std::unique_ptr< Expression > & argument () const
 
SkSpan< std::unique_ptr< Expression > > argumentSpan () final
 
SkSpan< const std::unique_ptr< Expression > > argumentSpan () const final
 
- Public Member Functions inherited from SkSL::AnyConstructor
 AnyConstructor (Position pos, Kind kind, const Type *type)
 
std::string description (OperatorPrecedence) const override
 
const TypecomponentType () const
 
bool supportsConstantValues () const override
 
std::optional< double > getConstantValue (int n) const override
 
ComparisonResult compareConstant (const Expression &other) const override
 
- Public Member Functions inherited from SkSL::Expression
 Expression (Position pos, Kind kind, const Type *type)
 
Kind kind () const
 
virtual const Typetype () const
 
bool isAnyConstructor () const
 
bool isIntLiteral () const
 
bool isFloatLiteral () const
 
bool isBoolLiteral () const
 
AnyConstructorasAnyConstructor ()
 
const AnyConstructorasAnyConstructor () const
 
bool isIncomplete (const Context &context) const
 
CoercionCost coercionCost (const Type &target) const
 
std::unique_ptr< Expressionclone () const
 
std::string description () const final
 
- Public Member Functions inherited from SkSL::IRNode
virtual ~IRNode ()
 
 IRNode (const IRNode &)=delete
 
IRNodeoperator= (const IRNode &)=delete
 
Position position () const
 
void setPosition (Position p)
 
template<typename T >
bool is () const
 
template<typename T >
const Tas () const
 
template<typename T >
Tas ()
 

Static Public Member Functions

static std::unique_ptr< ExpressionConvert (const Context &context, Position pos, const Type &rawType, ExpressionArray args)
 
static std::unique_ptr< ExpressionMake (const Context &context, Position pos, const Type &type, std::unique_ptr< Expression > arg)
 
- Static Public Member Functions inherited from SkSL::Poolable
static void * operator new (const size_t size)
 
static void operator delete (void *ptr)
 

Static Public Attributes

static constexpr Kind kIRNodeKind = Kind::kConstructorScalarCast
 

Additional Inherited Members

- Public Types inherited from SkSL::Expression
enum class  ComparisonResult { kUnknown = -1 , kNotEqual , kEqual }
 
using Kind = ExpressionKind
 
- Public Attributes inherited from SkSL::IRNode
Position fPosition
 
- Protected Member Functions inherited from SkSL::IRNode
 IRNode (Position position, int kind)
 
- Protected Attributes inherited from SkSL::IRNode
int fKind
 

Detailed Description

Represents the construction of a scalar cast, such as float(intVariable).

These always contain exactly 1 scalar of a differing type, and are never constant.

Definition at line 30 of file SkSLConstructorScalarCast.h.

Constructor & Destructor Documentation

◆ ConstructorScalarCast()

SkSL::ConstructorScalarCast::ConstructorScalarCast ( Position  pos,
const Type type,
std::unique_ptr< Expression arg 
)
inline

Definition at line 34 of file SkSLConstructorScalarCast.h.

35 : INHERITED(pos, kIRNodeKind, &type, std::move(arg)) {}
SkPoint pos
virtual const Type & type() const

Member Function Documentation

◆ clone()

std::unique_ptr< Expression > SkSL::ConstructorScalarCast::clone ( Position  pos) const
inlineoverridevirtual

Implements SkSL::Expression.

Definition at line 51 of file SkSLConstructorScalarCast.h.

51 {
52 return std::make_unique<ConstructorScalarCast>(pos, this->type(), argument()->clone());
53 }
std::unique_ptr< Expression > clone() const
std::unique_ptr< Expression > & argument()

◆ Convert()

std::unique_ptr< Expression > SkSL::ConstructorScalarCast::Convert ( const Context context,
Position  pos,
const Type rawType,
ExpressionArray  args 
)
static

Definition at line 23 of file SkSLConstructorScalarCast.cpp.

26 {
27 // As you might expect, scalar-cast constructors should only be created with scalar types.
28 const Type& type = rawType.scalarTypeForLiteral();
30
31 if (args.size() != 1) {
32 context.fErrors->error(pos, "invalid arguments to '" + type.displayName() +
33 "' constructor, (expected exactly 1 argument, but found " +
34 std::to_string(args.size()) + ")");
35 return nullptr;
36 }
37
38 const Type& argType = args[0]->type();
39 if (!argType.isScalar()) {
40 // Casting a vector-type into its scalar component type is treated as a slice in GLSL.
41 // We don't allow those casts in SkSL; recommend a .x swizzle instead.
42 const char* swizzleHint = "";
43 if (argType.componentType().matches(type)) {
44 if (argType.isVector()) {
45 swizzleHint = "; use '.x' instead";
46 } else if (argType.isMatrix()) {
47 swizzleHint = "; use '[0][0]' instead";
48 }
49 }
50
51 context.fErrors->error(pos,
52 "'" + argType.displayName() + "' is not a valid parameter to '" +
53 type.displayName() + "' constructor" + swizzleHint);
54 return nullptr;
55 }
56 if (type.checkForOutOfRangeLiteral(context, *args[0])) {
57 return nullptr;
58 }
59
60 return ConstructorScalarCast::Make(context, pos, type, std::move(args[0]));
61}
#define SkASSERT(cond)
Definition SkAssert.h:116
static std::unique_ptr< Expression > Make(const Context &context, Position pos, const Type &type, std::unique_ptr< Expression > arg)
virtual bool isScalar() const
Definition SkSLType.h:512
virtual const Type & scalarTypeForLiteral() const
Definition SkSLType.h:520
bool checkForOutOfRangeLiteral(const Context &context, const Expression &expr) const
std::string displayName() const
Definition SkSLType.h:234
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args

◆ Make()

std::unique_ptr< Expression > SkSL::ConstructorScalarCast::Make ( const Context context,
Position  pos,
const Type type,
std::unique_ptr< Expression arg 
)
static

Definition at line 63 of file SkSLConstructorScalarCast.cpp.

66 {
69 SkASSERT(arg->type().isScalar());
70
71 // No cast required when the types match.
72 if (arg->type().matches(type)) {
73 arg->setPosition(pos);
74 return arg;
75 }
76 // Look up the value of constant variables. This allows constant-expressions like `int(zero)` to
77 // be replaced with a literal zero.
79
80 // We can cast scalar literals at compile-time when possible. (If the resulting literal would be
81 // out of range for its type, we report an error and return zero to minimize error cascading.
82 // This can occur when code is inlined, so we can't necessarily catch it during Convert. As
83 // such, it's not safe to return null or assert.)
84 if (arg->is<Literal>()) {
85 double value = arg->as<Literal>().value();
86 if (type.checkForOutOfRangeLiteral(context, value, arg->fPosition)) {
87 value = 0.0;
88 }
89 return Literal::Make(pos, value, &type);
90 }
91
92 // We allow scalar casts to abstract types `$floatLiteral` or `$intLiteral`. This can be used to
93 // represent various expressions where SkSL still allows type flexibility. For instance, the
94 // expression `float x = myBool ? 1 : 0` is allowed in SkSL despite the apparent type mismatch,
95 // and the resolved type of expression `myBool ? 1 : 0` is actually `$intLiteral`. This
96 // expression could also be rewritten as `$intLiteral(myBool)` to replace a ternary with a cast.
97 //
98 // If we are casting an expression of the form `$intLiteral(...)` or `$floatLiteral(...)`, we
99 // can eliminate the intermediate constructor-cast since it no longer adds value.
100 if (arg->is<ConstructorScalarCast>() && arg->type().isLiteral()) {
101 std::unique_ptr<Expression> inner = std::move(arg->as<ConstructorScalarCast>().argument());
102 return ConstructorScalarCast::Make(context, pos, type, std::move(inner));
103 }
104
105 return std::make_unique<ConstructorScalarCast>(pos, type, std::move(arg));
106}
static std::unique_ptr< Expression > MakeConstantValueForVariable(Position pos, std::unique_ptr< Expression > expr)
static std::unique_ptr< Literal > Make(Position pos, double value, const Type *type)
Definition SkSLLiteral.h:81
bool isAllowedInES2(const Context &context) const
uint8_t value

Member Data Documentation

◆ kIRNodeKind

constexpr Kind SkSL::ConstructorScalarCast::kIRNodeKind = Kind::kConstructorScalarCast
inlinestaticconstexpr

Definition at line 32 of file SkSLConstructorScalarCast.h.


The documentation for this class was generated from the following files: