1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2016 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#ifndef SKSL_CODEGENERATOR 9cb93a386Sopenharmony_ci#define SKSL_CODEGENERATOR 10cb93a386Sopenharmony_ci 11cb93a386Sopenharmony_ci#include "src/sksl/SkSLOutputStream.h" 12cb93a386Sopenharmony_ci#include "src/sksl/ir/SkSLProgram.h" 13cb93a386Sopenharmony_ci 14cb93a386Sopenharmony_cinamespace SkSL { 15cb93a386Sopenharmony_ci 16cb93a386Sopenharmony_ci/** 17cb93a386Sopenharmony_ci * Abstract superclass of all code generators, which take a Program as input and produce code as 18cb93a386Sopenharmony_ci * output. 19cb93a386Sopenharmony_ci */ 20cb93a386Sopenharmony_ciclass CodeGenerator { 21cb93a386Sopenharmony_cipublic: 22cb93a386Sopenharmony_ci CodeGenerator(const Context* context, const Program* program, OutputStream* out) 23cb93a386Sopenharmony_ci : fContext(*context) 24cb93a386Sopenharmony_ci , fProgram(*program) 25cb93a386Sopenharmony_ci , fOut(out) {} 26cb93a386Sopenharmony_ci 27cb93a386Sopenharmony_ci virtual ~CodeGenerator() {} 28cb93a386Sopenharmony_ci 29cb93a386Sopenharmony_ci virtual bool generateCode() = 0; 30cb93a386Sopenharmony_ci 31cb93a386Sopenharmony_ci // Intended for use by AutoOutputStream. 32cb93a386Sopenharmony_ci OutputStream* outputStream() { return fOut; } 33cb93a386Sopenharmony_ci void setOutputStream(OutputStream* output) { fOut = output; } 34cb93a386Sopenharmony_ci 35cb93a386Sopenharmony_ciprotected: 36cb93a386Sopenharmony_ci const Context& fContext; 37cb93a386Sopenharmony_ci const Program& fProgram; 38cb93a386Sopenharmony_ci OutputStream* fOut; 39cb93a386Sopenharmony_ci}; 40cb93a386Sopenharmony_ci 41cb93a386Sopenharmony_ciclass AutoOutputStream { 42cb93a386Sopenharmony_cipublic: 43cb93a386Sopenharmony_ci // Maintains the current indentation level while writing to the new output stream. 44cb93a386Sopenharmony_ci AutoOutputStream(CodeGenerator* codeGen, OutputStream* newOutput) 45cb93a386Sopenharmony_ci : fCodeGen(codeGen) 46cb93a386Sopenharmony_ci , fOldOutput(codeGen->outputStream()) { 47cb93a386Sopenharmony_ci fCodeGen->setOutputStream(newOutput); 48cb93a386Sopenharmony_ci } 49cb93a386Sopenharmony_ci // Resets the indentation when entering the scope, and restores it when leaving. 50cb93a386Sopenharmony_ci AutoOutputStream(CodeGenerator* codeGen, OutputStream* newOutput, int *indentationPtr) 51cb93a386Sopenharmony_ci : fCodeGen(codeGen) 52cb93a386Sopenharmony_ci , fOldOutput(codeGen->outputStream()) 53cb93a386Sopenharmony_ci , fIndentationPtr(indentationPtr) 54cb93a386Sopenharmony_ci , fOldIndentation(indentationPtr ? *indentationPtr : 0) { 55cb93a386Sopenharmony_ci fCodeGen->setOutputStream(newOutput); 56cb93a386Sopenharmony_ci *fIndentationPtr = 0; 57cb93a386Sopenharmony_ci } 58cb93a386Sopenharmony_ci ~AutoOutputStream() { 59cb93a386Sopenharmony_ci fCodeGen->setOutputStream(fOldOutput); 60cb93a386Sopenharmony_ci if (fIndentationPtr) { 61cb93a386Sopenharmony_ci *fIndentationPtr = fOldIndentation; 62cb93a386Sopenharmony_ci } 63cb93a386Sopenharmony_ci } 64cb93a386Sopenharmony_ci 65cb93a386Sopenharmony_ciprivate: 66cb93a386Sopenharmony_ci CodeGenerator* fCodeGen = nullptr; 67cb93a386Sopenharmony_ci OutputStream* fOldOutput = nullptr; 68cb93a386Sopenharmony_ci int *fIndentationPtr = nullptr; 69cb93a386Sopenharmony_ci int fOldIndentation = 0; 70cb93a386Sopenharmony_ci}; 71cb93a386Sopenharmony_ci 72cb93a386Sopenharmony_ci} // namespace SkSL 73cb93a386Sopenharmony_ci 74cb93a386Sopenharmony_ci#endif 75