• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3#
4# Copyright (c) 2024 Huawei Device Co., Ltd.
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
18"""Types, supported by generator"""
19
20ast_nodes_supported = [
21    "AstNode",
22    "ArrowFunctionExpression",
23    "AssertStatement",
24    "AwaitExpression",
25    "BigIntLiteral",
26    "BinaryExpression",
27    "BlockStatement",
28    "BooleanLiteral",
29    "BreakStatement",
30    "CallExpression",
31    "CatchClause",
32    "ChainExpression",
33    "CharLiteral",
34    "ClassDefinition",
35    "ClassDeclaration",
36    "ClassExpression",
37    "ClassProperty",
38    "ClassStaticBlock",
39    "ConditionalExpression",
40    "ContinueStatement",
41    "DebuggerStatement",
42    "Decorator",
43    "DirectEvalExpression",
44    "DoWhileStatement",
45    "EmptyStatement",
46    "ExportAllDeclaration",
47    "ExportDefaultDeclaration",
48    "ExportNamedDeclaration",
49    "ExportSpecifier",
50    "ExpressionStatement",
51    "ForInStatement",
52    "ForOfStatement",
53    "ForUpdateStatement",
54    "FunctionDeclaration",
55    "FunctionExpression",
56    "Identifier",
57    "IfStatement",
58    "ImportDeclaration",
59    "ImportExpression",
60    "ImportDefaultSpecifier",
61    "ImportNamespaceSpecifier",
62    "ImportSpecifier",
63    "LabelledStatement",
64    "MemberExpression",
65    "MetaProperty",
66    "MethodDefinition",
67    "NamedType",
68    "NewExpression",
69    "NullLiteral",
70    "UndefinedLiteral",
71    "NumberLiteral",
72    "OmittedExpression",
73    "PrefixAssertionExpression",
74    "Property",
75    "RegExpLiteral",
76    "ETSReExportDeclaration",
77    "ReturnStatement",
78    "ScriptFunction",
79    "SequenceExpression",
80    "StringLiteral",
81    "ETSNullType",
82    "ETSUndefinedType",
83    "ETSFunctionType",
84    "ETSWildcardType",
85    "ETSPrimitiveType",
86    "ETSPackageDeclaration",
87    "ETSClassLiteral",
88    "ETSTypeReference",
89    "ETSTypeReferencePart",
90    "ETSUnionType",
91    "ETSLaunchExpression",
92    "ETSNewArrayInstanceExpression",
93    "ETSNewMultiDimArrayInstanceExpression",
94    "ETSNewClassInstanceExpression",
95    "ETSImportDeclaration",
96    "ETSParameterExpression",
97    "ETSTuple",
98    "ETSScript",
99    "SuperExpression",
100    "ETSStructDeclaration",
101    "SwitchCaseStatement",
102    "SwitchStatement",
103    "TSEnumDeclaration",
104    "TSEnumMember",
105    "TSExternalModuleReference",
106    "TSNumberKeyword",
107    "TSAnyKeyword",
108    "TSStringKeyword",
109    "TSBooleanKeyword",
110    "TSVoidKeyword",
111    "TSUndefinedKeyword",
112    "TSUnknownKeyword",
113    "TSObjectKeyword",
114    "TSBigintKeyword",
115    "TSNeverKeyword",
116    "TSNonNullExpression",
117    "TSNullKeyword",
118    "TSArrayType",
119    "TSUnionType",
120    "TSTypeLiteral",
121    "TSPropertySignature",
122    "TSMethodSignature",
123    "TSSignatureDeclaration",
124    "TSParenthesizedType",
125    "TSLiteralType",
126    "TSInferType",
127    "TSConditionalType",
128    "TSImportType",
129    "TSIntersectionType",
130    "TSMappedType",
131    "TSModuleBlock",
132    "TSThisType",
133    "TSTypeOperator",
134    "TSTypeParameter",
135    "TSTypeParameterDeclaration",
136    "TSTypeParameterInstantiation",
137    "TSTypePredicate",
138    "TSParameterProperty",
139    "TSModuleDeclaration",
140    "TSImportEqualsDeclaration",
141    "TSFunctionType",
142    "TSConstructorType",
143    "TSTypeAliasDeclaration",
144    "TSTypeReference",
145    "TSQualifiedName",
146    "TSIndexedAccessType",
147    "TSInterfaceDeclaration",
148    "TSInterfaceBody",
149    "TSInterfaceHeritage",
150    "TSTupleType",
151    "TSNamedTupleMember",
152    "TSIndexSignature",
153    "TSTypeQuery",
154    "TSAsExpression",
155    "TSClassImplements",
156    "TSTypeAssertion",
157    "TaggedTemplateExpression",
158    "TemplateElement",
159    "TemplateLiteral",
160    "ThisExpression",
161    "TypeofExpression",
162    "ThrowStatement",
163    "TryStatement",
164    "UnaryExpression",
165    "UpdateExpression",
166    "VariableDeclaration",
167    "VariableDeclarator",
168    "WhileStatement",
169    "YieldExpression",
170    "OpaqueTypeNode",
171    "BlockExpression",
172    "Statement",
173    "Expression",
174]
175
176all_types_supported = [
177
178    # Cpp types
179    "char",
180    "short",
181    "int",
182    "long",
183    "long long",
184    "float",
185    "double",
186    "long double",
187    "bool",
188    "void",
189
190    # enums
191    "AstNodeFlags",
192    "BoxingUnboxingFlags",
193    "ModifierFlags",
194    "ScriptFunctionFlags",
195    "TSOperatorType",
196    "MappedOption",
197    "PrivateFieldKind",
198
199    # astType
200    "Type",
201    "ArrayType",
202    "AnyType",
203    "BigintLiteralType",
204    "NumberType",
205    "StringType",
206    "BooleanType",
207    "VoidType",
208    "NullType",
209    "UndefinedType",
210    "UnknownType",
211    "NeverType",
212    "UnionType",
213    "ObjectType",
214    "BigintType",
215    "BooleanLiteralType",
216    "NumberLiteralType",
217    "StringLiteralType",
218    "EnumType",
219    "EnumLiteralType",
220    "TypeParameter",
221    "TypeReference",
222    "ByteType",
223    "ShortType",
224    "IntType",
225    "LongType",
226    "FloatType",
227    "DoubleType",
228    "CharType",
229    "ETSBooleanType",
230    "ETSVoidType",
231    "ETSNullType",
232    "ETSUndefinedType",
233    "ETSFunctionType",
234    "ETSObjectType",
235    "ETSArrayType",
236    "ETSUnionType",
237    "NonPrimitiveType",
238    "WildcardType",
239    "ETSTypeParameter",
240    "ETSNonNullishType",
241    "ETSEnumType",
242    "ETSStringEnumType",
243    "ETSExtensionFuncHelperType",
244    "ETSTupleType",
245    "FunctionType",
246    "TupleType",
247    "ObjectLiteralType",
248    "InterfaceType",
249
250    # Variable
251    "Variable",
252    "LocalVariable",
253    "GlobalVariable",
254    "ModuleVariable",
255    "EnumVariable",
256    "NamespaceVariable",
257    "ImportEqualsVariable",
258    "EnumLiteralVariable",
259
260    # others
261    "StringView",
262    "ArenaAllocator",
263    "Checker",
264    "string",
265]
266
267all_types_supported.extend(ast_nodes_supported)
268
269no_gen_keywords = {
270    "postfix": [
271        "= delete",
272        "override",
273    ],
274    "name_starts_with": [
275        "~",
276    ],
277    "return_type": [
278        "ETSChecker",
279        "ArenaAllocator",
280        "Allocator",
281    ],
282}
283
284
285def is_method_supported(function: dict) -> bool:
286    if "args" in function:
287        for arg in function["args"]:
288            if "type" not in arg or "name" not in arg["type"]:
289                raise RuntimeError(""" + arg + "",  "" + function + """)
290            if arg["type"]["name"] not in all_types_supported:
291                return False
292    if "return_type" in function:
293        if "name" in function["return_type"]:
294            if function["return_type"]["name"] not in all_types_supported:
295                return False
296
297    return True
298
299
300def need_to_gen(function: dict) -> bool:
301    if "postfix" in function:
302        for ban in no_gen_keywords["postfix"]: # CC-OFF(G.TYP.07) dict key exist
303            if function["postfix"].find(ban) != -1:
304                return False
305    for name_start in no_gen_keywords["name_starts_with"]: # CC-OFF(G.TYP.07) dict key exist
306        if function["name"].startswith(name_start):
307            return False
308    if "return_type" in function:
309        for ban in no_gen_keywords["return_type"]: # CC-OFF(G.TYP.07) dict key exist
310            if "name" in function["return_type"] and function["return_type"]["name"].find(ban) != -1:
311                return False
312    return True
313