Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_field_base.cc

Issue 1842653006: Update //third_party/protobuf to version 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 <limits>
32 #include <sstream>
33
34 #include <google/protobuf/compiler/code_generator.h>
35 #include <google/protobuf/compiler/plugin.h>
36 #include <google/protobuf/descriptor.h>
37 #include <google/protobuf/descriptor.pb.h>
38 #include <google/protobuf/io/coded_stream.h>
39 #include <google/protobuf/io/printer.h>
40 #include <google/protobuf/io/zero_copy_stream.h>
41 #include <google/protobuf/stubs/mathlimits.h>
42 #include <google/protobuf/stubs/strutil.h>
43 #include <google/protobuf/wire_format.h>
44
45 #include <google/protobuf/compiler/csharp/csharp_field_base.h>
46 #include <google/protobuf/compiler/csharp/csharp_helpers.h>
47 #include <google/protobuf/compiler/csharp/csharp_names.h>
48
49 using google::protobuf::internal::scoped_ptr;
50
51 namespace google {
52 namespace protobuf {
53 namespace compiler {
54 namespace csharp {
55
56 void FieldGeneratorBase::SetCommonFieldVariables(
57 map<string, string>* variables) {
58 // Note: this will be valid even though the tag emitted for packed and unpacke d versions of
59 // repeated fields varies by wire format. The wire format is encoded in the bo ttom 3 bits, which
60 // never effects the tag size.
61 int tag_size = internal::WireFormat::TagSize(descriptor_->number(), descriptor _->type());
62 uint tag = internal::WireFormat::MakeTag(descriptor_);
63 uint8 tag_array[5];
64 io::CodedOutputStream::WriteTagToArray(tag, tag_array);
65 string tag_bytes = SimpleItoa(tag_array[0]);
66 for (int i = 1; i < tag_size; i++) {
67 tag_bytes += ", " + SimpleItoa(tag_array[i]);
68 }
69
70 (*variables)["access_level"] = "public";
71 (*variables)["tag"] = SimpleItoa(tag);
72 (*variables)["tag_size"] = SimpleItoa(tag_size);
73 (*variables)["tag_bytes"] = tag_bytes;
74
75 (*variables)["property_name"] = property_name();
76 (*variables)["type_name"] = type_name();
77 (*variables)["name"] = name();
78 (*variables)["descriptor_name"] = descriptor_->name();
79 (*variables)["default_value"] = default_value();
80 if (has_default_value()) {
81 (*variables)["name_def_message"] =
82 (*variables)["name"] + "_ = " + (*variables)["default_value"];
83 } else {
84 (*variables)["name_def_message"] = (*variables)["name"] + "_";
85 }
86 (*variables)["capitalized_type_name"] = capitalized_type_name();
87 (*variables)["number"] = number();
88 (*variables)["has_property_check"] =
89 (*variables)["property_name"] + " != " + (*variables)["default_value"];
90 (*variables)["other_has_property_check"] = "other." +
91 (*variables)["property_name"] + " != " + (*variables)["default_value"];
92 }
93
94 void FieldGeneratorBase::SetCommonOneofFieldVariables(
95 map<string, string>* variables) {
96 (*variables)["oneof_name"] = oneof_name();
97 (*variables)["has_property_check"] = oneof_name() + "Case_ == " + oneof_proper ty_name() +
98 "OneofCase." + property_name();
99 (*variables)["oneof_property_name"] = oneof_property_name();
100 }
101
102 FieldGeneratorBase::FieldGeneratorBase(const FieldDescriptor* descriptor,
103 int fieldOrdinal)
104 : SourceGeneratorBase(descriptor->file()),
105 descriptor_(descriptor),
106 fieldOrdinal_(fieldOrdinal) {
107 SetCommonFieldVariables(&variables_);
108 }
109
110 FieldGeneratorBase::~FieldGeneratorBase() {
111 }
112
113 void FieldGeneratorBase::GenerateFreezingCode(io::Printer* printer) {
114 // No-op: only message fields and repeated fields need
115 // special handling for freezing, so default to not generating any code.
116 }
117
118 void FieldGeneratorBase::GenerateCodecCode(io::Printer* printer) {
119 // No-op: expect this to be overridden by appropriate types.
120 // Could fail if we get called here though...
121 }
122
123 void FieldGeneratorBase::AddDeprecatedFlag(io::Printer* printer) {
124 if (descriptor_->options().deprecated())
125 {
126 printer->Print("[global::System.ObsoleteAttribute()]\n");
127 }
128 }
129
130 void FieldGeneratorBase::AddPublicMemberAttributes(io::Printer* printer) {
131 AddDeprecatedFlag(printer);
132 }
133
134 std::string FieldGeneratorBase::oneof_property_name() {
135 return UnderscoresToCamelCase(descriptor_->containing_oneof()->name(), true);
136 }
137
138 std::string FieldGeneratorBase::oneof_name() {
139 return UnderscoresToCamelCase(descriptor_->containing_oneof()->name(), false);
140 }
141
142 std::string FieldGeneratorBase::property_name() {
143 return GetPropertyName(descriptor_);
144 }
145
146 std::string FieldGeneratorBase::name() {
147 return UnderscoresToCamelCase(GetFieldName(descriptor_), false);
148 }
149
150 std::string FieldGeneratorBase::type_name() {
151 return type_name(descriptor_);
152 }
153
154 std::string FieldGeneratorBase::type_name(const FieldDescriptor* descriptor) {
155 switch (descriptor->type()) {
156 case FieldDescriptor::TYPE_ENUM:
157 return GetClassName(descriptor->enum_type());
158 case FieldDescriptor::TYPE_MESSAGE:
159 case FieldDescriptor::TYPE_GROUP:
160 if (IsWrapperType(descriptor)) {
161 const FieldDescriptor* wrapped_field = descriptor->message_type()->field (0);
162 string wrapped_field_type_name = type_name(wrapped_field);
163 // String and ByteString go to the same type; other wrapped types go to the
164 // nullable equivalent.
165 if (wrapped_field->type() == FieldDescriptor::TYPE_STRING ||
166 wrapped_field->type() == FieldDescriptor::TYPE_BYTES) {
167 return wrapped_field_type_name;
168 } else {
169 return wrapped_field_type_name + "?";
170 }
171 }
172 return GetClassName(descriptor->message_type());
173 case FieldDescriptor::TYPE_DOUBLE:
174 return "double";
175 case FieldDescriptor::TYPE_FLOAT:
176 return "float";
177 case FieldDescriptor::TYPE_INT64:
178 return "long";
179 case FieldDescriptor::TYPE_UINT64:
180 return "ulong";
181 case FieldDescriptor::TYPE_INT32:
182 return "int";
183 case FieldDescriptor::TYPE_FIXED64:
184 return "ulong";
185 case FieldDescriptor::TYPE_FIXED32:
186 return "uint";
187 case FieldDescriptor::TYPE_BOOL:
188 return "bool";
189 case FieldDescriptor::TYPE_STRING:
190 return "string";
191 case FieldDescriptor::TYPE_BYTES:
192 return "pb::ByteString";
193 case FieldDescriptor::TYPE_UINT32:
194 return "uint";
195 case FieldDescriptor::TYPE_SFIXED32:
196 return "int";
197 case FieldDescriptor::TYPE_SFIXED64:
198 return "long";
199 case FieldDescriptor::TYPE_SINT32:
200 return "int";
201 case FieldDescriptor::TYPE_SINT64:
202 return "long";
203 default:
204 GOOGLE_LOG(FATAL)<< "Unknown field type.";
205 return "";
206 }
207 }
208
209 bool FieldGeneratorBase::has_default_value() {
210 switch (descriptor_->type()) {
211 case FieldDescriptor::TYPE_ENUM:
212 case FieldDescriptor::TYPE_MESSAGE:
213 case FieldDescriptor::TYPE_GROUP:
214 return true;
215 case FieldDescriptor::TYPE_DOUBLE:
216 return descriptor_->default_value_double() != 0.0;
217 case FieldDescriptor::TYPE_FLOAT:
218 return descriptor_->default_value_float() != 0.0;
219 case FieldDescriptor::TYPE_INT64:
220 return descriptor_->default_value_int64() != 0L;
221 case FieldDescriptor::TYPE_UINT64:
222 return descriptor_->default_value_uint64() != 0L;
223 case FieldDescriptor::TYPE_INT32:
224 return descriptor_->default_value_int32() != 0;
225 case FieldDescriptor::TYPE_FIXED64:
226 return descriptor_->default_value_uint64() != 0L;
227 case FieldDescriptor::TYPE_FIXED32:
228 return descriptor_->default_value_uint32() != 0;
229 case FieldDescriptor::TYPE_BOOL:
230 return descriptor_->default_value_bool();
231 case FieldDescriptor::TYPE_STRING:
232 return true;
233 case FieldDescriptor::TYPE_BYTES:
234 return true;
235 case FieldDescriptor::TYPE_UINT32:
236 return descriptor_->default_value_uint32() != 0;
237 case FieldDescriptor::TYPE_SFIXED32:
238 return descriptor_->default_value_int32() != 0;
239 case FieldDescriptor::TYPE_SFIXED64:
240 return descriptor_->default_value_int64() != 0L;
241 case FieldDescriptor::TYPE_SINT32:
242 return descriptor_->default_value_int32() != 0;
243 case FieldDescriptor::TYPE_SINT64:
244 return descriptor_->default_value_int64() != 0L;
245 default:
246 GOOGLE_LOG(FATAL)<< "Unknown field type.";
247 return true;
248 }
249 }
250
251 bool FieldGeneratorBase::is_nullable_type() {
252 switch (descriptor_->type()) {
253 case FieldDescriptor::TYPE_ENUM:
254 case FieldDescriptor::TYPE_DOUBLE:
255 case FieldDescriptor::TYPE_FLOAT:
256 case FieldDescriptor::TYPE_INT64:
257 case FieldDescriptor::TYPE_UINT64:
258 case FieldDescriptor::TYPE_INT32:
259 case FieldDescriptor::TYPE_FIXED64:
260 case FieldDescriptor::TYPE_FIXED32:
261 case FieldDescriptor::TYPE_BOOL:
262 case FieldDescriptor::TYPE_UINT32:
263 case FieldDescriptor::TYPE_SFIXED32:
264 case FieldDescriptor::TYPE_SFIXED64:
265 case FieldDescriptor::TYPE_SINT32:
266 case FieldDescriptor::TYPE_SINT64:
267 return false;
268
269 case FieldDescriptor::TYPE_MESSAGE:
270 case FieldDescriptor::TYPE_GROUP:
271 case FieldDescriptor::TYPE_STRING:
272 case FieldDescriptor::TYPE_BYTES:
273 return true;
274
275 default:
276 GOOGLE_LOG(FATAL)<< "Unknown field type.";
277 return true;
278 }
279 }
280
281 bool AllPrintableAscii(const std::string& text) {
282 for(int i = 0; i < text.size(); i++) {
283 if (text[i] < 0x20 || text[i] > 0x7e) {
284 return false;
285 }
286 }
287 return true;
288 }
289
290 std::string FieldGeneratorBase::GetStringDefaultValueInternal() {
291 // No other default values needed for proto3...
292 return "\"\"";
293 }
294
295 std::string FieldGeneratorBase::GetBytesDefaultValueInternal() {
296 // No other default values needed for proto3...
297 return "pb::ByteString.Empty";
298 }
299
300 std::string FieldGeneratorBase::default_value() {
301 return default_value(descriptor_);
302 }
303
304 std::string FieldGeneratorBase::default_value(const FieldDescriptor* descriptor) {
305 switch (descriptor->type()) {
306 case FieldDescriptor::TYPE_ENUM:
307 return type_name() + "." + descriptor->default_value_enum()->name();
308 case FieldDescriptor::TYPE_MESSAGE:
309 case FieldDescriptor::TYPE_GROUP:
310 if (IsWrapperType(descriptor)) {
311 const FieldDescriptor* wrapped_field = descriptor->message_type()->field (0);
312 return default_value(wrapped_field);
313 } else {
314 return "null";
315 }
316 case FieldDescriptor::TYPE_DOUBLE: {
317 double value = descriptor->default_value_double();
318 if (value == numeric_limits<double>::infinity()) {
319 return "double.PositiveInfinity";
320 } else if (value == -numeric_limits<double>::infinity()) {
321 return "double.NegativeInfinity";
322 } else if (MathLimits<double>::IsNaN(value)) {
323 return "double.NaN";
324 }
325 return SimpleDtoa(value) + "D";
326 }
327 case FieldDescriptor::TYPE_FLOAT: {
328 float value = descriptor->default_value_float();
329 if (value == numeric_limits<float>::infinity()) {
330 return "float.PositiveInfinity";
331 } else if (value == -numeric_limits<float>::infinity()) {
332 return "float.NegativeInfinity";
333 } else if (MathLimits<float>::IsNaN(value)) {
334 return "float.NaN";
335 }
336 return SimpleFtoa(value) + "F";
337 }
338 case FieldDescriptor::TYPE_INT64:
339 return SimpleItoa(descriptor->default_value_int64()) + "L";
340 case FieldDescriptor::TYPE_UINT64:
341 return SimpleItoa(descriptor->default_value_uint64()) + "UL";
342 case FieldDescriptor::TYPE_INT32:
343 return SimpleItoa(descriptor->default_value_int32());
344 case FieldDescriptor::TYPE_FIXED64:
345 return SimpleItoa(descriptor->default_value_uint64()) + "UL";
346 case FieldDescriptor::TYPE_FIXED32:
347 return SimpleItoa(descriptor->default_value_uint32());
348 case FieldDescriptor::TYPE_BOOL:
349 if (descriptor->default_value_bool()) {
350 return "true";
351 } else {
352 return "false";
353 }
354 case FieldDescriptor::TYPE_STRING:
355 return GetStringDefaultValueInternal();
356 case FieldDescriptor::TYPE_BYTES:
357 return GetBytesDefaultValueInternal();
358 case FieldDescriptor::TYPE_UINT32:
359 return SimpleItoa(descriptor->default_value_uint32());
360 case FieldDescriptor::TYPE_SFIXED32:
361 return SimpleItoa(descriptor->default_value_int32());
362 case FieldDescriptor::TYPE_SFIXED64:
363 return SimpleItoa(descriptor->default_value_int64()) + "L";
364 case FieldDescriptor::TYPE_SINT32:
365 return SimpleItoa(descriptor->default_value_int32());
366 case FieldDescriptor::TYPE_SINT64:
367 return SimpleItoa(descriptor->default_value_int64()) + "L";
368 default:
369 GOOGLE_LOG(FATAL)<< "Unknown field type.";
370 return "";
371 }
372 }
373
374 std::string FieldGeneratorBase::number() {
375 return SimpleItoa(descriptor_->number());
376 }
377
378 std::string FieldGeneratorBase::capitalized_type_name() {
379 switch (descriptor_->type()) {
380 case FieldDescriptor::TYPE_ENUM:
381 return "Enum";
382 case FieldDescriptor::TYPE_MESSAGE:
383 return "Message";
384 case FieldDescriptor::TYPE_GROUP:
385 return "Group";
386 case FieldDescriptor::TYPE_DOUBLE:
387 return "Double";
388 case FieldDescriptor::TYPE_FLOAT:
389 return "Float";
390 case FieldDescriptor::TYPE_INT64:
391 return "Int64";
392 case FieldDescriptor::TYPE_UINT64:
393 return "UInt64";
394 case FieldDescriptor::TYPE_INT32:
395 return "Int32";
396 case FieldDescriptor::TYPE_FIXED64:
397 return "Fixed64";
398 case FieldDescriptor::TYPE_FIXED32:
399 return "Fixed32";
400 case FieldDescriptor::TYPE_BOOL:
401 return "Bool";
402 case FieldDescriptor::TYPE_STRING:
403 return "String";
404 case FieldDescriptor::TYPE_BYTES:
405 return "Bytes";
406 case FieldDescriptor::TYPE_UINT32:
407 return "UInt32";
408 case FieldDescriptor::TYPE_SFIXED32:
409 return "SFixed32";
410 case FieldDescriptor::TYPE_SFIXED64:
411 return "SFixed64";
412 case FieldDescriptor::TYPE_SINT32:
413 return "SInt32";
414 case FieldDescriptor::TYPE_SINT64:
415 return "SInt64";
416 default:
417 GOOGLE_LOG(FATAL)<< "Unknown field type.";
418 return "";
419 }
420 }
421
422 } // namespace csharp
423 } // namespace compiler
424 } // namespace protobuf
425 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698