• 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 PANDA_TYPE_SYSTEM_HPP
17 #define PANDA_TYPE_SYSTEM_HPP
18 
19 #include "macros.h"
20 #include "runtime/include/class.h"
21 #include "runtime/include/language_context.h"
22 #include "runtime/include/method.h"
23 #include "runtime/include/mem/panda_containers.h"
24 
25 #include "verification/type/type_type.h"
26 #include "verification/value/variables.h"
27 
28 #include <memory>
29 #include <variant>
30 #include <functional>
31 #include <algorithm>
32 
33 namespace ark::verifier::plugin {
34 class Plugin;
35 }  // namespace ark::verifier::plugin
36 
37 namespace ark::verifier {
38 class VerifierService;
39 
40 /*
41 Design decision:
42 1. There are special initial and final types, named as Bot and Top, and all types are implicitly related
43    as Bot <: type <: Top
44 */
45 
46 class TypeSystem {
47 public:
48     // NOTE(vdyadov): change Id to hash from filename_id and entity id
49     using TypeId = panda_file::Type::TypeId;
50 
51     explicit TypeSystem(VerifierService *service, panda_file::SourceLang lang = panda_file::SourceLang::PANDA_ASSEMBLY);
52 
53     NO_COPY_SEMANTIC(TypeSystem);
54     DEFAULT_MOVE_SEMANTIC(TypeSystem);
55     ~TypeSystem() = default;
56 
SetSupertypeOfArray(Type supertype)57     void SetSupertypeOfArray(Type supertype)
58     {
59         supertypeOfArray_ = supertype;
60     }
61 
SupertypeOfArray()62     Type SupertypeOfArray() const
63     {
64         return supertypeOfArray_;
65     }
Object()66     Type Object() const
67     {
68         return object_;
69     }
StringClass()70     Type StringClass() const
71     {
72         return string_;
73     }
ClassClass()74     Type ClassClass() const
75     {
76         return class_;
77     }
Throwable()78     Type Throwable() const
79     {
80         return throwable_;
81     }
82 
NewVar()83     Variables::Var NewVar()
84     {
85         return variables_.NewVar();
86     }
87 
88     Type NormalizedTypeOf(Type type);
89     MethodSignature const *GetMethodSignature(Method const *method);
90 
91     PandaUnorderedSet<Type> const *SupertypesOfClass(Class const *klass);
92 
93     void DisplayTypeSystem(std::function<void(PandaString const &)> const &handler);
94 
95     /* Make TypeSystem remember the class for dumping */
96     void MentionClass(Class const *klass);
97 
98     Type BootDescriptorToType(uint8_t const *descr);
99 
ResetTypeSpans()100     void ResetTypeSpans()
101     {
102         typeSpans_.clear();
103     }
104 
GetTypeSpan(size_t start,size_t sz)105     Span<Type const> GetTypeSpan(size_t start, size_t sz) const
106     {
107         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
108         return {typeSpans_.data() + start, sz};
109     }
110 
GetNewTypeSpan(size_t sz)111     Span<Type> GetNewTypeSpan(size_t sz)
112     {
113         size_t start = typeSpans_.size();
114         typeSpans_.resize(start + sz);
115         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
116         return {typeSpans_.data() + start, sz};
117     }
118 
GetSpanIndex(Span<Type> span)119     size_t GetSpanIndex(Span<Type> span)
120     {
121         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
122         return span.begin() - typeSpans_.data();
123     }
124 
125 private:
126     VerifierService *service_;
127     plugin::Plugin const *plugin_;
128     LanguageContext langCtx_;
129     ClassLinkerContext *bootLinkerCtx_;
130 
131     PandaUnorderedMap<Type, Type> normalizedTypeOf_;
132     PandaUnorderedMap<Method::UniqId, Method const *> methodOfId_;
133     PandaUnorderedMap<Method::UniqId, MethodSignature> signatureOfMethod_;
134     PandaUnorderedMap<Class const *, PandaUnorderedSet<Type>> supertypesCache_;
135     PandaUnorderedSet<Class const *> knownClasses_;
136 
137     // Storage for members of intersection and union types.
138     PandaVector<Type> typeSpans_;
139 
140     Type supertypeOfArray_;
141     Type object_;
142     Type string_;
143     Type class_;
144     Type throwable_;
145     Variables variables_;
146 
147     void ExtendBySupers(PandaUnorderedSet<Type> *set, Class const *klass);
148 
149     void DisplayClasses(std::function<void(PandaString const &)> const &handler) const;
150     void DisplayMethods(std::function<void(PandaString const &, PandaString const &)> const &handler) const;
151     void DisplaySubtyping(std::function<void(PandaString const &, PandaString const &)> const &handler);
152 };
153 
154 }  // namespace ark::verifier
155 
156 #endif  // !PANDA_TYPE_SYSTEM_HPP
157