| OLD | NEW |
| 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 Loading... |
| 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 // | |
| 33 #ifndef GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__ | 31 #ifndef GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__ |
| 34 #define GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__ | 32 #define GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__ |
| 35 | 33 |
| 36 #include <string> | 34 #include <string> |
| 37 #include <set> | 35 #include <set> |
| 38 | 36 |
| 39 #include <google/protobuf/stubs/logging.h> | |
| 40 #include <google/protobuf/stubs/common.h> | |
| 41 #include <google/protobuf/compiler/code_generator.h> | 37 #include <google/protobuf/compiler/code_generator.h> |
| 42 | 38 |
| 43 namespace google { | 39 namespace google { |
| 44 namespace protobuf { | 40 namespace protobuf { |
| 45 | 41 |
| 46 class Descriptor; | 42 class Descriptor; |
| 47 class EnumDescriptor; | 43 class EnumDescriptor; |
| 48 class FieldDescriptor; | 44 class FieldDescriptor; |
| 49 class OneofDescriptor; | 45 class OneofDescriptor; |
| 50 class FileDescriptor; | 46 class FileDescriptor; |
| 51 | 47 |
| 52 namespace io { class Printer; } | 48 namespace io { class Printer; } |
| 53 | 49 |
| 54 namespace compiler { | 50 namespace compiler { |
| 55 namespace js { | 51 namespace js { |
| 56 | 52 |
| 57 struct GeneratorOptions { | 53 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; |
| 58 // Output path. | 59 // Output path. |
| 59 string output_dir; | 60 string output_dir; |
| 60 // Namespace prefix. | 61 // Namespace prefix. |
| 61 string namespace_prefix; | 62 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; | |
| 112 // Create a library with name <name>_lib.js rather than a separate .js file | 63 // Create a library with name <name>_lib.js rather than a separate .js file |
| 113 // per type? | 64 // per type? |
| 114 string library; | 65 string library; |
| 115 // Error if there are two types that would generate the same output file? | 66 // Error if there are two types that would generate the same output file? |
| 116 bool error_on_name_conflict; | 67 bool error_on_name_conflict; |
| 117 // The extension to use for output file names. | 68 // Enable binary-format support? |
| 118 string extension; | 69 bool binary; |
| 119 // Create a separate output file for each input file? | 70 // What style of imports should be used. |
| 120 bool one_output_file_per_input_file; | 71 enum ImportStyle { |
| 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); |
| 121 }; | 91 }; |
| 122 | 92 |
| 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. | |
| 127 class LIBPROTOC_EXPORT Generator : public CodeGenerator { | 93 class LIBPROTOC_EXPORT Generator : public CodeGenerator { |
| 128 public: | 94 public: |
| 129 Generator() {} | 95 Generator() {} |
| 130 virtual ~Generator() {} | 96 virtual ~Generator() {} |
| 131 | 97 |
| 132 virtual bool Generate(const FileDescriptor* file, | 98 virtual bool Generate(const FileDescriptor* file, |
| 133 const string& parameter, | 99 const string& parameter, |
| 134 GeneratorContext* context, | 100 GeneratorContext* context, |
| 135 string* error) const { | 101 string* error) const { |
| 136 *error = "Unimplemented Generate() method. Call GenerateAll() instead."; | 102 *error = "Unimplemented Generate() method. Call GenerateAll() instead."; |
| 137 return false; | 103 return false; |
| 138 } | 104 } |
| 139 | 105 |
| 140 virtual bool HasGenerateAll() const { return true; } | 106 virtual bool HasGenerateAll() const { return true; } |
| 141 | 107 |
| 142 virtual bool GenerateAll(const std::vector<const FileDescriptor*>& files, | 108 virtual bool GenerateAll(const vector<const FileDescriptor*>& files, |
| 143 const string& parameter, | 109 const string& parameter, |
| 144 GeneratorContext* context, | 110 GeneratorContext* context, |
| 145 string* error) const; | 111 string* error) const; |
| 146 | 112 |
| 147 private: | 113 private: |
| 148 void GenerateHeader(const GeneratorOptions& options, | 114 void GenerateHeader(const GeneratorOptions& options, |
| 149 io::Printer* printer) const; | 115 io::Printer* printer) const; |
| 150 | 116 |
| 151 // Generate goog.provides() calls. | 117 // Generate goog.provides() calls. |
| 152 void FindProvides(const GeneratorOptions& options, | 118 void FindProvides(const GeneratorOptions& options, |
| 153 io::Printer* printer, | 119 io::Printer* printer, |
| 154 const std::vector<const FileDescriptor*>& file, | 120 const vector<const FileDescriptor*>& file, |
| 155 std::set<string>* provided) const; | 121 std::set<string>* provided) const; |
| 156 void FindProvidesForFile(const GeneratorOptions& options, | 122 void FindProvidesForFile(const GeneratorOptions& options, |
| 157 io::Printer* printer, | 123 io::Printer* printer, |
| 158 const FileDescriptor* file, | 124 const FileDescriptor* file, |
| 159 std::set<string>* provided) const; | 125 std::set<string>* provided) const; |
| 160 void FindProvidesForMessage(const GeneratorOptions& options, | 126 void FindProvidesForMessage(const GeneratorOptions& options, |
| 161 io::Printer* printer, | 127 io::Printer* printer, |
| 162 const Descriptor* desc, | 128 const Descriptor* desc, |
| 163 std::set<string>* provided) const; | 129 std::set<string>* provided) const; |
| 164 void FindProvidesForEnum(const GeneratorOptions& options, | 130 void FindProvidesForEnum(const GeneratorOptions& options, |
| 165 io::Printer* printer, | 131 io::Printer* printer, |
| 166 const EnumDescriptor* enumdesc, | 132 const EnumDescriptor* enumdesc, |
| 167 std::set<string>* provided) const; | 133 std::set<string>* provided) const; |
| 168 // For extension fields at file scope. | 134 // For extension fields at file scope. |
| 169 void FindProvidesForFields(const GeneratorOptions& options, | 135 void FindProvidesForFields(const GeneratorOptions& options, |
| 170 io::Printer* printer, | 136 io::Printer* printer, |
| 171 const std::vector<const FieldDescriptor*>& fields, | 137 const vector<const FieldDescriptor*>& fields, |
| 172 std::set<string>* provided) const; | 138 std::set<string>* provided) const; |
| 173 // Print the goog.provides() found by the methods above. | 139 // Print the goog.provides() found by the methods above. |
| 174 void GenerateProvides(const GeneratorOptions& options, | 140 void GenerateProvides(const GeneratorOptions& options, |
| 175 io::Printer* printer, | 141 io::Printer* printer, |
| 176 std::set<string>* provided) const; | 142 std::set<string>* provided) const; |
| 177 | 143 |
| 178 // Generate goog.setTestOnly() if indicated. | 144 // Generate goog.setTestOnly() if indicated. |
| 179 void GenerateTestOnly(const GeneratorOptions& options, | 145 void GenerateTestOnly(const GeneratorOptions& options, |
| 180 io::Printer* printer) const; | 146 io::Printer* printer) const; |
| 181 | 147 |
| 182 // Generate goog.requires() calls. | 148 // Generate goog.requires() calls. |
| 183 void GenerateRequiresForLibrary( | 149 void GenerateRequiresForLibrary(const GeneratorOptions& options, |
| 184 const GeneratorOptions& options, io::Printer* printer, | 150 io::Printer* printer, |
| 185 const std::vector<const FileDescriptor*>& files, | 151 const vector<const FileDescriptor*>& files, |
| 186 std::set<string>* provided) const; | 152 std::set<string>* provided) const; |
| 187 void GenerateRequiresForMessage(const GeneratorOptions& options, | 153 void GenerateRequiresForMessage(const GeneratorOptions& options, |
| 188 io::Printer* printer, | 154 io::Printer* printer, |
| 189 const Descriptor* desc, | 155 const Descriptor* desc, |
| 190 std::set<string>* provided) const; | 156 std::set<string>* provided) const; |
| 191 // For extension fields at file scope. | 157 // For extension fields at file scope. |
| 192 void GenerateRequiresForExtensions( | 158 void GenerateRequiresForExtensions( |
| 193 const GeneratorOptions& options, io::Printer* printer, | 159 const GeneratorOptions& options, io::Printer* printer, |
| 194 const std::vector<const FieldDescriptor*>& fields, | 160 const vector<const FieldDescriptor*>& fields, |
| 195 std::set<string>* provided) const; | 161 std::set<string>* provided) const; |
| 196 void GenerateRequiresImpl(const GeneratorOptions& options, | 162 void GenerateRequiresImpl(const GeneratorOptions& options, |
| 197 io::Printer* printer, std::set<string>* required, | 163 io::Printer* printer, |
| 164 std::set<string>* required, |
| 198 std::set<string>* forwards, | 165 std::set<string>* forwards, |
| 199 std::set<string>* provided, bool require_jspb, | 166 std::set<string>* provided, |
| 200 bool require_extension, bool require_map) const; | 167 bool require_jspb, |
| 168 bool require_extension) const; |
| 201 void FindRequiresForMessage(const GeneratorOptions& options, | 169 void FindRequiresForMessage(const GeneratorOptions& options, |
| 202 const Descriptor* desc, | 170 const Descriptor* desc, |
| 203 std::set<string>* required, | 171 std::set<string>* required, |
| 204 std::set<string>* forwards, | 172 std::set<string>* forwards, |
| 205 bool* have_message) const; | 173 bool* have_message) const; |
| 206 void FindRequiresForField(const GeneratorOptions& options, | 174 void FindRequiresForField(const GeneratorOptions& options, |
| 207 const FieldDescriptor* field, | 175 const FieldDescriptor* field, |
| 208 std::set<string>* required, | 176 std::set<string>* required, |
| 209 std::set<string>* forwards) const; | 177 std::set<string>* forwards) const; |
| 210 void FindRequiresForExtension(const GeneratorOptions& options, | 178 void FindRequiresForExtension(const GeneratorOptions& options, |
| 211 const FieldDescriptor* field, | 179 const FieldDescriptor* field, |
| 212 std::set<string>* required, | 180 std::set<string>* required, |
| 213 std::set<string>* forwards) const; | 181 std::set<string>* forwards) const; |
| 214 | 182 |
| 215 void GenerateFile(const GeneratorOptions& options, | 183 void GenerateFile(const GeneratorOptions& options, |
| 216 io::Printer* printer, | 184 io::Printer* printer, |
| 217 const FileDescriptor* file) const; | 185 const FileDescriptor* file) const; |
| 218 | 186 |
| 219 // Generate definitions for all message classes and enums in all files, | 187 // Generate definitions for all message classes and enums in all files, |
| 220 // processing the files in dependence order. | 188 // processing the files in dependence order. |
| 221 void GenerateFilesInDepOrder( | 189 void GenerateFilesInDepOrder(const GeneratorOptions& options, |
| 222 const GeneratorOptions& options, io::Printer* printer, | 190 io::Printer* printer, |
| 223 const std::vector<const FileDescriptor*>& file) const; | 191 const vector<const FileDescriptor*>& file) const; |
| 224 // Helper for above. | 192 // Helper for above. |
| 225 void GenerateFileAndDeps(const GeneratorOptions& options, | 193 void GenerateFileAndDeps(const GeneratorOptions& options, |
| 226 io::Printer* printer, | 194 io::Printer* printer, |
| 227 const FileDescriptor* root, | 195 const FileDescriptor* root, |
| 228 std::set<const FileDescriptor*>* all_files, | 196 std::set<const FileDescriptor*>* all_files, |
| 229 std::set<const FileDescriptor*>* generated) const; | 197 std::set<const FileDescriptor*>* generated) const; |
| 230 | 198 |
| 231 // Generate definitions for all message classes and enums. | 199 // Generate definitions for all message classes and enums. |
| 232 void GenerateClassesAndEnums(const GeneratorOptions& options, | 200 void GenerateClassesAndEnums(const GeneratorOptions& options, |
| 233 io::Printer* printer, | 201 io::Printer* printer, |
| 234 const FileDescriptor* file) const; | 202 const FileDescriptor* file) const; |
| 235 | 203 |
| 236 void GenerateFieldValueExpression(io::Printer* printer, | |
| 237 const char* obj_reference, | |
| 238 const FieldDescriptor* field, | |
| 239 bool use_default) const; | |
| 240 | |
| 241 // Generate definition for one class. | 204 // Generate definition for one class. |
| 242 void GenerateClass(const GeneratorOptions& options, | 205 void GenerateClass(const GeneratorOptions& options, |
| 243 io::Printer* printer, | 206 io::Printer* printer, |
| 244 const Descriptor* desc) const; | 207 const Descriptor* desc) const; |
| 245 void GenerateClassConstructor(const GeneratorOptions& options, | 208 void GenerateClassConstructor(const GeneratorOptions& options, |
| 246 io::Printer* printer, | 209 io::Printer* printer, |
| 247 const Descriptor* desc) const; | 210 const Descriptor* desc) const; |
| 248 void GenerateClassFieldInfo(const GeneratorOptions& options, | 211 void GenerateClassFieldInfo(const GeneratorOptions& options, |
| 249 io::Printer* printer, | 212 io::Printer* printer, |
| 250 const Descriptor* desc) const; | 213 const Descriptor* desc) const; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 // Generate definition for one enum. | 263 // Generate definition for one enum. |
| 301 void GenerateEnum(const GeneratorOptions& options, | 264 void GenerateEnum(const GeneratorOptions& options, |
| 302 io::Printer* printer, | 265 io::Printer* printer, |
| 303 const EnumDescriptor* enumdesc) const; | 266 const EnumDescriptor* enumdesc) const; |
| 304 | 267 |
| 305 // Generate an extension definition. | 268 // Generate an extension definition. |
| 306 void GenerateExtension(const GeneratorOptions& options, | 269 void GenerateExtension(const GeneratorOptions& options, |
| 307 io::Printer* printer, | 270 io::Printer* printer, |
| 308 const FieldDescriptor* field) const; | 271 const FieldDescriptor* field) const; |
| 309 | 272 |
| 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 | |
| 321 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator); | 273 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator); |
| 322 }; | 274 }; |
| 323 | 275 |
| 324 } // namespace js | 276 } // namespace js |
| 325 } // namespace compiler | 277 } // namespace compiler |
| 326 } // namespace protobuf | 278 } // namespace protobuf |
| 327 | 279 |
| 328 } // namespace google | 280 } // namespace google |
| 329 #endif // GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__ | 281 #endif // GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__ |
| OLD | NEW |