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