• 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 #ifndef PANDA_RUNTIME_KLASS_HELPER_H_
16 #define PANDA_RUNTIME_KLASS_HELPER_H_
17 
18 #include <cstdint>
19 
20 #include "libpandabase/utils/span.h"
21 #include "libpandafile/file_items.h"
22 #include "runtime/include/coretypes/tagged_value.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 const uint8_t *GetDescriptor(const uint8_t *name, PandaString *storage);
44 
45     static const uint8_t *GetTypeDescriptor(const PandaString &name, PandaString *storage);
46 
47     static const uint8_t *GetArrayDescriptor(const uint8_t *component_name, size_t rank, PandaString *storage);
48 
49     static char GetPrimitiveTypeDescriptorChar(panda_file::Type::TypeId type_id);
50 
51     static const uint8_t *GetPrimitiveTypeDescriptorStr(panda_file::Type::TypeId type_id);
52 
53     static const char *GetPrimitiveTypeStr(panda_file::Type::TypeId type_id);
54 
55     static const uint8_t *GetPrimitiveDescriptor(panda_file::Type type, PandaString *storage);
56 
57     static const uint8_t *GetPrimitiveArrayDescriptor(panda_file::Type type, size_t rank, PandaString *storage);
58 
59     template <typename Str = std::string>
60     static Str GetName(const uint8_t *descriptor);
61 
IsArrayDescriptor(const uint8_t * descriptor)62     static bool IsArrayDescriptor(const uint8_t *descriptor)
63     {
64         Span<const uint8_t> sp(descriptor, 1);
65         return sp[0] == '[';
66     }
67 
GetComponentDescriptor(const uint8_t * descriptor)68     static const uint8_t *GetComponentDescriptor(const uint8_t *descriptor)
69     {
70         ASSERT(IsArrayDescriptor(descriptor));
71         Span<const uint8_t> sp(descriptor, 1);
72         return sp.cend();
73     }
74 
GetDimensionality(const uint8_t * descriptor)75     static size_t GetDimensionality(const uint8_t *descriptor)
76     {
77         ASSERT(IsArrayDescriptor(descriptor));
78         size_t dim = 0;
79         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
80         while (*descriptor++ == '[') {
81             ++dim;
82         }
83         return dim;
84     }
85 };
86 
87 // Str is std::string or PandaString
88 /* static */
89 template <typename Str>
GetName(const uint8_t * descriptor)90 Str ClassHelper::GetName(const uint8_t *descriptor)
91 {
92     switch (*descriptor) {
93         case 'V':
94             return "void";
95         case 'Z':
96             return "u1";
97         case 'B':
98             return "i8";
99         case 'H':
100             return "u8";
101         case 'S':
102             return "i16";
103         case 'C':
104             return "u16";
105         case 'I':
106             return "i32";
107         case 'U':
108             return "u32";
109         case 'J':
110             return "i64";
111         case 'Q':
112             return "u64";
113         case 'F':
114             return "f32";
115         case 'D':
116             return "f64";
117         case 'A':
118             return "any";
119         default: {
120             break;
121         }
122     }
123 
124     Str name = utf::Mutf8AsCString(descriptor);
125     if (name[0] == '[') {
126         return name;
127     }
128 
129     std::replace(name.begin(), name.end(), '/', '.');
130 
131     ASSERT(name.size() > 2);  // 2 - L and ;
132 
133     name.erase(0, 1);
134     name.pop_back();
135 
136     return name;
137 }
138 
139 }  // namespace panda
140 
141 #endif  // PANDA_RUNTIME_KLASS_HELPER_H_
142