1 /* 2 * Copyright (c) 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 "class.h" 17 #include "abc_file.h" 18 #include "function.h" 19 20 namespace panda::defect_scan_aux { GetClassName() const21const std::string &Class::GetClassName() const 22 { 23 return class_name_; 24 } 25 GetRecordName() const26const std::string &Class::GetRecordName() const 27 { 28 return record_name_; 29 } 30 GetAbcFileInstance() const31const AbcFile *Class::GetAbcFileInstance() const 32 { 33 return abc_file_; 34 } 35 GetDefiningFunction() const36Function *Class::GetDefiningFunction() const 37 { 38 return def_func_; 39 } 40 GetMemberFunctionCount() const41size_t Class::GetMemberFunctionCount() const 42 { 43 return member_func_list_.size(); 44 } 45 GetMemberFunctionList() const46const std::vector<const Function *> &Class::GetMemberFunctionList() const 47 { 48 return member_func_list_; 49 } 50 GetFuncNameWithoutPrefix(const std::string & func_name)51static std::string GetFuncNameWithoutPrefix(const std::string &func_name) 52 { 53 auto name = std::string(func_name); 54 auto pos1 = name.find_first_of("#"); 55 auto pos2 = name.find_last_of("#"); 56 if (pos1 == pos2) { 57 ASSERT(pos1 == std::string::npos); 58 return name; 59 } 60 ASSERT(pos1 != std::string::npos); 61 ASSERT(pos2 != std::string::npos); 62 auto record_name = name.substr(0, pos1); 63 auto fun_name = name.substr(pos2 + 1); 64 return record_name + fun_name; 65 } 66 GetMemberFunctionByName(std::string_view func_name) const67const Function *Class::GetMemberFunctionByName(std::string_view func_name) const 68 { 69 auto func_name_without_prefix = GetFuncNameWithoutPrefix(std::string(func_name)); 70 for (auto func : member_func_list_) { 71 if (GetFuncNameWithoutPrefix(func->GetFunctionName()) == func_name_without_prefix) { 72 return func; 73 } 74 } 75 if (par_class_info_.class_ != nullptr) { 76 return par_class_info_.class_->GetMemberFunctionByName(func_name); 77 } 78 return nullptr; 79 } 80 GetMemberFunctionByIndex(size_t index) const81const Function *Class::GetMemberFunctionByIndex(size_t index) const 82 { 83 ASSERT(index < member_func_list_.size()); 84 return member_func_list_[index]; 85 } 86 GetParentClass() const87const Class *Class::GetParentClass() const 88 { 89 return par_class_info_.class_; 90 } 91 GetParentClassName() const92const std::string &Class::GetParentClassName() const 93 { 94 return par_class_info_.class_name_; 95 } 96 GetParClassExternalModuleName() const97const std::string &Class::GetParClassExternalModuleName() const 98 { 99 return par_class_info_.external_module_name_; 100 } 101 GetParClassGlobalVarName() const102const std::string &Class::GetParClassGlobalVarName() const 103 { 104 return par_class_info_.global_var_name_; 105 } 106 SetParentClass(const Class * parent_class)107void Class::SetParentClass(const Class *parent_class) 108 { 109 ASSERT(parent_class != nullptr); 110 par_class_info_.class_ = parent_class; 111 par_class_info_.class_name_ = parent_class->GetClassName(); 112 } 113 SetParentClassName(std::string_view par_class_name)114void Class::SetParentClassName(std::string_view par_class_name) 115 { 116 par_class_info_.class_name_ = par_class_name; 117 } 118 SetParClassExternalModuleName(std::string_view external_module_name)119void Class::SetParClassExternalModuleName(std::string_view external_module_name) 120 { 121 par_class_info_.external_module_name_ = external_module_name; 122 } 123 SetParClassGlobalVarName(std::string global_var_name)124void Class::SetParClassGlobalVarName(std::string global_var_name) 125 { 126 par_class_info_.global_var_name_ = global_var_name; 127 } 128 AddMemberFunction(const Function * func)129void Class::AddMemberFunction(const Function *func) 130 { 131 ASSERT(func != nullptr); 132 member_func_list_.push_back(func); 133 } 134 } // namespace panda::defect_scan_aux 135