csharp_field_base.cc
Go to the documentation of this file.
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 <cmath>
32 #include <limits>
33 #include <sstream>
34 
43 
47 
48 namespace google {
49 namespace protobuf {
50 namespace compiler {
51 namespace csharp {
52 
54  std::map<string, string>* variables) {
55  // Note: this will be valid even though the tag emitted for packed and unpacked versions of
56  // repeated fields varies by wire format. The wire format is encoded in the bottom 3 bits, which
57  // never effects the tag size.
59  int part_tag_size = tag_size;
61  part_tag_size /= 2;
62  }
64  uint8 tag_array[5];
66  string tag_bytes = StrCat(tag_array[0]);
67  for (int i = 1; i < part_tag_size; i++) {
68  tag_bytes += ", " + StrCat(tag_array[i]);
69  }
70 
71  (*variables)["tag"] = StrCat(tag);
72  (*variables)["tag_size"] = StrCat(tag_size);
73  (*variables)["tag_bytes"] = tag_bytes;
74 
75  if (descriptor_->type() == FieldDescriptor::Type::TYPE_GROUP) {
80  tag_bytes = StrCat(tag_array[0]);
81  for (int i = 1; i < part_tag_size; i++) {
82  tag_bytes += ", " + StrCat(tag_array[i]);
83  }
84 
85  variables_["end_tag"] = StrCat(tag);
86  variables_["end_tag_bytes"] = tag_bytes;
87  }
88 
89  (*variables)["access_level"] = "public";
90 
91  (*variables)["property_name"] = property_name();
92  (*variables)["type_name"] = type_name();
93  (*variables)["extended_type"] = GetClassName(descriptor_->containing_type());
94  (*variables)["name"] = name();
95  (*variables)["descriptor_name"] = descriptor_->name();
96  (*variables)["default_value"] = default_value();
97  (*variables)["capitalized_type_name"] = capitalized_type_name();
98  (*variables)["number"] = number();
100  (*variables)["name_def_message"] =
101  (*variables)["name"] + "_ = " + (*variables)["default_value"];
102  } else {
103  (*variables)["name_def_message"] = (*variables)["name"] + "_";
104  }
105  if (IsProto2(descriptor_->file())) {
106  (*variables)["has_property_check"] = "Has" + (*variables)["property_name"];
107  (*variables)["other_has_property_check"] = "other.Has" + (*variables)["property_name"];
108  (*variables)["has_not_property_check"] = "!" + (*variables)["has_property_check"];
109  (*variables)["other_has_not_property_check"] = "!" + (*variables)["other_has_property_check"];
110  if (presenceIndex_ != -1) {
111  string hasBitsNumber = StrCat(presenceIndex_ / 32);
112  string hasBitsMask = StrCat(1 << (presenceIndex_ % 32));
113  (*variables)["has_field_check"] = "(_hasBits" + hasBitsNumber + " & " + hasBitsMask + ") != 0";
114  (*variables)["set_has_field"] = "_hasBits" + hasBitsNumber + " |= " + hasBitsMask;
115  (*variables)["clear_has_field"] = "_hasBits" + hasBitsNumber + " &= ~" + hasBitsMask;
116  }
117  } else {
118  (*variables)["has_property_check"] =
119  (*variables)["property_name"] + " != " + (*variables)["default_value"];
120  (*variables)["other_has_property_check"] = "other." +
121  (*variables)["property_name"] + " != " + (*variables)["default_value"];
122  }
123 }
124 
126  std::map<string, string>* variables) {
127  (*variables)["oneof_name"] = oneof_name();
128  if (IsProto2(descriptor_->file())) {
129  (*variables)["has_property_check"] = "Has" + property_name();
130  } else {
131  (*variables)["has_property_check"] =
132  oneof_name() + "Case_ == " + oneof_property_name() +
133  "OneofCase." + property_name();
134  }
135  (*variables)["oneof_property_name"] = oneof_property_name();
136 }
137 
139  int presenceIndex, const Options* options)
142  presenceIndex_(presenceIndex) {
144 }
145 
147 }
148 
150  // No-op: only message fields and repeated fields need
151  // special handling for freezing, so default to not generating any code.
152 }
153 
155  // No-op: expect this to be overridden by appropriate types.
156  // Could fail if we get called here though...
157 }
158 
160  // No-op: only message fields, enum fields, primitives,
161  // and repeated fields need this default is to not generate any code
162 }
163 
165  if (descriptor_->options().deprecated()) {
166  printer->Print("[global::System.ObsoleteAttribute]\n");
169  printer->Print("[global::System.ObsoleteAttribute]\n");
170  }
171 }
172 
174  AddDeprecatedFlag(printer);
176 }
177 
180 }
181 
184 }
185 
188 }
189 
192 }
193 
195  return type_name(descriptor_);
196 }
197 
199  switch (descriptor->type()) {
201  return GetClassName(descriptor->enum_type());
204  if (IsWrapperType(descriptor)) {
205  const FieldDescriptor* wrapped_field =
206  descriptor->message_type()->field(0);
207  string wrapped_field_type_name = type_name(wrapped_field);
208  // String and ByteString go to the same type; other wrapped types
209  // go to the nullable equivalent.
210  if (wrapped_field->type() == FieldDescriptor::TYPE_STRING ||
211  wrapped_field->type() == FieldDescriptor::TYPE_BYTES) {
212  return wrapped_field_type_name;
213  } else {
214  return wrapped_field_type_name + "?";
215  }
216  }
217  return GetClassName(descriptor->message_type());
219  return "double";
221  return "float";
223  return "long";
225  return "ulong";
227  return "int";
229  return "ulong";
231  return "uint";
233  return "bool";
235  return "string";
237  return "pb::ByteString";
239  return "uint";
241  return "int";
243  return "long";
245  return "int";
247  return "long";
248  default:
249  GOOGLE_LOG(FATAL)<< "Unknown field type.";
250  return "";
251  }
252 }
253 
255  switch (descriptor_->type()) {
259  return true;
261  return descriptor_->default_value_double() != 0.0;
263  return descriptor_->default_value_float() != 0.0;
265  return descriptor_->default_value_int64() != 0L;
267  return descriptor_->default_value_uint64() != 0L;
269  return descriptor_->default_value_int32() != 0;
271  return descriptor_->default_value_uint64() != 0L;
273  return descriptor_->default_value_uint32() != 0;
277  return true;
279  return true;
281  return descriptor_->default_value_uint32() != 0;
283  return descriptor_->default_value_int32() != 0;
285  return descriptor_->default_value_int64() != 0L;
287  return descriptor_->default_value_int32() != 0;
289  return descriptor_->default_value_int64() != 0L;
290  default:
291  GOOGLE_LOG(FATAL)<< "Unknown field type.";
292  return true;
293  }
294 }
295 
296 bool AllPrintableAscii(const std::string& text) {
297  for(int i = 0; i < text.size(); i++) {
298  if (text[i] < 0x20 || text[i] > 0x7e) {
299  return false;
300  }
301  }
302  return true;
303 }
304 
306  if (descriptor->default_value_string().empty())
307  return "\"\"";
308  else
309  return "global::System.Encoding.UTF8.GetString(global::System.Convert.FromBase64String(\" +" + StringToBase64(descriptor->default_value_string()) + " +\"))";
310 }
311 
313  if (descriptor->default_value_string().empty())
314  return "pb::ByteString.Empty";
315  else
316  return "pb::ByteString.FromBase64(\"" + StringToBase64(descriptor->default_value_string()) + "\")";
317 }
318 
320  return default_value(descriptor_);
321 }
322 
324  switch (descriptor->type()) {
326  return GetClassName(descriptor->default_value_enum()->type()) + "." +
327  GetEnumValueName(descriptor->default_value_enum()->type()->name(), descriptor->default_value_enum()->name());
330  if (IsWrapperType(descriptor)) {
331  const FieldDescriptor* wrapped_field = descriptor->message_type()->field(0);
332  return default_value(wrapped_field);
333  } else {
334  return "null";
335  }
337  double value = descriptor->default_value_double();
338  if (value == std::numeric_limits<double>::infinity()) {
339  return "double.PositiveInfinity";
340  } else if (value == -std::numeric_limits<double>::infinity()) {
341  return "double.NegativeInfinity";
342  } else if (std::isnan(value)) {
343  return "double.NaN";
344  }
345  return StrCat(value) + "D";
346  }
348  float value = descriptor->default_value_float();
349  if (value == std::numeric_limits<float>::infinity()) {
350  return "float.PositiveInfinity";
351  } else if (value == -std::numeric_limits<float>::infinity()) {
352  return "float.NegativeInfinity";
353  } else if (std::isnan(value)) {
354  return "float.NaN";
355  }
356  return StrCat(value) + "F";
357  }
359  return StrCat(descriptor->default_value_int64()) + "L";
361  return StrCat(descriptor->default_value_uint64()) + "UL";
363  return StrCat(descriptor->default_value_int32());
365  return StrCat(descriptor->default_value_uint64()) + "UL";
367  return StrCat(descriptor->default_value_uint32());
369  if (descriptor->default_value_bool()) {
370  return "true";
371  } else {
372  return "false";
373  }
379  return StrCat(descriptor->default_value_uint32());
381  return StrCat(descriptor->default_value_int32());
383  return StrCat(descriptor->default_value_int64()) + "L";
385  return StrCat(descriptor->default_value_int32());
387  return StrCat(descriptor->default_value_int64()) + "L";
388  default:
389  GOOGLE_LOG(FATAL)<< "Unknown field type.";
390  return "";
391  }
392 }
393 
395  return StrCat(descriptor_->number());
396 }
397 
399  switch (descriptor_->type()) {
401  return "Enum";
403  return "Message";
405  return "Group";
407  return "Double";
409  return "Float";
411  return "Int64";
413  return "UInt64";
415  return "Int32";
417  return "Fixed64";
419  return "Fixed32";
421  return "Bool";
423  return "String";
425  return "Bytes";
427  return "UInt32";
429  return "SFixed32";
431  return "SFixed64";
433  return "SInt32";
435  return "SInt64";
436  default:
437  GOOGLE_LOG(FATAL)<< "Unknown field type.";
438  return "";
439  }
440 }
441 
442 } // namespace csharp
443 } // namespace compiler
444 } // namespace protobuf
445 } // namespace google
google::protobuf::FieldDescriptor::default_value_int32
int32 default_value_int32() const
google::protobuf::compiler::csharp::FieldGeneratorBase::GetStringDefaultValueInternal
std::string GetStringDefaultValueInternal(const FieldDescriptor *descriptor)
Definition: csharp_field_base.cc:305
zero_copy_stream.h
google::protobuf::io::Printer::Print
void Print(const std::map< std::string, std::string > &variables, const char *text)
Definition: printer.cc:112
google::protobuf::FieldDescriptor::TYPE_SINT64
@ TYPE_SINT64
Definition: src/google/protobuf/descriptor.h:544
google::protobuf::FieldDescriptor::options
const FieldOptions & options() const
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
google::protobuf.internal::WireFormat::TagSize
static size_t TagSize(int field_number, FieldDescriptor::Type type)
Definition: wire_format.h:293
MessageOptions::deprecated
bool deprecated() const
Definition: descriptor.pb.h:10496
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
google::protobuf::FieldDescriptor::TYPE_SINT32
@ TYPE_SINT32
Definition: src/google/protobuf/descriptor.h:543
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
google::protobuf::uint8
uint8_t uint8
Definition: protobuf/src/google/protobuf/stubs/port.h:153
FATAL
const int FATAL
Definition: log_severity.h:60
google::protobuf::compiler::csharp::SourceGeneratorBase::WriteGeneratedCodeAttributes
void WriteGeneratedCodeAttributes(io::Printer *printer)
Definition: csharp_source_generator_base.cc:57
google::protobuf::FieldDescriptor::TYPE_BYTES
@ TYPE_BYTES
Definition: src/google/protobuf/descriptor.h:538
google::protobuf::compiler::csharp::StringToBase64
std::string StringToBase64(const std::string &input)
Definition: csharp_helpers.cc:440
google::protobuf::compiler::csharp::FieldGeneratorBase::GenerateExtensionCode
virtual void GenerateExtensionCode(io::Printer *printer)
Definition: csharp_field_base.cc:159
google::protobuf::FieldDescriptor::containing_type
const Descriptor * containing_type() const
google::protobuf::FieldDescriptor::default_value_bool
bool default_value_bool() const
google::protobuf.internal::WireFormat::MakeTag
static uint32 MakeTag(const FieldDescriptor *field)
Definition: wire_format.h:289
google::protobuf::compiler::csharp::FieldGeneratorBase::SetCommonOneofFieldVariables
void SetCommonOneofFieldVariables(std::map< string, string > *variables)
Definition: csharp_field_base.cc:125
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf.internal::WireFormatLite::WIRETYPE_END_GROUP
@ WIRETYPE_END_GROUP
Definition: wire_format_lite.h:106
google::protobuf::FieldDescriptor::default_value_double
double default_value_double() const
google::protobuf::compiler::csharp::FieldGeneratorBase::descriptor_
const FieldDescriptor * descriptor_
Definition: csharp_field_base.h:73
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::OneofDescriptor::name
const std::string & name() const
google::protobuf::compiler::csharp::FieldGeneratorBase::name
std::string name()
Definition: csharp_field_base.cc:190
google::protobuf::compiler::csharp::FieldGeneratorBase::GetBytesDefaultValueInternal
std::string GetBytesDefaultValueInternal(const FieldDescriptor *descriptor)
Definition: csharp_field_base.cc:312
google::protobuf::compiler::csharp::IsProto2
bool IsProto2(const FileDescriptor *descriptor)
Definition: csharp_helpers.h:156
google::protobuf::compiler::csharp::FieldGeneratorBase::AddDeprecatedFlag
void AddDeprecatedFlag(io::Printer *printer)
Definition: csharp_field_base.cc:164
google::protobuf::compiler::csharp::FieldGeneratorBase::variables_
std::map< string, string > variables_
Definition: csharp_field_base.h:75
google::protobuf::compiler::csharp::FieldGeneratorBase::default_value
std::string default_value()
Definition: csharp_field_base.cc:319
google::protobuf::Descriptor::options
const MessageOptions & options() const
google::protobuf::compiler::csharp::Options
Definition: csharp_options.h:42
google::protobuf::compiler::csharp::GetFieldName
std::string GetFieldName(const FieldDescriptor *descriptor)
Definition: csharp_helpers.cc:344
strutil.h
google::protobuf::FieldDescriptor::TYPE_UINT32
@ TYPE_UINT32
Definition: src/google/protobuf/descriptor.h:539
google::protobuf::FieldDescriptor::file
const FileDescriptor * file() const
google::protobuf::compiler::csharp::FieldGeneratorBase::SetCommonFieldVariables
void SetCommonFieldVariables(std::map< string, string > *variables)
Definition: csharp_field_base.cc:53
coded_stream.h
google::protobuf::compiler::csharp::FieldGeneratorBase::oneof_name
std::string oneof_name()
Definition: csharp_field_base.cc:182
google::protobuf::compiler::csharp::FieldGeneratorBase::AddPublicMemberAttributes
void AddPublicMemberAttributes(io::Printer *printer)
Definition: csharp_field_base.cc:173
FieldOptions::deprecated
bool deprecated() const
Definition: descriptor.pb.h:10640
google::protobuf::compiler::csharp::FieldGeneratorBase::GenerateFreezingCode
virtual void GenerateFreezingCode(io::Printer *printer)
Definition: csharp_field_base.cc:149
printer.h
google::protobuf::FieldDescriptor::number
int number() const
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: logging.h:146
google::protobuf::FieldDescriptor::TYPE_BOOL
@ TYPE_BOOL
Definition: src/google/protobuf/descriptor.h:533
google::protobuf::FieldDescriptor::TYPE_STRING
@ TYPE_STRING
Definition: src/google/protobuf/descriptor.h:534
code_generator.h
google::protobuf.internal::WireFormatLite::MakeTag
constexpr static uint32 MakeTag(int field_number, WireType type)
Definition: wire_format_lite.h:768
google::protobuf::compiler::csharp::FieldGeneratorBase::has_default_value
bool has_default_value()
Definition: csharp_field_base.cc:254
google::protobuf::FieldDescriptor::default_value_uint32
uint32 default_value_uint32() const
google::protobuf::compiler::csharp::GetPropertyName
std::string GetPropertyName(const FieldDescriptor *descriptor)
Definition: csharp_helpers.cc:356
google::protobuf::uint
unsigned int uint
Definition: protobuf/src/google/protobuf/stubs/port.h:135
google::protobuf::compiler::csharp::FieldGeneratorBase::presenceIndex_
const int presenceIndex_
Definition: csharp_field_base.h:74
google::protobuf::io::Printer
Definition: printer.h:181
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::FieldDescriptor::default_value_int64
int64 default_value_int64() const
google::protobuf::FieldDescriptor::TYPE_MESSAGE
@ TYPE_MESSAGE
Definition: src/google/protobuf/descriptor.h:536
google::protobuf::FieldDescriptor::TYPE_DOUBLE
@ TYPE_DOUBLE
Definition: src/google/protobuf/descriptor.h:522
csharp_helpers.h
google::protobuf::FieldDescriptor::TYPE_INT32
@ TYPE_INT32
Definition: src/google/protobuf/descriptor.h:528
google::protobuf::FieldDescriptor::TYPE_FLOAT
@ TYPE_FLOAT
Definition: src/google/protobuf/descriptor.h:523
google::protobuf::FieldDescriptor::default_value_float
float default_value_float() const
google::protobuf::FieldDescriptor::type
Type type() const
Definition: src/google/protobuf/descriptor.h:2052
csharp_field_base.h
google::protobuf::compiler::csharp::GetClassName
std::string GetClassName(const Descriptor *descriptor)
Definition: csharp_helpers.cc:333
google::protobuf::compiler::csharp::FieldGeneratorBase::capitalized_type_name
std::string capitalized_type_name()
Definition: csharp_field_base.cc:398
google::protobuf::FieldDescriptor::name
const std::string & name() const
google::protobuf::compiler::csharp::AllPrintableAscii
bool AllPrintableAscii(const std::string &text)
Definition: csharp_field_base.cc:296
google::protobuf::compiler::csharp::FieldGeneratorBase::property_name
std::string property_name()
Definition: csharp_field_base.cc:186
google::protobuf::FieldDescriptor::TYPE_FIXED64
@ TYPE_FIXED64
Definition: src/google/protobuf/descriptor.h:531
google::protobuf::compiler::csharp::FieldGeneratorBase::oneof_property_name
std::string oneof_property_name()
Definition: csharp_field_base.cc:178
csharp_names.h
google::protobuf::compiler::csharp::SourceGeneratorBase
Definition: csharp_source_generator_base.h:46
wire_format.h
descriptor.h
google::protobuf::FieldDescriptor::TYPE_GROUP
@ TYPE_GROUP
Definition: src/google/protobuf/descriptor.h:535
google::protobuf::compiler::csharp::IsWrapperType
bool IsWrapperType(const FieldDescriptor *descriptor)
Definition: csharp_helpers.h:151
google::protobuf::FieldDescriptor::TYPE_SFIXED32
@ TYPE_SFIXED32
Definition: src/google/protobuf/descriptor.h:541
google::protobuf::FieldDescriptor::default_value_uint64
uint64 default_value_uint64() const
google::protobuf::FieldDescriptor::message_type
const Descriptor * message_type() const
Definition: src/google/protobuf/descriptor.cc:7228
google::protobuf::FieldDescriptor::TYPE_UINT64
@ TYPE_UINT64
Definition: src/google/protobuf/descriptor.h:527
google::protobuf::io::CodedOutputStream::WriteTagToArray
static PROTOBUF_ALWAYS_INLINE uint8 * WriteTagToArray(uint32 value, uint8 *target)
Definition: coded_stream.h:1241
google::protobuf::FieldDescriptor::containing_oneof
const OneofDescriptor * containing_oneof() const
google::protobuf::FieldDescriptor::TYPE_INT64
@ TYPE_INT64
Definition: src/google/protobuf/descriptor.h:524
google::protobuf::compiler::csharp::FieldGeneratorBase::GenerateCodecCode
virtual void GenerateCodecCode(io::Printer *printer)
Definition: csharp_field_base.cc:154
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
google::protobuf::compiler::csharp::UnderscoresToCamelCase
std::string UnderscoresToCamelCase(const std::string &input, bool cap_next_letter, bool preserve_period)
Definition: csharp_helpers.cc:143
descriptor_
const Descriptor * descriptor_
Definition: field_comparator_test.cc:56
descriptor.pb.h
google::protobuf::compiler::csharp::GetEnumValueName
std::string GetEnumValueName(const std::string &enum_name, const std::string &enum_value_name)
Definition: csharp_helpers.cc:274
google::protobuf::compiler::csharp::FieldGeneratorBase::number
std::string number()
Definition: csharp_field_base.cc:394
google::protobuf::FieldDescriptor::TYPE_ENUM
@ TYPE_ENUM
Definition: src/google/protobuf/descriptor.h:540
google::protobuf::FieldDescriptor::TYPE_SFIXED64
@ TYPE_SFIXED64
Definition: src/google/protobuf/descriptor.h:542
google::protobuf::compiler::csharp::FieldGeneratorBase::type_name
std::string type_name()
Definition: csharp_field_base.cc:194
google::protobuf::compiler::csharp::FieldGeneratorBase::~FieldGeneratorBase
~FieldGeneratorBase()
Definition: csharp_field_base.cc:146
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
isnan
#define isnan(d)
Definition: cJSON.c:77
google::protobuf::compiler::csharp::FieldGeneratorBase::FieldGeneratorBase
FieldGeneratorBase(const FieldDescriptor *descriptor, int presenceIndex, const Options *options)
Definition: csharp_field_base.cc:138
google::protobuf::FieldDescriptor::TYPE_FIXED32
@ TYPE_FIXED32
Definition: src/google/protobuf/descriptor.h:532


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:49