// Protocol Buffers - Google's data interchange format // Copyright 2008 Google Inc. All rights reserved. // https://developers.google.com/protocol-buffers/ // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Author: [email protected] (Kenton Varda) // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. #include <algorithm> #include <limits> #include <vector> #include <sstream> #include <google/protobuf/compiler/csharp/csharp_helpers.h> #include <google/protobuf/compiler/csharp/csharp_names.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/io/printer.h> #include <google/protobuf/wire_format.h> #include <google/protobuf/stubs/strutil.h> #include <google/protobuf/compiler/csharp/csharp_field_base.h> #include <google/protobuf/compiler/csharp/csharp_enum_field.h> #include <google/protobuf/compiler/csharp/csharp_map_field.h> #include <google/protobuf/compiler/csharp/csharp_message_field.h> #include <google/protobuf/compiler/csharp/csharp_options.h> #include <google/protobuf/compiler/csharp/csharp_primitive_field.h> #include <google/protobuf/compiler/csharp/csharp_repeated_enum_field.h> #include <google/protobuf/compiler/csharp/csharp_repeated_message_field.h> #include <google/protobuf/compiler/csharp/csharp_repeated_primitive_field.h> #include <google/protobuf/compiler/csharp/csharp_wrapper_field.h> namespace google { namespace protobuf { namespace compiler { namespace csharp { CSharpType GetCSharpType(FieldDescriptor::Type type) { … } std::string StripDotProto(const std::string& proto_file) { … } std::string GetFileNamespace(const FileDescriptor* descriptor) { … } // Returns the Pascal-cased last part of the proto file. For example, // input of "google/protobuf/foo_bar.proto" would result in "FooBar". std::string GetFileNameBase(const FileDescriptor* descriptor) { … } std::string GetReflectionClassUnqualifiedName(const FileDescriptor* descriptor) { … } std::string GetExtensionClassUnqualifiedName(const FileDescriptor* descriptor) { … } // TODO(jtattermusch): can we reuse a utility function? std::string UnderscoresToCamelCase(const std::string& input, bool cap_next_letter, bool preserve_period) { … } std::string UnderscoresToPascalCase(const std::string& input) { … } // Convert a string which is expected to be SHOUTY_CASE (but may not be *precisely* shouty) // into a PascalCase string. Precise rules implemented: // Previous input character Current character Case // Any Non-alphanumeric Skipped // None - first char of input Alphanumeric Upper // Non-letter (e.g. _ or 1) Alphanumeric Upper // Numeric Alphanumeric Upper // Lower letter Alphanumeric Same as current // Upper letter Alphanumeric Lower std::string ShoutyToPascalCase(const std::string& input) { … } // Attempt to remove a prefix from a value, ignoring casing and skipping underscores. // (foo, foo_bar) => bar - underscore after prefix is skipped // (FOO, foo_bar) => bar - casing is ignored // (foo_bar, foobarbaz) => baz - underscore in prefix is ignored // (foobar, foo_barbaz) => baz - underscore in value is ignored // (foo, bar) => bar - prefix isn't matched; return original value std::string TryRemovePrefix(const std::string& prefix, const std::string& value) { … } // Format the enum value name in a pleasant way for C#: // - Strip the enum name as a prefix if possible // - Convert to PascalCase. // For example, an enum called Color with a value of COLOR_BLUE should // result in an enum value in C# called just Blue std::string GetEnumValueName(const std::string& enum_name, const std::string& enum_value_name) { … } uint GetGroupEndTag(const Descriptor* descriptor) { … } std::string ToCSharpName(const std::string& name, const FileDescriptor* file) { … } std::string GetReflectionClassName(const FileDescriptor* descriptor) { … } std::string GetFullExtensionName(const FieldDescriptor* descriptor) { … } std::string GetClassName(const Descriptor* descriptor) { … } std::string GetClassName(const EnumDescriptor* descriptor) { … } // Groups are hacky: The name of the field is just the lower-cased name // of the group type. In C#, though, we would like to retain the original // capitalization of the type name. std::string GetFieldName(const FieldDescriptor* descriptor) { … } std::string GetFieldConstantName(const FieldDescriptor* field) { … } std::string GetPropertyName(const FieldDescriptor* descriptor) { … } std::string GetOneofCaseName(const FieldDescriptor* descriptor) { … } std::string GetOutputFile(const FileDescriptor* descriptor, const std::string file_extension, const bool generate_directories, const std::string base_namespace, std::string* error) { … } // TODO: c&p from Java protoc plugin // For encodings with fixed sizes, returns that size in bytes. Otherwise // returns -1. int GetFixedSize(FieldDescriptor::Type type) { … } static const char base64_chars[] = …; std::string StringToBase64(const std::string& input) { … } std::string FileDescriptorToBase64(const FileDescriptor* descriptor) { … } FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor, int presenceIndex, const Options* options) { … } bool IsNullable(const FieldDescriptor* descriptor) { … } } // namespace csharp } // namespace compiler } // namespace protobuf } // namespace google