• 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
17from __future__ import absolute_import
18
19import file_parser
20import system_util
21
22# pylint:disable=huawei-redefined-outer-name
23
24# ------------------------------------------------------------------------------------------------------#
25
26def get_copyright():
27  content = \
28"""/*
29 * Copyright (c) $YEAR$ Huawei Device Co., Ltd.
30 * Licensed under the Apache License, Version 2.0 (the "License");
31 * you may not use this file except in compliance with the License.
32 * You may obtain a copy of the License at
33 *
34 *     http://www.apache.org/licenses/LICENSE-2.0
35 *
36 * Unless required by applicable law or agreed to in writing, software
37 * distributed under the License is distributed on an "AS IS" BASIS,
38 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39 * See the License for the specific language governing permissions and
40 * limitations under the License.
41 */
42"""
43
44  # add the copyright year
45  return content.replace('$YEAR$', system_util.get_year())
46
47# ------------------------------------------------------------------------------------------------------#
48
49def make_def_file(cls, class_name):
50  directory = cls.get_file_directory()
51
52  def_name = ''
53  if not directory is None:
54    def_name += directory + '_'
55
56  def_name += file_parser.get_capi_name(class_name, False)
57  return def_name
58
59# ------------------------------------------------------------------------------------------------------#
60
61def get_base_name(ident, header, class_name):
62  base_name = header.get_base_class_name(class_name)
63  if base_name == 'ArkWebBaseScoped':
64    base_name = 'ArkWeb' + ident + 'Scoped'
65  else:
66    base_name = 'ArkWeb' + ident + 'RefCounted'
67
68  return base_name
69
70# ------------------------------------------------------------------------------------------------------#
71
72def make_include_file(cls, ident, header, dir_name, class_name):
73  # include the headers for this class
74  content = '#include "' + dir_name + '/capi/' + cls.get_capi_file_name() + '"\n' \
75            '#include "' + dir_name + '/include/' + cls.get_file_name() + '"\n'
76
77  base_name = header.get_base_class_name(class_name)
78  if base_name == 'ArkWebBaseScoped':
79    base_file = ident.lower() + '_scoped.h'
80  else:
81    base_file = ident.lower() + '_ref_counted.h'
82
83  content += '#include "base/' + ident.lower() + '/ark_web_' + base_file + '"\n'
84  return {'content': content, 'base_name': get_base_name(ident, header, class_name)}
85
86# ------------------------------------------------------------------------------------------------------#
87
88def make_class_define(cls, ident, base_class, class_name, funcs_body):
89  capi_name = cls.get_capi_name()
90  content = 'class ' + class_name + ident + '\n' + \
91            '    : public ' + base_class + '<' + class_name + ident + ', ' + class_name + ', ' + capi_name + '> {\n' + \
92            '  public:\n' + \
93            '    ' + class_name + ident + '();\n' + \
94            '    virtual ~' + class_name + ident + '();\n\n'
95
96  if len(funcs_body) != 0:
97    content += funcs_body
98
99  content += '};\n'
100  return content
101
102# ------------------------------------------------------------------------------------------------------#
103
104def make_wrapper_type(class_name, parent_sig):
105  content = 'template<>\n' + \
106            'ArkWebBridgeType ' + parent_sig + '\n    ::kBridgeType = ' + \
107            file_parser.get_wrapper_type_enum(class_name) + ';'
108  return content
109
110# ------------------------------------------------------------------------------------------------------#
111
112def get_derived_classes(cls, header):
113  # identify all classes that derive form class
114  derived_classes = []
115  class_name = cls.get_name()
116  all_classes = header.get_classes()
117  for cur_cls in all_classes:
118    if cur_cls.get_name() == class_name:
119      continue
120    if cur_cls.has_parent(class_name):
121      derived_classes.append(cur_cls.get_name())
122
123  derived_classes = sorted(derived_classes)
124  return derived_classes
125
126# ------------------------------------------------------------------------------------------------------#
127
128def get_func_name_list(funcs):
129  name_list = []
130  for func in funcs:
131    name_list.append(func.get_capi_name())
132  return name_list
133
134# ------------------------------------------------------------------------------------------------------#
135
136def get_func_name_count(name, old_list, new_list):
137  if old_list.count(name) != 1:
138    new_list.append(name)
139  return new_list
140
141# ------------------------------------------------------------------------------------------------------#
142def get_func_invalid_info(name, func):
143  invalid = []
144  args = func.get_arguments()
145  for arg in args:
146    if arg.get_arg_type() == 'invalid':
147      invalid.append(arg.get_name())
148
149  retval = func.get_retval()
150  retval_type = retval.get_retval_type()
151  if retval_type == 'invalid':
152    invalid.append('(return value)')
153
154  if len(invalid) == 0:
155    return ''
156
157  file_parser.notify(name + ' could not be autogenerated')
158  # code could not be auto-generated
159  result = '\n  // BEGIN DELETE BEFORE MODIFYING'
160  result += '\n  // COULD NOT IMPLEMENT DUE TO: ' + ', '.join(invalid)
161  result += '\n  #pragma message("Warning: " __FILE__ ": ' + name + ' is not implemented")'
162  result += '\n  // END DELETE BEFORE MODIFYING'
163  result += '\n}\n\n'
164  return result
165