blob: 4ed17a846c6ae2eb7ee1b8ca47afc6d5161e4dad [file] [log] [blame]
Jan Tattermusch685ae362015-03-16 19:07:16 -07001// 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/descriptor.pb.h>
40#include <google/protobuf/descriptor.h>
41#include <google/protobuf/compiler/code_generator.h>
42#include <google/protobuf/io/printer.h>
43
44namespace google {
45namespace protobuf {
46namespace compiler {
47namespace csharp {
48
49class FieldGeneratorBase;
50
51// TODO: start using this enum.
52enum CSharpType {
53 CSHARPTYPE_INT32 = 1,
54 CSHARPTYPE_INT64 = 2,
55 CSHARPTYPE_UINT32 = 3,
56 CSHARPTYPE_UINT64 = 4,
57 CSHARPTYPE_FLOAT = 5,
58 CSHARPTYPE_DOUBLE = 6,
59 CSHARPTYPE_BOOL = 7,
60 CSHARPTYPE_STRING = 8,
61 CSHARPTYPE_BYTESTRING = 9,
62 CSHARPTYPE_MESSAGE = 10,
63 CSHARPTYPE_ENUM = 11,
64 MAX_CSHARPTYPE = 11
65};
66
67// Converts field type to corresponding C# type.
68CSharpType GetCSharpType(FieldDescriptor::Type type);
69
70std::string StripDotProto(const std::string& proto_file);
71
Jan Tattermusch43a2dee2015-07-29 20:15:03 -070072// Gets unqualified name of the umbrella class
73std::string GetUmbrellaClassUnqualifiedName(const FileDescriptor* descriptor);
Jan Tattermuschcacbedf2015-07-10 13:40:34 -070074
Jan Tattermusch43a2dee2015-07-29 20:15:03 -070075// Gets name of the nested for umbrella class (just the nested part,
76// not including the GetFileNamespace part).
77std::string GetUmbrellaClassNestedNamespace(const FileDescriptor* descriptor);
Jan Tattermusch685ae362015-03-16 19:07:16 -070078
Jon Skeet53c399a2015-07-20 19:24:31 +010079std::string GetClassName(const Descriptor* descriptor);
80
Jan Tattermusch685ae362015-03-16 19:07:16 -070081std::string GetClassName(const EnumDescriptor* descriptor);
82
83std::string GetFieldName(const FieldDescriptor* descriptor);
84
85std::string GetFieldConstantName(const FieldDescriptor* field);
86
87std::string GetPropertyName(const FieldDescriptor* descriptor);
88
89int GetFixedSize(FieldDescriptor::Type type);
90
Jon Skeet8482b6c2015-07-03 13:51:40 +010091std::string UnderscoresToCamelCase(const std::string& input, bool cap_next_letter, bool preserve_period);
92
93inline std::string UnderscoresToCamelCase(const std::string& input, bool cap_next_letter) {
94 return UnderscoresToCamelCase(input, cap_next_letter, false);
95}
Jan Tattermusch685ae362015-03-16 19:07:16 -070096
97std::string UnderscoresToPascalCase(const std::string& input);
98
99// TODO(jtattermusch): perhaps we could move this to strutil
100std::string StringToBase64(const std::string& input);
101
102std::string FileDescriptorToBase64(const FileDescriptor* descriptor);
103
Jon Skeet322ec532015-06-24 17:55:02 +0100104uint FixedMakeTag(const FieldDescriptor* descriptor);
105
Jan Tattermusch685ae362015-03-16 19:07:16 -0700106FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
107
Jon Skeet541b4422015-07-15 13:36:56 +0100108// Determines whether the given message is a map entry message, i.e. one implicitly created
109// by protoc due to a map<key, value> field.
110inline bool IsMapEntryMessage(const Descriptor* descriptor) {
111 return descriptor->options().map_entry();
112}
113
Jon Skeet493e3db2015-07-01 17:11:17 +0100114// Determines whether we're generating code for the proto representation of descriptors etc,
115// for use in the runtime. This is the only type which is allowed to use proto2 syntax,
116// and it generates internal classes.
117inline bool IsDescriptorProto(const FileDescriptor* descriptor) {
Jon Skeetca89a1a2015-08-25 14:32:28 +0100118 return descriptor->name() == "google/protobuf/descriptor.proto";
Jon Skeet493e3db2015-07-01 17:11:17 +0100119}
120
Jon Skeetb2ac8682015-07-15 13:17:42 +0100121inline bool IsWrapperType(const FieldDescriptor* descriptor) {
122 return descriptor->type() == FieldDescriptor::TYPE_MESSAGE &&
123 descriptor->message_type()->file()->name() == "google/protobuf/wrappers.proto";
124}
125
Jan Tattermusch685ae362015-03-16 19:07:16 -0700126} // namespace csharp
127} // namespace compiler
128} // namespace protobuf
129} // namespace google
130#endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_HELPERS_H__