Debian Codegraph @

build-aux/qmi-codegen/Container.py

raw
1 # -*- Mode: python; tab-width: 4; indent-tabs-mode: nil -*-
2 #
3 # This program is free software; you can redistribute it and/or modify it under
4 # the terms of the GNU Lesser General Public License as published by the Free
5 # Software Foundation; either version 2 of the License, or (at your option) any
6 # later version.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Lesser General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc., 51
15 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 #
17 # Copyright (C) 2012 Lanedo GmbH
18 # Copyright (C) 2012-2022 Aleksander Morgado <[email protected]>
19 #
20
21 import string
22
23 import utils
24 from FieldResult import FieldResult
25 from Field import Field
26
27 """
28 The Container class takes care of handling collections of Input or
29 Output fields
30 """
31 class Container:
32
33 """
34 Constructor
35 """
36 def __init__(self, service, prefix, container_type, dictionary, common_objects_dictionary, static, since, compat):
37 # The current QMI service
38 self.service = service
39 # The field container prefix usually contains the name of the Message,
40 # e.g. "Qmi Message Ctl Something"
41 self.prefix = prefix
42
43 # We may have 'Input' or 'Output' containers
44 if container_type == 'Input':
45 self.readonly = False
46 elif container_type == 'Output':
47 self.readonly = True
48 else:
49 raise ValueError('Cannot handle container type \'%s\'' % container_type)
50
51 self.name = container_type
52
53 self.static = static
54 self.since = since
55 self.compat = compat
56
57 # Create the composed full name (prefix + name),
58 # e.g. "Qmi Message Ctl Something Output"
59 self.fullname = self.prefix + ' ' + self.name
60
61 self.fields = None
62 if dictionary is not None:
63 self.fields = []
64
65 new_dict = []
66 # First, look for references to common types
67 for field_dictionary in dictionary:
68 if 'common-ref' in field_dictionary:
69 for common in common_objects_dictionary:
70 if common['type'] == 'TLV' and \
71 common['common-ref'] == field_dictionary['common-ref']:
72 # Replace the reference with a copy of the common dictionary
73 # If the source reference has prerequisites, add them to the copy
74 copy = dict(common)
75 if 'prerequisites' in field_dictionary:
76 copy['prerequisites'] = field_dictionary['prerequisites']
77
78 # Prefer message's TLV ID and name over the common-ref's TLV ID
79 # and name; some QMI messages use the same struct with different
80 # IDs and names (like NAS Get/Set Preferred Networks).
81 if 'id' in field_dictionary:
82 copy['id'] = field_dictionary['id']
83 if 'name' in field_dictionary:
84 copy['name'] = field_dictionary['name']
85
86 # If an explicit 'since' is given in the field, prefer it over any other one
87 if 'since' in field_dictionary:
88 copy['since'] = field_dictionary['since']
89 # If the common type does not have any explicit 'since', take the one from the message
90 # If the common type has a 'since', take it only if it is newer than the one from the message
91 elif not 'since' in copy or utils.version_compare(copy['since'],self.since) > 0:
92 copy['since'] = self.since
93
94 new_dict.append(copy)
95 break
96 else:
97 raise RuntimeError('Common type \'%s\' not found' % field_dictionary['name'])
98 else:
99 new_dict.append(field_dictionary)
100 dictionary = new_dict
101 # We need to sort the fields, so that the ones with prerequisites are
102 # include after the prerequisites themselves. Note: we don't currently
103 # support complex setups yet.
104 sorted_dictionary = []
105 for field_dictionary in dictionary:
106 if 'prerequisites' in field_dictionary:
107 sorted_dictionary.append(field_dictionary)
108 else:
109 sorted_dictionary.insert(0, field_dictionary)
110
111 # Then, really parse each field
112 for field_dictionary in sorted_dictionary:
113 if field_dictionary['type'] == 'TLV':
114 if field_dictionary['format'] == 'sequence' and \
115 field_dictionary['name'] == 'Result':
116 self.fields.append(FieldResult(self.service, self.fullname, field_dictionary, common_objects_dictionary, container_type, static))
117 else:
118 self.fields.append(Field(self.service, self.fullname, field_dictionary, common_objects_dictionary, container_type, static))
119
120
121 """
122 Emit enumeration of TLVs in the container
123 """
124 def __emit_tlv_ids_enum(self, f):
125 if self.fields is None:
126 return
127
128 f.write('\n')
129 for tlv in self.fields:
130 translations = { 'enum_name' : tlv.id_enum_name,
131 'enum_value' : tlv.id }
132 template = (
133 '#define ${enum_name} ${enum_value}\n')
134 f.write(string.Template(template).substitute(translations))
135
136
137 """
138 Emit new container types
139 """
140 def __emit_types(self, hfile, cfile, translations):
141 translations['type_macro'] = 'QMI_TYPE_' + utils.remove_prefix(utils.build_underscore_uppercase_name(self.fullname), 'QMI_')
142 # Emit types header
143 template = '\n'
144 if not self.static and self.service != 'CTL':
145 template += (
146 '/**\n'
147 ' * ${camelcase}:\n'
148 ' *\n'
149 ' * The #${camelcase} structure contains private data and should only be accessed\n'
150 ' * using the provided API.\n'
151 ' *\n'
152 ' * Since: ${since}\n'
153 ' */\n')
154 template += (
155 'typedef struct _${camelcase} ${camelcase};\n'
156 '${static}GType ${underscore}_get_type (void) G_GNUC_CONST;\n'
157 '#define ${type_macro} (${underscore}_get_type ())\n')
158 if self.compat:
159 template += (
160 'G_GNUC_INTERNAL\n'
161 'gpointer ${underscore}_get_compat_context (${camelcase} *self);\n'
162 'G_GNUC_INTERNAL\n'
163 'void ${underscore}_set_compat_context (\n'
164 ' ${camelcase} *self,\n'
165 ' gpointer compat_context,\n'
166 ' GDestroyNotify compat_context_free);\n')
167 hfile.write(string.Template(template).substitute(translations))
168
169 # Emit types source
170 template = (
171 '\n'
172 'struct _${camelcase} {\n'
173 ' volatile gint ref_count;\n')
174 if self.compat:
175 template += (
176 '\n'
177 ' gpointer compat_context;\n'
178 ' GDestroyNotify compat_context_free;\n')
179 cfile.write(string.Template(template).substitute(translations))
180
181 if self.fields is not None:
182 for field in self.fields:
183 if field.variable is not None:
184 translations['field_variable_name'] = field.variable_name
185 translations['field_name'] = field.name
186 template = (
187 '\n'
188 ' /* ${field_name} */\n'
189 ' gboolean ${field_variable_name}_set;\n')
190 cfile.write(string.Template(template).substitute(translations))
191 field.emit_variable_declaration(cfile)
192
193 cfile.write(
194 '};\n')
195
196
197 """
198 Emit container handling core implementation
199 """
200 def __emit_core(self, hfile, cfile, translations):
201 # Emit container core header
202 template = '\n'
203 if not self.static and self.service != 'CTL':
204 template += (
205 '\n'
206 '/**\n'
207 ' * ${underscore}_ref:\n'
208 ' * @self: a #${camelcase}.\n'
209 ' *\n'
210 ' * Atomically increments the reference count of @self by one.\n'
211 ' *\n'
212 ' * Returns: the new reference to @self.\n'
213 ' *\n'
214 ' * Since: ${since}\n'
215 ' */\n')
216 template += (
217 '${static}${camelcase} *${underscore}_ref (${camelcase} *self);\n'
218 '\n')
219 if not self.static and self.service != 'CTL':
220 template += (
221 '/**\n'
222 ' * ${underscore}_unref:\n'
223 ' * @self: a #${camelcase}.\n'
224 ' *\n'
225 ' * Atomically decrements the reference count of @self by one.\n'
226 ' * If the reference count drops to 0, @self is completely disposed.\n'
227 ' *\n'
228 ' * Since: ${since}\n'
229 ' */\n')
230 template += (
231 '${static}void ${underscore}_unref (${camelcase} *self);\n'
232 'G_DEFINE_AUTOPTR_CLEANUP_FUNC (${camelcase}, ${underscore}_unref)\n')
233 if not self.readonly:
234 if not self.static:
235 template += (
236 '\n'
237 '/**\n'
238 ' * ${underscore}_new:\n'
239 ' *\n'
240 ' * Allocates a new #${camelcase}.\n'
241 ' *\n'
242 ' * Returns: the newly created #${camelcase}. The returned value should be freed with ${underscore}_unref().\n'
243 ' *\n'
244 ' * Since: ${since}\n'
245 ' */\n')
246 template += (
247 '${static}${camelcase} *${underscore}_new (void);\n')
248
249 if self.static:
250 cfile.write(string.Template(template).substitute(translations))
251 else:
252 hfile.write(string.Template(template).substitute(translations))
253
254 # Emit container core source
255 template = (
256 '\n'
257 '${static}GType\n'
258 '${underscore}_get_type (void)\n'
259 '{\n'
260 ' static gsize g_define_type_id_initialized = 0;\n'
261 '\n'
262 ' if (g_once_init_enter (&g_define_type_id_initialized)) {\n'
263 ' GType g_define_type_id =\n'
264 ' g_boxed_type_register_static (g_intern_static_string ("${camelcase}"),\n'
265 ' (GBoxedCopyFunc) ${underscore}_ref,\n'
266 ' (GBoxedFreeFunc) ${underscore}_unref);\n'
267 '\n'
268 ' g_once_init_leave (&g_define_type_id_initialized, g_define_type_id);\n'
269 ' }\n'
270 '\n'
271 ' return g_define_type_id_initialized;\n'
272 '}\n'
273 '\n'
274 '${static}${camelcase} *\n'
275 '${underscore}_ref (${camelcase} *self)\n'
276 '{\n'
277 ' g_return_val_if_fail (self != NULL, NULL);\n'
278 '\n'
279 ' g_atomic_int_inc (&self->ref_count);\n'
280 ' return self;\n'
281 '}\n'
282 '\n'
283 '${static}void\n'
284 '${underscore}_unref (${camelcase} *self)\n'
285 '{\n'
286 ' g_return_if_fail (self != NULL);\n'
287 '\n'
288 ' if (g_atomic_int_dec_and_test (&self->ref_count)) {\n')
289
290 if self.compat:
291 template += (
292 ' if (self->compat_context && self->compat_context_free)\n'
293 ' self->compat_context_free (self->compat_context);\n')
294
295 if self.fields is not None:
296 for field in self.fields:
297 if field.variable is not None and field.variable.needs_dispose:
298 template += field.variable.build_dispose(' ', 'self->' + field.variable_name)
299 if field.variable.needs_compat_gir and self.service != 'CTL':
300 template += field.variable.build_dispose_gir(' ', 'self->' + field.variable_name)
301
302 template += (
303 ' g_slice_free (${camelcase}, self);\n'
304 ' }\n'
305 '}\n')
306
307 if self.compat:
308 template += (
309 'gpointer\n'
310 '${underscore}_get_compat_context (${camelcase} *self)\n'
311 '{\n'
312 ' g_return_val_if_fail (self != NULL, NULL);\n'
313 '\n'
314 ' return self->compat_context;\n'
315 '}\n'
316 '\n'
317 'void\n'
318 '${underscore}_set_compat_context (\n'
319 ' ${camelcase} *self,\n'
320 ' gpointer compat_context,\n'
321 ' GDestroyNotify compat_context_free)\n'
322 '{\n'
323 ' g_return_if_fail (self != NULL);\n'
324 '\n'
325 ' if (self->compat_context && self->compat_context_free)\n'
326 ' self->compat_context_free (self->compat_context);\n'
327 '\n'
328 ' self->compat_context = compat_context;\n'
329 ' self->compat_context_free = compat_context_free;\n'
330 '}\n')
331 cfile.write(string.Template(template).substitute(translations))
332
333 # _new() is only generated if the container is not readonly
334 if self.readonly:
335 return
336
337 template = (
338 '\n'
339 '${static}${camelcase} *\n'
340 '${underscore}_new (void)\n'
341 '{\n'
342 ' ${camelcase} *self;\n'
343 '\n'
344 ' self = g_slice_new0 (${camelcase});\n'
345 ' self->ref_count = 1;\n'
346 ' return self;\n'
347 '}\n')
348 cfile.write(string.Template(template).substitute(translations))
349
350
351 """
352 Emit container implementation
353 """
354 def emit(self, hfile, cfile):
355 translations = { 'name' : self.name,
356 'camelcase' : utils.build_camelcase_name (self.fullname),
357 'underscore' : utils.build_underscore_name (self.fullname),
358 'since' : self.since,
359 'static' : 'static ' if self.static else '' }
360
361 auxfile = cfile if self.static else hfile
362
363 if self.fields is None:
364 template = ('\n'
365 '/* Note: no fields in the ${name} container */\n')
366 auxfile.write(string.Template(template).substitute(translations))
367 cfile.write(string.Template(template).substitute(translations))
368 return
369
370 # Emit the container and field types
371 # Emit field getter/setter
372 if self.fields is not None:
373 for field in self.fields:
374 field.emit_types(auxfile, cfile)
375 self.__emit_types(auxfile, cfile, translations)
376
377 # Emit TLV enums
378 self.__emit_tlv_ids_enum(cfile)
379
380 # Emit fields
381 if self.fields is not None:
382 for field in self.fields:
383 field.emit_getter(auxfile, cfile)
384 if not self.readonly:
385 field.emit_setter(auxfile, cfile)
386
387 # Emit the container core
388 self.__emit_core(auxfile, cfile, translations)
389
390
391 """
392 Add sections
393 """
394 def add_sections(self, sections):
395 if self.fields is None:
396 return
397
398 translations = { 'name' : self.name,
399 'camelcase' : utils.build_camelcase_name (self.fullname),
400 'underscore' : utils.build_underscore_name (self.fullname),
401 'type_macro' : 'QMI_TYPE_' + utils.remove_prefix(utils.build_underscore_uppercase_name(self.fullname), 'QMI_') }
402
403 # Standard
404 template = (
405 '${underscore}_get_type\n'
406 '${type_macro}\n')
407 sections['standard'] += string.Template(template).substitute(translations)
408
409 # Private
410 if self.compat:
411 template = (
412 '${underscore}_get_compat_context\n'
413 '${underscore}_set_compat_context\n')
414 sections['private'] += string.Template(template).substitute(translations)
415
416 # Public types
417 template = (
418 '${camelcase}\n')
419 sections['public-types'] += string.Template(template).substitute(translations)
420
421 # Public methods
422 template = '<SUBSECTION ${camelcase}Methods>\n'
423 if not self.readonly:
424 template += (
425 '${underscore}_new\n')
426 template += (
427 '${underscore}_ref\n'
428 '${underscore}_unref\n')
429 sections['public-methods'] += string.Template(template).substitute(translations)
430
431 for field in self.fields:
432 field.add_sections(sections)