Refactor TStructure
Move methods and member variables that generalize to different types
of field lists into TFieldListCollection and put implementations of
methods in cpp files.
This prepares for making TStructure inherit from TSymbol.
BUG=angleproject:2267
TEST=angle_unittests
Change-Id: I63095242dd17aac2d2efd616b49be1143cfc1f92
Reviewed-on: https://chromium-review.googlesource.com/793813
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
diff --git a/src/compiler/translator/OutputGLSLBase.cpp b/src/compiler/translator/OutputGLSLBase.cpp
index edaf2eb..6e0f25f 100644
--- a/src/compiler/translator/OutputGLSLBase.cpp
+++ b/src/compiler/translator/OutputGLSLBase.cpp
@@ -312,7 +312,7 @@
if (!structure->name().empty())
{
- mDeclaredStructs.insert(structure->uniqueId());
+ mDeclaredStructs.insert(structure->uniqueId().get());
}
}
else if (type.getBasicType() == EbtInterfaceBlock)
@@ -1168,7 +1168,7 @@
return false;
}
- return (mDeclaredStructs.count(structure->uniqueId()) > 0);
+ return (mDeclaredStructs.count(structure->uniqueId().get()) > 0);
}
void TOutputGLSLBase::declareStruct(const TStructure *structure)
diff --git a/src/compiler/translator/RegenerateStructNames.cpp b/src/compiler/translator/RegenerateStructNames.cpp
index 55fb124..5f3577a 100644
--- a/src/compiler/translator/RegenerateStructNames.cpp
+++ b/src/compiler/translator/RegenerateStructNames.cpp
@@ -25,7 +25,7 @@
return;
}
- int uniqueId = userType->uniqueId();
+ int uniqueId = userType->uniqueId().get();
ASSERT(mScopeDepth > 0);
if (mScopeDepth == 1)
diff --git a/src/compiler/translator/RemoveUnreferencedVariables.cpp b/src/compiler/translator/RemoveUnreferencedVariables.cpp
index 74b5e73..f09895a 100644
--- a/src/compiler/translator/RemoveUnreferencedVariables.cpp
+++ b/src/compiler/translator/RemoveUnreferencedVariables.cpp
@@ -72,10 +72,10 @@
const auto *structure = type.getStruct();
if (structure != nullptr)
{
- auto structIter = mStructIdRefCounts.find(structure->uniqueId());
+ auto structIter = mStructIdRefCounts.find(structure->uniqueId().get());
if (structIter == mStructIdRefCounts.end())
{
- mStructIdRefCounts[structure->uniqueId()] = 1u;
+ mStructIdRefCounts[structure->uniqueId().get()] = 1u;
for (const auto &field : structure->fields())
{
@@ -158,8 +158,8 @@
auto *structure = type.getStruct();
if (structure != nullptr)
{
- ASSERT(mStructIdRefCounts->find(structure->uniqueId()) != mStructIdRefCounts->end());
- unsigned int structRefCount = --(*mStructIdRefCounts)[structure->uniqueId()];
+ ASSERT(mStructIdRefCounts->find(structure->uniqueId().get()) != mStructIdRefCounts->end());
+ unsigned int structRefCount = --(*mStructIdRefCounts)[structure->uniqueId().get()];
if (structRefCount == 0)
{
@@ -176,7 +176,7 @@
{
if (declarator->getType().isStructSpecifier() && !declarator->getType().isNamelessStruct())
{
- unsigned int structId = declarator->getType().getStruct()->uniqueId();
+ unsigned int structId = declarator->getType().getStruct()->uniqueId().get();
if ((*mStructIdRefCounts)[structId] > 1u)
{
// If this declaration declares a named struct type that is used elsewhere, we need to
diff --git a/src/compiler/translator/Types.cpp b/src/compiler/translator/Types.cpp
index 530ffe3..43d483e 100644
--- a/src/compiler/translator/Types.cpp
+++ b/src/compiler/translator/Types.cpp
@@ -422,6 +422,31 @@
return stream.str();
}
+int TType::getDeepestStructNesting() const
+{
+ return mStructure ? mStructure->deepestNesting() : 0;
+}
+
+bool TType::isNamelessStruct() const
+{
+ return mStructure && mStructure->name() == "";
+}
+
+bool TType::isStructureContainingArrays() const
+{
+ return mStructure ? mStructure->containsArrays() : false;
+}
+
+bool TType::isStructureContainingType(TBasicType t) const
+{
+ return mStructure ? mStructure->containsType(t) : false;
+}
+
+bool TType::isStructureContainingSamplers() const
+{
+ return mStructure ? mStructure->containsSamplers() : false;
+}
+
//
// Recursively generate mangled names.
//
@@ -553,10 +578,14 @@
mangledName += "ac";
break;
case EbtStruct:
- mangledName += mStructure->mangledName();
+ mangledName += "struct-";
+ mangledName += mStructure->name();
+ mangledName += mStructure->mangledFieldList();
break;
case EbtInterfaceBlock:
- mangledName += mInterfaceBlock->mangledName();
+ mangledName += "iblock-";
+ mangledName += mInterfaceBlock->name();
+ mangledName += mInterfaceBlock->mangledFieldList();
break;
default:
// EbtVoid, EbtAddress and non types
@@ -848,44 +877,38 @@
// TStructure implementation.
TStructure::TStructure(TSymbolTable *symbolTable, const TString *name, TFieldList *fields)
: TFieldListCollection(name, fields),
- mDeepestNesting(0),
mUniqueId(symbolTable->nextUniqueId()),
mAtGlobalScope(false)
{
}
-bool TStructure::equals(const TStructure &other) const
+bool TFieldListCollection::containsArrays() const
{
- return (uniqueId() == other.uniqueId());
-}
-
-bool TStructure::containsArrays() const
-{
- for (size_t i = 0; i < mFields->size(); ++i)
+ for (const auto *field : *mFields)
{
- const TType *fieldType = (*mFields)[i]->type();
+ const TType *fieldType = field->type();
if (fieldType->isArray() || fieldType->isStructureContainingArrays())
return true;
}
return false;
}
-bool TStructure::containsType(TBasicType type) const
+bool TFieldListCollection::containsType(TBasicType type) const
{
- for (size_t i = 0; i < mFields->size(); ++i)
+ for (const auto *field : *mFields)
{
- const TType *fieldType = (*mFields)[i]->type();
+ const TType *fieldType = field->type();
if (fieldType->getBasicType() == type || fieldType->isStructureContainingType(type))
return true;
}
return false;
}
-bool TStructure::containsSamplers() const
+bool TFieldListCollection::containsSamplers() const
{
- for (size_t i = 0; i < mFields->size(); ++i)
+ for (const auto *field : *mFields)
{
- const TType *fieldType = (*mFields)[i]->type();
+ const TType *fieldType = field->type();
if (IsSampler(fieldType->getBasicType()) || fieldType->isStructureContainingSamplers())
return true;
}
@@ -952,10 +975,9 @@
}
}
-TString TFieldListCollection::buildMangledName(const TString &mangledNamePrefix) const
+TString TFieldListCollection::buildMangledFieldList() const
{
- TString mangledName(mangledNamePrefix);
- mangledName += *mName;
+ TString mangledName;
for (size_t i = 0; i < mFields->size(); ++i)
{
mangledName += '-';
@@ -978,6 +1000,13 @@
return size;
}
+size_t TFieldListCollection::objectSize() const
+{
+ if (mObjectSize == 0)
+ mObjectSize = calculateObjectSize();
+ return mObjectSize;
+}
+
int TFieldListCollection::getLocationCount() const
{
int count = 0;
@@ -996,7 +1025,21 @@
return count;
}
-int TStructure::calculateDeepestNesting() const
+int TFieldListCollection::deepestNesting() const
+{
+ if (mDeepestNesting == 0)
+ mDeepestNesting = calculateDeepestNesting();
+ return mDeepestNesting;
+}
+
+const TString &TFieldListCollection::mangledFieldList() const
+{
+ if (mMangledFieldList.empty())
+ mMangledFieldList = buildMangledFieldList();
+ return mMangledFieldList;
+}
+
+int TFieldListCollection::calculateDeepestNesting() const
{
int maxNesting = 0;
for (size_t i = 0; i < mFields->size(); ++i)
diff --git a/src/compiler/translator/Types.h b/src/compiler/translator/Types.h
index 7dc84c5..0564b3c 100644
--- a/src/compiler/translator/Types.h
+++ b/src/compiler/translator/Types.h
@@ -59,29 +59,34 @@
const TString &name() const { return *mName; }
const TFieldList &fields() const { return *mFields; }
- size_t objectSize() const
- {
- if (mObjectSize == 0)
- mObjectSize = calculateObjectSize();
- return mObjectSize;
- }
+ bool containsArrays() const;
+ bool containsType(TBasicType t) const;
+ bool containsSamplers() const;
+ size_t objectSize() const;
// How many locations the field list consumes as a uniform.
int getLocationCount() const;
+ int deepestNesting() const;
+ const TString &mangledFieldList() const;
protected:
TFieldListCollection(const TString *name, TFieldList *fields)
- : mName(name), mFields(fields), mObjectSize(0)
+ : mName(name), mFields(fields), mObjectSize(0), mDeepestNesting(0)
{
}
- TString buildMangledName(const TString &mangledNamePrefix) const;
- size_t calculateObjectSize() const;
const TString *mName;
+
TFieldList *mFields;
- mutable TString mMangledName;
+ private:
+ size_t calculateObjectSize() const;
+ int calculateDeepestNesting() const;
+ TString buildMangledFieldList() const;
+
mutable size_t mObjectSize;
+ mutable int mDeepestNesting;
+ mutable TString mMangledFieldList;
};
// May also represent interface blocks
@@ -91,37 +96,17 @@
POOL_ALLOCATOR_NEW_DELETE();
TStructure(TSymbolTable *symbolTable, const TString *name, TFieldList *fields);
- int deepestNesting() const
- {
- if (mDeepestNesting == 0)
- mDeepestNesting = calculateDeepestNesting();
- return mDeepestNesting;
- }
- bool containsArrays() const;
- bool containsType(TBasicType t) const;
- bool containsSamplers() const;
-
void createSamplerSymbols(const TString &namePrefix,
const TString &apiNamePrefix,
TVector<TIntermSymbol *> *outputSymbols,
TMap<TIntermSymbol *, TString> *outputSymbolsToAPINames,
TSymbolTable *symbolTable) const;
- bool equals(const TStructure &other) const;
-
- int uniqueId() const { return mUniqueId.get(); }
+ const TSymbolUniqueId &uniqueId() const { return mUniqueId; }
void setAtGlobalScope(bool atGlobalScope) { mAtGlobalScope = atGlobalScope; }
-
bool atGlobalScope() const { return mAtGlobalScope; }
- const TString &mangledName() const
- {
- if (mMangledName.empty())
- mMangledName = buildMangledName("struct-");
- return mMangledName;
- }
-
private:
// TODO(zmo): Find a way to get rid of the const_cast in function
// setName(). At the moment keep this function private so only
@@ -133,9 +118,6 @@
*mutableName = name;
}
- int calculateDeepestNesting() const;
-
- mutable int mDeepestNesting;
const TSymbolUniqueId mUniqueId;
bool mAtGlobalScope;
};
@@ -161,12 +143,6 @@
TLayoutBlockStorage blockStorage() const { return mBlockStorage; }
TLayoutMatrixPacking matrixPacking() const { return mMatrixPacking; }
int blockBinding() const { return mBinding; }
- const TString &mangledName() const
- {
- if (mMangledName.empty())
- mMangledName = buildMangledName("iblock-");
- return mMangledName;
- }
private:
const TString *mInstanceName; // for interface block instance names
@@ -348,24 +324,13 @@
// For type "nesting2", this method would return 2 -- the number
// of structures through which indirection must occur to reach the
// deepest field (nesting2.field1.position).
- int getDeepestStructNesting() const { return mStructure ? mStructure->deepestNesting() : 0; }
+ int getDeepestStructNesting() const;
- bool isNamelessStruct() const { return mStructure && mStructure->name() == ""; }
+ bool isNamelessStruct() const;
- bool isStructureContainingArrays() const
- {
- return mStructure ? mStructure->containsArrays() : false;
- }
-
- bool isStructureContainingType(TBasicType t) const
- {
- return mStructure ? mStructure->containsType(t) : false;
- }
-
- bool isStructureContainingSamplers() const
- {
- return mStructure ? mStructure->containsSamplers() : false;
- }
+ bool isStructureContainingArrays() const;
+ bool isStructureContainingType(TBasicType t) const;
+ bool isStructureContainingSamplers() const;
bool isStructSpecifier() const { return mIsStructSpecifier; }
diff --git a/src/compiler/translator/UtilsHLSL.cpp b/src/compiler/translator/UtilsHLSL.cpp
index 8a77f00..57c7420 100644
--- a/src/compiler/translator/UtilsHLSL.cpp
+++ b/src/compiler/translator/UtilsHLSL.cpp
@@ -859,7 +859,7 @@
return Decorate(structure.name());
}
- return "ss" + str(structure.uniqueId()) + "_" + structure.name();
+ return "ss" + str(structure.uniqueId().get()) + "_" + structure.name();
}
TString QualifiedStructNameString(const TStructure &structure,