Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 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 | #include <sstream> |
| 32 | |
| 33 | #include <google/protobuf/compiler/code_generator.h> |
| 34 | #include <google/protobuf/compiler/plugin.h> |
| 35 | #include <google/protobuf/descriptor.h> |
| 36 | #include <google/protobuf/descriptor.pb.h> |
| 37 | #include <google/protobuf/io/printer.h> |
| 38 | #include <google/protobuf/io/zero_copy_stream.h> |
Jon Skeet | 5eb1fac | 2015-09-01 15:05:03 +0100 | [diff] [blame] | 39 | #include <google/protobuf/stubs/strutil.h> |
Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 40 | |
| 41 | #include <google/protobuf/compiler/csharp/csharp_generator.h> |
Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 42 | #include <google/protobuf/compiler/csharp/csharp_helpers.h> |
Jon Skeet | 5eb1fac | 2015-09-01 15:05:03 +0100 | [diff] [blame] | 43 | #include <google/protobuf/compiler/csharp/csharp_names.h> |
Gaurav Vaish | 74d8b0b | 2016-03-24 11:44:56 -0700 | [diff] [blame] | 44 | #include <google/protobuf/compiler/csharp/csharp_options.h> |
Jon Skeet | a6361a1 | 2015-11-19 13:05:17 +0000 | [diff] [blame] | 45 | #include <google/protobuf/compiler/csharp/csharp_reflection_class.h> |
Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 46 | |
| 47 | using google::protobuf::internal::scoped_ptr; |
| 48 | |
| 49 | namespace google { |
| 50 | namespace protobuf { |
| 51 | namespace compiler { |
| 52 | namespace csharp { |
| 53 | |
| 54 | void GenerateFile(const google::protobuf::FileDescriptor* file, |
Gaurav Vaish | 74d8b0b | 2016-03-24 11:44:56 -0700 | [diff] [blame] | 55 | io::Printer* printer, |
| 56 | const Options* options) { |
| 57 | ReflectionClassGenerator reflectionClassGenerator(file, options); |
Jon Skeet | a6361a1 | 2015-11-19 13:05:17 +0000 | [diff] [blame] | 58 | reflectionClassGenerator.Generate(printer); |
Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | bool Generator::Generate( |
| 62 | const FileDescriptor* file, |
| 63 | const string& parameter, |
| 64 | GeneratorContext* generator_context, |
| 65 | string* error) const { |
| 66 | |
Jan Tattermusch | f61e179 | 2015-04-13 15:10:07 -0700 | [diff] [blame] | 67 | vector<pair<string, string> > options; |
| 68 | ParseGeneratorParameter(parameter, &options); |
Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 69 | |
Jon Skeet | 493e3db | 2015-07-01 17:11:17 +0100 | [diff] [blame] | 70 | // We only support proto3 - but we make an exception for descriptor.proto. |
| 71 | if (file->syntax() != FileDescriptor::SYNTAX_PROTO3 && !IsDescriptorProto(file)) { |
| 72 | *error = "C# code generation only supports proto3 syntax"; |
| 73 | return false; |
| 74 | } |
| 75 | |
Gaurav Vaish | 74d8b0b | 2016-03-24 11:44:56 -0700 | [diff] [blame] | 76 | struct Options cli_options; |
| 77 | |
Jan Tattermusch | f61e179 | 2015-04-13 15:10:07 -0700 | [diff] [blame] | 78 | for (int i = 0; i < options.size(); i++) { |
Jie Luo | d9e3063 | 2015-05-01 11:20:50 -0700 | [diff] [blame] | 79 | if (options[i].first == "file_extension") { |
Gaurav Vaish | 74d8b0b | 2016-03-24 11:44:56 -0700 | [diff] [blame] | 80 | cli_options.file_extension = options[i].second; |
Jon Skeet | 5eb1fac | 2015-09-01 15:05:03 +0100 | [diff] [blame] | 81 | } else if (options[i].first == "base_namespace") { |
Gaurav Vaish | 74d8b0b | 2016-03-24 11:44:56 -0700 | [diff] [blame] | 82 | cli_options.base_namespace = options[i].second; |
Jon Skeet | 2a18bb5 | 2016-04-06 10:30:19 +0100 | [diff] [blame] | 83 | cli_options.base_namespace_specified = true; |
Gaurav Vaish | a6e3931 | 2016-04-07 10:10:59 -0700 | [diff] [blame] | 84 | } else if (options[i].first == "internal_access") { |
| 85 | cli_options.internal_access = true; |
Jon Skeet | 75626ed | 2016-04-08 12:19:10 +0100 | [diff] [blame] | 86 | } else if (options[i].first == "legacy_enum_values") { |
| 87 | // TODO: Remove this before final release |
| 88 | cli_options.legacy_enum_values = true; |
Jan Tattermusch | f61e179 | 2015-04-13 15:10:07 -0700 | [diff] [blame] | 89 | } else { |
| 90 | *error = "Unknown generator option: " + options[i].first; |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
Jon Skeet | 5eb1fac | 2015-09-01 15:05:03 +0100 | [diff] [blame] | 95 | string filename_error = ""; |
Gaurav Vaish | 74d8b0b | 2016-03-24 11:44:56 -0700 | [diff] [blame] | 96 | std::string filename = GetOutputFile(file, |
| 97 | cli_options.file_extension, |
Jon Skeet | 2a18bb5 | 2016-04-06 10:30:19 +0100 | [diff] [blame] | 98 | cli_options.base_namespace_specified, |
Gaurav Vaish | 74d8b0b | 2016-03-24 11:44:56 -0700 | [diff] [blame] | 99 | cli_options.base_namespace, |
| 100 | &filename_error); |
| 101 | |
Jan Tattermusch | c31f11d | 2015-09-23 15:12:17 -0700 | [diff] [blame] | 102 | if (filename.empty()) { |
Jon Skeet | 5eb1fac | 2015-09-01 15:05:03 +0100 | [diff] [blame] | 103 | *error = filename_error; |
| 104 | return false; |
| 105 | } |
Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 106 | scoped_ptr<io::ZeroCopyOutputStream> output( |
| 107 | generator_context->Open(filename)); |
| 108 | io::Printer printer(output.get(), '$'); |
Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 109 | |
Gaurav Vaish | 74d8b0b | 2016-03-24 11:44:56 -0700 | [diff] [blame] | 110 | GenerateFile(file, &printer, &cli_options); |
Jan Tattermusch | 685ae36 | 2015-03-16 19:07:16 -0700 | [diff] [blame] | 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | } // namespace csharp |
| 116 | } // namespace compiler |
| 117 | } // namespace protobuf |
| 118 | } // namespace google |