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_HCODEGENERATOR
9 #define SKSL_HCODEGENERATOR
10 
11 #include "src/sksl/codegen/SkSLCodeGenerator.h"
12 
13 #include "src/sksl/SkSLSectionAndParameterHelper.h"
14 #include "src/sksl/ir/SkSLType.h"
15 #include "src/sksl/ir/SkSLVariable.h"
16 
17 #include <cctype>
18 
19 #if defined(SKSL_STANDALONE) || GR_TEST_UTILS
20 
21 constexpr const char* kFragmentProcessorHeader =
22 R"(
23 /**************************************************************************************************
24  *** This file was autogenerated from %s.fp; do not modify.
25  **************************************************************************************************/
26 )";
27 
28 namespace SkSL {
29 
30 class HCodeGenerator : public CodeGenerator {
31 public:
32     HCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
33                    String name, OutputStream* out);
34 
35     bool generateCode() override;
36 
37     static String ParameterType(const Context& context, const Type& type, const Layout& layout);
38 
39     static Layout::CType ParameterCType(const Context& context, const Type& type,
40                                         const Layout& layout);
41 
42     static String FieldType(const Context& context, const Type& type, const Layout& layout);
43 
44     // Either the field type, or a const reference of the field type if the field type is complex.
45     static String AccessType(const Context& context, const Type& type, const Layout& layout);
46 
FieldName(const char * varName)47     static String FieldName(const char* varName) {
48         return String(varName);
49     }
50 
CoordTransformName(const String & arg,int index)51     static String CoordTransformName(const String& arg, int index) {
52         if (arg.size()) {
53             return HCodeGenerator::FieldName(arg.c_str()) + "CoordTransform";
54         }
55         return "fCoordTransform" + to_string(index);
56     }
57 
58     static String GetHeader(const Program& program, ErrorReporter& errors);
59 
60 private:
61     void writef(const char* s, va_list va) SK_PRINTF_LIKE(2, 0);
62 
63     void writef(const char* s, ...) SK_PRINTF_LIKE(2, 3);
64 
65     bool writeSection(const char* name, const char* prefix = "");
66 
67     // given a @constructorParams section of e.g. 'int x, float y', writes out "<separator>x, y".
68     // Writes nothing (not even the separator) if there is no @constructorParams section.
69     void writeExtraConstructorParams(const char* separator);
70 
71     void writeMake();
72 
73     void writeConstructor();
74 
75     void writeFields();
76 
77     void failOnSection(const char* section, const char* msg);
78 
79     const Context& fContext;
80     String fName;
81     String fFullName;
82     SectionAndParameterHelper fSectionAndParameterHelper;
83 
84     using INHERITED = CodeGenerator;
85 };
86 
87 } // namespace SkSL
88 
89 #endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS
90 
91 #endif // SKSL_HCODEGENERATOR
92