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