xref: /aosp_15_r20/external/skia/src/sksl/ir/SkSLExtension.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #include "src/sksl/ir/SkSLExtension.h"
9 
10 #include "include/private/base/SkAssert.h"
11 #include "src/sksl/SkSLContext.h"
12 #include "src/sksl/SkSLErrorReporter.h"
13 #include "src/sksl/SkSLProgramSettings.h"
14 
15 namespace SkSL {
16 
Convert(const Context & context,Position pos,std::string_view name,std::string_view behaviorText)17 std::unique_ptr<Extension> Extension::Convert(const Context& context,
18                                               Position pos,
19                                               std::string_view name,
20                                               std::string_view behaviorText) {
21     if (ProgramConfig::IsRuntimeEffect(context.fConfig->fKind)) {
22         // Runtime Effects do not allow any #extensions.
23         context.fErrors->error(pos, "unsupported directive '#extension'");
24         return nullptr;
25     }
26     if (behaviorText == "disable") {
27         // We allow `#extension <name> : disable`, but it is a no-op.
28         return nullptr;
29     }
30     if (behaviorText != "require" && behaviorText != "enable" && behaviorText != "warn") {
31         context.fErrors->error(pos, "expected 'require', 'enable', 'warn', or 'disable'");
32         return nullptr;
33     }
34     // We don't currently do anything different between `require`, `enable`, and `warn`.
35     return Extension::Make(context, pos, name);
36 }
37 
Make(const Context & context,Position pos,std::string_view name)38 std::unique_ptr<Extension> Extension::Make(const Context& context,
39                                            Position pos,
40                                            std::string_view name) {
41     SkASSERT(!ProgramConfig::IsRuntimeEffect(context.fConfig->fKind));
42     return std::make_unique<SkSL::Extension>(pos, name);
43 }
44 
45 }  // namespace SkSL
46