Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSLIsSameExpressionTree.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
24
25#include <cstddef>
26#include <memory>
27
28namespace SkSL {
29
31 if (left.kind() != right.kind() || !left.type().matches(right.type())) {
32 return false;
33 }
34
35 // This isn't a fully exhaustive list of expressions by any stretch of the imagination; for
36 // instance, `x[y+1] = x[y+1]` isn't detected because we don't look at BinaryExpressions.
37 // Since this is intended to be used for optimization purposes, handling the common cases is
38 // sufficient.
39 switch (left.kind()) {
40 case Expression::Kind::kLiteral:
41 return left.as<Literal>().value() == right.as<Literal>().value();
42
43 case Expression::Kind::kConstructorArray:
44 case Expression::Kind::kConstructorArrayCast:
45 case Expression::Kind::kConstructorCompound:
46 case Expression::Kind::kConstructorCompoundCast:
47 case Expression::Kind::kConstructorDiagonalMatrix:
48 case Expression::Kind::kConstructorMatrixResize:
49 case Expression::Kind::kConstructorScalarCast:
50 case Expression::Kind::kConstructorStruct:
51 case Expression::Kind::kConstructorSplat: {
52 if (left.kind() != right.kind()) {
53 return false;
54 }
55 const AnyConstructor& leftCtor = left.asAnyConstructor();
56 const AnyConstructor& rightCtor = right.asAnyConstructor();
57 const auto leftSpan = leftCtor.argumentSpan();
58 const auto rightSpan = rightCtor.argumentSpan();
59 if (leftSpan.size() != rightSpan.size()) {
60 return false;
61 }
62 for (size_t index = 0; index < leftSpan.size(); ++index) {
63 if (!IsSameExpressionTree(*leftSpan[index], *rightSpan[index])) {
64 return false;
65 }
66 }
67 return true;
68 }
69 case Expression::Kind::kFieldAccess:
70 return left.as<FieldAccess>().fieldIndex() == right.as<FieldAccess>().fieldIndex() &&
72 *right.as<FieldAccess>().base());
73
74 case Expression::Kind::kIndex:
76 *right.as<IndexExpression>().index()) &&
78 *right.as<IndexExpression>().base());
79
80 case Expression::Kind::kPrefix:
81 return (left.as<PrefixExpression>().getOperator().kind() ==
85
86 case Expression::Kind::kSwizzle:
87 return left.as<Swizzle>().components() == right.as<Swizzle>().components() &&
89
90 case Expression::Kind::kVariableReference:
91 return left.as<VariableReference>().variable() ==
92 right.as<VariableReference>().variable();
93
94 default:
95 return false;
96 }
97}
98
99} // namespace SkSL
static bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)
virtual SkSpan< std::unique_ptr< Expression > > argumentSpan()=0
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()
Kind kind() const
std::unique_ptr< Expression > & operand()
std::unique_ptr< Expression > & base()
Definition SkSLSwizzle.h:82
uint8_t value
bool IsSameExpressionTree(const Expression &left, const Expression &right)