1 /**
2  * Copyright (c) 2021-2024 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 PANDA_VERIFIER_VARIABLES_HPP
17 #define PANDA_VERIFIER_VARIABLES_HPP
18 
19 #include "verification/util/lazy.h"
20 
21 #include "runtime/include/mem/panda_containers.h"
22 #include "runtime/include/mem/panda_string.h"
23 
24 #include "util/str.h"
25 #include "verification/util/obj_pool.h"
26 
27 #include <memory>
28 
29 namespace ark::verifier {
30 
31 class Variables {
32     struct Wrapper {
Initark::verifier::Variables::Wrapper33         static void Init(Wrapper &v, size_t idx)
34         {
35             v.idx = idx;
36         }
37         size_t idx;
38     };
39 
40     using PoolType = ObjPool<Wrapper, PandaVector, void (*)(Wrapper &, size_t)>;
41 
42 public:
43     class Var {
44     public:
45         Var() = default;
46         // NOLINTNEXTLINE(google-explicit-constructor)
Var(PoolType::Accessor &&a)47         Var(PoolType::Accessor &&a) : accessor_ {std::move(a)} {}
48         Var(const Var &) = default;
49         Var(Var &&) = default;
50         Var &operator=(const Var &a) = default;
51         Var &operator=(Var &&a) = default;
52         ~Var() = default;
53 
operator ==(const Var &v)54         bool operator==(const Var &v)
55         {
56             return (*accessor_).idx == (*v.accessor_).idx;
57         }
58 
operator !=(const Var &v)59         bool operator!=(const Var &v)
60         {
61             return (*accessor_).idx != (*v.accessor_).idx;
62         }
63 
Image(const PandaString &prefix = �) const64         PandaString Image(const PandaString &prefix = "V") const
65         {
66             return prefix + NumToStr((*accessor_).idx);
67         }
68 
69     private:
70         PoolType::Accessor accessor_;
71     };
72 
73     using VarIdx = size_t;
74 
75     Variables() = default;
76     NO_COPY_SEMANTIC(Variables);
77     DEFAULT_MOVE_SEMANTIC(Variables);
78     ~Variables() = default;
79 
NewVar()80     Var NewVar()
81     {
82         return {varPool_.New()};
83     }
84 
AmountOfUsedVars() const85     size_t AmountOfUsedVars() const
86     {
87         return varPool_.Count() - varPool_.FreeCount();
88     }
89 
AllVariables()90     auto AllVariables()
91     {
92         return [fetcher = varPool_.AllObjects()]() mutable -> std::optional<Var> {
93             if (auto v = fetcher()) {
94                 return {Var {std::move(*v)}};
95             }
96             return std::nullopt;
97         };
98     }
99 
100 private:
101     PoolType varPool_ {Wrapper::Init};
102 };
103 
104 }  // namespace ark::verifier
105 
106 #endif  // !PANDA_VERIFIER_VARIABLES_HPP
107