Commit d88b7736 by alokp@chromium.org

Do not write extraneous semi-colons - some glsl compilers are do not like that…

Do not write extraneous semi-colons - some glsl compilers are do not like that even though it is so easy to ignore. Review URL: http://codereview.appspot.com/1301041 git-svn-id: https://angleproject.googlecode.com/svn/trunk@317 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 90033b9e
...@@ -46,12 +46,30 @@ TString arrayBrackets(const TType& type) ...@@ -46,12 +46,30 @@ TString arrayBrackets(const TType& type)
out << "[" << type.getArraySize() << "]"; out << "[" << type.getArraySize() << "]";
return TString(out.c_str()); return TString(out.c_str());
} }
bool isSingleStatement(TIntermNode* node) {
const TIntermAggregate* aggregate = node->getAsAggregate();
if (aggregate != NULL)
{
if ((aggregate->getOp() == EOpFunction) ||
(aggregate->getOp() == EOpSequence))
return false;
}
else if (node->getAsSelectionNode() != NULL)
{
return false;
}
else if (node->getAsLoopNode() != NULL)
{
return false;
}
return true;
}
} // namespace } // namespace
TOutputGLSL::TOutputGLSL(TInfoSinkBase& objSink) TOutputGLSL::TOutputGLSL(TInfoSinkBase& objSink)
: TIntermTraverser(true, true, true), : TIntermTraverser(true, true, true),
mObjSink(objSink), mObjSink(objSink),
mScopeSequences(false),
mDeclaringVariables(false) mDeclaringVariables(false)
{ {
} }
...@@ -390,23 +408,17 @@ bool TOutputGLSL::visitSelection(Visit visit, TIntermSelection* node) ...@@ -390,23 +408,17 @@ bool TOutputGLSL::visitSelection(Visit visit, TIntermSelection* node)
{ {
out << "if ("; out << "if (";
node->getCondition()->traverse(this); node->getCondition()->traverse(this);
out << ") {\n"; out << ")\n";
incrementDepth(); incrementDepth();
if (node->getTrueBlock()) visitCodeBlock(node->getTrueBlock());
{
node->getTrueBlock()->traverse(this);
}
out << ";\n}";
if (node->getFalseBlock()) if (node->getFalseBlock())
{ {
out << " else {\n"; out << "else\n";
node->getFalseBlock()->traverse(this); visitCodeBlock(node->getFalseBlock());
out << ";\n}";
} }
decrementDepth(); decrementDepth();
out << "\n";
} }
return false; return false;
} }
...@@ -417,23 +429,29 @@ bool TOutputGLSL::visitAggregate(Visit visit, TIntermAggregate* node) ...@@ -417,23 +429,29 @@ bool TOutputGLSL::visitAggregate(Visit visit, TIntermAggregate* node)
TInfoSinkBase& out = objSink(); TInfoSinkBase& out = objSink();
switch (node->getOp()) switch (node->getOp())
{ {
case EOpSequence: case EOpSequence: {
if (visit == PreVisit) // Scope the sequences except when at the global scope.
{ if (depth > 0) out << "{\n";
if (mScopeSequences)
out << "{\n"; incrementDepth();
} const TIntermSequence& sequence = node->getSequence();
else if (visit == InVisit) for (TIntermSequence::const_iterator iter = sequence.begin();
{ iter != sequence.end(); ++iter)
out << ";\n";
}
else if (visit == PostVisit)
{ {
out << ";\n"; TIntermNode* node = *iter;
if (mScopeSequences) ASSERT(node != NULL);
out << "}\n"; node->traverse(this);
if (isSingleStatement(node))
out << ";\n";
} }
decrementDepth();
// Scope the sequences except when at the global scope.
if (depth > 0) out << "}\n";
visitChildren = false;
break; break;
}
case EOpPrototype: { case EOpPrototype: {
// Function declaration. // Function declaration.
ASSERT(visit == PreVisit); ASSERT(visit == PreVisit);
...@@ -454,6 +472,7 @@ bool TOutputGLSL::visitAggregate(Visit visit, TIntermAggregate* node) ...@@ -454,6 +472,7 @@ bool TOutputGLSL::visitAggregate(Visit visit, TIntermAggregate* node)
TString functionName = TFunction::unmangleName(node->getName()); TString functionName = TFunction::unmangleName(node->getName());
out << returnType << " " << functionName; out << returnType << " " << functionName;
incrementDepth();
// Function definition node contains one or two children nodes // Function definition node contains one or two children nodes
// representing function parameters and function body. The latter // representing function parameters and function body. The latter
// is not present in case of empty function bodies. // is not present in case of empty function bodies.
...@@ -470,20 +489,8 @@ bool TOutputGLSL::visitAggregate(Visit visit, TIntermAggregate* node) ...@@ -470,20 +489,8 @@ bool TOutputGLSL::visitAggregate(Visit visit, TIntermAggregate* node)
// Traverse function body. // Traverse function body.
TIntermAggregate* body = ++seqIter != sequence.end() ? TIntermAggregate* body = ++seqIter != sequence.end() ?
(*seqIter)->getAsAggregate() : NULL; (*seqIter)->getAsAggregate() : NULL;
if (body != NULL) visitCodeBlock(body);
{ decrementDepth();
ASSERT(body->getOp() == EOpSequence);
// Sequences are scoped with {} inside function body so that
// variables are declared in the correct scope.
mScopeSequences = true;
body->traverse(this);
mScopeSequences = false;
}
else
{
// Empty function body.
out << "{}\n";
}
// Fully processed; no need to visit children. // Fully processed; no need to visit children.
visitChildren = false; visitChildren = false;
...@@ -602,6 +609,7 @@ bool TOutputGLSL::visitLoop(Visit visit, TIntermLoop* node) ...@@ -602,6 +609,7 @@ bool TOutputGLSL::visitLoop(Visit visit, TIntermLoop* node)
{ {
TInfoSinkBase& out = objSink(); TInfoSinkBase& out = objSink();
incrementDepth();
// Loop header. // Loop header.
if (node->testFirst()) // for loop if (node->testFirst()) // for loop
{ {
...@@ -616,29 +624,25 @@ bool TOutputGLSL::visitLoop(Visit visit, TIntermLoop* node) ...@@ -616,29 +624,25 @@ bool TOutputGLSL::visitLoop(Visit visit, TIntermLoop* node)
if (node->getTerminal()) if (node->getTerminal())
node->getTerminal()->traverse(this); node->getTerminal()->traverse(this);
out << ") {\n"; out << ")\n";
} }
else // do-while loop else // do-while loop
{ {
out << "do {\n"; out << "do\n";
} }
// Loop body. // Loop body.
if (node->getBody()) visitCodeBlock(node->getBody());
node->getBody()->traverse(this);
// Loop footer. // Loop footer.
if (node->testFirst()) // for loop if (!node->testFirst()) // while loop
{
out << "}\n";
}
else // do-while loop
{ {
out << "} while ("; out << "while (";
ASSERT(node->getTest() != NULL); ASSERT(node->getTest() != NULL);
node->getTest()->traverse(this); node->getTest()->traverse(this);
out << ");\n"; out << ");\n";
} }
decrementDepth();
// No need to visit children. They have been already processed in // No need to visit children. They have been already processed in
// this function. // this function.
...@@ -660,3 +664,19 @@ bool TOutputGLSL::visitBranch(Visit visit, TIntermBranch* node) ...@@ -660,3 +664,19 @@ bool TOutputGLSL::visitBranch(Visit visit, TIntermBranch* node)
return true; return true;
} }
void TOutputGLSL::visitCodeBlock(TIntermNode* node) {
TInfoSinkBase &out = objSink();
if (node != NULL)
{
node->traverse(this);
// Single statements not part of a sequence need to be terminated
// with semi-colon.
if (isSingleStatement(node))
out << ";\n";
}
else
{
out << "{\n}\n"; // Empty code block.
}
}
...@@ -33,9 +33,10 @@ protected: ...@@ -33,9 +33,10 @@ protected:
virtual bool visitLoop(Visit visit, TIntermLoop* node); virtual bool visitLoop(Visit visit, TIntermLoop* node);
virtual bool visitBranch(Visit visit, TIntermBranch* node); virtual bool visitBranch(Visit visit, TIntermBranch* node);
void visitCodeBlock(TIntermNode* node);
private: private:
TInfoSinkBase& mObjSink; TInfoSinkBase& mObjSink;
bool mScopeSequences;
bool mDeclaringVariables; bool mDeclaringVariables;
// Structs are declared as the tree is traversed. This set contains all // Structs are declared as the tree is traversed. This set contains all
......
...@@ -192,6 +192,7 @@ class TIntermConstantUnion; ...@@ -192,6 +192,7 @@ class TIntermConstantUnion;
class TIntermSelection; class TIntermSelection;
class TIntermTyped; class TIntermTyped;
class TIntermSymbol; class TIntermSymbol;
class TIntermLoop;
class TInfoSink; class TInfoSink;
// //
...@@ -205,13 +206,14 @@ public: ...@@ -205,13 +206,14 @@ public:
virtual TSourceLoc getLine() const { return line; } virtual TSourceLoc getLine() const { return line; }
virtual void setLine(TSourceLoc l) { line = l; } virtual void setLine(TSourceLoc l) { line = l; }
virtual void traverse(TIntermTraverser*) = 0; virtual void traverse(TIntermTraverser*) = 0;
virtual TIntermTyped* getAsTyped() { return 0; } virtual TIntermTyped* getAsTyped() { return 0; }
virtual TIntermConstantUnion* getAsConstantUnion() { return 0; } virtual TIntermConstantUnion* getAsConstantUnion() { return 0; }
virtual TIntermAggregate* getAsAggregate() { return 0; } virtual TIntermAggregate* getAsAggregate() { return 0; }
virtual TIntermBinary* getAsBinaryNode() { return 0; } virtual TIntermBinary* getAsBinaryNode() { return 0; }
virtual TIntermUnary* getAsUnaryNode() { return 0; } virtual TIntermUnary* getAsUnaryNode() { return 0; }
virtual TIntermSelection* getAsSelectionNode() { return 0; } virtual TIntermSelection* getAsSelectionNode() { return 0; }
virtual TIntermSymbol* getAsSymbolNode() { return 0; } virtual TIntermSymbol* getAsSymbolNode() { return 0; }
virtual TIntermLoop* getAsLoopNode() { return 0; }
virtual ~TIntermNode() { } virtual ~TIntermNode() { }
protected: protected:
TSourceLoc line; TSourceLoc line;
...@@ -264,6 +266,7 @@ public: ...@@ -264,6 +266,7 @@ public:
test(aTest), test(aTest),
terminal(aTerminal), terminal(aTerminal),
first(testFirst) { } first(testFirst) { }
virtual TIntermLoop* getAsLoopNode() { return this; }
virtual void traverse(TIntermTraverser*); virtual void traverse(TIntermTraverser*);
TIntermNode *getInit() { return init; } TIntermNode *getInit() { return init; }
TIntermNode *getBody() { return body; } TIntermNode *getBody() { return body; }
......
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