/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef OHOS_IDL_PARSER_H #define OHOS_IDL_PARSER_H #include #include #include #include #include #include #include "ast/ast.h" #include "ast/ast_array_type.h" #include "ast/ast_set_type.h" #include "ast/ast_attribute.h" #include "ast/ast_interface_type.h" #include "ast/ast_method.h" #include "ast/ast_type.h" #include "ast/ast_enum_type.h" #include "ast/ast_map_type.h" #include "ast/ast_orderedmap_type.h" #include "ast/ast_parameter.h" #include "ast/ast_ptr_type.h" #include "ast/ast_rawdata_type.h" #include "ast/ast_sequenceable_type.h" #include "ast/ast_smq_type.h" #include "ast/ast_struct_type.h" #include "ast/ast_union_type.h" #include "lexer/lexer.h" #include "lexer/token.h" #include "preprocessor/preprocessor.h" #include "util/autoptr.h" #include "util/light_refcount_base.h" #include "util/logger.h" namespace OHOS { namespace Idl { struct AstCompare { bool operator()(const AutoPtr &lhs, const AutoPtr &rhs) const { return lhs->GetMinorVer() < rhs->GetMinorVer(); } }; using AttrSet = std::set; using AstMergeMap = std::map, AstCompare>>; class Parser { public: Parser() = default; ~Parser() = default; bool Parse(const std::vector &fileDetails); using StrAstMap = std::unordered_map>; inline const StrAstMap &GetAllAst() const { return allAsts_; } private: static constexpr int MIN_TRANSACTION_ID = 0x01; static constexpr int MAX_TRANSACTION_ID = 0x00ffffff; static constexpr int MIN_IPC_CAPACITY = 1; static constexpr int MAX_IPC_CAPACITY = 0x0001ffff; class Attribute : public LightRefCountBase { public: bool isOneWay = false; bool isCallback = false; bool isFull = false; bool isLite = false; }; bool ParseOne(const std::string &sourceFile); bool Reset(const std::string &sourceFile); bool ParseFile(); std::string ParseLicense(); bool ParsePackage(); bool ParseInterfaceToken(); bool ParseKeywordWithId(TokenType expectedKeyword, const std::string& context); bool ParseSupportDelegator(); bool ParseOptionStubHooks(); bool ParseOptionParcelHooks(); bool ParserPackageInfo(const std::string &packageName); bool ParseImports(); void ParseImportInfo(); void ParseSequenceableInfo(); void ParseRawDataInfo(); bool ParseTypeDecls(); // parse attributes of type void ParseAttribute(); AttrSet ParseAttributeInfo(); bool ParseAttrUnit(AttrSet &attrs); void ParseAttrUnitFreezecontrol(AttrSet &attrs, Token &token); void ParseAttrUnitCustomMsgOption(AttrSet &attrs, Token &token); void ParseAttrUnitMarco(AttrSet &attrs, Token &token); void ParseAttrUnitIpccode(AttrSet &attrs, Token &token); void ParseAttrUnitIpcCapacity(AttrSet &attrs, Token &token); // parse interface type void ParseInterface(const AttrSet &attrs = {}); AutoPtr ParseInfAttrInfo(const AttrSet &attrs); void CheckInterfaceAttr(const AutoPtr &interface, Token token); void CheckIpcCodeValue( const AutoPtr &method, int32_t &ipcCodeValue, std::unordered_set &ipcCodeSet); void ParseInterfaceExternal(const AutoPtr &interface); void ParseInterfaceBody(const AutoPtr &interface); AutoPtr ParseMethod(const AutoPtr &interface); AutoPtr ParseMethodReturnType(); AutoPtr ParseMethodAttr(); AutoPtr CreateGetVersionMethod(); void CheckMethodAttr(const AutoPtr &interface, const AutoPtr &method); void ParseMethodParamList(const AutoPtr &method); AutoPtr ParseParam(); bool CheckParamAttr(); void SetParamAttrVal(Token token, AutoPtr attr); AutoPtr ParseParamAttr(); // parse type AutoPtr ParseType(); AutoPtr ParseBasicType(); AutoPtr ParseUnsignedType(); AutoPtr ParseArrayType(const AutoPtr &elementType); AutoPtr ParseListType(); AutoPtr ParseSetType(); AutoPtr ParsePtrType(TokenType ptrKind); AutoPtr ParseMapType(); AutoPtr ParseOrderedMapType(); AutoPtr ParseSmqType(); AutoPtr ParseUserDefType(); // parse declaration of enum void ParseEnumDeclaration(const AttrSet &attrs = {}); AutoPtr ParseEnumBaseType(); void ParserEnumMember(const AutoPtr &enumType); // parse declaration of struct void ParseStructDeclaration(const AttrSet &attrs = {}); AutoPtr ParseStructParentType(); void ParseStructMember(const AutoPtr &structType); // parse declaration of union void ParseUnionDeclaration(const AttrSet &attrs = {}); void ParseUnionMember(const AutoPtr &unionType); bool AddUnionMember( const AutoPtr &unionType, const AutoPtr &type, const std::string &name) const; AutoPtr ParseUserDefTypeAttr(const AttrSet &attrs); // parse expression AutoPtr ParseExpr(); AutoPtr ParseAndExpr(); AutoPtr ParseXorExpr(); AutoPtr ParseOrExpr(); AutoPtr ParseShiftExpr(); AutoPtr ParseAddExpr(); AutoPtr ParseMulExpr(); AutoPtr ParseUnaryExpr(); AutoPtr ParsePrimaryExpr(); AutoPtr ParseNumExpr(); AutoPtr ParseEnumExpr(); bool CheckNumber(const std::string& integerVal) const; bool CheckType(const Token &token, const AutoPtr &type); bool CheckTypeByMode(const Token &token, const AutoPtr &type); void SetAstFileType(); bool CheckPackageName(const std::string &filePath, const std::string &packageName) const; bool CheckImport(const Token &token); void ParseInterfaceExtends(AutoPtr &interface); void ParseExtendsInfo(AutoPtr &interfaceType); bool CheckExtendsName(AutoPtr &interfaceType, const std::string &extendsName); bool CheckExtendsVersion(AutoPtr extendsAst); bool CheckImportsVersion(AutoPtr extendsAst); inline static bool IsPrimitiveType(Token token) { return token.kind >= TokenType::BOOLEAN && token.kind <= TokenType::ASHMEM; } bool AddAst(const AutoPtr &ast); void LogError(const char *funcName, int fileLine, const std::string &message) { errors_.push_back(StringHelper::Format("[%s:%d] error:%s", funcName, fileLine, message.c_str())); } void LogError(const char *funcName, int fileLine, const Token &token, const std::string &message) { errors_.push_back(StringHelper::Format("[%s:%d] [%s] error:%s", funcName, fileLine, LocInfo(token).c_str(), message.c_str())); } void LogErrorBeforeToken(const char *funcName, int fileLine, const Token &token, const std::string &message) { errors_.push_back(StringHelper::Format("[%s:%d] [%s] error:%s before '%s' token", funcName, fileLine, LocInfo(token).c_str(), message.c_str(), token.value.c_str())); } void ShowError() { for (const auto &errMsg : errors_) { Logger::E(TAG, "%s", errMsg.c_str()); } } bool PostProcess(); bool CheckExistExtends(); bool GetGenVersion(std::vector &version, std::string &genPackageName); void GetGenNamespace(AutoPtr &ns); void SortAstByName(AstMergeMap &mergeMap, StrAstMap &allAsts); void MergeAsts(AstMergeMap &mergeMap); void MergeAst(AutoPtr &targetAst, AutoPtr sourceAst); void MergeImport(AutoPtr &targetAst, AutoPtr sourceAst); void MergeInterfaceDef(AutoPtr &targetAst, AutoPtr sourceAst); void MergeTypes(AutoPtr &targetAst, AutoPtr sourceAst); void MergeSequenceableDef(AutoPtr &targetAst, AutoPtr sourceAst); void MergeRawDataDef(AutoPtr &targetAst, AutoPtr sourceAst); void MergeTypeDefinitions(AutoPtr &targetAst, AutoPtr sourceAst); void ModifyImport(StrAstMap &allAsts, std::string &genPackageName); void ModifyPackageNameAndVersion(StrAstMap &allAsts, std::string &genPackageName, std::vector genVersion); void ModifyInterfaceNamespace(AutoPtr &ns); Lexer lexer_; std::vector errors_; AutoPtr ast_; StrAstMap allAsts_; std::string freezecontrolAttr_; AutoPtr currentEnum_; std::string messageOption_; std::string macroVal_; std::string macroType_; }; } // namespace Idl } // namespace OHOS #endif // OHOS_IDL_PARSER_H