Hash :
2d88e9bc
Author :
Date :
2017-07-21T16:52:03
Guarantee that symbol nodes get unique ids The code is refactored so that symbol nodes can only be initialized with an unique id object. This prevents accidentally forgetting to create an id for a symbol node. This opens up possibilities for future optimization: For example the names and types of symbols could be stored in a central location inside the SymbolTable, and TIntermSymbol nodes would only need to store the symbol id. The symbol id could be used to look up the name and type of the node. BUG=angleproject:1490 TEST=angle_unittests Change-Id: Ib8c8675d31493037a5a28c7b36bb9d1113cc10f6 Reviewed-on: https://chromium-review.googlesource.com/580955 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
//
// Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// IntermNode_test.cpp:
// Unit tests for the AST node classes.
//
#include "compiler/translator/IntermNode.h"
#include "angle_gl.h"
#include "compiler/translator/InfoSink.h"
#include "compiler/translator/PoolAlloc.h"
#include "compiler/translator/SymbolTable.h"
#include "gtest/gtest.h"
using namespace sh;
class IntermNodeTest : public testing::Test
{
public:
IntermNodeTest() : mUniqueIndex(0) {}
protected:
void SetUp() override
{
allocator.push();
SetGlobalPoolAllocator(&allocator);
}
void TearDown() override
{
SetGlobalPoolAllocator(nullptr);
allocator.pop();
}
TIntermSymbol *createTestSymbol(const TType &type)
{
TInfoSinkBase symbolNameOut;
symbolNameOut << "test" << mUniqueIndex;
TString symbolName = symbolNameOut.c_str();
++mUniqueIndex;
// We're using a dummy symbol table here, don't need to assign proper symbol ids to these
// nodes.
TSymbolTable symbolTable;
TIntermSymbol *node = new TIntermSymbol(symbolTable.nextUniqueId(), symbolName, type);
node->setLine(createUniqueSourceLoc());
node->setInternal(true);
node->getTypePointer()->setQualifier(EvqTemporary);
return node;
}
TIntermSymbol *createTestSymbol()
{
TType type(EbtFloat, EbpHigh);
return createTestSymbol(type);
}
void checkTypeEqualWithQualifiers(const TType &original, const TType ©)
{
ASSERT_EQ(original, copy);
ASSERT_EQ(original.getPrecision(), copy.getPrecision());
ASSERT_EQ(original.getQualifier(), copy.getQualifier());
}
void checkSymbolCopy(TIntermNode *aOriginal, TIntermNode *aCopy)
{
ASSERT_NE(aOriginal, aCopy);
TIntermSymbol *copy = aCopy->getAsSymbolNode();
TIntermSymbol *original = aOriginal->getAsSymbolNode();
ASSERT_NE(nullptr, copy);
ASSERT_NE(nullptr, original);
ASSERT_NE(original, copy);
ASSERT_EQ(original->getId(), copy->getId());
ASSERT_EQ(original->getName().getString(), copy->getName().getString());
ASSERT_EQ(original->getName().isInternal(), copy->getName().isInternal());
checkTypeEqualWithQualifiers(original->getType(), copy->getType());
ASSERT_EQ(original->getLine().first_file, copy->getLine().first_file);
ASSERT_EQ(original->getLine().first_line, copy->getLine().first_line);
ASSERT_EQ(original->getLine().last_file, copy->getLine().last_file);
ASSERT_EQ(original->getLine().last_line, copy->getLine().last_line);
}
TSourceLoc createUniqueSourceLoc()
{
TSourceLoc loc;
loc.first_file = mUniqueIndex;
loc.first_line = mUniqueIndex + 1;
loc.last_file = mUniqueIndex + 2;
loc.last_line = mUniqueIndex + 3;
++mUniqueIndex;
return loc;
}
static TSourceLoc getTestSourceLoc()
{
TSourceLoc loc;
loc.first_file = 1;
loc.first_line = 2;
loc.last_file = 3;
loc.last_line = 4;
return loc;
}
static void checkTestSourceLoc(const TSourceLoc &loc)
{
ASSERT_EQ(1, loc.first_file);
ASSERT_EQ(2, loc.first_line);
ASSERT_EQ(3, loc.last_file);
ASSERT_EQ(4, loc.last_line);
}
private:
TPoolAllocator allocator;
int mUniqueIndex;
};
// Check that the deep copy of a symbol node is an actual copy with the same attributes as the
// original.
TEST_F(IntermNodeTest, DeepCopySymbolNode)
{
TType type(EbtInt, EbpHigh);
// We're using a dummy symbol table here, don't need to assign proper symbol ids to these nodes.
TSymbolTable symbolTable;
TIntermSymbol *original = new TIntermSymbol(symbolTable.nextUniqueId(), TString("name"), type);
original->setLine(getTestSourceLoc());
original->setInternal(true);
TIntermTyped *copy = original->deepCopy();
checkSymbolCopy(original, copy);
checkTestSourceLoc(copy->getLine());
}
// Check that the deep copy of a constant union node is an actual copy with the same attributes as
// the original.
TEST_F(IntermNodeTest, DeepCopyConstantUnionNode)
{
TType type(EbtInt, EbpHigh);
TConstantUnion *constValue = new TConstantUnion[1];
constValue[0].setIConst(101);
TIntermConstantUnion *original = new TIntermConstantUnion(constValue, type);
original->setLine(getTestSourceLoc());
TIntermTyped *copyTyped = original->deepCopy();
TIntermConstantUnion *copy = copyTyped->getAsConstantUnion();
ASSERT_NE(nullptr, copy);
ASSERT_NE(original, copy);
checkTestSourceLoc(copy->getLine());
checkTypeEqualWithQualifiers(original->getType(), copy->getType());
ASSERT_EQ(101, copy->getIConst(0));
}
// Check that the deep copy of a binary node is an actual copy with the same attributes as the
// original. Child nodes also need to be copies with the same attributes as the original children.
TEST_F(IntermNodeTest, DeepCopyBinaryNode)
{
TType type(EbtFloat, EbpHigh);
TIntermBinary *original = new TIntermBinary(EOpAdd, createTestSymbol(), createTestSymbol());
original->setLine(getTestSourceLoc());
TIntermTyped *copyTyped = original->deepCopy();
TIntermBinary *copy = copyTyped->getAsBinaryNode();
ASSERT_NE(nullptr, copy);
ASSERT_NE(original, copy);
checkTestSourceLoc(copy->getLine());
checkTypeEqualWithQualifiers(original->getType(), copy->getType());
checkSymbolCopy(original->getLeft(), copy->getLeft());
checkSymbolCopy(original->getRight(), copy->getRight());
}
// Check that the deep copy of a unary node is an actual copy with the same attributes as the
// original. The child node also needs to be a copy with the same attributes as the original child.
TEST_F(IntermNodeTest, DeepCopyUnaryNode)
{
TType type(EbtFloat, EbpHigh);
TIntermUnary *original = new TIntermUnary(EOpPreIncrement, createTestSymbol());
original->setLine(getTestSourceLoc());
TIntermTyped *copyTyped = original->deepCopy();
TIntermUnary *copy = copyTyped->getAsUnaryNode();
ASSERT_NE(nullptr, copy);
ASSERT_NE(original, copy);
checkTestSourceLoc(copy->getLine());
checkTypeEqualWithQualifiers(original->getType(), copy->getType());
checkSymbolCopy(original->getOperand(), copy->getOperand());
}
// Check that the deep copy of an aggregate node is an actual copy with the same attributes as the
// original. Child nodes also need to be copies with the same attributes as the original children.
TEST_F(IntermNodeTest, DeepCopyAggregateNode)
{
TIntermSequence *originalSeq = new TIntermSequence();
originalSeq->push_back(createTestSymbol());
originalSeq->push_back(createTestSymbol());
originalSeq->push_back(createTestSymbol());
TIntermAggregate *original =
TIntermAggregate::Create(originalSeq->at(0)->getAsTyped()->getType(), EOpMix, originalSeq);
original->setLine(getTestSourceLoc());
TIntermTyped *copyTyped = original->deepCopy();
TIntermAggregate *copy = copyTyped->getAsAggregate();
ASSERT_NE(nullptr, copy);
ASSERT_NE(original, copy);
checkTestSourceLoc(copy->getLine());
checkTypeEqualWithQualifiers(original->getType(), copy->getType());
ASSERT_EQ(original->getSequence()->size(), copy->getSequence()->size());
TIntermSequence::size_type i = 0;
for (auto *copyChild : *copy->getSequence())
{
TIntermNode *originalChild = original->getSequence()->at(i);
checkSymbolCopy(originalChild, copyChild);
++i;
}
}
// Check that the deep copy of a ternary node is an actual copy with the same attributes as the
// original. Child nodes also need to be copies with the same attributes as the original children.
TEST_F(IntermNodeTest, DeepCopyTernaryNode)
{
TType type(EbtFloat, EbpHigh);
TIntermTernary *original = new TIntermTernary(createTestSymbol(TType(EbtBool, EbpUndefined)),
createTestSymbol(), createTestSymbol());
original->setLine(getTestSourceLoc());
TIntermTyped *copyTyped = original->deepCopy();
TIntermTernary *copy = copyTyped->getAsTernaryNode();
ASSERT_NE(nullptr, copy);
ASSERT_NE(original, copy);
checkTestSourceLoc(copy->getLine());
checkTypeEqualWithQualifiers(original->getType(), copy->getType());
checkSymbolCopy(original->getCondition(), copy->getCondition());
checkSymbolCopy(original->getTrueExpression(), copy->getTrueExpression());
checkSymbolCopy(original->getFalseExpression(), copy->getFalseExpression());
}