line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Google::ProtocolBuffers::CodeGen;
|
2
|
1
|
|
|
1
|
|
10
|
use strict;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
56
|
|
3
|
1
|
|
|
1
|
|
8
|
use warnings;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
48
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
8
|
use Google::ProtocolBuffers::Constants qw/:types :labels :complex_types/;
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1342
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
my %primitive_types = reverse (
|
8
|
|
|
|
|
|
|
TYPE_DOUBLE => TYPE_DOUBLE,
|
9
|
|
|
|
|
|
|
TYPE_FLOAT => TYPE_FLOAT,
|
10
|
|
|
|
|
|
|
TYPE_INT64 => TYPE_INT64,
|
11
|
|
|
|
|
|
|
TYPE_UINT64 => TYPE_UINT64,
|
12
|
|
|
|
|
|
|
TYPE_INT32 => TYPE_INT32,
|
13
|
|
|
|
|
|
|
TYPE_FIXED64=> TYPE_FIXED64,
|
14
|
|
|
|
|
|
|
TYPE_FIXED32=> TYPE_FIXED32,
|
15
|
|
|
|
|
|
|
TYPE_BOOL => TYPE_BOOL,
|
16
|
|
|
|
|
|
|
TYPE_STRING => TYPE_STRING,
|
17
|
|
|
|
|
|
|
TYPE_GROUP => TYPE_GROUP, ##
|
18
|
|
|
|
|
|
|
TYPE_MESSAGE=> TYPE_MESSAGE, ## should never appear, because 'message' is a 'complex type'
|
19
|
|
|
|
|
|
|
TYPE_BYTES => TYPE_BYTES,
|
20
|
|
|
|
|
|
|
TYPE_UINT32 => TYPE_UINT32,
|
21
|
|
|
|
|
|
|
TYPE_ENUM => TYPE_ENUM, ##
|
22
|
|
|
|
|
|
|
TYPE_SFIXED32=>TYPE_SFIXED32,
|
23
|
|
|
|
|
|
|
TYPE_SFIXED64=>TYPE_SFIXED64,
|
24
|
|
|
|
|
|
|
TYPE_SINT32 => TYPE_SINT32,
|
25
|
|
|
|
|
|
|
TYPE_SINT64 => TYPE_SINT64,
|
26
|
|
|
|
|
|
|
);
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my %labels = reverse (
|
29
|
|
|
|
|
|
|
LABEL_OPTIONAL => LABEL_OPTIONAL,
|
30
|
|
|
|
|
|
|
LABEL_REQUIRED => LABEL_REQUIRED,
|
31
|
|
|
|
|
|
|
LABEL_REPEATED => LABEL_REPEATED,
|
32
|
|
|
|
|
|
|
);
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _get_perl_literal {
|
35
|
79
|
|
|
79
|
|
107
|
my $v = shift;
|
36
|
79
|
|
|
|
|
99
|
my $opts = shift;
|
37
|
|
|
|
|
|
|
|
38
|
79
|
100
|
|
|
|
400
|
if ($v =~ /^-?\d+$/) {
|
|
|
100
|
|
|
|
|
|
39
|
|
|
|
|
|
|
## integer literal
|
40
|
55
|
100
|
100
|
|
|
603
|
if ($v>0x7fff_ffff || $v<-0x8000_0000) {
|
41
|
3
|
|
|
|
|
14
|
return "Math::BigInt->new('$v')";
|
42
|
|
|
|
|
|
|
} else {
|
43
|
52
|
|
|
|
|
146
|
return "$v";
|
44
|
|
|
|
|
|
|
}
|
45
|
|
|
|
|
|
|
} elsif ($v =~ /[-+]?\d*\.\d+([Ee][\+-]?\d+)?|[-+]?\d+[Ee][\+-]?\d+/i) {
|
46
|
|
|
|
|
|
|
## floating point literal
|
47
|
4
|
|
|
|
|
16
|
return "$v";
|
48
|
|
|
|
|
|
|
} else {
|
49
|
|
|
|
|
|
|
## string literal
|
50
|
20
|
|
|
|
|
110
|
$v =~ s/([\x00-\x1f'"\\$@%\x80-\xff])/ '\\x{' . sprintf("%02x", ord($1)) . '}' /ge;
|
|
16
|
|
|
|
|
69
|
|
51
|
20
|
|
|
|
|
70
|
return qq["$v"];
|
52
|
|
|
|
|
|
|
}
|
53
|
|
|
|
|
|
|
}
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub generate_code_of_enum {
|
56
|
5
|
|
|
5
|
0
|
10
|
my $self = shift;
|
57
|
5
|
|
|
|
|
6
|
my $opts = shift;
|
58
|
|
|
|
|
|
|
|
59
|
5
|
|
33
|
|
|
22
|
my $class_name = ref($self) || $self;
|
60
|
5
|
|
|
|
|
8
|
my $fields_text;
|
61
|
5
|
|
|
|
|
6
|
foreach my $f (@{ $self->_pb_fields_list }) {
|
|
5
|
|
|
|
|
27
|
|
62
|
21
|
|
|
|
|
51
|
my ($name, $value) = @$f;
|
63
|
21
|
|
|
|
|
37
|
$value = _get_perl_literal($value, $opts);
|
64
|
21
|
|
|
|
|
58
|
$fields_text .= " ['$name', $value],\n";
|
65
|
|
|
|
|
|
|
}
|
66
|
|
|
|
|
|
|
|
67
|
5
|
|
|
|
|
35
|
return <<"CODE";
|
68
|
|
|
|
|
|
|
unless ($class_name->can('_pb_fields_list')) {
|
69
|
|
|
|
|
|
|
Google::ProtocolBuffers->create_enum(
|
70
|
|
|
|
|
|
|
'$class_name',
|
71
|
|
|
|
|
|
|
[
|
72
|
|
|
|
|
|
|
$fields_text
|
73
|
|
|
|
|
|
|
]
|
74
|
|
|
|
|
|
|
);
|
75
|
|
|
|
|
|
|
}
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
CODE
|
78
|
|
|
|
|
|
|
}
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub generate_code_of_message_or_group {
|
82
|
34
|
|
|
34
|
0
|
133
|
my $self = shift;
|
83
|
34
|
|
|
|
|
48
|
my $opts = shift;
|
84
|
|
|
|
|
|
|
|
85
|
34
|
50
|
|
|
|
222
|
my $create_what =
|
|
|
100
|
|
|
|
|
|
86
|
|
|
|
|
|
|
($self->_pb_complex_type_kind==MESSAGE) ? 'create_message' :
|
87
|
|
|
|
|
|
|
($self->_pb_complex_type_kind==GROUP) ? 'create_group' : die;
|
88
|
|
|
|
|
|
|
|
89
|
34
|
|
33
|
|
|
139
|
my $class_name = ref($self) || $self;
|
90
|
|
|
|
|
|
|
|
91
|
34
|
|
|
|
|
45
|
my $fields_text = ''; # may be empty, as empty messages are allowed
|
92
|
34
|
|
|
|
|
44
|
foreach my $f (@{ $self->_pb_fields_list }) {
|
|
34
|
|
|
|
|
165
|
|
93
|
226
|
|
|
|
|
690
|
my ($label, $type, $name, $field_number, $default_value) = @$f;
|
94
|
|
|
|
|
|
|
|
95
|
226
|
50
|
|
|
|
542
|
die unless $labels{$label};
|
96
|
226
|
|
|
|
|
379
|
$label = "Google::ProtocolBuffers::Constants::$labels{$label}()";
|
97
|
|
|
|
|
|
|
|
98
|
226
|
100
|
|
|
|
503
|
if ($primitive_types{$type}) {
|
99
|
176
|
|
|
|
|
304
|
$type = "Google::ProtocolBuffers::Constants::$primitive_types{$type}()";
|
100
|
|
|
|
|
|
|
} else {
|
101
|
50
|
|
|
|
|
88
|
$type = "'$type'";
|
102
|
|
|
|
|
|
|
}
|
103
|
|
|
|
|
|
|
|
104
|
226
|
100
|
|
|
|
467
|
$default_value = (defined $default_value) ?
|
105
|
|
|
|
|
|
|
_get_perl_literal($default_value, $opts) : 'undef';
|
106
|
226
|
|
|
|
|
863
|
$fields_text .= <<"FIELD";
|
107
|
|
|
|
|
|
|
[
|
108
|
|
|
|
|
|
|
$label,
|
109
|
|
|
|
|
|
|
$type,
|
110
|
|
|
|
|
|
|
'$name', $field_number, $default_value
|
111
|
|
|
|
|
|
|
],
|
112
|
|
|
|
|
|
|
FIELD
|
113
|
|
|
|
|
|
|
}
|
114
|
|
|
|
|
|
|
|
115
|
34
|
|
|
|
|
62
|
my $options = '';
|
116
|
34
|
|
|
|
|
49
|
foreach my $opt_name (qw/create_accessors follow_best_practice/) {
|
117
|
68
|
100
|
|
|
|
192
|
if ($opts->{$opt_name}) {
|
118
|
34
|
|
|
|
|
85
|
$options .= "'$opt_name' => 1, "
|
119
|
|
|
|
|
|
|
}
|
120
|
|
|
|
|
|
|
}
|
121
|
|
|
|
|
|
|
|
122
|
34
|
|
|
|
|
689
|
return <<"CODE";
|
123
|
|
|
|
|
|
|
unless ($class_name->can('_pb_fields_list')) {
|
124
|
|
|
|
|
|
|
Google::ProtocolBuffers->$create_what(
|
125
|
|
|
|
|
|
|
'$class_name',
|
126
|
|
|
|
|
|
|
[
|
127
|
|
|
|
|
|
|
$fields_text
|
128
|
|
|
|
|
|
|
],
|
129
|
|
|
|
|
|
|
{ $options }
|
130
|
|
|
|
|
|
|
);
|
131
|
|
|
|
|
|
|
}
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
CODE
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
}
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
1;
|