Commit 437664b4 by Olli Etuaho Committed by Commit Bot

Clean up TSymbolTable entry points

TSymbolTable API can be cleaned up further now that we have separate logic for inserting builtins and user-defined symbols. BUG=angleprojec:2267 TEST=angle_unittests Change-Id: I7a228891ecdf4696e50868b9e7dfc2a50b5d8e92 Reviewed-on: https://chromium-review.googlesource.com/941301 Commit-Queue: Olli Etuaho <oetuaho@nvidia.com> Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org>
parent b7d14586
...@@ -193,12 +193,12 @@ class TScopedSymbolTableLevel ...@@ -193,12 +193,12 @@ class TScopedSymbolTableLevel
public: public:
TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table) TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table)
{ {
ASSERT(mTable->atBuiltInLevel()); ASSERT(mTable->isEmpty());
mTable->push(); mTable->push();
} }
~TScopedSymbolTableLevel() ~TScopedSymbolTableLevel()
{ {
while (!mTable->atBuiltInLevel()) while (!mTable->isEmpty())
mTable->pop(); mTable->pop();
} }
......
...@@ -1169,7 +1169,7 @@ bool TParseContext::declareVariable(const TSourceLoc &line, ...@@ -1169,7 +1169,7 @@ bool TParseContext::declareVariable(const TSourceLoc &line,
if (needsReservedCheck && !checkIsNotReserved(line, identifier)) if (needsReservedCheck && !checkIsNotReserved(line, identifier))
return false; return false;
if (!symbolTable.declareVariable(*variable)) if (!symbolTable.declare(*variable))
{ {
error(line, "redefinition", identifier); error(line, "redefinition", identifier);
return false; return false;
...@@ -3194,7 +3194,7 @@ TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction( ...@@ -3194,7 +3194,7 @@ TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
// Insert the parameter in the symbol table. // Insert the parameter in the symbol table.
if (insertParametersToSymbolTable) if (insertParametersToSymbolTable)
{ {
if (!symbolTable.declareVariable(variable)) if (!symbolTable.declare(variable))
{ {
error(location, "redefinition", param.name); error(location, "redefinition", param.name);
} }
...@@ -3792,7 +3792,7 @@ TIntermDeclaration *TParseContext::addInterfaceBlock( ...@@ -3792,7 +3792,7 @@ TIntermDeclaration *TParseContext::addInterfaceBlock(
TInterfaceBlock *interfaceBlock = new TInterfaceBlock( TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
&symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined); &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
if (!symbolTable.declareInterfaceBlock(interfaceBlock)) if (!symbolTable.declare(interfaceBlock))
{ {
error(nameLine, "redefinition of an interface block name", blockName); error(nameLine, "redefinition of an interface block name", blockName);
} }
...@@ -3826,7 +3826,7 @@ TIntermDeclaration *TParseContext::addInterfaceBlock( ...@@ -3826,7 +3826,7 @@ TIntermDeclaration *TParseContext::addInterfaceBlock(
TVariable *fieldVariable = TVariable *fieldVariable =
new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined); new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
if (!symbolTable.declareVariable(fieldVariable)) if (!symbolTable.declare(fieldVariable))
{ {
error(field->line(), "redefinition of an interface block member name", error(field->line(), "redefinition of an interface block member name",
field->name()); field->name());
...@@ -3838,7 +3838,7 @@ TIntermDeclaration *TParseContext::addInterfaceBlock( ...@@ -3838,7 +3838,7 @@ TIntermDeclaration *TParseContext::addInterfaceBlock(
checkIsNotReserved(instanceLine, instanceName); checkIsNotReserved(instanceLine, instanceName);
// add a symbol for this interface block // add a symbol for this interface block
if (!symbolTable.declareVariable(instanceVariable)) if (!symbolTable.declare(instanceVariable))
{ {
error(instanceLine, "redefinition of an interface block instance name", instanceName); error(instanceLine, "redefinition of an interface block instance name", instanceName);
} }
...@@ -4778,7 +4778,7 @@ TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine, ...@@ -4778,7 +4778,7 @@ TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
if (structSymbolType != SymbolType::Empty) if (structSymbolType != SymbolType::Empty)
{ {
checkIsNotReserved(nameLine, structName); checkIsNotReserved(nameLine, structName);
if (!symbolTable.declareStructType(structure)) if (!symbolTable.declare(structure))
{ {
error(nameLine, "redefinition of a struct", structName); error(nameLine, "redefinition of a struct", structName);
} }
......
...@@ -147,6 +147,16 @@ TSymbolTable::TSymbolTable() : mUniqueIdCounter(0), mUserDefinedUniqueIdsStart(- ...@@ -147,6 +147,16 @@ TSymbolTable::TSymbolTable() : mUniqueIdCounter(0), mUserDefinedUniqueIdsStart(-
TSymbolTable::~TSymbolTable() = default; TSymbolTable::~TSymbolTable() = default;
bool TSymbolTable::isEmpty() const
{
return mTable.empty();
}
bool TSymbolTable::atGlobalLevel() const
{
return mTable.size() == 1u;
}
void TSymbolTable::pushBuiltInLevel() void TSymbolTable::pushBuiltInLevel()
{ {
mBuiltInTable.push_back( mBuiltInTable.push_back(
...@@ -333,25 +343,17 @@ constexpr const TType *VectorType(const TType *type, int size) ...@@ -333,25 +343,17 @@ constexpr const TType *VectorType(const TType *type, int size)
} }
} }
bool TSymbolTable::declareVariable(TVariable *variable) bool TSymbolTable::declare(TSymbol *symbol)
{
ASSERT(variable->symbolType() == SymbolType::UserDefined);
return insertVariable(currentLevel(), variable);
}
bool TSymbolTable::declareStructType(TStructure *str)
{ {
return insertStructType(currentLevel(), str); ASSERT(!mTable.empty());
} ASSERT(symbol->symbolType() == SymbolType::UserDefined);
ASSERT(!symbol->isFunction());
bool TSymbolTable::declareInterfaceBlock(TInterfaceBlock *interfaceBlock) return mTable.back()->insert(symbol);
{
return insert(currentLevel(), interfaceBlock);
} }
void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUnmangledName) void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUnmangledName)
{ {
ASSERT(currentLevel() >= GLOBAL_LEVEL); ASSERT(!mTable.empty());
if (insertUnmangledName) if (insertUnmangledName)
{ {
// Insert the unmangled name to detect potential future redefinition as a variable. // Insert the unmangled name to detect potential future redefinition as a variable.
...@@ -360,27 +362,13 @@ void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUn ...@@ -360,27 +362,13 @@ void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUn
mTable[0]->insert(function); mTable[0]->insert(function);
} }
TVariable *TSymbolTable::insertVariable(ESymbolLevel level, void TSymbolTable::insertVariable(ESymbolLevel level,
const ImmutableString &name, const ImmutableString &name,
const TType *type) const TType *type)
{ {
ASSERT(level <= LAST_BUILTIN_LEVEL);
ASSERT(type->isRealized()); ASSERT(type->isRealized());
return insertVariable(level, name, type, SymbolType::BuiltIn); TVariable *var = new TVariable(this, name, type, SymbolType::BuiltIn);
} insertBuiltIn(level, var);
TVariable *TSymbolTable::insertVariable(ESymbolLevel level,
const ImmutableString &name,
const TType *type,
SymbolType symbolType)
{
ASSERT(level > LAST_BUILTIN_LEVEL || type->isRealized());
TVariable *var = new TVariable(this, name, type, symbolType);
if (insert(level, var))
{
return var;
}
return nullptr;
} }
void TSymbolTable::insertVariableExt(ESymbolLevel level, void TSymbolTable::insertVariableExt(ESymbolLevel level,
...@@ -388,59 +376,32 @@ void TSymbolTable::insertVariableExt(ESymbolLevel level, ...@@ -388,59 +376,32 @@ void TSymbolTable::insertVariableExt(ESymbolLevel level,
const ImmutableString &name, const ImmutableString &name,
const TType *type) const TType *type)
{ {
ASSERT(level <= LAST_BUILTIN_LEVEL);
ASSERT(type->isRealized()); ASSERT(type->isRealized());
TVariable *var = new TVariable(this, name, type, SymbolType::BuiltIn, ext); TVariable *var = new TVariable(this, name, type, SymbolType::BuiltIn, ext);
bool inserted = insert(level, var); insertBuiltIn(level, var);
UNUSED_VARIABLE(inserted);
ASSERT(inserted);
}
bool TSymbolTable::insertVariable(ESymbolLevel level, TVariable *variable)
{
ASSERT(variable);
ASSERT(level > LAST_BUILTIN_LEVEL || variable->getType().isRealized());
return insert(level, variable);
}
bool TSymbolTable::insert(ESymbolLevel level, TSymbol *symbol)
{
ASSERT(level > LAST_BUILTIN_LEVEL || mUserDefinedUniqueIdsStart == -1);
if (level <= LAST_BUILTIN_LEVEL)
{
return mBuiltInTable[level]->insert(symbol);
}
else
{
return mTable[level - LAST_BUILTIN_LEVEL - 1]->insert(symbol);
}
} }
bool TSymbolTable::insertStructType(ESymbolLevel level, TStructure *str) void TSymbolTable::insertBuiltIn(ESymbolLevel level, const TSymbol *symbol)
{ {
ASSERT(str); ASSERT(symbol);
return insert(level, str); ASSERT(level <= LAST_BUILTIN_LEVEL);
}
bool TSymbolTable::insertInterfaceBlock(ESymbolLevel level, TInterfaceBlock *interfaceBlock) mBuiltInTable[level]->insert(symbol);
{
ASSERT(interfaceBlock);
return insert(level, interfaceBlock);
} }
template <TPrecision precision> template <TPrecision precision>
bool TSymbolTable::insertConstInt(ESymbolLevel level, const ImmutableString &name, int value) void TSymbolTable::insertConstInt(ESymbolLevel level, const ImmutableString &name, int value)
{ {
TVariable *constant = new TVariable( TVariable *constant = new TVariable(
this, name, StaticType::Get<EbtInt, precision, EvqConst, 1, 1>(), SymbolType::BuiltIn); this, name, StaticType::Get<EbtInt, precision, EvqConst, 1, 1>(), SymbolType::BuiltIn);
TConstantUnion *unionArray = new TConstantUnion[1]; TConstantUnion *unionArray = new TConstantUnion[1];
unionArray[0].setIConst(value); unionArray[0].setIConst(value);
constant->shareConstPointer(unionArray); constant->shareConstPointer(unionArray);
return insert(level, constant); insertBuiltIn(level, constant);
} }
template <TPrecision precision> template <TPrecision precision>
bool TSymbolTable::insertConstIntExt(ESymbolLevel level, void TSymbolTable::insertConstIntExt(ESymbolLevel level,
TExtension ext, TExtension ext,
const ImmutableString &name, const ImmutableString &name,
int value) int value)
...@@ -450,11 +411,11 @@ bool TSymbolTable::insertConstIntExt(ESymbolLevel level, ...@@ -450,11 +411,11 @@ bool TSymbolTable::insertConstIntExt(ESymbolLevel level,
TConstantUnion *unionArray = new TConstantUnion[1]; TConstantUnion *unionArray = new TConstantUnion[1];
unionArray[0].setIConst(value); unionArray[0].setIConst(value);
constant->shareConstPointer(unionArray); constant->shareConstPointer(unionArray);
return insert(level, constant); insertBuiltIn(level, constant);
} }
template <TPrecision precision> template <TPrecision precision>
bool TSymbolTable::insertConstIvec3(ESymbolLevel level, void TSymbolTable::insertConstIvec3(ESymbolLevel level,
const ImmutableString &name, const ImmutableString &name,
const std::array<int, 3> &values) const std::array<int, 3> &values)
{ {
...@@ -468,7 +429,7 @@ bool TSymbolTable::insertConstIvec3(ESymbolLevel level, ...@@ -468,7 +429,7 @@ bool TSymbolTable::insertConstIvec3(ESymbolLevel level,
} }
constantIvec3->shareConstPointer(unionArray); constantIvec3->shareConstPointer(unionArray);
return insert(level, constantIvec3); insertBuiltIn(level, constantIvec3);
} }
void TSymbolTable::insertBuiltIn(ESymbolLevel level, void TSymbolTable::insertBuiltIn(ESymbolLevel level,
...@@ -630,7 +591,7 @@ void TSymbolTable::insertBuiltIn(ESymbolLevel level, ...@@ -630,7 +591,7 @@ void TSymbolTable::insertBuiltIn(ESymbolLevel level,
new TFunction(this, ImmutableString(name), ext, params, paramCount, rvalue, op, false); new TFunction(this, ImmutableString(name), ext, params, paramCount, rvalue, op, false);
ASSERT(hasUnmangledBuiltInAtLevel(name, level)); ASSERT(hasUnmangledBuiltInAtLevel(name, level));
insert(level, function); insertBuiltIn(level, function);
} }
} }
...@@ -671,7 +632,7 @@ void TSymbolTable::insertBuiltInFunctionNoParameters(ESymbolLevel level, ...@@ -671,7 +632,7 @@ void TSymbolTable::insertBuiltInFunctionNoParameters(ESymbolLevel level,
const char *name) const char *name)
{ {
insertUnmangledBuiltIn(name, TExtension::UNDEFINED, level); insertUnmangledBuiltIn(name, TExtension::UNDEFINED, level);
insert(level, new TFunction(this, ImmutableString(name), TExtension::UNDEFINED, nullptr, 0, insertBuiltIn(level, new TFunction(this, ImmutableString(name), TExtension::UNDEFINED, nullptr, 0,
rvalue, op, false)); rvalue, op, false));
} }
...@@ -682,7 +643,7 @@ void TSymbolTable::insertBuiltInFunctionNoParametersExt(ESymbolLevel level, ...@@ -682,7 +643,7 @@ void TSymbolTable::insertBuiltInFunctionNoParametersExt(ESymbolLevel level,
const char *name) const char *name)
{ {
insertUnmangledBuiltIn(name, ext, level); insertUnmangledBuiltIn(name, ext, level);
insert(level, new TFunction(this, ImmutableString(name), ext, nullptr, 0, rvalue, op, false)); insertBuiltIn(level, new TFunction(this, ImmutableString(name), ext, nullptr, 0, rvalue, op, false));
} }
void TSymbolTable::setDefaultPrecision(TBasicType type, TPrecision prec) void TSymbolTable::setDefaultPrecision(TBasicType type, TPrecision prec)
...@@ -1482,7 +1443,7 @@ void TSymbolTable::initializeBuiltInVariables(sh::GLenum type, ...@@ -1482,7 +1443,7 @@ void TSymbolTable::initializeBuiltInVariables(sh::GLenum type,
fields->push_back(diff); fields->push_back(diff);
TStructure *depthRangeStruct = new TStructure(this, ImmutableString("gl_DepthRangeParameters"), TStructure *depthRangeStruct = new TStructure(this, ImmutableString("gl_DepthRangeParameters"),
fields, SymbolType::BuiltIn); fields, SymbolType::BuiltIn);
insertStructType(COMMON_BUILTINS, depthRangeStruct); insertBuiltIn(COMMON_BUILTINS, depthRangeStruct);
TType *depthRangeType = new TType(depthRangeStruct); TType *depthRangeType = new TType(depthRangeStruct);
depthRangeType->setQualifier(EvqUniform); depthRangeType->setQualifier(EvqUniform);
depthRangeType->realize(); depthRangeType->realize();
...@@ -1781,7 +1742,7 @@ void TSymbolTable::initializeBuiltInVariables(sh::GLenum type, ...@@ -1781,7 +1742,7 @@ void TSymbolTable::initializeBuiltInVariables(sh::GLenum type,
TInterfaceBlock *glPerVertexInBlock = TInterfaceBlock *glPerVertexInBlock =
new TInterfaceBlock(this, glPerVertexString, glPerVertexFieldList, new TInterfaceBlock(this, glPerVertexString, glPerVertexFieldList,
TLayoutQualifier::Create(), SymbolType::BuiltIn, extension); TLayoutQualifier::Create(), SymbolType::BuiltIn, extension);
insertInterfaceBlock(ESSL3_1_BUILTINS, glPerVertexInBlock); insertBuiltIn(ESSL3_1_BUILTINS, glPerVertexInBlock);
// The array size of gl_in is undefined until we get a valid input primitive // The array size of gl_in is undefined until we get a valid input primitive
// declaration. // declaration.
......
...@@ -43,9 +43,7 @@ ...@@ -43,9 +43,7 @@
namespace sh namespace sh
{ {
// Define ESymbolLevel as int rather than an enum since level can go // Define ESymbolLevel as int rather than an enum so that we can do arithmetic on it.
// above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the
// compiler optimizes the >= of the last element to ==.
typedef int ESymbolLevel; typedef int ESymbolLevel;
const int COMMON_BUILTINS = 0; const int COMMON_BUILTINS = 0;
const int ESSL1_BUILTINS = 1; const int ESSL1_BUILTINS = 1;
...@@ -55,7 +53,6 @@ const int ESSL3_1_BUILTINS = 3; ...@@ -55,7 +53,6 @@ const int ESSL3_1_BUILTINS = 3;
// features in ANGLE's GLSL backend. They're not visible to the parser. // features in ANGLE's GLSL backend. They're not visible to the parser.
const int GLSL_BUILTINS = 4; const int GLSL_BUILTINS = 4;
const int LAST_BUILTIN_LEVEL = GLSL_BUILTINS; const int LAST_BUILTIN_LEVEL = GLSL_BUILTINS;
const int GLOBAL_LEVEL = 5;
struct UnmangledBuiltIn struct UnmangledBuiltIn
{ {
...@@ -76,22 +73,16 @@ class TSymbolTable : angle::NonCopyable ...@@ -76,22 +73,16 @@ class TSymbolTable : angle::NonCopyable
~TSymbolTable(); ~TSymbolTable();
// When the symbol table is initialized with the built-ins, there should bool isEmpty() const;
// 'push' calls, so that built-ins are at level 0 and the shader bool atGlobalLevel() const;
// globals are at level 1.
bool isEmpty() const { return mTable.empty(); }
bool atBuiltInLevel() const { return currentLevel() <= LAST_BUILTIN_LEVEL; }
bool atGlobalLevel() const { return currentLevel() == GLOBAL_LEVEL; }
void push(); void push();
void pop(); void pop();
// The declare* entry points are used when parsing and declare symbols at the current scope. // Declare a non-function symbol at the current scope. Return true in case the declaration was
// They return the created true in case the declaration was successful, and false if the // successful, and false if the declaration failed due to redefinition.
// declaration failed due to redefinition. bool declare(TSymbol *symbol);
bool declareVariable(TVariable *variable);
bool declareStructType(TStructure *str);
bool declareInterfaceBlock(TInterfaceBlock *interfaceBlock);
// Functions are always declared at global scope. // Functions are always declared at global scope.
void declareUserDefinedFunction(TFunction *function, bool insertUnmangledName); void declareUserDefinedFunction(TFunction *function, bool insertUnmangledName);
...@@ -151,34 +142,24 @@ class TSymbolTable : angle::NonCopyable ...@@ -151,34 +142,24 @@ class TSymbolTable : angle::NonCopyable
void pushBuiltInLevel(); void pushBuiltInLevel();
ESymbolLevel currentLevel() const
{
return static_cast<ESymbolLevel>(mTable.size() + LAST_BUILTIN_LEVEL);
}
// The insert* entry points are used when initializing the symbol table with built-ins. // The insert* entry points are used when initializing the symbol table with built-ins.
// They return the created symbol / true in case the declaration was successful, and nullptr / void insertVariable(ESymbolLevel level, const ImmutableString &name, const TType *type);
// false if the declaration failed due to redefinition.
TVariable *insertVariable(ESymbolLevel level, const ImmutableString &name, const TType *type);
void insertVariableExt(ESymbolLevel level, void insertVariableExt(ESymbolLevel level,
TExtension ext, TExtension ext,
const ImmutableString &name, const ImmutableString &name,
const TType *type); const TType *type);
bool insertVariable(ESymbolLevel level, TVariable *variable);
bool insertStructType(ESymbolLevel level, TStructure *str);
bool insertInterfaceBlock(ESymbolLevel level, TInterfaceBlock *interfaceBlock);
template <TPrecision precision> template <TPrecision precision>
bool insertConstInt(ESymbolLevel level, const ImmutableString &name, int value); void insertConstInt(ESymbolLevel level, const ImmutableString &name, int value);
template <TPrecision precision> template <TPrecision precision>
bool insertConstIntExt(ESymbolLevel level, void insertConstIntExt(ESymbolLevel level,
TExtension ext, TExtension ext,
const ImmutableString &name, const ImmutableString &name,
int value); int value);
template <TPrecision precision> template <TPrecision precision>
bool insertConstIvec3(ESymbolLevel level, void insertConstIvec3(ESymbolLevel level,
const ImmutableString &name, const ImmutableString &name,
const std::array<int, 3> &values); const std::array<int, 3> &values);
...@@ -259,7 +240,7 @@ class TSymbolTable : angle::NonCopyable ...@@ -259,7 +240,7 @@ class TSymbolTable : angle::NonCopyable
const TType *type, const TType *type,
SymbolType symbolType); SymbolType symbolType);
bool insert(ESymbolLevel level, TSymbol *symbol); void insertBuiltIn(ESymbolLevel level, const TSymbol *symbol);
TFunction *findUserDefinedFunction(const ImmutableString &name) const; TFunction *findUserDefinedFunction(const ImmutableString &name) const;
......
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