• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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_TYPE_TYPE_HPP__
17 #define _PANDA_TYPE_TYPE_HPP__
18 
19 #include "type_sort.h"
20 #include "type_index.h"
21 #include "type_info.h"
22 #include "type_tags.h"
23 
24 namespace panda::verifier {
25 class TypeSystem;
26 class TypeSet;
27 class TypeParam;
28 class TypeParams;
29 
30 class Type {
31     using TypeIndex = TaggedIndex<TypeSystemKindTag, ThreadNumTag, TypeNum>;
32 
33 public:
34     Type() = default;
35     Type(const Type &) = default;
36     Type(Type &&) = default;
37     Type &operator=(const Type &) = default;
38     Type &operator=(Type &&) = default;
39     ~Type() = default;
40 
41     bool operator==(const Type &t) const;
42 
43     bool operator!=(const Type &t) const;
44 
45     const Type &operator<<(const Type &t) const;
46 
47     // def subtyping a << (b | c) << d
48     const TypeSet &operator<<(const TypeSet &s) const;
49 
50     // a workaround for absence of mutualy-recursive classes in C++
51     template <typename...>
52     TypeSet operator|(const Type &t) const;
53 
54     template <typename...>
55     TypeParam operator+() const;
56 
57     template <typename...>
58     TypeParam operator-() const;
59 
60     template <typename...>
61     TypeParam operator~() const;
62 
63     // subtyping relation: <=
64     bool operator<=(const Type &rhs) const;
65     bool operator<=(const TypeParams &rhs) const;
66 
67     SortIdx Sort() const;
68 
69     size_t Arity() const;
70 
71     template <typename...>
72     TypeParams Params() const;
73 
74     size_t ParamsSize() const;
75 
76     TypeSystem &GetTypeSystem() const;
77 
78     TypeSystemKind GetTypeSystemKind() const;
79 
80     ThreadNum GetThreadNum() const;
81 
82     bool IsValid() const;
83 
84     bool IsTop() const;
85 
86     bool IsBot() const;
87 
88     TypeSet operator&(const Type &rhs) const;
89 
90     TypeSet operator&(const TypeSet &rhs) const;
91 
92     template <typename...>
93     TypeParam operator*(TypeVariance variance) const;
94 
95     // TODO(vdyadov): implement
96 
97     template <typename Handler>
98     void ForAllParams(Handler &&handler) const;
99 
100     template <typename Handler>
101     void ForAllSupertypes(Handler &&handler) const;
102 
103     template <typename Handler>
104     void ForAllSupertypesOfSort(SortIdx sort, Handler &&handler) const;
105 
106     template <typename Handler>
107     void ForAllSubtypes(Handler &&handler) const;
108 
109     template <typename Handler>
110     void ForAllSubtypesOfSort(SortIdx sort, Handler &&handler) const;
111 
112 private:
Type(TypeSystemKind kind,ThreadNum threadnum,TypeNum num)113     Type(TypeSystemKind kind, ThreadNum threadnum, TypeNum num) : Idx_ {kind, threadnum, num} {};
114 
115     TypeNum Number() const;
116 
117     TypeIndex Idx_;
118 
119     friend class TypeSystem;
120     friend class TypeParam;
121     friend class ParametricType;
122     friend class PandaTypes;
123     friend class TypeSet;
124 
125     friend class TypeImage;
126 
127     friend struct std::hash<Type>;
128 };
129 }  // namespace panda::verifier
130 
131 namespace std {
132 template <>
133 struct hash<panda::verifier::Type> {
134     size_t operator()(const panda::verifier::Type &type) const
135     {
136         return static_cast<size_t>(type.Number());
137     }
138 };
139 }  // namespace std
140 
141 #endif  // !_PANDA_TYPE_TYPE_HPP__
142