Flutter Engine
The Flutter Engine
SkSLIsTrivialExpression.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022 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
20
21#include <memory>
22
23namespace SkSL {
24
26 switch (expr.kind()) {
28 case Expression::Kind::kVariableReference:
29 return true;
30
31 case Expression::Kind::kSwizzle:
32 // All swizzles are considered to be trivial.
33 return IsTrivialExpression(*expr.as<Swizzle>().base());
34
35 case Expression::Kind::kPrefix: {
37 switch (prefix.getOperator().kind()) {
42 return IsTrivialExpression(*prefix.operand());
43
44 default:
45 return false;
46 }
47 }
48 case Expression::Kind::kFieldAccess:
49 // Accessing a field is trivial.
50 return IsTrivialExpression(*expr.as<FieldAccess>().base());
51
53 // Accessing a constant array index is trivial.
54 const IndexExpression& inner = expr.as<IndexExpression>();
55 return inner.index()->isIntLiteral() && IsTrivialExpression(*inner.base());
56 }
57 case Expression::Kind::kConstructorArray:
58 case Expression::Kind::kConstructorStruct:
59 // Only consider small arrays/structs of compile-time-constants to be trivial.
60 return expr.type().slotCount() <= 4 && IsCompileTimeConstant(expr);
61
62 case Expression::Kind::kConstructorArrayCast:
63 case Expression::Kind::kConstructorMatrixResize:
64 // These operations require function calls in Metal, so they're never trivial.
65 return false;
66
67 case Expression::Kind::kConstructorCompound:
68 // Only compile-time-constant compound constructors are considered to be trivial.
69 return IsCompileTimeConstant(expr);
70
71 case Expression::Kind::kConstructorCompoundCast:
72 case Expression::Kind::kConstructorScalarCast:
73 case Expression::Kind::kConstructorSplat:
74 case Expression::Kind::kConstructorDiagonalMatrix: {
75 // Single-argument constructors are trivial when their inner expression is trivial.
76 SkASSERT(expr.asAnyConstructor().argumentSpan().size() == 1);
77 const Expression& inner = *expr.asAnyConstructor().argumentSpan().front();
78 return IsTrivialExpression(inner);
79 }
80 default:
81 return false;
82 }
83}
84
85} // namespace SkSL
#define SkASSERT(cond)
Definition: SkAssert.h:116
virtual SkSpan< std::unique_ptr< Expression > > argumentSpan()=0
Kind kind() const
const Type & type() const
AnyConstructor & asAnyConstructor()
std::unique_ptr< Expression > & base()
const T & as() const
Definition: SkSLIRNode.h:133
std::unique_ptr< Expression > & base()
std::unique_ptr< Expression > & index()
std::unique_ptr< Expression > & base()
Definition: SkSLSwizzle.h:82
virtual size_t slotCount() const
Definition: SkSLType.h:457
bool IsTrivialExpression(const Expression &expr)
constexpr bool IsCompileTimeConstant(const T)