• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 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 "libabckit/src/helpers_common.h"
17 #include "libabckit/src/macros.h"
18 
19 namespace libabckit {
20 
ModuleEnumerateClassesHelper(AbckitCoreModule * m,void * data,bool (* cb)(AbckitCoreClass * klass,void * data))21 bool ModuleEnumerateClassesHelper(AbckitCoreModule *m, void *data, bool (*cb)(AbckitCoreClass *klass, void *data))
22 {
23     for (auto &[className, klass] : m->ct) {
24         if (!cb(klass.get(), data)) {
25             return false;
26         }
27     }
28     return true;
29 }
30 
ModuleEnumerateTopLevelFunctionsHelper(AbckitCoreModule * m,void * data,bool (* cb)(AbckitCoreFunction * function,void * data))31 bool ModuleEnumerateTopLevelFunctionsHelper(AbckitCoreModule *m, void *data,
32                                             bool (*cb)(AbckitCoreFunction *function, void *data))
33 {
34     LIBABCKIT_LOG_FUNC;
35     LIBABCKIT_BAD_ARGUMENT(m, false)
36     LIBABCKIT_BAD_ARGUMENT(cb, false)
37     for (auto &function : m->functions) {
38         if (!cb(function.get(), data)) {
39             return false;
40         }
41     }
42     return true;
43 }
44 
ModuleEnumerateAnnotationInterfacesHelper(AbckitCoreModule * m,void * data,bool (* cb)(AbckitCoreAnnotationInterface * ai,void * data))45 bool ModuleEnumerateAnnotationInterfacesHelper(AbckitCoreModule *m, void *data,
46                                                bool (*cb)(AbckitCoreAnnotationInterface *ai, void *data))
47 {
48     LIBABCKIT_LOG_FUNC;
49     LIBABCKIT_BAD_ARGUMENT(m, false)
50     LIBABCKIT_BAD_ARGUMENT(cb, false)
51     for (auto &[atName, at] : m->at) {
52         if (!cb(at.get(), data)) {
53             return false;
54         }
55     }
56     return true;
57 }
58 
ClassEnumerateMethodsHelper(AbckitCoreClass * klass,void * data,bool (* cb)(AbckitCoreFunction * method,void * data))59 bool ClassEnumerateMethodsHelper(AbckitCoreClass *klass, void *data, bool (*cb)(AbckitCoreFunction *method, void *data))
60 {
61     LIBABCKIT_LOG_FUNC;
62     for (auto &method : klass->methods) {
63         if (!cb(method.get(), data)) {
64             return false;
65         }
66     }
67     return true;
68 }
69 
IsDynamic(AbckitTarget target)70 bool IsDynamic(AbckitTarget target)
71 {
72     return target == ABCKIT_TARGET_JS || target == ABCKIT_TARGET_ARK_TS_V1;
73 }
74 
HashCombine(size_t seed,size_t value)75 static size_t HashCombine(size_t seed, size_t value)
76 {
77     const auto m = uint64_t {0xC6A4A7935BD1E995};
78     const uint32_t r = 47;
79 
80     value *= m;
81     value ^= value >> r;
82     value *= m;
83 
84     seed ^= value;
85     seed *= m;
86     return seed;
87 }
88 
GetOrCreateType(AbckitFile * file,AbckitTypeId id,size_t rank,AbckitCoreClass * klass)89 AbckitType *GetOrCreateType(AbckitFile *file, AbckitTypeId id, size_t rank, AbckitCoreClass *klass)
90 {
91     size_t hash = 0;
92     hash = HashCombine(hash, (size_t)id);
93     hash = HashCombine(hash, rank);
94     hash = HashCombine(hash, reinterpret_cast<size_t>(klass));
95 
96     auto &cache = file->types;
97     if (cache.count(hash) == 1) {
98         return cache[hash].get();
99     }
100 
101     auto type = std::make_unique<AbckitType>();
102     type->id = id;
103     type->rank = rank;
104     type->klass = klass;
105     cache.insert({hash, std::move(type)});
106     return cache[hash].get();
107 }
108 
109 }  // namespace libabckit
110