Commit 97fa8557 by Olli Etuaho Committed by Commit Bot

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/793813Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
parent 18841310
...@@ -312,7 +312,7 @@ void TOutputGLSLBase::writeVariableType(const TType &type) ...@@ -312,7 +312,7 @@ void TOutputGLSLBase::writeVariableType(const TType &type)
if (!structure->name().empty()) if (!structure->name().empty())
{ {
mDeclaredStructs.insert(structure->uniqueId()); mDeclaredStructs.insert(structure->uniqueId().get());
} }
} }
else if (type.getBasicType() == EbtInterfaceBlock) else if (type.getBasicType() == EbtInterfaceBlock)
...@@ -1168,7 +1168,7 @@ bool TOutputGLSLBase::structDeclared(const TStructure *structure) const ...@@ -1168,7 +1168,7 @@ bool TOutputGLSLBase::structDeclared(const TStructure *structure) const
return false; return false;
} }
return (mDeclaredStructs.count(structure->uniqueId()) > 0); return (mDeclaredStructs.count(structure->uniqueId().get()) > 0);
} }
void TOutputGLSLBase::declareStruct(const TStructure *structure) void TOutputGLSLBase::declareStruct(const TStructure *structure)
......
...@@ -25,7 +25,7 @@ void RegenerateStructNames::visitSymbol(TIntermSymbol *symbol) ...@@ -25,7 +25,7 @@ void RegenerateStructNames::visitSymbol(TIntermSymbol *symbol)
return; return;
} }
int uniqueId = userType->uniqueId(); int uniqueId = userType->uniqueId().get();
ASSERT(mScopeDepth > 0); ASSERT(mScopeDepth > 0);
if (mScopeDepth == 1) if (mScopeDepth == 1)
......
...@@ -72,10 +72,10 @@ void CollectVariableRefCountsTraverser::incrementStructTypeRefCount(const TType ...@@ -72,10 +72,10 @@ void CollectVariableRefCountsTraverser::incrementStructTypeRefCount(const TType
const auto *structure = type.getStruct(); const auto *structure = type.getStruct();
if (structure != nullptr) if (structure != nullptr)
{ {
auto structIter = mStructIdRefCounts.find(structure->uniqueId()); auto structIter = mStructIdRefCounts.find(structure->uniqueId().get());
if (structIter == mStructIdRefCounts.end()) if (structIter == mStructIdRefCounts.end())
{ {
mStructIdRefCounts[structure->uniqueId()] = 1u; mStructIdRefCounts[structure->uniqueId().get()] = 1u;
for (const auto &field : structure->fields()) for (const auto &field : structure->fields())
{ {
...@@ -158,8 +158,8 @@ void RemoveUnreferencedVariablesTraverser::decrementStructTypeRefCount(const TTy ...@@ -158,8 +158,8 @@ void RemoveUnreferencedVariablesTraverser::decrementStructTypeRefCount(const TTy
auto *structure = type.getStruct(); auto *structure = type.getStruct();
if (structure != nullptr) if (structure != nullptr)
{ {
ASSERT(mStructIdRefCounts->find(structure->uniqueId()) != mStructIdRefCounts->end()); ASSERT(mStructIdRefCounts->find(structure->uniqueId().get()) != mStructIdRefCounts->end());
unsigned int structRefCount = --(*mStructIdRefCounts)[structure->uniqueId()]; unsigned int structRefCount = --(*mStructIdRefCounts)[structure->uniqueId().get()];
if (structRefCount == 0) if (structRefCount == 0)
{ {
...@@ -176,7 +176,7 @@ void RemoveUnreferencedVariablesTraverser::removeVariableDeclaration(TIntermDecl ...@@ -176,7 +176,7 @@ void RemoveUnreferencedVariablesTraverser::removeVariableDeclaration(TIntermDecl
{ {
if (declarator->getType().isStructSpecifier() && !declarator->getType().isNamelessStruct()) 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 ((*mStructIdRefCounts)[structId] > 1u)
{ {
// If this declaration declares a named struct type that is used elsewhere, we need to // If this declaration declares a named struct type that is used elsewhere, we need to
......
...@@ -422,6 +422,31 @@ TString TType::getCompleteString() const ...@@ -422,6 +422,31 @@ TString TType::getCompleteString() const
return stream.str(); 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. // Recursively generate mangled names.
// //
...@@ -553,10 +578,14 @@ const char *TType::buildMangledName() const ...@@ -553,10 +578,14 @@ const char *TType::buildMangledName() const
mangledName += "ac"; mangledName += "ac";
break; break;
case EbtStruct: case EbtStruct:
mangledName += mStructure->mangledName(); mangledName += "struct-";
mangledName += mStructure->name();
mangledName += mStructure->mangledFieldList();
break; break;
case EbtInterfaceBlock: case EbtInterfaceBlock:
mangledName += mInterfaceBlock->mangledName(); mangledName += "iblock-";
mangledName += mInterfaceBlock->name();
mangledName += mInterfaceBlock->mangledFieldList();
break; break;
default: default:
// EbtVoid, EbtAddress and non types // EbtVoid, EbtAddress and non types
...@@ -848,44 +877,38 @@ void TType::invalidateMangledName() ...@@ -848,44 +877,38 @@ void TType::invalidateMangledName()
// TStructure implementation. // TStructure implementation.
TStructure::TStructure(TSymbolTable *symbolTable, const TString *name, TFieldList *fields) TStructure::TStructure(TSymbolTable *symbolTable, const TString *name, TFieldList *fields)
: TFieldListCollection(name, fields), : TFieldListCollection(name, fields),
mDeepestNesting(0),
mUniqueId(symbolTable->nextUniqueId()), mUniqueId(symbolTable->nextUniqueId()),
mAtGlobalScope(false) 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()) if (fieldType->isArray() || fieldType->isStructureContainingArrays())
return true; return true;
} }
return false; 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)) if (fieldType->getBasicType() == type || fieldType->isStructureContainingType(type))
return true; return true;
} }
return false; 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()) if (IsSampler(fieldType->getBasicType()) || fieldType->isStructureContainingSamplers())
return true; return true;
} }
...@@ -952,10 +975,9 @@ void TStructure::createSamplerSymbols(const TString &namePrefix, ...@@ -952,10 +975,9 @@ void TStructure::createSamplerSymbols(const TString &namePrefix,
} }
} }
TString TFieldListCollection::buildMangledName(const TString &mangledNamePrefix) const TString TFieldListCollection::buildMangledFieldList() const
{ {
TString mangledName(mangledNamePrefix); TString mangledName;
mangledName += *mName;
for (size_t i = 0; i < mFields->size(); ++i) for (size_t i = 0; i < mFields->size(); ++i)
{ {
mangledName += '-'; mangledName += '-';
...@@ -978,6 +1000,13 @@ size_t TFieldListCollection::calculateObjectSize() const ...@@ -978,6 +1000,13 @@ size_t TFieldListCollection::calculateObjectSize() const
return size; return size;
} }
size_t TFieldListCollection::objectSize() const
{
if (mObjectSize == 0)
mObjectSize = calculateObjectSize();
return mObjectSize;
}
int TFieldListCollection::getLocationCount() const int TFieldListCollection::getLocationCount() const
{ {
int count = 0; int count = 0;
...@@ -996,7 +1025,21 @@ int TFieldListCollection::getLocationCount() const ...@@ -996,7 +1025,21 @@ int TFieldListCollection::getLocationCount() const
return count; 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; int maxNesting = 0;
for (size_t i = 0; i < mFields->size(); ++i) for (size_t i = 0; i < mFields->size(); ++i)
......
...@@ -59,29 +59,34 @@ class TFieldListCollection : angle::NonCopyable ...@@ -59,29 +59,34 @@ class TFieldListCollection : angle::NonCopyable
const TString &name() const { return *mName; } const TString &name() const { return *mName; }
const TFieldList &fields() const { return *mFields; } const TFieldList &fields() const { return *mFields; }
size_t objectSize() const bool containsArrays() const;
{ bool containsType(TBasicType t) const;
if (mObjectSize == 0) bool containsSamplers() const;
mObjectSize = calculateObjectSize();
return mObjectSize;
}
size_t objectSize() const;
// How many locations the field list consumes as a uniform. // How many locations the field list consumes as a uniform.
int getLocationCount() const; int getLocationCount() const;
int deepestNesting() const;
const TString &mangledFieldList() const;
protected: protected:
TFieldListCollection(const TString *name, TFieldList *fields) 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; const TString *mName;
TFieldList *mFields; TFieldList *mFields;
mutable TString mMangledName; private:
size_t calculateObjectSize() const;
int calculateDeepestNesting() const;
TString buildMangledFieldList() const;
mutable size_t mObjectSize; mutable size_t mObjectSize;
mutable int mDeepestNesting;
mutable TString mMangledFieldList;
}; };
// May also represent interface blocks // May also represent interface blocks
...@@ -91,37 +96,17 @@ class TStructure : public TFieldListCollection ...@@ -91,37 +96,17 @@ class TStructure : public TFieldListCollection
POOL_ALLOCATOR_NEW_DELETE(); POOL_ALLOCATOR_NEW_DELETE();
TStructure(TSymbolTable *symbolTable, const TString *name, TFieldList *fields); 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, void createSamplerSymbols(const TString &namePrefix,
const TString &apiNamePrefix, const TString &apiNamePrefix,
TVector<TIntermSymbol *> *outputSymbols, TVector<TIntermSymbol *> *outputSymbols,
TMap<TIntermSymbol *, TString> *outputSymbolsToAPINames, TMap<TIntermSymbol *, TString> *outputSymbolsToAPINames,
TSymbolTable *symbolTable) const; TSymbolTable *symbolTable) const;
bool equals(const TStructure &other) const; const TSymbolUniqueId &uniqueId() const { return mUniqueId; }
int uniqueId() const { return mUniqueId.get(); }
void setAtGlobalScope(bool atGlobalScope) { mAtGlobalScope = atGlobalScope; } void setAtGlobalScope(bool atGlobalScope) { mAtGlobalScope = atGlobalScope; }
bool atGlobalScope() const { return mAtGlobalScope; } bool atGlobalScope() const { return mAtGlobalScope; }
const TString &mangledName() const
{
if (mMangledName.empty())
mMangledName = buildMangledName("struct-");
return mMangledName;
}
private: private:
// TODO(zmo): Find a way to get rid of the const_cast in function // TODO(zmo): Find a way to get rid of the const_cast in function
// setName(). At the moment keep this function private so only // setName(). At the moment keep this function private so only
...@@ -133,9 +118,6 @@ class TStructure : public TFieldListCollection ...@@ -133,9 +118,6 @@ class TStructure : public TFieldListCollection
*mutableName = name; *mutableName = name;
} }
int calculateDeepestNesting() const;
mutable int mDeepestNesting;
const TSymbolUniqueId mUniqueId; const TSymbolUniqueId mUniqueId;
bool mAtGlobalScope; bool mAtGlobalScope;
}; };
...@@ -161,12 +143,6 @@ class TInterfaceBlock : public TFieldListCollection ...@@ -161,12 +143,6 @@ class TInterfaceBlock : public TFieldListCollection
TLayoutBlockStorage blockStorage() const { return mBlockStorage; } TLayoutBlockStorage blockStorage() const { return mBlockStorage; }
TLayoutMatrixPacking matrixPacking() const { return mMatrixPacking; } TLayoutMatrixPacking matrixPacking() const { return mMatrixPacking; }
int blockBinding() const { return mBinding; } int blockBinding() const { return mBinding; }
const TString &mangledName() const
{
if (mMangledName.empty())
mMangledName = buildMangledName("iblock-");
return mMangledName;
}
private: private:
const TString *mInstanceName; // for interface block instance names const TString *mInstanceName; // for interface block instance names
...@@ -348,24 +324,13 @@ class TType ...@@ -348,24 +324,13 @@ class TType
// For type "nesting2", this method would return 2 -- the number // For type "nesting2", this method would return 2 -- the number
// of structures through which indirection must occur to reach the // of structures through which indirection must occur to reach the
// deepest field (nesting2.field1.position). // 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 bool isStructureContainingArrays() const;
{ bool isStructureContainingType(TBasicType t) const;
return mStructure ? mStructure->containsArrays() : false; bool isStructureContainingSamplers() const;
}
bool isStructureContainingType(TBasicType t) const
{
return mStructure ? mStructure->containsType(t) : false;
}
bool isStructureContainingSamplers() const
{
return mStructure ? mStructure->containsSamplers() : false;
}
bool isStructSpecifier() const { return mIsStructSpecifier; } bool isStructSpecifier() const { return mIsStructSpecifier; }
......
...@@ -859,7 +859,7 @@ TString StructNameString(const TStructure &structure) ...@@ -859,7 +859,7 @@ TString StructNameString(const TStructure &structure)
return Decorate(structure.name()); return Decorate(structure.name());
} }
return "ss" + str(structure.uniqueId()) + "_" + structure.name(); return "ss" + str(structure.uniqueId().get()) + "_" + structure.name();
} }
TString QualifiedStructNameString(const TStructure &structure, TString QualifiedStructNameString(const TStructure &structure,
......
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