xref: /aosp_15_r20/external/skia/src/sksl/ir/SkSLTypeReference.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 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 
8 #include "src/sksl/ir/SkSLTypeReference.h"
9 
10 #include "include/core/SkTypes.h"
11 #include "src/sksl/SkSLErrorReporter.h"
12 #include "src/sksl/SkSLProgramSettings.h"
13 
14 namespace SkSL {
15 
VerifyType(const Context & context,const SkSL::Type * type,Position pos)16 bool TypeReference::VerifyType(const Context& context, const SkSL::Type* type, Position pos) {
17     if (!context.fConfig->isBuiltinCode() && type) {
18         if (type->isGeneric() || type->isLiteral()) {
19             context.fErrors->error(pos, "type '" + std::string(type->name()) + "' is generic");
20             return false;
21         }
22         if (!type->isAllowedInES2(context)) {
23             context.fErrors->error(pos, "type '" + std::string(type->name()) +"' is not supported");
24             return false;
25         }
26     }
27     return true;
28 }
29 
Convert(const Context & context,Position pos,const Type * type)30 std::unique_ptr<TypeReference> TypeReference::Convert(const Context& context,
31                                                       Position pos,
32                                                       const Type* type) {
33     return VerifyType(context, type, pos) ? TypeReference::Make(context, pos, type)
34                                           : nullptr;
35 }
36 
Make(const Context & context,Position pos,const Type * type)37 std::unique_ptr<TypeReference> TypeReference::Make(const Context& context,
38                                                    Position pos,
39                                                    const Type* type) {
40     SkASSERT(type->isAllowedInES2(context));
41     return std::make_unique<TypeReference>(context, pos, type);
42 }
43 
44 } // namespace SkSL
45