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 "include/private/SkSLIRNode.h" 12 #include "include/sksl/SkSLPosition.h" 13 #include "src/sksl/SkSLBuiltinTypes.h" 14 #include "src/sksl/SkSLContext.h" 15 #include "src/sksl/ir/SkSLExpression.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 // Creates a reference to an SkSL type; uses the ErrorReporter to report errors. 38 static std::unique_ptr<TypeReference> Convert(const Context& context, 39 Position pos, 40 const Type* type); 41 42 // Creates a reference to an SkSL type; reports errors via ASSERT. 43 static std::unique_ptr<TypeReference> Make(const Context& context, Position pos, 44 const Type* type); 45 value()46 const Type& value() const { 47 return fValue; 48 } 49 description(OperatorPrecedence)50 std::string description(OperatorPrecedence) const override { 51 return std::string(this->value().name()); 52 } 53 clone(Position pos)54 std::unique_ptr<Expression> clone(Position pos) const override { 55 return std::unique_ptr<Expression>(new TypeReference(pos, &this->value(), &this->type())); 56 } 57 58 private: TypeReference(Position pos,const Type * value,const Type * type)59 TypeReference(Position pos, const Type* value, const Type* type) 60 : INHERITED(pos, kIRNodeKind, type) 61 , fValue(*value) {} 62 63 const Type& fValue; 64 65 using INHERITED = Expression; 66 }; 67 68 } // namespace SkSL 69 70 #endif 71