• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3#
4# Copyright (c) 2025 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    "ETSKeyofType",
92    "ETSNewArrayInstanceExpression",
93    "ETSNewMultiDimArrayInstanceExpression",
94    "ETSNewClassInstanceExpression",
95    "ETSImportDeclaration",
96    "ETSParameterExpression",
97    "ETSTuple",
98    "ETSModule",
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    # Cpp types
178    "char",
179    "short",
180    "int",
181    "long",
182    "long long",
183    "float",
184    "double",
185    "long double",
186    "bool",
187    "void",
188    # enums
189    "AstNodeFlags",
190    "BoxingUnboxingFlags",
191    "ModifierFlags",
192    "ScriptFunctionFlags",
193    "TSOperatorType",
194    "MappedOption",
195    "PrivateFieldKind",
196    # astType
197    "Type",
198    "ArrayType",
199    "AnyType",
200    "BigintLiteralType",
201    "NumberType",
202    "StringType",
203    "BooleanType",
204    "VoidType",
205    "NullType",
206    "UndefinedType",
207    "UnknownType",
208    "NeverType",
209    "UnionType",
210    "ObjectType",
211    "BigintType",
212    "BooleanLiteralType",
213    "NumberLiteralType",
214    "StringLiteralType",
215    "EnumType",
216    "EnumLiteralType",
217    "TypeParameter",
218    "TypeReference",
219    "ByteType",
220    "ShortType",
221    "IntType",
222    "LongType",
223    "FloatType",
224    "DoubleType",
225    "CharType",
226    "ETSBooleanType",
227    "ETSVoidType",
228    "ETSNullType",
229    "ETSUndefinedType",
230    "ETSFunctionType",
231    "ETSObjectType",
232    "ETSArrayType",
233    "ETSUnionType",
234    "NonPrimitiveType",
235    "WildcardType",
236    "ETSTypeParameter",
237    "ETSNonNullishType",
238    "ETSEnumType",
239    "ETSStringEnumType",
240    "ETSExtensionFuncHelperType",
241    "ETSTupleType",
242    "FunctionType",
243    "TupleType",
244    "ObjectLiteralType",
245    "InterfaceType",
246    # Variable
247    "Variable",
248    "LocalVariable",
249    "GlobalVariable",
250    "ModuleVariable",
251    "EnumVariable",
252    "NamespaceVariable",
253    "ImportEqualsVariable",
254    "EnumLiteralVariable",
255    # others
256    "StringView",
257    "ArenaAllocator",
258    "Checker",
259    "string",
260]
261
262all_types_supported.extend(ast_nodes_supported)
263
264no_gen_keywords = {
265    "postfix": [
266        "= delete",
267        "override",
268    ],
269    "name_starts_with": [
270        "~",
271    ],
272    "return_type": [
273        "ETSChecker",
274        "ArenaAllocator",
275        "Allocator",
276    ],
277}
278
279
280def is_method_supported(function: dict) -> bool:
281    if "args" in function:
282        for arg in function["args"]:
283            if "type" not in arg or "name" not in arg["type"]:
284                raise RuntimeError(""" + arg + "",  "" + function + """)
285            if arg["type"]["name"] not in all_types_supported:
286                return False
287    if "return_type" in function:
288        if "name" in function["return_type"]:
289            if function["return_type"]["name"] not in all_types_supported:
290                return False
291
292    return True
293
294
295def need_to_gen(function: dict) -> bool:
296    if "postfix" in function:
297        for ban in no_gen_keywords["postfix"]:  # CC-OFF(G.TYP.07) dict key exist
298            if function["postfix"].find(ban) != -1:
299                return False
300    for name_start in no_gen_keywords["name_starts_with"]:  # CC-OFF(G.TYP.07) dict key exist
301        if function["name"].startswith(name_start):
302            return False
303    if "return_type" in function:
304        for ban in no_gen_keywords["return_type"]:  # CC-OFF(G.TYP.07) dict key exist
305            if (
306                "name" in function["return_type"]
307                and function["return_type"]["name"].find(ban) != -1
308            ):
309                return False
310    return True
311