Hash :
91976352
Author :
Date :
2022-06-21T15:41:02
Use C++17 attributes instead of custom macros Bug: angleproject:6747 Change-Id: Iad6c7cd8a18d028e01da49b647c5d01af11e0522 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3718999 Reviewed-by: Cody Northrop <cnorthrop@google.com> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
//
// Copyright 2020 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.
//
#ifndef COMPILER_TRANSLATOR_TRANSLATORMETALDIRECT_INTERMREBUILD_H_
#define COMPILER_TRANSLATOR_TRANSLATORMETALDIRECT_INTERMREBUILD_H_
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "compiler/translator/tree_util/NodeType.h"
namespace sh
{
// Walks the tree to rebuild nodes.
// This class is intended to be derived with overridden visitXXX functions.
//
// Each visitXXX function that does not have a Visit parameter simply has the visitor called
// exactly once, regardless of (preVisit) or (postVisit) values.
// Each visitXXX function that has a Visit parameter behaves as follows:
// * If (preVisit):
// - The node is visited before children are traversed.
// - The returned value is used to replace the visited node. The returned value may be the same
// as the original node.
// - If multiple nodes are returned, children and post visits of the returned nodes are not
// preformed, even if it is a singleton collection.
// * If (childVisit)
// - If any new children are returned, the node is automatically rebuilt with the new children
// before post visit.
// - Depending on the type of the node, null children may be discarded.
// - Ill-typed children cause rebuild errors. Ill-typed means the node to automatically rebuild
// cannot accept a child of a certain type as input to its constructor.
// - Only instances of TIntermAggregateBase can accept Multi results for any of its children.
// If supplied, the nodes are spliced children at the spot of the original child.
// * If (postVisit)
// - The node is visited after any children are traversed.
// - Only after such a rebuild (or lack thereof), the post-visit is performed.
//
// Nodes in visit functions are allowed to be modified in place, including TIntermAggregateBase
// child sequences.
//
// The default implementations of all the visitXXX functions support full pre and post traversal
// without modifying the visited nodes.
//
class TIntermRebuild : angle::NonCopyable
{
enum class Action
{
ReplaceSingle,
ReplaceMulti,
Drop,
Fail,
};
public:
struct Fail
{};
enum VisitBits : size_t
{
// No bits are set.
Empty = 0u,
// Allow visit of returned node's children.
Children = 1u << 0u,
// Allow post visit of returned node.
Post = 1u << 1u,
// If (Children) bit, only visit if the returned node is the same as the original node.
ChildrenRequiresSame = 1u << 2u,
// If (Post) bit, only visit if the returned node is the same as the original node.
PostRequiresSame = 1u << 3u,
RequireSame = ChildrenRequiresSame | PostRequiresSame,
Neither = Empty,
Both = Children | Post,
BothWhenSame = Both | RequireSame,
};
private:
struct NodeStackGuard;
template <typename T>
struct ConsList
{
T value;
ConsList<T> *tail;
};
class BaseResult
{
BaseResult(const BaseResult &) = delete;
BaseResult &operator=(const BaseResult &) = delete;
public:
BaseResult(BaseResult &&other) = default;
BaseResult(BaseResult &other); // For subclass move constructor impls
BaseResult(TIntermNode &node, VisitBits visit);
BaseResult(TIntermNode *node, VisitBits visit);
BaseResult(std::nullptr_t);
BaseResult(Fail);
BaseResult(std::vector<TIntermNode *> &&nodes);
void moveAssignImpl(BaseResult &other); // For subclass move assign impls
static BaseResult Multi(std::vector<TIntermNode *> &&nodes);
template <typename Iter>
static BaseResult Multi(Iter nodesBegin, Iter nodesEnd)
{
std::vector<TIntermNode *> nodes;
for (Iter nodesCurr = nodesBegin; nodesCurr != nodesEnd; ++nodesCurr)
{
nodes.push_back(*nodesCurr);
}
return std::move(nodes);
}
bool isFail() const;
bool isDrop() const;
TIntermNode *single() const;
const std::vector<TIntermNode *> *multi() const;
public:
Action mAction;
VisitBits mVisit;
TIntermNode *mSingle;
std::vector<TIntermNode *> mMulti;
};
public:
class PreResult : private BaseResult
{
friend class TIntermRebuild;
public:
PreResult(PreResult &&other);
PreResult(TIntermNode &node, VisitBits visit = VisitBits::BothWhenSame);
PreResult(TIntermNode *node, VisitBits visit = VisitBits::BothWhenSame);
PreResult(std::nullptr_t); // Used to drop a node.
PreResult(Fail); // Used to signal failure.
void operator=(PreResult &&other);
static PreResult Multi(std::vector<TIntermNode *> &&nodes)
{
return BaseResult::Multi(std::move(nodes));
}
template <typename Iter>
static PreResult Multi(Iter nodesBegin, Iter nodesEnd)
{
return BaseResult::Multi(nodesBegin, nodesEnd);
}
using BaseResult::isDrop;
using BaseResult::isFail;
using BaseResult::multi;
using BaseResult::single;
private:
PreResult(BaseResult &&other);
};
class PostResult : private BaseResult
{
friend class TIntermRebuild;
public:
PostResult(PostResult &&other);
PostResult(TIntermNode &node);
PostResult(TIntermNode *node);
PostResult(std::nullptr_t); // Used to drop a node
PostResult(Fail); // Used to signal failure.
void operator=(PostResult &&other);
static PostResult Multi(std::vector<TIntermNode *> &&nodes)
{
return BaseResult::Multi(std::move(nodes));
}
template <typename Iter>
static PostResult Multi(Iter nodesBegin, Iter nodesEnd)
{
return BaseResult::Multi(nodesBegin, nodesEnd);
}
using BaseResult::isDrop;
using BaseResult::isFail;
using BaseResult::multi;
using BaseResult::single;
private:
PostResult(BaseResult &&other);
};
public:
TIntermRebuild(TCompiler &compiler, bool preVisit, bool postVisit);
virtual ~TIntermRebuild();
// Rebuilds the tree starting at the provided root. If a new node would be returned for the
// root, the root node's children become that of the new node instead. Returns false if failure
// occurred.
[[nodiscard]] bool rebuildRoot(TIntermBlock &root);
protected:
virtual PreResult visitSymbolPre(TIntermSymbol &node);
virtual PreResult visitConstantUnionPre(TIntermConstantUnion &node);
virtual PreResult visitFunctionPrototypePre(TIntermFunctionPrototype &node);
virtual PreResult visitPreprocessorDirectivePre(TIntermPreprocessorDirective &node);
virtual PreResult visitUnaryPre(TIntermUnary &node);
virtual PreResult visitBinaryPre(TIntermBinary &node);
virtual PreResult visitTernaryPre(TIntermTernary &node);
virtual PreResult visitSwizzlePre(TIntermSwizzle &node);
virtual PreResult visitIfElsePre(TIntermIfElse &node);
virtual PreResult visitSwitchPre(TIntermSwitch &node);
virtual PreResult visitCasePre(TIntermCase &node);
virtual PreResult visitLoopPre(TIntermLoop &node);
virtual PreResult visitBranchPre(TIntermBranch &node);
virtual PreResult visitDeclarationPre(TIntermDeclaration &node);
virtual PreResult visitBlockPre(TIntermBlock &node);
virtual PreResult visitAggregatePre(TIntermAggregate &node);
virtual PreResult visitFunctionDefinitionPre(TIntermFunctionDefinition &node);
virtual PreResult visitGlobalQualifierDeclarationPre(TIntermGlobalQualifierDeclaration &node);
virtual PostResult visitSymbolPost(TIntermSymbol &node);
virtual PostResult visitConstantUnionPost(TIntermConstantUnion &node);
virtual PostResult visitFunctionPrototypePost(TIntermFunctionPrototype &node);
virtual PostResult visitPreprocessorDirectivePost(TIntermPreprocessorDirective &node);
virtual PostResult visitUnaryPost(TIntermUnary &node);
virtual PostResult visitBinaryPost(TIntermBinary &node);
virtual PostResult visitTernaryPost(TIntermTernary &node);
virtual PostResult visitSwizzlePost(TIntermSwizzle &node);
virtual PostResult visitIfElsePost(TIntermIfElse &node);
virtual PostResult visitSwitchPost(TIntermSwitch &node);
virtual PostResult visitCasePost(TIntermCase &node);
virtual PostResult visitLoopPost(TIntermLoop &node);
virtual PostResult visitBranchPost(TIntermBranch &node);
virtual PostResult visitDeclarationPost(TIntermDeclaration &node);
virtual PostResult visitBlockPost(TIntermBlock &node);
virtual PostResult visitAggregatePost(TIntermAggregate &node);
virtual PostResult visitFunctionDefinitionPost(TIntermFunctionDefinition &node);
virtual PostResult visitGlobalQualifierDeclarationPost(TIntermGlobalQualifierDeclaration &node);
// Can be used to rebuild a specific node during a traversal. Useful for fine control of
// rebuilding a node's children.
[[nodiscard]] PostResult rebuild(TIntermNode &node);
// Rebuilds the provided node in place. If a new node would be returned, the old node's children
// become that of the new node instead. Returns false if failure occurred.
[[nodiscard]] bool rebuildInPlace(TIntermAggregate &node);
// Rebuilds the provided node in place. If a new node would be returned, the old node's children
// become that of the new node instead. Returns false if failure occurred.
[[nodiscard]] bool rebuildInPlace(TIntermBlock &node);
// Rebuilds the provided node in place. If a new node would be returned, the old node's children
// become that of the new node instead. Returns false if failure occurred.
[[nodiscard]] bool rebuildInPlace(TIntermDeclaration &node);
// If currently at or below a function declaration body, this returns the function that encloses
// the currently visited node. (This returns null if at a function declaration node.)
const TFunction *getParentFunction() const;
TIntermNode *getParentNode(size_t offset = 0) const;
private:
template <typename Node>
[[nodiscard]] bool rebuildInPlaceImpl(Node &node);
PostResult traverseAny(TIntermNode &node);
template <typename Node>
Node *traverseAnyAs(TIntermNode &node);
template <typename Node>
bool traverseAnyAs(TIntermNode &node, Node *&out);
PreResult traversePre(TIntermNode &originalNode);
TIntermNode *traverseChildren(NodeType currNodeType,
const TIntermNode &originalNode,
TIntermNode &currNode,
VisitBits visit);
PostResult traversePost(NodeType nodeType,
const TIntermNode &originalNode,
TIntermNode &currNode,
VisitBits visit);
bool traverseAggregateBaseChildren(TIntermAggregateBase &node);
TIntermNode *traverseUnaryChildren(TIntermUnary &node);
TIntermNode *traverseBinaryChildren(TIntermBinary &node);
TIntermNode *traverseTernaryChildren(TIntermTernary &node);
TIntermNode *traverseSwizzleChildren(TIntermSwizzle &node);
TIntermNode *traverseIfElseChildren(TIntermIfElse &node);
TIntermNode *traverseSwitchChildren(TIntermSwitch &node);
TIntermNode *traverseCaseChildren(TIntermCase &node);
TIntermNode *traverseLoopChildren(TIntermLoop &node);
TIntermNode *traverseBranchChildren(TIntermBranch &node);
TIntermNode *traverseDeclarationChildren(TIntermDeclaration &node);
TIntermNode *traverseBlockChildren(TIntermBlock &node);
TIntermNode *traverseAggregateChildren(TIntermAggregate &node);
TIntermNode *traverseFunctionDefinitionChildren(TIntermFunctionDefinition &node);
TIntermNode *traverseGlobalQualifierDeclarationChildren(
TIntermGlobalQualifierDeclaration &node);
protected:
TCompiler &mCompiler;
TSymbolTable &mSymbolTable;
const TFunction *mParentFunc = nullptr;
GetNodeType getNodeType;
private:
ConsList<TIntermNode *> mNodeStack{nullptr, nullptr};
bool mPreVisit;
bool mPostVisit;
};
} // namespace sh
#endif // COMPILER_TRANSLATOR_TRANSLATORMETALDIRECT_INTERMREBUILD_H_