Flutter Engine
The Flutter Engine
SkSLConstructorArrayCast.cpp
Go to the documentation of this file.
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
9
10#include "include/core/SkSpan.h"
19
20namespace SkSL {
21
22static std::unique_ptr<Expression> cast_constant_array(const Context& context,
24 const Type& destType,
25 std::unique_ptr<Expression> constCtor) {
26 const Type& scalarType = destType.componentType();
27
28 // Create a ConstructorArray(...) which typecasts each argument inside.
29 auto inputArgs = constCtor->as<ConstructorArray>().argumentSpan();
30 ExpressionArray typecastArgs;
31 typecastArgs.reserve_exact(inputArgs.size());
32 for (std::unique_ptr<Expression>& arg : inputArgs) {
33 Position argPos = arg->fPosition;
34 if (arg->type().isScalar()) {
35 typecastArgs.push_back(ConstructorScalarCast::Make(context, argPos, scalarType,
36 std::move(arg)));
37 } else {
38 typecastArgs.push_back(ConstructorCompoundCast::Make(context, argPos, scalarType,
39 std::move(arg)));
40 }
41 }
42
43 return ConstructorArray::Make(context, pos, destType, std::move(typecastArgs));
44}
45
46std::unique_ptr<Expression> ConstructorArrayCast::Make(const Context& context,
48 const Type& type,
49 std::unique_ptr<Expression> arg) {
50 // Only arrays of the same size are allowed.
53 SkASSERT(arg->type().isArray());
54 SkASSERT(type.columns() == arg->type().columns());
55
56 // If this is a no-op cast, return the expression as-is.
57 if (type.matches(arg->type())) {
58 arg->fPosition = pos;
59 return arg;
60 }
61
62 // Look up the value of constant variables. This allows constant-expressions like `myArray` to
63 // be replaced with the compile-time constant `int[2](0, 1)`.
65
66 // We can cast a vector of compile-time constants at compile-time.
68 return cast_constant_array(context, pos, type, std::move(arg));
69 }
70 return std::make_unique<ConstructorArrayCast>(pos, type, std::move(arg));
71}
72
73} // namespace SkSL
SkPoint pos
#define SkASSERT(cond)
Definition: SkAssert.h:116
GLenum type
static std::unique_ptr< Expression > MakeConstantValueForVariable(Position pos, std::unique_ptr< Expression > expr)
static std::unique_ptr< Expression > Make(const Context &context, Position pos, const Type &type, std::unique_ptr< Expression > arg)
static std::unique_ptr< Expression > Make(const Context &context, Position pos, const Type &type, ExpressionArray args)
static std::unique_ptr< Expression > Make(const Context &context, Position pos, const Type &type, std::unique_ptr< Expression > arg)
static std::unique_ptr< Expression > Make(const Context &context, Position pos, const Type &type, std::unique_ptr< Expression > arg)
const Type & type() const
virtual bool isArray() const
Definition: SkSLType.h:532
bool isAllowedInES2(const Context &context) const
virtual const Type & componentType() const
Definition: SkSLType.h:404
bool matches(const Type &other) const
Definition: SkSLType.h:269
virtual int columns() const
Definition: SkSLType.h:429
const T & as() const
Definition: SkSLType.h:210
void reserve_exact(int n)
Definition: SkTArray.h:181
bool IsCompileTimeConstant(const Expression &expr)
static std::unique_ptr< Expression > cast_constant_array(const Context &context, Position pos, const Type &destType, std::unique_ptr< Expression > constCtor)