1 /*
2 * Copyright (c) 2021 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_RUNTIME_INCLUDE_CLASS_HELPER_H_
17 #define PANDA_RUNTIME_INCLUDE_CLASS_HELPER_H_
18
19 #include <cstdint>
20
21 #include "libpandabase/utils/span.h"
22 #include "libpandafile/file_items.h"
23 #include "runtime/include/mem/panda_string.h"
24 #include "runtime/object_header_config.h"
25
26 namespace panda {
27
28 // Small helper
29 template <class Config>
30 class ClassConfig {
31 public:
32 using classWordSize = typename Config::Size;
33 };
34
35 class ClassHelper : private ClassConfig<MemoryModelConfig> {
36 public:
37 using classWordSize = typename ClassConfig::classWordSize; // To be visible outside
38
39 static constexpr size_t OBJECT_POINTER_SIZE = sizeof(classWordSize);
40 // In general for any T: sizeof(T*) != OBJECT_POINTER_SIZE
41 static constexpr size_t POINTER_SIZE = sizeof(uintptr_t);
42
43 static size_t ComputeClassSize(size_t vtable_size, size_t imt_size, size_t num_8bit_sfields,
44 size_t num_16bit_sfields, size_t num_32bit_sfields, size_t num_64bit_sfields,
45 size_t num_ref_sfields, size_t num_tagged_sfields);
46
47 static const uint8_t *GetDescriptor(const uint8_t *name, PandaString *storage);
48
49 static const uint8_t *GetTypeDescriptor(const PandaString &name, PandaString *storage);
50
51 static const uint8_t *GetArrayDescriptor(const uint8_t *component_name, size_t rank, PandaString *storage);
52
53 static char GetPrimitiveTypeDescriptorChar(panda_file::Type::TypeId type_id);
54
55 static const uint8_t *GetPrimitiveTypeDescriptorStr(panda_file::Type::TypeId type_id);
56
57 static const char *GetPrimitiveTypeStr(panda_file::Type::TypeId type_id);
58
59 static const uint8_t *GetPrimitiveDescriptor(panda_file::Type type, PandaString *storage);
60
61 static const uint8_t *GetPrimitiveArrayDescriptor(panda_file::Type type, size_t rank, PandaString *storage);
62
63 template <typename Str = std::string>
64 static Str GetName(const uint8_t *descriptor);
65
IsArrayDescriptor(const uint8_t * descriptor)66 static bool IsArrayDescriptor(const uint8_t *descriptor)
67 {
68 Span<const uint8_t> sp(descriptor, 1);
69 return sp[0] == '[';
70 }
71
GetComponentDescriptor(const uint8_t * descriptor)72 static const uint8_t *GetComponentDescriptor(const uint8_t *descriptor)
73 {
74 ASSERT(IsArrayDescriptor(descriptor));
75 Span<const uint8_t> sp(descriptor, 1);
76 return sp.cend();
77 }
78
GetDimensionality(const uint8_t * descriptor)79 static size_t GetDimensionality(const uint8_t *descriptor)
80 {
81 ASSERT(IsArrayDescriptor(descriptor));
82 size_t dim = 0;
83 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
84 while (*descriptor++ == '[') {
85 ++dim;
86 }
87 return dim;
88 }
89 };
90
91 // Str is std::string or PandaString
92 /* static */
93 template <typename Str>
GetName(const uint8_t * descriptor)94 Str ClassHelper::GetName(const uint8_t *descriptor)
95 {
96 switch (*descriptor) {
97 case 'V':
98 return "void";
99 case 'Z':
100 return "u1";
101 case 'B':
102 return "i8";
103 case 'H':
104 return "u8";
105 case 'S':
106 return "i16";
107 case 'C':
108 return "u16";
109 case 'I':
110 return "i32";
111 case 'U':
112 return "u32";
113 case 'J':
114 return "i64";
115 case 'Q':
116 return "u64";
117 case 'F':
118 return "f32";
119 case 'D':
120 return "f64";
121 case 'A':
122 return "any";
123 default: {
124 break;
125 }
126 }
127
128 Str name = utf::Mutf8AsCString(descriptor);
129 if (name[0] == '[') {
130 return name;
131 }
132
133 std::replace(name.begin(), name.end(), '/', '.');
134
135 ASSERT(name.size() > 2); // 2 - L and ;
136
137 name.erase(0, 1);
138 name.pop_back();
139
140 return name;
141 }
142
143 } // namespace panda
144
145 #endif // PANDA_RUNTIME_INCLUDE_CLASS_HELPER_H_
146