Commit 32db19b7 by Olli Etuaho Committed by Commit Bot

Ensure that if-else branches are always sequence nodes

This mainly affects RewriteElseBlocks, which was the only piece of code still adding TIntermIfElse nodes directly as children of other TIntermIfElse nodes. BUG=angleproject:1490 TEST=angle_unittests Change-Id: I5b25c2fb9c642424417cd6c29e37c20482c6ffaf Reviewed-on: https://chromium-review.googlesource.com/392847Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
parent 18319182
...@@ -310,8 +310,8 @@ bool TIntermTernary::replaceChildNode(TIntermNode *original, TIntermNode *replac ...@@ -310,8 +310,8 @@ bool TIntermTernary::replaceChildNode(TIntermNode *original, TIntermNode *replac
bool TIntermIfElse::replaceChildNode(TIntermNode *original, TIntermNode *replacement) bool TIntermIfElse::replaceChildNode(TIntermNode *original, TIntermNode *replacement)
{ {
REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement); REPLACE_IF_IS(mCondition, TIntermTyped, original, replacement);
REPLACE_IF_IS(mTrueBlock, TIntermNode, original, replacement); REPLACE_IF_IS(mTrueBlock, TIntermAggregate, original, replacement);
REPLACE_IF_IS(mFalseBlock, TIntermNode, original, replacement); REPLACE_IF_IS(mFalseBlock, TIntermAggregate, original, replacement);
return false; return false;
} }
......
...@@ -641,7 +641,7 @@ class TIntermTernary : public TIntermTyped ...@@ -641,7 +641,7 @@ class TIntermTernary : public TIntermTyped
class TIntermIfElse : public TIntermNode class TIntermIfElse : public TIntermNode
{ {
public: public:
TIntermIfElse(TIntermTyped *cond, TIntermNode *trueB, TIntermNode *falseB) TIntermIfElse(TIntermTyped *cond, TIntermAggregate *trueB, TIntermAggregate *falseB)
: TIntermNode(), mCondition(cond), mTrueBlock(trueB), mFalseBlock(falseB) : TIntermNode(), mCondition(cond), mTrueBlock(trueB), mFalseBlock(falseB)
{ {
} }
...@@ -650,14 +650,14 @@ class TIntermIfElse : public TIntermNode ...@@ -650,14 +650,14 @@ class TIntermIfElse : public TIntermNode
bool replaceChildNode(TIntermNode *original, TIntermNode *replacement) override; bool replaceChildNode(TIntermNode *original, TIntermNode *replacement) override;
TIntermTyped *getCondition() const { return mCondition; } TIntermTyped *getCondition() const { return mCondition; }
TIntermNode *getTrueBlock() const { return mTrueBlock; } TIntermAggregate *getTrueBlock() const { return mTrueBlock; }
TIntermNode *getFalseBlock() const { return mFalseBlock; } TIntermAggregate *getFalseBlock() const { return mFalseBlock; }
TIntermIfElse *getAsIfElseNode() override { return this; } TIntermIfElse *getAsIfElseNode() override { return this; }
protected: protected:
TIntermTyped *mCondition; TIntermTyped *mCondition;
TIntermNode *mTrueBlock; TIntermAggregate *mTrueBlock;
TIntermNode *mFalseBlock; TIntermAggregate *mFalseBlock;
}; };
// //
......
...@@ -142,11 +142,10 @@ TIntermAggregate *TIntermediate::growAggregate( ...@@ -142,11 +142,10 @@ TIntermAggregate *TIntermediate::growAggregate(
// //
// Returns an aggregate, unless NULL was passed in for the existing node. // Returns an aggregate, unless NULL was passed in for the existing node.
// //
TIntermAggregate *TIntermediate::makeAggregate( TIntermAggregate *TIntermediate::MakeAggregate(TIntermNode *node, const TSourceLoc &line)
TIntermNode *node, const TSourceLoc &line)
{ {
if (node == NULL) if (node == nullptr)
return NULL; return nullptr;
TIntermAggregate *aggNode = new TIntermAggregate; TIntermAggregate *aggNode = new TIntermAggregate;
aggNode->getSequence()->push_back(node); aggNode->getSequence()->push_back(node);
...@@ -159,7 +158,7 @@ TIntermAggregate *TIntermediate::makeAggregate( ...@@ -159,7 +158,7 @@ TIntermAggregate *TIntermediate::makeAggregate(
// If the input node is nullptr, return nullptr. // If the input node is nullptr, return nullptr.
// If the input node is a sequence (block) node, return it. // If the input node is a sequence (block) node, return it.
// If the input node is not a sequence node, put it inside a sequence node and return that. // If the input node is not a sequence node, put it inside a sequence node and return that.
TIntermAggregate *TIntermediate::ensureSequence(TIntermNode *node) TIntermAggregate *TIntermediate::EnsureSequence(TIntermNode *node)
{ {
if (node == nullptr) if (node == nullptr)
return nullptr; return nullptr;
...@@ -167,7 +166,7 @@ TIntermAggregate *TIntermediate::ensureSequence(TIntermNode *node) ...@@ -167,7 +166,7 @@ TIntermAggregate *TIntermediate::ensureSequence(TIntermNode *node)
if (aggNode != nullptr && aggNode->getOp() == EOpSequence) if (aggNode != nullptr && aggNode->getOp() == EOpSequence)
return aggNode; return aggNode;
aggNode = makeAggregate(node, node->getLine()); aggNode = MakeAggregate(node, node->getLine());
aggNode->setOp(EOpSequence); aggNode->setOp(EOpSequence);
return aggNode; return aggNode;
} }
...@@ -200,7 +199,7 @@ TIntermNode *TIntermediate::addIfElse(TIntermTyped *cond, ...@@ -200,7 +199,7 @@ TIntermNode *TIntermediate::addIfElse(TIntermTyped *cond,
} }
TIntermIfElse *node = TIntermIfElse *node =
new TIntermIfElse(cond, ensureSequence(nodePair.node1), ensureSequence(nodePair.node2)); new TIntermIfElse(cond, EnsureSequence(nodePair.node1), EnsureSequence(nodePair.node2));
node->setLine(line); node->setLine(line);
return node; return node;
...@@ -332,7 +331,7 @@ TIntermNode *TIntermediate::addLoop( ...@@ -332,7 +331,7 @@ TIntermNode *TIntermediate::addLoop(
TLoopType type, TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr, TLoopType type, TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr,
TIntermNode *body, const TSourceLoc &line) TIntermNode *body, const TSourceLoc &line)
{ {
TIntermNode *node = new TIntermLoop(type, init, cond, expr, ensureSequence(body)); TIntermNode *node = new TIntermLoop(type, init, cond, expr, EnsureSequence(body));
node->setLine(line); node->setLine(line);
return node; return node;
......
...@@ -35,8 +35,8 @@ class TIntermediate ...@@ -35,8 +35,8 @@ class TIntermediate
TOperator op, TIntermTyped *child, const TSourceLoc &line, const TType *funcReturnType); TOperator op, TIntermTyped *child, const TSourceLoc &line, const TType *funcReturnType);
TIntermAggregate *growAggregate( TIntermAggregate *growAggregate(
TIntermNode *left, TIntermNode *right, const TSourceLoc &); TIntermNode *left, TIntermNode *right, const TSourceLoc &);
TIntermAggregate *makeAggregate(TIntermNode *node, const TSourceLoc &); static TIntermAggregate *MakeAggregate(TIntermNode *node, const TSourceLoc &line);
TIntermAggregate *ensureSequence(TIntermNode *node); static TIntermAggregate *EnsureSequence(TIntermNode *node);
TIntermAggregate *setAggregateOperator(TIntermNode *, TOperator, const TSourceLoc &); TIntermAggregate *setAggregateOperator(TIntermNode *, TOperator, const TSourceLoc &);
TIntermNode *addIfElse(TIntermTyped *cond, TIntermNodePair code, const TSourceLoc &line); TIntermNode *addIfElse(TIntermTyped *cond, TIntermNodePair code, const TSourceLoc &line);
static TIntermTyped *AddTernarySelection(TIntermTyped *cond, static TIntermTyped *AddTernarySelection(TIntermTyped *cond,
......
...@@ -1944,9 +1944,8 @@ void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node) ...@@ -1944,9 +1944,8 @@ void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
outputLineDirective(out, node->getFalseBlock()->getLine().first_line); outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
// Either this is "else if" or the falseBlock child node will output braces. // The falseBlock child node will output braces.
ASSERT(IsSequence(node->getFalseBlock()) || ASSERT(IsSequence(node->getFalseBlock()));
node->getFalseBlock()->getAsIfElseNode() != nullptr);
node->getFalseBlock()->traverse(this); node->getFalseBlock()->traverse(this);
......
...@@ -1588,7 +1588,7 @@ TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType, ...@@ -1588,7 +1588,7 @@ TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
symbol->setId(variable->getUniqueId()); symbol->setId(variable->getUniqueId());
} }
return intermediate.makeAggregate(symbol, identifierOrTypeLocation); return TIntermediate::MakeAggregate(symbol, identifierOrTypeLocation);
} }
TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
...@@ -1619,7 +1619,7 @@ TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &public ...@@ -1619,7 +1619,7 @@ TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &public
if (variable && symbol) if (variable && symbol)
symbol->setId(variable->getUniqueId()); symbol->setId(variable->getUniqueId());
return intermediate.makeAggregate(symbol, identifierLocation); return TIntermediate::MakeAggregate(symbol, identifierLocation);
} }
TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType, TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
...@@ -1638,7 +1638,7 @@ TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &p ...@@ -1638,7 +1638,7 @@ TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &p
// //
// Build intermediate representation // Build intermediate representation
// //
return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr; return intermNode ? TIntermediate::MakeAggregate(intermNode, initLocation) : nullptr;
} }
else else
{ {
...@@ -1678,7 +1678,7 @@ TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration( ...@@ -1678,7 +1678,7 @@ TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
TIntermNode *initNode = nullptr; TIntermNode *initNode = nullptr;
if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode)) if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
{ {
return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr; return initNode ? TIntermediate::MakeAggregate(initNode, initLocation) : nullptr;
} }
else else
{ {
...@@ -1735,7 +1735,7 @@ TIntermAggregate *TParseContext::parseInvariantDeclaration( ...@@ -1735,7 +1735,7 @@ TIntermAggregate *TParseContext::parseInvariantDeclaration(
TIntermSymbol *intermSymbol = TIntermSymbol *intermSymbol =
intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc); intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc); TIntermAggregate *aggregate = TIntermediate::MakeAggregate(intermSymbol, identifierLoc);
aggregate->setOp(EOpInvariantDeclaration); aggregate->setOp(EOpInvariantDeclaration);
return aggregate; return aggregate;
} }
...@@ -2537,7 +2537,7 @@ TIntermAggregate *TParseContext::addInterfaceBlock( ...@@ -2537,7 +2537,7 @@ TIntermAggregate *TParseContext::addInterfaceBlock(
symbolName = instanceTypeDef->getName(); symbolName = instanceTypeDef->getName();
} }
TIntermAggregate *aggregate = intermediate.makeAggregate( TIntermAggregate *aggregate = TIntermediate::MakeAggregate(
intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
nameLine); nameLine);
aggregate->setOp(EOpDeclaration); aggregate->setOp(EOpDeclaration);
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
// //
#include "compiler/translator/RewriteElseBlocks.h" #include "compiler/translator/RewriteElseBlocks.h"
#include "compiler/translator/Intermediate.h"
#include "compiler/translator/NodeSearch.h" #include "compiler/translator/NodeSearch.h"
#include "compiler/translator/SymbolTable.h" #include "compiler/translator/SymbolTable.h"
...@@ -49,16 +51,7 @@ bool ElseBlockRewriter::visitAggregate(Visit visit, TIntermAggregate *node) ...@@ -49,16 +51,7 @@ bool ElseBlockRewriter::visitAggregate(Visit visit, TIntermAggregate *node)
TIntermIfElse *ifElse = statement->getAsIfElseNode(); TIntermIfElse *ifElse = statement->getAsIfElseNode();
if (ifElse && ifElse->getFalseBlock() != nullptr) if (ifElse && ifElse->getFalseBlock() != nullptr)
{ {
// Check for if / else if
TIntermIfElse *elseIfBranch = ifElse->getFalseBlock()->getAsIfElseNode();
if (elseIfBranch)
{
ifElse->replaceChildNode(elseIfBranch, rewriteIfElse(elseIfBranch));
delete elseIfBranch;
}
(*node->getSequence())[statementIndex] = rewriteIfElse(ifElse); (*node->getSequence())[statementIndex] = rewriteIfElse(ifElse);
delete ifElse;
} }
} }
} }
...@@ -84,7 +77,7 @@ TIntermNode *ElseBlockRewriter::rewriteIfElse(TIntermIfElse *ifElse) ...@@ -84,7 +77,7 @@ TIntermNode *ElseBlockRewriter::rewriteIfElse(TIntermIfElse *ifElse)
TIntermTyped *typedCondition = ifElse->getCondition()->getAsTyped(); TIntermTyped *typedCondition = ifElse->getCondition()->getAsTyped();
TIntermAggregate *storeCondition = createTempInitDeclaration(typedCondition); TIntermAggregate *storeCondition = createTempInitDeclaration(typedCondition);
TIntermIfElse *falseBlock = nullptr; TIntermAggregate *falseBlock = nullptr;
TType boolType(EbtBool, EbpUndefined, EvqTemporary); TType boolType(EbtBool, EbpUndefined, EvqTemporary);
...@@ -107,7 +100,9 @@ TIntermNode *ElseBlockRewriter::rewriteIfElse(TIntermIfElse *ifElse) ...@@ -107,7 +100,9 @@ TIntermNode *ElseBlockRewriter::rewriteIfElse(TIntermIfElse *ifElse)
TIntermSymbol *conditionSymbolElse = createTempSymbol(boolType); TIntermSymbol *conditionSymbolElse = createTempSymbol(boolType);
TIntermUnary *negatedCondition = new TIntermUnary(EOpLogicalNot, conditionSymbolElse); TIntermUnary *negatedCondition = new TIntermUnary(EOpLogicalNot, conditionSymbolElse);
falseBlock = new TIntermIfElse(negatedCondition, ifElse->getFalseBlock(), negatedElse); TIntermIfElse *falseIfElse =
new TIntermIfElse(negatedCondition, ifElse->getFalseBlock(), negatedElse);
falseBlock = TIntermediate::EnsureSequence(falseIfElse);
} }
TIntermSymbol *conditionSymbolSel = createTempSymbol(boolType); TIntermSymbol *conditionSymbolSel = createTempSymbol(boolType);
......
...@@ -351,7 +351,7 @@ function_call_header_with_parameters ...@@ -351,7 +351,7 @@ function_call_header_with_parameters
const TType *type = new TType($2->getType()); const TType *type = new TType($2->getType());
$1->addParameter(TConstParameter(type)); $1->addParameter(TConstParameter(type));
$$.function = $1; $$.function = $1;
$$.nodePair.node1 = context->intermediate.makeAggregate($2, @2); $$.nodePair.node1 = TIntermediate::MakeAggregate($2, @2);
} }
| function_call_header_with_parameters COMMA assignment_expression { | function_call_header_with_parameters COMMA assignment_expression {
const TType *type = new TType($3->getType()); const TType *type = new TType($3->getType());
...@@ -1319,7 +1319,7 @@ compound_statement_no_new_scope ...@@ -1319,7 +1319,7 @@ compound_statement_no_new_scope
statement_list statement_list
: statement { : statement {
$$ = context->intermediate.makeAggregate($1, @$); $$ = TIntermediate::MakeAggregate($1, @$);
} }
| statement_list statement { | statement_list statement {
$$ = context->intermediate.growAggregate($1, $2, @$); $$ = context->intermediate.growAggregate($1, $2, @$);
......
...@@ -2546,7 +2546,7 @@ yyreduce: ...@@ -2546,7 +2546,7 @@ yyreduce:
const TType *type = new TType((yyvsp[0].interm.intermTypedNode)->getType()); const TType *type = new TType((yyvsp[0].interm.intermTypedNode)->getType());
(yyvsp[-1].interm.function)->addParameter(TConstParameter(type)); (yyvsp[-1].interm.function)->addParameter(TConstParameter(type));
(yyval.interm).function = (yyvsp[-1].interm.function); (yyval.interm).function = (yyvsp[-1].interm.function);
(yyval.interm).nodePair.node1 = context->intermediate.makeAggregate((yyvsp[0].interm.intermTypedNode), (yylsp[0])); (yyval.interm).nodePair.node1 = TIntermediate::MakeAggregate((yyvsp[0].interm.intermTypedNode), (yylsp[0]));
} }
break; break;
...@@ -4371,7 +4371,7 @@ yyreduce: ...@@ -4371,7 +4371,7 @@ yyreduce:
case 234: case 234:
{ {
(yyval.interm.intermAggregate) = context->intermediate.makeAggregate((yyvsp[0].interm.intermNode), (yyloc)); (yyval.interm.intermAggregate) = TIntermediate::MakeAggregate((yyvsp[0].interm.intermNode), (yyloc));
} }
break; break;
......
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