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 <google/protobuf/compiler/java/kotlin_generator.h> |
32 | |
33 | #include <google/protobuf/compiler/code_generator.h> |
34 | #include <google/protobuf/compiler/java/file.h> |
35 | #include <google/protobuf/compiler/java/generator.h> |
36 | #include <google/protobuf/compiler/java/helpers.h> |
37 | #include <google/protobuf/compiler/java/options.h> |
38 | |
39 | namespace google { |
40 | namespace protobuf { |
41 | namespace compiler { |
42 | namespace java { |
43 | |
44 | KotlinGenerator::KotlinGenerator() {} |
45 | KotlinGenerator::~KotlinGenerator() {} |
46 | |
47 | uint64_t KotlinGenerator::GetSupportedFeatures() const { |
48 | return CodeGenerator::Feature::FEATURE_PROTO3_OPTIONAL; |
49 | } |
50 | |
51 | bool KotlinGenerator::Generate(const FileDescriptor* file, |
52 | const std::string& parameter, |
53 | GeneratorContext* context, |
54 | std::string* error) const { |
55 | // ----------------------------------------------------------------- |
56 | // parse generator options |
57 | |
58 | std::vector<std::pair<std::string, std::string> > options; |
59 | ParseGeneratorParameter(parameter, &options); |
60 | Options file_options; |
61 | |
62 | for (auto& option : options) { |
63 | if (option.first == "output_list_file" ) { |
64 | file_options.output_list_file = option.second; |
65 | } else if (option.first == "immutable" ) { |
66 | // Note: the option is considered always set regardless of the input. |
67 | file_options.generate_immutable_code = true; |
68 | } else if (option.first == "mutable" ) { |
69 | *error = "Mutable not supported by Kotlin generator" ; |
70 | return false; |
71 | } else if (option.first == "shared" ) { |
72 | // Note: the option is considered always set regardless of the input. |
73 | file_options.generate_shared_code = true; |
74 | } else if (option.first == "lite" ) { |
75 | file_options.enforce_lite = true; |
76 | } else if (option.first == "annotate_code" ) { |
77 | file_options.annotate_code = true; |
78 | } else if (option.first == "annotation_list_file" ) { |
79 | file_options.annotation_list_file = option.second; |
80 | } else { |
81 | *error = "Unknown generator option: " + option.first; |
82 | return false; |
83 | } |
84 | } |
85 | |
86 | // We only support generation of immutable code so we do it. |
87 | file_options.generate_immutable_code = true; |
88 | file_options.generate_shared_code = true; |
89 | |
90 | std::vector<std::string> all_files; |
91 | std::vector<std::string> all_annotations; |
92 | |
93 | std::unique_ptr<FileGenerator> file_generator( |
94 | new FileGenerator(file, file_options, /* immutable_api = */ true)); |
95 | |
96 | if (!file_generator || !file_generator->Validate(error)) { |
97 | return false; |
98 | } |
99 | |
100 | auto open_file = [context](const std::string& filename) { |
101 | return std::unique_ptr<io::ZeroCopyOutputStream>(context->Open(filename)); |
102 | }; |
103 | std::string package_dir = JavaPackageToDir(package_name: file_generator->java_package()); |
104 | std::string kotlin_filename = package_dir; |
105 | kotlin_filename += file_generator->GetKotlinClassname(); |
106 | kotlin_filename += ".kt" ; |
107 | all_files.push_back(x: kotlin_filename); |
108 | std::string info_full_path = kotlin_filename + ".pb.meta" ; |
109 | if (file_options.annotate_code) { |
110 | all_annotations.push_back(x: info_full_path); |
111 | } |
112 | |
113 | // Generate main kotlin file. |
114 | auto output = open_file(kotlin_filename); |
115 | GeneratedCodeInfo annotations; |
116 | io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector( |
117 | &annotations); |
118 | io::Printer printer( |
119 | output.get(), '$', |
120 | file_options.annotate_code ? &annotation_collector : nullptr); |
121 | |
122 | file_generator->GenerateKotlinSiblings(package_dir, generator_context: context, file_list: &all_files, |
123 | annotation_list: &all_annotations); |
124 | |
125 | if (file_options.annotate_code) { |
126 | auto info_output = open_file(info_full_path); |
127 | annotations.SerializeToZeroCopyStream(output: info_output.get()); |
128 | } |
129 | |
130 | // Generate output list if requested. |
131 | if (!file_options.output_list_file.empty()) { |
132 | // Generate output list. This is just a simple text file placed in a |
133 | // deterministic location which lists the .kt files being generated. |
134 | auto srclist_raw_output = open_file(file_options.output_list_file); |
135 | io::Printer srclist_printer(srclist_raw_output.get(), '$'); |
136 | for (auto& all_file : all_files) { |
137 | srclist_printer.Print(text: "$filename$\n" , args: "filename" , args: all_file); |
138 | } |
139 | } |
140 | |
141 | if (!file_options.annotation_list_file.empty()) { |
142 | // Generate output list. This is just a simple text file placed in a |
143 | // deterministic location which lists the .kt files being generated. |
144 | auto annotation_list_raw_output = |
145 | open_file(file_options.annotation_list_file); |
146 | io::Printer annotation_list_printer(annotation_list_raw_output.get(), '$'); |
147 | for (auto& all_annotation : all_annotations) { |
148 | annotation_list_printer.Print(text: "$filename$\n" , args: "filename" , args: all_annotation); |
149 | } |
150 | } |
151 | |
152 | return true; |
153 | } |
154 | |
155 | } // namespace java |
156 | } // namespace compiler |
157 | } // namespace protobuf |
158 | } // namespace google |
159 | |