1//
2// Copyright (c) 2017 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
7// FindMain.cpp: Find the main() function definition in a given AST.
8
9#include "compiler/translator/tree_util/FindMain.h"
10
11#include "compiler/translator/IntermNode.h"
12#include "compiler/translator/Symbol.h"
13
14namespace sh
15{
16
17size_t FindMainIndex(TIntermBlock *root)
18{
19 const TIntermSequence &sequence = *root->getSequence();
20 for (size_t index = 0; index < sequence.size(); ++index)
21 {
22 TIntermNode *node = sequence[index];
23 TIntermFunctionDefinition *nodeFunction = node->getAsFunctionDefinition();
24 if (nodeFunction != nullptr && nodeFunction->getFunction()->isMain())
25 {
26 return index;
27 }
28 }
29 return std::numeric_limits<size_t>::max();
30}
31
32TIntermFunctionDefinition *FindMain(TIntermBlock *root)
33{
34 for (TIntermNode *node : *root->getSequence())
35 {
36 TIntermFunctionDefinition *nodeFunction = node->getAsFunctionDefinition();
37 if (nodeFunction != nullptr && nodeFunction->getFunction()->isMain())
38 {
39 return nodeFunction;
40 }
41 }
42 return nullptr;
43}
44
45TIntermBlock *FindMainBody(TIntermBlock *root)
46{
47 TIntermFunctionDefinition *main = FindMain(root);
48 ASSERT(main != nullptr);
49 TIntermBlock *mainBody = main->getBody();
50 ASSERT(mainBody != nullptr);
51 return mainBody;
52}
53
54} // namespace sh
55