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 "ast/ast_module.h" 17 #include "util/string_builder.h" 18 19 namespace OHOS { 20 namespace Idl { ASTModule()21ASTModule::ASTModule() 22 { 23 booleanType_ = new ASTBooleanType(); 24 byteType_ = new ASTByteType(); 25 shortType_ = new ASTShortType(); 26 integerType_ = new ASTIntegerType(); 27 longType_ = new ASTLongType(); 28 floatType_ = new ASTFloatType(); 29 doubleType_ = new ASTDoubleType(); 30 charType_ = new ASTCharType(); 31 stringType_ = new ASTStringType(); 32 voidType_ = new ASTVoidType(); 33 34 types_["boolean"] = booleanType_.Get(); 35 types_["byte"] = byteType_.Get(); 36 types_["short"] = shortType_.Get(); 37 types_["int"] = integerType_.Get(); 38 types_["long"] = longType_.Get(); 39 types_["float"] = floatType_.Get(); 40 types_["double"] = doubleType_.Get(); 41 types_["char"] = charType_.Get(); 42 types_["String"] = stringType_.Get(); 43 types_["void"] = voidType_.Get(); 44 } 45 SetIdlFile(const String & idlFile)46void ASTModule::SetIdlFile(const String& idlFile) 47 { 48 idlFilePath_ = idlFile; 49 #ifdef __MINGW32__ 50 int index = idlFilePath_.LastIndexOf('\\'); 51 #else 52 int index = idlFilePath_.LastIndexOf('/'); 53 #endif 54 int end = idlFilePath_.LastIndexOf(".idl") == -1 ? 55 idlFilePath_.LastIndexOf(".idl") : idlFilePath_.LastIndexOf(".idl"); 56 name_ = idlFilePath_.Substring((index == -1) ? 0 : (index + 1), end); 57 } 58 ParseNamespace(const String & nspaceStr)59AutoPtr<ASTNamespace> ASTModule::ParseNamespace(const String& nspaceStr) 60 { 61 AutoPtr<ASTNamespace> currNspace; 62 int begin = 0; 63 int index = 0; 64 while ((index = nspaceStr.IndexOf('.', begin)) != -1) { 65 String ns = nspaceStr.Substring(begin, index); 66 AutoPtr<ASTNamespace> nspace; 67 if (currNspace == nullptr) { 68 nspace = FindNamespace(ns); 69 } else { 70 nspace = currNspace->FindNamespace(ns); 71 } 72 if (nspace == nullptr) { 73 nspace = new ASTNamespace(ns); 74 if (currNspace == nullptr) { 75 AddNamespace(nspace); 76 } else { 77 currNspace->AddNamespace(nspace); 78 } 79 } 80 currNspace = nspace; 81 begin = index + 1; 82 } 83 return currNspace; 84 } 85 AddNamespace(ASTNamespace * nspace)86void ASTModule::AddNamespace(ASTNamespace* nspace) 87 { 88 if (nspace == nullptr) { 89 return; 90 } 91 namespaces_.push_back(nspace); 92 } 93 FindNamespace(const String & nspaceStr)94AutoPtr<ASTNamespace> ASTModule::FindNamespace(const String& nspaceStr) 95 { 96 for (auto nspace : namespaces_) { 97 if (nspace->ToShortString().Equals(nspaceStr)) { 98 return nspace; 99 } 100 } 101 return nullptr; 102 } 103 GetNamespace(size_t index)104AutoPtr<ASTNamespace> ASTModule::GetNamespace(size_t index) 105 { 106 if (index >= namespaces_.size()) { 107 return nullptr; 108 } 109 110 return namespaces_[index]; 111 } 112 AddInterface(ASTInterfaceType * interface)113void ASTModule::AddInterface(ASTInterfaceType* interface) 114 { 115 if (interface == nullptr) { 116 return; 117 } 118 119 interfaces_.push_back(interface); 120 types_[interface->ToString()] = interface; 121 } 122 GetInterface(size_t index)123AutoPtr<ASTInterfaceType> ASTModule::GetInterface(size_t index) 124 { 125 if (index >= interfaces_.size()) { 126 return nullptr; 127 } 128 129 return interfaces_[index]; 130 } 131 IndexOf(ASTInterfaceType * interface)132int ASTModule::IndexOf(ASTInterfaceType* interface) 133 { 134 for (size_t i = 0; i < interfaces_.size(); i++) { 135 if (interfaces_[i] == interface) { 136 return i; 137 } 138 } 139 return -1; 140 } 141 AddSequenceable(ASTSequenceableType * sequenceable)142void ASTModule::AddSequenceable(ASTSequenceableType* sequenceable) 143 { 144 if (sequenceable == nullptr) { 145 return; 146 } 147 148 sequenceables_.push_back(sequenceable); 149 types_[sequenceable->ToString()] = sequenceable; 150 } 151 GetSequenceable(size_t index)152AutoPtr<ASTSequenceableType> ASTModule::GetSequenceable(size_t index) 153 { 154 if (index >= sequenceables_.size()) { 155 return nullptr; 156 } 157 158 return sequenceables_[index]; 159 } 160 IndexOf(ASTSequenceableType * sequenceable)161int ASTModule::IndexOf(ASTSequenceableType* sequenceable) 162 { 163 for (size_t i = 0; i < sequenceables_.size(); i++) { 164 if (sequenceables_[i] == sequenceable) { 165 return i; 166 } 167 } 168 return -1; 169 } 170 AddType(ASTType * type)171void ASTModule::AddType(ASTType* type) 172 { 173 if (type == nullptr) { 174 return; 175 } 176 177 types_[type->ToString()] = type; 178 } 179 FindType(const String & typeName)180AutoPtr<ASTType> ASTModule::FindType(const String& typeName) 181 { 182 if (typeName.IsEmpty()) { 183 return nullptr; 184 } 185 186 auto it = types_.find(typeName); 187 return it != types_.end() ? it->second : nullptr; 188 } 189 IndexOf(ASTType * type)190int ASTModule::IndexOf(ASTType* type) 191 { 192 int i = 0; 193 for (auto it = types_.begin(); it != types_.end(); ++it, ++i) { 194 if (it->second == type) { 195 return i; 196 } 197 } 198 return -1; 199 } 200 IsValid()201bool ASTModule::IsValid() 202 { 203 if (name_.IsEmpty()) { 204 return false; 205 } 206 207 return interfaces_.size() > 0; 208 } 209 Dump(const String & prefix)210String ASTModule::Dump(const String& prefix) 211 { 212 StringBuilder sb; 213 214 sb.Append(prefix); 215 sb.Append("Module["); 216 sb.Append("name: ").Append(name_).Append(" "); 217 sb.Append("file: ").Append(idlFilePath_); 218 sb.Append("]\n"); 219 220 for (auto sequenceable : sequenceables_) { 221 String info = sequenceable->Dump(" "); 222 sb.Append(info); 223 } 224 sb.Append('\n'); 225 226 for (auto interface : interfaces_) { 227 if (interface->IsExternal()) { 228 String info = interface->Dump(" "); 229 sb.Append(info); 230 } 231 } 232 sb.Append('\n'); 233 234 for (auto interface : interfaces_) { 235 if (!interface->IsExternal()) { 236 String info = interface->Dump(" "); 237 sb.Append(info); 238 } 239 } 240 241 return sb.ToString(); 242 } 243 } // namespace Idl 244 } // namespace OHOS 245