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 #ifndef OHOS_IDL_PARSER_H 17 #define OHOS_IDL_PARSER_H 18 19 #include <memory> 20 #include <set> 21 #include <vector> 22 #include <regex> 23 #include <unordered_set> 24 #include <algorithm> 25 #include "ast/ast.h" 26 #include "ast/ast_array_type.h" 27 #include "ast/ast_attribute.h" 28 #include "ast/ast_interface_type.h" 29 #include "ast/ast_method.h" 30 #include "ast/ast_type.h" 31 #include "ast/ast_enum_type.h" 32 #include "ast/ast_map_type.h" 33 #include "ast/ast_orderedmap_type.h" 34 #include "ast/ast_parameter.h" 35 #include "ast/ast_rawdata_type.h" 36 #include "ast/ast_sequenceable_type.h" 37 #include "ast/ast_smq_type.h" 38 #include "ast/ast_struct_type.h" 39 #include "ast/ast_union_type.h" 40 #include "lexer/lexer.h" 41 #include "lexer/token.h" 42 #include "preprocessor/preprocessor.h" 43 #include "util/autoptr.h" 44 #include "util/light_refcount_base.h" 45 #include "util/logger.h" 46 47 namespace OHOS { 48 namespace Idl { 49 50 struct AstCompare { operatorAstCompare51 bool operator()(const AutoPtr<AST> &lhs, const AutoPtr<AST> &rhs) const 52 { 53 return lhs->GetMinorVer() < rhs->GetMinorVer(); 54 } 55 }; 56 57 using AttrSet = std::set<Token, TokenTypeCompare>; 58 59 using AstMergeMap = std::map<std::string, std::set<AutoPtr<AST>, AstCompare>>; 60 61 class Parser { 62 public: 63 Parser() = default; 64 65 ~Parser() = default; 66 67 bool Parse(const std::vector<FileDetail> &fileDetails); 68 69 using StrAstMap = std::unordered_map<std::string, AutoPtr<AST>>; GetAllAst()70 inline const StrAstMap &GetAllAst() const 71 { 72 return allAsts_; 73 } 74 75 private: 76 static constexpr int MIN_TRANSACTION_ID = 0x01; 77 static constexpr int MAX_TRANSACTION_ID = 0x00ffffff; 78 static constexpr int MIN_IPC_CAPACITY = 1; 79 static constexpr int MAX_IPC_CAPACITY = 0x0001ffff; 80 81 class Attribute : public LightRefCountBase { 82 public: 83 bool isOneWay = false; 84 bool isCallback = false; 85 bool isFull = false; 86 bool isLite = false; 87 }; 88 89 bool ParseOne(const std::string &sourceFile); 90 91 bool Reset(const std::string &sourceFile); 92 93 bool ParseFile(); 94 95 std::string ParseLicense(); 96 97 bool ParsePackage(); 98 99 bool ParseInterfaceToken(); 100 101 bool ParseKeywordWithId(TokenType expectedKeyword, const std::string& context); 102 103 bool ParseSupportDelegator(); 104 105 bool ParseOptionStubHooks(); 106 107 bool ParseOptionParcelHooks(); 108 109 bool ParserPackageInfo(const std::string &packageName); 110 111 bool ParseImports(); 112 113 void ParseImportInfo(); 114 115 void ParseSequenceableInfo(); 116 117 void ParseRawDataInfo(); 118 119 bool ParseTypeDecls(); 120 121 // parse attributes of type 122 void ParseAttribute(); 123 124 AttrSet ParseAttributeInfo(); 125 126 bool ParseAttrUnit(AttrSet &attrs); 127 128 void ParseAttrUnitFreezecontrol(AttrSet &attrs, Token &token); 129 130 void ParseAttrUnitCustomMsgOption(AttrSet &attrs, Token &token); 131 132 void ParseAttrUnitMarco(AttrSet &attrs, Token &token); 133 134 void ParseAttrUnitIpccode(AttrSet &attrs, Token &token); 135 136 void ParseAttrUnitIpcCapacity(AttrSet &attrs, Token &token); 137 138 // parse interface type 139 void ParseInterface(const AttrSet &attrs = {}); 140 141 AutoPtr<ASTAttr> ParseInfAttrInfo(const AttrSet &attrs); 142 143 void CheckInterfaceAttr(const AutoPtr<ASTInterfaceType> &interface, Token token); 144 145 void CheckIpcCodeValue( 146 const AutoPtr<ASTMethod> &method, int32_t &ipcCodeValue, std::unordered_set<int32_t> &ipcCodeSet); 147 148 void ParseInterfaceExternal(const AutoPtr<ASTInterfaceType> &interface); 149 150 void ParseInterfaceBody(const AutoPtr<ASTInterfaceType> &interface); 151 152 AutoPtr<ASTMethod> ParseMethod(const AutoPtr<ASTInterfaceType> &interface); 153 154 AutoPtr<ASTType> ParseMethodReturnType(); 155 156 AutoPtr<ASTAttr> ParseMethodAttr(); 157 158 AutoPtr<ASTMethod> CreateGetVersionMethod(); 159 160 void CheckMethodAttr(const AutoPtr<ASTInterfaceType> &interface, const AutoPtr<ASTMethod> &method); 161 162 void ParseMethodParamList(const AutoPtr<ASTMethod> &method); 163 164 AutoPtr<ASTParameter> ParseParam(); 165 166 bool CheckParamAttr(); 167 168 void SetParamAttrVal(Token token, AutoPtr<ASTParamAttr> attr); 169 170 AutoPtr<ASTParamAttr> ParseParamAttr(); 171 172 // parse type 173 AutoPtr<ASTType> ParseType(); 174 175 AutoPtr<ASTType> ParseBasicType(); 176 177 AutoPtr<ASTType> ParseUnsignedType(); 178 179 AutoPtr<ASTType> ParseArrayType(const AutoPtr<ASTType> &elementType); 180 181 AutoPtr<ASTType> ParseListType(); 182 183 AutoPtr<ASTType> ParseMapType(); 184 185 AutoPtr<ASTType> ParseOrderedMapType(); 186 187 AutoPtr<ASTType> ParseSmqType(); 188 189 AutoPtr<ASTType> ParseUserDefType(); 190 191 // parse declaration of enum 192 void ParseEnumDeclaration(const AttrSet &attrs = {}); 193 194 AutoPtr<ASTType> ParseEnumBaseType(); 195 196 void ParserEnumMember(const AutoPtr<ASTEnumType> &enumType); 197 198 // parse declaration of struct 199 void ParseStructDeclaration(const AttrSet &attrs = {}); 200 201 AutoPtr<ASTStructType> ParseStructParentType(); 202 203 void ParseStructMember(const AutoPtr<ASTStructType> &structType); 204 205 // parse declaration of union 206 void ParseUnionDeclaration(const AttrSet &attrs = {}); 207 208 void ParseUnionMember(const AutoPtr<ASTUnionType> &unionType); 209 210 bool AddUnionMember( 211 const AutoPtr<ASTUnionType> &unionType, const AutoPtr<ASTType> &type, const std::string &name) const; 212 213 AutoPtr<ASTAttr> ParseUserDefTypeAttr(const AttrSet &attrs); 214 215 // parse expression 216 AutoPtr<ASTExpr> ParseExpr(); 217 218 AutoPtr<ASTExpr> ParseAndExpr(); 219 220 AutoPtr<ASTExpr> ParseXorExpr(); 221 222 AutoPtr<ASTExpr> ParseOrExpr(); 223 224 AutoPtr<ASTExpr> ParseShiftExpr(); 225 226 AutoPtr<ASTExpr> ParseAddExpr(); 227 228 AutoPtr<ASTExpr> ParseMulExpr(); 229 230 AutoPtr<ASTExpr> ParseUnaryExpr(); 231 232 AutoPtr<ASTExpr> ParsePrimaryExpr(); 233 234 AutoPtr<ASTExpr> ParseNumExpr(); 235 236 AutoPtr<ASTExpr> ParseEnumExpr(); 237 238 bool CheckNumber(const std::string& integerVal) const; 239 240 bool CheckType(const Token &token, const AutoPtr<ASTType> &type); 241 242 bool CheckTypeByMode(const Token &token, const AutoPtr<ASTType> &type); 243 244 void SetAstFileType(); 245 246 bool CheckPackageName(const std::string &filePath, const std::string &packageName) const; 247 248 bool CheckImport(const Token &token); 249 250 void ParseInterfaceExtends(AutoPtr<ASTInterfaceType> &interface); 251 252 void ParseExtendsInfo(AutoPtr<ASTInterfaceType> &interfaceType); 253 254 bool CheckExtendsName(AutoPtr<ASTInterfaceType> &interfaceType, const std::string &extendsName); 255 256 bool CheckExtendsVersion(AutoPtr<AST> extendsAst); 257 258 bool CheckImportsVersion(AutoPtr<AST> extendsAst); 259 IsPrimitiveType(Token token)260 inline static bool IsPrimitiveType(Token token) 261 { 262 return token.kind >= TokenType::BOOLEAN && token.kind <= TokenType::ASHMEM; 263 } 264 265 bool AddAst(const AutoPtr<AST> &ast); 266 LogError(const char * funcName,int fileLine,const std::string & message)267 void LogError(const char *funcName, int fileLine, const std::string &message) 268 { 269 errors_.push_back(StringHelper::Format("[%s:%d] error:%s", funcName, fileLine, message.c_str())); 270 } 271 LogError(const char * funcName,int fileLine,const Token & token,const std::string & message)272 void LogError(const char *funcName, int fileLine, const Token &token, const std::string &message) 273 { 274 errors_.push_back(StringHelper::Format("[%s:%d] [%s] error:%s", 275 funcName, fileLine, LocInfo(token).c_str(), message.c_str())); 276 } 277 LogErrorBeforeToken(const char * funcName,int fileLine,const Token & token,const std::string & message)278 void LogErrorBeforeToken(const char *funcName, int fileLine, const Token &token, const std::string &message) 279 { 280 errors_.push_back(StringHelper::Format("[%s:%d] [%s] error:%s before '%s' token", 281 funcName, fileLine, LocInfo(token).c_str(), message.c_str(), token.value.c_str())); 282 } 283 ShowError()284 void ShowError() 285 { 286 for (const auto &errMsg : errors_) { 287 Logger::E(TAG, "%s", errMsg.c_str()); 288 } 289 } 290 291 bool PostProcess(); 292 293 bool CheckExistExtends(); 294 295 bool GetGenVersion(std::vector<size_t> &version, std::string &genPackageName); 296 297 void GetGenNamespace(AutoPtr<ASTNamespace> &ns); 298 299 void SortAstByName(AstMergeMap &mergeMap, StrAstMap &allAsts); 300 301 void MergeAsts(AstMergeMap &mergeMap); 302 303 void MergeAst(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 304 305 void MergeImport(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 306 307 void MergeInterfaceDef(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 308 309 void MergeTypes(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 310 311 void MergeSequenceableDef(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 312 313 void MergeRawDataDef(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 314 315 void MergeTypeDefinitions(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 316 317 void ModifyImport(StrAstMap &allAsts, std::string &genPackageName); 318 319 void ModifyPackageNameAndVersion(StrAstMap &allAsts, std::string &genPackageName, std::vector<size_t> genVersion); 320 321 void ModifyInterfaceNamespace(AutoPtr<ASTNamespace> &ns); 322 323 Lexer lexer_; 324 std::vector<std::string> errors_; 325 AutoPtr<AST> ast_; 326 StrAstMap allAsts_; 327 std::string freezecontrolAttr_; 328 AutoPtr<ASTEnumType> currentEnum_; 329 std::string messageOption_; 330 std::string macroVal_; 331 std::string macroType_; 332 }; 333 } // namespace Idl 334 } // namespace OHOS 335 336 #endif // OHOS_IDL_PARSER_H