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
//
// Copyright 2002 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.
//
// The SeparateDeclarations function processes declarations, so that in the end each declaration
// contains only one declarator.
// This is useful as an intermediate step when initialization needs to be separated from
// declaration, or when things need to be unfolded out of the initializer.
// Example:
// int a[1] = int[1](1), b[1] = int[1](2);
// gets transformed when run through this class into the AST equivalent of:
// int a[1] = int[1](1);
// int b[1] = int[1](2);
#include "compiler/translator/tree_ops/SeparateDeclarations.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "compiler/translator/tree_util/ReplaceVariable.h"
namespace sh
{
namespace
{
class SeparateDeclarationsTraverser : private TIntermTraverser
{
public:
[[nodiscard]] static bool apply(TCompiler *compiler,
TIntermNode *root,
TSymbolTable *symbolTable);
private:
SeparateDeclarationsTraverser(TSymbolTable *symbolTable);
bool visitDeclaration(Visit, TIntermDeclaration *node) override;
void visitSymbol(TIntermSymbol *symbol) override;
void separateDeclarator(TIntermSequence *sequence,
size_t index,
TIntermSequence *replacementDeclarations,
const TStructure **replacementStructure);
VariableReplacementMap mVariableMap;
};
bool SeparateDeclarationsTraverser::apply(TCompiler *compiler,
TIntermNode *root,
TSymbolTable *symbolTable)
{
SeparateDeclarationsTraverser separateDecl(symbolTable);
root->traverse(&separateDecl);
return separateDecl.updateTree(compiler, root);
}
SeparateDeclarationsTraverser::SeparateDeclarationsTraverser(TSymbolTable *symbolTable)
: TIntermTraverser(true, false, false, symbolTable)
{}
bool SeparateDeclarationsTraverser::visitDeclaration(Visit, TIntermDeclaration *node)
{
TIntermSequence *sequence = node->getSequence();
if (sequence->size() <= 1)
{
return true;
}
TIntermBlock *parentBlock = getParentNode()->getAsBlock();
ASSERT(parentBlock != nullptr);
TIntermSequence replacementDeclarations;
const TStructure *replacementStructure = nullptr;
for (size_t ii = 0; ii < sequence->size(); ++ii)
{
separateDeclarator(sequence, ii, &replacementDeclarations, &replacementStructure);
}
mMultiReplacements.emplace_back(parentBlock, node, std::move(replacementDeclarations));
return false;
}
void SeparateDeclarationsTraverser::visitSymbol(TIntermSymbol *symbol)
{
const TVariable *variable = &symbol->variable();
if (mVariableMap.count(variable) > 0)
{
queueAccessChainReplacement(mVariableMap[variable]->deepCopy());
}
}
void SeparateDeclarationsTraverser::separateDeclarator(TIntermSequence *sequence,
size_t index,
TIntermSequence *replacementDeclarations,
const TStructure **replacementStructure)
{
TIntermTyped *declarator = sequence->at(index)->getAsTyped();
const TType &declaratorType = declarator->getType();
// If the declaration is not simultaneously declaring a struct, can use the same declarator.
// Otherwise, the first declarator is taken as-is if the struct has a name.
const TStructure *structure = declaratorType.getStruct();
const bool isStructSpecifier = declaratorType.isStructSpecifier();
if (!isStructSpecifier || (index == 0 && structure->symbolType() != SymbolType::Empty))
{
TIntermDeclaration *replacementDeclaration = new TIntermDeclaration;
// Make sure to update the declarator's initializers if any.
declarator->traverse(this);
replacementDeclaration->appendDeclarator(declarator);
replacementDeclaration->setLine(declarator->getLine());
replacementDeclarations->push_back(replacementDeclaration);
return;
}
// If the struct is nameless, split it out first.
if (structure->symbolType() == SymbolType::Empty)
{
if (*replacementStructure == nullptr)
{
TStructure *newStructure =
new TStructure(mSymbolTable, kEmptyImmutableString, &structure->fields(),
SymbolType::AngleInternal);
newStructure->setAtGlobalScope(structure->atGlobalScope());
*replacementStructure = structure = newStructure;
TType *namedType = new TType(structure, true);
namedType->setQualifier(EvqGlobal);
TVariable *structVariable =
new TVariable(mSymbolTable, kEmptyImmutableString, namedType, SymbolType::Empty);
TIntermDeclaration *structDeclaration = new TIntermDeclaration;
structDeclaration->appendDeclarator(new TIntermSymbol(structVariable));
structDeclaration->setLine(declarator->getLine());
replacementDeclarations->push_back(structDeclaration);
}
else
{
structure = *replacementStructure;
}
}
// Redeclare the declarator but not as a struct specifier.
TIntermSymbol *asSymbol = declarator->getAsSymbolNode();
TIntermTyped *initializer = nullptr;
if (asSymbol == nullptr)
{
TIntermBinary *asBinary = declarator->getAsBinaryNode();
ASSERT(asBinary->getOp() == EOpInitialize);
asSymbol = asBinary->getLeft()->getAsSymbolNode();
initializer = asBinary->getRight();
// Make sure the initializer itself has its variables replaced if necessary.
if (initializer->getAsSymbolNode())
{
const TVariable *initializerVariable = &initializer->getAsSymbolNode()->variable();
if (mVariableMap.count(initializerVariable) > 0)
{
initializer = mVariableMap[initializerVariable]->deepCopy();
}
}
else
{
initializer->traverse(this);
}
}
ASSERT(asSymbol && asSymbol->variable().symbolType() != SymbolType::Empty);
TType *newType = new TType(structure, false);
newType->setQualifier(asSymbol->getType().getQualifier());
newType->makeArrays(asSymbol->getType().getArraySizes());
TVariable *replacementVar = new TVariable(mSymbolTable, asSymbol->getName(), newType,
asSymbol->variable().symbolType());
TIntermSymbol *replacementSymbol = new TIntermSymbol(replacementVar);
TIntermTyped *replacement = replacementSymbol;
if (initializer)
{
replacement = new TIntermBinary(EOpInitialize, replacement, initializer);
}
TIntermDeclaration *replacementDeclaration = new TIntermDeclaration;
replacementDeclaration->appendDeclarator(replacement);
replacementDeclaration->setLine(declarator->getLine());
replacementDeclarations->push_back(replacementDeclaration);
mVariableMap[&asSymbol->variable()] = replacementSymbol;
}
} // namespace
bool SeparateDeclarations(TCompiler *compiler, TIntermNode *root, TSymbolTable *symbolTable)
{
return SeparateDeclarationsTraverser::apply(compiler, root, symbolTable);
}
} // namespace sh