protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h
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 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #ifndef GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__
36 #define GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__
37 
38 #include <string>
39 #include <google/protobuf/port.h>
40 #include <google/protobuf/stubs/common.h>
41 #include <google/protobuf/descriptor.pb.h>
42 #include <google/protobuf/descriptor.h>
43 #include <google/protobuf/compiler/code_generator.h>
44 #include <google/protobuf/io/printer.h>
45 
46 #include <google/protobuf/port_def.inc>
47 
48 namespace google {
49 namespace protobuf {
50 namespace compiler {
51 namespace csharp {
52 
53 struct Options;
54 class FieldGeneratorBase;
55 
56 // TODO: start using this enum.
57 enum CSharpType {
58  CSHARPTYPE_INT32 = 1,
59  CSHARPTYPE_INT64 = 2,
62  CSHARPTYPE_FLOAT = 5,
64  CSHARPTYPE_BOOL = 7,
67  CSHARPTYPE_MESSAGE = 10,
68  CSHARPTYPE_ENUM = 11,
69  MAX_CSHARPTYPE = 11
70 };
71 
72 // Converts field type to corresponding C# type.
74 
75 std::string StripDotProto(const std::string& proto_file);
76 
77 // Gets unqualified name of the reflection class
79 // Gets unqualified name of the extension class
81 
83 
85 
87 
89 
91 
93  bool cap_next_letter,
94  bool preserve_period);
95 
96 inline std::string UnderscoresToCamelCase(const std::string& input, bool cap_next_letter) {
97  return UnderscoresToCamelCase(input, cap_next_letter, false);
98 }
99 
101 
102 // Note that we wouldn't normally want to export this (we're not expecting
103 // it to be used outside libprotoc itself) but this exposes it for testing.
104 std::string PROTOC_EXPORT GetEnumValueName(const std::string& enum_name,
105  const std::string& enum_value_name);
106 
107 // TODO(jtattermusch): perhaps we could move this to strutil
109 
111 
112 FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor,
113  int presenceIndex,
114  const Options* options);
115 
117 
119 
120 // Determines whether the given message is a map entry message,
121 // i.e. one implicitly created by protoc due to a map<key, value> field.
122 inline bool IsMapEntryMessage(const Descriptor* descriptor) {
123  return descriptor->options().map_entry();
124 }
125 
126 // Checks if this descriptor is for a group and gets its end tag or 0 if it's not a group
128 
129 // Determines whether we're generating code for the proto representation of
130 // descriptors etc, for use in the runtime. This is the only type which is
131 // allowed to use proto2 syntax, and it generates internal classes.
132 inline bool IsDescriptorProto(const FileDescriptor* descriptor) {
133  return descriptor->name() == "google/protobuf/descriptor.proto";
134 }
135 
136 // Determines whether the given message is an options message within descriptor.proto.
137 inline bool IsDescriptorOptionMessage(const Descriptor* descriptor) {
138  if (!IsDescriptorProto(descriptor->file())) {
139  return false;
140  }
141  const std::string name = descriptor->full_name();
142  return name == "google.protobuf.FileOptions" ||
143  name == "google.protobuf.MessageOptions" ||
144  name == "google.protobuf.FieldOptions" ||
145  name == "google.protobuf.OneofOptions" ||
146  name == "google.protobuf.EnumOptions" ||
147  name == "google.protobuf.EnumValueOptions" ||
148  name == "google.protobuf.ServiceOptions" ||
149  name == "google.protobuf.MethodOptions";
150 }
151 
152 inline bool IsWrapperType(const FieldDescriptor* descriptor) {
153  return descriptor->type() == FieldDescriptor::TYPE_MESSAGE &&
154  descriptor->message_type()->file()->name() == "google/protobuf/wrappers.proto";
155 }
156 
157 inline bool IsProto2(const FileDescriptor* descriptor) {
158  return descriptor->syntax() == FileDescriptor::SYNTAX_PROTO2;
159 }
160 
162  // Unlike most languages, we don't generate Has/Clear members for message
163  // types, because they can always be set to null in C#. They're not really
164  // needed for oneof fields in proto2 either, as everything can be done via
165  // oneof case, but we follow the convention from other languages. Proto3
166  // oneof fields never have Has/Clear members - but will also never have
167  // the explicit optional keyword either.
168  //
169  // None of the built-in helpers (descriptor->has_presence() etc) describe
170  // quite the behavior we want, so the rules are explicit below.
171 
172  if (descriptor->is_repeated() ||
174  return false;
175  }
176  // has_optional_keyword() has more complex rules for proto2, but that
177  // doesn't matter given the first part of this condition.
178  return IsProto2(descriptor->file()) || descriptor->has_optional_keyword();
179 }
180 
184  !descriptor->is_extension() &&
185  !descriptor->real_containing_oneof();
186 }
187 
188 } // namespace csharp
189 } // namespace compiler
190 } // namespace protobuf
191 } // namespace google
192 
193 #include <google/protobuf/port_undef.inc>
194 
195 #endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__
google::protobuf::FieldDescriptor::Type
Type
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:521
google::protobuf::compiler::csharp::CSHARPTYPE_FLOAT
@ CSHARPTYPE_FLOAT
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:61
google::protobuf::compiler::csharp::GetFieldConstantName
std::string GetFieldConstantName(const FieldDescriptor *field)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:352
google::protobuf::compiler::csharp::CSHARPTYPE_BOOL
@ CSHARPTYPE_BOOL
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:63
google::protobuf::FieldDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:515
google::protobuf::compiler::csharp::GetClassName
std::string GetClassName(const Descriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:333
google::protobuf::FileDescriptor::SYNTAX_PROTO2
@ SYNTAX_PROTO2
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1393
options
double_dict options[]
Definition: capstone_test.c:55
FileDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:128
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf::compiler::csharp::RequiresPresenceBit
bool RequiresPresenceBit(const FieldDescriptor *descriptor)
Definition: protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:181
google::protobuf::compiler::csharp::StringToBase64
std::string StringToBase64(const std::string &input)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:440
google::protobuf::compiler::csharp::CSHARPTYPE_DOUBLE
@ CSHARPTYPE_DOUBLE
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:62
google::protobuf::compiler::csharp::GetFullExtensionName
std::string GetFullExtensionName(const FieldDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:324
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::compiler::csharp::StripDotProto
std::string StripDotProto(const std::string &proto_file)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:109
google::protobuf::compiler::csharp::GetFixedSize
int GetFixedSize(FieldDescriptor::Type type)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:407
setup.name
name
Definition: setup.py:542
google::protobuf::compiler::csharp::GetCSharpType
CSharpType GetCSharpType(FieldDescriptor::Type type)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:63
google::protobuf::compiler::csharp::IsDescriptorOptionMessage
bool IsDescriptorOptionMessage(const Descriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:136
google::protobuf::compiler::csharp::UnderscoresToPascalCase
std::string UnderscoresToPascalCase(const std::string &input)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:183
Descriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:121
google::protobuf::compiler::csharp::IsProto2
bool IsProto2(const FileDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:156
google::protobuf::compiler::csharp::CSHARPTYPE_STRING
@ CSHARPTYPE_STRING
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:64
google::protobuf::compiler::csharp::CSHARPTYPE_BYTESTRING
@ CSHARPTYPE_BYTESTRING
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:65
google::protobuf::compiler::csharp::MAX_CSHARPTYPE
@ MAX_CSHARPTYPE
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:68
FieldDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:133
google::protobuf::compiler::csharp::GetFieldName
std::string GetFieldName(const FieldDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:344
google::protobuf::compiler::csharp::CSHARPTYPE_ENUM
@ CSHARPTYPE_ENUM
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:67
google::protobuf::compiler::csharp::CSHARPTYPE_MESSAGE
@ CSHARPTYPE_MESSAGE
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:66
google::protobuf::compiler::csharp::CSHARPTYPE_INT64
@ CSHARPTYPE_INT64
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:58
google::protobuf::compiler::csharp::IsDescriptorProto
bool IsDescriptorProto(const FileDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:131
google::protobuf::compiler::csharp::GetReflectionClassUnqualifiedName
std::string GetReflectionClassUnqualifiedName(const FileDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:130
uint
unsigned int uint
Definition: bloaty/third_party/zlib/examples/gzlog.c:242
google::protobuf::compiler::csharp::GetPropertyName
std::string GetPropertyName(const FieldDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:356
google::protobuf::compiler::csharp::GetGroupEndTag
uint GetGroupEndTag(const Descriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:285
google::protobuf::compiler::csharp::CSHARPTYPE_UINT64
@ CSHARPTYPE_UINT64
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:60
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
google::protobuf::compiler::csharp::FileDescriptorToBase64
std::string FileDescriptorToBase64(const FileDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:471
google::protobuf::compiler::csharp::CSHARPTYPE_UINT32
@ CSHARPTYPE_UINT32
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:59
google::protobuf::compiler::csharp::IsNullable
bool IsNullable(const FieldDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:529
google::protobuf::compiler::csharp::IsMapEntryMessage
bool IsMapEntryMessage(const Descriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:121
google::protobuf::Descriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:231
google::protobuf::FieldDescriptor::TYPE_MESSAGE
@ TYPE_MESSAGE
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:536
google::protobuf::compiler::csharp::IsWrapperType
bool IsWrapperType(const FieldDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:151
google::protobuf::compiler::csharp::CSHARPTYPE_INT32
@ CSHARPTYPE_INT32
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:57
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
google::protobuf::FileDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1320
google::protobuf::compiler::csharp::GetExtensionClassUnqualifiedName
std::string GetExtensionClassUnqualifiedName(const FileDescriptor *descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:136
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
google::protobuf::compiler::csharp::UnderscoresToCamelCase
std::string UnderscoresToCamelCase(const std::string &input, bool cap_next_letter, bool preserve_period)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:143
google::protobuf::compiler::csharp::SupportsPresenceApi
bool SupportsPresenceApi(const FieldDescriptor *descriptor)
Definition: protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:161
google::protobuf::compiler::csharp::GetEnumValueName
std::string GetEnumValueName(const std::string &enum_name, const std::string &enum_value_name)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:274
google::protobuf::compiler::csharp::CreateFieldGenerator
FieldGeneratorBase * CreateFieldGenerator(const FieldDescriptor *descriptor, int presenceIndex, const Options *options)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.cc:479
google::protobuf::EnumDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:918
google::protobuf::compiler::csharp::CSharpType
CSharpType
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/csharp/csharp_helpers.h:56
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
compiler
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.pb.cc:21
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:07