- Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathinstantiations.py
462 lines (341 loc) · 16.5 KB
/
instantiations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c) 2024 Phil Thompson <phil@riverbankcomputing.com>
fromcopyimportcopy
from .scoped_nameimportScopedName
from .specificationimport (Argument, ArgumentType, FunctionCall,
IfaceFileType, KwArgs, Signature, TypeHints, Value, ValueType)
from .templatesimport (template_code, template_code_blocks,
template_expansions, template_string)
from .utilsimportappend_iface_file, cached_name, normalised_scoped_name
definstantiate_class(p, symbol, fq_cpp_name, tmpl_names, proto_class,
template, py_name, no_type_name, docstring, pm):
""" Instantiate a class template. """
# Create the expansions.
expansions=template_expansions(tmpl_names, template.types)
# Add a mapping from the template name to the instantiated name.
expansions[str(proto_class.iface_file.fq_cpp_name)] =str(fq_cpp_name)
# Create the new class starting with a shallow copy.
i_class=copy(proto_class)
ifdocstringisnotNone:
i_class.docstring=docstring
i_class.mro= []
i_class.virtual_overloads= []
i_class.visible_members= []
i_class.py_name=cached_name(pm.spec, py_name)
i_class.template=template
i_class.no_type_name=no_type_name
# Handle the interface file.
i_class.iface_file=pm.find_iface_file(p, symbol, fq_cpp_name,
IfaceFileType.CLASS)
i_class.iface_file.module=pm.module_state.module
used=i_class.iface_file.used
i_class.iface_file.type_header_code=template_code_blocks(pm.spec, used,
proto_class.iface_file.type_header_code, expansions)
# Make a copy of the used list and add the enclosing scope.
foriface_fileinproto_class.iface_file.used:
append_iface_file(i_class.iface_file.used, iface_file)
# Include any scope header code.
i_class.scope=pm.scope
ifi_class.scopeisnotNone:
i_class.iface_file.type_header_code.extend(
i_class.scope.iface_file.type_header_code)
ifpm.in_main_module:
i_class.iface_file.cpp_name.used=True
i_class.py_name.used=True
# Handle any type hints.
ifproto_class.type_hintsisnotNone:
i_class.type_hints=instantiate_type_hints(pm.spec,
proto_class.type_hints, expansions)
# Handle any flagged enums.
ifproto_class.pyqt_flags_enumsisnotNone:
i_class.pyqt_flags_enums= [template_string(s, expansions)
forsinproto_class.pyqt_flags_enums]
# Handle the super-classes.
i_class.superclasses= []
forsuperclassinproto_class.superclasses:
superclass_name=superclass.iface_file.fq_cpp_name
# Don't try and expand defined or scoped classes.
ifsuperclass.iface_file.moduleisnotNoneornotsuperclass_name.is_simple:
i_class.superclasses.append(superclass)
continue
fora, arginenumerate(tmpl_names.args):
ifsuperclass_name.base_name==arg.definition.base_name:
tmpl_arg=template.types.args[a]
iftmpl_arg.typeisArgumentType.DEFINED:
i_superclass=pm.find_class(p, symbol,
IfaceFileType.CLASS, tmpl_arg.definition)
eliftmpl_arg.typeisArgumentType.CLASS:
i_superclass=tmpl_arg.definition
else:
pm.parser_error(p, symbol,
"template argument '{0}' must expand to a class".format(superclass_name))
i_superclass=superclass
i_class.superclasses.append(i_superclass)
# Handle the enums.
_instantiate_enums(tmpl_names, proto_class, template, i_class, expansions,
pm)
# Handle the variables.
_instantiate_vars(tmpl_names, proto_class, template, i_class, expansions,
pm)
# Handle the typedefs.
_instantiate_typedefs(p, symbol, tmpl_names, proto_class, template,
i_class, expansions, pm)
# Handle the ctors.
i_class.ctors=_instantiate_ctors(tmpl_names, proto_class, template,
i_class, expansions, pm)
# Handle the methods.
i_class.members=_instantiate_methods(proto_class.members,
i_class.iface_file.module, pm)
i_class.overloads=_instantiate_overloads(proto_class.overloads,
proto_class.members, i_class.members, tmpl_names, proto_class,
template, i_class, expansions, pm)
# Handle the remaining handwritten code.
i_class.bi_get_buffer_code=template_code(pm.spec, used,
proto_class.bi_get_buffer_code, expansions)
i_class.bi_release_buffer_code=template_code(pm.spec, used,
proto_class.bi_release_buffer_code, expansions)
i_class.convert_from_type_code=template_code(pm.spec, used,
proto_class.convert_from_type_code, expansions)
i_class.convert_to_subclass_code=template_code(pm.spec, used,
proto_class.convert_to_subclass_code, expansions)
i_class.convert_to_type_code=template_code(pm.spec, used,
proto_class.convert_to_type_code, expansions)
i_class.dealloc_code=template_code_blocks(pm.spec, used,
proto_class.dealloc_code, expansions)
i_class.dtor_virtual_catcher_code=template_code(pm.spec, used,
proto_class.dtor_virtual_catcher_code, expansions)
i_class.finalisation_code=template_code(pm.spec, used,
proto_class.finalisation_code, expansions)
i_class.gc_clear_code=template_code(pm.spec, used,
proto_class.gc_clear_code, expansions)
i_class.gc_traverse_code=template_code(pm.spec, used,
proto_class.gc_traverse_code, expansions)
i_class.instance_code=template_code(pm.spec, used,
proto_class.instance_code, expansions)
i_class.pickle_code=template_code(pm.spec, used, proto_class.pickle_code,
expansions)
i_class.type_code=template_code_blocks(pm.spec, used,
proto_class.type_code, expansions)
i_class.type_hint_code=template_code(pm.spec, used,
proto_class.type_hint_code, expansions)
pm.spec.classes.insert(0, i_class)
def_instantiate_argument(proto_arg, proto_class, tmpl_names, template,
i_class, expansions, pm):
""" Return an instantiated Argument object. """
# Start with a shallow copy.
i_arg=copy(proto_arg)
# Descend into any sub-templates.
ifproto_arg.typeisArgumentType.TEMPLATE:
proto_template=proto_arg.definition
i_template=copy(proto_template)
i_template.types=_instantiate_signature(proto_template.types,
proto_class, tmpl_names, template, i_class, expansions, pm)
i_arg.definition=i_template
# Handle any default value.
ifproto_arg.default_valueisnotNone:
i_arg.default_value= [_instantiate_value(v, expansions)
forvinproto_arg.default_value]
# Handle any type hints.
ifproto_arg.type_hintsisnotNone:
i_arg.type_hints=instantiate_type_hints(pm.spec,
proto_arg.type_hints, expansions)
# Handle arguments that are unscoped names.
ifproto_arg.typeisArgumentType.DEFINEDandproto_arg.definition.is_simple:
name=proto_arg.definition.base_name
fora, arginenumerate(tmpl_names.args):
ifname==arg.definition.base_name:
tad=template.types.args[a]
i_arg.type=tad.type
i_arg.definition=tad.definition
# We take the constrained flag from the real type.
i_arg.is_constrained=tad.is_constrained
break
else:
# Handle the class name itself.
ifname==proto_class.iface_file.fq_cpp_name.base_name:
i_arg.type=ArgumentType.CLASS
i_arg.definition=i_class
i_arg.original_type=None
returni_arg
def_instantiate_ctors(tmpl_names, proto_class, template, i_class, expansions,
pm):
""" Return a list of the instantiated ctors of a template class. """
i_ctors= []
used=i_class.iface_file.used
forproto_ctorinproto_class.ctors:
# Start with a shallow copy.
i_ctor=copy(proto_ctor)
i_ctor.py_signature=_instantiate_signature(proto_ctor.py_signature,
proto_class, tmpl_names, template, i_class, expansions, pm,
kw_args=proto_ctor.kw_args)
ifproto_ctor.cpp_signatureisproto_ctor.py_signature:
i_ctor.cpp_signature=i_ctor.py_signature
else:
i_ctor.cpp_signature=_instantiate_signature(
proto_ctor.cpp_signature.cpp_signature, proto_class,
tmpl_names, template, i_class, expansions, pm)
i_ctor.method_code=template_code(pm.spec, used,
proto_ctor.method_code, expansions)
i_ctor.premethod_code=template_code(pm.spec, used,
proto_ctor.premethod_code, expansions)
# Handle the default ctor.
ifproto_class.default_ctorisproto_ctor:
i_class.default_ctor=i_ctor
i_ctors.append(i_ctor)
returni_ctors
def_instantiate_enums(tmpl_names, proto_class, template, i_class, expansions,
pm):
""" Instantiate the enums for a template class. """
forproto_enuminlist(pm.spec.enums):
ifproto_enum.scopeisnotproto_class:
continue
# Start with a shallow copy.
i_enum=copy(proto_enum)
ifproto_enum.fq_cpp_nameisnotNone:
i_enum.fq_cpp_name=normalised_scoped_name(proto_enum.fq_cpp_name,
i_class)
i_enum.cached_fq_cpp_name=cached_name(pm.spec,
str(i_enum.fq_cpp_name))
ifpm.in_main_module:
ifi_enum.py_nameisnotNone:
i_enum.py_name=True
ifi_enum.cached_fq_cpp_nameisnotNone:
i_enum.cached_fq_cpp_name.used=True
i_enum.scope=i_class
i_enum.module=i_class.iface_file.module
i_enum.members= []
forproto_memberinproto_enum.members:
# Start with a shallow copy.
w_member=copy(proto_member)
w_member.scope=i_enum
i_enum.members.append(w_member)
pm.spec.enums.insert(0, i_enum)
def_instantiate_methods(proto_methods, target_module, pm):
""" Return a list of the instantiated methods of a template class or enum.
"""
i_methods= []
forproto_methodinproto_methods:
# Start with a shallow copy.
i_method=copy(proto_method)
i_method.module=target_module
ifpm.in_main_module:
i_method.py_name.used=True
i_methods.append(i_method)
returni_methods
def_instantiate_overloads(proto_overloads, proto_methods, i_methods,
tmpl_names, proto_class, template, i_class, expansions, pm):
""" Return a list of the instantiated overloads of a template class or
enum.
"""
i_overloads= []
used=i_class.iface_file.used
forproto_overloadinproto_overloads:
# Start with a shallow copy.
i_overload=copy(proto_overload)
fori_method, proto_methodinzip(i_methods, proto_methods):
ifproto_overload.commonisproto_method:
i_overload.common=i_method
break
i_overload.py_signature=_instantiate_signature(
proto_overload.py_signature, proto_class, tmpl_names, template,
i_class, expansions, pm, kw_args=proto_overload.kw_args)
ifproto_overload.cpp_signatureisproto_overload.py_signature:
i_overload.cpp_signature=i_overload.py_signature
else:
i_overload.cpp_signature=_instantiate_signature(
proto_overload.cpp_signature.cpp_signature, proto_class,
tmpl_names, template, i_class, expansions, pm)
i_overload.method_code=template_code(pm.spec, used,
proto_overload.method_code, expansions)
i_overload.premethod_code=template_code(pm.spec, used,
proto_overload.premethod_code, expansions)
i_overload.virtual_call_code=template_code(pm.spec, used,
proto_overload.virtual_call_code, expansions)
i_overload.virtual_catcher_code=template_code(pm.spec, used,
proto_overload.virtual_catcher_code, expansions)
i_overloads.append(i_overload)
returni_overloads
def_instantiate_signature(proto_signature, proto_class, tmpl_names, template,
i_class, expansions, pm, kw_args=KwArgs.NONE):
""" Return an instantiated Signature object. """
i_signature=Signature()
forproto_arginproto_signature.args:
i_arg=_instantiate_argument(proto_arg, proto_class, tmpl_names,
template, i_class, expansions, pm)
i_signature.args.append(i_arg)
# Make sure we have the name of any keyword argument.
ifpm.in_main_moduleandi_arg.nameisnotNone:
ifkw_argsisKwArgs.ALLor (kw_argsisKwArgs.OPTIONALandi_arg.default_valueisnotNone):
i_arg.name.used=True
ifproto_signature.resultisnotNone:
i_signature.result=_instantiate_argument(proto_signature.result,
proto_class, tmpl_names, template, i_class, expansions, pm)
returni_signature
definstantiate_type_hints(spec, proto_type_hints, expansions):
""" Return an instantiated TypeHints object. """
ifproto_type_hints.hint_inisnotNone:
hint_in=template_string(proto_type_hints.hint_in, expansions,
scope_replacement='.')
else:
hint_in=None
ifproto_type_hints.hint_outisnotNone:
hint_out=template_string(proto_type_hints.hint_out, expansions,
scope_replacement='.')
else:
hint_out=None
returnTypeHints(hint_in=hint_in, hint_out=hint_out,
default_value=proto_type_hints.default_value)
def_instantiate_typedefs(p, symbol, tmpl_names, proto_class, template,
i_class, expansions, pm):
""" Instantiate the typedefs of a template class. """
forproto_typedefinpm.spec.typedefs:
ifproto_typedef.scopeisnotproto_class:
continue
# Start with a shallow copy.
i_typedef=copy(proto_typedef)
i_typedef.fq_cpp_name=normalised_scoped_name(
proto_typedef.fq_cpp_name, i_class)
i_typedef.scope=i_class
i_typedef.module=i_class.iface_file.module
i_typedef.type=_instantiate_argument(proto_typedef.type, proto_class,
tmpl_names, template, i_class, expansions, pm)
pm.add_typedef(p, symbol, i_typedef)
def_instantiate_value(proto_value, expansions):
""" Return an instantiated Value object. """
# We only handle the subset where the value is an function call, ie. a
# template ctor.
i_value=proto_value
ifproto_value.value_typeisValueType.FCALLandproto_value.value.result.typeisArgumentType.DEFINED:
proto_name=proto_value.value.result.definition
ifproto_name.is_simple:
i_name=ScopedName.parse(
template_string(proto_name.base_name, expansions))
i_result=Argument(type=ArgumentType.DEFINED, definition=i_name)
i_fcall=FunctionCall(result=i_result,
args=proto_value.value.args)
i_value=Value(ValueType.FCALL, value=i_fcall)
returni_value
def_instantiate_vars(tmpl_names, proto_class, template, i_class, expansions,
pm):
""" Instantiate the enums for a template class. """
used=i_class.iface_file.used
forproto_varinpm.spec.variables:
ifproto_var.scopeisnotproto_class:
continue
# Start with a shallow copy.
i_var=copy(proto_var)
ifpm.in_main_module:
i_var.py_name.used=True
i_var.fq_cpp_name=normalised_scoped_name(proto_var.fq_cpp_name,
i_class)
i_var.scope=i_class
i_var.module=i_class.iface_file.module
i_var.type=_instantiate_argument(proto_var.type, proto_class,
tmpl_names, template, i_class, expansions, pm)
i_var.access_code=template_code(pm.spec, used, proto_var.access_code,
expansions)
i_var.get_code=template_code(pm.spec, used, proto_var.get_code,
expansions)
i_var.set_code=template_code(pm.spec, used, proto_var.set_code,
expansions)
pm.spec.variables.append(i_var)