2016-07-01 15:22:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2017-03-31 17:56:23 +00:00
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
#ifndef SKSL_CODEGENERATOR
|
|
|
|
#define SKSL_CODEGENERATOR
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/sksl/SkSLOutputStream.h"
|
|
|
|
#include "src/sksl/ir/SkSLProgram.h"
|
2016-07-01 15:22:01 +00:00
|
|
|
|
|
|
|
namespace SkSL {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract superclass of all code generators, which take a Program as input and produce code as
|
|
|
|
* output.
|
|
|
|
*/
|
|
|
|
class CodeGenerator {
|
|
|
|
public:
|
2017-03-31 17:56:23 +00:00
|
|
|
CodeGenerator(const Program* program, ErrorReporter* errors, OutputStream* out)
|
2016-12-12 20:33:30 +00:00
|
|
|
: fProgram(*program)
|
|
|
|
, fErrors(*errors)
|
2020-09-14 15:33:47 +00:00
|
|
|
, fOut(out) {}
|
2016-12-12 20:33:30 +00:00
|
|
|
|
2016-08-03 19:43:36 +00:00
|
|
|
virtual ~CodeGenerator() {}
|
2016-11-28 21:30:17 +00:00
|
|
|
|
2016-12-12 20:33:30 +00:00
|
|
|
virtual bool generateCode() = 0;
|
|
|
|
|
2020-12-07 17:33:55 +00:00
|
|
|
// Intended for use by AutoOutputStream.
|
|
|
|
OutputStream* outputStream() { return fOut; }
|
|
|
|
void setOutputStream(OutputStream* output) { fOut = output; }
|
2016-12-12 20:33:30 +00:00
|
|
|
|
2020-12-07 17:33:55 +00:00
|
|
|
protected:
|
2016-12-12 20:33:30 +00:00
|
|
|
const Program& fProgram;
|
|
|
|
ErrorReporter& fErrors;
|
2017-03-31 17:56:23 +00:00
|
|
|
OutputStream* fOut;
|
2016-07-01 15:22:01 +00:00
|
|
|
};
|
|
|
|
|
2020-12-07 17:33:55 +00:00
|
|
|
class AutoOutputStream {
|
|
|
|
public:
|
|
|
|
// Maintains the current indentation level while writing to the new output stream.
|
|
|
|
AutoOutputStream(CodeGenerator* codeGen, OutputStream* newOutput)
|
|
|
|
: fCodeGen(codeGen)
|
|
|
|
, fOldOutput(codeGen->outputStream()) {
|
|
|
|
fCodeGen->setOutputStream(newOutput);
|
|
|
|
}
|
|
|
|
// Resets the indentation when entering the scope, and restores it when leaving.
|
|
|
|
AutoOutputStream(CodeGenerator* codeGen, OutputStream* newOutput, int *indentationPtr)
|
|
|
|
: fCodeGen(codeGen)
|
|
|
|
, fOldOutput(codeGen->outputStream())
|
|
|
|
, fIndentationPtr(indentationPtr)
|
|
|
|
, fOldIndentation(indentationPtr ? *indentationPtr : 0) {
|
|
|
|
fCodeGen->setOutputStream(newOutput);
|
|
|
|
*fIndentationPtr = 0;
|
|
|
|
}
|
|
|
|
~AutoOutputStream() {
|
|
|
|
fCodeGen->setOutputStream(fOldOutput);
|
|
|
|
if (fIndentationPtr) {
|
|
|
|
*fIndentationPtr = fOldIndentation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CodeGenerator* fCodeGen = nullptr;
|
|
|
|
OutputStream* fOldOutput = nullptr;
|
|
|
|
int *fIndentationPtr = nullptr;
|
|
|
|
int fOldIndentation = 0;
|
|
|
|
};
|
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace SkSL
|
2016-07-01 15:22:01 +00:00
|
|
|
|
|
|
|
#endif
|