• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GenerateFile(const FileDescriptor * file,io::Printer * printer,const Options * options)51 void GenerateFile(const FileDescriptor* file, io::Printer* printer,
52                   const Options* options) {
53   ReflectionClassGenerator reflectionClassGenerator(file, options);
54   reflectionClassGenerator.Generate(printer);
55 }
56 
Generate(const FileDescriptor * file,const string & parameter,GeneratorContext * generator_context,string * error) const57 bool Generator::Generate(
58     const FileDescriptor* file,
59     const string& parameter,
60     GeneratorContext* generator_context,
61     string* error) const {
62 
63   std::vector<std::pair<string, string> > options;
64   ParseGeneratorParameter(parameter, &options);
65 
66   // We only support proto3 - but we make an exception for descriptor.proto.
67   if (file->syntax() != FileDescriptor::SYNTAX_PROTO3 && !IsDescriptorProto(file)) {
68     *error = "C# code generation only supports proto3 syntax";
69     return false;
70   }
71 
72   struct Options cli_options;
73 
74   for (int i = 0; i < options.size(); i++) {
75     if (options[i].first == "file_extension") {
76       cli_options.file_extension = options[i].second;
77     } else if (options[i].first == "base_namespace") {
78       cli_options.base_namespace = options[i].second;
79       cli_options.base_namespace_specified = true;
80     } else if (options[i].first == "internal_access") {
81       cli_options.internal_access = true;
82     } else if (options[i].first == "serializable") {
83       cli_options.serializable = true;
84     } else {
85       *error = "Unknown generator option: " + options[i].first;
86       return false;
87     }
88   }
89 
90   string filename_error = "";
91   std::string filename = GetOutputFile(file,
92       cli_options.file_extension,
93       cli_options.base_namespace_specified,
94       cli_options.base_namespace,
95       &filename_error);
96 
97   if (filename.empty()) {
98     *error = filename_error;
99     return false;
100   }
101   std::unique_ptr<io::ZeroCopyOutputStream> output(
102       generator_context->Open(filename));
103   io::Printer printer(output.get(), '$');
104 
105   GenerateFile(file, &printer, &cli_options);
106 
107   return true;
108 }
109 
110 }  // namespace csharp
111 }  // namespace compiler
112 }  // namespace protobuf
113 }  // namespace google
114