Go to the documentation of this file.
35 #include <google/protobuf/compiler/code_generator.h>
36 #include <google/protobuf/descriptor.h>
37 #include <google/protobuf/descriptor.pb.h>
38 #include <google/protobuf/io/printer.h>
39 #include <google/protobuf/io/zero_copy_stream.h>
40 #include <google/protobuf/stubs/strutil.h>
41 #include <google/protobuf/wire_format.h>
42 #include <google/protobuf/wire_format_lite.h>
44 #include <google/protobuf/compiler/csharp/csharp_options.h>
45 #include <google/protobuf/compiler/csharp/csharp_doc_comment.h>
46 #include <google/protobuf/compiler/csharp/csharp_enum.h>
47 #include <google/protobuf/compiler/csharp/csharp_field_base.h>
48 #include <google/protobuf/compiler/csharp/csharp_helpers.h>
49 #include <google/protobuf/compiler/csharp/csharp_message.h>
50 #include <google/protobuf/compiler/csharp/csharp_names.h>
58 return d1->number() <
d2->number();
65 has_bit_field_count_(0),
67 has_extension_ranges_(
descriptor->extension_range_count() > 0) {
75 int presence_bit_count = 0;
104 printer->Print(
"[global::System.ObsoleteAttribute]\n");
109 if (this->
options()->serializable) {
110 printer->Print(
"[global::System.SerializableAttribute]\n");
115 std::map<std::string, std::string> vars;
125 "$access_level$ sealed partial class $class_name$ : ");
128 printer->Print(vars,
"pb::IExtendableMessage<$class_name$>\n");
131 printer->Print(vars,
"pb::IMessage<$class_name$>\n");
133 printer->Print(
"#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE\n");
134 printer->Print(
" , pb::IBufferMessage\n");
135 printer->Print(
"#endif\n");
136 printer->Print(
"{\n");
142 "private static readonly pb::MessageParser<$class_name$> _parser = new pb::MessageParser<$class_name$>(() => new $class_name$());\n");
145 "private pb::UnknownFieldSet _unknownFields;\n");
149 printer->Print(vars,
"internal pb::ExtensionSet<$class_name$> _extensions;\n");
151 printer->Print(vars,
"private pb::ExtensionSet<$class_name$> _extensions;\n");
157 "private pb::ExtensionSet<$class_name$> _Extensions { get { "
158 "return _extensions; } }\n");
164 printer->Print(
"private int _hasBits$i$;\n",
"i",
StrCat(i));
171 "public static pb::MessageParser<$class_name$> Parser { get { return _parser; } }\n\n");
185 "public static pbr::MessageDescriptor Descriptor {\n"
186 " get { return $descriptor_accessor$; }\n"
192 "pbr::MessageDescriptor pb::IMessage.Descriptor {\n"
193 " get { return Descriptor; }\n"
201 "public $class_name$() {\n"
202 " OnConstruction();\n"
204 "partial void OnConstruction();\n\n");
215 "/// <summary>Field number for the \"$field_name$\" field.</summary>\n"
216 "public const int $field_constant_name$ = $index$;\n",
217 "field_name", fieldDescriptor->name(),
219 "index",
StrCat(fieldDescriptor->number()));
220 std::unique_ptr<FieldGeneratorBase> generator(
222 generator->GenerateMembers(printer);
223 printer->Print(
"\n");
231 vars[
"original_name"] = oneof->name();
234 "private object $name$_;\n"
235 "/// <summary>Enum of possible cases for the \"$original_name$\" oneof.</summary>\n"
236 "public enum $property_name$OneofCase {\n");
238 printer->Print(
"None = 0,\n");
239 for (
int j = 0;
j < oneof->field_count();
j++) {
241 printer->Print(
"$field_property_name$ = $index$,\n",
246 printer->Print(
"}\n");
251 "private $property_name$OneofCase $name$Case_ = $property_name$OneofCase.None;\n");
255 "public $property_name$OneofCase $property_name$Case {\n"
256 " get { return $name$Case_; }\n"
261 "public void Clear$property_name$() {\n"
262 " $name$Case_ = $property_name$OneofCase.None;\n"
275 "public TValue GetExtension<TValue>(pb::Extension<$class_name$, "
276 "TValue> extension) {\n"
277 " return pb::ExtensionSet.Get(ref _extensions, extension);\n"
279 "public pbc::RepeatedField<TValue> "
280 "GetExtension<TValue>(pb::RepeatedExtension<$class_name$, TValue> "
282 " return pb::ExtensionSet.Get(ref _extensions, extension);\n"
284 "public pbc::RepeatedField<TValue> "
285 "GetOrInitializeExtension<TValue>(pb::RepeatedExtension<$class_name$, "
286 "TValue> extension) {\n"
287 " return pb::ExtensionSet.GetOrInitialize(ref _extensions, "
290 "public void SetExtension<TValue>(pb::Extension<$class_name$, TValue> "
291 "extension, TValue value) {\n"
292 " pb::ExtensionSet.Set(ref _extensions, extension, value);\n"
294 "public bool HasExtension<TValue>(pb::Extension<$class_name$, TValue> "
296 " return pb::ExtensionSet.Has(ref _extensions, extension);\n"
298 "public void ClearExtension<TValue>(pb::Extension<$class_name$, "
299 "TValue> extension) {\n"
300 " pb::ExtensionSet.Clear(ref _extensions, extension);\n"
303 "ClearExtension<TValue>(pb::RepeatedExtension<$class_name$, TValue> "
305 " pb::ExtensionSet.Clear(ref _extensions, extension);\n"
313 "#region Nested types\n"
314 "/// <summary>Container for nested types declared in the $class_name$ message type.</summary>\n");
316 printer->Print(
"public static partial class Types {\n");
320 enumGenerator.Generate(printer);
327 messageGenerator.Generate(printer);
339 "#region Extensions\n"
340 "/// <summary>Container for extensions for other messages declared in the $class_name$ message type.</summary>\n");
342 printer->Print(
"public static partial class Extensions {\n");
345 std::unique_ptr<FieldGeneratorBase> generator(
347 generator->GenerateExtensionCode(printer);
357 printer->Print(
"}\n");
358 printer->Print(
"\n");
377 std::map<std::string, std::string> vars;
382 "public $class_name$($class_name$ other) : this() {\n");
385 printer->Print(
"_hasBits$i$ = other._hasBits$i$;\n",
"i",
StrCat(i));
390 if (
field->real_containing_oneof()) {
394 generator->GenerateCloningCode(printer);
401 printer->Print(vars,
"switch (other.$property_name$Case) {\n");
403 for (
int j = 0;
j < oneof->field_count();
j++) {
409 "case $property_name$OneofCase.$field_property_name$:\n");
411 generator->GenerateCloningCode(printer);
412 printer->Print(
"break;\n");
416 printer->Print(
"}\n\n");
420 "_unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);\n");
423 "_extensions = pb::ExtensionSet.Clone(other._extensions);\n");
427 printer->Print(
"}\n\n");
432 "public $class_name$ Clone() {\n"
433 " return new $class_name$(this);\n"
441 std::map<std::string, std::string> vars;
448 "public override bool Equals(object other) {\n"
449 " return Equals(other as $class_name$);\n"
454 "public bool Equals($class_name$ other) {\n"
455 " if (ReferenceEquals(other, null)) {\n"
458 " if (ReferenceEquals(other, this)) {\n"
463 std::unique_ptr<FieldGeneratorBase> generator(
465 generator->WriteEquals(printer);
468 printer->Print(
"if ($property_name$Case != other.$property_name$Case) return false;\n",
473 "if (!Equals(_extensions, other._extensions)) {\n"
479 " return Equals(_unknownFields, other._unknownFields);\n"
486 "public override int GetHashCode() {\n"
490 std::unique_ptr<FieldGeneratorBase> generator(
492 generator->WriteHash(printer);
495 printer->Print(
"hash ^= (int) $name$Case_;\n",
500 "if (_extensions != null) {\n"
501 " hash ^= _extensions.GetHashCode();\n"
505 "if (_unknownFields != null) {\n"
506 " hash ^= _unknownFields.GetHashCode();\n"
510 printer->Print(
"}\n\n");
514 "public override string ToString() {\n"
515 " return pb::JsonFormatter.ToDiagnosticString(this);\n"
522 "public void WriteTo(pb::CodedOutputStream output) {\n");
523 printer->Print(
"#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE\n");
525 printer->Print(
"output.WriteRawMessage(this);\n");
527 printer->Print(
"#else\n");
531 printer->Print(
"#endif\n");
532 printer->Print(
"}\n\n");
534 printer->Print(
"#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE\n");
536 printer->Print(
"void pb::IBufferMessage.InternalWriteTo(ref pb::WriteContext output) {\n");
540 printer->Print(
"}\n");
541 printer->Print(
"#endif\n\n");
545 "public int CalculateSize() {\n");
547 printer->Print(
"int size = 0;\n");
549 std::unique_ptr<FieldGeneratorBase> generator(
551 generator->GenerateSerializedSizeCode(printer);
556 "if (_extensions != null) {\n"
557 " size += _extensions.CalculateSize();\n"
562 "if (_unknownFields != null) {\n"
563 " size += _unknownFields.CalculateSize();\n"
566 printer->Print(
"return size;\n");
568 printer->Print(
"}\n\n");
574 std::unique_ptr<FieldGeneratorBase> generator(
576 generator->GenerateSerializationCode(printer, use_write_context);
583 ?
"if (_extensions != null) {\n"
584 " _extensions.WriteTo(ref output);\n"
586 :
"if (_extensions != null) {\n"
587 " _extensions.WriteTo(output);\n"
594 ?
"if (_unknownFields != null) {\n"
595 " _unknownFields.WriteTo(ref output);\n"
597 :
"if (_unknownFields != null) {\n"
598 " _unknownFields.WriteTo(output);\n"
608 std::map<std::string, std::string> vars;
614 "public void MergeFrom($class_name$ other) {\n");
617 "if (other == null) {\n"
623 if (
field->real_containing_oneof()) {
627 generator->GenerateMergingCode(printer);
634 printer->
Print(vars,
"switch (other.$property_name$Case) {\n");
641 "case $property_name$OneofCase.$field_property_name$:\n");
644 generator->GenerateMergingCode(printer);
645 printer->
Print(
"break;\n");
649 printer->
Print(
"}\n\n");
653 printer->
Print(
"pb::ExtensionSet.MergeFrom(ref _extensions, other._extensions);\n");
658 "_unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);\n");
661 printer->
Print(
"}\n\n");
664 printer->
Print(
"public void MergeFrom(pb::CodedInputStream input) {\n");
665 printer->
Print(
"#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE\n");
667 printer->
Print(
"input.ReadRawMessage(this);\n");
669 printer->
Print(
"#else\n");
673 printer->
Print(
"#endif\n");
674 printer->
Print(
"}\n\n");
676 printer->
Print(
"#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE\n");
678 printer->
Print(
"void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {\n");
682 printer->
Print(
"}\n");
683 printer->
Print(
"#endif\n\n");
688 std::map<std::string, std::string> vars;
689 vars[
"maybe_ref_input"] = use_parse_context ?
"ref input" :
"input";
693 "while ((tag = input.ReadTag()) != 0) {\n"
706 " if (!pb::ExtensionSet.TryMergeFieldFrom(ref _extensions, $maybe_ref_input$)) {\n"
707 " _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, $maybe_ref_input$);\n"
713 " _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, $maybe_ref_input$);\n"
726 if (
field->is_packable()) {
728 "case $packed_tag$:\n",
738 std::unique_ptr<FieldGeneratorBase> generator(
740 generator->GenerateParsingCode(printer, use_parse_context);
741 printer->
Print(
"break;\n");
743 printer->
Print(
"}\n");
746 printer->
Print(
"}\n");
748 printer->
Print(
"}\n");
bool CompareFieldNumbers(const FieldDescriptor *d1, const FieldDescriptor *d2)
void Print(const std::map< std::string, std::string > &variables, const char *text)
std::string GetFieldConstantName(const FieldDescriptor *field)
std::string GetReflectionClassName(const FileDescriptor *descriptor)
void GenerateCloningCode(io::Printer *printer)
std::string GetClassName(const Descriptor *descriptor)
int real_oneof_decl_count() const
void WriteGeneratedCodeAttributes(io::Printer *printer)
int nested_type_count() const
bool RequiresPresenceBit(const FieldDescriptor *descriptor)
void GenerateMainParseLoop(io::Printer *printer, bool use_parse_context)
const Descriptor * containing_type() const
GRPC_CUSTOM_PRINTER Printer
std::string full_class_name()
void AddSerializableAttribute(io::Printer *printer)
const std::vector< const FieldDescriptor * > & fields_by_number()
void GenerateFreezingCode(io::Printer *printer)
const std::string & name() const
string StrCat(const AlphaNum &a, const AlphaNum &b)
const FieldDescriptor * field(int index) const
void GenerateMessageSerializationMethods(io::Printer *printer)
const MessageOptions & options() const
void GenerateFrameworkMethods(io::Printer *printer)
const FieldDescriptor * field(int index) const
static void * tag(intptr_t t)
const OneofDescriptor * oneof_decl(int index) const
bool IsDescriptorProto(const FileDescriptor *descriptor)
FieldGeneratorBase * CreateFieldGeneratorInternal(const FieldDescriptor *descriptor)
const Descriptor * descriptor_
int extension_count() const
const EnumDescriptor * enum_type(int index) const
void GenerateWriteToBody(io::Printer *printer, bool use_write_context)
std::vector< const FieldDescriptor * > fields_by_number_
const FieldDescriptor * extension(int index) const
void Generate(io::Printer *printer)
std::string GetPropertyName(const FieldDescriptor *descriptor)
void WriteMessageDocComment(io::Printer *printer, const Descriptor *message)
uint GetGroupEndTag(const Descriptor *descriptor)
const Descriptor * nested_type(int index) const
int enum_type_count() const
const FieldDescriptor * field
std::string class_access_level()
bool IsMapEntryMessage(const Descriptor *descriptor)
const EnumOptions & options() const
const Options * options()
bool HasNestedGeneratedTypes()
const std::string & name() const
const FileDescriptor * file() const
void AddDeprecatedFlag(io::Printer *printer)
MessageGenerator(const Descriptor *descriptor, const Options *options)
int GetPresenceIndex(const FieldDescriptor *descriptor)
std::string UnderscoresToCamelCase(const std::string &input, bool cap_next_letter, bool preserve_period)
void GenerateMergingMethods(io::Printer *printer)
FieldGeneratorBase * CreateFieldGenerator(const FieldDescriptor *descriptor, int presenceIndex, const Options *options)
#define GOOGLE_LOG(LEVEL)
static const char descriptor[1336]
bool has_extension_ranges_
grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:07