summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/angle/src/compiler/translator/ArrayReturnValueToOutParameter.cpp
blob: 510ade84c1f1b087f0631eda46199134c17791be (plain)
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
//
// Copyright (c) 2002-2015 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 ArrayReturnValueToOutParameter function changes return values of an array type to out parameters in
// function definitions, prototypes, and call sites.

#include "compiler/translator/ArrayReturnValueToOutParameter.h"

#include "compiler/translator/IntermNode.h"

namespace
{

void CopyAggregateChildren(TIntermAggregate *from, TIntermAggregate *to)
{
    const TIntermSequence *fromSequence = from->getSequence();
    for (size_t ii = 0; ii < fromSequence->size(); ++ii)
    {
        to->getSequence()->push_back(fromSequence->at(ii));
    }
}

TIntermSymbol *CreateReturnValueSymbol(const TType &type)
{
    TIntermSymbol *node = new TIntermSymbol(0, "angle_return", type);
    node->setInternal(true);
    return node;
}

TIntermSymbol *CreateReturnValueOutSymbol(const TType &type)
{
    TType outType(type);
    outType.setQualifier(EvqOut);
    return CreateReturnValueSymbol(outType);
}

TIntermAggregate *CreateReplacementCall(TIntermAggregate *originalCall, TIntermTyped *returnValueTarget)
{
    TIntermAggregate *replacementCall = new TIntermAggregate(EOpFunctionCall);
    replacementCall->setType(TType(EbtVoid));
    replacementCall->setUserDefined();
    replacementCall->setNameObj(originalCall->getNameObj());
    replacementCall->setFunctionId(originalCall->getFunctionId());
    replacementCall->setLine(originalCall->getLine());
    TIntermSequence *replacementParameters = replacementCall->getSequence();
    TIntermSequence *originalParameters = originalCall->getSequence();
    for (auto &param : *originalParameters)
    {
        replacementParameters->push_back(param);
    }
    replacementParameters->push_back(returnValueTarget);
    return replacementCall;
}

class ArrayReturnValueToOutParameterTraverser : private TIntermTraverser
{
  public:
    static void apply(TIntermNode *root, unsigned int *temporaryIndex);
  private:
    ArrayReturnValueToOutParameterTraverser();

    bool visitAggregate(Visit visit, TIntermAggregate *node) override;
    bool visitBranch(Visit visit, TIntermBranch *node) override;
    bool visitBinary(Visit visit, TIntermBinary *node) override;

