• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 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/tree_ops/AddDefaultReturnStatements.h"
11 
12 #include "compiler/translator/Compiler.h"
13 #include "compiler/translator/IntermNode.h"
14 #include "compiler/translator/tree_util/IntermNode_util.h"
15 #include "compiler/translator/util.h"
16 
17 namespace sh
18 {
19 
20 namespace
21 {
22 
NeedsReturnStatement(TIntermFunctionDefinition * node,TType * returnType)23 bool NeedsReturnStatement(TIntermFunctionDefinition *node, TType *returnType)
24 {
25     *returnType = node->getFunctionPrototype()->getType();
26     if (returnType->getBasicType() == EbtVoid)
27     {
28         return false;
29     }
30 
31     TIntermBlock *bodyNode    = node->getBody();
32     TIntermBranch *returnNode = bodyNode->getSequence()->back()->getAsBranchNode();
33     if (returnNode != nullptr && returnNode->getFlowOp() == EOpReturn)
34     {
35         return false;
36     }
37 
38     return true;
39 }
40 
41 }  // anonymous namespace
42 
AddDefaultReturnStatements(TCompiler * compiler,TIntermBlock * root)43 bool AddDefaultReturnStatements(TCompiler *compiler, TIntermBlock *root)
44 {
45     TType returnType;
46     for (TIntermNode *node : *root->getSequence())
47     {
48         TIntermFunctionDefinition *definition = node->getAsFunctionDefinition();
49         if (definition != nullptr && NeedsReturnStatement(definition, &returnType))
50         {
51             TIntermBranch *branch = new TIntermBranch(EOpReturn, CreateZeroNode(returnType));
52 
53             TIntermBlock *bodyNode = definition->getBody();
54             bodyNode->getSequence()->push_back(branch);
55         }
56     }
57 
58     return compiler->validateAST(root);
59 }
60 
61 }  // namespace sh
62