• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "runtime/include/class_helper.h"
17 
18 #include <algorithm>
19 
20 #include "libpandabase/mem/mem.h"
21 #include "libpandabase/utils/bit_utils.h"
22 #include "runtime/include/mem/panda_string.h"
23 
24 namespace panda {
25 
26 /* static */
GetDescriptor(const uint8_t * name,PandaString * storage)27 const uint8_t *ClassHelper::GetDescriptor(const uint8_t *name, PandaString *storage)
28 {
29     return GetArrayDescriptor(name, 0, storage);
30 }
31 
32 /* static */
GetArrayDescriptor(const uint8_t * component_name,size_t rank,PandaString * storage)33 const uint8_t *ClassHelper::GetArrayDescriptor(const uint8_t *component_name, size_t rank, PandaString *storage)
34 {
35     storage->clear();
36     storage->append(rank, '[');
37     storage->push_back('L');
38     storage->append(utf::Mutf8AsCString(component_name));
39     storage->push_back(';');
40     std::replace(storage->begin(), storage->end(), '.', '/');
41     return utf::CStringAsMutf8(storage->c_str());
42 }
43 
44 /* static */
GetPrimitiveTypeDescriptorChar(panda_file::Type::TypeId type_id)45 char ClassHelper::GetPrimitiveTypeDescriptorChar(panda_file::Type::TypeId type_id)
46 {
47     // static_cast isn't necessary in most implementations but may be by standard
48     return static_cast<char>(*GetPrimitiveTypeDescriptorStr(type_id));
49 }
50 
51 /* static */
GetPrimitiveTypeDescriptorStr(panda_file::Type::TypeId type_id)52 const uint8_t *ClassHelper::GetPrimitiveTypeDescriptorStr(panda_file::Type::TypeId type_id)
53 {
54     if (type_id == panda_file::Type::TypeId::REFERENCE) {
55         UNREACHABLE();
56         return nullptr;
57     }
58 
59     return utf::CStringAsMutf8(panda_file::Type::GetSignatureByTypeId(panda_file::Type(type_id)));
60 }
61 
62 /* static */
GetPrimitiveTypeStr(panda_file::Type::TypeId type_id)63 const char *ClassHelper::GetPrimitiveTypeStr(panda_file::Type::TypeId type_id)
64 {
65     switch (type_id) {
66         case panda_file::Type::TypeId::VOID:
67             return "void";
68         case panda_file::Type::TypeId::U1:
69             return "bool";
70         case panda_file::Type::TypeId::I8:
71             return "i8";
72         case panda_file::Type::TypeId::U8:
73             return "u8";
74         case panda_file::Type::TypeId::I16:
75             return "i16";
76         case panda_file::Type::TypeId::U16:
77             return "u16";
78         case panda_file::Type::TypeId::I32:
79             return "i32";
80         case panda_file::Type::TypeId::U32:
81             return "u32";
82         case panda_file::Type::TypeId::I64:
83             return "i64";
84         case panda_file::Type::TypeId::U64:
85             return "u64";
86         case panda_file::Type::TypeId::F32:
87             return "f32";
88         case panda_file::Type::TypeId::F64:
89             return "f64";
90         case panda_file::Type::TypeId::TAGGED:
91             return "any";
92         default:
93             UNREACHABLE();
94             break;
95     }
96     return nullptr;
97 }
98 
99 /* static */
GetPrimitiveDescriptor(panda_file::Type type,PandaString * storage)100 const uint8_t *ClassHelper::GetPrimitiveDescriptor(panda_file::Type type, PandaString *storage)
101 {
102     return GetPrimitiveArrayDescriptor(type, 0, storage);
103 }
104 
105 /* static */
GetPrimitiveArrayDescriptor(panda_file::Type type,size_t rank,PandaString * storage)106 const uint8_t *ClassHelper::GetPrimitiveArrayDescriptor(panda_file::Type type, size_t rank, PandaString *storage)
107 {
108     storage->clear();
109     storage->append(rank, '[');
110     storage->push_back(GetPrimitiveTypeDescriptorChar(type.GetId()));
111     return utf::CStringAsMutf8(storage->c_str());
112 }
113 
114 /* static */
GetTypeDescriptor(const PandaString & name,PandaString * storage)115 const uint8_t *ClassHelper::GetTypeDescriptor(const PandaString &name, PandaString *storage)
116 {
117     *storage = "L" + name + ";";
118     std::replace(storage->begin(), storage->end(), '.', '/');
119     return utf::CStringAsMutf8(storage->c_str());
120 }
121 
122 }  // namespace panda
123