    bool mInFunctionWithArrayReturnValue;
};

void ArrayReturnValueToOutParameterTraverser::apply(TIntermNode *root, unsigned int *temporaryIndex)
{
    ArrayReturnValueToOutParameterTraverser arrayReturnValueToOutParam;
    arrayReturnValueToOutParam.useTemporaryIndex(temporaryIndex);
    root->traverse(&arrayReturnValueToOutParam);
    arrayReturnValueToOutParam.updateTree();
}

ArrayReturnValueToOutParameterTraverser::ArrayReturnValueToOutParameterTraverser()
    : TIntermTraverser(true, false, true),
      mInFunctionWithArrayReturnValue(false)
{
}

bool ArrayReturnValueToOutParameterTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
{
    if (visit == PreVisit)
    {
        if (node->isArray())
        {
            if (node->getOp() == EOpFunction)
            {
                // Replace the parameters child node of the function definition with another node
                // that has the out parameter added.
                // Also set the function to return void.

                TIntermAggregate *params = node->getSequence()->front()->getAsAggregate();
                ASSERT(params != nullptr && params->getOp() == EOpParameters);

                TIntermAggregate *replacementParams = new TIntermAggregate;
                replacementParams->setOp(EOpParameters);
                CopyAggregateChildren(params, replacementParams);
                replacementParams->getSequence()->push_back(CreateReturnValueOutSymbol(node->getType()));
                replacementParams->setLine(params->getLine());

                mReplacements.push_back(NodeUpdateEntry(node, params, replacementParams, false));

                node->setType(TType(EbtVoid));

                mInFunctionWithArrayReturnValue = true;
            }
            else if (node->getOp() == EOpPrototype)
            {
                // Replace the whole prototype node with another node that has the out parameter added.
                TIntermAggregate *replacement = new TIntermAggregate;
                replacement->setOp(EOpPrototype);
                CopyAggregateChildren(node, replacement);
                replacement->getSequence()->push_back(CreateReturnValueOutSymbol(node->getType()));
                replacement->setUserDefined();
                replacement->setNameObj(node->getNameObj());
                replacement->setFunctionId(node->getFunctionId());
                replacement->setLine(node->getLine());
                replacement->setType(TType(EbtVoid));

                mReplacements.push_back(NodeUpdateEntry(getParentNode(), node, replacement, false));
            }
            else if (node->getOp() == EOpFunctionCall)
            {
                // Handle call sites where the returned array is not assigned.
                // Examples where f() is a function returning an array:
                // 1. f();
                // 2. another_array == f();
                // 3. another_function(f());
                // 4. return f();
                // Cases 2 to 4 are already converted to simpler cases by SeparateExpressionsReturningArrays, so we
                // only need to worry about the case where a function call returning an array forms an expression by
                // itself.
                TIntermAggregate *parentAgg = getParentNode()->getAsAggregate();
                if (parentAgg != nullptr && parentAgg->getOp() == EOpSequence)
                {
                    nextTemporaryIndex();
                    TIntermSequence replacements;
                    replacements.push_back(createTempDeclaration(node->getType()));
                    TIntermSymbol *returnSymbol = createTempSymbol(node->getType());
                    replacements.push_back(CreateReplacementCall(node, returnSymbol));
                    mMultiReplacements.push_back(NodeReplaceWithMultipleEntry(parentAgg, node, replacements));
                }
                return false;
            }
        }
    }
    else if (visit == PostVisit)
    {
        if (node->getOp() == EOpFunction)
        {
            mInFunctionWithArrayReturnValue = false;
        }
    }
    return true;
}

bool ArrayReturnValueToOutParameterTraverser::visitBranch(Visit visit, TIntermBranch *node)
{
    if (mInFunctionWithArrayReturnValue && node->getFlowOp() == EOpReturn)
    {
        // Instead of returning a value, assign to the out parameter and then return.
        TIntermSequence replacements;

        TIntermBinary *replacementAssignment = new TIntermBinary(EOpAssign);
        TIntermTyped *expression = node->getExpression();
        ASSERT(expression != nullptr);
        replacementAssignment->setLeft(CreateReturnValueSymbol(expression->getType()));
        replacementAssignment->setRight(node->getExpression());
        replacementAssignment->setType(expression->getType());
        replacementAssignment->setLine(expression->getLine());
        replacements.push_back(replacementAssignment);

        TIntermBranch *replacementBranch = new TIntermBranch(EOpReturn, nullptr);
        replacementBranch->setLine(node->getLine());
        replacements.push_back(replacementBranch);

        mMultiReplacements.push_back(NodeReplaceWithMultipleEntry(getParentNode()->getAsAggregate(), node, replacements));
    }
    return false;
}

bool ArrayReturnValueToOutParameterTraverser::visitBinary(Visit visit, TIntermBinary *node)
{
    if (node->getOp() == EOpAssign && node->getLeft()->isArray())
    {
        TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
        if (rightAgg != nullptr && rightAgg->getOp() == EOpFunctionCall && rightAgg->isUserDefined())
        {
            TIntermAggregate *replacementCall = CreateReplacementCall(rightAgg, node->getLeft());
            mReplacements.push_back(NodeUpdateEntry(getParentNode(), node, replacementCall, false));
        }
    }
    return false;
}

} // namespace

void ArrayReturnValueToOutParameter(TIntermNode *root, unsigned int *temporaryIndex)
{
    ArrayReturnValueToOutParameterTraverser::apply(root, temporaryIndex);
}