Coverage Report

Created: 2024-05-20 07:14

/src/skia/src/sksl/ir/SkSLIRHelpers.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2023 Google Inc.
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_IRHELPERS
9
#define SKSL_IRHELPERS
10
11
#include "include/core/SkTypes.h"
12
#include "src/sksl/SkSLAnalysis.h"
13
#include "src/sksl/SkSLBuiltinTypes.h"
14
#include "src/sksl/SkSLContext.h"
15
#include "src/sksl/SkSLDefines.h"
16
#include "src/sksl/SkSLOperator.h"
17
#include "src/sksl/SkSLPosition.h"
18
#include "src/sksl/ir/SkSLBinaryExpression.h"
19
#include "src/sksl/ir/SkSLConstructorCompound.h"
20
#include "src/sksl/ir/SkSLExpression.h"
21
#include "src/sksl/ir/SkSLExpressionStatement.h"
22
#include "src/sksl/ir/SkSLFieldAccess.h"
23
#include "src/sksl/ir/SkSLIndexExpression.h"
24
#include "src/sksl/ir/SkSLLiteral.h"
25
#include "src/sksl/ir/SkSLSwizzle.h"
26
#include "src/sksl/ir/SkSLVariableReference.h"
27
28
#include <memory>
29
#include <utility>
30
31
namespace SkSL {
32
33
class Statement;
34
class Variable;
35
36
struct IRHelpers {
37
0
    IRHelpers(const Context& c) : fContext(c) {}
38
39
    // Note that this class doesn't adhere to the typical Skia style rules; function names are
40
    // capitalized, and we don't use `this->` prefixes. This helps nested expressions flow more
41
    // naturally.
42
43
0
    std::unique_ptr<Expression> Ref(const Variable* var) const {
44
0
        return VariableReference::Make(Position(), var);
45
0
    }
46
47
0
    std::unique_ptr<Expression> Field(const Variable* var, int idx) const {
48
0
        return FieldAccess::Make(fContext, Position(), Ref(var), idx,
49
0
                                 FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
50
0
    }
51
52
0
    std::unique_ptr<Expression> Swizzle(std::unique_ptr<Expression> base, ComponentArray c) const {
53
0
        Position pos = base->fPosition;
54
0
        return Swizzle::Make(fContext, pos, std::move(base), std::move(c));
55
0
    }
56
57
    std::unique_ptr<Expression> Index(std::unique_ptr<Expression> base,
58
0
                                      std::unique_ptr<Expression> idx) const {
59
0
        Position pos = base->fPosition.rangeThrough(idx->fPosition);
60
0
        return IndexExpression::Make(fContext, pos, std::move(base), std::move(idx));
61
0
    }
62
63
    std::unique_ptr<Expression> Binary(std::unique_ptr<Expression> l,
64
                                       Operator op,
65
0
                                       std::unique_ptr<Expression> r) const {
66
0
        Position pos = l->fPosition.rangeThrough(r->fPosition);
67
0
        return BinaryExpression::Make(fContext, pos, std::move(l), op, std::move(r));
68
0
    }
69
70
    std::unique_ptr<Expression> Mul(std::unique_ptr<Expression> l,
71
0
                                    std::unique_ptr<Expression> r) const {
72
0
        return Binary(std::move(l), OperatorKind::STAR, std::move(r));
73
0
    }
74
75
    std::unique_ptr<Expression> Add(std::unique_ptr<Expression> l,
76
0
                                    std::unique_ptr<Expression> r) const {
77
0
        return Binary(std::move(l), OperatorKind::PLUS, std::move(r));
78
0
    }
79
80
0
    std::unique_ptr<Expression> Float(float value) const {
81
0
        return Literal::MakeFloat(Position(), value, fContext.fTypes.fFloat.get());
82
0
    }
83
84
0
    std::unique_ptr<Expression> Int(int value) const {
85
0
        return Literal::MakeInt(Position(), value, fContext.fTypes.fInt.get());
86
0
    }
87
88
    std::unique_ptr<Expression> CtorXYZW(std::unique_ptr<Expression> xy,
89
                                         std::unique_ptr<Expression> z,
90
0
                                         std::unique_ptr<Expression> w) const {
91
0
        ExpressionArray args;
92
0
        args.push_back(std::move(xy));
93
0
        args.push_back(std::move(z));
94
0
        args.push_back(std::move(w));
95
0
        return ConstructorCompound::Make(fContext, Position(), *fContext.fTypes.fFloat4,
96
0
                                         std::move(args));
97
0
    }
98
99
    std::unique_ptr<Statement> Assign(std::unique_ptr<Expression> l,
100
0
                                      std::unique_ptr<Expression> r) const {
101
0
        SkAssertResult(Analysis::UpdateVariableRefKind(l.get(), VariableRefKind::kWrite));
102
0
        return ExpressionStatement::Make(fContext,
103
0
                                         Binary(std::move(l), OperatorKind::EQ, std::move(r)));
104
0
    }
Unexecuted instantiation: SkSL::IRHelpers::Assign(std::__1::unique_ptr<SkSL::Expression, std::__1::default_delete<SkSL::Expression> >, std::__1::unique_ptr<SkSL::Expression, std::__1::default_delete<SkSL::Expression> >) const
Unexecuted instantiation: SkSL::IRHelpers::Assign(std::__1::unique_ptr<SkSL::Expression, std::__1::default_delete<SkSL::Expression> >, std::__1::unique_ptr<SkSL::Expression, std::__1::default_delete<SkSL::Expression> >) const
105
106
    const Context& fContext;
107
};
108
109
}  // namespace SkSL
110
111
#endif