forked from dxma/perl5-mods-old
/
format_qtedi_production.pl
executable file
·1370 lines (1199 loc) · 36.3 KB
/
format_qtedi_production.pl
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/perl -w
################################################################
# $Id$
# $Author$
# $Date$
# $Rev$
################################################################
use warnings;
use strict;
#use English qw( -no_match_vars );
use Fcntl qw(O_RDONLY O_WRONLY O_TRUNC O_CREAT);
use File::Spec;
use YAML::Syck qw(Dump Load);
my $filename;
=head1 DESCIPTION
Format production from Parse::QTEDI into more binding-make-specific
look. This will both strip unrelevant entry and renew the structure
of other interested entries.
B<NOTE>: All new hash keys inserted here will be uppercase to
differentiate with QTEDI output, except meta field such as 'subtype'.
=cut
sub usage {
print STDERR << "EOU";
usage: $0 <qtedi_production.yml> [<output_file>]
EOU
exit 1;
}
=head1 ELEMENTS
Format functions.
=cut
=over
=item $FUNCTION_PROPERTIES
Keep all known C++ function and QT-specific property keywords.
Function format will firstly filter out them from prototype line.
B<NOTE>: Some properties are stored inside 'PROPERTY' field array for
futher reference.
B<NOTE>: Q_DECL_EXPORT == __attribute((visibility(default)))__ in
gcc.
=back
=cut
################ DICTIONARY ################
sub P_IGNORE() { 0 }
sub P_KEEP () { 1 }
# QT-specific
my $QT_PROPERTIES = {
Q_TESTLIB_EXPORT => P_IGNORE,
Q_DECL_EXPORT => P_KEEP,
Q_DBUS_EXPORT => P_IGNORE,
};
# KDE-specific
my $KDE_PROPERTIES = {
};
# function-specific
my $FUNCTION_PROPERTIES = {
# C++ standard
explicit => P_IGNORE,
implicit => P_IGNORE,
virtual => P_KEEP,
'pure virtual' => P_KEEP,
inline => P_IGNORE,
static => P_KEEP,
friend => P_KEEP,
# const belongs to return type
# const => P_IGNORE,
%$QT_PROPERTIES,
%$KDE_PROPERTIES,
};
# class/struct/union-specific
my $CLASS_PROPERTIES = {
# C++ standard
inline => P_IGNORE,
static => P_KEEP,
friend => P_KEEP,
mutable => P_IGNORE,
%$QT_PROPERTIES,
%$KDE_PROPERTIES,
};
#enum-specific
my $ENUM_PROPERTIES = $FUNCTION_PROPERTIES;
#namespace-specific
my $NAMESPACE_PROPERTIES = $CLASS_PROPERTIES;
################ FORMAT UNIT ################
=over
=item __format_macro
Keep Q_OBJECT and Q_PROPERTY for further consideration.
Each property field inside a Q_PROPERTY will be stored as a new
key/value pair.
# spec of Q_PROPERTY
---
name : [from_QTEDI]
type : macro
NAME : [name]
TYPE : [type]
READ : [read function]
WRITE: [write function]
...
=back
=cut
sub __format_macro {
my $entry = shift;
# keep Q_OBJECT Q_PROPERTY
if ($entry->{name} eq 'Q_OBJECT') {
delete $entry->{subtype};
return 1;
}
elsif ($entry->{name} eq 'Q_PROPERTY') {
my @values = split / /, $entry->{values};
$entry->{TYPE} = shift @values;
$entry->{NAME} = shift @values;
while (@values) {
my $k = shift @values;
my $v = shift @values;
$entry->{$k} = $v;
}
delete $entry->{subtype};
delete $entry->{values};
return 1;
}
else {
return 0;
}
}
sub __format_class_or_struct {
my $entry = shift;
my $type = shift;
# $type == 0 => class
# $type == 1 => struct
# by default class
$type = 0 unless defined $type;
# format name and property
if ($entry->{name}) {
my @values = split /\s+/, $entry->{name};
my $cname = pop @values;
foreach my $v (@values) {
if (exists $CLASS_PROPERTIES->{$v} and
$CLASS_PROPERTIES->{$v} == P_KEEP) {
push @{$entry->{property}}, $v;
}
}
$entry->{NAME} = $cname;
}
foreach my $p (@{$entry->{property}}) {
$p =~ s/\s+$//o;
push @{$entry->{PROPERTY}}, $p;
}
delete $entry->{name};
delete $entry->{property};
# format inheritance line
if (exists $entry->{inheritance} and $entry->{inheritance}) {
my @isa = split /(public|private|protected)/,
$entry->{inheritance};
shift @isa;
my %isa = @isa;
while (my ( $r, $n ) = each %isa) {
$n =~ s/^\s+//io;
$n =~ s/(?:,|\s)+$//io;
push @{$entry->{ISA}}, {
NAME => $n, RELATIONSHIP => $r, };
}
delete $entry->{inheritance};
}
# format variable
if (exists $entry->{variable}) {
my @variable = split /\s*,\s*/, $entry->{variable};
foreach my $v (@variable) {
$v =~ s/\s+$//io;
push @{$entry->{VARIABLE}}, $v;
}
delete $entry->{variable};
}
# process body
# strip private part
my $abstract_class;
if (exists $entry->{body}) {
if ($type == 0) {
# class
( $entry->{BODY}, $abstract_class ) =
_format_class_body($entry->{body});
}
else {
# struct
( $entry->{BODY}, $abstract_class ) =
_format_struct_body($entry->{body});
}
push @{$entry->{PROPERTY}}, 'abstract' if $abstract_class;
delete $entry->{body};
}
return 1;
}
=over
=item __format_class
Extract class name string and store as new field. Recursively process
class body, strip private part.
Format inheritance line if has.
# spec
---
type : class
PROPERTY :
- [class property1]
...
NAME : [name]
ISA :
- NAME : [parent class name]
RELATIONSHIP : public/private/protected
...
BODY :
...
VARIABLE :
- [variable1]
...
=back
=cut
sub __format_class {
return __format_class_or_struct($_[0], 0);
}
=over
=item __format_struct
Similar as __format_class.
B<NOTE>: As defined in C++, top entries not covered by any
public/private/protected keyword will be treated private.
See __format_class above regarding output spec.
=back
=cut
sub __format_struct {
return __format_class_or_struct($_[0], 1);
}
=over
=item __format_union
Similar as __format_struct.
See __format_class above regarding output spec.
=back
=cut
sub __format_union {
# FIXME: how to deal with union
return __format_class_or_struct($_[0], 1);
}
=over
=item __normalize
Normalize type or function pointer string.
=back
=cut
sub __normalize {
my $string = shift;
# normalize
$string =~ s/^\s+//gio;
$string =~ s/\s+$//gio;
$string =~ s/\s+/ /gio;
$string =~ s/<\s+/</gio;
$string =~ s/\s+,/,/gio;
$string =~ s/,\s+/,/gio;
$string =~ s/\s+>/>/gio;
$string =~ s/>\s+::/>::/gio;
return $string;
}
=over
=item __format_fpointer
Format a function pointer entry.
# spec
---
type : fpointer
PROPERTY :
- [function property1]
...
NAME : [T_FPOINTER_BLAH]
NAME_ORIGIN : [BLAH]
PROTOTYPE : [prototype string]
DEFAULT_VALUE : [default value, mostly 0]
FPOINTERINFO :
- NAME : [same as NAME above]
[could be a ref to inner FPOINTERINFO structure
in case of function pointer which returns
another function pointer ]
- RETURN : [similar as in function]
- PARAMETER : [similar as in function]
=back
=cut
sub __format_fpointer {
my $entry = shift;
# grep function property from return field
my $properties =
exists $entry->{property} ? $entry->{property} : [];
my $fpreturn = [];
my @return = split /\s*\b\s*/, $entry->{return};
foreach my $e (@return) {
if (exists $FUNCTION_PROPERTIES->{$e} and
$FUNCTION_PROPERTIES->{$e} == P_KEEP) {
push @$properties, $e;
}
else {
push @$fpreturn, $e;
}
}
# cat return type string
my $fpreturn_type = shift @$fpreturn;
for (my $i = 0; $i < @$fpreturn; ) {
if ($fpreturn->[$i] eq '::') {
$fpreturn_type .= $fpreturn->[$i]. $fpreturn->[$i+1];
$i += 2;
}
else {
$fpreturn_type .= ' '. $fpreturn->[$i];
$i++;
}
}
# make new function pointer name
# and cat function prototype string
my $FP_TYPE_PREFIX = 'T_FPOINTER_';
my $fpname;
my $fpname_origin;
my $fproto = $fpreturn_type. ' ';
my $get_parameters = sub {
my ( $plist, $params ) = @_;
foreach my $p (@{$plist}) {
if ($p->{subtype} eq 'fpointer') {
__format_fpointer($p);
my $proto = $p->{PROTOTYPE};
if (exists $p->{DEFAULT_VALUE}) {
$proto .= ' = '. $p->{DEFAULT_VALUE};
}
push @$params, $proto;
}
elsif ($p->{name}) {
# skip $p->{name} is ''
my $param = $p->{name};
if (exists $p->{default}) {
$param .= ' = '. $p->{default};
}
push @$params, $param;
}
}
};
my $patch_fpointer_name = sub {
# add $FP_TYPE_PREFIX at the right place
# change name into upper case
# keep namespace prefix untouched
my $fullname = shift;
my @n = split /\:\:/, $fullname;
( my $patched = $n[-1] ) =~
s/^(\*+\s*)(.+)/$1.$FP_TYPE_PREFIX.uc($2).'_'.uc($filename)/eio;
my $origin = $2;
return [ join("::", @n[0 .. -1], $patched),
join("::", @n[0 .. -1], $origin)];
};
if (ref $entry->{name} eq 'HASH') {
# well, a function pointer which returns
# another function pointer
my $name = $entry->{name}->{name};
( $fpname, $fpname_origin ) =
@{ $patch_fpointer_name->($name) };
$fproto .= '(('. $fpname. ')(';
# process inner params
my $params = [];
$get_parameters->($entry->{name}->{parameter}, $params);
$fproto .= join(',', @$params). '))';
}
else {
my $name = $entry->{name};
( $fpname, $fpname_origin ) =
@{ $patch_fpointer_name->($name) };
$fproto .= '('. $fpname. ')';
}
# strip * inside $fpname
$fpname =~ s/\*+//io;
# process outer params
my $params = [];
$get_parameters->($entry->{parameter}, $params);
$fproto .= '('. join(',', @$params). ')';
# attach function pointer properties
foreach my $p (@$properties) {
if ($p eq 'const') {
$fproto .= ' const';
}
else {
$fproto = $p. ' '. $fproto;
}
}
# masquerade as a normal function entry
# delegate to __format_function
# fill RETURN and PARAMETER fields
# NOTE: soft copy
my $masque_function = {};
$masque_function->{name} =
join(" ", $entry->{return}, $fpname);
$masque_function->{parameter} = $entry->{parameter};
$masque_function->{type} = 'function';
__format_function($masque_function);
if (ref $entry->{name} eq 'HASH') {
# a function pointer returns another functipn pointer
# FIXME: delegate inner part to __format_function
my $masque_inner_function = {};
$masque_inner_function->{name} =
join(" ", $entry->{return}, $entry->{name}->{name});
$masque_inner_function->{parameter} =
$entry->{name}->{parameter};
$masque_inner_function->{type} = 'function';
__format_function($masque_inner_function);
# store in $masque_function
$masque_function->{NAME} = {};
$masque_function->{NAME}->{PARAMETER} =
$masque_inner_function->{PARAMETER} if
exists $masque_inner_function->{PARAMETER};
}
# normalize
$fpname = __normalize($fpname);
$fproto = __normalize($fproto);
# store
delete $entry->{name};
delete $entry->{return};
delete $entry->{parameter} if exists $entry->{parameter};
$entry->{NAME} = $fpname;
$entry->{NAME_ORIGIN} = $fpname_origin;
$entry->{PROTOTYPE} = $fproto;
$entry->{PROPERTY} = $properties if @$properties;
if (exists $entry->{default}) {
$entry->{DEFAULT_VALUE} = $entry->{default};
delete $entry->{default};
}
$entry->{FPOINTERINFO}= {};
$entry->{FPOINTERINFO}->{NAME} = $masque_function->{NAME};
$entry->{FPOINTERINFO}->{RETURN} = $masque_function->{RETURN};
$entry->{FPOINTERINFO}->{PARAMETER} =
$masque_function->{PARAMETER} if exists $masque_function->{PARAMETER};
return 1;
}
=over
=item __format_function
Format a function entry. Extract return type, function name and all
parameters from function entry from QTEDI.
# spec
---
type : function
subtype : 1/0 [is operator or not]
PROPERTY :
- [function property1]
...
NAME : [name]
RETURN : [return type]
PARAMETER :
- TYPE : [param1 type]
[NOTE: could be '...' in ansi]
NAME : [param1 name]
DEFAULT_VALUE : [param1 default value]
...
=back
=cut
sub __format_function {
my $entry = shift;
#print STDERR $entry->{name}, "\n";
my $fname_with_prefix = $entry->{name};
# filter out keywords from name
my @fvalues = split /\s*\b\s*/, $fname_with_prefix;
my $properties = [];
my @fname = ();
my @freturn_type = ();
# get function name
# pre-scan for operator function
my $is_operator_function = 0;
FN_OPERATOR_LOOP:
for (my $i = $#fvalues; $i >= 0; $i--) {
if ($fvalues[$i] eq 'operator') {
# store as function name starting by operator keyword
@fname = splice @fvalues, $i;
$is_operator_function = 1;
last FN_OPERATOR_LOOP;
}
}
unshift @fname, pop @fvalues unless $is_operator_function;
FN_LOOP:
for (my $i = $#fvalues; $i >= 0; ) {
if ($fvalues[$i] eq '::') {
# namespace
unshift @fname, pop @fvalues;
unshift @fname, pop @fvalues;
$i -= 2;
}
elsif ($fvalues[$i] eq '~') {
# C++ destructor
unshift @fname, pop @fvalues;
$i--;
}
elsif ($fvalues[$i] eq '::~') {
# destructor within namespace ;-(
unshift @fname, pop @fvalues;
unshift @fname, pop @fvalues;
$i -= 2;
}
elsif ($fvalues[$i] eq '>::') {
# template namespace
unshift @fname, pop @fvalues;
$i--;
TN_LOOP:
for (my $depth = 1; $i >= 0; ) {
if ($fvalues[$i] eq '<') {
unshift @fname, pop @fvalues;
unshift @fname, pop @fvalues;
$i -= 2;
last TN_LOOP if --$depth == 0;
}
elsif ($fvalues[$i] eq '>') {
unshift @fname, pop @fvalues;
$i--;
$depth++;
}
# FIXME: '>::'
else {
unshift @fname, pop @fvalues;
$i--;
}
}
}
else {
last FN_LOOP;
}
}
# get return type
# filter out properties
foreach my $v (@fvalues) {
if (exists $FUNCTION_PROPERTIES->{$v}) {
if ($FUNCTION_PROPERTIES->{$v} == P_KEEP) {
unshift @$properties, $v;
}
}
else {
push @freturn_type, $v;
}
}
if (exists $entry->{property}) {
foreach my $p (@{$entry->{property}}) {
if (exists $FUNCTION_PROPERTIES->{$p} and
$FUNCTION_PROPERTIES->{$p} == P_KEEP) {
unshift @$properties, $p;
}
}
}
# format return type
my $return_type;
if (@freturn_type) {
$return_type = shift @freturn_type;
for (my $i = 0; $i <= $#freturn_type; ) {
#print STDERR "$i :", $freturn_type[$i], "\n";
if ($freturn_type[$i] eq '::') {
$return_type .= $freturn_type[$i]. $freturn_type[$i+1];
$i += 2;
}
elsif ($freturn_type[$i] eq '>::') {
$return_type .= $freturn_type[$i]. $freturn_type[$i+1];
$i += 2;
}
elsif ($freturn_type[$i] eq '<') {
$return_type .= $freturn_type[$i];
$i++;
}
elsif ($freturn_type[$i] eq '>') {
$return_type .= $freturn_type[$i];
$i++;
}
else {
$return_type .= ' '. $freturn_type[$i];
$i++;
}
}
}
else {
$return_type = '';
}
# format params
my $parameters = [];
PARAMETER_MAIN_LOOP:
foreach my $p (@{$entry->{parameter}}) {
next PARAMETER_MAIN_LOOP if
$p->{subtype} eq 'simple' and $p->{name} eq '';
my $pname_with_type = $p->{name};
my $psubtype = $p->{subtype};
my $pdefault_value =
exists $p->{default} ? $p->{default} : '';
$pdefault_value =~ s/\s+$//o;
my ( $pname, $ptype, $fpinfo, );
if ($psubtype eq 'fpointer') {
__format_fpointer($p);
$pname = $p->{PROTOTYPE};
$ptype = $p->{NAME};
$fpinfo= $p->{FPOINTERINFO};
}
elsif ($pname_with_type =~ m/\[/io) {
# array pointer
# TODO: ugly match
# similar to fpointer
# store variable name in TYPE
# fall decl string in NAME
# NOTE: transform char array[] into char *array
if ($pname_with_type =~ /^(.+?)\b(\w+)(\s*\[\])/o) {
$pname_with_type = $1. '* T_ARRAY_'. uc($2);
$ptype = 'T_ARRAY_'. uc($2);
}
elsif ($pname_with_type =~ /^(.+?\*)\s*(\w+)(\s*\[)/o) {
$pname_with_type = $1. ' T_ARRAY_'. uc($2). $3;
$ptype = 'T_ARRAY_'. uc($2);
}
else {
$pname_with_type =~ s{^(.+?)\b(\w+)(\s*\[)}
{$1.$2.' T_ARRAY_'.uc($2).$3}eio;
$ptype = 'T_ARRAY_'. uc($2);
}
$pname = $pname_with_type;
}
else {
# simple && template
# split param name [optional] and param type
my @pvalues =
split /\s*(?<!::)\b(?!::)\s*/, $pname_with_type;
my @pname = ();
my @ptype = ();
if (@pvalues == 1) {
# only one entry, must be param type
# noop
}
elsif (@pvalues == 2 and $pvalues[0] eq 'const') {
# const type
# noop
}
# \w may be different on different systems
# here strictly as an 'old' word
elsif ($pvalues[$#pvalues] =~ m/^[a-z_A-Z_0-9_\_]+$/o) {
# process param name
unshift @pname, pop @pvalues;
FP_LOOP:
for (my $i = $#pvalues; $i >= 0; ) {
if ($pvalues[$i] eq '::') {
# namespace
unshift @pname, pop @pvalues;
unshift @pname, pop @pvalues;
$i -= 2;
}
else {
last FP_LOOP;
}
}
}
# left are type items
@ptype = @pvalues;
# workaround for '(un)signed' keyword
if ($ptype[$#ptype] eq 'signed' or
$ptype[$#ptype] eq 'unsigned') {
# don't pull back 'short' unsigned like 'unsigned sec'
if ($pname[0] =~ m/^(?:int|long|short|char)$/io) {
# shift one item back from @pname
push @ptype, shift @pname;
}
}
# workaround for 'long long' keyword
if ($ptype[$#ptype] eq 'long' and
@pname and $pname[0] eq 'long') {
# (un)signed long long
push @ptype, shift @pname;
}
# format param name
$pname = @pname ? join('', @pname) : '';
# format param type
$ptype = '';
if (@ptype) {
$ptype = shift @ptype;
for (my $i = 0; $i <= $#ptype; ) {
if ($ptype[$i] eq '::') {
$ptype .= $ptype[$i]. $ptype[$i+1];
$i += 2;
}
elsif ($ptype[$i] eq '<') {
$ptype .= $ptype[$i];
$i++;
}
elsif ($ptype[$i] eq '>') {
$ptype .= $ptype[$i];
$i++;
}
else {
$ptype .= ' '. $ptype[$i];
$i++;
}
}
}
$ptype =~ s/\s+$//o;
}
$ptype = __normalize($ptype);
# store param unit
my $p = { TYPE => $ptype };
$p->{NAME} = $pname if $pname;
$p->{DEFAULT_VALUE} = $pdefault_value if $pdefault_value ne '';
if ($psubtype eq 'fpointer') {
# attach FPOINTERINFO for function pointer
$p->{FPOINTERINFO} = $fpinfo;
}
push @$parameters, $p;
}
# format function name
my $fname = '';
if ($is_operator_function) {
my $i = 0;
FN_FORMAT_LOOP:
for (; $i < @fname; $i++) {
$fname .= $fname[$i];
last FN_FORMAT_LOOP if $fname[$i] eq 'operator';
}
if ($fname[++$i] =~ m/^[a-z_A-Z_0-9_\_]+$/o) {
# type cast operator such as
# operator int
$fname .= ' '. $fname[$i++];
}
else {
# operator+ and like
$fname .= $fname[$i++];
}
for (; $i < @fname; $i++) {
if ($fname[$i] eq '<') {
# template type
$fname .= $fname[$i];
}
elsif ($fname[$i] eq '>') {
$fname .= $fname[$i];
}
else {
$fname .= ' '. $fname[$i];
}
}
}
else {
$fname = join('', @fname);
}
# normalize
$fname = __normalize($fname);
$return_type = __normalize($return_type) if $return_type;
# store
$entry->{NAME} = $fname;
# meta info field
$entry->{subtype} = $is_operator_function ? 1 : 0;
$entry->{RETURN} = $return_type if $return_type;
$entry->{PROPERTY} = $properties if @$properties;
$entry->{PARAMETER} = $parameters if @$parameters;
delete $entry->{name};
delete $entry->{parameter};
delete $entry->{property};
return 1;
}
=over
=item __format_enum
Format enum, normalize name, property and enum value entries.
# spec
---
type : enum
NAME : [name]
PROPERTY :
- [enum property1]
...
VALUE :
- [enum value1]
...
VARIABLE :
- [variable1]
...
=back
=cut
sub __format_enum {
my $entry = shift;
# format name and property
if ($entry->{name}) {
my @values = split /\s+/, $entry->{name};
my $ename = pop @values;
foreach my $v (@values) {
if (exists $ENUM_PROPERTIES->{$v} and
$ENUM_PROPERTIES->{$v} == P_KEEP) {
push @{$entry->{property}}, $v;
}
}
$entry->{NAME} = $ename;
}
foreach my $p (@{$entry->{property}}) {
$p =~ s/\s+$//o;
push @{$entry->{PROPERTY}}, $p;
}
delete $entry->{name};
delete $entry->{property};
# normalize value entries
foreach my $i (@{$entry->{value}}) {
my $index = @$i == 2 ? 1 : 0;
my $v = $i->[$index];
$v =~ s/\s+$//o;
$i->[$index] = $v;
}
if (@{$entry->{value}}) {
$entry->{VALUE} = $entry->{value};
delete $entry->{value};
}
# format variable
if (exists $entry->{variable}) {
my @variable = split /\s*,\s*/, $entry->{variable};
foreach my $v (@variable) {
$v =~ s/\s+$//io;
push @{$entry->{VARIABLE}}, $v;
}
}
return 1;
}
=over
=item __format_accessibility
Format accessibility, normalize value entries.
B<NOTE>: private type should not appear here since being stripped.
# spec
---
type : accessibility
VALUE :
- [accessibility keyword1]
...
=back
=cut
sub __format_accessibility {
my $entry = shift;
# # normalize value entries
# foreach my $v (@{$entry->{value}}) {
# $v =~ s/\s+$//o;
# }
if (@{$entry->{value}}) {
$entry->{VALUE} = $entry->{value};
delete $entry->{value};
}
return 1;
}
=over
=item __format_typedef
Format typedef, normalize value entry.
Value entry could be of type:
1. typedef simple type C<< typedef A B; >>
2. typedef (anonymous) class/struct/enum/union C<< typdef enum A { } B; >>
3. typedef function pointer C<< typedef void (*P)(int, uint); >>
4. typedef an array C<< typedef unsigned char Digest[16]; >>
# spec
---
type : typedef
subtype : class/struct/enum/union/fpointer/simple
FROM : [from type name for simple typedef ]
[a hashref for class/struct/enum/union]
[type alias for function pointer ]
TO : [to type name]
[original name of function pointer ]
PROTOTYPE : [prototype string of function pointer ]
=back
=cut
sub __format_typedef {
my $entry = shift;
# extract body entry
if (ref $entry->{body} eq 'HASH') {
$entry->{subtype} = $entry->{body}->{type};
if ($entry->{subtype} eq 'fpointer') {
# fpointer
__format_fpointer($entry->{body});
$entry->{PROTOTYPE} = $entry->{body}->{PROTOTYPE};
$entry->{TO} = $entry->{body}->{NAME_ORIGIN};
$entry->{FROM} = $entry->{body}->{NAME};
$entry->{FPOINTERINFO} = $entry->{body}->{FPOINTERINFO};
}
else {
# other container type
my $temp = [];
_format_primitive_loop($entry->{body}, $temp);
my $body = $temp->[0];
$entry->{FROM} = $body->{NAME} if exists $body->{NAME};
# $body->{VARIABLE} should exist this case
# and has only one entry
# or else something is wrong
$entry->{TO} = $body->{VARIABLE}->[0];
# pointer/reference digit be moved into FROM
if ($entry->{TO} =~ s/^\s*((?:\*|\&))//io) {
$entry->{FROM} .= ' '. $1;
}
}
}
else {
# simple