1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #ifndef OHOS_HDI_PARSER_H 10 #define OHOS_HDI_PARSER_H 11 12 #include <memory> 13 #include <set> 14 #include <vector> 15 16 #include "ast/ast.h" 17 #include "ast/ast_attribute.h" 18 #include "ast/ast_interface_type.h" 19 #include "ast/ast_method.h" 20 #include "ast/ast_type.h" 21 #include "lexer/lexer.h" 22 #include "preprocessor/preprocessor.h" 23 #include "util/autoptr.h" 24 #include "util/light_refcount_base.h" 25 #include "util/options.h" 26 27 namespace OHOS { 28 namespace HDI { 29 using AttrSet = std::set<Token, TokenTypeCompare>; 30 struct AstCompare { operatorAstCompare31 bool operator()(const AutoPtr<AST> &lhs, const AutoPtr<AST> &rhs) const 32 { 33 return lhs->GetMinorVer() < rhs->GetMinorVer(); 34 } 35 }; 36 using AstMergeMap = std::map<std::string, std::set<AutoPtr<AST>, AstCompare>>; 37 class Parser { 38 public: 39 Parser() = default; 40 41 ~Parser() = default; 42 43 bool Parse(const std::vector<FileDetail> &fileDetails); 44 45 using StrAstMap = std::unordered_map<std::string, AutoPtr<AST>>; GetAllAst()46 inline const StrAstMap &GetAllAst() const 47 { 48 return allAsts_; 49 } 50 51 private: 52 class Attribute : public LightRefCountBase { 53 public: 54 bool isOneWay = false; 55 bool isCallback = false; 56 bool isFull = false; 57 bool isLite = false; 58 }; 59 60 bool ParseOne(const std::string &sourceFile); 61 62 bool Reset(const std::string &sourceFile); 63 64 bool ParseFile(); 65 66 std::string ParseLicense(); 67 68 bool ParsePackage(); 69 70 bool ParserPackageInfo(const std::string &packageName); 71 72 bool ParseImports(); 73 74 void ParseImportInfo(); 75 76 void ParseSequenceableInfo(); 77 78 bool ParseTypeDecls(); 79 80 // parse attributes of type 81 void ParseAttribute(); 82 83 AttrSet ParseAttributeInfo(); 84 85 bool AprseAttrUnit(AttrSet &attrs); 86 87 // parse interface type 88 void ParseInterface(const AttrSet &attrs = {}); 89 90 AutoPtr<ASTAttr> ParseInfAttrInfo(const AttrSet &attrs); 91 92 void CheckInterfaceAttr(const AutoPtr<ASTInterfaceType> &interface, Token token); 93 94 void ParseInterfaceBody(const AutoPtr<ASTInterfaceType> &interface); 95 96 AutoPtr<ASTMethod> ParseMethod(const AutoPtr<ASTInterfaceType> &interface); 97 98 AutoPtr<ASTAttr> ParseMethodAttr(); 99 100 AutoPtr<ASTMethod> CreateGetVersionMethod(); 101 102 void CheckMethodAttr(const AutoPtr<ASTInterfaceType> &interface, const AutoPtr<ASTMethod> &method); 103 104 void ParseMethodParamList(const AutoPtr<ASTMethod> &method); 105 106 AutoPtr<ASTParameter> ParseParam(); 107 108 AutoPtr<ASTParamAttr> ParseParamAttr(); 109 110 // parse type 111 AutoPtr<ASTType> ParseType(); 112 113 AutoPtr<ASTType> ParseBasicType(); 114 115 AutoPtr<ASTType> ParseUnsignedType(); 116 117 AutoPtr<ASTType> ParseArrayType(const AutoPtr<ASTType> &elementType); 118 119 AutoPtr<ASTType> ParseListType(); 120 121 AutoPtr<ASTType> ParseMapType(); 122 123 AutoPtr<ASTType> ParseSmqType(); 124 125 AutoPtr<ASTType> ParseUserDefType(); 126 127 // parse declaration of enum 128 void ParseEnumDeclaration(const AttrSet &attrs = {}); 129 130 AutoPtr<ASTType> ParseEnumBaseType(); 131 132 void ParserEnumMember(const AutoPtr<ASTEnumType> &enumType); 133 134 // parse declaration of struct 135 void ParseStructDeclaration(const AttrSet &attrs = {}); 136 137 void ParseStructMember(const AutoPtr<ASTStructType> &structType); 138 139 // parse declaration of union 140 void ParseUnionDeclaration(const AttrSet &attrs = {}); 141 142 void ParseUnionMember(const AutoPtr<ASTUnionType> &unionType); 143 144 bool AddUnionMember( 145 const AutoPtr<ASTUnionType> &unionType, const AutoPtr<ASTType> &type, const std::string &name) const; 146 147 AutoPtr<ASTAttr> ParseUserDefTypeAttr(const AttrSet &attrs); 148 149 // parse expression 150 AutoPtr<ASTExpr> ParseExpr(); 151 152 AutoPtr<ASTExpr> ParseAndExpr(); 153 154 AutoPtr<ASTExpr> ParseXorExpr(); 155 156 AutoPtr<ASTExpr> ParseOrExpr(); 157 158 AutoPtr<ASTExpr> ParseShiftExpr(); 159 160 AutoPtr<ASTExpr> ParseAddExpr(); 161 162 AutoPtr<ASTExpr> ParseMulExpr(); 163 164 AutoPtr<ASTExpr> ParseUnaryExpr(); 165 166 AutoPtr<ASTExpr> ParsePrimaryExpr(); 167 168 AutoPtr<ASTExpr> ParseNumExpr(); 169 170 bool CheckNumber(const std::string& integerVal) const; 171 172 bool CheckType(const Token &token, const AutoPtr<ASTType> &type); 173 174 bool CheckTypeByMode(const Token &token, const AutoPtr<ASTType> &type); 175 176 void SetAstFileType(); 177 178 bool CheckIntegrity(); 179 180 bool CheckInterfaceAst(); 181 182 bool CheckCallbackAst(); 183 184 bool CheckPackageName(const std::string &filePath, const std::string &packageName) const; 185 186 bool CheckImport(const std::string &importName); 187 188 void ParseInterfaceExtends(AutoPtr<ASTInterfaceType> &interface); 189 190 void ParseExtendsInfo(AutoPtr<ASTInterfaceType> &interfaceType); 191 192 bool CheckExtendsName(AutoPtr<ASTInterfaceType> &interfaceType, const std::string &extendsName); 193 194 bool CheckExtendsVersion( 195 AutoPtr<ASTInterfaceType> &interfaceType, const std::string &extendsName, AutoPtr<AST> extendsAst); 196 197 bool CheckImportsVersion(AutoPtr<AST> extendsAst); 198 199 void SetInterfaceVersion(AutoPtr<ASTInterfaceType> &interfaceType); 200 IsPrimitiveType(Token token)201 inline static bool IsPrimitiveType(Token token) 202 { 203 return token.kind >= TokenType::BOOLEAN && token.kind <= TokenType::ASHMEM; 204 } 205 206 bool AddAst(const AutoPtr<AST> &ast); 207 208 void LogError(const std::string &message); 209 210 void LogError(const Token &token, const std::string &message); 211 212 void ShowError(); 213 214 bool PostProcess(); 215 216 bool CheckExistExtends(); 217 218 bool GetGenVersion(std::vector<size_t> &version, std::string &genPackageName); 219 220 void GetGenNamespace(AutoPtr<ASTNamespace> &ns); 221 222 void SortAstByName(AstMergeMap &mergeMap, StrAstMap &allAsts); 223 224 void MergeAsts(AstMergeMap &mergeMap); 225 226 void MergeAst(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 227 228 void MergeImport(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 229 230 void MergeInterfaceDef(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 231 232 void MergeTypes(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 233 234 void MergeSequenceableDef(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 235 236 void MergeTypeDefinitions(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 237 238 void ModifyImport(StrAstMap &allAsts, std::string &genPackageName); 239 240 void ModifyPackageNameAndVersion(StrAstMap &allAsts, std::string &genPackageName, std::vector<size_t> genVersion); 241 242 void ModifyInterfaceNamespace(AutoPtr<ASTNamespace> &ns); 243 244 Lexer lexer_; 245 std::vector<std::string> errors_; 246 AutoPtr<AST> ast_; 247 StrAstMap allAsts_; 248 }; 249 } // namespace HDI 250 } // namespace OHOS 251 252 #endif // OHOS_HDI_PARSER_H