xref: /aosp_15_r20/external/skia/src/sksl/ir/SkSLTypeReference.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2016 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_TYPEREFERENCE
9 #define SKSL_TYPEREFERENCE
10 
11 #include "src/sksl/SkSLBuiltinTypes.h"
12 #include "src/sksl/SkSLContext.h"
13 #include "src/sksl/SkSLPosition.h"
14 #include "src/sksl/ir/SkSLExpression.h"
15 #include "src/sksl/ir/SkSLIRNode.h"
16 #include "src/sksl/ir/SkSLType.h"
17 
18 #include <cstdint>
19 #include <memory>
20 #include <string>
21 
22 namespace SkSL {
23 
24 enum class OperatorPrecedence : uint8_t;
25 
26 /**
27  * Represents an identifier referring to a type. This is an intermediate value: TypeReferences are
28  * always eventually replaced by Constructors in valid programs.
29  */
30 class TypeReference final : public Expression {
31 public:
32     inline static constexpr Kind kIRNodeKind = Kind::kTypeReference;
33 
TypeReference(const Context & context,Position pos,const Type * value)34     TypeReference(const Context& context, Position pos, const Type* value)
35             : TypeReference(pos, value, context.fTypes.fInvalid.get()) {}
36 
37     // Reports an error and returns false if the type is generic or, in a strict-ES2 program, if the
38     // type is not allowed in ES2. Otherwise, returns true. (These are the same checks performed by
39     // Convert.)
40     static bool VerifyType(const Context& context, const SkSL::Type* type, Position pos);
41 
42     // Creates a reference to an SkSL type; uses the ErrorReporter to report errors.
43     static std::unique_ptr<TypeReference> Convert(const Context& context,
44                                                   Position pos,
45                                                   const Type* type);
46 
47     // Creates a reference to an SkSL type; reports errors via ASSERT.
48     static std::unique_ptr<TypeReference> Make(const Context& context, Position pos,
49             const Type* type);
50 
value()51     const Type& value() const {
52         return fValue;
53     }
54 
description(OperatorPrecedence)55     std::string description(OperatorPrecedence) const override {
56         return std::string(this->value().name());
57     }
58 
clone(Position pos)59     std::unique_ptr<Expression> clone(Position pos) const override {
60         return std::unique_ptr<Expression>(new TypeReference(pos, &this->value(), &this->type()));
61     }
62 
63 private:
TypeReference(Position pos,const Type * value,const Type * type)64     TypeReference(Position pos, const Type* value, const Type* type)
65         : INHERITED(pos, kIRNodeKind, type)
66         , fValue(*value) {}
67 
68     const Type& fValue;
69 
70     using INHERITED = Expression;
71 };
72 
73 }  // namespace SkSL
74 
75 #endif
76