Line data Source code
1 : // Protocol Buffers - Google's data interchange format
2 : // Copyright 2008 Google Inc. All rights reserved.
3 : // https://developers.google.com/protocol-buffers/
4 : //
5 : // Redistribution and use in source and binary forms, with or without
6 : // modification, are permitted provided that the following conditions are
7 : // met:
8 : //
9 : // * Redistributions of source code must retain the above copyright
10 : // notice, this list of conditions and the following disclaimer.
11 : // * Redistributions in binary form must reproduce the above
12 : // copyright notice, this list of conditions and the following disclaimer
13 : // in the documentation and/or other materials provided with the
14 : // distribution.
15 : // * Neither the name of Google Inc. nor the names of its
16 : // contributors may be used to endorse or promote products derived from
17 : // this software without specific prior written permission.
18 : //
19 : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 : // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 : // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 : // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 : // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 : // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 : // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 : // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 : // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 : // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 :
31 : #include <sstream>
32 :
33 : #include <google/protobuf/compiler/code_generator.h>
34 : #include <google/protobuf/compiler/plugin.h>
35 : #include <google/protobuf/descriptor.h>
36 : #include <google/protobuf/descriptor.pb.h>
37 : #include <google/protobuf/io/printer.h>
38 : #include <google/protobuf/io/zero_copy_stream.h>
39 : #include <google/protobuf/stubs/strutil.h>
40 :
41 : #include <google/protobuf/compiler/csharp/csharp_helpers.h>
42 : #include <google/protobuf/compiler/csharp/csharp_primitive_field.h>
43 :
44 : namespace google {
45 : namespace protobuf {
46 : namespace compiler {
47 : namespace csharp {
48 :
49 0 : PrimitiveFieldGenerator::PrimitiveFieldGenerator(
50 0 : const FieldDescriptor* descriptor, int fieldOrdinal)
51 0 : : FieldGeneratorBase(descriptor, fieldOrdinal) {
52 : // TODO(jonskeet): Make this cleaner...
53 0 : is_value_type = descriptor->type() != FieldDescriptor::TYPE_STRING
54 0 : && descriptor->type() != FieldDescriptor::TYPE_BYTES;
55 0 : if (!is_value_type) {
56 0 : variables_["has_property_check"] = variables_["property_name"] + ".Length != 0";
57 0 : variables_["other_has_property_check"] = "other." + variables_["property_name"] + ".Length != 0";
58 : }
59 0 : }
60 :
61 0 : PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {
62 0 : }
63 :
64 0 : void PrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) {
65 : // TODO(jonskeet): Work out whether we want to prevent the fields from ever being
66 : // null, or whether we just handle it, in the cases of bytes and string.
67 : // (Basically, should null-handling code be in the getter or the setter?)
68 : printer->Print(
69 : variables_,
70 0 : "private $type_name$ $name_def_message$;\n");
71 0 : AddDeprecatedFlag(printer);
72 : printer->Print(
73 : variables_,
74 : "$access_level$ $type_name$ $property_name$ {\n"
75 : " get { return $name$_; }\n"
76 0 : " set {\n");
77 0 : if (is_value_type) {
78 : printer->Print(
79 : variables_,
80 0 : " $name$_ = value;\n");
81 : } else {
82 : printer->Print(
83 : variables_,
84 0 : " $name$_ = pb::Preconditions.CheckNotNull(value, \"value\");\n");
85 : }
86 : printer->Print(
87 : " }\n"
88 0 : "}\n");
89 0 : }
90 :
91 0 : void PrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) {
92 : printer->Print(
93 : variables_,
94 : "if ($other_has_property_check$) {\n"
95 : " $property_name$ = other.$property_name$;\n"
96 0 : "}\n");
97 0 : }
98 :
99 0 : void PrimitiveFieldGenerator::GenerateParsingCode(io::Printer* printer) {
100 : // Note: invoke the property setter rather than writing straight to the field,
101 : // so that we can normalize "null to empty" for strings and bytes.
102 : printer->Print(
103 : variables_,
104 0 : "$property_name$ = input.Read$capitalized_type_name$();\n");
105 0 : }
106 :
107 0 : void PrimitiveFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
108 : printer->Print(
109 : variables_,
110 : "if ($has_property_check$) {\n"
111 : " output.WriteRawTag($tag_bytes$);\n"
112 : " output.Write$capitalized_type_name$($property_name$);\n"
113 0 : "}\n");
114 0 : }
115 :
116 0 : void PrimitiveFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
117 : printer->Print(
118 : variables_,
119 0 : "if ($has_property_check$) {\n");
120 0 : printer->Indent();
121 0 : int fixedSize = GetFixedSize(descriptor_->type());
122 0 : if (fixedSize == -1) {
123 : printer->Print(
124 : variables_,
125 0 : "size += $tag_size$ + pb::CodedOutputStream.Compute$capitalized_type_name$Size($property_name$);\n");
126 : } else {
127 : printer->Print(
128 : "size += $tag_size$ + $fixed_size$;\n",
129 : "fixed_size", SimpleItoa(fixedSize),
130 0 : "tag_size", variables_["tag_size"]);
131 : }
132 0 : printer->Outdent();
133 0 : printer->Print("}\n");
134 0 : }
135 :
136 0 : void PrimitiveFieldGenerator::WriteHash(io::Printer* printer) {
137 : printer->Print(
138 : variables_,
139 0 : "if ($has_property_check$) hash ^= $property_name$.GetHashCode();\n");
140 0 : }
141 0 : void PrimitiveFieldGenerator::WriteEquals(io::Printer* printer) {
142 : printer->Print(
143 : variables_,
144 0 : "if ($property_name$ != other.$property_name$) return false;\n");
145 0 : }
146 0 : void PrimitiveFieldGenerator::WriteToString(io::Printer* printer) {
147 : printer->Print(
148 : variables_,
149 0 : "PrintField(\"$descriptor_name$\", $has_property_check$, $property_name$, writer);\n");
150 0 : }
151 :
152 0 : void PrimitiveFieldGenerator::GenerateCloningCode(io::Printer* printer) {
153 : printer->Print(variables_,
154 0 : "$name$_ = other.$name$_;\n");
155 0 : }
156 :
157 0 : void PrimitiveFieldGenerator::GenerateCodecCode(io::Printer* printer) {
158 : printer->Print(
159 : variables_,
160 0 : "pb::FieldCodec.For$capitalized_type_name$($tag$)");
161 0 : }
162 :
163 0 : PrimitiveOneofFieldGenerator::PrimitiveOneofFieldGenerator(
164 : const FieldDescriptor* descriptor, int fieldOrdinal)
165 0 : : PrimitiveFieldGenerator(descriptor, fieldOrdinal) {
166 0 : SetCommonOneofFieldVariables(&variables_);
167 0 : }
168 :
169 0 : PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator() {
170 0 : }
171 :
172 0 : void PrimitiveOneofFieldGenerator::GenerateMembers(io::Printer* printer) {
173 0 : AddDeprecatedFlag(printer);
174 : printer->Print(
175 : variables_,
176 : "$access_level$ $type_name$ $property_name$ {\n"
177 : " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : $default_value$; }\n"
178 0 : " set {\n");
179 0 : if (is_value_type) {
180 : printer->Print(
181 : variables_,
182 0 : " $oneof_name$_ = value;\n");
183 : } else {
184 : printer->Print(
185 : variables_,
186 0 : " $oneof_name$_ = pb::Preconditions.CheckNotNull(value, \"value\");\n");
187 : }
188 : printer->Print(
189 : variables_,
190 : " $oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
191 : " }\n"
192 0 : "}\n");
193 0 : }
194 :
195 0 : void PrimitiveOneofFieldGenerator::WriteToString(io::Printer* printer) {
196 : printer->Print(variables_,
197 0 : "PrintField(\"$descriptor_name$\", $has_property_check$, $oneof_name$_, writer);\n");
198 0 : }
199 :
200 0 : void PrimitiveOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) {
201 : printer->Print(
202 : variables_,
203 0 : "$property_name$ = input.Read$capitalized_type_name$();\n");
204 0 : }
205 :
206 0 : void PrimitiveOneofFieldGenerator::GenerateCloningCode(io::Printer* printer) {
207 : printer->Print(variables_,
208 0 : "$property_name$ = other.$property_name$;\n");
209 0 : }
210 :
211 : } // namespace csharp
212 : } // namespace compiler
213 : } // namespace protobuf
214 : } // namespace google
|