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 "SkSLCodeGenerator.h"
12#include "SkSLSectionAndParameterHelper.h"
13#include "ir/SkSLType.h"
14#include "ir/SkSLVariable.h"
15
16#include <cctype>
17
18constexpr const char* kFragmentProcessorHeader =
19R"(
20/**************************************************************************************************
21 *** This file was autogenerated from %s.fp; do not modify.
22 **************************************************************************************************/
23)";
24
25namespace SkSL {
26
27class HCodeGenerator : public CodeGenerator {
28public:
29    HCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
30                   String name, OutputStream* out);
31
32    bool generateCode() override;
33
34    static String ParameterType(const Context& context, const Type& type, const Layout& layout);
35
36    static String FieldType(const Context& context, const Type& type, const Layout& layout);
37
38    static String FieldName(const char* varName) {
39        return String::printf("f%c%s", toupper(varName[0]), varName + 1);
40    }
41
42    static String GetHeader(const Program& program, ErrorReporter& errors);
43
44private:
45    void writef(const char* s, va_list va) SKSL_PRINTF_LIKE(2, 0);
46
47    void writef(const char* s, ...) SKSL_PRINTF_LIKE(2, 3);
48
49    bool writeSection(const char* name, const char* prefix = "");
50
51    // given a @constructorParams section of e.g. 'int x, float y', writes out "<separator>x, y".
52    // Writes nothing (not even the separator) if there is no @constructorParams section.
53    void writeExtraConstructorParams(const char* separator);
54
55    void writeMake();
56
57    void writeConstructor();
58
59    void writeFields();
60
61    void failOnSection(const char* section, const char* msg);
62
63    const Context& fContext;
64    String fName;
65    String fFullName;
66    SectionAndParameterHelper fSectionAndParameterHelper;
67
68    typedef CodeGenerator INHERITED;
69};
70
71} // namespace SkSL
72
73#endif
74