• 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 #include "type_system.h"
17 
18 #include "type_sort.h"
19 #include "type_index.h"
20 #include "type_info.h"
21 
22 #include "type_set.h"
23 #include "type_param.h"
24 #include "type_type.h"
25 
26 namespace panda::verifier {
27 
28 template <>
operator |(const Type & t) const29 TypeSet Type::operator|(const Type &t) const
30 {
31     return TypeSet {*this, t};
32 }
33 
34 template <>
operator +() const35 TypeParam Type::operator+() const
36 {
37     return {TypeVariance::COVARIANT, *this};
38 }
39 
40 template <>
operator -() const41 TypeParam Type::operator-() const
42 {
43     return {TypeVariance::CONTRVARIANT, *this};
44 }
45 
46 template <>
operator ~() const47 TypeParam Type::operator~() const
48 {
49     return {TypeVariance::INVARIANT, *this};
50 }
51 
52 template <>
Params() const53 TypeParams Type::Params() const
54 {
55     return {GetTypeSystemKind(), GetThreadNum(), GetTypeSystem().GetParamsIdx(Idx_)};
56 }
57 
58 template <>
operator *(TypeVariance variance) const59 TypeParam Type::operator*(TypeVariance variance) const
60 {
61     return {variance, *this};
62 }
63 
operator ==(const Type & t) const64 bool Type::operator==(const Type &t) const
65 {
66     return t.Idx_ == Idx_;
67 }
68 
operator !=(const Type & t) const69 bool Type::operator!=(const Type &t) const
70 {
71     return t.Idx_ != Idx_;
72 }
73 
operator <<(const Type & t) const74 const Type &Type::operator<<(const Type &t) const
75 {
76     ASSERT(GetTypeSystemKind() == t.GetTypeSystemKind());
77     ASSERT(GetThreadNum() == t.GetThreadNum());
78     GetTypeSystem().Relate(Idx_, t.Idx_);
79     return t;
80 }
81 
operator <<(const TypeSet & s) const82 const TypeSet &Type::operator<<(const TypeSet &s) const
83 {
84     s.ForAll([&](const Type &t) {
85         operator<<(t);
86         return true;
87     });
88     return s;
89 }
90 
operator <=(const Type & rhs) const91 bool Type::operator<=(const Type &rhs) const
92 {
93     return GetTypeSystem().IsInDirectRelation(Idx_, rhs.Idx_);
94 }
95 
operator <=(const TypeParams & rhs) const96 bool Type::operator<=(const TypeParams &rhs) const
97 {
98     return TypeParams {GetTypeSystemKind(), GetThreadNum()} <= rhs;
99 }
100 
Sort() const101 SortIdx Type::Sort() const
102 {
103     return GetTypeSystem().GetSort(Idx_);
104 }
105 
Arity() const106 size_t Type::Arity() const
107 {
108     return GetTypeSystem().GetArity(Idx_);
109 }
110 
ParamsSize() const111 size_t Type::ParamsSize() const
112 {
113     return GetTypeSystem().GetParamsIdx(Idx_).size();
114 }
115 
GetTypeSystem() const116 TypeSystem &Type::GetTypeSystem() const
117 {
118     return TypeSystems::Get(GetTypeSystemKind(), GetThreadNum());
119 }
120 
GetTypeSystemKind() const121 TypeSystemKind Type::GetTypeSystemKind() const
122 {
123     return Idx_.GetTag<0>();
124 }
125 
GetThreadNum() const126 ThreadNum Type::GetThreadNum() const
127 {
128     return Idx_.GetTag<1>();
129 }
130 
IsValid() const131 bool Type::IsValid() const
132 {
133     return Idx_.IsValid();
134 }
135 
IsTop() const136 bool Type::IsTop() const
137 {
138     return GetTypeSystem().Top() == *this;
139 }
140 
IsBot() const141 bool Type::IsBot() const
142 {
143     return GetTypeSystem().Bot() == *this;
144 }
145 
operator &(const Type & rhs) const146 TypeSet Type::operator&(const Type &rhs) const
147 {
148     ASSERT(GetTypeSystemKind() == rhs.GetTypeSystemKind());
149     const TypeSystem &type_system = GetTypeSystem();
150     return TypeSet {GetTypeSystemKind(), GetThreadNum(),
151                     type_system.GetDirectlyRelated(Idx_) & type_system.GetDirectlyRelated(rhs.Idx_)};
152 }
153 
operator &(const TypeSet & rhs) const154 TypeSet Type::operator&(const TypeSet &rhs) const
155 {
156     return rhs & *this;
157 }
158 
Number() const159 TypeNum Type::Number() const
160 {
161     return Idx_;
162 }
163 
164 }  // namespace panda::verifier
165