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