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 "ast/ast_interface_type.h"
17 #include "util/string_builder.h"
18
19 namespace OHOS {
20 namespace Idl {
SetNamespace(const AutoPtr<ASTNamespace> & nspace)21 void ASTInterfaceType::SetNamespace(const AutoPtr<ASTNamespace> &nspace)
22 {
23 ASTType::SetNamespace(nspace);
24 if (namespace_ != nullptr) {
25 namespace_->AddInterface(this);
26 }
27 }
28
AddMethod(const AutoPtr<ASTMethod> & method)29 void ASTInterfaceType::AddMethod(const AutoPtr<ASTMethod> &method)
30 {
31 if (method == nullptr) {
32 return;
33 }
34 methods_.push_back(method);
35 }
36
GetMethod(size_t index)37 AutoPtr<ASTMethod> ASTInterfaceType::GetMethod(size_t index)
38 {
39 if (index >= methods_.size()) {
40 return nullptr;
41 }
42
43 return methods_[index];
44 }
45
AddExtendsInterface(AutoPtr<ASTInterfaceType> interface)46 bool ASTInterfaceType::AddExtendsInterface(AutoPtr<ASTInterfaceType> interface)
47 {
48 if (extendsInterface_ != nullptr) {
49 return false;
50 }
51 extendsInterface_ = interface;
52 return true;
53 }
54
SetVersion(size_t majorVer,size_t minorVer)55 void ASTInterfaceType::SetVersion(size_t majorVer, size_t minorVer)
56 {
57 majorVersion_ = majorVer;
58 minorVersion_ = minorVer;
59 }
60
GetMethods() const61 std::vector<AutoPtr<ASTMethod>> ASTInterfaceType::GetMethods() const
62 {
63 return methods_;
64 }
65
GetMethodsBySystem(SystemLevel system) const66 std::vector<AutoPtr<ASTMethod>> ASTInterfaceType::GetMethodsBySystem(SystemLevel system) const
67 {
68 std::vector<AutoPtr<ASTMethod>> methods;
69 for (const auto &method : methods_) {
70 if (method->GetAttribute()->Match(system)) {
71 methods.push_back(method);
72 }
73 }
74 return methods;
75 }
76
GetSignature()77 std::string ASTInterfaceType::GetSignature()
78 {
79 std::string fullName = namespace_ != nullptr ?
80 namespace_->ToString() + name_ : name_;
81 return "L" + StringHelper::Replace(fullName, '.', '/') + ";";
82 }
83
IsInterfaceType()84 bool ASTInterfaceType::IsInterfaceType()
85 {
86 return true;
87 }
88
Dump(const std::string & prefix)89 std::string ASTInterfaceType::Dump(const std::string &prefix)
90 {
91 StringBuilder sb;
92
93 sb.Append(prefix);
94 if (Options::GetInstance().GetInterfaceType() == InterfaceType::SA) {
95 sb.AppendFormat("interface %s", (namespace_->ToString() + name_).c_str());
96 } else {
97 sb.Append(attr_->Dump(prefix));
98 sb.AppendFormat("interface %s", name_.c_str());
99 }
100 if (isExternal_) {
101 sb.Append(";\n");
102 } else {
103 sb.Append(" {\n");
104 for (auto method : methods_) {
105 sb.Append(method->Dump(prefix + " "));
106 if (method != methods_[methods_.size() - 1]) {
107 sb.Append('\n');
108 }
109 }
110 sb.Append(prefix).Append("}\n");
111 }
112
113 return sb.ToString();
114 }
115
GetTypeKind()116 TypeKind ASTInterfaceType::GetTypeKind()
117 {
118 return TypeKind::TYPE_INTERFACE;
119 }
120
GetFullName() const121 std::string ASTInterfaceType::GetFullName() const
122 {
123 return namespace_->ToString() + name_;
124 }
125 } // namespace Idl
126 } // namespace OHOS
127