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