Commit 1283ef62 by Jamie Madill Committed by Commit Bot

translator: Add TIntermPreprocessorDirective.

This new node type can be used to inject preprocessor directives into the intermediate tree. Outputting the preprocessor directive is easy for the GLSL back-end. This can be used to implement workarounds such as the OpenGL line rasterization rules on Vulkan. Also fixes the build that was broken on Win/x64 with a prior change and makes more methods in IntermNode.h const. Bug: angleproject:2598 Change-Id: Ifd6d0ac7912ccf2137997bb9a0187fd063023d5e Reviewed-on: https://chromium-review.googlesource.com/1133420Reviewed-by: 's avatarOlli Etuaho <oetuaho@nvidia.com> Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
parent 9ff9c773
......@@ -202,34 +202,34 @@ void TIntermExpression::setTypePreservePrecision(const TType &t)
return true; \
}
unsigned int TIntermSymbol::getChildCount()
size_t TIntermSymbol::getChildCount() const
{
return 0;
}
TIntermNode *TIntermSymbol::getChildNode(unsigned int index)
TIntermNode *TIntermSymbol::getChildNode(size_t index) const
{
UNREACHABLE();
return nullptr;
}
unsigned int TIntermConstantUnion::getChildCount()
size_t TIntermConstantUnion::getChildCount() const
{
return 0;
}
TIntermNode *TIntermConstantUnion::getChildNode(unsigned int index)
TIntermNode *TIntermConstantUnion::getChildNode(size_t index) const
{
UNREACHABLE();
return nullptr;
}
unsigned int TIntermLoop::getChildCount()
size_t TIntermLoop::getChildCount() const
{
return (mInit ? 1 : 0) + (mCond ? 1 : 0) + (mExpr ? 1 : 0) + (mBody ? 1 : 0);
}
TIntermNode *TIntermLoop::getChildNode(unsigned int index)
TIntermNode *TIntermLoop::getChildNode(size_t index) const
{
TIntermNode *children[4];
unsigned int childIndex = 0;
......@@ -267,12 +267,12 @@ bool TIntermLoop::replaceChildNode(TIntermNode *original, TIntermNode *replaceme
return false;
}
unsigned int TIntermBranch::getChildCount()
size_t TIntermBranch::getChildCount() const
{
return (mExpression ? 1 : 0);
}
TIntermNode *TIntermBranch::getChildNode(unsigned int index)
TIntermNode *TIntermBranch::getChildNode(size_t index) const
{
ASSERT(mExpression);
ASSERT(index == 0);
......@@ -285,12 +285,12 @@ bool TIntermBranch::replaceChildNode(TIntermNode *original, TIntermNode *replace
return false;
}
unsigned int TIntermSwizzle::getChildCount()
size_t TIntermSwizzle::getChildCount() const
{
return 1;
}
TIntermNode *TIntermSwizzle::getChildNode(unsigned int index)
TIntermNode *TIntermSwizzle::getChildNode(size_t index) const
{
ASSERT(mOperand);
ASSERT(index == 0);
......@@ -304,12 +304,12 @@ bool TIntermSwizzle::replaceChildNode(TIntermNode *original, TIntermNode *replac
return false;
}
unsigned int TIntermBinary::getChildCount()
size_t TIntermBinary::getChildCount() const
{
return 2;
}
TIntermNode *TIntermBinary::getChildNode(unsigned int index)
TIntermNode *TIntermBinary::getChildNode(size_t index) const
{
ASSERT(index < 2);
if (index == 0)
......@@ -326,12 +326,12 @@ bool TIntermBinary::replaceChildNode(TIntermNode *original, TIntermNode *replace
return false;
}
unsigned int TIntermUnary::getChildCount()
size_t TIntermUnary::getChildCount() const
{
return 1;
}
TIntermNode *TIntermUnary::getChildNode(unsigned int index)
TIntermNode *TIntermUnary::getChildNode(size_t index) const
{
ASSERT(mOperand);
ASSERT(index == 0);
......@@ -345,12 +345,12 @@ bool TIntermUnary::replaceChildNode(TIntermNode *original, TIntermNode *replacem
return false;
}
unsigned int TIntermInvariantDeclaration::getChildCount()
size_t TIntermInvariantDeclaration::getChildCount() const
{
return 1;
}
TIntermNode *TIntermInvariantDeclaration::getChildNode(unsigned int index)
TIntermNode *TIntermInvariantDeclaration::getChildNode(size_t index) const
{
ASSERT(mSymbol);
ASSERT(index == 0);
......@@ -363,12 +363,12 @@ bool TIntermInvariantDeclaration::replaceChildNode(TIntermNode *original, TInter
return false;
}
unsigned int TIntermFunctionDefinition::getChildCount()
size_t TIntermFunctionDefinition::getChildCount() const
{
return 2;
}
TIntermNode *TIntermFunctionDefinition::getChildNode(unsigned int index)
TIntermNode *TIntermFunctionDefinition::getChildNode(size_t index) const
{
ASSERT(index < 2);
if (index == 0)
......@@ -385,12 +385,12 @@ bool TIntermFunctionDefinition::replaceChildNode(TIntermNode *original, TIntermN
return false;
}
unsigned int TIntermAggregate::getChildCount()
size_t TIntermAggregate::getChildCount() const
{
return mArguments.size();
}
TIntermNode *TIntermAggregate::getChildNode(unsigned int index)
TIntermNode *TIntermAggregate::getChildNode(size_t index) const
{
return mArguments[index];
}
......@@ -400,12 +400,12 @@ bool TIntermAggregate::replaceChildNode(TIntermNode *original, TIntermNode *repl
return replaceChildNodeInternal(original, replacement);
}
unsigned int TIntermBlock::getChildCount()
size_t TIntermBlock::getChildCount() const
{
return mStatements.size();
}
TIntermNode *TIntermBlock::getChildNode(unsigned int index)
TIntermNode *TIntermBlock::getChildNode(size_t index) const
{
return mStatements[index];
}
......@@ -415,12 +415,12 @@ bool TIntermBlock::replaceChildNode(TIntermNode *original, TIntermNode *replacem
return replaceChildNodeInternal(original, replacement);
}
unsigned int TIntermFunctionPrototype::getChildCount()
size_t TIntermFunctionPrototype::getChildCount() const
{
return 0;
}
TIntermNode *TIntermFunctionPrototype::getChildNode(unsigned int index)
TIntermNode *TIntermFunctionPrototype::getChildNode(size_t index) const
{
UNREACHABLE();
return nullptr;
......@@ -431,12 +431,12 @@ bool TIntermFunctionPrototype::replaceChildNode(TIntermNode *original, TIntermNo
return false;
}
unsigned int TIntermDeclaration::getChildCount()
size_t TIntermDeclaration::getChildCount() const
{
return mDeclarators.size();
}
TIntermNode *TIntermDeclaration::getChildNode(unsigned int index)
TIntermNode *TIntermDeclaration::getChildNode(size_t index) const
{
return mDeclarators[index];
}
......@@ -874,12 +874,12 @@ void TIntermDeclaration::appendDeclarator(TIntermTyped *declarator)
mDeclarators.push_back(declarator);
}
unsigned int TIntermTernary::getChildCount()
size_t TIntermTernary::getChildCount() const
{
return 3;
}
TIntermNode *TIntermTernary::getChildNode(unsigned int index)
TIntermNode *TIntermTernary::getChildNode(size_t index) const
{
ASSERT(index < 3);
if (index == 0)
......@@ -901,12 +901,12 @@ bool TIntermTernary::replaceChildNode(TIntermNode *original, TIntermNode *replac
return false;
}
unsigned int TIntermIfElse::getChildCount()
size_t TIntermIfElse::getChildCount() const
{
return 1 + (mTrueBlock ? 1 : 0) + (mFalseBlock ? 1 : 0);
}
TIntermNode *TIntermIfElse::getChildNode(unsigned int index)
TIntermNode *TIntermIfElse::getChildNode(size_t index) const
{
if (index == 0)
{
......@@ -927,12 +927,12 @@ bool TIntermIfElse::replaceChildNode(TIntermNode *original, TIntermNode *replace
return false;
}
unsigned int TIntermSwitch::getChildCount()
size_t TIntermSwitch::getChildCount() const
{
return 2;
}
TIntermNode *TIntermSwitch::getChildNode(unsigned int index)
TIntermNode *TIntermSwitch::getChildNode(size_t index) const
{
ASSERT(index < 2);
if (index == 0)
......@@ -950,12 +950,12 @@ bool TIntermSwitch::replaceChildNode(TIntermNode *original, TIntermNode *replace
return false;
}
unsigned int TIntermCase::getChildCount()
size_t TIntermCase::getChildCount() const
{
return (mCondition ? 1 : 0);
}
TIntermNode *TIntermCase::getChildNode(unsigned int index)
TIntermNode *TIntermCase::getChildNode(size_t index) const
{
ASSERT(index == 0);
ASSERT(mCondition);
......@@ -3739,4 +3739,23 @@ TConstantUnion *TIntermConstantUnion::FoldAggregateBuiltIn(TIntermAggregate *agg
return resultArray;
}
// TIntermPreprocessorDirective implementation.
TIntermPreprocessorDirective::TIntermPreprocessorDirective(PreprocessorDirective directive,
ImmutableString command)
: mDirective(directive), mCommand(std::move(command))
{
}
TIntermPreprocessorDirective::~TIntermPreprocessorDirective() = default;
size_t TIntermPreprocessorDirective::getChildCount() const
{
return 0;
}
TIntermNode *TIntermPreprocessorDirective::getChildNode(size_t index) const
{
UNREACHABLE();
return nullptr;
}
} // namespace sh
......@@ -46,6 +46,10 @@ bool isSingleStatement(TIntermNode *node)
{
return false;
}
else if (node->getAsPreprocessorDirective())
{
return false;
}
return true;
}
......@@ -1113,6 +1117,40 @@ void TOutputGLSLBase::visitCodeBlock(TIntermBlock *node)
}
}
void TOutputGLSLBase::visitPreprocessorDirective(TIntermPreprocessorDirective *node)
{
TInfoSinkBase &out = objSink();
out << "\n";
switch (node->getDirective())
{
case PreprocessorDirective::Define:
out << "#define";
break;
case PreprocessorDirective::Endif:
out << "#endif";
break;
case PreprocessorDirective::If:
out << "#if";
break;
case PreprocessorDirective::Ifdef:
out << "#ifdef";
break;
default:
UNREACHABLE();
break;
}
if (!node->getCommand().empty())
{
out << " " << node->getCommand();
}
out << "\n";
}
ImmutableString TOutputGLSLBase::getTypeName(const TType &type)
{
return GetTypeName(type, mHashFunction, &mNameMap);
......
......@@ -66,6 +66,7 @@ class TOutputGLSLBase : public TIntermTraverser
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override;
bool visitLoop(Visit visit, TIntermLoop *node) override;
bool visitBranch(Visit visit, TIntermBranch *node) override;
void visitPreprocessorDirective(TIntermPreprocessorDirective *node) override;
void visitCodeBlock(TIntermBlock *node);
......
......@@ -32,8 +32,8 @@ void TIntermTraverser::traverse(T *node)
if (visit)
{
unsigned int childIndex = 0;
unsigned int childCount = node->getChildCount();
size_t childIndex = 0;
size_t childCount = node->getChildCount();
while (childIndex < childCount && visit)
{
......@@ -103,6 +103,11 @@ void TIntermLoop::traverse(TIntermTraverser *it)
it->traverseLoop(this);
}
void TIntermPreprocessorDirective::traverse(TIntermTraverser *it)
{
it->visitPreprocessorDirective(this);
}
bool TIntermSymbol::visit(Visit visit, TIntermTraverser *it)
{
it->visitSymbol(this);
......@@ -191,6 +196,12 @@ bool TIntermCase::visit(Visit visit, TIntermTraverser *it)
return it->visitCase(visit, this);
}
bool TIntermPreprocessorDirective::visit(Visit visit, TIntermTraverser *it)
{
it->visitPreprocessorDirective(this);
return false;
}
TIntermTraverser::TIntermTraverser(bool preVisit,
bool inVisit,
bool postVisit,
......@@ -614,5 +625,4 @@ void TIntermTraverser::traverseLoop(TIntermLoop *node)
{
traverse(node);
}
} // namespace sh
......@@ -63,6 +63,7 @@ class TIntermTraverser : angle::NonCopyable
virtual bool visitDeclaration(Visit visit, TIntermDeclaration *node) { return true; }
virtual bool visitLoop(Visit visit, TIntermLoop *node) { return true; }
virtual bool visitBranch(Visit visit, TIntermBranch *node) { return true; }
virtual void visitPreprocessorDirective(TIntermPreprocessorDirective *node) {}
// The traverse functions contain logic for iterating over the children of the node
// and calling the visit functions in the appropriate places. They also track some
......
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