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_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 panda::verifier::plugin { 34 class Plugin; 35 } // namespace panda::verifier::plugin 36 37 namespace panda::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 } ClassClass()70 Type ClassClass() const 71 { 72 return class_; 73 } Throwable()74 Type Throwable() const 75 { 76 return throwable_; 77 } 78 NewVar()79 Variables::Var NewVar() 80 { 81 return variables_.NewVar(); 82 } 83 84 Type NormalizedTypeOf(Type type); 85 MethodSignature const *GetMethodSignature(Method const *method); 86 87 PandaUnorderedSet<Type> const *SupertypesOfClass(Class const *klass); 88 89 void DisplayTypeSystem(std::function<void(PandaString const &)> const &handler); 90 91 /* Make TypeSystem remember the class for dumping */ 92 void MentionClass(Class const *klass); 93 94 Type DescriptorToType(uint8_t const *descr); 95 ResetTypeSpans()96 void ResetTypeSpans() 97 { 98 typeSpans_.clear(); 99 } 100 GetTypeSpan(size_t start,size_t sz)101 Span<Type const> GetTypeSpan(size_t start, size_t sz) const 102 { 103 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 104 return {typeSpans_.data() + start, sz}; 105 } 106 GetNewTypeSpan(size_t sz)107 Span<Type> GetNewTypeSpan(size_t sz) 108 { 109 size_t start = typeSpans_.size(); 110 typeSpans_.resize(start + sz); 111 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 112 return {typeSpans_.data() + start, sz}; 113 } 114 GetSpanIndex(Span<Type> span)115 size_t GetSpanIndex(Span<Type> span) 116 { 117 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 118 return span.begin() - typeSpans_.data(); 119 } 120 121 Class const *DescriptorToClass(uint8_t const *descr); 122 123 private: 124 VerifierService *service_; 125 plugin::Plugin const *plugin_; 126 LanguageContext langCtx_; 127 ClassLinkerContext *linkerCtx_; 128 129 PandaUnorderedMap<Type, Type> normalizedTypeOf_; 130 PandaUnorderedMap<Method::UniqId, Method const *> methodOfId_; 131 PandaUnorderedMap<Method::UniqId, MethodSignature> signatureOfMethod_; 132 PandaUnorderedMap<Class const *, PandaUnorderedSet<Type>> supertypesCache_; 133 PandaUnorderedSet<Class const *> knownClasses_; 134 135 // Storage for members of intersection and union types. 136 PandaVector<Type> typeSpans_; 137 138 Type supertypeOfArray_; 139 Type object_; 140 Type class_; 141 Type throwable_; 142 Variables variables_; 143 144 void ExtendBySupers(PandaUnorderedSet<Type> *set, Class const *klass); 145 146 void DisplayClasses(std::function<void(PandaString const &)> const &handler) const; 147 void DisplayMethods(std::function<void(PandaString const &, PandaString const &)> const &handler) const; 148 void DisplaySubtyping(std::function<void(PandaString const &, PandaString const &)> const &handler); 149 }; 150 151 } // namespace panda::verifier 152 153 #endif // !PANDA_TYPE_SYSTEM_HPP 154