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 ASSEMBLER_ASSEMBLY_TYPE_H 17 #define ASSEMBLER_ASSEMBLY_TYPE_H 18 19 #include "define.h" 20 #include "file_items.h" 21 #include "isa.h" 22 23 namespace panda::pandasm { 24 25 class Type { 26 public: 27 enum VerificationType { 28 TYPE_ID_OBJECT, 29 TYPE_ID_ARRAY, 30 TYPE_ID_ANY_OBJECT, 31 }; 32 33 Type() = default; 34 DEFAULT_MOVE_SEMANTIC(Type); 35 DEFAULT_COPY_SEMANTIC(Type); 36 ~Type() = default; 37 38 Type(std::string_view component_name, size_t rank, bool ignore_primitive = false) component_name_(component_name)39 : component_name_(component_name), rank_(rank) 40 { 41 name_ = GetName(component_name_, rank_); 42 type_id_ = GetId(name_, ignore_primitive); 43 } 44 Type(const Type & component_type,size_t rank)45 Type(const Type &component_type, size_t rank) 46 : Type(component_type.GetComponentName(), component_type.GetRank() + rank) 47 { 48 } 49 50 std::string GetDescriptor(bool ignore_primitive = false) const; 51 GetName()52 std::string GetName() const 53 { 54 return name_; 55 } 56 GetPandasmName()57 std::string GetPandasmName() const 58 { 59 std::string name_pa {name_}; 60 std::replace(name_pa.begin(), name_pa.end(), '/', '.'); 61 return name_pa; 62 } 63 GetComponentName()64 std::string GetComponentName() const 65 { 66 return component_name_; 67 } 68 GetRank()69 size_t GetRank() const 70 { 71 return rank_; 72 } 73 GetComponentType()74 Type GetComponentType() const 75 { 76 return Type(component_name_, rank_ > 0 ? rank_ - 1 : 0); 77 } 78 GetId()79 panda_file::Type::TypeId GetId() const 80 { 81 return type_id_; 82 } 83 IsArrayContainsPrimTypes()84 bool IsArrayContainsPrimTypes() const 85 { 86 auto elem = GetId(component_name_); 87 return elem != panda_file::Type::TypeId::REFERENCE; 88 } 89 IsValid()90 bool IsValid() const 91 { 92 return !component_name_.empty(); 93 } 94 IsArray()95 bool IsArray() const 96 { 97 return rank_ > 0; 98 } 99 IsObject()100 bool IsObject() const 101 { 102 return type_id_ == panda_file::Type::TypeId::REFERENCE; 103 } 104 IsTagged()105 bool IsTagged() const 106 { 107 return type_id_ == panda_file::Type::TypeId::TAGGED; 108 } 109 IsIntegral()110 bool IsIntegral() const 111 { 112 return type_id_ == panda_file::Type::TypeId::U1 || type_id_ == panda_file::Type::TypeId::U8 || 113 type_id_ == panda_file::Type::TypeId::I8 || type_id_ == panda_file::Type::TypeId::U16 || 114 type_id_ == panda_file::Type::TypeId::I16 || type_id_ == panda_file::Type::TypeId::U32 || 115 type_id_ == panda_file::Type::TypeId::I32 || type_id_ == panda_file::Type::TypeId::U64 || 116 type_id_ == panda_file::Type::TypeId::I64; 117 } 118 FitsInto32()119 bool FitsInto32() const 120 { 121 return type_id_ == panda_file::Type::TypeId::U1 || type_id_ == panda_file::Type::TypeId::U8 || 122 type_id_ == panda_file::Type::TypeId::I8 || type_id_ == panda_file::Type::TypeId::U16 || 123 type_id_ == panda_file::Type::TypeId::I16 || type_id_ == panda_file::Type::TypeId::U32 || 124 type_id_ == panda_file::Type::TypeId::I32; 125 } 126 IsFloat32()127 bool IsFloat32() const 128 { 129 return type_id_ == panda_file::Type::TypeId::F32; 130 } 131 IsFloat64()132 bool IsFloat64() const 133 { 134 return type_id_ == panda_file::Type::TypeId::F64; 135 } 136 IsPrim32()137 bool IsPrim32() const 138 { 139 return (IsIntegral() && FitsInto32()) || IsFloat32(); 140 } 141 IsPrim64()142 bool IsPrim64() const 143 { 144 return (IsIntegral() && !FitsInto32()) || IsFloat64(); 145 } 146 IsPrimitive()147 bool IsPrimitive() const 148 { 149 return IsPrim64() || IsPrim32(); 150 } 151 IsVoid()152 bool IsVoid() const 153 { 154 return type_id_ == panda_file::Type::TypeId::VOID; 155 } 156 157 static panda_file::Type::TypeId GetId(std::string_view name, bool ignore_primitive = false); 158 159 bool operator==(const Type &type) const 160 { 161 return name_ == type.name_; 162 } 163 164 static Type FromDescriptor(std::string_view descriptor); 165 166 static Type FromName(std::string_view name, bool ignore_primitive = false); 167 168 static bool IsPandaPrimitiveType(const std::string &name); 169 static bool IsStringType(const std::string &name, panda::panda_file::SourceLang lang); 170 171 private: 172 static std::string GetName(std::string_view component_name, size_t rank); 173 174 std::string component_name_; 175 size_t rank_ {0}; 176 std::string name_; 177 panda_file::Type::TypeId type_id_ {panda_file::Type::TypeId::VOID}; 178 }; 179 180 } // namespace panda::pandasm 181 182 namespace std { 183 184 template <> 185 class hash<panda::pandasm::Type> { 186 public: operator()187 size_t operator()(const panda::pandasm::Type &type) const 188 { 189 return std::hash<std::string>()(type.GetName()); 190 } 191 }; 192 193 } // namespace std 194 195 #endif // ASSEMBLER_ASSEMBLY_TYPE_H 196