xref: /aosp_15_r20/external/angle/src/compiler/translator/IsASTDepthBelowLimit.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2017 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker 
7*8975f5c5SAndroid Build Coastguard Worker #include "compiler/translator/IsASTDepthBelowLimit.h"
8*8975f5c5SAndroid Build Coastguard Worker 
9*8975f5c5SAndroid Build Coastguard Worker #include "compiler/translator/tree_util/IntermTraverse.h"
10*8975f5c5SAndroid Build Coastguard Worker 
11*8975f5c5SAndroid Build Coastguard Worker namespace sh
12*8975f5c5SAndroid Build Coastguard Worker {
13*8975f5c5SAndroid Build Coastguard Worker 
14*8975f5c5SAndroid Build Coastguard Worker namespace
15*8975f5c5SAndroid Build Coastguard Worker {
16*8975f5c5SAndroid Build Coastguard Worker 
17*8975f5c5SAndroid Build Coastguard Worker // Traverse the tree and compute max depth. Takes a maximum depth limit to prevent stack overflow.
18*8975f5c5SAndroid Build Coastguard Worker class MaxDepthTraverser : public TIntermTraverser
19*8975f5c5SAndroid Build Coastguard Worker {
20*8975f5c5SAndroid Build Coastguard Worker   public:
MaxDepthTraverser(int depthLimit)21*8975f5c5SAndroid Build Coastguard Worker     MaxDepthTraverser(int depthLimit) : TIntermTraverser(true, false, false, nullptr)
22*8975f5c5SAndroid Build Coastguard Worker     {
23*8975f5c5SAndroid Build Coastguard Worker         setMaxAllowedDepth(depthLimit);
24*8975f5c5SAndroid Build Coastguard Worker     }
25*8975f5c5SAndroid Build Coastguard Worker };
26*8975f5c5SAndroid Build Coastguard Worker 
27*8975f5c5SAndroid Build Coastguard Worker }  // anonymous namespace
28*8975f5c5SAndroid Build Coastguard Worker 
IsASTDepthBelowLimit(TIntermNode * root,int maxDepth)29*8975f5c5SAndroid Build Coastguard Worker bool IsASTDepthBelowLimit(TIntermNode *root, int maxDepth)
30*8975f5c5SAndroid Build Coastguard Worker {
31*8975f5c5SAndroid Build Coastguard Worker     MaxDepthTraverser traverser(maxDepth + 1);
32*8975f5c5SAndroid Build Coastguard Worker     root->traverse(&traverser);
33*8975f5c5SAndroid Build Coastguard Worker 
34*8975f5c5SAndroid Build Coastguard Worker     return traverser.getMaxDepth() <= maxDepth;
35*8975f5c5SAndroid Build Coastguard Worker }
36*8975f5c5SAndroid Build Coastguard Worker 
37*8975f5c5SAndroid Build Coastguard Worker }  // namespace sh
38