Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: third_party/protobuf/src/google/protobuf/compiler/js/js_generator.h

Issue 2599263002: third_party/protobuf: Update to HEAD (f52e188fe4) (Closed)
Patch Set: Address comments Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 10 matching lines...) Expand all
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 30
31 // Generates JavaScript code for a given .proto file.
32 //
31 #ifndef GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__ 33 #ifndef GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__
32 #define GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__ 34 #define GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__
33 35
34 #include <string> 36 #include <string>
35 #include <set> 37 #include <set>
36 38
39 #include <google/protobuf/stubs/logging.h>
40 #include <google/protobuf/stubs/common.h>
37 #include <google/protobuf/compiler/code_generator.h> 41 #include <google/protobuf/compiler/code_generator.h>
38 42
39 namespace google { 43 namespace google {
40 namespace protobuf { 44 namespace protobuf {
41 45
42 class Descriptor; 46 class Descriptor;
43 class EnumDescriptor; 47 class EnumDescriptor;
44 class FieldDescriptor; 48 class FieldDescriptor;
45 class OneofDescriptor; 49 class OneofDescriptor;
46 class FileDescriptor; 50 class FileDescriptor;
47 51
48 namespace io { class Printer; } 52 namespace io { class Printer; }
49 53
50 namespace compiler { 54 namespace compiler {
51 namespace js { 55 namespace js {
52 56
53 struct GeneratorOptions { 57 struct GeneratorOptions {
54 // Add a `goog.requires()` call for each enum type used. If not set, a forward
55 // declaration with `goog.forwardDeclare` is produced instead.
56 bool add_require_for_enums;
57 // Set this as a test-only module via `goog.setTestOnly();`.
58 bool testonly;
59 // Output path. 58 // Output path.
60 string output_dir; 59 string output_dir;
61 // Namespace prefix. 60 // Namespace prefix.
62 string namespace_prefix; 61 string namespace_prefix;
62 // Enable binary-format support?
63 bool binary;
64 // What style of imports should be used.
65 enum ImportStyle {
66 kImportClosure, // goog.require()
67 kImportCommonJs, // require()
68 kImportBrowser, // no import statements
69 kImportEs6, // import { member } from ''
70 } import_style;
71
72 GeneratorOptions()
73 : output_dir("."),
74 namespace_prefix(""),
75 binary(false),
76 import_style(kImportClosure),
77 add_require_for_enums(false),
78 testonly(false),
79 library(""),
80 error_on_name_conflict(false),
81 extension(".js"),
82 one_output_file_per_input_file(false) {}
83
84 bool ParseFromOptions(
85 const std::vector< std::pair< string, string > >& options,
86 string* error);
87
88 // Returns the file name extension to use for generated code.
89 string GetFileNameExtension() const {
90 return import_style == kImportClosure ? extension : "_pb.js";
91 }
92
93 enum OutputMode {
94 // Create an output file for each input .proto file.
95 kOneOutputFilePerInputFile,
96 // Create an output file for each type.
97 kOneOutputFilePerType,
98 // Put everything in a single file named by the library option.
99 kEverythingInOneFile,
100 };
101
102 // Indicates how to output the generated code based on the provided options.
103 OutputMode output_mode() const;
104
105 // The remaining options are only relevant when we are using kImportClosure.
106
107 // Add a `goog.requires()` call for each enum type used. If not set, a
108 // forward declaration with `goog.forwardDeclare` is produced instead.
109 bool add_require_for_enums;
110 // Set this as a test-only module via `goog.setTestOnly();`.
111 bool testonly;
63 // Create a library with name <name>_lib.js rather than a separate .js file 112 // Create a library with name <name>_lib.js rather than a separate .js file
64 // per type? 113 // per type?
65 string library; 114 string library;
66 // Error if there are two types that would generate the same output file? 115 // Error if there are two types that would generate the same output file?
67 bool error_on_name_conflict; 116 bool error_on_name_conflict;
68 // Enable binary-format support? 117 // The extension to use for output file names.
69 bool binary; 118 string extension;
70 // What style of imports should be used. 119 // Create a separate output file for each input file?
71 enum ImportStyle { 120 bool one_output_file_per_input_file;
72 IMPORT_CLOSURE, // goog.require()
73 IMPORT_COMMONJS, // require()
74 IMPORT_BROWSER, // no import statements
75 IMPORT_ES6, // import { member } from ''
76 } import_style;
77
78 GeneratorOptions()
79 : add_require_for_enums(false),
80 testonly(false),
81 output_dir("."),
82 namespace_prefix(""),
83 library(""),
84 error_on_name_conflict(false),
85 binary(false),
86 import_style(IMPORT_CLOSURE) {}
87
88 bool ParseFromOptions(
89 const vector< pair< string, string > >& options,
90 string* error);
91 }; 121 };
92 122
123 // CodeGenerator implementation which generates a JavaScript source file and
124 // header. If you create your own protocol compiler binary and you want it to
125 // support JavaScript output, you can do so by registering an instance of this
126 // CodeGenerator with the CommandLineInterface in your main() function.
93 class LIBPROTOC_EXPORT Generator : public CodeGenerator { 127 class LIBPROTOC_EXPORT Generator : public CodeGenerator {
94 public: 128 public:
95 Generator() {} 129 Generator() {}
96 virtual ~Generator() {} 130 virtual ~Generator() {}
97 131
98 virtual bool Generate(const FileDescriptor* file, 132 virtual bool Generate(const FileDescriptor* file,
99 const string& parameter, 133 const string& parameter,
100 GeneratorContext* context, 134 GeneratorContext* context,
101 string* error) const { 135 string* error) const {
102 *error = "Unimplemented Generate() method. Call GenerateAll() instead."; 136 *error = "Unimplemented Generate() method. Call GenerateAll() instead.";
103 return false; 137 return false;
104 } 138 }
105 139
106 virtual bool HasGenerateAll() const { return true; } 140 virtual bool HasGenerateAll() const { return true; }
107 141
108 virtual bool GenerateAll(const vector<const FileDescriptor*>& files, 142 virtual bool GenerateAll(const std::vector<const FileDescriptor*>& files,
109 const string& parameter, 143 const string& parameter,
110 GeneratorContext* context, 144 GeneratorContext* context,
111 string* error) const; 145 string* error) const;
112 146
113 private: 147 private:
114 void GenerateHeader(const GeneratorOptions& options, 148 void GenerateHeader(const GeneratorOptions& options,
115 io::Printer* printer) const; 149 io::Printer* printer) const;
116 150
117 // Generate goog.provides() calls. 151 // Generate goog.provides() calls.
118 void FindProvides(const GeneratorOptions& options, 152 void FindProvides(const GeneratorOptions& options,
119 io::Printer* printer, 153 io::Printer* printer,
120 const vector<const FileDescriptor*>& file, 154 const std::vector<const FileDescriptor*>& file,
121 std::set<string>* provided) const; 155 std::set<string>* provided) const;
122 void FindProvidesForFile(const GeneratorOptions& options, 156 void FindProvidesForFile(const GeneratorOptions& options,
123 io::Printer* printer, 157 io::Printer* printer,
124 const FileDescriptor* file, 158 const FileDescriptor* file,
125 std::set<string>* provided) const; 159 std::set<string>* provided) const;
126 void FindProvidesForMessage(const GeneratorOptions& options, 160 void FindProvidesForMessage(const GeneratorOptions& options,
127 io::Printer* printer, 161 io::Printer* printer,
128 const Descriptor* desc, 162 const Descriptor* desc,
129 std::set<string>* provided) const; 163 std::set<string>* provided) const;
130 void FindProvidesForEnum(const GeneratorOptions& options, 164 void FindProvidesForEnum(const GeneratorOptions& options,
131 io::Printer* printer, 165 io::Printer* printer,
132 const EnumDescriptor* enumdesc, 166 const EnumDescriptor* enumdesc,
133 std::set<string>* provided) const; 167 std::set<string>* provided) const;
134 // For extension fields at file scope. 168 // For extension fields at file scope.
135 void FindProvidesForFields(const GeneratorOptions& options, 169 void FindProvidesForFields(const GeneratorOptions& options,
136 io::Printer* printer, 170 io::Printer* printer,
137 const vector<const FieldDescriptor*>& fields, 171 const std::vector<const FieldDescriptor*>& fields,
138 std::set<string>* provided) const; 172 std::set<string>* provided) const;
139 // Print the goog.provides() found by the methods above. 173 // Print the goog.provides() found by the methods above.
140 void GenerateProvides(const GeneratorOptions& options, 174 void GenerateProvides(const GeneratorOptions& options,
141 io::Printer* printer, 175 io::Printer* printer,
142 std::set<string>* provided) const; 176 std::set<string>* provided) const;
143 177
144 // Generate goog.setTestOnly() if indicated. 178 // Generate goog.setTestOnly() if indicated.
145 void GenerateTestOnly(const GeneratorOptions& options, 179 void GenerateTestOnly(const GeneratorOptions& options,
146 io::Printer* printer) const; 180 io::Printer* printer) const;
147 181
148 // Generate goog.requires() calls. 182 // Generate goog.requires() calls.
149 void GenerateRequiresForLibrary(const GeneratorOptions& options, 183 void GenerateRequiresForLibrary(
150 io::Printer* printer, 184 const GeneratorOptions& options, io::Printer* printer,
151 const vector<const FileDescriptor*>& files, 185 const std::vector<const FileDescriptor*>& files,
152 std::set<string>* provided) const; 186 std::set<string>* provided) const;
153 void GenerateRequiresForMessage(const GeneratorOptions& options, 187 void GenerateRequiresForMessage(const GeneratorOptions& options,
154 io::Printer* printer, 188 io::Printer* printer,
155 const Descriptor* desc, 189 const Descriptor* desc,
156 std::set<string>* provided) const; 190 std::set<string>* provided) const;
157 // For extension fields at file scope. 191 // For extension fields at file scope.
158 void GenerateRequiresForExtensions( 192 void GenerateRequiresForExtensions(
159 const GeneratorOptions& options, io::Printer* printer, 193 const GeneratorOptions& options, io::Printer* printer,
160 const vector<const FieldDescriptor*>& fields, 194 const std::vector<const FieldDescriptor*>& fields,
161 std::set<string>* provided) const; 195 std::set<string>* provided) const;
162 void GenerateRequiresImpl(const GeneratorOptions& options, 196 void GenerateRequiresImpl(const GeneratorOptions& options,
163 io::Printer* printer, 197 io::Printer* printer, std::set<string>* required,
164 std::set<string>* required,
165 std::set<string>* forwards, 198 std::set<string>* forwards,
166 std::set<string>* provided, 199 std::set<string>* provided, bool require_jspb,
167 bool require_jspb, 200 bool require_extension, bool require_map) const;
168 bool require_extension) const;
169 void FindRequiresForMessage(const GeneratorOptions& options, 201 void FindRequiresForMessage(const GeneratorOptions& options,
170 const Descriptor* desc, 202 const Descriptor* desc,
171 std::set<string>* required, 203 std::set<string>* required,
172 std::set<string>* forwards, 204 std::set<string>* forwards,
173 bool* have_message) const; 205 bool* have_message) const;
174 void FindRequiresForField(const GeneratorOptions& options, 206 void FindRequiresForField(const GeneratorOptions& options,
175 const FieldDescriptor* field, 207 const FieldDescriptor* field,
176 std::set<string>* required, 208 std::set<string>* required,
177 std::set<string>* forwards) const; 209 std::set<string>* forwards) const;
178 void FindRequiresForExtension(const GeneratorOptions& options, 210 void FindRequiresForExtension(const GeneratorOptions& options,
179 const FieldDescriptor* field, 211 const FieldDescriptor* field,
180 std::set<string>* required, 212 std::set<string>* required,
181 std::set<string>* forwards) const; 213 std::set<string>* forwards) const;
182 214
183 void GenerateFile(const GeneratorOptions& options, 215 void GenerateFile(const GeneratorOptions& options,
184 io::Printer* printer, 216 io::Printer* printer,
185 const FileDescriptor* file) const; 217 const FileDescriptor* file) const;
186 218
187 // Generate definitions for all message classes and enums in all files, 219 // Generate definitions for all message classes and enums in all files,
188 // processing the files in dependence order. 220 // processing the files in dependence order.
189 void GenerateFilesInDepOrder(const GeneratorOptions& options, 221 void GenerateFilesInDepOrder(
190 io::Printer* printer, 222 const GeneratorOptions& options, io::Printer* printer,
191 const vector<const FileDescriptor*>& file) const; 223 const std::vector<const FileDescriptor*>& file) const;
192 // Helper for above. 224 // Helper for above.
193 void GenerateFileAndDeps(const GeneratorOptions& options, 225 void GenerateFileAndDeps(const GeneratorOptions& options,
194 io::Printer* printer, 226 io::Printer* printer,
195 const FileDescriptor* root, 227 const FileDescriptor* root,
196 std::set<const FileDescriptor*>* all_files, 228 std::set<const FileDescriptor*>* all_files,
197 std::set<const FileDescriptor*>* generated) const; 229 std::set<const FileDescriptor*>* generated) const;
198 230
199 // Generate definitions for all message classes and enums. 231 // Generate definitions for all message classes and enums.
200 void GenerateClassesAndEnums(const GeneratorOptions& options, 232 void GenerateClassesAndEnums(const GeneratorOptions& options,
201 io::Printer* printer, 233 io::Printer* printer,
202 const FileDescriptor* file) const; 234 const FileDescriptor* file) const;
203 235
236 void GenerateFieldValueExpression(io::Printer* printer,
237 const char* obj_reference,
238 const FieldDescriptor* field,
239 bool use_default) const;
240
204 // Generate definition for one class. 241 // Generate definition for one class.
205 void GenerateClass(const GeneratorOptions& options, 242 void GenerateClass(const GeneratorOptions& options,
206 io::Printer* printer, 243 io::Printer* printer,
207 const Descriptor* desc) const; 244 const Descriptor* desc) const;
208 void GenerateClassConstructor(const GeneratorOptions& options, 245 void GenerateClassConstructor(const GeneratorOptions& options,
209 io::Printer* printer, 246 io::Printer* printer,
210 const Descriptor* desc) const; 247 const Descriptor* desc) const;
211 void GenerateClassFieldInfo(const GeneratorOptions& options, 248 void GenerateClassFieldInfo(const GeneratorOptions& options,
212 io::Printer* printer, 249 io::Printer* printer,
213 const Descriptor* desc) const; 250 const Descriptor* desc) const;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 // Generate definition for one enum. 300 // Generate definition for one enum.
264 void GenerateEnum(const GeneratorOptions& options, 301 void GenerateEnum(const GeneratorOptions& options,
265 io::Printer* printer, 302 io::Printer* printer,
266 const EnumDescriptor* enumdesc) const; 303 const EnumDescriptor* enumdesc) const;
267 304
268 // Generate an extension definition. 305 // Generate an extension definition.
269 void GenerateExtension(const GeneratorOptions& options, 306 void GenerateExtension(const GeneratorOptions& options,
270 io::Printer* printer, 307 io::Printer* printer,
271 const FieldDescriptor* field) const; 308 const FieldDescriptor* field) const;
272 309
310 // Generate addFoo() method for repeated primitive fields.
311 void GenerateRepeatedPrimitiveHelperMethods(const GeneratorOptions& options,
312 io::Printer* printer,
313 const FieldDescriptor* field,
314 bool untyped) const;
315
316 // Generate addFoo() method for repeated message fields.
317 void GenerateRepeatedMessageHelperMethods(const GeneratorOptions& options,
318 io::Printer* printer,
319 const FieldDescriptor* field) const;
320
273 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator); 321 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator);
274 }; 322 };
275 323
276 } // namespace js 324 } // namespace js
277 } // namespace compiler 325 } // namespace compiler
278 } // namespace protobuf 326 } // namespace protobuf
279 327
280 } // namespace google 328 } // namespace google
281 #endif // GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__ 329 #endif // GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698