Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:11:57

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 // Author: kenton@google.com (Kenton Varda)
0009 //  Based on original Protocol Buffers design by
0010 //  Sanjay Ghemawat, Jeff Dean, and others.
0011 
0012 #ifndef GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__
0013 #define GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__
0014 
0015 #include <string>
0016 
0017 #include "google/protobuf/compiler/code_generator.h"
0018 #include "absl/strings/string_view.h"
0019 #include "google/protobuf/compiler/csharp/names.h"
0020 #include "google/protobuf/descriptor.h"
0021 #include "google/protobuf/descriptor.pb.h"
0022 #include "google/protobuf/io/printer.h"
0023 #include "google/protobuf/port.h"
0024 #include "google/protobuf/port_def.inc"
0025 #include "google/protobuf/stubs/common.h"
0026 
0027 namespace google {
0028 namespace protobuf {
0029 namespace compiler {
0030 namespace csharp {
0031 
0032 struct Options;
0033 class FieldGeneratorBase;
0034 
0035 // TODO: start using this enum.
0036 enum CSharpType {
0037   CSHARPTYPE_INT32 = 1,
0038   CSHARPTYPE_INT64 = 2,
0039   CSHARPTYPE_UINT32 = 3,
0040   CSHARPTYPE_UINT64 = 4,
0041   CSHARPTYPE_FLOAT = 5,
0042   CSHARPTYPE_DOUBLE = 6,
0043   CSHARPTYPE_BOOL = 7,
0044   CSHARPTYPE_STRING = 8,
0045   CSHARPTYPE_BYTESTRING = 9,
0046   CSHARPTYPE_MESSAGE = 10,
0047   CSHARPTYPE_ENUM = 11,
0048   MAX_CSHARPTYPE = 11
0049 };
0050 
0051 // Converts field type to corresponding C# type.
0052 CSharpType GetCSharpType(FieldDescriptor::Type type);
0053 
0054 std::string GetFieldName(const FieldDescriptor* descriptor);
0055 
0056 std::string GetFieldConstantName(const FieldDescriptor* field);
0057 
0058 std::string GetPropertyName(const FieldDescriptor* descriptor);
0059 
0060 std::string GetOneofCaseName(const FieldDescriptor* descriptor);
0061 
0062 int GetFixedSize(FieldDescriptor::Type type);
0063 
0064 // Note that we wouldn't normally want to export this (we're not expecting
0065 // it to be used outside libprotoc itself) but this exposes it for testing.
0066 std::string PROTOC_EXPORT GetEnumValueName(absl::string_view enum_name,
0067                                            absl::string_view enum_value_name);
0068 
0069 // TODO: perhaps we could move this to strutil
0070 std::string StringToBase64(absl::string_view input);
0071 
0072 std::string FileDescriptorToBase64(const FileDescriptor* descriptor);
0073 
0074 FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor,
0075                                          int presenceIndex,
0076                                          const Options* options);
0077 
0078 std::string GetFullExtensionName(const FieldDescriptor* descriptor);
0079 
0080 bool IsNullable(const FieldDescriptor* descriptor);
0081 
0082 // Determines whether the given message is a map entry message,
0083 // i.e. one implicitly created by protoc due to a map<key, value> field.
0084 inline bool IsMapEntryMessage(const Descriptor* descriptor) {
0085   return descriptor->options().map_entry();
0086 }
0087 
0088 // Determines whether we're generating code for the proto representation of
0089 // descriptors etc, for use in the runtime. This is the only type which is
0090 // allowed to use proto2 syntax, and it generates internal classes.
0091 inline bool IsDescriptorProto(const FileDescriptor* descriptor) {
0092   return descriptor->name() == "google/protobuf/descriptor.proto" ||
0093          descriptor->name() == "net/proto2/proto/descriptor.proto";
0094 }
0095 
0096 // Determines whether the given message is an options message within descriptor.proto.
0097 inline bool IsDescriptorOptionMessage(const Descriptor* descriptor) {
0098   if (!IsDescriptorProto(descriptor->file())) {
0099     return false;
0100   }
0101   const absl::string_view name = descriptor->name();
0102   return name == "FileOptions" ||
0103       name == "MessageOptions" ||
0104       name == "FieldOptions" ||
0105       name == "OneofOptions" ||
0106       name == "EnumOptions" ||
0107       name == "EnumValueOptions" ||
0108       name == "ServiceOptions" ||
0109       name == "MethodOptions";
0110 }
0111 
0112 inline bool IsWrapperType(const FieldDescriptor* descriptor) {
0113   return descriptor->type() == FieldDescriptor::TYPE_MESSAGE &&
0114       descriptor->message_type()->file()->name() == "google/protobuf/wrappers.proto";
0115 }
0116 
0117 inline bool SupportsPresenceApi(const FieldDescriptor* descriptor) {
0118   // Unlike most languages, we don't generate Has/Clear members for message
0119   // types, because they can always be set to null in C#. They're not really
0120   // needed for oneof fields in proto2 either, as everything can be done via
0121   // oneof case, but we follow the convention from other languages.
0122   if (descriptor->type() == FieldDescriptor::TYPE_MESSAGE) {
0123     return false;
0124   }
0125 
0126   return descriptor->has_presence();
0127 }
0128 
0129 inline bool RequiresPresenceBit(const FieldDescriptor* descriptor) {
0130   return SupportsPresenceApi(descriptor) &&
0131     !IsNullable(descriptor) &&
0132     !descriptor->is_extension() &&
0133     !descriptor->real_containing_oneof();
0134 }
0135 
0136 }  // namespace csharp
0137 }  // namespace compiler
0138 }  // namespace protobuf
0139 }  // namespace google
0140 
0141 #include "google/protobuf/port_undef.inc"
0142 
0143 #endif  // GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__