1 /*
2  * Copyright 2017 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_SECTIONANDPARAMETERHELPER
9 #define SKSL_SECTIONANDPARAMETERHELPER
10 
11 #include "src/sksl/SkSLErrorReporter.h"
12 #include "src/sksl/ir/SkSLProgram.h"
13 #include "src/sksl/ir/SkSLSection.h"
14 #include "src/sksl/ir/SkSLVarDeclarations.h"
15 #include <unordered_map>
16 #include <vector>
17 
18 namespace SkSL {
19 
20 inline constexpr char kClassSection[] =              "class";
21 inline constexpr char kCloneSection[] =              "clone";
22 inline constexpr char kConstructorSection[] =        "constructor";
23 inline constexpr char kConstructorCodeSection[] =    "constructorCode";
24 inline constexpr char kConstructorParamsSection[] =  "constructorParams";
25 inline constexpr char kCppSection[] =                "cpp";
26 inline constexpr char kCppEndSection[] =             "cppEnd";
27 inline constexpr char kDumpInfoSection[] =           "dumpInfo";
28 inline constexpr char kEmitCodeSection[] =           "emitCode";
29 inline constexpr char kFieldsSection[] =             "fields";
30 inline constexpr char kHeaderSection[] =             "header";
31 inline constexpr char kHeaderEndSection[] =          "headerEnd";
32 inline constexpr char kInitializersSection[] =       "initializers";
33 inline constexpr char kMakeSection[] =               "make";
34 inline constexpr char kOptimizationFlagsSection[] =  "optimizationFlags";
35 inline constexpr char kSamplerParamsSection[] =      "samplerParams";
36 inline constexpr char kSetDataSection[] =            "setData";
37 inline constexpr char kTestCodeSection[] =           "test";
38 
39 class SectionAndParameterHelper {
40 public:
41     SectionAndParameterHelper(const Program* program, ErrorReporter& errors);
42 
getSection(const char * name)43     const Section* getSection(const char* name) {
44         SkASSERT(!SectionPermitsDuplicates(name));
45         auto found = fSections.find(name);
46         if (found == fSections.end()) {
47             return nullptr;
48         }
49         SkASSERT(found->second.size() == 1);
50         return found->second[0];
51     }
52 
getSections(const char * name)53     std::vector<const Section*> getSections(const char* name) {
54         auto found = fSections.find(name);
55         if (found == fSections.end()) {
56             return std::vector<const Section*>();
57         }
58         return found->second;
59     }
60 
getParameters()61     const std::vector<const Variable*>& getParameters() {
62         return fParameters;
63     }
64 
IsParameter(const Variable & var)65     static bool IsParameter(const Variable& var) {
66         return (var.modifiers().fFlags & Modifiers::kIn_Flag) &&
67                -1 == var.modifiers().fLayout.fBuiltin;
68     }
69 
IsSupportedSection(const char * name)70     static bool IsSupportedSection(const char* name) {
71         return !strcmp(name, kClassSection) ||
72                !strcmp(name, kCloneSection) ||
73                !strcmp(name, kConstructorSection) ||
74                !strcmp(name, kConstructorCodeSection) ||
75                !strcmp(name, kConstructorParamsSection) ||
76                !strcmp(name, kCppSection) ||
77                !strcmp(name, kCppEndSection) ||
78                !strcmp(name, kDumpInfoSection) ||
79                !strcmp(name, kEmitCodeSection) ||
80                !strcmp(name, kFieldsSection) ||
81                !strcmp(name, kHeaderSection) ||
82                !strcmp(name, kHeaderEndSection) ||
83                !strcmp(name, kInitializersSection) ||
84                !strcmp(name, kMakeSection) ||
85                !strcmp(name, kOptimizationFlagsSection) ||
86                !strcmp(name, kSamplerParamsSection) ||
87                !strcmp(name, kSetDataSection) ||
88                !strcmp(name, kTestCodeSection);
89     }
90 
SectionAcceptsArgument(const char * name)91     static bool SectionAcceptsArgument(const char* name) {
92         return !strcmp(name, kSamplerParamsSection) ||
93                !strcmp(name, kSetDataSection) ||
94                !strcmp(name, kTestCodeSection);
95     }
96 
SectionRequiresArgument(const char * name)97     static bool SectionRequiresArgument(const char* name) {
98         return !strcmp(name, kSamplerParamsSection) ||
99                !strcmp(name, kSetDataSection) ||
100                !strcmp(name, kTestCodeSection);
101     }
102 
SectionPermitsDuplicates(const char * name)103     static bool SectionPermitsDuplicates(const char* name) {
104         return !strcmp(name, kSamplerParamsSection);
105     }
106 
107 private:
108     const Program& fProgram;
109     std::vector<const Variable*> fParameters;
110     std::unordered_map<String, std::vector<const Section*>> fSections;
111 };
112 
113 } // namespace SkSL
114 
115 #endif
116