/* * 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 "ast/ast.h" #include "ast/ast_attribute.h" #include "ast/ast_interface_type.h" #include "ast/ast_method.h" #include "ast/ast_type.h" #include "lexer/lexer.h" #include "preprocessor/preprocessor.h" #include "util/autoptr.h" #include "util/light_refcount_base.h" #include "util/options.h" #include "parser/intf_type_check.h" namespace OHOS { namespace Idl { using AttrSet = std::set; struct AstCompare { bool operator()(const AutoPtr &lhs, const AutoPtr &rhs) const { return lhs->GetMinorVer() < rhs->GetMinorVer(); } }; 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: 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 ParseSupportDelegator(); bool ParserPackageInfo(const std::string &packageName); bool ParseImports(); void ParseImportInfo(); void ParseSequenceableInfo(); bool ParseTypeDecls(); // parse attributes of type void ParseAttribute(); AttrSet ParseAttributeInfo(); bool ParseAttrUnit(AttrSet &attrs); void ParseAttrUnitFreezecontrol(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 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(); bool CheckBasicType(Token token); AutoPtr ParseBasicType(); AutoPtr ParseUnsignedType(); AutoPtr ParseArrayType(const AutoPtr &elementType); AutoPtr ParseListType(); AutoPtr ParseMapType(); AutoPtr ParseSmqType(); bool CheckUserDefType(Token token); 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 std::string &importName); void ParseInterfaceExtends(AutoPtr &interface); void ParseExtendsInfo(AutoPtr &interfaceType); bool CheckExtendsName(AutoPtr &interfaceType, const std::string &extendsName); bool CheckExtendsVersion( AutoPtr &interfaceType, const std::string &extendsName, 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(); 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 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_; }; } // namespace Idl } // namespace OHOS #endif // OHOS_IDL_PARSER_H