• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
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 
16 #ifndef RECURSIVE_GUARD_H
17 #define RECURSIVE_GUARD_H
18 
19 namespace ark::es2panda::parser {
20 
21 constexpr unsigned int MAX_RECURSION_DEPTH = 1024;
22 
23 struct RecursiveContext {
24     unsigned depth = 0;
25 };
26 
27 class TrackRecursive {
28 public:
TrackRecursive(RecursiveContext & recursivecontext)29     explicit TrackRecursive(RecursiveContext &recursivecontext) : recursivecontext_(recursivecontext)
30     {
31         ++recursivecontext_.depth;
32         valid_ = recursivecontext_.depth <= MAX_RECURSION_DEPTH;
33     };
34 
~TrackRecursive()35     ~TrackRecursive()
36     {
37         --recursivecontext_.depth;
38     }
39 
40     TrackRecursive(const TrackRecursive &) = delete;
41     TrackRecursive(TrackRecursive &&) = delete;
42     TrackRecursive &operator=(const TrackRecursive &) = delete;
43     TrackRecursive &operator=(TrackRecursive &&) = delete;
44 
45     explicit operator bool() const
46     {
47         return valid_;
48     }
49 
50 private:
51     RecursiveContext &recursivecontext_;
52     bool valid_ = true;
53 };
54 
55 }  // namespace ark::es2panda::parser
56 
57 #endif  // UTIL_GUARD_H
58