1 /*
2 * Copyright (c) 2021 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 #include "type_type.h"
16
17 #include "type_system.h"
18
19 #include "type_sort.h"
20 #include "type_index.h"
21 #include "type_info.h"
22
23 #include "type_set.h"
24 #include "type_param.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 {*this, TypeVariance::COVARIANT};
38 }
39
40 template <>
operator -() const41 TypeParam Type::operator-() const
42 {
43 return {*this, TypeVariance::CONTRVARIANT};
44 }
45
46 template <>
operator ~() const47 TypeParam Type::operator~() const
48 {
49 return {*this, TypeVariance::INVARIANT};
50 }
51
52 template <>
Params() const53 TypeParams Type::Params() const
54 {
55 return TypeParams {GetTypeSystemKind(), GetTypeSystem().GetParamsIdx(Idx_)};
56 }
57
58 template <>
operator *(TypeVariance variance) const59 TypeParam Type::operator*(TypeVariance variance) const
60 {
61 return {*this, variance};
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 GetTypeSystem().Relate(Idx_, t.Idx_);
78 return t;
79 }
80
operator <<(const TypeSet & s) const81 const TypeSet &Type::operator<<(const TypeSet &s) const
82 {
83 s.ForAll([&](const Type &t) {
84 operator<<(t);
85 return true;
86 });
87 return s;
88 }
89
operator <=(const Type & rhs) const90 bool Type::operator<=(const Type &rhs) const
91 {
92 return GetTypeSystem().IsInDirectRelation(Idx_, rhs.Idx_);
93 }
94
operator <=(const TypeParams & rhs) const95 bool Type::operator<=(const TypeParams &rhs) const
96 {
97 return TypeParams {GetTypeSystemKind()} <= rhs;
98 }
99
Sort() const100 SortIdx Type::Sort() const
101 {
102 return GetTypeSystem().GetSort(Idx_);
103 }
104
Arity() const105 size_t Type::Arity() const
106 {
107 return GetTypeSystem().GetArity(Idx_);
108 }
109
ParamsSize() const110 size_t Type::ParamsSize() const
111 {
112 return GetTypeSystem().GetParamsIdx(Idx_).size();
113 }
114
GetTypeSystem() const115 TypeSystem &Type::GetTypeSystem() const
116 {
117 return TypeSystems::Get(GetTypeSystemKind());
118 }
119
GetTypeSystemKind() const120 TypeSystemKind Type::GetTypeSystemKind() const
121 {
122 return Idx_.GetTag();
123 }
124
IsValid() const125 bool Type::IsValid() const
126 {
127 return Idx_.IsValid();
128 }
129
IsTop() const130 bool Type::IsTop() const
131 {
132 return GetTypeSystem().Top() == *this;
133 }
134
IsBot() const135 bool Type::IsBot() const
136 {
137 return GetTypeSystem().Bot() == *this;
138 }
139
operator &(const Type & rhs) const140 TypeSet Type::operator&(const Type &rhs) const
141 {
142 ASSERT(GetTypeSystemKind() == rhs.GetTypeSystemKind());
143 const TypeSystem &type_system = GetTypeSystem();
144 return TypeSet {GetTypeSystemKind(),
145 type_system.GetDirectlyRelated(Idx_) & type_system.GetDirectlyRelated(rhs.Idx_)};
146 }
147
operator &(const TypeSet & rhs) const148 TypeSet Type::operator&(const TypeSet &rhs) const
149 {
150 return rhs & *this;
151 }
152
Index() const153 TypeIdx Type::Index() const
154 {
155 return Idx_;
156 }
157
158 } // namespace panda::verifier
159