4453237e52
This mirrors other "AutoXxxxxxx" classes used in SkSL to push and pop temporary changes into member variables, such as AutoSymbolTable or AutoLoopLevel. Metal and Pipeline code generators were updated to use this class. SkSL was left as-is for now; it modifies fOut more extensively than the others and will need special care. Change-Id: Icf505b9b55e3458de349e35e3b812dd005f9afed Reviewed-on: https://skia-review.googlesource.com/c/skia/+/341457 Commit-Queue: John Stiles <johnstiles@google.com> Auto-Submit: John Stiles <johnstiles@google.com> Reviewed-by: Brian Osman <brianosman@google.com>
75 lines
2.0 KiB
C++
75 lines
2.0 KiB
C++
/*
|
|
* Copyright 2016 Google Inc.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*/
|
|
|
|
#ifndef SKSL_CODEGENERATOR
|
|
#define SKSL_CODEGENERATOR
|
|
|
|
#include "src/sksl/SkSLOutputStream.h"
|
|
#include "src/sksl/ir/SkSLProgram.h"
|
|
|
|
namespace SkSL {
|
|
|
|
/**
|
|
* Abstract superclass of all code generators, which take a Program as input and produce code as
|
|
* output.
|
|
*/
|
|
class CodeGenerator {
|
|
public:
|
|
CodeGenerator(const Program* program, ErrorReporter* errors, OutputStream* out)
|
|
: fProgram(*program)
|
|
, fErrors(*errors)
|
|
, fOut(out) {}
|
|
|
|
virtual ~CodeGenerator() {}
|
|
|
|
virtual bool generateCode() = 0;
|
|
|
|
// Intended for use by AutoOutputStream.
|
|
OutputStream* outputStream() { return fOut; }
|
|
void setOutputStream(OutputStream* output) { fOut = output; }
|
|
|
|
protected:
|
|
const Program& fProgram;
|
|
ErrorReporter& fErrors;
|
|
OutputStream* fOut;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
} // namespace SkSL
|
|
|
|
#endif
|