Commit 5df2b9d2 by Olli Etuaho Committed by Commit Bot

Clean up AddDefaultReturnStatements

It doesn't need to use a traverser, since all function definitions can be found simply by iterating over the children of the root node. BUG=angleproject:2040 TEST=angle_end2end_tests Change-Id: I380942f90a0e73152f296b98d1fb027762d913b0 Reviewed-on: https://chromium-review.googlesource.com/508689Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
parent df22132f
...@@ -18,59 +18,41 @@ namespace sh ...@@ -18,59 +18,41 @@ namespace sh
namespace namespace
{ {
class AddDefaultReturnStatementsTraverser : private TIntermTraverser bool NeedsReturnStatement(TIntermFunctionDefinition *node, TType *returnType)
{ {
public: *returnType = node->getFunctionPrototype()->getType();
static void Apply(TIntermNode *root) if (returnType->getBasicType() == EbtVoid)
{ {
AddDefaultReturnStatementsTraverser separateInit; return false;
root->traverse(&separateInit);
separateInit.updateTree();
} }
private: TIntermBlock *bodyNode = node->getBody();
AddDefaultReturnStatementsTraverser() : TIntermTraverser(true, false, false) {} TIntermBranch *returnNode = bodyNode->getSequence()->back()->getAsBranchNode();
if (returnNode != nullptr && returnNode->getFlowOp() == EOpReturn)
static bool IsFunctionWithoutReturnStatement(TIntermFunctionDefinition *node, TType *returnType)
{ {
*returnType = node->getFunctionPrototype()->getType(); return false;
if (returnType->getBasicType() == EbtVoid) }
{
return false;
}
TIntermBlock *bodyNode = node->getBody(); return true;
TIntermBranch *returnNode = bodyNode->getSequence()->back()->getAsBranchNode(); }
if (returnNode != nullptr && returnNode->getFlowOp() == EOpReturn)
{
return false;
}
return true; } // anonymous namespace
}
bool visitFunctionDefinition(Visit, TIntermFunctionDefinition *node) override void AddDefaultReturnStatements(TIntermBlock *root)
{
TType returnType;
for (TIntermNode *node : *root->getSequence())
{ {
TType returnType; TIntermFunctionDefinition *definition = node->getAsFunctionDefinition();
if (IsFunctionWithoutReturnStatement(node, &returnType)) if (definition != nullptr && NeedsReturnStatement(definition, &returnType))
{ {
TIntermBranch *branch = TIntermBranch *branch =
new TIntermBranch(EOpReturn, TIntermTyped::CreateZero(returnType)); new TIntermBranch(EOpReturn, TIntermTyped::CreateZero(returnType));
TIntermBlock *bodyNode = node->getBody(); TIntermBlock *bodyNode = definition->getBody();
bodyNode->getSequence()->push_back(branch); bodyNode->getSequence()->push_back(branch);
return false;
} }
return true;
} }
};
} // anonymous namespace
void AddDefaultReturnStatements(TIntermNode *node)
{
AddDefaultReturnStatementsTraverser::Apply(node);
} }
} // namespace sh } // namespace sh
...@@ -10,12 +10,12 @@ ...@@ -10,12 +10,12 @@
#ifndef COMPILER_TRANSLATOR_ADDDEFAULTRETURNSTATEMENTS_H_ #ifndef COMPILER_TRANSLATOR_ADDDEFAULTRETURNSTATEMENTS_H_
#define COMPILER_TRANSLATOR_ADDDEFAULTRETURNSTATEMENTS_H_ #define COMPILER_TRANSLATOR_ADDDEFAULTRETURNSTATEMENTS_H_
class TIntermNode; class TIntermBlock;
namespace sh namespace sh
{ {
void AddDefaultReturnStatements(TIntermNode *node); void AddDefaultReturnStatements(TIntermBlock *root);
} // namespace sh } // namespace sh
......
...@@ -139,7 +139,7 @@ class TCompiler : public TShHandleBase ...@@ -139,7 +139,7 @@ class TCompiler : public TShHandleBase
virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
ShCompileOptions compileOptions){}; ShCompileOptions compileOptions){};
// Translate to object code. // Translate to object code.
virtual void translate(TIntermNode *root, ShCompileOptions compileOptions) = 0; virtual void translate(TIntermBlock *root, ShCompileOptions compileOptions) = 0;
// Returns true if, after applying the packing rules in the GLSL 1.017 spec // Returns true if, after applying the packing rules in the GLSL 1.017 spec
// Appendix A, section 7, the shader does not use too many uniforms. // Appendix A, section 7, the shader does not use too many uniforms.
bool enforcePackingRestrictions(); bool enforcePackingRestrictions();
......
...@@ -30,7 +30,7 @@ void TranslatorESSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, ...@@ -30,7 +30,7 @@ void TranslatorESSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
} }
} }
void TranslatorESSL::translate(TIntermNode *root, ShCompileOptions compileOptions) void TranslatorESSL::translate(TIntermBlock *root, ShCompileOptions compileOptions)
{ {
// The ESSL output doesn't define a default precision for float, so float literal statements // The ESSL output doesn't define a default precision for float, so float literal statements
// end up with no precision which is invalid ESSL. // end up with no precision which is invalid ESSL.
......
...@@ -21,7 +21,7 @@ class TranslatorESSL : public TCompiler ...@@ -21,7 +21,7 @@ class TranslatorESSL : public TCompiler
void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
ShCompileOptions compileOptions) override; ShCompileOptions compileOptions) override;
void translate(TIntermNode *root, ShCompileOptions compileOptions) override; void translate(TIntermBlock *root, ShCompileOptions compileOptions) override;
bool shouldFlattenPragmaStdglInvariantAll() override; bool shouldFlattenPragmaStdglInvariantAll() override;
private: private:
......
...@@ -45,7 +45,7 @@ void TranslatorGLSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, ...@@ -45,7 +45,7 @@ void TranslatorGLSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
InitBuiltInFunctionEmulatorForGLSLMissingFunctions(emu, getShaderType(), targetGLSLVersion); InitBuiltInFunctionEmulatorForGLSLMissingFunctions(emu, getShaderType(), targetGLSLVersion);
} }
void TranslatorGLSL::translate(TIntermNode *root, ShCompileOptions compileOptions) void TranslatorGLSL::translate(TIntermBlock *root, ShCompileOptions compileOptions)
{ {
TInfoSinkBase &sink = getInfoSink().obj; TInfoSinkBase &sink = getInfoSink().obj;
......
...@@ -21,7 +21,7 @@ class TranslatorGLSL : public TCompiler ...@@ -21,7 +21,7 @@ class TranslatorGLSL : public TCompiler
void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
ShCompileOptions compileOptions) override; ShCompileOptions compileOptions) override;
void translate(TIntermNode *root, ShCompileOptions compileOptions) override; void translate(TIntermBlock *root, ShCompileOptions compileOptions) override;
bool shouldFlattenPragmaStdglInvariantAll() override; bool shouldFlattenPragmaStdglInvariantAll() override;
bool shouldCollectVariables(ShCompileOptions compileOptions) override; bool shouldCollectVariables(ShCompileOptions compileOptions) override;
......
...@@ -32,7 +32,7 @@ TranslatorHLSL::TranslatorHLSL(sh::GLenum type, ShShaderSpec spec, ShShaderOutpu ...@@ -32,7 +32,7 @@ TranslatorHLSL::TranslatorHLSL(sh::GLenum type, ShShaderSpec spec, ShShaderOutpu
{ {
} }
void TranslatorHLSL::translate(TIntermNode *root, ShCompileOptions compileOptions) void TranslatorHLSL::translate(TIntermBlock *root, ShCompileOptions compileOptions)
{ {
const ShBuiltInResources &resources = getResources(); const ShBuiltInResources &resources = getResources();
int numRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1; int numRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
......
...@@ -24,7 +24,7 @@ class TranslatorHLSL : public TCompiler ...@@ -24,7 +24,7 @@ class TranslatorHLSL : public TCompiler
const std::map<std::string, unsigned int> *getUniformRegisterMap() const; const std::map<std::string, unsigned int> *getUniformRegisterMap() const;
protected: protected:
void translate(TIntermNode *root, ShCompileOptions compileOptions) override; void translate(TIntermBlock *root, ShCompileOptions compileOptions) override;
bool shouldFlattenPragmaStdglInvariantAll() override; bool shouldFlattenPragmaStdglInvariantAll() override;
// collectVariables needs to be run always so registers can be assigned. // collectVariables needs to be run always so registers can be assigned.
......
...@@ -22,7 +22,7 @@ TranslatorVulkan::TranslatorVulkan(sh::GLenum type, ShShaderSpec spec) ...@@ -22,7 +22,7 @@ TranslatorVulkan::TranslatorVulkan(sh::GLenum type, ShShaderSpec spec)
{ {
} }
void TranslatorVulkan::translate(TIntermNode *root, ShCompileOptions compileOptions) void TranslatorVulkan::translate(TIntermBlock *root, ShCompileOptions compileOptions)
{ {
TInfoSinkBase &sink = getInfoSink().obj; TInfoSinkBase &sink = getInfoSink().obj;
......
...@@ -23,7 +23,7 @@ class TranslatorVulkan : public TCompiler ...@@ -23,7 +23,7 @@ class TranslatorVulkan : public TCompiler
TranslatorVulkan(sh::GLenum type, ShShaderSpec spec); TranslatorVulkan(sh::GLenum type, ShShaderSpec spec);
protected: protected:
void translate(TIntermNode *root, ShCompileOptions compileOptions) override; void translate(TIntermBlock *root, ShCompileOptions compileOptions) override;
bool shouldFlattenPragmaStdglInvariantAll() override; bool shouldFlattenPragmaStdglInvariantAll() override;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment