Commit 40dbdd6c by Olli Etuaho Committed by Commit Bot

Clean up remaining extra semicolons from HLSL output

There are many types of statements after which a semicolon is not needed. Skip writing the semicolon in HLSL output in these cases to make the output code more readable. BUG=angleproject:1013 TEST=angle_end2end_tests Change-Id: I8f6a5e4ecfe5def456fdf19cca5ca451c13d7f35 Reviewed-on: https://chromium-review.googlesource.com/718420 Commit-Queue: Olli Etuaho <oetuaho@nvidia.com> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org>
parent 3755c48d
...@@ -106,6 +106,7 @@ class TIntermNode : angle::NonCopyable ...@@ -106,6 +106,7 @@ class TIntermNode : angle::NonCopyable
virtual TIntermAggregate *getAsAggregate() { return 0; } virtual TIntermAggregate *getAsAggregate() { return 0; }
virtual TIntermBlock *getAsBlock() { return nullptr; } virtual TIntermBlock *getAsBlock() { return nullptr; }
virtual TIntermFunctionPrototype *getAsFunctionPrototypeNode() { return nullptr; } virtual TIntermFunctionPrototype *getAsFunctionPrototypeNode() { return nullptr; }
virtual TIntermInvariantDeclaration *getAsInvariantDeclarationNode() { return nullptr; }
virtual TIntermDeclaration *getAsDeclarationNode() { return nullptr; } virtual TIntermDeclaration *getAsDeclarationNode() { return nullptr; }
virtual TIntermSwizzle *getAsSwizzleNode() { return nullptr; } virtual TIntermSwizzle *getAsSwizzleNode() { return nullptr; }
virtual TIntermBinary *getAsBinaryNode() { return 0; } virtual TIntermBinary *getAsBinaryNode() { return 0; }
...@@ -795,6 +796,8 @@ class TIntermInvariantDeclaration : public TIntermNode ...@@ -795,6 +796,8 @@ class TIntermInvariantDeclaration : public TIntermNode
public: public:
TIntermInvariantDeclaration(TIntermSymbol *symbol, const TSourceLoc &line); TIntermInvariantDeclaration(TIntermSymbol *symbol, const TSourceLoc &line);
virtual TIntermInvariantDeclaration *getAsInvariantDeclarationNode() override { return this; }
TIntermSymbol *getSymbol() { return mSymbol; } TIntermSymbol *getSymbol() { return mSymbol; }
void traverse(TIntermTraverser *it) override; void traverse(TIntermTraverser *it) override;
......
...@@ -46,6 +46,16 @@ TString ArrayHelperFunctionName(const char *prefix, const TType &type) ...@@ -46,6 +46,16 @@ TString ArrayHelperFunctionName(const char *prefix, const TType &type)
return fnName.str(); return fnName.str();
} }
bool IsDeclarationWrittenOut(TIntermDeclaration *node)
{
TIntermSequence *sequence = node->getSequence();
TIntermTyped *variable = (*sequence)[0]->getAsTyped();
ASSERT(sequence->size() == 1);
ASSERT(variable);
return (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal ||
variable->getQualifier() == EvqConst);
}
} // anonymous namespace } // anonymous namespace
void OutputHLSL::writeFloat(TInfoSinkBase &out, float f) void OutputHLSL::writeFloat(TInfoSinkBase &out, float f)
...@@ -1610,21 +1620,33 @@ bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node) ...@@ -1610,21 +1620,33 @@ bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
out << "{\n"; out << "{\n";
} }
for (TIntermSequence::iterator sit = node->getSequence()->begin(); for (TIntermNode *statement : *node->getSequence())
sit != node->getSequence()->end(); sit++)
{ {
outputLineDirective(out, (*sit)->getLine().first_line); outputLineDirective(out, statement->getLine().first_line);
(*sit)->traverse(this); statement->traverse(this);
// Don't output ; after case labels, they're terminated by : // Don't output ; after case labels, they're terminated by :
// This is needed especially since outputting a ; after a case statement would turn empty // This is needed especially since outputting a ; after a case statement would turn empty
// case statements into non-empty case statements, disallowing fall-through from them. // case statements into non-empty case statements, disallowing fall-through from them.
// Also no need to output ; after if statements or sequences. This is done just for // Also the output code is clearer if we don't output ; after statements where it is not
// code clarity. // needed:
if ((*sit)->getAsCaseNode() == nullptr && (*sit)->getAsIfElseNode() == nullptr && // * if statements
(*sit)->getAsBlock() == nullptr) // * switch statements
// * blocks
// * function definitions
// * loops (do-while loops output the semicolon in VisitLoop)
// * declarations that don't generate output.
if (statement->getAsCaseNode() == nullptr && statement->getAsIfElseNode() == nullptr &&
statement->getAsBlock() == nullptr && statement->getAsLoopNode() == nullptr &&
statement->getAsSwitchNode() == nullptr &&
statement->getAsFunctionDefinition() == nullptr &&
(statement->getAsDeclarationNode() == nullptr ||
IsDeclarationWrittenOut(statement->getAsDeclarationNode())) &&
statement->getAsInvariantDeclarationNode() == nullptr)
{
out << ";\n"; out << ";\n";
}
} }
if (mInsideFunction) if (mInsideFunction)
...@@ -1702,7 +1724,6 @@ bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition ...@@ -1702,7 +1724,6 @@ bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition
bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node) bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
{ {
TInfoSinkBase &out = getInfoSink();
if (visit == PreVisit) if (visit == PreVisit)
{ {
TIntermSequence *sequence = node->getSequence(); TIntermSequence *sequence = node->getSequence();
...@@ -1710,9 +1731,9 @@ bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node) ...@@ -1710,9 +1731,9 @@ bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
ASSERT(sequence->size() == 1); ASSERT(sequence->size() == 1);
ASSERT(variable); ASSERT(variable);
if ((variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal || if (IsDeclarationWrittenOut(node))
variable->getQualifier() == EvqConst))
{ {
TInfoSinkBase &out = getInfoSink();
ensureStructDefined(variable->getType()); ensureStructDefined(variable->getType());
if (!variable->getAsSymbolNode() || if (!variable->getAsSymbolNode() ||
...@@ -2240,11 +2261,11 @@ bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node) ...@@ -2240,11 +2261,11 @@ bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
if (node->getType() == ELoopDoWhile) if (node->getType() == ELoopDoWhile)
{ {
outputLineDirective(out, node->getCondition()->getLine().first_line); outputLineDirective(out, node->getCondition()->getLine().first_line);
out << "while(\n"; out << "while (";
node->getCondition()->traverse(this); node->getCondition()->traverse(this);
out << ");"; out << ");\n";
} }
out << "}\n"; out << "}\n";
......
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