Line data Source code
1 : //
2 : // Copyright (c) 2016 The ANGLE Project Authors. All rights reserved.
3 : // Use of this source code is governed by a BSD-style license that can be
4 : // found in the LICENSE file.
5 : //
6 : // AddDefaultReturnStatements.cpp: Add default return statements to functions that do not end in a
7 : // return.
8 : //
9 :
10 : #include "compiler/translator/AddDefaultReturnStatements.h"
11 :
12 : #include "compiler/translator/IntermNode.h"
13 : #include "compiler/translator/util.h"
14 :
15 : namespace sh
16 : {
17 :
18 : namespace
19 : {
20 :
21 0 : class AddDefaultReturnStatementsTraverser : private TIntermTraverser
22 : {
23 : public:
24 0 : static void Apply(TIntermNode *root)
25 : {
26 0 : AddDefaultReturnStatementsTraverser separateInit;
27 0 : root->traverse(&separateInit);
28 0 : separateInit.updateTree();
29 0 : }
30 :
31 : private:
32 0 : AddDefaultReturnStatementsTraverser() : TIntermTraverser(true, false, false) {}
33 :
34 0 : static bool IsFunctionWithoutReturnStatement(TIntermFunctionDefinition *node, TType *returnType)
35 : {
36 0 : *returnType = node->getType();
37 0 : if (node->getType().getBasicType() == EbtVoid)
38 : {
39 0 : return false;
40 : }
41 :
42 0 : TIntermBlock *bodyNode = node->getBody();
43 0 : TIntermBranch *returnNode = bodyNode->getSequence()->back()->getAsBranchNode();
44 0 : if (returnNode != nullptr && returnNode->getFlowOp() == EOpReturn)
45 : {
46 0 : return false;
47 : }
48 :
49 0 : return true;
50 : }
51 :
52 0 : bool visitFunctionDefinition(Visit, TIntermFunctionDefinition *node) override
53 : {
54 0 : TType returnType;
55 0 : if (IsFunctionWithoutReturnStatement(node, &returnType))
56 : {
57 : TIntermBranch *branch =
58 0 : new TIntermBranch(EOpReturn, TIntermTyped::CreateZero(returnType));
59 :
60 0 : TIntermBlock *bodyNode = node->getBody();
61 0 : bodyNode->getSequence()->push_back(branch);
62 :
63 0 : return false;
64 : }
65 :
66 0 : return true;
67 : }
68 : };
69 : } // anonymous namespace
70 :
71 0 : void AddDefaultReturnStatements(TIntermNode *node)
72 : {
73 0 : AddDefaultReturnStatementsTraverser::Apply(node);
74 0 : }
75 :
76 : } // namespace sh
|