forked from chromiumembedded/cef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_cpptoc_impl.py
765 lines (656 loc) · 29.8 KB
/
make_cpptoc_impl.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
# reserved. Use of this source code is governed by a BSD-style license that
# can be found in the LICENSE file.
from __future__ import absolute_import
from cef_parser import *
def make_cpptoc_impl_proto(name, func, parts):
if isinstance(func, obj_function_virtual):
proto = parts['retval'] + ' CEF_CALLBACK'
else:
proto = 'CEF_EXPORT ' + parts['retval']
proto += ' ' + name + '(' + ', '.join(parts['args']) + ')'
return proto
def make_cpptoc_function_impl_existing(cls, name, func, impl, defined_names):
notify(name + ' has manual edits')
# retrieve the C API prototype parts
parts = func.get_capi_parts(defined_names, True)
changes = format_translation_changes(impl, parts)
if len(changes) > 0:
notify(name + ' prototype changed')
return make_cpptoc_impl_proto(
name, func, parts) + '{' + changes + impl['body'] + '\n}\n\n'
def make_cpptoc_function_impl_new(cls, name, func, defined_names, base_scoped):
# Special handling for the cef_shutdown global function.
is_cef_shutdown = name == 'cef_shutdown' and isinstance(
func.parent, obj_header)
# retrieve the C API prototype parts
parts = func.get_capi_parts(defined_names, True)
result = make_cpptoc_impl_proto(name, func, parts) + ' {'
if isinstance(func.parent, obj_class) and \
not func.parent.has_attrib('no_debugct_check') and \
not base_scoped:
result += '\n shutdown_checker::AssertNotShutdown();\n'
invalid = []
# retrieve the function arguments
args = func.get_arguments()
# determine the argument types
for arg in args:
if arg.get_arg_type() == 'invalid':
invalid.append(arg.get_name())
# retrieve the function return value
retval = func.get_retval()
retval_type = retval.get_retval_type()
if retval_type == 'invalid':
invalid.append('(return value)')
retval_default = ''
else:
retval_default = retval.get_retval_default(True)
if len(retval_default) > 0:
retval_default = ' ' + retval_default
if len(invalid) > 0:
notify(name + ' could not be autogenerated')
# code could not be auto-generated
result += '\n // BEGIN DELETE BEFORE MODIFYING'
result += '\n // AUTO-GENERATED CONTENT'
result += '\n // COULD NOT IMPLEMENT DUE TO: ' + ', '.join(invalid)
result += '\n #pragma message("Warning: " __FILE__ ": ' + name + ' is not implemented")'
result += '\n // END DELETE BEFORE MODIFYING'
result += '\n}\n\n'
return result
result += '\n // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING\n'
result_len = len(result)
optional = []
# parameter verification
if isinstance(func, obj_function_virtual):
result += '\n DCHECK(self);'\
'\n if (!self) {'\
'\n return'+retval_default+';'\
'\n }'
for arg in args:
arg_type = arg.get_arg_type()
arg_name = arg.get_type().get_name()
# skip optional params
optional_params = arg.parent.get_attrib_list('optional_param')
if not optional_params is None and arg_name in optional_params:
optional.append(arg_name)
continue
comment = '\n // Verify param: ' + arg_name + '; type: ' + arg_type
if arg_type == 'simple_byref' or arg_type == 'simple_byref_const' or \
arg_type == 'simple_byaddr' or arg_type == 'bool_byref' or arg_type == 'bool_byaddr' or \
arg_type == 'struct_byref_const' or arg_type == 'struct_byref' or \
arg_type == 'string_byref_const' or arg_type == 'string_byref' or \
arg_type == 'refptr_same' or arg_type == 'refptr_same_byref' or \
arg_type == 'refptr_diff' or arg_type == 'refptr_diff_byref' or \
arg_type == 'ownptr_same' or arg_type == 'ownptr_same_byref' or \
arg_type == 'ownptr_diff' or arg_type == 'ownptr_diff_byref' or \
arg_type == 'rawptr_same' or arg_type == 'rawptr_same_byref' or \
arg_type == 'rawptr_diff' or arg_type == 'rawptr_diff_byref' or \
arg_type == 'string_vec_byref' or arg_type == 'string_vec_byref_const' or \
arg_type == 'string_map_single_byref' or arg_type == 'string_map_single_byref_const' or \
arg_type == 'string_map_multi_byref' or arg_type == 'string_map_multi_byref_const':
result += comment+\
'\n DCHECK('+arg_name+');'\
'\n if (!'+arg_name+') {'\
'\n return'+retval_default+';'\
'\n }'
if arg_type == 'struct_byref_const' or arg_type == 'struct_byref':
result +=\
'\n if (!template_util::has_valid_size('+arg_name+')) {'\
'\n DCHECK(false) << "invalid '+arg_name+'->[base.]size";'\
'\n return'+retval_default+';'\
'\n }'
elif arg_type == 'simple_vec_byref' or arg_type == 'bool_vec_byref' or \
arg_type == 'refptr_vec_same_byref' or arg_type == 'refptr_vec_diff_byref' or \
arg_type == 'ownptr_vec_same_byref' or arg_type == 'ownptr_vec_diff_byref' or \
arg_type == 'rawptr_vec_same_byref' or arg_type == 'rawptr_vec_diff_byref':
result += comment+\
'\n DCHECK('+arg_name+'Count && (*'+arg_name+'Count == 0 || '+arg_name+'));'\
'\n if (!'+arg_name+'Count || (*'+arg_name+'Count > 0 && !'+arg_name+')) {'\
'\n return'+retval_default+';'\
'\n }'
elif arg_type == 'simple_vec_byref_const' or arg_type == 'bool_vec_byref_const' or \
arg_type == 'refptr_vec_same_byref_const' or arg_type == 'refptr_vec_diff_byref_const' or \
arg_type == 'ownptr_vec_same_byref_const' or arg_type == 'ownptr_vec_diff_byref_const' or \
arg_type == 'rawptr_vec_same_byref_const' or arg_type == 'rawptr_vec_diff_byref_const':
result += comment+\
'\n DCHECK('+arg_name+'Count == 0 || '+arg_name+');'\
'\n if ('+arg_name+'Count > 0 && !'+arg_name+') {'\
'\n return'+retval_default+';'\
'\n }'
# check index params
index_params = arg.parent.get_attrib_list('index_param')
if not index_params is None and arg_name in index_params:
result += comment+\
'\n DCHECK_GE('+arg_name+', 0);'\
'\n if ('+arg_name+' < 0) {'\
'\n return'+retval_default+';'\
'\n }'
if len(optional) > 0:
# Wrap the comment at 80 characters.
str = '\n // Unverified params: ' + optional[0]
for name in optional[1:]:
str += ','
if len(str) + len(name) + 1 > 80:
result += str
str = '\n //'
str += ' ' + name
result += str
if len(result) != result_len:
result += '\n'
result_len = len(result)
# parameter translation
params = []
for arg in args:
arg_type = arg.get_arg_type()
arg_name = arg.get_type().get_name()
comment = '\n // Translate param: ' + arg_name + '; type: ' + arg_type
if arg_type == 'simple_byval' or arg_type == 'simple_byaddr':
params.append(arg_name)
elif arg_type == 'simple_byref' or arg_type == 'simple_byref_const':
data_type = arg.get_type().get_type()
default = arg.get_type().get_result_simple_default()
result += comment+\
'\n '+data_type+' '+arg_name+'Val = '+arg_name+'?*'+arg_name+':'+default+';'
params.append(arg_name + 'Val')
elif arg_type == 'bool_byval':
params.append(arg_name + '?true:false')
elif arg_type == 'bool_byref' or arg_type == 'bool_byaddr':
result += comment+\
'\n bool '+arg_name+'Bool = ('+arg_name+' && *'+arg_name+')?true:false;'
if arg_type == 'bool_byref':
params.append(arg_name + 'Bool')
else:
params.append('&' + arg_name + 'Bool')
elif arg_type == 'struct_byref_const':
struct_type = arg.get_type().get_type()
result += comment+\
'\n '+struct_type+' '+arg_name+'Obj;'\
'\n if ('+arg_name+') {'\
'\n '+arg_name+'Obj.Set(*'+arg_name+', false);'\
'\n }'
params.append(arg_name + 'Obj')
elif arg_type == 'struct_byref':
struct_type = arg.get_type().get_type()
result += comment+\
'\n '+struct_type+' '+arg_name+'Obj;'\
'\n if ('+arg_name+') {'\
'\n '+arg_name+'Obj.AttachTo(*'+arg_name+');'\
'\n }'
params.append(arg_name + 'Obj')
elif arg_type == 'string_byref_const':
params.append('CefString(' + arg_name + ')')
elif arg_type == 'string_byref':
result += comment+\
'\n CefString '+arg_name+'Str('+arg_name+');'
params.append(arg_name + 'Str')
elif arg_type == 'refptr_same' or arg_type == 'refptr_diff':
ptr_class = arg.get_type().get_ptr_type()
if arg_type == 'refptr_same':
params.append(ptr_class + 'CppToC::Unwrap(' + arg_name + ')')
else:
params.append(ptr_class + 'CToCpp::Wrap(' + arg_name + ')')
elif arg_type == 'ownptr_same' or arg_type == 'rawptr_same':
ptr_class = arg.get_type().get_ptr_type()
if arg_type == 'ownptr_same':
params.append(ptr_class + 'CppToC::UnwrapOwn(' + arg_name + ')')
else:
params.append(ptr_class + 'CppToC::UnwrapRaw(' + arg_name + ')')
elif arg_type == 'ownptr_diff' or arg_type == 'rawptr_diff':
ptr_class = arg.get_type().get_ptr_type()
result += comment+\
'\n CefOwnPtr<'+ptr_class+'> '+arg_name+'Ptr('+ptr_class+'CToCpp::Wrap('+arg_name+'));'
if arg_type == 'ownptr_diff':
params.append('std::move(' + arg_name + 'Ptr)')
else:
params.append(arg_name + 'Ptr.get()')
elif arg_type == 'refptr_same_byref' or arg_type == 'refptr_diff_byref':
ptr_class = arg.get_type().get_ptr_type()
if arg_type == 'refptr_same_byref':
assign = ptr_class + 'CppToC::Unwrap(*' + arg_name + ')'
else:
assign = ptr_class + 'CToCpp::Wrap(*' + arg_name + ')'
result += comment+\
'\n CefRefPtr<'+ptr_class+'> '+arg_name+'Ptr;'\
'\n if ('+arg_name+' && *'+arg_name+') {'\
'\n '+arg_name+'Ptr = '+assign+';'\
'\n }'\
'\n '+ptr_class+'* '+arg_name+'Orig = '+arg_name+'Ptr.get();'
params.append(arg_name + 'Ptr')
elif arg_type == 'string_vec_byref' or arg_type == 'string_vec_byref_const':
result += comment+\
'\n std::vector<CefString> '+arg_name+'List;'\
'\n transfer_string_list_contents('+arg_name+', '+arg_name+'List);'
params.append(arg_name + 'List')
elif arg_type == 'string_map_single_byref' or arg_type == 'string_map_single_byref_const':
result += comment+\
'\n std::map<CefString, CefString> '+arg_name+'Map;'\
'\n transfer_string_map_contents('+arg_name+', '+arg_name+'Map);'
params.append(arg_name + 'Map')
elif arg_type == 'string_map_multi_byref' or arg_type == 'string_map_multi_byref_const':
result += comment+\
'\n std::multimap<CefString, CefString> '+arg_name+'Multimap;'\
'\n transfer_string_multimap_contents('+arg_name+', '+arg_name+'Multimap);'
params.append(arg_name + 'Multimap')
elif arg_type == 'simple_vec_byref' or arg_type == 'bool_vec_byref' or \
arg_type == 'refptr_vec_same_byref' or arg_type == 'refptr_vec_diff_byref':
vec_type = arg.get_type().get_vector_type()
if arg_type == 'simple_vec_byref':
assign = arg_name + '[i]'
elif arg_type == 'bool_vec_byref':
assign = arg_name + '[i]?true:false'
elif arg_type == 'refptr_vec_same_byref':
ptr_class = arg.get_type().get_ptr_type()
assign = ptr_class + 'CppToC::Unwrap(' + arg_name + '[i])'
elif arg_type == 'refptr_vec_diff_byref':
ptr_class = arg.get_type().get_ptr_type()
assign = ptr_class + 'CToCpp::Wrap(' + arg_name + '[i])'
result += comment+\
'\n std::vector<'+vec_type+' > '+arg_name+'List;'\
'\n if ('+arg_name+'Count && *'+arg_name+'Count > 0 && '+arg_name+') {'\
'\n for (size_t i = 0; i < *'+arg_name+'Count; ++i) {'\
'\n '+arg_name+'List.push_back('+assign+');'\
'\n }'\
'\n }'
params.append(arg_name + 'List')
elif arg_type == 'simple_vec_byref_const' or arg_type == 'bool_vec_byref_const' or \
arg_type == 'refptr_vec_same_byref_const' or arg_type == 'refptr_vec_diff_byref_const' or \
arg_type == 'rawptr_vec_same_byref_const' or arg_type == 'rawptr_vec_diff_byref_const':
vec_type = arg.get_type().get_vector_type()
if arg_type == 'simple_vec_byref_const':
assign = arg_name + '[i]'
elif arg_type == 'bool_vec_byref_const':
assign = arg_name + '[i]?true:false'
else:
ptr_class = arg.get_type().get_ptr_type()
if arg_type == 'refptr_vec_same_byref_const':
assign = ptr_class + 'CppToC::Unwrap(' + arg_name + '[i])'
elif arg_type == 'refptr_vec_diff_byref_const':
assign = ptr_class + 'CToCpp::Wrap(' + arg_name + '[i])'
elif arg_type == 'rawptr_vec_same_byref_const':
assign = ptr_class + 'CppToC::UnwrapRaw(' + arg_name + '[i])'
elif arg_type == 'rawptr_vec_diff_byref_const':
assign = ptr_class + 'CToCpp::Wrap(' + arg_name + '[i]).release()'
result += comment+\
'\n std::vector<'+vec_type+' > '+arg_name+'List;'\
'\n if ('+arg_name+'Count > 0) {'\
'\n for (size_t i = 0; i < '+arg_name+'Count; ++i) {'\
'\n '+vec_type+' '+arg_name+'Val = '+assign+';'\
'\n '+arg_name+'List.push_back('+arg_name+'Val);'\
'\n }'\
'\n }'
params.append(arg_name + 'List')
else:
raise Exception('Unsupported argument type %s for parameter %s in %s' %
(arg_type, arg_name, name))
if len(result) != result_len:
result += '\n'
result_len = len(result)
if is_cef_shutdown:
result += '\n\n#if DCHECK_IS_ON()'\
'\n shutdown_checker::SetIsShutdown();'\
'\n#endif\n'
# execution
result += '\n // Execute\n '
if retval_type != 'none':
# has a return value
if retval_type == 'simple':
result += retval.get_type().get_result_simple_type()
else:
result += retval.get_type().get_type()
result += ' _retval = '
if isinstance(func.parent, obj_class):
# virtual and static class methods
if isinstance(func, obj_function_virtual):
if cls.get_name() == func.parent.get_name():
# virtual method for the current class
result += func.parent.get_name() + 'CppToC::Get(self)->'
else:
# virtual method for a parent class
result += cls.get_name(
) + 'CppToC::Get(reinterpret_cast<' + cls.get_capi_name() + '*>(self))->'
else:
result += func.parent.get_name() + '::'
result += func.get_name() + '('
if len(params) > 0:
result += '\n ' + ',\n '.join(params)
result += ');\n'
result_len = len(result)
# parameter restoration
for arg in args:
arg_type = arg.get_arg_type()
arg_name = arg.get_type().get_name()
comment = '\n // Restore param: ' + arg_name + '; type: ' + arg_type
if arg_type == 'simple_byref':
result += comment+\
'\n if ('+arg_name+') {'\
'\n *'+arg_name+' = '+arg_name+'Val;'\
'\n }'
elif arg_type == 'bool_byref' or arg_type == 'bool_byaddr':
result += comment+\
'\n if ('+arg_name+') {'\
'\n *'+arg_name+' = '+arg_name+'Bool?true:false;'\
'\n }'
elif arg_type == 'struct_byref':
result += comment+\
'\n if ('+arg_name+') {'\
'\n '+arg_name+'Obj.DetachTo(*'+arg_name+');'\
'\n }'
elif arg_type == 'refptr_same_byref' or arg_type == 'refptr_diff_byref':
ptr_class = arg.get_type().get_ptr_type()
if arg_type == 'refptr_same_byref':
assign = ptr_class + 'CppToC::Wrap(' + arg_name + 'Ptr)'
else:
assign = ptr_class + 'CToCpp::Unwrap(' + arg_name + 'Ptr)'
result += comment+\
'\n if ('+arg_name+') {'\
'\n if ('+arg_name+'Ptr.get()) {'\
'\n if ('+arg_name+'Ptr.get() != '+arg_name+'Orig) {'\
'\n *'+arg_name+' = '+assign+';'\
'\n }'\
'\n } else {'\
'\n *'+arg_name+' = nullptr;'\
'\n }'\
'\n }'
elif arg_type == 'string_vec_byref':
result += comment+\
'\n cef_string_list_clear('+arg_name+');'\
'\n transfer_string_list_contents('+arg_name+'List, '+arg_name+');'
elif arg_type == 'string_map_single_byref':
result += comment+\
'\n cef_string_map_clear('+arg_name+');'\
'\n transfer_string_map_contents('+arg_name+'Map, '+arg_name+');'
elif arg_type == 'string_map_multi_byref':
result += comment+\
'\n cef_string_multimap_clear('+arg_name+');'\
'\n transfer_string_multimap_contents('+arg_name+'Multimap, '+arg_name+');'
elif arg_type == 'simple_vec_byref' or arg_type == 'bool_vec_byref' or \
arg_type == 'refptr_vec_same_byref' or arg_type == 'refptr_vec_diff_byref':
if arg_type == 'simple_vec_byref' or arg_type == 'bool_vec_byref':
assign = arg_name + 'List[i]'
elif arg_type == 'refptr_vec_same_byref':
ptr_class = arg.get_type().get_ptr_type()
assign = ptr_class + 'CppToC::Wrap(' + arg_name + 'List[i])'
elif arg_type == 'refptr_vec_diff_byref':
ptr_class = arg.get_type().get_ptr_type()
assign = ptr_class + 'CToCpp::Unwrap(' + arg_name + 'List[i])'
result += comment+\
'\n if ('+arg_name+'Count && '+arg_name+') {'\
'\n *'+arg_name+'Count = std::min('+arg_name+'List.size(), *'+arg_name+'Count);'\
'\n if (*'+arg_name+'Count > 0) {'\
'\n for (size_t i = 0; i < *'+arg_name+'Count; ++i) {'\
'\n '+arg_name+'[i] = '+assign+';'\
'\n }'\
'\n }'\
'\n }'
elif arg_type == 'rawptr_vec_diff_byref_const':
result += comment+\
'\n if ('+arg_name+'Count > 0) {'\
'\n for (size_t i = 0; i < '+arg_name+'Count; ++i) {'\
'\n delete '+arg_name+'List[i];'\
'\n }'\
'\n }'
if len(result) != result_len:
result += '\n'
result_len = len(result)
if len(result) != result_len:
result += '\n'
result_len = len(result)
# return translation
if retval_type != 'none':
# has a return value
result += '\n // Return type: ' + retval_type
if retval_type == 'simple' or retval_type == 'bool':
result += '\n return _retval;'
elif retval_type == 'string':
result += '\n return _retval.DetachToUserFree();'
elif retval_type == 'refptr_same':
ptr_class = retval.get_type().get_ptr_type()
result += '\n return ' + ptr_class + 'CppToC::Wrap(_retval);'
elif retval_type == 'refptr_diff':
ptr_class = retval.get_type().get_ptr_type()
result += '\n return ' + ptr_class + 'CToCpp::Unwrap(_retval);'
elif retval_type == 'ownptr_same':
ptr_class = retval.get_type().get_ptr_type()
result += '\n return ' + ptr_class + 'CppToC::WrapOwn(std::move(_retval));'
elif retval_type == 'ownptr_diff':
ptr_class = retval.get_type().get_ptr_type()
result += '\n return ' + ptr_class + 'CToCpp::UnwrapOwn(std::move(_retval));'
else:
raise Exception('Unsupported return type %s in %s' % (retval_type, name))
if len(result) != result_len:
result += '\n'
result += '}\n\n'
return result
def make_cpptoc_function_impl(cls, funcs, existing, prefixname, defined_names,
base_scoped):
impl = ''
for func in funcs:
if not prefixname is None:
name = prefixname + '_' + func.get_capi_name()
else:
name = func.get_capi_name()
value = get_next_function_impl(existing, name)
if not value is None \
and value['body'].find('// AUTO-GENERATED CONTENT') < 0:
# an implementation exists that was not auto-generated
impl += make_cpptoc_function_impl_existing(cls, name, func, value,
defined_names)
else:
impl += make_cpptoc_function_impl_new(cls, name, func, defined_names,
base_scoped)
return impl
def make_cpptoc_virtual_function_impl(header, cls, existing, prefixname,
defined_names, base_scoped):
funcs = []
funcs.extend(cls.get_virtual_funcs())
cur_cls = cls
while True:
parent_name = cur_cls.get_parent_name()
if is_base_class(parent_name):
break
else:
parent_cls = header.get_class(parent_name, defined_names)
if parent_cls is None:
raise Exception('Class does not exist: ' + parent_name)
funcs.extend(parent_cls.get_virtual_funcs())
cur_cls = header.get_class(parent_name, defined_names)
return make_cpptoc_function_impl(cls, funcs, existing, prefixname,
defined_names, base_scoped)
def make_cpptoc_virtual_function_assignment_block(funcs, offset, prefixname):
impl = ''
for func in funcs:
name = func.get_capi_name()
impl += ' GetStruct()->' + offset + name + ' = ' + prefixname + '_' + name + ';\n'
return impl
def make_cpptoc_virtual_function_assignment(header, cls, prefixname,
defined_names):
impl = make_cpptoc_virtual_function_assignment_block(cls.get_virtual_funcs(),
'', prefixname)
cur_cls = cls
offset = ''
while True:
parent_name = cur_cls.get_parent_name()
offset += 'base.'
if is_base_class(parent_name):
break
else:
parent_cls = header.get_class(parent_name, defined_names)
if parent_cls is None:
raise Exception('Class does not exist: ' + parent_name)
impl += make_cpptoc_virtual_function_assignment_block(
parent_cls.get_virtual_funcs(), offset, prefixname)
cur_cls = header.get_class(parent_name, defined_names)
return impl
def make_cpptoc_unwrap_derived(header, cls, base_scoped):
# identify all classes that derive from cls
derived_classes = []
cur_clsname = cls.get_name()
allclasses = header.get_classes()
for cur_cls in allclasses:
if cur_cls.get_name() == cur_clsname:
continue
if cur_cls.has_parent(cur_clsname):
derived_classes.append(cur_cls.get_name())
derived_classes = sorted(derived_classes)
if base_scoped:
impl = ['', '']
for clsname in derived_classes:
impl[0] += ' if (type == '+get_wrapper_type_enum(clsname)+') {\n'+\
' return '+clsname+'CppToC::UnwrapOwn(reinterpret_cast<'+\
get_capi_name(clsname, True)+'*>(s));\n'+\
' }\n'
impl[1] += ' if (type == '+get_wrapper_type_enum(clsname)+') {\n'+\
' return '+clsname+'CppToC::UnwrapRaw(reinterpret_cast<'+\
get_capi_name(clsname, True)+'*>(s));\n'+\
' }\n'
else:
impl = ''
for clsname in derived_classes:
impl += ' if (type == '+get_wrapper_type_enum(clsname)+') {\n'+\
' return '+clsname+'CppToC::Unwrap(reinterpret_cast<'+\
get_capi_name(clsname, True)+'*>(s));\n'+\
' }\n'
return impl
def make_cpptoc_class_impl(header, clsname, impl):
# structure names that have already been defined
defined_names = header.get_defined_structs()
# retrieve the class and populate the defined names
cls = header.get_class(clsname, defined_names)
if cls is None:
raise Exception('Class does not exist: ' + clsname)
capiname = cls.get_capi_name()
prefixname = get_capi_name(clsname[3:], False)
# retrieve the existing virtual function implementations
existing = get_function_impls(impl, 'CEF_CALLBACK')
base_class_name = header.get_base_class_name(clsname)
base_scoped = True if base_class_name == 'CefBaseScoped' else False
if base_scoped:
template_class = 'CefCppToCScoped'
else:
template_class = 'CefCppToCRefCounted'
# generate virtual functions
virtualimpl = make_cpptoc_virtual_function_impl(
header, cls, existing, prefixname, defined_names, base_scoped)
if len(virtualimpl) > 0:
virtualimpl = '\nnamespace {\n\n// MEMBER FUNCTIONS - Body may be edited by hand.\n\n' + virtualimpl + '} // namespace'
# the current class is already defined for static functions
defined_names.append(cls.get_capi_name())
# retrieve the existing static function implementations
existing = get_function_impls(impl, 'CEF_EXPORT')
# generate static functions
staticimpl = make_cpptoc_function_impl(cls,
cls.get_static_funcs(), existing, None,
defined_names, base_scoped)
if len(staticimpl) > 0:
staticimpl = '\n// GLOBAL FUNCTIONS - Body may be edited by hand.\n\n' + staticimpl
resultingimpl = staticimpl + virtualimpl
# any derived classes can be unwrapped
unwrapderived = make_cpptoc_unwrap_derived(header, cls, base_scoped)
const = '// CONSTRUCTOR - Do not edit by hand.\n\n'+ \
clsname+'CppToC::'+clsname+'CppToC() {\n'
const += make_cpptoc_virtual_function_assignment(header, cls, prefixname,
defined_names)
const += '}\n\n'+ \
'// DESTRUCTOR - Do not edit by hand.\n\n'+ \
clsname+'CppToC::~'+clsname+'CppToC() {\n'
if not cls.has_attrib('no_debugct_check') and not base_scoped:
const += ' shutdown_checker::AssertNotShutdown();\n'
const += '}\n\n'
# determine what includes are required by identifying what translation
# classes are being used
includes = format_translation_includes(header, const + resultingimpl +
(unwrapderived[0]
if base_scoped else unwrapderived))
# build the final output
result = get_copyright()
result += includes + '\n' + resultingimpl + '\n'
parent_sig = template_class + '<' + clsname + 'CppToC, ' + clsname + ', ' + capiname + '>'
if base_scoped:
const += 'template<> CefOwnPtr<'+clsname+'> '+parent_sig+'::UnwrapDerivedOwn(CefWrapperType type, '+capiname+'* s) {\n' + \
unwrapderived[0] + \
' DCHECK(false) << "Unexpected class type: " << type;\n'+ \
' return CefOwnPtr<'+clsname+'>();\n'+ \
'}\n\n' + \
'template<> CefRawPtr<'+clsname+'> '+parent_sig+'::UnwrapDerivedRaw(CefWrapperType type, '+capiname+'* s) {\n' + \
unwrapderived[1] + \
' DCHECK(false) << "Unexpected class type: " << type;\n'+ \
' return nullptr;\n'+ \
'}\n\n'
else:
const += 'template<> CefRefPtr<'+clsname+'> '+parent_sig+'::UnwrapDerived(CefWrapperType type, '+capiname+'* s) {\n' + \
unwrapderived + \
' DCHECK(false) << "Unexpected class type: " << type;\n'+ \
' return nullptr;\n'+ \
'}\n\n'
const += 'template<> CefWrapperType ' + parent_sig + '::kWrapperType = ' + get_wrapper_type_enum(
clsname) + ';'
result += '\n\n' + const
return result
def make_cpptoc_global_impl(header, impl):
# structure names that have already been defined
defined_names = header.get_defined_structs()
# retrieve the existing global function implementations
existing = get_function_impls(impl, 'CEF_EXPORT')
# generate global functions
impl = make_cpptoc_function_impl(None,
header.get_funcs(), existing, None,
defined_names, False)
if len(impl) > 0:
impl = '\n// GLOBAL FUNCTIONS - Body may be edited by hand.\n\n' + impl
includes = ''
# include required headers for global functions
filenames = []
for func in header.get_funcs():
filename = func.get_file_name()
if not filename in filenames:
includes += '#include "include/'+func.get_file_name()+'"\n' \
'#include "include/capi/'+func.get_capi_file_name()+'"\n'
filenames.append(filename)
# determine what includes are required by identifying what translation
# classes are being used
includes += format_translation_includes(header, impl)
# build the final output
result = get_copyright()
result += includes + '\n' + impl
return result
def write_cpptoc_impl(header, clsname, dir):
if clsname is None:
# global file
file = dir
else:
# class file
# give the output file the same directory offset as the input file
cls = header.get_class(clsname)
dir = os.path.dirname(os.path.join(dir, cls.get_file_name()))
file = os.path.join(dir, get_capi_name(clsname[3:], False) + '_cpptoc.cc')
if path_exists(file):
oldcontents = read_file(file)
else:
oldcontents = ''
if clsname is None:
newcontents = make_cpptoc_global_impl(header, oldcontents)
else:
newcontents = make_cpptoc_class_impl(header, clsname, oldcontents)
return (file, newcontents)
# test the module
if __name__ == "__main__":
import sys
# verify that the correct number of command-line arguments are provided
if len(sys.argv) < 4:
sys.stderr.write('Usage: ' + sys.argv[0] +
' <infile> <classname> <existing_impl>')
sys.exit()
# create the header object
header = obj_header()
header.add_file(sys.argv[1])
# read the existing implementation file into memory
try:
with open(sys.argv[3], 'r') as f:
data = f.read()
except IOError as e:
(errno, strerror) = e.args
raise Exception('Failed to read file ' + sys.argv[3] + ': ' + strerror)
else:
f.close()
# dump the result to stdout
sys.stdout.write(make_cpptoc_class_impl(header, sys.argv[2], data))