Commit 3778979c by John Kessenich

HLSL: non-static member functions: track and find active anonymous 'this' scopes and members.

Thanks to @steve-lunarg for his input and discussions on handling member functions.
parent f4ba25e0
......@@ -2743,12 +2743,16 @@ void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslF
std::vector<spv::Decoration> paramPrecisions;
glslang::TIntermSequence& parameters = glslFunction->getSequence()[0]->getAsAggregate()->getSequence();
bool implicitThis = (int)parameters.size() > 0 && parameters[0]->getAsSymbolNode()->getName() == glslangIntermediate->implicitThisName;
for (int p = 0; p < (int)parameters.size(); ++p) {
const glslang::TType& paramType = parameters[p]->getAsTyped()->getType();
spv::Id typeId = convertGlslangToSpvType(paramType);
if (paramType.containsOpaque() ||
// can we pass by reference?
if (paramType.containsOpaque() || // sampler, etc.
(paramType.getBasicType() == glslang::EbtBlock &&
paramType.getQualifier().storage == glslang::EvqBuffer))
paramType.getQualifier().storage == glslang::EvqBuffer) || // SSBO
p == 0 && implicitThis) // implicit 'this'
typeId = builder.makePointer(TranslateStorageClass(paramType), typeId);
else if (paramType.getQualifier().storage != glslang::EvqConstReadOnly)
typeId = builder.makePointer(spv::StorageClassFunction, typeId);
......@@ -2762,6 +2766,8 @@ void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslF
spv::Function *function = builder.makeFunctionEntry(TranslatePrecisionDecoration(glslFunction->getType()),
convertGlslangToSpvType(glslFunction->getType()),
glslFunction->getName().c_str(), paramTypes, paramPrecisions, &functionBlock);
if (implicitThis)
function->setImplicitThis();
// Track function to emit/call later
functionMap[glslFunction->getName().c_str()] = function;
......@@ -3233,7 +3239,8 @@ spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAgg
const glslang::TType& paramType = glslangArgs[a]->getAsTyped()->getType();
spv::Id arg;
if (paramType.containsOpaque() ||
(paramType.getBasicType() == glslang::EbtBlock && qualifiers[a] == glslang::EvqBuffer)) {
(paramType.getBasicType() == glslang::EbtBlock && qualifiers[a] == glslang::EvqBuffer) ||
(a == 0 && function->hasImplicitThis())) {
builder.setAccessChain(lValues[lValueCount]);
arg = builder.accessChainGetLValue();
++lValueCount;
......
......@@ -273,6 +273,10 @@ public:
const std::vector<Block*>& getBlocks() const { return blocks; }
void addLocalVariable(std::unique_ptr<Instruction> inst);
Id getReturnType() const { return functionInstruction.getTypeId(); }
void setImplicitThis() { implicitThis = true; }
bool hasImplicitThis() const { return implicitThis; }
void dump(std::vector<unsigned int>& out) const
{
// OpFunction
......@@ -296,6 +300,7 @@ protected:
Instruction functionInstruction;
std::vector<Instruction*> parameterInstructions;
std::vector<Block*> blocks;
bool implicitThis; // true if this is a member function expecting to be passed a 'this' as the first argument
};
//
......@@ -354,7 +359,7 @@ protected:
// - the OpFunction instruction
// - all the OpFunctionParameter instructions
__inline Function::Function(Id id, Id resultType, Id functionType, Id firstParamId, Module& parent)
: parent(parent), functionInstruction(id, resultType, OpFunction)
: parent(parent), functionInstruction(id, resultType, OpFunction), implicitThis(false)
{
// OpFunction
functionInstruction.addImmediateOperand(FunctionControlMaskNone);
......
struct Test
static float2 i = float2(1.0, 2.0);
struct type1
{
void setmem(float4 m) { memVar = m; }
void seti(int si) { i = si; }
float4 memVar;
float4 memFun(float4 a) : SV_Position
{
return 2 * a;
return i * a + memVar;
}
int memFun(int a) : SV_Position
{
return 2 + a;
return i + a - memVar.z;
}
int i;
};
static float2 j = i;
struct type2
{
float2 memFun() { return i; }
};
float4 main() : SV_Target0
{
Test test;
type1 test;
test.setmem(float4(2.0,2.0,2.0,2.0));
test.seti(17);
float4 f4 = float4(1.0,1.0,1.0,1.0);
f4 += test.memFun(float4(5.0f,5.0f,5.0f,5.0f));
f4 += test.memFun(7);
......
......@@ -2,5 +2,5 @@
// For the version, it uses the latest git tag followed by the number of commits.
// For the date, it uses the current date (when then script is run).
#define GLSLANG_REVISION "Overload400-PrecQual.1923"
#define GLSLANG_REVISION "Overload400-PrecQual.1929"
#define GLSLANG_DATE "21-Mar-2017"
......@@ -300,6 +300,7 @@ TFunction::TFunction(const TFunction& copyOf) : TSymbol(copyOf)
defined = copyOf.defined;
prototyped = copyOf.prototyped;
implicitThis = copyOf.implicitThis;
illegalImplicitThis = copyOf.illegalImplicitThis;
defaultParamCount = copyOf.defaultParamCount;
}
......@@ -324,6 +325,7 @@ TSymbolTableLevel* TSymbolTableLevel::clone() const
{
TSymbolTableLevel *symTableLevel = new TSymbolTableLevel();
symTableLevel->anonId = anonId;
symTableLevel->thisLevel = thisLevel;
std::vector<bool> containerCopied(anonId, false);
tLevel::const_iterator iter;
for (iter = level.begin(); iter != level.end(); ++iter) {
......
......@@ -219,12 +219,12 @@ public:
explicit TFunction(TOperator o) :
TSymbol(0),
op(o),
defined(false), prototyped(false), implicitThis(false), defaultParamCount(0) { }
defined(false), prototyped(false), implicitThis(false), illegalImplicitThis(false), defaultParamCount(0) { }
TFunction(const TString *name, const TType& retType, TOperator tOp = EOpNull) :
TSymbol(name),
mangledName(*name + '('),
op(tOp),
defined(false), prototyped(false), implicitThis(false), defaultParamCount(0)
defined(false), prototyped(false), implicitThis(false), illegalImplicitThis(false), defaultParamCount(0)
{
returnType.shallowCopy(retType);
declaredBuiltIn = retType.getQualifier().builtIn;
......@@ -251,9 +251,9 @@ public:
// Install 'this' as the first parameter.
// 'this' is reflected in the list of parameters, but not the mangled name.
virtual void addThisParameter(TType& type)
virtual void addThisParameter(TType& type, const char* name)
{
TParameter p = { NewPoolTString("this"), new TType, nullptr };
TParameter p = { NewPoolTString(name), new TType, nullptr };
p.type->shallowCopy(type);
parameters.insert(parameters.begin(), p);
}
......@@ -276,6 +276,8 @@ public:
virtual bool isPrototyped() const { return prototyped; }
virtual void setImplicitThis() { assert(writable); implicitThis = true; }
virtual bool hasImplicitThis() const { return implicitThis; }
virtual void setIllegalImplicitThis() { assert(writable); illegalImplicitThis = true; }
virtual bool hasIllegalImplicitThis() const { return illegalImplicitThis; }
// Return total number of parameters
virtual int getParamCount() const { return static_cast<int>(parameters.size()); }
......@@ -302,7 +304,11 @@ protected:
TOperator op;
bool defined;
bool prototyped;
bool implicitThis;
bool implicitThis; // True if this function is allowed to see all members of 'this'
bool illegalImplicitThis; // True if this function is not supposed to have access to dynamic members of 'this',
// even if it finds member variables in the symbol table.
// This is important for a static member function that has member variables in scope,
// but is not allowed to use them, or see hidden symbols instead.
int defaultParamCount;
};
......@@ -350,7 +356,7 @@ protected:
class TSymbolTableLevel {
public:
POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
TSymbolTableLevel() : defaultPrecision(0), anonId(0) { }
TSymbolTableLevel() : defaultPrecision(0), anonId(0), thisLevel(false) { }
~TSymbolTableLevel();
bool insert(TSymbol& symbol, bool separateNameSpaces)
......@@ -508,6 +514,9 @@ public:
TSymbolTableLevel* clone() const;
void readOnly();
void setThisLevel() { thisLevel = true; }
bool isThisLevel() const { return thisLevel; }
protected:
explicit TSymbolTableLevel(TSymbolTableLevel&);
TSymbolTableLevel& operator=(TSymbolTableLevel&);
......@@ -519,6 +528,8 @@ protected:
tLevel level; // named mappings
TPrecisionQualifier *defaultPrecision;
int anonId;
bool thisLevel; // True if this level of the symbol table is a structure scope containing member function
// that are supposed to see anonymous access to member variables.
};
class TSymbolTable {
......@@ -575,6 +586,20 @@ public:
table.push_back(new TSymbolTableLevel);
}
// Make a new symbol-table level to represent the scope introduced by a structure
// containing member functions, such that the member functions can find anonymous
// references to member variables.
//
// 'thisSymbol' should have a name of "" to trigger anonymous structure-member
// symbol finds.
void pushThis(TSymbol& thisSymbol)
{
assert(thisSymbol.getName().size() == 0);
table.push_back(new TSymbolTableLevel);
table.back()->setThisLevel();
insert(thisSymbol);
}
void pop(TPrecisionQualifier *p)
{
table[currentLevel()]->getPreviousDefaultPrecisions(p);
......@@ -661,6 +686,8 @@ public:
}
}
// Normal find of a symbol, that can optionally say whether the symbol was found
// at a built-in level or the current top-scope level.
TSymbol* find(const TString& name, bool* builtIn = 0, bool *currentScope = 0)
{
int level = currentLevel();
......@@ -678,6 +705,27 @@ public:
return symbol;
}
// Find of a symbol that returns how many layers deep of nested
// structures-with-member-functions ('this' scopes) deep the symbol was
// found in.
TSymbol* find(const TString& name, int& thisDepth)
{
int level = currentLevel();
TSymbol* symbol;
thisDepth = 0;
do {
if (table[level]->isThisLevel())
++thisDepth;
symbol = table[level]->find(name);
--level;
} while (symbol == 0 && level >= 0);
if (! table[level + 1]->isThisLevel())
thisDepth = 0;
return symbol;
}
bool isFunctionNameVariable(const TString& name) const
{
if (separateNameSpaces)
......
......@@ -428,6 +428,8 @@ public:
return semanticNameSet.insert(name).first->c_str();
}
const char* const implicitThisName = "@this";
protected:
TIntermSymbol* addSymbol(int Id, const TString&, const TType&, const TConstUnionArray&, TIntermTyped* subtree, const TSourceLoc&);
void error(TInfoSink& infoSink, const char*);
......
......@@ -1842,12 +1842,13 @@ bool HlslGrammar::acceptStruct(TType& type, TIntermNode*& nodeList)
for (int b = 0; b < (int)functionDeclarators.size(); ++b) {
// update signature
if (functionDeclarators[b].function->hasImplicitThis())
functionDeclarators[b].function->addThisParameter(type);
functionDeclarators[b].function->addThisParameter(type, intermediate.implicitThisName);
}
// All member functions get parsed inside the class/struct namespace and with the
// class/struct members in a symbol-table level.
parseContext.pushNamespace(structName);
parseContext.pushThisScope(type);
bool deferredSuccess = true;
for (int b = 0; b < (int)functionDeclarators.size() && deferredSuccess; ++b) {
// parse body
......@@ -1856,6 +1857,7 @@ bool HlslGrammar::acceptStruct(TType& type, TIntermNode*& nodeList)
deferredSuccess = false;
popTokenStream();
}
parseContext.popThisScope();
parseContext.popNamespace();
return deferredSuccess;
......@@ -2075,6 +2077,8 @@ bool HlslGrammar::acceptMemberFunctionDefinition(TIntermNode*& nodeList, const T
declarator.function = new TFunction(functionName, type);
if (type.getQualifier().storage == EvqTemporary)
declarator.function->setImplicitThis();
else
declarator.function->setIllegalImplicitThis();
// function_parameters
if (acceptFunctionParameters(*declarator.function)) {
......
......@@ -603,7 +603,8 @@ int HlslParseContext::getMatrixComponentsColumn(int rows, const TSwizzleSelector
//
TIntermTyped* HlslParseContext::handleVariable(const TSourceLoc& loc, const TString* string)
{
TSymbol* symbol = symbolTable.find(*string);
int thisDepth;
TSymbol* symbol = symbolTable.find(*string, thisDepth);
if (symbol && symbol->getAsVariable() && symbol->getAsVariable()->isUserType()) {
error(loc, "expected symbol, not user-defined type", string->c_str(), "");
return nullptr;
......@@ -613,14 +614,21 @@ TIntermTyped* HlslParseContext::handleVariable(const TSourceLoc& loc, const TStr
if (symbol && symbol->getNumExtensions())
requireExtensions(loc, symbol->getNumExtensions(), symbol->getExtensions(), symbol->getName().c_str());
const TVariable* variable;
const TVariable* variable = nullptr;
const TAnonMember* anon = symbol ? symbol->getAsAnonMember() : nullptr;
TIntermTyped* node = nullptr;
if (anon) {
// It was a member of an anonymous container.
// It was a member of an anonymous container, which could be a 'this' structure.
// Create a subtree for its dereference.
variable = anon->getAnonContainer().getAsVariable();
if (thisDepth > 0) {
variable = getImplicitThis(thisDepth);
if (variable == nullptr)
error(loc, "cannot access member variables (static member function?)", "this", "");
}
if (variable == nullptr)
variable = anon->getAnonContainer().getAsVariable();
TIntermTyped* container = intermediate.addSymbol(*variable, loc);
TIntermTyped* constNode = intermediate.addConstantUnion(anon->getMemberNumber(), loc);
node = intermediate.addIndex(EOpIndexDirectStruct, container, constNode, loc);
......@@ -1529,18 +1537,25 @@ TIntermAggregate* HlslParseContext::handleFunctionDefinition(const TSourceLoc& l
if (param.name != nullptr) {
TVariable *variable = new TVariable(param.name, *param.type);
// Insert the parameters with name in the symbol table.
if (! symbolTable.insert(*variable))
error(loc, "redefinition", variable->getName().c_str(), "");
else {
// Add the parameter to the AST
paramNodes = intermediate.growAggregate(paramNodes,
intermediate.addSymbol(*variable, loc),
loc);
if (i == 0 && function.hasImplicitThis()) {
// 'this' members are already in a symbol-table level,
// and we need to know what function parameter to map them to
symbolTable.makeInternalVariable(*variable);
pushImplicitThis(variable);
} else {
// Insert the parameters with name in the symbol table.
if (! symbolTable.insert(*variable))
error(loc, "redefinition", variable->getName().c_str(), "");
}
// Add the parameter to the AST
paramNodes = intermediate.growAggregate(paramNodes,
intermediate.addSymbol(*variable, loc),
loc);
} else
paramNodes = intermediate.growAggregate(paramNodes, intermediate.addSymbol(*param.type, loc), loc);
}
if (function.hasIllegalImplicitThis())
pushImplicitThis(nullptr);
intermediate.setAggregateOperator(paramNodes, EOpParameters, TType(EbtVoid), loc);
loopNestingLevel = 0;
......@@ -1826,6 +1841,8 @@ void HlslParseContext::handleFunctionBody(const TSourceLoc& loc, TFunction& func
node->getAsAggregate()->setName(function.getMangledName().c_str());
popScope();
if (function.hasImplicitThis())
popImplicitThis();
if (function.getType().getBasicType() != EbtVoid && ! functionReturnsValue)
error(loc, "function does not return a value:", "", function.getName().c_str());
......@@ -7087,6 +7104,15 @@ TIntermNode* HlslParseContext::addSwitch(const TSourceLoc& loc, TIntermTyped* ex
return switchNode;
}
// Make a new symbol-table level that is made out of the members of a structure.
// This should be done as an anonymous struct (name is "") so that the symbol table
// finds the members with on explicit reference to a 'this' variable.
void HlslParseContext::pushThisScope(const TType& thisStruct)
{
TVariable& thisVariable = *new TVariable(NewPoolTString(""), thisStruct);
symbolTable.pushThis(thisVariable);
}
// Track levels of class/struct/namespace nesting with a prefix string using
// the type names separated by the scoping operator. E.g., two levels
// would look like:
......
......@@ -160,6 +160,13 @@ public:
void pushScope() { symbolTable.push(); }
void popScope() { symbolTable.pop(0); }
void pushThisScope(const TType&);
void popThisScope() { symbolTable.pop(0); }
void pushImplicitThis(TVariable* thisParameter) { implicitThisStack.push_back(thisParameter); }
void popImplicitThis() { implicitThisStack.pop_back(); }
TVariable* getImplicitThis(int thisDepth) const { return implicitThisStack[implicitThisStack.size() - thisDepth]; }
void pushNamespace(const TString& name);
void popNamespace();
TString* getFullNamespaceName(const TString& localName) const;
......@@ -387,7 +394,8 @@ protected:
TString patchConstantFunctionName; // hull shader patch constant function name, from function level attribute.
TMap<TBuiltInVariable, TSymbol*> builtInLinkageSymbols; // used for tessellation, finding declared builtins
TVector<TString> currentTypePrefix;
TVector<TString> currentTypePrefix; // current scoping prefix for nested structures
TVector<TVariable*> implicitThisStack; // currently active 'this' variables for nested structures
};
// This is the prefix we use for builtin methods to avoid namespace collisions with
......
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