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/stubs/port.h>
40 #include <google/protobuf/descriptor.pb.h>
41 #include <google/protobuf/descriptor.h>
42 #include <google/protobuf/compiler/code_generator.h>
43 #include <google/protobuf/io/printer.h>
44
45 #include <google/protobuf/port_def.inc>
46
47 namespace google {
48 namespace protobuf {
49 namespace compiler {
50 namespace csharp {
51
52 struct Options;
53 class FieldGeneratorBase;
54
55 // TODO: start using this enum.
56 enum CSharpType {
57 CSHARPTYPE_INT32 = 1,
58 CSHARPTYPE_INT64 = 2,
59 CSHARPTYPE_UINT32 = 3,
60 CSHARPTYPE_UINT64 = 4,
61 CSHARPTYPE_FLOAT = 5,
62 CSHARPTYPE_DOUBLE = 6,
63 CSHARPTYPE_BOOL = 7,
64 CSHARPTYPE_STRING = 8,
65 CSHARPTYPE_BYTESTRING = 9,
66 CSHARPTYPE_MESSAGE = 10,
67 CSHARPTYPE_ENUM = 11,
68 MAX_CSHARPTYPE = 11
69 };
70
71 // Converts field type to corresponding C# type.
72 CSharpType GetCSharpType(FieldDescriptor::Type type);
73
74 std::string StripDotProto(const std::string& proto_file);
75
76 // Gets unqualified name of the reflection class
77 std::string GetReflectionClassUnqualifiedName(const FileDescriptor* descriptor);
78 // Gets unqualified name of the extension class
79 std::string GetExtensionClassUnqualifiedName(const FileDescriptor* descriptor);
80
81 std::string GetClassName(const EnumDescriptor* descriptor);
82
83 std::string GetFieldName(const FieldDescriptor* descriptor);
84
85 std::string GetFieldConstantName(const FieldDescriptor* field);
86
87 std::string GetPropertyName(const FieldDescriptor* descriptor);
88
89 int GetFixedSize(FieldDescriptor::Type type);
90
91 std::string UnderscoresToCamelCase(const std::string& input,
92 bool cap_next_letter,
93 bool preserve_period);
94
UnderscoresToCamelCase(const std::string & input,bool cap_next_letter)95 inline std::string UnderscoresToCamelCase(const std::string& input, bool cap_next_letter) {
96 return UnderscoresToCamelCase(input, cap_next_letter, false);
97 }
98
99 std::string UnderscoresToPascalCase(const std::string& input);
100
101 // Note that we wouldn't normally want to export this (we're not expecting
102 // it to be used outside libprotoc itself) but this exposes it for testing.
103 std::string PROTOC_EXPORT GetEnumValueName(const std::string& enum_name,
104 const std::string& enum_value_name);
105
106 // TODO(jtattermusch): perhaps we could move this to strutil
107 std::string StringToBase64(const std::string& input);
108
109 std::string FileDescriptorToBase64(const FileDescriptor* descriptor);
110
111 FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor,
112 int presenceIndex,
113 const Options* options);
114
115 std::string GetFullExtensionName(const FieldDescriptor* descriptor);
116
117 bool IsNullable(const FieldDescriptor* descriptor);
118
119 // Determines whether the given message is a map entry message,
120 // i.e. one implicitly created by protoc due to a map<key, value> field.
IsMapEntryMessage(const Descriptor * descriptor)121 inline bool IsMapEntryMessage(const Descriptor* descriptor) {
122 return descriptor->options().map_entry();
123 }
124
125 // Checks if this descriptor is for a group and gets its end tag or 0 if it's not a group
126 uint GetGroupEndTag(const Descriptor* descriptor);
127
128 // Determines whether we're generating code for the proto representation of
129 // descriptors etc, for use in the runtime. This is the only type which is
130 // allowed to use proto2 syntax, and it generates internal classes.
IsDescriptorProto(const FileDescriptor * descriptor)131 inline bool IsDescriptorProto(const FileDescriptor* descriptor) {
132 return descriptor->name() == "google/protobuf/descriptor.proto";
133 }
134
135 // Determines whether the given message is an options message within descriptor.proto.
IsDescriptorOptionMessage(const Descriptor * descriptor)136 inline bool IsDescriptorOptionMessage(const Descriptor* descriptor) {
137 if (!IsDescriptorProto(descriptor->file())) {
138 return false;
139 }
140 const string name = descriptor->full_name();
141 return name == "google.protobuf.FileOptions" ||
142 name == "google.protobuf.MessageOptions" ||
143 name == "google.protobuf.FieldOptions" ||
144 name == "google.protobuf.OneofOptions" ||
145 name == "google.protobuf.EnumOptions" ||
146 name == "google.protobuf.EnumValueOptions" ||
147 name == "google.protobuf.ServiceOptions" ||
148 name == "google.protobuf.MethodOptions";
149 }
150
IsWrapperType(const FieldDescriptor * descriptor)151 inline bool IsWrapperType(const FieldDescriptor* descriptor) {
152 return descriptor->type() == FieldDescriptor::TYPE_MESSAGE &&
153 descriptor->message_type()->file()->name() == "google/protobuf/wrappers.proto";
154 }
155
IsProto2(const FileDescriptor * descriptor)156 inline bool IsProto2(const FileDescriptor* descriptor) {
157 return descriptor->syntax() == FileDescriptor::SYNTAX_PROTO2;
158 }
159
160 } // namespace csharp
161 } // namespace compiler
162 } // namespace protobuf
163 } // namespace google
164
165 #include <google/protobuf/port_undef.inc>
166
167 #endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__
168