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/descriptor.h>
35 #include <google/protobuf/descriptor.pb.h>
36 #include <google/protobuf/io/printer.h>
37 #include <google/protobuf/io/zero_copy_stream.h>
38 #include <google/protobuf/stubs/strutil.h>
39
40 #include <google/protobuf/compiler/csharp/csharp_generator.h>
41 #include <google/protobuf/compiler/csharp/csharp_helpers.h>
42 #include <google/protobuf/compiler/csharp/csharp_names.h>
43 #include <google/protobuf/compiler/csharp/csharp_options.h>
44 #include <google/protobuf/compiler/csharp/csharp_reflection_class.h>
45
46 namespace google {
47 namespace protobuf {
48 namespace compiler {
49 namespace csharp {
50
Generator()51 Generator::Generator() {}
~Generator()52 Generator::~Generator() {}
53
GetSupportedFeatures() const54 uint64_t Generator::GetSupportedFeatures() const {
55 return CodeGenerator::Feature::FEATURE_PROTO3_OPTIONAL;
56 }
57
GenerateFile(const FileDescriptor * file,io::Printer * printer,const Options * options)58 void GenerateFile(const FileDescriptor* file, io::Printer* printer,
59 const Options* options) {
60 ReflectionClassGenerator reflectionClassGenerator(file, options);
61 reflectionClassGenerator.Generate(printer);
62 }
63
Generate(const FileDescriptor * file,const string & parameter,GeneratorContext * generator_context,string * error) const64 bool Generator::Generate(
65 const FileDescriptor* file,
66 const string& parameter,
67 GeneratorContext* generator_context,
68 string* error) const {
69
70 std::vector<std::pair<string, string> > options;
71 ParseGeneratorParameter(parameter, &options);
72
73 struct Options cli_options;
74
75 for (int i = 0; i < options.size(); i++) {
76 if (options[i].first == "file_extension") {
77 cli_options.file_extension = options[i].second;
78 } else if (options[i].first == "base_namespace") {
79 cli_options.base_namespace = options[i].second;
80 cli_options.base_namespace_specified = true;
81 } else if (options[i].first == "internal_access") {
82 cli_options.internal_access = true;
83 } else if (options[i].first == "serializable") {
84 cli_options.serializable = true;
85 } else {
86 *error = "Unknown generator option: " + options[i].first;
87 return false;
88 }
89 }
90
91 string filename_error = "";
92 std::string filename = GetOutputFile(file,
93 cli_options.file_extension,
94 cli_options.base_namespace_specified,
95 cli_options.base_namespace,
96 &filename_error);
97
98 if (filename.empty()) {
99 *error = filename_error;
100 return false;
101 }
102 std::unique_ptr<io::ZeroCopyOutputStream> output(
103 generator_context->Open(filename));
104 io::Printer printer(output.get(), '$');
105
106 GenerateFile(file, &printer, &cli_options);
107
108 return true;
109 }
110
111 } // namespace csharp
112 } // namespace compiler
113 } // namespace protobuf
114 } // namespace google
115