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 { operator ()OHOS::HDI::AstCompare31 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() const46 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 AutoPtr<ASTStructType> ParseStructParentType(); 138 139 void ParseStructMember(const AutoPtr<ASTStructType> &structType); 140 141 // parse declaration of union 142 void ParseUnionDeclaration(const AttrSet &attrs = {}); 143 144 void ParseUnionMember(const AutoPtr<ASTUnionType> &unionType); 145 146 bool AddUnionMember( 147 const AutoPtr<ASTUnionType> &unionType, const AutoPtr<ASTType> &type, const std::string &name) const; 148 149 AutoPtr<ASTAttr> ParseUserDefTypeAttr(const AttrSet &attrs); 150 151 // parse expression 152 AutoPtr<ASTExpr> ParseExpr(); 153 154 AutoPtr<ASTExpr> ParseAndExpr(); 155 156 AutoPtr<ASTExpr> ParseXorExpr(); 157 158 AutoPtr<ASTExpr> ParseOrExpr(); 159 160 AutoPtr<ASTExpr> ParseShiftExpr(); 161 162 AutoPtr<ASTExpr> ParseAddExpr(); 163 164 AutoPtr<ASTExpr> ParseMulExpr(); 165 166 AutoPtr<ASTExpr> ParseUnaryExpr(); 167 168 AutoPtr<ASTExpr> ParsePrimaryExpr(); 169 170 AutoPtr<ASTExpr> ParseNumExpr(); 171 172 AutoPtr<ASTExpr> ParseEnumExpr(); 173 174 bool CheckNumber(const std::string& integerVal) const; 175 176 bool CheckType(const Token &token, const AutoPtr<ASTType> &type); 177 178 bool CheckTypeByMode(const Token &token, const AutoPtr<ASTType> &type); 179 180 void SetAstFileType(); 181 182 bool CheckIntegrity(); 183 184 bool CheckInterfaceAst(); 185 186 bool CheckCallbackAst(); 187 188 bool CheckPackageName(const std::string &filePath, const std::string &packageName) const; 189 190 bool CheckImport(const std::string &importName); 191 192 void ParseInterfaceExtends(AutoPtr<ASTInterfaceType> &interface); 193 194 void ParseExtendsInfo(AutoPtr<ASTInterfaceType> &interfaceType); 195 196 bool CheckExtendsName(AutoPtr<ASTInterfaceType> &interfaceType, const std::string &extendsName); 197 198 bool CheckExtendsVersion( 199 AutoPtr<ASTInterfaceType> &interfaceType, const std::string &extendsName, AutoPtr<AST> extendsAst); 200 201 bool CheckImportsVersion(AutoPtr<AST> extendsAst); 202 203 void SetInterfaceVersion(AutoPtr<ASTInterfaceType> &interfaceType); 204 IsPrimitiveType(Token token)205 inline static bool IsPrimitiveType(Token token) 206 { 207 return token.kind >= TokenType::BOOLEAN && token.kind <= TokenType::ASHMEM; 208 } 209 210 bool AddAst(const AutoPtr<AST> &ast); 211 212 void LogError(const std::string &message); 213 214 void LogError(const Token &token, const std::string &message); 215 216 void ShowError(); 217 218 bool PostProcess(); 219 220 bool CheckExistExtends(); 221 222 bool GetGenVersion(std::vector<size_t> &version, std::string &genPackageName); 223 224 void GetGenNamespace(AutoPtr<ASTNamespace> &ns); 225 226 void SortAstByName(AstMergeMap &mergeMap, StrAstMap &allAsts); 227 228 void MergeAsts(AstMergeMap &mergeMap); 229 230 void MergeAst(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 231 232 void MergeImport(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 233 234 void MergeInterfaceDef(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 235 236 void MergeTypes(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 237 238 void MergeSequenceableDef(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 239 240 void MergeTypeDefinitions(AutoPtr<AST> &targetAst, AutoPtr<AST> sourceAst); 241 242 void ModifyImport(StrAstMap &allAsts, std::string &genPackageName); 243 244 void ModifyPackageNameAndVersion(StrAstMap &allAsts, std::string &genPackageName, std::vector<size_t> genVersion); 245 246 void ModifyInterfaceNamespace(AutoPtr<ASTNamespace> &ns); 247 248 Lexer lexer_; 249 std::vector<std::string> errors_; 250 AutoPtr<AST> ast_; 251 StrAstMap allAsts_; 252 }; 253 } // namespace HDI 254 } // namespace OHOS 255 256 #endif // OHOS_HDI_PARSER_H