ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | */ |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_CODEGENERATOR |
| 9 | #define SKSL_CODEGENERATOR |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/sksl/SkSLOutputStream.h" |
| 12 | #include "src/sksl/ir/SkSLProgram.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 13 | |
| 14 | namespace SkSL { |
| 15 | |
| 16 | /** |
| 17 | * Abstract superclass of all code generators, which take a Program as input and produce code as |
| 18 | * output. |
| 19 | */ |
| 20 | class CodeGenerator { |
| 21 | public: |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 22 | CodeGenerator(const Program* program, ErrorReporter* errors, OutputStream* out) |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 23 | : fProgram(*program) |
| 24 | , fErrors(*errors) |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 25 | , fOut(out) {} |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 26 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 27 | virtual ~CodeGenerator() {} |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 28 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 29 | virtual bool generateCode() = 0; |
| 30 | |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 31 | // Intended for use by AutoOutputStream. |
| 32 | OutputStream* outputStream() { return fOut; } |
| 33 | void setOutputStream(OutputStream* output) { fOut = output; } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 34 | |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 35 | protected: |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 36 | const Program& fProgram; |
| 37 | ErrorReporter& fErrors; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 38 | OutputStream* fOut; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
John Stiles | 4453237 | 2020-12-07 12:33:55 -0500 | [diff] [blame] | 41 | class AutoOutputStream { |
| 42 | public: |
| 43 | // Maintains the current indentation level while writing to the new output stream. |
| 44 | AutoOutputStream(CodeGenerator* codeGen, OutputStream* newOutput) |
| 45 | : fCodeGen(codeGen) |
| 46 | , fOldOutput(codeGen->outputStream()) { |
| 47 | fCodeGen->setOutputStream(newOutput); |
| 48 | } |
| 49 | // Resets the indentation when entering the scope, and restores it when leaving. |
| 50 | AutoOutputStream(CodeGenerator* codeGen, OutputStream* newOutput, int *indentationPtr) |
| 51 | : fCodeGen(codeGen) |
| 52 | , fOldOutput(codeGen->outputStream()) |
| 53 | , fIndentationPtr(indentationPtr) |
| 54 | , fOldIndentation(indentationPtr ? *indentationPtr : 0) { |
| 55 | fCodeGen->setOutputStream(newOutput); |
| 56 | *fIndentationPtr = 0; |
| 57 | } |
| 58 | ~AutoOutputStream() { |
| 59 | fCodeGen->setOutputStream(fOldOutput); |
| 60 | if (fIndentationPtr) { |
| 61 | *fIndentationPtr = fOldIndentation; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | CodeGenerator* fCodeGen = nullptr; |
| 67 | OutputStream* fOldOutput = nullptr; |
| 68 | int *fIndentationPtr = nullptr; |
| 69 | int fOldIndentation = 0; |
| 70 | }; |
| 71 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 72 | } // namespace SkSL |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 73 | |
| 74 | #endif |