blob: d74e8c8847208fa7fa35669f6cae9509c00cf3d3 [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#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 Skeet5eb1fac2015-09-01 15:05:03 +010039#include <google/protobuf/stubs/strutil.h>
Jan Tattermusch685ae362015-03-16 19:07:16 -070040
41#include <google/protobuf/compiler/csharp/csharp_generator.h>
Jan Tattermusch685ae362015-03-16 19:07:16 -070042#include <google/protobuf/compiler/csharp/csharp_helpers.h>
Jon Skeet5eb1fac2015-09-01 15:05:03 +010043#include <google/protobuf/compiler/csharp/csharp_names.h>
Gaurav Vaish74d8b0b2016-03-24 11:44:56 -070044#include <google/protobuf/compiler/csharp/csharp_options.h>
Jon Skeeta6361a12015-11-19 13:05:17 +000045#include <google/protobuf/compiler/csharp/csharp_reflection_class.h>
Jan Tattermusch685ae362015-03-16 19:07:16 -070046
47using google::protobuf::internal::scoped_ptr;
48
49namespace google {
50namespace protobuf {
51namespace compiler {
52namespace csharp {
53
54void GenerateFile(const google::protobuf::FileDescriptor* file,
Gaurav Vaish74d8b0b2016-03-24 11:44:56 -070055 io::Printer* printer,
56 const Options* options) {
57 ReflectionClassGenerator reflectionClassGenerator(file, options);
Jon Skeeta6361a12015-11-19 13:05:17 +000058 reflectionClassGenerator.Generate(printer);
Jan Tattermusch685ae362015-03-16 19:07:16 -070059}
60
61bool Generator::Generate(
62 const FileDescriptor* file,
63 const string& parameter,
64 GeneratorContext* generator_context,
65 string* error) const {
66
Jan Tattermuschf61e1792015-04-13 15:10:07 -070067 vector<pair<string, string> > options;
68 ParseGeneratorParameter(parameter, &options);
Jan Tattermusch685ae362015-03-16 19:07:16 -070069
Jon Skeet493e3db2015-07-01 17:11:17 +010070 // 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 Vaish74d8b0b2016-03-24 11:44:56 -070076 struct Options cli_options;
77
Jan Tattermuschf61e1792015-04-13 15:10:07 -070078 for (int i = 0; i < options.size(); i++) {
Jie Luod9e30632015-05-01 11:20:50 -070079 if (options[i].first == "file_extension") {
Gaurav Vaish74d8b0b2016-03-24 11:44:56 -070080 cli_options.file_extension = options[i].second;
Jon Skeet5eb1fac2015-09-01 15:05:03 +010081 } else if (options[i].first == "base_namespace") {
Gaurav Vaish74d8b0b2016-03-24 11:44:56 -070082 cli_options.base_namespace = options[i].second;
Jon Skeet2a18bb52016-04-06 10:30:19 +010083 cli_options.base_namespace_specified = true;
Gaurav Vaisha6e39312016-04-07 10:10:59 -070084 } else if (options[i].first == "internal_access") {
85 cli_options.internal_access = true;
Jon Skeet75626ed2016-04-08 12:19:10 +010086 } else if (options[i].first == "legacy_enum_values") {
87 // TODO: Remove this before final release
88 cli_options.legacy_enum_values = true;
Jan Tattermuschf61e1792015-04-13 15:10:07 -070089 } else {
90 *error = "Unknown generator option: " + options[i].first;
91 return false;
92 }
93 }
94
Jon Skeet5eb1fac2015-09-01 15:05:03 +010095 string filename_error = "";
Gaurav Vaish74d8b0b2016-03-24 11:44:56 -070096 std::string filename = GetOutputFile(file,
97 cli_options.file_extension,
Jon Skeet2a18bb52016-04-06 10:30:19 +010098 cli_options.base_namespace_specified,
Gaurav Vaish74d8b0b2016-03-24 11:44:56 -070099 cli_options.base_namespace,
100 &filename_error);
101
Jan Tattermuschc31f11d2015-09-23 15:12:17 -0700102 if (filename.empty()) {
Jon Skeet5eb1fac2015-09-01 15:05:03 +0100103 *error = filename_error;
104 return false;
105 }
Jan Tattermusch685ae362015-03-16 19:07:16 -0700106 scoped_ptr<io::ZeroCopyOutputStream> output(
107 generator_context->Open(filename));
108 io::Printer printer(output.get(), '$');
Jan Tattermusch685ae362015-03-16 19:07:16 -0700109
Gaurav Vaish74d8b0b2016-03-24 11:44:56 -0700110 GenerateFile(file, &printer, &cli_options);
Jan Tattermusch685ae362015-03-16 19:07:16 -0700111
112 return true;
113}
114
115} // namespace csharp
116} // namespace compiler
117} // namespace protobuf
118} // namespace google