1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef COMPILER_ANALYZE_CALL_DEPTH_H_
16 #define COMPILER_ANALYZE_CALL_DEPTH_H_
17 
18 #include "intermediate.h"
19 
20 #include <set>
21 #include <limits.h>
22 
23 // Traverses intermediate tree to analyze call depth or detect function recursion
24 class AnalyzeCallDepth : public TIntermTraverser
25 {
26 public:
27 	AnalyzeCallDepth(TIntermNode *root);
28 	~AnalyzeCallDepth();
29 
30 	virtual bool visitSwitch(Visit, TIntermSwitch*);
31 	virtual bool visitAggregate(Visit, TIntermAggregate*);
32 
33 	unsigned int analyzeCallDepth();
34 
35 private:
36 	class FunctionNode
37 	{
38 	public:
39 		FunctionNode(TIntermAggregate *node);
40 
41 		const TString &getName() const;
42 		void addCallee(FunctionNode *callee);
43 		unsigned int analyzeCallDepth(AnalyzeCallDepth *analyzeCallDepth);
44 		unsigned int getLastDepth() const;
45 
46 		void removeIfUnreachable();
47 
48 	private:
49 		TIntermAggregate *const node;
50 		TVector<FunctionNode*> callees;
51 
52 		Visit visit;
53 		unsigned int callDepth;
54 	};
55 
56 	FunctionNode *findFunctionByName(const TString &name);
57 
58 	std::vector<FunctionNode*> functions;
59 	typedef std::set<FunctionNode*> FunctionSet;
60 	FunctionSet globalFunctionCalls;
61 	FunctionNode *currentFunction;
62 };
63 
64 #endif  // COMPILER_ANALYZE_CALL_DEPTH_H_
65