summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/angle/src/compiler/translator/AddDefaultReturnStatements.cpp
blob: 4dfe60c0bcbd46d536199bf8665bb42c5cbea873 (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
//
// Copyright (c) 2016 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.
//
// AddDefaultReturnStatements.cpp: Add default return statements to functions that do not end in a
//                                 return.
//

#include "compiler/translator/AddDefaultReturnStatements.h"

#include "compiler/translator/IntermNode.h"
#include "compiler/translator/IntermNode_util.h"
#include "compiler/translator/util.h"

namespace sh
{

namespace
{

bool NeedsReturnStatement(TIntermFunctionDefinition *node, TType *returnType)
{
    *returnType = node->getFunctionPrototype()->getType();
    if (returnType->getBasicType() == EbtVoid)
    {
        return false;
    }

    TIntermBlock *bodyNode    = node->getBody();
    TIntermBranch *returnNode = bodyNode->getSequence()->back()->getAsBranchNode();
    if (returnNode != nullptr && returnNode->getFlowOp() == EOpReturn)
    {
        return false;
    }

    return true;
}

}  // anonymous namespace

void AddDefaultReturnStatements(TIntermBlock *root)
{
    TType returnType;
    for (TIntermNode *node : *root->getSequence())
    {
        TIntermFunctionDefinition *definition = node->getAsFunctionDefinition();
        if (definition != nullptr && NeedsReturnStatement(definition, &returnType))
        {
            TIntermBranch *branch = new TIntermBranch(EOpReturn, CreateZeroNode(returnType));

            TIntermBlock *bodyNode = definition->getBody();
            bodyNode->getSequence()->push_back(branch);
        }
    }
}

}  // namespace sh