Hash :
df90bbc5
Author :
Date :
2024-07-12T18:04:53
Refactoring: move angle::HashMap and HashSet to own header Underlying abseil includes pull in a large set of headers Bug: angleproject:42266508 Change-Id: Icee47143a8a59bb0795a054b67c0aa4ddcfca4d4 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5704137 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
//
// Copyright 2024 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// SeparateStructFromFunctionDeclarations: Separate struct declarations from function declaration
// return type.
//
#include "compiler/translator/tree_ops/SeparateStructFromFunctionDeclarations.h"
#include "common/hash_containers.h"
#include "compiler/translator/Compiler.h"
#include "compiler/translator/IntermRebuild.h"
#include "compiler/translator/SymbolTable.h"
namespace sh
{
namespace
{
class SeparateStructFromFunctionDeclarationsTraverser : public TIntermRebuild
{
public:
explicit SeparateStructFromFunctionDeclarationsTraverser(TCompiler &compiler)
: TIntermRebuild(compiler, true, true)
{}
PreResult visitFunctionPrototypePre(TIntermFunctionPrototype &node) override
{
const TFunction *function = node.getFunction();
if (mFunctionsToReplace.count(function) > 0)
{
TIntermFunctionPrototype *newFuncProto =
new TIntermFunctionPrototype(mFunctionsToReplace[function]);
return newFuncProto;
}
else if (node.getType().isStructSpecifier())
{
const TType &oldType = node.getType();
const TStructure *structure = oldType.getStruct();
// Name unnamed inline structs
if (structure->symbolType() == SymbolType::Empty)
{
structure = new TStructure(&mSymbolTable, kEmptyImmutableString,
&structure->fields(), SymbolType::AngleInternal);
}
TVariable *structVar = new TVariable(&mSymbolTable, ImmutableString(""),
new TType(structure, true), SymbolType::Empty);
ASSERT(!mStructDeclarations.empty());
mStructDeclarations.back().push_back(new TIntermDeclaration({structVar}));
TType *returnType = new TType(structure, false);
if (oldType.isArray())
{
returnType->makeArrays(oldType.getArraySizes());
}
returnType->setQualifier(oldType.getQualifier());
const TFunction *oldFunc = function;
ASSERT(oldFunc->symbolType() == SymbolType::UserDefined);
const TFunction *newFunc = cloneFunctionAndChangeReturnType(oldFunc, returnType);
mFunctionsToReplace[oldFunc] = newFunc;
return new TIntermFunctionPrototype(newFunc);
}
return node;
}
PreResult visitAggregatePre(TIntermAggregate &node) override
{
const TFunction *function = node.getFunction();
if (mFunctionsToReplace.count(function) > 0)
{
TIntermAggregate *replacementNode = TIntermAggregate::CreateFunctionCall(
*mFunctionsToReplace[function], node.getSequence());
return PreResult(replacementNode, VisitBits::Children);
}
return node;
}
PreResult visitBlockPre(TIntermBlock &node) override
{
mStructDeclarations.push_back({});
return node;
}
PostResult visitBlockPost(TIntermBlock &node) override
{
ASSERT(!mStructDeclarations.empty());
std::vector<TIntermDeclaration *> declarations = mStructDeclarations.back();
mStructDeclarations.pop_back();
if (!declarations.empty())
{
TIntermBlock *blockWithStructDeclarations = new TIntermBlock();
if (node.isTreeRoot())
{
blockWithStructDeclarations->setIsTreeRoot();
}
for (TIntermDeclaration *structDecl : declarations)
{
blockWithStructDeclarations->appendStatement(structDecl);
}
for (TIntermNode *statement : *node.getSequence())
{
blockWithStructDeclarations->appendStatement(statement);
}
return blockWithStructDeclarations;
}
return node;
}
private:
const TFunction *cloneFunctionAndChangeReturnType(const TFunction *oldFunc,
const TType *newReturnType)
{
ASSERT(oldFunc->symbolType() == SymbolType::UserDefined);
TFunction *newFunc = new TFunction(&mSymbolTable, oldFunc->name(), oldFunc->symbolType(),
newReturnType, oldFunc->isKnownToNotHaveSideEffects());
if (oldFunc->isDefined())
{
newFunc->setDefined();
}
if (oldFunc->hasPrototypeDeclaration())
{
newFunc->setHasPrototypeDeclaration();
}
const size_t paramCount = oldFunc->getParamCount();
for (size_t i = 0; i < paramCount; ++i)
{
const TVariable *var = oldFunc->getParam(i);
newFunc->addParameter(var);
}
return newFunc;
}
using FunctionReplacement = angle::HashMap<const TFunction *, const TFunction *>;
FunctionReplacement mFunctionsToReplace;
// Stack of struct declarations to insert per block
std::vector<std::vector<TIntermDeclaration *>> mStructDeclarations;
};
} // anonymous namespace
bool SeparateStructFromFunctionDeclarations(TCompiler &compiler, TIntermBlock &root)
{
SeparateStructFromFunctionDeclarationsTraverser separateStructDecls(compiler);
return separateStructDecls.rebuildRoot(root);
}
} // namespace sh