1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/angle/src/compiler/DetectCallDepth.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,80 @@ 1.4 +// 1.5 +// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved. 1.6 +// Use of this source code is governed by a BSD-style license that can be 1.7 +// found in the LICENSE file. 1.8 +// 1.9 + 1.10 +#ifndef COMPILER_DETECT_RECURSION_H_ 1.11 +#define COMPILER_DETECT_RECURSION_H_ 1.12 + 1.13 +#include "GLSLANG/ShaderLang.h" 1.14 + 1.15 +#include <limits.h> 1.16 +#include "compiler/intermediate.h" 1.17 +#include "compiler/VariableInfo.h" 1.18 + 1.19 +class TInfoSink; 1.20 + 1.21 +// Traverses intermediate tree to detect function recursion. 1.22 +class DetectCallDepth : public TIntermTraverser { 1.23 +public: 1.24 + enum ErrorCode { 1.25 + kErrorMissingMain, 1.26 + kErrorRecursion, 1.27 + kErrorMaxDepthExceeded, 1.28 + kErrorNone 1.29 + }; 1.30 + 1.31 + DetectCallDepth(TInfoSink& infoSync, bool limitCallStackDepth, int maxCallStackDepth); 1.32 + ~DetectCallDepth(); 1.33 + 1.34 + virtual bool visitAggregate(Visit, TIntermAggregate*); 1.35 + 1.36 + bool checkExceedsMaxDepth(int depth); 1.37 + 1.38 + ErrorCode detectCallDepth(); 1.39 + 1.40 +private: 1.41 + class FunctionNode { 1.42 + public: 1.43 + static const int kInfiniteCallDepth = INT_MAX; 1.44 + 1.45 + FunctionNode(const TString& fname); 1.46 + 1.47 + const TString& getName() const; 1.48 + 1.49 + // If a function is already in the callee list, this becomes a no-op. 1.50 + void addCallee(FunctionNode* callee); 1.51 + 1.52 + // Returns kInifinityCallDepth if recursive function calls are detected. 1.53 + int detectCallDepth(DetectCallDepth* detectCallDepth, int depth); 1.54 + 1.55 + // Reset state. 1.56 + void reset(); 1.57 + 1.58 + private: 1.59 + // mangled function name is unique. 1.60 + TString name; 1.61 + 1.62 + // functions that are directly called by this function. 1.63 + TVector<FunctionNode*> callees; 1.64 + 1.65 + Visit visit; 1.66 + }; 1.67 + 1.68 + ErrorCode detectCallDepthForFunction(FunctionNode* func); 1.69 + FunctionNode* findFunctionByName(const TString& name); 1.70 + void resetFunctionNodes(); 1.71 + 1.72 + TInfoSink& getInfoSink() { return infoSink; } 1.73 + 1.74 + TVector<FunctionNode*> functions; 1.75 + FunctionNode* currentFunction; 1.76 + TInfoSink& infoSink; 1.77 + int maxDepth; 1.78 + 1.79 + DetectCallDepth(const DetectCallDepth&); 1.80 + void operator=(const DetectCallDepth&); 1.81 +}; 1.82 + 1.83 +#endif // COMPILER_DETECT_RECURSION_H_