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
namespace
{
class AddDefaultReturnStatementsTraverser : private TIntermTraverser
bool NeedsReturnStatement(TIntermFunctionDefinition *node, TType *returnType)
{
public:
static void Apply(TIntermNode *root)
*returnType = node->getFunctionPrototype()->getType();
if (returnType->getBasicType() == EbtVoid)
{
AddDefaultReturnStatementsTraverser separateInit;
root->traverse(&separateInit);
separateInit.updateTree();
return false;
}
private:
AddDefaultReturnStatementsTraverser() : TIntermTraverser(true, false, false) {}
static bool IsFunctionWithoutReturnStatement(TIntermFunctionDefinition *node, TType *returnType)
TIntermBlock *bodyNode = node->getBody();
TIntermBranch *returnNode = bodyNode->getSequence()->back()->getAsBranchNode();
if (returnNode != nullptr && returnNode->getFlowOp() == EOpReturn)
{
*returnType = node->getFunctionPrototype()->getType();
if (returnType->getBasicType() == EbtVoid)
{
return false;
}
return false;
}
TIntermBlock *bodyNode = node->getBody();
TIntermBranch *returnNode = bodyNode->getSequence()->back()->getAsBranchNode();
if (returnNode != nullptr && returnNode->getFlowOp() == EOpReturn)
{
return false;
}
return true;
}
return true;
}
} // anonymous namespace
bool visitFunctionDefinition(Visit, TIntermFunctionDefinition *node) override
void AddDefaultReturnStatements(TIntermBlock *root)
{
TType returnType;
for (TIntermNode *node : *root->getSequence())
{
TType returnType;
if (IsFunctionWithoutReturnStatement(node, &returnType))
TIntermFunctionDefinition *definition = node->getAsFunctionDefinition();
if (definition != nullptr && NeedsReturnStatement(definition, &returnType))
{
TIntermBranch *branch =
new TIntermBranch(EOpReturn, TIntermTyped::CreateZero(returnType));
TIntermBlock *bodyNode = node->getBody();
TIntermBlock *bodyNode = definition->getBody();
bodyNode->getSequence()->push_back(branch);
return false;
}
return true;
}
};
} // anonymous namespace
void AddDefaultReturnStatements(TIntermNode *node)
{
AddDefaultReturnStatementsTraverser::Apply(node);
}
} // namespace sh
......@@ -10,12 +10,12 @@
#ifndef COMPILER_TRANSLATOR_ADDDEFAULTRETURNSTATEMENTS_H_
#define COMPILER_TRANSLATOR_ADDDEFAULTRETURNSTATEMENTS_H_
class TIntermNode;
class TIntermBlock;
namespace sh
{
void AddDefaultReturnStatements(TIntermNode *node);
void AddDefaultReturnStatements(TIntermBlock *root);
} // namespace sh
......
......@@ -139,7 +139,7 @@ class TCompiler : public TShHandleBase
virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
ShCompileOptions compileOptions){};
// 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
// Appendix A, section 7, the shader does not use too many uniforms.
bool enforcePackingRestrictions();
......
......@@ -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
// end up with no precision which is invalid ESSL.
......
......@@ -21,7 +21,7 @@ class TranslatorESSL : public TCompiler
void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
ShCompileOptions compileOptions) override;
void translate(TIntermNode *root, ShCompileOptions compileOptions) override;
void translate(TIntermBlock *root, ShCompileOptions compileOptions) override;
bool shouldFlattenPragmaStdglInvariantAll() override;
private:
......
......@@ -45,7 +45,7 @@ void TranslatorGLSL::initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
InitBuiltInFunctionEmulatorForGLSLMissingFunctions(emu, getShaderType(), targetGLSLVersion);
}
void TranslatorGLSL::translate(TIntermNode *root, ShCompileOptions compileOptions)
void TranslatorGLSL::translate(TIntermBlock *root, ShCompileOptions compileOptions)
{
TInfoSinkBase &sink = getInfoSink().obj;
......
......@@ -21,7 +21,7 @@ class TranslatorGLSL : public TCompiler
void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
ShCompileOptions compileOptions) override;
void translate(TIntermNode *root, ShCompileOptions compileOptions) override;
void translate(TIntermBlock *root, ShCompileOptions compileOptions) override;
bool shouldFlattenPragmaStdglInvariantAll() override;
bool shouldCollectVariables(ShCompileOptions compileOptions) override;
......
......@@ -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();
int numRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
......
......@@ -24,7 +24,7 @@ class TranslatorHLSL : public TCompiler
const std::map<std::string, unsigned int> *getUniformRegisterMap() const;
protected:
void translate(TIntermNode *root, ShCompileOptions compileOptions) override;
void translate(TIntermBlock *root, ShCompileOptions compileOptions) override;
bool shouldFlattenPragmaStdglInvariantAll() override;
// collectVariables needs to be run always so registers can be assigned.
......
......@@ -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;
......
......@@ -23,7 +23,7 @@ class TranslatorVulkan : public TCompiler
TranslatorVulkan(sh::GLenum type, ShShaderSpec spec);
protected:
void translate(TIntermNode *root, ShCompileOptions compileOptions) override;
void translate(TIntermBlock *root, ShCompileOptions compileOptions) 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