1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "google/protobuf/compiler/csharp/csharp_generator.h"
9
10 #include <sstream>
11
12 #include "google/protobuf/compiler/code_generator.h"
13 #include "google/protobuf/descriptor.h"
14 #include "google/protobuf/compiler/csharp/csharp_helpers.h"
15 #include "google/protobuf/compiler/csharp/csharp_options.h"
16 #include "google/protobuf/compiler/csharp/csharp_reflection_class.h"
17 #include "google/protobuf/compiler/csharp/names.h"
18 #include "google/protobuf/descriptor.pb.h"
19 #include "google/protobuf/io/printer.h"
20 #include "google/protobuf/io/zero_copy_stream.h"
21
22 namespace google {
23 namespace protobuf {
24 namespace compiler {
25 namespace csharp {
26
Generator()27 Generator::Generator() {}
~Generator()28 Generator::~Generator() {}
29
GetSupportedFeatures() const30 uint64_t Generator::GetSupportedFeatures() const {
31 return CodeGenerator::Feature::FEATURE_PROTO3_OPTIONAL |
32 CodeGenerator::Feature::FEATURE_SUPPORTS_EDITIONS;
33 }
34
GenerateFile(const FileDescriptor * file,io::Printer * printer,const Options * options)35 void GenerateFile(const FileDescriptor* file, io::Printer* printer,
36 const Options* options) {
37 ReflectionClassGenerator reflectionClassGenerator(file, options);
38 reflectionClassGenerator.Generate(printer);
39 }
40
Generate(const FileDescriptor * file,const std::string & parameter,GeneratorContext * generator_context,std::string * error) const41 bool Generator::Generate(const FileDescriptor* file,
42 const std::string& parameter,
43 GeneratorContext* generator_context,
44 std::string* error) const {
45 std::vector<std::pair<std::string, std::string> > options;
46 ParseGeneratorParameter(parameter, &options);
47
48 struct Options cli_options;
49
50 for (size_t i = 0; i < options.size(); i++) {
51 if (options[i].first == "file_extension") {
52 cli_options.file_extension = options[i].second;
53 } else if (options[i].first == "base_namespace") {
54 cli_options.base_namespace = options[i].second;
55 cli_options.base_namespace_specified = true;
56 } else if (options[i].first == "internal_access") {
57 cli_options.internal_access = true;
58 } else if (options[i].first == "serializable") {
59 cli_options.serializable = true;
60 } else if (options[i].first == "experimental_strip_nonfunctional_codegen") {
61 cli_options.strip_nonfunctional_codegen = true;
62 } else {
63 *error = absl::StrCat("Unknown generator option: ", options[i].first);
64 return false;
65 }
66 }
67
68 std::string filename_error = "";
69 std::string filename = GetOutputFile(file,
70 cli_options.file_extension,
71 cli_options.base_namespace_specified,
72 cli_options.base_namespace,
73 &filename_error);
74
75 if (filename.empty()) {
76 *error = filename_error;
77 return false;
78 }
79 std::unique_ptr<io::ZeroCopyOutputStream> output(
80 generator_context->Open(filename));
81 io::Printer printer(output.get(), '$');
82
83 GenerateFile(file, &printer, &cli_options);
84
85 return true;
86 }
87
88 } // namespace csharp
89 } // namespace compiler
90 } // namespace protobuf
91 } // namespace google
92