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